Debian Codegraph @

libairspyhf/src/airspyhf.c

raw symbols ◀ ▶
Source reconstructed at patch state 14 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 #ifdef __ANDROID__
819 // LibUSB does not support device discovery on android
820 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
821 #endif
822
823 if (libusb_init(&context) != 0)
824 {
825 return AIRSPYHF_ERROR;
826 }
827
828 if (libusb_get_device_list(context, &devices) < 0)
829 {
830 return AIRSPYHF_ERROR;
831 }
832
833 i = 0;
834 output_count = 0;
835 while ((dev = devices[i++]) != NULL && (!serials || output_count < count))
836 {
837 libusb_get_device_descriptor(dev, &device_descriptor);
838
839 if ((device_descriptor.idVendor == airspyhf_usb_vid) &&
840 (device_descriptor.idProduct == airspyhf_usb_pid))
841 {
842 serial_descriptor_index = device_descriptor.iSerialNumber;
843 if (serial_descriptor_index > 0)
844 {
845 if (libusb_open(dev, &libusb_dev_handle) != 0)
846 {
847 continue;
848 }
849
850 serial_number_len = libusb_get_string_descriptor_ascii(libusb_dev_handle,
851 serial_descriptor_index,
852 serial_number,
853 sizeof(serial_number));
854
855 if (serial_number_len == AIRSPYHF_SERIAL_SIZE && !memcmp(str_prefix_serial_airspyhf, serial_number, STR_PREFIX_SERIAL_AIRSPYHF_SIZE))
856 {
857 char *start, *end;
858 uint64_t serial;
859
860 serial_number[AIRSPYHF_SERIAL_SIZE] = 0;
861 start = (char*)(serial_number + STR_PREFIX_SERIAL_AIRSPYHF_SIZE);
862 end = NULL;
863 serial = strtoull(start, &end, 16);
864 if (serial == 0 && start == end)
865 {
866 libusb_close(libusb_dev_handle);
867 continue;
868 }
869
870 if (serials)
871 {
872 serials[output_count] = serial;
873 }
874 output_count++;
875 }
876
877 libusb_close(libusb_dev_handle);
878 }
879 }
880 }
881
882 libusb_free_device_list(devices, 1);
883 libusb_exit(context);
884 return output_count;
885 }
886
887 static int airspyhf_open_init(airspyhf_device_t** device, uint64_t serial_number, int fd)
888 {
889 airspyhf_device_t* lib_device;
890 calibration_record_t record;
891 int libusb_error;
892 int result;
893
894 *device = NULL;
895
896 lib_device = (airspyhf_device_t*) calloc(1, sizeof(airspyhf_device_t));
897 if (lib_device == NULL)
898 {
899 return AIRSPYHF_ERROR;
900 }
901
902 #ifdef __ANDROID__
903 // LibUSB does not support device discovery on android
904 libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
905 #endif
906
907 libusb_error = libusb_init(&lib_device->usb_context);
908 if (libusb_error != 0)
909 {
910 free(lib_device);
911 return AIRSPYHF_ERROR;
912 }
913
914 if (fd == FILE_DESCRIPTOR_UNUSED) {
915 airspyhf_open_device(lib_device,
916 &result,
917 airspyhf_usb_vid,
918 airspyhf_usb_pid,
919 serial_number);
920 }
921 else {
922 airspyhf_open_device_fd(lib_device,
923 &result,
924 fd);
925 }
926
927 if (lib_device->usb_device == NULL)
928 {
929 libusb_exit(lib_device->usb_context);
930 free(lib_device);
931 return result;
932 }
933
934 lib_device->transfers = NULL;
935 lib_device->callback = NULL;
936 lib_device->transfer_count = 16;
937 lib_device->buffer_size = SAMPLES_TO_TRANSFER * sizeof(airspyhf_complex_int16_t);
938 lib_device->streaming = false;
939 lib_device->stop_requested = false;
940
941 lib_device->supported_samplerates = NULL;
942 lib_device->samplerate_architectures = NULL;
943
944 result = airspyhf_read_samplerates_from_fw(lib_device, &lib_device->supported_samplerate_count, 0);
945 if (result == AIRSPYHF_SUCCESS)
946 {
947 lib_device->supported_samplerates = (uint32_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
948 result = airspyhf_read_samplerates_from_fw(lib_device, lib_device->supported_samplerates, lib_device->supported_samplerate_count);
949 if (result == AIRSPYHF_SUCCESS)
950 {
951 lib_device->samplerate_architectures = (uint8_t *)malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
952 result = airspyhf_read_samplerate_architectures_from_fw(lib_device, lib_device->samplerate_architectures, lib_device->supported_samplerate_count);
953 if (result != AIRSPYHF_SUCCESS)
954 {
955 memset(lib_device->samplerate_architectures, 0, lib_device->supported_samplerate_count * sizeof(uint8_t)); // Assume Zero IF for all
956 result = AIRSPYHF_SUCCESS; // Clear this error for backward compatibility.
957 }
958 }
959 else
960 {
961 free(lib_device->supported_samplerates);
962 lib_device->supported_samplerates = NULL;
963 }
964 }
965
966 if (result != AIRSPYHF_SUCCESS)
967 {
968 // Assume one default sample rate with Zero IF
969
970 lib_device->supported_samplerate_count = 1;
971 lib_device->supported_samplerates = (uint32_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint32_t));
972 lib_device->supported_samplerates[0] = DEFAULT_SAMPLERATE;
973
974 lib_device->samplerate_architectures = (uint8_t *) malloc(lib_device->supported_samplerate_count * sizeof(uint8_t));
975 lib_device->samplerate_architectures[0] = 0;
976 }
977
978 lib_device->current_samplerate = lib_device->supported_samplerates[0];
979 lib_device->is_low_if = lib_device->samplerate_architectures[0];
980
981 result = allocate_transfers(lib_device);
982 if (result != 0)
983 {
984 airspyhf_open_exit(lib_device);
985 free(lib_device);
986 return AIRSPYHF_ERROR;
987 }
988
989 pthread_cond_init(&lib_device->consumer_cv, NULL);
990 pthread_mutex_init(&lib_device->consumer_mp, NULL);
991
992 lib_device->freq_hz = 0;
993 lib_device->freq_khz = 0;
994 lib_device->freq_shift = 0;
995 lib_device->vec.re = 1.0f;
996 lib_device->vec.im = 0.0f;
997 lib_device->optimal_point = 0.0f;
998 lib_device->filter_gain = 1.0f;
999 lib_device->enable_dsp = 1;
1000
1001 if (airspyhf_config_read(lib_device, (uint8_t *) &record, sizeof(record)) == AIRSPYHF_SUCCESS)
1002 {
1003 if (record.magic_number == CALIBRATION_MAGIC)
1004 {
1005 lib_device->calibration_ppb = record.calibration_ppb;
1006 }
1007 else
1008 {
1009 lib_device->calibration_ppb = 0;
1010 }
1011 }
1012 else
1013 {
1014 lib_device->calibration_ppb = 0;
1015 }
1016
1017 lib_device->iq_balancer = iq_balancer_create(INITIAL_PHASE, INITIAL_AMPLITUDE);
1018
1019 *device = lib_device;
1020
1021 return AIRSPYHF_SUCCESS;
1022 }
1023
1024 void ADDCALL airspyhf_lib_version(airspyhf_lib_version_t* lib_version)
1025 {
1026 lib_version->major_version = AIRSPYHF_VER_MAJOR;
1027 lib_version->minor_version = AIRSPYHF_VER_MINOR;
1028 lib_version->revision = AIRSPYHF_VER_REVISION;
1029 }
1030
1031 int ADDCALL airspyhf_open_sn(airspyhf_device_t** device, uint64_t serial_number)
1032 {
1033 int result;
1034
1035 result = airspyhf_open_init(device, serial_number, FILE_DESCRIPTOR_UNUSED);
1036 return result;
1037 }
1038
1039 int ADDCALL airspyhf_open_fd(airspyhf_device_t** device, int fd)
1040 {
1041 int result;
1042
1043 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, fd);
1044 return result;
1045 }
1046
1047 int ADDCALL airspyhf_open(airspyhf_device_t** device)
1048 {
1049 int result;
1050
1051 result = airspyhf_open_init(device, SERIAL_NUMBER_UNUSED, FILE_DESCRIPTOR_UNUSED);
1052 return result;
1053 }
1054
1055 int ADDCALL airspyhf_close(airspyhf_device_t* device)
1056 {
1057 int result;
1058
1059 result = AIRSPYHF_SUCCESS;
1060
1061 if (device != NULL)
1062 {
1063 result = airspyhf_stop(device);
1064
1065 pthread_cond_destroy(&device->consumer_cv);
1066 pthread_mutex_destroy(&device->consumer_mp);
1067
1068 free_transfers(device);
1069 free(device->supported_samplerates);
1070 free(device->samplerate_architectures);
1071 iq_balancer_destroy(device->iq_balancer);
1072 airspyhf_open_exit(device);
1073 free(device);
1074 }
1075
1076 return result;
1077 }
1078
1079 int ADDCALL airspyhf_get_output_size(airspyhf_device_t * device)
1080 {
1081 // Todo: Make this configurable
1082 return SAMPLES_TO_TRANSFER;
1083 }
1084
1085 int ADDCALL airspyhf_is_low_if(airspyhf_device_t* device)
1086 {
1087 return device->is_low_if;
1088 }
1089
1090 int ADDCALL airspyhf_get_samplerates(airspyhf_device_t* device, uint32_t* buffer, const uint32_t len)
1091 {
1092 if (len == 0)
1093 {
1094 *buffer = device->supported_samplerate_count;
1095 }
1096 else if (len <= device->supported_samplerate_count)
1097 {
1098 memcpy(buffer, device->supported_samplerates, len * sizeof(uint32_t));
1099 }
1100 else
1101 {
1102 return AIRSPYHF_ERROR;
1103 }
1104
1105 return AIRSPYHF_SUCCESS;
1106 }
1107
1108 int ADDCALL airspyhf_set_samplerate(airspyhf_device_t* device, uint32_t samplerate)
1109 {
1110 int result;
1111 uint32_t i;
1112 uint8_t gain;
1113
1114 if (samplerate > MAX_SAMPLERATE_INDEX)
1115 {
1116 for (i = 0; i < device->supported_samplerate_count; i++)
1117 {
1118 if (samplerate == device->supported_samplerates[i])
1119 {
1120 samplerate = i;
1121 break;
1122 }
1123 }
1124 }
1125
1126 if (samplerate >= device->supported_samplerate_count)
1127 {
1128 return AIRSPYHF_ERROR;
1129 }
1130
1131 device->current_samplerate = device->supported_samplerates[samplerate];
1132 device->is_low_if = device->samplerate_architectures[samplerate];
1133
1134 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | 1);
1135
1136 if (!device->is_low_if && device->freq_khz < MIN_ZERO_IF_LO)
1137 {
1138 uint8_t buf[4];
1139 buf[0] = (uint8_t)((MIN_ZERO_IF_LO >> 24) & 0xff);
1140 buf[1] = (uint8_t)((MIN_ZERO_IF_LO >> 16) & 0xff);
1141 buf[2] = (uint8_t)((MIN_ZERO_IF_LO >> 8) & 0xff);
1142 buf[3] = (uint8_t)((MIN_ZERO_IF_LO) & 0xff);
1143
1144 result = libusb_control_transfer(
1145 device->usb_device,
1146 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1147 AIRSPYHF_SET_FREQ,
1148 0,
1149 0,
1150 (unsigned char*) &buf,
1151 sizeof(buf),
1152 0);
1153
1154 if (result < sizeof(buf))
1155 {
1156 return AIRSPYHF_ERROR;
1157 }
1158
1159 device->freq_khz = MIN_ZERO_IF_LO;
1160 }
1161
1162 result = libusb_control_transfer(
1163 device->usb_device,
1164 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1165 AIRSPYHF_SET_SAMPLERATE,
1166 0,
1167 samplerate,
1168 NULL,
1169 0,
1170 0
1171 );
1172
1173 if (result != 0)
1174 {
1175 return AIRSPYHF_ERROR;
1176 }
1177
1178 result = libusb_control_transfer(
1179 device->usb_device,
1180 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1181 AIRSPYHF_GET_FILTER_GAIN,
1182 0,
1183 0,
1184 &gain,
1185 sizeof(uint8_t),
1186 0);
1187
1188 if (result == sizeof(uint8_t))
1189 {
1190 device->filter_gain = powf(10.0f, (float) gain * -0.05f);
1191 }
1192 else
1193 {
1194 device->filter_gain = 1.0;
1195 }
1196
1197 airspyhf_set_freq(device, device->freq_hz);
1198
1199 return AIRSPYHF_SUCCESS;
1200 }
1201
1202 int ADDCALL airspyhf_set_receiver_mode(airspyhf_device_t* device, receiver_mode_t value)
1203 {
1204 int result;
1205 result = libusb_control_transfer(
1206 device->usb_device,
1207 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1208 AIRSPYHF_RECEIVER_MODE,
1209 value,
1210 0,
1211 NULL,
1212 0,
1213 0);
1214
1215 if (result != 0)
1216 {
1217 return AIRSPYHF_ERROR;
1218 }
1219
1220 return AIRSPYHF_SUCCESS;
1221 }
1222
1223 int ADDCALL airspyhf_start(airspyhf_device_t* device, airspyhf_sample_block_cb_fn callback, void* ctx)
1224 {
1225 int result;
1226
1227 memset(device->dropped_buffers_queue, 0, RAW_BUFFER_COUNT * sizeof(uint32_t));
1228 device->dropped_buffers = 0;
1229
1230 device->vec.re = 1.0f;
1231 device->vec.im = 0.0f;
1232
1233 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1234 if (result != AIRSPYHF_SUCCESS)
1235 {
1236 return result;
1237 }
1238
1239 libusb_clear_halt(device->usb_device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN);
1240
1241 result = airspyhf_set_receiver_mode(device, RECEIVER_MODE_ON);
1242 if (result == AIRSPYHF_SUCCESS)
1243 {
1244 device->ctx = ctx;
1245 result = create_io_threads(device, callback);
1246 }
1247
1248 return result;
1249 }
1250
1251 int ADDCALL airspyhf_is_streaming(airspyhf_device_t* device)
1252 {
1253 return device->streaming && !device->stop_requested;
1254 }
1255
1256 int ADDCALL airspyhf_stop(airspyhf_device_t* device)
1257 {
1258 int result1, result2;
1259 result1 = kill_io_threads(device);
1260 result2 = airspyhf_set_receiver_mode(device, RECEIVER_MODE_OFF);
1261 libusb_interrupt_event_handler(device->usb_context);
1262
1263 if (result2 != AIRSPYHF_SUCCESS)
1264 {
1265 return result2;
1266 }
1267 return result1;
1268 }
1269
1270 int ADDCALL airspyhf_set_freq(airspyhf_device_t* device, const uint32_t freq_hz)
1271 {
1272 const int tuning_alignment = 1000;
1273
1274 int result;
1275 uint8_t buf[4];
1276 uint32_t lo_low_khz = device->is_low_if ? MIN_LOW_IF_LO : MIN_ZERO_IF_LO;
1277 uint32_t if_shift = (device->enable_dsp && !device->is_low_if) ? DEFAULT_IF_SHIFT : 0;
1278 uint32_t adjusted_freq_hz = (uint32_t) ((int64_t) freq_hz * (int64_t)(1000000000LL + device->calibration_ppb) / 1000000000LL);
1279 uint32_t freq_khz = MAX(lo_low_khz, (adjusted_freq_hz + if_shift + tuning_alignment / 2) / tuning_alignment);
1280
1281 if (device->freq_khz != freq_khz)
1282 {
1283 buf[0] = (uint8_t) ((freq_khz >> 24) & 0xff);
1284 buf[1] = (uint8_t) ((freq_khz >> 16) & 0xff);
1285 buf[2] = (uint8_t) ((freq_khz >> 8) & 0xff);
1286 buf[3] = (uint8_t) ((freq_khz) & 0xff);
1287
1288 result = libusb_control_transfer(
1289 device->usb_device,
1290 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1291 AIRSPYHF_SET_FREQ,
1292 0,
1293 0,
1294 (unsigned char*) &buf,
1295 sizeof(buf),
1296 0);
1297
1298 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1299
1300 if (result < sizeof(buf))
1301 {
1302 return AIRSPYHF_ERROR;
1303 }
1304
1305 device->freq_khz = freq_khz;
1306 }
1307
1308 device->freq_hz = freq_hz;
1309 device->freq_shift = adjusted_freq_hz - freq_khz * 1000;
1310
1311 return AIRSPYHF_SUCCESS;
1312 }
1313
1314 static int airspyhf_config_write(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1315 {
1316 int result;
1317 uint8_t buf[256];
1318
1319 memset(buf, 0, sizeof(buf));
1320
1321 memcpy(buf, buffer, MIN(length, sizeof(buf)));
1322
1323 result = libusb_control_transfer(
1324 device->usb_device,
1325 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1326 AIRSPYHF_CONFIG_WRITE,
1327 0,
1328 0,
1329 buf,
1330 sizeof(buf),
1331 0);
1332
1333 if (result < sizeof(buf))
1334 {
1335 return AIRSPYHF_ERROR;
1336 }
1337
1338 return AIRSPYHF_SUCCESS;
1339 }
1340
1341 static int airspyhf_config_read(airspyhf_device_t* device, uint8_t *buffer, uint16_t length)
1342 {
1343 uint8_t buf[256];
1344 int result;
1345
1346 result = libusb_control_transfer(
1347 device->usb_device,
1348 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1349 AIRSPYHF_CONFIG_READ,
1350 0,
1351 0,
1352 buf,
1353 sizeof(buf),
1354 0);
1355
1356 memcpy(buffer, buf, MIN(length, sizeof(buf)));
1357
1358 if (result < sizeof(buf))
1359 {
1360 return AIRSPYHF_ERROR;
1361 }
1362
1363 return AIRSPYHF_SUCCESS;
1364 }
1365
1366 int ADDCALL airspyhf_get_calibration(airspyhf_device_t* device, int32_t* ppb)
1367 {
1368 if (ppb)
1369 {
1370 *ppb = device->calibration_ppb;
1371 return AIRSPYHF_SUCCESS;
1372 }
1373
1374 return AIRSPYHF_ERROR;
1375 }
1376
1377 int ADDCALL airspyhf_flash_calibration(airspyhf_device_t* device)
1378 {
1379 calibration_record_t record;
1380 record.magic_number = CALIBRATION_MAGIC;
1381 record.calibration_ppb = device->calibration_ppb;
1382
1383 if (airspyhf_is_streaming(device))
1384 {
1385 return AIRSPYHF_ERROR;
1386 }
1387
1388 if (airspyhf_config_write(device, (uint8_t *)&record, sizeof(record)) != AIRSPYHF_SUCCESS)
1389 {
1390 return AIRSPYHF_ERROR;
1391 }
1392
1393 return AIRSPYHF_SUCCESS;
1394 }
1395
1396 int ADDCALL airspyhf_set_calibration(airspyhf_device_t* device, int32_t ppb)
1397 {
1398 device->calibration_ppb = ppb;
1399 return airspyhf_set_freq(device, device->freq_hz);
1400 }
1401
1402 int ADDCALL airspyhf_set_optimal_iq_correction_point(airspyhf_device_t* device, float w)
1403 {
1404 device->optimal_point = w;
1405 iq_balancer_set_optimal_point(device->iq_balancer, device->optimal_point);
1406 return AIRSPYHF_SUCCESS;
1407 }
1408
1409 int ADDCALL airspyhf_iq_balancer_configure(airspyhf_device_t* device, int buffers_to_skip, int fft_integration, int fft_overlap, int correlation_integration)
1410 {
1411 iq_balancer_configure(device->iq_balancer, buffers_to_skip, fft_integration, fft_overlap, correlation_integration);
1412 return AIRSPYHF_SUCCESS;
1413 }
1414
1415 int ADDCALL airspyhf_board_partid_serialno_read(airspyhf_device_t* device, airspyhf_read_partid_serialno_t* read_partid_serialno)
1416 {
1417 uint8_t length;
1418 int result;
1419
1420 length = sizeof(airspyhf_read_partid_serialno_t);
1421 result = libusb_control_transfer(
1422 device->usb_device,
1423 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1424 AIRSPYHF_GET_SERIALNO_BOARDID,
1425 0,
1426 0,
1427 (unsigned char*)read_partid_serialno,
1428 length,
1429 0);
1430
1431 if (result < length)
1432 {
1433 return AIRSPYHF_ERROR;
1434 }
1435
1436 return AIRSPYHF_SUCCESS;
1437 }
1438
1439 int ADDCALL airspyhf_version_string_read(airspyhf_device_t* device, char* version, uint8_t length)
1440 {
1441 int result;
1442 char version_local[MAX_VERSION_STRING_SIZE];
1443
1444 result = libusb_control_transfer(
1445 device->usb_device,
1446 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1447 AIRSPYHF_GET_VERSION_STRING,
1448 0,
1449 0,
1450 (unsigned char*) version_local,
1451 (MAX_VERSION_STRING_SIZE - 1),
1452 0);
1453
1454 if (result < 0)
1455 {
1456 return AIRSPYHF_ERROR;
1457 }
1458 else
1459 {
1460 if (length > 0)
1461 {
1462 memcpy(version, version_local, length - 1);
1463 version[length - 1] = 0;
1464 return AIRSPYHF_SUCCESS;
1465 }
1466 else
1467 {
1468 return AIRSPYHF_ERROR;
1469 }
1470 }
1471 }
1472
1473 int ADDCALL airspyhf_set_user_output(airspyhf_device_t* device, airspyhf_user_output_t pin, airspyhf_user_output_state_t value)
1474 {
1475 int result;
1476
1477 result = libusb_control_transfer(
1478 device->usb_device,
1479 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1480 AIRSPYHF_SET_USER_OUTPUT,
1481 (uint16_t)pin,
1482 (uint16_t)value,
1483 NULL,
1484 0,
1485 0);
1486
1487 if (result < 0)
1488 {
1489 return AIRSPYHF_ERROR;
1490 }
1491
1492 return AIRSPYHF_SUCCESS;
1493 }
1494
1495 int ADDCALL airspyhf_set_hf_agc(airspyhf_device_t* device, uint8_t flag)
1496 {
1497 int result;
1498
1499 result = libusb_control_transfer(
1500 device->usb_device,
1501 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1502 AIRSPYHF_SET_HF_AGC,
1503 (uint16_t) flag,
1504 0,
1505 NULL,
1506 0,
1507 0);
1508
1509 if (result < 0)
1510 {
1511 return AIRSPYHF_ERROR;
1512 }
1513
1514 return AIRSPYHF_SUCCESS;
1515 }
1516
1517 int ADDCALL airspyhf_set_hf_agc_threshold(airspyhf_device_t* device, uint8_t flag)
1518 {
1519 int result;
1520
1521 result = libusb_control_transfer(
1522 device->usb_device,
1523 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1524 AIRSPYHF_SET_HF_AGC_THRESHOLD,
1525 (uint16_t) flag,
1526 0,
1527 NULL,
1528 0,
1529 0);
1530
1531 if (result < 0)
1532 {
1533 return AIRSPYHF_ERROR;
1534 }
1535
1536 return AIRSPYHF_SUCCESS;
1537 }
1538
1539 int ADDCALL airspyhf_set_hf_att(airspyhf_device_t* device, uint8_t value)
1540 {
1541 int result;
1542
1543 result = libusb_control_transfer(
1544 device->usb_device,
1545 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1546 AIRSPYHF_SET_HF_ATT,
1547 (uint16_t) value,
1548 0,
1549 NULL,
1550 0,
1551 0);
1552
1553 if (result < 0)
1554 {
1555 return AIRSPYHF_ERROR;
1556 }
1557
1558 return AIRSPYHF_SUCCESS;
1559 }
1560
1561 int ADDCALL airspyhf_set_hf_lna(airspyhf_device_t* device, uint8_t flag)
1562 {
1563 int result;
1564
1565 result = libusb_control_transfer(
1566 device->usb_device,
1567 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1568 AIRSPYHF_SET_HF_LNA,
1569 (uint16_t) flag,
1570 0,
1571 NULL,
1572 0,
1573 0);
1574
1575 if (result < 0)
1576 {
1577 return AIRSPYHF_ERROR;
1578 }
1579
1580 return AIRSPYHF_SUCCESS;
1581 }
1582
1583 int ADDCALL airspyhf_set_lib_dsp(airspyhf_device_t* device, const uint8_t flag)
1584 {
1585 device->enable_dsp = flag;
1586 return AIRSPYHF_SUCCESS;
1587 }
1588
1589 //#include "special.c"