This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
gozw.go
561 lines (451 loc) · 14.3 KB
/
gozw.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
package gozw
import (
"context"
"encoding"
"encoding/json"
"fmt"
"time"
"github.com/gozwave/gozw/cc"
zwsec "github.com/gozwave/gozw/cc/security"
"github.com/gozwave/gozw/frame"
"github.com/gozwave/gozw/protocol"
"github.com/gozwave/gozw/security"
"github.com/gozwave/gozw/serialapi"
"github.com/gozwave/gozw/session"
"github.com/gozwave/gozw/transport"
"github.com/boltdb/bolt"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// MaxSecureInclusionDuration is the timeout for secure inclusion mode. If this
// timeout expires, secure inclusion will be canceled no matter how far the
// process has proceeded.
const MaxSecureInclusionDuration = time.Second * 60
// Maximum possible size (in bytes) of the plaintext payload to be sent when
// sending a secure frame, based on the SendData options. The smallest possible
// must be used based on the given option bitset (e.g. if using both no route
// and explore, the maximum size is 26).
const (
SecurePayloadMaxSizeExplore = 26 // in bytes
SecurePayloadMaxSizeAutoRoute = 28
SecurePayloadMaxSizeNoRoute = 34
)
type Client struct {
Controller Controller
serialAPI serialapi.ILayer
securityLayer security.ILayer
networkKey []byte
nodes map[byte]*Node
// REPLACE THIS WITH A GENERIC CALLBACK FUNCTION
// EventBus EventBus.Bus
EventCallback func(*Client, byte, cc.Command)
l *zap.Logger
db *bolt.DB
ctx context.Context
cancel context.CancelFunc
secureInclusionStep map[byte]chan error
}
func NewDefaultClient(dbName, serialPort string, baudRate int, networkKey []byte) (*Client, error) {
logger, err := NewLogger()
if err != nil {
return nil, errors.Wrap(err, "initialize logger")
}
client := Client{
Controller: Controller{},
networkKey: networkKey,
nodes: map[byte]*Node{},
EventCallback: DefaultEventCallback,
l: logger,
secureInclusionStep: map[byte]chan error{},
}
client.ctx, client.cancel = context.WithCancel(context.Background())
transport, err := transport.NewSerialPortTransport(serialPort, baudRate)
if err != nil {
return nil, errors.Wrap(err, "initializing transport")
}
frameLayer, err := frame.NewFrameLayer(client.ctx, transport, logger)
if err != nil {
return nil, errors.Wrap(err, "initialize frame layer")
}
sessionLayer := session.NewSessionLayer(client.ctx, frameLayer, logger)
client.serialAPI = serialapi.NewLayer(client.ctx, sessionLayer, logger)
client.securityLayer = security.NewLayer(client.networkKey, logger)
err = client.initDb(dbName)
if err != nil {
return nil, errors.Wrap(err, "initialize db")
}
go client.handleApplicationCommands()
go client.handleControllerUpdates()
err = client.initZWave()
if err != nil {
return nil, errors.Wrap(err, "initializing z-wave")
}
return &client, nil
}
func (c *Client) SetLogger(logger *zap.Logger) {
c.l = logger
}
func (c *Client) initDb(dbName string) (err error) {
c.db, err = bolt.Open(dbName, 0600, &bolt.Options{})
if err != nil {
return
}
c.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("nodes"))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte("controller"))
if err != nil {
return err
}
return nil
})
return err
}
// NewLogger builds a new logger.
func NewLogger() (*zap.Logger, error) {
rawJSON := []byte(`{
"level": "debug",
"encoding": "json",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
return nil, errors.Wrap(err, "unmarshal config")
}
logger, err := cfg.Build()
if err != nil {
return nil, errors.Wrap(err, "build logger")
}
return logger, nil
}
// Nodes will return all nodes
func (c *Client) Nodes() map[byte]*Node {
return c.nodes
}
// Node will retrieve a single node.
func (c *Client) Node(nodeID byte) (*Node, error) {
if node, ok := c.nodes[nodeID]; ok {
return node, nil
}
return nil, errors.New("Node not found")
}
func (c *Client) initZWave() error {
version, err := c.serialAPI.GetVersion()
if err != nil {
return err
}
c.Controller.APIVersion = version.Version
c.Controller.APILibraryType = version.GetLibraryTypeString()
c.Controller.HomeID, c.Controller.NodeID, err = c.serialAPI.MemoryGetID()
if err != nil {
return err
}
serialAPICapabilities, err := c.serialAPI.GetCapabilities()
if err != nil {
return err
}
c.Controller.ApplicationVersion = serialAPICapabilities.ApplicationVersion
c.Controller.ApplicationRevision = serialAPICapabilities.ApplicationRevision
c.Controller.SupportedFunctions = serialAPICapabilities.GetSupportedFunctions()
initData, err := c.serialAPI.GetInitAppData()
if err != nil {
return err
}
c.Controller.Version = initData.Version
c.Controller.APIType = initData.GetAPIType()
c.Controller.IsPrimaryController = initData.IsPrimaryController()
c.Controller.NodeList = initData.GetNodeIDs()
for _, nodeID := range c.Controller.NodeList {
node, err := NewNode(c, nodeID)
if err != nil {
spew.Dump(err)
continue
}
c.nodes[nodeID] = node
}
return nil
}
// Shutdown will stop the client.
func (c *Client) Shutdown() error {
c.cancel()
return nil
}
func (c *Client) AddNode() (*Node, error) {
newNodeInfo, err := c.serialAPI.AddNode()
if err != nil {
return nil, err
}
if newNodeInfo == nil {
return nil, errors.New("Adding node failed")
}
node, err := NewNode(c, newNodeInfo.Source)
if err != nil {
return nil, err
}
node.setFromAddNodeCallback(newNodeInfo)
c.nodes[node.NodeID] = node
if node.IsSecure() {
c.l.Debug("starting secure inclusion")
err = c.includeSecureNode(node)
if err != nil {
return nil, err
}
}
node.nextQueryStage()
select {
case <-node.queryStageVersionsComplete:
c.l.Info("node queries complete")
case <-time.After(time.Second * 30):
c.l.Warn("node query timeout", zap.String("node", fmt.Sprint(node.NodeID)))
}
node.AddAssociation(1, 1)
return node, nil
}
func (c *Client) RemoveNode() (byte, error) {
result, err := c.serialAPI.RemoveNode()
if err != nil {
return 0, err
}
if result == nil {
return 0, errors.New("Removing node failed")
}
return result.Source, nil
}
func (c *Client) RemoveFailedNode(nodeID byte) (ok bool, err error) {
return c.serialAPI.RemoveFailedNode(nodeID)
}
func (c *Client) handleApplicationCommands() {
for {
select {
case cmd := <-c.serialAPI.ControllerCommands():
switch cc.CommandClassID(cmd.CommandData[0]) {
case cc.Security:
c.interceptSecurityCommandClass(cmd)
default:
if node, err := c.Node(cmd.SrcNodeID); err == nil {
go node.receiveApplicationCommand(cmd)
} else {
c.l.Warn("Received command for unknown node", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
}
}
case <-c.ctx.Done():
c.l.Info("stopping application commands handler")
return
}
}
}
// SetEventCallback will set the event callback for any events received
func (c *Client) SetEventCallback(callback func(c *Client, nodeID byte, e cc.Command)) {
c.EventCallback = callback
}
// DefaultEventCallback is the default callback for handling events.
func DefaultEventCallback(c *Client, nodeID byte, e cc.Command) {
c.l.Info("event received", zap.Any("event", e), zap.Int("nodeID", int(nodeID)))
}
func (c *Client) handleControllerUpdates() {
for {
select {
case update := <-c.serialAPI.ControllerUpdates():
switch update.Status {
case protocol.UpdateStateNodeInfoReceived,
protocol.UpdateStateNodeInfoReqFailed:
if node, ok := c.nodes[update.NodeID]; ok {
node.receiveControllerUpdate(update)
} else {
c.l.Debug("controller update:", zap.String("data", spew.Sdump(update)))
}
default:
c.l.Debug("controller update:", zap.String("data", spew.Sdump(update)))
}
case <-c.ctx.Done():
c.l.Info("stopping controller updates handler")
return
}
}
}
func (c *Client) SendData(dstNode byte, payload encoding.BinaryMarshaler) error {
marshaled, err := payload.MarshalBinary()
if err != nil {
return err
}
_, err = c.serialAPI.SendData(dstNode, marshaled)
return err
}
// SendDataSecure encapsulates payload in a security encapsulation command and
// sends it to the destination node.
func (c *Client) SendDataSecure(dstNode byte, message encoding.BinaryMarshaler) error {
// This function wraps the private sendDataSecure because no external packages
// should ever call this while in inclusion mode (and doing so would be incorrect)
return c.sendDataSecure(dstNode, message, false)
}
func (c *Client) requestNonceForNode(dstNode byte) (security.Nonce, error) {
err := c.SendData(dstNode, &zwsec.NonceGet{})
if err != nil {
return nil, err
}
return c.securityLayer.WaitForExternalNonce(dstNode)
}
func (c *Client) getOrRequestNonceForNode(dstNode byte) (nonce security.Nonce, err error) {
if nonce, err = c.securityLayer.GetExternalNonce(dstNode); err == nil {
return nonce, nil
}
for i := 0; i < 3; i++ {
nonce, err = c.requestNonceForNode(dstNode)
if err == nil {
break
}
c.l.Error("error: get nonce attempt failed", zap.Int("attempt", i))
time.Sleep(50 * time.Millisecond)
}
return nonce, err
}
func (c *Client) sendDataSecure(dstNode byte, message encoding.BinaryMarshaler, inclusionMode bool) error {
// Previously, this function would just split and prepare the payload based on
// whether it should be split after figuring out whether to segment. For now,
// we're just going to assume that we will never have to worry about segmenting.
// It wasn't too hard to implement before, but since I couldn't find a real payload
// big enough, it wasn't possible to verify the implementation, so I didn't port
// it while refactoring (for simplicity's sake).
payload, err := message.MarshalBinary()
if err != nil {
return err
}
// Get a nonce from the other node
receiverNonce, err := c.getOrRequestNonceForNode(dstNode)
if err != nil {
return err
}
senderNonce, err := c.securityLayer.GenerateInternalNonce()
if err != nil {
return err
}
var securityByte byte
// var securityByte byte = sequenceCounter & SecuritySequenceCounterMask
// if sequenced {
// securityByte |= SecuritySequenceSequencedFlag
//
// if isSecondFrame {
// securityByte |= SecuritySequenceSecondFrameFlag
// }
// }
securePayload := append([]byte{securityByte}, payload...)
encapsulatedMessage, err := c.securityLayer.EncapsulateMessage(
1,
dstNode,
zwsec.CommandMessageEncapsulation, // @todo CC should be determined by sequencing
senderNonce,
receiverNonce,
securePayload,
inclusionMode,
)
if err != nil {
c.l.Error("failed to encrypt message", zap.String("err", err.Error()), zap.String("node", fmt.Sprint(dstNode)))
return err
}
return c.SendData(dstNode, encapsulatedMessage)
}
func (c *Client) includeSecureNode(node *Node) error {
c.secureInclusionStep[node.NodeID] = make(chan error)
c.SendData(node.NodeID, &zwsec.SchemeGet{})
defer close(c.secureInclusionStep[node.NodeID])
defer delete(c.secureInclusionStep, node.NodeID)
c.l.Info("requesting security scheme")
select {
case err := <-c.secureInclusionStep[node.NodeID]:
if err != nil {
return err
}
case <-time.After(time.Second * 10):
return errors.New("Secure inclusion timeout")
}
c.l.Info("sending network key")
node.NetworkKeySent = true
c.sendDataSecure(
node.NodeID,
&zwsec.NetworkKeySet{NetworkKeyByte: c.networkKey},
true,
)
select {
case err := <-c.secureInclusionStep[node.NodeID]:
return err
case <-time.After(time.Second * 20):
return errors.New("Secure inclusion timeout")
}
}
func (c *Client) interceptSecurityCommandClass(cmd serialapi.ApplicationCommand) {
command, err := cc.Parse(1, cmd.CommandData)
if err != nil {
c.l.Error(err.Error())
return
}
switch command.(type) {
case *zwsec.MessageEncapsulation, *zwsec.MessageEncapsulationNonceGet:
c.l.Info("rx secure message", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
// @todo determine whether to bother with sequenced messages. According to
// openzwave, they didn't bother to implement it because they never ran across
// a situation where a frame was large enough that it needed to be sequenced.
// in any case, the following is the following is the process to follow with
// or without sequencing:
// 1. decrypt message
// 2. if it's the first half of a sequenced message, wait for the second half
// 2.5 if it's an EncapsulationGetNonce, then send a NonceReport back to the sender
// 3. if it's the second half of a sequenced message, reassemble the payloads
// 4. emit the decrypted (possibly recombined) message back
var decrypted []byte
node, err := c.Node(cmd.SrcNodeID)
if err != nil {
return
}
if !node.NetworkKeySent {
decrypted, err = c.securityLayer.DecryptMessage(cmd, true)
} else {
decrypted, err = c.securityLayer.DecryptMessage(cmd, false)
}
if err != nil {
return
}
c.l.Info("received encapsulated message", zap.String("data", spew.Sdump(decrypted)))
if decrypted[1] == byte(cc.Security) &&
decrypted[2] == byte(zwsec.CommandNetworkKeyVerify) {
c.l.Info("network key verify", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
if ch, ok := c.secureInclusionStep[cmd.SrcNodeID]; ok {
ch <- nil
}
return
}
if node, ok := c.nodes[cmd.SrcNodeID]; ok {
cmd.CommandData = decrypted[1:]
go node.receiveApplicationCommand(cmd)
} else {
c.l.Warn("received secure command for unknown node", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
}
case *zwsec.NonceGet:
c.l.Info("nonce get", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
nonce, err := c.securityLayer.GenerateInternalNonce()
if err != nil {
}
reply := &zwsec.NonceReport{NonceByte: nonce}
c.SendData(cmd.SrcNodeID, reply)
case *zwsec.NonceReport:
c.l.Info("nonce report", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
c.securityLayer.ReceiveNonce(cmd.SrcNodeID, *command.(*zwsec.NonceReport))
case *zwsec.SchemeReport:
c.l.Info("security scheme report", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
if ch, ok := c.secureInclusionStep[cmd.SrcNodeID]; ok {
ch <- nil
} else {
c.l.Warn("not in secure inclusion mode", zap.String("node", fmt.Sprint(cmd.SrcNodeID)))
}
default:
c.l.Warn("unexpected security command", zap.String("data", spew.Sdump(cmd)))
}
}