-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwyzesense.go
576 lines (454 loc) · 11.2 KB
/
wyzesense.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package gosense
import (
"context"
"encoding/binary"
"encoding/hex"
"os"
"time"
log "github.com/sirupsen/logrus"
)
// WyzeSense defines a WyzeSense USB device.
type WyzeSense struct {
Name string
fd *os.File
handlers map[cmdType]func(*WyzeSense, packet, context.CancelFunc) error
alarmch chan (Alarm)
sensorch chan (SenseSensor)
ctx context.Context
currentCmdCancel context.CancelFunc
currentResponse *packet
}
// NewWyzeSense creates a new WyzeSense object.
func NewWyzeSense(devicename string,
alarmch chan (Alarm),
sensorch chan (SenseSensor)) (*WyzeSense, error) {
log.Infof("NewWyzeSense: trying to open device: [%s]", devicename)
s := &WyzeSense{
handlers: make(map[cmdType]func(*WyzeSense, packet, context.CancelFunc) error),
ctx: context.Background(),
alarmch: alarmch,
sensorch: sensorch,
}
fs, err := os.OpenFile(devicename, os.O_RDWR, 0777)
if err != nil {
log.Errorf("Failed opening device: {%s}", err)
return s, err
}
s.fd = fs
// Register the async cmd handlers
s.handlers[notifySyncTime] = onSyncTime
s.handlers[notifySensorAlarm] = onSensorAlarm
s.handlers[notifyEventLog] = onEventLog
//--------
// start processing
go s.readerLoop()
return s, nil
}
func (s *WyzeSense) Start() error {
err := s.inquiry()
if err != nil {
log.Debugf("Inquiry failed: %s", err)
return err
}
err = s.getMac()
if err != nil {
log.Debugf("GetMac failed: %s", err)
return err
}
ver, err := s.getVersion()
if err != nil {
log.Debugf("GetVersion failed: %s", err)
return err
}
log.Debugf("GetVersion: ver: [%s]", ver)
sensors, err := s.GetSensorList()
if err != nil {
log.Debugf("GetSensorList failed: %s", err)
return err
}
log.Debugf("GetSensorList: sensors: [%v]", sensors)
err = s.finishAuth()
if err != nil {
log.Debugf("FinishAuth failed: %s", err)
return err
}
log.Debugf("FinishAuth: success!")
return nil
}
func (s *WyzeSense) Close() {
if s.fd != nil {
s.fd.Close()
}
}
func findMagic(buf []byte) int {
next := false
for i, n := range buf {
if 0x55 == n {
next = true
continue
}
if next && 0xaa == n {
return i - 1
}
next = false
}
return -1
}
func (s *WyzeSense) readerLoop() {
var err error
buf := make([]byte, 0x4)
buf = nil
//--------
// set up the async handlers
for {
i := findMagic(buf)
if i < 0 {
buf, err = s.readRawHid()
if err != nil {
continue
}
continue
}
buf = buf[i:]
l := len(buf)
log.Debugf("Trying to parse: %d bytes: [ %s ]",
l, hex.EncodeToString(buf))
//--------
// Handle the response
p, err := parsePacket(buf)
if err != nil {
log.Error("Error parsing buffer as packet")
buf = buf[l:]
continue
}
if cmdType(p.cmd>>8) == asyncCommand && p.cmd != asyncAck {
log.Debugf(" ==> Sending ACK packet for cmd %04x", p.cmd)
s.sendPacket(AsyncAck(p.cmd))
}
buf = buf[p.lenght():]
if handler, ok := s.handlers[p.cmd]; ok {
err = handler(s, p, s.currentCmdCancel)
}
}
}
func (s *WyzeSense) readRawHid() ([]byte, error) {
buf := make([]byte, 0x80)
_, err := s.fd.Read(buf)
if err != nil {
log.Errorf("Failed reading from device: ", err)
return nil, err
}
l := buf[0]
buf = buf[1 : l+1]
log.Debugf("readRawHid: %d bytes: [ %s ]",
l, hex.EncodeToString(buf))
return buf, nil
}
func (s *WyzeSense) sendPacket(p packet) error {
log.Debugf("====> sending: [%s]", p.toString())
return p.send(s.fd)
}
func (s *WyzeSense) doCommand(cmd packet,
handler func(*WyzeSense, packet, context.CancelFunc) error) (*packet, error) {
var err error
s.handlers[cmd.cmd+1] = handler
ctxTimeout, cancelFunc := context.WithTimeout(s.ctx, 5*time.Second)
s.currentCmdCancel = cancelFunc
// Write the command
err = cmd.send(s.fd)
if err != nil {
log.Errorf("Failed sending to device: %s", err.Error())
return nil, err
}
// Wait for the response(s)
select {
case <-ctxTimeout.Done():
break
}
cancelFunc()
delete(s.handlers, cmd.cmd+1)
return s.currentResponse, err
}
func (s *WyzeSense) doSimpleCommand(cmd packet) (*packet, error) {
var err error
p, err := s.doCommand(cmd, func(s *WyzeSense, cmd packet, cancelFun context.CancelFunc) error {
s.currentResponse = &cmd
cancelFun()
return nil
})
return p, err
}
// inquiry sends the Inquiry cmd
func (s *WyzeSense) inquiry() error {
var err error
log.Debug("Start Inquiry...")
p, err := s.doSimpleCommand(inquiry())
if err != nil {
return err
}
if p == nil {
log.Debug("Inquiry timed out")
}
ret := p.payload[0]
log.Debugf("Inquiry returned: %d", ret)
return nil
}
// getMac sends the getmac cmd
func (s *WyzeSense) getMac() error {
var err error
log.Debug("Start GetMac...")
p, err := s.doSimpleCommand(getMac())
if err != nil {
return err
}
if p == nil {
log.Debug("GetMac timed out")
}
log.Debugf("GetMac returned: %s", (p.payload))
return nil
}
// getVersion gets the dongle version
func (s *WyzeSense) getVersion() ([]byte, error) {
var err error
log.Debug("Start GetVersion...")
p, err := s.doSimpleCommand(getVersion())
if err != nil {
return nil, err
}
if p == nil {
log.Debug("GetVersion timed out")
}
log.Debugf("GetVersion returned: %s", (p.payload))
return p.payload, nil
}
// GetSensorList enumerates the sensors registered with the dongle
func (s *WyzeSense) GetSensorList() ([]string, error) {
var err error
log.Debug("Start GetSensorList...")
p, err := s.doSimpleCommand(getSensorCount())
if err != nil {
return nil, err
}
if p == nil {
log.Debug("GetSensorList timed out")
}
if len(p.payload) != 1 {
panic("Payload is not 1!")
}
count := p.payload[0]
log.Debugf("GetSensorCount returned: %d", count)
index := byte(0)
sensors := make([]string, count)
if count > 0 {
_, err := s.doCommand(getSensorList(count), func(s *WyzeSense, pkt packet, cancelFun context.CancelFunc) error {
if len(pkt.payload) != 8 {
panic("Payload is not 8!")
}
log.Debugf("Sensor %d/%d, MAC: [%s]", index+1, count, pkt.payload)
sensors[index] = string(pkt.payload)
index = index + 1
if index == count {
cancelFun()
}
return nil
})
if err != nil {
return nil, err
}
}
return sensors, nil
}
// ScanSensor scans for sensor and adds it to the dongle
func (s *WyzeSense) ScanSensor() (string, error) {
var err error
log.Debug("Start ScanSensor...")
p, err := s.doSimpleCommand(enableScan(1))
if err != nil {
return "", err
}
if p == nil {
log.Debug("EnableScan (start) timed out")
}
//--------
// Wait for a new sensor to come
var MAC string
var version byte
ctxTimeout, cancelFuncScan := context.WithTimeout(s.ctx, 30*time.Second)
onNewSensor := func(s *WyzeSense, pkt packet, cancelFun context.CancelFunc) error {
if len(pkt.payload) != 11 {
panic("Payload is not 11!")
}
MAC = string(pkt.payload[1:9])
sensor := SenseSensor{
MAC: string(MAC),
SensorType: SensorType(pkt.payload[9]),
Present: true,
}
version = pkt.payload[10]
log.Debugf("New sensor: MAC: [%s], type: %d, version: %d", sensor.MAC, sensor.SensorType, version)
cancelFuncScan()
if s.sensorch != nil {
s.sensorch <- sensor
}
return nil
}
// Register the async handler for scan results
s.handlers[notifySensorScan] = onNewSensor
// Wait for the response(s)
select {
case <-ctxTimeout.Done():
break
}
cancelFuncScan()
delete(s.handlers, notifySensorScan)
if len(MAC) > 0 {
p, err = s.doSimpleCommand(getSensorR1(string(MAC), "Ok5HPNQ4lf77u754"))
if err != nil {
return "", err
}
if p == nil {
log.Debug("GetSensorR1 (stop) timed out")
}
}
// Stop scanning
p, err = s.doSimpleCommand(enableScan(0))
if err != nil {
return "", err
}
if p == nil {
log.Debug("EnableScan (stop) timed out")
}
if len(MAC) > 0 {
// Verify sensor
p, err = s.doSimpleCommand(verifySensor(string(MAC)))
if err != nil {
return "", err
}
if p == nil {
log.Debug("VerifySensor timed out")
}
}
return string(MAC), nil
}
// DeleteSensor removes a sensor from the dongle
func (s *WyzeSense) DeleteSensor(MAC string) error {
var err error
log.Debug("Start DeleteSensor...")
p, err := s.doSimpleCommand(deleteSensor(MAC))
if err != nil {
return err
}
if p == nil {
log.Debug("DeleteSensor timed out")
}
if len(p.payload) != 9 {
panic("Payload is not 9!")
}
ackMac := string(p.payload[:7])
ackCode := p.payload[8]
log.Debugf("DeleteSensor returned: [%s], code: %d", ackMac, ackCode)
if ackCode == 0xff {
sensor := SenseSensor{
MAC: string(MAC),
Present: false,
}
s.sensorch <- sensor
}
return nil
}
// VerifySensor validates the sensor is responsive
func (s *WyzeSense) VerifySensor(MAC string) error {
var err error
log.Debug("Start VerifySensor...")
p, err := s.doSimpleCommand(verifySensor(MAC))
if err != nil {
return err
}
if p == nil {
log.Debug("VerifySensor timed out")
}
return nil
}
// FinishAuth finish the dongle initialization
func (s *WyzeSense) finishAuth() error {
var err error
log.Debug("Start FinishAuth...")
p, err := s.doSimpleCommand(finishAuth())
if err != nil {
return err
}
if p == nil {
log.Debug("FinishAuth timed out")
}
return nil
}
func onSyncTime(s *WyzeSense, p packet, finished context.CancelFunc) error {
log.Debugf(" onSyncTime ==> Sending SyncTypeAck packet for cmd %04x", p.cmd)
s.sendPacket(syncTimeAck(p.cmd))
return nil
}
func onSensorAlarm(s *WyzeSense, p packet, finished context.CancelFunc) error {
/*
8 bytes: big endian timestamp in milliseconds
1 byte: sensor flags?, 0xa2 for both contact and motion sensor
8 bytes: sensor MAC
1 byte: sensor type
1 byte: signal strength
1 byte: remaining battery percentage
1 byte: unknown, referred to as p1329
1 byte: unknown
1 byte: binary sensor state (1: opened/motion detected, 0: closed/no motion detected)
2 bytes: big endian counter sensor state change, reset when power is restored (battery changed)
1 byte: signal strength, presumably something like absolute value of RSSI (lower is better)
*/
pay := p.payload
ts := binary.BigEndian.Uint64(pay) / 1000
event := Event{
SensorFlags: pay[8],
MAC: string(pay[9 : 9+8]),
SensorType: SensorType(pay[17]),
Timestamp: time.Unix(int64(ts), 0),
}
pay = pay[18:]
if event.SensorFlags == 0xa2 {
alarm := Alarm{
Event: event,
SignalStrength: pay[7],
Battery: pay[1],
State: pay[4],
}
pay = pay[8:]
log.Infof("ALARM: time=%s, mac: %s, flags: %x, type: %x, battery: %d, signal: %d, state: %d, data=%s",
alarm.Timestamp.Format(time.RFC822Z),
alarm.MAC,
alarm.SensorFlags,
byte(alarm.SensorType),
alarm.Battery,
alarm.SignalStrength,
alarm.State,
hex.EncodeToString(pay))
if s.alarmch != nil {
s.alarmch <- alarm
}
} else {
log.Infof("EVENT: time=%s, mac: %s, flags: %x, type: %x, data=%s ==> UNKNOWN",
event.Timestamp.Format(time.RFC822Z),
event.MAC,
event.SensorFlags,
byte(event.SensorType),
hex.EncodeToString(pay))
}
return nil
}
func onEventLog(s *WyzeSense, p packet, finished context.CancelFunc) error {
if len(p.payload) < 9 {
panic("payload < 9!")
}
pay := p.payload
ts := binary.BigEndian.Uint64(pay) / 1000
//l := pay[8]
t := time.Unix(int64(ts), 0)
msg := pay[9:]
log.Infof("LOG: time=%s, data=%s", t.Format(time.RFC822Z), (msg))
return nil
}