-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallerid.go
396 lines (365 loc) · 8.57 KB
/
callerid.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
package main
import (
"flag"
"fmt"
"github.com/brian-armstrong/gpio"
"github.com/tarm/serial"
"github.com/yosssi/gmq/mqtt"
"github.com/yosssi/gmq/mqtt/client"
"log"
"os"
"os/signal"
"strings"
"time"
)
const ringPin = 17
const rly1Pin = 22
const rly2Pin = 24
const rly3Pin = 25
const rly4Pin = 26
type Cinfo struct {
Name string
Number string
Time string
}
func checksum_valid(msg_data []byte) bool {
msg_len := msg_data[1]
var sum byte
var j byte
for j = 0; j < msg_len+3; j++ {
sum = sum + msg_data[j]
}
if (sum) == 0 {
return true
} else {
return false
}
}
func parse_MDMF(msg_data []byte, callinfo *Cinfo, verbose bool) {
var idx int
var bleft byte
var time string
var id string
var name string
var dn_reason string
var name_reason string
bleft = byte(len(msg_data))
for bleft > 2 {
dtype := msg_data[idx]
dlen := msg_data[idx+1]
bleft = bleft - 2
idx = idx + 2
switch dtype {
case 0x01:
/* Time mmddHHMM*/
time = string(msg_data[idx : idx+int(dlen)])
callinfo.Time = time
if verbose {
fmt.Printf("Time: %s\n", time)
}
case 0x02:
/* ID */
id = string(msg_data[idx : idx+int(dlen)])
callinfo.Number = id
if verbose {
fmt.Printf("Number: %s\n", id)
}
case 0x03:
/* Reserved for Dialable DN */
if verbose {
fmt.Printf("parsed reserved value %02d\n", dtype)
}
case 0x04:
/* Reason for absense of DN */
/* O = blocked */
dn_reason = string(msg_data[idx : idx+int(dlen)])
callinfo.Number = dn_reason
if verbose {
if strings.Compare(dn_reason, string("O")) != 0 {
fmt.Printf("dn Reason: %s\n", dn_reason)
} else {
fmt.Printf("Number Blocked\n")
}
}
case 0x05:
/* Reserved for Redirection */
if verbose {
fmt.Printf("parsed reserved value %02d\n", dtype)
}
case 0x06:
/* Call Qualifier */
if verbose {
fmt.Printf("parsed call qualifier value %02d\n", dtype)
}
case 0x07:
/* Name */
name = string(msg_data[idx : idx+int(dlen)])
callinfo.Name = name
if verbose {
fmt.Printf("Name: %s\n", name)
}
case 0x08:
/* Reason for absence of Name */
/* P = private */
name_reason = string(msg_data[idx : idx+int(dlen)])
callinfo.Name = name_reason
if verbose {
fmt.Printf("Name Reason: %s\n", name_reason)
}
case 0x0B:
/* Message Waiting */
if verbose {
fmt.Printf("parsed Message Waiting\n")
}
default:
if verbose {
fmt.Printf("parsed unrecognized value %02d\n", dtype)
}
}
bleft = bleft - dlen
idx = idx + int(dlen)
}
if verbose {
fmt.Printf("========================\n")
}
}
func main() {
var ipaddress_string string
var serialport_string string
var verbose = flag.Bool("v", false, "Enable verbose output")
flag.StringVar(&ipaddress_string, "ip", "172.31.0.51", "ipv4 address of the mqtt server")
flag.StringVar(&serialport_string, "port", "/dev/ttyAMA0", "path for the serial port device")
flag.Parse()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill)
cli := client.New(&client.Options{
ErrorHandler: func(err error) {
fmt.Println(err)
},
})
defer cli.Terminate()
ipstr := ipaddress_string + ":1883"
if *verbose {
fmt.Printf("MQTT server addr: %s\n", ipstr)
}
// Connect to the MQTT Server.
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: ipstr,
ClientID: []byte("callerid-client"),
})
if err != nil {
panic(err)
}
if *verbose {
fmt.Printf("Serial device path: %s\n", serialport_string)
}
c := &serial.Config{Name: serialport_string, Baud: 1200}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
defer s.Close()
go func() {
/* Listen to the serial port and parse caller ID data */
const IDLE = 0
const SYNC = 1
const READING_LENGTH = 2
const READING_DATA = 3
const READING_CHECKSUM = 4
const SYNC_NEED = 16
const SYNC_HOLD = 16
var callinfo Cinfo
var inp byte
var num_sync byte
var sync_holding byte
var msg_type byte
var msg_len byte
var working_len byte
buf := make([]byte, 128)
msg_data := make([]byte, 300)
state := IDLE
num_sync = 0
data_idx := 0
for {
n, err := s.Read(buf)
if err != nil {
log.Fatal(err)
}
idx := 0
for n > 0 {
/* process the byte depending on state */
switch state {
case IDLE:
inp = buf[idx]
if inp == 'U' {
num_sync = num_sync + 1
if num_sync > SYNC_NEED {
state = SYNC
sync_holding = SYNC_HOLD
num_sync = 0
}
} else {
num_sync = 0
}
case SYNC:
msg_type = buf[idx]
if msg_type == 0x80 { /* Our BGW210 router/VOIP interface always sends type 0x80 */
msg_data[data_idx] = buf[idx]
data_idx = data_idx + 1
state = READING_LENGTH
} else {
sync_holding = sync_holding - 1
if sync_holding == 0 {
state = IDLE
}
}
case READING_LENGTH:
msg_len = buf[idx]
if msg_len > 0 {
working_len = msg_len
msg_data[data_idx] = buf[idx]
data_idx = data_idx + 1
state = READING_DATA
} else {
state = IDLE
data_idx = 0
}
case READING_DATA:
msg_data[data_idx] = buf[idx]
data_idx = data_idx + 1
working_len = working_len - 1
if working_len == 0 {
state = READING_CHECKSUM
}
case READING_CHECKSUM:
msg_data[data_idx] = buf[idx]
data_idx = data_idx + 1
if checksum_valid(msg_data) {
parse_MDMF(msg_data[2:msg_len+2], &callinfo, *verbose)
textinfo := fmt.Sprintf("name:%s, time:%s, number:%s", callinfo.Name, callinfo.Time, callinfo.Number)
err = cli.Publish(&client.PublishOptions{
QoS: mqtt.QoS1,
TopicName: []byte("home-assistant/phone/callerid"),
Message: []byte(textinfo),
})
if err != nil {
log.Fatal(err)
}
} else {
if *verbose {
fmt.Printf("CSUM NOT valid\n")
}
}
state = IDLE
data_idx = 0
default:
if *verbose {
fmt.Printf("Invalid State, back to IDLE\n")
}
state = IDLE
data_idx = 0
}
n = n - 1
idx = idx + 1
}
}
}()
go func() {
/* Monitor the Ring indicator GPIO pin and publish to the MQTT topic
* Ring indicator is active low on ringPin
* U.S. ring cadence is 2 seconds on and 4 off
*
* We periodically get repeated indications that the Value is 1 even when it hasn't changed
*/
var ringing bool
var notify gpio.WatcherNotification
tmr := time.NewTimer(10)
tmr.Stop()
watcher := gpio.NewWatcher()
watcher.AddPin(ringPin)
defer watcher.Close()
ringing = false
for {
select {
case notify = <-watcher.Notification:
if notify.Pin == ringPin {
if notify.Value == 0 {
/* Ring Start */
if !ringing {
ringing = true
/* This is the beginning of a set of rings */
/* Send a topic notification that we're ringing */
textinfo := fmt.Sprintf("Yes")
err = cli.Publish(&client.PublishOptions{
QoS: mqtt.QoS1,
TopicName: []byte("home-assistant/phone/ringing"),
Message: []byte(textinfo),
})
if err != nil {
log.Fatal(err)
}
}
tmr = time.NewTimer(7 * time.Second)
}
}
case <-tmr.C:
ringing = false
// Send a topic notification that we're NOT ringing
textinfo := fmt.Sprintf("No")
err = cli.Publish(&client.PublishOptions{
QoS: mqtt.QoS1,
TopicName: []byte("home-assistant/phone/ringing"),
Message: []byte(textinfo),
})
if err != nil {
log.Fatal(err)
}
}
}
}()
rly1 := gpio.NewOutput(rly1Pin, true)
rly2 := gpio.NewOutput(rly2Pin, true)
gpio.NewOutput(rly3Pin, true)
gpio.NewOutput(rly4Pin, true)
/* Subscribe to the control topic and control the relay */
err = cli.Subscribe(&client.SubscribeOptions{
SubReqs: []*client.SubReq{
&client.SubReq{
TopicFilter: []byte("home-assistant/phone/mode"),
QoS: mqtt.QoS0,
// Define the processing of the message handler.
Handler: func(topicName, message []byte) {
if strings.Compare(string(message), "bypass") == 0 {
rly1.Low()
rly2.Low()
} else {
rly1.High()
rly2.High()
}
fmt.Println(string(topicName), string(message))
},
},
},
})
if err != nil {
panic(err)
}
for {
// Wait for receiving a signal.
<-sigc
// Disconnect the Network Connection.
if err := cli.Disconnect(); err != nil {
panic(err)
}
// Unsubscribe from topics.
err = cli.Unsubscribe(&client.UnsubscribeOptions{
TopicFilters: [][]byte{
[]byte("home-assistant/phone/mode"),
},
})
if err != nil {
panic(err)
}
}
}