forked from ka9q/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-pkt.c
286 lines (236 loc) · 7 KB
/
show-pkt.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
// Display RTP statistics
// Adapted from control.c
// Copyright 2021 Phil Karn, KA9Q
#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";
int decode_rtp_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;
int Cmd_cnt;
char const *Input_data_source_socket = "";
char const *Input_data_dest_socket = "";
char const *Input_metadata_source_socket = "";
char const *Input_metadata_dest_socket = "";
int Input_SSRC;
int Input_metadata_packets;
int Input_data_packets;
int Input_drops;
int Input_dupes;
char const *Output_data_source_socket = "";
char const *Output_data_dest_socket = "";
int Output_SSRC;
int Output_TTL;
int Output_metadata_packets;
int Output_data_packets;
char const *Output_metadata_source_socket = "";
char const *Output_metadata_dest_socket = "";
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;
// 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':
VERSION();
exit(EX_OK);
case 'v':
Verbose++;
break;
}
}
}
{
// 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);
int n = Radio_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] == STATUS){ // Ignore our own command packets
decode_rtp_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 = 30;
int header_indent = 5;
werase(stdscr);
hline(0,20);
mvprintw(row++,header_indent,"Input data"); // on top of line
mvprintw(row++,col,"%s -> %s\n",Input_data_source_socket,Input_data_dest_socket);
mvprintw(row,col,"%*x\n",data_indent,Input_SSRC);
mvprintw(row++,col,"Input SSRC");
mvprintw(row,col,"%'*d\n",data_indent,Input_data_packets);
mvprintw(row++,col,"Input data pkts");
mvprintw(row,col,"%'*d\n",data_indent,Input_drops);
mvprintw(row++,col,"Input data drops");
mvprintw(row,col,"%'*d\n",data_indent,Input_dupes);
mvprintw(row++,col,"Input data dups");
row++;
move(row,0);
hline(0,20);
mvprintw(row++,header_indent,"Input meta"); // on top of line
mvprintw(row++,col,"%s -> %s\n",Input_metadata_source_socket,Input_metadata_dest_socket);
mvprintw(row,col,"%'*d\n",data_indent,Input_metadata_packets);
mvprintw(row++,col,"Input meta pkts");
row++;
move(row,0);
hline(0,20);
mvprintw(row++,header_indent,"Output data"); // On top of ine
mvprintw(row++,col,"%s -> %s\n",Output_data_source_socket,Output_data_dest_socket);
mvprintw(row,col,"%*x\n",data_indent,Output_SSRC);
mvprintw(row++,col,"Output SSRC");
mvprintw(row,col,"%*d\n",data_indent,Output_TTL);
mvprintw(row++,col,"Output TTL");
mvprintw(row,col,"%'*d",data_indent,Output_data_packets);
mvprintw(row++,col,"Output data pkts");
row++;
move(row,0);
hline(0,20);
mvprintw(row++,header_indent,"Output meta");
mvprintw(row++,col,"%s -> %s\n",Output_metadata_source_socket,Output_metadata_dest_socket);
mvprintw(row,col,"%'*d",data_indent,Output_metadata_packets);
mvprintw(row++,col,"Output meta pkts");
mvprintw(row,col,"%'*d\n",data_indent,Cmd_cnt);
mvprintw(row++,col,"Commands");
wnoutrefresh(stdscr);
doupdate();
}
// 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 CMD_CNT:
Cmd_cnt = decode_int(cp,optlen);
break;
case OUTPUT_DATA_SOURCE_SOCKET:
{
struct sockaddr_storage tmp;
Output_data_source_socket = formatsock(decode_socket(&tmp,cp,optlen));
}
break;
case OUTPUT_DATA_DEST_SOCKET:
{
struct sockaddr_storage tmp;
Output_data_dest_socket = formatsock(decode_socket(&tmp,cp,optlen));
}
break;
case OUTPUT_SSRC:
Output_SSRC = decode_int(cp,optlen);
break;
case OUTPUT_TTL:
Output_TTL = decode_int(cp,optlen);
break;
case OUTPUT_METADATA_PACKETS:
Output_metadata_packets = decode_int(cp,optlen);
break;
case OUTPUT_DATA_PACKETS:
Output_data_packets = decode_int(cp,optlen);
break;
default:
break;
}
// Implicitly
cp += optlen;
}
return 0;
}