This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
xbeep.cpp
458 lines (379 loc) · 13 KB
/
xbeep.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
libxbee - a C library to aid the use of Digi's XBee wireless modules
running in API mode.
Copyright (C) 2009 onwards Attie Grande ([email protected])
libxbee is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libxbee is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <iostream>
#ifdef LIBXBEE_BUILD
#include "xsys.h"
#else
#define EXPORT
#endif
#include <xbee.h>
#include <xbeep.h>
/* ========================================================================== */
EXPORT std::list<libxbee::XBee*> libxbee::xbeeList;
EXPORT std::list<std::string> libxbee::getModes(void) {
xbee_err ret;
char **modes;
int i;
std::list<std::string> modeList;
if ((ret = xbee_modeGetList(&modes)) != XBEE_ENONE) throw(ret);
for (i = 0; modes[i] != NULL; i++) {
modeList.push_back(std::string(modes[i]));
}
free(modes);
return modeList;
}
/* ========================================================================== */
EXPORT libxbee::XBee::XBee(const std::string &mode) {
xbee_err ret;
if ((ret = xbee_setup(&xbee, mode.c_str())) != XBEE_ENONE) throw(ret);
xbeeList.push_back(this);
}
EXPORT libxbee::XBee::XBee(const std::string &mode, const std::string &device, int baudrate) {
xbee_err ret;
if ((ret = xbee_setup(&xbee, mode.c_str(), device.c_str(), baudrate)) != XBEE_ENONE) throw(ret);
xbeeList.push_back(this);
}
EXPORT libxbee::XBee::XBee(const std::string &mode, va_list ap) {
xbee_err ret;
if ((ret = xbee_vsetup(&xbee, mode.c_str(), ap)) != XBEE_ENONE) throw(ret);
xbeeList.push_back(this);
}
EXPORT libxbee::XBee::~XBee(void) {
std::list<Con*>::iterator con;
for (con = conList.begin(); con != conList.end(); con++) {
(*con)->xbee = NULL;
xbee_conEnd((*con)->con);
(*con)->con = NULL;
}
xbee_shutdown(xbee);
xbeeList.remove(this);
}
EXPORT struct xbee *libxbee::XBee::getHnd(void) {
return xbee;
}
EXPORT const struct xbee *libxbee::XBee::getHnd(void) const {
return xbee;
}
EXPORT void libxbee::XBee::conRegister(Con *con) {
xbee_err ret;
if ((ret = xbee_conValidate(con->getHnd())) != XBEE_ENONE) throw(ret);
conList.push_back(con);
conList.unique();
}
EXPORT void libxbee::XBee::conUnregister(Con *con) {
conList.remove(con);
}
EXPORT const libxbee::Con *libxbee::XBee::conLocate(const struct xbee_con *con) const {
std::list<Con*>::const_iterator i;
for (i = conList.begin(); i != conList.end(); i++) {
if ((*i)->getHnd() == con) return (*i);
}
return NULL;
}
EXPORT libxbee::Con *libxbee::XBee::conLocate(const struct xbee_con *con) {
std::list<Con*>::const_iterator i;
for (i = conList.begin(); i != conList.end(); i++) {
if ((*i)->getHnd() == con) return (*i);
}
return NULL;
}
EXPORT std::list<std::string> libxbee::XBee::getConTypes(void) const {
xbee_err ret;
char **types;
int i;
std::list<std::string> typeList;
if ((ret = xbee_conGetTypes(xbee, &types)) != XBEE_ENONE) throw(ret);
for (i = 0; types[i] != NULL; i++) {
typeList.push_back(std::string(types[i]));
}
free(types);
return typeList;
}
EXPORT std::string libxbee::XBee::mode(void) const {
xbee_err ret;
const char *mode;
if ((ret = xbee_modeGet(getHnd(), &mode)) != XBEE_ENONE) throw(ret);
return std::string(mode);
}
EXPORT void libxbee::XBee::setLogTarget(FILE *f) const {
xbee_err ret;
if ((ret = xbee_logTargetSet(xbee, f)) != XBEE_ENONE) throw(ret);
}
EXPORT void libxbee::XBee::setLogLevel(int level) const {
xbee_err ret;
if ((ret = xbee_logLevelSet(xbee, level)) != XBEE_ENONE) throw(ret);
}
EXPORT int libxbee::XBee::getLogLevel(void) const {
xbee_err ret;
int level;
if ((ret = xbee_logLevelGet(xbee, &level)) != XBEE_ENONE) throw(ret);
return level;
}
/* ========================================================================== */
EXPORT libxbee::Con::Con(XBee &parent, const std::string &type, struct xbee_conAddress *address) : parent(parent) {
xbee_err ret;
if ((xbee = parent.getHnd()) == NULL) throw(XBEE_EINVAL);
if ((ret = xbee_conNew(xbee, &con, type.c_str(), address)) != XBEE_ENONE) throw(ret);
if ((ret = xbee_conDataSet(con, (void*)this, NULL)) != XBEE_ENONE) {
xbee_conEnd(con);
throw(ret);
}
try {
parent.conRegister(this);
} catch (xbee_err ret) {
xbee_conEnd(con);
throw(ret);
}
}
EXPORT libxbee::Con::~Con(void) {
if (xbee != NULL) parent.conUnregister(this);
if (con != NULL) xbee_conEnd(con);
}
EXPORT void libxbee::Con::xbee_conCallback(Pkt **pkt) { }
EXPORT unsigned char libxbee::Con::operator<< (const std::string &data) const {
return Tx(data);
}
EXPORT unsigned char libxbee::Con::operator<< (const std::vector<unsigned char> &data) const {
return Tx(data);
}
EXPORT unsigned char libxbee::Con::operator<< (const std::vector<char> &data) const {
return Tx(data);
}
EXPORT void libxbee::Con::operator>> (Pkt &pkt) const {
return Rx(pkt);
}
EXPORT void libxbee::Con::operator>> (std::string &data) const {
libxbee::Pkt p;
p << *this;
p >> data;
}
EXPORT void libxbee::Con::operator>> (std::vector<unsigned char> &data) const {
libxbee::Pkt p;
p << *this;
p >> data;
}
EXPORT void libxbee::Con::operator>> (std::vector<char> &data) const {
libxbee::Pkt p;
p << *this;
p >> data;
}
EXPORT const struct xbee_con *libxbee::Con::getHnd(void) const {
if (con == NULL) throw(XBEE_ESHUTDOWN);
return con;
}
EXPORT struct xbee_con *libxbee::Con::getHnd(void) {
return const_cast<struct xbee_con *>(static_cast<const libxbee::Con *>(this)->getHnd());
}
EXPORT unsigned char libxbee::Con::Tx(const std::string &data) const {
return Tx((unsigned char *)NULL, (const unsigned char *)data.c_str(), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(const std::vector<unsigned char> &data) const {
return Tx((unsigned char *)NULL, &(data[0]), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(const std::vector<char> &data) const {
return Tx((unsigned char *)NULL, (unsigned char *)&(data[0]), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(const unsigned char *buf, int len) const {
return Tx((unsigned char *)NULL, buf, len);
}
EXPORT unsigned char libxbee::Con::Tx(unsigned char *frameId, const std::string &data) const {
return Tx(frameId, (const unsigned char *)data.c_str(), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(unsigned char *frameId, const std::vector<unsigned char> &data) const {
return Tx(frameId, &(data[0]), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(unsigned char *frameId, const std::vector<char> &data) const {
return Tx(frameId, (unsigned char *)&(data[0]), data.size());
}
EXPORT unsigned char libxbee::Con::Tx(unsigned char *frameId, const unsigned char *buf, int len) const {
unsigned char retVal;
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_connxTx(con, &retVal, frameId, buf, len)) == XBEE_ENONE) return retVal;
if (ret == XBEE_ETX) throw(libxbee::xbee_etx(ret, retVal));
throw(ret);
}
EXPORT void libxbee::Con::Rx(Pkt &pkt, int *remainingPackets) const {
struct xbee_pkt *raw_pkt;
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conRx(con, &raw_pkt, remainingPackets)) != XBEE_ENONE) throw(ret);
pkt.setHnd(raw_pkt);
}
EXPORT int libxbee::Con::RxAvailable(void) const {
int remainingPackets;
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conRx(con, NULL, &remainingPackets)) != XBEE_ENONE) throw(ret);
return remainingPackets;
}
EXPORT void libxbee::Con::purge(void) {
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conPurge(con)) != XBEE_ENONE) throw(ret);
}
EXPORT void libxbee::Con::sleep(void) {
setSleep(CON_SLEEP);
}
EXPORT void libxbee::Con::snooze(void) {
setSleep(CON_SNOOZE);
std::cout << "Sent for snooze\n";
}
EXPORT void libxbee::Con::wake(void) {
setSleep(CON_AWAKE);
}
EXPORT void libxbee::Con::setSleep(enum xbee_conSleepStates state) {
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conSleepSet(con, state)) != XBEE_ENONE) throw(ret);
}
EXPORT enum xbee_conSleepStates libxbee::Con::getSleep(void) {
xbee_err ret;
enum xbee_conSleepStates state;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conSleepGet(con, &state)) != XBEE_ENONE) throw(ret);
return state;
}
EXPORT void libxbee::Con::getSettings(struct xbee_conSettings *settings) {
xbee_err ret;
if (settings == NULL) throw(XBEE_EINVAL);
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conSettings(con, NULL, settings)) != XBEE_ENONE) throw(ret);
}
EXPORT void libxbee::Con::setSettings(struct xbee_conSettings *settings) {
xbee_err ret;
if (settings == NULL) throw(XBEE_EINVAL);
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conSettings(con, settings, NULL)) != XBEE_ENONE) throw(ret);
}
/* ========================================================================== */
EXPORT libxbee::ConCallback::ConCallback(XBee &parent, std::string type, struct xbee_conAddress *address) : Con(parent, type, address), parent(parent) {
xbee_err ret;
if (con == NULL) throw(XBEE_ESHUTDOWN);
if ((ret = xbee_conCallbackSet(con, ConCallback::libxbee_callbackFunction, NULL)) != XBEE_ENONE) throw(ret);
}
void libxbee::ConCallback::libxbee_callbackFunction(struct xbee *xbee, struct xbee_con *con, struct xbee_pkt **pkt, void **data) {
Con *c = (libxbee::Con*)*data;
if (c->parent.getHnd() != xbee) {
std::cerr << " -1#[" << __FILE__ << ":" << __LINE__ << "] " << __FUNCTION__ << "(): A connection called back to the C++ interface, but it didn't provide suitable information...\n";
return;
}
Pkt *p = new Pkt(*pkt);
*pkt = NULL; /* we'll either delete its container right after the callback, or it becomes the user's responsibility */
c->xbee_conCallback(&p);
delete p;
}
/* ========================================================================== */
EXPORT libxbee::Pkt::Pkt(struct xbee_pkt *pkt) {
this->pkt = pkt;
}
EXPORT libxbee::Pkt::~Pkt(void) {
if (pkt != NULL) xbee_pktFree(pkt);
}
EXPORT unsigned char libxbee::Pkt::operator[] (int index) {
if (pkt == NULL) throw(XBEE_EINVAL);
if (index > size()) throw(XBEE_ERANGE);
return pkt->data[index];
}
EXPORT void libxbee::Pkt::operator<< (const Con &con) {
con.Rx(*this);
}
EXPORT void libxbee::Pkt::operator>> (std::string &data) const{
data = getData();
}
EXPORT void libxbee::Pkt::operator>> (std::vector<unsigned char> &data) const {
data = getVector();
}
EXPORT void libxbee::Pkt::operator>> (std::vector<char> &data) const {
data = getVector2();
}
EXPORT int libxbee::Pkt::size(void) const {
if (pkt == NULL) throw(XBEE_EINVAL);
return pkt->dataLen;
}
EXPORT const struct xbee_pkt *libxbee::Pkt::getHnd(void) const {
return pkt;
}
EXPORT struct xbee_pkt *libxbee::Pkt::getHnd(void) {
return pkt;
}
EXPORT void libxbee::Pkt::setHnd(struct xbee_pkt *pkt) {
if (this->pkt) xbee_pktFree(this->pkt);
this->pkt = pkt;
}
EXPORT struct xbee_pkt *libxbee::Pkt::dropHnd(void) {
struct xbee_pkt *t;
t = this->pkt;
this->pkt = NULL;
return t;
}
EXPORT std::string libxbee::Pkt::getData(void) const {
return std::string((char*)pkt->data, pkt->dataLen);
}
EXPORT std::vector<unsigned char> libxbee::Pkt::getVector(void) const {
std::vector<unsigned char> data;
for (int i = 0; i < pkt->dataLen; i++) {
data.push_back(pkt->data[i]);
}
return data;
}
EXPORT std::vector<char> libxbee::Pkt::getVector2(void) const {
std::vector<char> data;
for (int i = 0; i < pkt->dataLen; i++) {
data.push_back((char)pkt->data[i]);
}
return data;
}
EXPORT void *libxbee::Pkt::getData(const char *key) const {
return getData(key, 0, 0);
}
EXPORT void *libxbee::Pkt::getData(const char *key, int id) const {
return getData(key, id, 0);
}
EXPORT void *libxbee::Pkt::getData(const char *key, int id, int index) const {
xbee_err ret;
void *p;
if ((ret = xbee_pktDataGet(pkt, key, id, index, &p)) != XBEE_ENONE) throw(ret);
return p;
}
EXPORT std::string libxbee::Pkt::getATCommand(void) const {
std::string type(pkt->conType);
if (type != "Local AT" && type != "Remote AT") throw(XBEE_EINVAL);
return std::string((char*)pkt->atCommand, 2);
}
EXPORT int libxbee::Pkt::getAnalog(int channel) const {
return getAnalog(channel, 0);
}
EXPORT int libxbee::Pkt::getAnalog(int channel, int index) const {
xbee_err ret;
int value;
if ((ret = xbee_pktAnalogGet(pkt, channel, index, &value)) != XBEE_ENONE) throw(ret);
return value;
}
EXPORT bool libxbee::Pkt::getDigital(int channel) const {
return getDigital(channel, 0);
}
EXPORT bool libxbee::Pkt::getDigital(int channel, int index) const {
xbee_err ret;
int value;
if ((ret = xbee_pktDigitalGet(pkt, channel, index, &value)) != XBEE_ENONE) throw(ret);
return (bool)!!value;
}
EXPORT unsigned char libxbee::Pkt::getRssi(void) const {
return pkt->rssi;
}