forked from ka9q/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tune.c
418 lines (374 loc) · 11 KB
/
tune.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
// Interactive program to tune radiod in ka9q-radio
// Copyright 2023 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <getopt.h>
#include <unistd.h>
#if defined(linux)
#include <bsd/stdlib.h>
#include <bsd/string.h>
#else
#include <stdlib.h>
#endif
#include <string.h>
#include <sys/socket.h>
#include <locale.h>
#include <errno.h>
#include <sysexits.h>
#include <poll.h>
#include "misc.h"
#include "multicast.h"
#include "status.h"
int Mcast_ttl = 1;
int IP_tos = 0;
const char *App_path;
int Verbose;
char const *Radio = NULL;
char const *Locale = "en_US.UTF-8";
char const *Iface;
char const *Mode;
uint32_t Ssrc;
float Gain = INFINITY;
double Frequency = INFINITY;
float Low = INFINITY;
float High = INFINITY;
int Samprate = 0;
bool Quiet = false;
enum encoding Encoding = NO_ENCODING;
float RFgain = INFINITY;
float RFatten = INFINITY;
int Agc_enable = -1;
struct sockaddr_storage Control_address;
int Status_sock = -1;
int Control_sock = -1;
char Optstring[] = "aA:e:f:g:G:H:hi:L:l:m:qr:R:s:vV";
struct option Options[] = {
{"agc", no_argument, NULL, 'a'},
{"rfatten", required_argument, NULL, 'A'},
{"featten", required_argument, NULL, 'A'},
{"encoding", required_argument, NULL, 'e'},
{"frequency", required_argument, NULL, 'f'},
{"gain", required_argument, NULL, 'g'},
{"rfgain", required_argument, NULL, 'G'},
{"fegain", required_argument, NULL, 'G'},
{"help", no_argument, NULL, 'h'},
{"iface", required_argument, NULL, 'i'},
{"locale", required_argument, NULL, 'l'},
{"low", required_argument, NULL, 'L'},
{"high", required_argument, NULL, 'H'},
{"mode", required_argument, NULL, 'm'},
{"quiet", no_argument, NULL, 'q'},
{"radio", required_argument, NULL, 'r'},
{"samprate", required_argument, NULL, 'R'},
{"ssrc", required_argument, NULL, 's'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
void usage(void);
int main(int argc,char *argv[]){
App_path = argv[0];
{
char * const cp = getenv("LANG");
if(cp != NULL)
Locale = cp;
}
{
int c;
while((c = getopt_long(argc,argv,Optstring,Options,NULL)) != -1){
switch(c){
case 'a':
Agc_enable = 1;
break;
case 'A':
RFatten = strtod(optarg,NULL);
break;
case 'G':
RFgain = strtod(optarg,NULL);
break;
case 'e':
Encoding = parse_encoding(optarg);
if(Encoding == NO_ENCODING){
fprintf(stdout,"Unknown encoding %s\n",optarg);
fprintf(stdout,"Encodings: S16BE S16LE F32 F16 OPUS\n");
}
break;
case 'f':
Frequency = parse_frequency(optarg,true);
break;
case 'g':
Gain = strtod(optarg,NULL);
break;
case 'i':
Iface = optarg;
break;
case 'm':
Mode = optarg;
break;
case 's':
Ssrc = strtol(optarg,NULL,0);
break;
case 'v':
Verbose++;
break;
case 'l':
Locale = optarg;
break;
case 'r':
Radio = optarg;
break;
case 'q':
Quiet = true;
break;
case 'R':
Samprate = parse_frequency(optarg,false);
break;
case 'L':
Low = parse_frequency(optarg,false);
break;
case 'H':
High = parse_frequency(optarg,false);
break;
case 'V':
VERSION();
exit(EX_OK);
default:
fprintf(stdout,"Invalid command line option -%c\n",c);
case 'h':
usage();
exit(EX_USAGE);
}
}
// -r option not specified, see if it was given as an additional arg
if(Radio == NULL && argc >= optind)
Radio = argv[optind];
}
setlocale(LC_ALL,Locale);
if(Radio == NULL)
Radio = getenv("RADIO");
if(Radio == NULL){
fprintf(stdout,"--radio not specified and $RADIO not set\n");
usage();
exit(EX_USAGE);
}
if(Ssrc == 0){
fprintf(stdout,"--ssrc not specified\n");
usage();
exit(EX_USAGE);
}
{
if(Verbose)
fprintf(stdout,"Resolving %s\n",Radio);
char iface[1024];
resolve_mcast(Radio,&Control_address,DEFAULT_STAT_PORT,iface,sizeof(iface),0);
char const *ifc = (Iface != NULL) ? Iface : iface;
if(Verbose)
fprintf(stdout,"Listening\n");
Status_sock = listen_mcast(&Control_address,ifc);
if(Status_sock == -1){
fprintf(stdout,"Can't open Status_sock socket to radio control channel %s: %s\n",Radio,strerror(errno));
exit(EX_IOERR);
}
if(Verbose)
fprintf(stdout,"Connecting\n");
Control_sock = connect_mcast(&Control_address,ifc,Mcast_ttl,IP_tos);
if(Control_sock == -1){
fprintf(stdout,"Can't open cmd socket to radio control channel %s: %s\n",Radio,strerror(errno));
exit(EX_IOERR);
}
}
// Begin polling SSRC to ensure the multicast group is up and radiod is listening
long long last_command_time = 0;
if(Low > High){
float temp = Low;
Low = High;
High = temp;
}
uint32_t received_tag = 0;
double received_freq = INFINITY;
uint32_t received_ssrc = 0;
int received_agc_enable = -1;
float received_gain = INFINITY;
char *preset = NULL;
float noise_density = INFINITY;
float baseband_level = INFINITY;
float low_edge = INFINITY;
float high_edge = INFINITY;
float received_rf_gain = INFINITY;
float received_rf_atten = INFINITY;
enum encoding received_encoding = NO_ENCODING;
int received_rf_agc = -1;
int samprate = 0;
uint32_t sent_tag = 0;
while(true){
// (re)send command until we get a response;
if(gps_time_ns() >= last_command_time + BILLION/10){ // Rate limit command packets to 10 Hz
uint8_t cmd_buffer[PKTSIZE];
uint8_t *bp = cmd_buffer;
*bp++ = 1; // Generate command packet
sent_tag = arc4random();
encode_int(&bp,COMMAND_TAG,sent_tag);
encode_int(&bp,OUTPUT_SSRC,Ssrc);
if(Mode != NULL)
encode_string(&bp,PRESET,Mode,strlen(Mode));
if(Samprate != 0)
encode_int(&bp,OUTPUT_SAMPRATE,Samprate);
if(Low != INFINITY)
encode_float(&bp,LOW_EDGE,Low);
if(High != INFINITY)
encode_float(&bp,HIGH_EDGE,High);
if(Frequency != INFINITY)
encode_double(&bp,RADIO_FREQUENCY,Frequency); // Hz
if(Gain != INFINITY){
encode_float(&bp,GAIN,Gain);
encode_int(&bp,AGC_ENABLE,false); // Turn off AGC for manual gain
} else if(Agc_enable == 1)
encode_int(&bp,AGC_ENABLE,true);
if(Encoding != NO_ENCODING)
encode_int(&bp,OUTPUT_ENCODING,Encoding);
if(RFgain != INFINITY)
encode_float(&bp,RF_GAIN,RFgain);
if(RFatten != INFINITY)
encode_float(&bp,RF_ATTEN,RFatten);
encode_eol(&bp);
int cmd_len = bp - cmd_buffer;
if(send(Control_sock, cmd_buffer, cmd_len, 0) != cmd_len)
perror("command send");
last_command_time = gps_time_ns();
if(Verbose)
fprintf(stdout,"Command sent\n");
}
// Look for response
// Set 100 ms timeout for response
struct pollfd fds[1];
fds[0].fd = Status_sock;
fds[0].events = POLLIN;
int event = poll(fds,1,100);
if(event == 0)
continue; // Timeout; go back and resend
if(event < 0){
fprintf(stdout,"poll error: %s\n",strerror(errno));
exit(1);
}
// Incoming packet should be ready
uint8_t response_buffer[PKTSIZE];
uint8_t const * cp = response_buffer; // make response read-only
int length = recvfrom(Status_sock,response_buffer,sizeof(response_buffer),0,NULL,NULL);
if(length <= 0){
fprintf(stdout,"recvfrom status socket error: %s\n",strerror(errno));
exit(1);
}
if(Verbose)
fprintf(stdout,"Message received, %d bytes, type %d\n",length,*cp);
if(*cp++ != 0)
continue; // ignore non-response; go back and receive again
// Process response
while(cp - response_buffer < length){
enum status_type type = *cp++;
if(type == EOL)
break;
unsigned int optlen = *cp++;
if(cp - response_buffer + optlen > length)
break; // Invalid length
switch(type){
default:
break;
case COMMAND_TAG:
received_tag = decode_int32(cp,optlen);
break;
case RADIO_FREQUENCY:
received_freq = decode_double(cp,optlen);
break;
case OUTPUT_SSRC:
received_ssrc = decode_int32(cp,optlen);
break;
case AGC_ENABLE:
received_agc_enable = decode_bool(cp,optlen);
break;
case GAIN:
received_gain = decode_float(cp,optlen);
break;
case RF_GAIN:
received_rf_gain = decode_float(cp,optlen);
break;
case RF_ATTEN:
received_rf_atten = decode_float(cp,optlen);
break;
case RF_AGC:
received_rf_agc = decode_int(cp,optlen);
break;
case PRESET:
FREE(preset); // Unlikely, but just in case
preset = decode_string(cp,optlen);
break;
case LOW_EDGE:
low_edge = decode_float(cp,optlen);
break;
case HIGH_EDGE:
high_edge = decode_float(cp,optlen);
break;
case NOISE_DENSITY:
noise_density = decode_float(cp,optlen);
break;
case BASEBAND_POWER:
baseband_level = decode_float(cp,optlen);
break;
case OUTPUT_SAMPRATE:
samprate = decode_int(cp,optlen);
break;
case OUTPUT_ENCODING:
received_encoding = decode_int(cp,optlen);
break;
}
cp += optlen;
}
if(received_ssrc == Ssrc && received_tag == sent_tag)
break; // For us; we're done
if(Verbose)
fprintf(stdout,"Not for us: ssrc %'u, tag %'u\n",(int)received_ssrc,(int)received_tag);
}
// Show responses unless quiet
if(!Quiet){
printf("SSRC %'u\n",Ssrc);
if(strlen(preset) > 0)
printf("Preset %s\n",preset);
FREE(preset);
if(samprate != 0)
printf("Sample rate %'d Hz\n",samprate);
if(received_encoding != NO_ENCODING)
printf("Encoding %s\n",encoding_string(received_encoding));
if(received_freq != INFINITY)
printf("Frequency %'.3lf Hz\n",received_freq);
if(received_agc_enable != -1)
printf("Channel AGC %s\n",received_agc_enable ? "on" : "off");
if(received_gain != INFINITY)
printf("Channel Gain %.1f dB\n",received_gain);
if(received_rf_agc != -1)
printf("RF AGC %s\n",received_rf_agc ? "on" : "off");
if(received_rf_gain != INFINITY)
printf("RF Gain %.1f dB\n",received_rf_gain);
if(received_rf_atten != INFINITY)
printf("RF Atten %.1f dB\n",received_rf_atten);
if(baseband_level != INFINITY)
printf("Baseband power %.1f dB\n",baseband_level);
if(low_edge != INFINITY && high_edge != INFINITY)
printf("Passband %'.1f Hz to %'.1f Hz (%.1f dB-Hz)\n",low_edge,high_edge,10*log10(fabsf(high_edge - low_edge)));
if(noise_density != INFINITY)
printf("N0 %.1f dB/Hz\n",noise_density);
if(baseband_level != INFINITY &&
low_edge != INFINITY &&
high_edge != INFINITY &&
noise_density != INFINITY){
float noise_power = dB2power(noise_density) * fabsf(high_edge - low_edge);
float signal_plus_noise_power = dB2power(baseband_level);
printf("SNR %.1f dB\n",power2dB(signal_plus_noise_power / noise_power - 1));
}
}
exit(EX_OK); // We're done
}
void usage(void){
fprintf(stdout,"Usage: %s [-h|--help] [-v|--verbose] -r/--radio RADIO -s/--ssrc SSRC [-R|--samprate <sample_rate>] [-i|--iface <iface>] [-l|--locale LOCALE] \
[-f|--frequency <frequency>] [-L|--low <low-edge>] [-H|--high <high-edge>] [[-a|--agc] [-g|--gain <gain dB>]] [-m|--mode <mode>] [--rfgain <gain dB>] [--rfatten <atten dB>]\n" ,App_path);
}