File: ttyacm-multiplex

package info (click to toggle)
chiark-tcl-applet 1.0-2
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 144 kB
  • sloc: tcl: 860; makefile: 32
file content (335 lines) | stat: -rwxr-xr-x 8,087 bytes parent folder | download
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
#!/usr/bin/tclsh8.4
# -*- tcl -*-

# Copyright 2016,2020 Ian Jackson
# SPDX-License-Identifier: GPL-3.0-or-later
# There is NO WARRANTY.

# This is unfinished attempt at a program to multiplex multiple
# clients all wanting to speak to a /dev/ttyACM GSM modem / GPS.

set expected_devs 3

proc debug {m} { puts "DEBUG $m" }

proc log {m} { puts "LOG $m" }

proc experror {m} {
    error $m {} EXPECTED
}

proc find-devices {} {
    global errorCode errorInfo devices expected_devs
    set base /sys/class/tty
    foreach candidate [glob -nocomplain -directory $base -tails ttyACM*] {
	debug "candidate $candidate"
	if {[catch { file link $base/$candidate/device } ltarget]} {
	    debug " readlink failed [lrange $errorCode 0 1]"
	    switch -glob $errorCode {
		{POSIX EINVAL *} continue
		{POSIX ENOENT *} continue
		default { error "$ltarget \[$errorCode] $errorInfo $errorCode" }
	    }
	}
	if {![regexp {^(.*)\.(\d+)$} $ltarget dummy dbase interf]} {
	    debug " readlink bad target $ltarget"
	    continue
	}
	debug " approved $dbase $interf $candidate"
	lappend devs($dbase) [list $interf $candidate]
    }
    set howmany [array size devs]
    if {!$howmany} {
	experror "no appropriate device(s) found"
    }
    if {$howmany > 1} {
	experror "several appropriate device(s) found [array names $devs]"
    }
    set devices {}
    foreach dev [lsort -index 0 -integer $devs([lindex [array names devs] 0])] {
	lappend devices [lindex $dev 1]
    }
    if {[llength $devices] != $expected_devs} {
	experror "wrong # devices ($devices), expected $expected"
    }
}

proc create-dev-nodes {} {
    global devices expected_devs
    set ourdevs /dev/atmux
    set ttyat ttyAT
    for {set i 0} {$i < $expected_devs-1} {incr i} {
	set new $ttyAT$i
	file link -symbolic ../$device $ourdevs/.new.$new
	file rename -force $ourdevs/.new.$new $ourdevs/$new
	set wanted($new) 1
    }
    foreach candidate [glob -nocomplain -directory $ourdevs -tails ttyAT*] {
	if {![info exists wanted($candidate)]} {
	    file remove $ourdevs/$candidate
	}
    }
}

proc reopen-our-device {} {
    global devices dchan
    set dchan [open /dev/[lindex $devices 1] r+]
    fconfigure $dchan -blocking no -buffering line -translation {crlf cr}
    read $dchan; # flush input
    puts $dchan ATE0
    flush $dchan
    after 250
    set result [read $dchan]
    if {![regexp -line {^OK$} $result]} { experror "got [logquote $result]" }
    fileevent $dchan readable chan-readable $dchan dchan "modem device"
}

proc devices {} {
    find-devices
    create-dev-nodes
    reopen-our-device
}

proc dchan-failure {dummy} {
    global dchan
    if {[info exists dchan]} {
	catch { close $dchan }
	catch { unset dchan }
    }
}

proc try-open-our-device {} {
    global devices
    if {[catch {
	reopen-our-device
    } emsg]} {
	devfailure $emsg
	return
    }
    sendout-async *TTYATMUX "*TTYATMUXDEVS [join $devices ,]"
    sendout-async *TTYATMUX "*TTYATMUXOPEN"
}

proc trap-log {what body var} {
    global errorCode errorInfo
    upvar 1 $var result
    set rc [catch {
	uplevel 1 $body
    } result]
    switch -exact $rc {
	1 {
	    switch -glob $errorCode {
		{POSIX *} - EXPECTED { log "$what: $result" }
		default {
		    log "unexpected: $what: $result"
		    foreach l [split $errorInfo "\n"] { log "  $l" }
		}
	    }
	    return 1
	}
	0 {
	    return 0
	}
	default {
	    return -code $rc -errorinfo $errorInfo \
		-errorcode $errorCode $result
	}
    }
}

proc chan-readable {chan how what args} {
    while 1 {
	if {[trap-log "$what failure" {
	    gets $chan l
	} r]} {
	    $how-failure $chan
	    return
	}
	if {$r<0} {
	    if {![eof $chan]} return
	    log "device eof"
	    $how-failure $chan
	    return
	}
	trap-log "error processing $what data" {
	    $how-line $l
	} dummy
    }
}

proc async-notif fixme this has wrong arguments

proc async-notif-or-resp-fixed {asid l} {
    global current_command_asid
    if {![string compare $asid $current_command_asid]} {
	sync-reply $l
    } else {
	async-notif $asid $l
    }
}

