Digi XBeeLib fixed for NonCopyable issue

Dependents:   XBeeZB_Receive_Data

Fork of XBeeLib by Digi International Inc.

This lib fixes NonCopyable<T> issues of Digi XBeeLib. Also, lib has been reworked in order to make it RTOS-aware, overcoming several others issues due to stdio Mutex operations.

Committer:
Lorenzo Maiorfi
Date:
Sun Mar 25 16:54:52 2018 +0200
Revision:
12:8dc9761210c1
Parent:
11:c49cf952d67d
Child:
13:09e0b4f5a9e1
Fixed thread-affinity. Please use ENABLE_THREAD_SAFE_LOGGING #define in config.h to enable RTOS support.

Who changed what in which revision?

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