-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
254 lines (220 loc) · 6 KB
/
main.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
/*
* Tracking Tower - Recieves and Processes data from SDRs.
* Copyright (C) 2018 Adam Morris <[email protected]>
* Copyright (C) 2018 Nicholas Borchardt <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Include Standard Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include SDR Library
#include "rtl-sdr.h"
// Include Project Libraries
#include "control.h"
#include "data_processing.h"
// Include external functions
extern void sdrs_setup(void);
extern void rtlsdr_setup(int, rtlsdr_dev_t *);
extern void rtlsdr_bias(int, uint8_t);
extern void collect(int, int, rtlsdr_dev_t *);
// Initialize variables
extern pthread_mutex_t file;
int sdr0[2];
int sdr1[2];
int sdr2[2];
int m_time = 100000;
// DSP Thread
void * dodsp(void * ptr)
{
// Initialize
uint8_t * sdr0Data = malloc(2*BSIZE);
uint8_t * sdr1Data = malloc(2*BSIZE);
uint8_t * sdr2Data = malloc(2*BSIZE);
memcpy(sdr0Data, sdrs[0].buffer[0], BSIZE);
memcpy(sdr0Data + BSIZE, sdrs[0].buffer[1], BSIZE);
memcpy(sdr1Data, sdrs[1].buffer[0], BSIZE);
memcpy(sdr1Data + BSIZE, sdrs[1].buffer[1], BSIZE);
memcpy(sdr2Data, sdrs[2].buffer[0], BSIZE);
memcpy(sdr2Data + BSIZE, sdrs[2].buffer[1], BSIZE);
fft_init();
correlateInit();
// Find Phase Difference
findPhaseDifference(sdr0Data, sdr1Data, sdr2Data);
// Find Signal in the Data Haystack
DSP(sdr0Data, sdr1Data, sdr2Data);
free(sdr0Data);
free(sdr1Data);
free(sdr2Data);
pthread_exit(NULL);
}
// Individual SDR Collection Thread
void * collect_t(void * ptr)
{
struct thread_struct * ts = (struct thread_struct *)ptr;
// Tell Super thread we're at collection
int val = 1;
if (ts->id == 0) {
write(sdr0[WRITE], &val, 1);
} else if (ts->id == 1) {
write(sdr1[WRITE], &val, 1);
} else if (ts->id == 2) {
write(sdr2[WRITE], &val, 1);
}
printf("here %d\n", ts->id);
// Wait for Super thread to continue
int ret = 1;
while(ret)
{
if (ts->id == 0) {
read(sdr0[READ], &ret, 1);
} else if (ts->id == 1) {
read(sdr1[READ], &ret, 1);
} else if (ts->id == 2) {
read(sdr2[READ], &ret, 1);
}
}
// Collect Data
collect(ts->id, ts->freq, ts->dev);
pthread_exit(NULL);
}
void * init_t(void * ptr)
{
struct thread_struct * ts = (struct thread_struct *)ptr;
rtlsdr_dev_t *dev = NULL;
rtlsdr_open(&dev, ts->id);
ts->dev = dev;
sdrs[ts->id].dev = dev;
rtlsdr_setup(ts->freq, dev);
rtlsdr_reset_buffer(dev);
rtlsdr_set_bias_tee(dev, 1);
pthread_exit(NULL);
}
// Main Program
int main(void)
{
// Declare variables
int i, j, n;
pthread_t dsp = (pthread_t)malloc(sizeof(pthread_t));
// Prepare structures
sdrs_setup();
// Initialize pipes
if((pipe(sdr0) < 0) || (pipe(sdr1) < 0) || (pipe(sdr2) < 0))
{
printf("\n pipe init has failed\n");
return 1;
}
// Initialize mutex
if(pthread_mutex_init(&file, NULL))
{
printf("\n mutex init has failed\n");
return 1;
}
n = 3;
while(1)
{
// for(n = 0; n < 4; ++n)
// {
struct thread_struct tmp[3];
for(i = 0; i < NUM_SDRS; ++i)
{
rtlsdr_dev_t *dev = NULL;
tmp[i].id = i;
tmp[i].freq = n;
tmp[i].dev = dev;
struct thread_struct * ts = &tmp[i];
if(pthread_create(&(sdrs[i].initialize_t), NULL, init_t, (void *)ts)) {
exit(1);
}
}
for(i = 0; i < NUM_SDRS; ++i)
{
if(pthread_join(sdrs[i].initialize_t, NULL)) {
exit(1);
}
}
rtlsdr_open(&(super.dev), 3);
for(j = 0; j < 2; ++j)
{
rtlsdr_bias(0, 0x1f);
printf("Frequency: %d\n", freq[n]);
// Create a collection thread for each RTL-SDR
for(i = 0; i < NUM_SDRS; ++i)
{
struct thread_struct * ts = &tmp[i];
if(pthread_create(&(sdrs[i].collection_t), NULL, collect_t, (void *)ts)) {
fprintf(stderr, "Error creating thread\n");
exit(1);
}
}
// Wait for SDRs to be at collection
int ret;
for(i = 0; i < NUM_SDRS; ++i)
{
ret = 0;
while(!ret)
{
if (i == 0) {
read(sdr0[READ], &ret, 1);
} else if (i == 1) {
read(sdr1[READ], &ret, 1);
} else if (i == 2) {
read(sdr2[READ], &ret, 1);
}
}
}
// Tell threads to continue
ret = 0;
write(sdr0[WRITE], &ret, 1);
write(sdr1[WRITE], &ret, 1);
write(sdr2[WRITE], &ret, 1);
// Sleep for 100 milliseconds
usleep(8000);
// Switch RTL-SDRs Bias for Data Collection
rtlsdr_bias(0, 0x00);
//usleep(10000);
//rtlsdr_bias(0, 0x1f);
// Wait for each collection thread to join
for(i = 0; i < NUM_SDRS; ++i)
{
if(pthread_join(sdrs[i].collection_t, NULL)) {
fprintf(stderr, "Error joining thread\n");
exit(1);
}
}
}
// Close RTL-SDR device
rtlsdr_close(sdrs[0].dev);
rtlsdr_close(sdrs[1].dev);
rtlsdr_close(sdrs[2].dev);
// Close Supervisory Channel
rtlsdr_close(super.dev);
// Perform DSP
/* if(pthread_create(&dsp, NULL, dodsp, (void *)NULL)) {
fprintf(stderr, "Error creating thread\n");
exit(1);
}
if(pthread_join(dsp, NULL)) {
fprintf(stderr, "Error joining thread\n");
exit(1);
}*/
// }
}
// Destroy mutex
pthread_mutex_destroy(&file);
// Free Space
free_controls();
return 0;
}