-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathhackrf.c
More file actions
3602 lines (3121 loc) · 84 KB
/
Copy pathhackrf.c
File metadata and controls
3602 lines (3121 loc) · 84 KB
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) 2012-2026 Great Scott Gadgets <info@greatscottgadgets.com>
Copyright (c) 2012, Jared Boone <jared@sharebrained.com>
Copyright (c) 2013, Benjamin Vernoux <titanmkd@gmail.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 Great Scott Gadgets 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 "hackrf.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#include <signal.h>
#endif
#include <libusb.h>
#ifdef _WIN32
/* Avoid redefinition of timespec from time.h (included by libusb.h) */
#define HAVE_STRUCT_TIMESPEC 1
#define strdup _strdup
#define strcasecmp _stricmp
#endif
#include <pthread.h>
#ifdef HACKRF_BIG_ENDIAN
#define TO_LE(x) __builtin_bswap32(x)
#define TO_LE64(x) __builtin_bswap64(x)
#define FROM_LE16(x) __builtin_bswap16(x)
#define FROM_LE32(x) __builtin_bswap32(x)
#define FROM_LE64(x) __builtin_bswap64(x)
#else
#define TO_LE(x) x
#define TO_LE64(x) x
#define FROM_LE16(x) x
#define FROM_LE32(x) x
#define FROM_LE64(x) x
#endif
#define DEFAULT_REQUEST_TIMEOUT 100
#define CPLD_WRITE_TIMEOUT 10000
#define SPIFLASH_WRITE_TIMEOUT 50000 // W25Q32JV max chip erase time
// TODO: Factor this into a shared #include so that firmware can use
// the same values.
typedef enum {
HACKRF_VENDOR_REQUEST_SET_TRANSCEIVER_MODE = 1,
HACKRF_VENDOR_REQUEST_MAX283X_WRITE = 2,
HACKRF_VENDOR_REQUEST_MAX283X_READ = 3,
HACKRF_VENDOR_REQUEST_SI5351C_WRITE = 4,
HACKRF_VENDOR_REQUEST_SI5351C_READ = 5,
HACKRF_VENDOR_REQUEST_SAMPLE_RATE_SET = 6,
HACKRF_VENDOR_REQUEST_BASEBAND_FILTER_BANDWIDTH_SET = 7,
HACKRF_VENDOR_REQUEST_RFFC5071_WRITE = 8,
HACKRF_VENDOR_REQUEST_RFFC5071_READ = 9,
HACKRF_VENDOR_REQUEST_SPIFLASH_ERASE = 10,
HACKRF_VENDOR_REQUEST_SPIFLASH_WRITE = 11,
HACKRF_VENDOR_REQUEST_SPIFLASH_READ = 12,
HACKRF_VENDOR_REQUEST_BOARD_ID_READ = 14,
HACKRF_VENDOR_REQUEST_VERSION_STRING_READ = 15,
HACKRF_VENDOR_REQUEST_SET_FREQ = 16,
HACKRF_VENDOR_REQUEST_AMP_ENABLE = 17,
HACKRF_VENDOR_REQUEST_BOARD_PARTID_SERIALNO_READ = 18,
HACKRF_VENDOR_REQUEST_SET_LNA_GAIN = 19,
HACKRF_VENDOR_REQUEST_SET_VGA_GAIN = 20,
HACKRF_VENDOR_REQUEST_SET_TXVGA_GAIN = 21,
HACKRF_VENDOR_REQUEST_ANTENNA_ENABLE = 23,
HACKRF_VENDOR_REQUEST_SET_FREQ_EXPLICIT = 24,
HACKRF_VENDOR_REQUEST_USB_WCID_VENDOR_REQ = 25,
HACKRF_VENDOR_REQUEST_INIT_SWEEP = 26,
HACKRF_VENDOR_REQUEST_OPERACAKE_GET_BOARDS = 27,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_PORTS = 28,
HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE = 29,
HACKRF_VENDOR_REQUEST_RESET = 30,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_RANGES = 31,
HACKRF_VENDOR_REQUEST_CLKOUT_ENABLE = 32,
HACKRF_VENDOR_REQUEST_SPIFLASH_STATUS = 33,
HACKRF_VENDOR_REQUEST_SPIFLASH_CLEAR_STATUS = 34,
HACKRF_VENDOR_REQUEST_OPERACAKE_GPIO_TEST = 35,
HACKRF_VENDOR_REQUEST_CPLD_CHECKSUM = 36,
HACKRF_VENDOR_REQUEST_UI_ENABLE = 37,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_MODE = 38,
HACKRF_VENDOR_REQUEST_OPERACAKE_GET_MODE = 39,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_DWELL_TIMES = 40,
HACKRF_VENDOR_REQUEST_GET_M0_STATE = 41,
HACKRF_VENDOR_REQUEST_SET_TX_UNDERRUN_LIMIT = 42,
HACKRF_VENDOR_REQUEST_SET_RX_OVERRUN_LIMIT = 43,
HACKRF_VENDOR_REQUEST_GET_CLKIN_STATUS = 44,
HACKRF_VENDOR_REQUEST_BOARD_REV_READ = 45,
HACKRF_VENDOR_REQUEST_SUPPORTED_PLATFORM_READ = 46,
HACKRF_VENDOR_REQUEST_SET_LEDS = 47,
HACKRF_VENDOR_REQUEST_SET_USER_BIAS_T_OPTS = 48,
HACKRF_VENDOR_REQUEST_FPGA_WRITE_REG = 49,
HACKRF_VENDOR_REQUEST_FPGA_READ_REG = 50,
HACKRF_VENDOR_REQUEST_P2_CTRL = 51,
HACKRF_VENDOR_REQUEST_P1_CTRL = 52,
HACKRF_VENDOR_REQUEST_SET_NARROWBAND_FILTER = 53,
HACKRF_VENDOR_REQUEST_SET_FPGA_BITSTREAM = 54,
HACKRF_VENDOR_REQUEST_CLKIN_CTRL = 55,
HACKRF_VENDOR_REQUEST_READ_SELFTEST = 56,
HACKRF_VENDOR_REQUEST_READ_ADC = 57,
HACKRF_VENDOR_REQUEST_TEST_RTC_OSC = 58,
HACKRF_VENDOR_REQUEST_RADIO_WRITE_REG = 59,
HACKRF_VENDOR_REQUEST_RADIO_READ_REG = 60,
HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 61,
HACKRF_VENDOR_REQUEST_READ_TEMPERATURE = 62,
} hackrf_vendor_request;
#define USB_CONFIG_STANDARD 0x1
#define RX_ENDPOINT_ADDRESS (LIBUSB_ENDPOINT_IN | 1)
#define TX_ENDPOINT_ADDRESS (LIBUSB_ENDPOINT_OUT | 2)
typedef enum {
HACKRF_TRANSCEIVER_MODE_OFF = 0,
HACKRF_TRANSCEIVER_MODE_RECEIVE = 1,
HACKRF_TRANSCEIVER_MODE_TRANSMIT = 2,
HACKRF_TRANSCEIVER_MODE_SS = 3,
TRANSCEIVER_MODE_CPLD_UPDATE = 4,
TRANSCEIVER_MODE_RX_SWEEP = 5,
} hackrf_transceiver_mode;
typedef enum {
HACKRF_HW_SYNC_MODE_OFF = 0,
HACKRF_HW_SYNC_MODE_ON = 1,
} hackrf_hw_sync_mode;
#define TRANSFER_COUNT 4
#define TRANSFER_BUFFER_SIZE 262144
#define USB_MAX_SERIAL_LENGTH 32
struct hackrf_device {
libusb_device_handle* usb_device;
uint16_t usb_api_version;
struct libusb_transfer** transfers;
hackrf_sample_block_cb_fn callback;
volatile bool
transfer_thread_started; /* volatile shared between threads (read only) */
pthread_t transfer_thread;
volatile bool streaming; /* volatile shared between threads (read only) */
void* rx_ctx;
void* tx_ctx;
volatile bool do_exit;
unsigned char buffer[TRANSFER_COUNT * TRANSFER_BUFFER_SIZE];
bool transfers_setup; /* true if the USB transfers have been setup */
pthread_mutex_t transfer_lock; /* must be held to cancel or restart transfers */
volatile int active_transfers; /* number of active transfers */
pthread_cond_t all_finished_cv; /* signalled when all transfers have finished */
bool flush;
struct libusb_transfer* flush_transfer;
hackrf_flush_cb_fn flush_callback;
hackrf_tx_block_complete_cb_fn tx_completion_callback;
void* flush_ctx;
uint32_t buffer_size;
};
typedef struct {
uint32_t bandwidth_hz;
} max2837_ft_t;
static const max2837_ft_t max2837_ft[] = {
{1750000},
{2500000},
{3500000},
{5000000},
{5500000},
{6000000},
{7000000},
{8000000},
{9000000},
{10000000},
{12000000},
{14000000},
{15000000},
{20000000},
{24000000},
{28000000},
{0}};
#define USB_API_REQUIRED(device, version) \
if (device->usb_api_version < version) \
return HACKRF_ERROR_USB_API_VERSION;
static const uint16_t hackrf_usb_vid = 0x1d50;
static const uint16_t hackrf_jawbreaker_usb_pid = 0x604b;
static const uint16_t hackrf_one_usb_pid = 0x6089;
static const uint16_t rad1o_usb_pid = 0xcc15;
static uint16_t open_devices = 0;
static int create_transfer_thread(hackrf_device* device);
static libusb_context* g_libusb_context = NULL;
int last_libusb_error = LIBUSB_SUCCESS;
/*
* Check if the transfers are setup and owned by libusb.
*
* Returns true if the device transfers are currently setup
* in libusb, false otherwise.
*/
static int transfers_check_setup(hackrf_device* device)
{
if ((device->transfers != NULL) && (device->transfers_setup == true))
return true;
return false;
}
/*
* Cancel any transfers that are in-flight.
*
* This cancels any transfers that hvae been given to libusb for
* either transmit or receive.
*
* This must be done whilst the libusb thread is running, as
* on some platforms cancelling transfers requires some work
* to be done inside the libusb thread to completely cancel
* pending transfers.
*
* Returns HACKRF_SUCCESS if OK, HACKRF_ERROR_OTHER if the
* transfers aren't currently setup.
*/
static int cancel_transfers(hackrf_device* device)
{
uint32_t transfer_index;
// If we're cancelling transfers for any reason, we're shutting down.
device->streaming = false;
if (transfers_check_setup(device) == true) {
// Take lock while cancelling transfers. This blocks the
// transfer completion callback from restarting a transfer
// while we're in the middle of trying to cancel them all.
pthread_mutex_lock(&device->transfer_lock);
for (transfer_index = 0; transfer_index < TRANSFER_COUNT;
transfer_index++) {
if (device->transfers[transfer_index] != NULL) {
libusb_cancel_transfer(device->transfers[transfer_index]);
}
}
if (device->flush_transfer != NULL)
libusb_cancel_transfer(device->flush_transfer);
device->transfers_setup = false;
device->flush = false;
// Now wait for the transfer thread to signal that all transfers
// have finished, either by completing or being fully cancelled.
while (device->active_transfers > 0) {
pthread_cond_wait(
&device->all_finished_cv,
&device->transfer_lock);
}
pthread_mutex_unlock(&device->transfer_lock);
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_OTHER;
}
}
static int free_transfers(hackrf_device* device)
{
uint32_t transfer_index;
if (device->transfers != NULL) {
// libusb_close() should free all transfers referenced from this array.
for (transfer_index = 0; transfer_index < TRANSFER_COUNT;
transfer_index++) {
if (device->transfers[transfer_index] != NULL) {
libusb_free_transfer(device->transfers[transfer_index]);
device->transfers[transfer_index] = NULL;
}
}
free(device->transfers);
device->transfers = NULL;
}
libusb_free_transfer(device->flush_transfer);
return HACKRF_SUCCESS;
}
static int allocate_transfers(hackrf_device* const device)
{
if (device->transfers == NULL) {
uint32_t transfer_index;
device->transfers = (struct libusb_transfer**) calloc(
TRANSFER_COUNT,
sizeof(struct libusb_transfer*));
if (device->transfers == NULL) {
return HACKRF_ERROR_NO_MEM;
}
memset(device->buffer, 0, TRANSFER_COUNT * TRANSFER_BUFFER_SIZE);
for (transfer_index = 0; transfer_index < TRANSFER_COUNT;
transfer_index++) {
device->transfers[transfer_index] = libusb_alloc_transfer(0);
if (device->transfers[transfer_index] == NULL) {
return HACKRF_ERROR_LIBUSB;
}
libusb_fill_bulk_transfer(
device->transfers[transfer_index],
device->usb_device,
0,
&device->buffer[transfer_index * TRANSFER_BUFFER_SIZE],
TRANSFER_BUFFER_SIZE,
NULL,
device,
0);
if (device->transfers[transfer_index]->buffer == NULL) {
return HACKRF_ERROR_NO_MEM;
}
}
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_BUSY;
}
}
static int prepare_transfers(
hackrf_device* device,
const uint_fast8_t endpoint_address,
libusb_transfer_cb_fn callback)
{
int error;
uint32_t transfer_index;
uint32_t ready_transfers = 0;
if (device->transfers == NULL) {
// This shouldn't happen.
return HACKRF_ERROR_OTHER;
}
// If setting up for TX, call the TX callback to fill each
// transfer buffer.
// All transfers must be filled before any are submitted.
// Otherwise a transfer might complete whilst the others are
// still being filled, causing the transfer thread to make a
// concurrent call to the TX callback.
// We also need to handle the case where the callback returns
// nonzero to indicate completion, so keep count of how many
// transfers were made ready to submit at this stage.
if (endpoint_address == TX_ENDPOINT_ADDRESS) {
for (transfer_index = 0; transfer_index < TRANSFER_COUNT;
transfer_index++) {
hackrf_transfer transfer = {
.device = device,
.buffer = device->transfers[transfer_index]->buffer,
.buffer_length = TRANSFER_BUFFER_SIZE,
.valid_length = TRANSFER_BUFFER_SIZE,
.rx_ctx = device->rx_ctx,
.tx_ctx = device->tx_ctx,
};
if ((device->callback(&transfer) == 0) &&
(transfer.valid_length > 0)) {
device->transfers[transfer_index]->length =
transfer.valid_length;
ready_transfers++;
} else {
break;
}
}
} else {
// For RX, all transfers are already ready for use.
ready_transfers = TRANSFER_COUNT;
}
// Now everything is ready, go ahead and submit the ready transfers. We must hold
// the transfer lock whilst doing this, so that completion callbacks cannot resubmit
// any transfers until all transfers have been initially submitted.
pthread_mutex_lock(&device->transfer_lock);
for (transfer_index = 0; transfer_index < ready_transfers; transfer_index++) {
struct libusb_transfer* transfer = device->transfers[transfer_index];
transfer->endpoint = endpoint_address;
transfer->callback = callback;
// Pad the size of a short transfer to the next 512-byte boundary.
if (endpoint_address == TX_ENDPOINT_ADDRESS) {
while (transfer->length % 512 != 0)
transfer->buffer[transfer->length++] = 0;
}
error = libusb_submit_transfer(transfer);
if (error != 0) {
last_libusb_error = error;
break;
}
device->active_transfers++;
}
if (error == 0) {
// We should only continue streaming if all transfers were made ready
// and submitted above. Otherwise, set streaming to false so that the
// libusb completion callback won't submit further transfers.
device->streaming = (ready_transfers == TRANSFER_COUNT);
device->transfers_setup = true;
// If we're not continuing streaming, follow up with a flush if needed.
if (!device->streaming && device->flush) {
error = libusb_submit_transfer(device->flush_transfer);
if (error != 0) {
last_libusb_error = error;
}
}
}
// Now we can release the transfer lock.
pthread_mutex_unlock(&device->transfer_lock);
if (error == 0) {
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_LIBUSB;
}
}
static int detach_kernel_drivers(libusb_device_handle* usb_device_handle)
{
int i, num_interfaces, result;
libusb_device* dev;
struct libusb_config_descriptor* config;
dev = libusb_get_device(usb_device_handle);
result = libusb_get_active_config_descriptor(dev, &config);
if (result < 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
num_interfaces = config->bNumInterfaces;
libusb_free_config_descriptor(config);
for (i = 0; i < num_interfaces; i++) {
result = libusb_kernel_driver_active(usb_device_handle, i);
if (result < 0) {
if (result == LIBUSB_ERROR_NOT_SUPPORTED) {
return 0;
}
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
} else if (result == 1) {
result = libusb_detach_kernel_driver(usb_device_handle, i);
if (result != 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
}
}
return HACKRF_SUCCESS;
}
static int set_hackrf_configuration(libusb_device_handle* usb_device, int config)
{
int result, curr_config;
result = libusb_get_configuration(usb_device, &curr_config);
if (result != 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
if (curr_config != config) {
result = detach_kernel_drivers(usb_device);
if (result != 0) {
return result;
}
result = libusb_set_configuration(usb_device, config);
if (result != 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
}
result = detach_kernel_drivers(usb_device);
if (result != 0) {
return result;
}
return LIBUSB_SUCCESS;
}
#ifdef __cplusplus
extern "C" {
#endif
int ADDCALL hackrf_init(void)
{
int libusb_error;
if (g_libusb_context != NULL) {
return HACKRF_SUCCESS;
}
libusb_error = libusb_init(&g_libusb_context);
if (libusb_error != 0) {
last_libusb_error = libusb_error;
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}
int ADDCALL hackrf_exit(void)
{
if (open_devices == 0) {
if (g_libusb_context != NULL) {
libusb_exit(g_libusb_context);
g_libusb_context = NULL;
}
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_NOT_LAST_DEVICE;
}
}
#ifndef LIBRARY_VERSION
#define LIBRARY_VERSION "unknown"
#endif
const char* ADDCALL hackrf_library_version()
{
return LIBRARY_VERSION;
}
#ifndef LIBRARY_RELEASE
#define LIBRARY_RELEASE "unknown"
#endif
const char* ADDCALL hackrf_library_release()
{
return LIBRARY_RELEASE;
}
hackrf_device_list_t* ADDCALL hackrf_device_list()
{
int i;
libusb_device_handle* usb_device = NULL;
uint8_t serial_descriptor_index;
char serial_number[64];
uint8_t idx, serial_number_length;
hackrf_device_list_t* list = calloc(1, sizeof(*list));
if (list == NULL)
return NULL;
list->usb_devicecount = (int) libusb_get_device_list(
g_libusb_context,
(libusb_device***) &list->usb_devices);
list->serial_numbers = calloc(list->usb_devicecount, sizeof(void*));
list->usb_board_ids =
calloc(list->usb_devicecount, sizeof(enum hackrf_usb_board_id));
list->usb_device_index = calloc(list->usb_devicecount, sizeof(int));
if (list->serial_numbers == NULL || list->usb_board_ids == NULL ||
list->usb_device_index == NULL) {
hackrf_device_list_free(list);
return NULL;
}
for (i = 0; i < list->usb_devicecount; i++) {
struct libusb_device_descriptor device_descriptor;
libusb_get_device_descriptor(list->usb_devices[i], &device_descriptor);
if (device_descriptor.idVendor == hackrf_usb_vid) {
if ((device_descriptor.idProduct == hackrf_one_usb_pid) ||
(device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) ||
(device_descriptor.idProduct == rad1o_usb_pid)) {
idx = list->devicecount++;
list->usb_board_ids[idx] = device_descriptor.idProduct;
list->usb_device_index[idx] = i;
serial_descriptor_index = device_descriptor.iSerialNumber;
if (serial_descriptor_index > 0) {
if (libusb_open(
list->usb_devices[i],
&usb_device) != 0) {
usb_device = NULL;
continue;
}
serial_number_length =
libusb_get_string_descriptor_ascii(
usb_device,
serial_descriptor_index,
(unsigned char*) serial_number,
sizeof(serial_number));
if (serial_number_length >= USB_MAX_SERIAL_LENGTH)
serial_number_length =
USB_MAX_SERIAL_LENGTH;
serial_number[serial_number_length] = 0;
list->serial_numbers[idx] = strdup(serial_number);
libusb_close(usb_device);
usb_device = NULL;
}
}
}
}
return list;
}
void ADDCALL hackrf_device_list_free(hackrf_device_list_t* list)
{
int i;
libusb_free_device_list((libusb_device**) list->usb_devices, 1);
for (i = 0; i < list->devicecount; i++) {
if (list->serial_numbers[i])
free(list->serial_numbers[i]);
}
free(list->serial_numbers);
free(list->usb_board_ids);
free(list->usb_device_index);
free(list);
}
libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number)
{
libusb_device_handle* usb_device = NULL;
libusb_device** devices = NULL;
const ssize_t list_length = libusb_get_device_list(g_libusb_context, &devices);
ssize_t match_len = 0;
ssize_t i;
char serial_number[64];
int serial_number_length;
if (desired_serial_number) {
/* If a shorter serial number is specified, only match against the suffix.
* Should probably complain if the match is not unique, currently doesn't.
*/
match_len = strlen(desired_serial_number);
if (match_len > 32)
return NULL;
}
for (i = 0; i < list_length; i++) {
struct libusb_device_descriptor device_descriptor;
libusb_get_device_descriptor(devices[i], &device_descriptor);
if (device_descriptor.idVendor == hackrf_usb_vid) {
if ((device_descriptor.idProduct == hackrf_one_usb_pid) ||
(device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) ||
(device_descriptor.idProduct == rad1o_usb_pid)) {
if (desired_serial_number != NULL) {
const uint_fast8_t serial_descriptor_index =
device_descriptor.iSerialNumber;
if (serial_descriptor_index > 0) {
if (libusb_open(
devices[i],
&usb_device) != 0) {
usb_device = NULL;
continue;
}
serial_number_length =
libusb_get_string_descriptor_ascii(
usb_device,
serial_descriptor_index,
(unsigned char*)
serial_number,
sizeof(serial_number));
if (serial_number_length >=
USB_MAX_SERIAL_LENGTH)
serial_number_length =
USB_MAX_SERIAL_LENGTH;
serial_number[serial_number_length] = 0;
if (strncmp(serial_number +
serial_number_length -
match_len,
desired_serial_number,
match_len) == 0) {
break;
} else {
libusb_close(usb_device);
usb_device = NULL;
}
}
} else {
libusb_open(devices[i], &usb_device);
break;
}
}
}
}
libusb_free_device_list(devices, 1);
return usb_device;
}
static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** device)
{
int result;
hackrf_device* lib_device;
uint32_t buffer_size;
struct libusb_device_descriptor device_descriptor;
libusb_device* dev = libusb_get_device(usb_device);
result = libusb_get_device_descriptor(dev, &device_descriptor);
if (result < 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
//int speed = libusb_get_device_speed(usb_device);
// TODO: Error or warning if not high speed USB?
result = set_hackrf_configuration(usb_device, USB_CONFIG_STANDARD);
if (result != LIBUSB_SUCCESS) {
libusb_close(usb_device);
return result;
}
result = libusb_claim_interface(usb_device, 0);
if (result != LIBUSB_SUCCESS) {
last_libusb_error = result;
libusb_close(usb_device);
return HACKRF_ERROR_LIBUSB;
}
lib_device = NULL;
lib_device = (hackrf_device*) calloc(1, sizeof(*lib_device));
if (lib_device == NULL) {
libusb_release_interface(usb_device, 0);
libusb_close(usb_device);
return HACKRF_ERROR_NO_MEM;
}
#if LIBUSB_API_VERSION >= 0x0100010C
// WinUSB: Use RAW_IO to improve throughput on RX
if (libusb_endpoint_supports_raw_io(usb_device, RX_ENDPOINT_ADDRESS) == 1) {
libusb_endpoint_set_raw_io(usb_device, RX_ENDPOINT_ADDRESS, 1);
}
#endif
lib_device->usb_device = usb_device;
lib_device->usb_api_version = device_descriptor.bcdDevice;
lib_device->transfers = NULL;
lib_device->callback = NULL;
lib_device->transfer_thread_started = false;
lib_device->streaming = false;
lib_device->do_exit = false;
lib_device->active_transfers = 0;
lib_device->flush = false;
lib_device->flush_transfer = NULL;
lib_device->flush_callback = NULL;
lib_device->flush_ctx = NULL;
lib_device->tx_completion_callback = NULL;
if (lib_device->usb_api_version >= 0x0112) {
// Fetch buffer size from device so we know how many bytes to flush TX with.
result = libusb_control_transfer(
lib_device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE,
0,
0,
(unsigned char*) &buffer_size,
4,
DEFAULT_REQUEST_TIMEOUT);
if (result < 4) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
lib_device->buffer_size = FROM_LE32(buffer_size);
} else {
// All older firmware uses a fixed 32KB buffer size.
lib_device->buffer_size = 32768;
}
result = pthread_mutex_init(&lib_device->transfer_lock, NULL);
if (result != 0) {
free(lib_device);
libusb_release_interface(usb_device, 0);
libusb_close(usb_device);
return HACKRF_ERROR_THREAD;
}
result = pthread_cond_init(&lib_device->all_finished_cv, NULL);
if (result != 0) {
free(lib_device);
libusb_release_interface(usb_device, 0);
libusb_close(usb_device);
return HACKRF_ERROR_THREAD;
}
result = allocate_transfers(lib_device);
if (result != 0) {
free(lib_device);
libusb_release_interface(usb_device, 0);
libusb_close(usb_device);
return HACKRF_ERROR_NO_MEM;
}
result = create_transfer_thread(lib_device);
if (result != 0) {
free(lib_device);
libusb_release_interface(usb_device, 0);
libusb_close(usb_device);
return result;
}
*device = lib_device;
open_devices++;
return HACKRF_SUCCESS;
}
int ADDCALL hackrf_open(hackrf_device** device)
{
libusb_device_handle* usb_device;
if (device == NULL) {
return HACKRF_ERROR_INVALID_PARAM;
}
usb_device = libusb_open_device_with_vid_pid(
g_libusb_context,
hackrf_usb_vid,
hackrf_one_usb_pid);
if (usb_device == NULL) {
usb_device = libusb_open_device_with_vid_pid(
g_libusb_context,
hackrf_usb_vid,
hackrf_jawbreaker_usb_pid);
}
if (usb_device == NULL) {
usb_device = libusb_open_device_with_vid_pid(
g_libusb_context,
hackrf_usb_vid,
rad1o_usb_pid);
}
if (usb_device == NULL) {
return HACKRF_ERROR_NOT_FOUND;
}
return hackrf_open_setup(usb_device, device);
}
int ADDCALL hackrf_open_by_serial(
const char* const desired_serial_number,
hackrf_device** device)
{
libusb_device_handle* usb_device;
if (desired_serial_number == NULL) {
return hackrf_open(device);
}
if (device == NULL) {
return HACKRF_ERROR_INVALID_PARAM;
}
usb_device = hackrf_open_usb(desired_serial_number);
if (usb_device == NULL) {
return HACKRF_ERROR_NOT_FOUND;
}
return hackrf_open_setup(usb_device, device);
}
int ADDCALL hackrf_device_list_open(
hackrf_device_list_t* list,
int idx,
hackrf_device** device)
{
libusb_device_handle* usb_device;
int i, result;
if (device == NULL || list == NULL || idx < 0 || idx >= list->devicecount) {
return HACKRF_ERROR_INVALID_PARAM;
}
i = list->usb_device_index[idx];
result = libusb_open(list->usb_devices[i], &usb_device);
if (result != 0) {
usb_device = NULL;
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
}
return hackrf_open_setup(usb_device, device);
}
int ADDCALL hackrf_device_list_bus_sharing(hackrf_device_list_t* list, int idx)
{
libusb_device *usb_dev, *hackrf_dev;
uint8_t hackrf_bus;
int other_device_count = 0;
int i;
if (list == NULL || list->usb_devices == NULL || list->usb_device_index == NULL ||
idx < 0 || idx > list->devicecount) {
return HACKRF_ERROR_INVALID_PARAM;
}
hackrf_dev = list->usb_devices[list->usb_device_index[idx]];
hackrf_bus = libusb_get_bus_number(hackrf_dev);
for (i = 0; i < list->usb_devicecount; i++) {
usb_dev = (libusb_device*) list->usb_devices[i];
// Don't count the HackRF, devices on other buses, or the root hub.
if (usb_dev != hackrf_dev &&
libusb_get_bus_number(usb_dev) == hackrf_bus &&
libusb_get_parent(usb_dev) != NULL) {
other_device_count++;
}
}
return other_device_count;
}
int ADDCALL hackrf_set_transceiver_mode(
hackrf_device* device,
hackrf_transceiver_mode value)
{
int result;
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_SET_TRANSCEIVER_MODE,
value,
0,
NULL,
0,
DEFAULT_REQUEST_TIMEOUT);
if (result != 0) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}
int ADDCALL hackrf_max2837_read(
hackrf_device* device,
uint8_t register_number,
uint16_t* value)
{
int result;
if (register_number >= 32) {
return HACKRF_ERROR_INVALID_PARAM;
}
result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_MAX283X_READ,
0,
register_number,
(unsigned char*) value,
2,
DEFAULT_REQUEST_TIMEOUT);
if (result < 2) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}
int ADDCALL hackrf_max2831_read(
hackrf_device* device,
uint8_t register_number,
uint16_t* value)
{
int result;