Debian Codegraph @

libairspyhf/src/airspyhf.c

raw symbols ◀ ▶
Source reconstructed at patch state 21 of 23 — not the installed (fully patched) source.
23 Debian patches touch this file
1 /*
2 Copyright (c) 2013-2023, 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 DEFAULT_ATT_STEP_COUNT (9)
65 #define DEFAULT_ATT_STEP_INCREMENT (6.0f)
66
67 #define CALIBRATION_MAGIC (0xA5CA71B0)
68
69 #define DEFAULT_IF_SHIFT (5000)
70 #define MIN_ZERO_IF_LO (180)
71 #define MIN_LOW_IF_LO (84)
72
73 #define STR_PREFIX_SERIAL_AIRSPYHF_SIZE (12)
74 static const char str_prefix_serial_airspyhf[STR_PREFIX_SERIAL_AIRSPYHF_SIZE] =
75 { 'A', 'I', 'R', 'S', 'P', 'Y', 'H', 'F', ' ', 'S', 'N', ':' };
76
77 #define INITIAL_PHASE (0.00006f)
78 #define INITIAL_AMPLITUDE (-0.0045f)
79
80 #define LIBUSB_CTRL_TIMEOUT_MS (500)
81
82 #define IQ_BALANCER_EVAL_SKIP (RAW_BUFFER_COUNT)
83
84 #pragma pack(push,1)
85
86 typedef struct {
87 int16_t im;
88 int16_t re;
89 } airspyhf_complex_int16_t;
90
91 #pragma pack(pop)
92
93 typedef struct airspyhf_device
94 {
95 libusb_context* usb_context;
96 libusb_device_handle* usb_device;
97 struct libusb_transfer** transfers;
98 airspyhf_sample_block_cb_fn callback;
99 pthread_t transfer_thread;
100 pthread_t consumer_thread;
101 bool transfer_thread_running;
102 bool consumer_thread_running;
103 pthread_cond_t consumer_cv;
104 pthread_mutex_t consumer_mp;
105 uint32_t supported_samplerate_count;
106 uint32_t *supported_samplerates;
107 uint8_t *samplerate_architectures;
108 uint32_t supported_att_step_count;
109 float *supported_att_steps;
110 volatile uint32_t current_samplerate;
111 volatile double freq_hz;
112 volatile uint32_t freq_khz;
113 volatile double freq_delta_hz;
114 volatile double freq_shift;
115 volatile int32_t calibration_ppb;
116 volatile int32_t calibration_vctcxo;
117 volatile uint32_t frontend_options;
118 volatile float optimal_point;
119 uint8_t enable_dsp;
120 uint8_t is_low_if;
121 float filter_gain;
122 airspyhf_complex_float_t vec;
123 struct iq_balancer_t *iq_balancer;
124 volatile int32_t iq_balancer_eval_skip;
125 uint32_t transfer_count;
126 int32_t transfer_live;
127 uint32_t buffer_size;
128 uint32_t dropped_buffers;
129 uint32_t dropped_buffers_queue[RAW_BUFFER_COUNT];
130 airspyhf_complex_int16_t *received_samples_queue[RAW_BUFFER_COUNT];
131 volatile bool streaming;
132 volatile bool stop_requested;
133 volatile int received_samples_queue_head;
134 volatile int received_samples_queue_tail;
135 volatile int received_buffer_count;
136 airspyhf_complex_float_t *output_buffer;
137 void* ctx;
138 } airspyhf_device_t;
139
140 typedef struct flash_config
141 {
142 uint32_t magic_number;
143 int32_t calibration_ppb;
144 int32_t calibration_vctcxo;
145 uint32_t frontend_options;
146 } flash_config_t;
147
148 static const uint16_t airspyhf_usb_vid = 0x03EB;
149 static const uint16_t airspyhf_usb_pid = 0x800C;
150
151 static int airspyhf_config_read(airspyhf_device_t* device, uint8_t *buffer, uint16_t length);
152
153 static int cancel_transfers(airspyhf_device_t* device)
154 {
155 uint32_t transfer_index;
156 struct timeval canceltv = { 0, 50 };
157
158 if (device->transfers != NULL)
159 {
160 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
161 {
162 if (device->transfers[transfer_index] != NULL)
163 {
164 libusb_cancel_transfer(device->transfers[transfer_index]);
165 }
166 }
167
168 for (transfer_index = 0; transfer_index < device->transfer_count && device->transfer_live > 0; transfer_index++)
169 libusb_handle_events_timeout_completed(device->usb_context, &canceltv, NULL);
170
171 return AIRSPYHF_SUCCESS;
172 }
173 else
174 {
175 return AIRSPYHF_ERROR;
176 }
177 }
178
179 static int free_transfers(airspyhf_device_t* device)
180 {
181 int i;
182 uint32_t transfer_index;
183
184 if (device->transfers != NULL)
185 {
186 free(device->output_buffer);
187 device->output_buffer = NULL;
188
189 for (transfer_index = 0; transfer_index < device->transfer_count; transfer_index++)
190 {
191 if (device->transfers[transfer_index] != NULL)
192 {
193 libusb_free_transfer(device->transfers[transfer_index]);
194 free(device->transfers[transfer_index]->buffer);
195 device->transfers[transfer_index] = NULL;
196 }
197 }
198 free(device->transfers);
199 device->transfers = NULL;
200
201 for (i = 0; i < RAW_BUFFER_COUNT; i++)
202 {
203 if (device->received_samples_queue[i] != NULL)
204 {
205 free(device->received_samples_queue[i]);
206 device->received_samples_queue[i] = NULL;
207 }
208 }
209 }
210
211 return AIRSPYHF_SUCCESS;
212 }
213
214 static int allocate_transfers(airspyhf_device_t* const device)
215 {
216 int i;
217 uint32_t transfer_index;
218
219 if (device->transfers == NULL)
220 {
221 device->output_buffer = (airspyhf_complex_float_t *) malloc((device->buffer_size / sizeof(airspyhf_complex_int16_t)) * sizeof(airspyhf_complex_float_t));
222
223 for (i = 0; i < RAW_BUFFER_COUNT; i++)
224 {
225 device->received_samples_queue[i] = (airspyhf_complex_int16_t *) malloc(device->buffer_size);
226 if (device->received_samples_queue[i] == NULL)
227 {
228 return AIRSPYHF_ERROR;
229 }
230
231 memset(device->received_samples_queue[i], 0, device->buffer_size);
232 }
233
234 device->transfers = (struct libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer));
235 if (device->transfers == NULL)
236 {
237 return AIRSPYHF_ERROR;
238 }
239
240 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
241 {
242 device->transfers[transfer_index] = libusb_alloc_transfer(0);
243 if (device->transfers[transfer_index] == NULL)
244 {
245 return AIRSPYHF_ERROR;
246 }
247
248 libusb_fill_bulk_transfer(
249 device->transfers[transfer_index],
250 device->usb_device,
251 0,
252 (unsigned char*) malloc(device->buffer_size),
253 device->buffer_size,
254 NULL,
255 device,
256 0);
257
258 if (device->transfers[transfer_index]->buffer == NULL)
259 {
260 return AIRSPYHF_ERROR;
261 }
262 }
263 return AIRSPYHF_SUCCESS;
264 }
265 else
266 {
267 return AIRSPYHF_ERROR;
268 }
269 }
270
271 static int prepare_transfers(airspyhf_device_t* device, const uint_fast8_t endpoint_address, libusb_transfer_cb_fn callback)
272 {
273 int error;
274 uint32_t transfer_index;
275
276 if (device->transfers != NULL)
277 {
278 for (transfer_index = 0; transfer_index<device->transfer_count; transfer_index++)
279 {
280 device->transfers[transfer_index]->endpoint = endpoint_address;
281 device->transfers[transfer_index]->callback = callback;
282
283 error = libusb_submit_transfer(device->transfers[transfer_index]);
284 if (error != 0)
285 {
286 return AIRSPYHF_ERROR;
287 }
288 else
289 device->transfer_live++;
290 }
291 return AIRSPYHF_SUCCESS;
292 }
293
294 return AIRSPYHF_ERROR;
295 }
296
297 static void multiply_complex_complex(airspyhf_complex_float_t *a, const airspyhf_complex_float_t *b)
298 {
299 float re = a->re * b->re - a->im * b->im;
300 a->im = a->im * b->re + a->re * b->im;
301 a->re = re;
302 }
303
304 static void multiply_complex_real(airspyhf_complex_float_t *a, const float b)
305 {
306 a->re *= b;
307 a->im *= b;
308 }
309
310 static inline void rotate_complex(airspyhf_complex_float_t *vec, const airspyhf_complex_float_t *rot)
311 {
312 multiply_complex_complex(vec, rot);
313
314 float norm = 1.99f - (vec->re * vec->re + vec->im * vec->im);
315
316 multiply_complex_real(vec, norm);
317 }
318
319 static void convert_samples(airspyhf_device_t* device, airspyhf_complex_int16_t *src, airspyhf_complex_float_t *dest, int count)
320 {
321 const float scale = 1.0f / 32768;
322
323 int i;
324 airspyhf_complex_float_t vec;
325 airspyhf_complex_float_t rot;
326 double angle;
327 float conversion_gain;
328 uint8_t iqb_eval_skip = 0;
329
330 conversion_gain = scale * device->filter_gain;
331
332 for (i = 0; i < count; i++)
333 {
334 dest[i].re = src[i].re * conversion_gain;
335 dest[i].im = src[i].im * conversion_gain;
336 }
337
338 if (device->iq_balancer_eval_skip > 0)
339 {
340 device->iq_balancer_eval_skip--;
341 iqb_eval_skip = 1;
342 }
343
344 if (device->enable_dsp)
345 {
346 if (!device->is_low_if)
347 {
348 // Zero IF requires external IQ correction
349 iq_balancer_process(device->iq_balancer, dest, count, iqb_eval_skip);
350 }
351
352 // Fine tuning
353 if (device->freq_shift != 0)
354 {
355 angle = 2.0 * M_PI * device->freq_shift / (double) device->current_samplerate;
356
357 vec = device->vec;
358
359 rot.re = (float) cos(angle);
360 rot.im = (float) -sin(angle);
361
362 for (i = 0; i < count; i++)
363 {
364 rotate_complex(&vec, &rot);
365 multiply_complex_complex(&dest[i], &vec);
366 }
367
368 device->vec = vec;
369 }
370 }
371 }
372
373 static void* consumer_threadproc(void *arg)
374 {
375 int sample_count;
376 airspyhf_complex_int16_t *input_samples;
377 uint32_t dropped_buffers;
378 airspyhf_device_t* device = (airspyhf_device_t*) arg;
379 airspyhf_transfer_t transfer;
380
381 #ifdef _WIN32
382
383 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
384
385 #endif
386
387 pthread_mutex_lock(&device->consumer_mp);
388
389 while (device->streaming && !device->stop_requested)
390 {
391 while (device->received_buffer_count == 0 && device->streaming && !device->stop_requested)
392 {
393 pthread_cond_wait(&device->consumer_cv, &device->consumer_mp);
394 }
395 if (!device->streaming || device->stop_requested)
396 {
397 break;
398 }
399
400 input_samples = (airspyhf_complex_int16_t *) device->received_samples_queue[device->received_samples_queue_tail];
401 dropped_buffers = device->dropped_buffers_queue[device->received_samples_queue_tail];
402 device->received_samples_queue_tail = (device->received_samples_queue_tail + 1) & (RAW_BUFFER_COUNT - 1);
403
404 pthread_mutex_unlock(&device->consumer_mp);
405
406 sample_count = device->buffer_size / sizeof(airspyhf_complex_int16_t);
407
408 convert_samples(device, input_samples, device->output_buffer, sample_count);
409
410 transfer.device = device;
411 transfer.ctx = device->ctx;
412 transfer.samples = device->output_buffer;
413 transfer.sample_count = sample_count;
414 transfer.dropped_samples = (uint64_t) dropped_buffers * (uint64_t) sample_count;
415
416 if (device->callback(&transfer) != 0)
417 {
418 device->streaming = false;
419 }
420
421 pthread_mutex_lock(&device->consumer_mp);
422 device->received_buffer_count--;
423 }
424
425 device->streaming = false;
426
427 pthread_mutex_unlock(&device->consumer_mp);
428
429 return NULL;
430 }
431
432 static void LIBUSB_CALL airspyhf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
433 {
434 airspyhf_complex_int16_t *temp;
435 airspyhf_device_t* device = (airspyhf_device_t*) usb_transfer->user_data;
436
437 device->transfer_live--;
438 if (!device->streaming || device->stop_requested)
439 {
440 return;
441 }
442
443 if (usb_transfer->status == LIBUSB_TRANSFER_COMPLETED && usb_transfer->actual_length == usb_transfer->length)
444 {
445 pthread_mutex_lock(&device->consumer_mp);
446
447 if (device->received_buffer_count < RAW_BUFFER_COUNT)
448 {
449 temp = device->received_samples_queue[device->received_samples_queue_head];
450 device->received_samples_queue[device->received_samples_queue_head] = (airspyhf_complex_int16_t *) usb_transfer->buffer;
451 usb_transfer->buffer = (uint8_t *) temp;
452
453 device->dropped_buffers_queue[device->received_samples_queue_head] = device->dropped_buffers;
454 device->dropped_buffers = 0;
455
456 device->received_samples_queue_head = (device->received_samples_queue_head + 1) & (RAW_BUFFER_COUNT - 1);
457 device->received_buffer_count++;
458
459 pthread_cond_signal(&device->consumer_cv);
460 }
461 else
462 {
463 device->dropped_buffers++;
464 }
465
466 pthread_mutex_unlock(&device->consumer_mp);
467
468 if (libusb_submit_transfer(usb_transfer) != 0)
469 {
470 device->streaming = false;
471 }
472 else
473 device->transfer_live ++;
474 }
475 else
476 {
477 device->streaming = false;
478 }
479 }
480
481 static void* transfer_threadproc(void* arg)
482 {
483 airspyhf_device_t* device = (struct airspyhf_device*) arg;
484 int error;
485 struct timeval timeout = { 0, 500000 };
486
487 #ifdef _WIN32
488
489 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
490
491 #endif
492
493 while (device->streaming && !device->stop_requested)
494 {
495 error = libusb_handle_events_timeout_completed(device->usb_context, &timeout, NULL);
496 if (error < 0)
497 {
498 if (error != LIBUSB_ERROR_INTERRUPTED)
499 device->streaming = false;
500 }
501 }
502
503 device->streaming = false;
504
505 return NULL;
506 }
507
508 static int kill_io_threads(airspyhf_device_t* device)
509 {
510 struct timeval timeout = { 0, 0 };
511
512 if (device->stop_requested)
513 {
514 device->stop_requested = false;
515 device->streaming = false;
516 cancel_transfers(device);
517
518 pthread_mutex_lock(&device->consumer_mp);
519 pthread_cond_signal(&device->consumer_cv);
520 pthread_mutex_unlock(&device->consumer_mp);
521
522 if (device->transfer_thread_running) {
523 pthread_join(device->transfer_thread, NULL);
524 device->transfer_thread_running = false;
525 }
526 if (device->consumer_thread_running) {
527 pthread_join(device->consumer_thread, NULL);
528 device->consumer_thread_running = false;
529 }
530
531 libusb_handle_events_timeout_completed(device->usb_context, &timeout, NULL);
532 }
533
534 return AIRSPYHF_SUCCESS;
535 }
536
537 static int create_io_threads(airspyhf_device_t* device, airspyhf_sample_block_cb_fn callback)
538 {
539 int result;
540 pthread_attr_t attr;
541
542 if (!device->streaming && !device->stop_requested)
543 {
544 device->callback = callback;
545 device->streaming = true;
546
547 result = prepare_transfers(device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN, (libusb_transfer_cb_fn)airspyhf_libusb_transfer_callback);
548 if (result != AIRSPYHF_SUCCESS)
549 {
550 return result;
551 }
552
553 device->received_samples_queue_head = 0;
554 device->received_samples_queue_tail = 0;
555 device->received_buffer_count = 0;
556
557 pthread_attr_init(&attr);
558 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
559
560 result = pthread_create(&device->consumer_thread, &attr, consumer_threadproc, device);
561 if (result != 0)
562 {
563 return AIRSPYHF_ERROR;
564 }
565 device->consumer_thread_running = true;
566
567 result = pthread_create(&device->transfer_thread, &attr, transfer_threadproc, device);
568 if (result != 0)
569 {
570 return AIRSPYHF_ERROR;
571 }
572 device->transfer_thread_running = true;
573
574 pthread_attr_destroy(&attr);
575 }
576 else {
577 return AIRSPYHF_ERROR;
578 }
579
580 return AIRSPYHF_SUCCESS;
581 }
582
583 static void airspyhf_open_exit(airspyhf_device_t* device)
584 {
585 if (device->usb_device != NULL)
586 {
587 libusb_release_interface(device->usb_device, 0);
588 libusb_close(device->usb_device);
589 device->usb_device = NULL;
590 }
591 libusb_exit(device->usb_context);
592 device->usb_context = NULL;
593 }
594
595 static int airspyhf_read_samplerates_from_fw(airspyhf_device_t* device, uint32_t* buffer, const uint32_t len)
596 {
597 int result;
598
599 result = libusb_control_transfer(
600 device->usb_device,
601 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
602 AIRSPYHF_GET_SAMPLERATES,
603 0,
604 len,
605 (unsigned char*) buffer,
606 (len > 0 ? len : 1) * (int16_t) sizeof(uint32_t),
607 LIBUSB_CTRL_TIMEOUT_MS);
608
609 if (result < 1)
610 {
611 return AIRSPYHF_ERROR;
612 }
613 return AIRSPYHF_SUCCESS;
614 }
615
616 static int airspyhf_read_att_steps_from_fw(airspyhf_device_t* device, void* buffer, const uint32_t count)
617 {
618 int result;
619
620 result = libusb_control_transfer(
621 device->usb_device,
622 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
623 AIRSPYHF_GET_ATT_STEPS,
624 0,
625 count,
626 (unsigned char*) buffer,
627 (int16_t)(count > 0 ? (count * sizeof(float)) : sizeof(int32_t)),
628 LIBUSB_CTRL_TIMEOUT_MS);
629
630 if (result < 1)
631 {
632 return AIRSPYHF_ERROR;
633 }
634 return AIRSPYHF_SUCCESS;
635 }
636
637 static int airspyhf_read_samplerate_architectures_from_fw(airspyhf_device_t* device, uint8_t* buffer, const uint32_t len)
638 {
639 int result;
640
641 result = libusb_control_transfer(
642 device->usb_device,
643 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
644 AIRSPYHF_GET_SAMPLERATE_ARCHITECTURES,
645 0,
646 len,
647 (unsigned char*) buffer,
648 (len > 0 ? len : 1) * (int16_t) sizeof(uint32_t),
649 LIBUSB_CTRL_TIMEOUT_MS);
650
651 if (result < 1)
652 {
653 return AIRSPYHF_ERROR;
654 }
655
656 return AIRSPYHF_SUCCESS;
657 }
658
659
660 static void airspyhf_open_device(airspyhf_device_t* device,
661 int* ret,
662 uint16_t vid,
663 uint16_t pid,
664 uint64_t serial_number_val)
665 {
666 int i;
667 int result;
668 libusb_device_handle** libusb_dev_handle;
669 int serial_number_len;
670 libusb_device_handle* dev_handle;
671 libusb_device *dev;
672 libusb_device** devices = NULL;
673
674 ssize_t cnt;
675 int serial_descriptor_index;
676 struct libusb_device_descriptor device_descriptor;
677 unsigned char serial_number[AIRSPYHF_SERIAL_SIZE + 1];
678
679 libusb_dev_handle = &device->usb_device;
680 *libusb_dev_handle = NULL;
681
682 cnt = libusb_get_device_list(device->usb_context, &devices);
683 if (cnt < 0)
684 {
685 *ret = AIRSPYHF_ERROR;
686 return;
687 }
688
689 i = 0;
690 while ((dev = devices[i++]) != NULL)
691 {
692 libusb_get_device_descriptor(dev, &device_descriptor);
693
694 if ((device_descriptor.idVendor == vid) &&
695 (device_descriptor.idProduct == pid))
696 {
697 if (serial_number_val != SERIAL_NUMBER_UNUSED)
698 {
699 serial_descriptor_index = device_descriptor.iSerialNumber;
700 if (serial_descriptor_index > 0)
701 {
702 if (libusb_open(dev, libusb_dev_handle) != 0)
703 {
704 *libusb_dev_handle = NULL;
705 continue;
706 }
707 dev_handle = *libusb_dev_handle;
708 serial_number_len = libusb_get_string_descriptor_ascii(dev_handle,
709 serial_descriptor_index,
710 serial_number,
711 sizeof(serial_number));
712
713 if (serial_number_len == AIRSPYHF_SERIAL_SIZE && !memcmp(str_prefix_serial_airspyhf, serial_number, STR_PREFIX_SERIAL_AIRSPYHF_SIZE))
714 {
715 uint64_t serial = SERIAL_NUMBER_UNUSED;
716 // use same code to determine device's serial number as in airspyhf_list_devices()
717 {
718 char *start, *end;
719 serial_number[AIRSPYHF_SERIAL_SIZE] = 0;
720 start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPYHF_SIZE);
721 end = NULL;
722 serial = strtoull(start, &end, 16);
723 }
724 if (serial == serial_number_val)
725 {
726 result = libusb_set_configuration(dev_handle, 1);
727 if (result != 0)
728 {
729 libusb_close(dev_handle);
730 *libusb_dev_handle = NULL;
731 continue;
732 }
733 result = libusb_claim_interface(dev_handle, 0);
734 if (result != 0)
735 {
736 libusb_close(dev_handle);
737 *libusb_dev_handle = NULL;
738 continue;
739 }
740
741 result = libusb_set_interface_alt_setting(dev_handle, 0, 1);
742 if (result != 0)
743 {
744 libusb_close(dev_handle);
745 *libusb_dev_handle = NULL;
746 continue;
747 }
748
749 break;
750 }
751 else
752 {
753 libusb_close(dev_handle);
754 *libusb_dev_handle = NULL;
755 continue;
756 }
757 }
758 else
759 {
760 libusb_close(dev_handle);
761 *libusb_dev_handle = NULL;
762 continue;
763 }
764 }
765 }
766 else
767 {
768 if (libusb_open(dev, libusb_dev_handle) == 0)
769 {
770 dev_handle = *libusb_dev_handle;
771 result = libusb_set_configuration(dev_handle, 1);
772 if (result != 0)
773 {
774 libusb_close(dev_handle);
775 *libusb_dev_handle = NULL;
776 continue;
777 }
778 result = libusb_claim_interface(dev_handle, 0);
779 if (result != 0)
780 {
781 libusb_close(dev_handle);
782 *libusb_dev_handle = NULL;
783 continue;
784 }
785
786 result = libusb_set_interface_alt_setting(dev_handle, 0, 1);
787 if (result != 0)
788 {
789 libusb_close(dev_handle);
790 *libusb_dev_handle = NULL;
791 continue;
792 }
793 break;
794 }
795 }
796 }
797 }
798 libusb_free_device_list(devices, 1);
799
800 dev_handle = device->usb_device;
801 if (dev_handle == NULL)
802 {
803 *ret = AIRSPYHF_ERROR;
804 return;
805 }
806
807 *ret = AIRSPYHF_SUCCESS;
808 return;
809 }
810
811 static void airspyhf_open_device_fd(airspyhf_device_t* device,
812 int* ret,
813 int fd) {
814 int result = -1;
815
816 #ifdef __ANDROID__
817 result = libusb_wrap_sys_device(device->usb_context, (intptr_t)fd, &device->usb_device);
818 #else
819 device->usb_device = NULL;
820 *ret = AIRSPYHF_UNSUPPORTED;
821 return;
822 #endif
823
824 if (result != 0 || device->usb_device == NULL)
825 {
826 *ret = AIRSPYHF_ERROR;
827 return;
828 }
829
830 result = libusb_set_configuration(device->usb_device, 1);
831 if (result != 0)
832 {
833 libusb_close(device->usb_device);
834 device->usb_device = NULL;
835 *ret = AIRSPYHF_ERROR;
836 return;
837 }
838
839 result = libusb_claim_interface(device->usb_device, 0);
840 if (result != 0)
841 {
842 libusb_close(device->usb_device);
843 device->usb_device = NULL;
844 *ret = AIRSPYHF_ERROR;
845 return;
846 }
847
848 result = libusb_set_interface_alt_setting(device->usb_device, 0, 1);
849 if (result != 0)
850 {
851 libusb_close(device->usb_device);
852 device->usb_device = NULL;
853 *ret = AIRSPYHF_ERROR;
854 return;
855 }
856
857 *ret = AIRSPYHF_SUCCESS;
858 return;
859 }
860
861 int airspyhf_list_devices(uint64_t *serials, int count)
862 {
863 libusb_device_handle* libusb_dev_handle;
864 struct libusb_context *context;
865 libusb_device** devices = NULL;
866 libusb_device *dev;
867 struct libusb_device_descriptor device_descriptor;
868
869 int serial_descriptor_index;
870 int serial_number_len;
871 int output_count;
872 int i;
873 unsigned char serial_number[AIRSPYHF_SERIAL_SIZE + 1];
874
875 if (serials)
876 {
877 memset(serials, 0, sizeof(uint64_t) * count);
878 }
879
880 #ifdef __ANDROID__
881 // LibUSB does not support device discovery on android
882 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
883 #endif
884
885 if (libusb_init(&context) != 0)
886 {
887 return AIRSPYHF_ERROR;
888 }
889
890 if (libusb_get_device_list(context, &devices) < 0)
891 {
892 return AIRSPYHF_ERROR;
893 }
894
895 i = 0;
896 output_count = 0;
897 while ((dev = devices[i++]) != NULL && (!serials || output_count < count))
898 {
899 libusb_get_device_descriptor(dev, &device_descriptor);
900
901 if ((device_descriptor.idVendor == airspyhf_usb_vid) &&
902 (device_descriptor.idProduct == airspyhf_usb_pid))
903 {
904 serial_descriptor_index = device_descriptor.iSerialNumber;
905 if (serial_descriptor_index > 0)
906 {
907 if (libusb_open(dev, &libusb_dev_handle) != 0)
908 {
909 continue;
910 }
911
912 serial_number_len = libusb_get_string_descriptor_ascii(libusb_dev_handle,
913 serial_descriptor_index,
914 serial_number,
915 sizeof(serial_number));
916
917 if (serial_number_len == AIRSPYHF_SERIAL_SIZE && !memcmp(str_prefix_serial_airspyhf, serial_number, STR_PREFIX_SERIAL_AIRSPYHF_SIZE))
918 {
919 char *start, *end;
920 uint64_t serial;
921
922 serial_number[AIRSPYHF_SERIAL_SIZE] = 0;
923 start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPYHF_SIZE);
924 end = NULL;
925 serial = strtoull(start, &end, 16);
926 if (serial == 0 && start == end)
927 {
928 libusb_close(libusb_dev_handle);
929 continue;
930 }
931
932 if (serials)
933 {
934 serials[output_count] = serial;
935 }
936 output_count++;
937 }
938
939 libusb_close(libusb_dev_handle);
940 }
941 }
942 }
943
944 libusb_free_device_list(devices, 1);
945 libusb_exit(context);
946 return output_count;
947 }
948
949 static int airspyhf_open_init(airspyhf_device_t** device, uint64_t serial_number, int fd)
950 {
951 airspyhf_device_t* lib_device;
952 flash_config_t config;
953 int libusb_error;
954 int result;
955
956 *device = NULL;
957
958 lib_device = (airspyhf_device_t*) calloc(1, sizeof(airspyhf_device_t));
959 if (lib_device == NULL)
960 {
961 return AIRSPYHF_ERROR;
962 }
963
964 #ifdef __ANDROID__
965 // LibUSB does not support device discovery on android
966 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
967 #endif
968
969 libusb_error = libusb_init(&lib_device->usb_context);
970 if (libusb_error != 0)
971 {
972 free(lib_device);
973 return AIRSPYHF_ERROR;
974 }
975
976 if (fd == FILE_DESCRIPTOR_UNUSED) {
977 airspyhf_open_device(lib_device,
978 &result,
979 airspyhf_usb_vid,
980 airspyhf_usb_pid,
981 serial_number);
982 }
983 else {
984 airspyhf_open_device_fd(lib_device,
985 &result,
986 fd);
987 }
988
989 if (lib_device->usb_device == NULL)
990 {
991 libusb_exit(lib_device->usb_context);
992 free(lib_device);
993 return result;
994 }
995
996 lib_device->transfers = NULL;
997 lib_device->callback = NULL;
998 lib_device->transfer_count = 16;
999 lib_device->buffer_size = SAMPLES_TO_TRANSFER * sizeof(airspyhf_complex_int16_t);
1000 lib_device->streaming = false;
1001 lib_device->stop_requested = false;
1002
1003 lib_device->supported_samplerates = NULL;
1004 lib_device->samplerate_architectures = NULL;
1005
1006 result = airspyhf_read_samplerates_from_fw(lib_device, &lib_device->supported_samplerate_count, 0);
1007 if (result == AIRSPYHF_SUCCESS)
1008 {
1009 lib_device->supported_samplerates = (uint32_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
1010 result = airspyhf_read_samplerates_from_fw(lib_device, lib_device->supported_samplerates, lib_device->supported_samplerate_count);
1011 if (result == AIRSPYHF_SUCCESS)
1012 {
1013 lib_device->samplerate_architectures = (uint8_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
1014 result = airspyhf_read_samplerate_architectures_from_fw(lib_device, lib_device->samplerate_architectures, lib_device->supported_samplerate_count);
1015 if (result != AIRSPYHF_SUCCESS)
1016 {
1017 memset(lib_device->samplerate_architectures, 0, lib_device->supported_samplerate_count * sizeof(uint8_t)); // Assume Zero IF for all
1018 result = AIRSPYHF_SUCCESS; // Clear this error for backward compatibility.
1019 }
1020 }
1021 else
1022 {
1023 free(lib_device->supported_samplerates);
1024 lib_device->supported_samplerates = NULL;
1025 }
1026 }
1027
1028 if (result != AIRSPYHF_SUCCESS)
1029 {
1030 // Assume one default sample rate with Zero IF
1031
1032 lib_device->supported_samplerate_count = 1;
1033 lib_device->supported_samplerates = (uint32_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
1034 lib_device->supported_samplerates[0] = DEFAULT_SAMPLERATE;
1035
1036 lib_device->samplerate_architectures = (uint8_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
1037 lib_device->samplerate_architectures[0] = 0;
1038 }
1039
1040 lib_device->current_samplerate = lib_device->supported_samplerates[0];
1041 lib_device->is_low_if = lib_device->samplerate_architectures[0];
1042
1043 result = airspyhf_read_att_steps_from_fw(lib_device, &lib_device->supported_att_step_count, 0);
1044 if (result == AIRSPYHF_SUCCESS)
1045 {
1046 lib_device->supported_att_steps = (float*)malloc(lib_device->supported_att_step_count * sizeof(float));
1047 result = airspyhf_read_att_steps_from_fw(lib_device, lib_device->supported_att_steps, lib_device->supported_att_step_count);
1048 if (result != AIRSPYHF_SUCCESS)
1049 {
1050 free(lib_device->supported_att_steps);
1051 lib_device->supported_att_steps = NULL;
1052 lib_device->supported_att_step_count = 0;
1053 }
1054 }
1055
1056 if (result != AIRSPYHF_SUCCESS)
1057 {
1058 // Assume ATT steps of the Airspy HF+ Discovery
1059
1060 lib_device->supported_att_step_count = DEFAULT_ATT_STEP_COUNT;
1061 lib_device->supported_att_steps = (float*)malloc(lib_device->supported_att_step_count * sizeof(float));
1062 for (uint32_t i = 0; i < lib_device->supported_att_step_count; i++)
1063 {
1064 lib_device->supported_att_steps[i] = i * DEFAULT_ATT_STEP_INCREMENT;
1065 }
1066 }
1067
1068 result = allocate_transfers(lib_device);
1069 if (result != 0)
1070 {
1071 airspyhf_open_exit(lib_device);
1072 free(lib_device);
1073 return AIRSPYHF_ERROR;
1074 }
1075
1076 pthread_cond_init(&lib_device->consumer_cv, NULL);
1077 pthread_mutex_init(&lib_device->consumer_mp, NULL);
1078
1079 lib_device->freq_hz = 0;
1080 lib_device->freq_khz = 0;
1081 lib_device->freq_delta_hz = 0;
1082 lib_device->freq_shift = 0;
1083 lib_device->vec.re = 1.0f;
1084 lib_device->vec.im = 0.0f;
1085 lib_device->optimal_point = 0.0f;
1086 lib_device->filter_gain = 1.0f;
1087 lib_device->enable_dsp = 1;
1088
1089 if (airspyhf_config_read(lib_device, (uint8_t *) &config, sizeof(config)) == AIRSPYHF_SUCCESS)
1090 {
1091 if (config.magic_number == CALIBRATION_MAGIC)
1092 {
1093 lib_device->calibration_ppb = config.calibration_ppb;
1094 airspyhf_set_vctcxo_calibration(lib_device, config.calibration_vctcxo);
1095 airspyhf_set_frontend_options(lib_device, config.frontend_options);
1096 }
1097 else
1098 {
1099 lib_device->calibration_ppb = 0;
1100 lib_device->calibration_vctcxo = 0;
1101 lib_device->frontend_options = 0;
1102 }
1103 }
1104 else
1105 {
1106 lib_device->calibration_ppb = 0;
1107 lib_device->calibration_vctcxo = 0;
1108 lib_device->frontend_options = 0;
1109 }
1110
1111 lib_device->iq_balancer = iq_balancer_create(INITIAL_PHASE, INITIAL_AMPLITUDE);
1112 lib_device->iq_balancer_eval_skip = 0;
1113
1114 *device = lib_device;
1115
1116 return AIRSPYHF_SUCCESS;
1117 }
1118
1119 void ADDCALL airspyhf_lib_version(airspyhf_lib_version_t* lib_version)
1120 {
1121 lib_version->major_version = AIRSPYHF_VER_MAJOR;
1122 lib_version->minor_version = AIRSPYHF_VER_MINOR;
1123 lib_version->revision = AIRSPYHF_VER_REVISION;
1124 }
1125
1126 int ADDCALL airspyhf_open_sn(airspyhf_device_t** device, uint64_t serial_number)
1127 {
1128 int result;
1129
1130 result = airspyhf_open_init(device, serial_number, FILE_DESCRIPTOR_UNUSED);
1131 return result;
1132 }
1133
1134 int ADDCALL airspyhf_open_fd(airspyhf_device_t** device, int fd)
1135 {
1136 int result;
1137
1138 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, fd);
1139 return result;
1140 }
1141
1142 int ADDCALL airspyhf_open(airspyhf_device_t** device)
1143 {
1144 int result;
1145
1146 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, FILE_DESCRIPTOR_UNUSED);
1147 return result;
1148 }
1149
1150 int ADDCALL airspyhf_close(airspyhf_device_t* device)
1151 {
1152 int result;
1153
1154 result = AIRSPYHF_SUCCESS;
1155
1156 if (device != NULL)
1157 {
1158 result = airspyhf_stop(device);
1159 free_transfers(device);
1160 airspyhf_open_exit(device);
1161
1162 free(device->supported_samplerates);
1163 free(device->samplerate_architectures);
1164 free(device->supported_att_steps);
1165 iq_balancer_destroy(device->iq_balancer);
1166
1167 pthread_cond_destroy(&device->consumer_cv);
1168 pthread_mutex_destroy(&device->consumer_mp);
1169
1170 free(device);
1171 }
1172
1173 return result;
1174 }
1175
1176 int ADDCALL airspyhf_get_output_size(airspyhf_device_t * device)
1177 {
1178 // Todo: Make this configurable
1179 return SAMPLES_TO_TRANSFER;
1180 }
1181
1182 int ADDCALL airspyhf_is_low_if(airspyhf_device_t* device)
1183 {
1184 return device->is_low_if;
1185 }
1186
1187 int ADDCALL airspyhf_get_samplerates(airspyhf_device_t* device, uint32_t* buffer, const uint32_t len)
1188 {
1189 if (len == 0)
1190 {
1191 *buffer = device->supported_samplerate_count;
1192 }
1193 else if (len <= device->supported_samplerate_count)
1194 {
1195 memcpy(buffer, device->supported_samplerates, len * sizeof(uint32_t));
1196 }
1197 else
1198 {
1199 return AIRSPYHF_ERROR;
1200 }
1201
1202 return AIRSPYHF_SUCCESS;
1203 }
1204
1205 int ADDCALL airspyhf_set_samplerate(airspyhf_device_t* device, uint32_t samplerate)
1206 {
1207 int result;
1208 uint32_t i;
1209 uint8_t gain;
1210
1211 if (samplerate > MAX_SAMPLERATE_INDEX)
1212 {
1213 for (i = 0; i < device->supported_samplerate_count; i++)
1214 {
1215 if (samplerate == device->supported_samplerates[i])
1216 {
1217 samplerate = i;
1218 break;
1219 }
1220 }
1221 }
1222
1223 if (samplerate >= device->supported_samplerate_count)
1224 {
1225 return AIRSPYHF_ERROR;
1226 }
1227
1228 device->current_samplerate = device->supported_samplerates[samplerate];
1229 device->is_low_if = device->samplerate_architectures[samplerate];
1230
1231 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | 1);
1232
1233 if (!device->is_low_if && device->freq_khz < MIN_ZERO_IF_LO)
1234 {
1235 uint8_t buf[4];
1236 buf[0] = (uint8_t)((MIN_ZERO_IF_LO >> 24) & 0xff);
1237 buf[1] = (uint8_t)((MIN_ZERO_IF_LO >> 16) & 0xff);
1238 buf[2] = (uint8_t)((MIN_ZERO_IF_LO >> 8) & 0xff);
1239 buf[3] = (uint8_t)((MIN_ZERO_IF_LO) & 0xff);
1240
1241 result = libusb_control_transfer(
1242 device->usb_device,
1243 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1244 AIRSPYHF_SET_FREQ,
1245 0,
1246 0,
1247 (unsigned char*) &buf,
1248 sizeof(buf),
1249 LIBUSB_CTRL_TIMEOUT_MS);
1250
1251 if (result < sizeof(buf))
1252 {
1253 return AIRSPYHF_ERROR;
1254 }
1255
1256 device->freq_khz = MIN_ZERO_IF_LO;
1257 }
1258
1259 result = libusb_control_transfer(
1260 device->usb_device,
1261 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1262 AIRSPYHF_SET_SAMPLERATE,
1263 0,
1264 samplerate,
1265 NULL,
1266 0,
1267 LIBUSB_CTRL_TIMEOUT_MS);
1268
1269 if (result != 0)
1270 {
1271 return AIRSPYHF_ERROR;
1272 }
1273
1274 result = libusb_control_transfer(
1275 device->usb_device,
1276 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1277 AIRSPYHF_GET_FILTER_GAIN,
1278 0,
1279 0,
1280 &gain,
1281 sizeof(uint8_t),
1282 LIBUSB_CTRL_TIMEOUT_MS);
1283
1284 if (result == sizeof(uint8_t))
1285 {
1286 device->filter_gain = powf(10.0f, (float) gain * -0.05f);
1287 }
1288 else
1289 {
1290 device->filter_gain = 1.0;
1291 }
1292
1293 airspyhf_set_freq_double(device, device->freq_hz);
1294
1295 return AIRSPYHF_SUCCESS;
1296 }
1297
1298 int ADDCALL airspyhf_set_receiver_mode(airspyhf_device_t* device, receiver_mode_t value)
1299 {
1300 int result;
1301 result = libusb_control_transfer(
1302 device->usb_device,
1303 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1304 AIRSPYHF_RECEIVER_MODE,
1305 value,
1306 0,
1307 NULL,
1308 0,
1309 LIBUSB_CTRL_TIMEOUT_MS);
1310
1311 if (result != 0)
1312 {
1313 return AIRSPYHF_ERROR;
1314 }
1315
1316 return AIRSPYHF_SUCCESS;
1317 }
1318
1319 int ADDCALL airspyhf_start(airspyhf_device_t* device, airspyhf_sample_block_cb_fn callback, void* ctx)
1320 {
1321 int result;
1322
1323 memset(device->dropped_buffers_queue, 0, RAW_BUFFER_COUNT * sizeof(uint32_t));
1324 device->dropped_buffers = 0;
1325
1326 device->vec.re = 1.0f;
1327 device->vec.im = 0.0f;
1328
1329 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1330 if (result != AIRSPYHF_SUCCESS)
1331 {
1332 return result;
1333 }
1334
1335 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN);
1336
1337 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_ON);
1338 if (result == AIRSPYHF_SUCCESS)
1339 {
1340 device->ctx = ctx;
1341 result = create_io_threads(device, callback);
1342 }
1343
1344 return result;
1345 }
1346
1347 int ADDCALL airspyhf_is_streaming(airspyhf_device_t* device)
1348 {
1349 return device->streaming && !device->stop_requested;
1350 }
1351
1352 int ADDCALL airspyhf_stop(airspyhf_device_t* device)
1353 {
1354 int result1, result2;
1355 device->stop_requested = true;
1356 result1 = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1357 result2 = kill_io_threads(device);
1358
1359 #ifndef _WIN32
1360 libusb_interrupt_event_handler(device->usb_context);
1361 #endif
1362
1363 if (result2 != AIRSPYHF_SUCCESS)
1364 {
1365 return result2;
1366 }
1367 return result1;
1368 }
1369
1370
1371 int ADDCALL airspyhf_set_freq(airspyhf_device_t* device, const uint32_t freq_hz)
1372 {
1373 return airspyhf_set_freq_double(device, freq_hz);
1374 }
1375
1376 int ADDCALL airspyhf_set_freq_double(airspyhf_device_t* device, const double freq_hz)
1377 {
1378 int result;
1379 uint8_t buf[4];
1380 double if_shift = (device->enable_dsp && !device->is_low_if) ? DEFAULT_IF_SHIFT : 0;
1381 double adjusted_freq_hz = freq_hz * (1.0e9 + device->calibration_ppb) * 1.0e-9;
1382 uint32_t lo_low_khz = device->is_low_if ? MIN_LOW_IF_LO : MIN_ZERO_IF_LO;
1383 uint32_t freq_khz = MAX(lo_low_khz, (uint32_t)round((adjusted_freq_hz + if_shift) * 1e-3));
1384
1385 if (device->freq_khz != freq_khz)
1386 {
1387 device->iq_balancer_eval_skip = IQ_BALANCER_EVAL_SKIP;
1388
1389 buf[0] = (uint8_t)((freq_khz >> 24) & 0xff);
1390 buf[1] = (uint8_t)((freq_khz >> 16) & 0xff);
1391 buf[2] = (uint8_t)((freq_khz >> 8) & 0xff);
1392 buf[3] = (uint8_t)((freq_khz) & 0xff);
1393
1394 result = libusb_control_transfer(
1395 device->usb_device,
1396 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1397 AIRSPYHF_SET_FREQ,
1398 0,
1399 0,
1400 (unsigned char*)&buf,
1401 sizeof(buf),
1402 LIBUSB_CTRL_TIMEOUT_MS);
1403
1404 if (result < sizeof(buf))
1405 {
1406 return AIRSPYHF_ERROR;
1407 }
1408
1409 device->freq_khz = freq_khz;
1410
1411 result = libusb_control_transfer(
1412 device->usb_device,
1413 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1414 AIRSPYHF_GET_FREQ_DELTA,
1415 0,
1416 0,
1417 (unsigned char*)&buf,
1418 sizeof(buf),
1419 LIBUSB_CTRL_TIMEOUT_MS);
1420
1421 if (result == sizeof(buf))
1422 {
1423 device->freq_delta_hz = (((int8_t)buf[3] << 16) | (buf[2] << 8) | buf[1]) * 1e3 / (1 << buf[0]);
1424 }
1425
1426 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1427 }
1428
1429 device->freq_hz = freq_hz;
1430 device->freq_shift = adjusted_freq_hz - freq_khz * 1e3 + device->freq_delta_hz;
1431
1432 return AIRSPYHF_SUCCESS;
1433 }
1434
1435 int ADDCALL airspyhf_get_frontend_options(airspyhf_device_t* device, uint32_t* flags)
1436 {
1437 if (flags)
1438 {
1439 *flags = device->frontend_options;
1440 return AIRSPYHF_SUCCESS;
1441 }
1442
1443 return AIRSPYHF_ERROR;
1444 }
1445
1446 int ADDCALL airspyhf_set_frontend_options(airspyhf_device_t* device, uint32_t flags)
1447 {
1448 int result;
1449
1450 device->frontend_options = flags;
1451
1452 result = libusb_control_transfer(
1453 device->usb_device,
1454 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1455 AIRSPYHF_SET_FRONTEND_OPTIONS,
1456 (uint16_t)(flags & 0xffff),
1457 (uint16_t)(flags >> 16),
1458 NULL,
1459 0,
1460 LIBUSB_CTRL_TIMEOUT_MS);
1461
1462 if (result < 0)
1463 {
1464 return AIRSPYHF_ERROR;
1465 }
1466 return AIRSPYHF_SUCCESS;
1467 }
1468
1469 static int airspyhf_config_write(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1470 {
1471 int result;
1472 uint8_t buf[256];
1473
1474 memset(buf, 0, sizeof(buf));
1475
1476 memcpy(buf, buffer, MIN(length, sizeof(buf)));
1477
1478 result = libusb_control_transfer(
1479 device->usb_device,
1480 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1481 AIRSPYHF_CONFIG_WRITE,
1482 0,
1483 0,
1484 buf,
1485 sizeof(buf),
1486 LIBUSB_CTRL_TIMEOUT_MS);
1487
1488 if (result < sizeof(buf))
1489 {
1490 return AIRSPYHF_ERROR;
1491 }
1492
1493 return AIRSPYHF_SUCCESS;
1494 }
1495
1496 static int airspyhf_config_read(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1497 {
1498 uint8_t buf[256];
1499 int result;
1500
1501 result = libusb_control_transfer(
1502 device->usb_device,
1503 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1504 AIRSPYHF_CONFIG_READ,
1505 0,
1506 0,
1507 buf,
1508 sizeof(buf),
1509 LIBUSB_CTRL_TIMEOUT_MS);
1510
1511 memcpy(buffer, buf, MIN(length, sizeof(buf)));
1512
1513 if (result < sizeof(buf))
1514 {
1515 return AIRSPYHF_ERROR;
1516 }
1517
1518 return AIRSPYHF_SUCCESS;
1519 }
1520
1521 int ADDCALL airspyhf_get_calibration(airspyhf_device_t* device, int32_t* ppb)
1522 {
1523 if (ppb)
1524 {
1525 *ppb = device->calibration_ppb;
1526 return AIRSPYHF_SUCCESS;
1527 }
1528
1529 return AIRSPYHF_ERROR;
1530 }
1531
1532 int ADDCALL airspyhf_set_calibration(airspyhf_device_t* device, int32_t ppb)
1533 {
1534 device->calibration_ppb = ppb;
1535 return airspyhf_set_freq_double(device, device->freq_hz);
1536 }
1537
1538 int ADDCALL airspyhf_get_vctcxo_calibration(airspyhf_device_t* device, uint16_t* vc)
1539 {
1540 if (vc)
1541 {
1542 *vc = device->calibration_vctcxo;
1543 return AIRSPYHF_SUCCESS;
1544 }
1545
1546 return AIRSPYHF_ERROR;
1547 }
1548
1549 int ADDCALL airspyhf_set_vctcxo_calibration(airspyhf_device_t* device, uint16_t vc)
1550 {
1551 int result;
1552 device->calibration_vctcxo = vc;
1553
1554 result = libusb_control_transfer(
1555 device->usb_device,
1556 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1557 AIRSPYHF_SET_VCTCXO_CALIBRATION,
1558 vc,
1559 0,
1560 NULL,
1561 0,
1562 LIBUSB_CTRL_TIMEOUT_MS);
1563
1564 if (result < 0)
1565 {
1566 return AIRSPYHF_ERROR;
1567 }
1568 return AIRSPYHF_SUCCESS;
1569 }
1570
1571 int ADDCALL airspyhf_flash_configuration(airspyhf_device_t* device)
1572 {
1573 flash_config_t config;
1574 config.magic_number = CALIBRATION_MAGIC;
1575 config.calibration_ppb = device->calibration_ppb;
1576 config.calibration_vctcxo = device->calibration_vctcxo;
1577 config.frontend_options = device->frontend_options;
1578
1579 if (airspyhf_is_streaming(device))
1580 {
1581 return AIRSPYHF_ERROR;
1582 }
1583
1584 if (airspyhf_config_write(device, (uint8_t *)&config, sizeof(config)) != AIRSPYHF_SUCCESS)
1585 {
1586 return AIRSPYHF_ERROR;
1587 }
1588
1589 return AIRSPYHF_SUCCESS;
1590 }
1591
1592 int ADDCALL airspyhf_set_optimal_iq_correction_point(airspyhf_device_t* device, float w)
1593 {
1594 device->optimal_point = w;
1595 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1596 return AIRSPYHF_SUCCESS;
1597 }
1598
1599 int ADDCALL airspyhf_iq_balancer_configure(airspyhf_device_t* device, int buffers_to_skip, int fft_integration, int fft_overlap, int correlation_integration)
1600 {
1601 iq_balancer_configure(device->iq_balancer, buffers_to_skip, fft_integration, fft_overlap, correlation_integration);
1602 return AIRSPYHF_SUCCESS;
1603 }
1604
1605 int ADDCALL airspyhf_set_lib_dsp(airspyhf_device_t* device, const uint8_t flag)
1606 {
1607 device->enable_dsp = flag;
1608 return AIRSPYHF_SUCCESS;
1609 }
1610
1611 int ADDCALL airspyhf_board_partid_serialno_read(airspyhf_device_t* device, airspyhf_read_partid_serialno_t* read_partid_serialno)
1612 {
1613 uint8_t length;
1614 int result;
1615
1616 length = sizeof(airspyhf_read_partid_serialno_t);
1617 result = libusb_control_transfer(
1618 device->usb_device,
1619 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1620 AIRSPYHF_GET_SERIALNO_BOARDID,
1621 0,
1622 0,
1623 (unsigned char*)read_partid_serialno,
1624 length,
1625 LIBUSB_CTRL_TIMEOUT_MS);
1626
1627 if (result < length)
1628 {
1629 return AIRSPYHF_ERROR;
1630 }
1631
1632 return AIRSPYHF_SUCCESS;
1633 }
1634
1635 int ADDCALL airspyhf_version_string_read(airspyhf_device_t* device, char* version, uint8_t length)
1636 {
1637 int result;
1638 char version_local[MAX_VERSION_STRING_SIZE];
1639
1640 result = libusb_control_transfer(
1641 device->usb_device,
1642 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1643 AIRSPYHF_GET_VERSION_STRING,
1644 0,
1645 0,
1646 (unsigned char*) version_local,
1647 (MAX_VERSION_STRING_SIZE - 1),
1648 LIBUSB_CTRL_TIMEOUT_MS);
1649
1650 if (result < 0)
1651 {
1652 return AIRSPYHF_ERROR;
1653 }
1654 else
1655 {
1656 if (length > 0)
1657 {
1658 memcpy(version, version_local, length - 1);
1659 version[length - 1] = 0;
1660 return AIRSPYHF_SUCCESS;
1661 }
1662 else
1663 {
1664 return AIRSPYHF_ERROR;
1665 }
1666 }
1667 }
1668
1669 int ADDCALL airspyhf_get_att_steps(airspyhf_device_t* device, void* buffer, const uint32_t len)
1670 {
1671 if (len == 0)
1672 {
1673 *(uint32_t*)buffer = device->supported_att_step_count;
1674 }
1675 else if (len <= device->supported_att_step_count)
1676 {
1677 memcpy(buffer, device->supported_att_steps, len * sizeof(float));
1678 }
1679 else
1680 {
1681 return AIRSPYHF_ERROR;
1682 }
1683
1684 return AIRSPYHF_SUCCESS;
1685 }
1686
1687 int ADDCALL airspyhf_set_att(airspyhf_device_t* device, float att)
1688 {
1689 int result;
1690 uint16_t att_index = 0;
1691
1692 for (uint32_t i = 0; i < device->supported_att_step_count; i++)
1693 {
1694 if (device->supported_att_steps[i] >= att)
1695 {
1696 att_index = i;
1697 break;
1698 }
1699 }
1700
1701 result = libusb_control_transfer(
1702 device->usb_device,
1703 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1704 AIRSPYHF_SET_ATT,
1705 att_index,
1706 0,
1707 NULL,
1708 0,
1709 LIBUSB_CTRL_TIMEOUT_MS);
1710
1711 if (result < 0)
1712 {
1713 return AIRSPYHF_ERROR;
1714 }
1715
1716 return AIRSPYHF_SUCCESS;
1717 }
1718
1719 int ADDCALL airspyhf_set_bias_tee(airspyhf_device_t* device, int8_t value)
1720 {
1721 int result;
1722
1723 result = libusb_control_transfer(
1724 device->usb_device,
1725 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1726 AIRSPYHF_SET_BIAS_TEE,
1727 (uint16_t)value,
1728 0,
1729 NULL,
1730 0,
1731 LIBUSB_CTRL_TIMEOUT_MS);
1732
1733 if (result < 0)
1734 {
1735 return AIRSPYHF_ERROR;
1736 }
1737
1738 return AIRSPYHF_SUCCESS;
1739 }
1740
1741 int ADDCALL airspyhf_get_bias_tee_count(airspyhf_device_t* device, int32_t* value)
1742 {
1743 uint8_t length;
1744 int result;
1745
1746 length = sizeof(airspyhf_read_partid_serialno_t);
1747 result = libusb_control_transfer(
1748 device->usb_device,
1749 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1750 AIRSPYHF_GET_BIAS_TEE_COUNT,
1751 0,
1752 0,
1753 (unsigned char*)value,
1754 length,
1755 LIBUSB_CTRL_TIMEOUT_MS);
1756
1757 if (result < length)
1758 {
1759 *value = 0;
1760 return AIRSPYHF_ERROR;
1761 }
1762
1763 return AIRSPYHF_SUCCESS;
1764 }
1765
1766 int ADDCALL airspyhf_get_bias_tee_name(airspyhf_device_t* device, int32_t index, char* version, uint8_t length)
1767 {
1768 int result;
1769 char name_local[MAX_NAME_STRING_SIZE];
1770
1771 result = libusb_control_transfer(
1772 device->usb_device,
1773 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1774 AIRSPYHF_GET_BIAS_TEE_NAME,
1775 0,
1776 (int16_t)index,
1777 (unsigned char*)name_local,
1778 (MAX_NAME_STRING_SIZE - 1),
1779 LIBUSB_CTRL_TIMEOUT_MS);
1780
1781 if (result < 0)
1782 {
1783 return AIRSPYHF_ERROR;
1784 }
1785 else
1786 {
1787 if (length > 0)
1788 {
1789 memcpy(version, name_local, length - 1);
1790 version[length - 1] = 0;
1791 return AIRSPYHF_SUCCESS;
1792 }
1793 else
1794 {
1795 return AIRSPYHF_ERROR;
1796 }
1797 }
1798 }
1799
1800 // Legacy for HF+ dual and HF+ Discovery
1801
1802 int ADDCALL airspyhf_set_hf_att(airspyhf_device_t* device, uint8_t att_index)
1803 {
1804 int result;
1805
1806 result = libusb_control_transfer(
1807 device->usb_device,
1808 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1809 AIRSPYHF_SET_ATT,
1810 (uint16_t)att_index,
1811 0,
1812 NULL,
1813 0,
1814 LIBUSB_CTRL_TIMEOUT_MS);
1815
1816 if (result < 0)
1817 {
1818 return AIRSPYHF_ERROR;
1819 }
1820
1821 return AIRSPYHF_SUCCESS;
1822 }
1823
1824 int ADDCALL airspyhf_set_hf_lna(airspyhf_device_t* device, uint8_t flag)
1825 {
1826 int result;
1827
1828 result = libusb_control_transfer(
1829 device->usb_device,
1830 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1831 AIRSPYHF_SET_LNA,
1832 (uint16_t) flag,
1833 0,
1834 NULL,
1835 0,
1836 LIBUSB_CTRL_TIMEOUT_MS);
1837
1838 if (result < 0)
1839 {
1840 return AIRSPYHF_ERROR;
1841 }
1842
1843 return AIRSPYHF_SUCCESS;
1844 }
1845
1846 int ADDCALL airspyhf_set_user_output(airspyhf_device_t* device, airspyhf_user_output_t pin, airspyhf_user_output_state_t value)
1847 {
1848 int result;
1849
1850 result = libusb_control_transfer(
1851 device->usb_device,
1852 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1853 AIRSPYHF_SET_USER_OUTPUT,
1854 (uint16_t)pin,
1855 (uint16_t)value,
1856 NULL,
1857 0,
1858 LIBUSB_CTRL_TIMEOUT_MS);
1859
1860 if (result < 0)
1861 {
1862 return AIRSPYHF_ERROR;
1863 }
1864
1865 return AIRSPYHF_SUCCESS;
1866 }
1867
1868 int ADDCALL airspyhf_set_hf_agc(airspyhf_device_t* device, uint8_t flag)
1869 {
1870 int result;
1871
1872 result = libusb_control_transfer(
1873 device->usb_device,
1874 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1875 AIRSPYHF_SET_AGC,
1876 (uint16_t)flag,
1877 0,
1878 NULL,
1879 0,
1880 LIBUSB_CTRL_TIMEOUT_MS);
1881
1882 if (result < 0)
1883 {
1884 return AIRSPYHF_ERROR;
1885 }
1886
1887 return AIRSPYHF_SUCCESS;
1888 }
1889
1890 int ADDCALL airspyhf_set_hf_agc_threshold(airspyhf_device_t* device, uint8_t flag)
1891 {
1892 int result;
1893
1894 result = libusb_control_transfer(
1895 device->usb_device,
1896 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1897 AIRSPYHF_SET_AGC_THRESHOLD,
1898 (uint16_t)flag,
1899 0,
1900 NULL,
1901 0,
1902 LIBUSB_CTRL_TIMEOUT_MS);
1903
1904 if (result < 0)
1905 {
1906 return AIRSPYHF_ERROR;
1907 }
1908
1909 return AIRSPYHF_SUCCESS;
1910 }