-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinbound_modem.cc
373 lines (314 loc) · 10.5 KB
/
inbound_modem.cc
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <string>
extern "C" {
#define delete _delete
#include "modem.h"
#undef delete
/* modem init externals : FIXME remove it */
extern int dp_dummy_init(void);
extern void dp_dummy_exit(void);
extern int dp_sinus_init(void);
extern void dp_sinus_exit(void);
extern int prop_dp_init(void);
extern void prop_dp_exit(void);
extern int datafile_load_info(char *name,struct dsp_info *info);
extern int datafile_save_info(char *name,struct dsp_info *info);
extern int modem_ring_detector_start(struct modem *m);
#include "resample.h"
}
typedef struct {
struct modem * modem;
int active;
ResamplerState in_resamp_state;
ResamplerState out_resamp_state;
int delay;
} ExtModModem;
#ifdef DEBUG_LOG
FILE *logFile;
#define DLPRINTF(args...) fprintf(logFile, args)
#else
#define DLPRINTF(unsed...)
#endif
#ifdef DEBUG_SAMPLES
FILE *inSamples;
FILE *outSamples;
FILE *inResamples;
FILE *outResamples;
#define SFWRITE(args...) fwrite(args)
#else
#define SFWRITE(args...)
#endif // DEBUG_SAMPLES
static int yate_extmod_modem_start(struct modem *m)
{
ExtModModem * t = (ExtModModem *)m->dev_data;
t->active = 1;
//t->delay = 0;
t->delay = 256; // 160 sample buffer from YATE, 96 sample jitter buffer from ATA
resamp_8khz_9k6hz_init(&t->in_resamp_state);
resamp_9k6hz_8khz_init(&t->out_resamp_state);
DLPRINTF("yate_extmod_modem_start, rate = %d\n", m->srate);
return 0;
}
static int yate_extmod_modem_stop(struct modem *m)
{
ExtModModem * t = (ExtModModem *)m->dev_data;
t->active = 0;
DLPRINTF("yate_extmod_modem_stop\n");
return 0;
}
static int yate_extmod_modem_ioctl(struct modem *m, unsigned int cmd, unsigned long arg)
{
ExtModModem * t = (ExtModModem *)m->dev_data;
DLPRINTF("yate_extmod_modem_ioctl: cmd %x, arg %lx...\n", cmd, arg);
switch (cmd) {
case MDMCTL_CAPABILITIES:
return -1;
case MDMCTL_HOOKSTATE:
case MDMCTL_SPEED:
case MDMCTL_GETFMTS:
case MDMCTL_SETFMT:
case MDMCTL_SETFRAGMENT:
case MDMCTL_SPEAKERVOL:
return 0;
case MDMCTL_CODECTYPE:
return CODEC_UNKNOWN;
case MDMCTL_IODELAY:
DLPRINTF(" ... delay = %d\n", t->delay);
return t->delay; // FIXME?
default:
return 0;
}
}
struct modem_driver yate_extmod_modem_driver = {
.name = "YATE External Module Modem Driver",
.start = yate_extmod_modem_start,
.stop = yate_extmod_modem_stop,
.ioctl = yate_extmod_modem_ioctl,
};
int init_modem(ExtModModem *m) {
struct termios termios;
char * pty_name;
int pty;
pty = getpt();
if (pty < 0 || grantpt(pty) < 0 || unlockpt(pty) < 0) {
fprintf(stderr, "Error creating pty: %s\n", strerror(errno));
return errno;
}
tcgetattr(pty, &termios);
cfmakeraw(&termios);
cfsetispeed(&termios, B115200);
cfsetospeed(&termios, B115200);
if (tcsetattr(pty, TCSANOW, &termios)) {
fprintf(stderr, "Error creating pty in tcsetattr: %s\n",
strerror(errno));
return errno;
}
fcntl(pty, F_SETFL, O_NONBLOCK);
pty_name = ptsname(pty);
m->active = 0;
m->modem = modem_create(&yate_extmod_modem_driver, pty_name);
m->modem->dev_data = (void *)m;
m->modem->pty = pty;
m->modem->pty_name = pty_name;
modem_update_termios(m->modem, &termios);
return 0;
}
void process_msg(std::string & in_msg) {
size_t id_begin_idx;
size_t id_len;
id_begin_idx = in_msg.find(':') + 1;
id_len = in_msg.find(':', id_begin_idx) - id_begin_idx;
std::cout << "%%<message:" <<
in_msg.substr(id_begin_idx, id_len) << ":true:" << std::endl;
}
typedef int16_t samp_t;
int main(int argc, char *argv[]) {
int len;
int numSamples;
int skipSamples;
char buf[4096];
samp_t inSampleBuf[sizeof(buf) / 2];
samp_t outSampleBuf[sizeof(buf) / 2];
fd_set in_fds;
fd_set out_fds;
fd_set err_fds;
std::string in_msg;
ExtModModem modem;
struct termios termios;
int child = 0;
#ifdef DEBUG_LOG
logFile = fopen("/tmp/inbound_modem_dbg.log", "wt");
#endif
#ifdef DEBUG_SAMPLES
inSamples = fopen("/tmp/inbound_modem_in.snd", "wb");
outSamples = fopen("/tmp/inbound_modem_out.snd", "wb");
inResamples = fopen("/tmp/inbound_modem_in_resamp.snd", "wb");
outResamples = fopen("/tmp/inbound_modem_out_resamp.snd", "wb");
#endif
dp_dummy_init();
dp_sinus_init();
prop_dp_init();
modem_timer_init();
init_modem(&modem);
if (argc > 1) {
// If a program is specified as a command line argument, then we run
// the command with the pty name as the argument.
int attach_pty = strstr(argv[0], "attach") != NULL;
const char * const prgName = argv[1];
const char * const prgArgv[3] = { prgName, modem.modem->pty_name, NULL };
child = fork();
if (!child) {
if (attach_pty) {
int pty = open(modem.modem->pty_name, O_RDWR);
dup2(pty, 0);
dup2(pty, 1);
dup2(pty, 2);
}
// The const-ness is probably safe to case away here since we've fork()ed
execv(argv[1], (char * const *)prgArgv);
return 0;
}
}
DLPRINTF("Modem pty is fd %d\n", modem.modem->pty);
while (!child || !waitpid(child, NULL, WNOHANG)) {
if (modem.modem->event) {
modem_event(modem.modem);
}
FD_ZERO(&in_fds);
FD_ZERO(&out_fds);
FD_ZERO(&err_fds);
FD_SET(0, &in_fds);
FD_SET(3, &in_fds);
FD_SET(modem.modem->pty, &in_fds);
FD_SET(1, &out_fds);
FD_SET(4, &out_fds);
for (int i = 0; i < 5; i++) {
FD_SET(i, &err_fds);
}
FD_SET(modem.modem->pty, &err_fds);
if (select(modem.modem->pty + 1, &in_fds, NULL, &err_fds, NULL) <= 0) {
break;
}
/*
for (int i = 0; i < modem.modem->pty; i++) {
if (FD_ISSET(i, &in_fds) && i != 3) {
fprintf(logFile, "fd %d is ready to read\n", i);
}
}
*/
for (int i = 0; i <= modem.modem->pty; i++) {
if (FD_ISSET(i, &err_fds)) {
fprintf(stderr, "fd %d is exceptional\n", i);
break;
}
}
if (FD_ISSET(0, &in_fds)) {
len = read(0, buf, sizeof(buf));
if (len <= 0) {
break;
}
//fwrite(buf, 1, len, logFile);
for (int i = 0; i < len; i++) {
in_msg.push_back(buf[i]);
if (buf[i] == '\n') {
process_msg(in_msg);
in_msg.clear();
}
}
}
if (FD_ISSET(3, &in_fds)) {
len = read(3, buf, sizeof(buf));
if (len <= 0) {
break;
}
if (modem.active) {
if (len % 2) {
DLPRINTF("read an odd number of bytes!! (%d)\n", len);
}
SFWRITE(buf, 1, len, inSamples);
numSamples = (sizeof(inSampleBuf) / 2) -
resample(&modem.in_resamp_state, (int16_t *)buf, len / 2,
inSampleBuf, sizeof(inSampleBuf) / 2);
DLPRINTF("Received %d bytes (%d samples), resampled to %d samples (ud = %d)\n",
len, len / 2, numSamples, modem.modem->update_delay);
SFWRITE(inSampleBuf, 2, numSamples, inResamples);
skipSamples = 0;
if (modem.modem->update_delay < 0) {
if ( -modem.modem->update_delay >= len / 2) {
DLPRINTF("change delay -%d...\n", len / 2);
modem.delay -= len / 2;
modem.modem->update_delay += len / 2;
continue;
}
DLPRINTF("change delay %d...\n", modem.modem->update_delay);
skipSamples = modem.modem->update_delay;
numSamples += skipSamples; // skipSamples is negative here
modem.delay += modem.modem->update_delay;
modem.modem->update_delay = 0;
}
modem_process(modem.modem, inSampleBuf - skipSamples, outSampleBuf, numSamples);
SFWRITE(outSampleBuf, 2, numSamples, outSamples);
numSamples = (sizeof(buf) / 2) -
resample(&modem.out_resamp_state, outSampleBuf, numSamples,
(int16_t *)buf, sizeof(buf) / 2);
DLPRINTF("upconverted modem samples to %d\n", numSamples);
SFWRITE(buf, 2, numSamples, outResamples);
} else {
// Return silence
memset(buf, 0, len);
numSamples = len / 2;
}
DLPRINTF("Writing %d samples (%d bytes) back to YATE\n", numSamples, numSamples * 2);
if (write(4, buf, numSamples * 2) != numSamples * 2) {
fprintf(stderr, "can't write the entire outgoing buffer!\n");
break;
}
if (modem.modem->update_delay > 0) {
DLPRINTF("change delay +%d...\n", modem.modem->update_delay);
len = modem.modem->update_delay * 2;
if (len > sizeof(buf)) {
DLPRINTF("Delay change required %d bytes, only %d available\n",
len, sizeof(buf));
len = sizeof(buf);
}
memset(buf, 0, len);
if (write(4, buf, len) != len) {
fprintf(stderr, "can't write the entire outgoing delay buffer!\n");
break;
}
modem.delay += modem.modem->update_delay;
modem.modem->update_delay = 0;
}
}
if (FD_ISSET(modem.modem->pty, &in_fds)) {
tcgetattr(modem.modem->pty, &termios);
if (memcmp(&termios, &modem.modem->termios, sizeof(termios))) {
modem_update_termios(modem.modem, &termios);
}
len = modem.modem->xmit.size - modem.modem->xmit.count;
if (len == 0) {
continue;
}
if (len > sizeof(buf)) {
len = sizeof(buf);
}
len = read(modem.modem->pty, buf, len);
if (len > 0) {
modem_write(modem.modem, buf, len);
}
}
}
kill(child, SIGHUP);
dp_dummy_exit();
dp_sinus_exit();
prop_dp_exit();
}