-
Notifications
You must be signed in to change notification settings - Fork 42
/
waitsms.go
107 lines (98 loc) · 2.44 KB
/
waitsms.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
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <warthog618@gmail.com>.
// waitsms waits for SMSs to be received by the modem, and dumps them to
// stdout.
//
// This provides an example of using indications, as well as a test that the
// library works with the modem.
//
// The modem device provided must support notifications or no SMSs will be seen.
// (the notification port is typically USB2, hence the default)
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/warthog618/modem/at"
"github.com/warthog618/modem/gsm"
"github.com/warthog618/modem/serial"
"github.com/warthog618/modem/trace"
)
var version = "undefined"
func main() {
dev := flag.String("d", "/dev/ttyUSB2", "path to modem device")
baud := flag.Int("b", 115200, "baud rate")
period := flag.Duration("p", 10*time.Minute, "period to wait")
timeout := flag.Duration("t", 400*time.Millisecond, "command timeout period")
verbose := flag.Bool("v", false, "log modem interactions")
hex := flag.Bool("x", false, "hex dump modem responses")
vsn := flag.Bool("version", false, "report version and exit")
flag.Parse()
if *vsn {
fmt.Printf("%s %s\n", os.Args[0], version)
os.Exit(0)
}
m, err := serial.New(serial.WithPort(*dev), serial.WithBaud(*baud))
if err != nil {
log.Println(err)
return
}
defer m.Close()
var mio io.ReadWriter = m
if *hex {
mio = trace.New(m, trace.WithReadFormat("r: %v"))
} else if *verbose {
mio = trace.New(m)
}
g := gsm.New(at.New(mio, at.WithTimeout(*timeout)))
err = g.Init()
if err != nil {
log.Println(err)
return
}
go pollSignalQuality(g, timeout)
err = g.StartMessageRx(
func(msg gsm.Message) {
log.Printf("%s: %s\n", msg.Number, msg.Message)
},
func(err error) {
log.Printf("err: %v\n", err)
})
if err != nil {
log.Println(err)
return
}
defer g.StopMessageRx()
for {
select {
case <-time.After(*period):
log.Println("exiting...")
return
case <-g.Closed():
log.Fatal("modem closed, exiting...")
}
}
}
// pollSignalQuality polls the modem to read signal quality every minute.
//
// This is run in parallel to SMS reception to demonstrate separate goroutines
// interacting with the modem.
func pollSignalQuality(g *gsm.GSM, timeout *time.Duration) {
for {
select {
case <-time.After(time.Minute):
i, err := g.Command("+CSQ")
if err != nil {
log.Println(err)
} else {
log.Printf("Signal quality: %v\n", i)
}
case <-g.Closed():
return
}
}
}