BayEOS-Arduino  1.8.0_0.0.4
XBee.h
1 
22 #ifndef XBee_h
23 #define XBee_h
24 
25 #if defined(ARDUINO) && ARDUINO >= 100
26  #include "Arduino.h"
27 #else
28  #include "WProgram.h"
29 #endif
30 
31 #include <inttypes.h>
32 #include <BayEOS.h>
33 
34 #define SERIES_1
35 #define SERIES_2
36 
37 // set to ATAP value of XBee. AP=2 is recommended
38 #define ATAP 2
39 
40 #define START_BYTE 0x7e
41 #define ESCAPE 0x7d
42 #define XON 0x11
43 #define XOFF 0x13
44 
45 // This value determines the size of the byte array for receiving RX packets
46 // Most users won't be dealing with packets this large so you can adjust this
47 // value to reduce memory consumption. But, remember that
48 // if a RX packet exceeds this size, it cannot be parsed!
49 
50 // 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
51 #define MAX_FRAME_DATA_SIZE 110
52 
53 #define BROADCAST_ADDRESS 0xffff
54 #define ZB_BROADCAST_ADDRESS 0xfffe
55 
56 // 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)
57 #define ZB_TX_API_LENGTH 12
58 #define TX_16_API_LENGTH 3
59 #define TX_64_API_LENGTH 9
60 #define AT_COMMAND_API_LENGTH 2
61 #define REMOTE_AT_COMMAND_API_LENGTH 13
62 // start/length(2)/api/frameid/checksum bytes
63 #define PACKET_OVERHEAD_LENGTH 6
64 // api is always the third byte in packet
65 #define API_ID_INDEX 3
66 
67 // frame position of rssi byte
68 #define RX_16_RSSI_OFFSET 2
69 #define RX_64_RSSI_OFFSET 8
70 
71 #define DEFAULT_FRAME_ID 1
72 #define NO_RESPONSE_FRAME_ID 0
73 
74 // TODO put in tx16 class
75 #define ACK_OPTION 0
76 #define DISABLE_ACK_OPTION 1
77 #define BROADCAST_OPTION 4
78 
79 // RX options
80 #define ZB_PACKET_ACKNOWLEDGED 0x01
81 #define ZB_BROADCAST_PACKET 0x02
82 
83 // not everything is implemented!
87 #define TX_64_REQUEST 0x0
88 #define TX_16_REQUEST 0x1
89 #define AT_COMMAND_REQUEST 0x08
90 #define AT_COMMAND_QUEUE_REQUEST 0x09
91 #define REMOTE_AT_REQUEST 0x17
92 #define ZB_TX_REQUEST 0x10
93 #define ZB_EXPLICIT_TX_REQUEST 0x11
94 #define RX_64_RESPONSE 0x80
95 #define RX_16_RESPONSE 0x81
96 #define RX_64_IO_RESPONSE 0x82
97 #define RX_16_IO_RESPONSE 0x83
98 #define AT_RESPONSE 0x88
99 #define TX_STATUS_RESPONSE 0x89
100 #define MODEM_STATUS_RESPONSE 0x8a
101 #define ZB_RX_RESPONSE 0x90
102 #define ZB_EXPLICIT_RX_RESPONSE 0x91
103 #define ZB_TX_STATUS_RESPONSE 0x8b
104 #define ZB_IO_SAMPLE_RESPONSE 0x92
105 #define ZB_IO_NODE_IDENTIFIER_RESPONSE 0x95
106 #define AT_COMMAND_RESPONSE 0x88
107 #define REMOTE_AT_COMMAND_RESPONSE 0x97
108 
109 
113 #define SUCCESS 0x0
114 #define CCA_FAILURE 0x2
115 #define INVALID_DESTINATION_ENDPOINT_SUCCESS 0x15
116 #define NETWORK_ACK_FAILURE 0x21
117 #define NOT_JOINED_TO_NETWORK 0x22
118 #define SELF_ADDRESSED 0x23
119 #define ADDRESS_NOT_FOUND 0x24
120 #define ROUTE_NOT_FOUND 0x25
121 #define PAYLOAD_TOO_LARGE 0x74
122 
123 // modem status
124 #define HARDWARE_RESET 0
125 #define WATCHDOG_TIMER_RESET 1
126 #define ASSOCIATED 2
127 #define DISASSOCIATED 3
128 #define SYNCHRONIZATION_LOST 4
129 #define COORDINATOR_REALIGNMENT 5
130 #define COORDINATOR_STARTED 6
131 
132 #define ZB_BROADCAST_RADIUS_MAX_HOPS 0
133 
134 #define ZB_TX_UNICAST 0
135 #define ZB_TX_BROADCAST 8
136 
137 #define AT_OK 0
138 #define AT_ERROR 1
139 #define AT_INVALID_COMMAND 2
140 #define AT_INVALID_PARAMETER 3
141 #define AT_NO_RESPONSE 4
142 
143 #define NO_ERROR 0
144 #define CHECKSUM_FAILURE 1
145 #define PACKET_EXCEEDS_BYTE_ARRAY_LENGTH 2
146 #define UNEXPECTED_START_BYTE 3
147 
155 public:
156  //static const int MODEM_STATUS = 0x8a;
160  XBeeResponse();
164  uint8_t getApiId();
165  void setApiId(uint8_t apiId);
169  uint8_t getMsbLength();
170  void setMsbLength(uint8_t msbLength);
174  uint8_t getLsbLength();
175  void setLsbLength(uint8_t lsbLength);
179  uint8_t getChecksum();
180  void setChecksum(uint8_t checksum);
185  uint8_t getFrameDataLength();
186  void setFrameData(uint8_t* frameDataPtr);
195  uint8_t* getFrameData();
196 
197  void setFrameLength(uint8_t frameLength);
198  // to support future 65535 byte packets I guess
202  uint16_t getPacketLength();
206  void reset();
210  void init();
211 #ifdef SERIES_2
212 
216  void getZBTxStatusResponse(XBeeResponse &response);
221  void getZBRxResponse(XBeeResponse &response);
226  void getZBRxIoSampleResponse(XBeeResponse &response);
227 #endif
228 #ifdef SERIES_1
229 
232  void getTxStatusResponse(XBeeResponse &response);
236  void getRx16Response(XBeeResponse &response);
240  void getRx64Response(XBeeResponse &response);
244  void getRx16IoSampleResponse(XBeeResponse &response);
248  void getRx64IoSampleResponse(XBeeResponse &response);
249 #endif
250 
253  void getAtCommandResponse(XBeeResponse &responses);
261  void getModemStatusResponse(XBeeResponse &response);
265  bool isAvailable();
266  void setAvailable(bool complete);
270  bool isError();
275  uint8_t getErrorCode();
276  void setErrorCode(uint8_t errorCode);
277 protected:
278  // pointer to frameData
279  uint8_t* _frameDataPtr;
280 private:
281  void setCommon(XBeeResponse &target);
282  uint8_t _apiId;
283  uint8_t _msbLength;
284  uint8_t _lsbLength;
285  uint8_t _checksum;
286  uint8_t _frameLength;
287  bool _complete;
288  uint8_t _errorCode;
289 };
290 
291 class XBeeAddress {
292 public:
293  XBeeAddress();
294 };
295 
299 class XBeeAddress64 : public XBeeAddress {
300 public:
301  XBeeAddress64(uint32_t msb, uint32_t lsb);
302  XBeeAddress64();
303  uint32_t getMsb();
304  uint32_t getLsb();
305  void setMsb(uint32_t msb);
306  void setLsb(uint32_t lsb);
307 private:
308  uint32_t _msb;
309  uint32_t _lsb;
310 };
311 
312 //class XBeeAddress16 : public XBeeAddress {
313 //public:
314 // XBeeAddress16(uint16_t addr);
315 // XBeeAddress16();
316 // uint16_t getAddress();
317 // void setAddress(uint16_t addr);
318 //private:
319 // uint16_t _addr;
320 //};
321 
326 public:
327  FrameIdResponse();
328  uint8_t getFrameId();
329 private:
330  uint8_t _frameId;
331 };
332 
336 class RxDataResponse : public XBeeResponse {
337 public:
338  RxDataResponse();
343  uint8_t getData(int index);
347  uint8_t* getData();
351  virtual uint8_t getDataLength() = 0;
355  virtual uint8_t getDataOffset() = 0;
356 };
357 
358 // getResponse to return the proper subclass:
359 // we maintain a pointer to each type of response, when a response is parsed, it is allocated only if NULL
360 // can we allocate an object in a function?
361 
362 #ifdef SERIES_2
363 
367  public:
369  uint16_t getRemoteAddress();
370  uint8_t getTxRetryCount();
371  uint8_t getDeliveryStatus();
372  uint8_t getDiscoveryStatus();
373  bool isSuccess();
374 };
375 
379 class ZBRxResponse : public RxDataResponse {
380 public:
381  ZBRxResponse();
382  XBeeAddress64& getRemoteAddress64();
383  uint16_t getRemoteAddress16();
384  uint8_t getOption();
385  uint8_t getDataLength();
386  // frame position where data starts
387  uint8_t getDataOffset();
388 private:
389  XBeeAddress64 _remoteAddress64;
390 };
391 
396 public:
398  bool containsAnalog();
399  bool containsDigital();
403  bool isAnalogEnabled(uint8_t pin);
407  bool isDigitalEnabled(uint8_t pin);
412  uint16_t getAnalog(uint8_t pin);
417  bool isDigitalOn(uint8_t pin);
418  uint8_t getDigitalMaskMsb();
419  uint8_t getDigitalMaskLsb();
420  uint8_t getAnalogMask();
421 };
422 
423 #endif
424 
425 #ifdef SERIES_1
426 
430  public:
432  uint8_t getStatus();
433  bool isSuccess();
434 };
435 
439 class RxResponse : public RxDataResponse {
440 public:
441  RxResponse();
442  // remember rssi is negative but this is unsigned byte so it's up to you to convert
443  uint8_t getRssi();
444  uint8_t getOption();
445  bool isAddressBroadcast();
446  bool isPanBroadcast();
447  uint8_t getDataLength();
448  uint8_t getDataOffset();
449  virtual uint8_t getRssiOffset() = 0;
450 };
451 
455 class Rx16Response : public RxResponse {
456 public:
457  Rx16Response();
458  uint8_t getRssiOffset();
459  uint16_t getRemoteAddress16();
460 protected:
461  uint16_t _remoteAddress;
462 };
463 
467 class Rx64Response : public RxResponse {
468 public:
469  Rx64Response();
470  uint8_t getRssiOffset();
471  XBeeAddress64& getRemoteAddress64();
472 private:
473  XBeeAddress64 _remoteAddress;
474 };
475 
480  public:
485  uint8_t getSampleSize();
486  bool containsAnalog();
487  bool containsDigital();
491  bool isAnalogEnabled(uint8_t pin);
495  bool isDigitalEnabled(uint8_t pin);
500  uint16_t getAnalog(uint8_t pin, uint8_t sample);
505  bool isDigitalOn(uint8_t pin, uint8_t sample);
506  uint8_t getSampleOffset();
507  private:
508 };
509 
511 public:
513  uint16_t getRemoteAddress16();
514  uint8_t getRssiOffset();
515 
516 };
517 
519 public:
521  XBeeAddress64& getRemoteAddress64();
522  uint8_t getRssiOffset();
523 private:
524  XBeeAddress64 _remoteAddress;
525 };
526 
527 #endif
528 
533 public:
535  uint8_t getStatus();
536 };
537 
542  public:
547  uint8_t* getCommand();
552  uint8_t getStatus();
557  uint8_t* getValue();
561  uint8_t getValueLength();
565  bool isOk();
566 };
567 
572  public:
577  uint8_t* getCommand();
582  uint8_t getStatus();
587  uint8_t* getValue();
591  uint8_t getValueLength();
595  uint16_t getRemoteAddress16();
599  XBeeAddress64& getRemoteAddress64();
603  bool isOk();
604  private:
605  XBeeAddress64 _remoteAddress64;
606 };
607 
608 
616 class XBeeRequest {
617 public:
622  XBeeRequest(uint8_t apiId, uint8_t frameId);
626  void setFrameId(uint8_t frameId);
630  uint8_t getFrameId();
634  uint8_t getApiId();
635  // setting = 0 makes this a pure virtual function, meaning the subclass must implement, like abstract in java
642  virtual uint8_t getFrameData(uint8_t pos) = 0;
646  virtual uint8_t getFrameDataLength() = 0;
647  //void reset();
648 protected:
649  void setApiId(uint8_t apiId);
650 private:
651  uint8_t _apiId;
652  uint8_t _frameId;
653 };
654 
655 
656 
657 // TODO add reset/clear method since responses are often reused
680 public:
681  XBeeInterface();
682 
683 
684  void readPacket();
689  bool readPacket(int timeout);
695  void readPacketUntilAvailable();
699  void getResponse(XBeeResponse &response);
704  XBeeResponse& getResponse();
708  void send(XBeeRequest &request);
709  //uint8_t sendAndWaitForResponse(XBeeRequest &request, int timeout);
714  uint16_t getPANID(void);
715 
716  uint8_t parseRX16(BayEOS &client, int rx_panid);
717 
718 
719  uint8_t getNextFrameId();
720  virtual void i_begin(long baud) = 0;
721  virtual int i_available() = 0;
722  virtual int read() = 0;
723  virtual void flush() = 0;
724  virtual size_t write(uint8_t val) = 0;
725 protected:
726  void sendByte(uint8_t b, bool escape);
727  void resetResponse();
728  XBeeResponse _response;
729  bool _escape;
730  // current packet position for response. just a state variable for packet parsing and has no relevance for the response otherwise
731  uint8_t _pos;
732  // last byte read
733  uint8_t b;
734  uint8_t _checksumTotal;
735  uint8_t _nextFrameId;
736  // buffer for incoming RX packets. holds only the api specific frame data, starting after the api id byte and prior to checksum
737  uint8_t _responseFrameData[MAX_FRAME_DATA_SIZE];
738 };
739 
740 class XBee : public XBeeInterface {
741 private:
742  HardwareSerial* _serial;
743 
744 public:
745  XBee():XBeeInterface()
746  {
747  _serial = &Serial;
748  }
749 
750  void setSerial(HardwareSerial &serial){
751  _serial = &serial;
752  }
753 
754  int i_available(void){
755  return _serial->available();
756  }
757  void begin(long baud){
758  _serial->begin(baud);
759  }
760  void i_begin(long baud){
761  _serial->begin(baud);
762  }
763  void flush(void){
764  _serial->flush();
765  }
766  int read(void){
767  return _serial->read();
768  }
769  size_t write(uint8_t c){
770  return _serial->write(c);
771  }
772 
773 
774 };
778 class PayloadRequest : public XBeeRequest {
779 public:
780  PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, uint8_t payloadLength);
784  uint8_t* getPayload();
788  void setPayload(uint8_t* payloadPtr);
792  uint8_t getPayloadLength();
798  void setPayloadLength(uint8_t payloadLength);
799 private:
800  uint8_t* _payloadPtr;
801  uint8_t _payloadLength;
802 };
803 
804 #ifdef SERIES_1
805 
814 class Tx16Request : public PayloadRequest {
815 public:
816  Tx16Request(uint16_t addr16, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
820  Tx16Request(uint16_t addr16, uint8_t *payload, uint8_t payloadLength);
825  Tx16Request();
826  uint16_t getAddress16();
827  void setAddress16(uint16_t addr16);
828  uint8_t getOption();
829  void setOption(uint8_t option);
830  uint8_t getFrameData(uint8_t pos);
831  uint8_t getFrameDataLength();
832 protected:
833 private:
834  uint16_t _addr16;
835  uint8_t _option;
836 };
837 
846 class Tx64Request : public PayloadRequest {
847 public:
848  Tx64Request(XBeeAddress64 &addr64, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
852  Tx64Request(XBeeAddress64 &addr64, uint8_t *payload, uint8_t payloadLength);
857  Tx64Request();
858  XBeeAddress64& getAddress64();
859  void setAddress64(XBeeAddress64& addr64);
860  // TODO move option to superclass
861  uint8_t getOption();
862  void setOption(uint8_t option);
863  uint8_t getFrameData(uint8_t pos);
864  uint8_t getFrameDataLength();
865 private:
866  XBeeAddress64 _addr64;
867  uint8_t _option;
868 };
869 
870 #endif
871 
872 
873 #ifdef SERIES_2
874 
887 class ZBTxRequest : public PayloadRequest {
888 public:
892  ZBTxRequest(XBeeAddress64 &addr64, uint8_t *payload, uint8_t payloadLength);
893  ZBTxRequest(XBeeAddress64 &addr64, uint16_t addr16, uint8_t broadcastRadius, uint8_t option, uint8_t *payload, uint8_t payloadLength, uint8_t frameId);
898  ZBTxRequest();
899  XBeeAddress64& getAddress64();
900  uint16_t getAddress16();
901  uint8_t getBroadcastRadius();
902  uint8_t getOption();
903  void setAddress64(XBeeAddress64& addr64);
904  void setAddress16(uint16_t addr16);
905  void setBroadcastRadius(uint8_t broadcastRadius);
906  void setOption(uint8_t option);
907 protected:
908  // declare virtual functions
909  uint8_t getFrameData(uint8_t pos);
910  uint8_t getFrameDataLength();
911 private:
912  XBeeAddress64 _addr64;
913  uint16_t _addr16;
914  uint8_t _broadcastRadius;
915  uint8_t _option;
916 };
917 
918 #endif
919 
925 public:
927  AtCommandRequest(uint8_t *command);
928  AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
929  uint8_t getFrameData(uint8_t pos);
930  uint8_t getFrameDataLength();
931  uint8_t* getCommand();
932  void setCommand(uint8_t* command);
933  uint8_t* getCommandValue();
934  void setCommandValue(uint8_t* command);
935  uint8_t getCommandValueLength();
936  void setCommandValueLength(uint8_t length);
940  void clearCommandValue();
941  //void reset();
942 private:
943  uint8_t *_command;
944  uint8_t *_commandValue;
945  uint8_t _commandValueLength;
946 };
947 
953 public:
959  RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
964  RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command);
969  RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength);
974  RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command);
975  uint16_t getRemoteAddress16();
976  void setRemoteAddress16(uint16_t remoteAddress16);
977  XBeeAddress64& getRemoteAddress64();
978  void setRemoteAddress64(XBeeAddress64 &remoteAddress64);
979  bool getApplyChanges();
980  void setApplyChanges(bool applyChanges);
981  uint8_t getFrameData(uint8_t pos);
982  uint8_t getFrameDataLength();
983  static XBeeAddress64 broadcastAddress64;
984 // static uint16_t broadcast16Address;
985 private:
986  XBeeAddress64 _remoteAddress64;
987  uint16_t _remoteAddress16;
988  bool _applyChanges;
989 };
990 
991 
992 
993 #endif //XBee_h
uint8_t getApiId()
Definition: XBee.cpp:36
uint8_t getChecksum()
Definition: XBee.cpp:60
void getZBRxIoSampleResponse(XBeeResponse &response)
Definition: XBee.cpp:257
void init()
Definition: XBee.cpp:735
uint8_t getLsbLength()
Definition: XBee.cpp:52
void getRx16IoSampleResponse(XBeeResponse &response)
Definition: XBee.cpp:486
void getRx64Response(XBeeResponse &response)
Definition: XBee.cpp:580
uint8_t getErrorCode()
Definition: XBee.cpp:88
void getModemStatusResponse(XBeeResponse &response)
Definition: XBee.cpp:675
uint16_t getPacketLength()
Definition: XBee.cpp:723
bool isAvailable()
Definition: XBee.cpp:76
bool isError()
Definition: XBee.cpp:84
XBeeResponse()
Definition: XBee.cpp:32
void getRx16Response(XBeeResponse &response)
Definition: XBee.cpp:565
void getZBTxStatusResponse(XBeeResponse &response)
Definition: XBee.cpp:133
Definition: BayEOS.h:160
uint8_t getFrameDataLength()
Definition: XBee.cpp:68
uint8_t getMsbLength()
Definition: XBee.cpp:44
void getRemoteAtCommandResponse(XBeeResponse &response)
Definition: XBee.cpp:632
uint8_t * getFrameData()
Definition: XBee.cpp:727
Definition: XBee.h:740
void getTxStatusResponse(XBeeResponse &response)
Definition: XBee.cpp:528
void getAtCommandResponse(XBeeResponse &responses)
Definition: XBee.cpp:714
void getZBRxResponse(XBeeResponse &response)
Definition: XBee.cpp:167
void getRx64IoSampleResponse(XBeeResponse &response)
Definition: XBee.cpp:506
void reset()
Definition: XBee.cpp:741