-
Notifications
You must be signed in to change notification settings - Fork 10
/
ZStackNwkMgrServer.go
210 lines (161 loc) · 5.26 KB
/
ZStackNwkMgrServer.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
package zigbee
import (
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gogo/protobuf/proto"
"github.com/ninjasphere/go-zigbee/nwkmgr"
)
type ZStackNwkMgr struct {
*ZStackServer
pendingResponses map[uint32]*pendingNwkMgrResponse
OnDeviceFound func(deviceInfo *nwkmgr.NwkDeviceInfoT)
OnNetworkReady func()
}
type zStackNwkMgrCommand interface {
proto.Message
GetCmdId() nwkmgr.NwkMgrCmdIdT
}
type pendingNwkMgrResponse struct {
response zStackNwkMgrCommand
finished chan error
}
// SendAsyncCommand sends a command that requires an async response from the device, using ZCL SequenceNumber
func (s *ZStackNwkMgr) SendAsyncCommand(request zStackNwkMgrCommand, response zStackNwkMgrCommand, timeout time.Duration) error {
confirmation := &nwkmgr.NwkZigbeeGenericCnf{}
// spew.Dump("sending", request)
err := s.SendCommand(request, confirmation)
if err != nil {
return err
}
//spew.Dump(confirmation)
if confirmation.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Invalid confirmation status: %s", confirmation.Status.String())
}
return s.waitForSequenceResponse(*confirmation.SequenceNumber, response, timeout)
}
func (s *ZStackNwkMgr) waitForSequenceResponse(sequenceNumber uint32, response zStackNwkMgrCommand, timeoutDuration time.Duration) error {
// We accept uint32 as thats what comes back from protobuf
log.Debugf("Waiting for sequence %d", sequenceNumber)
_, exists := s.pendingResponses[sequenceNumber]
if exists {
s.pendingResponses[sequenceNumber].finished <- fmt.Errorf("Another command with the same sequence id (%d) has been sent.", sequenceNumber)
}
pending := &pendingNwkMgrResponse{
response: response,
finished: make(chan error),
}
s.pendingResponses[sequenceNumber] = pending
timeout := make(chan bool, 1)
go func() {
time.Sleep(timeoutDuration)
timeout <- true
}()
var err error
select {
case error := <-pending.finished:
err = error
case <-timeout:
err = fmt.Errorf("The request timed out after %s", timeoutDuration)
}
s.pendingResponses[sequenceNumber] = nil
return err
}
// SendCommand sends a protobuf Message to the Z-Stack server, and waits for the response
func (s *ZStackNwkMgr) SendCommand(request zStackNwkMgrCommand, response zStackNwkMgrCommand) error {
return s.sendCommand(
&zStackCommand{
message: request,
commandID: uint8(request.GetCmdId()),
},
&zStackCommand{
message: response,
commandID: uint8(response.GetCmdId()),
},
)
}
func (d *ZStackNwkMgr) FetchDeviceList() error {
deviceListResponse := &nwkmgr.NwkGetDeviceListCnf{}
err := d.SendCommand(&nwkmgr.NwkGetDeviceListReq{}, deviceListResponse)
if err != nil {
log.Fatalf("Failed to get device list: %s", err)
}
log.Debugf("Found %d device(s): ", len(deviceListResponse.DeviceList))
for _, deviceInfo := range deviceListResponse.DeviceList {
d.OnDeviceFound(deviceInfo)
}
return nil
}
func (s *ZStackNwkMgr) Reset(hard bool) error {
log.Infof("Resetting. Hard: %t", hard)
mode := nwkmgr.NwkResetModeT_SOFT_RESET.Enum()
if hard {
mode = nwkmgr.NwkResetModeT_HARD_RESET.Enum()
}
response := nwkmgr.NwkZigbeeSystemResetCnf{}
err := s.SendCommand(&nwkmgr.NwkZigbeeSystemResetReq{
Mode: mode,
}, &response)
if err != nil {
return err
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Invalid confirmation status: %s", response.Status.String())
}
return nil
}
func (s *ZStackNwkMgr) onIncoming(commandID uint8, bytes *[]byte) {
//bytes := <-s.Incoming
log.Debugf("nwkmgr: Got nwkmgr message (%s) % X", nwkmgr.NwkMgrCmdIdT_name[int32(commandID)], bytes)
switch commandID {
case uint8(nwkmgr.NwkMgrCmdIdT_NWK_ZIGBEE_DEVICE_IND):
device := &nwkmgr.NwkZigbeeDeviceInd{}
err := proto.Unmarshal(*bytes, device)
if err != nil {
log.Errorf("nwkmgr: Failed to read device announcement : %s, %v", err, *bytes)
return
}
s.OnDeviceFound(device.DeviceInfo)
case uint8(nwkmgr.NwkMgrCmdIdT_NWK_ZIGBEE_SYSTEM_RESET_CNF):
confirmation := &nwkmgr.NwkZigbeeSystemResetCnf{}
err := proto.Unmarshal(*bytes, confirmation)
if err != nil {
log.Errorf("nwkmgr: Failed to read reset confirmation : %s, %v", err, *bytes)
return
}
log.Infof("nwkmgr: Reset Confirmed")
if log.IsDebugEnabled() {
spew.Dump(confirmation)
}
case uint8(nwkmgr.NwkMgrCmdIdT_NWK_ZIGBEE_NWK_READY_IND):
log.Infof("nwkmgr: Network Ready")
if s.OnNetworkReady != nil {
s.OnNetworkReady()
}
default:
log.Debugf("nwkmgr: Unknown incoming network manager message: %d!", commandID)
}
}
func ConnectToNwkMgrServer(hostname string, port int) (*ZStackNwkMgr, error) {
server, err := connectToServer("NwkMgr", uint8(nwkmgr.ZStackNwkMgrSysIdT_RPC_SYS_PB_NWK_MGR), hostname, port)
if err != nil {
return nil, err
}
nwkmgr := &ZStackNwkMgr{
pendingResponses: make(map[uint32]*pendingNwkMgrResponse),
ZStackServer: server,
OnDeviceFound: func(deviceInfo *nwkmgr.NwkDeviceInfoT) {
log.Warningf("nwkmgr: Warning: Device found. You must add an onDeviceFound handler!")
if log.IsDebugEnabled() {
spew.Dump(deviceInfo)
}
},
OnNetworkReady: func() {
log.Warningf("nwkmgr: Warning: Network ready. You must add an OnNetworkReady handler!")
},
}
server.onIncoming = func(commandID uint8, bytes *[]byte) {
nwkmgr.onIncoming(commandID, bytes)
}
return nwkmgr, nil
}