-
Notifications
You must be signed in to change notification settings - Fork 53
/
at.go
455 lines (407 loc) · 11 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
package at
import (
"bufio"
"errors"
"os"
"strings"
"time"
"github.com/xlab/at/calls"
"github.com/xlab/at/pdu"
"github.com/xlab/at/sms"
)
// DefaultTimeout to close the connection in case of modem is being not responsive at all.
const DefaultTimeout = time.Minute
// <CR><LF> sequence.
const Sep = "\r\n"
// Ctrl+Z code.
const Sub = "\x1A"
// Common errors.
var (
ErrTimeout = errors.New("at: timeout")
ErrUnknownEncoding = errors.New("at: unsupported encoding")
ErrClosed = errors.New("at: device ports are closed")
ErrNotInitialized = errors.New("at: not initialized")
ErrWriteFailed = errors.New("at: write failed")
ErrParseReport = errors.New("at: error while parsing report")
ErrUnknownReport = errors.New("at: got unknown report")
)
// Encoding is an encoding option to use.
type Encoding byte
// Encodings represents all the supported encodings.
var Encodings = struct {
Gsm7Bit Encoding
UCS2 Encoding
}{
15, 72,
}
// Device represents a physical modem that supports Hayes AT-commands.
type Device struct {
// Name is the label to distinguish different devices.
Name string
// CommandPort is the path or name of command serial port.
CommandPort string
// CommandPort is the path or name of notification serial port.
NotifyPort string
// State holds the device state.
State *DeviceState
// Commands is a profile that provides implementation of Init and the other commands.
Commands DeviceProfile
// Timeout to override the default timeout (1m)
Timeout time.Duration
cmdPort *os.File
notifyPort *os.File
incomingCallerIDs chan *calls.CallerID
messages chan *sms.Message
ussd chan Ussd
updated chan struct{}
closed chan struct{}
active bool
}
// IncomingCallerID fires when an incoming caller ID was received.
func (d *Device) IncomingCallerID() <-chan *calls.CallerID {
return d.incomingCallerIDs
}
// IncomingSms fires when an SMS was received.
func (d *Device) IncomingSms() <-chan *sms.Message {
return d.messages
}
// UssdReply fires when an Ussd reply was received.
func (d *Device) UssdReply() <-chan Ussd {
return d.ussd
}
// StateUpdate fires when DeviceState was updated by a received event.
func (d *Device) StateUpdate() <-chan struct{} {
return d.updated
}
// Closed fires when the connection was closed.
func (d *Device) Closed() <-chan struct{} {
return d.closed
}
// sendInteractive is a special case of Send, but this one is used whether
// a prompt should be received first (i.e. when sending SMS, the PDU should be
// entered after the device replied with '>') and then the second part of payload
// should be sent (the second payload will be sent using Send).
func (d *Device) sendInteractive(part1, part2 string, prompt byte) (reply string, err error) {
err = d.withTimeout(func() error {
_, err := d.cmdPort.Write([]byte(part1 + Sep))
if err != nil {
return err
}
// finally: send control character to exit interactive mode
defer d.cmdPort.Write([]byte{pdu.Esc})
buf := bufio.NewReader(d.cmdPort)
reply, err = buf.ReadString(prompt)
if err != nil {
return err
}
reply, err = d.Send(part2 + Sub)
return err
})
return reply, err
}
// sanityCheck checks whether ports are opened and (if requested) that the initialization
// was done.
func (d *Device) sanityCheck(initialized bool) error {
if d.cmdPort == nil {
return ErrClosed
}
if d.notifyPort == nil {
return ErrClosed
}
if initialized {
if d.Commands == nil {
return ErrNotInitialized
}
}
return nil
}
// Send writes a command to the device's command port and parses the output.
// Result will not contain any FinalReply since they're used to detect error status.
// Multiple lines will be joined with '\n'.
func (d *Device) Send(req string) (reply string, err error) {
if err = d.sanityCheck(true); err != nil {
return
}
err = d.withTimeout(func() error {
_, err := d.cmdPort.Write([]byte(req + Sep))
if err != nil {
return err
}
var line string
buf := bufio.NewReader(d.cmdPort)
if line, err = buf.ReadString('\r'); err != nil {
return err
}
text := strings.TrimSpace(line)
if !strings.HasPrefix(req, text) {
return err
}
var done bool
for !done {
if line, err = buf.ReadString('\r'); err != nil {
break
}
text := strings.TrimSpace(line)
if len(text) < 1 {
continue
}
switch opt := FinalResults.Resolve(text); opt {
case FinalResults.Ok, FinalResults.Noop:
done = true
case FinalResults.Timeout:
err = ErrTimeout
done = true
case FinalResults.CmeError, FinalResults.CmsError:
err = errors.New(text)
done = true
case FinalResults.Error, FinalResults.NotSupported,
FinalResults.TooManyParameters, FinalResults.NoCarrier:
err = errors.New(opt.Description)
done = true
default:
if len(reply) > 0 {
reply += "\n"
}
reply += text
}
}
return err
})
return
}
// runs the passed method with a timeout set on the cmdPort
func (d *Device) withTimeout(f func() error) error {
timeout := d.Timeout
if timeout == 0 {
timeout = DefaultTimeout
}
// enable deadline
d.cmdPort.SetDeadline(time.Now().Add(timeout))
err := f()
// disable deadline
d.cmdPort.SetDeadline(time.Time{})
if err != nil && os.IsTimeout(err) {
// reset connection on timeouts
d.cmdPort.Write([]byte(KillCmd + Sep))
}
return err
}
// Watch starts a monitoring process that will wait for events
// from the device's notification port.
func (d *Device) Watch() error {
if d.notifyPort == nil {
return errors.New("at: notification port not initialized")
}
go func() {
<-d.closed
d.notifyPort.Write([]byte(KillCmd + Sep))
}()
buf := bufio.NewReader(d.notifyPort)
for {
select {
case <-d.closed:
return nil
default:
line, err := buf.ReadString(byte('\r'))
if err != nil {
d.Close()
return nil
}
text := strings.TrimSpace(line)
if len(text) < 1 {
continue
}
d.handleReport(text) // ignore errors
}
}
}
// handleReport detects and parses a report from the notification port represented
// as a string. The parsed values may change the inner state or be sent over out channels.
func (d *Device) handleReport(str string) (err error) {
report := Reports.Resolve(str)
str = strings.TrimSpace(strings.TrimPrefix(str, report.ID))
switch report {
case Reports.CallerID:
var report callerIDReport
if err = report.Parse(str); err != nil {
return
}
callerID := report.GetCallerID()
d.incomingCallerIDs <- callerID
case Reports.Message:
var report messageReport
if err = report.Parse(str); err != nil {
return
}
var octets []byte
octets, err = d.Commands.CMGR(report.Index)
if err != nil {
return
}
if err = d.Commands.CMGD(report.Index, DeleteOptions.Index); err != nil {
return
}
var msg sms.Message
if _, err = msg.ReadFrom(octets); err != nil {
return
}
d.messages <- &msg
case Reports.Ussd:
var ussd ussdReport
if err = ussd.Parse(str); err != nil {
return
}
var text string
if ussd.Enc == Encodings.UCS2 {
text, err = pdu.DecodeUcs2(ussd.Octets, false)
if err != nil {
return
}
} else if ussd.Enc == Encodings.Gsm7Bit {
text, err = pdu.Decode7Bit(ussd.Octets)
if err != nil {
return
}
} else {
return ErrUnknownEncoding
}
d.ussd <- Ussd(text)
case Reports.SignalStrength:
var rssi signalStrengthReport
if err = rssi.Parse(str); err != nil {
return
}
if d.State.SignalStrength != int(rssi) {
d.State.SignalStrength = int(rssi)
d.updated <- struct{}{}
}
case Reports.Mode:
var report modeReport
if err = report.Parse(str); err != nil {
return
}
var updated bool
if d.State.SystemMode != report.Mode {
d.State.SystemMode = report.Mode
updated = true
}
if d.State.SystemSubmode != report.Submode {
d.State.SystemSubmode = report.Submode
updated = true
}
if updated {
d.updated <- struct{}{}
}
case Reports.ServiceState:
var report serviceStateReport
if err = report.Parse(str); err != nil {
return
}
if d.State.ServiceState != Opt(report) {
d.State.ServiceState = Opt(report)
d.updated <- struct{}{}
}
case Reports.SimState:
var report simStateReport
if err = report.Parse(str); err != nil {
return
}
if d.State.SimState != Opt(report) {
d.State.SimState = Opt(report)
d.updated <- struct{}{}
}
case Reports.BootHandshake:
var token bootHandshakeReport
if err = token.Parse(str); err != nil {
return
}
if err = d.Commands.BOOT(uint64(token)); err != nil {
return
}
case Reports.Stin:
// ignore. what is this btw?
default:
switch FinalResults.Resolve(str) {
case FinalResults.Noop, FinalResults.NotSupported, FinalResults.Timeout:
// ignore
default:
return errors.New("at: unknown report: " + str)
}
}
return nil
}
// Open is used to open serial ports of the device. This should be used first.
// The method returns error if open was not succeed, i.e. if device is absent.
func (d *Device) Open() (err error) {
if d.cmdPort, err = os.OpenFile(d.CommandPort, os.O_RDWR, 0); err != nil {
return
}
if d.NotifyPort != "" && d.NotifyPort != d.CommandPort {
if d.notifyPort, err = os.OpenFile(d.NotifyPort, os.O_RDWR, 0); err != nil {
d.cmdPort.Close()
return
}
}
return
}
// Init checks whether device is opened, initializes event channels
// and runs init procedure defined within the supplied DeviceProfile.
func (d *Device) Init(profile DeviceProfile) error {
if err := d.sanityCheck(false); err != nil {
return err
}
d.active = true
d.closed = make(chan struct{})
d.incomingCallerIDs = make(chan *calls.CallerID, 100)
d.messages = make(chan *sms.Message, 100)
d.ussd = make(chan Ussd, 100)
d.updated = make(chan struct{}, 100)
d.Commands = profile
return profile.Init(d)
}
// Close closes all the event channels and also closes
// both command and notification modem's ports. This function may block
// until the device will reply or the reply timeout timer will fire.
//
// Close is a no-op if already closed.
func (d *Device) Close() (err error) {
if d.active {
d.active = false
close(d.closed)
}
if d.cmdPort != nil {
err = d.cmdPort.Close()
}
if d.notifyPort != nil {
if err2 := d.notifyPort.Close(); err2 != nil {
err = err2
}
}
return
}
// SendUSSD sends an USSD request, the encoding and other parameters are default.
func (d *Device) SendUSSD(req string) (err error) {
err = d.Commands.CUSD(UssdResultReporting.Enable, pdu.Encode7Bit(req), Encodings.Gsm7Bit)
return
}
// SendSMS sends an SMS message with given text to the given address,
// the encoding and other parameters are default.
func (d *Device) SendSMS(text string, address sms.PhoneNumber) (err error) {
msg := sms.Message{
Text: text,
Type: sms.MessageTypes.Submit,
Encoding: sms.Encodings.Gsm7Bit,
Address: address,
VPFormat: sms.ValidityPeriodFormats.Relative,
VP: sms.ValidityPeriod(24 * time.Hour * 4),
}
if !pdu.Is7BitEncodable(text) {
msg.Encoding = sms.Encodings.UCS2
}
n, octets, err := msg.PDU()
if err != nil {
return
}
_, err = d.Commands.CMGS(n, octets)
return
}