xtalk_send.c
5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Written by Oron Peled <[email protected]>
* Copyright (C) 2008-2011, Xorcom
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>
#include <xtalk/debug.h>
#include <xtalk/xusb.h>
#include <xtalk/proto_sync.h>
#include <autoconfig.h>
#define DBG_MASK 0x80
static char *progname;
static int timeout = 500; /* msec */
static int iface_num = 1;
static void usage()
{
fprintf(stderr, "Usage: %s [options...] -D <busnum>/<devnum> [hexnum ....]\n",
progname);
fprintf(stderr, "\tOptions:\n");
fprintf(stderr,
"\t\t[-I<iface_num>] # Interface number (default %d)\n",
iface_num);
fprintf(stderr,
"\t\t[-t<timeout>] # Timeout (default %d)\n",
timeout);
fprintf(stderr, "\t\t[-Q] # Query protocol version\n");
fprintf(stderr, "\t\t[-v] # Increase verbosity\n");
fprintf(stderr,
"\t\t[-d mask] # Debug mask (0xFF for everything)\n");
fprintf(stderr, "\t\t[-h] # This help\n");
exit(1);
}
/***** XTALK Interface *****************************************************/
static const struct xtalk_protocol xtalk_protocol = {
.name = "XTALK-DERIVED",
.proto_version = 0,
.commands = {
},
.ack_statuses = {
}
};
/***** XUSB Interface ******************************************************/
static int sendto_device(struct xusb_iface *iface, int nargs, char *args[])
{
char *reply = NULL;
char *buf = NULL;
int ret = 0;
int i;
assert(nargs >= 0);
if (!nargs)
goto out;
buf = malloc(nargs);
if (!buf) {
ERR("Out of memory for %d command bytes\n", nargs);
ret = -ENOMEM;
goto out;
}
for (i = 0; i < nargs; i++) {
int val = strtoul(args[i], NULL, 16);
printf("%d> 0x%02X\n", i, val);
buf[i] = val;
}
ret = xusb_send(iface, buf, nargs, timeout);
if (ret < 0) {
ERR("xusb_send failed ret=%d\n", ret);
goto out;
}
reply = malloc(PACKET_SIZE);
if (!reply) {
ERR("Out of memory\n");
ret = -ENOMEM;
goto out;
}
ret = xusb_recv(iface, reply, PACKET_SIZE, timeout);
if (ret < 0) {
ERR("Receive from usb failed.\n");
goto out;
}
dump_packet(LOG_INFO, 0, "REPLY", reply, ret);
ret = 0;
out:
if (reply)
free(reply);
if (buf)
free(buf);
return ret;
}
static int show_protocol(struct xtalk_sync *xtalk_sync, struct xusb_iface *iface)
{
int ret;
ret = xtalk_sync_set_protocol(xtalk_sync, &xtalk_protocol);
if (ret < 0) {
ERR("%s Protocol registration failed: %s\n",
xtalk_protocol.name, strerror(-ret));
return ret;
}
ret = xtalk_proto_query(xtalk_sync);
if (ret < 0) {
ERR("Protocol query error: %s\n", strerror(-ret));
return ret;
}
INFO("usb:%s: Protocol version 0x%X\n", xusb_devpath(xusb_deviceof(iface)), ret);
return 0;
}
int main(int argc, char *argv[])
{
char *devpath = NULL;
struct xusb_device *xusb_device;
struct xtalk_base *xtalk_base;
struct xtalk_sync *xtalk_sync;
struct xusb_iface *iface;
const char options[] = "vd:D:t:I:i:o:Q";
int query = 0;
int ret;
progname = argv[0];
while (1) {
int c;
c = getopt(argc, argv, options);
if (c == -1)
break;
switch (c) {
case 'D':
devpath = optarg;
break;
case 'I':
iface_num = strtoul(optarg, NULL, 0);
break;
case 't':
timeout = strtoul(optarg, NULL, 0);
break;
case 'Q':
query++;
break;
case 'v':
verbose++;
break;
case 'd':
debug_mask = strtoul(optarg, NULL, 0);
break;
case 'h':
default:
ERR("Unknown option '%c'\n", c);
usage();
}
}
if (!devpath) {
ERR("Missing device path\n");
usage();
}
xusb_device = xusb_find_bypath(devpath);
if (!xusb_device) {
ERR("No XUSB device found\n");
return 1;
}
ret = xusb_claim(xusb_device, iface_num, &iface);
if (ret < 0) {
ERR("Claiming interface #%d failed (ret = %d)\n", iface_num, ret);
return 1;
}
xusb_showinfo(xusb_deviceof(iface));
xtalk_base = xtalk_base_new_on_xusb(iface);
if (!xtalk_base) {
ERR("Failed creating the xtalk device abstraction\n");
return 1;
}
xtalk_sync = xtalk_sync_new(xtalk_base);
if (!xtalk_sync) {
ERR("Failed creating the xtalk device abstraction\n");
return 1;
}
if (query) {
ret = show_protocol(xtalk_sync, iface);
if (ret < 0)
return 1;
}
ret = sendto_device(iface, argc - optind, argv + optind);
if (ret < 0)
ERR("Command failed: %d\n", ret);
xtalk_sync_delete(xtalk_sync);
xtalk_base_delete(xtalk_base);
xusb_destroy(xusb_deviceof(iface));
return 0;
}