forked from ka9q/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-sig.c
349 lines (298 loc) · 9.36 KB
/
show-sig.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
// Display radiod signal levels -- Out of date
// Copyright 2021 Phil Karn, KA9Q
// Adapted from show-pkt.c
#define _GNU_SOURCE 1
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#if defined(linux)
#include <bsd/string.h>
#endif
#include <locale.h>
#include <ncurses.h>
#include <sysexits.h>
#include "misc.h"
#include "multicast.h"
#include "status.h"
char Locale[256] = "en_US.UTF-8";
// Fix this name conficts with status.h
int decode_rtp_status(uint8_t const *buffer,int length);
int decode_frontend_status(uint8_t const *buffer,int length);
const char *App_path;
int Verbose,Dump;
struct sockaddr_storage Output_metadata_dest_address;
struct sockaddr_storage Output_metadata_source_address;
struct sockaddr_storage Input_metadata_dest_address; // needed to listen to front end directly
char const *Input_metadata_source_socket = "";
char const *Input_metadata_dest_socket = "";
char *Description = NULL;
char const *Output_data_source_socket = "";
char const *Output_data_dest_socket = "";
float Output_level;
int LNA_gain;
int Mixer_gain;
int IF_gain;
int Input_SSRC;
int Output_SSRC;
char const *Output_metadata_source_socket = "";
char const *Output_metadata_dest_socket = "";
double Frequency;
float Low_edge,High_edge;
float IF_power;
float Baseband_power;
float Noise_density;
float Demod_snr = NAN;
float Headroom;
float Gain;
float Output_level;
void doscreen(void);
static FILE *Tty;
static SCREEN *Term;
void display_cleanup(void){
echo();
nocbreak();
endwin();
if(Term)
delscreen(Term);
Term = NULL;
if(Tty)
fclose(Tty);
Tty = NULL;
}
int Radio_fd;
int FE_fd = -1;
// Thread to display receiver state, updated at 10Hz by default
// Uses the ancient ncurses text windowing library
// Also services keyboard, mouse and tuning knob, if present
int main(int argc,char *argv[]){
App_path = argv[0];
{
int c;
while((c = getopt(argc,argv,"vdV")) != EOF){
switch(c){
case 'v':
Verbose++;
break;
case 'V':
VERSION();
exit(EX_OK);
}
}
}
{
// We assume en_US.UTF-8, or anything with a thousands grouping character
char const * const cp = getenv("LANG");
if(cp != NULL){
strlcpy(Locale,cp,sizeof(Locale));
}
}
setlocale(LC_ALL,Locale); // Set either the hardwired default or the value of $LANG if it exists
atexit(display_cleanup);
Radio_fd = setup_mcast_in(argv[optind],(struct sockaddr *)&Output_metadata_dest_address,2,0);
if(Radio_fd == -1){
fprintf(stderr,"Can't listen to %s\n",argv[optind]);
exit(1);
}
/* Main loop:
Read radio/front end status from network with 100 ms timeout to serve as polling rate
Update local status; retransmit radio command if necessary
Repaint display windows
Poll keyboard and process user commands
*/
// Set up display subwindows
// ncurses setup
atexit(display_cleanup);
#if 0
Tty = fopen("/dev/tty","r+");
Term = newterm(NULL,Tty,Tty);
set_term(Term);
#endif
initscr();
keypad(stdscr,TRUE);
meta(stdscr,TRUE);
timeout(0); // Don't block in getch()
cbreak();
noecho();
for(;;){
struct timespec const timeout = { 0, 100000000 }; // 100 ms -> 10 Hz
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(Radio_fd,&fdset);
if(FE_fd != -1)
FD_SET(FE_fd,&fdset);
int n = max(Radio_fd,FE_fd) + 1;
n = pselect(n,&fdset,NULL,NULL,&timeout,NULL);
if(FD_ISSET(Radio_fd,&fdset)){
// Message from the radio program
uint8_t buffer[8192];
memset(buffer,0,sizeof(buffer));
socklen_t ssize = sizeof(Output_metadata_source_address);
int length = recvfrom(Radio_fd,buffer,sizeof(buffer),0,(struct sockaddr *)&Output_metadata_source_address,&ssize);
if(length <= 0){
usleep(100000); // don't burn time in a tight error loop
continue;
}
Output_metadata_source_socket = formatsock(&Output_metadata_source_address);
Output_metadata_dest_socket = formatsock(&Output_metadata_dest_address);
// Parse entries
if(length >= 2 && buffer[0] == 0){ // Ignore our own command packets
decode_rtp_status(buffer+1,length-1);
}
}
if(FE_fd == -1 && ((struct sockaddr_in *)&Input_metadata_dest_address) -> sin_family != 0){
FE_fd = setup_mcast_in(NULL,(struct sockaddr *)&Input_metadata_dest_address,0,0);
}
if(FD_ISSET(FE_fd,&fdset)){
// Message from the front end
uint8_t buffer[8192];
memset(buffer,0,sizeof(buffer));
socklen_t ssize = sizeof(Output_metadata_source_address);
int length = recvfrom(FE_fd,buffer,sizeof(buffer),0,(struct sockaddr *)&Output_metadata_source_address,&ssize);
if(length <= 0){
usleep(100000); // don't burn time in a tight error loop
continue;
}
// Parse entries - careful, E.g,, radio's input is front end's output
if(length >= 2 && buffer[0] == 0){ // Ignore our own command packets
decode_frontend_status(buffer+1,length-1);
}
}
doscreen();
}
endwin();
set_term(NULL);
if(Term != NULL)
delscreen(Term);
// if(Tty != NULL)
// fclose(Tty);
exit(0);
}
void doscreen(void){
int row = 0;
int col = 0;
int data_indent = 10;
int header_indent = 5;
werase(stdscr);
hline(0,31);
mvprintw(row++,header_indent,"Front end metadata"); // on top of line
mvprintw(row++,col,"%s -> %s\n",Input_metadata_source_socket,Input_metadata_dest_socket);
mvprintw(row++,col,"SSRC %x\n",Input_SSRC);
mvprintw(row++,col,"%s\n",Description);
hline(0,31);
mvprintw(row++,header_indent,"Radio metadata"); // on top of line
mvprintw(row++,col,"%s -> %s\n",Output_metadata_source_socket,Output_metadata_dest_socket);
mvprintw(row++,col,"SSRC %x\n",Output_SSRC);
mvprintw(row++,col,"Frequency %'.3lf Hz\n",Frequency);
hline(0,31);
mvprintw(row++,header_indent,"Signal levels"); // overwrite line
mvprintw(row++,col,"A/D Power %*.1f dBFS\n",data_indent,Output_level);
// These should be floats
mvprintw(row++,col,"LNA Gain %*d dB\n",data_indent,LNA_gain);
mvprintw(row++,col,"Mixer Gain %*d dB\n",data_indent,Mixer_gain);
mvprintw(row++,col,"IF Gain %*d dB\n",data_indent,IF_gain);
mvprintw(row++,col,"RF/IF Power %*.1f dB\n",data_indent,IF_power);
mvprintw(row++,col,"Baseband Power %*.1f dB\n",data_indent,Baseband_power);
mvprintw(row++,col,"Noise density %*.1f dB/Hz\n",data_indent,Noise_density);
float bw = 10*log10(fabsf(High_edge - Low_edge));
float noise_power = dB2power(Noise_density + bw);
// S = total baseband power - noise power (bw*N0) in linear units
float sn0 = power2dB(dB2power(Baseband_power) - noise_power ) - Noise_density;
float snr = sn0 - bw;
mvprintw(row++,col,"S/N0 %*.1f dB Hz\n",data_indent,sn0);
mvprintw(row++,col,"Bandwidth %*.1f dB Hz\n",data_indent,bw);
mvprintw(row++,col,"SNR %*.1f dB\n",data_indent,snr);
if(!isnan(Demod_snr))
mvprintw(row++,col,"Demod SNR %*.1f dB\n",data_indent,Demod_snr);
mvprintw(row++,col,"Gain %*.1f dB\n",data_indent,Gain);
mvprintw(row++,col,"Output level %*.1f dB\n",data_indent,Output_level);
mvprintw(row++,col,"Headroom %*.1f dB\n",data_indent,Headroom);
wnoutrefresh(stdscr);
doupdate();
}
// Decode incoming status message from the front end
int decode_frontend_status(uint8_t const *buffer,int length){
uint8_t const *cp = buffer;
while(cp - buffer < length){
enum status_type type = *cp++; // increment cp to length field
if(type == EOL)
break; // end of list
unsigned int optlen = *cp++;
if(cp - buffer + optlen >= length)
break; // invalid length; we can't continue to scan
switch(type){
case OUTPUT_LEVEL:
Output_level = decode_float(cp,optlen);
break;
case LNA_GAIN:
LNA_gain = decode_int(cp,optlen);
break;
case MIXER_GAIN:
Mixer_gain = decode_int(cp,optlen);
break;
case IF_GAIN:
IF_gain = decode_int(cp,optlen);
break;
default:
;
}
cp += optlen;
}
return 0;
}
// Decode incoming status message from the radio program
int decode_rtp_status(uint8_t const *buffer,int length){
uint8_t const *cp = buffer;
while(cp - buffer < length){
enum status_type type = *cp++; // increment cp to length field
if(type == EOL)
break; // end of list
unsigned int optlen = *cp++;
if(cp - buffer + optlen >= length)
break; // invalid length; we can't continue to scan
switch(type){
case DESCRIPTION:
FREE(Description);
Description = decode_string(cp,optlen);
break;
case OUTPUT_SSRC:
Output_SSRC = decode_int(cp,optlen);
break;
case IF_POWER:
IF_power = decode_float(cp,optlen);
break;
case RADIO_FREQUENCY:
Frequency = decode_double(cp,optlen);
break;
case LOW_EDGE:
Low_edge = decode_float(cp,optlen);
break;
case HIGH_EDGE:
High_edge = decode_float(cp,optlen);
break;
case BASEBAND_POWER:
Baseband_power = decode_float(cp,optlen);
break;
case NOISE_DENSITY:
Noise_density = decode_float(cp,optlen);
break;
case DEMOD_SNR:
Demod_snr = decode_float(cp,optlen);
break;
case HEADROOM:
Headroom = decode_float(cp,optlen);
break;
case GAIN:
Gain = decode_float(cp,optlen);
break;
case OUTPUT_LEVEL:
Output_level = decode_float(cp,optlen);
break;
default:
;
}
cp += optlen;
}
return 0;
}