XBee API mode library

Committer:
yamaguch
Date:
Thu Mar 21 06:51:31 2013 +0000
Revision:
17:2f728fd13bc0
Parent:
16:cdfcb63b2c4b
rewrote to use rtos Semaphore only

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