-
Notifications
You must be signed in to change notification settings - Fork 8
/
nvram.go
184 lines (134 loc) · 3.88 KB
/
nvram.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
package zstack
import (
"context"
"errors"
"fmt"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/zigbee"
"reflect"
)
var NVRAMUnsuccessful = errors.New("nvram operation unsuccessful")
var NVRAMUnrecognised = errors.New("nvram structure unrecognised")
func (z *ZStack) writeNVRAM(ctx context.Context, v interface{}) error {
configId, found := nvramStructToID[reflect.TypeOf(v)]
if !found {
return NVRAMUnrecognised
}
configValue, err := bytecodec.Marshal(v)
if err != nil {
return err
}
writeRequest := SysOSALNVWrite{
NVItemID: configId,
Offset: 0,
Value: configValue,
}
writeResponse := SysOSALNVWriteReply{}
if err := z.requestResponder.RequestResponse(ctx, writeRequest, &writeResponse); err != nil {
return err
}
if writeResponse.Status != ZSuccess {
return fmt.Errorf("%w: write: configId = %v, status = %v", NVRAMUnsuccessful, configId, writeResponse.Status)
}
return nil
}
func (z *ZStack) readNVRAM(ctx context.Context, v interface{}) error {
vType := reflect.TypeOf(v)
if vType.Kind() == reflect.Ptr {
vType = vType.Elem()
}
configId, found := nvramStructToID[vType]
if !found {
return NVRAMUnrecognised
}
readRequest := SysOSALNVRead{
NVItemID: configId,
Offset: 0,
}
readResponse := SysOSALNVReadReply{}
if err := z.requestResponder.RequestResponse(ctx, readRequest, &readResponse); err != nil {
return err
}
if readResponse.Status != ZSuccess {
return fmt.Errorf("%w: read: configId = %v, status = %v", NVRAMUnsuccessful, configId, readResponse.Status)
}
return bytecodec.Unmarshal(readResponse.Value, v)
}
type SysOSALNVWrite struct {
NVItemID uint16
Offset uint8
Value []byte `bcsliceprefix:"8"`
}
const SysOSALNVWriteID uint8 = 0x09
type SysOSALNVWriteReply GenericZStackStatus
const SysOSALNVWriteReplyID uint8 = 0x09
type SysOSALNVRead struct {
NVItemID uint16
Offset uint8
}
const SysOSALNVReadID uint8 = 0x08
type SysOSALNVReadReply struct {
Status ZStackStatus
Value []byte `bcsliceprefix:"8"`
}
const SysOSALNVReadReplyID uint8 = 0x08
var nvramStructToID = map[reflect.Type]uint16{
reflect.TypeOf(ZCDNVStartUpOption{}): ZCDNVStartUpOptionID,
reflect.TypeOf(ZCDNVLogicalType{}): ZCDNVLogicalTypeID,
reflect.TypeOf(ZCDNVSecurityMode{}): ZCDNVSecurityModeID,
reflect.TypeOf(ZCDNVPreCfgKeysEnable{}): ZCDNVPreCfgKeysEnableID,
reflect.TypeOf(ZCDNVPreCfgKey{}): ZCDNVPreCfgKeyID,
reflect.TypeOf(ZCDNVZDODirectCB{}): ZCDNVZDODirectCBID,
reflect.TypeOf(ZCDNVChanList{}): ZCDNVChanListID,
reflect.TypeOf(ZCDNVPANID{}): ZCDNVPANIDID,
reflect.TypeOf(ZCDNVExtPANID{}): ZCDNVExtPANIDID,
reflect.TypeOf(ZCDNVUseDefaultTCLK{}): ZCDNVUseDefaultTCLKID,
reflect.TypeOf(ZCDNVTCLKTableStart{}): ZCDNVTCLKTableStartID,
}
const ZCDNVStartUpOptionID uint16 = 0x0003
type ZCDNVStartUpOption struct {
StartOption uint8
}
const ZCDNVLogicalTypeID uint16 = 0x0087
type ZCDNVLogicalType struct {
LogicalType zigbee.LogicalType
}
const ZCDNVSecurityModeID uint16 = 0x0064
type ZCDNVSecurityMode struct {
Enabled uint8
}
const ZCDNVPreCfgKeysEnableID uint16 = 0x0063
type ZCDNVPreCfgKeysEnable struct {
Enabled uint8
}
const ZCDNVPreCfgKeyID uint16 = 0x0062
type ZCDNVPreCfgKey struct {
NetworkKey zigbee.NetworkKey
}
const ZCDNVZDODirectCBID uint16 = 0x008f
type ZCDNVZDODirectCB struct {
Enabled uint8
}
const ZCDNVChanListID uint16 = 0x0084
type ZCDNVChanList struct {
Channels [4]byte
}
const ZCDNVPANIDID uint16 = 0x0083
type ZCDNVPANID struct {
PANID zigbee.PANID
}
const ZCDNVExtPANIDID uint16 = 0x002d
type ZCDNVExtPANID struct {
ExtendedPANID zigbee.ExtendedPANID
}
const ZCDNVUseDefaultTCLKID uint16 = 0x006d
type ZCDNVUseDefaultTCLK struct {
Enabled uint8
}
const ZCDNVTCLKTableStartID uint16 = 0x0101
type ZCDNVTCLKTableStart struct {
Address zigbee.IEEEAddress
NetworkKey zigbee.NetworkKey
TXFrameCounter uint32
RXFrameCounter uint32
}