-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat_test.go
128 lines (116 loc) · 2.76 KB
/
at_test.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
package at
import (
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/boonsanti/at/pdu"
)
// Needs to be changed for each particular configuration.
const (
CommandPortPath = "/dev/tty.HUAWEIMobile-Modem"
NotifyPortPath = "/dev/tty.HUAWEIMobile-Pcui"
TestPhoneAddress = "+79269965690"
BalanceUSSD = "*100#"
)
var dev *Device
// openDevice opens the hardcoded device paths for reading and writing,
// also inits this device with the default device profile.
func openDevice() (err error) {
dev = &Device{
CommandPort: CommandPortPath,
NotifyPort: NotifyPortPath,
}
if err = dev.Open(); err != nil {
return
}
if err = dev.Init(DeviceE173()); err != nil {
return
}
return
}
// waitDevice monitors available channels for the given period of time, or
// until the fetch process exits.
func waitDevice(n int) {
t := time.NewTimer(time.Second * time.Duration(n))
defer t.Stop()
go dev.Watch()
for {
select {
case <-t.C:
return
case <-dev.Closed():
return
case msg, ok := <-dev.IncomingSms():
if ok {
log.Printf("Incoming sms from %s: %s", msg.Address, msg.Text)
}
case ussd, ok := <-dev.UssdReply():
if ok {
log.Printf("USSD result: %s", ussd)
}
case <-dev.StateUpdate():
log.Printf("Signal strength: %d (%s/%s)", dev.State.SignalStrength, dev.State.OperatorName,
dev.State.SystemSubmode.Description)
}
}
}
// Test the device lifecycle.
func TestOpenInitWaitClose(t *testing.T) {
err := openDevice()
if !assert.NoError(t, err) {
return
}
defer dev.Close()
waitDevice(1)
}
// Test the "AT" command.
func TestNoop(t *testing.T) {
err := openDevice()
if !assert.NoError(t, err) {
return
}
defer dev.Close()
_, err = dev.Send(NoopCmd)
assert.NoError(t, err)
}
// Test USSD queries, the result will be reported asynchroniously.
func TestUssd(t *testing.T) {
err := openDevice()
if !assert.NoError(t, err) {
return
}
defer func() {
dev.Close()
}()
err = dev.Commands.CUSD(UssdResultReporting.Enable, pdu.Encode7Bit(BalanceUSSD), Encodings.Gsm7Bit)
if !assert.NoError(t, err) {
return
}
waitDevice(10)
}
// This costs money (but works)
// func TestSmsSend(t *testing.T) {
// err := openDevice()
// if !assert.NoError(t, err) {
// return
// }
// defer dev.Close()
// msg := sms.Message{
// Text: "Lazy fox jumps over ленивая собака",
// Type: sms.MessageTypes.Submit,
// Encoding: sms.Encodings.UCS2,
// Address: sms.PhoneNumber(TestPhoneAddress),
// VPFormat: sms.ValidityPeriodFormats.Relative,
// VP: sms.ValidityPeriod(24 * time.Hour * 4),
// }
// n, octets, err := msg.PDU()
// if !assert.NoError(t, err) {
// return
// }
// err = dev.Commands.CMGS(n, octets)
// if !assert.NoError(t, err) {
// return
// }
// waitDevice(10)
// }