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
|
/*
* Shared max25-bcprd run loop (KISS thread + SER12 engine).
*/
#define _GNU_SOURCE
#include "bcpr/bcpr_daemon.h"
#include "bcpr/bcpr_ser12.h"
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static volatile sig_atomic_t g_stop;
static void on_sig(int sig)
{
(void)sig;
g_stop = 1;
}
static void kiss_write_frame(int fd, const uint8_t *kiss, int len)
{
uint8_t out[BCPR_MAXFLEN * 2 + 4];
int o = 0;
int i;
if (fd < 0 || !kiss || len <= 0) {
return;
}
out[o++] = 0xC0;
for (i = 0; i < len && o < (int)sizeof(out) - 2; i++) {
if (kiss[i] == 0xC0) {
out[o++] = 0xDB;
out[o++] = 0xDC;
} else if (kiss[i] == 0xDB) {
out[o++] = 0xDB;
out[o++] = 0xDD;
} else {
out[o++] = kiss[i];
}
}
out[o++] = 0xC0;
(void)write(fd, out, (size_t)o);
}
static bcpr_kiss_pty_t *g_pty;
static int g_npty;
static void on_rx(int dev_idx, const uint8_t *kiss, int len, void *ud)
{
int i;
(void)ud;
for (i = 0; i < g_npty; i++) {
if (g_pty[i].idx == dev_idx) {
kiss_write_frame(g_pty[i].master_fd, kiss, len);
return;
}
}
}
static void kiss_feed(bcpr_engine_t *e, bcpr_kiss_pty_t *kp)
{
static uint8_t acc[BCPR_MAX_DEVICES][BCPR_MAXFLEN + 8];
static int alen[BCPR_MAX_DEVICES];
static int esc[BCPR_MAX_DEVICES];
static int in_frame[BCPR_MAX_DEVICES];
uint8_t buf[512];
ssize_t n;
int i;
int di = kp->idx;
if (di < 0 || di >= BCPR_MAX_DEVICES) {
return;
}
n = read(kp->master_fd, buf, sizeof(buf));
if (n <= 0) {
return;
}
for (i = 0; i < (int)n; i++) {
uint8_t b = buf[i];
if (!in_frame[di]) {
if (b == 0xC0) {
in_frame[di] = 1;
alen[di] = 0;
esc[di] = 0;
}
continue;
}
if (esc[di]) {
esc[di] = 0;
if (b == 0xDC) {
b = 0xC0;
} else if (b == 0xDD) {
b = 0xDB;
}
if (alen[di] < (int)sizeof(acc[di])) {
acc[di][alen[di]++] = b;
}
continue;
}
if (b == 0xDB) {
esc[di] = 1;
continue;
}
if (b == 0xC0) {
if (alen[di] >= 2) {
int q;
int w;
for (w = 0; w < 70; w++) {
q = bcpr_engine_queue_kiss(e, di, acc[di], alen[di]);
if (q == 0) {
break;
}
usleep(50000);
}
if (q != 0) {
fprintf(stderr,
"max25-bcprd: queue_kiss drop bc%d len=%d (busy)\n",
di, alen[di]);
}
}
in_frame[di] = 0;
alen[di] = 0;
continue;
}
if (alen[di] < (int)sizeof(acc[di])) {
acc[di][alen[di]++] = b;
}
}
}
static void *kiss_thread(void *arg)
{
bcpr_engine_t *e = (bcpr_engine_t *)arg;
while (!g_stop && !e->stop) {
struct pollfd pf[BCPR_MAX_DEVICES];
int nf = 0;
int map[BCPR_MAX_DEVICES];
int i;
int ret;
int backoff = 0;
for (i = 0; i < g_npty; i++) {
if (g_pty[i].master_fd < 0) {
continue;
}
pf[nf].fd = g_pty[i].master_fd;
pf[nf].events = POLLIN | POLLHUP | POLLERR;
map[nf] = i;
nf++;
}
if (nf == 0) {
usleep(50000);
continue;
}
ret = poll(pf, (nfds_t)nf, 50);
if (ret <= 0) {
continue;
}
for (i = 0; i < nf; i++) {
short re = pf[i].revents;
if (re & POLLIN) {
kiss_feed(e, &g_pty[map[i]]);
}
if (re & (POLLHUP | POLLERR | POLLNVAL)) {
backoff = 1;
}
}
if (backoff) {
usleep(50000);
}
}
return NULL;
}
static int ensure_dir(const char *path)
{
char tmp[256];
char *p;
size_t len;
if (!path || !path[0]) {
return -1;
}
snprintf(tmp, sizeof(tmp), "%s", path);
len = strlen(tmp);
if (len == 0) {
return -1;
}
if (tmp[len - 1] == '/') {
tmp[len - 1] = '\0';
}
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
(void)mkdir(tmp, 0755);
*p = '/';
}
}
return mkdir(tmp, 0755) == 0 || errno == EEXIST ? 0 : -1;
}
int bcpr_daemon_run(bcpr_config_t *cfg, bcpr_kiss_pty_t *ptys, int npty,
bcpr_engine_t *engine, const bcpr_daemon_opts_t *opts)
{
pthread_t thr;
int thr_ok = 0;
int i;
bcpr_engine_t local_engine;
bcpr_engine_t *e = engine;
int cal_mode;
int seconds;
if (!cfg || !opts) {
return 1;
}
g_stop = 0;
g_pty = ptys;
g_npty = npty;
cal_mode = opts->cal_mode;
seconds = opts->seconds;
signal(SIGINT, on_sig);
signal(SIGTERM, on_sig);
for (i = 0; i < BCPR_MAX_DEVICES; i++) {
if (opts->cli_txd_bias >= 0) {
cfg->dev[i].txd_bias = opts->cli_txd_bias;
}
if (opts->cli_ptt_wd >= 0) {
cfg->dev[i].ptt_wd = opts->cli_ptt_wd;
}
if (opts->cli_ptt_wd_key_ms > 0) {
cfg->dev[i].ptt_wd_key_ms = opts->cli_ptt_wd_key_ms;
}
if (opts->cli_ptt_wd_pause_ms > 0) {
cfg->dev[i].ptt_wd_pause_ms = opts->cli_ptt_wd_pause_ms;
}
}
(void)ensure_dir(cfg->state_dir);
if (!opts->engine_preopened) {
e = &local_engine;
if (bcpr_engine_open(e, cfg) != 0) {
for (i = 0; i < npty; i++) {
bcpr_kiss_pty_close(&ptys[i]);
}
return 1;
}
}
e->run_seconds = seconds;
if (cal_mode != BCPR_CAL_OFF) {
bcpr_engine_set_cal(e, cal_mode);
}
bcpr_engine_set_rx(e, on_rx, NULL);
if (npty > 0 && cal_mode == BCPR_CAL_OFF &&
pthread_create(&thr, NULL, kiss_thread, e) == 0) {
thr_ok = 1;
}
if (cal_mode != BCPR_CAL_OFF && !cfg->dry_run) {
fprintf(stderr,
"bcprd: *** WATCH NOW *** cal PTT+tone for %d s — then unkey\n",
seconds);
}
(void)bcpr_engine_run(e);
g_stop = 1;
e->stop = 1;
if (thr_ok) {
pthread_join(thr, NULL);
}
bcpr_engine_close(e);
for (i = 0; i < npty; i++) {
bcpr_kiss_pty_close(&ptys[i]);
}
fprintf(stderr, "max25-bcprd: stopped\n");
return 0;
}
|