PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /**
  2. * Copyright (c) 2009 Andrew Rapp. All rights reserved.
  3. *
  4. * This file is part of XBee-Arduino.
  5. *
  6. * XBee-Arduino is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * XBee-Arduino is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef XBee_h
  20. #define XBee_h
  21. #if defined(ARDUINO) && ARDUINO >= 100
  22. #include "Arduino.h"
  23. #else
  24. #include "WProgram.h"
  25. #endif
  26. #include <inttypes.h>
  27. #define SERIES_1
  28. #define SERIES_2
  29. // set to ATAP value of XBee. AP=2 is recommended
  30. #define ATAP 2
  31. #define START_BYTE 0x7e
  32. #define ESCAPE 0x7d
  33. #define XON 0x11
  34. #define XOFF 0x13
  35. // This value determines the size of the byte array for receiving RX packets
  36. // Most users won't be dealing with packets this large so you can adjust this
  37. // value to reduce memory consumption. But, remember that
  38. // if a RX packet exceeds this size, it cannot be parsed!
  39. // This value is determined by the largest packet size (100 byte payload + 64-bit address + option byte and rssi byte) of a series 1 radio
  40. #define MAX_FRAME_DATA_SIZE 110
  41. #define BROADCAST_ADDRESS 0xffff
  42. #define ZB_BROADCAST_ADDRESS 0xfffe
  43. // the non-variable length of the frame data (not including frame id or api id or variable data size (e.g. payload, at command set value)
  44. #define ZB_TX_API_LENGTH 12
  45. #define TX_16_API_LENGTH 3
  46. #define TX_64_API_LENGTH 9
  47. #define AT_COMMAND_API_LENGTH 2
  48. #define REMOTE_AT_COMMAND_API_LENGTH 13
  49. // start/length(2)/api/frameid/checksum bytes
  50. #define PACKET_OVERHEAD_LENGTH 6
  51. // api is always the third byte in packet
  52. #define API_ID_INDEX 3
  53. // frame position of rssi byte
  54. #define RX_16_RSSI_OFFSET 2
  55. #define RX_64_RSSI_OFFSET 8
  56. #define DEFAULT_FRAME_ID 1
  57. #define NO_RESPONSE_FRAME_ID 0
  58. // TODO put in tx16 class
  59. #define ACK_OPTION 0
  60. #define DISABLE_ACK_OPTION 1
  61. #define BROADCAST_OPTION 4
  62. // RX options
  63. #define ZB_PACKET_ACKNOWLEDGED 0x01
  64. #define ZB_BROADCAST_PACKET 0x02
  65. // not everything is implemented!
  66. /**
  67. * Api Id constants
  68. */
  69. #define TX_64_REQUEST 0x0
  70. #define TX_16_REQUEST 0x1
  71. #define AT_COMMAND_REQUEST 0x08
  72. #define AT_COMMAND_QUEUE_REQUEST 0x09
  73. #define REMOTE_AT_REQUEST 0x17
  74. #define ZB_TX_REQUEST 0x10
  75. #define ZB_EXPLICIT_TX_REQUEST 0x11
  76. #define RX_64_RESPONSE 0x80
  77. #define RX_16_RESPONSE 0x81
  78. #define RX_64_IO_RESPONSE 0x82
  79. #define RX_16_IO_RESPONSE 0x83
  80. #define AT_RESPONSE 0x88
  81. #define TX_STATUS_RESPONSE 0x89
  82. #define MODEM_STATUS_RESPONSE 0x8a
  83. #define ZB_RX_RESPONSE 0x90
  84. #define ZB_EXPLICIT_RX_RESPONSE 0x91
  85. #define ZB_TX_STATUS_RESPONSE 0x8b
  86. #define ZB_IO_SAMPLE_RESPONSE 0x92
  87. #define ZB_IO_NODE_IDENTIFIER_RESPONSE 0x95
  88. #define AT_COMMAND_RESPONSE 0x88
  89. #define REMOTE_AT_COMMAND_RESPONSE 0x97
  90. /**
  91. * TX STATUS constants
  92. */
  93. #define SUCCESS 0x0
  94. #define CCA_FAILURE 0x2
  95. #define INVALID_DESTINATION_ENDPOINT_SUCCESS 0x15
  96. #define NETWORK_ACK_FAILURE 0x21
  97. #define NOT_JOINED_TO_NETWORK 0x22
  98. #define SELF_ADDRESSED 0x23
  99. #define ADDRESS_NOT_FOUND 0x24
  100. #define ROUTE_NOT_FOUND 0x25
  101. #define PAYLOAD_TOO_LARGE 0x74
  102. // modem status
  103. #define HARDWARE_RESET 0
  104. #define WATCHDOG_TIMER_RESET 1
  105. #define ASSOCIATED 2
  106. #define DISASSOCIATED 3
  107. #define SYNCHRONIZATION_LOST 4
  108. #define COORDINATOR_REALIGNMENT 5
  109. #define COORDINATOR_STARTED 6
  110. #define ZB_BROADCAST_RADIUS_MAX_HOPS 0
  111. #define ZB_TX_UNICAST 0
  112. #define ZB_TX_BROADCAST 8
  113. #define AT_OK 0
  114. #define AT_ERROR 1
  115. #define AT_INVALID_COMMAND 2
  116. #define AT_INVALID_PARAMETER 3
  117. #define AT_NO_RESPONSE 4
  118. #define NO_ERROR 0
  119. #define CHECKSUM_FAILURE 1
  120. #define PACKET_EXCEEDS_BYTE_ARRAY_LENGTH 2
  121. #define UNEXPECTED_START_BYTE 3
  122. /**
  123. * The super class of all XBee responses (RX packets)
  124. * Users should never attempt to create an instance of this class; instead
  125. * create an instance of a subclass
  126. * It is recommend to reuse subclasses to conserve memory
  127. */
  128. class XBeeResponse {
  129. public:
  130. //static const int MODEM_STATUS = 0x8a;
  131. /**
  132. * Default constructor
  133. */
  134. XBeeResponse();
  135. /**
  136. * Returns Api Id of the response
  137. */
  138. uint8_t getApiId();
  139. void setApiId(uint8_t apiId);
  140. /**
  141. * Returns the MSB length of the packet
  142. */
  143. uint8_t getMsbLength();
  144. void setMsbLength(uint8_t msbLength);
  145. /**
  146. * Returns the LSB length of the packet
  147. */
  148. uint8_t getLsbLength();
  149. void setLsbLength(uint8_t lsbLength);
  150. /**
  151. * Returns the packet checksum
  152. */
  153. uint8_t getChecksum();
  154. void setChecksum(uint8_t checksum);
  155. /**
  156. * Returns the length of the frame data: all bytes after the api id, and prior to the checksum
  157. * Note up to release 0.1.2, this was incorrectly including the checksum in the length.
  158. */
  159. uint8_t getFrameDataLength();
  160. void setFrameData(uint8_t* frameDataPtr);
  161. /**
  162. * Returns the buffer that contains the response.
  163. * Starts with byte that follows API ID and includes all bytes prior to the checksum
  164. * Length is specified by getFrameDataLength()
  165. * Note: Unlike Digi's definition of the frame data, this does not start with the API ID..
  166. * The reason for this is all responses include an API ID, whereas my frame data
  167. * includes only the API specific data.
  168. */
  169. uint8_t* getFrameData();
  170. void setFrameLength(uint8_t frameLength);
  171. // to support future 65535 byte packets I guess
  172. /**
  173. * Returns the length of the packet
  174. */
  175. uint16_t getPacketLength();
  176. /**
  177. * Resets the response to default values
  178. */
  179. void reset();
  180. /**
  181. * Initializes the response
  182. */
  183. void init();
  184. #ifdef SERIES_2
  185. /**
  186. * Call with instance of ZBTxStatusResponse class only if getApiId() == ZB_TX_STATUS_RESPONSE
  187. * to populate response
  188. */
  189. void getZBTxStatusResponse(XBeeResponse &response);
  190. /**
  191. * Call with instance of ZBRxResponse class only if getApiId() == ZB_RX_RESPONSE
  192. * to populate response
  193. */
  194. void getZBRxResponse(XBeeResponse &response);
  195. /**
  196. * Call with instance of ZBRxIoSampleResponse class only if getApiId() == ZB_IO_SAMPLE_RESPONSE
  197. * to populate response
  198. */
  199. void getZBRxIoSampleResponse(XBeeResponse &response);
  200. #endif
  201. #ifdef SERIES_1
  202. /**
  203. * Call with instance of TxStatusResponse only if getApiId() == TX_STATUS_RESPONSE
  204. */
  205. void getTxStatusResponse(XBeeResponse &response);
  206. /**
  207. * Call with instance of Rx16Response only if getApiId() == RX_16_RESPONSE
  208. */
  209. void getRx16Response(XBeeResponse &response);
  210. /**
  211. * Call with instance of Rx64Response only if getApiId() == RX_64_RESPONSE
  212. */
  213. void getRx64Response(XBeeResponse &response);
  214. /**
  215. * Call with instance of Rx16IoSampleResponse only if getApiId() == RX_16_IO_RESPONSE
  216. */
  217. void getRx16IoSampleResponse(XBeeResponse &response);
  218. /**
  219. * Call with instance of Rx64IoSampleResponse only if getApiId() == RX_64_IO_RESPONSE
  220. */
  221. void getRx64IoSampleResponse(XBeeResponse &response);
  222. #endif
  223. /**
  224. * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE
  225. */
  226. void getAtCommandResponse(XBeeResponse &responses);
  227. /**
  228. * Call with instance of RemoteAtCommandResponse only if getApiId() == REMOTE_AT_COMMAND_RESPONSE
  229. */
  230. void getRemoteAtCommandResponse(XBeeResponse &response);
  231. /**
  232. * Call with instance of ModemStatusResponse only if getApiId() == MODEM_STATUS_RESPONSE
  233. */
  234. void getModemStatusResponse(XBeeResponse &response);
  235. /**
  236. * Returns true if the response has been successfully parsed and is complete and ready for use
  237. */
  238. bool isAvailable();
  239. void setAvailable(bool complete);
  240. /**
  241. * Returns true if the response contains errors
  242. */
  243. bool isError();
  244. /**
  245. * Returns an error code, or zero, if successful.
  246. * Error codes include: CHECKSUM_FAILURE, PACKET_EXCEEDS_BYTE_ARRAY_LENGTH, UNEXPECTED_START_BYTE
  247. */
  248. uint8_t getErrorCode();
  249. void setErrorCode(uint8_t errorCode);
  250. protected:
  251. // pointer to frameData
  252. uint8_t* _frameDataPtr;
  253. private:
  254. void setCommon(XBeeResponse &target);
  255. uint8_t _apiId;
  256. uint8_t _msbLength;
  257. uint8_t _lsbLength;
  258. uint8_t _checksum;
  259. uint8_t _frameLength;
  260. bool _complete;
  261. uint8_t _errorCode;
  262. };
  263. class XBeeAddress {
  264. public:
  265. XBeeAddress();
  266. };
  267. /**
  268. * Represents a 64-bit XBee Address
  269. */
  270. class XBeeAddress64 : public XBeeAddress {
  271. public:
  272. XBeeAddress64(uint32_t msb, uint32_t lsb);
  273. XBeeAddress64();
  274. uint32_t getMsb();
  275. uint32_t getLsb();
  276. void setMsb(uint32_t msb);
  277. void setLsb(uint32_t lsb);
  278. private:
  279. uint32_t _msb;
  280. uint32_t _lsb;
  281. };
  282. //class XBeeAddress16 : public XBeeAddress {
  283. //public:
  284. // XBeeAddress16(uint16_t addr);
  285. // XBeeAddress16();
  286. // uint16_t getAddress();
  287. // void setAddress(uint16_t addr);
  288. //private:
  289. // uint16_t _addr;
  290. //};
  291. /**
  292. * This class is extended by all Responses that include a frame id
  293. */
  294. class FrameIdResponse : public XBeeResponse {
  295. public:
  296. FrameIdResponse();
  297. uint8_t getFrameId();
  298. private:
  299. uint8_t _frameId;
  300. };
  301. /**
  302. * Common functionality for both Series 1 and 2 data RX data packets
  303. */
  304. class RxDataResponse : public XBeeResponse {
  305. public:
  306. RxDataResponse();
  307. /**
  308. * Returns the specified index of the payload. The index may be 0 to getDataLength() - 1
  309. * This method is deprecated; use uint8_t* getData()
  310. */
  311. uint8_t getData(int index);
  312. /**
  313. * Returns the payload array. This may be accessed from index 0 to getDataLength() - 1
  314. */
  315. uint8_t* getData();
  316. /**
  317. * Returns the length of the payload
  318. */
  319. virtual uint8_t getDataLength() = 0;
  320. /**
  321. * Returns the position in the frame data where the data begins
  322. */
  323. virtual uint8_t getDataOffset() = 0;
  324. };
  325. // getResponse to return the proper subclass:
  326. // we maintain a pointer to each type of response, when a response is parsed, it is allocated only if NULL
  327. // can we allocate an object in a function?
  328. #ifdef SERIES_2
  329. /**
  330. * Represents a Series 2 TX status packet
  331. */
  332. class ZBTxStatusResponse : public FrameIdResponse {
  333. public:
  334. ZBTxStatusResponse();
  335. uint16_t getRemoteAddress();
  336. uint8_t getTxRetryCount();
  337. uint8_t getDeliveryStatus();
  338. uint8_t getDiscoveryStatus();
  339. bool isSuccess();
  340. };
  341. /**
  342. * Represents a Series 2 RX packet
  343. */
  344. class ZBRxResponse : public RxDataResponse {
  345. public:
  346. ZBRxResponse();
  347. XBeeAddress64& getRemoteAddress64();
  348. uint16_t getRemoteAddress16();
  349. uint8_t getOption();
  350. uint8_t getDataLength();
  351. // frame position where data starts
  352. uint8_t getDataOffset();
  353. private:
  354. XBeeAddress64 _remoteAddress64;
  355. };
  356. /**
  357. * Represents a Series 2 RX I/O Sample packet
  358. */
  359. class ZBRxIoSampleResponse : public ZBRxResponse {
  360. public:
  361. ZBRxIoSampleResponse();
  362. bool containsAnalog();
  363. bool containsDigital();
  364. /**
  365. * Returns true if the pin is enabled
  366. */
  367. bool isAnalogEnabled(uint8_t pin);
  368. /**
  369. * Returns true if the pin is enabled
  370. */
  371. bool isDigitalEnabled(uint8_t pin);
  372. /**
  373. * Returns the 10-bit analog reading of the specified pin.
  374. * Valid pins include ADC:xxx.
  375. */
  376. uint16_t getAnalog(uint8_t pin);
  377. /**
  378. * Returns true if the specified pin is high/on.
  379. * Valid pins include DIO:xxx.
  380. */
  381. bool isDigitalOn(uint8_t pin);
  382. uint8_t getDigitalMaskMsb();
  383. uint8_t getDigitalMaskLsb();
  384. uint8_t getAnalogMask();
  385. };
  386. #endif
  387. #ifdef SERIES_1
  388. /**
  389. * Represents a Series 1 TX Status packet
  390. */
  391. class TxStatusResponse : public FrameIdResponse {
  392. public:
  393. TxStatusResponse();
  394. uint8_t getStatus();
  395. bool isSuccess();
  396. };
  397. /**
  398. * Represents a Series 1 RX packet
  399. */
  400. class RxResponse : public RxDataResponse {
  401. public:
  402. RxResponse();
  403. // remember rssi is negative but this is unsigned byte so it's up to you to convert
  404. uint8_t getRssi();
  405. uint8_t getOption();
  406. bool isAddressBroadcast();
  407. bool isPanBroadcast();
  408. uint8_t getDataLength();
  409. uint8_t getDataOffset();
  410. virtual uint8_t getRssiOffset() = 0;
  411. };
  412. /**
  413. * Represents a Series 1 16-bit address RX packet
  414. */
  415. class Rx16Response : public RxResponse {
  416. public:
  417. Rx16Response();
  418. uint8_t getRssiOffset();
  419. uint16_t getRemoteAddress16();
  420. protected:
  421. uint16_t _remoteAddress;
  422. };
  423. /**
  424. * Represents a Series 1 64-bit address RX packet
  425. */
  426. class Rx64Response : public RxResponse {
  427. public:
  428. Rx64Response();
  429. uint8_t getRssiOffset();
  430. XBeeAddress64& getRemoteAddress64();
  431. private:
  432. XBeeAddress64 _remoteAddress;
  433. };
  434. /**
  435. * Represents a Series 1 RX I/O Sample packet
  436. */
  437. class RxIoSampleBaseResponse : public RxResponse {
  438. public:
  439. RxIoSampleBaseResponse();
  440. /**
  441. * Returns the number of samples in this packet
  442. */
  443. uint8_t getSampleSize();
  444. bool containsAnalog();
  445. bool containsDigital();
  446. /**
  447. * Returns true if the specified analog pin is enabled
  448. */
  449. bool isAnalogEnabled(uint8_t pin);
  450. /**
  451. * Returns true if the specified digital pin is enabled
  452. */
  453. bool isDigitalEnabled(uint8_t pin);
  454. /**
  455. * Returns the 10-bit analog reading of the specified pin.
  456. * Valid pins include ADC:0-5. Sample index starts at 0
  457. */
  458. uint16_t getAnalog(uint8_t pin, uint8_t sample);
  459. /**
  460. * Returns true if the specified pin is high/on.
  461. * Valid pins include DIO:0-8. Sample index starts at 0
  462. */
  463. bool isDigitalOn(uint8_t pin, uint8_t sample);
  464. uint8_t getSampleOffset();
  465. private:
  466. };
  467. class Rx16IoSampleResponse : public RxIoSampleBaseResponse {
  468. public:
  469. Rx16IoSampleResponse();
  470. uint16_t getRemoteAddress16();
  471. uint8_t getRssiOffset();
  472. };
  473. class Rx64IoSampleResponse : public RxIoSampleBaseResponse {
  474. public:
  475. Rx64IoSampleResponse();
  476. XBeeAddress64& getRemoteAddress64();
  477. uint8_t getRssiOffset();
  478. private:
  479. XBeeAddress64 _remoteAddress;
  480. };
  481. #endif
  482. /**
  483. * Represents a Modem Status RX packet
  484. */
  485. class ModemStatusResponse : public XBeeResponse {
  486. public:
  487. ModemStatusResponse();
  488. uint8_t getStatus();
  489. };
  490. /**
  491. * Represents an AT Command RX packet
  492. */
  493. class AtCommandResponse : public FrameIdResponse {
  494. public:
  495. AtCommandResponse();
  496. /**
  497. * Returns an array containing the two character command
  498. */
  499. uint8_t* getCommand();
  500. /**
  501. * Returns the command status code.
  502. * Zero represents a successful command
  503. */
  504. uint8_t getStatus();
  505. /**
  506. * Returns an array containing the command value.
  507. * This is only applicable to query commands.
  508. */
  509. uint8_t* getValue();
  510. /**
  511. * Returns the length of the command value array.
  512. */
  513. uint8_t getValueLength();
  514. /**
  515. * Returns true if status equals AT_OK
  516. */
  517. bool isOk();
  518. };
  519. /**
  520. * Represents a Remote AT Command RX packet
  521. */
  522. class RemoteAtCommandResponse : public AtCommandResponse {
  523. public:
  524. RemoteAtCommandResponse();
  525. /**
  526. * Returns an array containing the two character command
  527. */
  528. uint8_t* getCommand();
  529. /**
  530. * Returns the command status code.
  531. * Zero represents a successful command
  532. */
  533. uint8_t getStatus();
  534. /**
  535. * Returns an array containing the command value.
  536. * This is only applicable to query commands.
  537. */
  538. uint8_t* getValue();
  539. /**
  540. * Returns the length of the command value array.
  541. */
  542. uint8_t getValueLength();
  543. /**
  544. * Returns the 16-bit address of the remote radio
  545. */
  546. uint16_t getRemoteAddress16();
  547. /**
  548. * Returns the 64-bit address of the remote radio
  549. */
  550. XBeeAddress64& getRemoteAddress64();
  551. /**
  552. * Returns true if command was successful
  553. */
  554. bool isOk();
  555. private:
  556. XBeeAddress64 _remoteAddress64;
  557. };
  558. /**
  559. * Super class of all XBee requests (TX packets)
  560. * Users should never create an instance of this class; instead use an subclass of this class
  561. * It is recommended to reuse Subclasses of the class to conserve memory
  562. * <p/>
  563. * This class allocates a buffer to
  564. */
  565. class XBeeRequest {
  566. public:
  567. /**
  568. * Constructor
  569. * TODO make protected
  570. */
  571. XBeeRequest(uint8_t apiId, uint8_t frameId);
  572. /**
  573. * Sets the frame id. Must be between 1 and 255 inclusive to get a TX status response.
  574. */
  575. void setFrameId(uint8_t frameId);
  576. /**
  577. * Returns the frame id
  578. */
  579. uint8_t getFrameId();
  580. /**
  581. * Returns the API id
  582. */
  583. uint8_t getApiId();
  584. // setting = 0 makes this a pure virtual function, meaning the subclass must implement, like abstract in java
  585. /**
  586. * Starting after the frame id (pos = 0) and up to but not including the checksum
  587. * Note: Unlike Digi's definition of the frame data, this does not start with the API ID.
  588. * The reason for this is the API ID and Frame ID are common to all requests, whereas my definition of
  589. * frame data is only the API specific data.
  590. */
  591. virtual uint8_t getFrameData(uint8_t pos) = 0;
  592. /**
  593. * Returns the size of the api frame (not including frame id or api id or checksum).
  594. */
  595. virtual uint8_t getFrameDataLength() = 0;
  596. //void reset();
  597. protected:
  598. void setApiId(uint8_t apiId);
  599. private:
  600. uint8_t _apiId;
  601. uint8_t _frameId;
  602. };
  603. // TODO add reset/clear method since responses are often reused
  604. /**
  605. * Primary interface for communicating with an XBee Radio.
  606. * This class provides methods for sending and receiving packets with an XBee radio via the serial port.
  607. * The XBee radio must be configured in API (packet) mode (AP=2)
  608. * in order to use this software.
  609. * <p/>
  610. * Since this code is designed to run on a microcontroller, with only one thread, you are responsible for reading the
  611. * data off the serial buffer in a timely manner. This involves a call to a variant of readPacket(...).
  612. * If your serial port is receiving data faster than you are reading, you can expect to lose packets.
  613. * Arduino only has a 128 byte serial buffer so it can easily overflow if two or more packets arrive
  614. * without a call to readPacket(...)
  615. * <p/>
  616. * In order to conserve resources, this class only supports storing one response packet in memory at a time.
  617. * This means that you must fully consume the packet prior to calling readPacket(...), because calling
  618. * readPacket(...) overwrites the previous response.
  619. * <p/>
  620. * This class creates an array of size MAX_FRAME_DATA_SIZE for storing the response packet. You may want
  621. * to adjust this value to conserve memory.
  622. *
  623. * \author Andrew Rapp
  624. */
  625. class XBee {
  626. public:
  627. XBee();
  628. /**
  629. * Reads all available serial bytes until a packet is parsed, an error occurs, or the buffer is empty.
  630. * You may call <i>xbee</i>.getResponse().isAvailable() after calling this method to determine if
  631. * a packet is ready, or <i>xbee</i>.getResponse().isError() to determine if
  632. * a error occurred.
  633. * <p/>
  634. * This method should always return quickly since it does not wait for serial data to arrive.
  635. * You will want to use this method if you are doing other timely stuff in your loop, where
  636. * a delay would cause problems.
  637. * NOTE: calling this method resets the current response, so make sure you first consume the
  638. * current response
  639. */
  640. void readPacket();
  641. /**
  642. * Waits a maximum of <i>timeout</i> milliseconds for a response packet before timing out; returns true if packet is read.
  643. * Returns false if timeout or error occurs.
  644. */
  645. bool readPacket(int timeout);
  646. /**
  647. * Reads until a packet is received or an error occurs.
  648. * Caution: use this carefully since if you don't get a response, your Arduino code will hang on this
  649. * call forever!! often it's better to use a timeout: readPacket(int)
  650. */
  651. void readPacketUntilAvailable();
  652. /**
  653. * Starts the serial connection on the specified serial port
  654. */
  655. void begin(Stream &serial);
  656. void getResponse(XBeeResponse &response);
  657. /**
  658. * Returns a reference to the current response
  659. * Note: once readPacket is called again this response will be overwritten!
  660. */
  661. XBeeResponse& getResponse();
  662. /**
  663. * Sends a XBeeRequest (TX packet) out the serial port
  664. */
  665. void send(XBeeRequest &request);
  666. //uint8_t sendAndWaitForResponse(XBeeRequest &request, int timeout);
  667. /**
  668. * Returns a sequential frame id between 1 and 255
  669. */
  670. uint8_t getNextFrameId();
  671. /**
  672. * Specify the serial port. Only relevant for Arduinos that support multiple serial ports (e.g. Mega)
  673. */
  674. void setSerial(Stream &serial);
  675. private:
  676. bool available();
  677. uint8_t read();
  678. void flush();
  679. void write(uint8_t val);
  680. void sendByte(uint8_t b, bool escape);
  681. void resetResponse();
  682. XBeeResponse _response;
  683. bool _escape;
  684. // current packet position for response. just a state variable for packet parsing and has no relevance for the response otherwise
  685. uint8_t _pos;
  686. // last byte read
  687. uint8_t b;
  688. uint8_t _checksumTotal;
  689. uint8_t _nextFrameId;
  690. // buffer for incoming RX packets. holds only the api specific frame data, starting after the api id byte and prior to checksum
  691. uint8_t _responseFrameData[MAX_FRAME_DATA_SIZE];
  692. Stream* _serial;
  693. };
  694. /**
  695. * All TX packets that support payloads extend this class
  696. */
  697. class PayloadRequest : public XBeeRequest {
  698. public:
  699. PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, uint8_t payloadLength);
  700. /**
  701. * Returns the payload of the packet, if not null
  702. */
  703. uint8_t* getPayload();
  704. /**
  705. * Sets the payload array
  706. */
  707. void setPayload(uint8_t* payloadPtr);
  708. /**
  709. * Returns the length of the payload array, as specified by the user.
  710. */
  711. uint8_t getPayloadLength();
  712. /**
  713. * Sets the length of the payload to include in the request. For example if the payload array
  714. * is 50 bytes and you only want the first 10 to be included in the packet, set the length to 10.
  715. * Length must be <= to the array length.
  716. */
  717. void setPayloadLength(uint8_t payloadLength);
  718. private:
  719. uint8_t* _payloadPtr;
  720. uint8_t _payloadLength;
  721. };
  722. #ifdef SERIES_1
  723. /**
  724. * Represents a Series 1 TX packet that corresponds to Api Id: TX_16_REQUEST
  725. * <p/>
  726. * Be careful not to send a data array larger than the max packet size of your radio.
  727. * This class does not perform any validation of packet size and there will be no indication
  728. * if the packet is too large, other than you will not get a TX Status response.
  729. * The datasheet says 100 bytes is the maximum, although that could change in future firmware.
  730. */
  731. class Tx16Request : public PayloadRequest {
  732. public:
  733. Tx16Request(uint16_t addr16, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
  734. /**
  735. * Creates a Unicast Tx16Request with the ACK option and DEFAULT_FRAME_ID
  736. */
  737. Tx16Request(uint16_t addr16, uint8_t *payload, uint8_t payloadLength);
  738. /**
  739. * Creates a default instance of this class. At a minimum you must specify
  740. * a payload, payload length and a destination address before sending this request.
  741. */
  742. Tx16Request();
  743. uint16_t getAddress16();
  744. void setAddress16(uint16_t addr16);
  745. uint8_t getOption();
  746. void setOption(uint8_t option);
  747. uint8_t getFrameData(uint8_t pos);
  748. uint8_t getFrameDataLength();
  749. protected:
  750. private:
  751. uint16_t _addr16;
  752. uint8_t _option;
  753. };
  754. /**
  755. * Represents a Series 1 TX packet that corresponds to Api Id: TX_64_REQUEST
  756. *
  757. * Be careful not to send a data array larger than the max packet size of your radio.
  758. * This class does not perform any validation of packet size and there will be no indication
  759. * if the packet is too large, other than you will not get a TX Status response.
  760. * The datasheet says 100 bytes is the maximum, although that could change in future firmware.
  761. */
  762. class Tx64Request : public PayloadRequest {
  763. public:
  764. Tx64Request(XBeeAddress64 &addr64, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
  765. /**
  766. * Creates a unicast Tx64Request with the ACK option and DEFAULT_FRAME_ID
  767. */
  768. Tx64Request(XBeeAddress64 &addr64, uint8_t *payload, uint8_t payloadLength);
  769. /**
  770. * Creates a default instance of this class. At a minimum you must specify
  771. * a payload, payload length and a destination address before sending this request.
  772. */
  773. Tx64Request();
  774. XBeeAddress64& getAddress64();
  775. void setAddress64(XBeeAddress64& addr64);
  776. // TODO move option to superclass
  777. uint8_t getOption();
  778. void setOption(uint8_t option);
  779. uint8_t getFrameData(uint8_t pos);
  780. uint8_t getFrameDataLength();
  781. private:
  782. XBeeAddress64 _addr64;
  783. uint8_t _option;
  784. };
  785. #endif
  786. #ifdef SERIES_2
  787. /**
  788. * Represents a Series 2 TX packet that corresponds to Api Id: ZB_TX_REQUEST
  789. *
  790. * Be careful not to send a data array larger than the max packet size of your radio.
  791. * This class does not perform any validation of packet size and there will be no indication
  792. * if the packet is too large, other than you will not get a TX Status response.
  793. * The datasheet says 72 bytes is the maximum for ZNet firmware and ZB Pro firmware provides
  794. * the ATNP command to get the max supported payload size. This command is useful since the
  795. * maximum payload size varies according to certain settings, such as encryption.
  796. * ZB Pro firmware provides a PAYLOAD_TOO_LARGE that is returned if payload size
  797. * exceeds the maximum.
  798. */
  799. class ZBTxRequest : public PayloadRequest {
  800. public:
  801. /**
  802. * Creates a unicast ZBTxRequest with the ACK option and DEFAULT_FRAME_ID
  803. */
  804. ZBTxRequest(XBeeAddress64 &addr64, uint8_t *payload, uint8_t payloadLength);
  805. ZBTxRequest(XBeeAddress64 &addr64, uint16_t addr16, uint8_t broadcastRadius, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
  806. /**
  807. * Creates a default instance of this class. At a minimum you must specify
  808. * a payload, payload length and a destination address before sending this request.
  809. */
  810. ZBTxRequest();
  811. XBeeAddress64& getAddress64();
  812. uint16_t getAddress16();
  813. uint8_t getBroadcastRadius();
  814. uint8_t getOption();
  815. void setAddress64(XBeeAddress64& addr64);
  816. void setAddress16(uint16_t addr16);
  817. void setBroadcastRadius(uint8_t broadcastRadius);
  818. void setOption(uint8_t option);
  819. protected:
  820. // declare virtual functions
  821. uint8_t getFrameData(uint8_t pos);
  822. uint8_t getFrameDataLength();
  823. private:
  824. XBeeAddress64 _addr64;
  825. uint16_t _addr16;
  826. uint8_t _broadcastRadius;
  827. uint8_t _option;
  828. };
  829. #endif
  830. /**
  831. * Represents an AT Command TX packet
  832. * The command is used to configure the serially connected XBee radio
  833. */
  834. class AtCommandRequest : public XBeeRequest {
  835. public:
  836. AtCommandRequest();
  837. AtCommandRequest(uint8_t *command);
  838. AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
  839. uint8_t getFrameData(uint8_t pos);
  840. uint8_t getFrameDataLength();
  841. uint8_t* getCommand();
  842. void setCommand(uint8_t* command);
  843. uint8_t* getCommandValue();
  844. void setCommandValue(uint8_t* command);
  845. uint8_t getCommandValueLength();
  846. void setCommandValueLength(uint8_t length);
  847. /**
  848. * Clears the optional commandValue and commandValueLength so that a query may be sent
  849. */
  850. void clearCommandValue();
  851. //void reset();
  852. private:
  853. uint8_t *_command;
  854. uint8_t *_commandValue;
  855. uint8_t _commandValueLength;
  856. };
  857. /**
  858. * Represents an Remote AT Command TX packet
  859. * The command is used to configure a remote XBee radio
  860. */
  861. class RemoteAtCommandRequest : public AtCommandRequest {
  862. public:
  863. RemoteAtCommandRequest();
  864. /**
  865. * Creates a RemoteAtCommandRequest with 16-bit address to set a command.
  866. * 64-bit address defaults to broadcast and applyChanges is true.
  867. */
  868. RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
  869. /**
  870. * Creates a RemoteAtCommandRequest with 16-bit address to query a command.
  871. * 64-bit address defaults to broadcast and applyChanges is true.
  872. */
  873. RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command);
  874. /**
  875. * Creates a RemoteAtCommandRequest with 64-bit address to set a command.
  876. * 16-bit address defaults to broadcast and applyChanges is true.
  877. */
  878. RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
  879. /**
  880. * Creates a RemoteAtCommandRequest with 16-bit address to query a command.
  881. * 16-bit address defaults to broadcast and applyChanges is true.
  882. */
  883. RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command);
  884. uint16_t getRemoteAddress16();
  885. void setRemoteAddress16(uint16_t remoteAddress16);
  886. XBeeAddress64& getRemoteAddress64();
  887. void setRemoteAddress64(XBeeAddress64 &remoteAddress64);
  888. bool getApplyChanges();
  889. void setApplyChanges(bool applyChanges);
  890. uint8_t getFrameData(uint8_t pos);
  891. uint8_t getFrameDataLength();
  892. static XBeeAddress64 broadcastAddress64;
  893. // static uint16_t broadcast16Address;
  894. private:
  895. XBeeAddress64 _remoteAddress64;
  896. uint16_t _remoteAddress16;
  897. bool _applyChanges;
  898. };
  899. #endif //XBee_h