Debian Codegraph @

libairspyhf/src/airspyhf.c

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