-
Notifications
You must be signed in to change notification settings - Fork 91
/
airspy.c
2011 lines (1689 loc) · 45.8 KB
/
airspy.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
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
/*
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"
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif
#define PACKET_SIZE (12)
#define UNPACKED_SIZE (16)
#define RAW_BUFFER_COUNT (8)
#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;
typedef struct airspy_device
{
libusb_context* usb_context;
libusb_device_handle* usb_device;
struct libusb_transfer** transfers;
airspy_sample_block_cb_fn callback;
volatile bool streaming;
volatile bool stop_requested;
pthread_t transfer_thread;
pthread_t consumer_thread;
bool transfer_thread_running;
bool consumer_thread_running;
pthread_cond_t consumer_cv;
pthread_mutex_t consumer_mp;
uint32_t supported_samplerate_count;
uint32_t *supported_samplerates;
uint32_t transfer_count;
uint32_t buffer_size;
uint32_t dropped_buffers;
uint32_t dropped_buffers_queue[RAW_BUFFER_COUNT];
uint16_t *received_samples_queue[RAW_BUFFER_COUNT];
volatile int received_samples_queue_head;
volatile int received_samples_queue_tail;
volatile int received_buffer_count;
void *output_buffer;
uint16_t *unpacked_samples;
bool packing_enabled;
iqconverter_float_t *cnv_f;
iqconverter_int16_t *cnv_i;
void* ctx;
enum airspy_sample_type sample_type;
} airspy_device_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)