24 #if defined(ARDUINO) && ARDUINO >= 100 30 #include "HardwareSerial.h" 40 void XBeeResponse::setApiId(uint8_t apiId) {
48 void XBeeResponse::setMsbLength(uint8_t msbLength) {
49 _msbLength = msbLength;
56 void XBeeResponse::setLsbLength(uint8_t lsbLength) {
57 _lsbLength = lsbLength;
64 void XBeeResponse::setChecksum(uint8_t checksum) {
72 void XBeeResponse::setFrameLength(uint8_t frameLength) {
73 _frameLength = frameLength;
80 void XBeeResponse::setAvailable(
bool complete) {
85 return _errorCode > 0;
92 void XBeeResponse::setErrorCode(uint8_t errorCode) {
93 _errorCode = errorCode;
113 uint16_t ZBTxStatusResponse::getRemoteAddress() {
117 uint8_t ZBTxStatusResponse::getTxRetryCount() {
121 uint8_t ZBTxStatusResponse::getDeliveryStatus() {
125 uint8_t ZBTxStatusResponse::getDiscoveryStatus() {
129 bool ZBTxStatusResponse::isSuccess() {
130 return getDeliveryStatus() == SUCCESS;
139 setCommon(zbXBeeResponse);
146 uint16_t ZBRxResponse::getRemoteAddress16() {
150 uint8_t ZBRxResponse::getOption() {
164 return _remoteAddress64;
175 setCommon(rxResponse);
182 ZBRxIoSampleResponse::ZBRxIoSampleResponse() :
ZBRxResponse() {
187 uint8_t ZBRxIoSampleResponse::getDigitalMaskMsb() {
191 uint8_t ZBRxIoSampleResponse::getDigitalMaskLsb() {
195 uint8_t ZBRxIoSampleResponse::getAnalogMask() {
199 bool ZBRxIoSampleResponse::containsAnalog() {
200 return getAnalogMask() > 0;
203 bool ZBRxIoSampleResponse::containsDigital() {
204 return getDigitalMaskMsb() > 0 || getDigitalMaskLsb() > 0;
208 return ((getAnalogMask() >> pin) & 1) == 1;
214 return ((getDigitalMaskLsb() >> pin) & 1) == 1;
216 return ((getDigitalMaskMsb() >> (pin - 8)) & 1) == 1;
224 if (containsDigital()) {
232 for (
int i = 0; i < pin; i++) {
233 if (isAnalogEnabled(i)) {
277 uint16_t Rx16Response::getRemoteAddress16() {
282 return _remoteAddress;
293 RxIoSampleBaseResponse::RxIoSampleBaseResponse() :
RxResponse() {
297 uint8_t RxIoSampleBaseResponse::getSampleOffset() {
299 return getRssiOffset() + 2;
307 bool RxIoSampleBaseResponse::containsAnalog() {
308 return (
getFrameData()[getSampleOffset() + 1] & 0x7e) > 0;
311 bool RxIoSampleBaseResponse::containsDigital() {
320 return (((
getFrameData()[getSampleOffset() + 1] >> (pin + 1)) & 1) == 1);
325 return ((
getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
327 return (
getFrameData()[getSampleOffset() + 3] & 1) == 1;
428 if (containsDigital()) {
430 start+=2*(sample + 1);
436 for (
int i = 0; i <= 5; i++) {
437 if (isAnalogEnabled(i)) {
446 for (
int i = 0; i < pin; i++) {
447 if (isAnalogEnabled(i)) {
452 start+= sample * spacing;
458 return (uint16_t)((
getFrameData()[getSampleOffset() + start] << 8) +
getFrameData()[getSampleOffset() + start + 1]);
463 return ((
getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
465 return (
getFrameData()[getSampleOffset() + 3] & 1) == 1;
478 uint16_t Rx16IoSampleResponse::getRemoteAddress16() {
482 uint8_t Rx16IoSampleResponse::getRssiOffset() {
499 return _remoteAddress;
502 uint8_t Rx64IoSampleResponse::getRssiOffset() {
520 uint8_t TxStatusResponse::getStatus() {
524 bool TxStatusResponse::isSuccess() {
525 return getStatus() == SUCCESS;
534 setCommon(txResponse);
537 uint8_t RxResponse::getRssi() {
541 uint8_t RxResponse::getOption() {
545 bool RxResponse::isAddressBroadcast() {
546 return (getOption() & 2) == 2;
549 bool RxResponse::isPanBroadcast() {
550 return (getOption() & 4) == 4;
558 return getRssiOffset() + 2;
561 uint8_t Rx16Response::getRssiOffset() {
562 return RX_16_RSSI_OFFSET;
571 setCommon(rx16Response);
576 uint8_t Rx64Response::getRssiOffset() {
577 return RX_64_RSSI_OFFSET;
586 setCommon(rx64Response);
608 return getStatus() == AT_OK;
616 if (getValueLength() > 0) {
629 return _remoteAddress64;
658 FrameIdResponse::FrameIdResponse() {
662 uint8_t FrameIdResponse::getFrameId() {
667 ModemStatusResponse::ModemStatusResponse() {
671 uint8_t ModemStatusResponse::getStatus() {
681 setCommon(modemStatusResponse);
685 AtCommandResponse::AtCommandResponse() {
702 if (getValueLength() > 0) {
711 return getStatus() == AT_OK;
720 setCommon(atCommandResponse);
724 return ((_msbLength << 8) & 0xff) + (_lsbLength & 0xff);
728 return _frameDataPtr;
731 void XBeeResponse::setFrameData(uint8_t* frameDataPtr) {
732 _frameDataPtr = frameDataPtr;
737 _errorCode = NO_ERROR;
749 _errorCode = NO_ERROR;
751 for (
int i = 0; i < MAX_FRAME_DATA_SIZE; i++) {
777 void XBeeRequest::setApiId(uint8_t apiId) {
794 PayloadRequest::PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, uint8_t payloadLength) :
XBeeRequest(apiId, frameId) {
795 _payloadPtr = payload;
796 _payloadLength = payloadLength;
804 _payloadPtr = payload;
808 return _payloadLength;
812 _payloadLength = payloadLength;
816 XBeeAddress::XBeeAddress() {
824 XBeeAddress64::XBeeAddress64(uint32_t msb, uint32_t lsb) :
XBeeAddress() {
829 uint32_t XBeeAddress64::getMsb() {
833 void XBeeAddress64::setMsb(uint32_t msb) {
837 uint32_t XBeeAddress64::getLsb() {
841 void XBeeAddress64::setLsb(uint32_t lsb) {
855 _broadcastRadius = broadcastRadius;
861 _addr16 = ZB_BROADCAST_ADDRESS;
862 _broadcastRadius = ZB_BROADCAST_RADIUS_MAX_HOPS;
863 _option = ZB_TX_UNICAST;
868 return (_addr64.getMsb() >> 24) & 0xff;
869 }
else if (pos == 1) {
870 return (_addr64.getMsb() >> 16) & 0xff;
871 }
else if (pos == 2) {
872 return (_addr64.getMsb() >> 8) & 0xff;
873 }
else if (pos == 3) {
874 return _addr64.getMsb() & 0xff;
875 }
else if (pos == 4) {
876 return (_addr64.getLsb() >> 24) & 0xff;
877 }
else if (pos == 5) {
878 return (_addr64.getLsb() >> 16) & 0xff;
879 }
else if (pos == 6) {
880 return (_addr64.getLsb() >> 8) & 0xff;
881 }
else if (pos == 7) {
882 return _addr64.getLsb() & 0xff;
883 }
else if (pos == 8) {
884 return (_addr16 >> 8) & 0xff;
885 }
else if (pos == 9) {
886 return _addr16 & 0xff;
887 }
else if (pos == 10) {
888 return _broadcastRadius;
889 }
else if (pos == 11) {
904 uint16_t ZBTxRequest::getAddress16() {
908 uint8_t ZBTxRequest::getBroadcastRadius() {
909 return _broadcastRadius;
912 uint8_t ZBTxRequest::getOption() {
920 void ZBTxRequest::setAddress16(uint16_t addr16) {
924 void ZBTxRequest::setBroadcastRadius(uint8_t broadcastRadius) {
925 _broadcastRadius = broadcastRadius;
928 void ZBTxRequest::setOption(uint8_t option) {
947 _option = ACK_OPTION;
953 return (_addr16 >> 8) & 0xff;
954 }
else if (pos == 1) {
955 return _addr16 & 0xff;
956 }
else if (pos == 2) {
967 uint16_t Tx16Request::getAddress16() {
971 void Tx16Request::setAddress16(uint16_t addr16) {
975 uint8_t Tx16Request::getOption() {
979 void Tx16Request::setOption(uint8_t option) {
994 _option = ACK_OPTION;
1000 return (_addr64.getMsb() >> 24) & 0xff;
1001 }
else if (pos == 1) {
1002 return (_addr64.getMsb() >> 16) & 0xff;
1003 }
else if (pos == 2) {
1004 return (_addr64.getMsb() >> 8) & 0xff;
1005 }
else if (pos == 3) {
1006 return _addr64.getMsb() & 0xff;
1007 }
else if (pos == 4) {
1008 return (_addr64.getLsb() >> 24) & 0xff;
1009 }
else if (pos == 5) {
1010 return (_addr64.getLsb() >> 16) & 0xff;
1011 }
else if (pos == 6) {
1012 return(_addr64.getLsb() >> 8) & 0xff;
1013 }
else if (pos == 7) {
1014 return _addr64.getLsb() & 0xff;
1015 }
else if (pos == 8) {
1034 uint8_t Tx64Request::getOption() {
1038 void Tx64Request::setOption(uint8_t option) {
1044 AtCommandRequest::AtCommandRequest() :
XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
1046 clearCommandValue();
1049 AtCommandRequest::AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) :
XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
1051 _commandValue = commandValue;
1052 _commandValueLength = commandValueLength;
1055 AtCommandRequest::AtCommandRequest(uint8_t *command) :
XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
1057 clearCommandValue();
1060 uint8_t* AtCommandRequest::getCommand() {
1064 uint8_t* AtCommandRequest::getCommandValue() {
1065 return _commandValue;
1068 uint8_t AtCommandRequest::getCommandValueLength() {
1069 return _commandValueLength;
1072 void AtCommandRequest::setCommand(uint8_t* command) {
1076 void AtCommandRequest::setCommandValue(uint8_t* value) {
1077 _commandValue = value;
1080 void AtCommandRequest::setCommandValueLength(uint8_t length) {
1081 _commandValueLength = length;
1088 }
else if (pos == 1) {
1091 return _commandValue[pos - AT_COMMAND_API_LENGTH];
1096 _commandValue = NULL;
1097 _commandValueLength = 0;
1106 return AT_COMMAND_API_LENGTH + _commandValueLength;
1111 RemoteAtCommandRequest::RemoteAtCommandRequest() :
AtCommandRequest(NULL, NULL, 0) {
1112 _remoteAddress16 = 0;
1113 _applyChanges =
false;
1114 setApiId(REMOTE_AT_REQUEST);
1117 RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) :
AtCommandRequest(command, commandValue, commandValueLength) {
1118 _remoteAddress64 = broadcastAddress64;
1119 _remoteAddress16 = remoteAddress16;
1120 _applyChanges =
true;
1121 setApiId(REMOTE_AT_REQUEST);
1124 RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command) :
AtCommandRequest(command, NULL, 0) {
1125 _remoteAddress64 = broadcastAddress64;
1126 _remoteAddress16 = remoteAddress16;
1127 _applyChanges =
false;
1128 setApiId(REMOTE_AT_REQUEST);
1131 RemoteAtCommandRequest::RemoteAtCommandRequest(
XBeeAddress64 &remoteAddress64, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) :
AtCommandRequest(command, commandValue, commandValueLength) {
1132 _remoteAddress64 = remoteAddress64;
1134 _remoteAddress16 = ZB_BROADCAST_ADDRESS;
1135 _applyChanges =
true;
1136 setApiId(REMOTE_AT_REQUEST);
1140 _remoteAddress64 = remoteAddress64;
1141 _remoteAddress16 = ZB_BROADCAST_ADDRESS;
1142 _applyChanges =
false;
1143 setApiId(REMOTE_AT_REQUEST);
1146 uint16_t RemoteAtCommandRequest::getRemoteAddress16() {
1147 return _remoteAddress16;
1150 void RemoteAtCommandRequest::setRemoteAddress16(uint16_t remoteAddress16) {
1151 _remoteAddress16 = remoteAddress16;
1154 XBeeAddress64& RemoteAtCommandRequest::getRemoteAddress64() {
1155 return _remoteAddress64;
1158 void RemoteAtCommandRequest::setRemoteAddress64(
XBeeAddress64 &remoteAddress64) {
1159 _remoteAddress64 = remoteAddress64;
1162 bool RemoteAtCommandRequest::getApplyChanges() {
1163 return _applyChanges;
1166 void RemoteAtCommandRequest::setApplyChanges(
bool applyChanges) {
1167 _applyChanges = applyChanges;
1173 return (_remoteAddress64.getMsb() >> 24) & 0xff;
1174 }
else if (pos == 1) {
1175 return (_remoteAddress64.getMsb() >> 16) & 0xff;
1176 }
else if (pos == 2) {
1177 return (_remoteAddress64.getMsb() >> 8) & 0xff;
1178 }
else if (pos == 3) {
1179 return _remoteAddress64.getMsb() & 0xff;
1180 }
else if (pos == 4) {
1181 return (_remoteAddress64.getLsb() >> 24) & 0xff;
1182 }
else if (pos == 5) {
1183 return (_remoteAddress64.getLsb() >> 16) & 0xff;
1184 }
else if (pos == 6) {
1185 return(_remoteAddress64.getLsb() >> 8) & 0xff;
1186 }
else if (pos == 7) {
1187 return _remoteAddress64.getLsb() & 0xff;
1188 }
else if (pos == 8) {
1189 return (_remoteAddress16 >> 8) & 0xff;
1190 }
else if (pos == 9) {
1191 return _remoteAddress16 & 0xff;
1192 }
else if (pos == 10) {
1193 return _applyChanges ? 2: 0;
1194 }
else if (pos == 11) {
1195 return getCommand()[0];
1196 }
else if (pos == 12) {
1197 return getCommand()[1];
1199 return getCommandValue()[pos - REMOTE_AT_COMMAND_API_LENGTH];
1204 return REMOTE_AT_COMMAND_API_LENGTH + getCommandValueLength();
1213 XBeeInterface::XBeeInterface():_response(
XBeeResponse())
1221 _response.setFrameData(_responseFrameData);
1225 void XBeeInterface::resetResponse() {
1232 uint8_t XBeeInterface::getNextFrameId() {
1236 if (_nextFrameId == 0) {
1241 return _nextFrameId;
1252 response.setMsbLength(_response.getMsbLength());
1253 response.setLsbLength(_response.getLsbLength());
1254 response.setApiId(_response.getApiId());
1255 response.setFrameLength(_response.getFrameDataLength());
1257 response.setFrameData(_response.getFrameData());
1261 while (!(getResponse().isAvailable() || getResponse().isError())) {
1267 bool XBeeInterface::readPacket(
int timeout) {
1273 unsigned long start = millis();
1275 while (
int((millis() - start)) < timeout) {
1279 if (getResponse().isAvailable()) {
1281 }
else if (getResponse().isError()) {
1290 void XBeeInterface::readPacket() {
1292 if (_response.isAvailable() || _response.isError()) {
1297 while (i_available()) {
1300 if (_pos > 0 && b == START_BYTE && ATAP == 2) {
1302 _response.setErrorCode(UNEXPECTED_START_BYTE);
1306 if (_pos > 0 && b == ESCAPE) {
1307 if (i_available()) {
1317 if (_escape ==
true) {
1323 if (_pos >= API_ID_INDEX) {
1329 if (b == START_BYTE) {
1336 _response.setMsbLength(b);
1342 _response.setLsbLength(b);
1347 _response.setApiId(b);
1354 if (_pos > MAX_FRAME_DATA_SIZE) {
1356 _response.setErrorCode(PACKET_EXCEEDS_BYTE_ARRAY_LENGTH);
1362 if (_pos == (_response.getPacketLength() + 3)) {
1367 if ((_checksumTotal & 0xff) == 0xff) {
1368 _response.setChecksum(b);
1369 _response.setAvailable(
true);
1371 _response.setErrorCode(NO_ERROR);
1374 _response.setErrorCode(CHECKSUM_FAILURE);
1379 _response.setFrameLength(_pos - 4);
1389 _response.getFrameData()[_pos - 4] = b;
1401 sendByte(START_BYTE,
false);
1407 sendByte(msbLen,
true);
1408 sendByte(lsbLen,
true);
1411 sendByte(request.
getApiId(),
true);
1414 uint8_t checksum = 0;
1429 checksum = 0xff - checksum;
1434 sendByte(checksum,
true);
1440 void XBeeInterface::sendByte(uint8_t b,
bool escape) {
1442 if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF)) {
1452 uint8_t cmd[]={
'I',
'D'};
1456 if (readPacket(5000)) {
1458 if (getResponse().
getApiId() == AT_COMMAND_RESPONSE) {
1459 getResponse().getAtCommandResponse(atResponse);
1461 if (atResponse.
isOk()) {
1469 uint8_t XBeeInterface::parseRX16(
BayEOS& client,
int rx_panid){
1472 if (getResponse().isError())
1474 if (getResponse().
getApiId() == RX_16_IO_RESPONSE) {
1475 getResponse().getRx16IoSampleResponse(rx16io);
1477 (uint8_t) rx16io.getRssi());
1480 for (uint8_t i = 0; i <= 5; i++) {
1486 for (uint8_t i = 0; i <= 8; i++) {
1495 if (getResponse().getApiId() == RX_16_RESPONSE) {
1497 getResponse().getRx16Response(rx16);
1499 (uint8_t) rx16.getRssi());
uint8_t getFrameDataLength()
bool isAnalogEnabled(uint8_t pin)
uint8_t getPayloadLength()
void getZBRxIoSampleResponse(XBeeResponse &response)
bool isAnalogEnabled(uint8_t pin)
void getRx16IoSampleResponse(XBeeResponse &response)
void getRx64Response(XBeeResponse &response)
uint8_t getFrameDataLength()
void getModemStatusResponse(XBeeResponse &response)
uint16_t getPacketLength()
uint8_t getFrameDataLength()
bool isDigitalOn(uint8_t pin, uint8_t sample)
void startRoutedFrame(uint16_t sourceMyID, uint16_t sourcePANID, uint8_t rssi=0)
void setPayload(uint8_t *payloadPtr)
uint8_t addToPayload(uint8_t b)
uint16_t getAnalog(uint8_t pin)
void send(XBeeRequest &request)
bool isDigitalOn(uint8_t pin)
uint8_t getFrameData(uint8_t pos)
void getRx16Response(XBeeResponse &response)
void getZBTxStatusResponse(XBeeResponse &response)
uint8_t getFrameData(uint8_t pos)
XBeeAddress64 & getRemoteAddress64()
uint8_t getFrameDataLength()
XBeeResponse & getResponse()
bool isDigitalEnabled(uint8_t pin)
void getRemoteAtCommandResponse(XBeeResponse &response)
uint16_t getRemoteAddress16()
uint8_t getFrameData(uint8_t pos)
bool isDigitalEnabled(uint8_t pin)
void getTxStatusResponse(XBeeResponse &response)
void setPayloadLength(uint8_t payloadLength)
void getAtCommandResponse(XBeeResponse &responses)
virtual uint8_t getFrameData(uint8_t pos)=0
uint16_t getAnalog(uint8_t pin, uint8_t sample)
uint8_t getFrameData(uint8_t pos)
void setFrameId(uint8_t frameId)
virtual uint8_t getFrameDataLength()=0
uint8_t getFrameDataLength()
void getZBRxResponse(XBeeResponse &response)
XBeeRequest(uint8_t apiId, uint8_t frameId)
uint8_t getData(int index)
void readPacketUntilAvailable()
void getRx64IoSampleResponse(XBeeResponse &response)
uint8_t getFrameData(uint8_t pos)
uint8_t getFrameDataLength()