Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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