-
Notifications
You must be signed in to change notification settings - Fork 1
/
hackrf_buffer.c
116 lines (97 loc) · 2.74 KB
/
hackrf_buffer.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <libhackrf/hackrf.h>
#include <pthread.h>
#include <string.h>
#include "hackrf_buffer.h"
static pthread_mutex_t hackrfBufferLock;
static int hackrfBufferHead;
static int hackrfBufferTail;
static int hackrfBufferEnd;
#define HACKRF_BUFFER_SIZE (1024 * 1024 * 8)
static uint8_t *hackrfBuffer;
static int verbose = 0;
void hackrf_buffer_init(int size, int _verbose)
{
if (size == 0) {
size = 1024 * 1024 *8;
}
verbose = _verbose;
hackrfBuffer = malloc(size);
if ( !hackrfBuffer ) {
fprintf(stderr,"Unable to allocate hackrfBuffer\n");
exit(1);
}
hackrfBufferHead = 0;
hackrfBufferTail = 0;
hackrfBufferEnd = size;
pthread_mutex_init(&hackrfBufferLock, NULL);
}
int hackrf_rx_callback(hackrf_transfer* transfer) {
int i;
if ( verbose ) {
fprintf(stderr,"rx_callback %d bytes\n", transfer -> valid_length);
}
int overflow = 0;
pthread_mutex_lock(&hackrfBufferLock);
/*
* If there isn't enough room, move things to the front
*/
int inBuffer = hackrfBufferTail - hackrfBufferHead;
int tailLeft = hackrfBufferEnd - hackrfBufferTail;
if ( inBuffer + transfer -> valid_length > hackrfBufferEnd ) {
/* reader not keeping up, signal overflow and reset buffer */
overflow = 1;
hackrfBufferHead = 0;
hackrfBufferTail = 0;
} else if (tailLeft < transfer -> valid_length) {
memcpy(&hackrfBuffer[0], &hackrfBuffer[hackrfBufferHead], inBuffer);
hackrfBufferHead = 0;
hackrfBufferTail = inBuffer;
}
memcpy(&hackrfBuffer[hackrfBufferTail],
transfer -> buffer, transfer -> valid_length);
hackrfBufferTail += transfer -> valid_length;
pthread_mutex_unlock(&hackrfBufferLock);
if ( overflow && verbose ) {
fprintf(stderr,"hackrf_buffer overflow!\n");
}
if ( verbose ) {
fprintf(stderr,"Exit hackrf_rx_callback\n");
}
return 0; /* always be happy */
}
void
hackrf_buffer_reset()
{
pthread_mutex_lock(&hackrfBufferLock);
hackrfBufferHead = 0;
hackrfBufferTail = 0;
pthread_mutex_unlock(&hackrfBufferLock);
}
void hackrf_read_sync(uint8_t *buffer, int bufsize, int *bytesread)
{
while (hackrfBufferTail - hackrfBufferHead < bufsize) {
if ( verbose ) {
fprintf(stderr,"wait for buffered data..\n");
}
usleep(1000);
}
pthread_mutex_lock(&hackrfBufferLock);
/*
* If there isn't enough room, move things to the front
*/
int inBuffer = hackrfBufferTail - hackrfBufferHead;
int tailLeft = hackrfBufferEnd - hackrfBufferTail;
int toMove = bufsize;
if (inBuffer < bufsize) {
toMove = inBuffer;
}
if (toMove > 0) {
memcpy(buffer, &hackrfBuffer[hackrfBufferHead], toMove);
hackrfBufferHead += toMove;
}
pthread_mutex_unlock(&hackrfBufferLock);
*bytesread = toMove;
}