Debian Codegraph @

libairspyhf/src/airspyhf.c

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