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