proc async-notif-creg {asid l} {
    set ll [llength [split $l ,]]
    switch -exact $ll {
	4 { sync-reply $l }
	3 { 
	    async-notif {+CREG} 2 2 $l
	    async-notif {+CREG} 1 1 [lindex [split $l ,] 0]
	}
	2 { sync-reply $l }
	1 { 
	    async-notif {+CREG} 1 2 $l
	}
	default {
	    bad-data $l "async-notif-creg $ll"
	}
    }
}

proc async-control-max0 {c l allows} {
    async-control-core $c $l $allows {
	set wanted 0
    } {
	set tw 0
	manyset $ca($c) tw
	if {$tw} { set wanted 1 }
    } {
	set send $wanted
	foreach allow $allows { lappend $send [lindex $allow 0] }
	sync-subcommand $c "$cmd=[join $send ,]" async-updated-ok $c
    }
}

proc async-control-cmer {c l allows} {
    async-control-core $c $l $allows {
	set send 0,0,0,0
    } {
	set mode 0; set ind 0
	manyset $ca($c) mode keyp disp ind bfr
	if {$mode==3 && $ind} { set send 3,0,0,1 }
    } {
	sync-subcommand $c "$cmd=$send" async-updated-ok $c
    }
}

proc async-updated-ok

proc async-control-core {c l allows ubody_init ubody_perclient ubody_finish} {
    global clients
    uplevel 1 cmd cmd
    if {[regexp {^(AT[^=?])\?$} $l dummy cmd]} {
	sync-subcommand $c $l async-massage-result-subs $c $cmd
    } elseif {[regexp {^(AT[^=?])=\?$} $l dummy cmd]} {
	sync-subcommand $c $l async-massage-result-support $c $cmd $allows
    } elseif {[regexp {^(AT[^=?])=([0-9,]+)$} $l dummy cmd values]} {
	set values [split $values ,]
	if {[llength $values] > [llength $allows]} {
	    bad-command "too many values"
	    return
	}
	while {[llength $values] < [llength $allows]} {
	    lappend values 0
	}
	foreach val $values allow $allows {
	    if {[lsearch -exact $allow $val]<0} {
		bad-command "$val not in allowed $allow ($allows)"
		return
	    }
	}
	uplevel 1 [list upvar #0 client_async/$cmd ca]
	upvar #0 client_async/$cmd ca
	set ca($c) $values
	uplevel 1 $ubody_init
	upvar 1 c uc
	foreach uc [array names clients] {
	    uplevel 1 $ubody_perclient
	}
	uplevel 1 $ubody_finish
    } else {
	bad-command "unknown async control syntax"
    }
}

proc set-client-echo {c yn} {
    global client_echo
    set client_echo($c) 0
    client-command-complete $c OK
}

proc simple-command {c l} {
    sync-subcommand $c $l simple-command-complete
}

proc client-command-complete

proc process-client-command {c nl} {
    switch -regexp $l {
	{^AT\+CREG\b} { async-control-max0 $c $l {{0 1 2}} }
	{^AT\+CGREG\b} { async-control-max0 $c $l {{0 1 2}} }
	{^AT\*ERINFO\b} { async-control-max0 $c $l {{0 1}} }
	{^AT\+CGEREP\b} { async-control-max0 $c $l {{0 1 2} 0} }
	{^AT\+CMER\b} { async-control-cmer $c $l {{0 3} 0 0 {0 1} 0} }
	{^ATE0$} { set-client-echo $c 0 }
	{^ATE1$} { set-client-echo $c 1 }
	{^AT\+CFUN\b} { simple-command $c $l }
	default { bad-command "unknown command" }
    }
}

proc dchan-line {l} {
    global cclient
    switch -regexp $l {
	{\+CREG:}   { async-notif-creg             +CREG     $l }
	{\+CGREG:}  { async-notif-creg             +CGREG    $l }
	{\*ERINFO:} { async-notif-or-resp-fixed    *ERINFO   $l }
	{\+CGEV:}   { async-notif                  +CGEREP   $l }
	{\+CIEV:}   { async-notif                  +CMER     $l }
	default     { syync-reply $l }
    }
}

proc cchan-line {c l} {
    lappend queue [list $c $l]
    check-busy
}

proc sync-subcommand {c l args} {
    global busy dchan
    if {[info exists busy]} { error "already busy $busy; want $c $l $args" }
    if {[trap-log "write device" { puts $dchan $l } dummy]} {
	

proc sync-reply {l} {
    global busy
    if {![info exists busy]} {
	bad-data $l "unexpected sync reply"
	return
    }
    eval $

proc check-busy {} {
    global busy queue
    while {![info exists busy] && [llength $queue]} {
	manyset [lindex $queue 0] c l
	set queue [lrange $queue 1 end]
	if {[trap-log "process for $c [logquote $l]"] {
	    process-client-command $c $l
	} dummy]} {
	    client-command-complete $c ERROR
	}
    }
}

proc client-command-complete {c l} {
    if {[trap-log "write to $c" { puts $c $l } dummy]} {
	client-failure $c
    }
}

proc cchan-readable {

proc sendout-async
proc logquote


try-open-our-device