-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
504 lines (460 loc) · 13.9 KB
/
commands.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
package at
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/boonsanti/at/pdu"
"github.com/boonsanti/at/sms"
"github.com/boonsanti/at/util"
log "github.com/sirupsen/logrus"
)
// DeviceProfile hides the device-specific implementation
// and provides a set of methods that can be used on a device.
// Init should be called first.
type DeviceProfile interface {
Init(*Device) error
CMGS(length int, octets []byte) (err error)
CUSD(reporting Opt, octets []byte, enc Encoding) (err error)
CMGR(index uint64) (octets []byte, err error)
CMGD(index uint64, option Opt) (err error)
CMGL(flag Opt) (octets map[uint64][]byte, err error)
CMGF(text bool) (err error)
CNMI(mode, mt, bm, ds, bfr int) (err error)
CPMS(mem1 StringOpt, mem2 StringOpt, mem3 StringOpt) (err error)
BOOT(token uint64) (err error)
SYSCFG(roaming, cellular bool) (err error)
SYSINFO() (info *SystemInfoReport, err error)
COPS(auto bool, text bool) (err error)
OperatorName() (str string, err error)
ModelName() (str string, err error)
IMEI() (str string, err error)
QURCCFG(urcPortValue string) (err error)
CSQ() (value int, err error)
}
// DeviceE173 returns an instance of DeviceProfile implementation for Huawei E173,
// it's also the default one.
func DeviceE173() DeviceProfile {
return &DefaultProfile{}
}
// DefaultProfile is a reference implementation that could be embedded
// in any other custom implementation of the DeviceProfile interface.
type DefaultProfile struct {
dev *Device
}
// Init invokes a set of methods that will make the initial setup of the modem.
func (p *DefaultProfile) Init(d *Device) (err error) {
p.dev = d
p.dev.Send(NoopCmd) // kinda flush
if err = p.QURCCFG("uart1"); err != nil {
log.Error(err)
return errors.New("at init: unable to switch URC to uart1")
}
if err = p.COPS(true, true); err != nil {
return errors.New("at init: unable to adjust the format of operator's name")
}
var info *SystemInfoReport
info = &SystemInfoReport{}
// if info, err = p.SYSINFO(); err != nil {
// return errors.New("at init: unable to read system info")
// }
p.dev.State = &DeviceState{
ServiceState: info.ServiceState,
ServiceDomain: info.ServiceDomain,
RoamingState: info.RoamingState,
SystemMode: info.SystemMode,
SystemSubmode: info.SystemSubmode,
SimState: info.SimState,
}
if p.dev.State.OperatorName, err = p.OperatorName(); err != nil {
log.Error(err)
return errors.New("at init: unable to read operator's name")
}
if p.dev.State.SignalStrength, err = p.CSQ(); err != nil {
log.Error(err)
return errors.New("at init: unable to read signal strength")
}
if p.dev.State.ModelName, err = p.ModelName(); err != nil {
log.Error(err)
return errors.New("at init: unable to read modem's model name")
}
if p.dev.State.IMEI, err = p.IMEI(); err != nil {
log.Error(err)
return errors.New("at init: unable to read modem's IMEI code")
}
if err = p.CMGF(false); err != nil {
log.Error(err)
return errors.New("at init: unable to switch message format to PDU")
}
if err = p.CPMS(MemoryTypes.NvRAM, MemoryTypes.NvRAM, MemoryTypes.NvRAM); err != nil {
log.Error(err)
return errors.New("at init: unable to set messages storage")
}
if err = p.CNMI(1, 1, 0, 0, 0); err != nil {
log.Error(err)
return errors.New("at init: unable to turn on message notifications")
}
var octets map[uint64][]byte
if octets, err = p.CMGL(MessageFlags.Any); err != nil {
log.Error(err)
return errors.New("at init: unable to check message inbox")
}
for n, oct := range octets {
var msg sms.Message
if _, err := msg.ReadFrom(oct); err != nil {
log.Error(err)
return errors.New("at init: error while parsing message inbox")
}
if err := p.CMGD(n, DeleteOptions.Index); err != nil {
log.Error(err)
return errors.New("at init: error while cleaning message inbox")
}
d.messages <- &msg
}
return nil
}
type signalStrengthReport uint64
func (s *signalStrengthReport) Parse(str string) (err error) {
var u uint64
u, err = strconv.ParseUint(str, 10, 8)
*s = signalStrengthReport(u)
return
}
type modeReport struct {
Mode Opt
Submode Opt
}
func (m *modeReport) Parse(str string) (err error) {
fields := strings.Split(str, ",")
if len(fields) < 2 {
return ErrParseReport
}
var mode, submode uint64
if mode, err = strconv.ParseUint(fields[0], 10, 8); err != nil {
return
}
if submode, err = strconv.ParseUint(fields[0], 10, 8); err != nil {
return
}
m.Mode = SystemModes.Resolve(int(mode))
m.Submode = SystemSubmodes.Resolve(int(submode))
return
}
type simStateReport Opt
func (s *simStateReport) Parse(str string) (err error) {
var o uint64
if o, err = strconv.ParseUint(str, 10, 8); err != nil {
return
}
*s = simStateReport(SimStates.Resolve(int(o)))
return
}
type serviceStateReport Opt
func (s *serviceStateReport) Parse(str string) (err error) {
var o uint64
if o, err = strconv.ParseUint(str, 10, 8); err != nil {
return
}
*s = serviceStateReport(ServiceStates.Resolve(int(o)))
return
}
type bootHandshakeReport uint64
func (b *bootHandshakeReport) Parse(str string) (err error) {
fields := strings.Split(str, ",")
if len(fields) < 1 {
return ErrParseReport
}
var key uint64
if key, err = strconv.ParseUint(fields[0], 10, 8); err != nil {
return
}
*b = bootHandshakeReport(key)
return
}
// Ussd type represents the USSD query string
type Ussd string
// Encode converts the query string into bytes according to the
// specified encoding.
func (u *Ussd) Encode(enc Encoding) ([]byte, error) {
switch enc {
case Encodings.Gsm7Bit:
return pdu.Encode7Bit(u.String()), nil
case Encodings.UCS2:
return pdu.EncodeUcs2(u.String()), nil
default:
return nil, ErrUnknownEncoding
}
}
func (u *Ussd) String() string {
return string(*u)
}
type ussdReport struct {
N uint64
Octets []byte
Enc Encoding
}
func (r *ussdReport) Parse(str string) (err error) {
fields := strings.Split(str, ",")
if len(fields) < 3 {
return ErrParseReport
}
if r.N, err = strconv.ParseUint(fields[0], 10, 8); err != nil {
return
}
if r.Octets, err = util.Bytes(strings.Trim(fields[1], `"`)); err != nil {
return
}
var e uint64
if e, err = strconv.ParseUint(fields[2], 10, 8); err != nil {
return
}
r.Enc = Encoding(e)
return
}
// CUSD sends AT+CUSD with the given parameters to the device. This will invoke an USSD request.
func (p *DefaultProfile) CUSD(reporting Opt, octets []byte, enc Encoding) (err error) {
req := fmt.Sprintf(`AT+CUSD=%d,%02X,%d`, reporting.ID, octets, enc)
_, err = p.dev.Send(req)
return
}
type messageReport struct {
Memory StringOpt
Index uint64
}
func (m *messageReport) Parse(str string) (err error) {
fields := strings.Split(str, ",")
if len(fields) < 2 {
return ErrParseReport
}
if m.Memory = MemoryTypes.Resolve(strings.Trim(fields[0], `"`)); m.Memory == UnknownStringOpt {
return ErrParseReport
}
if m.Index, err = strconv.ParseUint(fields[1], 10, 16); err != nil {
return
}
return
}
// CMGR sends AT+CMGR with the given index to the device and returns the message contents.
func (p *DefaultProfile) CMGR(index uint64) (octets []byte, err error) {
req := fmt.Sprintf(`AT+CMGR=%d`, index)
reply, err := p.dev.Send(req)
if err != nil {
return
}
lines := strings.Split(reply, "\n")
if len(lines) < 2 {
return nil, ErrParseReport
}
octets, err = util.Bytes(lines[1])
return
}
// CMGD sends AT+CMGD with the given index and option to the device. Option defines the mode
// in which messages will be deleted. The default mode is to delete by index.
func (p *DefaultProfile) CMGD(index uint64, option Opt) (err error) {
req := fmt.Sprintf(`AT+CMGD=%d,%d`, index, option.ID)
_, err = p.dev.Send(req)
return
}
// CPMS sends AT+CPMS with the given options to the device. It allows to select
// the storage type for different kinds of messages and message notifications.
func (p *DefaultProfile) CPMS(mem1 StringOpt, mem2 StringOpt, mem3 StringOpt) (err error) {
req := fmt.Sprintf(`AT+CPMS="%s","%s","%s"`, mem1.ID, mem2.ID, mem3.ID)
_, err = p.dev.Send(req)
return
}
// CNMI sends AT+CNMI with the given parameters to the device.
// It's used to adjust the settings of the new message arrival notifications.
func (p *DefaultProfile) CNMI(mode, mt, bm, ds, bfr int) (err error) {
req := fmt.Sprintf(`AT+CNMI=%d,%d,%d,%d,%d`, mode, mt, bm, ds, bfr)
_, err = p.dev.Send(req)
return
}
// CMGF sends AT+CMGF with the given value to the device. It toggles
// the mode of message handling betwen PDU and TEXT.
//
// Note, that the at package works only in PDU mode.
func (p *DefaultProfile) CMGF(text bool) (err error) {
var flag int
if text {
flag = 1
}
req := fmt.Sprintf(`AT+CMGF=%d`, flag)
_, err = p.dev.Send(req)
return
}
// CMGL sends AT+CMGL with the given filtering flag to the device and then parses
// the list of received messages that match ther filter. See MessageFlags for the
// list of supported filters.
func (p *DefaultProfile) CMGL(flag Opt) (octets map[uint64][]byte, err error) {
req := fmt.Sprintf(`AT+CMGL=%d`, flag.ID)
reply, err := p.dev.Send(req)
if err != nil {
return
}
lines := strings.Split(reply, "\n")
if len(lines) < 2 {
return
}
octets = make(map[uint64][]byte)
for i := 0; i < len(lines); i += 2 {
header := strings.TrimPrefix(lines[i], `+CMGL: `)
fields := strings.Split(header, ",")
if len(fields) < 4 {
return nil, ErrParseReport
}
n, err := strconv.ParseUint(fields[0], 10, 16)
if err != nil {
return nil, ErrParseReport
}
var oct []byte
if oct, err = util.Bytes(lines[i+1]); err != nil {
return nil, ErrParseReport
}
octets[n] = oct
}
return
}
// BOOT sends AT^BOOT with the given token to the device. This completes
// the handshaking procedure.
func (p *DefaultProfile) BOOT(token uint64) (err error) {
req := fmt.Sprintf(`AT^BOOT=%d,0`, token)
_, err = p.dev.Send(req)
return
}
// CMGS sends AT+CMGS with the given parameters to the device. This is used to send SMS
// using the given PDU data. Length is a number of TPDU bytes.
func (p *DefaultProfile) CMGS(length int, octets []byte) (err error) {
part1 := fmt.Sprintf("AT+CMGS=%d", length)
part2 := fmt.Sprintf("%02X", octets)
err = p.dev.sendInteractive(part1, part2, byte('>'))
return
}
// SYSCFG sends AT^SYSCFG with the given parameters to the device.
// The arguments of this command may vary, so the options are limited to switchng roaming and
// cellular mode on/off.
func (p *DefaultProfile) SYSCFG(roaming, cellular bool) (err error) {
var roam int
if roaming {
roam = 1
}
var cell int
if cellular {
cell = 2
} else {
cell = 1
}
req := fmt.Sprintf(`AT^SYSCFG=2,2,3FFFFFFF,%d,%d`, roam, cell)
_, err = p.dev.Send(req)
return
}
// SystemInfoReport represents the report from the AT^SYSINFO command.
type SystemInfoReport struct {
ServiceState Opt
ServiceDomain Opt
RoamingState Opt
SystemMode Opt
SystemSubmode Opt
SimState Opt
}
// Parse scans the AT^SYSINFO report into a non-nil SystemInfoReport struct.
func (s *SystemInfoReport) Parse(str string) (err error) {
fields := strings.Split(str, ",")
if len(fields) < 7 {
return ErrParseReport
}
fetch := func(str string, field *Opt, resolver func(id int) Opt) error {
if n, err := strconv.ParseUint(str, 10, 8); err != nil {
return err
} else if opt := resolver(int(n)); opt == UnknownOpt {
return errors.New("resolver: unknwon opt")
} else {
*field = opt
return nil
}
}
if err = fetch(fields[0], &s.ServiceState, ServiceStates.Resolve); err != nil {
return ErrParseReport
}
if err = fetch(fields[1], &s.ServiceDomain, ServiceDomains.Resolve); err != nil {
return ErrParseReport
}
if err = fetch(fields[2], &s.RoamingState, RoamingStates.Resolve); err != nil {
return ErrParseReport
}
if err = fetch(fields[3], &s.SystemMode, SystemModes.Resolve); err != nil {
return ErrParseReport
}
if err = fetch(fields[4], &s.SimState, SimStates.Resolve); err != nil {
return ErrParseReport
}
if err = fetch(fields[6], &s.SystemSubmode, SystemSubmodes.Resolve); err != nil {
return ErrParseReport
}
return
}
// SYSINFO sends AT^SYSINFO to the device and parses the output.
func (p *DefaultProfile) SYSINFO() (info *SystemInfoReport, err error) {
reply, err := p.dev.Send(`AT^SYSINFO`)
if err != nil {
return nil, err
}
info = new(SystemInfoReport)
err = info.Parse(strings.TrimPrefix(reply, `^SYSINFO:`))
return
}
// COPS sends AT+COPS to the device with parameters that define autosearch and
// the operator's name representation. The default represenation is numerical.
func (p *DefaultProfile) COPS(auto bool, text bool) (err error) {
var a, t int
if !auto {
a = 1
}
if !text {
t = 2
}
req := fmt.Sprintf(`AT+COPS=%d,%d`, a, t)
_, err = p.dev.Send(req)
return
}
// OperatorName sends AT+COPS? to the device and gets the operator's name.
func (p *DefaultProfile) OperatorName() (str string, err error) {
result, err := p.dev.Send(`AT+COPS?`)
fields := strings.Split(strings.TrimPrefix(result, `+COPS: `), ",")
if len(fields) < 4 {
err = ErrParseReport
return
}
str = strings.TrimLeft(strings.TrimRight(fields[2], `"`), `"`)
return
}
// ModelName sends AT+GMM to the device and gets the modem's model name.
func (p *DefaultProfile) ModelName() (str string, err error) {
str, err = p.dev.Send(`AT+GMM`)
return
}
// IMEI sends AT+GSN to the device and gets the modem's IMEI code.
func (p *DefaultProfile) IMEI() (str string, err error) {
str, err = p.dev.Send(`AT+GSN`)
return
}
// QUECTEL UC20G
// AT+QURCCFG Configure URC Indication Option.
// This command is used to configure the output port of URC.
// parameter ("usbat","usbmodem","uart1")
func (p *DefaultProfile) QURCCFG(urcPortValue string) (err error) {
req := fmt.Sprintf(`AT+QURCCFG="urcport","%s"`, urcPortValue)
_, err = p.dev.Send(req)
return
}
// QUECTEL UC20G
// AT+CSQ Signal Quality Report
func (p *DefaultProfile) CSQ() (value int, err error) {
result, err := p.dev.Send(`AT+CSQ`)
fields := strings.Split(strings.TrimPrefix(result, `+CSQ: `), ",")
if len(fields) < 2 {
err = ErrParseReport
return
}
value, err = strconv.Atoi(fields[0])
return
}