XBee API mode library

Committer:
yamaguch
Date:
Thu Sep 20 10:50:00 2012 +0000
Revision:
3:8453df14bd30
Parent:
0:0232a97b3883
Child:
4:c05dc188f177
fixed confirm transmit status bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:0232a97b3883 1 /*
yamaguch 0:0232a97b3883 2 Copyright (c) 2011, 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 0:0232a97b3883 25 XBee::XBee(Serial& ser, int apiMode, bool debug)
yamaguch 0:0232a97b3883 26 : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 0:0232a97b3883 27 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
yamaguch 0:0232a97b3883 28 memset(buf, 0, BUFSIZE);
yamaguch 0:0232a97b3883 29 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
yamaguch 0:0232a97b3883 30 }
yamaguch 0:0232a97b3883 31
yamaguch 0:0232a97b3883 32 XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug)
yamaguch 0:0232a97b3883 33 : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 0:0232a97b3883 34 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
yamaguch 0:0232a97b3883 35 memset(buf, 0, BUFSIZE);
yamaguch 0:0232a97b3883 36 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
yamaguch 0:0232a97b3883 37 }
yamaguch 0:0232a97b3883 38
yamaguch 0:0232a97b3883 39 XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug)
yamaguch 0:0232a97b3883 40 : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 0:0232a97b3883 41 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
yamaguch 0:0232a97b3883 42 memset(buf, 0, BUFSIZE);
yamaguch 0:0232a97b3883 43 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
yamaguch 0:0232a97b3883 44 }
yamaguch 0:0232a97b3883 45
yamaguch 0:0232a97b3883 46 XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug)
yamaguch 0:0232a97b3883 47 : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
yamaguch 0:0232a97b3883 48 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
yamaguch 0:0232a97b3883 49 memset(buf, 0, BUFSIZE);
yamaguch 0:0232a97b3883 50 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
yamaguch 0:0232a97b3883 51 }
yamaguch 0:0232a97b3883 52
yamaguch 0:0232a97b3883 53 bool XBee::init(float timeout) {
yamaguch 0:0232a97b3883 54 while (readable()) getc();
yamaguch 0:0232a97b3883 55 while (timeout > 0 && getFirmwareVersion() == -1) {
yamaguch 0:0232a97b3883 56 timeout -= 1;
yamaguch 0:0232a97b3883 57 wait(0.8);
yamaguch 0:0232a97b3883 58 }
yamaguch 0:0232a97b3883 59
yamaguch 0:0232a97b3883 60 switch (getFirmwareVersion() & 0xFF00) {
yamaguch 0:0232a97b3883 61 case 0x2100:
yamaguch 0:0232a97b3883 62 setDestination(0xFFFFULL, 0xFFFE);
yamaguch 0:0232a97b3883 63 return true;
yamaguch 0:0232a97b3883 64
yamaguch 0:0232a97b3883 65 case 0x2300:
yamaguch 0:0232a97b3883 66 case 0x2900:
yamaguch 0:0232a97b3883 67 setDestination(0x00ULL);
yamaguch 0:0232a97b3883 68 return true;
yamaguch 0:0232a97b3883 69
yamaguch 0:0232a97b3883 70 default:
yamaguch 0:0232a97b3883 71 return false;
yamaguch 0:0232a97b3883 72 }
yamaguch 0:0232a97b3883 73 }
yamaguch 0:0232a97b3883 74
yamaguch 0:0232a97b3883 75 void XBee::setDestination(XBeeAddress64 address64, XBeeAddress16 address16) {
yamaguch 0:0232a97b3883 76 setDestination(address64.raw_address(), address16.raw_address());
yamaguch 0:0232a97b3883 77 }
yamaguch 0:0232a97b3883 78
yamaguch 0:0232a97b3883 79 void XBee::setDestination(uint64_t address64, uint16_t address16) {
yamaguch 0:0232a97b3883 80 for (int i = 0; i < 8; i++, address64 >>= 8)
yamaguch 0:0232a97b3883 81 destination64[7 - i] = address64 & 255;
yamaguch 0:0232a97b3883 82 destination16[0] = (address16 >> 8) & 255;
yamaguch 0:0232a97b3883 83 destination16[1] = address16 & 255;
yamaguch 0:0232a97b3883 84 }
yamaguch 0:0232a97b3883 85
yamaguch 0:0232a97b3883 86 void XBee::setDestination(char address64[], char address16[]) {
yamaguch 0:0232a97b3883 87 memcpy(destination64, address64, 8);
yamaguch 0:0232a97b3883 88 destination16[0] = address16[0];
yamaguch 0:0232a97b3883 89 destination16[1] = address16[1];
yamaguch 0:0232a97b3883 90 }
yamaguch 0:0232a97b3883 91
yamaguch 0:0232a97b3883 92 XBee::operator bool() {
yamaguch 0:0232a97b3883 93 return getFirmwareVersion() != -1;
yamaguch 0:0232a97b3883 94 }
yamaguch 0:0232a97b3883 95
yamaguch 0:0232a97b3883 96 char XBee::getFrameID() {
yamaguch 0:0232a97b3883 97 return frame_id;
yamaguch 0:0232a97b3883 98 }
yamaguch 0:0232a97b3883 99
yamaguch 0:0232a97b3883 100 void *XBee::executeCommand(const char *command, int8_t param) {
yamaguch 0:0232a97b3883 101 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 102 }
yamaguch 0:0232a97b3883 103
yamaguch 0:0232a97b3883 104 void *XBee::executeCommand(const char *command, int16_t param) {
yamaguch 0:0232a97b3883 105 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 106 }
yamaguch 0:0232a97b3883 107
yamaguch 0:0232a97b3883 108 void *XBee::executeCommand(const char *command, int32_t param) {
yamaguch 0:0232a97b3883 109 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 110 }
yamaguch 0:0232a97b3883 111
yamaguch 0:0232a97b3883 112 void *XBee::executeCommand(const char *command, int64_t param) {
yamaguch 0:0232a97b3883 113 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 114 }
yamaguch 0:0232a97b3883 115
yamaguch 0:0232a97b3883 116 void *XBee::executeCommand(const char *command, uint8_t param) {
yamaguch 0:0232a97b3883 117 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 118 }
yamaguch 0:0232a97b3883 119
yamaguch 0:0232a97b3883 120 void *XBee::executeCommand(const char *command, uint16_t param) {
yamaguch 0:0232a97b3883 121 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 122 }
yamaguch 0:0232a97b3883 123
yamaguch 0:0232a97b3883 124 void *XBee::executeCommand(const char *command, uint32_t param) {
yamaguch 0:0232a97b3883 125 return executeCommand(command, (uint64_t) param);
yamaguch 0:0232a97b3883 126 }
yamaguch 0:0232a97b3883 127
yamaguch 0:0232a97b3883 128 void *XBee::executeCommand(const char *command, uint64_t param) {
yamaguch 0:0232a97b3883 129 uint8_t param_buf[8] = {
yamaguch 0:0232a97b3883 130 (uint8_t) (param >> 56), (uint8_t) (param >> 48),
yamaguch 0:0232a97b3883 131 (uint8_t) (param >> 40), (uint8_t) (param >> 32),
yamaguch 0:0232a97b3883 132 (uint8_t) (param >> 24), (uint8_t) (param >> 16),
yamaguch 0:0232a97b3883 133 (uint8_t) (param >> 8), (uint8_t) param
yamaguch 0:0232a97b3883 134 };
yamaguch 0:0232a97b3883 135
yamaguch 0:0232a97b3883 136 int n = 0;
yamaguch 0:0232a97b3883 137 while (n < 7 && param_buf[n] == 0) n++;
yamaguch 0:0232a97b3883 138
yamaguch 0:0232a97b3883 139 return executeCommand(command, param_buf + n, 8 - n);
yamaguch 0:0232a97b3883 140 }
yamaguch 0:0232a97b3883 141
yamaguch 0:0232a97b3883 142 void *XBee::executeCommand(const char *command, const char *param) {
yamaguch 0:0232a97b3883 143 return executeCommand(command, (uint8_t *) param, strlen(param));
yamaguch 0:0232a97b3883 144 }
yamaguch 0:0232a97b3883 145
yamaguch 0:0232a97b3883 146 void *XBee::executeCommand(const char *command, const uint8_t *param, int param_length) {
yamaguch 0:0232a97b3883 147 static char data[21];
yamaguch 0:0232a97b3883 148 bool succeeded = false;
yamaguch 0:0232a97b3883 149 char id = getFrameID();
yamaguch 0:0232a97b3883 150
yamaguch 0:0232a97b3883 151 sendCommand(command, param, param_length);
yamaguch 3:8453df14bd30 152 int index = seekFor(XBee::ATCommandResponse, id, 0.1);
yamaguch 0:0232a97b3883 153 if (index != -1) {
yamaguch 3:8453df14bd30 154 char status;
yamaguch 3:8453df14bd30 155 if (scan(index, Status, &status) && status == 0) {
yamaguch 0:0232a97b3883 156 memset(data, 0, sizeof(data));
yamaguch 0:0232a97b3883 157 if ((command[0] == 'N' && command[1] == 'I') ||
yamaguch 0:0232a97b3883 158 (command[0] == 'I' && command[1] == 'S')) {
yamaguch 0:0232a97b3883 159 if (scan(index, CommandData, data, sizeof(data)))
yamaguch 0:0232a97b3883 160 succeeded = true;
yamaguch 0:0232a97b3883 161 } else {
yamaguch 0:0232a97b3883 162 char buf2[8];
yamaguch 0:0232a97b3883 163 int length;
yamaguch 0:0232a97b3883 164 if (scan(index, CommandData, buf2, sizeof(buf2), &length) && length <= sizeof(buf2)) {
yamaguch 0:0232a97b3883 165 for (int i = 0; i < length; i++)
yamaguch 0:0232a97b3883 166 data[i] = buf2[length - i - 1];
yamaguch 0:0232a97b3883 167 succeeded = true;
yamaguch 0:0232a97b3883 168 }
yamaguch 0:0232a97b3883 169 }
yamaguch 0:0232a97b3883 170 }
yamaguch 0:0232a97b3883 171 buf[INDEX(index + 2)] = 0x00;
yamaguch 0:0232a97b3883 172 }
yamaguch 0:0232a97b3883 173
yamaguch 0:0232a97b3883 174 return succeeded ? data : 0;
yamaguch 0:0232a97b3883 175 }
yamaguch 0:0232a97b3883 176
yamaguch 0:0232a97b3883 177 int XBee::getFirmwareVersion() {
yamaguch 0:0232a97b3883 178 static int firmwareVersion = -1;
yamaguch 0:0232a97b3883 179
yamaguch 0:0232a97b3883 180 if (firmwareVersion == -1) {
yamaguch 0:0232a97b3883 181 unsigned short *data = (unsigned short *) executeCommand("VR");
yamaguch 0:0232a97b3883 182 if (data) firmwareVersion = *data; //data[0] << 8 | data[1];
yamaguch 0:0232a97b3883 183 }
yamaguch 0:0232a97b3883 184 return firmwareVersion;
yamaguch 0:0232a97b3883 185 }
yamaguch 0:0232a97b3883 186
yamaguch 0:0232a97b3883 187 void XBee::copy(char *toBuf, int fromIndex, int length) {
yamaguch 0:0232a97b3883 188 int length1 = min(BUFSIZE - fromIndex, length);
yamaguch 0:0232a97b3883 189 memcpy(toBuf, &buf[fromIndex], length1);
yamaguch 0:0232a97b3883 190 if (length1 < length)
yamaguch 0:0232a97b3883 191 memcpy(toBuf + length1, &buf[0], length - length1);
yamaguch 0:0232a97b3883 192 }
yamaguch 0:0232a97b3883 193
yamaguch 0:0232a97b3883 194 void XBee::setDebug(bool debug) {
yamaguch 0:0232a97b3883 195 this->debug = debug;
yamaguch 0:0232a97b3883 196 }