XBee-mbed library Forked from http://mbed.org/users/okini3939/notebook/xbee-mbed/

Fork of XBee by Suga koubou

Committer:
okini3939
Date:
Mon Nov 22 10:51:06 2010 +0000
Revision:
0:c4ca662ef73e
Child:
1:e3b2027e685c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:c4ca662ef73e 1 /**
okini3939 0:c4ca662ef73e 2 * XBee-mbed library
okini3939 0:c4ca662ef73e 3 * Modified for mbed, 2010 Suga.
okini3939 0:c4ca662ef73e 4 *
okini3939 0:c4ca662ef73e 5 *
okini3939 0:c4ca662ef73e 6 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
okini3939 0:c4ca662ef73e 7 *
okini3939 0:c4ca662ef73e 8 * This file is part of XBee-Arduino.
okini3939 0:c4ca662ef73e 9 *
okini3939 0:c4ca662ef73e 10 * XBee-Arduino is free software: you can redistribute it and/or modify
okini3939 0:c4ca662ef73e 11 * it under the terms of the GNU General Public License as published by
okini3939 0:c4ca662ef73e 12 * the Free Software Foundation, either version 3 of the License, or
okini3939 0:c4ca662ef73e 13 * (at your option) any later version.
okini3939 0:c4ca662ef73e 14 *
okini3939 0:c4ca662ef73e 15 * XBee-Arduino is distributed in the hope that it will be useful,
okini3939 0:c4ca662ef73e 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
okini3939 0:c4ca662ef73e 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
okini3939 0:c4ca662ef73e 18 * GNU General Public License for more details.
okini3939 0:c4ca662ef73e 19 *
okini3939 0:c4ca662ef73e 20 * You should have received a copy of the GNU General Public License
okini3939 0:c4ca662ef73e 21 * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
okini3939 0:c4ca662ef73e 22 */
okini3939 0:c4ca662ef73e 23
okini3939 0:c4ca662ef73e 24 #include "mbed.h"
okini3939 0:c4ca662ef73e 25 #include "XBee.h"
okini3939 0:c4ca662ef73e 26
okini3939 0:c4ca662ef73e 27 XBeeResponse::XBeeResponse() {
okini3939 0:c4ca662ef73e 28
okini3939 0:c4ca662ef73e 29 }
okini3939 0:c4ca662ef73e 30
okini3939 0:c4ca662ef73e 31 uint8_t XBeeResponse::getApiId() {
okini3939 0:c4ca662ef73e 32 return _apiId;
okini3939 0:c4ca662ef73e 33 }
okini3939 0:c4ca662ef73e 34
okini3939 0:c4ca662ef73e 35 void XBeeResponse::setApiId(uint8_t apiId) {
okini3939 0:c4ca662ef73e 36 _apiId = apiId;
okini3939 0:c4ca662ef73e 37 }
okini3939 0:c4ca662ef73e 38
okini3939 0:c4ca662ef73e 39 uint8_t XBeeResponse::getMsbLength() {
okini3939 0:c4ca662ef73e 40 return _msbLength;
okini3939 0:c4ca662ef73e 41 }
okini3939 0:c4ca662ef73e 42
okini3939 0:c4ca662ef73e 43 void XBeeResponse::setMsbLength(uint8_t msbLength) {
okini3939 0:c4ca662ef73e 44 _msbLength = msbLength;
okini3939 0:c4ca662ef73e 45 }
okini3939 0:c4ca662ef73e 46
okini3939 0:c4ca662ef73e 47 uint8_t XBeeResponse::getLsbLength() {
okini3939 0:c4ca662ef73e 48 return _lsbLength;
okini3939 0:c4ca662ef73e 49 }
okini3939 0:c4ca662ef73e 50
okini3939 0:c4ca662ef73e 51 void XBeeResponse::setLsbLength(uint8_t lsbLength) {
okini3939 0:c4ca662ef73e 52 _lsbLength = lsbLength;
okini3939 0:c4ca662ef73e 53 }
okini3939 0:c4ca662ef73e 54
okini3939 0:c4ca662ef73e 55 uint8_t XBeeResponse::getChecksum() {
okini3939 0:c4ca662ef73e 56 return _checksum;
okini3939 0:c4ca662ef73e 57 }
okini3939 0:c4ca662ef73e 58
okini3939 0:c4ca662ef73e 59 void XBeeResponse::setChecksum(uint8_t checksum) {
okini3939 0:c4ca662ef73e 60 _checksum = checksum;
okini3939 0:c4ca662ef73e 61 }
okini3939 0:c4ca662ef73e 62
okini3939 0:c4ca662ef73e 63 uint8_t XBeeResponse::getFrameDataLength() {
okini3939 0:c4ca662ef73e 64 return _frameLength;
okini3939 0:c4ca662ef73e 65 }
okini3939 0:c4ca662ef73e 66
okini3939 0:c4ca662ef73e 67 void XBeeResponse::setFrameLength(uint8_t frameLength) {
okini3939 0:c4ca662ef73e 68 _frameLength = frameLength;
okini3939 0:c4ca662ef73e 69 }
okini3939 0:c4ca662ef73e 70
okini3939 0:c4ca662ef73e 71 bool XBeeResponse::isAvailable() {
okini3939 0:c4ca662ef73e 72 return _complete;
okini3939 0:c4ca662ef73e 73 }
okini3939 0:c4ca662ef73e 74
okini3939 0:c4ca662ef73e 75 void XBeeResponse::setAvailable(bool complete) {
okini3939 0:c4ca662ef73e 76 _complete = complete;
okini3939 0:c4ca662ef73e 77 }
okini3939 0:c4ca662ef73e 78
okini3939 0:c4ca662ef73e 79 bool XBeeResponse::isError() {
okini3939 0:c4ca662ef73e 80 return _errorCode > 0;
okini3939 0:c4ca662ef73e 81 }
okini3939 0:c4ca662ef73e 82
okini3939 0:c4ca662ef73e 83 uint8_t XBeeResponse::getErrorCode() {
okini3939 0:c4ca662ef73e 84 return _errorCode;
okini3939 0:c4ca662ef73e 85 }
okini3939 0:c4ca662ef73e 86
okini3939 0:c4ca662ef73e 87 void XBeeResponse::setErrorCode(uint8_t errorCode) {
okini3939 0:c4ca662ef73e 88 _errorCode = errorCode;
okini3939 0:c4ca662ef73e 89 }
okini3939 0:c4ca662ef73e 90
okini3939 0:c4ca662ef73e 91 // copy common fields from xbee response to target response
okini3939 0:c4ca662ef73e 92 void XBeeResponse::setCommon(XBeeResponse &target) {
okini3939 0:c4ca662ef73e 93 target.setApiId(getApiId());
okini3939 0:c4ca662ef73e 94 target.setAvailable(isAvailable());
okini3939 0:c4ca662ef73e 95 target.setChecksum(getChecksum());
okini3939 0:c4ca662ef73e 96 target.setErrorCode(getErrorCode());
okini3939 0:c4ca662ef73e 97 target.setFrameLength(getFrameDataLength());
okini3939 0:c4ca662ef73e 98 target.setMsbLength(getMsbLength());
okini3939 0:c4ca662ef73e 99 target.setLsbLength(getLsbLength());
okini3939 0:c4ca662ef73e 100 }
okini3939 0:c4ca662ef73e 101
okini3939 0:c4ca662ef73e 102 #ifdef SERIES_2
okini3939 0:c4ca662ef73e 103
okini3939 0:c4ca662ef73e 104 ZBTxStatusResponse::ZBTxStatusResponse() : FrameIdResponse() {
okini3939 0:c4ca662ef73e 105
okini3939 0:c4ca662ef73e 106 }
okini3939 0:c4ca662ef73e 107
okini3939 0:c4ca662ef73e 108 uint16_t ZBTxStatusResponse::getRemoteAddress() {
okini3939 0:c4ca662ef73e 109 return (getFrameData()[1] << 8) + getFrameData()[2];
okini3939 0:c4ca662ef73e 110 }
okini3939 0:c4ca662ef73e 111
okini3939 0:c4ca662ef73e 112 uint8_t ZBTxStatusResponse::getTxRetryCount() {
okini3939 0:c4ca662ef73e 113 return getFrameData()[3];
okini3939 0:c4ca662ef73e 114 }
okini3939 0:c4ca662ef73e 115
okini3939 0:c4ca662ef73e 116 uint8_t ZBTxStatusResponse::getDeliveryStatus() {
okini3939 0:c4ca662ef73e 117 return getFrameData()[4];
okini3939 0:c4ca662ef73e 118 }
okini3939 0:c4ca662ef73e 119
okini3939 0:c4ca662ef73e 120 uint8_t ZBTxStatusResponse::getDiscoveryStatus() {
okini3939 0:c4ca662ef73e 121 return getFrameData()[5];
okini3939 0:c4ca662ef73e 122 }
okini3939 0:c4ca662ef73e 123
okini3939 0:c4ca662ef73e 124 bool ZBTxStatusResponse::isSuccess() {
okini3939 0:c4ca662ef73e 125 return getDeliveryStatus() == SUCCESS;
okini3939 0:c4ca662ef73e 126 }
okini3939 0:c4ca662ef73e 127
okini3939 0:c4ca662ef73e 128 void XBeeResponse::getZBTxStatusResponse(XBeeResponse &zbXBeeResponse) {
okini3939 0:c4ca662ef73e 129
okini3939 0:c4ca662ef73e 130 // way off?
okini3939 0:c4ca662ef73e 131 ZBTxStatusResponse* zb = static_cast<ZBTxStatusResponse*>(&zbXBeeResponse);
okini3939 0:c4ca662ef73e 132 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 133 zb->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 134 setCommon(zbXBeeResponse);
okini3939 0:c4ca662ef73e 135 }
okini3939 0:c4ca662ef73e 136
okini3939 0:c4ca662ef73e 137 ZBRxResponse::ZBRxResponse(): RxDataResponse() {
okini3939 0:c4ca662ef73e 138 _remoteAddress64 = XBeeAddress64();
okini3939 0:c4ca662ef73e 139 }
okini3939 0:c4ca662ef73e 140
okini3939 0:c4ca662ef73e 141 uint16_t ZBRxResponse::getRemoteAddress16() {
okini3939 0:c4ca662ef73e 142 return (getFrameData()[8] << 8) + getFrameData()[9];
okini3939 0:c4ca662ef73e 143 }
okini3939 0:c4ca662ef73e 144
okini3939 0:c4ca662ef73e 145 uint8_t ZBRxResponse::getOption() {
okini3939 0:c4ca662ef73e 146 return getFrameData()[10];
okini3939 0:c4ca662ef73e 147 }
okini3939 0:c4ca662ef73e 148
okini3939 0:c4ca662ef73e 149 // markers to read data from packet array. this is the index, so the 12th item in the array
okini3939 0:c4ca662ef73e 150 uint8_t ZBRxResponse::getDataOffset() {
okini3939 0:c4ca662ef73e 151 return 11;
okini3939 0:c4ca662ef73e 152 }
okini3939 0:c4ca662ef73e 153
okini3939 0:c4ca662ef73e 154 uint8_t ZBRxResponse::getDataLength() {
okini3939 0:c4ca662ef73e 155 return getPacketLength() - getDataOffset() - 1;
okini3939 0:c4ca662ef73e 156 }
okini3939 0:c4ca662ef73e 157
okini3939 0:c4ca662ef73e 158 XBeeAddress64& ZBRxResponse::getRemoteAddress64() {
okini3939 0:c4ca662ef73e 159 return _remoteAddress64;
okini3939 0:c4ca662ef73e 160 }
okini3939 0:c4ca662ef73e 161
okini3939 0:c4ca662ef73e 162 void XBeeResponse::getZBRxResponse(XBeeResponse &rxResponse) {
okini3939 0:c4ca662ef73e 163
okini3939 0:c4ca662ef73e 164 ZBRxResponse* zb = static_cast<ZBRxResponse*>(&rxResponse);
okini3939 0:c4ca662ef73e 165
okini3939 0:c4ca662ef73e 166 //TODO verify response api id matches this api for this response
okini3939 0:c4ca662ef73e 167
okini3939 0:c4ca662ef73e 168 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 169 zb->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 170 setCommon(rxResponse);
okini3939 0:c4ca662ef73e 171
okini3939 0:c4ca662ef73e 172 zb->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
okini3939 0:c4ca662ef73e 173 zb->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + (getFrameData()[7]));
okini3939 0:c4ca662ef73e 174 }
okini3939 0:c4ca662ef73e 175
okini3939 0:c4ca662ef73e 176
okini3939 0:c4ca662ef73e 177 ZBRxIoSampleResponse::ZBRxIoSampleResponse() : ZBRxResponse() {
okini3939 0:c4ca662ef73e 178
okini3939 0:c4ca662ef73e 179 }
okini3939 0:c4ca662ef73e 180
okini3939 0:c4ca662ef73e 181 // 64 + 16 addresses, sample size, option = 12 (index 11), so this starts at 12
okini3939 0:c4ca662ef73e 182 uint8_t ZBRxIoSampleResponse::getDigitalMaskMsb() {
okini3939 0:c4ca662ef73e 183 return getFrameData()[12] & 0x1c;
okini3939 0:c4ca662ef73e 184 }
okini3939 0:c4ca662ef73e 185
okini3939 0:c4ca662ef73e 186 uint8_t ZBRxIoSampleResponse::getDigitalMaskLsb() {
okini3939 0:c4ca662ef73e 187 return getFrameData()[13];
okini3939 0:c4ca662ef73e 188 }
okini3939 0:c4ca662ef73e 189
okini3939 0:c4ca662ef73e 190 uint8_t ZBRxIoSampleResponse::getAnalogMask() {
okini3939 0:c4ca662ef73e 191 return getFrameData()[14] & 0x8f;
okini3939 0:c4ca662ef73e 192 }
okini3939 0:c4ca662ef73e 193
okini3939 0:c4ca662ef73e 194 bool ZBRxIoSampleResponse::containsAnalog() {
okini3939 0:c4ca662ef73e 195 return getAnalogMask() > 0;
okini3939 0:c4ca662ef73e 196 }
okini3939 0:c4ca662ef73e 197
okini3939 0:c4ca662ef73e 198 bool ZBRxIoSampleResponse::containsDigital() {
okini3939 0:c4ca662ef73e 199 return getDigitalMaskMsb() > 0 || getDigitalMaskLsb() > 0;
okini3939 0:c4ca662ef73e 200 }
okini3939 0:c4ca662ef73e 201
okini3939 0:c4ca662ef73e 202 bool ZBRxIoSampleResponse::isAnalogEnabled(uint8_t pin) {
okini3939 0:c4ca662ef73e 203 return ((getAnalogMask() >> pin) & 1) == 1;
okini3939 0:c4ca662ef73e 204 }
okini3939 0:c4ca662ef73e 205
okini3939 0:c4ca662ef73e 206 bool ZBRxIoSampleResponse::isDigitalEnabled(uint8_t pin) {
okini3939 0:c4ca662ef73e 207 if (pin <= 7) {
okini3939 0:c4ca662ef73e 208 // added extra parens to calm avr compiler
okini3939 0:c4ca662ef73e 209 return ((getDigitalMaskLsb() >> pin) & 1) == 1;
okini3939 0:c4ca662ef73e 210 } else {
okini3939 0:c4ca662ef73e 211 return ((getDigitalMaskMsb() >> (pin - 8)) & 1) == 1;
okini3939 0:c4ca662ef73e 212 }
okini3939 0:c4ca662ef73e 213 }
okini3939 0:c4ca662ef73e 214
okini3939 0:c4ca662ef73e 215 uint16_t ZBRxIoSampleResponse::getAnalog(uint8_t pin) {
okini3939 0:c4ca662ef73e 216 // analog starts 13 bytes after sample size, if no dio enabled
okini3939 0:c4ca662ef73e 217 uint8_t start = 15;
okini3939 0:c4ca662ef73e 218
okini3939 0:c4ca662ef73e 219 if (containsDigital()) {
okini3939 0:c4ca662ef73e 220 // make room for digital i/o
okini3939 0:c4ca662ef73e 221 start+=2;
okini3939 0:c4ca662ef73e 222 }
okini3939 0:c4ca662ef73e 223
okini3939 0:c4ca662ef73e 224 // std::cout << "spacing is " << static_cast<unsigned int>(spacing) << std::endl;
okini3939 0:c4ca662ef73e 225
okini3939 0:c4ca662ef73e 226 // start depends on how many pins before this pin are enabled
okini3939 0:c4ca662ef73e 227 for (int i = 0; i < pin; i++) {
okini3939 0:c4ca662ef73e 228 if (isAnalogEnabled(pin)) {
okini3939 0:c4ca662ef73e 229 start+=2;
okini3939 0:c4ca662ef73e 230 }
okini3939 0:c4ca662ef73e 231 }
okini3939 0:c4ca662ef73e 232
okini3939 0:c4ca662ef73e 233 // std::cout << "start for analog pin ["<< static_cast<unsigned int>(pin) << "]/sample " << static_cast<unsigned int>(sample) << " is " << static_cast<unsigned int>(start) << std::endl;
okini3939 0:c4ca662ef73e 234
okini3939 0:c4ca662ef73e 235 // std::cout << "returning index " << static_cast<unsigned int>(getSampleOffset() + start) << " and index " << static_cast<unsigned int>(getSampleOffset() + start + 1) << ", val is " << static_cast<unsigned int>(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast<unsigned int>(getFrameData()[getSampleOffset() + start + 1]) << std::endl;
okini3939 0:c4ca662ef73e 236
okini3939 0:c4ca662ef73e 237 return (uint16_t)((getFrameData()[start] << 8) + getFrameData()[start + 1]);
okini3939 0:c4ca662ef73e 238 }
okini3939 0:c4ca662ef73e 239
okini3939 0:c4ca662ef73e 240 bool ZBRxIoSampleResponse::isDigitalOn(uint8_t pin) {
okini3939 0:c4ca662ef73e 241 if (pin <= 7) {
okini3939 0:c4ca662ef73e 242 // D0-7
okini3939 0:c4ca662ef73e 243 // DIO LSB is index 5
okini3939 0:c4ca662ef73e 244 return ((getFrameData()[16] >> pin) & 1) == 1;
okini3939 0:c4ca662ef73e 245 } else {
okini3939 0:c4ca662ef73e 246 // D10-12
okini3939 0:c4ca662ef73e 247 // DIO MSB is index 4
okini3939 0:c4ca662ef73e 248 return ((getFrameData()[15] >> (pin - 8)) & 1) == 1;
okini3939 0:c4ca662ef73e 249 }
okini3939 0:c4ca662ef73e 250 }
okini3939 0:c4ca662ef73e 251
okini3939 0:c4ca662ef73e 252 void XBeeResponse::getZBRxIoSampleResponse(XBeeResponse &response) {
okini3939 0:c4ca662ef73e 253 ZBRxIoSampleResponse* zb = static_cast<ZBRxIoSampleResponse*>(&response);
okini3939 0:c4ca662ef73e 254
okini3939 0:c4ca662ef73e 255
okini3939 0:c4ca662ef73e 256 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 257 zb->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 258 setCommon(response);
okini3939 0:c4ca662ef73e 259
okini3939 0:c4ca662ef73e 260 zb->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
okini3939 0:c4ca662ef73e 261 zb->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + (getFrameData()[7]));
okini3939 0:c4ca662ef73e 262 }
okini3939 0:c4ca662ef73e 263
okini3939 0:c4ca662ef73e 264 #endif
okini3939 0:c4ca662ef73e 265
okini3939 0:c4ca662ef73e 266 #ifdef SERIES_1
okini3939 0:c4ca662ef73e 267
okini3939 0:c4ca662ef73e 268 RxResponse::RxResponse() : RxDataResponse() {
okini3939 0:c4ca662ef73e 269
okini3939 0:c4ca662ef73e 270 }
okini3939 0:c4ca662ef73e 271
okini3939 0:c4ca662ef73e 272 uint16_t Rx16Response::getRemoteAddress16() {
okini3939 0:c4ca662ef73e 273 return (getFrameData()[0] << 8) + getFrameData()[1];
okini3939 0:c4ca662ef73e 274 }
okini3939 0:c4ca662ef73e 275
okini3939 0:c4ca662ef73e 276 XBeeAddress64& Rx64Response::getRemoteAddress64() {
okini3939 0:c4ca662ef73e 277 return _remoteAddress;
okini3939 0:c4ca662ef73e 278 }
okini3939 0:c4ca662ef73e 279
okini3939 0:c4ca662ef73e 280 Rx64Response::Rx64Response() : RxResponse() {
okini3939 0:c4ca662ef73e 281 _remoteAddress = XBeeAddress64();
okini3939 0:c4ca662ef73e 282 }
okini3939 0:c4ca662ef73e 283
okini3939 0:c4ca662ef73e 284 Rx16Response::Rx16Response() : RxResponse() {
okini3939 0:c4ca662ef73e 285
okini3939 0:c4ca662ef73e 286 }
okini3939 0:c4ca662ef73e 287
okini3939 0:c4ca662ef73e 288 RxIoSampleBaseResponse::RxIoSampleBaseResponse() : RxResponse() {
okini3939 0:c4ca662ef73e 289
okini3939 0:c4ca662ef73e 290 }
okini3939 0:c4ca662ef73e 291
okini3939 0:c4ca662ef73e 292 uint8_t RxIoSampleBaseResponse::getSampleOffset() {
okini3939 0:c4ca662ef73e 293 // sample starts 2 bytes after rssi
okini3939 0:c4ca662ef73e 294 return getRssiOffset() + 2;
okini3939 0:c4ca662ef73e 295 }
okini3939 0:c4ca662ef73e 296
okini3939 0:c4ca662ef73e 297
okini3939 0:c4ca662ef73e 298 uint8_t RxIoSampleBaseResponse::getSampleSize() {
okini3939 0:c4ca662ef73e 299 return getFrameData()[getSampleOffset()];
okini3939 0:c4ca662ef73e 300 }
okini3939 0:c4ca662ef73e 301
okini3939 0:c4ca662ef73e 302 bool RxIoSampleBaseResponse::containsAnalog() {
okini3939 0:c4ca662ef73e 303 return (getFrameData()[getSampleOffset() + 1] & 0x7e) > 0;
okini3939 0:c4ca662ef73e 304 }
okini3939 0:c4ca662ef73e 305
okini3939 0:c4ca662ef73e 306 bool RxIoSampleBaseResponse::containsDigital() {
okini3939 0:c4ca662ef73e 307 return (getFrameData()[getSampleOffset() + 1] & 0x1) > 0 || getFrameData()[getSampleOffset() + 2] > 0;
okini3939 0:c4ca662ef73e 308 }
okini3939 0:c4ca662ef73e 309
okini3939 0:c4ca662ef73e 310 //uint16_t RxIoSampleBaseResponse::getAnalog0(uint8_t sample) {
okini3939 0:c4ca662ef73e 311 // return getAnalog(0, sample);
okini3939 0:c4ca662ef73e 312 //}
okini3939 0:c4ca662ef73e 313
okini3939 0:c4ca662ef73e 314 bool RxIoSampleBaseResponse::isAnalogEnabled(uint8_t pin) {
okini3939 0:c4ca662ef73e 315 return (((getFrameData()[getSampleOffset() + 1] >> (pin + 1)) & 1) == 1);
okini3939 0:c4ca662ef73e 316 }
okini3939 0:c4ca662ef73e 317
okini3939 0:c4ca662ef73e 318 bool RxIoSampleBaseResponse::isDigitalEnabled(uint8_t pin) {
okini3939 0:c4ca662ef73e 319 if (pin < 8) {
okini3939 0:c4ca662ef73e 320 return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
okini3939 0:c4ca662ef73e 321 } else {
okini3939 0:c4ca662ef73e 322 return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
okini3939 0:c4ca662ef73e 323 }
okini3939 0:c4ca662ef73e 324 }
okini3939 0:c4ca662ef73e 325
okini3939 0:c4ca662ef73e 326 uint16_t RxIoSampleBaseResponse::getAnalog(uint8_t pin, uint8_t sample) {
okini3939 0:c4ca662ef73e 327
okini3939 0:c4ca662ef73e 328 // analog starts 3 bytes after sample size, if no dio enabled
okini3939 0:c4ca662ef73e 329 uint8_t start = 3;
okini3939 0:c4ca662ef73e 330
okini3939 0:c4ca662ef73e 331 if (containsDigital()) {
okini3939 0:c4ca662ef73e 332 // make room for digital i/o sample (2 bytes per sample)
okini3939 0:c4ca662ef73e 333 start+=2*(sample + 1);
okini3939 0:c4ca662ef73e 334 }
okini3939 0:c4ca662ef73e 335
okini3939 0:c4ca662ef73e 336 uint8_t spacing = 0;
okini3939 0:c4ca662ef73e 337
okini3939 0:c4ca662ef73e 338 // spacing between samples depends on how many are enabled. add one for each analog that's enabled
okini3939 0:c4ca662ef73e 339 for (int i = 0; i <= 5; i++) {
okini3939 0:c4ca662ef73e 340 if (isAnalogEnabled(i)) {
okini3939 0:c4ca662ef73e 341 // each analog is two bytes
okini3939 0:c4ca662ef73e 342 spacing+=2;
okini3939 0:c4ca662ef73e 343 }
okini3939 0:c4ca662ef73e 344 }
okini3939 0:c4ca662ef73e 345
okini3939 0:c4ca662ef73e 346 // std::cout << "spacing is " << static_cast<unsigned int>(spacing) << std::endl;
okini3939 0:c4ca662ef73e 347
okini3939 0:c4ca662ef73e 348 // start depends on how many pins before this pin are enabled
okini3939 0:c4ca662ef73e 349 for (int i = 0; i < pin; i++) {
okini3939 0:c4ca662ef73e 350 if (isAnalogEnabled(pin)) {
okini3939 0:c4ca662ef73e 351 start+=2;
okini3939 0:c4ca662ef73e 352 }
okini3939 0:c4ca662ef73e 353 }
okini3939 0:c4ca662ef73e 354
okini3939 0:c4ca662ef73e 355 start+= sample * spacing;
okini3939 0:c4ca662ef73e 356
okini3939 0:c4ca662ef73e 357 // std::cout << "start for analog pin ["<< static_cast<unsigned int>(pin) << "]/sample " << static_cast<unsigned int>(sample) << " is " << static_cast<unsigned int>(start) << std::endl;
okini3939 0:c4ca662ef73e 358
okini3939 0:c4ca662ef73e 359 // std::cout << "returning index " << static_cast<unsigned int>(getSampleOffset() + start) << " and index " << static_cast<unsigned int>(getSampleOffset() + start + 1) << ", val is " << static_cast<unsigned int>(getFrameData()[getSampleOffset() + start] << 8) << " and " << + static_cast<unsigned int>(getFrameData()[getSampleOffset() + start + 1]) << std::endl;
okini3939 0:c4ca662ef73e 360
okini3939 0:c4ca662ef73e 361 return (uint16_t)((getFrameData()[getSampleOffset() + start] << 8) + getFrameData()[getSampleOffset() + start + 1]);
okini3939 0:c4ca662ef73e 362 }
okini3939 0:c4ca662ef73e 363
okini3939 0:c4ca662ef73e 364 bool RxIoSampleBaseResponse::isDigitalOn(uint8_t pin, uint8_t sample) {
okini3939 0:c4ca662ef73e 365 if (pin < 8) {
okini3939 0:c4ca662ef73e 366 return ((getFrameData()[getSampleOffset() + 4] >> pin) & 1) == 1;
okini3939 0:c4ca662ef73e 367 } else {
okini3939 0:c4ca662ef73e 368 return (getFrameData()[getSampleOffset() + 3] & 1) == 1;
okini3939 0:c4ca662ef73e 369 }
okini3939 0:c4ca662ef73e 370 }
okini3939 0:c4ca662ef73e 371
okini3939 0:c4ca662ef73e 372
okini3939 0:c4ca662ef73e 373 //bool RxIoSampleBaseResponse::isDigital0On(uint8_t sample) {
okini3939 0:c4ca662ef73e 374 // return isDigitalOn(0, sample);
okini3939 0:c4ca662ef73e 375 //}
okini3939 0:c4ca662ef73e 376
okini3939 0:c4ca662ef73e 377 Rx16IoSampleResponse::Rx16IoSampleResponse() : RxIoSampleBaseResponse() {
okini3939 0:c4ca662ef73e 378
okini3939 0:c4ca662ef73e 379 }
okini3939 0:c4ca662ef73e 380
okini3939 0:c4ca662ef73e 381 uint16_t Rx16IoSampleResponse::getRemoteAddress16() {
okini3939 0:c4ca662ef73e 382 return (uint16_t)((getFrameData()[0] << 8) + getFrameData()[1]);
okini3939 0:c4ca662ef73e 383 }
okini3939 0:c4ca662ef73e 384
okini3939 0:c4ca662ef73e 385 uint8_t Rx16IoSampleResponse::getRssiOffset() {
okini3939 0:c4ca662ef73e 386 return 2;
okini3939 0:c4ca662ef73e 387 }
okini3939 0:c4ca662ef73e 388
okini3939 0:c4ca662ef73e 389 void XBeeResponse::getRx16IoSampleResponse(XBeeResponse &response) {
okini3939 0:c4ca662ef73e 390 Rx16IoSampleResponse* rx = static_cast<Rx16IoSampleResponse*>(&response);
okini3939 0:c4ca662ef73e 391
okini3939 0:c4ca662ef73e 392 rx->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 393 setCommon(response);
okini3939 0:c4ca662ef73e 394 }
okini3939 0:c4ca662ef73e 395
okini3939 0:c4ca662ef73e 396
okini3939 0:c4ca662ef73e 397 Rx64IoSampleResponse::Rx64IoSampleResponse() : RxIoSampleBaseResponse() {
okini3939 0:c4ca662ef73e 398 _remoteAddress = XBeeAddress64();
okini3939 0:c4ca662ef73e 399 }
okini3939 0:c4ca662ef73e 400
okini3939 0:c4ca662ef73e 401 XBeeAddress64& Rx64IoSampleResponse::getRemoteAddress64() {
okini3939 0:c4ca662ef73e 402 return _remoteAddress;
okini3939 0:c4ca662ef73e 403 }
okini3939 0:c4ca662ef73e 404
okini3939 0:c4ca662ef73e 405 uint8_t Rx64IoSampleResponse::getRssiOffset() {
okini3939 0:c4ca662ef73e 406 return 8;
okini3939 0:c4ca662ef73e 407 }
okini3939 0:c4ca662ef73e 408
okini3939 0:c4ca662ef73e 409 void XBeeResponse::getRx64IoSampleResponse(XBeeResponse &response) {
okini3939 0:c4ca662ef73e 410 Rx64IoSampleResponse* rx = static_cast<Rx64IoSampleResponse*>(&response);
okini3939 0:c4ca662ef73e 411
okini3939 0:c4ca662ef73e 412 rx->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 413 setCommon(response);
okini3939 0:c4ca662ef73e 414
okini3939 0:c4ca662ef73e 415 rx->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
okini3939 0:c4ca662ef73e 416 rx->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + getFrameData()[7]);
okini3939 0:c4ca662ef73e 417 }
okini3939 0:c4ca662ef73e 418
okini3939 0:c4ca662ef73e 419 TxStatusResponse::TxStatusResponse() : FrameIdResponse() {
okini3939 0:c4ca662ef73e 420
okini3939 0:c4ca662ef73e 421 }
okini3939 0:c4ca662ef73e 422
okini3939 0:c4ca662ef73e 423 uint8_t TxStatusResponse::getStatus() {
okini3939 0:c4ca662ef73e 424 return getFrameData()[1];
okini3939 0:c4ca662ef73e 425 }
okini3939 0:c4ca662ef73e 426
okini3939 0:c4ca662ef73e 427 bool TxStatusResponse::isSuccess() {
okini3939 0:c4ca662ef73e 428 return getStatus() == SUCCESS;
okini3939 0:c4ca662ef73e 429 }
okini3939 0:c4ca662ef73e 430
okini3939 0:c4ca662ef73e 431 void XBeeResponse::getTxStatusResponse(XBeeResponse &txResponse) {
okini3939 0:c4ca662ef73e 432
okini3939 0:c4ca662ef73e 433 TxStatusResponse* txStatus = static_cast<TxStatusResponse*>(&txResponse);
okini3939 0:c4ca662ef73e 434
okini3939 0:c4ca662ef73e 435 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 436 txStatus->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 437 setCommon(txResponse);
okini3939 0:c4ca662ef73e 438 }
okini3939 0:c4ca662ef73e 439
okini3939 0:c4ca662ef73e 440 uint8_t RxResponse::getRssi() {
okini3939 0:c4ca662ef73e 441 return getFrameData()[getRssiOffset()];
okini3939 0:c4ca662ef73e 442 }
okini3939 0:c4ca662ef73e 443
okini3939 0:c4ca662ef73e 444 uint8_t RxResponse::getOption() {
okini3939 0:c4ca662ef73e 445 return getFrameData()[getRssiOffset() + 1];
okini3939 0:c4ca662ef73e 446 }
okini3939 0:c4ca662ef73e 447
okini3939 0:c4ca662ef73e 448 bool RxResponse::isAddressBroadcast() {
okini3939 0:c4ca662ef73e 449 return (getOption() & 2) == 2;
okini3939 0:c4ca662ef73e 450 }
okini3939 0:c4ca662ef73e 451
okini3939 0:c4ca662ef73e 452 bool RxResponse::isPanBroadcast() {
okini3939 0:c4ca662ef73e 453 return (getOption() & 4) == 4;
okini3939 0:c4ca662ef73e 454 }
okini3939 0:c4ca662ef73e 455
okini3939 0:c4ca662ef73e 456 uint8_t RxResponse::getDataLength() {
okini3939 0:c4ca662ef73e 457 return getPacketLength() - getDataOffset() - 1;
okini3939 0:c4ca662ef73e 458 }
okini3939 0:c4ca662ef73e 459
okini3939 0:c4ca662ef73e 460 uint8_t RxResponse::getDataOffset() {
okini3939 0:c4ca662ef73e 461 return getRssiOffset() + 2;
okini3939 0:c4ca662ef73e 462 }
okini3939 0:c4ca662ef73e 463
okini3939 0:c4ca662ef73e 464 uint8_t Rx16Response::getRssiOffset() {
okini3939 0:c4ca662ef73e 465 return RX_16_RSSI_OFFSET;
okini3939 0:c4ca662ef73e 466 }
okini3939 0:c4ca662ef73e 467
okini3939 0:c4ca662ef73e 468 void XBeeResponse::getRx16Response(XBeeResponse &rx16Response) {
okini3939 0:c4ca662ef73e 469
okini3939 0:c4ca662ef73e 470 Rx16Response* rx16 = static_cast<Rx16Response*>(&rx16Response);
okini3939 0:c4ca662ef73e 471
okini3939 0:c4ca662ef73e 472 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 473 rx16->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 474 setCommon(rx16Response);
okini3939 0:c4ca662ef73e 475
okini3939 0:c4ca662ef73e 476 // rx16->getRemoteAddress16().setAddress((getFrameData()[0] << 8) + getFrameData()[1]);
okini3939 0:c4ca662ef73e 477 }
okini3939 0:c4ca662ef73e 478
okini3939 0:c4ca662ef73e 479 uint8_t Rx64Response::getRssiOffset() {
okini3939 0:c4ca662ef73e 480 return RX_64_RSSI_OFFSET;
okini3939 0:c4ca662ef73e 481 }
okini3939 0:c4ca662ef73e 482
okini3939 0:c4ca662ef73e 483 void XBeeResponse::getRx64Response(XBeeResponse &rx64Response) {
okini3939 0:c4ca662ef73e 484
okini3939 0:c4ca662ef73e 485 Rx64Response* rx64 = static_cast<Rx64Response*>(&rx64Response);
okini3939 0:c4ca662ef73e 486
okini3939 0:c4ca662ef73e 487 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 488 rx64->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 489 setCommon(rx64Response);
okini3939 0:c4ca662ef73e 490
okini3939 0:c4ca662ef73e 491 rx64->getRemoteAddress64().setMsb((uint32_t(getFrameData()[0]) << 24) + (uint32_t(getFrameData()[1]) << 16) + (uint16_t(getFrameData()[2]) << 8) + getFrameData()[3]);
okini3939 0:c4ca662ef73e 492 rx64->getRemoteAddress64().setLsb((uint32_t(getFrameData()[4]) << 24) + (uint32_t(getFrameData()[5]) << 16) + (uint16_t(getFrameData()[6]) << 8) + getFrameData()[7]);
okini3939 0:c4ca662ef73e 493 }
okini3939 0:c4ca662ef73e 494
okini3939 0:c4ca662ef73e 495 #endif
okini3939 0:c4ca662ef73e 496
okini3939 0:c4ca662ef73e 497 RemoteAtCommandResponse::RemoteAtCommandResponse() : AtCommandResponse() {
okini3939 0:c4ca662ef73e 498
okini3939 0:c4ca662ef73e 499 }
okini3939 0:c4ca662ef73e 500
okini3939 0:c4ca662ef73e 501 uint8_t* RemoteAtCommandResponse::getCommand() {
okini3939 0:c4ca662ef73e 502 return getFrameData() + 11;
okini3939 0:c4ca662ef73e 503 }
okini3939 0:c4ca662ef73e 504
okini3939 0:c4ca662ef73e 505 uint8_t RemoteAtCommandResponse::getStatus() {
okini3939 0:c4ca662ef73e 506 return getFrameData()[13];
okini3939 0:c4ca662ef73e 507 }
okini3939 0:c4ca662ef73e 508
okini3939 0:c4ca662ef73e 509 bool RemoteAtCommandResponse::isOk() {
okini3939 0:c4ca662ef73e 510 // weird c++ behavior. w/o this method, it calls AtCommandResponse::isOk(), which calls the AtCommandResponse::getStatus, not this.getStatus!!!
okini3939 0:c4ca662ef73e 511 return getStatus() == AT_OK;
okini3939 0:c4ca662ef73e 512 }
okini3939 0:c4ca662ef73e 513
okini3939 0:c4ca662ef73e 514 uint8_t RemoteAtCommandResponse::getValueLength() {
okini3939 0:c4ca662ef73e 515 return getFrameDataLength() - 14;
okini3939 0:c4ca662ef73e 516 }
okini3939 0:c4ca662ef73e 517
okini3939 0:c4ca662ef73e 518 uint8_t* RemoteAtCommandResponse::getValue() {
okini3939 0:c4ca662ef73e 519 if (getValueLength() > 0) {
okini3939 0:c4ca662ef73e 520 // value is only included for query commands. set commands does not return a value
okini3939 0:c4ca662ef73e 521 return getFrameData() + 14;
okini3939 0:c4ca662ef73e 522 }
okini3939 0:c4ca662ef73e 523
okini3939 0:c4ca662ef73e 524 return NULL;
okini3939 0:c4ca662ef73e 525 }
okini3939 0:c4ca662ef73e 526
okini3939 0:c4ca662ef73e 527 uint16_t RemoteAtCommandResponse::getRemoteAddress16() {
okini3939 0:c4ca662ef73e 528 return uint16_t((getFrameData()[9] << 8) + getFrameData()[10]);
okini3939 0:c4ca662ef73e 529 }
okini3939 0:c4ca662ef73e 530
okini3939 0:c4ca662ef73e 531 XBeeAddress64& RemoteAtCommandResponse::getRemoteAddress64() {
okini3939 0:c4ca662ef73e 532 return _remoteAddress64;
okini3939 0:c4ca662ef73e 533 }
okini3939 0:c4ca662ef73e 534
okini3939 0:c4ca662ef73e 535 void XBeeResponse::getRemoteAtCommandResponse(XBeeResponse &response) {
okini3939 0:c4ca662ef73e 536
okini3939 0:c4ca662ef73e 537 // TODO no real need to cast. change arg to match expected class
okini3939 0:c4ca662ef73e 538 RemoteAtCommandResponse* at = static_cast<RemoteAtCommandResponse*>(&response);
okini3939 0:c4ca662ef73e 539
okini3939 0:c4ca662ef73e 540 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 541 at->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 542 setCommon(response);
okini3939 0:c4ca662ef73e 543
okini3939 0:c4ca662ef73e 544 at->getRemoteAddress64().setMsb((uint32_t(getFrameData()[1]) << 24) + (uint32_t(getFrameData()[2]) << 16) + (uint16_t(getFrameData()[3]) << 8) + getFrameData()[4]);
okini3939 0:c4ca662ef73e 545 at->getRemoteAddress64().setLsb((uint32_t(getFrameData()[5]) << 24) + (uint32_t(getFrameData()[6]) << 16) + (uint16_t(getFrameData()[7]) << 8) + (getFrameData()[8]));
okini3939 0:c4ca662ef73e 546
okini3939 0:c4ca662ef73e 547 }
okini3939 0:c4ca662ef73e 548
okini3939 0:c4ca662ef73e 549 RxDataResponse::RxDataResponse() : XBeeResponse() {
okini3939 0:c4ca662ef73e 550
okini3939 0:c4ca662ef73e 551 }
okini3939 0:c4ca662ef73e 552
okini3939 0:c4ca662ef73e 553 uint8_t RxDataResponse::getData(int index) {
okini3939 0:c4ca662ef73e 554 return getFrameData()[getDataOffset() + index];
okini3939 0:c4ca662ef73e 555 }
okini3939 0:c4ca662ef73e 556
okini3939 0:c4ca662ef73e 557 uint8_t* RxDataResponse::getData() {
okini3939 0:c4ca662ef73e 558 return getFrameData() + getDataOffset();
okini3939 0:c4ca662ef73e 559 }
okini3939 0:c4ca662ef73e 560
okini3939 0:c4ca662ef73e 561 FrameIdResponse::FrameIdResponse() {
okini3939 0:c4ca662ef73e 562
okini3939 0:c4ca662ef73e 563 }
okini3939 0:c4ca662ef73e 564
okini3939 0:c4ca662ef73e 565 uint8_t FrameIdResponse::getFrameId() {
okini3939 0:c4ca662ef73e 566 return getFrameData()[0];
okini3939 0:c4ca662ef73e 567 }
okini3939 0:c4ca662ef73e 568
okini3939 0:c4ca662ef73e 569
okini3939 0:c4ca662ef73e 570 ModemStatusResponse::ModemStatusResponse() {
okini3939 0:c4ca662ef73e 571
okini3939 0:c4ca662ef73e 572 }
okini3939 0:c4ca662ef73e 573
okini3939 0:c4ca662ef73e 574 uint8_t ModemStatusResponse::getStatus() {
okini3939 0:c4ca662ef73e 575 return getFrameData()[0];
okini3939 0:c4ca662ef73e 576 }
okini3939 0:c4ca662ef73e 577
okini3939 0:c4ca662ef73e 578 void XBeeResponse::getModemStatusResponse(XBeeResponse &modemStatusResponse) {
okini3939 0:c4ca662ef73e 579
okini3939 0:c4ca662ef73e 580 ModemStatusResponse* modem = static_cast<ModemStatusResponse*>(&modemStatusResponse);
okini3939 0:c4ca662ef73e 581
okini3939 0:c4ca662ef73e 582 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 583 modem->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 584 setCommon(modemStatusResponse);
okini3939 0:c4ca662ef73e 585
okini3939 0:c4ca662ef73e 586 }
okini3939 0:c4ca662ef73e 587
okini3939 0:c4ca662ef73e 588 AtCommandResponse::AtCommandResponse() {
okini3939 0:c4ca662ef73e 589
okini3939 0:c4ca662ef73e 590 }
okini3939 0:c4ca662ef73e 591
okini3939 0:c4ca662ef73e 592 uint8_t* AtCommandResponse::getCommand() {
okini3939 0:c4ca662ef73e 593 return getFrameData() + 1;
okini3939 0:c4ca662ef73e 594 }
okini3939 0:c4ca662ef73e 595
okini3939 0:c4ca662ef73e 596 uint8_t AtCommandResponse::getStatus() {
okini3939 0:c4ca662ef73e 597 return getFrameData()[3];
okini3939 0:c4ca662ef73e 598 }
okini3939 0:c4ca662ef73e 599
okini3939 0:c4ca662ef73e 600 uint8_t AtCommandResponse::getValueLength() {
okini3939 0:c4ca662ef73e 601 return getFrameDataLength() - 4;
okini3939 0:c4ca662ef73e 602 }
okini3939 0:c4ca662ef73e 603
okini3939 0:c4ca662ef73e 604 uint8_t* AtCommandResponse::getValue() {
okini3939 0:c4ca662ef73e 605 if (getValueLength() > 0) {
okini3939 0:c4ca662ef73e 606 // value is only included for query commands. set commands does not return a value
okini3939 0:c4ca662ef73e 607 return getFrameData() + 4;
okini3939 0:c4ca662ef73e 608 }
okini3939 0:c4ca662ef73e 609
okini3939 0:c4ca662ef73e 610 return NULL;
okini3939 0:c4ca662ef73e 611 }
okini3939 0:c4ca662ef73e 612
okini3939 0:c4ca662ef73e 613 bool AtCommandResponse::isOk() {
okini3939 0:c4ca662ef73e 614 return getStatus() == AT_OK;
okini3939 0:c4ca662ef73e 615 }
okini3939 0:c4ca662ef73e 616
okini3939 0:c4ca662ef73e 617 void XBeeResponse::getAtCommandResponse(XBeeResponse &atCommandResponse) {
okini3939 0:c4ca662ef73e 618
okini3939 0:c4ca662ef73e 619 AtCommandResponse* at = static_cast<AtCommandResponse*>(&atCommandResponse);
okini3939 0:c4ca662ef73e 620
okini3939 0:c4ca662ef73e 621 // pass pointer array to subclass
okini3939 0:c4ca662ef73e 622 at->setFrameData(getFrameData());
okini3939 0:c4ca662ef73e 623 setCommon(atCommandResponse);
okini3939 0:c4ca662ef73e 624 }
okini3939 0:c4ca662ef73e 625
okini3939 0:c4ca662ef73e 626 uint16_t XBeeResponse::getPacketLength() {
okini3939 0:c4ca662ef73e 627 return ((_msbLength << 8) & 0xff) + (_lsbLength & 0xff);
okini3939 0:c4ca662ef73e 628 }
okini3939 0:c4ca662ef73e 629
okini3939 0:c4ca662ef73e 630 uint8_t* XBeeResponse::getFrameData() {
okini3939 0:c4ca662ef73e 631 return _frameDataPtr;
okini3939 0:c4ca662ef73e 632 }
okini3939 0:c4ca662ef73e 633
okini3939 0:c4ca662ef73e 634 void XBeeResponse::setFrameData(uint8_t* frameDataPtr) {
okini3939 0:c4ca662ef73e 635 _frameDataPtr = frameDataPtr;
okini3939 0:c4ca662ef73e 636 }
okini3939 0:c4ca662ef73e 637
okini3939 0:c4ca662ef73e 638 void XBeeResponse::init() {
okini3939 0:c4ca662ef73e 639 _complete = false;
okini3939 0:c4ca662ef73e 640 _errorCode = NO_ERROR;
okini3939 0:c4ca662ef73e 641 _checksum = 0;
okini3939 0:c4ca662ef73e 642 }
okini3939 0:c4ca662ef73e 643
okini3939 0:c4ca662ef73e 644 void XBeeResponse::reset() {
okini3939 0:c4ca662ef73e 645 init();
okini3939 0:c4ca662ef73e 646 _apiId = 0;
okini3939 0:c4ca662ef73e 647 _msbLength = 0;
okini3939 0:c4ca662ef73e 648 _lsbLength = 0;
okini3939 0:c4ca662ef73e 649 _checksum = 0;
okini3939 0:c4ca662ef73e 650 _frameLength = 0;
okini3939 0:c4ca662ef73e 651
okini3939 0:c4ca662ef73e 652 for (int i = 0; i < MAX_FRAME_DATA_SIZE; i++) {
okini3939 0:c4ca662ef73e 653 getFrameData()[i] = 0;
okini3939 0:c4ca662ef73e 654 }
okini3939 0:c4ca662ef73e 655 }
okini3939 0:c4ca662ef73e 656
okini3939 0:c4ca662ef73e 657 void XBee::resetResponse() {
okini3939 0:c4ca662ef73e 658 _pos = 0;
okini3939 0:c4ca662ef73e 659 _escape = false;
okini3939 0:c4ca662ef73e 660 _response.reset();
okini3939 0:c4ca662ef73e 661 }
okini3939 0:c4ca662ef73e 662
okini3939 0:c4ca662ef73e 663 XBee::XBee(PinName p_tx, PinName p_rx): _xbee(p_tx, p_rx), _response(XBeeResponse()) {
okini3939 0:c4ca662ef73e 664 _pos = 0;
okini3939 0:c4ca662ef73e 665 _escape = false;
okini3939 0:c4ca662ef73e 666 _checksumTotal = 0;
okini3939 0:c4ca662ef73e 667 _nextFrameId = 0;
okini3939 0:c4ca662ef73e 668
okini3939 0:c4ca662ef73e 669 _response.init();
okini3939 0:c4ca662ef73e 670 _response.setFrameData(_responseFrameData);
okini3939 0:c4ca662ef73e 671 }
okini3939 0:c4ca662ef73e 672
okini3939 0:c4ca662ef73e 673 uint8_t XBee::getNextFrameId() {
okini3939 0:c4ca662ef73e 674
okini3939 0:c4ca662ef73e 675 _nextFrameId++;
okini3939 0:c4ca662ef73e 676
okini3939 0:c4ca662ef73e 677 if (_nextFrameId == 0) {
okini3939 0:c4ca662ef73e 678 // can't send 0 because that disables status response
okini3939 0:c4ca662ef73e 679 _nextFrameId = 1;
okini3939 0:c4ca662ef73e 680 }
okini3939 0:c4ca662ef73e 681
okini3939 0:c4ca662ef73e 682 return _nextFrameId;
okini3939 0:c4ca662ef73e 683 }
okini3939 0:c4ca662ef73e 684
okini3939 0:c4ca662ef73e 685 void XBee::begin(long baud) {
okini3939 0:c4ca662ef73e 686 _xbee.baud(baud);
okini3939 0:c4ca662ef73e 687 }
okini3939 0:c4ca662ef73e 688
okini3939 0:c4ca662ef73e 689 /*
okini3939 0:c4ca662ef73e 690 void XBee::setSerial(HardwareSerial serial) {
okini3939 0:c4ca662ef73e 691 Serial = serial;
okini3939 0:c4ca662ef73e 692 }
okini3939 0:c4ca662ef73e 693 */
okini3939 0:c4ca662ef73e 694
okini3939 0:c4ca662ef73e 695 XBeeResponse& XBee::getResponse() {
okini3939 0:c4ca662ef73e 696 return _response;
okini3939 0:c4ca662ef73e 697 }
okini3939 0:c4ca662ef73e 698
okini3939 0:c4ca662ef73e 699 // TODO how to convert response to proper subclass?
okini3939 0:c4ca662ef73e 700 void XBee::getResponse(XBeeResponse &response) {
okini3939 0:c4ca662ef73e 701
okini3939 0:c4ca662ef73e 702 response.setMsbLength(_response.getMsbLength());
okini3939 0:c4ca662ef73e 703 response.setLsbLength(_response.getLsbLength());
okini3939 0:c4ca662ef73e 704 response.setApiId(_response.getApiId());
okini3939 0:c4ca662ef73e 705 response.setFrameLength(_response.getFrameDataLength());
okini3939 0:c4ca662ef73e 706
okini3939 0:c4ca662ef73e 707 response.setFrameData(_response.getFrameData());
okini3939 0:c4ca662ef73e 708 }
okini3939 0:c4ca662ef73e 709
okini3939 0:c4ca662ef73e 710 void XBee::readPacketUntilAvailable() {
okini3939 0:c4ca662ef73e 711 while (!(getResponse().isAvailable() || getResponse().isError())) {
okini3939 0:c4ca662ef73e 712 // read some more
okini3939 0:c4ca662ef73e 713 readPacket();
okini3939 0:c4ca662ef73e 714 }
okini3939 0:c4ca662ef73e 715 }
okini3939 0:c4ca662ef73e 716
okini3939 0:c4ca662ef73e 717 bool XBee::readPacket(int timeout) {
okini3939 0:c4ca662ef73e 718 Timer t;
okini3939 0:c4ca662ef73e 719
okini3939 0:c4ca662ef73e 720 if (timeout < 0) {
okini3939 0:c4ca662ef73e 721 return false;
okini3939 0:c4ca662ef73e 722 }
okini3939 0:c4ca662ef73e 723
okini3939 0:c4ca662ef73e 724 /*
okini3939 0:c4ca662ef73e 725 unsigned long start = millis();
okini3939 0:c4ca662ef73e 726
okini3939 0:c4ca662ef73e 727 while (int((millis() - start)) < timeout) {
okini3939 0:c4ca662ef73e 728 */
okini3939 0:c4ca662ef73e 729 t.start();
okini3939 0:c4ca662ef73e 730 while (t.read_ms() < timeout) {
okini3939 0:c4ca662ef73e 731 readPacket();
okini3939 0:c4ca662ef73e 732
okini3939 0:c4ca662ef73e 733 if (getResponse().isAvailable()) {
okini3939 0:c4ca662ef73e 734 t.stop();
okini3939 0:c4ca662ef73e 735 return true;
okini3939 0:c4ca662ef73e 736 } else if (getResponse().isError()) {
okini3939 0:c4ca662ef73e 737 t.stop();
okini3939 0:c4ca662ef73e 738 return false;
okini3939 0:c4ca662ef73e 739 }
okini3939 0:c4ca662ef73e 740 }
okini3939 0:c4ca662ef73e 741
okini3939 0:c4ca662ef73e 742 // timed out
okini3939 0:c4ca662ef73e 743 t.stop();
okini3939 0:c4ca662ef73e 744 return false;
okini3939 0:c4ca662ef73e 745 }
okini3939 0:c4ca662ef73e 746
okini3939 0:c4ca662ef73e 747 void XBee::readPacket() {
okini3939 0:c4ca662ef73e 748 // reset previous response
okini3939 0:c4ca662ef73e 749 if (_response.isAvailable() || _response.isError()) {
okini3939 0:c4ca662ef73e 750 // discard previous packet and start over
okini3939 0:c4ca662ef73e 751 resetResponse();
okini3939 0:c4ca662ef73e 752 }
okini3939 0:c4ca662ef73e 753
okini3939 0:c4ca662ef73e 754 while (_xbee.readable()) {
okini3939 0:c4ca662ef73e 755
okini3939 0:c4ca662ef73e 756 b = _xbee.getc();
okini3939 0:c4ca662ef73e 757
okini3939 0:c4ca662ef73e 758 if (_pos > 0 && b == START_BYTE && ATAP == 2) {
okini3939 0:c4ca662ef73e 759 // new packet start before previous packeted completed -- discard previous packet and start over
okini3939 0:c4ca662ef73e 760 _response.setErrorCode(UNEXPECTED_START_BYTE);
okini3939 0:c4ca662ef73e 761 return;
okini3939 0:c4ca662ef73e 762 }
okini3939 0:c4ca662ef73e 763
okini3939 0:c4ca662ef73e 764 if (_pos > 0 && b == ESCAPE) {
okini3939 0:c4ca662ef73e 765 if (_xbee.readable()) {
okini3939 0:c4ca662ef73e 766 b = _xbee.getc();
okini3939 0:c4ca662ef73e 767 b = 0x20 ^ b;
okini3939 0:c4ca662ef73e 768 } else {
okini3939 0:c4ca662ef73e 769 // escape byte. next byte will be
okini3939 0:c4ca662ef73e 770 _escape = true;
okini3939 0:c4ca662ef73e 771 continue;
okini3939 0:c4ca662ef73e 772 }
okini3939 0:c4ca662ef73e 773 }
okini3939 0:c4ca662ef73e 774
okini3939 0:c4ca662ef73e 775 if (_escape == true) {
okini3939 0:c4ca662ef73e 776 b = 0x20 ^ b;
okini3939 0:c4ca662ef73e 777 _escape = false;
okini3939 0:c4ca662ef73e 778 }
okini3939 0:c4ca662ef73e 779
okini3939 0:c4ca662ef73e 780 // checksum includes all bytes starting with api id
okini3939 0:c4ca662ef73e 781 if (_pos >= API_ID_INDEX) {
okini3939 0:c4ca662ef73e 782 _checksumTotal+= b;
okini3939 0:c4ca662ef73e 783 }
okini3939 0:c4ca662ef73e 784
okini3939 0:c4ca662ef73e 785 switch(_pos) {
okini3939 0:c4ca662ef73e 786 case 0:
okini3939 0:c4ca662ef73e 787 if (b == START_BYTE) {
okini3939 0:c4ca662ef73e 788 _pos++;
okini3939 0:c4ca662ef73e 789 }
okini3939 0:c4ca662ef73e 790
okini3939 0:c4ca662ef73e 791 break;
okini3939 0:c4ca662ef73e 792 case 1:
okini3939 0:c4ca662ef73e 793 // length msb
okini3939 0:c4ca662ef73e 794 _response.setMsbLength(b);
okini3939 0:c4ca662ef73e 795 _pos++;
okini3939 0:c4ca662ef73e 796
okini3939 0:c4ca662ef73e 797 break;
okini3939 0:c4ca662ef73e 798 case 2:
okini3939 0:c4ca662ef73e 799 // length lsb
okini3939 0:c4ca662ef73e 800 _response.setLsbLength(b);
okini3939 0:c4ca662ef73e 801 _pos++;
okini3939 0:c4ca662ef73e 802
okini3939 0:c4ca662ef73e 803 break;
okini3939 0:c4ca662ef73e 804 case 3:
okini3939 0:c4ca662ef73e 805 _response.setApiId(b);
okini3939 0:c4ca662ef73e 806 _pos++;
okini3939 0:c4ca662ef73e 807
okini3939 0:c4ca662ef73e 808 break;
okini3939 0:c4ca662ef73e 809 default:
okini3939 0:c4ca662ef73e 810 // starts at fifth byte
okini3939 0:c4ca662ef73e 811
okini3939 0:c4ca662ef73e 812 if (_pos > MAX_FRAME_DATA_SIZE) {
okini3939 0:c4ca662ef73e 813 // exceed max size. should never occur
okini3939 0:c4ca662ef73e 814 _response.setErrorCode(PACKET_EXCEEDS_BYTE_ARRAY_LENGTH);
okini3939 0:c4ca662ef73e 815 return;
okini3939 0:c4ca662ef73e 816 }
okini3939 0:c4ca662ef73e 817
okini3939 0:c4ca662ef73e 818 // check if we're at the end of the packet
okini3939 0:c4ca662ef73e 819 // packet length does not include start, length, or checksum bytes, so add 3
okini3939 0:c4ca662ef73e 820 if (_pos == (_response.getPacketLength() + 3)) {
okini3939 0:c4ca662ef73e 821 // verify checksum
okini3939 0:c4ca662ef73e 822
okini3939 0:c4ca662ef73e 823 //std::cout << "read checksum " << static_cast<unsigned int>(b) << " at pos " << static_cast<unsigned int>(_pos) << std::endl;
okini3939 0:c4ca662ef73e 824
okini3939 0:c4ca662ef73e 825 if ((_checksumTotal & 0xff) == 0xff) {
okini3939 0:c4ca662ef73e 826 _response.setChecksum(b);
okini3939 0:c4ca662ef73e 827 _response.setAvailable(true);
okini3939 0:c4ca662ef73e 828
okini3939 0:c4ca662ef73e 829 _response.setErrorCode(NO_ERROR);
okini3939 0:c4ca662ef73e 830 } else {
okini3939 0:c4ca662ef73e 831 // checksum failed
okini3939 0:c4ca662ef73e 832 _response.setErrorCode(CHECKSUM_FAILURE);
okini3939 0:c4ca662ef73e 833 }
okini3939 0:c4ca662ef73e 834
okini3939 0:c4ca662ef73e 835 // minus 4 because we start after start,msb,lsb,api and up to but not including checksum
okini3939 0:c4ca662ef73e 836 // e.g. if frame was one byte, _pos=4 would be the byte, pos=5 is the checksum, where end stop reading
okini3939 0:c4ca662ef73e 837 _response.setFrameLength(_pos - 4);
okini3939 0:c4ca662ef73e 838
okini3939 0:c4ca662ef73e 839 // reset state vars
okini3939 0:c4ca662ef73e 840 _pos = 0;
okini3939 0:c4ca662ef73e 841
okini3939 0:c4ca662ef73e 842 _checksumTotal = 0;
okini3939 0:c4ca662ef73e 843
okini3939 0:c4ca662ef73e 844 return;
okini3939 0:c4ca662ef73e 845 } else {
okini3939 0:c4ca662ef73e 846 // add to packet array, starting with the fourth byte of the apiFrame
okini3939 0:c4ca662ef73e 847 _response.getFrameData()[_pos - 4] = b;
okini3939 0:c4ca662ef73e 848 _pos++;
okini3939 0:c4ca662ef73e 849 }
okini3939 0:c4ca662ef73e 850 }
okini3939 0:c4ca662ef73e 851 }
okini3939 0:c4ca662ef73e 852 }
okini3939 0:c4ca662ef73e 853
okini3939 0:c4ca662ef73e 854 // it's peanut butter jelly time!!
okini3939 0:c4ca662ef73e 855
okini3939 0:c4ca662ef73e 856 XBeeRequest::XBeeRequest(uint8_t apiId, uint8_t frameId) {
okini3939 0:c4ca662ef73e 857 _apiId = apiId;
okini3939 0:c4ca662ef73e 858 _frameId = frameId;
okini3939 0:c4ca662ef73e 859 }
okini3939 0:c4ca662ef73e 860
okini3939 0:c4ca662ef73e 861 void XBeeRequest::setFrameId(uint8_t frameId) {
okini3939 0:c4ca662ef73e 862 _frameId = frameId;
okini3939 0:c4ca662ef73e 863 }
okini3939 0:c4ca662ef73e 864
okini3939 0:c4ca662ef73e 865 uint8_t XBeeRequest::getFrameId() {
okini3939 0:c4ca662ef73e 866 return _frameId;
okini3939 0:c4ca662ef73e 867 }
okini3939 0:c4ca662ef73e 868
okini3939 0:c4ca662ef73e 869 uint8_t XBeeRequest::getApiId() {
okini3939 0:c4ca662ef73e 870 return _apiId;
okini3939 0:c4ca662ef73e 871 }
okini3939 0:c4ca662ef73e 872
okini3939 0:c4ca662ef73e 873 void XBeeRequest::setApiId(uint8_t apiId) {
okini3939 0:c4ca662ef73e 874 _apiId = apiId;
okini3939 0:c4ca662ef73e 875 }
okini3939 0:c4ca662ef73e 876
okini3939 0:c4ca662ef73e 877 //void XBeeRequest::reset() {
okini3939 0:c4ca662ef73e 878 // _frameId = DEFAULT_FRAME_ID;
okini3939 0:c4ca662ef73e 879 //}
okini3939 0:c4ca662ef73e 880
okini3939 0:c4ca662ef73e 881 //uint8_t XBeeRequest::getPayloadOffset() {
okini3939 0:c4ca662ef73e 882 // return _payloadOffset;
okini3939 0:c4ca662ef73e 883 //}
okini3939 0:c4ca662ef73e 884 //
okini3939 0:c4ca662ef73e 885 //uint8_t XBeeRequest::setPayloadOffset(uint8_t payloadOffset) {
okini3939 0:c4ca662ef73e 886 // _payloadOffset = payloadOffset;
okini3939 0:c4ca662ef73e 887 //}
okini3939 0:c4ca662ef73e 888
okini3939 0:c4ca662ef73e 889
okini3939 0:c4ca662ef73e 890 PayloadRequest::PayloadRequest(uint8_t apiId, uint8_t frameId, uint8_t *payload, uint8_t payloadLength) : XBeeRequest(apiId, frameId) {
okini3939 0:c4ca662ef73e 891 _payloadPtr = payload;
okini3939 0:c4ca662ef73e 892 _payloadLength = payloadLength;
okini3939 0:c4ca662ef73e 893 }
okini3939 0:c4ca662ef73e 894
okini3939 0:c4ca662ef73e 895 uint8_t* PayloadRequest::getPayload() {
okini3939 0:c4ca662ef73e 896 return _payloadPtr;
okini3939 0:c4ca662ef73e 897 }
okini3939 0:c4ca662ef73e 898
okini3939 0:c4ca662ef73e 899 void PayloadRequest::setPayload(uint8_t* payload) {
okini3939 0:c4ca662ef73e 900 _payloadPtr = payload;
okini3939 0:c4ca662ef73e 901 }
okini3939 0:c4ca662ef73e 902
okini3939 0:c4ca662ef73e 903 uint8_t PayloadRequest::getPayloadLength() {
okini3939 0:c4ca662ef73e 904 return _payloadLength;
okini3939 0:c4ca662ef73e 905 }
okini3939 0:c4ca662ef73e 906
okini3939 0:c4ca662ef73e 907 void PayloadRequest::setPayloadLength(uint8_t payloadLength) {
okini3939 0:c4ca662ef73e 908 _payloadLength = payloadLength;
okini3939 0:c4ca662ef73e 909 }
okini3939 0:c4ca662ef73e 910
okini3939 0:c4ca662ef73e 911
okini3939 0:c4ca662ef73e 912 XBeeAddress::XBeeAddress() {
okini3939 0:c4ca662ef73e 913
okini3939 0:c4ca662ef73e 914 }
okini3939 0:c4ca662ef73e 915
okini3939 0:c4ca662ef73e 916 XBeeAddress64::XBeeAddress64() : XBeeAddress() {
okini3939 0:c4ca662ef73e 917
okini3939 0:c4ca662ef73e 918 }
okini3939 0:c4ca662ef73e 919
okini3939 0:c4ca662ef73e 920 XBeeAddress64::XBeeAddress64(uint32_t msb, uint32_t lsb) : XBeeAddress() {
okini3939 0:c4ca662ef73e 921 _msb = msb;
okini3939 0:c4ca662ef73e 922 _lsb = lsb;
okini3939 0:c4ca662ef73e 923 }
okini3939 0:c4ca662ef73e 924
okini3939 0:c4ca662ef73e 925 uint32_t XBeeAddress64::getMsb() {
okini3939 0:c4ca662ef73e 926 return _msb;
okini3939 0:c4ca662ef73e 927 }
okini3939 0:c4ca662ef73e 928
okini3939 0:c4ca662ef73e 929 void XBeeAddress64::setMsb(uint32_t msb) {
okini3939 0:c4ca662ef73e 930 _msb = msb;
okini3939 0:c4ca662ef73e 931 }
okini3939 0:c4ca662ef73e 932
okini3939 0:c4ca662ef73e 933 uint32_t XBeeAddress64::getLsb() {
okini3939 0:c4ca662ef73e 934 return _lsb;
okini3939 0:c4ca662ef73e 935 }
okini3939 0:c4ca662ef73e 936
okini3939 0:c4ca662ef73e 937 void XBeeAddress64::setLsb(uint32_t lsb) {
okini3939 0:c4ca662ef73e 938 _lsb = lsb;
okini3939 0:c4ca662ef73e 939 }
okini3939 0:c4ca662ef73e 940
okini3939 0:c4ca662ef73e 941
okini3939 0:c4ca662ef73e 942 #ifdef SERIES_2
okini3939 0:c4ca662ef73e 943
okini3939 0:c4ca662ef73e 944 ZBTxRequest::ZBTxRequest() : PayloadRequest(ZB_TX_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
okini3939 0:c4ca662ef73e 945
okini3939 0:c4ca662ef73e 946 }
okini3939 0:c4ca662ef73e 947
okini3939 0:c4ca662ef73e 948 ZBTxRequest::ZBTxRequest(XBeeAddress64 &addr64, uint16_t addr16, uint8_t broadcastRadius, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId): PayloadRequest(ZB_TX_REQUEST, frameId, data, dataLength) {
okini3939 0:c4ca662ef73e 949 _addr64 = addr64;
okini3939 0:c4ca662ef73e 950 _addr16 = addr16;
okini3939 0:c4ca662ef73e 951 _broadcastRadius = broadcastRadius;
okini3939 0:c4ca662ef73e 952 _option = option;
okini3939 0:c4ca662ef73e 953 }
okini3939 0:c4ca662ef73e 954
okini3939 0:c4ca662ef73e 955 ZBTxRequest::ZBTxRequest(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLength): PayloadRequest(ZB_TX_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
okini3939 0:c4ca662ef73e 956 _addr64 = addr64;
okini3939 0:c4ca662ef73e 957 _addr16 = ZB_BROADCAST_ADDRESS;
okini3939 0:c4ca662ef73e 958 _broadcastRadius = ZB_BROADCAST_RADIUS_MAX_HOPS;
okini3939 0:c4ca662ef73e 959 _option = ZB_TX_UNICAST;
okini3939 0:c4ca662ef73e 960 }
okini3939 0:c4ca662ef73e 961
okini3939 0:c4ca662ef73e 962 uint8_t ZBTxRequest::getFrameData(uint8_t pos) {
okini3939 0:c4ca662ef73e 963 if (pos == 0) {
okini3939 0:c4ca662ef73e 964 return (_addr64.getMsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 965 } else if (pos == 1) {
okini3939 0:c4ca662ef73e 966 return (_addr64.getMsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 967 } else if (pos == 2) {
okini3939 0:c4ca662ef73e 968 return (_addr64.getMsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 969 } else if (pos == 3) {
okini3939 0:c4ca662ef73e 970 return _addr64.getMsb() & 0xff;
okini3939 0:c4ca662ef73e 971 } else if (pos == 4) {
okini3939 0:c4ca662ef73e 972 return (_addr64.getLsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 973 } else if (pos == 5) {
okini3939 0:c4ca662ef73e 974 return (_addr64.getLsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 975 } else if (pos == 6) {
okini3939 0:c4ca662ef73e 976 return (_addr64.getLsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 977 } else if (pos == 7) {
okini3939 0:c4ca662ef73e 978 return _addr64.getLsb() & 0xff;
okini3939 0:c4ca662ef73e 979 } else if (pos == 8) {
okini3939 0:c4ca662ef73e 980 return (_addr16 >> 8) & 0xff;
okini3939 0:c4ca662ef73e 981 } else if (pos == 9) {
okini3939 0:c4ca662ef73e 982 return _addr16 & 0xff;
okini3939 0:c4ca662ef73e 983 } else if (pos == 10) {
okini3939 0:c4ca662ef73e 984 return _broadcastRadius;
okini3939 0:c4ca662ef73e 985 } else if (pos == 11) {
okini3939 0:c4ca662ef73e 986 return _option;
okini3939 0:c4ca662ef73e 987 } else {
okini3939 0:c4ca662ef73e 988 return getPayload()[pos - ZB_TX_API_LENGTH];
okini3939 0:c4ca662ef73e 989 }
okini3939 0:c4ca662ef73e 990 }
okini3939 0:c4ca662ef73e 991
okini3939 0:c4ca662ef73e 992 uint8_t ZBTxRequest::getFrameDataLength() {
okini3939 0:c4ca662ef73e 993 return ZB_TX_API_LENGTH + getPayloadLength();
okini3939 0:c4ca662ef73e 994 }
okini3939 0:c4ca662ef73e 995
okini3939 0:c4ca662ef73e 996 XBeeAddress64& ZBTxRequest::getAddress64() {
okini3939 0:c4ca662ef73e 997 return _addr64;
okini3939 0:c4ca662ef73e 998 }
okini3939 0:c4ca662ef73e 999
okini3939 0:c4ca662ef73e 1000 uint16_t ZBTxRequest::getAddress16() {
okini3939 0:c4ca662ef73e 1001 return _addr16;
okini3939 0:c4ca662ef73e 1002 }
okini3939 0:c4ca662ef73e 1003
okini3939 0:c4ca662ef73e 1004 uint8_t ZBTxRequest::getBroadcastRadius() {
okini3939 0:c4ca662ef73e 1005 return _broadcastRadius;
okini3939 0:c4ca662ef73e 1006 }
okini3939 0:c4ca662ef73e 1007
okini3939 0:c4ca662ef73e 1008 uint8_t ZBTxRequest::getOption() {
okini3939 0:c4ca662ef73e 1009 return _option;
okini3939 0:c4ca662ef73e 1010 }
okini3939 0:c4ca662ef73e 1011
okini3939 0:c4ca662ef73e 1012 void ZBTxRequest::setAddress64(XBeeAddress64& addr64) {
okini3939 0:c4ca662ef73e 1013 _addr64 = addr64;
okini3939 0:c4ca662ef73e 1014 }
okini3939 0:c4ca662ef73e 1015
okini3939 0:c4ca662ef73e 1016 void ZBTxRequest::setAddress16(uint16_t addr16) {
okini3939 0:c4ca662ef73e 1017 _addr16 = addr16;
okini3939 0:c4ca662ef73e 1018 }
okini3939 0:c4ca662ef73e 1019
okini3939 0:c4ca662ef73e 1020 void ZBTxRequest::setBroadcastRadius(uint8_t broadcastRadius) {
okini3939 0:c4ca662ef73e 1021 _broadcastRadius = broadcastRadius;
okini3939 0:c4ca662ef73e 1022 }
okini3939 0:c4ca662ef73e 1023
okini3939 0:c4ca662ef73e 1024 void ZBTxRequest::setOption(uint8_t option) {
okini3939 0:c4ca662ef73e 1025 _option = option;
okini3939 0:c4ca662ef73e 1026 }
okini3939 0:c4ca662ef73e 1027
okini3939 0:c4ca662ef73e 1028 #endif
okini3939 0:c4ca662ef73e 1029
okini3939 0:c4ca662ef73e 1030 #ifdef SERIES_1
okini3939 0:c4ca662ef73e 1031
okini3939 0:c4ca662ef73e 1032 Tx16Request::Tx16Request() : PayloadRequest(TX_16_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
okini3939 0:c4ca662ef73e 1033
okini3939 0:c4ca662ef73e 1034 }
okini3939 0:c4ca662ef73e 1035
okini3939 0:c4ca662ef73e 1036 Tx16Request::Tx16Request(uint16_t addr16, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId) : PayloadRequest(TX_16_REQUEST, frameId, data, dataLength) {
okini3939 0:c4ca662ef73e 1037 _addr16 = addr16;
okini3939 0:c4ca662ef73e 1038 _option = option;
okini3939 0:c4ca662ef73e 1039 }
okini3939 0:c4ca662ef73e 1040
okini3939 0:c4ca662ef73e 1041 Tx16Request::Tx16Request(uint16_t addr16, uint8_t *data, uint8_t dataLength) : PayloadRequest(TX_16_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
okini3939 0:c4ca662ef73e 1042 _addr16 = addr16;
okini3939 0:c4ca662ef73e 1043 _option = ACK_OPTION;
okini3939 0:c4ca662ef73e 1044 }
okini3939 0:c4ca662ef73e 1045
okini3939 0:c4ca662ef73e 1046 uint8_t Tx16Request::getFrameData(uint8_t pos) {
okini3939 0:c4ca662ef73e 1047
okini3939 0:c4ca662ef73e 1048 if (pos == 0) {
okini3939 0:c4ca662ef73e 1049 return (_addr16 >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1050 } else if (pos == 1) {
okini3939 0:c4ca662ef73e 1051 return _addr16 & 0xff;
okini3939 0:c4ca662ef73e 1052 } else if (pos == 2) {
okini3939 0:c4ca662ef73e 1053 return _option;
okini3939 0:c4ca662ef73e 1054 } else {
okini3939 0:c4ca662ef73e 1055 return getPayload()[pos - TX_16_API_LENGTH];
okini3939 0:c4ca662ef73e 1056 }
okini3939 0:c4ca662ef73e 1057 }
okini3939 0:c4ca662ef73e 1058
okini3939 0:c4ca662ef73e 1059 uint8_t Tx16Request::getFrameDataLength() {
okini3939 0:c4ca662ef73e 1060 return TX_16_API_LENGTH + getPayloadLength();
okini3939 0:c4ca662ef73e 1061 }
okini3939 0:c4ca662ef73e 1062
okini3939 0:c4ca662ef73e 1063 uint16_t Tx16Request::getAddress16() {
okini3939 0:c4ca662ef73e 1064 return _addr16;
okini3939 0:c4ca662ef73e 1065 }
okini3939 0:c4ca662ef73e 1066
okini3939 0:c4ca662ef73e 1067 void Tx16Request::setAddress16(uint16_t addr16) {
okini3939 0:c4ca662ef73e 1068 _addr16 = addr16;
okini3939 0:c4ca662ef73e 1069 }
okini3939 0:c4ca662ef73e 1070
okini3939 0:c4ca662ef73e 1071 uint8_t Tx16Request::getOption() {
okini3939 0:c4ca662ef73e 1072 return _option;
okini3939 0:c4ca662ef73e 1073 }
okini3939 0:c4ca662ef73e 1074
okini3939 0:c4ca662ef73e 1075 void Tx16Request::setOption(uint8_t option) {
okini3939 0:c4ca662ef73e 1076 _option = option;
okini3939 0:c4ca662ef73e 1077 }
okini3939 0:c4ca662ef73e 1078
okini3939 0:c4ca662ef73e 1079 Tx64Request::Tx64Request() : PayloadRequest(TX_64_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
okini3939 0:c4ca662ef73e 1080
okini3939 0:c4ca662ef73e 1081 }
okini3939 0:c4ca662ef73e 1082
okini3939 0:c4ca662ef73e 1083 Tx64Request::Tx64Request(XBeeAddress64 &addr64, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId) : PayloadRequest(TX_64_REQUEST, frameId, data, dataLength) {
okini3939 0:c4ca662ef73e 1084 _addr64 = addr64;
okini3939 0:c4ca662ef73e 1085 _option = option;
okini3939 0:c4ca662ef73e 1086 }
okini3939 0:c4ca662ef73e 1087
okini3939 0:c4ca662ef73e 1088 Tx64Request::Tx64Request(XBeeAddress64 &addr64, uint8_t *data, uint8_t dataLength) : PayloadRequest(TX_64_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
okini3939 0:c4ca662ef73e 1089 _addr64 = addr64;
okini3939 0:c4ca662ef73e 1090 _option = ACK_OPTION;
okini3939 0:c4ca662ef73e 1091 }
okini3939 0:c4ca662ef73e 1092
okini3939 0:c4ca662ef73e 1093 uint8_t Tx64Request::getFrameData(uint8_t pos) {
okini3939 0:c4ca662ef73e 1094
okini3939 0:c4ca662ef73e 1095 if (pos == 0) {
okini3939 0:c4ca662ef73e 1096 return (_addr64.getMsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 1097 } else if (pos == 1) {
okini3939 0:c4ca662ef73e 1098 return (_addr64.getMsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 1099 } else if (pos == 2) {
okini3939 0:c4ca662ef73e 1100 return (_addr64.getMsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1101 } else if (pos == 3) {
okini3939 0:c4ca662ef73e 1102 return _addr64.getMsb() & 0xff;
okini3939 0:c4ca662ef73e 1103 } else if (pos == 4) {
okini3939 0:c4ca662ef73e 1104 return (_addr64.getLsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 1105 } else if (pos == 5) {
okini3939 0:c4ca662ef73e 1106 return (_addr64.getLsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 1107 } else if (pos == 6) {
okini3939 0:c4ca662ef73e 1108 return(_addr64.getLsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1109 } else if (pos == 7) {
okini3939 0:c4ca662ef73e 1110 return _addr64.getLsb() & 0xff;
okini3939 0:c4ca662ef73e 1111 } else if (pos == 8) {
okini3939 0:c4ca662ef73e 1112 return _option;
okini3939 0:c4ca662ef73e 1113 } else {
okini3939 0:c4ca662ef73e 1114 return getPayload()[pos - TX_64_API_LENGTH];
okini3939 0:c4ca662ef73e 1115 }
okini3939 0:c4ca662ef73e 1116 }
okini3939 0:c4ca662ef73e 1117
okini3939 0:c4ca662ef73e 1118 uint8_t Tx64Request::getFrameDataLength() {
okini3939 0:c4ca662ef73e 1119 return TX_64_API_LENGTH + getPayloadLength();
okini3939 0:c4ca662ef73e 1120 }
okini3939 0:c4ca662ef73e 1121
okini3939 0:c4ca662ef73e 1122 XBeeAddress64& Tx64Request::getAddress64() {
okini3939 0:c4ca662ef73e 1123 return _addr64;
okini3939 0:c4ca662ef73e 1124 }
okini3939 0:c4ca662ef73e 1125
okini3939 0:c4ca662ef73e 1126 void Tx64Request::setAddress64(XBeeAddress64& addr64) {
okini3939 0:c4ca662ef73e 1127 _addr64 = addr64;
okini3939 0:c4ca662ef73e 1128 }
okini3939 0:c4ca662ef73e 1129
okini3939 0:c4ca662ef73e 1130 uint8_t Tx64Request::getOption() {
okini3939 0:c4ca662ef73e 1131 return _option;
okini3939 0:c4ca662ef73e 1132 }
okini3939 0:c4ca662ef73e 1133
okini3939 0:c4ca662ef73e 1134 void Tx64Request::setOption(uint8_t option) {
okini3939 0:c4ca662ef73e 1135 _option = option;
okini3939 0:c4ca662ef73e 1136 }
okini3939 0:c4ca662ef73e 1137
okini3939 0:c4ca662ef73e 1138 #endif
okini3939 0:c4ca662ef73e 1139
okini3939 0:c4ca662ef73e 1140 AtCommandRequest::AtCommandRequest() : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
okini3939 0:c4ca662ef73e 1141 _command = NULL;
okini3939 0:c4ca662ef73e 1142 clearCommandValue();
okini3939 0:c4ca662ef73e 1143 }
okini3939 0:c4ca662ef73e 1144
okini3939 0:c4ca662ef73e 1145 AtCommandRequest::AtCommandRequest(uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
okini3939 0:c4ca662ef73e 1146 _command = command;
okini3939 0:c4ca662ef73e 1147 _commandValue = commandValue;
okini3939 0:c4ca662ef73e 1148 _commandValueLength = commandValueLength;
okini3939 0:c4ca662ef73e 1149 }
okini3939 0:c4ca662ef73e 1150
okini3939 0:c4ca662ef73e 1151 AtCommandRequest::AtCommandRequest(uint8_t *command) : XBeeRequest(AT_COMMAND_REQUEST, DEFAULT_FRAME_ID) {
okini3939 0:c4ca662ef73e 1152 _command = command;
okini3939 0:c4ca662ef73e 1153 clearCommandValue();
okini3939 0:c4ca662ef73e 1154 }
okini3939 0:c4ca662ef73e 1155
okini3939 0:c4ca662ef73e 1156 uint8_t* AtCommandRequest::getCommand() {
okini3939 0:c4ca662ef73e 1157 return _command;
okini3939 0:c4ca662ef73e 1158 }
okini3939 0:c4ca662ef73e 1159
okini3939 0:c4ca662ef73e 1160 uint8_t* AtCommandRequest::getCommandValue() {
okini3939 0:c4ca662ef73e 1161 return _commandValue;
okini3939 0:c4ca662ef73e 1162 }
okini3939 0:c4ca662ef73e 1163
okini3939 0:c4ca662ef73e 1164 uint8_t AtCommandRequest::getCommandValueLength() {
okini3939 0:c4ca662ef73e 1165 return _commandValueLength;
okini3939 0:c4ca662ef73e 1166 }
okini3939 0:c4ca662ef73e 1167
okini3939 0:c4ca662ef73e 1168 void AtCommandRequest::setCommand(uint8_t* command) {
okini3939 0:c4ca662ef73e 1169 _command = command;
okini3939 0:c4ca662ef73e 1170 }
okini3939 0:c4ca662ef73e 1171
okini3939 0:c4ca662ef73e 1172 void AtCommandRequest::setCommandValue(uint8_t* value) {
okini3939 0:c4ca662ef73e 1173 _commandValue = value;
okini3939 0:c4ca662ef73e 1174 }
okini3939 0:c4ca662ef73e 1175
okini3939 0:c4ca662ef73e 1176 void AtCommandRequest::setCommandValueLength(uint8_t length) {
okini3939 0:c4ca662ef73e 1177 _commandValueLength = length;
okini3939 0:c4ca662ef73e 1178 }
okini3939 0:c4ca662ef73e 1179
okini3939 0:c4ca662ef73e 1180 uint8_t AtCommandRequest::getFrameData(uint8_t pos) {
okini3939 0:c4ca662ef73e 1181
okini3939 0:c4ca662ef73e 1182 if (pos == 0) {
okini3939 0:c4ca662ef73e 1183 return _command[0];
okini3939 0:c4ca662ef73e 1184 } else if (pos == 1) {
okini3939 0:c4ca662ef73e 1185 return _command[1];
okini3939 0:c4ca662ef73e 1186 } else {
okini3939 0:c4ca662ef73e 1187 return _commandValue[pos - AT_COMMAND_API_LENGTH];
okini3939 0:c4ca662ef73e 1188 }
okini3939 0:c4ca662ef73e 1189 }
okini3939 0:c4ca662ef73e 1190
okini3939 0:c4ca662ef73e 1191 void AtCommandRequest::clearCommandValue() {
okini3939 0:c4ca662ef73e 1192 _commandValue = NULL;
okini3939 0:c4ca662ef73e 1193 _commandValueLength = 0;
okini3939 0:c4ca662ef73e 1194 }
okini3939 0:c4ca662ef73e 1195
okini3939 0:c4ca662ef73e 1196 //void AtCommandRequest::reset() {
okini3939 0:c4ca662ef73e 1197 // XBeeRequest::reset();
okini3939 0:c4ca662ef73e 1198 //}
okini3939 0:c4ca662ef73e 1199
okini3939 0:c4ca662ef73e 1200 uint8_t AtCommandRequest::getFrameDataLength() {
okini3939 0:c4ca662ef73e 1201 // command is 2 byte + length of value
okini3939 0:c4ca662ef73e 1202 return AT_COMMAND_API_LENGTH + _commandValueLength;
okini3939 0:c4ca662ef73e 1203 }
okini3939 0:c4ca662ef73e 1204
okini3939 0:c4ca662ef73e 1205 XBeeAddress64 RemoteAtCommandRequest::broadcastAddress64 = XBeeAddress64(0x0, BROADCAST_ADDRESS);
okini3939 0:c4ca662ef73e 1206
okini3939 0:c4ca662ef73e 1207 RemoteAtCommandRequest::RemoteAtCommandRequest() : AtCommandRequest(NULL, NULL, 0) {
okini3939 0:c4ca662ef73e 1208 _remoteAddress16 = 0;
okini3939 0:c4ca662ef73e 1209 _applyChanges = false;
okini3939 0:c4ca662ef73e 1210 setApiId(REMOTE_AT_REQUEST);
okini3939 0:c4ca662ef73e 1211 }
okini3939 0:c4ca662ef73e 1212
okini3939 0:c4ca662ef73e 1213 RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : AtCommandRequest(command, commandValue, commandValueLength) {
okini3939 0:c4ca662ef73e 1214 _remoteAddress64 = broadcastAddress64;
okini3939 0:c4ca662ef73e 1215 _remoteAddress16 = remoteAddress16;
okini3939 0:c4ca662ef73e 1216 _applyChanges = true;
okini3939 0:c4ca662ef73e 1217 setApiId(REMOTE_AT_REQUEST);
okini3939 0:c4ca662ef73e 1218 }
okini3939 0:c4ca662ef73e 1219
okini3939 0:c4ca662ef73e 1220 RemoteAtCommandRequest::RemoteAtCommandRequest(uint16_t remoteAddress16, uint8_t *command) : AtCommandRequest(command, NULL, 0) {
okini3939 0:c4ca662ef73e 1221 _remoteAddress64 = broadcastAddress64;
okini3939 0:c4ca662ef73e 1222 _remoteAddress16 = remoteAddress16;
okini3939 0:c4ca662ef73e 1223 _applyChanges = false;
okini3939 0:c4ca662ef73e 1224 setApiId(REMOTE_AT_REQUEST);
okini3939 0:c4ca662ef73e 1225 }
okini3939 0:c4ca662ef73e 1226
okini3939 0:c4ca662ef73e 1227 RemoteAtCommandRequest::RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command, uint8_t *commandValue, uint8_t commandValueLength) : AtCommandRequest(command, commandValue, commandValueLength) {
okini3939 0:c4ca662ef73e 1228 _remoteAddress64 = remoteAddress64;
okini3939 0:c4ca662ef73e 1229 // don't worry.. works for series 1 too!
okini3939 0:c4ca662ef73e 1230 _remoteAddress16 = ZB_BROADCAST_ADDRESS;
okini3939 0:c4ca662ef73e 1231 _applyChanges = true;
okini3939 0:c4ca662ef73e 1232 setApiId(REMOTE_AT_REQUEST);
okini3939 0:c4ca662ef73e 1233 }
okini3939 0:c4ca662ef73e 1234
okini3939 0:c4ca662ef73e 1235 RemoteAtCommandRequest::RemoteAtCommandRequest(XBeeAddress64 &remoteAddress64, uint8_t *command) : AtCommandRequest(command, NULL, 0) {
okini3939 0:c4ca662ef73e 1236 _remoteAddress64 = remoteAddress64;
okini3939 0:c4ca662ef73e 1237 _remoteAddress16 = ZB_BROADCAST_ADDRESS;
okini3939 0:c4ca662ef73e 1238 _applyChanges = false;
okini3939 0:c4ca662ef73e 1239 setApiId(REMOTE_AT_REQUEST);
okini3939 0:c4ca662ef73e 1240 }
okini3939 0:c4ca662ef73e 1241
okini3939 0:c4ca662ef73e 1242 uint16_t RemoteAtCommandRequest::getRemoteAddress16() {
okini3939 0:c4ca662ef73e 1243 return _remoteAddress16;
okini3939 0:c4ca662ef73e 1244 }
okini3939 0:c4ca662ef73e 1245
okini3939 0:c4ca662ef73e 1246 void RemoteAtCommandRequest::setRemoteAddress16(uint16_t remoteAddress16) {
okini3939 0:c4ca662ef73e 1247 _remoteAddress16 = remoteAddress16;
okini3939 0:c4ca662ef73e 1248 }
okini3939 0:c4ca662ef73e 1249
okini3939 0:c4ca662ef73e 1250 XBeeAddress64& RemoteAtCommandRequest::getRemoteAddress64() {
okini3939 0:c4ca662ef73e 1251 return _remoteAddress64;
okini3939 0:c4ca662ef73e 1252 }
okini3939 0:c4ca662ef73e 1253
okini3939 0:c4ca662ef73e 1254 void RemoteAtCommandRequest::setRemoteAddress64(XBeeAddress64 &remoteAddress64) {
okini3939 0:c4ca662ef73e 1255 _remoteAddress64 = remoteAddress64;
okini3939 0:c4ca662ef73e 1256 }
okini3939 0:c4ca662ef73e 1257
okini3939 0:c4ca662ef73e 1258 bool RemoteAtCommandRequest::getApplyChanges() {
okini3939 0:c4ca662ef73e 1259 return _applyChanges;
okini3939 0:c4ca662ef73e 1260 }
okini3939 0:c4ca662ef73e 1261
okini3939 0:c4ca662ef73e 1262 void RemoteAtCommandRequest::setApplyChanges(bool applyChanges) {
okini3939 0:c4ca662ef73e 1263 _applyChanges = applyChanges;
okini3939 0:c4ca662ef73e 1264 }
okini3939 0:c4ca662ef73e 1265
okini3939 0:c4ca662ef73e 1266
okini3939 0:c4ca662ef73e 1267 uint8_t RemoteAtCommandRequest::getFrameData(uint8_t pos) {
okini3939 0:c4ca662ef73e 1268 if (pos == 0) {
okini3939 0:c4ca662ef73e 1269 return (_remoteAddress64.getMsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 1270 } else if (pos == 1) {
okini3939 0:c4ca662ef73e 1271 return (_remoteAddress64.getMsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 1272 } else if (pos == 2) {
okini3939 0:c4ca662ef73e 1273 return (_remoteAddress64.getMsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1274 } else if (pos == 3) {
okini3939 0:c4ca662ef73e 1275 return _remoteAddress64.getMsb() & 0xff;
okini3939 0:c4ca662ef73e 1276 } else if (pos == 4) {
okini3939 0:c4ca662ef73e 1277 return (_remoteAddress64.getLsb() >> 24) & 0xff;
okini3939 0:c4ca662ef73e 1278 } else if (pos == 5) {
okini3939 0:c4ca662ef73e 1279 return (_remoteAddress64.getLsb() >> 16) & 0xff;
okini3939 0:c4ca662ef73e 1280 } else if (pos == 6) {
okini3939 0:c4ca662ef73e 1281 return(_remoteAddress64.getLsb() >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1282 } else if (pos == 7) {
okini3939 0:c4ca662ef73e 1283 return _remoteAddress64.getLsb() & 0xff;
okini3939 0:c4ca662ef73e 1284 } else if (pos == 8) {
okini3939 0:c4ca662ef73e 1285 return (_remoteAddress16 >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1286 } else if (pos == 9) {
okini3939 0:c4ca662ef73e 1287 return _remoteAddress16 & 0xff;
okini3939 0:c4ca662ef73e 1288 } else if (pos == 10) {
okini3939 0:c4ca662ef73e 1289 return _applyChanges ? 2: 0;
okini3939 0:c4ca662ef73e 1290 } else if (pos == 11) {
okini3939 0:c4ca662ef73e 1291 return getCommand()[0];
okini3939 0:c4ca662ef73e 1292 } else if (pos == 12) {
okini3939 0:c4ca662ef73e 1293 return getCommand()[1];
okini3939 0:c4ca662ef73e 1294 } else {
okini3939 0:c4ca662ef73e 1295 return getCommandValue()[pos - REMOTE_AT_COMMAND_API_LENGTH];
okini3939 0:c4ca662ef73e 1296 }
okini3939 0:c4ca662ef73e 1297 }
okini3939 0:c4ca662ef73e 1298
okini3939 0:c4ca662ef73e 1299 uint8_t RemoteAtCommandRequest::getFrameDataLength() {
okini3939 0:c4ca662ef73e 1300 return REMOTE_AT_COMMAND_API_LENGTH + getCommandValueLength();
okini3939 0:c4ca662ef73e 1301 }
okini3939 0:c4ca662ef73e 1302
okini3939 0:c4ca662ef73e 1303
okini3939 0:c4ca662ef73e 1304 // TODO
okini3939 0:c4ca662ef73e 1305 //GenericRequest::GenericRequest(uint8_t* frame, uint8_t len, uint8_t apiId): XBeeRequest(apiId, *(frame), len) {
okini3939 0:c4ca662ef73e 1306 // _frame = frame;
okini3939 0:c4ca662ef73e 1307 //}
okini3939 0:c4ca662ef73e 1308
okini3939 0:c4ca662ef73e 1309 void XBee::send(XBeeRequest &request) {
okini3939 0:c4ca662ef73e 1310 // the new new deal
okini3939 0:c4ca662ef73e 1311
okini3939 0:c4ca662ef73e 1312 sendByte(START_BYTE, false);
okini3939 0:c4ca662ef73e 1313
okini3939 0:c4ca662ef73e 1314 // send length
okini3939 0:c4ca662ef73e 1315 uint8_t msbLen = ((request.getFrameDataLength() + 2) >> 8) & 0xff;
okini3939 0:c4ca662ef73e 1316 uint8_t lsbLen = (request.getFrameDataLength() + 2) & 0xff;
okini3939 0:c4ca662ef73e 1317
okini3939 0:c4ca662ef73e 1318 sendByte(msbLen, true);
okini3939 0:c4ca662ef73e 1319 sendByte(lsbLen, true);
okini3939 0:c4ca662ef73e 1320
okini3939 0:c4ca662ef73e 1321 // api id
okini3939 0:c4ca662ef73e 1322 sendByte(request.getApiId(), true);
okini3939 0:c4ca662ef73e 1323 sendByte(request.getFrameId(), true);
okini3939 0:c4ca662ef73e 1324
okini3939 0:c4ca662ef73e 1325 uint8_t checksum = 0;
okini3939 0:c4ca662ef73e 1326
okini3939 0:c4ca662ef73e 1327 // compute checksum, start at api id
okini3939 0:c4ca662ef73e 1328 checksum+= request.getApiId();
okini3939 0:c4ca662ef73e 1329 checksum+= request.getFrameId();
okini3939 0:c4ca662ef73e 1330
okini3939 0:c4ca662ef73e 1331 //std::cout << "frame length is " << static_cast<unsigned int>(request.getFrameDataLength()) << std::endl;
okini3939 0:c4ca662ef73e 1332
okini3939 0:c4ca662ef73e 1333 for (int i = 0; i < request.getFrameDataLength(); i++) {
okini3939 0:c4ca662ef73e 1334 // std::cout << "sending byte [" << static_cast<unsigned int>(i) << "] " << std::endl;
okini3939 0:c4ca662ef73e 1335 sendByte(request.getFrameData(i), true);
okini3939 0:c4ca662ef73e 1336 checksum+= request.getFrameData(i);
okini3939 0:c4ca662ef73e 1337 }
okini3939 0:c4ca662ef73e 1338
okini3939 0:c4ca662ef73e 1339 // perform 2s complement
okini3939 0:c4ca662ef73e 1340 checksum = 0xff - checksum;
okini3939 0:c4ca662ef73e 1341
okini3939 0:c4ca662ef73e 1342 // std::cout << "checksum is " << static_cast<unsigned int>(checksum) << std::endl;
okini3939 0:c4ca662ef73e 1343
okini3939 0:c4ca662ef73e 1344 // send checksum
okini3939 0:c4ca662ef73e 1345 sendByte(checksum, true);
okini3939 0:c4ca662ef73e 1346 /*
okini3939 0:c4ca662ef73e 1347 // send packet
okini3939 0:c4ca662ef73e 1348 Serial.flush();
okini3939 0:c4ca662ef73e 1349 */
okini3939 0:c4ca662ef73e 1350 }
okini3939 0:c4ca662ef73e 1351
okini3939 0:c4ca662ef73e 1352 void XBee::sendByte(uint8_t b, bool escape) {
okini3939 0:c4ca662ef73e 1353
okini3939 0:c4ca662ef73e 1354 if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF)) {
okini3939 0:c4ca662ef73e 1355 // std::cout << "escaping byte [" << toHexString(b) << "] " << std::endl;
okini3939 0:c4ca662ef73e 1356 _xbee.putc(ESCAPE);
okini3939 0:c4ca662ef73e 1357 _xbee.putc(b ^ 0x20);
okini3939 0:c4ca662ef73e 1358 } else {
okini3939 0:c4ca662ef73e 1359 _xbee.putc(b);
okini3939 0:c4ca662ef73e 1360 }
okini3939 0:c4ca662ef73e 1361 }
okini3939 0:c4ca662ef73e 1362