-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl_reset.c
328 lines (278 loc) · 7.73 KB
/
rtl_reset.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* rtl_reset - based on rtl_test, test and benchmark tool
*
* Copyright (C) 2012-2014 by Steve Markgraf <[email protected]>
* Copyright (C) 2012-2014 by Kyle Keen <[email protected]>
* Copyright (C) 2014 by Michael Tatarinov <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef __APPLE__
#include <sys/time.h>
#else
#include <time.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>
#include "getopt/getopt.h"
#endif
#include <libusb-1.0/libusb.h>
#include "knowndevices.h"
void usage( void ) {
fprintf( stderr,
"rtl_reset, a tool for resetting RTL2832 based DVB-T receivers\n"
"Usage:\n"
"\t[-d device_index (default: 0)]\n" );
exit( 1 );
}
static rtlsdr_dongle_t *find_known_device( uint16_t vid, uint16_t pid ) {
unsigned int i;
rtlsdr_dongle_t *device = NULL;
for ( i = 0; i < sizeof( known_devices ) / sizeof( rtlsdr_dongle_t ); i++ ) {
if ( known_devices[i].vid == vid && known_devices[i].pid == pid ) {
device = &known_devices[i];
break;
}
}
return device;
}
uint32_t rtlsdr_get_device_count( void ) {
int i;
libusb_context *ctx;
libusb_device **list;
uint32_t device_count = 0;
struct libusb_device_descriptor dd;
ssize_t cnt;
libusb_init( &ctx );
cnt = libusb_get_device_list( ctx, &list );
for ( i = 0; i < cnt; i++ ) {
libusb_get_device_descriptor( list[i], &dd );
if ( find_known_device( dd.idVendor, dd.idProduct ) ) {
device_count++;
}
}
libusb_free_device_list( list, 1 );
libusb_exit( ctx );
return device_count;
}
const char *rtlsdr_get_device_name( uint32_t index ) {
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
rtlsdr_dongle_t *device = NULL;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init( &ctx );
cnt = libusb_get_device_list( ctx, &list );
for ( i = 0; i < cnt; i++ ) {
libusb_get_device_descriptor( list[i], &dd );
device = find_known_device( dd.idVendor, dd.idProduct );
if ( device ) {
device_count++;
if ( index == device_count - 1 ) {
break;
}
}
}
libusb_free_device_list( list, 1 );
libusb_exit( ctx );
if ( device ) {
return device->name;
} else {
return "";
}
}
int verbose_device_search( char *s ) {
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256];
device_count = rtlsdr_get_device_count();
if ( !device_count ) {
fprintf( stderr, "No supported devices found.\n" );
return -1;
}
fprintf( stderr, "Found %d device(s):\n", device_count );
for ( i = 0; i < device_count; i++ ) {
rtlsdr_get_device_usb_strings( (uint32_t)i, vendor, product, serial );
fprintf( stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial );
}
fprintf( stderr, "\n" );
/* does string look like raw id number */
device = (int)strtol( s, &s2, 0 );
if ( s2[0] == '\0' && device >= 0 && device < device_count ) {
fprintf( stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name( (uint32_t)device ) );
return device;
}
/* does string exact match a serial */
for ( i = 0; i < device_count; i++ ) {
rtlsdr_get_device_usb_strings( (uint32_t)i, vendor, product, serial );
if ( strcmp( s, serial ) != 0 ) {
continue;
}
device = i;
fprintf( stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name( (uint32_t)device ) );
return device;
}
/* does string prefix match a serial */
for ( i = 0; i < device_count; i++ ) {
rtlsdr_get_device_usb_strings( (uint32_t)i, vendor, product, serial );
if ( strncmp( s, serial, strlen( s ) ) != 0 ) {
continue;
}
device = i;
fprintf( stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name( (uint32_t)device ) );
return device;
}
/* does string suffix match a serial */
for ( i = 0; i < device_count; i++ ) {
rtlsdr_get_device_usb_strings( (uint32_t)i, vendor, product, serial );
offset = strlen( serial ) - strlen( s );
if ( offset < 0 ) {
continue;
}
if ( strncmp( s, serial + offset, strlen( s ) ) != 0 ) {
continue;
}
device = i;
fprintf( stderr, "Resetting device %d: %s\n",
device, rtlsdr_get_device_name( (uint32_t)device ) );
return device;
}
fprintf( stderr, "No matching devices found.\n" );
return -1;
}
int libusb_get_usb_strings( libusb_device_handle *devh, char *manufact, char *product,
char *serial ) {
struct libusb_device_descriptor dd;
libusb_device *device = NULL;
const int buf_max = 256;
int r = 0;
device = libusb_get_device( devh );
r = libusb_get_device_descriptor( device, &dd );
if ( r < 0 ) {
return -1;
}
if ( manufact ) {
memset( manufact, 0, buf_max );
libusb_get_string_descriptor_ascii( devh, dd.iManufacturer, (unsigned char *)manufact, buf_max );
}
if ( product ) {
memset( product, 0, buf_max );
libusb_get_string_descriptor_ascii( devh, dd.iProduct, (unsigned char *)product, buf_max );
}
if ( serial ) {
memset( serial, 0, buf_max );
libusb_get_string_descriptor_ascii( devh, dd.iSerialNumber, (unsigned char *)serial, buf_max );
}
return 0;
}
int rtlsdr_get_device_usb_strings( uint32_t index, char *manufact, char *product, char *serial ) {
int r = -2;
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
rtlsdr_dongle_t *device = NULL;
struct libusb_device_handle *devh;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init( &ctx );
cnt = libusb_get_device_list( ctx, &list );
for ( i = 0; i < cnt; i++ ) {
libusb_get_device_descriptor( list[i], &dd );
device = find_known_device( dd.idVendor, dd.idProduct );
if ( device ) {
device_count++;
if ( index == device_count - 1 ) {
r = libusb_open( list[i], &devh );
if ( !r ) {
r = libusb_get_usb_strings( devh, manufact, product, serial );
libusb_close( devh );
}
break;
}
}
}
libusb_free_device_list( list, 1 );
libusb_exit( ctx );
return r;
}
int rtl_device_reset( uint32_t index ) {
int r = -2;
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
rtlsdr_dongle_t *device = NULL;
struct libusb_device_handle *devh;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init( &ctx );
cnt = libusb_get_device_list( ctx, &list );
for ( i = 0; i < cnt; i++ ) {
libusb_get_device_descriptor( list[i], &dd );
device = find_known_device( dd.idVendor, dd.idProduct );
if ( device ) {
device_count++;
if ( index == device_count - 1 ) {
r = libusb_open( list[i], &devh );
if ( !r ) {
r = libusb_reset_device( devh );
libusb_close( devh );
}
break;
}
}
}
libusb_free_device_list( list, 1 );
libusb_exit( ctx );
return r;
}
int main( int argc, char **argv ) {
int opt = -1;
int devGiven = 0;
int devIndex = 0;
while ( ( opt = getopt( argc, argv, "d:h" ) ) != -1 ) {
switch ( opt ) {
case 'd':
devIndex = verbose_device_search( optarg );
devGiven = 1;
break;
case 'h':
default:
usage();
break;
}
}
if ( !devGiven ) {
devIndex = verbose_device_search( "0" );
}
if ( devIndex < 0 ) {
return -ENODEV;
}
return rtl_device_reset( devIndex );
}