XBee API mode library

Committer:
yamaguch
Date:
Wed Mar 13 19:09:55 2013 +0000
Revision:
10:3da24a020e67
Parent:
8:776b8dc51932
Child:
11:3bf9f8790340
changed to use AbstractThread;; added setSendConfirmation;; changed send

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:0232a97b3883 1 /*
yamaguch 8:776b8dc51932 2 Copyright (c) 2013, Senio Networks, Inc.
yamaguch 0:0232a97b3883 3
yamaguch 0:0232a97b3883 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 0:0232a97b3883 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 0:0232a97b3883 6 in the Software without restriction, including without limitation the rights
yamaguch 0:0232a97b3883 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 0:0232a97b3883 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 0:0232a97b3883 9 furnished to do so, subject to the following conditions:
yamaguch 0:0232a97b3883 10
yamaguch 0:0232a97b3883 11 The above copyright notice and this permission notice shall be included in
yamaguch 0:0232a97b3883 12 all copies or substantial portions of the Software.
yamaguch 0:0232a97b3883 13
yamaguch 0:0232a97b3883 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 0:0232a97b3883 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 0:0232a97b3883 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 0:0232a97b3883 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 0:0232a97b3883 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 0:0232a97b3883 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 0:0232a97b3883 20 THE SOFTWARE.
yamaguch 0:0232a97b3883 21 */
yamaguch 0:0232a97b3883 22
yamaguch 0:0232a97b3883 23 #include "XBee.h"
yamaguch 0:0232a97b3883 24
yamaguch 8:776b8dc51932 25 #ifndef XBEE_RTOS
yamaguch 0:0232a97b3883 26 XBee::XBee(Serial& ser, int apiMode, bool debug)
yamaguch 8:776b8dc51932 27 : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 8:776b8dc51932 28 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug)
yamaguch 8:776b8dc51932 29 {
yamaguch 0:0232a97b3883 30 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 31 attach(this, &XBee::rxInterruptHandler, RxIrq);
yamaguch 0:0232a97b3883 32 }
yamaguch 0:0232a97b3883 33
yamaguch 0:0232a97b3883 34 XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug)
yamaguch 8:776b8dc51932 35 : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 8:776b8dc51932 36 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug)
yamaguch 8:776b8dc51932 37 {
yamaguch 0:0232a97b3883 38 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 39 attach(this, &XBee::rxInterruptHandler, RxIrq);
yamaguch 0:0232a97b3883 40 }
yamaguch 0:0232a97b3883 41
yamaguch 0:0232a97b3883 42 XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug)
yamaguch 8:776b8dc51932 43 : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 44 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 45 {
yamaguch 0:0232a97b3883 46 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 47 attach(this, &XBee::rxInterruptHandler, RxIrq);
yamaguch 0:0232a97b3883 48 }
yamaguch 0:0232a97b3883 49
yamaguch 0:0232a97b3883 50 XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug)
yamaguch 8:776b8dc51932 51 : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 52 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 53 {
yamaguch 0:0232a97b3883 54 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 55 attach(this, &XBee::rxInterruptHandler, RxIrq);
yamaguch 8:776b8dc51932 56 }
yamaguch 8:776b8dc51932 57 #else
yamaguch 8:776b8dc51932 58 XBee::XBee(Serial& ser, int apiMode, bool debug)
yamaguch 8:776b8dc51932 59 : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 60 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 61 {
yamaguch 8:776b8dc51932 62 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 63 start();
yamaguch 8:776b8dc51932 64 attach(this, &XBee::rxISR, RxIrq);
yamaguch 0:0232a97b3883 65 }
yamaguch 0:0232a97b3883 66
yamaguch 8:776b8dc51932 67 XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug)
yamaguch 8:776b8dc51932 68 : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 69 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 70 {
yamaguch 8:776b8dc51932 71 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 72 start();
yamaguch 8:776b8dc51932 73 attach(this, &XBee::rxISR, RxIrq);
yamaguch 8:776b8dc51932 74 }
yamaguch 8:776b8dc51932 75
yamaguch 8:776b8dc51932 76 XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug)
yamaguch 8:776b8dc51932 77 : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 78 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 79 {
yamaguch 8:776b8dc51932 80 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 81 start();
yamaguch 8:776b8dc51932 82 attach(this, &XBee::rxISR, RxIrq);
yamaguch 8:776b8dc51932 83 }
yamaguch 8:776b8dc51932 84
yamaguch 8:776b8dc51932 85 XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug)
yamaguch 8:776b8dc51932 86 : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 10:3da24a020e67 87 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug)
yamaguch 8:776b8dc51932 88 {
yamaguch 8:776b8dc51932 89 memset(buf, 0, BUFSIZE);
yamaguch 8:776b8dc51932 90 start();
yamaguch 8:776b8dc51932 91 attach(this, &XBee::rxISR, RxIrq);
yamaguch 8:776b8dc51932 92 }
yamaguch 8:776b8dc51932 93 #endif
yamaguch 8:776b8dc51932 94
yamaguch 8:776b8dc51932 95 bool XBee::init(float timeout)
yamaguch 8:776b8dc51932 96 {
yamaguch 0:0232a97b3883 97 while (readable()) getc();
yamaguch 0:0232a97b3883 98 while (timeout > 0 && getFirmwareVersion() == -1) {
yamaguch 0:0232a97b3883 99 timeout -= 1;
yamaguch 0:0232a97b3883 100 wait(0.8);
yamaguch 0:0232a97b3883 101 }
yamaguch 0:0232a97b3883 102
yamaguch 0:0232a97b3883 103 switch (getFirmwareVersion() & 0xFF00) {
yamaguch 0:0232a97b3883 104 case 0x2100:
yamaguch 0:0232a97b3883 105 setDestination(0xFFFFULL, 0xFFFE);
yamaguch 0:0232a97b3883 106 return true;
yamaguch 0:0232a97b3883 107
yamaguch 0:0232a97b3883 108 case 0x2300:
yamaguch 0:0232a97b3883 109 case 0x2900:
yamaguch 0:0232a97b3883 110 setDestination(0x00ULL);
yamaguch 0:0232a97b3883 111 return true;
yamaguch 0:0232a97b3883 112
yamaguch 0:0232a97b3883 113 default:
yamaguch 0:0232a97b3883 114 return false;
yamaguch 0:0232a97b3883 115 }
yamaguch 0:0232a97b3883 116 }
yamaguch 0:0232a97b3883 117
yamaguch 8:776b8dc51932 118 void XBee::setDestination(XBeeAddress64 address64, XBeeAddress16 address16)
yamaguch 8:776b8dc51932 119 {
yamaguch 0:0232a97b3883 120 setDestination(address64.raw_address(), address16.raw_address());
yamaguch 0:0232a97b3883 121 }
yamaguch 0:0232a97b3883 122
yamaguch 8:776b8dc51932 123 void XBee::setDestination(uint64_t address64, uint16_t address16)
yamaguch 8:776b8dc51932 124 {
yamaguch 0:0232a97b3883 125 for (int i = 0; i < 8; i++, address64 >>= 8)
yamaguch 0:0232a97b3883 126 destination64[7 - i] = address64 & 255;
yamaguch 0:0232a97b3883 127 destination16[0] = (address16 >> 8) & 255;
yamaguch 0:0232a97b3883 128 destination16[1] = address16 & 255;
yamaguch 0:0232a97b3883 129 }
yamaguch 0:0232a97b3883 130
yamaguch 8:776b8dc51932 131 void XBee::setDestination(char address64[], char address16[])
yamaguch 8:776b8dc51932 132 {
yamaguch 0:0232a97b3883 133 memcpy(destination64, address64, 8);
yamaguch 0:0232a97b3883 134 destination16[0] = address16[0];
yamaguch 0:0232a97b3883 135 destination16[1] = address16[1];
yamaguch 0:0232a97b3883 136 }
yamaguch 0:0232a97b3883 137
yamaguch 8:776b8dc51932 138 XBee::operator bool()
yamaguch 8:776b8dc51932 139 {
yamaguch 0:0232a97b3883 140 return getFirmwareVersion() != -1;
yamaguch 0:0232a97b3883 141 }
yamaguch 0:0232a97b3883 142
yamaguch 8:776b8dc51932 143 char XBee::getFrameID()
yamaguch 8:776b8dc51932 144 {
yamaguch 0:0232a97b3883 145 return frame_id;
yamaguch 0:0232a97b3883 146 }
yamaguch 0:0232a97b3883 147
yamaguch 8:776b8dc51932 148 void *XBee::executeCommand(const char *command, int8_t param)
yamaguch 8:776b8dc51932 149 {
yamaguch 0:0232a97b3883 150 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 151 }
yamaguch 0:0232a97b3883 152
yamaguch 8:776b8dc51932 153 void *XBee::executeCommand(const char *command, int16_t param)
yamaguch 8:776b8dc51932 154 {
yamaguch 0:0232a97b3883 155 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 156 }
yamaguch 0:0232a97b3883 157
yamaguch 8:776b8dc51932 158 void *XBee::executeCommand(const char *command, int32_t param)
yamaguch 8:776b8dc51932 159 {
yamaguch 0:0232a97b3883 160 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 161 }
yamaguch 0:0232a97b3883 162
yamaguch 8:776b8dc51932 163 void *XBee::executeCommand(const char *command, int64_t param)
yamaguch 8:776b8dc51932 164 {
yamaguch 0:0232a97b3883 165 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 166 }
yamaguch 0:0232a97b3883 167
yamaguch 8:776b8dc51932 168 void *XBee::executeCommand(const char *command, uint8_t param)
yamaguch 8:776b8dc51932 169 {
yamaguch 0:0232a97b3883 170 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 171 }
yamaguch 0:0232a97b3883 172
yamaguch 8:776b8dc51932 173 void *XBee::executeCommand(const char *command, uint16_t param)
yamaguch 8:776b8dc51932 174 {
yamaguch 0:0232a97b3883 175 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 176 }
yamaguch 0:0232a97b3883 177
yamaguch 8:776b8dc51932 178 void *XBee::executeCommand(const char *command, uint32_t param)
yamaguch 8:776b8dc51932 179 {
yamaguch 0:0232a97b3883 180 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 181 }
yamaguch 0:0232a97b3883 182
yamaguch 8:776b8dc51932 183 void *XBee::executeCommand(const char *command, uint64_t param)
yamaguch 8:776b8dc51932 184 {
yamaguch 0:0232a97b3883 185 uint8_t param_buf[8] = {
yamaguch 0:0232a97b3883 186 (uint8_t) (param >> 56), (uint8_t) (param >> 48),
yamaguch 0:0232a97b3883 187 (uint8_t) (param >> 40), (uint8_t) (param >> 32),
yamaguch 0:0232a97b3883 188 (uint8_t) (param >> 24), (uint8_t) (param >> 16),
yamaguch 0:0232a97b3883 189 (uint8_t) (param >> 8), (uint8_t) param
yamaguch 0:0232a97b3883 190 };
yamaguch 0:0232a97b3883 191
yamaguch 0:0232a97b3883 192 int n = 0;
yamaguch 0:0232a97b3883 193 while (n < 7 && param_buf[n] == 0) n++;
yamaguch 0:0232a97b3883 194
yamaguch 0:0232a97b3883 195 return executeCommand(command, param_buf + n, 8 - n);
yamaguch 0:0232a97b3883 196 }
yamaguch 0:0232a97b3883 197
yamaguch 8:776b8dc51932 198 void *XBee::executeCommand(const char *command, const char *param)
yamaguch 8:776b8dc51932 199 {
yamaguch 0:0232a97b3883 200 return executeCommand(command, (uint8_t *) param, strlen(param));
yamaguch 0:0232a97b3883 201 }
yamaguch 0:0232a97b3883 202
yamaguch 8:776b8dc51932 203 void *XBee::executeCommand(const char *command, const uint8_t *param, int param_length)
yamaguch 8:776b8dc51932 204 {
yamaguch 0:0232a97b3883 205 static char data[21];
yamaguch 0:0232a97b3883 206 bool succeeded = false;
yamaguch 0:0232a97b3883 207 char id = getFrameID();
yamaguch 0:0232a97b3883 208
yamaguch 10:3da24a020e67 209 sendCommand(command, param, param_length, 0);
yamaguch 3:8453df14bd30 210 int index = seekFor(XBee::ATCommandResponse, id, 0.1);
yamaguch 0:0232a97b3883 211 if (index != -1) {
yamaguch 3:8453df14bd30 212 char status;
yamaguch 3:8453df14bd30 213 if (scan(index, Status, &status) && status == 0) {
yamaguch 0:0232a97b3883 214 memset(data, 0, sizeof(data));
yamaguch 0:0232a97b3883 215 if ((command[0] == 'N' && command[1] == 'I') ||
yamaguch 0:0232a97b3883 216 (command[0] == 'I' && command[1] == 'S')) {
yamaguch 0:0232a97b3883 217 if (scan(index, CommandData, data, sizeof(data)))
yamaguch 0:0232a97b3883 218 succeeded = true;
yamaguch 0:0232a97b3883 219 } else {
yamaguch 0:0232a97b3883 220 char buf2[8];
yamaguch 0:0232a97b3883 221 int length;
yamaguch 0:0232a97b3883 222 if (scan(index, CommandData, buf2, sizeof(buf2), &length) && length <= sizeof(buf2)) {
yamaguch 0:0232a97b3883 223 for (int i = 0; i < length; i++)
yamaguch 0:0232a97b3883 224 data[i] = buf2[length - i - 1];
yamaguch 0:0232a97b3883 225 succeeded = true;
yamaguch 0:0232a97b3883 226 }
yamaguch 0:0232a97b3883 227 }
yamaguch 0:0232a97b3883 228 }
yamaguch 0:0232a97b3883 229 buf[INDEX(index + 2)] = 0x00;
yamaguch 0:0232a97b3883 230 }
yamaguch 0:0232a97b3883 231
yamaguch 0:0232a97b3883 232 return succeeded ? data : 0;
yamaguch 0:0232a97b3883 233 }
yamaguch 0:0232a97b3883 234
yamaguch 8:776b8dc51932 235 int XBee::getFirmwareVersion()
yamaguch 8:776b8dc51932 236 {
yamaguch 0:0232a97b3883 237 static int firmwareVersion = -1;
yamaguch 0:0232a97b3883 238
yamaguch 0:0232a97b3883 239 if (firmwareVersion == -1) {
yamaguch 0:0232a97b3883 240 unsigned short *data = (unsigned short *) executeCommand("VR");
yamaguch 0:0232a97b3883 241 if (data) firmwareVersion = *data; //data[0] << 8 | data[1];
yamaguch 0:0232a97b3883 242 }
yamaguch 0:0232a97b3883 243 return firmwareVersion;
yamaguch 0:0232a97b3883 244 }
yamaguch 0:0232a97b3883 245
yamaguch 8:776b8dc51932 246 void XBee::copy(char *toBuf, int fromIndex, int length)
yamaguch 8:776b8dc51932 247 {
yamaguch 0:0232a97b3883 248 int length1 = min(BUFSIZE - fromIndex, length);
yamaguch 0:0232a97b3883 249 memcpy(toBuf, &buf[fromIndex], length1);
yamaguch 0:0232a97b3883 250 if (length1 < length)
yamaguch 0:0232a97b3883 251 memcpy(toBuf + length1, &buf[0], length - length1);
yamaguch 0:0232a97b3883 252 }
yamaguch 0:0232a97b3883 253
yamaguch 8:776b8dc51932 254 void XBee::setDebug(bool debug)
yamaguch 8:776b8dc51932 255 {
yamaguch 0:0232a97b3883 256 this->debug = debug;
yamaguch 0:0232a97b3883 257 }