Debian Codegraph @

libairspyhf/src/airspyhf.c

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