Debian Codegraph @

libairspyhf/src/airspyhf.c

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