-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbee_zha.h
365 lines (342 loc) · 9.96 KB
/
xbee_zha.h
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
#include "zha_functions.h"
XbeeZha zha;
void zbTxStatusResp(ZBTxStatusResponse &resp, uintptr_t)
{
if (resp.isSuccess())
{
Serial.println(F("TX OK"));
if (cmd_result != nullptr)
{
*cmd_result = 0x01;
}
}
else
{
Serial.println(F("TX FAIL"));
Serial.println(resp.getDeliveryStatus(), HEX);
if (cmd_result != nullptr)
{
*cmd_result = 0x00;
}
if (resp.getFrameId() == zha.cmd_frame_id)
{
if (zha.dev_status == DEV_ANN)
{
zha.sendDevAnnounce();
}
else
{
zha.sendActiveEpResp(zha.last_seq_id);
}
}
}
}
void otherResp(XBeeResponse &resp, uintptr_t)
{
Serial.println(F("Other Response: "));
}
void atCmdResp(AtCommandResponse &resp, uintptr_t)
{
if (resp.getStatus() == AT_OK)
{
if (resp.getCommand()[0] == assocCmd[0] &&
resp.getCommand()[1] == assocCmd[1])
{
// Association Status
if (resp.getValue()[0] != 0x00)
{
zha.getAssociation();
}
else
{
cur_step_cmp = 1;
}
}
else if (resp.getCommand()[0] == netCmd[0] &&
resp.getCommand()[1] == netCmd[1])
{
if (zha.netAddr != 0xFFFE)
{
memcpy(&zha.netAddr, resp.getValue(), 2);
zha.netAddr = SWAP_UINT16(zha.netAddr);
Serial.print(F("NWK: 0x"));
Serial.print(resp.getValue()[0], HEX);
Serial.println(resp.getValue()[1], HEX);
cur_step_cmp = 1;
}
}
else
{
Serial.println(F("Ukn Cmd"));
}
}
else
{
Serial.println(F("AT Fail"));
}
}
uint8_t getAttrCfgRecLen(uint8_t direction, uint8_t attr_data_type)
{
uint8_t reclen = 3; // 1 byte for direction, 2 bytes for attrid
if (direction == 0x01)
{
// Serial.println(F("ToSrv"));
reclen += 2; // 2 bytes for timeout
}
else
{
reclen += 5; // 1 bytes data type, 2 bytes for min rpt int, 2 bytes for max rpt int
// Serial.println(F("ToClnt"));
if (
(attr_data_type > 0x07 && attr_data_type < 0x20) ||
(attr_data_type > 0x2f && attr_data_type < 0x38) ||
(attr_data_type > 0xe2)
)
{
// skip it's discrete
// Serial.println(F("discrete"));
}
else if (attr_data_type > 0x1f && attr_data_type < 0x28)
{
reclen += (attr_data_type - 0x1f);
// Serial.println(F("uint"));
}
else if (attr_data_type > 0x27 && attr_data_type < 0x30)
{
reclen += (attr_data_type - 0x27);
// Serial.println(F("int"));
}
else if (attr_data_type == 0x38)
{
reclen += 2;
// Serial.println(F("semi"));
}
else if (attr_data_type == 0x39)
{
reclen += 4;
// Serial.println(F("single"));
}
else if (attr_data_type == 0x3a)
{
reclen += 8;
// Serial.println(F("double"));
}
else
{
reclen += 2;
}
}
return reclen;
}
void printDiagnostic(ZBExplicitRxResponse &erx)
{
Serial.print(F("ZDO: EP: "));
Serial.print(erx.getDstEndpoint());
Serial.print(F(", Clstr: "));
Serial.print(erx.getClusterId(), HEX);
Serial.print(F(" Cmd Id: "));
Serial.print(erx.getFrameData()[erx.getDataOffset() + 2], HEX);
Serial.print(F(" FrmCtl: "));
Serial.print(erx.getFrameData()[erx.getDataOffset()], BIN);
Serial.print(F(" Dir: "));
if ((erx.getFrameData()[erx.getDataOffset()] >> 3) & 1)
{
Serial.println(F("Clnt"));
}
else
{
Serial.println(F("Srv"));
}
zha.print_payload(erx.getFrameData(), erx.getFrameDataLength());
}
void zdoReceive(ZBExplicitRxResponse &erx, uintptr_t)
{
// Create a reply packet containing the same data
Serial.println(F("ZDO Cmd"));
if (erx.getDstEndpoint() == 0)
{
if (erx.getClusterId() == ACTIVE_EP_RQST)
{
// Have to match sequence number in response
cmd_result = nullptr;
zha.last_seq_id = erx.getFrameData()[erx.getDataOffset()];
zha.sendActiveEpResp(zha.last_seq_id);
}
else if (erx.getClusterId() == SIMPLE_DESC_RQST)
{
Serial.print("Simple Desc Rqst, Ep: ");
// Have to match sequence number in response
// Payload is EndPoint
// Can this just be regular ep?
uint8_t ep_msg = erx.getFrameData()[erx.getDataOffset() + 3];
Serial.println(ep_msg, HEX);
zha.sendSimpleDescRpt(ep_msg, erx.getFrameData()[erx.getDataOffset()]);
}
else if (erx.getClusterId() == NODE_DESC_RESP_CMD)
{
Serial.println(F("Node Desc Rsp"));
}
else if (erx.getClusterId() == IEEE_ADDR_RESP_CMD)
{
Serial.println(F("IEEE Rsp"));
}
else if (erx.getClusterId() == DEV_ANN_CMD)
{
Serial.println(F("R Derv An"));
}
else
{
zha.print_payload(erx.getFrameData() + erx.getDataOffset(), erx.getDataLength());
Serial.print(F("ZDO: EP: "));
Serial.print(erx.getDstEndpoint());
Serial.print(F(", Clstr: "));
Serial.print(erx.getClusterId(), HEX);
Serial.println(F(" Cmd Id: "));
Serial.println(F("Handle Me?"));
}
}
else if (erx.getRemoteAddress16() == 0) // Why did I care about this again?
{
printDiagnostic(erx);
zha.cmd_seq_id = erx.getFrameData()[erx.getDataOffset() + 1];
Serial.print(F("Cmd Seq: "));
Serial.println(zha.cmd_seq_id, HEX);
uint8_t cmd_id = erx.getFrameData()[erx.getDataOffset() + 2];
if (erx.getFrameData()[erx.getDataOffset()] & 0x03) // frame type
{
// Cluster command, pass to app
zha.zha_clstr_cb(erx);
}
else
{
// Handle global commands here
Serial.println(F("Glbl Cmd"));
// This needs redone to send as one response, similar to rpt.
Endpoint end_point = zha.GetEndpoint(erx.getDstEndpoint());
if (cmd_id == READ_ATTRIBUTES)
{
Serial.println(F("RD Attr Cmd"));
if (end_point.ClusterExists(erx.getClusterId()))
{
Cluster cluster = end_point.GetCluster(erx.getClusterId());
// Read attributes
uint8_t attr_count = 0;
uint16_t cur_attr_id;
for (uint8_t i = 3; i < (erx.getDataLength()); i += 2)
{
cur_attr_id = (erx.getData()[i + 1] << 8) |
(erx.getData()[i] & 0xff);
attr_count++;
}
uint16_t attr_ids[attr_count] = {};
uint8_t attrpos = 0;
for (uint8_t i = 3; i < (erx.getDataLength()); i += 2)
{
cur_attr_id = (erx.getData()[i + 1] << 8) |
(erx.getData()[i] & 0xff);
attr_ids[attrpos] = cur_attr_id;
attrpos++;
}
Serial.print(F("Attr: "));
for(uint8_t j = 0; j<attr_count; j++){
Serial.print(attr_ids[j], HEX);
Serial.print(F(", "));
}
Serial.println();
zha.sendAttributeRespMult(&cluster, attr_ids, attr_count, end_point.id, 1);
}
else
{
Serial.println("Clstr Err");
}
}
else if (cmd_id == WRITE_ATTRIBUTES)
{
zha.zha_write_attr_cb(erx);
}
else if (cmd_id == DEF_RESP)
{
// Default Response
Serial.println(F("Def Rsp"));
}
else if (cmd_id == READ_RPT_CFG)
{
Serial.println(F("Rd Rpt Cfg"));
uint8_t len_data = erx.getDataLength() - 3;
uint16_t attr_rqst[len_data / 2];
for (uint8_t i = erx.getDataOffset() + 3; i < (len_data + erx.getDataOffset() + 3); i += 2)
{
attr_rqst[i / 2] = (erx.getFrameData()[i + 1] << 8) |
(erx.getFrameData()[i] & 0xff);
Serial.print(F("Clstr Rd Att: "));
Serial.println(attr_rqst[i / 2], HEX);
}
}
else if (cmd_id == CFG_RPT)
{
Serial.println(F("Cfg Rpt Cmd"));
// This works but not using, so commented out to save space
uint16_t cur_attr_id;
uint8_t attr_data_type;
uint8_t direction;
/*
uint16_t min_rpt_int;
uint16_t max_rpt_int;
*/
uint8_t bad_att_count = 0;
uint8_t pktpos = 3;
while (pktpos < erx.getDataLength())
{
direction = erx.getData()[pktpos];
cur_attr_id = (erx.getData()[pktpos + 2] << 8) |
(erx.getData()[pktpos + 1] & 0xff);
attr_data_type = erx.getData()[pktpos + 3];
uint8_t rec_length = getAttrCfgRecLen(direction, attr_data_type);
pktpos += rec_length;
if (!zha.GetEndpoint(erx.getDstEndpoint()).GetCluster(erx.getClusterId()).AttributeExists(cur_attr_id))
{
bad_att_count++;
}
}
uint16_t bad_attr_ids[bad_att_count];
bad_att_count = 0;
pktpos = 3;
Serial.print(F("Unsup Attr: "));
while (pktpos < erx.getDataLength())
{
direction = erx.getData()[pktpos];
cur_attr_id = (erx.getData()[pktpos + 2] << 8) |
(erx.getData()[pktpos + 1] & 0xff);
attr_data_type = erx.getData()[pktpos + 3];
uint8_t rec_length = getAttrCfgRecLen(direction, attr_data_type);
pktpos += rec_length;
if (!zha.GetEndpoint(erx.getDstEndpoint()).GetCluster(erx.getClusterId()).AttributeExists(cur_attr_id))
{
bad_attr_ids[bad_att_count] = cur_attr_id;
bad_att_count++;
Serial.print(cur_attr_id, HEX);
Serial.print(F(", "));
}
}
Serial.println();
/*
min_rpt_int = (erx.getFrameData()[i + 4] << 8) |
(erx.getFrameData()[i + 3] & 0xff);
max_rpt_int = (erx.getFrameData()[i + 6] << 8) |
(erx.getFrameData()[i + 5] & 0xff);*/
zha.sendAttributeCfgRptResp(erx.getClusterId(), bad_attr_ids, bad_att_count, erx.getDstEndpoint(), 0x01, zha.cmd_seq_id);
}
else
{
// Unsupported Global Command
Serial.print(F("Unsprt Cmd ID: "));
Serial.println(cmd_id, HEX);
}
}
}
else
{
// Not from coordinator?
Serial.println(F("Rcv Non Cdr Msg"));
}
}