-
Notifications
You must be signed in to change notification settings - Fork 45
/
setfilt.c
178 lines (161 loc) · 4.24 KB
/
setfilt.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
// Interactive program to set predetection filters - out of date
// Copyright 2023 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <unistd.h>
#if defined(linux)
#include <bsd/string.h>
#endif
#include <string.h>
#include <sys/socket.h>
#include <locale.h>
#include <errno.h>
#include <ctype.h>
#include <sysexits.h>
#include "misc.h"
#include "multicast.h"
#include "status.h"
int Mcast_ttl = 5;
int IP_tos = 0;
const char *App_path;
int Verbose;
char *Radio = NULL;
char *Locale = "en_US.UTF-8";
struct sockaddr_storage Metadata_dest_socket;
int Status_sock = -1;
int Control_sock = -1;
char Optstring[] = "vl:r:V";
struct option Options[] = {
{"radio", required_argument, NULL, 'r'},
{"locale", required_argument, NULL, 'l'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
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 'V':
VERSION();
exit(EX_OK);
case 'v':
Verbose++;
break;
case 'l':
Locale = optarg;
break;
case 'r':
Radio = optarg;
break;
}
}
}
setlocale(LC_ALL,Locale);
if(Radio == NULL)
Radio = getenv("RADIO");
if(Radio == NULL){
fprintf(stderr,"--radio not specified and $RADIO not set\n");
exit(1);
}
char iface[1024];
resolve_mcast(Radio,&Metadata_dest_socket,DEFAULT_STAT_PORT,iface,sizeof(iface),0);
Status_sock = listen_mcast(&Metadata_dest_socket,iface);
if(Status_sock == -1){
fprintf(stderr,"Can't open Status_sock socket to radio control channel %s: %s\n",Radio,strerror(errno));
exit(1);
}
Control_sock = connect_mcast(&Metadata_dest_socket,iface,Mcast_ttl,IP_tos);
if(Control_sock == -1){
fprintf(stderr,"Can't open cmd socket to radio control channel %s: %s\n",Radio,strerror(errno));
exit(1);
}
uint32_t sent_tag = 0; // Used only if sent_freq != 0
int cmd_sent = 0;
if(optind+1 < argc){
// Args specified
cmd_sent = 1;
if(tolower(argv[optind][0]) == 'm')
argv[optind][0] = '-';
float low = strtof(argv[optind],NULL);
if(tolower(argv[optind+1][0]) == 'm')
argv[optind+1][0] = '-';
float high = strtof(argv[optind+1],NULL);
if(low > high){
float t = low;
low = high;
high = t;
}
uint8_t buffer[8192];
uint8_t *bp = buffer;
*bp++ = 1; // Generate command packet
sent_tag = random();
encode_int(&bp,COMMAND_TAG,sent_tag);
encode_float(&bp,LOW_EDGE,low);
encode_float(&bp,HIGH_EDGE,high);
encode_eol(&bp);
int cmd_len = bp - buffer;
if(send(Control_sock, buffer, cmd_len, 0) != cmd_len)
perror("command send");
}
// Read and process status
for(;;){
uint8_t buffer[8192];
int length = recvfrom(Status_sock,buffer,sizeof(buffer),0,NULL,NULL);
if(length <= 0){
fprintf(stderr,"recvfrom status socket error: %s\n",strerror(errno));
sleep(1);
continue;
}
// We could check the source address here, but we have no way of verifying it.
// But there should only be one host sending status to this group anyway
uint8_t const * cp = buffer;
if(*cp++ != 0)
continue; // Look only at status packets
uint32_t received_tag = 0;
float low = NAN;
float high = NAN;
while(cp - buffer < length){
enum status_type type = *cp++;
if(type == EOL)
break;
unsigned int optlen = *cp++;
if(cp - buffer + optlen > length)
break; // Invalid length
switch(type){
default:
break;
case COMMAND_TAG:
received_tag = decode_int32(cp,optlen);
break;
case LOW_EDGE:
low = decode_float(cp,optlen);
break;
case HIGH_EDGE:
high = decode_float(cp,optlen);
break;
}
cp += optlen;
}
if(isnan(low) || isnan(high))
continue; // Didn't get what we wanted
// If we sent a command, wait for its acknowledgement
// Otherwise, just display the current frequency
if(cmd_sent && received_tag != sent_tag)
continue;
printf("%'.0f <-> %'.0f Hz\n",low,high);
break;
}
exit(0);
}