1
1
// Control the Q2 Internet Wi-Fi Radio by command line
2
2
// Q2Radio is free software; you can redistribute it and/or modify it under
3
- // the terms of the GNU General Public License as published by the Free
3
+ // the terms of the GNU General Public License as published by the Free
4
4
// Software Foundation; either version 3, or (at your option) any later
5
5
// version.
6
- //
7
- // Q2Radio is distributed in the hope that it will be useful, but WITHOUT ANY
8
- // WARRANTY; without even the implied warranty of MERCHANTABILITY or
9
- // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10
- // for more details.
11
- //
6
+ //
7
+ // Q2Radio is distributed in the hope that it will be useful, but WITHOUT ANY
8
+ // WARRANTY; without even the implied warranty of MERCHANTABILITY or
9
+ // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10
+ // for more details.
11
+ //
12
12
// You should have received a copy of the GNU General Public License
13
- // along with Q2Radio; see the file COPYING3. If not see
14
- // <http://www.gnu.org/licenses/>.
13
+ // along with Q2Radio; see the file COPYING3. If not see
14
+ // <http://www.gnu.org/licenses/>.
15
15
16
16
// #define DBG(CMD) CMD
17
17
#define DBG (CMD )
18
18
19
+ #include < assert.h>
20
+ #include < getopt.h>
21
+ #include < iomanip> // setfill(), setw()
19
22
#include < iostream>
20
23
#include < libusb-1.0/libusb.h>
21
- #include < stdlib.h>
22
- #include < iomanip> // setfill(), setw()
23
24
#include < stdio.h>
25
+ #include < stdlib.h>
24
26
#include < string.h>
25
- #include < assert.h>
26
27
#include < unistd.h>
27
- #include < getopt.h>
28
28
29
29
int ENDPOINT = 2 ;
30
30
@@ -34,8 +34,9 @@ void printDev(libusb_device *dev) {
34
34
std::cerr << " libusb_get_device_descriptor() failed\n " ;
35
35
return ;
36
36
}
37
- std::cout << " Num of configurations: " << (int ) desc.bNumConfigurations << " \n " ;
38
- std::cout << " Device class: " << (int ) desc.bDeviceClass << " \n " ;
37
+ std::cout << " Num of configurations: " << (int )desc.bNumConfigurations
38
+ << " \n " ;
39
+ std::cout << " Device class: " << (int )desc.bDeviceClass << " \n " ;
39
40
fprintf (stdout, " %04x:%04x\n " , desc.idVendor , desc.idProduct );
40
41
libusb_config_descriptor *config;
41
42
libusb_get_config_descriptor (dev, 0 , &config);
@@ -52,9 +53,11 @@ void printDev(libusb_device *dev) {
52
53
int numOfEndpoints = (int )IFdesc->bNumEndpoints ;
53
54
std::cout << " Num of Endpoints: " << numOfEndpoints << " \n " ;
54
55
for (int EP = 0 ; EP < numOfEndpoints; ++EP) {
55
- const libusb_endpoint_descriptor *EPdesc = &IFdesc->endpoint [EP];
56
- std::cout << " Descriptor Type: " << (int )EPdesc->bDescriptorType << " \n " ;
57
- std::cout << " EP Address: " << (int )EPdesc->bEndpointAddress << " \n " ;
56
+ const libusb_endpoint_descriptor *EPdesc = &IFdesc->endpoint [EP];
57
+ std::cout << " Descriptor Type: " << (int )EPdesc->bDescriptorType
58
+ << " \n " ;
59
+ std::cout << " EP Address: " << (int )EPdesc->bEndpointAddress
60
+ << " \n " ;
58
61
}
59
62
}
60
63
}
@@ -67,59 +70,60 @@ void printAllDevs(libusb_device **devs, ssize_t dev_cnt) {
67
70
}
68
71
}
69
72
70
-
71
- unsigned char end1 = (unsigned char ) ' \r ' ;
72
- unsigned char end2 = (unsigned char ) ' \n ' ;
73
+ unsigned char end1 = (unsigned char )' \r ' ;
74
+ unsigned char end2 = (unsigned char )' \n ' ;
73
75
74
76
void sendStr (libusb_device_handle *handle, const char *dataStr) {
75
77
DBG (std::cout << " sendStr: " << dataStr << " \n " );
76
78
int dataLen = strlen ((const char *)dataStr);
77
- unsigned char *data = (unsigned char *) dataStr;
79
+ unsigned char *data = (unsigned char *)dataStr;
78
80
int actual;
79
- if (! (libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT),
80
- data, dataLen, &actual, 0 ) == 0 ) && actual == dataLen) {
81
+ if (!(libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT), data,
82
+ dataLen, &actual, 0 ) == 0 ) &&
83
+ actual == dataLen) {
81
84
std::cerr << " Error writing data\n " ;
82
85
exit (1 );
83
86
}
84
87
85
- if (! (libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT),
86
- &end1, 1 , &actual, 0 ) == 0 ) && actual == 1 ) {
88
+ if (!(libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT), &end1, 1 ,
89
+ &actual, 0 ) == 0 ) &&
90
+ actual == 1 ) {
87
91
std::cerr << " Error writing 0xd\n " ;
88
92
exit (1 );
89
93
}
90
94
91
- if (! (libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT),
92
- &end2, 1 , &actual, 0 ) == 0 ) && actual == 1 ) {
95
+ if (!(libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_OUT), &end2, 1 ,
96
+ &actual, 0 ) == 0 ) &&
97
+ actual == 1 ) {
93
98
std::cerr << " Error writing 0xa\n " ;
94
99
exit (1 );
95
100
}
96
-
97
101
}
98
102
99
-
100
103
bool getStr (libusb_device_handle *handle, char *buf, size_t size) {
101
104
int i = 0 ;
102
105
103
106
while (1 ) {
104
- #define TMPSIZE 400
107
+ #define TMPSIZE 400
105
108
unsigned char tmpBuf[TMPSIZE];
106
109
107
110
int actual;
108
111
int succ = libusb_bulk_transfer (handle, (ENDPOINT | LIBUSB_ENDPOINT_IN),
109
- tmpBuf, TMPSIZE, &actual, 100 );
112
+ tmpBuf, TMPSIZE, &actual, 100 );
110
113
if (succ != 0 ) {
111
114
// std::cerr << "libusb_bulk_transfer(IN) failed!\n";
112
115
return 0 ;
113
116
}
114
117
if (actual > TMPSIZE) {
115
- // std::cerr << "ERROR: actual (=" << actual << ") > TMPSIZE (=" << TMPSIZE << ")\n";
118
+ // std::cerr << "ERROR: actual (=" << actual << ") > TMPSIZE (=" <<
119
+ // TMPSIZE << ")\n";
116
120
return 0 ;
117
121
}
118
122
119
123
memcpy (buf + i, tmpBuf, actual);
120
- i+= actual;
124
+ i += actual;
121
125
122
- if (i >= 3 && buf[i- 2 ] == ' \r ' && buf[i- 1 ] == ' \n ' ) {
126
+ if (i >= 3 && buf[i - 2 ] == ' \r ' && buf[i - 1 ] == ' \n ' ) {
123
127
break ;
124
128
}
125
129
}
@@ -129,14 +133,12 @@ bool getStr(libusb_device_handle *handle, char *buf, size_t size) {
129
133
130
134
void printStrV (const char *buf) {
131
135
for (int i = 0 ; i < strlen (buf); ++i) {
132
- fprintf (stderr, " %c(=%02x)\n " , (buf[i] >= 32 && buf[i] <= 126 ) ? buf[i] : ' ' , buf[i]);
136
+ fprintf (stderr, " %c(=%02x)\n " ,
137
+ (buf[i] >= 32 && buf[i] <= 126 ) ? buf[i] : ' ' , buf[i]);
133
138
}
134
139
}
135
140
136
- void printStr (const char *buf) {
137
- fprintf (stderr, " %s" , buf);
138
- }
139
-
141
+ void printStr (const char *buf) { fprintf (stderr, " %s" , buf); }
140
142
141
143
struct Args {
142
144
Args (void ) {
@@ -154,17 +156,17 @@ struct Args {
154
156
155
157
void check (void ) {
156
158
if (side != -1 ) {
157
- if (! stationName) {
158
- std::cerr << " ERROR: Name empty!\n " ;
159
- exit (1 );
159
+ if (!stationName) {
160
+ std::cerr << " ERROR: Name empty!\n " ;
161
+ exit (1 );
160
162
}
161
- if (! stationUrl) {
162
- std::cerr << " ERROR: URL empty!\n " ;
163
- exit (1 );
163
+ if (!stationUrl) {
164
+ std::cerr << " ERROR: URL empty!\n " ;
165
+ exit (1 );
164
166
}
165
167
}
166
168
}
167
-
169
+
168
170
void dump (void ) {
169
171
std::cout << " --------------\n " ;
170
172
std::cout << " Side: " << side << " \n " ;
@@ -187,21 +189,17 @@ struct Args {
187
189
std::cout << " \n " ;
188
190
std::cout << " --------------\n " ;
189
191
}
190
- bool isEmpty (void ) {
191
- return side == -1 ;
192
- }
192
+ bool isEmpty (void ) { return side == -1 ; }
193
193
};
194
194
195
-
196
195
static struct option long_options[] = {
197
- {" side" , required_argument, 0 , ' s' },
198
- {" name" , required_argument, 0 , ' n' },
199
- {" url" , required_argument, 0 , ' u' },
200
- {" list" , no_argument, 0 , ' l' },
201
- {" custom" , required_argument, 0 , ' c' },
202
- // WARNING: Keep "help" last for automatically generated usage() message
203
- {" help" , no_argument, 0 , ' h' }
204
- };
196
+ {" side" , required_argument, 0 , ' s' },
197
+ {" name" , required_argument, 0 , ' n' },
198
+ {" url" , required_argument, 0 , ' u' },
199
+ {" list" , no_argument, 0 , ' l' },
200
+ {" custom" , required_argument, 0 , ' c' },
201
+ // WARNING: Keep "help" last for automatically generated usage() message
202
+ {" help" , no_argument, 0 , ' h' }};
205
203
206
204
void usage (char **argv) {
207
205
std::cerr << " Available options:\n " ;
@@ -227,7 +225,8 @@ void usage(char **argv) {
227
225
}
228
226
++i;
229
227
}
230
- std::cerr << " Example: --side 0 --name \" Rebel State Radio\" --url \" http://eco.onestreaming.com:8142\"\n " ;
228
+ std::cerr << " Example: --side 0 --name \" Rebel State Radio\" --url "
229
+ " \" http://eco.onestreaming.com:8142\"\n " ;
231
230
std::cerr << " Example: --custom \" gpre 0 name\" :\n " ;
232
231
}
233
232
@@ -244,8 +243,8 @@ void parseArgs(int argc, char **argv, Args *args) {
244
243
case ' s' :
245
244
args->side = atoi (optarg );
246
245
if (!(args->side >= 0 && args->side < 4 )) {
247
- std::cerr << " Bad side: " << args->side << " . Should be 0-4\n " ;
248
- exit (1 );
246
+ std::cerr << " Bad side: " << args->side << " . Should be 0-4\n " ;
247
+ exit (1 );
249
248
}
250
249
break ;
251
250
case ' n' :
@@ -277,16 +276,15 @@ void parseArgs(int argc, char **argv, Args *args) {
277
276
}
278
277
}
279
278
280
-
281
279
#define SIZE 400
282
280
283
281
void printResponse (libusb_device_handle *handle, bool out = true ) {
284
282
char buf[SIZE];
285
283
286
- while (1 ) {
284
+ while (1 ) {
287
285
memset (buf, 0 , SIZE);
288
- bool ok = getStr (handle, buf, SIZE- 1 );
289
- if (! ok) {
286
+ bool ok = getStr (handle, buf, SIZE - 1 );
287
+ if (!ok) {
290
288
break ;
291
289
}
292
290
if (out) {
@@ -308,7 +306,7 @@ int main(int argc, char **argv) {
308
306
args.check ();
309
307
// args.dump();
310
308
311
- libusb_device **devs; // list of devices
309
+ libusb_device **devs; // list of devices
312
310
libusb_context *ctx = NULL ;
313
311
int r;
314
312
if ((r = libusb_init (&ctx)) < 0 ) {
@@ -328,7 +326,8 @@ int main(int argc, char **argv) {
328
326
int VID = 0x1f2e ;
329
327
int PID = 0x000a ;
330
328
if ((handle = libusb_open_device_with_vid_pid (ctx, VID, PID)) == NULL ) {
331
- DBG (fprintf (stderr, " libusb_open_device_with_vid_pid(%04x:%04x)\n " , VID, PID));
329
+ DBG (fprintf (stderr, " libusb_open_device_with_vid_pid(%04x:%04x)\n " , VID,
330
+ PID));
332
331
exit (1 );
333
332
}
334
333
// libusb_free_device_list(devs, 1);
@@ -351,7 +350,7 @@ int main(int argc, char **argv) {
351
350
printResponse (handle);
352
351
return 0 ;
353
352
}
354
-
353
+
355
354
// Query radio for all sides and print them
356
355
if (args.list || args.side == -1 ) {
357
356
printResponse (handle, false ); // skip any outstanding output
@@ -370,7 +369,7 @@ int main(int argc, char **argv) {
370
369
}
371
370
372
371
if (args.side != -1 ) {
373
- #define BUFSIZE 400
372
+ #define BUFSIZE 400
374
373
char buf[BUFSIZE];
375
374
snprintf (buf, BUFSIZE, " spre %d name \" %s\" " , args.side , args.stationName );
376
375
sendStr (handle, buf);
@@ -381,7 +380,6 @@ int main(int argc, char **argv) {
381
380
printResponse (handle);
382
381
return 0 ;
383
382
}
384
-
385
383
386
384
if (libusb_release_interface (handle, 0 ) != 0 ) {
387
385
std::cerr << " libusb_release_interface() failed!\n " ;
0 commit comments