-
Notifications
You must be signed in to change notification settings - Fork 3
/
WinUSBDevice.cpp
718 lines (646 loc) · 24.5 KB
/
WinUSBDevice.cpp
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Icom ICR8600 SoapySDR Library
*
* Made in 2018 by D.Eliuseev [email protected]
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "WinUSBDevice.h"
#ifdef _WIN32
#pragma comment(lib, "winusb.lib")
#pragma comment(lib, "Cfgmgr32.lib")
#else
void Sleep(int milliseconds)
{
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
}
#endif
#define decToBcd(val) (UCHAR)((((val) / 10 * 16) + ((val) % 10)))
#define bcdToDec(val) (ULONG)((val>>4)*10 + (val & 0x0f))
HRESULT RetrieveDevicePath(_Out_bytecap_(BufLen) LPTSTR DevicePath, _In_ ULONG BufLen, _Out_opt_ PBOOL FailureDeviceNotFound);
BOOL FindICR8600Device()
{
SoapySDR_logf(SOAPY_SDR_TRACE, "FindICR8600Device");
#ifdef _WIN32
DEVICE_DATA deviceData;
BOOL notFound = false;
HRESULT hr = RetrieveDevicePath(deviceData.DevicePath, sizeof(deviceData.DevicePath), ¬Found);
if (FAILED(hr) || notFound) {
return FALSE;
}
return TRUE;
#else
int r = libusb_init(NULL);
if (r < 0)
return FALSE;
BOOL res = FALSE;
libusb_device **devs;
ssize_t cnt = libusb_get_device_list(NULL, &devs);
for (int i = 0; i < cnt; i++) {
libusb_device *device = devs[i];
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(device, &desc);
if (r < 0) continue;
printf("Device %04x:%04x, Manufacturer: %d\n", desc.idVendor, desc.idProduct, desc.iManufacturer);
// libusb_device_handle *handle;
// int err = libusb_open(device, &handle);
// if (err) {
// printf(" Error: cannot open device\n");
// continue;
// }
// // Get description
// const int STRING_LENGTH = 255;
// unsigned char stringDescription[STRING_LENGTH];
// if (desc.iManufacturer > 0) {
// r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, stringDescription, STRING_LENGTH);
// if (r >= 0)
// printf("Manufacturer = %s\n", stringDescription);
// }
// if (desc.iProduct > 0) {
// r = libusb_get_string_descriptor_ascii(handle, desc.iProduct, stringDescription, STRING_LENGTH);
// if ( r >= 0 )
// printf("Product = %s\n", stringDescription);
// }
// if (desc.iSerialNumber > 0) {
// r = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, stringDescription, STRING_LENGTH);
// if (r >= 0)
// printf("SerialNumber = %s\n", stringDescription);
// }
// printf("\n");
// libusb_close(handle);
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return res;
#endif
}
HRESULT OpenDevice(_Out_ PDEVICE_DATA DeviceData, _Out_opt_ PBOOL FailureDeviceNotFound)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "OpenDevice");
HRESULT hr = S_OK;
BOOL bResult;
DeviceData->HandlesOpen = FALSE;
hr = RetrieveDevicePath(DeviceData->DevicePath, sizeof(DeviceData->DevicePath), FailureDeviceNotFound);
if (FAILED(hr)) {
return hr;
}
DeviceData->DeviceHandle = CreateFile(DeviceData->DevicePath,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (INVALID_HANDLE_VALUE == DeviceData->DeviceHandle) {
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
bResult = WinUsb_Initialize(DeviceData->DeviceHandle, &DeviceData->WinusbHandle);
if (FALSE == bResult) {
hr = HRESULT_FROM_WIN32(GetLastError());
CloseHandle(DeviceData->DeviceHandle);
return hr;
}
DeviceData->HandlesOpen = TRUE;
return hr;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "OpenDevice: Only WIN32 Supported");
return 0;
#endif
}
BOOL GetDeviceDescriptor(WINUSB_INTERFACE_HANDLE hDeviceHandle, _Out_ USB_DEVICE_DESCRIPTOR *pDeviceDesc)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "GetDeviceDescriptor");
ULONG lengthReceived;
BOOL bResult = WinUsb_GetDescriptor(hDeviceHandle, USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, (PBYTE)pDeviceDesc, sizeof(USB_DEVICE_DESCRIPTOR), &lengthReceived);
if (FALSE == bResult || lengthReceived != sizeof(USB_DEVICE_DESCRIPTOR)) {
SoapySDR_logf(SOAPY_SDR_ERROR, "GetDeviceDescriptor: Error among LastError %d or lengthReceived %d", FALSE == bResult ? GetLastError() : 0, lengthReceived);
return FALSE;
}
return TRUE;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "GetDeviceDescriptor: Only WIN32 Supported");
return FALSE;
#endif
}
VOID CloseDevice(_Inout_ PDEVICE_DATA DeviceData)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "CloseDevice");
if (FALSE == DeviceData->HandlesOpen) {
//
// Called on an uninitialized DeviceData
//
return;
}
WinUsb_Free(DeviceData->WinusbHandle);
CloseHandle(DeviceData->DeviceHandle);
DeviceData->HandlesOpen = FALSE;
DeviceData->DeviceHandle = INVALID_HANDLE_VALUE;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "CloseDevice: Only WIN32 Supported");
return;
#endif
}
HRESULT RetrieveDevicePath(_Out_bytecap_(BufLen) LPTSTR DevicePath, _In_ ULONG BufLen, _Out_opt_ PBOOL FailureDeviceNotFound)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "RetrieveDevicePath");
CONFIGRET cr = CR_SUCCESS;
HRESULT hr = S_OK;
PTSTR DeviceInterfaceList = NULL;
ULONG DeviceInterfaceListLength = 0;
if (NULL != FailureDeviceNotFound) {
*FailureDeviceNotFound = FALSE;
}
//
// Enumerate all devices exposing the interface. Do this in a loop
// in case a new interface is discovered while this code is executing,
// causing CM_Get_Device_Interface_List to return CR_BUFFER_SMALL.
//
do {
cr = CM_Get_Device_Interface_List_Size(&DeviceInterfaceListLength,
(LPGUID)&GUID_DEVINTERFACE_WinUSB1,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
hr = HRESULT_FROM_WIN32(CM_MapCrToWin32Err(cr, ERROR_INVALID_DATA));
break;
}
DeviceInterfaceList = (PTSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
DeviceInterfaceListLength * sizeof(TCHAR));
if (DeviceInterfaceList == NULL) {
hr = E_OUTOFMEMORY;
break;
}
cr = CM_Get_Device_Interface_List((LPGUID)&GUID_DEVINTERFACE_WinUSB1,
NULL,
DeviceInterfaceList,
DeviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
if (cr != CR_BUFFER_SMALL) {
hr = HRESULT_FROM_WIN32(CM_MapCrToWin32Err(cr, ERROR_INVALID_DATA));
}
}
} while (cr == CR_BUFFER_SMALL);
if (FAILED(hr)) {
return hr;
}
//
// If the interface list is empty, no devices were found.
//
if (*DeviceInterfaceList == TEXT('\0')) {
if (NULL != FailureDeviceNotFound) {
*FailureDeviceNotFound = TRUE;
}
hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
return hr;
}
//
// Give path of the first found device interface instance to the caller. CM_Get_Device_Interface_List ensured
// the instance is NULL-terminated.
//
hr = StringCbCopy(DevicePath,
BufLen,
DeviceInterfaceList);
HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
return hr;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "RetrieveDevicePath: Only WIN32 Supported");
return 0;
#endif
}
BOOL WriteToBulkEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR ID, ULONG* pcbWritten, PUCHAR send, ULONG cbSize)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "WriteToBulkEndpoint");
if (hDeviceHandle == INVALID_HANDLE_VALUE || !pcbWritten) {
SoapySDR_logf(SOAPY_SDR_FATAL, "WriteToBulkEndpoint: Invalid Handle");
return FALSE;
}
BOOL bResult = TRUE;
ULONG cbSent = 0;
bResult = WinUsb_WritePipe(hDeviceHandle, ID, send, cbSize, &cbSent, 0);
if (bResult) {
SoapySDR_logf(SOAPY_SDR_TRACE, "WriteToBulkEndpoint: 0x%x: %d bytes, actual data transferred: %d", ID, cbSize, cbSent);
*pcbWritten = cbSent;
}
else {
SoapySDR_logf(SOAPY_SDR_ERROR, "WriteToBulkEndpoint: WinUsb_WritePipe Failed");
}
return bResult;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "WriteToBulkEndpoint: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ReadFromBulkEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR ID, ULONG cbSize)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadFromBulkEndpoint");
if (hDeviceHandle == INVALID_HANDLE_VALUE) {
SoapySDR_logf(SOAPY_SDR_FATAL, "ReadFromBulkEndpoint: Invalid Handle");
return FALSE;
}
BOOL bResult = TRUE;
UCHAR* szBuffer = (UCHAR*)LocalAlloc(LPTR, sizeof(UCHAR)*cbSize);
ULONG cbRead = 0;
bResult = WinUsb_ReadPipe(hDeviceHandle, ID, szBuffer, cbSize, &cbRead, 0);
if (bResult) {
if (cbRead == 6) {
// FE FE E0 96 FB FD - OK
if (szBuffer[3] == 0x96 && szBuffer[4] == 0xFB)
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadFromBulkEndpoint: OK");
// FE FE E0 96 FA FD - Fail
else if (szBuffer[3] == 0x96 && szBuffer[4] == 0xFA)
SoapySDR_logf(SOAPY_SDR_ERROR, "ReadFromBulkEndpoint: Fail");
else
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadFromBulkEndpoint: output %Xh %Xh %Xh %Xh %Xh %Xh", szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5]);
}
else {
// printf("ReadFromBulkEndpoint: pipe 0x%x: size %d, actual data read: %d.\n", ID, cbSize, cbRead);
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadFromBulkEndpoint output (%d): %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh", cbRead, szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5], szBuffer[6], szBuffer[7],
szBuffer[8], szBuffer[9], szBuffer[10], szBuffer[11], szBuffer[12], szBuffer[13], szBuffer[14], szBuffer[15]);
}
}
else {
SoapySDR_logf(SOAPY_SDR_ERROR, "ReadFromBulkEndpoint: WinUsb_ReadPipe Failed");
}
LocalFree(szBuffer);
return bResult;
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "ReadFromBulkEndpoint: Only WIN32 Supported");
return FALSE;
#endif
}
ULONG ReadBufferFromBulkEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR ID, PUCHAR szBuffer, ULONG cbSize)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadBufferFromBulkEndpoint");
if (hDeviceHandle == INVALID_HANDLE_VALUE) {
SoapySDR_logf(SOAPY_SDR_FATAL, "ReadBufferFromBulkEndpoint: Invalid Handle");
return FALSE;
}
BOOL bResult = TRUE;
ULONG cbRead = 0;
bResult = WinUsb_ReadPipe(hDeviceHandle, ID, szBuffer, cbSize, &cbRead, 0);
if (bResult) {
SoapySDR_logf(SOAPY_SDR_TRACE, "ReadBufferFromBulkEndpoint: Read %d", cbRead);
return cbRead;
}
else {
SoapySDR_logf(SOAPY_SDR_ERROR, "ReadBufferFromBulkEndpoint: WinUsb_ReadPipe Failed");
return 0;
}
#else
SoapySDR_logf(SOAPY_SDR_FATAL, "ReadBufferFromBulkEndpoint: Only WIN32 Supported");
return 0;
#endif
}
BOOL GetAck(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR ID)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "GetAck");
if (hDeviceHandle == INVALID_HANDLE_VALUE) {
SoapySDR_logf(SOAPY_SDR_FATAL, "GetAck: Invalid Handle");
return FALSE;
}
ULONG cbSize = 64;
BOOL bResult = TRUE;
UCHAR* szBuffer = (UCHAR*)LocalAlloc(LPTR, sizeof(UCHAR)*cbSize);
ULONG cbRead = 0;
bResult = WinUsb_ReadPipe(hDeviceHandle, ID, szBuffer, cbSize, &cbRead, 0);
if (bResult) {
if (cbRead == 6) {
// FE FE E0 96 FB FD - OK
if (szBuffer[3] == 0x96 && szBuffer[4] == 0xFB)
SoapySDR_logf(SOAPY_SDR_TRACE, "GetAck: Valid Command");
// FE FE E0 96 FA FD - Fail
else if (szBuffer[3] == 0x96 && szBuffer[4] == 0xFA)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "GetAck: Invalid Command");
bResult = false;
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "GetAck: Unexpected Response %Xh %Xh %Xh %Xh %Xh %Xh", szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5]);
bResult = false;
}
}
else {
SoapySDR_logf(SOAPY_SDR_ERROR, "GetAck: Unexpected Response (%d) %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh %Xh", cbRead, szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5], szBuffer[6], szBuffer[7],
szBuffer[8], szBuffer[9], szBuffer[10], szBuffer[11], szBuffer[12], szBuffer[13], szBuffer[14], szBuffer[15]);
bResult = false;
}
}
else {
SoapySDR_logf(SOAPY_SDR_FATAL, "GetAck: WinUsb_ReadPipe Failed");
}
LocalFree(szBuffer);
return bResult;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "GetAck: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetRemoteOn(WINUSB_INTERFACE_HANDLE hDeviceHandle)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetRemoteOn");
UCHAR remote_on_cmd[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x00, 0x01, 0xFD, 0xFF };
ULONG sent = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, remote_on_cmd, sizeof(remote_on_cmd));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetRemoteOn: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetRemoteOff(WINUSB_INTERFACE_HANDLE hDeviceHandle)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetRemoteOff");
ULONG sent = 0;
UCHAR remote_off_cmd[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x00, 0x00, 0xFD, 0xFF };
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, remote_off_cmd, sizeof(remote_off_cmd));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetRemoteOff: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetSampleRate(WINUSB_INTERFACE_HANDLE hDeviceHandle, ULONG sampleRate)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetSampleRate");
UCHAR iq_5120_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x01, 0xFD, 0xFF };
UCHAR iq_3840_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x02, 0xFD, 0xFF };
UCHAR iq_1920_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x03, 0xFD, 0xFF };
UCHAR iq_960_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x04, 0xFD, 0xFF };
UCHAR iq_480_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x05, 0xFD, 0xFF };
UCHAR iq_240_16bit[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x1A, 0x13, 0x01, 0x01, 0x00, 0x06, 0xFD, 0xFF };
ULONG sent = 0;
if (sampleRate == 240000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 240000");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_240_16bit, sizeof(iq_240_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
if (sampleRate == 480000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 480000\n");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_480_16bit, sizeof(iq_480_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
if (sampleRate == 960000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 960000\n");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_960_16bit, sizeof(iq_960_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
if (sampleRate == 1920000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 1920000\n");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_1920_16bit, sizeof(iq_1920_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
if (sampleRate == 3840000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 3840000\n");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_3840_16bit, sizeof(iq_3840_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
if (sampleRate == 5120000) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetSampleRate: 5120000\n");
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, iq_5120_16bit, sizeof(iq_5120_16bit));
Sleep(100);
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
}
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetSampleRate: Undefined Sample Rate");
return FALSE;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetSampleRate: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetFrequency(WINUSB_INTERFACE_HANDLE hDeviceHandle, ULONG frequency)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetFrequency");
ULONG f1 = frequency % 100, f2 = (frequency / 100) % 100, f3 = (frequency / 10000) % 100, f4 = (frequency / 1000000) % 100, f5 = (frequency / 100000000) % 100;
UCHAR set_freq[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x05, decToBcd(f1),decToBcd(f2),decToBcd(f3),decToBcd(f4),decToBcd(f5), 0xFD, 0xFF };
ULONG sent = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_freq, sizeof(set_freq));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetFrequency: Only WIN32 Supported");
return FALSE;
#endif
}
//
// Antenna commands, both Set and Get, will only work if the R8600
// is tuned to the HF band, otherwise the R8600 will respond 'Invalid Command'
//
BOOL ICR8600SetAntenna(WINUSB_INTERFACE_HANDLE hDeviceHandle, ULONG antennaIndex)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetAntenna");
UCHAR set_ant[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x12, (UCHAR)(antennaIndex & 0xff), 0xFD, 0xFF };
ULONG sent = 0;
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600SetAntenna: Index = %d", antennaIndex);
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_ant, sizeof(set_ant));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetAntenna: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600GetAntenna(WINUSB_INTERFACE_HANDLE hDeviceHandle, PULONG antennaIndex)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600GetAntenna");
UCHAR set_ant[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x12, 0xFD };
ULONG sent = 0;
UCHAR response[64];
ULONG recv = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_ant, sizeof(set_ant));
recv = ReadBufferFromBulkEndpoint(hDeviceHandle, PIPE_RESPONSE_ID, response, sizeof(response));
if (recv == 8) {
*antennaIndex = (ULONG)response[5];
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetAntenna: Index = %d", *antennaIndex);
return true;
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetAntenna: Invalid Command");
return false;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600GetAntenna: Only WIN32 Supported");
return FALSE;
#endif
}
ULONG ICR8600ReadPipe(WINUSB_INTERFACE_HANDLE hDeviceHandle, PUCHAR Buffer, ULONG BufferLength)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600ReadPipe");
ULONG cbRead = 0;
BOOL bResult = WinUsb_ReadPipe(hDeviceHandle, PIPE_IQ_ID, Buffer, BufferLength, &cbRead, 0);
if (bResult) {
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600ReadPipe: Read %d", cbRead);
return cbRead;
}
else {
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600ReadPipe: WinUsb_ReadPipe Failed");
return 0;
}
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600ReadPipe: Only WIN32 Supported");
return 0;
#endif
}
BOOL ICR8600SetPreAmpOn(WINUSB_INTERFACE_HANDLE hDeviceHandle)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetPreAmpOn");
UCHAR set_preamp[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x16, 0x02, 0x01, 0xFD };
ULONG sent = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_preamp, sizeof(set_preamp));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetPreAmpOn: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetPreAmpOff(WINUSB_INTERFACE_HANDLE hDeviceHandle)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetPreAmpOff");
UCHAR set_preamp[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x16, 0x02, 0x00, 0xFD };
ULONG sent = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_preamp, sizeof(set_preamp));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetPreAmpOff: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetGainRF(WINUSB_INTERFACE_HANDLE hDeviceHandle, ULONG gain)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetGainRF");
ULONG g1 = gain % 100, g2 = (gain / 100) % 100;
UCHAR set_gain[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x14, 0x02, decToBcd(g2), decToBcd(g1), 0xFD, 0xFF };
ULONG sent = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_gain, sizeof(set_gain));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetGainRF: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600SetAttenuator(WINUSB_INTERFACE_HANDLE hDeviceHandle, ULONG atten)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetAttenuator");
UCHAR set_atten[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x11, decToBcd(atten), 0xFD, 0xFF };
ULONG sent = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600SetAttenuator: Attenuator = %d", atten);
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_atten, sizeof(set_atten));
return GetAck(hDeviceHandle, PIPE_RESPONSE_ID);
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600SetAttenuator: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600GetGainRF(WINUSB_INTERFACE_HANDLE hDeviceHandle, PULONG gain)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600GetGainRF");
UCHAR set_gain[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x14, 0x02, 0xFD, 0xFF };
UCHAR response[64];
ULONG sent = 0;
ULONG recv = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_gain, sizeof(set_gain));
recv = ReadBufferFromBulkEndpoint(hDeviceHandle, PIPE_RESPONSE_ID, response, sizeof(response));
if (recv == 10) {
ULONG g1 = bcdToDec(response[6]);
ULONG g2 = bcdToDec(response[7]);
*gain = (g1 * 100) + g2;
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetGainRF: RF Gain = %d", *gain);
return true;
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetGainRF: Invalid Command");
return false;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600GetGainRF: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600GetPreAmpState(WINUSB_INTERFACE_HANDLE hDeviceHandle, PBOOL on)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600GetPreAmpState");
UCHAR set_preamp[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x16, 0x02, 0xFD, 0xFF };
UCHAR response[64];
ULONG sent = 0;
ULONG recv = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_preamp, sizeof(set_preamp));
recv = ReadBufferFromBulkEndpoint(hDeviceHandle, PIPE_RESPONSE_ID, response, sizeof(response));
if (recv == 8) {
if (response[6] == 0x00) {
*on = false;
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetPreAmpState: OFF");
return true;
}
else if (response[6] == 0x01) {
*on = true;
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetPreAmpState: ON");
return true;
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetPreAmpState: Unexpected Response %Xh", response[6]);
return false;
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetPreAmpState: Invalid Command %d", recv);
return false;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600GetPreAmpState: Only WIN32 Supported");
return FALSE;
#endif
}
BOOL ICR8600GetAttenuator(WINUSB_INTERFACE_HANDLE hDeviceHandle, PULONG gain)
{
#ifdef _WIN32
SoapySDR_logf(SOAPY_SDR_TRACE, "ICR8600GetAttenuator");
UCHAR set_gain[] = { 0xFE, 0xFE, 0x96, 0xE0, 0x11, 0xFD };
UCHAR response[64];
ULONG sent = 0;
ULONG recv = 0;
WriteToBulkEndpoint(hDeviceHandle, PIPE_CONTROL_ID, &sent, set_gain, sizeof(set_gain));
recv = ReadBufferFromBulkEndpoint(hDeviceHandle, PIPE_RESPONSE_ID, response, sizeof(response));
if (recv == 8) {
*gain = bcdToDec(response[5]);
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetAttenuator: Gain = %d", *gain);
return true;
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "ICR8600GetAttenuator: Invalid Command");
return false;
#else
SoapySDR_logf(SOAPY_SDR_ERROR, "ICR8600GetAttenuator: Only WIN32 Supported");
return FALSE;
#endif
}