Library to easily communicate with XBee modules.

Fork of XBeeLib by Digi International Inc.

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?

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 "XBeeZB.h"
spastor 0:fcaad0dfa051 14 #include "IO/IOSampleZB.h"
spastor 0:fcaad0dfa051 15 #include "Frames/ZigbeeFrames.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 XBeeZB::XBeeZB(PinName tx, PinName rx, PinName reset, PinName rts, PinName cts, int baud) :
spastor 0:fcaad0dfa051 21 XBee(tx, rx, reset, rts, cts, baud), _nw_role(UnknownRole), _joined_network(false),
spastor 0:fcaad0dfa051 22 _vcc_exceeded_cnt(0), _broadcast_radious(0), _nd_handler(NULL),
spastor 0:fcaad0dfa051 23 _rx_pkt_handler(NULL), _io_data_handler(NULL)
spastor 0:fcaad0dfa051 24 {
spastor 0:fcaad0dfa051 25 _reset_timeout = RESET_TIMEOUT_MS;
spastor 0:fcaad0dfa051 26 }
spastor 0:fcaad0dfa051 27
spastor 0:fcaad0dfa051 28 RadioStatus XBeeZB::init()
spastor 0:fcaad0dfa051 29 {
spastor 0:fcaad0dfa051 30 RadioStatus retval = XBee::init();
spastor 0:fcaad0dfa051 31 /* Determine the role of this device in the network */
spastor 0:fcaad0dfa051 32 switch(_fw_version & 0xFF00) {
spastor 0:fcaad0dfa051 33 case 0x2100: _nw_role = Coordinator; break;
spastor 0:fcaad0dfa051 34 case 0x2300: _nw_role = Router; break;
spastor 0:fcaad0dfa051 35 case 0x2900: _nw_role = EndDevice; break;
spastor 0:fcaad0dfa051 36 default: _nw_role = UnknownRole; break;
spastor 0:fcaad0dfa051 37 }
spastor 0:fcaad0dfa051 38
spastor 0:fcaad0dfa051 39 const RadioProtocol radioProtocol = get_radio_protocol();
spastor 0:fcaad0dfa051 40 if (radioProtocol != ZigBee) {
spastor 0:fcaad0dfa051 41 digi_log(LogLevelError, "Radio protocol does not match, needed a %d got a %d\r\n", ZigBee, radioProtocol);
spastor 0:fcaad0dfa051 42 retval = Failure;
spastor 0:fcaad0dfa051 43 }
spastor 0:fcaad0dfa051 44 assert(radioProtocol == ZigBee);
spastor 0:fcaad0dfa051 45
spastor 0:fcaad0dfa051 46 return retval;
spastor 0:fcaad0dfa051 47 }
spastor 0:fcaad0dfa051 48
spastor 0:fcaad0dfa051 49 /* Class destructor */
spastor 0:fcaad0dfa051 50 XBeeZB::~XBeeZB()
spastor 0:fcaad0dfa051 51 {
spastor 0:fcaad0dfa051 52 unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 53 unregister_receive_cb();
spastor 0:fcaad0dfa051 54 unregister_io_sample_cb();
spastor 0:fcaad0dfa051 55 }
spastor 0:fcaad0dfa051 56
spastor 0:fcaad0dfa051 57 RadioStatus XBeeZB::set_channel_mask(uint16_t chmask)
spastor 0:fcaad0dfa051 58 {
spastor 0:fcaad0dfa051 59 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 60
spastor 0:fcaad0dfa051 61 cmdresp = set_param("SC", chmask);
spastor 0:fcaad0dfa051 62 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 63 return Failure;
spastor 0:fcaad0dfa051 64 }
spastor 0:fcaad0dfa051 65 return Success;
spastor 0:fcaad0dfa051 66 }
spastor 0:fcaad0dfa051 67
spastor 0:fcaad0dfa051 68 RadioStatus XBeeZB::get_channel_mask(uint16_t * const chmask)
spastor 0:fcaad0dfa051 69 {
spastor 0:fcaad0dfa051 70 if (chmask == NULL) {
spastor 0:fcaad0dfa051 71 return Failure;
spastor 0:fcaad0dfa051 72 }
spastor 0:fcaad0dfa051 73 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 74
spastor 0:fcaad0dfa051 75 uint32_t var32;
spastor 0:fcaad0dfa051 76 cmdresp = get_param("SC", &var32);
spastor 0:fcaad0dfa051 77 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 78 return Failure;
spastor 0:fcaad0dfa051 79 *chmask = var32;
spastor 0:fcaad0dfa051 80 return Success;
spastor 0:fcaad0dfa051 81 }
spastor 0:fcaad0dfa051 82
spastor 0:fcaad0dfa051 83 RadioStatus XBeeZB::set_panid(uint64_t panid)
spastor 0:fcaad0dfa051 84 {
spastor 0:fcaad0dfa051 85 uint8_t panid_u8[8];
spastor 0:fcaad0dfa051 86 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 87
spastor 0:fcaad0dfa051 88 rmemcpy(panid_u8, (const uint8_t *) &panid, sizeof panid_u8);
spastor 0:fcaad0dfa051 89
spastor 0:fcaad0dfa051 90 cmdresp = set_param("ID", panid_u8, sizeof panid_u8);
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 XBeeZB::get_operating_panid(uint64_t * const opanid)
spastor 0:fcaad0dfa051 98 {
spastor 0:fcaad0dfa051 99 if (opanid == NULL) {
spastor 0:fcaad0dfa051 100 return Failure;
spastor 0:fcaad0dfa051 101 }
spastor 0:fcaad0dfa051 102 uint8_t opanid_u8[8];
spastor 0:fcaad0dfa051 103 uint16_t len = sizeof opanid_u8;
spastor 0:fcaad0dfa051 104 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 105
spastor 0:fcaad0dfa051 106 cmdresp = get_param("OP", opanid_u8, &len);
spastor 0:fcaad0dfa051 107 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 108 return Failure;
spastor 0:fcaad0dfa051 109 }
spastor 0:fcaad0dfa051 110 if (len != sizeof opanid_u8) {
spastor 0:fcaad0dfa051 111 digi_log(LogLevelError, "XBeeZB::get_operating_panid: Read %d bytes instead of %d for OP", len, sizeof opanid_u8);
spastor 0:fcaad0dfa051 112 return Failure;
spastor 0:fcaad0dfa051 113 }
spastor 0:fcaad0dfa051 114 rmemcpy((uint8_t *)opanid, opanid_u8, len);
spastor 0:fcaad0dfa051 115 return Success;
spastor 0:fcaad0dfa051 116 }
spastor 0:fcaad0dfa051 117
spastor 0:fcaad0dfa051 118 RadioStatus XBeeZB::get_configured_panid(uint64_t * const panid)
spastor 0:fcaad0dfa051 119 {
spastor 0:fcaad0dfa051 120 if (panid == NULL) {
spastor 0:fcaad0dfa051 121 return Failure;
spastor 0:fcaad0dfa051 122 }
spastor 0:fcaad0dfa051 123 uint8_t panid_u8[8];
spastor 0:fcaad0dfa051 124 uint16_t len = sizeof panid_u8;
spastor 0:fcaad0dfa051 125 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 126
spastor 0:fcaad0dfa051 127 cmdresp = get_param("ID", panid_u8, &len);
spastor 0:fcaad0dfa051 128 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 129 return Failure;
spastor 0:fcaad0dfa051 130 }
spastor 0:fcaad0dfa051 131 if (len != sizeof panid_u8) {
spastor 0:fcaad0dfa051 132 digi_log(LogLevelError, "XBeeZB::get_configured_panid: Read %d bytes instead of %d for ID", len, sizeof panid_u8);
spastor 0:fcaad0dfa051 133 return Failure;
spastor 0:fcaad0dfa051 134 }
spastor 0:fcaad0dfa051 135 rmemcpy((uint8_t *)panid, panid_u8, len);
spastor 0:fcaad0dfa051 136 return Success;
spastor 0:fcaad0dfa051 137 }
spastor 0:fcaad0dfa051 138
spastor 0:fcaad0dfa051 139 RadioStatus XBeeZB::set_panid(const RemoteXBee& remote, uint64_t panid)
spastor 0:fcaad0dfa051 140 {
spastor 0:fcaad0dfa051 141 uint8_t panid_u8[8];
spastor 0:fcaad0dfa051 142 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 143
spastor 0:fcaad0dfa051 144 rmemcpy(panid_u8, (const uint8_t *) &panid, sizeof panid_u8);
spastor 0:fcaad0dfa051 145
spastor 0:fcaad0dfa051 146 cmdresp = set_param(remote, "ID", panid_u8, sizeof panid_u8);
spastor 0:fcaad0dfa051 147 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 148 return Failure;
spastor 0:fcaad0dfa051 149 }
spastor 0:fcaad0dfa051 150 return Success;
spastor 0:fcaad0dfa051 151 }
spastor 0:fcaad0dfa051 152
spastor 0:fcaad0dfa051 153 RadioStatus XBeeZB::get_operating_panid(const RemoteXBee& remote, uint64_t * const opanid)
spastor 0:fcaad0dfa051 154 {
spastor 0:fcaad0dfa051 155 if (opanid == NULL) {
spastor 0:fcaad0dfa051 156 return Failure;
spastor 0:fcaad0dfa051 157 }
spastor 0:fcaad0dfa051 158 uint8_t opanid_u8[8];
spastor 0:fcaad0dfa051 159 uint16_t len = sizeof opanid_u8;
spastor 0:fcaad0dfa051 160 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 161
spastor 0:fcaad0dfa051 162 cmdresp = get_param(remote, "OP", opanid_u8, &len);
spastor 0:fcaad0dfa051 163 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 164 return Failure;
spastor 0:fcaad0dfa051 165 }
spastor 0:fcaad0dfa051 166 if (len != sizeof opanid_u8) {
spastor 0:fcaad0dfa051 167 digi_log(LogLevelError, "XBeeZB::get_operating_panid: Read %d bytes instead of %d for OP", len, sizeof opanid_u8);
spastor 0:fcaad0dfa051 168 return Failure;
spastor 0:fcaad0dfa051 169 }
spastor 0:fcaad0dfa051 170 rmemcpy((uint8_t *)opanid, opanid_u8, len);
spastor 0:fcaad0dfa051 171 return Success;
spastor 0:fcaad0dfa051 172 }
spastor 0:fcaad0dfa051 173
spastor 0:fcaad0dfa051 174 RadioStatus XBeeZB::get_configured_panid(const RemoteXBee& remote, uint64_t * const panid)
spastor 0:fcaad0dfa051 175 {
spastor 0:fcaad0dfa051 176 if (panid == NULL) {
spastor 0:fcaad0dfa051 177 return Failure;
spastor 0:fcaad0dfa051 178 }
spastor 0:fcaad0dfa051 179 uint8_t panid_u8[8];
spastor 0:fcaad0dfa051 180 uint16_t len = sizeof panid_u8;
spastor 0:fcaad0dfa051 181 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 182
spastor 0:fcaad0dfa051 183 cmdresp = get_param(remote, "ID", panid_u8, &len);
spastor 0:fcaad0dfa051 184 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 185 return Failure;
spastor 0:fcaad0dfa051 186 }
spastor 0:fcaad0dfa051 187 if (len != sizeof panid_u8) {
spastor 0:fcaad0dfa051 188 digi_log(LogLevelError, "XBeeZB::get_configured_panid: Read %d bytes instead of %d for ID", len, sizeof panid_u8);
spastor 0:fcaad0dfa051 189 return Failure;
spastor 0:fcaad0dfa051 190 }
spastor 0:fcaad0dfa051 191 rmemcpy((uint8_t *)panid, panid_u8, len);
spastor 0:fcaad0dfa051 192 return Success;
spastor 0:fcaad0dfa051 193 }
spastor 0:fcaad0dfa051 194
spastor 0:fcaad0dfa051 195 RadioStatus XBeeZB::check_for_coordinator_at_start(bool enable)
spastor 0:fcaad0dfa051 196 {
spastor 0:fcaad0dfa051 197 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 198
spastor 0:fcaad0dfa051 199 cmdresp = set_param("JV", (uint8_t)enable);
spastor 0:fcaad0dfa051 200 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 201 return Failure;
spastor 0:fcaad0dfa051 202 return Success;
spastor 0:fcaad0dfa051 203 }
spastor 0:fcaad0dfa051 204
spastor 0:fcaad0dfa051 205 RadioStatus XBeeZB::enable_network_encryption(bool enable)
spastor 0:fcaad0dfa051 206 {
spastor 0:fcaad0dfa051 207 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 208
spastor 0:fcaad0dfa051 209 cmdresp = set_param("EE", enable);
spastor 0:fcaad0dfa051 210 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 211 return Failure;
spastor 0:fcaad0dfa051 212 return Success;
spastor 0:fcaad0dfa051 213 }
spastor 0:fcaad0dfa051 214
spastor 0:fcaad0dfa051 215 RadioStatus XBeeZB::set_encryption_key(const uint8_t * const key, const uint16_t length)
spastor 0:fcaad0dfa051 216 {
spastor 0:fcaad0dfa051 217 if (key == NULL || length == 0) {
spastor 0:fcaad0dfa051 218 return Failure;
spastor 0:fcaad0dfa051 219 }
spastor 0:fcaad0dfa051 220 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 221
spastor 0:fcaad0dfa051 222 cmdresp = set_param("NK", key, length);
spastor 0:fcaad0dfa051 223 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 224 return Failure;
spastor 0:fcaad0dfa051 225 return Success;
spastor 0:fcaad0dfa051 226 }
spastor 0:fcaad0dfa051 227
spastor 0:fcaad0dfa051 228 RadioStatus XBeeZB::set_tc_link_key(const uint8_t * const key, const uint16_t length)
spastor 0:fcaad0dfa051 229 {
spastor 0:fcaad0dfa051 230 if (key == NULL || length == 0) {
spastor 0:fcaad0dfa051 231 return Failure;
spastor 0:fcaad0dfa051 232 }
spastor 0:fcaad0dfa051 233 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 234
spastor 0:fcaad0dfa051 235 cmdresp = set_param("KY", key, length);
spastor 0:fcaad0dfa051 236 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 237 return Failure;
spastor 0:fcaad0dfa051 238 return Success;
spastor 0:fcaad0dfa051 239 }
spastor 0:fcaad0dfa051 240
spastor 0:fcaad0dfa051 241 RadioStatus XBeeZB::set_encryption_options(const uint8_t options)
spastor 0:fcaad0dfa051 242 {
spastor 0:fcaad0dfa051 243 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 244
spastor 0:fcaad0dfa051 245 cmdresp = set_param("EO", options);
spastor 0:fcaad0dfa051 246 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 247 return Failure;
spastor 0:fcaad0dfa051 248 return Success;
spastor 0:fcaad0dfa051 249 }
spastor 0:fcaad0dfa051 250
spastor 0:fcaad0dfa051 251 void XBeeZB::radio_status_update(AtCmdFrame::ModemStatus modem_status)
spastor 0:fcaad0dfa051 252 {
spastor 0:fcaad0dfa051 253 /* Update the radio status variables */
spastor 0:fcaad0dfa051 254 if (modem_status == AtCmdFrame::HwReset) {
spastor 0:fcaad0dfa051 255 _hw_reset_cnt++;
spastor 0:fcaad0dfa051 256 _joined_network = false;
spastor 0:fcaad0dfa051 257 }
spastor 0:fcaad0dfa051 258 else if (modem_status == AtCmdFrame::WdReset) {
spastor 0:fcaad0dfa051 259 _wd_reset_cnt++;
spastor 0:fcaad0dfa051 260 _joined_network = false;
spastor 0:fcaad0dfa051 261 }
spastor 0:fcaad0dfa051 262 else if (modem_status == AtCmdFrame::JoinedNW)
spastor 0:fcaad0dfa051 263 _joined_network = true;
spastor 0:fcaad0dfa051 264 else if (modem_status == AtCmdFrame::Disassociated)
spastor 0:fcaad0dfa051 265 _joined_network = false;
spastor 0:fcaad0dfa051 266 else if (modem_status == AtCmdFrame::VccExceeded)
spastor 0:fcaad0dfa051 267 _vcc_exceeded_cnt++;
spastor 0:fcaad0dfa051 268
spastor 0:fcaad0dfa051 269 _modem_status = modem_status;
spastor 0:fcaad0dfa051 270
spastor 0:fcaad0dfa051 271 digi_log(LogLevelDebug, "\r\nUpdating radio status: %02x\r\n", modem_status);
spastor 0:fcaad0dfa051 272 }
spastor 0:fcaad0dfa051 273
spastor 0:fcaad0dfa051 274 TxStatus XBeeZB::send_data(const RemoteXBee& remote, const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 275 {
spastor 0:fcaad0dfa051 276 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 277 return TxStatusInvalidAddr;
spastor 0:fcaad0dfa051 278
spastor 0:fcaad0dfa051 279 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 280 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 281
spastor 0:fcaad0dfa051 282 TxFrameZB frame = TxFrameZB(remote64, remote16, _broadcast_radious,
spastor 0:fcaad0dfa051 283 _tx_options, data, len);
spastor 0:fcaad0dfa051 284 return send_data(&frame);
spastor 0:fcaad0dfa051 285 }
spastor 0:fcaad0dfa051 286
spastor 0:fcaad0dfa051 287 TxStatus XBeeZB::send_data(uint64_t remote64, const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 288 {
spastor 0:fcaad0dfa051 289 TxFrameZB frame = TxFrameZB(remote64, ADDR16_UNKNOWN, _broadcast_radious,
spastor 0:fcaad0dfa051 290 _tx_options, data, len);
spastor 0:fcaad0dfa051 291 return send_data(&frame);
spastor 0:fcaad0dfa051 292 }
spastor 0:fcaad0dfa051 293
spastor 0:fcaad0dfa051 294 TxStatus XBeeZB::send_data(uint64_t remote64, uint16_t remote16,
spastor 0:fcaad0dfa051 295 const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 296 {
spastor 0:fcaad0dfa051 297 TxFrameZB frame = TxFrameZB(remote64, remote16, _broadcast_radious,
spastor 0:fcaad0dfa051 298 _tx_options, data, len);
spastor 0:fcaad0dfa051 299 return send_data(&frame);
spastor 0:fcaad0dfa051 300 }
spastor 0:fcaad0dfa051 301
spastor 0:fcaad0dfa051 302 TxStatus XBeeZB::send_data(const RemoteXBee& remote, uint8_t source_ep,
spastor 0:fcaad0dfa051 303 uint8_t dest_ep, uint16_t cluster_id, uint16_t profile_id,
spastor 0:fcaad0dfa051 304 const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 305 {
spastor 0:fcaad0dfa051 306 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 307 return TxStatusInvalidAddr;
spastor 0:fcaad0dfa051 308
spastor 0:fcaad0dfa051 309 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 310 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 311
spastor 0:fcaad0dfa051 312 TxFrameZB frame = TxFrameZB(remote64, remote16, source_ep, dest_ep,
spastor 0:fcaad0dfa051 313 cluster_id, profile_id, _broadcast_radious,
spastor 0:fcaad0dfa051 314 _tx_options, data, len);
spastor 0:fcaad0dfa051 315 return send_data(&frame);
spastor 0:fcaad0dfa051 316 }
spastor 0:fcaad0dfa051 317
spastor 0:fcaad0dfa051 318 TxStatus XBeeZB::send_data(uint64_t remote64, uint16_t remote16, uint8_t source_ep,
spastor 0:fcaad0dfa051 319 uint8_t dest_ep, uint16_t cluster_id, uint16_t profile_id,
spastor 0:fcaad0dfa051 320 const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 321 {
spastor 0:fcaad0dfa051 322 TxFrameZB frame = TxFrameZB(remote64, remote16, source_ep, dest_ep,
spastor 0:fcaad0dfa051 323 cluster_id, profile_id, _broadcast_radious,
spastor 0:fcaad0dfa051 324 _tx_options, data, len);
spastor 0:fcaad0dfa051 325 return send_data(&frame);
spastor 0:fcaad0dfa051 326 }
spastor 0:fcaad0dfa051 327
spastor 0:fcaad0dfa051 328 TxStatus XBeeZB::send_data_to_coordinator(const uint8_t *const data, uint16_t len)
spastor 0:fcaad0dfa051 329 {
spastor 0:fcaad0dfa051 330 const uint64_t remaddr = ADDR64_COORDINATOR;
spastor 0:fcaad0dfa051 331
spastor 0:fcaad0dfa051 332 TxFrameZB frame = TxFrameZB(remaddr, ADDR16_UNKNOWN, _broadcast_radious,
spastor 0:fcaad0dfa051 333 _tx_options, data, len);
spastor 0:fcaad0dfa051 334 return send_data(&frame);
spastor 0:fcaad0dfa051 335 }
spastor 0:fcaad0dfa051 336
spastor 0:fcaad0dfa051 337 NetworkRole XBeeZB::get_network_role()
spastor 0:fcaad0dfa051 338 {
spastor 0:fcaad0dfa051 339 return _nw_role;
spastor 0:fcaad0dfa051 340 }
spastor 0:fcaad0dfa051 341
spastor 0:fcaad0dfa051 342 bool XBeeZB::is_joined()
spastor 0:fcaad0dfa051 343 {
spastor 0:fcaad0dfa051 344 return _joined_network;
spastor 0:fcaad0dfa051 345 }
spastor 0:fcaad0dfa051 346
spastor 0:fcaad0dfa051 347 void XBeeZB::register_node_discovery_cb(node_discovery_zb_cb_t function)
spastor 0:fcaad0dfa051 348 {
spastor 0:fcaad0dfa051 349 if (_nd_handler == NULL) {
spastor 0:fcaad0dfa051 350 _nd_handler = new FH_NodeDiscoveryZB();
spastor 0:fcaad0dfa051 351 register_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 352 }
spastor 0:fcaad0dfa051 353 _nd_handler->register_node_discovery_cb(function);
spastor 0:fcaad0dfa051 354 }
spastor 0:fcaad0dfa051 355
spastor 0:fcaad0dfa051 356 void XBeeZB::unregister_node_discovery_cb()
spastor 0:fcaad0dfa051 357 {
spastor 0:fcaad0dfa051 358 if (_nd_handler != NULL) {
spastor 0:fcaad0dfa051 359 _nd_handler->unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 360 unregister_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 361 delete _nd_handler;
spastor 0:fcaad0dfa051 362 _nd_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 363 }
spastor 0:fcaad0dfa051 364 }
spastor 0:fcaad0dfa051 365
spastor 0:fcaad0dfa051 366 void XBeeZB::register_receive_cb(receive_zb_cb_t function)
spastor 0:fcaad0dfa051 367 {
spastor 0:fcaad0dfa051 368 if (_rx_pkt_handler == NULL) {
spastor 0:fcaad0dfa051 369 _rx_pkt_handler = new FH_RxPacketZB();
spastor 0:fcaad0dfa051 370 register_frame_handler(_rx_pkt_handler);
spastor 0:fcaad0dfa051 371 }
spastor 0:fcaad0dfa051 372 _rx_pkt_handler->register_receive_cb(function);
spastor 0:fcaad0dfa051 373 }
spastor 0:fcaad0dfa051 374
spastor 0:fcaad0dfa051 375 void XBeeZB::unregister_receive_cb()
spastor 0:fcaad0dfa051 376 {
spastor 0:fcaad0dfa051 377 if (_rx_pkt_handler != NULL) {
spastor 0:fcaad0dfa051 378 _rx_pkt_handler->unregister_receive_cb();
spastor 0:fcaad0dfa051 379 unregister_frame_handler(_rx_pkt_handler);
spastor 0:fcaad0dfa051 380 delete _rx_pkt_handler;
spastor 0:fcaad0dfa051 381 _rx_pkt_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 382 }
spastor 0:fcaad0dfa051 383 }
spastor 0:fcaad0dfa051 384
spastor 0:fcaad0dfa051 385 void XBeeZB::register_io_sample_cb(io_data_cb_zb_t function)
spastor 0:fcaad0dfa051 386 {
spastor 0:fcaad0dfa051 387 if (_io_data_handler == NULL) {
spastor 0:fcaad0dfa051 388 _io_data_handler = new FH_IoDataSampeZB();
spastor 0:fcaad0dfa051 389 register_frame_handler(_io_data_handler);
spastor 0:fcaad0dfa051 390 }
spastor 0:fcaad0dfa051 391 _io_data_handler->register_io_data_cb(function);
spastor 0:fcaad0dfa051 392 }
spastor 0:fcaad0dfa051 393
spastor 0:fcaad0dfa051 394 void XBeeZB::unregister_io_sample_cb()
spastor 0:fcaad0dfa051 395 {
spastor 0:fcaad0dfa051 396 if (_io_data_handler != NULL) {
spastor 0:fcaad0dfa051 397 _io_data_handler->unregister_io_data_cb();
spastor 0:fcaad0dfa051 398 unregister_frame_handler(_io_data_handler);
spastor 0:fcaad0dfa051 399 delete _io_data_handler;
spastor 0:fcaad0dfa051 400 _io_data_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 401 }
spastor 0:fcaad0dfa051 402 }
spastor 0:fcaad0dfa051 403
hbujanda 1:794d1d3e4a08 404 XBeeZB * XBeeZB::get_device_by_id(const char * const node_id)
hbujanda 1:794d1d3e4a08 405 {
hbujanda 1:794d1d3e4a08 406 return NULL;
hbujanda 1:794d1d3e4a08 407 }
hbujanda 1:794d1d3e4a08 408
hbujanda 1:794d1d3e4a08 409 RadioStatus XBeeZB::get_device_by_id(const char * const node_id, uint64_t * const dev_addr)
hbujanda 1:794d1d3e4a08 410 {
hbujanda 1:794d1d3e4a08 411 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 1:794d1d3e4a08 412 uint32_t dh, dl;
hbujanda 1:794d1d3e4a08 413
hbujanda 1:794d1d3e4a08 414 if (strlen(node_id) > MAX_NI_PARAM_LEN)
hbujanda 1:794d1d3e4a08 415 return Failure;
hbujanda 1:794d1d3e4a08 416
hbujanda 1:794d1d3e4a08 417 cmdresp = set_param("DN", (const uint8_t *)node_id, strlen(node_id));
hbujanda 1:794d1d3e4a08 418 if (cmdresp != AtCmdFrame::AtCmdRespOk)
hbujanda 1:794d1d3e4a08 419 return Failure;
hbujanda 1:794d1d3e4a08 420
hbujanda 1:794d1d3e4a08 421 /* Read the address of the remote device from the DH, DL parameters */
hbujanda 1:794d1d3e4a08 422 cmdresp = get_param("DH", &dh);
hbujanda 1:794d1d3e4a08 423 if (cmdresp != AtCmdFrame::AtCmdRespOk)
hbujanda 1:794d1d3e4a08 424 return Failure;
hbujanda 1:794d1d3e4a08 425
hbujanda 1:794d1d3e4a08 426 cmdresp = get_param("DL", &dl);
hbujanda 1:794d1d3e4a08 427 if (cmdresp != AtCmdFrame::AtCmdRespOk)
hbujanda 1:794d1d3e4a08 428 return Failure;
hbujanda 1:794d1d3e4a08 429
hbujanda 1:794d1d3e4a08 430 *dev_addr = UINT64(dh, dl);
hbujanda 1:794d1d3e4a08 431
hbujanda 1:794d1d3e4a08 432 return Success;
hbujanda 1:794d1d3e4a08 433 }
hbujanda 1:794d1d3e4a08 434
spastor 0:fcaad0dfa051 435 AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint32_t * const data)
spastor 0:fcaad0dfa051 436 {
spastor 0:fcaad0dfa051 437 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 438 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 439
spastor 0:fcaad0dfa051 440 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 441 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 442 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 443 AtCmdFrame::AtCmdResp atCmdResponse;
spastor 0:fcaad0dfa051 444
spastor 0:fcaad0dfa051 445 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param);
spastor 0:fcaad0dfa051 446 atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len, RadioRemote);
spastor 0:fcaad0dfa051 447
spastor 0:fcaad0dfa051 448 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data)
spastor 0:fcaad0dfa051 449 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 0:fcaad0dfa051 450
spastor 0:fcaad0dfa051 451 return atCmdResponse;
spastor 0:fcaad0dfa051 452 }
spastor 0:fcaad0dfa051 453
spastor 0:fcaad0dfa051 454 AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, uint32_t data)
spastor 0:fcaad0dfa051 455 {
spastor 0:fcaad0dfa051 456 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 457 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 458
spastor 0:fcaad0dfa051 459 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 460 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 461
spastor 0:fcaad0dfa051 462 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param, data);
spastor 0:fcaad0dfa051 463 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 464 }
spastor 0:fcaad0dfa051 465
spastor 0:fcaad0dfa051 466 AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, const uint8_t * data, uint16_t len)
spastor 0:fcaad0dfa051 467 {
spastor 0:fcaad0dfa051 468 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 469 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 470
spastor 0:fcaad0dfa051 471 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 472 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 473
spastor 0:fcaad0dfa051 474 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param, data, len);
spastor 0:fcaad0dfa051 475 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 476 }
spastor 0:fcaad0dfa051 477
spastor 0:fcaad0dfa051 478 AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint8_t * const data, uint16_t * const len)
spastor 0:fcaad0dfa051 479 {
spastor 0:fcaad0dfa051 480
spastor 0:fcaad0dfa051 481 if (!remote.is_valid_addr64b())
spastor 0:fcaad0dfa051 482 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 0:fcaad0dfa051 483
spastor 0:fcaad0dfa051 484 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 485 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 486
spastor 0:fcaad0dfa051 487 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param);
spastor 0:fcaad0dfa051 488 return send_at_cmd(&cmd_frame, data, len, RadioRemote, false);
spastor 0:fcaad0dfa051 489 }
spastor 0:fcaad0dfa051 490
spastor 0:fcaad0dfa051 491 static void get_dio_cmd(XBeeZB::IoLine line, char * const iocmd)
spastor 0:fcaad0dfa051 492 {
spastor 0:fcaad0dfa051 493 if (line >= XBeeZB::DIO10) {
spastor 0:fcaad0dfa051 494 iocmd[0] = 'P';
spastor 0:fcaad0dfa051 495 iocmd[1] = '0' + line - XBeeZB::DIO10;
spastor 0:fcaad0dfa051 496 } else {
spastor 0:fcaad0dfa051 497 iocmd[0] = 'D';
spastor 0:fcaad0dfa051 498 iocmd[1] = '0' + line;
spastor 0:fcaad0dfa051 499 }
spastor 0:fcaad0dfa051 500 iocmd[2] = '\0';
spastor 0:fcaad0dfa051 501 }
spastor 0:fcaad0dfa051 502
spastor 0:fcaad0dfa051 503 RadioStatus XBeeZB::set_pin_config(const RemoteXBee& remote, IoLine line, IoMode mode)
spastor 0:fcaad0dfa051 504 {
spastor 0:fcaad0dfa051 505 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 506 char iocmd[3];
spastor 0:fcaad0dfa051 507
spastor 0:fcaad0dfa051 508 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 509
spastor 0:fcaad0dfa051 510 cmdresp = set_param(remote, iocmd, (uint8_t)mode);
spastor 0:fcaad0dfa051 511 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 512 digi_log(LogLevelError, "set_pin_config: set_param returned %d\r\n", cmdresp);
spastor 0:fcaad0dfa051 513 return Failure;
spastor 0:fcaad0dfa051 514 }
spastor 0:fcaad0dfa051 515
spastor 0:fcaad0dfa051 516 return Success;
spastor 0:fcaad0dfa051 517 }
spastor 0:fcaad0dfa051 518
spastor 0:fcaad0dfa051 519 RadioStatus XBeeZB::get_pin_config(const RemoteXBee& remote, IoLine line, IoMode * const mode)
spastor 0:fcaad0dfa051 520 {
spastor 0:fcaad0dfa051 521 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 522 char iocmd[3];
spastor 0:fcaad0dfa051 523
spastor 0:fcaad0dfa051 524 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 525
spastor 0:fcaad0dfa051 526 uint32_t var32;
spastor 0:fcaad0dfa051 527 cmdresp = get_param(remote, iocmd, &var32);
spastor 0:fcaad0dfa051 528 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 529 return Failure;
spastor 0:fcaad0dfa051 530 }
spastor 0:fcaad0dfa051 531 *mode = (IoMode)var32;
spastor 0:fcaad0dfa051 532
spastor 0:fcaad0dfa051 533 return Success;
spastor 0:fcaad0dfa051 534 }
spastor 0:fcaad0dfa051 535
spastor 0:fcaad0dfa051 536 RadioStatus XBeeZB::set_dio(const RemoteXBee& remote, IoLine line, DioVal val)
spastor 0:fcaad0dfa051 537 {
spastor 0:fcaad0dfa051 538 if (val == Low)
spastor 0:fcaad0dfa051 539 return set_pin_config(remote, line, DigitalOutLow);
spastor 0:fcaad0dfa051 540 else
spastor 0:fcaad0dfa051 541 return set_pin_config(remote, line, DigitalOutHigh);
spastor 0:fcaad0dfa051 542 }
spastor 0:fcaad0dfa051 543
spastor 0:fcaad0dfa051 544 RadioStatus XBeeZB::get_dio(const RemoteXBee& remote, IoLine line, DioVal * const val)
spastor 0:fcaad0dfa051 545 {
spastor 0:fcaad0dfa051 546 uint8_t io_sample[MAX_IO_SAMPLE_BUF_LEN];
spastor 0:fcaad0dfa051 547 uint16_t len;
spastor 0:fcaad0dfa051 548
spastor 0:fcaad0dfa051 549 RadioStatus resp = get_iosample(remote, io_sample, &len);
spastor 0:fcaad0dfa051 550 if (resp != Success)
spastor 0:fcaad0dfa051 551 return resp;
spastor 0:fcaad0dfa051 552
spastor 0:fcaad0dfa051 553 IOSampleZB ioSample = IOSampleZB(io_sample, len);
spastor 0:fcaad0dfa051 554 return ioSample.get_dio(line, val);
spastor 0:fcaad0dfa051 555 }
spastor 0:fcaad0dfa051 556
spastor 0:fcaad0dfa051 557 RadioStatus XBeeZB::get_adc(const RemoteXBee& remote, IoLine line, uint16_t * const val)
spastor 0:fcaad0dfa051 558 {
spastor 0:fcaad0dfa051 559 uint8_t io_sample[MAX_IO_SAMPLE_BUF_LEN];
spastor 0:fcaad0dfa051 560 uint16_t len;
spastor 0:fcaad0dfa051 561
spastor 0:fcaad0dfa051 562 switch (line) {
spastor 0:fcaad0dfa051 563 case SUPPLY_VOLTAGE:
spastor 0:fcaad0dfa051 564 case DIO3_AD3:
spastor 0:fcaad0dfa051 565 case DIO2_AD2:
spastor 0:fcaad0dfa051 566 case DIO1_AD1:
spastor 0:fcaad0dfa051 567 case DIO0_AD0:
spastor 0:fcaad0dfa051 568 break;
spastor 0:fcaad0dfa051 569 default:
spastor 0:fcaad0dfa051 570 digi_log(LogLevelError, "get_adc: Pin %d not supported as ADC\r\n", line);
spastor 0:fcaad0dfa051 571 return Failure;
spastor 0:fcaad0dfa051 572 }
spastor 0:fcaad0dfa051 573
spastor 0:fcaad0dfa051 574 RadioStatus resp = get_iosample(remote, io_sample, &len);
spastor 0:fcaad0dfa051 575 if (resp != Success)
spastor 0:fcaad0dfa051 576 return resp;
spastor 0:fcaad0dfa051 577
spastor 0:fcaad0dfa051 578 IOSampleZB ioSample = IOSampleZB(io_sample, len);
spastor 0:fcaad0dfa051 579 return ioSample.get_adc(line, val);
spastor 0:fcaad0dfa051 580 }
spastor 0:fcaad0dfa051 581
spastor 0:fcaad0dfa051 582 static uint16_t get_dio_pr_mask(XBeeZB::IoLine line)
spastor 0:fcaad0dfa051 583 {
spastor 0:fcaad0dfa051 584 switch (line) {
spastor 0:fcaad0dfa051 585 case XBeeZB::DIO4:
spastor 0:fcaad0dfa051 586 return (1 << 0);
spastor 0:fcaad0dfa051 587 case XBeeZB::DIO3_AD3:
spastor 0:fcaad0dfa051 588 return (1 << 1);
spastor 0:fcaad0dfa051 589 case XBeeZB::DIO2_AD2:
spastor 0:fcaad0dfa051 590 return (1 << 2);
spastor 0:fcaad0dfa051 591 case XBeeZB::DIO1_AD1:
spastor 0:fcaad0dfa051 592 return (1 << 3);
spastor 0:fcaad0dfa051 593 case XBeeZB::DIO0_AD0:
spastor 0:fcaad0dfa051 594 return (1 << 4);
spastor 0:fcaad0dfa051 595 case XBeeZB::DIO6:
spastor 0:fcaad0dfa051 596 return (1 << 5);
spastor 0:fcaad0dfa051 597 case XBeeZB::DIO5:
spastor 0:fcaad0dfa051 598 return (1 << 8);
spastor 0:fcaad0dfa051 599 case XBeeZB::DIO12:
spastor 0:fcaad0dfa051 600 return (1 << 10);
spastor 0:fcaad0dfa051 601 case XBeeZB::DIO10:
spastor 0:fcaad0dfa051 602 return (1 << 11);
spastor 0:fcaad0dfa051 603 case XBeeZB::DIO11:
spastor 0:fcaad0dfa051 604 return (1 << 12);
spastor 0:fcaad0dfa051 605 case XBeeZB::DIO7:
spastor 0:fcaad0dfa051 606 return (1 << 13);
spastor 0:fcaad0dfa051 607 default:
spastor 0:fcaad0dfa051 608 return 0;
spastor 0:fcaad0dfa051 609 }
spastor 0:fcaad0dfa051 610 }
spastor 0:fcaad0dfa051 611
spastor 0:fcaad0dfa051 612 RadioStatus XBeeZB::set_pin_pull_up(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 613 {
spastor 0:fcaad0dfa051 614 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 615 uint32_t var32;
spastor 0:fcaad0dfa051 616 uint16_t pr;
spastor 0:fcaad0dfa051 617
spastor 0:fcaad0dfa051 618 cmdresp = get_param(remote, "PR", &var32);
spastor 0:fcaad0dfa051 619 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 620 return Failure;
spastor 0:fcaad0dfa051 621 }
spastor 0:fcaad0dfa051 622 pr = var32;
spastor 0:fcaad0dfa051 623
spastor 0:fcaad0dfa051 624 const uint16_t dio_mask = get_dio_pr_mask(line);
spastor 0:fcaad0dfa051 625 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 626 digi_log(LogLevelError, "XBeeZB::set_pin_pull_up: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 627 return Failure;
spastor 0:fcaad0dfa051 628 }
spastor 0:fcaad0dfa051 629
spastor 0:fcaad0dfa051 630 if (enable) {
spastor 0:fcaad0dfa051 631 pr |= dio_mask;
spastor 0:fcaad0dfa051 632 } else {
spastor 0:fcaad0dfa051 633 pr &= ~dio_mask;
spastor 0:fcaad0dfa051 634 }
spastor 0:fcaad0dfa051 635
spastor 0:fcaad0dfa051 636 cmdresp = set_param(remote, "PR", pr);
spastor 0:fcaad0dfa051 637 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 638 return Failure;
spastor 0:fcaad0dfa051 639
spastor 0:fcaad0dfa051 640 return Success;
spastor 0:fcaad0dfa051 641 }
spastor 0:fcaad0dfa051 642
spastor 0:fcaad0dfa051 643 static uint16_t get_dio_ic_mask(XBeeZB::IoLine line)
spastor 0:fcaad0dfa051 644 {
spastor 0:fcaad0dfa051 645 if (line < XBeeZB::DIO12) {
spastor 0:fcaad0dfa051 646 return (1 << line);
spastor 0:fcaad0dfa051 647 }
spastor 0:fcaad0dfa051 648 return 0;
spastor 0:fcaad0dfa051 649 }
spastor 0:fcaad0dfa051 650
spastor 0:fcaad0dfa051 651 RadioStatus XBeeZB::enable_dio_change_detection(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 652 {
spastor 0:fcaad0dfa051 653 if (line > DIO11) {
spastor 0:fcaad0dfa051 654 digi_log(LogLevelError, "XBeeZB::enable_dio_change_detection: pin not supported (%d)\r\n", line);
spastor 0:fcaad0dfa051 655 return Failure;
spastor 0:fcaad0dfa051 656 }
spastor 0:fcaad0dfa051 657
spastor 0:fcaad0dfa051 658 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 659 uint32_t var32;
spastor 0:fcaad0dfa051 660 uint16_t ic;
spastor 0:fcaad0dfa051 661
spastor 0:fcaad0dfa051 662 cmdresp = get_param(remote, "IC", &var32);
spastor 0:fcaad0dfa051 663 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 664 return Failure;
spastor 0:fcaad0dfa051 665 }
spastor 0:fcaad0dfa051 666 ic = var32;
spastor 0:fcaad0dfa051 667
spastor 0:fcaad0dfa051 668 const uint16_t dio_mask = get_dio_ic_mask(line);
spastor 0:fcaad0dfa051 669 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 670 digi_log(LogLevelError, "XBeeZB::enable_dio_change_detection: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 671 return Failure;
spastor 0:fcaad0dfa051 672 }
spastor 0:fcaad0dfa051 673
spastor 0:fcaad0dfa051 674 if (enable) {
spastor 0:fcaad0dfa051 675 ic |= dio_mask;
spastor 0:fcaad0dfa051 676 } else {
spastor 0:fcaad0dfa051 677 ic &= ~dio_mask;
spastor 0:fcaad0dfa051 678 }
spastor 0:fcaad0dfa051 679
spastor 0:fcaad0dfa051 680 cmdresp = set_param(remote, "IC", ic);
spastor 0:fcaad0dfa051 681 if (cmdresp != AtCmdFrame::AtCmdRespOk)
spastor 0:fcaad0dfa051 682 return Failure;
spastor 0:fcaad0dfa051 683
spastor 0:fcaad0dfa051 684 return Success;
spastor 0:fcaad0dfa051 685 }