Debian Codegraph @

libairspyhf/src/airspyhf.c

raw symbols ◀ ▶
Source reconstructed at patch state 15 of 23 — not the installed (fully patched) source.
23 Debian patches touch this file
1 /*
2 Copyright (c) 2013-2018, Youssef Touil <[email protected]>
3 Copyright (c) 2013-2017, Ian Gilmour <[email protected]>
4 Copyright (c) 2013-2017, Benjamin Vernoux <[email protected]>
5 Copyright (c) 2013, Michael Ossmann <[email protected]>
6 Copyright (c) 2012, Jared Boone <[email protected]>
7
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
11
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 Neither the name of Airspy HF+ nor the names of its contributors may be used to endorse or promote products derived from this software
16 without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifdef _WIN32
27 #define HAVE_STRUCT_TIMESPEC
28 #define _CRT_SECURE_NO_WARNINGS
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <libusb.h>
35 #include <pthread.h>
36 #include <math.h>
37
38 #include "iqbalancer.h"
39 #include "airspyhf.h"
40 #include "airspyhf_commands.h"
41
42 #ifndef bool
43 typedef int bool;
44 #define true 1
45 #define false 0
46 #endif
47
48 #ifndef M_PI
49 #define M_PI (3.14159265359)
50 #endif
51
52 #define MAX(a,b) ((a) > (b) ? a : b)
53 #define MIN(a,b) ((a) < (b) ? a : b)
54
55 #define SAMPLES_TO_TRANSFER (1024 * 2)
56 #define SERIAL_NUMBER_UNUSED (0)
57 #define FILE_DESCRIPTOR_UNUSED (-1)
58 #define RAW_BUFFER_COUNT (8)
59 #define AIRSPYHF_SERIAL_SIZE (28)
60
61 #define MAX_SAMPLERATE_INDEX (100)
62 #define DEFAULT_SAMPLERATE (768000)
63
64 #define CALIBRATION_MAGIC (0xA5CA71B0)
65
66 #define DEFAULT_IF_SHIFT (5000)
67 #define MIN_ZERO_IF_LO (180)
68 #define MIN_LOW_IF_LO (84)
69
70 #define STR_PREFIX_SERIAL_AIRSPYHF_SIZE (12)
71 static const char str_prefix_serial_airspyhf[STR_PREFIX_SERIAL_AIRSPYHF_SIZE] =
72 { 'A', 'I', 'R', 'S', 'P', 'Y', 'H', 'F', ' ', 'S', 'N', ':' };
73
74 #define INITIAL_PHASE (0.00006f)
75 #define INITIAL_AMPLITUDE (-0.0045f)
76
77 #pragma pack(push,1)
78
79 typedef struct {
80 int16_t im;
81 int16_t re;
82 } airspyhf_complex_int16_t;
83
84 #pragma pack(pop)
85
86 typedef struct airspyhf_device
87 {
88 libusb_context* usb_context;
89 libusb_device_handle* usb_device;
90 struct libusb_transfer** transfers;
91 airspyhf_sample_block_cb_fn callback;
92 pthread_t transfer_thread;
93 pthread_t consumer_thread;
94 pthread_cond_t consumer_cv;
95 pthread_mutex_t consumer_mp;
96 uint32_t supported_samplerate_count;
97 uint32_t *supported_samplerates;
98 uint8_t *samplerate_architectures;
99 volatile uint32_t current_samplerate;
100 volatile uint32_t freq_hz;
101 volatile uint32_t freq_khz;
102 volatile int32_t freq_shift;
103 volatile int32_t calibration_ppb;
104 volatile float optimal_point;
105 uint8_t enable_dsp;
106 uint8_t is_low_if;
107 float filter_gain;
108 airspyhf_complex_float_t vec;
109 struct iq_balancer_t *iq_balancer;
110 uint32_t transfer_count;
111 int32_t transfer_live;
112 uint32_t buffer_size;
113 uint32_t dropped_buffers;
114 uint32_t dropped_buffers_queue[RAW_BUFFER_COUNT];
115 airspyhf_complex_int16_t *received_samples_queue[RAW_BUFFER_COUNT];
116 volatile bool streaming;
117 volatile bool stop_requested;
118 volatile int received_samples_queue_head;
119 volatile int received_samples_queue_tail;
120 volatile int received_buffer_count;
121 airspyhf_complex_float_t *output_buffer;
122 void* ctx;
123 } airspyhf_device_t;
124
125 typedef struct calibration_record
126 {
127 uint32_t magic_number;
128 int32_t calibration_ppb;
129 } calibration_record_t;
130
131 static const uint16_t airspyhf_usb_vid = 0x03EB;
132 static const uint16_t airspyhf_usb_pid = 0x800C;
133
134 static int airspyhf_config_read(airspyhf_device_t* device, uint8_t *buffer, uint16_t length);
135
136 static int cancel_transfers(airspyhf_device_t* device)
137 {
138 uint32_t transfer_index;
139 struct timeval canceltv = { 0, 50 };
140
141 if (device->transfers != NULL)
142 {
143 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
144 {
145 if (device->transfers[transfer_index] != NULL)
146 {
147 libusb_cancel_transfer(device->transfers[transfer_index]);
148 }
149 }
150
151 for (transfer_index = 0; transfer_index < device->transfer_count && device->transfer_live > 0; transfer_index++)
152 libusb_handle_events_timeout_completed(device->usb_context, &canceltv, NULL);
153
154 return AIRSPYHF_SUCCESS;
155 }
156 else
157 {
158 return AIRSPYHF_ERROR;
159 }
160 }
161
162 static int free_transfers(airspyhf_device_t* device)
163 {
164 int i;
165 uint32_t transfer_index;
166
167 if (device->transfers != NULL)
168 {
169 free(device->output_buffer);
170 device->output_buffer = NULL;
171
172 for (transfer_index = 0; transfer_index < device->transfer_count; transfer_index++)
173 {
174 if (device->transfers[transfer_index] != NULL)
175 {
176 free(device->transfers[transfer_index]->buffer);
177 libusb_free_transfer(device->transfers[transfer_index]);
178 device->transfers[transfer_index] = NULL;
179 }
180 }
181 free(device->transfers);
182 device->transfers = NULL;
183
184 for (i = 0; i < RAW_BUFFER_COUNT; i++)
185 {
186 if (device->received_samples_queue[i] != NULL)
187 {
188 free(device->received_samples_queue[i]);
189 device->received_samples_queue[i] = NULL;
190 }
191 }
192 }
193
194 return AIRSPYHF_SUCCESS;
195 }
196
197 static int allocate_transfers(airspyhf_device_t* const device)
198 {
199 int i;
200 uint32_t transfer_index;
201
202 if (device->transfers == NULL)
203 {
204 device->output_buffer = (airspyhf_complex_float_t *) malloc((device->buffer_size / sizeof(airspyhf_complex_int16_t)) * sizeof(airspyhf_complex_float_t));
205
206 for (i = 0; i < RAW_BUFFER_COUNT; i++)
207 {
208 device->received_samples_queue[i] = (airspyhf_complex_int16_t *) malloc(device->buffer_size);
209 if (device->received_samples_queue[i] == NULL)
210 {
211 return AIRSPYHF_ERROR;
212 }
213
214 memset(device->received_samples_queue[i], 0, device->buffer_size);
215 }
216
217 device->transfers = (struct libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer));
218 if (device->transfers == NULL)
219 {
220 return AIRSPYHF_ERROR;
221 }
222
223 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
224 {
225 device->transfers[transfer_index] = libusb_alloc_transfer(0);
226 if (device->transfers[transfer_index] == NULL)
227 {
228 return AIRSPYHF_ERROR;
229 }
230
231 libusb_fill_bulk_transfer(
232 device->transfers[transfer_index],
233 device->usb_device,
234 0,
235 (unsigned char*) malloc(device->buffer_size),
236 device->buffer_size,
237 NULL,
238 device,
239 0);
240
241 if (device->transfers[transfer_index]->buffer == NULL)
242 {
243 return AIRSPYHF_ERROR;
244 }
245 }
246 return AIRSPYHF_SUCCESS;
247 }
248 else
249 {
250 return AIRSPYHF_ERROR;
251 }
252 }
253
254 static int prepare_transfers(airspyhf_device_t* device, const uint_fast8_t endpoint_address, libusb_transfer_cb_fn callback)
255 {
256 int error;
257 uint32_t transfer_index;
258
259 if (device->transfers != NULL)
260 {
261 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
262 {
263 device->transfers[transfer_index]->endpoint = endpoint_address;
264 device->transfers[transfer_index]->callback = callback;
265
266 error = libusb_submit_transfer(device->transfers[transfer_index]);
267 if (error != 0)
268 {
269 return AIRSPYHF_ERROR;
270 }
271 else
272 device->transfer_live++;
273 }
274 return AIRSPYHF_SUCCESS;
275 }
276
277 return AIRSPYHF_ERROR;
278 }
279
280 static void multiply_complex_complex(airspyhf_complex_float_t *a, const airspyhf_complex_float_t *b)
281 {
282 float re = a->re * b->re - a->im * b->im;
283 a->im = a->im * b->re + a->re * b->im;
284 a->re = re;
285 }
286
287 static void multiply_complex_real(airspyhf_complex_float_t *a, const float b)
288 {
289 a->re *= b;
290 a->im *= b;
291 }
292
293 static inline void rotate_complex(airspyhf_complex_float_t *vec, const airspyhf_complex_float_t *rot)
294 {
295 multiply_complex_complex(vec, rot);
296
297 float norm = 1.99f - (vec->re * vec->re + vec->im * vec->im);
298
299 multiply_complex_real(vec, norm);
300 }
301
302 static void convert_samples(airspyhf_device_t* device, airspyhf_complex_int16_t *src, airspyhf_complex_float_t *dest, int count)
303 {
304 const float scale = 1.0f / 32768;
305
306 int i;
307 airspyhf_complex_float_t vec;
308 airspyhf_complex_float_t rot;
309 double angle;
310 float conversion_gain;
311
312 conversion_gain = scale * device->filter_gain;
313
314 for (i = 0; i < count; i++)
315 {
316 dest[i].re = src[i].re * conversion_gain;
317 dest[i].im = src[i].im * conversion_gain;
318 }
319
320 if (device->enable_dsp)
321 {
322 if (!device->is_low_if)
323 {
324 // Zero IF requires external IQ correction
325 iq_balancer_process(device->iq_balancer, dest, count);
326 }
327
328 // Fine tuning
329 if (device->freq_shift != 0)
330 {
331 angle = 2.0 * M_PI * device->freq_shift / (double) device->current_samplerate;
332
333 vec = device->vec;
334
335 rot.re = (float) cos(angle);
336 rot.im = (float) -sin(angle);
337
338 for (i = 0; i < count; i++)
339 {
340 rotate_complex(&vec, &rot);
341 multiply_complex_complex(&dest[i], &vec);
342 }
343
344 device->vec = vec;
345 }
346 }
347 }
348
349 static void* consumer_threadproc(void *arg)
350 {
351 int sample_count;
352 airspyhf_complex_int16_t *input_samples;
353 uint32_t dropped_buffers;
354 airspyhf_device_t* device = (airspyhf_device_t*) arg;
355 airspyhf_transfer_t transfer;
356
357 #ifdef _WIN32
358
359 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
360
361 #endif
362
363 pthread_mutex_lock(&device->consumer_mp);
364
365 while (device->streaming && !device->stop_requested)
366 {
367 while (device->received_buffer_count == 0 && device->streaming && !device->stop_requested)
368 {
369 pthread_cond_wait(&device->consumer_cv, &device->consumer_mp);
370 }
371 if (!device->streaming || device->stop_requested)
372 {
373 break;
374 }
375
376 input_samples = (airspyhf_complex_int16_t *) device->received_samples_queue[device->received_samples_queue_tail];
377 dropped_buffers = device->dropped_buffers_queue[device->received_samples_queue_tail];
378 device->received_samples_queue_tail = (device->received_samples_queue_tail + 1) & (RAW_BUFFER_COUNT - 1);
379
380 pthread_mutex_unlock(&device->consumer_mp);
381
382 sample_count = device->buffer_size / sizeof(airspyhf_complex_int16_t);
383
384 convert_samples(device, input_samples, device->output_buffer, sample_count);
385
386 transfer.device = device;
387 transfer.ctx = device->ctx;
388 transfer.samples = device->output_buffer;
389 transfer.sample_count = sample_count;
390 transfer.dropped_samples = (uint64_t) dropped_buffers * (uint64_t) sample_count;
391
392 if (device->callback(&transfer) != 0)
393 {
394 device->stop_requested = true;
395 }
396
397 pthread_mutex_lock(&device->consumer_mp);
398 device->received_buffer_count--;
399 }
400
401 pthread_mutex_unlock(&device->consumer_mp);
402
403 pthread_exit(NULL);
404
405 return NULL;
406 }
407
408 static void LIBUSB_CALL airspyhf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
409 {
410 airspyhf_complex_int16_t *temp;
411 airspyhf_device_t* device = (airspyhf_device_t*) usb_transfer->user_data;
412
413 device->transfer_live--;
414 if (!device->streaming || device->stop_requested)
415 {
416 return;
417 }
418
419 if (usb_transfer->status == LIBUSB_TRANSFER_COMPLETED && usb_transfer->actual_length == usb_transfer->length)
420 {
421 pthread_mutex_lock(&device->consumer_mp);
422
423 if (device->received_buffer_count < RAW_BUFFER_COUNT)
424 {
425 temp = device->received_samples_queue[device->received_samples_queue_head];
426 device->received_samples_queue[device->received_samples_queue_head] = (airspyhf_complex_int16_t *) usb_transfer->buffer;
427 usb_transfer->buffer = (uint8_t *) temp;
428
429 device->dropped_buffers_queue[device->received_samples_queue_head] = device->dropped_buffers;
430 device->dropped_buffers = 0;
431
432 device->received_samples_queue_head = (device->received_samples_queue_head + 1) & (RAW_BUFFER_COUNT - 1);
433 device->received_buffer_count++;
434
435 pthread_cond_signal(&device->consumer_cv);
436 }
437 else
438 {
439 device->dropped_buffers++;
440 }
441
442 pthread_mutex_unlock(&device->consumer_mp);
443
444 if (libusb_submit_transfer(usb_transfer) != 0)
445 {
446 device->stop_requested = true;
447 }
448 else
449 device->transfer_live ++;
450 }
451 else
452 {
453 device->stop_requested = true;
454 }
455 }
456
457 static void* transfer_threadproc(void* arg)
458 {
459 airspyhf_device_t* device = (struct airspyhf_device*) arg;
460 int error;
461 struct timeval timeout = { 0, 500000 };
462
463 #ifdef _WIN32
464
465 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
466
467 #endif
468
469 while (device->streaming && !device->stop_requested)
470 {
471 error = libusb_handle_events_timeout_completed(device->usb_context, &timeout, NULL);
472 if (error < 0)
473 {
474 if (error != LIBUSB_ERROR_INTERRUPTED)
475 device->streaming = false;
476 }
477 }
478
479 pthread_exit(NULL);
480
481 return NULL;
482 }
483
484 static int kill_io_threads(airspyhf_device_t* device)
485 {
486 if (device->streaming)
487 {
488 device->stop_requested = true;
489 cancel_transfers(device);
490
491 pthread_mutex_lock(&device->consumer_mp);
492 pthread_cond_signal(&device->consumer_cv);
493 pthread_mutex_unlock(&device->consumer_mp);
494
495 pthread_join(device->transfer_thread, NULL);
496 pthread_join(device->consumer_thread, NULL);
497
498 device->stop_requested = false;
499 device->streaming = false;
500 }
501
502 return AIRSPYHF_SUCCESS;
503 }
504
505 static int create_io_threads(airspyhf_device_t* device, airspyhf_sample_block_cb_fn callback)
506 {
507 int result;
508 pthread_attr_t attr;
509
510 if (!device->streaming && !device->stop_requested)
511 {
512 device->callback = callback;
513 device->streaming = true;
514
515 result = prepare_transfers(device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN, (libusb_transfer_cb_fn)airspyhf_libusb_transfer_callback);
516 if (result != AIRSPYHF_SUCCESS)
517 {
518 return result;
519 }
520
521 device->received_samples_queue_head = 0;
522 device->received_samples_queue_tail = 0;
523 device->received_buffer_count = 0;
524
525 pthread_attr_init(&attr);
526 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
527
528 result = pthread_create(&device->consumer_thread, &attr, consumer_threadproc, device);
529 if (result != 0)
530 {
531 return AIRSPYHF_ERROR;
532 }
533
534 result = pthread_create(&device->transfer_thread, &attr, transfer_threadproc, device);
535 if (result != 0)
536 {
537 return AIRSPYHF_ERROR;
538 }
539
540 pthread_attr_destroy(&attr);
541 }
542 else {
543 return AIRSPYHF_ERROR;
544 }
545
546 return AIRSPYHF_SUCCESS;
547 }
548
549 static void airspyhf_open_exit(airspyhf_device_t* device)
550 {
551 if (device->usb_device != NULL)
552 {
553 libusb_release_interface(device->usb_device, 0);
554 libusb_close(device->usb_device);
555 device->usb_device = NULL;
556 }
557 libusb_exit(device->usb_context);
558 device->usb_context = NULL;
559 }
560
561 static int airspyhf_read_samplerates_from_fw(airspyhf_device_t* device, uint32_t* buffer, const uint32_t len)
562 {
563 int result;
564
565 result = libusb_control_transfer(
566 device->usb_device,
567 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
568 AIRSPYHF_GET_SAMPLERATES,
569 0,
570 len,
571 (unsigned char*) buffer,
572 (len > 0 ? len : 1) * (int16_t) sizeof(uint32_t),
573 0);
574
575 if (result < 1)
576 {
577 return AIRSPYHF_ERROR;
578 }
579 return AIRSPYHF_SUCCESS;
580 }
581
582 static int airspyhf_read_samplerate_architectures_from_fw(airspyhf_device_t* device, uint8_t* buffer, const uint32_t len)
583 {
584 int result;
585
586 result = libusb_control_transfer(
587 device->usb_device,
588 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
589 AIRSPYHF_GET_SAMPLERATE_ARCHITECTURES,
590 0,
591 len,
592 (unsigned char*) buffer,
593 (len > 0 ? len : 1) * (int16_t) sizeof(uint32_t),
594 0);
595
596 if (result < 1)
597 {
598 return AIRSPYHF_ERROR;
599 }
600
601 return AIRSPYHF_SUCCESS;
602 }
603
604
605 static void airspyhf_open_device(airspyhf_device_t* device,
606 int* ret,
607 uint16_t vid,
608 uint16_t pid,
609 uint64_t serial_number_val)
610 {
611 int i;
612 int result;
613 libusb_device_handle** libusb_dev_handle;
614 int serial_number_len;
615 libusb_device_handle* dev_handle;
616 libusb_device *dev;
617 libusb_device** devices = NULL;
618
619 ssize_t cnt;
620 int serial_descriptor_index;
621 struct libusb_device_descriptor device_descriptor;
622 unsigned char serial_number[AIRSPYHF_SERIAL_SIZE + 1];
623
624 libusb_dev_handle = &device->usb_device;
625 *libusb_dev_handle = NULL;
626
627 cnt = libusb_get_device_list(device->usb_context, &devices);
628 if (cnt < 0)
629 {
630 *ret = AIRSPYHF_ERROR;
631 return;
632 }
633
634 i = 0;
635 while ((dev = devices[i++]) != NULL)
636 {
637 libusb_get_device_descriptor(dev, &device_descriptor);
638
639 if ((device_descriptor.idVendor == vid) &&
640 (device_descriptor.idProduct == pid))
641 {
642 if (serial_number_val != SERIAL_NUMBER_UNUSED)
643 {
644 serial_descriptor_index = device_descriptor.iSerialNumber;
645 if (serial_descriptor_index > 0)
646 {
647 if (libusb_open(dev, libusb_dev_handle) != 0)
648 {
649 *libusb_dev_handle = NULL;
650 continue;
651 }
652 dev_handle = *libusb_dev_handle;
653 serial_number_len = libusb_get_string_descriptor_ascii(dev_handle,
654 serial_descriptor_index,
655 serial_number,
656 sizeof(serial_number));
657
658 if (serial_number_len == AIRSPYHF_SERIAL_SIZE && !memcmp(str_prefix_serial_airspyhf, serial_number, STR_PREFIX_SERIAL_AIRSPYHF_SIZE))
659 {
660 uint64_t serial = SERIAL_NUMBER_UNUSED;
661 // use same code to determine device's serial number as in airspyhf_list_devices()
662 {
663 char *start, *end;
664 serial_number[AIRSPYHF_SERIAL_SIZE] = 0;
665 start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPYHF_SIZE);
666 end = NULL;
667 serial = strtoull(start, &end, 16);
668 }
669 if (serial == serial_number_val)
670 {
671 result = libusb_set_configuration(dev_handle, 1);
672 if (result != 0)
673 {
674 libusb_close(dev_handle);
675 *libusb_dev_handle = NULL;
676 continue;
677 }
678 result = libusb_claim_interface(dev_handle, 0);
679 if (result != 0)
680 {
681 libusb_close(dev_handle);
682 *libusb_dev_handle = NULL;
683 continue;
684 }
685
686 result = libusb_set_interface_alt_setting(dev_handle, 0, 1);
687 if (result != 0)
688 {
689 libusb_close(dev_handle);
690 *libusb_dev_handle = NULL;
691 continue;
692 }
693
694 break;
695 }
696 else
697 {
698 libusb_close(dev_handle);
699 *libusb_dev_handle = NULL;
700 continue;
701 }
702 }
703 else
704 {
705 libusb_close(dev_handle);
706 *libusb_dev_handle = NULL;
707 continue;
708 }
709 }
710 }
711 else
712 {
713 if (libusb_open(dev, libusb_dev_handle) == 0)
714 {
715 dev_handle = *libusb_dev_handle;
716 result = libusb_set_configuration(dev_handle, 1);
717 if (result != 0)
718 {
719 libusb_close(dev_handle);
720 *libusb_dev_handle = NULL;
721 continue;
722 }
723 result = libusb_claim_interface(dev_handle, 0);
724 if (result != 0)
725 {
726 libusb_close(dev_handle);
727 *libusb_dev_handle = NULL;
728 continue;
729 }
730
731 result = libusb_set_interface_alt_setting(dev_handle, 0, 1);
732 if (result != 0)
733 {
734 libusb_close(dev_handle);
735 *libusb_dev_handle = NULL;
736 continue;
737 }
738 break;
739 }
740 }
741 }
742 }
743 libusb_free_device_list(devices, 1);
744
745 dev_handle = device->usb_device;
746 if (dev_handle == NULL)
747 {
748 *ret = AIRSPYHF_ERROR;
749 return;
750 }
751
752 *ret = AIRSPYHF_SUCCESS;
753 return;
754 }
755
756 static void airspyhf_open_device_fd(airspyhf_device_t* device,
757 int* ret,
758 int fd) {
759 int result;
760
761 #ifndef _WIN32
762 result = libusb_wrap_sys_device(device->usb_context, (intptr_t)fd, &device->usb_device);
763 #else
764 device->usb_device = NULL;
765 *ret = AIRSPYHF_UNSUPPORTED;
766 return;
767 #endif
768
769 if (result != 0 || device->usb_device == NULL)
770 {
771 *ret = AIRSPYHF_ERROR;
772 return;
773 }
774
775 result = libusb_set_configuration(device->usb_device, 1);
776 if (result != 0)
777 {
778 libusb_close(device->usb_device);
779 device->usb_device = NULL;
780 *ret = AIRSPYHF_ERROR;
781 return;
782 }
783
784 result = libusb_claim_interface(device->usb_device, 0);
785 if (result != 0)
786 {
787 libusb_close(device->usb_device);
788 device->usb_device = NULL;
789 *ret = AIRSPYHF_ERROR;
790 return;
791 }
792
793 result = libusb_set_interface_alt_setting(device->usb_device, 0, 1);
794 if (result != 0)
795 {
796 libusb_close(device->usb_device);
797 device->usb_device = NULL;
798 *ret = AIRSPYHF_ERROR;
799 return;
800 }
801
802 *ret = AIRSPYHF_SUCCESS;
803 return;
804 }
805
806 int airspyhf_list_devices(uint64_t *serials, int count)
807 {
808 libusb_device_handle* libusb_dev_handle;
809 struct libusb_context *context;
810 libusb_device** devices = NULL;
811 libusb_device *dev;
812 struct libusb_device_descriptor device_descriptor;
813
814 int serial_descriptor_index;
815 int serial_number_len;
816 int output_count;
817 int i;
818 unsigned char serial_number[AIRSPYHF_SERIAL_SIZE + 1];
819
820 if (serials)
821 {
822 memset(serials, 0, sizeof(uint64_t) * count);
823 }
824
825 #ifdef __ANDROID__
826 // LibUSB does not support device discovery on android
827 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
828 #endif
829
830 if (libusb_init(&context) != 0)
831 {
832 return AIRSPYHF_ERROR;
833 }
834
835 if (libusb_get_device_list(context, &devices) < 0)
836 {
837 return AIRSPYHF_ERROR;
838 }
839
840 i = 0;
841 output_count = 0;
842 while ((dev = devices[i++]) != NULL && (!serials || output_count < count))
843 {
844 libusb_get_device_descriptor(dev, &device_descriptor);
845
846 if ((device_descriptor.idVendor == airspyhf_usb_vid) &&
847 (device_descriptor.idProduct == airspyhf_usb_pid))
848 {
849 serial_descriptor_index = device_descriptor.iSerialNumber;
850 if (serial_descriptor_index > 0)
851 {
852 if (libusb_open(dev, &libusb_dev_handle) != 0)
853 {
854 continue;
855 }
856
857 serial_number_len = libusb_get_string_descriptor_ascii(libusb_dev_handle,
858 serial_descriptor_index,
859 serial_number,
860 sizeof(serial_number));
861
862 if (serial_number_len == AIRSPYHF_SERIAL_SIZE && !memcmp(str_prefix_serial_airspyhf, serial_number, STR_PREFIX_SERIAL_AIRSPYHF_SIZE))
863 {
864 char *start, *end;
865 uint64_t serial;
866
867 serial_number[AIRSPYHF_SERIAL_SIZE] = 0;
868 start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPYHF_SIZE);
869 end = NULL;
870 serial = strtoull(start, &end, 16);
871 if (serial == 0 && start == end)
872 {
873 libusb_close(libusb_dev_handle);
874 continue;
875 }
876
877 if (serials)
878 {
879 serials[output_count] = serial;
880 }
881 output_count++;
882 }
883
884 libusb_close(libusb_dev_handle);
885 }
886 }
887 }
888
889 libusb_free_device_list(devices, 1);
890 libusb_exit(context);
891 return output_count;
892 }
893
894 static int airspyhf_open_init(airspyhf_device_t** device, uint64_t serial_number, int fd)
895 {
896 airspyhf_device_t* lib_device;
897 calibration_record_t record;
898 int libusb_error;
899 int result;
900
901 *device = NULL;
902
903 lib_device = (airspyhf_device_t*) calloc(1, sizeof(airspyhf_device_t));
904 if (lib_device == NULL)
905 {
906 return AIRSPYHF_ERROR;
907 }
908
909 #ifdef __ANDROID__
910 // LibUSB does not support device discovery on android
911 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
912 #endif
913
914 libusb_error = libusb_init(&lib_device->usb_context);
915 if (libusb_error != 0)
916 {
917 free(lib_device);
918 return AIRSPYHF_ERROR;
919 }
920
921 if (fd == FILE_DESCRIPTOR_UNUSED) {
922 airspyhf_open_device(lib_device,
923 &result,
924 airspyhf_usb_vid,
925 airspyhf_usb_pid,
926 serial_number);
927 }
928 else {
929 airspyhf_open_device_fd(lib_device,
930 &result,
931 fd);
932 }
933
934 if (lib_device->usb_device == NULL)
935 {
936 libusb_exit(lib_device->usb_context);
937 free(lib_device);
938 return result;
939 }
940
941 lib_device->transfers = NULL;
942 lib_device->callback = NULL;
943 lib_device->transfer_count = 16;
944 lib_device->buffer_size = SAMPLES_TO_TRANSFER * sizeof(airspyhf_complex_int16_t);
945 lib_device->streaming = false;
946 lib_device->stop_requested = false;
947
948 lib_device->supported_samplerates = NULL;
949 lib_device->samplerate_architectures = NULL;
950
951 result = airspyhf_read_samplerates_from_fw(lib_device, &lib_device->supported_samplerate_count, 0);
952 if (result == AIRSPYHF_SUCCESS)
953 {
954 lib_device->supported_samplerates = (uint32_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
955 result = airspyhf_read_samplerates_from_fw(lib_device, lib_device->supported_samplerates, lib_device->supported_samplerate_count);
956 if (result == AIRSPYHF_SUCCESS)
957 {
958 lib_device->samplerate_architectures = (uint8_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
959 result = airspyhf_read_samplerate_architectures_from_fw(lib_device, lib_device->samplerate_architectures, lib_device->supported_samplerate_count);
960 if (result != AIRSPYHF_SUCCESS)
961 {
962 memset(lib_device->samplerate_architectures, 0, lib_device->supported_samplerate_count * sizeof(uint8_t)); // Assume Zero IF for all
963 result = AIRSPYHF_SUCCESS; // Clear this error for backward compatibility.
964 }
965 }
966 else
967 {
968 free(lib_device->supported_samplerates);
969 lib_device->supported_samplerates = NULL;
970 }
971 }
972
973 if (result != AIRSPYHF_SUCCESS)
974 {
975 // Assume one default sample rate with Zero IF
976
977 lib_device->supported_samplerate_count = 1;
978 lib_device->supported_samplerates = (uint32_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
979 lib_device->supported_samplerates[0] = DEFAULT_SAMPLERATE;
980
981 lib_device->samplerate_architectures = (uint8_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
982 lib_device->samplerate_architectures[0] = 0;
983 }
984
985 lib_device->current_samplerate = lib_device->supported_samplerates[0];
986 lib_device->is_low_if = lib_device->samplerate_architectures[0];
987
988 result = allocate_transfers(lib_device);
989 if (result != 0)
990 {
991 airspyhf_open_exit(lib_device);
992 free(lib_device);
993 return AIRSPYHF_ERROR;
994 }
995
996 pthread_cond_init(&lib_device->consumer_cv, NULL);
997 pthread_mutex_init(&lib_device->consumer_mp, NULL);
998
999 lib_device->freq_hz = 0;
1000 lib_device->freq_khz = 0;
1001 lib_device->freq_shift = 0;
1002 lib_device->vec.re = 1.0f;
1003 lib_device->vec.im = 0.0f;
1004 lib_device->optimal_point = 0.0f;
1005 lib_device->filter_gain = 1.0f;
1006 lib_device->enable_dsp = 1;
1007
1008 if (airspyhf_config_read(lib_device, (uint8_t *) &record, sizeof(record)) == AIRSPYHF_SUCCESS)
1009 {
1010 if (record.magic_number == CALIBRATION_MAGIC)
1011 {
1012 lib_device->calibration_ppb = record.calibration_ppb;
1013 }
1014 else
1015 {
1016 lib_device->calibration_ppb = 0;
1017 }
1018 }
1019 else
1020 {
1021 lib_device->calibration_ppb = 0;
1022 }
1023
1024 lib_device->iq_balancer = iq_balancer_create(INITIAL_PHASE, INITIAL_AMPLITUDE);
1025
1026 *device = lib_device;
1027
1028 return AIRSPYHF_SUCCESS;
1029 }
1030
1031 void ADDCALL airspyhf_lib_version(airspyhf_lib_version_t* lib_version)
1032 {
1033 lib_version->major_version = AIRSPYHF_VER_MAJOR;
1034 lib_version->minor_version = AIRSPYHF_VER_MINOR;
1035 lib_version->revision = AIRSPYHF_VER_REVISION;
1036 }
1037
1038 int ADDCALL airspyhf_open_sn(airspyhf_device_t** device, uint64_t serial_number)
1039 {
1040 int result;
1041
1042 result = airspyhf_open_init(device, serial_number, FILE_DESCRIPTOR_UNUSED);
1043 return result;
1044 }
1045
1046 int ADDCALL airspyhf_open_fd(airspyhf_device_t** device, int fd)
1047 {
1048 int result;
1049
1050 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, fd);
1051 return result;
1052 }
1053
1054 int ADDCALL airspyhf_open(airspyhf_device_t** device)
1055 {
1056 int result;
1057
1058 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, FILE_DESCRIPTOR_UNUSED);
1059 return result;
1060 }
1061
1062 int ADDCALL airspyhf_close(airspyhf_device_t* device)
1063 {
1064 int result;
1065
1066 result = AIRSPYHF_SUCCESS;
1067
1068 if (device != NULL)
1069 {
1070 result = airspyhf_stop(device);
1071
1072 pthread_cond_destroy(&device->consumer_cv);
1073 pthread_mutex_destroy(&device->consumer_mp);
1074
1075 free_transfers(device);
1076 free(device->supported_samplerates);
1077 free(device->samplerate_architectures);
1078 iq_balancer_destroy(device->iq_balancer);
1079 airspyhf_open_exit(device);
1080 free(device);
1081 }
1082
1083 return result;
1084 }
1085
1086 int ADDCALL airspyhf_get_output_size(airspyhf_device_t * device)
1087 {
1088 // Todo: Make this configurable
1089 return SAMPLES_TO_TRANSFER;
1090 }
1091
1092 int ADDCALL airspyhf_is_low_if(airspyhf_device_t* device)
1093 {
1094 return device->is_low_if;
1095 }
1096
1097 int ADDCALL airspyhf_get_samplerates(airspyhf_device_t* device, uint32_t* buffer, const uint32_t len)
1098 {
1099 if (len == 0)
1100 {
1101 *buffer = device->supported_samplerate_count;
1102 }
1103 else if (len <= device->supported_samplerate_count)
1104 {
1105 memcpy(buffer, device->supported_samplerates, len * sizeof(uint32_t));
1106 }
1107 else
1108 {
1109 return AIRSPYHF_ERROR;
1110 }
1111
1112 return AIRSPYHF_SUCCESS;
1113 }
1114
1115 int ADDCALL airspyhf_set_samplerate(airspyhf_device_t* device, uint32_t samplerate)
1116 {
1117 int result;
1118 uint32_t i;
1119 uint8_t gain;
1120
1121 if (samplerate > MAX_SAMPLERATE_INDEX)
1122 {
1123 for (i = 0; i < device->supported_samplerate_count; i++)
1124 {
1125 if (samplerate == device->supported_samplerates[i])
1126 {
1127 samplerate = i;
1128 break;
1129 }
1130 }
1131 }
1132
1133 if (samplerate >= device->supported_samplerate_count)
1134 {
1135 return AIRSPYHF_ERROR;
1136 }
1137
1138 device->current_samplerate = device->supported_samplerates[samplerate];
1139 device->is_low_if = device->samplerate_architectures[samplerate];
1140
1141 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | 1);
1142
1143 if (!device->is_low_if && device->freq_khz < MIN_ZERO_IF_LO)
1144 {
1145 uint8_t buf[4];
1146 buf[0] = (uint8_t)((MIN_ZERO_IF_LO >> 24) & 0xff);
1147 buf[1] = (uint8_t)((MIN_ZERO_IF_LO >> 16) & 0xff);
1148 buf[2] = (uint8_t)((MIN_ZERO_IF_LO >> 8) & 0xff);
1149 buf[3] = (uint8_t)((MIN_ZERO_IF_LO) & 0xff);
1150
1151 result = libusb_control_transfer(
1152 device->usb_device,
1153 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1154 AIRSPYHF_SET_FREQ,
1155 0,
1156 0,
1157 (unsigned char*) &buf,
1158 sizeof(buf),
1159 0);
1160
1161 if (result < sizeof(buf))
1162 {
1163 return AIRSPYHF_ERROR;
1164 }
1165
1166 device->freq_khz = MIN_ZERO_IF_LO;
1167 }
1168
1169 result = libusb_control_transfer(
1170 device->usb_device,
1171 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1172 AIRSPYHF_SET_SAMPLERATE,
1173 0,
1174 samplerate,
1175 NULL,
1176 0,
1177 0
1178 );
1179
1180 if (result != 0)
1181 {
1182 return AIRSPYHF_ERROR;
1183 }
1184
1185 result = libusb_control_transfer(
1186 device->usb_device,
1187 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1188 AIRSPYHF_GET_FILTER_GAIN,
1189 0,
1190 0,
1191 &gain,
1192 sizeof(uint8_t),
1193 0);
1194
1195 if (result == sizeof(uint8_t))
1196 {
1197 device->filter_gain = powf(10.0f, (float) gain * -0.05f);
1198 }
1199 else
1200 {
1201 device->filter_gain = 1.0;
1202 }
1203
1204 airspyhf_set_freq(device, device->freq_hz);
1205
1206 return AIRSPYHF_SUCCESS;
1207 }
1208
1209 int ADDCALL airspyhf_set_receiver_mode(airspyhf_device_t* device, receiver_mode_t value)
1210 {
1211 int result;
1212 result = libusb_control_transfer(
1213 device->usb_device,
1214 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1215 AIRSPYHF_RECEIVER_MODE,
1216 value,
1217 0,
1218 NULL,
1219 0,
1220 0);
1221
1222 if (result != 0)
1223 {
1224 return AIRSPYHF_ERROR;
1225 }
1226
1227 return AIRSPYHF_SUCCESS;
1228 }
1229
1230 int ADDCALL airspyhf_start(airspyhf_device_t* device, airspyhf_sample_block_cb_fn callback, void* ctx)
1231 {
1232 int result;
1233
1234 memset(device->dropped_buffers_queue, 0, RAW_BUFFER_COUNT * sizeof(uint32_t));
1235 device->dropped_buffers = 0;
1236
1237 device->vec.re = 1.0f;
1238 device->vec.im = 0.0f;
1239
1240 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1241 if (result != AIRSPYHF_SUCCESS)
1242 {
1243 return result;
1244 }
1245
1246 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN);
1247
1248 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_ON);
1249 if (result == AIRSPYHF_SUCCESS)
1250 {
1251 device->ctx = ctx;
1252 result = create_io_threads(device, callback);
1253 }
1254
1255 return result;
1256 }
1257
1258 int ADDCALL airspyhf_is_streaming(airspyhf_device_t* device)
1259 {
1260 return device->streaming && !device->stop_requested;
1261 }
1262
1263 int ADDCALL airspyhf_stop(airspyhf_device_t* device)
1264 {
1265 int result1, result2;
1266 result1 = kill_io_threads(device);
1267 result2 = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1268 libusb_interrupt_event_handler(device->usb_context);
1269
1270 if (result2 != AIRSPYHF_SUCCESS)
1271 {
1272 return result2;
1273 }
1274 return result1;
1275 }
1276
1277 int ADDCALL airspyhf_set_freq(airspyhf_device_t* device, const uint32_t freq_hz)
1278 {
1279 const int tuning_alignment = 1000;
1280
1281 int result;
1282 uint8_t buf[4];
1283 uint32_t lo_low_khz = device->is_low_if ? MIN_LOW_IF_LO : MIN_ZERO_IF_LO;
1284 uint32_t if_shift = (device->enable_dsp && !device->is_low_if) ? DEFAULT_IF_SHIFT : 0;
1285 uint32_t adjusted_freq_hz = (uint32_t) ((int64_t) freq_hz * (int64_t)(1000000000LL + device->calibration_ppb) / 1000000000LL);
1286 uint32_t freq_khz = MAX(lo_low_khz, (adjusted_freq_hz + if_shift + tuning_alignment / 2) / tuning_alignment);
1287
1288 if (device->freq_khz != freq_khz)
1289 {
1290 buf[0] = (uint8_t) ((freq_khz >> 24) & 0xff);
1291 buf[1] = (uint8_t) ((freq_khz >> 16) & 0xff);
1292 buf[2] = (uint8_t) ((freq_khz >> 8) & 0xff);
1293 buf[3] = (uint8_t) ((freq_khz) & 0xff);
1294
1295 result = libusb_control_transfer(
1296 device->usb_device,
1297 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1298 AIRSPYHF_SET_FREQ,
1299 0,
1300 0,
1301 (unsigned char*) &buf,
1302 sizeof(buf),
1303 0);
1304
1305 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1306
1307 if (result < sizeof(buf))
1308 {
1309 return AIRSPYHF_ERROR;
1310 }
1311
1312 device->freq_khz = freq_khz;
1313 }
1314
1315 device->freq_hz = freq_hz;
1316 device->freq_shift = adjusted_freq_hz - freq_khz * 1000;
1317
1318 return AIRSPYHF_SUCCESS;
1319 }
1320
1321 static int airspyhf_config_write(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1322 {
1323 int result;
1324 uint8_t buf[256];
1325
1326 memset(buf, 0, sizeof(buf));
1327
1328 memcpy(buf, buffer, MIN(length, sizeof(buf)));
1329
1330 result = libusb_control_transfer(
1331 device->usb_device,
1332 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1333 AIRSPYHF_CONFIG_WRITE,
1334 0,
1335 0,
1336 buf,
1337 sizeof(buf),
1338 0);
1339
1340 if (result < sizeof(buf))
1341 {
1342 return AIRSPYHF_ERROR;
1343 }
1344
1345 return AIRSPYHF_SUCCESS;
1346 }
1347
1348 static int airspyhf_config_read(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1349 {
1350 uint8_t buf[256];
1351 int result;
1352
1353 result = libusb_control_transfer(
1354 device->usb_device,
1355 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1356 AIRSPYHF_CONFIG_READ,
1357 0,
1358 0,
1359 buf,
1360 sizeof(buf),
1361 0);
1362
1363 memcpy(buffer, buf, MIN(length, sizeof(buf)));
1364
1365 if (result < sizeof(buf))
1366 {
1367 return AIRSPYHF_ERROR;
1368 }
1369
1370 return AIRSPYHF_SUCCESS;
1371 }
1372
1373 int ADDCALL airspyhf_get_calibration(airspyhf_device_t* device, int32_t* ppb)
1374 {
1375 if (ppb)
1376 {
1377 *ppb = device->calibration_ppb;
1378 return AIRSPYHF_SUCCESS;
1379 }
1380
1381 return AIRSPYHF_ERROR;
1382 }
1383
1384 int ADDCALL airspyhf_flash_calibration(airspyhf_device_t* device)
1385 {
1386 calibration_record_t record;
1387 record.magic_number = CALIBRATION_MAGIC;
1388 record.calibration_ppb = device->calibration_ppb;
1389
1390 if (airspyhf_is_streaming(device))
1391 {
1392 return AIRSPYHF_ERROR;
1393 }
1394
1395 if (airspyhf_config_write(device, (uint8_t *)&record, sizeof(record)) != AIRSPYHF_SUCCESS)
1396 {
1397 return AIRSPYHF_ERROR;
1398 }
1399
1400 return AIRSPYHF_SUCCESS;
1401 }
1402
1403 int ADDCALL airspyhf_set_calibration(airspyhf_device_t* device, int32_t ppb)
1404 {
1405 device->calibration_ppb = ppb;
1406 return airspyhf_set_freq(device, device->freq_hz);
1407 }
1408
1409 int ADDCALL airspyhf_set_optimal_iq_correction_point(airspyhf_device_t* device, float w)
1410 {
1411 device->optimal_point = w;
1412 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1413 return AIRSPYHF_SUCCESS;
1414 }
1415
1416 int ADDCALL airspyhf_iq_balancer_configure(airspyhf_device_t* device, int buffers_to_skip, int fft_integration, int fft_overlap, int correlation_integration)
1417 {
1418 iq_balancer_configure(device->iq_balancer, buffers_to_skip, fft_integration, fft_overlap, correlation_integration);
1419 return AIRSPYHF_SUCCESS;
1420 }
1421
1422 int ADDCALL airspyhf_board_partid_serialno_read(airspyhf_device_t* device, airspyhf_read_partid_serialno_t* read_partid_serialno)
1423 {
1424 uint8_t length;
1425 int result;
1426
1427 length = sizeof(airspyhf_read_partid_serialno_t);
1428 result = libusb_control_transfer(
1429 device->usb_device,
1430 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1431 AIRSPYHF_GET_SERIALNO_BOARDID,
1432 0,
1433 0,
1434 (unsigned char*)read_partid_serialno,
1435 length,
1436 0);
1437
1438 if (result < length)
1439 {
1440 return AIRSPYHF_ERROR;
1441 }
1442
1443 return AIRSPYHF_SUCCESS;
1444 }
1445
1446 int ADDCALL airspyhf_version_string_read(airspyhf_device_t* device, char* version, uint8_t length)
1447 {
1448 int result;
1449 char version_local[MAX_VERSION_STRING_SIZE];
1450
1451 result = libusb_control_transfer(
1452 device->usb_device,
1453 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1454 AIRSPYHF_GET_VERSION_STRING,
1455 0,
1456 0,
1457 (unsigned char*) version_local,
1458 (MAX_VERSION_STRING_SIZE - 1),
1459 0);
1460
1461 if (result < 0)
1462 {
1463 return AIRSPYHF_ERROR;
1464 }
1465 else
1466 {
1467 if (length > 0)
1468 {
1469 memcpy(version, version_local, length - 1);
1470 version[length - 1] = 0;
1471 return AIRSPYHF_SUCCESS;
1472 }
1473 else
1474 {
1475 return AIRSPYHF_ERROR;
1476 }
1477 }
1478 }
1479
1480 int ADDCALL airspyhf_set_user_output(airspyhf_device_t* device, airspyhf_user_output_t pin, airspyhf_user_output_state_t value)
1481 {
1482 int result;
1483
1484 result = libusb_control_transfer(
1485 device->usb_device,
1486 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1487 AIRSPYHF_SET_USER_OUTPUT,
1488 (uint16_t)pin,
1489 (uint16_t)value,
1490 NULL,
1491 0,
1492 0);
1493
1494 if (result < 0)
1495 {
1496 return AIRSPYHF_ERROR;
1497 }
1498
1499 return AIRSPYHF_SUCCESS;
1500 }
1501
1502 int ADDCALL airspyhf_set_hf_agc(airspyhf_device_t* device, uint8_t flag)
1503 {
1504 int result;
1505
1506 result = libusb_control_transfer(
1507 device->usb_device,
1508 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1509 AIRSPYHF_SET_HF_AGC,
1510 (uint16_t) flag,
1511 0,
1512 NULL,
1513 0,
1514 0);
1515
1516 if (result < 0)
1517 {
1518 return AIRSPYHF_ERROR;
1519 }
1520
1521 return AIRSPYHF_SUCCESS;
1522 }
1523
1524 int ADDCALL airspyhf_set_hf_agc_threshold(airspyhf_device_t* device, uint8_t flag)
1525 {
1526 int result;
1527
1528 result = libusb_control_transfer(
1529 device->usb_device,
1530 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1531 AIRSPYHF_SET_HF_AGC_THRESHOLD,
1532 (uint16_t) flag,
1533 0,
1534 NULL,
1535 0,
1536 0);
1537
1538 if (result < 0)
1539 {
1540 return AIRSPYHF_ERROR;
1541 }
1542
1543 return AIRSPYHF_SUCCESS;
1544 }
1545
1546 int ADDCALL airspyhf_set_hf_att(airspyhf_device_t* device, uint8_t value)
1547 {
1548 int result;
1549
1550 result = libusb_control_transfer(
1551 device->usb_device,
1552 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1553 AIRSPYHF_SET_HF_ATT,
1554 (uint16_t) value,
1555 0,
1556 NULL,
1557 0,
1558 0);
1559
1560 if (result < 0)
1561 {
1562 return AIRSPYHF_ERROR;
1563 }
1564
1565 return AIRSPYHF_SUCCESS;
1566 }
1567
1568 int ADDCALL airspyhf_set_hf_lna(airspyhf_device_t* device, uint8_t flag)
1569 {
1570 int result;
1571
1572 result = libusb_control_transfer(
1573 device->usb_device,
1574 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1575 AIRSPYHF_SET_HF_LNA,
1576 (uint16_t) flag,
1577 0,
1578 NULL,
1579 0,
1580 0);
1581
1582 if (result < 0)
1583 {
1584 return AIRSPYHF_ERROR;
1585 }
1586
1587 return AIRSPYHF_SUCCESS;
1588 }
1589
1590 int ADDCALL airspyhf_set_lib_dsp(airspyhf_device_t* device, const uint8_t flag)
1591 {
1592 device->enable_dsp = flag;
1593 return AIRSPYHF_SUCCESS;
1594 }
1595
1596 //#include "special.c"