Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
XBee.cpp
00001 /* 00002 Copyright (c) 2011, Senio Networks, Inc. 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #include "XBee.h" 00024 00025 XBee::XBee(Serial& ser, int apiMode, bool debug) 00026 : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), 00027 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) { 00028 memset(buf, 0, BUFSIZE); 00029 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq); 00030 } 00031 00032 XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug) 00033 : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), 00034 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) { 00035 memset(buf, 0, BUFSIZE); 00036 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq); 00037 } 00038 00039 XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug) 00040 : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), 00041 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) { 00042 memset(buf, 0, BUFSIZE); 00043 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq); 00044 } 00045 00046 XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug) 00047 : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), 00048 state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) { 00049 memset(buf, 0, BUFSIZE); 00050 attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq); 00051 } 00052 00053 bool XBee::init(float timeout) { 00054 while (readable()) getc(); 00055 while (timeout > 0 && getFirmwareVersion() == -1) { 00056 timeout -= 1; 00057 wait(0.8); 00058 } 00059 00060 switch (getFirmwareVersion() & 0xFF00) { 00061 case 0x2100: 00062 setDestination(0xFFFFULL, 0xFFFE); 00063 return true; 00064 00065 case 0x2300: 00066 case 0x2900: 00067 setDestination(0x00ULL); 00068 return true; 00069 00070 default: 00071 return false; 00072 } 00073 } 00074 00075 void XBee::setDestination(XBeeAddress64 address64, XBeeAddress16 address16) { 00076 setDestination(address64.raw_address(), address16.raw_address()); 00077 } 00078 00079 void XBee::setDestination(uint64_t address64, uint16_t address16) { 00080 for (int i = 0; i < 8; i++, address64 >>= 8) 00081 destination64[7 - i] = address64 & 255; 00082 destination16[0] = (address16 >> 8) & 255; 00083 destination16[1] = address16 & 255; 00084 } 00085 00086 void XBee::setDestination(char address64[], char address16[]) { 00087 memcpy(destination64, address64, 8); 00088 destination16[0] = address16[0]; 00089 destination16[1] = address16[1]; 00090 } 00091 00092 XBee::operator bool() { 00093 return getFirmwareVersion() != -1; 00094 } 00095 00096 char XBee::getFrameID() { 00097 return frame_id; 00098 } 00099 00100 void *XBee::executeCommand(const char *command, int8_t param) { 00101 return executeCommand(command, (uint64_t) param); 00102 } 00103 00104 void *XBee::executeCommand(const char *command, int16_t param) { 00105 return executeCommand(command, (uint64_t) param); 00106 } 00107 00108 void *XBee::executeCommand(const char *command, int32_t param) { 00109 return executeCommand(command, (uint64_t) param); 00110 } 00111 00112 void *XBee::executeCommand(const char *command, int64_t param) { 00113 return executeCommand(command, (uint64_t) param); 00114 } 00115 00116 void *XBee::executeCommand(const char *command, uint8_t param) { 00117 return executeCommand(command, (uint64_t) param); 00118 } 00119 00120 void *XBee::executeCommand(const char *command, uint16_t param) { 00121 return executeCommand(command, (uint64_t) param); 00122 } 00123 00124 void *XBee::executeCommand(const char *command, uint32_t param) { 00125 return executeCommand(command, (uint64_t) param); 00126 } 00127 00128 void *XBee::executeCommand(const char *command, uint64_t param) { 00129 uint8_t param_buf[8] = { 00130 (uint8_t) (param >> 56), (uint8_t) (param >> 48), 00131 (uint8_t) (param >> 40), (uint8_t) (param >> 32), 00132 (uint8_t) (param >> 24), (uint8_t) (param >> 16), 00133 (uint8_t) (param >> 8), (uint8_t) param 00134 }; 00135 00136 int n = 0; 00137 while (n < 7 && param_buf[n] == 0) n++; 00138 00139 return executeCommand(command, param_buf + n, 8 - n); 00140 } 00141 00142 void *XBee::executeCommand(const char *command, const char *param) { 00143 return executeCommand(command, (uint8_t *) param, strlen(param)); 00144 } 00145 00146 void *XBee::executeCommand(const char *command, const uint8_t *param, int param_length) { 00147 static char data[21]; 00148 bool succeeded = false; 00149 char id = getFrameID(); 00150 00151 sendCommand(command, param, param_length); 00152 int index = seekFor(XBee::ATCommandResponse, 0.1); 00153 if (index != -1) { 00154 char id2, status; 00155 if (scan(index, FrameID, &id2) && id == id2 && scan(index, Status, &status) && status == 0) { 00156 memset(data, 0, sizeof(data)); 00157 if ((command[0] == 'N' && command[1] == 'I') || 00158 (command[0] == 'I' && command[1] == 'S')) { 00159 if (scan(index, CommandData, data, sizeof(data))) 00160 succeeded = true; 00161 } else { 00162 char buf2[8]; 00163 int length; 00164 if (scan(index, CommandData, buf2, sizeof(buf2), &length) && length <= sizeof(buf2)) { 00165 for (int i = 0; i < length; i++) 00166 data[i] = buf2[length - i - 1]; 00167 succeeded = true; 00168 } 00169 } 00170 } 00171 buf[INDEX(index + 2)] = 0x00; 00172 } 00173 00174 return succeeded ? data : 0; 00175 } 00176 00177 int XBee::getFirmwareVersion() { 00178 static int firmwareVersion = -1; 00179 00180 if (firmwareVersion == -1) { 00181 unsigned short *data = (unsigned short *) executeCommand("VR"); 00182 if (data) firmwareVersion = *data; //data[0] << 8 | data[1]; 00183 } 00184 return firmwareVersion; 00185 } 00186 00187 void XBee::copy(char *toBuf, int fromIndex, int length) { 00188 int length1 = min(BUFSIZE - fromIndex, length); 00189 memcpy(toBuf, &buf[fromIndex], length1); 00190 if (length1 < length) 00191 memcpy(toBuf + length1, &buf[0], length - length1); 00192 } 00193 00194 void XBee::setDebug(bool debug) { 00195 this->debug = debug; 00196 }
Generated on Wed Jul 20 2022 22:15:57 by
