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