portado para MBED OS 6
XBee/XBee.cpp@0:fcaad0dfa051, 2015-05-08 (annotated)
- Committer:
- spastor
- Date:
- Fri May 08 11:50:56 2015 +0200
- Revision:
- 0:fcaad0dfa051
- Child:
- 1:794d1d3e4a08
Automatic upload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spastor | 0:fcaad0dfa051 | 1 | /** |
spastor | 0:fcaad0dfa051 | 2 | * Copyright (c) 2015 Digi International Inc., |
spastor | 0:fcaad0dfa051 | 3 | * All rights not expressly granted are reserved. |
spastor | 0:fcaad0dfa051 | 4 | * |
spastor | 0:fcaad0dfa051 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
spastor | 0:fcaad0dfa051 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
spastor | 0:fcaad0dfa051 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
spastor | 0:fcaad0dfa051 | 8 | * |
spastor | 0:fcaad0dfa051 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
spastor | 0:fcaad0dfa051 | 10 | * ======================================================================= |
spastor | 0:fcaad0dfa051 | 11 | */ |
spastor | 0:fcaad0dfa051 | 12 | |
spastor | 0:fcaad0dfa051 | 13 | #include "XBeeLib.h" |
spastor | 0:fcaad0dfa051 | 14 | #include "FrameHandlers/FH_ModemStatus.h" |
spastor | 0:fcaad0dfa051 | 15 | |
spastor | 0:fcaad0dfa051 | 16 | /* States for the state machine that processes incoming data on the serial port */ |
spastor | 0:fcaad0dfa051 | 17 | #define WAITING_FOR_START_FRAME (0) |
spastor | 0:fcaad0dfa051 | 18 | #define WAITING_FOR_LENGTH_MSB (1) |
spastor | 0:fcaad0dfa051 | 19 | #define WAITING_FOR_LENGTH_LSB (2) |
spastor | 0:fcaad0dfa051 | 20 | #define WAITING_FOR_PAYLOAD (3) |
spastor | 0:fcaad0dfa051 | 21 | #define WAITING_FOR_CHECKSUM (4) |
spastor | 0:fcaad0dfa051 | 22 | |
spastor | 0:fcaad0dfa051 | 23 | #define IS_API2() (_mode == ModeAPI2) |
spastor | 0:fcaad0dfa051 | 24 | #define IS_API_MODE() (_mode == ModeAPI1 || _mode == ModeAPI2) |
spastor | 0:fcaad0dfa051 | 25 | |
spastor | 0:fcaad0dfa051 | 26 | using namespace XBeeLib; |
spastor | 0:fcaad0dfa051 | 27 | |
spastor | 0:fcaad0dfa051 | 28 | Timer XBee::_timer; |
spastor | 0:fcaad0dfa051 | 29 | FrameBuffer XBee::_framebuf; |
spastor | 0:fcaad0dfa051 | 30 | |
spastor | 0:fcaad0dfa051 | 31 | /* Class constructor */ |
spastor | 0:fcaad0dfa051 | 32 | XBee::XBee(PinName tx, PinName rx, PinName reset, PinName rts, PinName cts, int baud) : |
spastor | 0:fcaad0dfa051 | 33 | _mode(ModeUnknown), _type(Unknown), _hw_version(0), _fw_version(0), _timeout_ms(SYNC_OPS_TIMEOUT_MS), _dev_addr64(ADDR64_UNASSIGNED), _dev_addr16(ADDR16_UNKNOWN), |
spastor | 0:fcaad0dfa051 | 34 | _reset(NULL), _tx_options(0), _bc_radius(0), _hw_reset_cnt(0), _wd_reset_cnt(0), _reset_timeout(0), _modem_status_handler(NULL), _modem_status(AtCmdFrame::HwReset), _initializing(true), _pm_mode(SleepDisabled) |
spastor | 0:fcaad0dfa051 | 35 | { |
spastor | 0:fcaad0dfa051 | 36 | |
spastor | 0:fcaad0dfa051 | 37 | if (reset != NC) { |
spastor | 0:fcaad0dfa051 | 38 | _reset = new DigitalOut(reset); |
spastor | 0:fcaad0dfa051 | 39 | } |
spastor | 0:fcaad0dfa051 | 40 | #if defined(ENABLE_PM_SUPPORT) |
spastor | 0:fcaad0dfa051 | 41 | _on_sleep = NULL; |
spastor | 0:fcaad0dfa051 | 42 | _sleep_req = NULL; |
spastor | 0:fcaad0dfa051 | 43 | #endif /* defined(ENABLE_PM_SUPPORT) */ |
spastor | 0:fcaad0dfa051 | 44 | |
spastor | 0:fcaad0dfa051 | 45 | _uart = new RawSerial(tx, rx); |
spastor | 0:fcaad0dfa051 | 46 | _uart->baud(baud); |
spastor | 0:fcaad0dfa051 | 47 | #if defined(DEVICE_SERIAL_FC) |
spastor | 0:fcaad0dfa051 | 48 | if (rts != NC && cts != NC) { |
spastor | 0:fcaad0dfa051 | 49 | _uart->set_flow_control(SerialBase::RTSCTS, rts, cts); |
spastor | 0:fcaad0dfa051 | 50 | } |
spastor | 0:fcaad0dfa051 | 51 | #endif |
spastor | 0:fcaad0dfa051 | 52 | /* Enable the reception of bytes on the serial interface by providing a cb */ |
spastor | 0:fcaad0dfa051 | 53 | _uart->attach(this, &XBee::uart_read_cb, Serial::RxIrq); |
spastor | 0:fcaad0dfa051 | 54 | |
spastor | 0:fcaad0dfa051 | 55 | for (int i = 0; i < MAX_FRAME_HANDLERS; i++) { |
spastor | 0:fcaad0dfa051 | 56 | _fhandlers[i] = NULL; |
spastor | 0:fcaad0dfa051 | 57 | } |
spastor | 0:fcaad0dfa051 | 58 | } |
spastor | 0:fcaad0dfa051 | 59 | |
spastor | 0:fcaad0dfa051 | 60 | /* Class destructor */ |
spastor | 0:fcaad0dfa051 | 61 | XBee::~XBee() |
spastor | 0:fcaad0dfa051 | 62 | { |
spastor | 0:fcaad0dfa051 | 63 | unregister_modem_status_cb(); |
spastor | 0:fcaad0dfa051 | 64 | |
spastor | 0:fcaad0dfa051 | 65 | if (_uart != NULL) { |
spastor | 0:fcaad0dfa051 | 66 | delete _uart; |
spastor | 0:fcaad0dfa051 | 67 | } |
spastor | 0:fcaad0dfa051 | 68 | if (_reset != NULL) { |
spastor | 0:fcaad0dfa051 | 69 | delete _reset; |
spastor | 0:fcaad0dfa051 | 70 | } |
spastor | 0:fcaad0dfa051 | 71 | } |
spastor | 0:fcaad0dfa051 | 72 | |
spastor | 0:fcaad0dfa051 | 73 | #include <inttypes.h> |
spastor | 0:fcaad0dfa051 | 74 | |
spastor | 0:fcaad0dfa051 | 75 | RadioStatus XBee::init(void) |
spastor | 0:fcaad0dfa051 | 76 | { |
spastor | 0:fcaad0dfa051 | 77 | AtCmdFrame::AtCmdResp cmd_resp; |
spastor | 0:fcaad0dfa051 | 78 | uint32_t var32; |
spastor | 0:fcaad0dfa051 | 79 | |
spastor | 0:fcaad0dfa051 | 80 | /* Start the timer, used for the timeouts */ |
spastor | 0:fcaad0dfa051 | 81 | _timer.reset(); |
spastor | 0:fcaad0dfa051 | 82 | _timer.start(); |
spastor | 0:fcaad0dfa051 | 83 | |
spastor | 0:fcaad0dfa051 | 84 | _initializing = true; |
spastor | 0:fcaad0dfa051 | 85 | |
spastor | 0:fcaad0dfa051 | 86 | device_reset(); |
spastor | 0:fcaad0dfa051 | 87 | |
spastor | 0:fcaad0dfa051 | 88 | /* Check if radio is in API1 or API2 _mode */ |
spastor | 0:fcaad0dfa051 | 89 | cmd_resp = get_param("AP", &var32); |
spastor | 0:fcaad0dfa051 | 90 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 91 | return Failure; |
spastor | 0:fcaad0dfa051 | 92 | |
spastor | 0:fcaad0dfa051 | 93 | _mode = (RadioMode)var32; |
spastor | 0:fcaad0dfa051 | 94 | |
spastor | 0:fcaad0dfa051 | 95 | /* Read the device unique 64b address */ |
spastor | 0:fcaad0dfa051 | 96 | uint32_t serialn_high, serialn_low; |
spastor | 0:fcaad0dfa051 | 97 | cmd_resp = get_param("SH", &serialn_high); |
spastor | 0:fcaad0dfa051 | 98 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 99 | return Failure; |
spastor | 0:fcaad0dfa051 | 100 | cmd_resp = get_param("SL", &serialn_low); |
spastor | 0:fcaad0dfa051 | 101 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 102 | return Failure; |
spastor | 0:fcaad0dfa051 | 103 | |
spastor | 0:fcaad0dfa051 | 104 | _dev_addr64 = ((uint64_t)serialn_high << 32) | serialn_low; |
spastor | 0:fcaad0dfa051 | 105 | |
spastor | 0:fcaad0dfa051 | 106 | /* Read some important parameters */ |
spastor | 0:fcaad0dfa051 | 107 | cmd_resp = get_param("HV", &var32); |
spastor | 0:fcaad0dfa051 | 108 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 109 | return Failure; |
spastor | 0:fcaad0dfa051 | 110 | _hw_version = var32; |
spastor | 0:fcaad0dfa051 | 111 | |
spastor | 0:fcaad0dfa051 | 112 | cmd_resp = get_param("VR", &var32); |
spastor | 0:fcaad0dfa051 | 113 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 114 | return Failure; |
spastor | 0:fcaad0dfa051 | 115 | _fw_version = var32; |
spastor | 0:fcaad0dfa051 | 116 | |
spastor | 0:fcaad0dfa051 | 117 | cmd_resp = get_param("MY", &var32); |
spastor | 0:fcaad0dfa051 | 118 | if (cmd_resp != AtCmdFrame::AtCmdRespOk) |
spastor | 0:fcaad0dfa051 | 119 | return Failure; |
spastor | 0:fcaad0dfa051 | 120 | _dev_addr16 = var32; |
spastor | 0:fcaad0dfa051 | 121 | _type = (RadioType)(_hw_version >> 8); |
spastor | 0:fcaad0dfa051 | 122 | |
spastor | 0:fcaad0dfa051 | 123 | digi_log(LogLevelInfo, "mode: %02x\r\n", (uint8_t)_mode); |
spastor | 0:fcaad0dfa051 | 124 | digi_log(LogLevelInfo, "HV: %04x\r\n", _hw_version); |
spastor | 0:fcaad0dfa051 | 125 | digi_log(LogLevelInfo, "VR: %04x\r\n", _fw_version); |
spastor | 0:fcaad0dfa051 | 126 | digi_log(LogLevelInfo, "ADDR64: %08x:%08x\r\n", UINT64_HI32(_dev_addr64), UINT64_LO32(_dev_addr64)); |
spastor | 0:fcaad0dfa051 | 127 | digi_log(LogLevelInfo, "ADDR16: %04x\r\n", _dev_addr16); |
spastor | 0:fcaad0dfa051 | 128 | |
spastor | 0:fcaad0dfa051 | 129 | _initializing = false; |
spastor | 0:fcaad0dfa051 | 130 | if (_modem_status_handler != NULL) { |
spastor | 0:fcaad0dfa051 | 131 | const ApiFrame frame = ApiFrame(ApiFrame::AtModemStatus, (uint8_t *)&_modem_status, sizeof(_modem_status)); |
spastor | 0:fcaad0dfa051 | 132 | _modem_status_handler->process_frame_data(&frame); |
spastor | 0:fcaad0dfa051 | 133 | } |
spastor | 0:fcaad0dfa051 | 134 | |
spastor | 0:fcaad0dfa051 | 135 | return Success; |
spastor | 0:fcaad0dfa051 | 136 | } |
spastor | 0:fcaad0dfa051 | 137 | |
spastor | 0:fcaad0dfa051 | 138 | uint64_t XBee::get_addr64() const |
spastor | 0:fcaad0dfa051 | 139 | { |
spastor | 0:fcaad0dfa051 | 140 | return _dev_addr64; |
spastor | 0:fcaad0dfa051 | 141 | } |
spastor | 0:fcaad0dfa051 | 142 | |
spastor | 0:fcaad0dfa051 | 143 | uint16_t XBee::get_addr16() const |
spastor | 0:fcaad0dfa051 | 144 | { |
spastor | 0:fcaad0dfa051 | 145 | return _dev_addr16; |
spastor | 0:fcaad0dfa051 | 146 | } |
spastor | 0:fcaad0dfa051 | 147 | |
spastor | 0:fcaad0dfa051 | 148 | RadioStatus XBee::hardware_reset() |
spastor | 0:fcaad0dfa051 | 149 | { |
spastor | 0:fcaad0dfa051 | 150 | if (_reset != NULL) { |
spastor | 0:fcaad0dfa051 | 151 | volatile uint16_t * const rst_cnt_p = &_hw_reset_cnt; |
spastor | 0:fcaad0dfa051 | 152 | const uint16_t init_rst_cnt = *rst_cnt_p; |
spastor | 0:fcaad0dfa051 | 153 | *_reset = 0; |
spastor | 0:fcaad0dfa051 | 154 | wait_ms(10); |
spastor | 0:fcaad0dfa051 | 155 | *_reset = 1; |
spastor | 0:fcaad0dfa051 | 156 | return wait_for_module_to_reset(rst_cnt_p, init_rst_cnt); |
spastor | 0:fcaad0dfa051 | 157 | } |
spastor | 0:fcaad0dfa051 | 158 | |
spastor | 0:fcaad0dfa051 | 159 | return Failure; |
spastor | 0:fcaad0dfa051 | 160 | } |
spastor | 0:fcaad0dfa051 | 161 | |
spastor | 0:fcaad0dfa051 | 162 | RadioStatus XBee::device_reset() |
spastor | 0:fcaad0dfa051 | 163 | { |
spastor | 0:fcaad0dfa051 | 164 | if (hardware_reset() == Success) |
spastor | 0:fcaad0dfa051 | 165 | return Success; |
spastor | 0:fcaad0dfa051 | 166 | |
spastor | 0:fcaad0dfa051 | 167 | return software_reset(); |
spastor | 0:fcaad0dfa051 | 168 | } |
spastor | 0:fcaad0dfa051 | 169 | |
spastor | 0:fcaad0dfa051 | 170 | RadioStatus XBee::wait_for_module_to_reset(volatile uint16_t *rst_cnt_p, uint16_t init_rst_cnt) |
spastor | 0:fcaad0dfa051 | 171 | { |
spastor | 0:fcaad0dfa051 | 172 | const int rst_timeout = _timer.read_ms() + _reset_timeout; |
spastor | 0:fcaad0dfa051 | 173 | while (*rst_cnt_p == init_rst_cnt && _timer.read_ms() < rst_timeout) { |
spastor | 0:fcaad0dfa051 | 174 | wait_ms(100); |
spastor | 0:fcaad0dfa051 | 175 | } |
spastor | 0:fcaad0dfa051 | 176 | |
spastor | 0:fcaad0dfa051 | 177 | if (_reset_timeout && *rst_cnt_p == init_rst_cnt) { |
spastor | 0:fcaad0dfa051 | 178 | digi_log(LogLevelWarning, "Reset Timeout\r\n"); |
spastor | 0:fcaad0dfa051 | 179 | return Failure; |
spastor | 0:fcaad0dfa051 | 180 | } |
spastor | 0:fcaad0dfa051 | 181 | return Success; |
spastor | 0:fcaad0dfa051 | 182 | } |
spastor | 0:fcaad0dfa051 | 183 | |
spastor | 0:fcaad0dfa051 | 184 | /** Callback function called when data is received on the serial port */ |
spastor | 0:fcaad0dfa051 | 185 | void XBee::uart_read_cb(void) |
spastor | 0:fcaad0dfa051 | 186 | { |
spastor | 0:fcaad0dfa051 | 187 | static uint8_t rxstate = WAITING_FOR_START_FRAME; |
spastor | 0:fcaad0dfa051 | 188 | static uint16_t framelen = 0; |
spastor | 0:fcaad0dfa051 | 189 | static uint16_t bytes_read; |
spastor | 0:fcaad0dfa051 | 190 | static uint8_t chksum; |
spastor | 0:fcaad0dfa051 | 191 | static ApiFrame *frame = NULL; |
spastor | 0:fcaad0dfa051 | 192 | static bool last_byte_escaped = false; |
spastor | 0:fcaad0dfa051 | 193 | |
spastor | 0:fcaad0dfa051 | 194 | while (_uart->readable()) { |
spastor | 0:fcaad0dfa051 | 195 | uint8_t data = _uart->getc(); |
spastor | 0:fcaad0dfa051 | 196 | |
spastor | 0:fcaad0dfa051 | 197 | if (IS_API2() && rxstate != WAITING_FOR_START_FRAME) { |
spastor | 0:fcaad0dfa051 | 198 | if (last_byte_escaped) { |
spastor | 0:fcaad0dfa051 | 199 | data = data ^ DR_ESCAPE_XOR_BYTE; |
spastor | 0:fcaad0dfa051 | 200 | last_byte_escaped = false; |
spastor | 0:fcaad0dfa051 | 201 | } else if (data == DR_ESCAPE_BYTE) { |
spastor | 0:fcaad0dfa051 | 202 | last_byte_escaped = true; |
spastor | 0:fcaad0dfa051 | 203 | continue; |
spastor | 0:fcaad0dfa051 | 204 | } |
spastor | 0:fcaad0dfa051 | 205 | } |
spastor | 0:fcaad0dfa051 | 206 | |
spastor | 0:fcaad0dfa051 | 207 | switch (rxstate) { |
spastor | 0:fcaad0dfa051 | 208 | case WAITING_FOR_START_FRAME: |
spastor | 0:fcaad0dfa051 | 209 | if (data == DR_START_OF_FRAME) |
spastor | 0:fcaad0dfa051 | 210 | rxstate = WAITING_FOR_LENGTH_MSB; |
spastor | 0:fcaad0dfa051 | 211 | break; |
spastor | 0:fcaad0dfa051 | 212 | |
spastor | 0:fcaad0dfa051 | 213 | case WAITING_FOR_LENGTH_MSB: |
spastor | 0:fcaad0dfa051 | 214 | framelen = data << 8; |
spastor | 0:fcaad0dfa051 | 215 | rxstate = WAITING_FOR_LENGTH_LSB; |
spastor | 0:fcaad0dfa051 | 216 | break; |
spastor | 0:fcaad0dfa051 | 217 | |
spastor | 0:fcaad0dfa051 | 218 | case WAITING_FOR_LENGTH_LSB: |
spastor | 0:fcaad0dfa051 | 219 | framelen |= data; |
spastor | 0:fcaad0dfa051 | 220 | rxstate = WAITING_FOR_PAYLOAD; /* FIXME, if frame is NULL the status should be WAITING */ |
spastor | 0:fcaad0dfa051 | 221 | bytes_read = 0; |
spastor | 0:fcaad0dfa051 | 222 | chksum = 0; |
spastor | 0:fcaad0dfa051 | 223 | /* Sanity check that the frame is smaller than... */ |
spastor | 0:fcaad0dfa051 | 224 | if (framelen > MAX_FRAME_PAYLOAD_LEN) { |
spastor | 0:fcaad0dfa051 | 225 | digi_log(LogLevelDebug, "framelen=%d too long\r\n", framelen); |
spastor | 0:fcaad0dfa051 | 226 | digi_log(LogLevelWarning, "Frame dropped, frame too long. Increase MAX_FRAME_PAYLOAD_LEN define\r\n"); |
spastor | 0:fcaad0dfa051 | 227 | rxstate = WAITING_FOR_START_FRAME; |
spastor | 0:fcaad0dfa051 | 228 | } |
spastor | 0:fcaad0dfa051 | 229 | |
spastor | 0:fcaad0dfa051 | 230 | frame = _framebuf.get_next_free_frame(); |
spastor | 0:fcaad0dfa051 | 231 | if (frame == NULL) { |
spastor | 0:fcaad0dfa051 | 232 | /* It's not possible to achive this condition as we discard older frames and only one frame can be used by syncr. commands */ |
spastor | 0:fcaad0dfa051 | 233 | assert(frame != NULL); |
spastor | 0:fcaad0dfa051 | 234 | rxstate = WAITING_FOR_START_FRAME; |
spastor | 0:fcaad0dfa051 | 235 | } else { |
spastor | 0:fcaad0dfa051 | 236 | frame->set_data_len(framelen - 1); |
spastor | 0:fcaad0dfa051 | 237 | } |
spastor | 0:fcaad0dfa051 | 238 | break; |
spastor | 0:fcaad0dfa051 | 239 | |
spastor | 0:fcaad0dfa051 | 240 | case WAITING_FOR_PAYLOAD: |
spastor | 0:fcaad0dfa051 | 241 | if (!bytes_read) |
spastor | 0:fcaad0dfa051 | 242 | frame->set_frame_type((ApiFrame::ApiFrameType)data); |
spastor | 0:fcaad0dfa051 | 243 | else { |
spastor | 0:fcaad0dfa051 | 244 | frame->set_data(data, bytes_read - 1); |
spastor | 0:fcaad0dfa051 | 245 | } |
spastor | 0:fcaad0dfa051 | 246 | chksum += data; |
spastor | 0:fcaad0dfa051 | 247 | bytes_read++; |
spastor | 0:fcaad0dfa051 | 248 | if (bytes_read == framelen) |
spastor | 0:fcaad0dfa051 | 249 | rxstate = WAITING_FOR_CHECKSUM; |
spastor | 0:fcaad0dfa051 | 250 | break; |
spastor | 0:fcaad0dfa051 | 251 | |
spastor | 0:fcaad0dfa051 | 252 | case WAITING_FOR_CHECKSUM: |
spastor | 0:fcaad0dfa051 | 253 | chksum += data; |
spastor | 0:fcaad0dfa051 | 254 | if (chksum == 0xFF) { |
spastor | 0:fcaad0dfa051 | 255 | /* We got a valid frame!! */ |
spastor | 0:fcaad0dfa051 | 256 | frame->dump(); |
spastor | 0:fcaad0dfa051 | 257 | |
spastor | 0:fcaad0dfa051 | 258 | /* If its a _modem status frame, process it to update the status info of the library. |
spastor | 0:fcaad0dfa051 | 259 | * The frame is also queued to allow processing it other handlers registered. |
spastor | 0:fcaad0dfa051 | 260 | * Note that radio_status_update() has to be fast to minimize the impact of processing |
spastor | 0:fcaad0dfa051 | 261 | * the funcion here */ |
spastor | 0:fcaad0dfa051 | 262 | if (frame->get_frame_type() == ApiFrame::AtModemStatus) { |
spastor | 0:fcaad0dfa051 | 263 | radio_status_update((AtCmdFrame::ModemStatus)frame->get_data_at(0)); |
spastor | 0:fcaad0dfa051 | 264 | if (_initializing) |
spastor | 0:fcaad0dfa051 | 265 | _framebuf.free_frame(frame); |
spastor | 0:fcaad0dfa051 | 266 | else |
spastor | 0:fcaad0dfa051 | 267 | _framebuf.complete_frame(frame); |
spastor | 0:fcaad0dfa051 | 268 | } |
spastor | 0:fcaad0dfa051 | 269 | else { |
spastor | 0:fcaad0dfa051 | 270 | _framebuf.complete_frame(frame); |
spastor | 0:fcaad0dfa051 | 271 | /* Note, the frame will be released elsewhere, once it has been processed */ |
spastor | 0:fcaad0dfa051 | 272 | } |
spastor | 0:fcaad0dfa051 | 273 | } else { |
spastor | 0:fcaad0dfa051 | 274 | _framebuf.free_frame(frame); |
spastor | 0:fcaad0dfa051 | 275 | digi_log(LogLevelWarning, "Checksum error, got %02x, %02x\r\n", data, chksum); |
spastor | 0:fcaad0dfa051 | 276 | } |
spastor | 0:fcaad0dfa051 | 277 | /* Intentional fall-through */ |
spastor | 0:fcaad0dfa051 | 278 | default: |
spastor | 0:fcaad0dfa051 | 279 | rxstate = WAITING_FOR_START_FRAME; |
spastor | 0:fcaad0dfa051 | 280 | break; |
spastor | 0:fcaad0dfa051 | 281 | } |
spastor | 0:fcaad0dfa051 | 282 | } |
spastor | 0:fcaad0dfa051 | 283 | /* TODO, signal the thread processing incoming frames */ |
spastor | 0:fcaad0dfa051 | 284 | } |
spastor | 0:fcaad0dfa051 | 285 | |
spastor | 0:fcaad0dfa051 | 286 | /* This is a pure virtual function, but exists here because its called from this class to |
spastor | 0:fcaad0dfa051 | 287 | * to update the status of the object, and can be called before the construction of the |
spastor | 0:fcaad0dfa051 | 288 | * object has been completed and the virtual functions filled */ |
spastor | 0:fcaad0dfa051 | 289 | void XBee::radio_status_update(AtCmdFrame::ModemStatus modem_status) |
spastor | 0:fcaad0dfa051 | 290 | { |
spastor | 0:fcaad0dfa051 | 291 | UNUSED_PARAMETER(modem_status); |
spastor | 0:fcaad0dfa051 | 292 | } |
spastor | 0:fcaad0dfa051 | 293 | |
spastor | 0:fcaad0dfa051 | 294 | void XBee::set_timeout(uint16_t timeout_ms) |
spastor | 0:fcaad0dfa051 | 295 | { |
spastor | 0:fcaad0dfa051 | 296 | this->_timeout_ms = timeout_ms; |
spastor | 0:fcaad0dfa051 | 297 | } |
spastor | 0:fcaad0dfa051 | 298 | |
spastor | 0:fcaad0dfa051 | 299 | uint16_t XBee::get_timeout(void) const |
spastor | 0:fcaad0dfa051 | 300 | { |
spastor | 0:fcaad0dfa051 | 301 | return _timeout_ms; |
spastor | 0:fcaad0dfa051 | 302 | } |
spastor | 0:fcaad0dfa051 | 303 | |
spastor | 0:fcaad0dfa051 | 304 | RadioType XBee::get_radio_type() const |
spastor | 0:fcaad0dfa051 | 305 | { |
spastor | 0:fcaad0dfa051 | 306 | return _type; |
spastor | 0:fcaad0dfa051 | 307 | } |
spastor | 0:fcaad0dfa051 | 308 | |
spastor | 0:fcaad0dfa051 | 309 | |
spastor | 0:fcaad0dfa051 | 310 | |
spastor | 0:fcaad0dfa051 | 311 | /* */ |
spastor | 0:fcaad0dfa051 | 312 | ApiFrame * XBee::get_this_api_frame(uint8_t id, ApiFrame::ApiFrameType type, |
spastor | 0:fcaad0dfa051 | 313 | ApiFrame::ApiFrameType type2) |
spastor | 0:fcaad0dfa051 | 314 | { |
spastor | 0:fcaad0dfa051 | 315 | int const timeout = _timer.read_ms() + _timeout_ms; |
spastor | 0:fcaad0dfa051 | 316 | |
spastor | 0:fcaad0dfa051 | 317 | while (_timer.read_ms() < timeout) { |
spastor | 0:fcaad0dfa051 | 318 | ApiFrame * frame = _framebuf.get_next_complete_frame_syncr(); |
spastor | 0:fcaad0dfa051 | 319 | if (frame == NULL) { |
spastor | 0:fcaad0dfa051 | 320 | wait_ms(10); |
spastor | 0:fcaad0dfa051 | 321 | continue; |
spastor | 0:fcaad0dfa051 | 322 | } |
spastor | 0:fcaad0dfa051 | 323 | |
spastor | 0:fcaad0dfa051 | 324 | if ((frame->get_frame_type() != type) && |
spastor | 0:fcaad0dfa051 | 325 | (frame->get_frame_type() != type2)) { |
spastor | 0:fcaad0dfa051 | 326 | _framebuf.complete_frame(frame); |
spastor | 0:fcaad0dfa051 | 327 | wait_ms(1); |
spastor | 0:fcaad0dfa051 | 328 | continue; |
spastor | 0:fcaad0dfa051 | 329 | } |
spastor | 0:fcaad0dfa051 | 330 | |
spastor | 0:fcaad0dfa051 | 331 | if (frame->get_data_at(ATCMD_RESP_FRAME_ID_OFFSET) != id) { |
spastor | 0:fcaad0dfa051 | 332 | _framebuf.complete_frame(frame); |
spastor | 0:fcaad0dfa051 | 333 | wait_ms(1); |
spastor | 0:fcaad0dfa051 | 334 | continue; |
spastor | 0:fcaad0dfa051 | 335 | } |
spastor | 0:fcaad0dfa051 | 336 | |
spastor | 0:fcaad0dfa051 | 337 | /* frame found */ |
spastor | 0:fcaad0dfa051 | 338 | return frame; |
spastor | 0:fcaad0dfa051 | 339 | } |
spastor | 0:fcaad0dfa051 | 340 | |
spastor | 0:fcaad0dfa051 | 341 | digi_log(LogLevelWarning, "Frame type: %02x, id: %02x, timeout\r\n", (uint8_t)type, id); |
spastor | 0:fcaad0dfa051 | 342 | |
spastor | 0:fcaad0dfa051 | 343 | return NULL; |
spastor | 0:fcaad0dfa051 | 344 | } |
spastor | 0:fcaad0dfa051 | 345 | |
spastor | 0:fcaad0dfa051 | 346 | void XBee::send_byte_escaping_if(uint8_t data) |
spastor | 0:fcaad0dfa051 | 347 | { |
spastor | 0:fcaad0dfa051 | 348 | if (IS_API2()) { |
spastor | 0:fcaad0dfa051 | 349 | switch (data) { |
spastor | 0:fcaad0dfa051 | 350 | case DR_START_OF_FRAME: |
spastor | 0:fcaad0dfa051 | 351 | case DR_ESCAPE_BYTE: |
spastor | 0:fcaad0dfa051 | 352 | case DR_XON_BYTE: |
spastor | 0:fcaad0dfa051 | 353 | case DR_XOFF_BYTE: |
spastor | 0:fcaad0dfa051 | 354 | _uart->putc(DR_ESCAPE_BYTE); |
spastor | 0:fcaad0dfa051 | 355 | _uart->putc(data ^ DR_ESCAPE_XOR_BYTE); |
spastor | 0:fcaad0dfa051 | 356 | break; |
spastor | 0:fcaad0dfa051 | 357 | default: |
spastor | 0:fcaad0dfa051 | 358 | _uart->putc(data); |
spastor | 0:fcaad0dfa051 | 359 | } |
spastor | 0:fcaad0dfa051 | 360 | } else { |
spastor | 0:fcaad0dfa051 | 361 | _uart->putc(data); |
spastor | 0:fcaad0dfa051 | 362 | } |
spastor | 0:fcaad0dfa051 | 363 | } |
spastor | 0:fcaad0dfa051 | 364 | |
spastor | 0:fcaad0dfa051 | 365 | void XBee::send_api_frame(ApiFrame *frame) |
spastor | 0:fcaad0dfa051 | 366 | { |
spastor | 0:fcaad0dfa051 | 367 | uint8_t chksum; |
spastor | 0:fcaad0dfa051 | 368 | const uint8_t *data; |
spastor | 0:fcaad0dfa051 | 369 | uint16_t bytes_sent = 0, frame_len; |
spastor | 0:fcaad0dfa051 | 370 | |
spastor | 0:fcaad0dfa051 | 371 | frame->dump(); |
spastor | 0:fcaad0dfa051 | 372 | |
spastor | 0:fcaad0dfa051 | 373 | frame_len = 1 + frame->get_data_len(); /* frame type + frame payload */ |
spastor | 0:fcaad0dfa051 | 374 | data = frame->get_data(); |
spastor | 0:fcaad0dfa051 | 375 | |
spastor | 0:fcaad0dfa051 | 376 | /* Send the start of frame delimiter */ |
spastor | 0:fcaad0dfa051 | 377 | _uart->putc(DR_START_OF_FRAME); |
spastor | 0:fcaad0dfa051 | 378 | |
spastor | 0:fcaad0dfa051 | 379 | /* Now the length */ |
spastor | 0:fcaad0dfa051 | 380 | send_byte_escaping_if((uint8_t)(frame_len >> 8)); |
spastor | 0:fcaad0dfa051 | 381 | send_byte_escaping_if((uint8_t)frame_len); |
spastor | 0:fcaad0dfa051 | 382 | |
spastor | 0:fcaad0dfa051 | 383 | /* Send the Frame type and then the payload */ |
spastor | 0:fcaad0dfa051 | 384 | chksum = (uint8_t)frame->get_frame_type(); |
spastor | 0:fcaad0dfa051 | 385 | send_byte_escaping_if(chksum); |
spastor | 0:fcaad0dfa051 | 386 | bytes_sent++; |
spastor | 0:fcaad0dfa051 | 387 | |
spastor | 0:fcaad0dfa051 | 388 | /* And now, send the packet payload */ |
spastor | 0:fcaad0dfa051 | 389 | while (bytes_sent++ < frame_len) { |
spastor | 0:fcaad0dfa051 | 390 | chksum += *data; |
spastor | 0:fcaad0dfa051 | 391 | send_byte_escaping_if(*data++); |
spastor | 0:fcaad0dfa051 | 392 | } |
spastor | 0:fcaad0dfa051 | 393 | |
spastor | 0:fcaad0dfa051 | 394 | /* And finally send the checksum */ |
spastor | 0:fcaad0dfa051 | 395 | send_byte_escaping_if(~chksum); |
spastor | 0:fcaad0dfa051 | 396 | } |
spastor | 0:fcaad0dfa051 | 397 | |
spastor | 0:fcaad0dfa051 | 398 | /** */ |
spastor | 0:fcaad0dfa051 | 399 | RadioStatus XBee::register_frame_handler(FrameHandler *const handler) |
spastor | 0:fcaad0dfa051 | 400 | { |
spastor | 0:fcaad0dfa051 | 401 | if (handler != NULL) { |
spastor | 0:fcaad0dfa051 | 402 | for (int i = 0; i < MAX_FRAME_HANDLERS; i++) { |
spastor | 0:fcaad0dfa051 | 403 | if (_fhandlers[i] != NULL) |
spastor | 0:fcaad0dfa051 | 404 | continue; |
spastor | 0:fcaad0dfa051 | 405 | _fhandlers[i] = handler; |
spastor | 0:fcaad0dfa051 | 406 | return Success; |
spastor | 0:fcaad0dfa051 | 407 | } |
spastor | 0:fcaad0dfa051 | 408 | } |
spastor | 0:fcaad0dfa051 | 409 | |
spastor | 0:fcaad0dfa051 | 410 | digi_log(LogLevelError, "No more Frame Handlers available. Increase MAX_FRAME_HANDLERS define\r\n"); |
spastor | 0:fcaad0dfa051 | 411 | |
spastor | 0:fcaad0dfa051 | 412 | return Failure; |
spastor | 0:fcaad0dfa051 | 413 | } |
spastor | 0:fcaad0dfa051 | 414 | |
spastor | 0:fcaad0dfa051 | 415 | /** */ |
spastor | 0:fcaad0dfa051 | 416 | RadioStatus XBee::unregister_frame_handler(FrameHandler *const handler) |
spastor | 0:fcaad0dfa051 | 417 | { |
spastor | 0:fcaad0dfa051 | 418 | int i; |
spastor | 0:fcaad0dfa051 | 419 | |
spastor | 0:fcaad0dfa051 | 420 | if (handler != NULL) { |
spastor | 0:fcaad0dfa051 | 421 | for (i = 0; i < MAX_FRAME_HANDLERS; i++) { |
spastor | 0:fcaad0dfa051 | 422 | if (_fhandlers[i] == handler) |
spastor | 0:fcaad0dfa051 | 423 | break; |
spastor | 0:fcaad0dfa051 | 424 | } |
spastor | 0:fcaad0dfa051 | 425 | |
spastor | 0:fcaad0dfa051 | 426 | if (i == MAX_FRAME_HANDLERS) |
spastor | 0:fcaad0dfa051 | 427 | return Failure; |
spastor | 0:fcaad0dfa051 | 428 | |
spastor | 0:fcaad0dfa051 | 429 | do { |
spastor | 0:fcaad0dfa051 | 430 | if (i == MAX_FRAME_HANDLERS - 1) |
spastor | 0:fcaad0dfa051 | 431 | _fhandlers[i] = NULL; |
spastor | 0:fcaad0dfa051 | 432 | else |
spastor | 0:fcaad0dfa051 | 433 | _fhandlers[i] = _fhandlers[i + 1]; |
spastor | 0:fcaad0dfa051 | 434 | } while (++i < MAX_FRAME_HANDLERS); |
spastor | 0:fcaad0dfa051 | 435 | } |
spastor | 0:fcaad0dfa051 | 436 | |
spastor | 0:fcaad0dfa051 | 437 | return Success; |
spastor | 0:fcaad0dfa051 | 438 | } |
spastor | 0:fcaad0dfa051 | 439 | |
spastor | 0:fcaad0dfa051 | 440 | XBee::RadioProtocol XBee::get_radio_protocol(void) const |
spastor | 0:fcaad0dfa051 | 441 | { |
spastor | 0:fcaad0dfa051 | 442 | enum HardwareVersion { |
spastor | 0:fcaad0dfa051 | 443 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 444 | X09_009 = 0x01, |
spastor | 0:fcaad0dfa051 | 445 | X09_019 = 0x02, |
spastor | 0:fcaad0dfa051 | 446 | XH9_009 = 0x03, |
spastor | 0:fcaad0dfa051 | 447 | XH9_019 = 0x04, |
spastor | 0:fcaad0dfa051 | 448 | X24_009 = 0x05, |
spastor | 0:fcaad0dfa051 | 449 | X24_019 = 0x06, |
spastor | 0:fcaad0dfa051 | 450 | X09_001 = 0x07, |
spastor | 0:fcaad0dfa051 | 451 | XH9_001 = 0x08, |
spastor | 0:fcaad0dfa051 | 452 | X08_004 = 0x09, |
spastor | 0:fcaad0dfa051 | 453 | XC09_009 = 0x0A, |
spastor | 0:fcaad0dfa051 | 454 | XC09_038 = 0x0B, |
spastor | 0:fcaad0dfa051 | 455 | X24_038 = 0x0C, |
spastor | 0:fcaad0dfa051 | 456 | X09_009_TX = 0x0D, |
spastor | 0:fcaad0dfa051 | 457 | X09_019_TX = 0x0E, |
spastor | 0:fcaad0dfa051 | 458 | XH9_009_TX = 0x0F, |
spastor | 0:fcaad0dfa051 | 459 | XH9_019_TX = 0x10, |
spastor | 0:fcaad0dfa051 | 460 | X09_001_TX = 0x11, |
spastor | 0:fcaad0dfa051 | 461 | XH9_001_TX = 0x12, |
spastor | 0:fcaad0dfa051 | 462 | XT09B_XXX = 0x13, |
spastor | 0:fcaad0dfa051 | 463 | XT09_XXX = 0x14, |
spastor | 0:fcaad0dfa051 | 464 | XC08_009 = 0x15, |
spastor | 0:fcaad0dfa051 | 465 | XC08_038 = 0x16, |
spastor | 0:fcaad0dfa051 | 466 | #endif |
spastor | 0:fcaad0dfa051 | 467 | XB24_AXX_XX = 0x17, |
spastor | 0:fcaad0dfa051 | 468 | XBP24_AXX_XX = 0x18, |
spastor | 0:fcaad0dfa051 | 469 | XB24_BXIX_XXX = 0x19, |
spastor | 0:fcaad0dfa051 | 470 | XBP24_BXIX_XXX = 0x1A, |
spastor | 0:fcaad0dfa051 | 471 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 472 | XBP09_DXIX_XXX = 0x1B, |
spastor | 0:fcaad0dfa051 | 473 | XBP09_XCXX_XXX = 0x1C, |
spastor | 0:fcaad0dfa051 | 474 | XBP08_DXXX_XXX = 0x1D, |
spastor | 0:fcaad0dfa051 | 475 | #endif |
spastor | 0:fcaad0dfa051 | 476 | XBP24B = 0x1E, |
spastor | 0:fcaad0dfa051 | 477 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 478 | XB24_WF = 0x1F, |
spastor | 0:fcaad0dfa051 | 479 | AMBER_MBUS = 0x20, |
spastor | 0:fcaad0dfa051 | 480 | #endif |
spastor | 0:fcaad0dfa051 | 481 | XBP24C = 0x21, |
spastor | 0:fcaad0dfa051 | 482 | XB24C = 0x22, |
spastor | 0:fcaad0dfa051 | 483 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 484 | XSC_GEN3 = 0x23, |
spastor | 0:fcaad0dfa051 | 485 | SRD_868_GEN3 = 0x24, |
spastor | 0:fcaad0dfa051 | 486 | ABANDONATED = 0x25, |
spastor | 0:fcaad0dfa051 | 487 | SMT_900LP = 0x26, |
spastor | 0:fcaad0dfa051 | 488 | WIFI_ATHEROS = 0x27, |
spastor | 0:fcaad0dfa051 | 489 | SMT_WIFI_ATHEROS = 0x28, |
spastor | 0:fcaad0dfa051 | 490 | SMT_475LP = 0x29, |
spastor | 0:fcaad0dfa051 | 491 | XBEE_CELL_TH = 0x2A, |
spastor | 0:fcaad0dfa051 | 492 | XLR_MODULE = 0x2B, |
spastor | 0:fcaad0dfa051 | 493 | XB900HP_NZ = 0x2C, |
spastor | 0:fcaad0dfa051 | 494 | XBP24C_TH_DIP = 0x2D, |
spastor | 0:fcaad0dfa051 | 495 | XB24C_TH_DIP = 0x2E, |
spastor | 0:fcaad0dfa051 | 496 | XLR_BASEBOARD = 0x2F |
spastor | 0:fcaad0dfa051 | 497 | #endif |
spastor | 0:fcaad0dfa051 | 498 | }; |
spastor | 0:fcaad0dfa051 | 499 | const bool fw_4_bytes_len = _fw_version > 0x0FFF && _fw_version < 0xFFFF; |
spastor | 0:fcaad0dfa051 | 500 | const uint8_t fw_nibble_3 = (_fw_version >> (4 * 3)) & 0x000F; |
spastor | 0:fcaad0dfa051 | 501 | const uint8_t fw_nibble_1 = (_fw_version >> (4 * 1)) & 0x000F; |
spastor | 0:fcaad0dfa051 | 502 | const uint8_t fw_nibble_0 = (_fw_version >> (4 * 0)) & 0x000F; |
spastor | 0:fcaad0dfa051 | 503 | const uint8_t hw_version_msb = _hw_version >> 8; |
spastor | 0:fcaad0dfa051 | 504 | |
spastor | 0:fcaad0dfa051 | 505 | if (hw_version_msb == XB24_AXX_XX || hw_version_msb == XBP24_AXX_XX) { |
spastor | 0:fcaad0dfa051 | 506 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 507 | if (fw_4_bytes_len && fw_nibble_3 == 8) { |
spastor | 0:fcaad0dfa051 | 508 | return DigiMesh; |
spastor | 0:fcaad0dfa051 | 509 | } |
spastor | 0:fcaad0dfa051 | 510 | return Raw_802_15_4; |
spastor | 0:fcaad0dfa051 | 511 | #else |
spastor | 0:fcaad0dfa051 | 512 | if (!(fw_4_bytes_len && fw_nibble_3 == 8)) { |
spastor | 0:fcaad0dfa051 | 513 | return Raw_802_15_4; |
spastor | 0:fcaad0dfa051 | 514 | } |
spastor | 0:fcaad0dfa051 | 515 | #endif |
spastor | 0:fcaad0dfa051 | 516 | } else if (hw_version_msb == XB24_BXIX_XXX || hw_version_msb == XBP24_BXIX_XXX) { |
spastor | 0:fcaad0dfa051 | 517 | if (fw_4_bytes_len && ((fw_nibble_3 == 1 && fw_nibble_1 == 2 && fw_nibble_0 == 0) || fw_nibble_3 == 2)) { |
spastor | 0:fcaad0dfa051 | 518 | return ZigBee; |
spastor | 0:fcaad0dfa051 | 519 | } |
spastor | 0:fcaad0dfa051 | 520 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 521 | if (fw_4_bytes_len && fw_nibble_3 == 3) { |
spastor | 0:fcaad0dfa051 | 522 | return SmartEnergy; |
spastor | 0:fcaad0dfa051 | 523 | } |
spastor | 0:fcaad0dfa051 | 524 | return ZNet; |
spastor | 0:fcaad0dfa051 | 525 | } else if (hw_version_msb == XBP09_DXIX_XXX) { |
spastor | 0:fcaad0dfa051 | 526 | if (fw_4_bytes_len && (fw_nibble_3 == 8 || fw_nibble_1 == 8)) { |
spastor | 0:fcaad0dfa051 | 527 | return DigiMesh; |
spastor | 0:fcaad0dfa051 | 528 | } |
spastor | 0:fcaad0dfa051 | 529 | return DigiPoint; |
spastor | 0:fcaad0dfa051 | 530 | } else if (hw_version_msb == XBP08_DXXX_XXX) { |
spastor | 0:fcaad0dfa051 | 531 | return DigiPoint; |
spastor | 0:fcaad0dfa051 | 532 | #endif |
spastor | 0:fcaad0dfa051 | 533 | } else if (hw_version_msb == XBP24B) { |
spastor | 0:fcaad0dfa051 | 534 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 535 | if (fw_4_bytes_len && fw_nibble_3 == 3) { |
spastor | 0:fcaad0dfa051 | 536 | return SmartEnergy; |
spastor | 0:fcaad0dfa051 | 537 | } |
spastor | 0:fcaad0dfa051 | 538 | return ZigBee; |
spastor | 0:fcaad0dfa051 | 539 | #else |
spastor | 0:fcaad0dfa051 | 540 | if (!(fw_4_bytes_len && fw_nibble_3 == 3)) { |
spastor | 0:fcaad0dfa051 | 541 | return ZigBee; |
spastor | 0:fcaad0dfa051 | 542 | } |
spastor | 0:fcaad0dfa051 | 543 | #endif |
spastor | 0:fcaad0dfa051 | 544 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 545 | } else if (hw_version_msb == XB24_WF || hw_version_msb == WIFI_ATHEROS || hw_version_msb == SMT_WIFI_ATHEROS) { |
spastor | 0:fcaad0dfa051 | 546 | return XBeeWiFi; |
spastor | 0:fcaad0dfa051 | 547 | #endif |
spastor | 0:fcaad0dfa051 | 548 | } else if (hw_version_msb == XBP24C || hw_version_msb == XB24C) { |
spastor | 0:fcaad0dfa051 | 549 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 550 | if (fw_4_bytes_len && fw_nibble_3 == 5) { |
spastor | 0:fcaad0dfa051 | 551 | return SmartEnergy; |
spastor | 0:fcaad0dfa051 | 552 | } |
spastor | 0:fcaad0dfa051 | 553 | return ZigBee; |
spastor | 0:fcaad0dfa051 | 554 | #else |
spastor | 0:fcaad0dfa051 | 555 | if (!(fw_4_bytes_len && fw_nibble_3 == 5)) { |
spastor | 0:fcaad0dfa051 | 556 | return ZigBee; |
spastor | 0:fcaad0dfa051 | 557 | } |
spastor | 0:fcaad0dfa051 | 558 | #endif |
spastor | 0:fcaad0dfa051 | 559 | #ifdef EXTRA_XBEE_PROTOCOLS |
spastor | 0:fcaad0dfa051 | 560 | } else if (hw_version_msb == XSC_GEN3 || hw_version_msb == SRD_868_GEN3) { |
spastor | 0:fcaad0dfa051 | 561 | if (fw_4_bytes_len && fw_nibble_3 == 8) { |
spastor | 0:fcaad0dfa051 | 562 | return DigiMesh; |
spastor | 0:fcaad0dfa051 | 563 | } else if (fw_4_bytes_len && fw_nibble_3 == 1) { |
spastor | 0:fcaad0dfa051 | 564 | return DigiPoint; |
spastor | 0:fcaad0dfa051 | 565 | } |
spastor | 0:fcaad0dfa051 | 566 | return None; |
spastor | 0:fcaad0dfa051 | 567 | } else if (hw_version_msb == XBEE_CELL_TH) { |
spastor | 0:fcaad0dfa051 | 568 | return None; |
spastor | 0:fcaad0dfa051 | 569 | } else if (hw_version_msb == XLR_MODULE) { |
spastor | 0:fcaad0dfa051 | 570 | return None; |
spastor | 0:fcaad0dfa051 | 571 | } else if (hw_version_msb == XLR_BASEBOARD) { |
spastor | 0:fcaad0dfa051 | 572 | return None; |
spastor | 0:fcaad0dfa051 | 573 | } else if (hw_version_msb == XB900HP_NZ) { |
spastor | 0:fcaad0dfa051 | 574 | return DigiPoint; |
spastor | 0:fcaad0dfa051 | 575 | } |
spastor | 0:fcaad0dfa051 | 576 | #else |
spastor | 0:fcaad0dfa051 | 577 | } |
spastor | 0:fcaad0dfa051 | 578 | #endif |
spastor | 0:fcaad0dfa051 | 579 | |
spastor | 0:fcaad0dfa051 | 580 | return None; |
spastor | 0:fcaad0dfa051 | 581 | } |
spastor | 0:fcaad0dfa051 | 582 | |
spastor | 0:fcaad0dfa051 | 583 | #define TX_STATUS_OFFSET_ZB 4 |
spastor | 0:fcaad0dfa051 | 584 | #define TX_STATUS_OFFSET_802 1 |
spastor | 0:fcaad0dfa051 | 585 | |
spastor | 0:fcaad0dfa051 | 586 | TxStatus XBee::send_data(ApiFrame *frame) |
spastor | 0:fcaad0dfa051 | 587 | { |
spastor | 0:fcaad0dfa051 | 588 | TxStatus resp = TxStatusTimeout; |
spastor | 0:fcaad0dfa051 | 589 | ApiFrame *resp_frame; |
spastor | 0:fcaad0dfa051 | 590 | |
spastor | 0:fcaad0dfa051 | 591 | send_api_frame(frame); |
spastor | 0:fcaad0dfa051 | 592 | |
spastor | 0:fcaad0dfa051 | 593 | /* Wait for the transmit status response packet */ |
spastor | 0:fcaad0dfa051 | 594 | resp_frame = get_this_api_frame(frame->get_frame_id(), |
spastor | 0:fcaad0dfa051 | 595 | ApiFrame::TxStatusZB, ApiFrame::TxStatus); |
spastor | 0:fcaad0dfa051 | 596 | if (resp_frame == NULL) |
spastor | 0:fcaad0dfa051 | 597 | return resp; |
spastor | 0:fcaad0dfa051 | 598 | |
spastor | 0:fcaad0dfa051 | 599 | uint8_t index = resp_frame->get_frame_type() == ApiFrame::TxStatusZB ? |
spastor | 0:fcaad0dfa051 | 600 | TX_STATUS_OFFSET_ZB : TX_STATUS_OFFSET_802; |
spastor | 0:fcaad0dfa051 | 601 | |
spastor | 0:fcaad0dfa051 | 602 | resp = (TxStatus)resp_frame->get_data_at(index); |
spastor | 0:fcaad0dfa051 | 603 | |
spastor | 0:fcaad0dfa051 | 604 | /* Once processed, remove the frame from the buffer */ |
spastor | 0:fcaad0dfa051 | 605 | _framebuf.free_frame(resp_frame); |
spastor | 0:fcaad0dfa051 | 606 | |
spastor | 0:fcaad0dfa051 | 607 | return resp; |
spastor | 0:fcaad0dfa051 | 608 | } |
spastor | 0:fcaad0dfa051 | 609 | |
spastor | 0:fcaad0dfa051 | 610 | TxStatus XBee::send_data_broadcast(const uint8_t *const data, uint16_t len) |
spastor | 0:fcaad0dfa051 | 611 | { |
spastor | 0:fcaad0dfa051 | 612 | const RemoteXBee remoteDevice = RemoteXBee(ADDR64_BROADCAST); |
spastor | 0:fcaad0dfa051 | 613 | return send_data(remoteDevice, data, len); |
spastor | 0:fcaad0dfa051 | 614 | } |
spastor | 0:fcaad0dfa051 | 615 | |
spastor | 0:fcaad0dfa051 | 616 | #if defined(ENABLE_PM_SUPPORT) |
spastor | 0:fcaad0dfa051 | 617 | |
spastor | 0:fcaad0dfa051 | 618 | RadioStatus XBee::set_pm_control(PmMode mode, PinName on_sleep, PinName sleep_rq) |
spastor | 0:fcaad0dfa051 | 619 | { |
spastor | 0:fcaad0dfa051 | 620 | if (on_sleep != NC) { |
spastor | 0:fcaad0dfa051 | 621 | _on_sleep = new InterruptIn(on_sleep); |
spastor | 0:fcaad0dfa051 | 622 | } |
spastor | 0:fcaad0dfa051 | 623 | if (sleep_rq != NC) { |
spastor | 0:fcaad0dfa051 | 624 | _sleep_req = new DigitalOut(sleep_rq); |
spastor | 0:fcaad0dfa051 | 625 | } |
spastor | 0:fcaad0dfa051 | 626 | _pm_mode = mode; |
spastor | 0:fcaad0dfa051 | 627 | /* TODO, set PM mode in the radio */ |
spastor | 0:fcaad0dfa051 | 628 | //return 0; |
spastor | 0:fcaad0dfa051 | 629 | return Success; |
spastor | 0:fcaad0dfa051 | 630 | } |
spastor | 0:fcaad0dfa051 | 631 | |
spastor | 0:fcaad0dfa051 | 632 | |
spastor | 0:fcaad0dfa051 | 633 | RadioStatus XBee::get_pm_mode(PmMode *mode) |
spastor | 0:fcaad0dfa051 | 634 | { |
spastor | 0:fcaad0dfa051 | 635 | *mode = _pm_mode; |
spastor | 0:fcaad0dfa051 | 636 | return Success; |
spastor | 0:fcaad0dfa051 | 637 | } |
spastor | 0:fcaad0dfa051 | 638 | |
spastor | 0:fcaad0dfa051 | 639 | RadioStatus XBee::config_pm_timing(uint32_t before_sleep_ms, uint32_t total_sleep_period_ms) |
spastor | 0:fcaad0dfa051 | 640 | { |
spastor | 0:fcaad0dfa051 | 641 | UNUSED_PARAMETER(before_sleep_ms); |
spastor | 0:fcaad0dfa051 | 642 | UNUSED_PARAMETER(total_sleep_period_ms); |
spastor | 0:fcaad0dfa051 | 643 | return Success; |
spastor | 0:fcaad0dfa051 | 644 | } |
spastor | 0:fcaad0dfa051 | 645 | |
spastor | 0:fcaad0dfa051 | 646 | RadioStatus XBee::enter_sleep_mode() |
spastor | 0:fcaad0dfa051 | 647 | { |
spastor | 0:fcaad0dfa051 | 648 | if (_sleep_req == NULL) |
spastor | 0:fcaad0dfa051 | 649 | return sleep_now(); |
spastor | 0:fcaad0dfa051 | 650 | |
spastor | 0:fcaad0dfa051 | 651 | _sleep_req->write(1); |
spastor | 0:fcaad0dfa051 | 652 | return Success; |
spastor | 0:fcaad0dfa051 | 653 | } |
spastor | 0:fcaad0dfa051 | 654 | |
spastor | 0:fcaad0dfa051 | 655 | void XBee::exit_sleep_mode() |
spastor | 0:fcaad0dfa051 | 656 | { |
spastor | 0:fcaad0dfa051 | 657 | /* TODO, check also mode? */ |
spastor | 0:fcaad0dfa051 | 658 | if (_sleep_req != NULL) |
spastor | 0:fcaad0dfa051 | 659 | _sleep_req->write(0); |
spastor | 0:fcaad0dfa051 | 660 | } |
spastor | 0:fcaad0dfa051 | 661 | |
spastor | 0:fcaad0dfa051 | 662 | bool XBee::is_sleeping() |
spastor | 0:fcaad0dfa051 | 663 | { |
spastor | 0:fcaad0dfa051 | 664 | /* TODO */ |
spastor | 0:fcaad0dfa051 | 665 | return true; |
spastor | 0:fcaad0dfa051 | 666 | } |
spastor | 0:fcaad0dfa051 | 667 | |
spastor | 0:fcaad0dfa051 | 668 | |
spastor | 0:fcaad0dfa051 | 669 | |
spastor | 0:fcaad0dfa051 | 670 | void XBee::register_wakeup_cb(void (*f)(void)) |
spastor | 0:fcaad0dfa051 | 671 | { |
spastor | 0:fcaad0dfa051 | 672 | if (_on_sleep == NULL) |
spastor | 0:fcaad0dfa051 | 673 | return; |
spastor | 0:fcaad0dfa051 | 674 | |
spastor | 0:fcaad0dfa051 | 675 | _on_sleep->rise(f); |
spastor | 0:fcaad0dfa051 | 676 | } |
spastor | 0:fcaad0dfa051 | 677 | |
spastor | 0:fcaad0dfa051 | 678 | void XBee::unregister_wakeup_cb() |
spastor | 0:fcaad0dfa051 | 679 | { |
spastor | 0:fcaad0dfa051 | 680 | _on_sleep->disable_irq(); |
spastor | 0:fcaad0dfa051 | 681 | _on_sleep->rise(NULL); |
spastor | 0:fcaad0dfa051 | 682 | } |
spastor | 0:fcaad0dfa051 | 683 | |
spastor | 0:fcaad0dfa051 | 684 | #endif /* defined(ENABLE_PM_SUPPORT) */ |
spastor | 0:fcaad0dfa051 | 685 | |
spastor | 0:fcaad0dfa051 | 686 | |
spastor | 0:fcaad0dfa051 | 687 | uint32_t XBee::process_rx_frames(void) |
spastor | 0:fcaad0dfa051 | 688 | { |
spastor | 0:fcaad0dfa051 | 689 | ApiFrame *frame = NULL; |
spastor | 0:fcaad0dfa051 | 690 | |
spastor | 0:fcaad0dfa051 | 691 | while ((frame = _framebuf.get_next_complete_frame_app()) != NULL) { |
spastor | 0:fcaad0dfa051 | 692 | for (int i = 0; i < MAX_FRAME_HANDLERS; i++) { |
spastor | 0:fcaad0dfa051 | 693 | |
spastor | 0:fcaad0dfa051 | 694 | if (_fhandlers[i] == NULL) { |
spastor | 0:fcaad0dfa051 | 695 | /* No more handlers, break here */ |
spastor | 0:fcaad0dfa051 | 696 | break; |
spastor | 0:fcaad0dfa051 | 697 | } |
spastor | 0:fcaad0dfa051 | 698 | |
spastor | 0:fcaad0dfa051 | 699 | /* Check if frame and handler match, if not... go for the next one */ |
spastor | 0:fcaad0dfa051 | 700 | if (frame->get_frame_type() != _fhandlers[i]->get_type()) { |
spastor | 0:fcaad0dfa051 | 701 | continue; |
spastor | 0:fcaad0dfa051 | 702 | } |
spastor | 0:fcaad0dfa051 | 703 | |
spastor | 0:fcaad0dfa051 | 704 | _fhandlers[i]->process_frame_data(frame); |
spastor | 0:fcaad0dfa051 | 705 | } |
spastor | 0:fcaad0dfa051 | 706 | |
spastor | 0:fcaad0dfa051 | 707 | /* Once processed, remove the frame from the buffer */ |
spastor | 0:fcaad0dfa051 | 708 | _framebuf.free_frame(frame); |
spastor | 0:fcaad0dfa051 | 709 | } |
spastor | 0:fcaad0dfa051 | 710 | |
spastor | 0:fcaad0dfa051 | 711 | return _framebuf.get_dropped_frames_count(); |
spastor | 0:fcaad0dfa051 | 712 | } |
spastor | 0:fcaad0dfa051 | 713 | |
spastor | 0:fcaad0dfa051 | 714 | void XBee::register_modem_status_cb(modem_status_cb_t function) |
spastor | 0:fcaad0dfa051 | 715 | { |
spastor | 0:fcaad0dfa051 | 716 | if (_modem_status_handler == NULL) { |
spastor | 0:fcaad0dfa051 | 717 | _modem_status_handler = new FH_ModemStatus(); |
spastor | 0:fcaad0dfa051 | 718 | register_frame_handler(_modem_status_handler); |
spastor | 0:fcaad0dfa051 | 719 | } |
spastor | 0:fcaad0dfa051 | 720 | _modem_status_handler->register_modem_status_cb(function); |
spastor | 0:fcaad0dfa051 | 721 | } |
spastor | 0:fcaad0dfa051 | 722 | |
spastor | 0:fcaad0dfa051 | 723 | void XBee::unregister_modem_status_cb() |
spastor | 0:fcaad0dfa051 | 724 | { |
spastor | 0:fcaad0dfa051 | 725 | if (_modem_status_handler != NULL) { |
spastor | 0:fcaad0dfa051 | 726 | _modem_status_handler->unregister_modem_status_cb(); |
spastor | 0:fcaad0dfa051 | 727 | unregister_frame_handler(_modem_status_handler); |
spastor | 0:fcaad0dfa051 | 728 | delete _modem_status_handler; |
spastor | 0:fcaad0dfa051 | 729 | _modem_status_handler = NULL; /* as delete does not set to NULL */ |
spastor | 0:fcaad0dfa051 | 730 | } |
spastor | 0:fcaad0dfa051 | 731 | } |
spastor | 0:fcaad0dfa051 | 732 |