forked from andrewrapp/xbee-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printers.cpp
374 lines (343 loc) · 11.2 KB
/
Printers.cpp
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
#include "Printers.h"
void printHex(Print& p, const uint8_t* buf, size_t len, const __FlashStringHelper* byte_sep, const __FlashStringHelper* group_sep, size_t group_by) {
size_t cur_group = 0;
while (len--) {
// Print the group separator whenever starting a new
// group
if (group_by && group_sep && cur_group == group_by) {
p.print(group_sep);
cur_group = 0;
}
// Print the byte separator, except when at the start of
// a new group (this also excludes the first byte)
if (cur_group != 0 && byte_sep)
p.print(byte_sep);
printHex(p, *buf);
buf++;
cur_group++;
}
}
void printErrorCb(uint8_t code, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->print(F("Error reading API packet. Error code: 0x"));
p->println(code, HEX);
}
void printErrorCb(ZBTxStatusResponse& r, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
if (!r.isSuccess()) {
p->print(F("Error sending Zigbee packet. Delivery status: 0x"));
p->println(r.getDeliveryStatus(), HEX);
}
}
void printErrorCb(TxStatusResponse& r, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
if (!r.isSuccess()) {
p->print(F("Error sending packet. Delivery status: 0x"));
p->println(r.getStatus(), HEX);
}
}
void printErrorCb(AtCommandResponse& r, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
if (!r.isOk()) {
p->print(F("Error sending "));
p->write(r.getCommand(), 2);
p->print(F(" command. Status: 0x"));
p->println(r.getStatus(), HEX);
}
}
void printErrorCb(RemoteAtCommandResponse& r, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
if (!r.isOk()) {
p->print(F("Error sending remote "));
p->write(r.getCommand(), 2);
p->print(F(" command. Status: 0x"));
p->println(r.getStatus(), HEX);
}
}
void printErrorCb(XBeeResponse& r, uintptr_t data) {
uint8_t id = r.getApiId();
// Figure out the API type and call the corresonding function
if (id == ZB_TX_STATUS_RESPONSE) {
ZBTxStatusResponse response;
r.getZBTxStatusResponse(response);
printErrorCb(response, data);
} else if (id == TX_STATUS_RESPONSE) {
TxStatusResponse response;
r.getTxStatusResponse(response);
printErrorCb(response, data);
} else if (id == AT_COMMAND_RESPONSE) {
AtCommandResponse response;
r.getAtCommandResponse(response);
printErrorCb(response, data);
} else if (id == REMOTE_AT_COMMAND_RESPONSE) {
RemoteAtCommandResponse response;
r.getRemoteAtCommandResponse(response);
printErrorCb(response, data);
}
}
void printRawResponseCb(XBeeResponse& response, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->print("Response: ");
// Reconstruct the original packet
uint8_t header[] = {START_BYTE, response.getMsbLength(), response.getLsbLength(), response.getApiId()};
printHex(*p, header, sizeof(header), F(" "), NULL);
p->write(' ');
printHex(*p, response.getFrameData(), response.getFrameDataLength(), F(" "), NULL);
p->println();
}
/**
* Helper function to print a field name, followed by the hexadecimal
* value and a newline.
*/
template <typename T>
static void printField(Print* p, const __FlashStringHelper *prefix, T data) {
p->print(prefix);
printHex(*p, data);
p->println();
}
void printResponseCb(ZBTxStatusResponse& status, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println(F("ZBTxStatusResponse:"));
printField(p, F(" FrameId: 0x"), status.getFrameId());
printField(p, F(" To: 0x"), status.getRemoteAddress());
printField(p, F(" Delivery status: 0x"), status.getDeliveryStatus());
printField(p, F(" Discovery status: 0x"), status.getDiscoveryStatus());
}
void printResponseCb(ZBRxResponse& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println(F("ZBRxResponse:"));
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
printField(p, F(" Receive options: 0x"), rx.getOption());
if (rx.getDataLength() > 8)
p->print(" Payload:\r\n ");
else
p->print(" Payload: ");
printHex(*p, rx.getFrameData() + rx.getDataOffset(), rx.getDataLength(), F(" "), F("\r\n "), 8);
p->println();
}
void printResponseCb(ZBExplicitRxResponse& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println(F("ZBExplicitRxResponse:"));
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
printField(p, F(" Receive options: 0x"), rx.getOption());
printField(p, F(" Src endpoint: 0x"), rx.getSrcEndpoint());
printField(p, F(" Dst endpoint: 0x"), rx.getDstEndpoint());
printField(p, F(" Cluster id: 0x"), rx.getClusterId());
printField(p, F(" Profile id: 0x"), rx.getProfileId());
if (rx.getDataLength() > 8)
p->print(" Payload:\r\n ");
else
p->print(" Payload: ");
printHex(*p, rx.getFrameData() + rx.getDataOffset(), rx.getDataLength(), F(" "), F("\r\n "), 8);
p->println();
}
void printResponseCb(ZBRxIoSampleResponse& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println(F("ZBRxIoSampleResponse:"));
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
printField(p, F(" Receive options: 0x"), rx.getOption());
for (uint8_t i = 0; i < 16; ++i) {
if (rx.isDigitalEnabled(i)) {
p->print(F(" Digital pin "));
p->print(i);
p->print(F(": "));
p->print(rx.isDigitalOn(i) ? "HIGH" : "LOW");
p->println();
}
}
for (uint8_t i = 0; i < 8; ++i) {
if (rx.isAnalogEnabled(i)) {
p->print(F(" Analog pin "));
p->print(i);
p->print(F(": 0x"));
printHex(*p, rx.getAnalog(i));
p->println();
}
}
}
void printResponseCb(TxStatusResponse& status, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println(F("TxStatusResponse:"));
printField(p, F(" FrameId: 0x"), status.getFrameId());
printField(p, F(" Status: 0x"), status.getStatus());
}
void printResponseCb(Rx16Response& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("Rx16Response:");
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
printField(p, F(" Rssi: 0x"), rx.getRssi());
printField(p, F(" Receive options: 0x"), rx.getOption());
if (rx.getDataLength() > 8)
p->print(" Payload:\r\n ");
else
p->print(" Payload: ");
printHex(*p, rx.getFrameData() + rx.getDataOffset(), rx.getDataLength(), F(" "), F("\r\n "), 8);
p->println();
}
void printResponseCb(Rx64Response& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("Rx64Response:");
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
printField(p, F(" Rssi: 0x"), rx.getRssi());
printField(p, F(" Receive options: 0x"), rx.getOption());
if (rx.getDataLength() > 8)
p->print(" Payload:\r\n ");
else
p->print(" Payload: ");
printHex(*p, rx.getFrameData() + rx.getDataOffset(), rx.getDataLength(), F(" "), F("\r\n "), 8);
p->println();
}
/**
* Helper function to share common functionality between the two sample
* resonses.
*/
static void printSamples(Print* p, RxIoSampleBaseResponse& rx) {
for (uint8_t s = 0; s < rx.getSampleSize(); ++s) {
p->print(F(" Sample "));
p->print(s);
p->println(F(":"));
for (uint8_t i = 0; i < 9; ++i) {
if (rx.isDigitalEnabled(i)) {
p->print(F(" Digital pin "));
p->print(i);
p->print(F(": "));
p->print(rx.isDigitalOn(i, s) ? "HIGH" : "LOW");
p->println();
}
}
for (uint8_t i = 0; i < 7; ++i) {
if (rx.isAnalogEnabled(i)) {
p->print(F(" Analog pin "));
p->print(i);
p->print(F(": 0x"));
printHex(*p, rx.getAnalog(i, s));
p->println();
}
}
}
}
void printResponseCb(Rx16IoSampleResponse& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("Rx16IoSampleResponse:");
printField(p, F(" From: 0x"), rx.getRemoteAddress16());
printField(p, F(" Rssi: 0x"), rx.getRssi());
printField(p, F(" Receive options: 0x"), rx.getOption());
printField(p, F(" Number of samples: 0x"), rx.getSampleSize());
printSamples(p, rx);
}
void printResponseCb(Rx64IoSampleResponse& rx, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("Rx64IoSampleResponse:");
printField(p, F(" From: 0x"), rx.getRemoteAddress64());
printField(p, F(" Rssi: 0x"), rx.getRssi());
printField(p, F(" Receive options: 0x"), rx.getOption());
printField(p, F(" Number of samples: 0x"), rx.getSampleSize());
printSamples(p, rx);
}
void printResponseCb(ModemStatusResponse& status, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("ModemStatusResponse:");
printField(p, F(" Status: 0x"), status.getStatus());
}
void printResponseCb(AtCommandResponse& at, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("AtCommandResponse:");
p->print(F(" Command: "));
p->write(at.getCommand(), 2);
p->println();
printField(p, F(" Status: 0x"), at.getStatus());
if (at.getValueLength()) {
p->print(F(" Value: "));
printHex(*p, at.getValue(), at.getValueLength(), F(" "), NULL);
p->println();
}
}
void printResponseCb(RemoteAtCommandResponse& at, uintptr_t data) {
Print *p = (Print*)data;
if (!p) return;
p->println("AtRemoteCommandResponse:");
printField(p, F(" To: 0x"), at.getRemoteAddress64());
printField(p, F(" To: 0x"), at.getRemoteAddress16());
p->print(F(" Command: "));
p->write(at.getCommand(), 2);
p->println();
printField(p, F(" Status: 0x"), at.getStatus());
if (at.getValueLength()) {
p->print(F(" Value: "));
printHex(*p, at.getValue(), at.getValueLength(), F(" "), NULL);
p->println();
}
}
void printResponseCb(XBeeResponse& r, uintptr_t data) {
uint8_t id = r.getApiId();
// Figure out the API type and call the corresponding function
if (id == ZB_TX_STATUS_RESPONSE) {
ZBTxStatusResponse response;
r.getZBTxStatusResponse(response);
printResponseCb(response, data);
} else if (id == ZB_RX_RESPONSE) {
ZBRxResponse response;
r.getZBRxResponse(response);
printResponseCb(response, data);
} else if (id == ZB_EXPLICIT_RX_RESPONSE) {
ZBExplicitRxResponse response;
r.getZBExplicitRxResponse(response);
printResponseCb(response, data);
} else if (id == ZB_IO_SAMPLE_RESPONSE) {
ZBRxIoSampleResponse response;
r.getZBRxIoSampleResponse(response);
printResponseCb(response, data);
} else if (id == TX_STATUS_RESPONSE) {
TxStatusResponse response;
r.getTxStatusResponse(response);
printResponseCb(response, data);
} else if (id == RX_16_RESPONSE) {
Rx16Response response;
r.getRx16Response(response);
printResponseCb(response, data);
} else if (id == RX_64_RESPONSE) {
Rx64Response response;
r.getRx64Response(response);
printResponseCb(response, data);
} else if (id == RX_16_IO_RESPONSE) {
Rx16IoSampleResponse response;
r.getRx16IoSampleResponse(response);
printResponseCb(response, data);
} else if (id == RX_64_IO_RESPONSE) {
Rx64IoSampleResponse response;
r.getRx64IoSampleResponse(response);
printResponseCb(response, data);
} else if (id == MODEM_STATUS_RESPONSE) {
ModemStatusResponse response;
r.getModemStatusResponse(response);
printResponseCb(response, data);
} else if (id == AT_COMMAND_RESPONSE) {
AtCommandResponse response;
r.getAtCommandResponse(response);
printResponseCb(response, data);
} else if (id == REMOTE_AT_COMMAND_RESPONSE) {
RemoteAtCommandResponse response;
r.getRemoteAtCommandResponse(response);
printResponseCb(response, data);
}
}