-
Notifications
You must be signed in to change notification settings - Fork 1
/
py_hackrf.c
781 lines (628 loc) · 22.9 KB
/
py_hackrf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#include <Python.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <libhackrf/hackrf.h>
#include "queue.h"
#if defined(DEBUG) && (DEBUG == 1)
#include <stdio.h>
#define DEBUG_OUT(...) printf(__VA_ARGS__);
#else
#define DEBUG_OUT(...)
#endif
struct packet {
int8_t *buf;
size_t size;
};
typedef struct {
PyObject_HEAD
hackrf_device *device;
struct queue pkt_queue;
struct packet data_pkt;
size_t tx_len;
size_t tx_idx;
size_t rx_idx;
bool allow_overruns;
volatile bool busy;
} HackrfObject;
static struct queue **queue_list;
static int queue_list_size;
static void (*py_sigint_handler)(int);
static int pkt_allocate(HackrfObject *self, size_t size) {
if (self->data_pkt.buf != NULL) {
DEBUG_OUT("buffer is dirty - reallocation\n");
self->data_pkt.buf = (int8_t *) realloc(self->data_pkt.buf, size);
} else {
DEBUG_OUT("allocating buffer, size %zu\n", size);
self->data_pkt.buf = (int8_t *) malloc(size);
}
if (self->data_pkt.buf == NULL) {
return -1;
}
self->data_pkt.size = size;
return 0;
}
static void pkt_free(HackrfObject *self) {
if (self->data_pkt.buf != NULL) {
DEBUG_OUT("freeing buffer\n");
free(self->data_pkt.buf);
self->data_pkt.buf = NULL;
self->data_pkt.size = 0;
}
}
static void flush_queue(struct queue *q) {
struct packet pkt;
while (queue_pop_noblock(q, &pkt)) {
if (pkt.buf != NULL) {
free(pkt.buf);
}
}
}
static void flush_callback(void *flush_ctx, int success) {
DEBUG_OUT("flush callback: %d\n", success);
HackrfObject *self = (HackrfObject *) flush_ctx;
self->busy = false;
}
static int tx_callback(hackrf_transfer *transfer) {
HackrfObject *self = (HackrfObject *) transfer->tx_ctx;
int ret = 0;
if (!self->busy) {
DEBUG_OUT("tx done!\n");
return -1;
}
size_t buffer_length = transfer->buffer_length;
size_t len = buffer_length;
if (self->tx_idx + buffer_length >= self->data_pkt.size) {
len = self->data_pkt.size - self->tx_idx;
ret = -1;
}
DEBUG_OUT("buffer_length = %zu, len = %zu\n", buffer_length, len);
memcpy(transfer->buffer, self->data_pkt.buf + self->tx_idx, len);
self->tx_idx += buffer_length;
return ret;
}
static int rx_callback(hackrf_transfer *transfer) {
DEBUG_OUT("buffer_length = %d\n", transfer->valid_length);
HackrfObject *self = (HackrfObject *) transfer->rx_ctx;
if (!self->busy) {
DEBUG_OUT("rx done!\n");
return -1;
}
size_t len = transfer->valid_length;
if (self->rx_idx + len >= self->data_pkt.size) {
len = self->data_pkt.size - self->rx_idx;
DEBUG_OUT("rx last chunk, len = %zu\n", len);
memcpy(self->data_pkt.buf + self->rx_idx, transfer->buffer, len);
self->busy = false;
return -1;
}
memcpy(self->data_pkt.buf + self->rx_idx, transfer->buffer, len);
self->rx_idx += len;
return 0;
}
static int rx_stream_callback(hackrf_transfer *transfer) {
DEBUG_OUT("rx_len = %d\n", transfer->valid_length);
HackrfObject *self = (HackrfObject *) transfer->rx_ctx;
struct packet pkt;
if (!self->busy) {
DEBUG_OUT("rx done!\n");
return -1;
}
if (transfer->valid_length > 0) {
pkt.buf = malloc(transfer->valid_length);
if (pkt.buf == NULL) {
DEBUG_OUT("unable to allocate memory for rx stream\n");
return -1;
}
memcpy(pkt.buf, transfer->buffer, transfer->valid_length);
pkt.size = transfer->valid_length;
if (!queue_push_noblock(&self->pkt_queue, &pkt)) {
if (!self->allow_overruns) {
goto RX_STREAM_CLEANUP;
}
// pop first element and push new one (circular buffer)
struct packet p;
if (!queue_pop_noblock(&self->pkt_queue, &p)) {
goto RX_STREAM_CLEANUP;
}
free(p.buf);
if (!queue_push_noblock(&self->pkt_queue, &pkt)) {
goto RX_STREAM_CLEANUP;
}
}
}
return 0;
RX_STREAM_CLEANUP:
DEBUG_OUT("rx queue full - dropping pkt\n");
free(pkt.buf);
self->busy = false;
return -1;
}
static int tx_stream_callback(hackrf_transfer *transfer) {
HackrfObject *self = (HackrfObject *) transfer->tx_ctx;
size_t idx = 0;
if (!self->busy) {
DEBUG_OUT("tx done!\n");
return -1;
}
if (self->data_pkt.buf) {
if (self->tx_len > (size_t) transfer->buffer_length) {
DEBUG_OUT("tx draining pkt: %zu\n", self->tx_len);
memcpy(transfer->buffer, self->data_pkt.buf + self->tx_idx, transfer->buffer_length);
self->tx_idx += transfer->buffer_length;
self->tx_len -= transfer->buffer_length;
return 0;
} else {
DEBUG_OUT("tx drained pkt: %zu\n", self->tx_len);
memcpy(transfer->buffer, self->data_pkt.buf + self->tx_idx, self->tx_len);
idx = self->tx_len;
free(self->data_pkt.buf);
self->data_pkt.buf = NULL;
}
}
// pop messages from queue until we have enough data to send
size_t remaining_bytes = transfer->buffer_length - idx;
while (remaining_bytes > 0) {
if (!queue_pop_noblock(&self->pkt_queue, &self->data_pkt)) {
DEBUG_OUT("tx queue is empty - idling\n");
memset(transfer->buffer + idx, 0, remaining_bytes);
return self->allow_overruns ? 0 : -1;
}
if (self->data_pkt.size > remaining_bytes) {
DEBUG_OUT("tx pkt_size = %zu, remaining = %zu\n", self->data_pkt.size, remaining_bytes);
memcpy(transfer->buffer + idx, self->data_pkt.buf, remaining_bytes);
self->tx_idx = remaining_bytes;
self->tx_len = self->data_pkt.size - remaining_bytes;
return 0;
}
DEBUG_OUT("tx copy %zu, idx = %zu\n", self->data_pkt.size, idx);
memcpy(transfer->buffer + idx, self->data_pkt.buf, self->data_pkt.size);
idx += self->data_pkt.size;
free(self->data_pkt.buf);
self->data_pkt.buf = NULL;
}
DEBUG_OUT("tx %d\n", transfer->buffer_length);
return 0;
}
static PyObject *py_set_sample_rate(HackrfObject *self, PyObject *args) {
uint64_t sample_rate;
if (!PyArg_ParseTuple(args, "K", &sample_rate)) {
PyErr_SetString(PyExc_TypeError, "argument must be unsigned long long");
Py_RETURN_NONE;
}
hackrf_set_sample_rate(self->device, sample_rate);
Py_RETURN_NONE;
}
static PyObject *py_set_freq(HackrfObject *self, PyObject *args) {
uint64_t freq;
if (!PyArg_ParseTuple(args, "K", &freq)) {
PyErr_SetString(PyExc_TypeError, "argument must be unsigned long long");
Py_RETURN_NONE;
}
hackrf_set_freq(self->device, freq);
Py_RETURN_NONE;
}
static PyObject *py_set_baseband_filter_bandwidth(HackrfObject *self, PyObject *args) {
uint32_t freq;
if (!PyArg_ParseTuple(args, "I", &freq)) {
PyErr_SetString(PyExc_TypeError, "argument must be an integer");
Py_RETURN_NONE;
}
int ok = hackrf_set_baseband_filter_bandwidth(self->device, freq);
return PyLong_FromLong(ok);
}
static PyObject *py_set_tx_gain(HackrfObject *self, PyObject *args) {
uint32_t gain;
if (!PyArg_ParseTuple(args, "I", &gain)) {
PyErr_SetString(PyExc_TypeError, "argument must be an integer");
Py_RETURN_NONE;
}
int ok = hackrf_set_txvga_gain(self->device, gain);
return PyLong_FromLong(ok);
}
static PyObject *py_set_rx_gain(HackrfObject *self, PyObject *args) {
uint32_t lna_gain, vga_gain;
int ok = 0;
if (!PyArg_ParseTuple(args, "II", &vga_gain, &lna_gain)) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
Py_RETURN_NONE;
}
if (hackrf_set_vga_gain(self->device, vga_gain) != HACKRF_SUCCESS) {
ok = -1;
DEBUG_OUT("could not set vga_gain\n");
}
if (hackrf_set_lna_gain(self->device, lna_gain) != HACKRF_SUCCESS) {
ok = -1;
DEBUG_OUT("could not set lna_gain\n");
}
return PyLong_FromLong(ok);
}
static PyObject *py_set_amp(HackrfObject *self, PyObject *args) {
uint32_t enable;
if (!PyArg_ParseTuple(args, "I", &enable)) {
PyErr_SetString(PyExc_TypeError, "argument must be int");
Py_RETURN_NONE;
}
int ok = hackrf_set_amp_enable(self->device, (uint8_t) enable);
return PyLong_FromLong(ok);
}
static PyObject *py_set_antenna_enable(HackrfObject *self, PyObject *args) {
int enable;
if (!PyArg_ParseTuple(args, "p", &enable)) {
PyErr_SetString(PyExc_TypeError, "argument must be bool");
Py_RETURN_NONE;
}
int ok = hackrf_set_antenna_enable(self->device, (uint8_t) enable);
return PyLong_FromLong(ok);
}
static PyObject *py_set_hw_sync_mode(HackrfObject *self, PyObject *args) {
int enable;
if (!PyArg_ParseTuple(args, "p", &enable)) {
PyErr_SetString(PyExc_TypeError, "argument must be bool");
Py_RETURN_NONE;
}
int ok = hackrf_set_hw_sync_mode(self->device, (uint8_t) enable);
return PyLong_FromLong(ok);
}
static PyObject *py_read(HackrfObject *self, PyObject *Py_UNUSED(unused)) {
if (self->busy) {
DEBUG_OUT("rx busy\n");
Py_RETURN_NONE;
}
PyObject *array = PyByteArray_FromStringAndSize((const char *) self->data_pkt.buf, self->data_pkt.size);
pkt_free(self);
return array;
}
static PyObject *py_pop(HackrfObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"block", "timeout", NULL};
bool block = true;
uint32_t timeout = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pI", kwlist, &block, &timeout)) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
Py_RETURN_NONE;
}
if (self->pkt_queue.size == 0) {
PyErr_SetString(PyExc_RuntimeError, "queue not initialized");
Py_RETURN_NONE;
}
struct packet pkt = {0};
bool ok = block ? queue_pop(&self->pkt_queue, &pkt, timeout) :
queue_pop_noblock(&self->pkt_queue, &pkt);
if (ok) {
if (pkt.buf == NULL) {
DEBUG_OUT("rx thread: buffer is null\n");
Py_RETURN_NONE;
}
DEBUG_OUT("pop %zu bytes\n", pkt.size);
PyObject *array = PyByteArray_FromStringAndSize((const char *) pkt.buf, pkt.size);
free(pkt.buf);
return array;
}
Py_RETURN_NONE;
}
static PyObject *py_push(HackrfObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"item", "block", "timeout", NULL};
bool block = true;
uint32_t timeout = 0;
PyObject *tx_buf;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pI", kwlist, &tx_buf, &timeout, &block)) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
Py_RETURN_NONE;
}
if (!PyByteArray_Check(tx_buf)) {
PyErr_SetString(PyExc_TypeError, "argument must be a bytearray");
Py_RETURN_NONE;
}
if (self->pkt_queue.size == 0) {
PyErr_SetString(PyExc_RuntimeError, "queue not initialized");
Py_RETURN_NONE;
}
size_t len = PyByteArray_GET_SIZE(tx_buf);
struct packet pkt;
pkt.size = len;
pkt.buf = malloc(len);
if (pkt.buf == NULL) {
PyErr_NoMemory();
Py_RETURN_NONE;
}
memcpy(pkt.buf, PyByteArray_AS_STRING(tx_buf), len);
bool ok = block ? queue_push(&self->pkt_queue, &pkt, timeout) :
queue_push_noblock(&self->pkt_queue, &pkt);
if (!ok) {
DEBUG_OUT("rx queue full - dropping pkt\n");
free(pkt.buf);
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
static PyObject *py_start_rx(HackrfObject *self, PyObject *args) {
if (self->busy) {
Py_RETURN_FALSE;
}
size_t rx_len = 0;
if (!PyArg_ParseTuple(args, "K", &rx_len)) {
PyErr_SetString(PyExc_TypeError, "invalid length");
Py_RETURN_NONE;
}
if (pkt_allocate(self, rx_len) != 0) {
PyErr_NoMemory();
Py_RETURN_NONE;
}
self->rx_idx = 0;
int ok = hackrf_start_rx(self->device, rx_callback, (void *) self);
self->busy = (ok == HACKRF_SUCCESS);
return PyBool_FromLong(ok);
}
static PyObject *py_start_rx_stream(HackrfObject *self, PyObject *Py_UNUSED(unused)) {
if (self->busy) {
Py_RETURN_FALSE;
}
if (self->pkt_queue.size == 0) {
PyErr_SetString(PyExc_RuntimeError, "queue not initialized");
Py_RETURN_NONE;
}
flush_queue(&self->pkt_queue);
int ok = hackrf_start_rx(self->device, rx_stream_callback, (void *) self);
self->busy = (ok == HACKRF_SUCCESS);
return PyBool_FromLong(ok);
}
static PyObject *py_start_tx(HackrfObject *self, PyObject *args) {
if (self->busy) {
Py_RETURN_FALSE;
}
PyObject *tx_buf;
if (!PyArg_ParseTuple(args, "O", &tx_buf)) {
Py_RETURN_NONE;
}
if (!PyByteArray_Check(tx_buf)) {
PyErr_SetString(PyExc_TypeError, "argument must be a bytearray");
Py_RETURN_NONE;
}
size_t len = PyByteArray_GET_SIZE(tx_buf);
if (pkt_allocate(self, len) != 0) {
PyErr_NoMemory();
Py_RETURN_NONE;
}
memcpy(self->data_pkt.buf, PyByteArray_AS_STRING(tx_buf), len);
self->busy = true;
self->tx_idx = 0;
int ok = hackrf_start_tx(self->device, tx_callback, (void *) self);
return PyBool_FromLong(ok);
}
static PyObject *py_start_tx_stream(HackrfObject *self, PyObject *Py_UNUSED(unused)) {
if (self->busy) {
Py_RETURN_FALSE;
}
if (self->pkt_queue.size == 0) {
PyErr_SetString(PyExc_RuntimeError, "queue not initialized");
Py_RETURN_NONE;
}
flush_queue(&self->pkt_queue);
self->busy = true;
self->tx_len = 0;
self->tx_idx = 0;
int ok = hackrf_start_tx(self->device, tx_stream_callback, (void *) self);
return PyBool_FromLong(ok);
}
static PyObject *py_start_sweep(HackrfObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"freqs_list", "chunks", "step_width", "offset", NULL};
uint16_t frequencies[MAX_SWEEP_RANGES * 2];
uint32_t chunks;
uint32_t step_width;
uint32_t offset;
PyObject *freqs_list;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OIII", kwlist,
&freqs_list, &chunks, &step_width, &offset)) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
Py_RETURN_NONE;
}
Py_ssize_t size = PyList_Size(freqs_list);
if (size >= MAX_SWEEP_RANGES) {
PyErr_SetString(PyExc_ValueError, "number of ranges exceeds MAX_SWEEP_RANGES");
return NULL;
}
int ctr = 0;
for (Py_ssize_t i = 0; i < size; i++) {
PyObject* tuple = PyList_GetItem(freqs_list, i);
if (!PyTuple_Check(tuple) || PyTuple_Size(tuple) != 2) {
PyErr_SetString(PyExc_TypeError, "each element must be a tuple with two integers");
return NULL;
}
PyObject* v0 = PyTuple_GetItem(tuple, 0);
PyObject* v1 = PyTuple_GetItem(tuple, 1);
if (!PyLong_Check(v0) || !PyLong_Check(v1)) {
PyErr_SetString(PyExc_TypeError, "both elements in the tuple must be integers");
return NULL;
}
// Extract integer values
uint32_t freq_min = PyLong_AsLong(v0);
uint32_t freq_max = PyLong_AsLong(v1);
frequencies[ctr++] = freq_min;
frequencies[ctr++] = freq_max;
}
int ok = hackrf_init_sweep(
self->device,
frequencies,
size,
chunks * BYTES_PER_BLOCK,
step_width,
offset,
INTERLEAVED);
if (ok != HACKRF_SUCCESS) {
PyErr_SetString(PyExc_RuntimeError, "failed to initialize sweep");
Py_RETURN_FALSE;
}
ok = hackrf_start_rx_sweep(self->device, rx_stream_callback, (void *) self);
self->busy = (ok == HACKRF_SUCCESS);
return PyBool_FromLong(ok);
}
static PyObject *py_allow_overruns(HackrfObject *self, PyObject *args) {
PyObject *val;
if (!PyArg_ParseTuple(args, "O", &val)) {
PyErr_SetString(PyExc_TypeError, "argument must be bool");
Py_RETURN_NONE;
}
self->allow_overruns = (PyObject_IsTrue(val) > 0);
Py_RETURN_NONE;
}
static PyObject *py_stop_transfer(HackrfObject *self, PyObject *Py_UNUSED(unused)) {
self->busy = false;
hackrf_stop_tx(self->device); // same code used for rx
Py_RETURN_NONE;
}
static PyObject *py_busy(HackrfObject *self, PyObject *Py_UNUSED(unused)) {
return Py_NewRef(self->busy ? Py_True : Py_False);
}
static PyObject *py_set_fifo_size(HackrfObject *self, PyObject *args) {
uint32_t q_len;
if (!PyArg_ParseTuple(args, "I", &q_len) || q_len == 0) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
Py_RETURN_NONE;
}
flush_queue(&self->pkt_queue);
if (!queue_resize(&self->pkt_queue, q_len)) {
PyErr_NoMemory();
Py_RETURN_NONE;
}
Py_RETURN_NONE;
}
static int py_init(HackrfObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"fifo_len", "device_serial", NULL};
const char *serial = NULL;
uint32_t q_len = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Is", kwlist, &q_len, &serial)) {
PyErr_SetString(PyExc_TypeError, "invalid argument");
return -1;
}
if (hackrf_open_by_serial(serial, &self->device) != HACKRF_SUCCESS) {
PyErr_SetString(PyExc_RuntimeError, "failed to open hackrf");
return -1;
}
// queue_init with q_len = 0 is valid
if (!queue_init(&self->pkt_queue, sizeof(struct packet), q_len)) {
PyErr_SetString(PyExc_RuntimeError, "failed to initialize queue");
return -1;
}
// keep track of queues - used for cleanup
queue_list_size++;
queue_list = realloc(queue_list, queue_list_size * sizeof(struct queue *));
if (queue_list == NULL) {
PyErr_NoMemory();
return -1;
}
queue_list[queue_list_size - 1] = &self->pkt_queue;
hackrf_set_hw_sync_mode(self->device, 0);
hackrf_enable_tx_flush(self->device, flush_callback, (void*) self);
self->busy = false;
self->allow_overruns = false;
memset(&self->data_pkt, 0, sizeof(struct packet));
return 0;
}
static void py_dealloc(HackrfObject *self) {
hackrf_close(self->device);
flush_queue(&self->pkt_queue);
queue_deinit(&self->pkt_queue);
pkt_free(self);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject *py_device_list(PyObject *Py_UNUSED(unused)) {
hackrf_device_list_t *list = hackrf_device_list();
PyObject *devices = PyList_New(0);
for (int i = 0; i < list->devicecount; i++) {
PyObject *device = Py_BuildValue("s", list->serial_numbers[i]);
PyList_Append(devices, device);
Py_DECREF(device);
}
hackrf_device_list_free(list);
return devices;
}
static PyObject *py_bytes_per_transfer(PyObject *Py_UNUSED(unused)) {
// 16 blocks per transfer, defined in hackrf_sweep.c
return PyLong_FromLong(BYTES_PER_BLOCK * 16);
}
static void cleanup() {
hackrf_exit();
}
static void sigint_handler(int signum) {
DEBUG_OUT("terminating..\n");
for (int i = 0; i < queue_list_size; i++)
queue_terminate(queue_list[i]);
if (py_sigint_handler)
py_sigint_handler(signum);
}
static PyMethodDef hackrf_methods[] = {
{"busy", (PyCFunction) py_busy, METH_NOARGS, "check if transmission is in progress"},
{"set_fifo_size", (PyCFunction) py_set_fifo_size, METH_VARARGS, "set FIFO size"},
{"start_tx", (PyCFunction) py_start_tx, METH_VARARGS, "start transmission"},
{"start_rx", (PyCFunction) py_start_rx, METH_VARARGS, "start reception of fixed length"},
{"start_rx_stream", (PyCFunction) py_start_rx_stream, METH_NOARGS, "start rx stream"},
{"start_tx_stream", (PyCFunction) py_start_tx_stream, METH_NOARGS, "start tx stream"},
{"start_sweep", (PyCFunction) py_start_sweep, METH_VARARGS | METH_KEYWORDS,
"start rx sweep.\n"
"frequency_list - list of start-stop frequency pairs in MHz, must be less than 10\n"
"chunks - number of 16384 byte chunks to capture per tuning\n"
"step_width - width of each tuning step in Hz\n"
"offset - frequency offset added to tuned frequencies. sample_rate / 2 is a good value"
},
{"allow_overruns", (PyCFunction) py_allow_overruns, METH_VARARGS, "allow dropping packets"},
{"push", (PyCFunction) py_push, METH_VARARGS | METH_KEYWORDS, "push data to tx queue"},
{"pop", (PyCFunction) py_pop, METH_VARARGS | METH_KEYWORDS, "pop data from rx queue"},
{"read", (PyCFunction) py_read, METH_NOARGS, "read received data"},
{"set_sample_rate", (PyCFunction) py_set_sample_rate, METH_VARARGS, "set sample rate"},
{"set_freq", (PyCFunction) py_set_freq, METH_VARARGS, "set frequency"},
{"set_baseband_filter_bandwidth", (PyCFunction) py_set_baseband_filter_bandwidth, METH_VARARGS,
"set baseband filter bandwidth in Hz.\n"
"Possible values: 1.75, 2.5, 3.5, 5, 5.5, 6, 7, 8, 9, 10, 12, 14, 15, 20, 24, 28MHz"
},
{"set_tx_gain", (PyCFunction) py_set_tx_gain, METH_VARARGS, "set tx gain"},
{"set_rx_gain", (PyCFunction) py_set_rx_gain, METH_VARARGS, "set rx lna and vga"},
{"set_amp", (PyCFunction) py_set_amp, METH_VARARGS, "set rf amp state"},
{"set_antenna_enable", (PyCFunction) py_set_antenna_enable, METH_VARARGS, "toggle antenna port power"},
{"set_hw_sync_mode", (PyCFunction) py_set_hw_sync_mode, METH_VARARGS, "toggle hardware sync"},
{"stop_transfer", (PyCFunction) py_stop_transfer, METH_NOARGS, "stop rx/tx"},
{NULL}
};
static PyMethodDef module_method_table[] = {
{"device_list", (PyCFunction) py_device_list, METH_NOARGS, "list available hackrf devices"},
{"bytes_per_transfer", (PyCFunction) py_bytes_per_transfer, METH_NOARGS, "get number of bytes per usb transfer"},
{NULL, NULL, 0, NULL}
};
static PyTypeObject HackrfType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "py_hackrf.hackrf",
.tp_doc = "hackrf object",
.tp_basicsize = sizeof(HackrfObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = (initproc) py_init,
.tp_dealloc = (destructor) py_dealloc,
.tp_methods = hackrf_methods
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT, "py_hackrf", "hackrf python library", -1, module_method_table
};
PyMODINIT_FUNC PyInit_py_hackrf() {
if (PyType_Ready(&HackrfType) < 0)
return NULL;
PyObject *m = PyModule_Create(&module);
if (m == NULL)
return NULL;
Py_INCREF(&HackrfType);
if (PyModule_AddObject(m, "hackrf", (PyObject *) &HackrfType) < 0) {
Py_DECREF(&HackrfType);
Py_DECREF(m);
return NULL;
}
// save python's sigint handler and set our own
py_sigint_handler = signal(SIGINT, sigint_handler);
if (signal(SIGINT, sigint_handler) == SIG_ERR)
return NULL;
if (hackrf_init() != HACKRF_SUCCESS)
return NULL;
atexit(cleanup);
return m;
}