Library to easily communicate with XBee modules.

Fork of XBeeLib by Digi International Inc.

Committer:
spastor
Date:
Fri May 08 11:50:56 2015 +0200
Revision:
0:fcaad0dfa051
Child:
1:794d1d3e4a08
Automatic upload

Who changed what in which revision?

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 #include "XBee802.h"
spastor 0:fcaad0dfa051 13 #include "IO/IOSample802.h"
spastor 0:fcaad0dfa051 14 #include "Frames/802_Frames.h"
spastor 0:fcaad0dfa051 15 #include "FrameHandlers/FH_ModemStatus.h"
spastor 0:fcaad0dfa051 16
spastor 0:fcaad0dfa051 17 using namespace XBeeLib;
spastor 0:fcaad0dfa051 18
spastor 0:fcaad0dfa051 19 /* Class constructor */
spastor 0:fcaad0dfa051 20 XBee802::XBee802(PinName tx, PinName rx, PinName reset, PinName rts, PinName cts, int baud) :
spastor 0:fcaad0dfa051 21 XBee(tx, rx, reset, rts, cts, baud),
spastor 0:fcaad0dfa051 22 _sync_lost_cnt(0), _nd_handler(NULL), _rx_64b_handler(NULL), _rx_16b_handler(NULL),
spastor 0:fcaad0dfa051 23 _io_data_64b_handler(NULL), _io_data_16b_handler(NULL)
spastor 0:fcaad0dfa051 24 {
spastor 0:fcaad0dfa051 25
spastor 0:fcaad0dfa051 26 _reset_timeout = RESET_TIMEOUT_MS;
spastor 0:fcaad0dfa051 27 }
spastor 0:fcaad0dfa051 28
spastor 0:fcaad0dfa051 29 /* Class destructor */
spastor 0:fcaad0dfa051 30 XBee802::~XBee802()
spastor 0:fcaad0dfa051 31 {
spastor 0:fcaad0dfa051 32 unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 33 unregister_receive_cb();
spastor 0:fcaad0dfa051 34 unregister_io_sample_cb();
spastor 0:fcaad0dfa051 35 }
spastor 0:fcaad0dfa051 36
spastor 0:fcaad0dfa051 37 RadioStatus XBee802::init()
spastor 0:fcaad0dfa051 38 {
spastor 0:fcaad0dfa051 39 RadioStatus retval = XBee::init();
spastor 0:fcaad0dfa051 40 const RadioProtocol radioProtocol = get_radio_protocol();
spastor 0:fcaad0dfa051 41 if (radioProtocol != Raw_802_15_4) {
spastor 0:fcaad0dfa051 42 digi_log(LogLevelError, "Radio protocol does not match, needed a %d got a %d\r\n", Raw_802_15_4, radioProtocol);
spastor 0:fcaad0dfa051 43 retval = Failure;
spastor 0:fcaad0dfa051 44 }
spastor 0:fcaad0dfa051 45 assert(radioProtocol == Raw_802_15_4);
spastor 0:fcaad0dfa051 46
spastor 0:fcaad0dfa051 47 return retval;
spastor 0:fcaad0dfa051 48 }
spastor 0:fcaad0dfa051 49
spastor 0:fcaad0dfa051 50 RadioStatus XBee802::set_channel(uint8_t channel)
spastor 0:fcaad0dfa051 51 {
spastor 0:fcaad0dfa051 52 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 53
spastor 0:fcaad0dfa051 54 if (is_PRO()) {
spastor 0:fcaad0dfa051 55 if (channel < 0x0C || channel > 0x17) {
spastor 0:fcaad0dfa051 56 return Failure;
spastor 0:fcaad0dfa051 57 }
spastor 0:fcaad0dfa051 58 } else {
spastor 0:fcaad0dfa051 59 if (channel < 0x0B || channel > 0x1A) {
spastor 0:fcaad0dfa051 60 return Failure;
spastor 0:fcaad0dfa051 61 }
spastor 0:fcaad0dfa051 62 }
spastor 0:fcaad0dfa051 63
spastor 0:fcaad0dfa051 64 cmdresp = set_param("CH", channel);
spastor 0:fcaad0dfa051 65 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 66 return Failure;
spastor 0:fcaad0dfa051 67 }
spastor 0:fcaad0dfa051 68 return Success;
spastor 0:fcaad0dfa051 69 }
spastor 0:fcaad0dfa051 70
spastor 0:fcaad0dfa051 71 RadioStatus XBee802::get_channel(uint8_t * const channel)
spastor 0:fcaad0dfa051 72 {
spastor 0:fcaad0dfa051 73 if (channel == NULL) {
spastor 0:fcaad0dfa051 74 return Failure;
spastor 0:fcaad0dfa051 75 }
spastor 0:fcaad0dfa051 76 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 77
spastor 0:fcaad0dfa051 78 uint32_t var32;
spastor 0:fcaad0dfa051 79 cmdresp = get_param("CH", &var32);
spastor 0:fcaad0dfa051 80 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 81 return Failure;
spastor 0:fcaad0dfa051 82 *channel = var32;
spastor 0:fcaad0dfa051 83 return Success;
spastor 0:fcaad0dfa051 84 }
spastor 0:fcaad0dfa051 85
spastor 0:fcaad0dfa051 86 RadioStatus XBee802::set_panid(uint16_t panid)
spastor 0:fcaad0dfa051 87 {
spastor 0:fcaad0dfa051 88 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 89
spastor 0:fcaad0dfa051 90 cmdresp = set_param("ID", panid);
spastor 0:fcaad0dfa051 91 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 92 return Failure;
spastor 0:fcaad0dfa051 93 }
spastor 0:fcaad0dfa051 94 return Success;
spastor 0:fcaad0dfa051 95 }
spastor 0:fcaad0dfa051 96
spastor 0:fcaad0dfa051 97 RadioStatus XBee802::get_panid(uint16_t * const panid)
spastor 0:fcaad0dfa051 98 {
spastor 0:fcaad0dfa051 99 if (panid == NULL) {
spastor 0:fcaad0dfa051 100 return Failure;
spastor 0:fcaad0dfa051 101 }
spastor 0:fcaad0dfa051 102 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 103
spastor 0:fcaad0dfa051 104 uint32_t var32;
spastor 0:fcaad0dfa051 105 cmdresp = get_param("ID", &var32);
spastor 0:fcaad0dfa051 106 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 107 return Failure;
spastor 0:fcaad0dfa051 108 *panid = var32;
spastor 0:fcaad0dfa051 109 return Success;
spastor 0:fcaad0dfa051 110 }
spastor 0:fcaad0dfa051 111
spastor 0:fcaad0dfa051 112 RadioStatus XBee802::set_network_address(uint16_t addr16)
spastor 0:fcaad0dfa051 113 {
spastor 0:fcaad0dfa051 114 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 115
spastor 0:fcaad0dfa051 116 cmdresp = set_param("MY", addr16);
spastor 0:fcaad0dfa051 117 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 118 return Failure;
spastor 0:fcaad0dfa051 119 }
spastor 0:fcaad0dfa051 120 return Success;
spastor 0:fcaad0dfa051 121 }
spastor 0:fcaad0dfa051 122
spastor 0:fcaad0dfa051 123 void XBee802::radio_status_update(AtCmdFrame::ModemStatus modem_status)
spastor 0:fcaad0dfa051 124 {
spastor 0:fcaad0dfa051 125 /* Update the radio status variables */
spastor 0:fcaad0dfa051 126 if (modem_status == AtCmdFrame::HwReset)
spastor 0:fcaad0dfa051 127 _hw_reset_cnt++;
spastor 0:fcaad0dfa051 128 else if (modem_status == AtCmdFrame::WdReset)
spastor 0:fcaad0dfa051 129 _wd_reset_cnt++;
spastor 0:fcaad0dfa051 130 else if (modem_status == AtCmdFrame::SyncLost)
spastor 0:fcaad0dfa051 131 _sync_lost_cnt++;
spastor 0:fcaad0dfa051 132
spastor 0:fcaad0dfa051 133 _modem_status = modem_status;
spastor 0:fcaad0dfa051 134
spastor 0:fcaad0dfa051 135 digi_log(LogLevelDebug, "\r\nUpdating radio status: %02x\r\n", modem_status);
spastor 0:fcaad0dfa051 136 }
spastor 0:fcaad0dfa051 137
spastor 0:fcaad0dfa051 138 TxStatus XBee802::send_data(const RemoteXBee& remote, const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 139 {
spastor 0:fcaad0dfa051 140 if (remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 141 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 142
spastor 0:fcaad0dfa051 143 digi_log(LogLevelDebug, "send_data ADDR64: %08x:%08x\r\n", UINT64_HI32(remote64), UINT64_LO32(remote64));
spastor 0:fcaad0dfa051 144
spastor 0:fcaad0dfa051 145 TxFrame802 frame = TxFrame802(remote64, _tx_options, data, len);
spastor 0:fcaad0dfa051 146
spastor 0:fcaad0dfa051 147 return send_data(&frame);
spastor 0:fcaad0dfa051 148 }
spastor 0:fcaad0dfa051 149
spastor 0:fcaad0dfa051 150 if (remote.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 151 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 152
spastor 0:fcaad0dfa051 153 digi_log(LogLevelDebug, "send_data ADDR16: %04x\r\n", remote16);
spastor 0:fcaad0dfa051 154
spastor 0:fcaad0dfa051 155 TxFrame802 frame = TxFrame802(remote16, _tx_options, data, len);
spastor 0:fcaad0dfa051 156
spastor 0:fcaad0dfa051 157 return send_data(&frame);
spastor 0:fcaad0dfa051 158 }
spastor 0:fcaad0dfa051 159
spastor 0:fcaad0dfa051 160 return TxStatusInvalidAddr;
spastor 0:fcaad0dfa051 161 }
spastor 0:fcaad0dfa051 162
spastor 0:fcaad0dfa051 163 RemoteXBee802 XBee802::get_remote_node_by_id(const char * const node_id)
spastor 0:fcaad0dfa051 164 {
spastor 0:fcaad0dfa051 165 uint64_t addr64;
spastor 0:fcaad0dfa051 166 uint16_t addr16;
spastor 0:fcaad0dfa051 167 _get_remote_node_by_id(node_id, &addr64, &addr16);
spastor 0:fcaad0dfa051 168 return RemoteXBee802(addr64, addr16);
spastor 0:fcaad0dfa051 169 }
spastor 0:fcaad0dfa051 170
spastor 0:fcaad0dfa051 171 void XBee802::register_node_discovery_cb(node_discovery_802_cb_t function)
spastor 0:fcaad0dfa051 172 {
spastor 0:fcaad0dfa051 173 if (_nd_handler == NULL) {
spastor 0:fcaad0dfa051 174 _nd_handler = new FH_NodeDiscovery802();
spastor 0:fcaad0dfa051 175 register_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 176 }
spastor 0:fcaad0dfa051 177 _nd_handler->register_node_discovery_cb(function);
spastor 0:fcaad0dfa051 178 }
spastor 0:fcaad0dfa051 179
spastor 0:fcaad0dfa051 180 void XBee802::unregister_node_discovery_cb()
spastor 0:fcaad0dfa051 181 {
spastor 0:fcaad0dfa051 182 if (_nd_handler != NULL) {
spastor 0:fcaad0dfa051 183 _nd_handler->unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 184 unregister_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 185 delete _nd_handler;
spastor 0:fcaad0dfa051 186 _nd_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 187 }
spastor 0:fcaad0dfa051 188 }
spastor 0:fcaad0dfa051 189
spastor 0:fcaad0dfa051 190 void XBee802::register_receive_cb(receive_802_cb_t function)
spastor 0:fcaad0dfa051 191 {
spastor 0:fcaad0dfa051 192 if (_rx_64b_handler == NULL) {
spastor 0:fcaad0dfa051 193 _rx_64b_handler = new FH_RxPacket64b802();
spastor 0:fcaad0dfa051 194 register_frame_handler(_rx_64b_handler);
spastor 0:fcaad0dfa051 195 }
spastor 0:fcaad0dfa051 196 _rx_64b_handler->register_receive_cb(function);
spastor 0:fcaad0dfa051 197
spastor 0:fcaad0dfa051 198 if (_rx_16b_handler == NULL) {
spastor 0:fcaad0dfa051 199 _rx_16b_handler = new FH_RxPacket16b802();
spastor 0:fcaad0dfa051 200 register_frame_handler(_rx_16b_handler);
spastor 0:fcaad0dfa051 201 }
spastor 0:fcaad0dfa051 202 _rx_16b_handler->register_receive_cb(function);
spastor 0:fcaad0dfa051 203 }
spastor 0:fcaad0dfa051 204
spastor 0:fcaad0dfa051 205 void XBee802::unregister_receive_cb()
spastor 0:fcaad0dfa051 206 {
spastor 0:fcaad0dfa051 207 if (_rx_64b_handler != NULL) {
spastor 0:fcaad0dfa051 208 _rx_64b_handler->unregister_receive_cb();
spastor 0:fcaad0dfa051 209 unregister_frame_handler(_rx_64b_handler);
spastor 0:fcaad0dfa051 210 delete _rx_64b_handler;
spastor 0:fcaad0dfa051 211 _rx_64b_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 212 }
spastor 0:fcaad0dfa051 213
spastor 0:fcaad0dfa051 214 if (_rx_16b_handler != NULL) {
spastor 0:fcaad0dfa051 215 _rx_16b_handler->unregister_receive_cb();
spastor 0:fcaad0dfa051 216 unregister_frame_handler(_rx_16b_handler);
spastor 0:fcaad0dfa051 217 delete _rx_16b_handler;
spastor 0:fcaad0dfa051 218 _rx_16b_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 219 }
spastor 0:fcaad0dfa051 220 }
spastor 0:fcaad0dfa051 221
spastor 0:fcaad0dfa051 222 void XBee802::register_io_sample_cb(io_data_cb_802_t function)
spastor 0:fcaad0dfa051 223 {
spastor 0:fcaad0dfa051 224 if (_io_data_64b_handler == NULL) {
spastor 0:fcaad0dfa051 225 _io_data_64b_handler = new FH_IoDataSampe64b802();
spastor 0:fcaad0dfa051 226 register_frame_handler(_io_data_64b_handler);
spastor 0:fcaad0dfa051 227 }
spastor 0:fcaad0dfa051 228 _io_data_64b_handler->register_io_data_cb(function);
spastor 0:fcaad0dfa051 229
spastor 0:fcaad0dfa051 230 if (_io_data_16b_handler == NULL) {
spastor 0:fcaad0dfa051 231 _io_data_16b_handler = new FH_IoDataSampe16b802();
spastor 0:fcaad0dfa051 232 register_frame_handler(_io_data_16b_handler);
spastor 0:fcaad0dfa051 233 }
spastor 0:fcaad0dfa051 234 _io_data_16b_handler->register_io_data_cb(function);
spastor 0:fcaad0dfa051 235 }
spastor 0:fcaad0dfa051 236
spastor 0:fcaad0dfa051 237 void XBee802::unregister_io_sample_cb()
spastor 0:fcaad0dfa051 238 {
spastor 0:fcaad0dfa051 239 if (_io_data_64b_handler != NULL) {
spastor 0:fcaad0dfa051 240 _io_data_64b_handler->unregister_io_data_cb();
spastor 0:fcaad0dfa051 241 unregister_frame_handler(_io_data_64b_handler);
spastor 0:fcaad0dfa051 242 delete _io_data_64b_handler;
spastor 0:fcaad0dfa051 243 _io_data_64b_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 244 }
spastor 0:fcaad0dfa051 245
spastor 0:fcaad0dfa051 246 if (_io_data_16b_handler != NULL) {
spastor 0:fcaad0dfa051 247 _io_data_16b_handler->unregister_io_data_cb();
spastor 0:fcaad0dfa051 248 unregister_frame_handler(_io_data_16b_handler);
spastor 0:fcaad0dfa051 249 delete _io_data_16b_handler;
spastor 0:fcaad0dfa051 250 _io_data_16b_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 251 }
spastor 0:fcaad0dfa051 252 }
spastor 0:fcaad0dfa051 253
spastor 0:fcaad0dfa051 254 AtCmdFrame::AtCmdResp XBee802::get_param(const RemoteXBee& remote, const char * const param, uint32_t * const data)
spastor 0:fcaad0dfa051 255 {
spastor 0:fcaad0dfa051 256 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 257 AtCmdFrame::AtCmdResp atCmdResponse;
spastor 0:fcaad0dfa051 258
spastor 0:fcaad0dfa051 259 if (remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 260 const uint64_t dev_addr64 = remote.get_addr64();
spastor 0:fcaad0dfa051 261
spastor 0:fcaad0dfa051 262 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr64, param);
spastor 0:fcaad0dfa051 263 atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len, RadioRemote);
spastor 0:fcaad0dfa051 264 } else if (remote.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 265 const uint16_t dev_addr16 = remote.get_addr16();
spastor 0:fcaad0dfa051 266
spastor 0:fcaad0dfa051 267 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr16, param);
spastor 0:fcaad0dfa051 268 atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len, RadioRemote);
spastor 0:fcaad0dfa051 269 } else {
spastor 0:fcaad0dfa051 270 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 271 }
spastor 0:fcaad0dfa051 272
spastor 0:fcaad0dfa051 273 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) {
spastor 0:fcaad0dfa051 274 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 0:fcaad0dfa051 275 }
spastor 0:fcaad0dfa051 276
spastor 0:fcaad0dfa051 277 return atCmdResponse;
spastor 0:fcaad0dfa051 278 }
spastor 0:fcaad0dfa051 279
spastor 0:fcaad0dfa051 280 AtCmdFrame::AtCmdResp XBee802::set_param(const RemoteXBee& remote, const char * const param, uint32_t data)
spastor 0:fcaad0dfa051 281 {
spastor 0:fcaad0dfa051 282 if (remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 283 const uint64_t dev_addr64 = remote.get_addr64();
spastor 0:fcaad0dfa051 284
spastor 0:fcaad0dfa051 285 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr64, param, data);
spastor 0:fcaad0dfa051 286 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 287 }
spastor 0:fcaad0dfa051 288
spastor 0:fcaad0dfa051 289 if (remote.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 290 const uint16_t dev_addr16 = remote.get_addr16();
spastor 0:fcaad0dfa051 291
spastor 0:fcaad0dfa051 292 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr16, param, data);
spastor 0:fcaad0dfa051 293 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 294 }
spastor 0:fcaad0dfa051 295
spastor 0:fcaad0dfa051 296 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 297 }
spastor 0:fcaad0dfa051 298
spastor 0:fcaad0dfa051 299 AtCmdFrame::AtCmdResp XBee802::set_param(const RemoteXBee& remote, const char * const param, const uint8_t * data, uint16_t len)
spastor 0:fcaad0dfa051 300 {
spastor 0:fcaad0dfa051 301 if (remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 302 const uint64_t dev_addr64 = remote.get_addr64();
spastor 0:fcaad0dfa051 303
spastor 0:fcaad0dfa051 304 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr64, param, data, len);
spastor 0:fcaad0dfa051 305 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 306 }
spastor 0:fcaad0dfa051 307
spastor 0:fcaad0dfa051 308 if (remote.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 309 const uint16_t dev_addr16 = remote.get_addr16();
spastor 0:fcaad0dfa051 310
spastor 0:fcaad0dfa051 311 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr16, param, data, len);
spastor 0:fcaad0dfa051 312 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 313 }
spastor 0:fcaad0dfa051 314
spastor 0:fcaad0dfa051 315 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 316 }
spastor 0:fcaad0dfa051 317
spastor 0:fcaad0dfa051 318 AtCmdFrame::AtCmdResp XBee802::get_param(const RemoteXBee& remote, const char * const param, uint8_t * const data, uint16_t * const len)
spastor 0:fcaad0dfa051 319 {
spastor 0:fcaad0dfa051 320 if (remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 321 uint64_t dev_addr64 = remote.get_addr64();
spastor 0:fcaad0dfa051 322
spastor 0:fcaad0dfa051 323 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr64, param);
spastor 0:fcaad0dfa051 324 return send_at_cmd(&cmd_frame, data, len, RadioRemote, false);
spastor 0:fcaad0dfa051 325 }
spastor 0:fcaad0dfa051 326
spastor 0:fcaad0dfa051 327 if (remote.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 328 uint16_t dev_addr16 = remote.get_addr16();
spastor 0:fcaad0dfa051 329
spastor 0:fcaad0dfa051 330 AtCmdFrame cmd_frame = AtCmdFrame(dev_addr16, param);
spastor 0:fcaad0dfa051 331 return send_at_cmd(&cmd_frame, data, len, RadioRemote, false);
spastor 0:fcaad0dfa051 332 }
spastor 0:fcaad0dfa051 333
spastor 0:fcaad0dfa051 334 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 335 }
spastor 0:fcaad0dfa051 336
spastor 0:fcaad0dfa051 337 static void get_dio_cmd(XBee802::IoLine line, char * const iocmd)
spastor 0:fcaad0dfa051 338 {
spastor 0:fcaad0dfa051 339 if (line >= XBee802::PWM0) {
spastor 0:fcaad0dfa051 340 iocmd[0] = 'P';
spastor 0:fcaad0dfa051 341 iocmd[1] = '0' + line - XBee802::PWM0;
spastor 0:fcaad0dfa051 342 } else {
spastor 0:fcaad0dfa051 343 iocmd[0] = 'D';
spastor 0:fcaad0dfa051 344 iocmd[1] = '0' + line;
spastor 0:fcaad0dfa051 345 }
spastor 0:fcaad0dfa051 346 iocmd[2] = '\0';
spastor 0:fcaad0dfa051 347 }
spastor 0:fcaad0dfa051 348
spastor 0:fcaad0dfa051 349 RadioStatus XBee802::set_pin_config(const RemoteXBee& remote, IoLine line, IoMode mode)
spastor 0:fcaad0dfa051 350 {
spastor 0:fcaad0dfa051 351 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 352 char iocmd[3];
spastor 0:fcaad0dfa051 353
spastor 0:fcaad0dfa051 354 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 355
spastor 0:fcaad0dfa051 356 cmdresp = set_param(remote, iocmd, (uint8_t)mode);
spastor 0:fcaad0dfa051 357 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 358 digi_log(LogLevelError, "set_pin_config: set_param returned %d\r\n", cmdresp);
spastor 0:fcaad0dfa051 359 return Failure;
spastor 0:fcaad0dfa051 360 }
spastor 0:fcaad0dfa051 361
spastor 0:fcaad0dfa051 362 return Success;
spastor 0:fcaad0dfa051 363 }
spastor 0:fcaad0dfa051 364
spastor 0:fcaad0dfa051 365 RadioStatus XBee802::get_pin_config(const RemoteXBee& remote, IoLine line, IoMode * const mode)
spastor 0:fcaad0dfa051 366 {
spastor 0:fcaad0dfa051 367 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 368 char iocmd[3];
spastor 0:fcaad0dfa051 369
spastor 0:fcaad0dfa051 370 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 371
spastor 0:fcaad0dfa051 372 uint32_t var32;
spastor 0:fcaad0dfa051 373 cmdresp = get_param(remote, iocmd, &var32);
spastor 0:fcaad0dfa051 374 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 375 return Failure;
spastor 0:fcaad0dfa051 376 }
spastor 0:fcaad0dfa051 377 *mode = (IoMode)var32;
spastor 0:fcaad0dfa051 378
spastor 0:fcaad0dfa051 379 return Success;
spastor 0:fcaad0dfa051 380 }
spastor 0:fcaad0dfa051 381
spastor 0:fcaad0dfa051 382 RadioStatus XBee802::set_dio(const RemoteXBee& remote, IoLine line, DioVal val)
spastor 0:fcaad0dfa051 383 {
spastor 0:fcaad0dfa051 384 if (line > DI8) {
spastor 0:fcaad0dfa051 385 digi_log(LogLevelError, "set_dio: Pin %d not supported as IO\r\n", line);
spastor 0:fcaad0dfa051 386 return Failure;
spastor 0:fcaad0dfa051 387 }
spastor 0:fcaad0dfa051 388
spastor 0:fcaad0dfa051 389 if (val == Low) {
spastor 0:fcaad0dfa051 390 return set_pin_config(remote, line, DigitalOutLow);
spastor 0:fcaad0dfa051 391 } else {
spastor 0:fcaad0dfa051 392 return set_pin_config(remote, line, DigitalOutHigh);
spastor 0:fcaad0dfa051 393 }
spastor 0:fcaad0dfa051 394 }
spastor 0:fcaad0dfa051 395
spastor 0:fcaad0dfa051 396 RadioStatus XBee802::get_dio(const RemoteXBee& remote, IoLine line, DioVal * const val)
spastor 0:fcaad0dfa051 397 {
spastor 0:fcaad0dfa051 398 uint8_t io_sample[MAX_IO_SAMPLE_BUF_LEN];
spastor 0:fcaad0dfa051 399 uint16_t len;
spastor 0:fcaad0dfa051 400
spastor 0:fcaad0dfa051 401 if (line > DI8) {
spastor 0:fcaad0dfa051 402 digi_log(LogLevelError, "get_dio: Pin %d not supported as IO\r\n", line);
spastor 0:fcaad0dfa051 403 return Failure;
spastor 0:fcaad0dfa051 404 }
spastor 0:fcaad0dfa051 405
spastor 0:fcaad0dfa051 406 RadioStatus resp = get_iosample(remote, io_sample, &len);
spastor 0:fcaad0dfa051 407 if (resp != Success)
spastor 0:fcaad0dfa051 408 return resp;
spastor 0:fcaad0dfa051 409
spastor 0:fcaad0dfa051 410 IOSample802 ioSample = IOSample802(io_sample, len);
spastor 0:fcaad0dfa051 411 return ioSample.get_dio(line, val);
spastor 0:fcaad0dfa051 412 }
spastor 0:fcaad0dfa051 413
spastor 0:fcaad0dfa051 414 RadioStatus XBee802::get_adc(const RemoteXBee& remote, IoLine line, uint16_t * const val)
spastor 0:fcaad0dfa051 415 {
spastor 0:fcaad0dfa051 416 uint8_t io_sample[MAX_IO_SAMPLE_BUF_LEN];
spastor 0:fcaad0dfa051 417 uint16_t len;
spastor 0:fcaad0dfa051 418
spastor 0:fcaad0dfa051 419 if (line > DIO5_AD5) {
spastor 0:fcaad0dfa051 420 digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line);
spastor 0:fcaad0dfa051 421 return Failure;
spastor 0:fcaad0dfa051 422 }
spastor 0:fcaad0dfa051 423
spastor 0:fcaad0dfa051 424 RadioStatus resp = get_iosample(remote, io_sample, &len);
spastor 0:fcaad0dfa051 425 if (resp != Success) {
spastor 0:fcaad0dfa051 426 return resp;
spastor 0:fcaad0dfa051 427 }
spastor 0:fcaad0dfa051 428
spastor 0:fcaad0dfa051 429 IOSample802 ioSample = IOSample802(io_sample, len);
spastor 0:fcaad0dfa051 430 return ioSample.get_adc(line, val);
spastor 0:fcaad0dfa051 431 }
spastor 0:fcaad0dfa051 432
spastor 0:fcaad0dfa051 433 RadioStatus XBee802::set_pwm(const RemoteXBee& remote, IoLine line, float duty_cycle)
spastor 0:fcaad0dfa051 434 {
spastor 0:fcaad0dfa051 435 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 436 char iocmd[3] = { 'M', '0', '\0' };
spastor 0:fcaad0dfa051 437
spastor 0:fcaad0dfa051 438 if (line != PWM0 && line != PWM1) {
spastor 0:fcaad0dfa051 439 return Failure;
spastor 0:fcaad0dfa051 440 }
spastor 0:fcaad0dfa051 441 if (line == PWM1) {
spastor 0:fcaad0dfa051 442 iocmd[1] = '1';
spastor 0:fcaad0dfa051 443 }
spastor 0:fcaad0dfa051 444
spastor 0:fcaad0dfa051 445 uint16_t pwm_val = (uint16_t)(duty_cycle * DR_PWM_MAX_VAL / 100);
spastor 0:fcaad0dfa051 446
spastor 0:fcaad0dfa051 447 cmdresp = set_param(remote, iocmd, pwm_val);
spastor 0:fcaad0dfa051 448 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 449 return Failure;
spastor 0:fcaad0dfa051 450
spastor 0:fcaad0dfa051 451 return Success;
spastor 0:fcaad0dfa051 452 }
spastor 0:fcaad0dfa051 453
spastor 0:fcaad0dfa051 454 static uint8_t get_dio_mask(XBee802::IoLine line)
spastor 0:fcaad0dfa051 455 {
spastor 0:fcaad0dfa051 456 switch (line) {
spastor 0:fcaad0dfa051 457 case XBee802::DIO4_AD4:
spastor 0:fcaad0dfa051 458 return (1 << 0);
spastor 0:fcaad0dfa051 459 case XBee802::DIO3_AD3:
spastor 0:fcaad0dfa051 460 return (1 << 1);
spastor 0:fcaad0dfa051 461 case XBee802::DIO2_AD2:
spastor 0:fcaad0dfa051 462 return (1 << 2);
spastor 0:fcaad0dfa051 463 case XBee802::DIO1_AD1:
spastor 0:fcaad0dfa051 464 return (1 << 3);
spastor 0:fcaad0dfa051 465 case XBee802::DIO0_AD0:
spastor 0:fcaad0dfa051 466 return (1 << 4);
spastor 0:fcaad0dfa051 467 case XBee802::DIO6:
spastor 0:fcaad0dfa051 468 return (1 << 5);
spastor 0:fcaad0dfa051 469 case XBee802::DI8:
spastor 0:fcaad0dfa051 470 return (1 << 6);
spastor 0:fcaad0dfa051 471 default:
spastor 0:fcaad0dfa051 472 return 0;
spastor 0:fcaad0dfa051 473 }
spastor 0:fcaad0dfa051 474 }
spastor 0:fcaad0dfa051 475
spastor 0:fcaad0dfa051 476 RadioStatus XBee802::set_pin_pull_up(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 477 {
spastor 0:fcaad0dfa051 478 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 479 uint32_t var32;
spastor 0:fcaad0dfa051 480 uint8_t pr;
spastor 0:fcaad0dfa051 481
spastor 0:fcaad0dfa051 482 cmdresp = get_param(remote, "PR", &var32);
spastor 0:fcaad0dfa051 483 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 484 return Failure;
spastor 0:fcaad0dfa051 485 pr = var32;
spastor 0:fcaad0dfa051 486
spastor 0:fcaad0dfa051 487 const uint8_t dio_mask = get_dio_mask(line);
spastor 0:fcaad0dfa051 488 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 489 digi_log(LogLevelError, "XBee802::set_pin_pull_up: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 490 return Failure;
spastor 0:fcaad0dfa051 491 }
spastor 0:fcaad0dfa051 492
spastor 0:fcaad0dfa051 493 if (enable) {
spastor 0:fcaad0dfa051 494 pr |= dio_mask;
spastor 0:fcaad0dfa051 495 } else {
spastor 0:fcaad0dfa051 496 pr &= ~dio_mask;
spastor 0:fcaad0dfa051 497 }
spastor 0:fcaad0dfa051 498
spastor 0:fcaad0dfa051 499 cmdresp = set_param(remote, "PR", pr);
spastor 0:fcaad0dfa051 500 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 501 return Failure;
spastor 0:fcaad0dfa051 502
spastor 0:fcaad0dfa051 503 return Success;
spastor 0:fcaad0dfa051 504 }
spastor 0:fcaad0dfa051 505
spastor 0:fcaad0dfa051 506 static uint8_t get_dio_ic_mask(XBee802::IoLine line)
spastor 0:fcaad0dfa051 507 {
spastor 0:fcaad0dfa051 508 if (line < XBee802::DI8) {
spastor 0:fcaad0dfa051 509 return (1 << line);
spastor 0:fcaad0dfa051 510 }
spastor 0:fcaad0dfa051 511 return 0;
spastor 0:fcaad0dfa051 512 }
spastor 0:fcaad0dfa051 513
spastor 0:fcaad0dfa051 514 RadioStatus XBee802::enable_dio_change_detection(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 515 {
spastor 0:fcaad0dfa051 516 if (line > DIO7) {
spastor 0:fcaad0dfa051 517 digi_log(LogLevelError, "XBee802::enable_dio_change_detection: pin not supported (%d)\r\n", line);
spastor 0:fcaad0dfa051 518 return Failure;
spastor 0:fcaad0dfa051 519 }
spastor 0:fcaad0dfa051 520
spastor 0:fcaad0dfa051 521 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 522 uint32_t var32;
spastor 0:fcaad0dfa051 523 uint8_t ic;
spastor 0:fcaad0dfa051 524
spastor 0:fcaad0dfa051 525 cmdresp = get_param(remote, "IC", &var32);
spastor 0:fcaad0dfa051 526 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 527 return Failure;
spastor 0:fcaad0dfa051 528 }
spastor 0:fcaad0dfa051 529 ic = var32;
spastor 0:fcaad0dfa051 530
spastor 0:fcaad0dfa051 531 const uint8_t dio_mask = get_dio_ic_mask(line);
spastor 0:fcaad0dfa051 532 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 533 digi_log(LogLevelError, "XBeeZB::enable_dio_change_detection: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 534 return Failure;
spastor 0:fcaad0dfa051 535 }
spastor 0:fcaad0dfa051 536
spastor 0:fcaad0dfa051 537 if (enable) {
spastor 0:fcaad0dfa051 538 ic |= dio_mask;
spastor 0:fcaad0dfa051 539 } else {
spastor 0:fcaad0dfa051 540 ic &= ~dio_mask;
spastor 0:fcaad0dfa051 541 }
spastor 0:fcaad0dfa051 542
spastor 0:fcaad0dfa051 543 cmdresp = set_param(remote, "IC", ic);
spastor 0:fcaad0dfa051 544 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 545 return Failure;
spastor 0:fcaad0dfa051 546
spastor 0:fcaad0dfa051 547 return Success;
spastor 0:fcaad0dfa051 548 }
spastor 0:fcaad0dfa051 549
spastor 0:fcaad0dfa051 550 #ifdef GET_PWM_AVAILABLE
spastor 0:fcaad0dfa051 551 RadioStatus XBee802::get_pwm(const RemoteXBee& remote, IoLine line, float * const duty_cycle)
spastor 0:fcaad0dfa051 552 {
spastor 0:fcaad0dfa051 553 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 554 char iocmd[3] = { 'M', '0', '\0' };
spastor 0:fcaad0dfa051 555
spastor 0:fcaad0dfa051 556 if (line != PWM0 && line != PWM1)
spastor 0:fcaad0dfa051 557 return Failure;
spastor 0:fcaad0dfa051 558
spastor 0:fcaad0dfa051 559 if (line == PWM1)
spastor 0:fcaad0dfa051 560 iocmd[1] = '1';
spastor 0:fcaad0dfa051 561
spastor 0:fcaad0dfa051 562 uint16_t pwm_val;
spastor 0:fcaad0dfa051 563
spastor 0:fcaad0dfa051 564 cmdresp = get_param(remote, iocmd, &pwm_val);
spastor 0:fcaad0dfa051 565 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 566 return Failure;
spastor 0:fcaad0dfa051 567
spastor 0:fcaad0dfa051 568 *duty_cycle = (float)(pwm_val * 100 / DR_PWM_MAX_VAL);
spastor 0:fcaad0dfa051 569
spastor 0:fcaad0dfa051 570 return Success;
spastor 0:fcaad0dfa051 571 }
spastor 0:fcaad0dfa051 572 #endif