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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
|
/*
Copyright (c) 2013, Michael Ossmann <mike@ossmann.com>
Copyright (c) 2012, Jared Boone <jared@sharebrained.com>
Copyright (c) 2014, Youssef Touil <youssef@airspy.com>
Copyright (c) 2014, Benjamin Vernoux <bvernoux@airspy.com>
Copyright (c) 2015, Ian Gilmour <ian@sdrsharp.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of AirSpy nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libusb.h"
#if _MSC_VER > 1700 // To avoid error with Visual Studio 2017/2019 or more define which define timespec as it is already defined in pthread.h
#define HAVE_STRUCT_TIMESPEC
#endif
#include <pthread.h>
#include "airspy.h"
#include "iqconverter_float.h"
#include "iqconverter_int16.h"
#include "filters.h"
#define PACKET_SIZE (12)
#define UNPACKED_SIZE (16)
#ifdef AIRSPY_BIG_ENDIAN
#define TO_LE(x) __builtin_bswap32(x)
#else
#define TO_LE(x) x
#endif
#define SAMPLE_RESOLUTION 12
#define SAMPLE_ENCAPSULATION 16
#define SAMPLE_SHIFT (SAMPLE_ENCAPSULATION - SAMPLE_RESOLUTION)
#define SAMPLE_SCALE (1.0f / (1 << (15 - SAMPLE_SHIFT)))
#define SERIAL_NUMBER_UNUSED (0ULL)
#define FILE_DESCRIPTOR_UNUSED (-1)
#define USB_PRODUCT_ID (2)
#define STR_DESCRIPTOR_SIZE (250)
#define MIN_SAMPLERATE_BY_VALUE (1000000)
#define SAMPLE_TYPE_IS_IQ(x) ((x) == AIRSPY_SAMPLE_FLOAT32_IQ || (x) == AIRSPY_SAMPLE_INT16_IQ)
#define LIBUSB_CTRL_TIMEOUT_MS (500)
typedef struct {
uint32_t freq_hz;
} set_freq_params_t;
static const uint16_t airspy_usb_vid = 0x1d50;
static const uint16_t airspy_usb_pid = 0x60a1;
#define STR_PREFIX_SERIAL_AIRSPY_SIZE (10)
#define SERIAL_AIRSPY_EXPECTED_SIZE (26)
#define GAIN_COUNT (22)
uint8_t airspy_linearity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4 };
uint8_t airspy_linearity_mixer_gains[GAIN_COUNT] = { 12, 12, 11, 9, 8, 7, 6, 6, 5, 0, 0, 1, 0, 0, 2, 2, 1, 1, 1, 1, 0, 0 };
uint8_t airspy_linearity_lna_gains[GAIN_COUNT] = { 14, 14, 14, 13, 12, 10, 9, 9, 8, 9, 8, 6, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0 };
uint8_t airspy_sensitivity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
uint8_t airspy_sensitivity_mixer_gains[GAIN_COUNT] = { 12, 12, 12, 12, 11, 10, 10, 9, 9, 8, 7, 4, 4, 4, 3, 2, 2, 1, 0, 0, 0, 0 };
uint8_t airspy_sensitivity_lna_gains[GAIN_COUNT] = { 14, 14, 14, 14, 14, 14, 14, 14, 14, 13, 12, 12, 9, 9, 8, 7, 6, 5, 3, 2, 1, 0 };
static int cancel_transfers(airspy_device_t* device)
{
uint32_t transfer_index;
if (device->transfers != NULL)
{
for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
{
if (device->transfers[transfer_index] != NULL)
{
libusb_cancel_transfer(device->transfers[transfer_index]);
}
}
return AIRSPY_SUCCESS;
}
else {
return AIRSPY_ERROR_OTHER;
}
}
static int free_transfers(airspy_device_t* device)
{
int i;
uint32_t transfer_index;
if (device->transfers != NULL)
{
// libusb_close() should free all transfers referenced from this array.
for (transfer_index = 0; transfer_index < device->transfer_count; transfer_index++)
{
if (device->transfers[transfer_index] != NULL)
{
free(device->transfers[transfer_index]->buffer);
libusb_free_transfer(device->transfers[transfer_index]);
device->transfers[transfer_index] = NULL;
}
}
free(device->transfers);
device->transfers = NULL;
if (device->output_buffer != NULL)
{
free(device->output_buffer);
device->output_buffer = NULL;
}
if (device->unpacked_samples != NULL)
{
free(device->unpacked_samples);
device->unpacked_samples = NULL;
}
for (i = 0; i < RAW_BUFFER_COUNT; i++)
{
if (device->received_samples_queue[i] != NULL)
{
free(device->received_samples_queue[i]);
device->received_samples_queue[i] = NULL;
}
}
}
return AIRSPY_SUCCESS;
}
static int allocate_transfers(airspy_device_t* const device)
{
int i;
size_t sample_count;
uint32_t transfer_index;
if (device->transfers == NULL)
{
for (i = 0; i < RAW_BUFFER_COUNT; i++)
{
device->received_samples_queue[i] = (uint16_t *)malloc(device->buffer_size);
if (device->received_samples_queue[i] == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
memset(device->received_samples_queue[i], 0, device->buffer_size);
}
if (device->packing_enabled)
{
sample_count = ((device->buffer_size / 2) * 4) / 3;
}
else
{
sample_count = device->buffer_size / 2;
}
device->output_buffer = (float *)malloc(sample_count * sizeof(float));
if (device->output_buffer == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
if (device->packing_enabled)
{
device->unpacked_samples = (uint16_t*)malloc(sample_count * sizeof(uint16_t));
if (device->unpacked_samples == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
}
device->transfers = (struct libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer));
if (device->transfers == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
{
device->transfers[transfer_index] = libusb_alloc_transfer(0);
if (device->transfers[transfer_index] == NULL)
{
return AIRSPY_ERROR_LIBUSB;
}
libusb_fill_bulk_transfer(
device->transfers[transfer_index],
device->usb_device,
0,
(unsigned char*)malloc(device->buffer_size),
device->buffer_size,
NULL,
device,
0
);
if (device->transfers[transfer_index]->buffer == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
}
return AIRSPY_SUCCESS;
}
else
{
return AIRSPY_ERROR_BUSY;
}
}
static int prepare_transfers(airspy_device_t* device, const uint_fast8_t endpoint_address, libusb_transfer_cb_fn callback)
{
int error;
uint32_t transfer_index;
if (device->transfers != NULL)
{
for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
{
device->transfers[transfer_index]->endpoint = endpoint_address;
device->transfers[transfer_index]->callback = callback;
error = libusb_submit_transfer(device->transfers[transfer_index]);
if (error != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
}
return AIRSPY_SUCCESS;
}
else {
// This shouldn't happen.
return AIRSPY_ERROR_OTHER;
}
}
static void convert_samples_int16(uint16_t *src, int16_t *dest, int count)
{
int i;
for (i = 0; i < count; i += 4)
{
dest[i + 0] = (src[i + 0] - 2048) << SAMPLE_SHIFT;
dest[i + 1] = (src[i + 1] - 2048) << SAMPLE_SHIFT;
dest[i + 2] = (src[i + 2] - 2048) << SAMPLE_SHIFT;
dest[i + 3] = (src[i + 3] - 2048) << SAMPLE_SHIFT;
}
}
static void convert_samples_float(uint16_t *src, float *dest, int count)
{
int i;
for (i = 0; i < count; i += 4)
{
dest[i + 0] = (src[i + 0] - 2048) * SAMPLE_SCALE;
dest[i + 1] = (src[i + 1] - 2048) * SAMPLE_SCALE;
dest[i + 2] = (src[i + 2] - 2048) * SAMPLE_SCALE;
dest[i + 3] = (src[i + 3] - 2048) * SAMPLE_SCALE;
}
}
static inline void unpack_samples(uint32_t *input, uint16_t *output, int length)
{
int i, j;
for (i = 0, j = 0; j < length; i += 3, j += 8)
{
output[j + 0] = (input[i] >> 20) & 0xfff;
output[j + 1] = (input[i] >> 8) & 0xfff;
output[j + 2] = ((input[i] & 0xff) << 4) | ((input[i + 1] >> 28) & 0xf);
output[j + 3] = ((input[i + 1] & 0xfff0000) >> 16);
output[j + 4] = ((input[i + 1] & 0xfff0) >> 4);
output[j + 5] = ((input[i + 1] & 0xf) << 8) | ((input[i + 2] & 0xff000000) >> 24);
output[j + 6] = ((input[i + 2] >> 12) & 0xfff);
output[j + 7] = ((input[i + 2] & 0xfff));
}
}
static void* consumer_threadproc(void *arg)
{
int sample_count;
uint16_t* input_samples;
uint32_t dropped_buffers;
airspy_device_t* device = (airspy_device_t*)arg;
airspy_transfer_t transfer;
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#endif
pthread_mutex_lock(&device->consumer_mp);
while (device->streaming && !device->stop_requested)
{
while (device->received_buffer_count == 0 && device->streaming && !device->stop_requested)
{
pthread_cond_wait(&device->consumer_cv, &device->consumer_mp);
}
if (!device->streaming || device->stop_requested)
{
break;
}
input_samples = device->received_samples_queue[device->received_samples_queue_tail];
dropped_buffers = device->dropped_buffers_queue[device->received_samples_queue_tail];
device->received_samples_queue_tail = (device->received_samples_queue_tail + 1) & (RAW_BUFFER_COUNT - 1);
pthread_mutex_unlock(&device->consumer_mp);
if (device->packing_enabled)
{
sample_count = ((device->buffer_size / 2) * 4) / 3;
if (device->sample_type != AIRSPY_SAMPLE_RAW)
{
unpack_samples((uint32_t*)input_samples, device->unpacked_samples, sample_count);
input_samples = device->unpacked_samples;
}
}
else
{
sample_count = device->buffer_size / 2;
}
switch (device->sample_type)
{
case AIRSPY_SAMPLE_FLOAT32_IQ:
convert_samples_float(input_samples, (float *)device->output_buffer, sample_count);
iqconverter_float_process(device->cnv_f, (float *) device->output_buffer, sample_count);
sample_count /= 2;
transfer.samples = device->output_buffer;
break;
case AIRSPY_SAMPLE_FLOAT32_REAL:
convert_samples_float(input_samples, (float *)device->output_buffer, sample_count);
transfer.samples = device->output_buffer;
break;
case AIRSPY_SAMPLE_INT16_IQ:
convert_samples_int16(input_samples, (int16_t *)device->output_buffer, sample_count);
iqconverter_int16_process(device->cnv_i, (int16_t *) device->output_buffer, sample_count);
sample_count /= 2;
transfer.samples = device->output_buffer;
break;
case AIRSPY_SAMPLE_INT16_REAL:
convert_samples_int16(input_samples, (int16_t *)device->output_buffer, sample_count);
transfer.samples = device->output_buffer;
break;
case AIRSPY_SAMPLE_UINT16_REAL:
case AIRSPY_SAMPLE_RAW:
transfer.samples = input_samples;
break;
case AIRSPY_SAMPLE_END:
// Just to shut GCC's moaning
break;
}
transfer.device = device;
transfer.ctx = device->ctx;
transfer.sample_count = sample_count;
transfer.sample_type = device->sample_type;
transfer.dropped_samples = (uint64_t) dropped_buffers * (uint64_t) sample_count;
if (device->callback(&transfer) != 0)
{
device->streaming = false;
}
pthread_mutex_lock(&device->consumer_mp);
device->received_buffer_count--;
}
device->streaming = false;
pthread_mutex_unlock(&device->consumer_mp);
return NULL;
}
static void airspy_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
{
uint16_t *temp;
airspy_device_t* device = (airspy_device_t*)usb_transfer->user_data;
if (!device->streaming || device->stop_requested)
{
return;
}
if (usb_transfer->status == LIBUSB_TRANSFER_COMPLETED && usb_transfer->actual_length == usb_transfer->length)
{
pthread_mutex_lock(&device->consumer_mp);
if (device->received_buffer_count < RAW_BUFFER_COUNT)
{
temp = device->received_samples_queue[device->received_samples_queue_head];
device->received_samples_queue[device->received_samples_queue_head] = (uint16_t *)usb_transfer->buffer;
usb_transfer->buffer = (uint8_t *)temp;
device->dropped_buffers_queue[device->received_samples_queue_head] = device->dropped_buffers;
device->dropped_buffers = 0;
device->received_samples_queue_head = (device->received_samples_queue_head + 1) & (RAW_BUFFER_COUNT - 1);
device->received_buffer_count++;
pthread_cond_signal(&device->consumer_cv);
}
else
{
device->dropped_buffers++;
}
pthread_mutex_unlock(&device->consumer_mp);
if (libusb_submit_transfer(usb_transfer) != 0)
{
device->streaming = false;
}
}
else
{
device->streaming = false;
}
}
static void* transfer_threadproc(void* arg)
{
airspy_device_t* device = (airspy_device_t*)arg;
int error;
struct timeval timeout = { 0, 500000 };
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#endif
while (device->streaming && !device->stop_requested)
{
error = libusb_handle_events_timeout_completed(device->usb_context, &timeout, NULL);
if (error < 0)
{
if (error != LIBUSB_ERROR_INTERRUPTED)
device->streaming = false;
}
}
device->streaming = false;
return NULL;
}
static int kill_io_threads(airspy_device_t* device)
{
struct timeval timeout = { 0, 0 };
if (device->stop_requested)
{
device->stop_requested = false;
device->streaming = false;
cancel_transfers(device);
pthread_mutex_lock(&device->consumer_mp);
pthread_cond_signal(&device->consumer_cv);
pthread_mutex_unlock(&device->consumer_mp);
if (device->transfer_thread_running) {
pthread_join(device->transfer_thread, NULL);
device->transfer_thread_running = false;
}
if (device->consumer_thread_running) {
pthread_join(device->consumer_thread, NULL);
device->consumer_thread_running = false;
}
libusb_handle_events_timeout_completed(device->usb_context, &timeout, NULL);
}
return AIRSPY_SUCCESS;
}
static int create_io_threads(airspy_device_t* device, airspy_sample_block_cb_fn callback)
{
int result;
pthread_attr_t attr;
if (!device->streaming && !device->stop_requested)
{
device->callback = callback;
device->streaming = true;
result = prepare_transfers(device, LIBUSB_ENDPOINT_IN | 1, (libusb_transfer_cb_fn)airspy_libusb_transfer_callback);
if (result != AIRSPY_SUCCESS)
{
return result;
}
device->received_samples_queue_head = 0;
device->received_samples_queue_tail = 0;
device->received_buffer_count = 0;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
result = pthread_create(&device->consumer_thread, &attr, consumer_threadproc, device);
if (result != 0)
{
return AIRSPY_ERROR_THREAD;
}
device->consumer_thread_running = true;
result = pthread_create(&device->transfer_thread, &attr, transfer_threadproc, device);
if (result != 0)
{
return AIRSPY_ERROR_THREAD;
}
device->transfer_thread_running = true;
pthread_attr_destroy(&attr);
}
else {
return AIRSPY_ERROR_BUSY;
}
return AIRSPY_SUCCESS;
}
static void airspy_open_exit(airspy_device_t* device)
{
if (device->usb_device != NULL)
{
libusb_release_interface(device->usb_device, 0);
libusb_close(device->usb_device);
device->usb_device = NULL;
}
libusb_exit(device->usb_context);
device->usb_context = NULL;
}
static void airspy_open_device(airspy_device_t* device,
int* ret,
uint16_t vid,
uint16_t pid,
uint64_t serial_number_val)
{
int i;
int result;
libusb_device_handle** libusb_dev_handle;
int serial_number_len;
libusb_device_handle* dev_handle;
libusb_device *dev;
libusb_device** devices = NULL;
ssize_t cnt;
int serial_descriptor_index;
struct libusb_device_descriptor device_descriptor;
unsigned char serial_number[SERIAL_AIRSPY_EXPECTED_SIZE + 1];
libusb_dev_handle = &device->usb_device;
*libusb_dev_handle = NULL;
cnt = libusb_get_device_list(device->usb_context, &devices);
if (cnt < 0)
{
*ret = AIRSPY_ERROR_NOT_FOUND;
return;
}
i = 0;
while ((dev = devices[i++]) != NULL)
{
libusb_get_device_descriptor(dev, &device_descriptor);
if ((device_descriptor.idVendor == vid) &&
(device_descriptor.idProduct == pid))
{
if (serial_number_val != SERIAL_NUMBER_UNUSED)
{
serial_descriptor_index = device_descriptor.iSerialNumber;
if (serial_descriptor_index > 0)
{
if (libusb_open(dev, libusb_dev_handle) != 0)
{
*libusb_dev_handle = NULL;
continue;
}
dev_handle = *libusb_dev_handle;
serial_number_len = libusb_get_string_descriptor_ascii(dev_handle,
serial_descriptor_index,
serial_number,
sizeof(serial_number));
if (serial_number_len == SERIAL_AIRSPY_EXPECTED_SIZE)
{
uint64_t serial = 0;
// use same code to determine device's serial number as in airspy_list_devices()
{
char *start, *end;
serial_number[SERIAL_AIRSPY_EXPECTED_SIZE] = 0;
start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPY_SIZE);
end = NULL;
serial = strtoull(start, &end, 16);
}
if (serial == serial_number_val)
{
#ifdef __linux__
/* Check whether a kernel driver is attached to interface #0. If so, we'll
* need to detach it.
*/
if (libusb_kernel_driver_active(dev_handle, 0))
{
libusb_detach_kernel_driver(dev_handle, 0);
}
#endif
result = libusb_set_configuration(dev_handle, 1);
if (result != 0)
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
result = libusb_claim_interface(dev_handle, 0);
if (result != 0)
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
break;
}
else
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
}
else
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
}
}
else
{
if (libusb_open(dev, libusb_dev_handle) == 0)
{
dev_handle = *libusb_dev_handle;
#ifdef __linux__
/* Check whether a kernel driver is attached to interface #0. If so, we'll
* need to detach it.
*/
if (libusb_kernel_driver_active(dev_handle, 0))
{
libusb_detach_kernel_driver(dev_handle, 0);
}
#endif
result = libusb_set_configuration(dev_handle, 1);
if (result != 0)
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
result = libusb_claim_interface(dev_handle, 0);
if (result != 0)
{
libusb_close(dev_handle);
*libusb_dev_handle = NULL;
continue;
}
break;
}
}
}
}
libusb_free_device_list(devices, 1);
dev_handle = device->usb_device;
if (dev_handle == NULL)
{
*ret = AIRSPY_ERROR_NOT_FOUND;
return;
}
*ret = AIRSPY_SUCCESS;
return;
}
static void airspy_open_device_fd(airspy_device_t* device,
int* ret,
int fd)
{
int result = -1;
#ifdef __ANDROID__
result = libusb_wrap_sys_device(device->usb_context, (intptr_t)fd, &device->usb_device);
#else
device->usb_device = NULL;
*ret = AIRSPY_ERROR_UNSUPPORTED;
return;
#endif
if (result != 0 || device->usb_device == NULL)
{
*ret = AIRSPY_ERROR_LIBUSB;
return;
}
#ifdef __linux__
/* Check whether a kernel driver is attached to interface #0. If so, we'll
* need to detach it.
*/
if (libusb_kernel_driver_active(device->usb_device, 0))
{
libusb_detach_kernel_driver(device->usb_device, 0);
}
#endif
result = libusb_set_configuration(device->usb_device, 1);
if (result != 0)
{
libusb_close(device->usb_device);
device->usb_device = NULL;
*ret = AIRSPY_ERROR_LIBUSB;
return;
}
result = libusb_claim_interface(device->usb_device, 0);
if (result != 0)
{
libusb_close(device->usb_device);
device->usb_device = NULL;
*ret = AIRSPY_ERROR_LIBUSB;
return;
}
*ret = AIRSPY_SUCCESS;
return;
}
static int airspy_read_samplerates_from_fw(struct airspy_device* device, uint32_t* buffer, const uint32_t len)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_GET_SAMPLERATES,
0,
len,
(unsigned char*)buffer,
(len > 0 ? len : 1) * sizeof(uint32_t),
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_OTHER;
}
return AIRSPY_SUCCESS;
}
static int airspy_open_init(airspy_device_t** device, uint64_t serial_number, int fd)
{
airspy_device_t* lib_device;
int libusb_error;
int result;
*device = NULL;
lib_device = (airspy_device_t*)calloc(1, sizeof(airspy_device_t));
if (lib_device == NULL)
{
return AIRSPY_ERROR_NO_MEM;
}
#ifdef __ANDROID__
// LibUSB does not support device discovery on android
libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
#endif
libusb_error = libusb_init(&lib_device->usb_context);
if (libusb_error != 0)
{
free(lib_device);
return AIRSPY_ERROR_LIBUSB;
}
if (fd == FILE_DESCRIPTOR_UNUSED) {
airspy_open_device(lib_device,
&result,
airspy_usb_vid,
airspy_usb_pid,
serial_number);
}
else {
airspy_open_device_fd(lib_device,
&result,
fd);
}
if (lib_device->usb_device == NULL)
{
libusb_exit(lib_device->usb_context);
free(lib_device);
return result;
}
lib_device->transfers = NULL;
lib_device->callback = NULL;
lib_device->transfer_count = 16;
lib_device->buffer_size = 262144;
lib_device->packing_enabled = false;
lib_device->streaming = false;
lib_device->stop_requested = false;
lib_device->sample_type = AIRSPY_SAMPLE_FLOAT32_IQ;
result = airspy_read_samplerates_from_fw(lib_device, &lib_device->supported_samplerate_count, 0);
if (result == AIRSPY_SUCCESS)
{
lib_device->supported_samplerates = (uint32_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
result = airspy_read_samplerates_from_fw(lib_device, lib_device->supported_samplerates, lib_device->supported_samplerate_count);
if (result != AIRSPY_SUCCESS)
{
free(lib_device->supported_samplerates);
}
}
if (result != AIRSPY_SUCCESS)
{
lib_device->supported_samplerate_count = 2;
lib_device->supported_samplerates = (uint32_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
lib_device->supported_samplerates[0] = 10000000;
lib_device->supported_samplerates[1] = 2500000;
}
airspy_set_packing(lib_device, 0);
result = allocate_transfers(lib_device);
if (result != 0)
{
airspy_open_exit(lib_device);
free(lib_device->supported_samplerates);
free(lib_device);
return AIRSPY_ERROR_NO_MEM;
}
lib_device->cnv_f = iqconverter_float_create(HB_KERNEL_FLOAT, HB_KERNEL_FLOAT_LEN);
lib_device->cnv_i = iqconverter_int16_create(HB_KERNEL_INT16, HB_KERNEL_INT16_LEN);
pthread_cond_init(&lib_device->consumer_cv, NULL);
pthread_mutex_init(&lib_device->consumer_mp, NULL);
*device = lib_device;
return AIRSPY_SUCCESS;
}
#ifdef __cplusplus
extern "C"
{
#endif
void ADDCALL airspy_lib_version(airspy_lib_version_t* lib_version)
{
lib_version->major_version = AIRSPY_VER_MAJOR;
lib_version->minor_version = AIRSPY_VER_MINOR;
lib_version->revision = AIRSPY_VER_REVISION;
}
/* airspy_init() deprecated */
int ADDCALL airspy_init(void)
{
return AIRSPY_SUCCESS;
}
/* airspy_exit() deprecated */
int ADDCALL airspy_exit(void)
{
return AIRSPY_SUCCESS;
}
int airspy_list_devices(uint64_t *serials, int count)
{
libusb_device_handle* libusb_dev_handle;
struct libusb_context *context;
libusb_device** devices = NULL;
libusb_device *dev;
struct libusb_device_descriptor device_descriptor;
int serial_descriptor_index;
int serial_number_len;
int output_count;
int i;
unsigned char serial_number[SERIAL_AIRSPY_EXPECTED_SIZE + 1];
if (serials)
{
memset(serials, 0, sizeof(uint64_t) * count);
}
#ifdef __ANDROID__
// LibUSB does not support device discovery on android
libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
#endif
if (libusb_init(&context) != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
if (libusb_get_device_list(context, &devices) < 0)
{
return AIRSPY_ERROR_NOT_FOUND;
}
i = 0;
output_count = 0;
while ((dev = devices[i++]) != NULL && (!serials || output_count < count))
{
libusb_get_device_descriptor(dev, &device_descriptor);
if ((device_descriptor.idVendor == airspy_usb_vid) &&
(device_descriptor.idProduct == airspy_usb_pid))
{
serial_descriptor_index = device_descriptor.iSerialNumber;
if (serial_descriptor_index > 0)
{
if (libusb_open(dev, &libusb_dev_handle) != 0)
{
continue;
}
serial_number_len = libusb_get_string_descriptor_ascii(libusb_dev_handle,
serial_descriptor_index,
serial_number,
sizeof(serial_number));
if (serial_number_len == SERIAL_AIRSPY_EXPECTED_SIZE)
{
char *start, *end;
uint64_t serial;
serial_number[SERIAL_AIRSPY_EXPECTED_SIZE] = 0;
start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPY_SIZE);
end = NULL;
serial = strtoull(start, &end, 16);
if (serial == 0 && start == end)
{
libusb_close(libusb_dev_handle);
continue;
}
if (serials)
{
serials[output_count] = serial;
}
output_count++;
}
libusb_close(libusb_dev_handle);
}
}
}
libusb_free_device_list(devices, 1);
libusb_exit(context);
return output_count;
}
int ADDCALL airspy_open_sn(airspy_device_t** device, uint64_t serial_number)
{
int result;
result = airspy_open_init(device, serial_number, FILE_DESCRIPTOR_UNUSED);
return result;
}
int ADDCALL airspy_open_fd(airspy_device_t** device, int fd)
{
int result;
result = airspy_open_init(device, SERIAL_NUMBER_UNUSED, fd);
return result;
}
int ADDCALL airspy_open(airspy_device_t** device)
{
int result;
result = airspy_open_init(device, SERIAL_NUMBER_UNUSED, FILE_DESCRIPTOR_UNUSED);
return result;
}
int ADDCALL airspy_close(airspy_device_t* device)
{
int result;
result = AIRSPY_SUCCESS;
if (device != NULL)
{
result = airspy_stop_rx(device);
iqconverter_float_free(device->cnv_f);
iqconverter_int16_free(device->cnv_i);
pthread_cond_destroy(&device->consumer_cv);
pthread_mutex_destroy(&device->consumer_mp);
free_transfers(device);
airspy_open_exit(device);
free(device->supported_samplerates);
free(device);
}
return result;
}
int ADDCALL airspy_get_samplerates(struct airspy_device* device, uint32_t* buffer, const uint32_t len)
{
uint32_t i;
if (len == 0)
{
*buffer = device->supported_samplerate_count;
}
else if (len <= device->supported_samplerate_count)
{
memcpy(buffer, device->supported_samplerates, len * sizeof(uint32_t));
if (!SAMPLE_TYPE_IS_IQ(device->sample_type))
{
for (i = 0; i < len; i++)
{
buffer[i] *= 2;
}
}
}
else
{
return AIRSPY_ERROR_INVALID_PARAM;
}
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_samplerate(airspy_device_t* device, uint32_t samplerate)
{
int result;
uint8_t retval;
uint8_t length;
uint32_t i;
if (samplerate >= MIN_SAMPLERATE_BY_VALUE)
{
for (i = 0; i < device->supported_samplerate_count; i++)
{
if (samplerate == device->supported_samplerates[i])
{
samplerate = i;
break;
}
}
if (samplerate >= MIN_SAMPLERATE_BY_VALUE)
{
if (SAMPLE_TYPE_IS_IQ(device->sample_type))
{
samplerate *= 2;
}
samplerate /= 1000;
}
}
libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | 1);
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_SAMPLERATE,
0,
samplerate,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_receiver_mode(airspy_device_t* device, receiver_mode_t value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_RECEIVER_MODE,
value,
0,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_start_rx(airspy_device_t* device, airspy_sample_block_cb_fn callback, void* ctx)
{
int result;
iqconverter_float_reset(device->cnv_f);
iqconverter_int16_reset(device->cnv_i);
memset(device->dropped_buffers_queue, 0, RAW_BUFFER_COUNT * sizeof(uint32_t));
device->dropped_buffers = 0;
result = airspy_set_receiver_mode(device, RECEIVER_MODE_OFF);
if (result != AIRSPY_SUCCESS)
{
return result;
}
libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | 1);
result = airspy_set_receiver_mode(device, RECEIVER_MODE_RX);
if (result == AIRSPY_SUCCESS)
{
device->ctx = ctx;
result = create_io_threads(device, callback);
}
return result;
}
int ADDCALL airspy_stop_rx(airspy_device_t* device)
{
int result1, result2;
device->stop_requested = true;
result1 = airspy_set_receiver_mode(device, RECEIVER_MODE_OFF);
result2 = kill_io_threads(device);
if (result1 != AIRSPY_SUCCESS)
{
return result1;
}
return result2;
}
int ADDCALL airspy_si5351c_read(airspy_device_t* device, uint8_t register_number, uint8_t* value)
{
uint8_t temp_value;
int result;
temp_value = 0;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SI5351C_READ,
0,
register_number,
(unsigned char*)&temp_value,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
*value = temp_value;
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_si5351c_write(airspy_device_t* device, uint8_t register_number, uint8_t value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SI5351C_WRITE,
value,
register_number,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_r820t_read(airspy_device_t* device, uint8_t register_number, uint8_t* value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_R820T_READ,
0,
register_number,
(unsigned char*)value,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_r820t_write(airspy_device_t* device, uint8_t register_number, uint8_t value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_R820T_WRITE,
value,
register_number,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_gpio_read(airspy_device_t* device, airspy_gpio_port_t port, airspy_gpio_pin_t pin, uint8_t* value)
{
int result;
uint8_t port_pin;
port_pin = ((uint8_t)port) << 5;
port_pin = port_pin | pin;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_GPIO_READ,
0,
port_pin,
(unsigned char*)value,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_gpio_write(airspy_device_t* device, airspy_gpio_port_t port, airspy_gpio_pin_t pin, uint8_t value)
{
int result;
uint8_t port_pin;
port_pin = ((uint8_t)port) << 5;
port_pin = port_pin | pin;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_GPIO_WRITE,
value,
port_pin,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_gpiodir_read(airspy_device_t* device, airspy_gpio_port_t port, airspy_gpio_pin_t pin, uint8_t* value)
{
int result;
uint8_t port_pin;
port_pin = ((uint8_t)port) << 5;
port_pin = port_pin | pin;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_GPIODIR_READ,
0,
port_pin,
(unsigned char*)value,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_gpiodir_write(airspy_device_t* device, airspy_gpio_port_t port, airspy_gpio_pin_t pin, uint8_t value)
{
int result;
uint8_t port_pin;
port_pin = ((uint8_t)port) << 5;
port_pin = port_pin | pin;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_GPIODIR_WRITE,
value,
port_pin,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_spiflash_erase(airspy_device_t* device)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SPIFLASH_ERASE,
0,
0,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_spiflash_erase_sector(airspy_device_t* device, const uint16_t sector_num)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SPIFLASH_ERASE_SECTOR,
sector_num,
0,
NULL,
0,
LIBUSB_CTRL_TIMEOUT_MS);
if (result != 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_spiflash_write(airspy_device_t* device, const uint32_t address, const uint16_t length, unsigned char* const data)
{
int result;
if (address > 0x0FFFFF)
{
return AIRSPY_ERROR_INVALID_PARAM;
}
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SPIFLASH_WRITE,
address >> 16,
address & 0xFFFF,
data,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_spiflash_read(airspy_device_t* device, const uint32_t address, const uint16_t length, unsigned char* data)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SPIFLASH_READ,
address >> 16,
address & 0xFFFF,
data,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_board_id_read(airspy_device_t* device, uint8_t* value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_BOARD_ID_READ,
0,
0,
value,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_version_string_read(airspy_device_t* device, char* version, uint8_t length)
{
#define VERSION_LOCAL_SIZE (128)
int result;
char version_local[VERSION_LOCAL_SIZE] = "";
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_VERSION_STRING_READ,
0,
0,
(unsigned char*)version_local,
(VERSION_LOCAL_SIZE - 1),
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 0)
{
return AIRSPY_ERROR_LIBUSB;
}
else
{
if (length > 0)
{
const int num_bytes_to_copy = (length > VERSION_LOCAL_SIZE ? VERSION_LOCAL_SIZE : length) - 1;
memcpy(version, version_local, num_bytes_to_copy);
version[num_bytes_to_copy] = 0;
return AIRSPY_SUCCESS;
}
else
{
return AIRSPY_ERROR_INVALID_PARAM;
}
}
}
int ADDCALL airspy_board_partid_serialno_read(airspy_device_t* device, airspy_read_partid_serialno_t* read_partid_serialno)
{
uint8_t length;
int result;
length = sizeof(airspy_read_partid_serialno_t);
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_BOARD_PARTID_SERIALNO_READ,
0,
0,
(unsigned char*)read_partid_serialno,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
read_partid_serialno->part_id[0] = TO_LE(read_partid_serialno->part_id[0]);
read_partid_serialno->part_id[1] = TO_LE(read_partid_serialno->part_id[1]);
read_partid_serialno->serial_no[0] = TO_LE(read_partid_serialno->serial_no[0]);
read_partid_serialno->serial_no[1] = TO_LE(read_partid_serialno->serial_no[1]);
read_partid_serialno->serial_no[2] = TO_LE(read_partid_serialno->serial_no[2]);
read_partid_serialno->serial_no[3] = TO_LE(read_partid_serialno->serial_no[3]);
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_sample_type(struct airspy_device* device, enum airspy_sample_type sample_type)
{
device->sample_type = sample_type;
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_freq(airspy_device_t* device, const uint32_t freq_hz)
{
set_freq_params_t set_freq_params;
uint8_t length;
int result;
set_freq_params.freq_hz = TO_LE(freq_hz);
length = sizeof(set_freq_params_t);
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_FREQ,
0,
0,
(unsigned char*)&set_freq_params,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_conversion_filter_float32(struct airspy_device* device, const float *kernel, const uint32_t len)
{
if (device->streaming)
{
return AIRSPY_ERROR_BUSY;
}
iqconverter_float_free(device->cnv_f);
device->cnv_f = iqconverter_float_create(kernel, len);
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_conversion_filter_int16(struct airspy_device* device, const int16_t *kernel, const uint32_t len)
{
if (device->streaming)
{
return AIRSPY_ERROR_BUSY;
}
iqconverter_int16_free(device->cnv_i);
device->cnv_i = iqconverter_int16_create(kernel, len);
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_lna_gain(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
uint8_t length;
if (value > 14)
value = 14;
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_LNA_GAIN,
0,
value,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_mixer_gain(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
uint8_t length;
if (value > 15)
value = 15;
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_MIXER_GAIN,
0,
value,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_vga_gain(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
uint8_t length;
if (value > 15)
value = 15;
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_VGA_GAIN,
0,
value,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_lna_agc(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
uint8_t length;
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_LNA_AGC,
0,
value,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_mixer_agc(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
uint8_t length;
length = 1;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_MIXER_AGC,
0,
value,
&retval,
length,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < length)
{
return AIRSPY_ERROR_LIBUSB;
}
else {
return AIRSPY_SUCCESS;
}
}
int ADDCALL airspy_set_linearity_gain(struct airspy_device* device, uint8_t value)
{
int rc;
if (value >= GAIN_COUNT)
{
value = GAIN_COUNT - 1;
}
value = GAIN_COUNT - 1 - value;
rc = airspy_set_mixer_agc(device, 0);
if (rc < 0)
return rc;
rc = airspy_set_lna_agc(device, 0);
if (rc < 0)
return rc;
rc = airspy_set_vga_gain(device, airspy_linearity_vga_gains[value]);
if (rc < 0)
return rc;
rc = airspy_set_mixer_gain(device, airspy_linearity_mixer_gains[value]);
if (rc < 0)
return rc;
rc = airspy_set_lna_gain(device, airspy_linearity_lna_gains[value]);
if (rc < 0)
return rc;
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_sensitivity_gain(struct airspy_device* device, uint8_t value)
{
int rc;
if (value >= GAIN_COUNT)
{
value = GAIN_COUNT - 1;
}
value = GAIN_COUNT - 1 - value;
rc = airspy_set_mixer_agc(device, 0);
if (rc < 0)
return rc;
rc = airspy_set_lna_agc(device, 0);
if (rc < 0)
return rc;
rc = airspy_set_vga_gain(device, airspy_sensitivity_vga_gains[value]);
if (rc < 0)
return rc;
rc = airspy_set_mixer_gain(device, airspy_sensitivity_mixer_gains[value]);
if (rc < 0)
return rc;
rc = airspy_set_lna_gain(device, airspy_sensitivity_lna_gains[value]);
if (rc < 0)
return rc;
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_set_rf_bias(airspy_device_t* device, uint8_t value)
{
return airspy_gpio_write(device, GPIO_PORT1, GPIO_PIN13, value);
}
int ADDCALL airspy_set_packing(airspy_device_t* device, uint8_t value)
{
int result;
uint8_t retval;
bool packing_enabled;
if (device->streaming)
{
return AIRSPY_ERROR_BUSY;
}
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
AIRSPY_SET_PACKING,
0,
value,
&retval,
1,
LIBUSB_CTRL_TIMEOUT_MS);
if (result < 1)
{
return AIRSPY_ERROR_LIBUSB;
}
packing_enabled = value ? true : false;
if (packing_enabled != device->packing_enabled)
{
cancel_transfers(device);
free_transfers(device);
device->packing_enabled = packing_enabled;
device->buffer_size = packing_enabled ? (6144 * 24) : 262144;
result = allocate_transfers(device);
if (result != 0)
{
return AIRSPY_ERROR_NO_MEM;
}
}
return AIRSPY_SUCCESS;
}
int ADDCALL airspy_is_streaming(airspy_device_t* device)
{
return (device->streaming == true && device->stop_requested == false);
}
const char* ADDCALL airspy_error_name(enum airspy_error errcode)
{
switch (errcode)
{
case AIRSPY_SUCCESS:
return "AIRSPY_SUCCESS";
case AIRSPY_TRUE:
return "AIRSPY_TRUE";
case AIRSPY_ERROR_INVALID_PARAM:
return "AIRSPY_ERROR_INVALID_PARAM";
case AIRSPY_ERROR_NOT_FOUND:
return "AIRSPY_ERROR_NOT_FOUND";
case AIRSPY_ERROR_BUSY:
return "AIRSPY_ERROR_BUSY";
case AIRSPY_ERROR_NO_MEM:
return "AIRSPY_ERROR_NO_MEM";
case AIRSPY_ERROR_LIBUSB:
return "AIRSPY_ERROR_LIBUSB";
case AIRSPY_ERROR_THREAD:
return "AIRSPY_ERROR_THREAD";
case AIRSPY_ERROR_STREAMING_THREAD_ERR:
return "AIRSPY_ERROR_STREAMING_THREAD_ERR";
case AIRSPY_ERROR_STREAMING_STOPPED:
return "AIRSPY_ERROR_STREAMING_STOPPED";
case AIRSPY_ERROR_OTHER:
return "AIRSPY_ERROR_OTHER";
default:
return "airspy unknown error";
}
}
const char* ADDCALL airspy_board_id_name(enum airspy_board_id board_id)
{
switch (board_id)
{
case AIRSPY_BOARD_ID_PROTO_AIRSPY:
return "AIRSPY";
case AIRSPY_BOARD_ID_INVALID:
return "Invalid Board ID";
default:
return "Unknown Board ID";
}
}
#ifdef __cplusplus
} // __cplusplus defined.
#endif
|