/
at.go
819 lines (737 loc) · 20.3 KB
/
at.go
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <warthog618@gmail.com>.
// Package at provides a low level driver for AT modems.
package at
import (
"bufio"
"fmt"
"io"
"strings"
"time"
"github.com/pkg/errors"
)
// AT represents a modem that can be managed using AT commands.
//
// Commands can be issued to the modem using the Command and SMSCommand methods.
//
// The AT closes the closed channel when the connection to the underlying
// modem is broken (Read returns EOF).
//
// When closed, all outstanding commands return ErrClosed and the state of the
// underlying modem becomes unknown.
//
// Once closed the AT cannot be re-opened - it must be recreated.
type AT struct {
// channel for commands issued to the modem
//
// Handled by the cmdLoop.
cmdCh chan func()
// channel for changes to inds
//
// Handled by the indLoop.
indCh chan func()
// closed when modem is closed
closed chan struct{}
// channel for all lines read from the modem
//
// Handled by the indLoop.
iLines chan string
// channel for lines read from the modem after indications removed
//
// Handled by the cmdLoop.
cLines chan string
// the underlying modem
//
// Only accessed from the cmdLoop.
modem io.ReadWriter
// the minimum time between an escape command and the subsequent command
escTime time.Duration
// time to wait for individual commands to complete
cmdTimeout time.Duration
// indications mapped by prefix
//
// Only accessed from the indLoop
inds map[string]Indication
// commands issued by Init.
initCmds []string
// if not-nil, the timer that must expire before the subsequent command is issued
//
// Only accessed from the cmdLoop.
escGuard *time.Timer
}
// Option is a construction option for an AT.
type Option interface {
applyOption(*AT)
}
// CommandOption defines a behaviouralk option for Command and SMSCommand.
type CommandOption interface {
applyCommandOption(*commandConfig)
}
// InitOption defines a behaviouralk option for Init.
type InitOption interface {
applyInitOption(*initConfig)
}
// New creates a new AT modem.
func New(modem io.ReadWriter, options ...Option) *AT {
a := &AT{
modem: modem,
cmdCh: make(chan func()),
indCh: make(chan func()),
iLines: make(chan string),
cLines: make(chan string),
closed: make(chan struct{}),
escTime: 20 * time.Millisecond,
cmdTimeout: time.Second,
inds: make(map[string]Indication),
}
for _, option := range options {
option.applyOption(a)
}
if a.initCmds == nil {
a.initCmds = []string{
"Z", // reset to factory defaults (also clears the escape from the rx buffer)
"E0", // disable echo
}
}
go lineReader(a.modem, a.iLines)
go a.indLoop(a.indCh, a.iLines, a.cLines)
go cmdLoop(a.cmdCh, a.cLines, a.closed)
return a
}
const (
sub = "\x1a"
esc = "\x1b"
)
// WithEscTime sets the guard time for the modem.
//
// The escape time is the minimum time between an escape command being sent to
// the modem and any subsequent commands.
//
// The default guard time is 20msec.
func WithEscTime(d time.Duration) EscTimeOption {
return EscTimeOption(d)
}
// EscTimeOption defines the escape guard time for the modem.
type EscTimeOption time.Duration
func (o EscTimeOption) applyOption(a *AT) {
a.escTime = time.Duration(o)
}
// InfoHandler receives indication info.
type InfoHandler func([]string)
// WithIndication adds an indication during construction.
func WithIndication(prefix string, handler InfoHandler, options ...IndicationOption) Indication {
return newIndication(prefix, handler, options...)
}
func (o Indication) applyOption(a *AT) {
a.inds[o.prefix] = o
}
// CmdsOption specifies the set of AT commands issued by Init.
type CmdsOption []string
func (o CmdsOption) applyOption(a *AT) {
a.initCmds = []string(o)
}
func (o CmdsOption) applyInitOption(i *initConfig) {
i.cmds = []string(o)
}
// WithCmds specifies the set of AT commands issued by Init.
//
// The default commands are ATZ.
func WithCmds(cmds ...string) CmdsOption {
return CmdsOption(cmds)
}
// WithTimeout specifies the maximum time allowed for the modem to complete a
// command.
func WithTimeout(d time.Duration) TimeoutOption {
return TimeoutOption(d)
}
// TimeoutOption specifies the maximum time allowed for the modem to complete a
// command.
type TimeoutOption time.Duration
func (o TimeoutOption) applyOption(a *AT) {
a.cmdTimeout = time.Duration(o)
}
func (o TimeoutOption) applyInitOption(i *initConfig) {
i.cmdOpts = append(i.cmdOpts, o)
}
func (o TimeoutOption) applyCommandOption(c *commandConfig) {
c.timeout = time.Duration(o)
}
// AddIndication adds a handler for a set of lines beginning with the prefixed
// line and the following trailing lines.
func (a *AT) AddIndication(prefix string, handler InfoHandler, options ...IndicationOption) (err error) {
ind := newIndication(prefix, handler, options...)
errs := make(chan error)
indf := func() {
if _, ok := a.inds[ind.prefix]; ok {
errs <- ErrIndicationExists
return
}
a.inds[ind.prefix] = ind
close(errs)
}
select {
case <-a.closed:
err = ErrClosed
case a.indCh <- indf:
err = <-errs
}
return
}
// CancelIndication removes any indication corresponding to the prefix.
//
// If any such indication exists its return channel is closed and no further
// indications will be sent to it.
func (a *AT) CancelIndication(prefix string) {
done := make(chan struct{})
indf := func() {
delete(a.inds, prefix)
close(done)
}
select {
case <-a.closed:
case a.indCh <- indf:
<-done
}
}
// Closed returns a channel which will block while the modem is not closed.
func (a *AT) Closed() <-chan struct{} {
return a.closed
}
// Command issues the command to the modem and returns the result.
//
// The command should NOT include the AT prefix, nor <CR><LF> suffix which is
// automatically added.
//
// The return value includes the info (the lines returned by the modem between
// the command and the status line), or an error if the command did not
// complete successfully.
func (a *AT) Command(cmd string, options ...CommandOption) ([]string, error) {
cfg := commandConfig{timeout: a.cmdTimeout}
for _, option := range options {
option.applyCommandOption(&cfg)
}
done := make(chan response)
cmdf := func() {
info, err := a.processReq(cmd, cfg.timeout)
done <- response{info: info, err: err}
}
select {
case <-a.closed:
return nil, ErrClosed
case a.cmdCh <- cmdf:
rsp := <-done
return rsp.info, rsp.err
}
}
// Escape issues an escape sequence to the modem.
//
// It does not wait for any response, but it does inhibit subsequent commands
// until the escTime has elapsed.
//
// The escape sequence is "\x1b\r\n". Additional characters may be added to
// the sequence using the b parameter.
func (a *AT) Escape(b ...byte) {
done := make(chan struct{})
cmdf := func() {
a.escape(b...)
close(done)
}
select {
case <-a.closed:
case a.cmdCh <- cmdf:
<-done
}
}
// Init initialises the modem by escaping any outstanding SMS commands and
// resetting the modem to factory defaults.
//
// The Init is intended to be called after creation and before any other
// commands are issued in order to get the modem into a known state. It can
// also be used subsequently to return the modem to a known state.
//
// The default init commands can be overridden by the options parameter.
func (a *AT) Init(options ...InitOption) error {
// escape any outstanding SMS operations then CR to flush the command
// buffer
a.Escape([]byte("\r\n")...)
cfg := initConfig{cmds: a.initCmds}
for _, option := range options {
option.applyInitOption(&cfg)
}
for _, cmd := range cfg.cmds {
_, err := a.Command(cmd, cfg.cmdOpts...)
switch err {
case nil:
case ErrDeadlineExceeded:
return err
default:
return fmt.Errorf("AT%s returned error: %w", cmd, err)
}
}
return nil
}
// SMSCommand issues an SMS command to the modem, and returns the result.
//
// An SMS command is issued in two steps; first the command line:
//
// AT<command><CR>
//
// which the modem responds to with a ">" prompt, after which the SMS PDU is
// sent to the modem:
//
// <sms><Ctrl-Z>
//
// The modem then completes the command as per other commands, such as those
// issued by Command.
//
// The format of the sms may be a text message or a hex coded SMS PDU,
// depending on the configuration of the modem (text or PDU mode).
func (a *AT) SMSCommand(cmd string, sms string, options ...CommandOption) (info []string, err error) {
cfg := commandConfig{timeout: a.cmdTimeout}
for _, option := range options {
option.applyCommandOption(&cfg)
}
done := make(chan response)
cmdf := func() {
info, err := a.processSmsReq(cmd, sms, cfg.timeout)
done <- response{info: info, err: err}
}
select {
case <-a.closed:
return nil, ErrClosed
case a.cmdCh <- cmdf:
rsp := <-done
return rsp.info, rsp.err
}
}
// cmdLoop is responsible for the interface to the modem.
//
// It serialises the issuing of commands and awaits the responses.
// If no command is pending then any lines received are discarded.
//
// The cmdLoop terminates when the downstream closes.
func cmdLoop(cmds chan func(), in <-chan string, out chan struct{}) {
for {
select {
case cmd := <-cmds:
cmd()
case _, ok := <-in:
if !ok {
close(out)
return
}
}
}
}
// lineReader takes lines from m and redirects them to out.
//
// lineReader exits when m closes.
func lineReader(m io.Reader, out chan string) {
scanner := bufio.NewScanner(m)
scanner.Split(scanLines)
for scanner.Scan() {
out <- scanner.Text()
}
close(out) // tell pipeline we're done - end of pipeline will close the AT.
}
// indLoop is responsible for pulling indications from the stream of lines read
// from the modem, and forwarding them to handlers.
//
// Non-indication lines are passed upstream. Indication trailing lines are
// assumed to arrive in a contiguous block immediately after the indication.
//
// indLoop exits when the in channel closes.
func (a *AT) indLoop(cmds chan func(), in <-chan string, out chan string) {
defer close(out)
for {
select {
case cmd := <-cmds:
cmd()
case line, ok := <-in:
if !ok {
return
}
for prefix, ind := range a.inds {
if strings.HasPrefix(line, prefix) {
n := make([]string, ind.lines)
n[0] = line
for i := 1; i < ind.lines; i++ {
t, ok := <-in
if !ok {
return
}
n[i] = t
}
go ind.handler(n)
continue
}
}
out <- line
}
}
}
// issue an escape command
//
// This should only be called from within the cmdLoop.
func (a *AT) escape(b ...byte) {
cmd := append([]byte(esc+"\r\n"), b...)
a.modem.Write(cmd)
a.escGuard = time.NewTimer(a.escTime)
}
// perform a request - issuing the command and awaiting the response.
func (a *AT) processReq(cmd string, timeout time.Duration) (info []string, err error) {
a.waitEscGuard()
err = a.writeCommand(cmd)
if err != nil {
return
}
cmdID := parseCmdID(cmd)
var expChan <-chan time.Time
if timeout >= 0 {
expiry := time.NewTimer(timeout)
expChan = expiry.C
defer expiry.Stop()
}
for {
select {
case <-expChan:
err = ErrDeadlineExceeded
return
case line, ok := <-a.cLines:
if !ok {
return nil, ErrClosed
}
if line == "" {
continue
}
lt := parseRxLine(line, cmdID)
i, done, perr := a.processRxLine(lt, line)
if i != nil {
info = append(info, *i)
}
if perr != nil {
err = perr
return
}
if done {
return
}
}
}
}
// perform a SMS request - issuing the command, awaiting the prompt, sending
// the data and awaiting the response.
func (a *AT) processSmsReq(cmd string, sms string, timeout time.Duration) (info []string, err error) {
a.waitEscGuard()
err = a.writeSMSCommand(cmd)
if err != nil {
return
}
cmdID := parseCmdID(cmd)
var expChan <-chan time.Time
if timeout >= 0 {
expiry := time.NewTimer(timeout)
expChan = expiry.C
defer expiry.Stop()
}
for {
select {
case <-expChan:
// cancel outstanding SMS request
a.escape()
err = ErrDeadlineExceeded
return
case line, ok := <-a.cLines:
if !ok {
err = ErrClosed
return
}
if line == "" {
continue
}
lt := parseRxLine(line, cmdID)
i, done, perr := a.processSmsRxLine(lt, line, sms)
if i != nil {
info = append(info, *i)
}
if perr != nil {
err = perr
return
}
if done {
return
}
}
}
}
// processRxLine parses a line received from the modem and determines how it
// adds to the response for the current command.
//
// The return values are:
// - a line of info to be added to the response (optional)
// - a flag indicating if the command is complete.
// - an error detected while processing the command.
func (a *AT) processRxLine(lt rxl, line string) (info *string, done bool, err error) {
switch lt {
case rxlStatusOK:
done = true
case rxlStatusError:
err = newError(line)
case rxlUnknown, rxlInfo:
info = &line
case rxlConnect:
info = &line
done = true
case rxlConnectError:
err = ConnectError(line)
}
return
}
// processSmsRxLine parses a line received from the modem and determines how it
// adds to the response for the current command.
//
// The return values are:
// - a line of info to be added to the response (optional)
// - a flag indicating if the command is complete.
// - an error detected while processing the command.
func (a *AT) processSmsRxLine(lt rxl, line string, sms string) (info *string, done bool, err error) {
switch lt {
case rxlUnknown:
if strings.HasSuffix(line, sub) && strings.HasPrefix(line, sms) {
// swallow echoed SMS PDU
return
}
info = &line
case rxlSMSPrompt:
if err = a.writeSMS(sms); err != nil {
// escape SMS
a.escape()
}
default:
return a.processRxLine(lt, line)
}
return
}
// waitEscGuard waits for a write guard to allow a write to the modem.
//
// This should only be called from within the cmdLoop.
func (a *AT) waitEscGuard() {
if a.escGuard == nil {
return
}
Loop:
for {
select {
case _, ok := <-a.cLines:
if !ok {
a.escGuard.Stop()
break Loop
}
case <-a.escGuard.C:
break Loop
}
}
a.escGuard = nil
}
// writeCommand writes a one line command to the modem.
//
// This should only be called from within the cmdLoop.
func (a *AT) writeCommand(cmd string) error {
cmdLine := "AT" + cmd + "\r\n"
_, err := a.modem.Write([]byte(cmdLine))
return err
}
// writeSMSCommand writes a the first line of an SMS command to the modem.
//
// This should only be called from within the cmdLoop.
func (a *AT) writeSMSCommand(cmd string) error {
cmdLine := "AT" + cmd + "\r"
_, err := a.modem.Write([]byte(cmdLine))
return err
}
// writeSMS writes the first line of a two line SMS command to the modem.
//
// This should only be called from within the cmdLoop.
func (a *AT) writeSMS(sms string) error {
_, err := a.modem.Write([]byte(sms + string(sub)))
return err
}
// CMEError indicates a CME Error was returned by the modem.
//
// The value is the error value, in string form, which may be the numeric or
// textual, depending on the modem configuration.
type CMEError string
// CMSError indicates a CMS Error was returned by the modem.
//
// The value is the error value, in string form, which may be the numeric or
// textual, depending on the modem configuration.
type CMSError string
// ConnectError indicates an attempt to dial failed.
//
// The value of the error is the failure indication returned by the modem.
type ConnectError string
func (e CMEError) Error() string {
return string("CME Error: " + e)
}
func (e CMSError) Error() string {
return string("CMS Error: " + e)
}
func (e ConnectError) Error() string {
return string("Connect: " + e)
}
var (
// ErrClosed indicates an operation cannot be performed as the modem has
// been closed.
ErrClosed = errors.New("closed")
// ErrDeadlineExceeded indicates the modem failed to complete an operation
// within the required time.
ErrDeadlineExceeded = errors.New("deadline exceeded")
// ErrError indicates the modem returned a generic AT ERROR in response to
// an operation.
ErrError = errors.New("ERROR")
// ErrIndicationExists indicates there is already a indication registered
// for a prefix.
ErrIndicationExists = errors.New("indication exists")
)
// newError parses a line and creates an error corresponding to the content.
func newError(line string) error {
var err error
switch {
case strings.HasPrefix(line, "ERROR"):
err = ErrError
case strings.HasPrefix(line, "+CMS ERROR:"):
err = CMSError(strings.TrimSpace(line[11:]))
case strings.HasPrefix(line, "+CME ERROR:"):
err = CMEError(strings.TrimSpace(line[11:]))
}
return err
}
// response represents the result of a request operation performed on the
// modem.
//
// info is the collection of lines returned between the command and the status
// line. err corresponds to any error returned by the modem or while
// interacting with the modem.
type response struct {
info []string
err error
}
// Received line types.
type rxl int
const (
rxlUnknown rxl = iota
rxlEchoCmdLine
rxlInfo
rxlStatusOK
rxlStatusError
rxlAsync
rxlSMSPrompt
rxlConnect
rxlConnectError
)
// Indication represents an unsolicited result code (URC) from the modem, such
// as a received SMS message.
//
// Indications are lines prefixed with a particular pattern, and may include a
// number of trailing lines. The matching lines are bundled into a slice and
// sent to the handler.
type Indication struct {
prefix string
lines int
handler InfoHandler
}
func newIndication(prefix string, handler InfoHandler, options ...IndicationOption) Indication {
ind := Indication{
prefix: prefix,
handler: handler,
lines: 1,
}
for _, option := range options {
option.applyIndicationOption(&ind)
}
return ind
}
// IndicationOption alters the behavior of the indication.
type IndicationOption interface {
applyIndicationOption(*Indication)
}
// TrailingLinesOption specifies the number of trailing lines expected after an
// indication line.
type TrailingLinesOption int
func (o TrailingLinesOption) applyIndicationOption(ind *Indication) {
ind.lines = int(o) + 1
}
// WithTrailingLines indicates the number of lines after the line containing
// the indication that arew to be collected as part of the indication.
//
// The default is 0 - only the indication line itself is collected and returned.
func WithTrailingLines(l int) TrailingLinesOption {
return TrailingLinesOption(l)
}
// WithTrailingLine indicates the indication includes one line after the line
// containing the indication.
var WithTrailingLine = TrailingLinesOption(1)
// parseCmdID returns the identifier component of the command.
//
// This is the section prior to any '=' or '?' and is generally, but not
// always, used to prefix info lines corresponding to the command.
func parseCmdID(cmdLine string) string {
if idx := strings.IndexAny(cmdLine, "=?"); idx != -1 {
return cmdLine[0:idx]
}
return cmdLine
}
// parseRxLine parses a received line and identifies the line type.
func parseRxLine(line string, cmdID string) rxl {
switch {
case line == "OK":
return rxlStatusOK
case strings.HasPrefix(line, "ERROR"),
strings.HasPrefix(line, "+CME ERROR:"),
strings.HasPrefix(line, "+CMS ERROR:"):
return rxlStatusError
case strings.HasPrefix(line, cmdID+":"):
return rxlInfo
case line == ">":
return rxlSMSPrompt
case strings.HasPrefix(line, "AT"+cmdID):
return rxlEchoCmdLine
case len(cmdID) == 0 || cmdID[0] != 'D':
// Short circuit non-ATD commands.
// No attempt to identify SMS PDUs at this level, so they will
// be caught here, along with other unidentified lines.
return rxlUnknown
case strings.HasPrefix(line, "CONNECT"):
return rxlConnect
case line == "BUSY",
line == "NO ANSWER",
line == "NO CARRIER",
line == "NO DIALTONE":
return rxlConnectError
default:
// No attempt to identify SMS PDUs at this level, so they will
// be caught here, along with other unidentified lines.
return rxlUnknown
}
}
// scanLines is a custom line scanner for lineReader that recognises the prompt
// returned by the modem in response to SMS commands such as +CMGS.
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
// handle SMS prompt special case - no CR at prompt
if len(data) >= 1 && data[0] == '>' {
i := 1
// there may be trailing space, so swallow that...
for ; i < len(data) && data[i] == ' '; i++ {
}
return i, data[0:1], nil
}
return bufio.ScanLines(data, atEOF)
}
type commandConfig struct {
timeout time.Duration
}
type initConfig struct {
cmds []string
cmdOpts []CommandOption
}