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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
implement Modem;
include "sys.m";
sys: Sys;
include "lock.m";
lock: Lock;
Semaphore: import lock;
include "draw.m";
include "modem.m";
hangupcmd := "ATH0"; # was ATZH0 but some modem versions on Umec hung on ATZ (BUG: should be in modeminfo)
# modem return codes
Ok, Success, Failure, Abort, Noise, Found: con iota;
maxspeed: con 115200;
#
# modem return messages
#
Msg: adt {
text: string;
code: int;
};
msgs: array of Msg = array [] of {
("OK", Ok),
("NO CARRIER", Failure),
("ERROR", Failure),
("NO DIALTONE", Failure),
("BUSY", Failure),
("NO ANSWER", Failure),
("CONNECT", Success),
};
kill(pid: int)
{
fd := sys->open("#p/"+string pid+"/ctl", Sys->OWRITE);
if(fd == nil || sys->fprint(fd, "kill") < 0)
sys->print("modem: can't kill %d: %r\n", pid);
}
#
# prepare a modem port
#
openserial(d: ref Device)
{
if (d==nil) {
raise "fail: device not initialized";
return;
}
d.data = nil;
d.ctl = nil;
d.data = sys->open(d.local, Sys->ORDWR);
if(d.data == nil) {
raise "fail: can't open "+d.local;
return;
}
d.ctl = sys->open(d.local+"ctl", Sys->ORDWR);
if(d.ctl == nil) {
raise "can't open "+d.local+"ctl";
return;
}
d.speed = maxspeed;
d.avail = nil;
}
#
# shut down the monitor (if any) and return the connection
#
close(m: ref Device): ref Sys->Connection
{
if(m == nil)
return nil;
if(m.pid != 0){
kill(m.pid);
m.pid = 0;
}
if(m.data == nil)
return nil;
mc := ref sys->Connection(m.data, m.ctl, nil);
m.ctl = nil;
m.data = nil;
return mc;
}
#
# Send a string to the modem
#
send(d: ref Device, x: string): int
{
if (d == nil)
return -1;
a := array of byte x;
f := sys->write(d.data, a, len a);
if (f < 0) {
# let's attempt to close & reopen the modem
close(d);
openserial(d);
f = sys->write(d.data,a, len a);
}
sys->print("->%s\n",x);
return f;
}
#
# apply a string of commands to modem & look for a response
#
apply(d: ref Device, s: string, substr: string, secs: int): int
{
m := Ok;
buf := "";
for(i := 0; i < len s; i++){
c := s[i];
buf[len buf] = c; # assume no Unicode
if(c == '\r' || i == (len s -1)){
if(c != '\r')
buf[len buf] = '\r';
if(send(d, buf) < 0)
return Abort;
(m, nil) = readmsg(d, secs, substr);
buf = "";
}
}
return m;
}
#
# get modem into command mode if it isn't already
#
GUARDTIME: con 1100; # usual default for S12=50 in units of 1/50 sec; allow 100ms fuzz
attention(d: ref Device): int
{
for(i := 0; i < 3; i++){
if(apply(d, hangupcmd, nil, 2) == Ok)
return Ok;
sys->sleep(GUARDTIME);
if(send(d, "+++") < 0)
return Abort;
sys->sleep(GUARDTIME);
(nil, msg) := readmsg(d, 0, nil);
if(msg != nil)
sys->print("status: %s\n", msg);
}
return Failure;
}
#
# apply a command type
#
applyspecial(d: ref Device, cmd: string): int
{
if(cmd == nil)
return Failure;
return apply(d, cmd, nil, 2);
}
#
# hang up any connections in progress and close the device
#
onhook(d: ref Device)
{
if(d == nil)
return;
# hang up the modem
monitoring(d);
if(attention(d) != Ok)
sys->print("modem: no attention\n");
# hangup the stream (eg, for ppp) and toggle the lines to the modem
if(d.ctl != nil) {
sys->fprint(d.ctl,"d0\n");
sys->fprint(d.ctl,"r0\n");
sys->fprint(d.ctl, "h\n"); # hangup on native serial
sys->sleep(250);
sys->fprint(d.ctl,"r1\n");
sys->fprint(d.ctl,"d1\n");
}
close(d);
}
#
# does string s contain t anywhere?
#
contains(s, t: string): int
{
if(t == nil)
return 1;
if(s == nil)
return 0;
n := len t;
for(i := 0; i+n <= len s; i++)
if(s[i:i+n] == t)
return 1;
return 0;
}
#
# read till we see a message or we time out
#
readmsg(d: ref Device, secs: int, substr: string): (int, string)
{
if (d == nil)
return (Abort, "device not initialized");
found := 0;
secs *= 1000;
limit := 1000; # pretty arbitrary
s := "";
for(start := sys->millisec(); sys->millisec() <= start+secs;){
a := getinput(d,1);
if(len a == 0){
if(limit){
sys->sleep(1);
continue;
}
break;
}
if(a[0] == byte '\n' || a[0] == byte '\r' || limit == 0){
if (len s) {
if (s[(len s)-1] == '\r')
s[(len s)-1] = '\n';
sys->print("<-%s\n",s);
}
if(substr != nil && contains(s, substr))
found = 1;
for(k := 0; k < len msgs; k++)
if(len s >= len msgs[k].text &&
s[0:len msgs[k].text] == msgs[k].text){
if(found)
return (Found, s);
return (msgs[k].code, s);
}
start = sys->millisec();
s = "";
continue;
}
s[len s] = int a[0];
limit--;
}
s = "No response from modem";
if(found)
return (Found, s);
return (Noise, s);
}
#
# get baud rate from a connect message
#
getspeed(msg: string, speed: int): int
{
p := msg[7:]; # skip "CONNECT"
while(p[0] == ' ' || p[0] == '\t')
p = p[1:];
s := int p;
if(s <= 0)
return speed;
else
return s;
}
#
# set speed and RTS/CTS modem flow control
#
setspeed(d: ref Device, baud: int)
{
if(d != nil && d.ctl != nil){
sys->fprint(d.ctl, "b%d", baud);
sys->fprint(d.ctl, "m1");
}
}
dumpa(a: array of byte): string
{
s := "";
for(i:=0; i<len a; i++){
b := int a[i];
if(b >= ' ' && b < 16r7f)
s[len s] = b;
else
s += sys->sprint("\\%.2x", b);
}
return s;
}
monitoring(d: ref Device)
{
# if no monitor then spawn one
if(d.pid == 0) {
pidc := chan of int;
spawn monitor(d, pidc);
d.pid = <-pidc;
}
}
#
# a process to read input from a modem.
#
monitor(d: ref Device, pidc: chan of int)
{
openserial(d);
pidc <-= sys->pctl(0, nil); # pidc can be written once only.
a := array[Sys->ATOMICIO] of byte;
for(;;) {
d.lock.obtain();
d.status = "Idle";
d.remote = "";
setspeed(d, d.speed);
d.lock.release();
# shuttle bytes
while((n := sys->read(d.data, a, len a)) > 0){
d.lock.obtain();
if (len d.avail < Sys->ATOMICIO) {
na := array[len d.avail + n] of byte;
na[0:] = d.avail[0:];
na[len d.avail:] = a[0:n];
d.avail = na;
}
d.lock.release();
}
# on an error, try reopening the device
d.data = nil;
d.ctl = nil;
openserial(d);
}
}
#
# return up to n bytes read from the modem by monitor()
#
getinput(d: ref Device, n: int): array of byte
{
if(d==nil || n <= 0)
return nil;
a: array of byte;
d.lock.obtain();
if(len d.avail != 0){
if(n > len d.avail)
n = len d.avail;
a = d.avail[0:n];
d.avail = d.avail[n:];
}
d.lock.release();
return a;
}
getc(m: ref Device, timo: int): int
{
start := sys->millisec();
while((b := getinput(m, 1)) == nil) {
if (timo && sys->millisec() > start+timo)
return 0;
sys->sleep(1);
}
return int b[0];
}
init(modeminfo: ref ModemInfo): ref Device
{
if (sys == nil) {
sys = load Sys Sys->PATH;
lock = load Lock Lock->PATH;
if (lock == nil) {
raise "fail: Couldn't load lock module";
return nil;
}
lock->init();
}
newdev := ref Device;
newdev.lock = Semaphore.new();
newdev.local = modeminfo.path;
newdev.pid = 0;
newdev.t = modeminfo;
return newdev;
}
#
# dial a number
#
dial(d: ref Device, number: string)
{
if (d==nil) {
raise "fail: Device not initialized";
return;
}
monitoring(d);
# modem type should already be established, but just in case
sys->print("Attention\n");
x := attention(d);
if (x != Ok)
sys->print("Attention failed\n");
#
# extended Hayes commands, meaning depends on modem (VGA all over again)
#
sys->print("Init\n");
if(d.t.country != nil)
applyspecial(d, d.t.country);
if(d.t.init != nil)
applyspecial(d, d.t.init);
if(d.t.other != nil)
applyspecial(d, d.t.other);
applyspecial(d, d.t.errorcorrection);
compress := Abort;
if(d.t.mnponly != nil)
compress = applyspecial(d, d.t.mnponly);
if(d.t.compression != nil)
compress = applyspecial(d, d.t.compression);
rateadjust := Abort;
if(compress != Ok)
rateadjust = applyspecial(d, d.t.rateadjust);
applyspecial(d, d.t.flowctl);
# finally, dialout
sys->print("Dialing\n");
if((dt := d.t.dialtype) == nil)
dt = "ATDT";
if(send(d, sys->sprint("%s%s\r", dt, number)) < 0) {
raise "can't dial "+number;
return;
}
(i, msg) := readmsg(d, 120, nil);
if(i != Success) {
raise "fail: "+msg;
return;
}
connectspeed := getspeed(msg, d.speed);
# change line rate if not compressing
if(rateadjust == Ok)
setspeed(d, connectspeed);
if(d.ctl != nil){
if(d != nil)
sys->fprint(d.ctl, "s%d", connectspeed); # set DCE speed (if device implements it)
sys->fprint(d.ctl, "c1"); # enable CD monitoring
}
}
|