Debian Codegraph @

libairspyhf/src/airspyhf.c

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