-
Notifications
You must be signed in to change notification settings - Fork 8
/
adapter_initialise.go
374 lines (312 loc) · 11.1 KB
/
adapter_initialise.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
package zstack
import (
"context"
"errors"
"fmt"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/retry"
"github.com/shimmeringbee/zigbee"
"golang.org/x/sync/semaphore"
"reflect"
)
func (z *ZStack) Initialise(pctx context.Context, nc zigbee.NetworkConfiguration) error {
z.NetworkProperties.PANID = nc.PANID
z.NetworkProperties.ExtendedPANID = nc.ExtendedPANID
z.NetworkProperties.NetworkKey = nc.NetworkKey
z.NetworkProperties.Channel = nc.Channel
ctx, segmentEnd := z.logger.Segment(pctx, "Adapter Initialise.")
defer segmentEnd()
z.logger.LogInfo(ctx, "Restarting adapter.")
version, err := z.waitForAdapterReset(ctx)
if err != nil {
return err
}
if version.IsV3() {
z.sem = semaphore.NewWeighted(16)
} else {
z.sem = semaphore.NewWeighted(2)
}
z.logger.LogInfo(ctx, "Verifying existing network configuration.")
if valid, err := z.verifyAdapterNetworkConfig(ctx, version); err != nil {
return err
} else if !valid {
z.logger.LogWarn(ctx, "Adapter network configuration is invalid, resetting adapter.")
if err := z.wipeAdapter(ctx); err != nil {
return err
}
z.logger.LogInfo(ctx, "Setting adapter to coordinator.")
if err := z.makeCoordinator(ctx); err != nil {
return err
}
z.logger.LogInfo(ctx, "Configuring adapter.")
if err := z.configureNetwork(ctx, version); err != nil {
return err
}
}
z.logger.LogInfo(ctx, "Starting Zigbee stack.")
if err := z.startZigbeeStack(ctx, version); err != nil {
return err
}
z.logger.LogInfo(ctx, "Fetching adapter IEEE and Network addresses.")
if err := z.retrieveAdapterAddresses(ctx); err != nil {
return err
}
z.logger.LogInfo(ctx, "Enforcing denial of network joins.")
if err := z.DenyJoin(ctx); err != nil {
return err
}
z.startNetworkManager()
z.startMessageReceiver()
return nil
}
func (z *ZStack) waitForAdapterReset(ctx context.Context) (Version, error) {
retVersion := Version{}
err := retry.Retry(ctx, DefaultZStackTimeout, 18, func(invokeCtx context.Context) error {
version, err := z.resetAdapter(invokeCtx, Soft)
retVersion = version
return err
})
return retVersion, err
}
func (z *ZStack) verifyAdapterNetworkConfig(ctx context.Context, version Version) (bool, error) {
configToVerify := []interface{}{
&ZCDNVLogicalType{LogicalType: zigbee.Coordinator},
&ZCDNVPANID{PANID: z.NetworkProperties.PANID},
&ZCDNVExtPANID{ExtendedPANID: z.NetworkProperties.ExtendedPANID},
&ZCDNVChanList{Channels: channelToBits(z.NetworkProperties.Channel)},
}
for _, expectedConfig := range configToVerify {
configType := reflect.TypeOf(expectedConfig).Elem()
actualConfig := reflect.New(configType).Interface()
if err := z.readNVRAM(ctx, actualConfig); err != nil {
return false, err
}
if !reflect.DeepEqual(expectedConfig, actualConfig) {
return false, nil
}
}
return true, nil
}
func (z *ZStack) wipeAdapter(ctx context.Context) error {
return retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
return z.writeNVRAM(invokeCtx, ZCDNVStartUpOption{StartOption: 0x03})
},
func(invokeCtx context.Context) error {
_, err := z.resetAdapter(invokeCtx, Soft)
return err
},
})
}
func (z *ZStack) makeCoordinator(ctx context.Context) error {
return retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
return z.writeNVRAM(invokeCtx, ZCDNVLogicalType{LogicalType: zigbee.Coordinator})
},
func(invokeCtx context.Context) error {
_, err := z.resetAdapter(invokeCtx, Soft)
return err
},
})
}
func (z *ZStack) configureNetwork(ctx context.Context, version Version) error {
channelBits := channelToBits(z.NetworkProperties.Channel)
if err := retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Enabling preconfigured keys.")
return z.writeNVRAM(invokeCtx, ZCDNVPreCfgKeysEnable{Enabled: 1})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configuring network key.")
return z.writeNVRAM(invokeCtx, ZCDNVPreCfgKey{NetworkKey: z.NetworkProperties.NetworkKey})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Enable ZDO callbacks.")
return z.writeNVRAM(invokeCtx, ZCDNVZDODirectCB{Enabled: 1})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configuring network channel.")
return z.writeNVRAM(invokeCtx, ZCDNVChanList{Channels: channelBits})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configuring network PANID.")
return z.writeNVRAM(invokeCtx, ZCDNVPANID{PANID: z.NetworkProperties.PANID})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configuring network extended PANID.")
return z.writeNVRAM(invokeCtx, ZCDNVExtPANID{ExtendedPANID: z.NetworkProperties.ExtendedPANID})
},
}); err != nil {
return err
}
if !version.IsV3() {
z.logger.LogDebug(ctx, "Adapter Initialisation: Not Version 3.X.X.")
/* Less than Z-Stack 3.X.X requires the Trust Centre key to be loaded. */
return retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Enable default trust center.")
return z.writeNVRAM(invokeCtx, ZCDNVUseDefaultTCLK{Enabled: 1})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configuring ZLL trust center key.")
return z.writeNVRAM(invokeCtx, ZCDNVTCLKTableStart{
Address: zigbee.IEEEAddress(0xffffffffffffffff),
NetworkKey: zigbee.TCLinkKey,
TXFrameCounter: 0,
RXFrameCounter: 0,
})
},
})
} else {
/* Z-Stack 3.X.X requires configuration of Base Device Behaviour. */
z.logger.LogDebug(ctx, "Adapter Initialisation: Version 3.X.X.")
if err := retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configure primary channel.")
return z.requestResponder.RequestResponse(ctx, APPCNFBDBSetChannelRequest{IsPrimary: true, Channel: channelBits}, &APPCNFBDBSetChannelRequestReply{})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Configure secondary channels.")
return z.requestResponder.RequestResponse(ctx, APPCNFBDBSetChannelRequest{IsPrimary: false, Channel: [4]byte{}}, &APPCNFBDBSetChannelRequestReply{})
},
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Request commissioning.")
return z.requestResponder.RequestResponse(ctx, APPCNFBDBStartCommissioningRequest{Mode: 0x04}, &APPCNFBDBStartCommissioningRequestReply{})
},
}); err != nil {
return err
}
z.logger.LogDebug(ctx, "Adapter Initialisation: Waiting for coordinator to start.")
if err := z.waitForCoordinatorStart(ctx); err != nil {
return err
}
return retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
z.logger.LogDebug(ctx, "Adapter Initialisation: Waiting for commissioning to complete.")
return z.requestResponder.RequestResponse(ctx, APPCNFBDBStartCommissioningRequest{Mode: 0x02}, &APPCNFBDBStartCommissioningRequestReply{})
},
})
}
}
func (z *ZStack) retrieveAdapterAddresses(ctx context.Context) error {
return retryFunctions(ctx, []func(context.Context) error{
func(invokeCtx context.Context) error {
if address, err := z.GetAdapterIEEEAddress(ctx); err != nil {
return err
} else {
z.NetworkProperties.IEEEAddress = address
return nil
}
},
func(ctx context.Context) error {
if address, err := z.GetAdapterNetworkAddress(ctx); err != nil {
return err
} else {
z.NetworkProperties.NetworkAddress = address
return nil
}
},
})
}
func (z *ZStack) startZigbeeStack(ctx context.Context, version Version) error {
if err := retry.Retry(ctx, DefaultZStackTimeout, DefaultZStackRetries, func(invokeCtx context.Context) error {
return z.requestResponder.RequestResponse(invokeCtx, ZDOStartUpFromAppRequest{StartDelay: 100}, &ZDOStartUpFromAppRequestReply{})
}); err != nil {
return err
}
if version.IsV3() {
return nil
}
ch := make(chan bool, 1)
defer close(ch)
err, cancel := z.subscriber.Subscribe(&ZDOStateChangeInd{}, func(v interface{}) {
stateChange := v.(*ZDOStateChangeInd)
z.logger.LogDebug(ctx, "Waiting for zigbee stack to start, state change.", logwrap.Datum("State", stateChange.State), logwrap.Datum("DesiredState", DeviceZBCoordinator))
if stateChange.State == DeviceZBCoordinator {
ch <- true
}
})
defer cancel()
if err != nil {
return err
}
select {
case <-ch:
return nil
case <-ctx.Done():
return errors.New("context expired while waiting for adapter start up")
}
}
func (z *ZStack) waitForCoordinatorStart(ctx context.Context) error {
ch := make(chan bool, 1)
defer close(ch)
err, cancel := z.subscriber.Subscribe(&ZDOStateChangeInd{}, func(v interface{}) {
stateChange := v.(*ZDOStateChangeInd)
z.logger.LogDebug(ctx, "Waiting for coordinator start, state change.", logwrap.Datum("State", stateChange.State), logwrap.Datum("DesiredState", DeviceZBCoordinator))
if stateChange.State == DeviceZBCoordinator {
ch <- true
}
})
defer cancel()
if err != nil {
return err
}
select {
case <-ch:
return nil
case <-ctx.Done():
return errors.New("context expired while waiting for adapter start up")
}
}
func retryFunctions(ctx context.Context, funcs []func(context.Context) error) error {
for _, f := range funcs {
if err := retry.Retry(ctx, DefaultZStackTimeout, DefaultZStackRetries, f); err != nil {
return fmt.Errorf("failed during configuration and initialisation: %w", err)
}
}
return nil
}
func channelToBits(channel uint8) [4]byte {
channelBits := 1 << channel
channelBytes := [4]byte{}
channelBytes[0] = byte((channelBits >> 0) & 0xff)
channelBytes[1] = byte((channelBits >> 8) & 0xff)
channelBytes[2] = byte((channelBits >> 16) & 0xff)
channelBytes[3] = byte((channelBits >> 24) & 0xff)
return channelBytes
}
type ZBStartStatus uint8
const (
ZBSuccess ZBStartStatus = 0x00
ZBInit ZBStartStatus = 0x22
)
type ZDOState uint8
const (
DeviceCoordinatorStarting ZDOState = 0x08
DeviceZBCoordinator ZDOState = 0x09
)
type ZDOStateChangeInd struct {
State ZDOState
}
const ZDOStateChangeIndID uint8 = 0xc0
type APPCNFBDBStartCommissioningRequest struct {
Mode uint8
}
const APPCNFBDBStartCommissioningRequestID uint8 = 0x05
type APPCNFBDBStartCommissioningRequestReply GenericZStackStatus
const APPCNFBDBStartCommissioningRequestReplyID uint8 = 0x05
type APPCNFBDBSetChannelRequest struct {
IsPrimary bool `bcwidth:"8"`
Channel [4]byte
}
const APPCNFBDBSetChannelRequestID uint8 = 0x08
type APPCNFBDBSetChannelRequestReply GenericZStackStatus
const APPCNFBDBSetChannelRequestReplyID uint8 = 0x08
type ZDOStartUpFromAppRequest struct {
StartDelay uint16
}
const ZDOStartUpFromAppRequestId uint8 = 0x40
type ZDOStartUpFromAppRequestReply struct {
Status uint8
}
const ZDOStartUpFromAppRequestReplyID uint8 = 0x40