diff --git a/XBee.cpp b/XBee.cpp index 3d8b670..e9a8119 100755 --- a/XBee.cpp +++ b/XBee.cpp @@ -31,7 +31,7 @@ XBeeResponse::XBeeResponse() { } -uint8_t XBeeResponse::getApiId() { +uint8_t XBeeResponse::getApiId() const { return _apiId; } @@ -39,7 +39,7 @@ void XBeeResponse::setApiId(uint8_t apiId) { _apiId = apiId; } -uint8_t XBeeResponse::getMsbLength() { +uint8_t XBeeResponse::getMsbLength() const { return _msbLength; } @@ -47,7 +47,7 @@ void XBeeResponse::setMsbLength(uint8_t msbLength) { _msbLength = msbLength; } -uint8_t XBeeResponse::getLsbLength() { +uint8_t XBeeResponse::getLsbLength() const { return _lsbLength; } @@ -55,7 +55,7 @@ void XBeeResponse::setLsbLength(uint8_t lsbLength) { _lsbLength = lsbLength; } -uint8_t XBeeResponse::getChecksum() { +uint8_t XBeeResponse::getChecksum() const { return _checksum; } @@ -63,7 +63,7 @@ void XBeeResponse::setChecksum(uint8_t checksum) { _checksum = checksum; } -uint8_t XBeeResponse::getFrameDataLength() { +uint8_t XBeeResponse::getFrameDataLength() const { return _frameLength; } @@ -71,7 +71,7 @@ void XBeeResponse::setFrameLength(uint8_t frameLength) { _frameLength = frameLength; } -bool XBeeResponse::isAvailable() { +bool XBeeResponse::isAvailable() const { return _complete; } @@ -79,11 +79,11 @@ void XBeeResponse::setAvailable(bool complete) { _complete = complete; } -bool XBeeResponse::isError() { +bool XBeeResponse::isError() const { return _errorCode > 0; } -uint8_t XBeeResponse::getErrorCode() { +uint8_t XBeeResponse::getErrorCode() const { return _errorCode; } @@ -92,7 +92,7 @@ void XBeeResponse::setErrorCode(uint8_t errorCode) { } // copy common fields from xbee response to target response -void XBeeResponse::setCommon(XBeeResponse &target) { +void XBeeResponse::setCommon(XBeeResponse &target) const { target.setApiId(getApiId()); target.setAvailable(isAvailable()); target.setChecksum(getChecksum()); @@ -108,27 +108,27 @@ ZBTxStatusResponse::ZBTxStatusResponse() : FrameIdResponse() { } -uint16_t ZBTxStatusResponse::getRemoteAddress() { +uint16_t ZBTxStatusResponse::getRemoteAddress() const { return (getFrameData()[1] << 8) + getFrameData()[2]; } -uint8_t ZBTxStatusResponse::getTxRetryCount() { +uint8_t ZBTxStatusResponse::getTxRetryCount() const { return getFrameData()[3]; } -uint8_t ZBTxStatusResponse::getDeliveryStatus() { +uint8_t ZBTxStatusResponse::getDeliveryStatus() const { return getFrameData()[4]; } -uint8_t ZBTxStatusResponse::getDiscoveryStatus() { +uint8_t ZBTxStatusResponse::getDiscoveryStatus() const { return getFrameData()[5]; } -bool ZBTxStatusResponse::isSuccess() { +bool ZBTxStatusResponse::isSuccess() const { return getDeliveryStatus() == SUCCESS; } -void XBeeResponse::getZBTxStatusResponse(XBeeResponse &zbXBeeResponse) { +void XBeeResponse::getZBTxStatusResponse(XBeeResponse &zbXBeeResponse) const { // way off? ZBTxStatusResponse* zb = static_cast(&zbXBeeResponse); @@ -141,28 +141,28 @@ ZBRxResponse::ZBRxResponse(): RxDataResponse() { _remoteAddress64 = XBeeAddress64(); } -uint16_t ZBRxResponse::getRemoteAddress16() { +uint16_t ZBRxResponse::getRemoteAddress16() const { return (getFrameData()[8] << 8) + getFrameData()[9]; } -uint8_t ZBRxResponse::getOption() { +uint8_t ZBRxResponse::getOption() const { return getFrameData()[10]; } // markers to read data from packet array. this is the index, so the 12th item in the array -uint8_t ZBRxResponse::getDataOffset() { +uint8_t ZBRxResponse::getDataOffset() const { return 11; } -uint8_t ZBRxResponse::getDataLength() { +uint8_t ZBRxResponse::getDataLength() const { return getPacketLength() - getDataOffset() - 1; } -XBeeAddress64& ZBRxResponse::getRemoteAddress64() { +const XBeeAddress64& ZBRxResponse::getRemoteAddress64() const { return _remoteAddress64; } -void XBeeResponse::getZBRxResponse(XBeeResponse &rxResponse) { +void XBeeResponse::getZBRxResponse(XBeeResponse &rxResponse) const { ZBRxResponse* zb = static_cast(&rxResponse); @@ -179,36 +179,36 @@ void XBeeResponse::getZBRxResponse(XBeeResponse &rxResponse) { ZBExplicitRxResponse::ZBExplicitRxResponse(): ZBRxResponse() { } -uint8_t ZBExplicitRxResponse::getSrcEndpoint() { +uint8_t ZBExplicitRxResponse::getSrcEndpoint() const { return getFrameData()[10]; } -uint8_t ZBExplicitRxResponse::getDstEndpoint() { +uint8_t ZBExplicitRxResponse::getDstEndpoint() const { return getFrameData()[11]; } -uint16_t ZBExplicitRxResponse::getClusterId() { +uint16_t ZBExplicitRxResponse::getClusterId() const { return (uint16_t)(getFrameData()[12]) << 8 | getFrameData()[13]; } -uint16_t ZBExplicitRxResponse::getProfileId() { +uint16_t ZBExplicitRxResponse::getProfileId() const { return (uint16_t)(getFrameData()[14]) << 8 | getFrameData()[15]; } -uint8_t ZBExplicitRxResponse::getOption() { +uint8_t ZBExplicitRxResponse::getOption() const { return getFrameData()[16]; } // markers to read data from packet array. -uint8_t ZBExplicitRxResponse::getDataOffset() { +uint8_t ZBExplicitRxResponse::getDataOffset() const { return 17; } -uint8_t ZBExplicitRxResponse::getDataLength() { +uint8_t ZBExplicitRxResponse::getDataLength() const { return getPacketLength() - getDataOffset() - 1; } -void XBeeResponse::getZBExplicitRxResponse(XBeeResponse &rxResponse) { +void XBeeResponse::getZBExplicitRxResponse(XBeeResponse &rxResponse) const { // Nothing to add to that getZBRxResponse(rxResponse); } @@ -219,31 +219,31 @@ ZBRxIoSampleResponse::ZBRxIoSampleResponse() : ZBRxResponse() { } // 64 + 16 addresses, sample size, option = 12 (index 11), so this starts at 12 -uint8_t ZBRxIoSampleResponse::getDigitalMaskMsb() { +uint8_t ZBRxIoSampleResponse::getDigitalMaskMsb() const { return getFrameData()[12] & 0x1c; } -uint8_t ZBRxIoSampleResponse::getDigitalMaskLsb() { +uint8_t ZBRxIoSampleResponse::getDigitalMaskLsb() const { return getFrameData()[13]; } -uint8_t ZBRxIoSampleResponse::getAnalogMask() { +uint8_t ZBRxIoSampleResponse::getAnalogMask() const { return getFrameData()[14] & 0x8f; } -bool ZBRxIoSampleResponse::containsAnalog() { +bool ZBRxIoSampleResponse::containsAnalog() const { return getAnalogMask() > 0; } -bool ZBRxIoSampleResponse::containsDigital() { +bool ZBRxIoSampleResponse::containsDigital() const { return getDigitalMaskMsb() > 0 || getDigitalMaskLsb() > 0; } -bool ZBRxIoSampleResponse::isAnalogEnabled(uint8_t pin) { +bool ZBRxIoSampleResponse::isAnalogEnabled(uint8_t pin) const { return ((getAnalogMask() >> pin) & 1) == 1; } -bool ZBRxIoSampleResponse::isDigitalEnabled(uint8_t pin) { +bool ZBRxIoSampleResponse::isDigitalEnabled(uint8_t pin) const { if (pin <= 7) { // added extra parens to calm avr compiler return ((getDigitalMaskLsb() >> pin) & 1) == 1; @@ -252,7 +252,7 @@ bool ZBRxIoSampleResponse::isDigitalEnabled(uint8_t pin) { } } -uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) { +uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) const { // analog starts 13 bytes after sample size, if no dio enabled uint8_t start = 15; @@ -261,6 +261,8 @@ uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) { start+=2; } +// std::cout << "spacing is " << static_cast(spacing) << std::endl; + // start depends on how many pins before this pin are enabled for (int i = 0; i < pin; i++) { if (isAnalogEnabled(i)) { @@ -268,10 +270,14 @@ uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) { } } +// std::cout << "start for analog pin ["<< static_cast(pin) << "]/sample " << static_cast(sample) << " is " << static_cast(start) << std::endl; + +// std::cout << "returning index " << static_cast(getSampleOffset() + start) << " and index " << static_cast(getSampleOffset() + start + 1) << ", val is " << static_cast(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast(getFrameData()[getSampleOffset() + start + 1]) << std::endl; + return (uint16_t)((getFrameData()[start] << 8) + getFrameData()[start + 1]); } -bool ZBRxIoSampleResponse::isDigitalOn(uint8_t pin) { +bool ZBRxIoSampleResponse::isDigitalOn(uint8_t pin) const { if (pin <= 7) { // D0-7 // DIO LSB is index 5 @@ -283,7 +289,7 @@ bool ZBRxIoSampleResponse::isDigitalOn(uint8_t pin) { } } -void XBeeResponse::getZBRxIoSampleResponse(XBeeResponse &response) { +void XBeeResponse::getZBRxIoSampleResponse(XBeeResponse &response) const { ZBRxIoSampleResponse* zb = static_cast(&response); @@ -303,11 +309,11 @@ RxResponse::RxResponse() : RxDataResponse() { } -uint16_t Rx16Response::getRemoteAddress16() { +uint16_t Rx16Response::getRemoteAddress16() const { return (getFrameData()[0] << 8) + getFrameData()[1]; } -XBeeAddress64& Rx64Response::getRemoteAddress64() { +const XBeeAddress64& Rx64Response::getRemoteAddress64() const { return _remoteAddress; } @@ -323,21 +329,21 @@ RxIoSampleBaseResponse::RxIoSampleBaseResponse() : RxResponse() { } -uint8_t RxIoSampleBaseResponse::getSampleOffset() { +uint8_t RxIoSampleBaseResponse::getSampleOffset() const { // sample starts 2 bytes after rssi return getRssiOffset() + 2; } -uint8_t RxIoSampleBaseResponse::getSampleSize() { +uint8_t RxIoSampleBaseResponse::getSampleSize() const { return getFrameData()[getSampleOffset()]; } -bool RxIoSampleBaseResponse::containsAnalog() { +bool RxIoSampleBaseResponse::containsAnalog() const { return (getFrameData()[getSampleOffset() + 1] & 0x7e) > 0; } -bool RxIoSampleBaseResponse::containsDigital() { +bool RxIoSampleBaseResponse::containsDigital() const { return (getFrameData()[getSampleOffset() + 1] & 0x1) > 0 || getFrameData()[getSampleOffset() + 2] > 0; } @@ -345,11 +351,11 @@ bool RxIoSampleBaseResponse::containsDigital() { // return getAnalog(0, sample); //} -bool RxIoSampleBaseResponse::isAnalogEnabled(uint8_t pin) { +bool RxIoSampleBaseResponse::isAnalogEnabled(uint8_t pin) const { return (((getFrameData()[getSampleOffset() + 1] >> (pin + 1)) & 1) == 1); } -bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) { +bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) const { if (pin < 8) { return ((getFrameData()[getSampleOffset() + 2] >> pin) & 1) == 1; } else { @@ -368,12 +374,12 @@ bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) { // width+=2; // } // } -// +// // if (this.containsDigital()) { // // digital enabled takes two bytes, no matter how many pins enabled // width+= 2; // } -// +// // return width; // } // @@ -388,29 +394,29 @@ bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) { // // 64 bit // startIndex = 13; // } -// +// // return startIndex; // } -// +// // public int getDigitalMsb(int sample) { // // msb digital always starts 3 bytes after sample size // return this.getProcessedPacketBytes()[this.getStartIndex() + 3 + this.getSampleWidth() * sample]; // } -// +// // public int getDigitalLsb(int sample) { // return this.getProcessedPacketBytes()[this.getStartIndex() + 3 + this.getSampleWidth() * sample + 1]; -// } +// } // // public Boolean isDigitalOn(int pin, int sample) { -// +// // if (sample < 0 || sample >= this.getSampleSize()) { // throw new IllegalArgumentException("invalid sample size: " + sample); // } -// +// // if (!this.containsDigital()) { // throw new RuntimeException("Digital is not enabled"); // } -// +// // if (pin >= 0 && pin < 8) { // return ((this.getDigitalLsb(sample) >> pin) & 1) == 1; // } else if (pin == 8) { @@ -418,23 +424,23 @@ bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) { // return (this.getDigitalMsb(sample) & 1) == 1; // } else { // throw new IllegalArgumentException("Invalid pin: " + pin); -// } +// } // } -// +// // public Integer getAnalog(int pin, int sample) { -// +// // if (sample < 0 || sample >= this.getSampleSize()) { // throw new IllegalArgumentException("invalid sample size: " + sample); // } -// +// // // analog starts 3 bytes after start of sample, if no dio enabled // int startIndex = this.getStartIndex() + 3; -// +// // if (this.containsDigital()) { // // make room for digital i/o sample (2 bytes per sample) // startIndex+= 2; // } -// +// // startIndex+= this.getSampleWidth() * sample; // // // start depends on how many pins before this pin are enabled @@ -445,10 +451,10 @@ bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) { // } // } // -// return (this.getProcessedPacketBytes()[startIndex] << 8) + this.getProcessedPacketBytes()[startIndex + 1]; +// return (this.getProcessedPacketBytes()[startIndex] << 8) + this.getProcessedPacketBytes()[startIndex + 1]; // } - -uint8_t RxIoSampleBaseResponse::getSampleStart(uint8_t sample) { + +uint8_t RxIoSampleBaseResponse::getSampleStart(uint8_t sample) const { uint8_t spacing = 0; if (containsDigital()) { @@ -469,7 +475,7 @@ uint8_t RxIoSampleBaseResponse::getSampleStart(uint8_t sample) { return getSampleOffset() + 3 + sample * spacing; } -uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) { +uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) const { uint8_t start = getSampleStart(sample); if (containsDigital()) { @@ -487,7 +493,7 @@ uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) { return (uint16_t)((getFrameData()[start] << 8) + getFrameData()[start + 1]); } -bool RxIoSampleBaseResponse::isDigitalOn(uint8_t pin, uint8_t sample) { +bool RxIoSampleBaseResponse::isDigitalOn(uint8_t pin, uint8_t sample) const { if (pin < 8) { return ((getFrameData()[getSampleStart(sample) + 1] >> pin) & 1) == 1; } else { @@ -504,15 +510,15 @@ Rx16IoSampleResponse::Rx16IoSampleResponse() : RxIoSampleBaseResponse() { } -uint16_t Rx16IoSampleResponse::getRemoteAddress16() { +uint16_t Rx16IoSampleResponse::getRemoteAddress16() const { return (uint16_t)((getFrameData()[0] << 8) + getFrameData()[1]); } -uint8_t Rx16IoSampleResponse::getRssiOffset() { +uint8_t Rx16IoSampleResponse::getRssiOffset() const { return 2; } -void XBeeResponse::getRx16IoSampleResponse(XBeeResponse &response) { +void XBeeResponse::getRx16IoSampleResponse(XBeeResponse &response) const { Rx16IoSampleResponse* rx = static_cast(&response); rx->setFrameData(getFrameData()); @@ -524,15 +530,15 @@ Rx64IoSampleResponse::Rx64IoSampleResponse() : RxIoSampleBaseResponse() { _remoteAddress = XBeeAddress64(); } -XBeeAddress64& Rx64IoSampleResponse::getRemoteAddress64() { +const XBeeAddress64& Rx64IoSampleResponse::getRemoteAddress64() const { return _remoteAddress; } -uint8_t Rx64IoSampleResponse::getRssiOffset() { +uint8_t Rx64IoSampleResponse::getRssiOffset() const { return 8; } -void XBeeResponse::getRx64IoSampleResponse(XBeeResponse &response) { +void XBeeResponse::getRx64IoSampleResponse(XBeeResponse &response) const { Rx64IoSampleResponse* rx = static_cast(&response); rx->setFrameData(getFrameData()); @@ -546,15 +552,15 @@ TxStatusResponse::TxStatusResponse() : FrameIdResponse() { } -uint8_t TxStatusResponse::getStatus() { +uint8_t TxStatusResponse::getStatus() const { return getFrameData()[1]; } -bool TxStatusResponse::isSuccess() { +bool TxStatusResponse::isSuccess() const { return getStatus() == SUCCESS; } -void XBeeResponse::getTxStatusResponse(XBeeResponse &txResponse) { +void XBeeResponse::getTxStatusResponse(XBeeResponse &txResponse) const { TxStatusResponse* txStatus = static_cast(&txResponse); @@ -563,48 +569,50 @@ void XBeeResponse::getTxStatusResponse(XBeeResponse &txResponse) { setCommon(txResponse); } -uint8_t RxResponse::getRssi() { +uint8_t RxResponse::getRssi() const { return getFrameData()[getRssiOffset()]; } -uint8_t RxResponse::getOption() { +uint8_t RxResponse::getOption() const { return getFrameData()[getRssiOffset() + 1]; } -bool RxResponse::isAddressBroadcast() { +bool RxResponse::isAddressBroadcast() const { return (getOption() & 2) == 2; } -bool RxResponse::isPanBroadcast() { +bool RxResponse::isPanBroadcast() const { return (getOption() & 4) == 4; } -uint8_t RxResponse::getDataLength() { +uint8_t RxResponse::getDataLength() const { return getPacketLength() - getDataOffset() - 1; } -uint8_t RxResponse::getDataOffset() { +uint8_t RxResponse::getDataOffset() const { return getRssiOffset() + 2; } -uint8_t Rx16Response::getRssiOffset() { +uint8_t Rx16Response::getRssiOffset() const { return RX_16_RSSI_OFFSET; } -void XBeeResponse::getRx16Response(XBeeResponse &rx16Response) { +void XBeeResponse::getRx16Response(XBeeResponse &rx16Response) const { Rx16Response* rx16 = static_cast(&rx16Response); // pass pointer array to subclass rx16->setFrameData(getFrameData()); setCommon(rx16Response); + +// rx16->getRemoteAddress16().setAddress((getFrameData()[0] << 8) + getFrameData()[1]); } -uint8_t Rx64Response::getRssiOffset() { +uint8_t Rx64Response::getRssiOffset() const { return RX_64_RSSI_OFFSET; } -void XBeeResponse::getRx64Response(XBeeResponse &rx64Response) { +void XBeeResponse::getRx64Response(XBeeResponse &rx64Response) const { Rx64Response* rx64 = static_cast(&rx64Response); @@ -622,24 +630,24 @@ RemoteAtCommandResponse::RemoteAtCommandResponse() : AtCommandResponse() { } -uint8_t* RemoteAtCommandResponse::getCommand() { +uint8_t* RemoteAtCommandResponse::getCommand() const { return getFrameData() + 11; } -uint8_t RemoteAtCommandResponse::getStatus() { +uint8_t RemoteAtCommandResponse::getStatus() const { return getFrameData()[13]; } -bool RemoteAtCommandResponse::isOk() { +bool RemoteAtCommandResponse::isOk() const { // weird c++ behavior. w/o this method, it calls AtCommandResponse::isOk(), which calls the AtCommandResponse::getStatus, not this.getStatus!!! return getStatus() == AT_OK; } -uint8_t RemoteAtCommandResponse::getValueLength() { +uint8_t RemoteAtCommandResponse::getValueLength() const { return getFrameDataLength() - 14; } -uint8_t* RemoteAtCommandResponse::getValue() { +uint8_t* RemoteAtCommandResponse::getValue() const { if (getValueLength() > 0) { // value is only included for query commands. set commands does not return a value return getFrameData() + 14; @@ -648,15 +656,15 @@ uint8_t* RemoteAtCommandResponse::getValue() { return NULL; } -uint16_t RemoteAtCommandResponse::getRemoteAddress16() { +uint16_t RemoteAtCommandResponse::getRemoteAddress16() const { return uint16_t((getFrameData()[9] << 8) + getFrameData()[10]); } -XBeeAddress64& RemoteAtCommandResponse::getRemoteAddress64() { +const XBeeAddress64& RemoteAtCommandResponse::getRemoteAddress64() const { return _remoteAddress64; } -void XBeeResponse::getRemoteAtCommandResponse(XBeeResponse &response) { +void XBeeResponse::getRemoteAtCommandResponse(XBeeResponse &response) const { // TODO no real need to cast. change arg to match expected class RemoteAtCommandResponse* at = static_cast(&response); @@ -674,11 +682,11 @@ RxDataResponse::RxDataResponse() : XBeeResponse() { } -uint8_t RxDataResponse::getData(int index) { +uint8_t RxDataResponse::getData(int index) const { return getFrameData()[getDataOffset() + index]; } -uint8_t* RxDataResponse::getData() { +uint8_t* RxDataResponse::getData() const { return getFrameData() + getDataOffset(); } @@ -686,7 +694,7 @@ FrameIdResponse::FrameIdResponse() { } -uint8_t FrameIdResponse::getFrameId() { +uint8_t FrameIdResponse::getFrameId() const { return getFrameData()[0]; } @@ -695,11 +703,11 @@ ModemStatusResponse::ModemStatusResponse() { } -uint8_t ModemStatusResponse::getStatus() { +uint8_t ModemStatusResponse::getStatus() const { return getFrameData()[0]; } -void XBeeResponse::getModemStatusResponse(XBeeResponse &modemStatusResponse) { +void XBeeResponse::getModemStatusResponse(XBeeResponse &modemStatusResponse) const { ModemStatusResponse* modem = static_cast(&modemStatusResponse); @@ -713,19 +721,19 @@ AtCommandResponse::AtCommandResponse() { } -uint8_t* AtCommandResponse::getCommand() { +uint8_t* AtCommandResponse::getCommand() const { return getFrameData() + 1; } -uint8_t AtCommandResponse::getStatus() { +uint8_t AtCommandResponse::getStatus() const { return getFrameData()[3]; } -uint8_t AtCommandResponse::getValueLength() { +uint8_t AtCommandResponse::getValueLength() const { return getFrameDataLength() - 4; } -uint8_t* AtCommandResponse::getValue() { +uint8_t* AtCommandResponse::getValue() const { if (getValueLength() > 0) { // value is only included for query commands. set commands does not return a value return getFrameData() + 4; @@ -734,11 +742,11 @@ uint8_t* AtCommandResponse::getValue() { return NULL; } -bool AtCommandResponse::isOk() { +bool AtCommandResponse::isOk() const { return getStatus() == AT_OK; } -void XBeeResponse::getAtCommandResponse(XBeeResponse &atCommandResponse) { +void XBeeResponse::getAtCommandResponse(XBeeResponse &atCommandResponse) const { AtCommandResponse* at = static_cast(&atCommandResponse); @@ -747,11 +755,11 @@ void XBeeResponse::getAtCommandResponse(XBeeResponse &atCommandResponse) { setCommon(atCommandResponse); } -uint16_t XBeeResponse::getPacketLength() { +uint16_t XBeeResponse::getPacketLength() const { return ((_msbLength << 8) & 0xff) + (_lsbLength & 0xff); } -uint8_t* XBeeResponse::getFrameData() { +uint8_t* XBeeResponse::getFrameData() const { return _frameDataPtr; } @@ -792,14 +800,14 @@ XBee::XBee(): _response(XBeeResponse()) { _response.init(); _response.setFrameData(_responseFrameData); // Contributed by Paul Stoffregen for Teensy support -#if defined(__AVR_ATmega32U4__) || (defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL))) +#if defined(__AVR_ATmega32U4__) || defined(__MK20DX128__) _serial = &Serial1; #else _serial = &Serial; #endif } -uint8_t XBee::getNextFrameId() { +uint8_t XBee::getNextFrameId() const { _nextFrameId++; @@ -820,24 +828,32 @@ void XBee::setSerial(Stream &serial) { _serial = &serial; } -bool XBee::available() { +bool XBee::available() const { return _serial->available(); } uint8_t XBee::read() { return _serial->read(); -} +} -void XBee::write(uint8_t val) { +void XBee::flush() const { + _serial->flush(); +} + +void XBee::write(uint8_t val) const { _serial->write(val); } -XBeeResponse& XBee::getResponse() { +const XBeeResponse& XBee::getResponse() const { return _response; } +XBeeResponse& XBee::getResponse() { + return _response; +} + // TODO how to convert response to proper subclass? -void XBee::getResponse(XBeeResponse &response) { +void XBee::getResponse(XBeeResponse &response) const { response.setMsbLength(_response.getMsbLength()); response.setLsbLength(_response.getLsbLength()); @@ -953,6 +969,8 @@ void XBee::readPacket() { if (_pos == (_response.getPacketLength() + 3)) { // verify checksum + //std::cout << "read checksum " << static_cast(b) << " at pos " << static_cast(_pos) << std::endl; + if ((_checksumTotal & 0xff) == 0xff) { _response.setChecksum(b); _response.setAvailable(true); @@ -991,11 +1009,11 @@ void XBeeRequest::setFrameId(uint8_t frameId) { _frameId = frameId; } -uint8_t XBeeRequest::getFrameId() { +uint8_t XBeeRequest::getFrameId() const { return _frameId; } -uint8_t XBeeRequest::getApiId() { +uint8_t XBeeRequest::getApiId() const { return _apiId; } @@ -1021,7 +1039,7 @@ PayloadRequest::PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, _payloadLength = payloadLength; } -uint8_t* PayloadRequest::getPayload() { +uint8_t* PayloadRequest::getPayload() const { return _payloadPtr; } @@ -1029,7 +1047,7 @@ void PayloadRequest::setPayload(uint8_t* payload) { _payloadPtr = payload; } -uint8_t PayloadRequest::getPayloadLength() { +uint8_t PayloadRequest::getPayloadLength() const { return _payloadLength; } @@ -1059,7 +1077,7 @@ ZBTxRequest::ZBTxRequest(const XBeeAddress64 &addr64, uint8_t *data, uint8_t dat _option = ZB_TX_UNICAST; } -uint8_t ZBTxRequest::getFrameData(uint8_t pos) { +uint8_t ZBTxRequest::getFrameData(uint8_t pos) const { if (pos == 0) { return (_addr64.getMsb() >> 24) & 0xff; } else if (pos == 1) { @@ -1089,7 +1107,7 @@ uint8_t ZBTxRequest::getFrameData(uint8_t pos) { } } -uint8_t ZBTxRequest::getFrameDataLength() { +uint8_t ZBTxRequest::getFrameDataLength() const { return ZB_TX_API_LENGTH + getPayloadLength(); } @@ -1153,7 +1171,7 @@ ZBExplicitTxRequest::ZBExplicitTxRequest(XBeeAddress64 &addr64, uint8_t *payload setApiId(ZB_EXPLICIT_TX_REQUEST); } -uint8_t ZBExplicitTxRequest::getFrameData(uint8_t pos) { +uint8_t ZBExplicitTxRequest::getFrameData(uint8_t pos) const { if (pos < 10) { return ZBTxRequest::getFrameData(pos); } else if (pos == 10) { @@ -1177,7 +1195,7 @@ uint8_t ZBExplicitTxRequest::getFrameData(uint8_t pos) { } } -uint8_t ZBExplicitTxRequest::getFrameDataLength() { +uint8_t ZBExplicitTxRequest::getFrameDataLength() const { return ZB_EXPLICIT_TX_API_LENGTH + getPayloadLength(); } @@ -1230,7 +1248,7 @@ Tx16Request::Tx16Request(uint16_t addr16, uint8_t *data, uint8_t dataLength) : P _option = ACK_OPTION; } -uint8_t Tx16Request::getFrameData(uint8_t pos) { +uint8_t Tx16Request::getFrameData(uint8_t pos) const { if (pos == 0) { return (_addr16 >> 8) & 0xff; @@ -1243,7 +1261,7 @@ uint8_t Tx16Request::getFrameData(uint8_t pos) { } } -uint8_t Tx16Request::getFrameDataLength() { +uint8_t Tx16Request::getFrameDataLength() const { return TX_16_API_LENGTH + getPayloadLength(); } @@ -1277,7 +1295,7 @@ Tx64Request::Tx64Request(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLengt _option = ACK_OPTION; } -uint8_t Tx64Request::getFrameData(uint8_t pos) { +uint8_t Tx64Request::getFrameData(uint8_t pos) const { if (pos == 0) { return (_addr64.getMsb() >> 24) & 0xff; @@ -1302,7 +1320,7 @@ uint8_t Tx64Request::getFrameData(uint8_t pos) { } } -uint8_t Tx64Request::getFrameDataLength() { +uint8_t Tx64Request::getFrameDataLength() const { return TX_64_API_LENGTH + getPayloadLength(); } @@ -1340,15 +1358,15 @@ AtCommandRequest::AtCommandRequest(uint8_t *command) : XBeeRequest(AT_COMMAND_RE clearCommandValue(); } -uint8_t* AtCommandRequest::getCommand() { +uint8_t* AtCommandRequest::getCommand() const { return _command; } -uint8_t* AtCommandRequest::getCommandValue() { +uint8_t* AtCommandRequest::getCommandValue() const { return _commandValue; } -uint8_t AtCommandRequest::getCommandValueLength() { +uint8_t AtCommandRequest::getCommandValueLength() const { return _commandValueLength; } @@ -1364,7 +1382,7 @@ void AtCommandRequest::setCommandValueLength(uint8_t length) { _commandValueLength = length; } -uint8_t AtCommandRequest::getFrameData(uint8_t pos) { +uint8_t AtCommandRequest::getFrameData(uint8_t pos) const { if (pos == 0) { return _command[0]; @@ -1384,7 +1402,7 @@ void AtCommandRequest::clearCommandValue() { // XBeeRequest::reset(); //} -uint8_t AtCommandRequest::getFrameDataLength() { +uint8_t AtCommandRequest::getFrameDataLength() const { // command is 2 byte + length of value return AT_COMMAND_API_LENGTH + _commandValueLength; } @@ -1434,7 +1452,7 @@ void RemoteAtCommandRequest::setRemoteAddress16(uint16_t remoteAddress16) { _remoteAddress16 = remoteAddress16; } -XBeeAddress64& RemoteAtCommandRequest::getRemoteAddress64() { +const XBeeAddress64& RemoteAtCommandRequest::getRemoteAddress64() const { return _remoteAddress64; } @@ -1451,7 +1469,7 @@ void RemoteAtCommandRequest::setApplyChanges(bool applyChanges) { } -uint8_t RemoteAtCommandRequest::getFrameData(uint8_t pos) { +uint8_t RemoteAtCommandRequest::getFrameData(uint8_t pos) const { if (pos == 0) { return (_remoteAddress64.getMsb() >> 24) & 0xff; } else if (pos == 1) { @@ -1483,7 +1501,7 @@ uint8_t RemoteAtCommandRequest::getFrameData(uint8_t pos) { } } -uint8_t RemoteAtCommandRequest::getFrameDataLength() { +uint8_t RemoteAtCommandRequest::getFrameDataLength() const { return REMOTE_AT_COMMAND_API_LENGTH + getCommandValueLength(); } @@ -1493,7 +1511,7 @@ uint8_t RemoteAtCommandRequest::getFrameDataLength() { // _frame = frame; //} -void XBee::send(XBeeRequest &request) { +void XBee::send(XBeeRequest &request) const { // the new new deal sendByte(START_BYTE, false); @@ -1515,7 +1533,10 @@ void XBee::send(XBeeRequest &request) { checksum+= request.getApiId(); checksum+= request.getFrameId(); + //std::cout << "frame length is " << static_cast(request.getFrameDataLength()) << std::endl; + for (int i = 0; i < request.getFrameDataLength(); i++) { +// std::cout << "sending byte [" << static_cast(i) << "] " << std::endl; sendByte(request.getFrameData(i), true); checksum+= request.getFrameData(i); } @@ -1523,13 +1544,19 @@ void XBee::send(XBeeRequest &request) { // perform 2s complement checksum = 0xff - checksum; +// std::cout << "checksum is " << static_cast(checksum) << std::endl; + // send checksum sendByte(checksum, true); + + // send packet (Note: prior to Arduino 1.0 this flushed the incoming buffer, which of course was not so great) + flush(); } -void XBee::sendByte(uint8_t b, bool escape) { +void XBee::sendByte(uint8_t b, bool escape) const { if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF)) { +// std::cout << "escaping byte [" << toHexString(b) << "] " << std::endl; write(ESCAPE); write(b ^ 0x20); } else { diff --git a/XBee.h b/XBee.h index c37fd0c..7602cf0 100755 --- a/XBee.h +++ b/XBee.h @@ -181,28 +181,28 @@ class XBeeResponse { /** * Returns Api Id of the response */ - uint8_t getApiId(); + uint8_t getApiId() const; void setApiId(uint8_t apiId); /** * Returns the MSB length of the packet */ - uint8_t getMsbLength(); + uint8_t getMsbLength() const; void setMsbLength(uint8_t msbLength); /** * Returns the LSB length of the packet */ - uint8_t getLsbLength(); + uint8_t getLsbLength() const; void setLsbLength(uint8_t lsbLength); /** * Returns the packet checksum */ - uint8_t getChecksum(); + uint8_t getChecksum() const; void setChecksum(uint8_t checksum); /** * Returns the length of the frame data: all bytes after the api id, and prior to the checksum * Note up to release 0.1.2, this was incorrectly including the checksum in the length. */ - uint8_t getFrameDataLength(); + uint8_t getFrameDataLength() const; void setFrameData(uint8_t* frameDataPtr); /** * Returns the buffer that contains the response. @@ -212,14 +212,14 @@ class XBeeResponse { * The reason for this is all responses include an API ID, whereas my frame data * includes only the API specific data. */ - uint8_t* getFrameData(); + uint8_t* getFrameData() const; void setFrameLength(uint8_t frameLength); // to support future 65535 byte packets I guess /** * Returns the length of the packet */ - uint16_t getPacketLength(); + uint16_t getPacketLength() const; /** * Resets the response to default values */ @@ -233,77 +233,77 @@ class XBeeResponse { * Call with instance of ZBTxStatusResponse class only if getApiId() == ZB_TX_STATUS_RESPONSE * to populate response */ - void getZBTxStatusResponse(XBeeResponse &response); + void getZBTxStatusResponse(XBeeResponse &response) const; /** * Call with instance of ZBRxResponse class only if getApiId() == ZB_RX_RESPONSE * to populate response */ - void getZBRxResponse(XBeeResponse &response); + void getZBRxResponse(XBeeResponse &response) const; /** * Call with instance of ZBExplicitRxResponse class only if getApiId() == ZB_EXPLICIT_RX_RESPONSE * to populate response */ - void getZBExplicitRxResponse(XBeeResponse &response); + void getZBExplicitRxResponse(XBeeResponse &response) const; /** * Call with instance of ZBRxIoSampleResponse class only if getApiId() == ZB_IO_SAMPLE_RESPONSE * to populate response */ - void getZBRxIoSampleResponse(XBeeResponse &response); + void getZBRxIoSampleResponse(XBeeResponse &response) const; #endif #ifdef SERIES_1 /** * Call with instance of TxStatusResponse only if getApiId() == TX_STATUS_RESPONSE */ - void getTxStatusResponse(XBeeResponse &response); + void getTxStatusResponse(XBeeResponse &response) const; /** * Call with instance of Rx16Response only if getApiId() == RX_16_RESPONSE */ - void getRx16Response(XBeeResponse &response); + void getRx16Response(XBeeResponse &response) const; /** * Call with instance of Rx64Response only if getApiId() == RX_64_RESPONSE */ - void getRx64Response(XBeeResponse &response); + void getRx64Response(XBeeResponse &response) const; /** * Call with instance of Rx16IoSampleResponse only if getApiId() == RX_16_IO_RESPONSE */ - void getRx16IoSampleResponse(XBeeResponse &response); + void getRx16IoSampleResponse(XBeeResponse &response) const; /** * Call with instance of Rx64IoSampleResponse only if getApiId() == RX_64_IO_RESPONSE */ - void getRx64IoSampleResponse(XBeeResponse &response); + void getRx64IoSampleResponse(XBeeResponse &response) const; #endif /** * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE */ - void getAtCommandResponse(XBeeResponse &responses); + void getAtCommandResponse(XBeeResponse &responses) const; /** * Call with instance of RemoteAtCommandResponse only if getApiId() == REMOTE_AT_COMMAND_RESPONSE */ - void getRemoteAtCommandResponse(XBeeResponse &response); + void getRemoteAtCommandResponse(XBeeResponse &response) const; /** * Call with instance of ModemStatusResponse only if getApiId() == MODEM_STATUS_RESPONSE */ - void getModemStatusResponse(XBeeResponse &response); + void getModemStatusResponse(XBeeResponse &response) const; /** * Returns true if the response has been successfully parsed and is complete and ready for use */ - bool isAvailable(); + bool isAvailable() const; void setAvailable(bool complete); /** * Returns true if the response contains errors */ - bool isError(); + bool isError() const; /** * Returns an error code, or zero, if successful. * Error codes include: CHECKSUM_FAILURE, PACKET_EXCEEDS_BYTE_ARRAY_LENGTH, UNEXPECTED_START_BYTE */ - uint8_t getErrorCode(); + uint8_t getErrorCode() const; void setErrorCode(uint8_t errorCode); protected: // pointer to frameData uint8_t* _frameDataPtr; private: - void setCommon(XBeeResponse &target); + void setCommon(XBeeResponse &target) const; uint8_t _apiId; uint8_t _msbLength; uint8_t _lsbLength; @@ -334,8 +334,8 @@ class XBeeAddress64 : public XBeeAddress { uint32_t getLsb() {return _lsb;} uint64_t get() {return (static_cast(_msb) << 32) | _lsb;} operator uint64_t() {return get();} - void setMsb(uint32_t msb) {_msb = msb;} - void setLsb(uint32_t lsb) {_lsb = lsb;} + void setMsb(uint32_t msb) const {_msb = msb;} + void setLsb(uint32_t lsb) const {_lsb = lsb;} void set(uint64_t addr) { _msb = addr >> 32; _lsb = addr; @@ -343,8 +343,8 @@ class XBeeAddress64 : public XBeeAddress { private: // Once https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66511 is // fixed, it might make sense to merge these into a uint64_t. - uint32_t _msb; - uint32_t _lsb; + mutable uint32_t _msb; + mutable uint32_t _lsb; }; //class XBeeAddress16 : public XBeeAddress { @@ -363,7 +363,7 @@ class XBeeAddress64 : public XBeeAddress { class FrameIdResponse : public XBeeResponse { public: FrameIdResponse(); - uint8_t getFrameId(); + uint8_t getFrameId() const; private: uint8_t _frameId; }; @@ -378,19 +378,19 @@ class RxDataResponse : public XBeeResponse { * Returns the specified index of the payload. The index may be 0 to getDataLength() - 1 * This method is deprecated; use uint8_t* getData() */ - uint8_t getData(int index); + uint8_t getData(int index) const; /** * Returns the payload array. This may be accessed from index 0 to getDataLength() - 1 */ - uint8_t* getData(); + uint8_t* getData() const; /** * Returns the length of the payload */ - virtual uint8_t getDataLength() = 0; + virtual uint8_t getDataLength() const = 0; /** * Returns the position in the frame data where the data begins */ - virtual uint8_t getDataOffset() = 0; + virtual uint8_t getDataOffset() const = 0; }; // getResponse to return the proper subclass: @@ -404,11 +404,11 @@ class RxDataResponse : public XBeeResponse { class ZBTxStatusResponse : public FrameIdResponse { public: ZBTxStatusResponse(); - uint16_t getRemoteAddress(); - uint8_t getTxRetryCount(); - uint8_t getDeliveryStatus(); - uint8_t getDiscoveryStatus(); - bool isSuccess(); + uint16_t getRemoteAddress() const; + uint8_t getTxRetryCount() const; + uint8_t getDeliveryStatus() const; + uint8_t getDiscoveryStatus() const; + bool isSuccess() const; static const uint8_t API_ID = ZB_TX_STATUS_RESPONSE; }; @@ -419,12 +419,12 @@ class ZBTxStatusResponse : public FrameIdResponse { class ZBRxResponse : public RxDataResponse { public: ZBRxResponse(); - XBeeAddress64& getRemoteAddress64(); - uint16_t getRemoteAddress16(); - uint8_t getOption(); - uint8_t getDataLength(); + const XBeeAddress64& getRemoteAddress64() const; + uint16_t getRemoteAddress16() const; + uint8_t getOption() const; + uint8_t getDataLength() const; // frame position where data starts - uint8_t getDataOffset(); + uint8_t getDataOffset() const; static const uint8_t API_ID = ZB_RX_RESPONSE; private: @@ -440,14 +440,14 @@ class ZBRxResponse : public RxDataResponse { class ZBExplicitRxResponse : public ZBRxResponse { public: ZBExplicitRxResponse(); - uint8_t getSrcEndpoint(); - uint8_t getDstEndpoint(); - uint16_t getClusterId(); - uint16_t getProfileId(); - uint8_t getOption(); - uint8_t getDataLength(); + uint8_t getSrcEndpoint() const; + uint8_t getDstEndpoint() const; + uint16_t getClusterId() const; + uint16_t getProfileId() const; + uint8_t getOption() const; + uint8_t getDataLength() const; // frame position where data starts - uint8_t getDataOffset(); + uint8_t getDataOffset() const; static const uint8_t API_ID = ZB_EXPLICIT_RX_RESPONSE; }; @@ -458,29 +458,29 @@ class ZBExplicitRxResponse : public ZBRxResponse { class ZBRxIoSampleResponse : public ZBRxResponse { public: ZBRxIoSampleResponse(); - bool containsAnalog(); - bool containsDigital(); + bool containsAnalog() const; + bool containsDigital() const; /** * Returns true if the pin is enabled */ - bool isAnalogEnabled(uint8_t pin); + bool isAnalogEnabled(uint8_t pin) const; /** * Returns true if the pin is enabled */ - bool isDigitalEnabled(uint8_t pin); + bool isDigitalEnabled(uint8_t pin) const; /** * Returns the 10-bit analog reading of the specified pin. * Valid pins include ADC:xxx. */ - uint16_t getAnalog(uint8_t pin); + uint16_t getAnalog(uint8_t pin) const; /** * Returns true if the specified pin is high/on. * Valid pins include DIO:xxx. */ - bool isDigitalOn(uint8_t pin); - uint8_t getDigitalMaskMsb(); - uint8_t getDigitalMaskLsb(); - uint8_t getAnalogMask(); + bool isDigitalOn(uint8_t pin) const; + uint8_t getDigitalMaskMsb() const; + uint8_t getDigitalMaskLsb() const; + uint8_t getAnalogMask() const; static const uint8_t API_ID = ZB_IO_SAMPLE_RESPONSE; }; @@ -494,8 +494,8 @@ class ZBRxIoSampleResponse : public ZBRxResponse { class TxStatusResponse : public FrameIdResponse { public: TxStatusResponse(); - uint8_t getStatus(); - bool isSuccess(); + uint8_t getStatus() const; + bool isSuccess() const; static const uint8_t API_ID = TX_STATUS_RESPONSE; }; @@ -507,13 +507,13 @@ class RxResponse : public RxDataResponse { public: RxResponse(); // remember rssi is negative but this is unsigned byte so it's up to you to convert - uint8_t getRssi(); - uint8_t getOption(); - bool isAddressBroadcast(); - bool isPanBroadcast(); - uint8_t getDataLength(); - uint8_t getDataOffset(); - virtual uint8_t getRssiOffset() = 0; + uint8_t getRssi() const; + uint8_t getOption() const; + bool isAddressBroadcast() const; + bool isPanBroadcast() const; + uint8_t getDataLength() const; + uint8_t getDataOffset() const; + virtual uint8_t getRssiOffset() const = 0; }; /** @@ -522,8 +522,8 @@ class RxResponse : public RxDataResponse { class Rx16Response : public RxResponse { public: Rx16Response(); - uint8_t getRssiOffset(); - uint16_t getRemoteAddress16(); + uint8_t getRssiOffset() const; + uint16_t getRemoteAddress16() const; static const uint8_t API_ID = RX_16_RESPONSE; protected: @@ -536,8 +536,8 @@ class Rx16Response : public RxResponse { class Rx64Response : public RxResponse { public: Rx64Response(); - uint8_t getRssiOffset(); - XBeeAddress64& getRemoteAddress64(); + uint8_t getRssiOffset() const; + const XBeeAddress64& getRemoteAddress64() const; static const uint8_t API_ID = RX_64_RESPONSE; private: @@ -553,41 +553,41 @@ class RxIoSampleBaseResponse : public RxResponse { /** * Returns the number of samples in this packet */ - uint8_t getSampleSize(); - bool containsAnalog(); - bool containsDigital(); + uint8_t getSampleSize() const; + bool containsAnalog() const; + bool containsDigital() const; /** * Returns true if the specified analog pin is enabled */ - bool isAnalogEnabled(uint8_t pin); + bool isAnalogEnabled(uint8_t pin) const; /** * Returns true if the specified digital pin is enabled */ - bool isDigitalEnabled(uint8_t pin); + bool isDigitalEnabled(uint8_t pin) const; /** * Returns the 10-bit analog reading of the specified pin. * Valid pins include ADC:0-5. Sample index starts at 0 */ - uint16_t getAnalog(uint8_t pin, uint8_t sample); + uint16_t getAnalog(uint8_t pin, uint8_t sample) const; /** * Returns true if the specified pin is high/on. * Valid pins include DIO:0-8. Sample index starts at 0 */ - bool isDigitalOn(uint8_t pin, uint8_t sample); - uint8_t getSampleOffset(); + bool isDigitalOn(uint8_t pin, uint8_t sample) const; + uint8_t getSampleOffset() const; /** * Gets the offset of the start of the given sample. */ - uint8_t getSampleStart(uint8_t sample); + uint8_t getSampleStart(uint8_t sample) const; private: }; class Rx16IoSampleResponse : public RxIoSampleBaseResponse { public: Rx16IoSampleResponse(); - uint16_t getRemoteAddress16(); - uint8_t getRssiOffset(); + uint16_t getRemoteAddress16() const; + uint8_t getRssiOffset() const; static const uint8_t API_ID = RX_16_IO_RESPONSE; }; @@ -595,8 +595,8 @@ class Rx16IoSampleResponse : public RxIoSampleBaseResponse { class Rx64IoSampleResponse : public RxIoSampleBaseResponse { public: Rx64IoSampleResponse(); - XBeeAddress64& getRemoteAddress64(); - uint8_t getRssiOffset(); + const XBeeAddress64& getRemoteAddress64() const; + uint8_t getRssiOffset() const; static const uint8_t API_ID = RX_64_IO_RESPONSE; private: @@ -611,7 +611,7 @@ class Rx64IoSampleResponse : public RxIoSampleBaseResponse { class ModemStatusResponse : public XBeeResponse { public: ModemStatusResponse(); - uint8_t getStatus(); + uint8_t getStatus() const; static const uint8_t API_ID = MODEM_STATUS_RESPONSE; }; @@ -625,25 +625,25 @@ class AtCommandResponse : public FrameIdResponse { /** * Returns an array containing the two character command */ - uint8_t* getCommand(); + uint8_t* getCommand() const; /** * Returns the command status code. * Zero represents a successful command */ - uint8_t getStatus(); + uint8_t getStatus() const; /** * Returns an array containing the command value. * This is only applicable to query commands. */ - uint8_t* getValue(); + uint8_t* getValue() const; /** * Returns the length of the command value array. */ - uint8_t getValueLength(); + uint8_t getValueLength() const; /** * Returns true if status equals AT_OK */ - bool isOk(); + bool isOk() const; static const uint8_t API_ID = AT_COMMAND_RESPONSE; }; @@ -657,33 +657,33 @@ class RemoteAtCommandResponse : public AtCommandResponse { /** * Returns an array containing the two character command */ - uint8_t* getCommand(); + uint8_t* getCommand() const; /** * Returns the command status code. * Zero represents a successful command */ - uint8_t getStatus(); + uint8_t getStatus() const; /** * Returns an array containing the command value. * This is only applicable to query commands. */ - uint8_t* getValue(); + uint8_t* getValue() const; /** * Returns the length of the command value array. */ - uint8_t getValueLength(); + uint8_t getValueLength() const; /** * Returns the 16-bit address of the remote radio */ - uint16_t getRemoteAddress16(); + uint16_t getRemoteAddress16() const; /** * Returns the 64-bit address of the remote radio */ - XBeeAddress64& getRemoteAddress64(); + const XBeeAddress64& getRemoteAddress64() const; /** * Returns true if command was successful */ - bool isOk(); + bool isOk() const; static const uint8_t API_ID = REMOTE_AT_COMMAND_RESPONSE; private: @@ -712,11 +712,11 @@ class XBeeRequest { /** * Returns the frame id */ - uint8_t getFrameId(); + uint8_t getFrameId() const; /** * Returns the API id */ - uint8_t getApiId(); + uint8_t getApiId() const; // setting = 0 makes this a pure virtual function, meaning the subclass must implement, like abstract in java /** * Starting after the frame id (pos = 0) and up to but not including the checksum @@ -724,11 +724,11 @@ class XBeeRequest { * The reason for this is the API ID and Frame ID are common to all requests, whereas my definition of * frame data is only the API specific data. */ - virtual uint8_t getFrameData(uint8_t pos) = 0; + virtual uint8_t getFrameData(uint8_t pos) const = 0; /** * Returns the size of the api frame (not including frame id or api id or checksum). */ - virtual uint8_t getFrameDataLength() = 0; + virtual uint8_t getFrameDataLength() const = 0; //void reset(); protected: void setApiId(uint8_t apiId); @@ -790,31 +790,32 @@ class XBee { * Starts the serial connection on the specified serial port */ void begin(Stream &serial); - void getResponse(XBeeResponse &response); + void getResponse(XBeeResponse &response) const; /** * Returns a reference to the current response * Note: once readPacket is called again this response will be overwritten! */ - XBeeResponse& getResponse(); + const XBeeResponse& getResponse() const; + XBeeResponse& getResponse(); /** * Sends a XBeeRequest (TX packet) out the serial port */ - void send(XBeeRequest &request); + void send(XBeeRequest &request) const; //uint8_t sendAndWaitForResponse(XBeeRequest &request, int timeout); /** * Returns a sequential frame id between 1 and 255 */ - uint8_t getNextFrameId(); + uint8_t getNextFrameId() const; /** * Specify the serial port. Only relevant for Arduinos that support multiple serial ports (e.g. Mega) */ void setSerial(Stream &serial); private: - bool available(); + bool available() const; uint8_t read(); - void flush(); - void write(uint8_t val); - void sendByte(uint8_t b, bool escape); + void flush() const; + void write(uint8_t val) const; + void sendByte(uint8_t b, bool escape) const; void resetResponse(); XBeeResponse _response; bool _escape; @@ -823,7 +824,7 @@ class XBee { // last byte read uint8_t b; uint8_t _checksumTotal; - uint8_t _nextFrameId; + mutable uint8_t _nextFrameId; // buffer for incoming RX packets. holds only the api specific frame data, starting after the api id byte and prior to checksum uint8_t _responseFrameData[MAX_FRAME_DATA_SIZE]; Stream* _serial; @@ -1071,7 +1072,7 @@ class PayloadRequest : public XBeeRequest { /** * Returns the payload of the packet, if not null */ - uint8_t* getPayload(); + uint8_t* getPayload() const; /** * Sets the payload array */ @@ -1088,7 +1089,7 @@ class PayloadRequest : public XBeeRequest { /** * Returns the length of the payload array, as specified by the user. */ - uint8_t getPayloadLength(); + uint8_t getPayloadLength() const; /** * Sets the length of the payload to include in the request. For example if the payload array * is 50 bytes and you only want the first 10 to be included in the packet, set the length to 10. @@ -1126,8 +1127,8 @@ class Tx16Request : public PayloadRequest { void setAddress16(uint16_t addr16); uint8_t getOption(); void setOption(uint8_t option); - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; protected: private: uint16_t _addr16; @@ -1159,10 +1160,10 @@ class Tx64Request : public PayloadRequest { // TODO move option to superclass uint8_t getOption(); void setOption(uint8_t option); - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; private: - XBeeAddress64 _addr64; + mutable XBeeAddress64 _addr64; uint8_t _option; }; @@ -1206,9 +1207,9 @@ class ZBTxRequest : public PayloadRequest { void setOption(uint8_t option); protected: // declare virtual functions - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); - XBeeAddress64 _addr64; + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; + mutable XBeeAddress64 _addr64; uint16_t _addr16; uint8_t _broadcastRadius; uint8_t _option; @@ -1258,8 +1259,8 @@ class ZBExplicitTxRequest : public ZBTxRequest { void setProfileId(uint16_t profileId); protected: // declare virtual functions - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; private: uint8_t _srcEndpoint; uint8_t _dstEndpoint; @@ -1278,13 +1279,13 @@ class AtCommandRequest : public XBeeRequest { AtCommandRequest(); AtCommandRequest(uint8_t *command); AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength); - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); - uint8_t* getCommand(); + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; + uint8_t* getCommand() const; void setCommand(uint8_t* command); - uint8_t* getCommandValue(); + uint8_t* getCommandValue() const; void setCommandValue(uint8_t* command); - uint8_t getCommandValueLength(); + uint8_t getCommandValueLength() const; void setCommandValueLength(uint8_t length); /** * Clears the optional commandValue and commandValueLength so that a query may be sent @@ -1326,16 +1327,16 @@ class RemoteAtCommandRequest : public AtCommandRequest { RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command); uint16_t getRemoteAddress16(); void setRemoteAddress16(uint16_t remoteAddress16); - XBeeAddress64& getRemoteAddress64(); + const XBeeAddress64& getRemoteAddress64() const; void setRemoteAddress64(XBeeAddress64 &remoteAddress64); bool getApplyChanges(); void setApplyChanges(bool applyChanges); - uint8_t getFrameData(uint8_t pos); - uint8_t getFrameDataLength(); + uint8_t getFrameData(uint8_t pos) const; + uint8_t getFrameDataLength() const; static XBeeAddress64 broadcastAddress64; // static uint16_t broadcast16Address; private: - XBeeAddress64 _remoteAddress64; + mutable XBeeAddress64 _remoteAddress64; uint16_t _remoteAddress16; bool _applyChanges; };