XBeeLib modificado para projeto do VSSS

Dependencies:   DigiLogger

Fork of XBeeLib by Digi International Inc.

Committer:
hbujanda
Date:
Fri Jul 29 12:10:38 2016 +0200
Revision:
8:b5f4a0e92249
Parent:
6:06522f3a6642
Child:
9:780db84ce891
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 4:629712865107 19 #define BROADCAST_RADIUS_USE_NH 0x00
spastor 4:629712865107 20
spastor 0:fcaad0dfa051 21 /* Class constructor */
spastor 0:fcaad0dfa051 22 XBeeZB::XBeeZB(PinName tx, PinName rx, PinName reset, PinName rts, PinName cts, int baud) :
spastor 4:629712865107 23 XBee(tx, rx, reset, rts, cts, baud), _nd_handler(NULL), _rx_pkt_handler(NULL), _io_data_handler(NULL)
spastor 0:fcaad0dfa051 24 {
spastor 0:fcaad0dfa051 25 }
spastor 0:fcaad0dfa051 26
spastor 0:fcaad0dfa051 27 RadioStatus XBeeZB::init()
spastor 0:fcaad0dfa051 28 {
spastor 0:fcaad0dfa051 29 RadioStatus retval = XBee::init();
hbujanda 6:06522f3a6642 30 uint16_t addr16;
hbujanda 6:06522f3a6642 31 RadioStatus error = get_network_address(&addr16);
hbujanda 6:06522f3a6642 32 if (error == Success) {
hbujanda 6:06522f3a6642 33 digi_log(LogLevelInfo, "ADDR16: %04x\r\n", addr16);
hbujanda 6:06522f3a6642 34 } else {
hbujanda 6:06522f3a6642 35 digi_log(LogLevelInfo, "ADDR16: UNKNOWN\r\n");
hbujanda 6:06522f3a6642 36 }
spastor 0:fcaad0dfa051 37
spastor 0:fcaad0dfa051 38 const RadioProtocol radioProtocol = get_radio_protocol();
spastor 0:fcaad0dfa051 39 if (radioProtocol != ZigBee) {
spastor 0:fcaad0dfa051 40 digi_log(LogLevelError, "Radio protocol does not match, needed a %d got a %d\r\n", ZigBee, radioProtocol);
spastor 0:fcaad0dfa051 41 retval = Failure;
spastor 0:fcaad0dfa051 42 }
spastor 0:fcaad0dfa051 43 assert(radioProtocol == ZigBee);
spastor 0:fcaad0dfa051 44
spastor 0:fcaad0dfa051 45 return retval;
spastor 0:fcaad0dfa051 46 }
spastor 0:fcaad0dfa051 47
spastor 0:fcaad0dfa051 48 /* Class destructor */
spastor 0:fcaad0dfa051 49 XBeeZB::~XBeeZB()
spastor 0:fcaad0dfa051 50 {
spastor 0:fcaad0dfa051 51 unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 52 unregister_receive_cb();
spastor 0:fcaad0dfa051 53 unregister_io_sample_cb();
spastor 0:fcaad0dfa051 54 }
spastor 0:fcaad0dfa051 55
spastor 0:fcaad0dfa051 56 RadioStatus XBeeZB::set_channel_mask(uint16_t chmask)
spastor 0:fcaad0dfa051 57 {
spastor 0:fcaad0dfa051 58 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 59
spastor 0:fcaad0dfa051 60 cmdresp = set_param("SC", chmask);
spastor 0:fcaad0dfa051 61 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 62 return Failure;
spastor 0:fcaad0dfa051 63 }
spastor 0:fcaad0dfa051 64 return Success;
spastor 0:fcaad0dfa051 65 }
spastor 0:fcaad0dfa051 66
spastor 0:fcaad0dfa051 67 RadioStatus XBeeZB::get_channel_mask(uint16_t * const chmask)
spastor 0:fcaad0dfa051 68 {
spastor 0:fcaad0dfa051 69 if (chmask == NULL) {
spastor 0:fcaad0dfa051 70 return Failure;
spastor 0:fcaad0dfa051 71 }
spastor 0:fcaad0dfa051 72 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 73
spastor 0:fcaad0dfa051 74 uint32_t var32;
spastor 0:fcaad0dfa051 75 cmdresp = get_param("SC", &var32);
spastor 4:629712865107 76 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 77 return Failure;
spastor 4:629712865107 78 }
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
hbujanda 6:06522f3a6642 195 RadioStatus XBeeZB::get_network_address(uint16_t * const addr16)
hbujanda 6:06522f3a6642 196 {
hbujanda 6:06522f3a6642 197 if (addr16 == NULL) {
hbujanda 6:06522f3a6642 198 return Failure;
hbujanda 6:06522f3a6642 199 }
hbujanda 6:06522f3a6642 200 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 6:06522f3a6642 201
hbujanda 6:06522f3a6642 202 uint32_t var32;
hbujanda 6:06522f3a6642 203 cmdresp = get_param("MY", &var32);
hbujanda 6:06522f3a6642 204 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 6:06522f3a6642 205 return Failure;
hbujanda 6:06522f3a6642 206 }
hbujanda 6:06522f3a6642 207 *addr16 = var32;
hbujanda 6:06522f3a6642 208 return Success;
hbujanda 6:06522f3a6642 209 }
hbujanda 6:06522f3a6642 210
hbujanda 6:06522f3a6642 211 RadioStatus XBeeZB::get_node_discovery_timeout(uint16_t * const timeout_ms)
hbujanda 6:06522f3a6642 212 {
hbujanda 6:06522f3a6642 213 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 6:06522f3a6642 214 uint32_t var32;
hbujanda 6:06522f3a6642 215
hbujanda 6:06522f3a6642 216 cmdresp = get_param("NT", &var32);
hbujanda 6:06522f3a6642 217 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 6:06522f3a6642 218 return Failure;
hbujanda 6:06522f3a6642 219 }
hbujanda 6:06522f3a6642 220 *timeout_ms = (uint16_t)var32;
hbujanda 6:06522f3a6642 221
hbujanda 6:06522f3a6642 222 /* No N? command available for this protocol. Add a fix 1s guard time */
hbujanda 6:06522f3a6642 223 *timeout_ms += 1000;
hbujanda 6:06522f3a6642 224
hbujanda 6:06522f3a6642 225 return Success;
hbujanda 6:06522f3a6642 226 }
hbujanda 6:06522f3a6642 227
hbujanda 6:06522f3a6642 228 RadioStatus XBeeZB::get_node_discovery_timeout(uint16_t * const timeout_ms, bool * const wait_for_complete_timeout)
hbujanda 6:06522f3a6642 229 {
hbujanda 6:06522f3a6642 230 const RadioStatus status = get_node_discovery_timeout(timeout_ms);
hbujanda 6:06522f3a6642 231
hbujanda 6:06522f3a6642 232 *wait_for_complete_timeout = false;
hbujanda 6:06522f3a6642 233
hbujanda 6:06522f3a6642 234 return status;
hbujanda 6:06522f3a6642 235 }
hbujanda 6:06522f3a6642 236
spastor 0:fcaad0dfa051 237 RadioStatus XBeeZB::check_for_coordinator_at_start(bool enable)
spastor 0:fcaad0dfa051 238 {
spastor 0:fcaad0dfa051 239 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 240
spastor 0:fcaad0dfa051 241 cmdresp = set_param("JV", (uint8_t)enable);
spastor 4:629712865107 242 return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure;
spastor 0:fcaad0dfa051 243 }
spastor 0:fcaad0dfa051 244
spastor 4:629712865107 245 RadioStatus XBeeZB::set_network_security_key(const uint8_t * const key, const uint16_t length)
spastor 0:fcaad0dfa051 246 {
spastor 4:629712865107 247 if (key == NULL || length == 0 || length > 16) {
spastor 0:fcaad0dfa051 248 return Failure;
spastor 0:fcaad0dfa051 249 }
spastor 0:fcaad0dfa051 250 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 251
spastor 0:fcaad0dfa051 252 cmdresp = set_param("NK", key, length);
spastor 4:629712865107 253 return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure;
spastor 0:fcaad0dfa051 254 }
spastor 0:fcaad0dfa051 255
spastor 0:fcaad0dfa051 256 RadioStatus XBeeZB::set_encryption_options(const uint8_t options)
spastor 0:fcaad0dfa051 257 {
spastor 0:fcaad0dfa051 258 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 259
spastor 0:fcaad0dfa051 260 cmdresp = set_param("EO", options);
spastor 4:629712865107 261 return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure;
spastor 0:fcaad0dfa051 262 }
spastor 0:fcaad0dfa051 263
hbujanda 8:b5f4a0e92249 264 RadioStatus XBeeZB::set_tc_link_key(const uint8_t * const key, const uint16_t length)
hbujanda 8:b5f4a0e92249 265 {
hbujanda 8:b5f4a0e92249 266 if (key == NULL || length == 0 || length > 16) {
hbujanda 8:b5f4a0e92249 267 return Failure;
hbujanda 8:b5f4a0e92249 268 }
hbujanda 8:b5f4a0e92249 269 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 8:b5f4a0e92249 270
hbujanda 8:b5f4a0e92249 271 cmdresp = set_param("KY", key, length);
hbujanda 8:b5f4a0e92249 272 return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure;
hbujanda 8:b5f4a0e92249 273 }
hbujanda 8:b5f4a0e92249 274
spastor 0:fcaad0dfa051 275 void XBeeZB::radio_status_update(AtCmdFrame::ModemStatus modem_status)
spastor 0:fcaad0dfa051 276 {
spastor 0:fcaad0dfa051 277 /* Update the radio status variables */
spastor 0:fcaad0dfa051 278 if (modem_status == AtCmdFrame::HwReset) {
spastor 0:fcaad0dfa051 279 _hw_reset_cnt++;
spastor 4:629712865107 280 } else if (modem_status == AtCmdFrame::WdReset) {
spastor 0:fcaad0dfa051 281 _wd_reset_cnt++;
spastor 0:fcaad0dfa051 282 }
spastor 0:fcaad0dfa051 283
spastor 0:fcaad0dfa051 284 _modem_status = modem_status;
spastor 0:fcaad0dfa051 285
spastor 0:fcaad0dfa051 286 digi_log(LogLevelDebug, "\r\nUpdating radio status: %02x\r\n", modem_status);
spastor 0:fcaad0dfa051 287 }
spastor 0:fcaad0dfa051 288
spastor 3:8662ebe83570 289 TxStatus XBeeZB::send_data(const RemoteXBee& remote, const uint8_t *const data, uint16_t len, bool syncr)
spastor 0:fcaad0dfa051 290 {
spastor 4:629712865107 291 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 292 return TxStatusInvalidAddr;
spastor 4:629712865107 293 }
spastor 0:fcaad0dfa051 294
spastor 0:fcaad0dfa051 295 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 296 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 297
spastor 4:629712865107 298 TxFrameZB frame = TxFrameZB(remote64, remote16, BROADCAST_RADIUS_USE_NH,
spastor 0:fcaad0dfa051 299 _tx_options, data, len);
spastor 3:8662ebe83570 300 if (syncr) {
spastor 3:8662ebe83570 301 return send_data(&frame);
spastor 3:8662ebe83570 302 } else {
spastor 3:8662ebe83570 303 frame.set_data(0, 0); /* Set frame id to 0 so there is no answer */
spastor 3:8662ebe83570 304 send_api_frame(&frame);
spastor 3:8662ebe83570 305 return TxStatusSuccess;
spastor 3:8662ebe83570 306 }
spastor 0:fcaad0dfa051 307 }
spastor 0:fcaad0dfa051 308
spastor 4:629712865107 309 TxStatus XBeeZB::send_data(const RemoteXBee& remote, uint8_t source_ep,
spastor 0:fcaad0dfa051 310 uint8_t dest_ep, uint16_t cluster_id, uint16_t profile_id,
spastor 3:8662ebe83570 311 const uint8_t *const data, uint16_t len, bool syncr)
spastor 0:fcaad0dfa051 312 {
spastor 4:629712865107 313 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 314 return TxStatusInvalidAddr;
spastor 4:629712865107 315 }
spastor 0:fcaad0dfa051 316
spastor 0:fcaad0dfa051 317 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 318 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 319
spastor 0:fcaad0dfa051 320 TxFrameZB frame = TxFrameZB(remote64, remote16, source_ep, dest_ep,
spastor 4:629712865107 321 cluster_id, profile_id, BROADCAST_RADIUS_USE_NH,
spastor 0:fcaad0dfa051 322 _tx_options, data, len);
spastor 3:8662ebe83570 323 if (syncr) {
spastor 3:8662ebe83570 324 return send_data(&frame);
spastor 3:8662ebe83570 325 } else {
spastor 3:8662ebe83570 326 frame.set_data(0, 0); /* Set frame id to 0 so there is no answer */
spastor 3:8662ebe83570 327 send_api_frame(&frame);
spastor 3:8662ebe83570 328 return TxStatusSuccess;
spastor 3:8662ebe83570 329 }
spastor 0:fcaad0dfa051 330 }
spastor 4:629712865107 331
spastor 3:8662ebe83570 332 TxStatus XBeeZB::send_data_to_coordinator(const uint8_t *const data, uint16_t len, bool syncr)
spastor 0:fcaad0dfa051 333 {
spastor 0:fcaad0dfa051 334 const uint64_t remaddr = ADDR64_COORDINATOR;
spastor 4:629712865107 335
spastor 4:629712865107 336 TxFrameZB frame = TxFrameZB(remaddr, ADDR16_UNKNOWN, BROADCAST_RADIUS_USE_NH, _tx_options, data, len);
spastor 3:8662ebe83570 337 if (syncr) {
spastor 3:8662ebe83570 338 return send_data(&frame);
spastor 3:8662ebe83570 339 } else {
spastor 3:8662ebe83570 340 frame.set_data(0, 0); /* Set frame id to 0 so there is no answer */
spastor 3:8662ebe83570 341 send_api_frame(&frame);
spastor 3:8662ebe83570 342 return TxStatusSuccess;
spastor 3:8662ebe83570 343 }
spastor 0:fcaad0dfa051 344 }
spastor 0:fcaad0dfa051 345
hbujanda 2:2ee1b6d51df2 346 RemoteXBeeZB XBeeZB::get_remote_node_by_id(const char * const node_id)
hbujanda 2:2ee1b6d51df2 347 {
hbujanda 2:2ee1b6d51df2 348 uint64_t addr64;
hbujanda 2:2ee1b6d51df2 349 uint16_t addr16;
hbujanda 2:2ee1b6d51df2 350 _get_remote_node_by_id(node_id, &addr64, &addr16);
hbujanda 2:2ee1b6d51df2 351 return RemoteXBeeZB(addr64, addr16);
hbujanda 2:2ee1b6d51df2 352 }
hbujanda 2:2ee1b6d51df2 353
spastor 4:629712865107 354 XBeeZB::AssocStatus XBeeZB::get_assoc_status(void)
spastor 0:fcaad0dfa051 355 {
spastor 4:629712865107 356 return (AssocStatus)get_AI();
spastor 0:fcaad0dfa051 357 }
spastor 0:fcaad0dfa051 358
spastor 0:fcaad0dfa051 359 bool XBeeZB::is_joined()
spastor 0:fcaad0dfa051 360 {
spastor 4:629712865107 361 return get_assoc_status() == Joined ? true : false;
spastor 0:fcaad0dfa051 362 }
spastor 0:fcaad0dfa051 363
spastor 0:fcaad0dfa051 364 void XBeeZB::register_node_discovery_cb(node_discovery_zb_cb_t function)
spastor 0:fcaad0dfa051 365 {
spastor 0:fcaad0dfa051 366 if (_nd_handler == NULL) {
spastor 0:fcaad0dfa051 367 _nd_handler = new FH_NodeDiscoveryZB();
spastor 0:fcaad0dfa051 368 register_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 369 }
spastor 0:fcaad0dfa051 370 _nd_handler->register_node_discovery_cb(function);
spastor 0:fcaad0dfa051 371 }
spastor 0:fcaad0dfa051 372
spastor 0:fcaad0dfa051 373 void XBeeZB::unregister_node_discovery_cb()
spastor 0:fcaad0dfa051 374 {
spastor 0:fcaad0dfa051 375 if (_nd_handler != NULL) {
spastor 0:fcaad0dfa051 376 _nd_handler->unregister_node_discovery_cb();
spastor 0:fcaad0dfa051 377 unregister_frame_handler(_nd_handler);
spastor 0:fcaad0dfa051 378 delete _nd_handler;
spastor 0:fcaad0dfa051 379 _nd_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 380 }
spastor 0:fcaad0dfa051 381 }
spastor 0:fcaad0dfa051 382
spastor 0:fcaad0dfa051 383 void XBeeZB::register_receive_cb(receive_zb_cb_t function)
spastor 0:fcaad0dfa051 384 {
spastor 0:fcaad0dfa051 385 if (_rx_pkt_handler == NULL) {
spastor 0:fcaad0dfa051 386 _rx_pkt_handler = new FH_RxPacketZB();
spastor 0:fcaad0dfa051 387 register_frame_handler(_rx_pkt_handler);
spastor 0:fcaad0dfa051 388 }
spastor 0:fcaad0dfa051 389 _rx_pkt_handler->register_receive_cb(function);
spastor 0:fcaad0dfa051 390 }
spastor 0:fcaad0dfa051 391
spastor 0:fcaad0dfa051 392 void XBeeZB::unregister_receive_cb()
spastor 0:fcaad0dfa051 393 {
spastor 0:fcaad0dfa051 394 if (_rx_pkt_handler != NULL) {
spastor 0:fcaad0dfa051 395 _rx_pkt_handler->unregister_receive_cb();
spastor 0:fcaad0dfa051 396 unregister_frame_handler(_rx_pkt_handler);
spastor 0:fcaad0dfa051 397 delete _rx_pkt_handler;
spastor 0:fcaad0dfa051 398 _rx_pkt_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 399 }
spastor 0:fcaad0dfa051 400 }
spastor 0:fcaad0dfa051 401
spastor 0:fcaad0dfa051 402 void XBeeZB::register_io_sample_cb(io_data_cb_zb_t function)
spastor 0:fcaad0dfa051 403 {
spastor 0:fcaad0dfa051 404 if (_io_data_handler == NULL) {
spastor 0:fcaad0dfa051 405 _io_data_handler = new FH_IoDataSampeZB();
spastor 0:fcaad0dfa051 406 register_frame_handler(_io_data_handler);
spastor 0:fcaad0dfa051 407 }
spastor 0:fcaad0dfa051 408 _io_data_handler->register_io_data_cb(function);
spastor 0:fcaad0dfa051 409 }
spastor 0:fcaad0dfa051 410
spastor 0:fcaad0dfa051 411 void XBeeZB::unregister_io_sample_cb()
spastor 0:fcaad0dfa051 412 {
spastor 0:fcaad0dfa051 413 if (_io_data_handler != NULL) {
spastor 0:fcaad0dfa051 414 _io_data_handler->unregister_io_data_cb();
spastor 0:fcaad0dfa051 415 unregister_frame_handler(_io_data_handler);
spastor 0:fcaad0dfa051 416 delete _io_data_handler;
spastor 0:fcaad0dfa051 417 _io_data_handler = NULL; /* as delete does not set to NULL */
spastor 0:fcaad0dfa051 418 }
spastor 0:fcaad0dfa051 419 }
spastor 0:fcaad0dfa051 420
spastor 0:fcaad0dfa051 421 AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint32_t * const data)
spastor 0:fcaad0dfa051 422 {
spastor 4:629712865107 423 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 424 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 4:629712865107 425 }
spastor 0:fcaad0dfa051 426
spastor 0:fcaad0dfa051 427 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 428 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 429 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 430 AtCmdFrame::AtCmdResp atCmdResponse;
spastor 0:fcaad0dfa051 431
spastor 0:fcaad0dfa051 432 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param);
spastor 0:fcaad0dfa051 433 atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len, RadioRemote);
spastor 0:fcaad0dfa051 434
spastor 4:629712865107 435 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) {
spastor 0:fcaad0dfa051 436 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 4:629712865107 437 }
spastor 0:fcaad0dfa051 438
spastor 0:fcaad0dfa051 439 return atCmdResponse;
spastor 0:fcaad0dfa051 440 }
spastor 0:fcaad0dfa051 441
spastor 0:fcaad0dfa051 442 AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, uint32_t data)
spastor 0:fcaad0dfa051 443 {
spastor 4:629712865107 444 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 445 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 4:629712865107 446 }
spastor 0:fcaad0dfa051 447
spastor 0:fcaad0dfa051 448 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 449 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 450
spastor 0:fcaad0dfa051 451 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param, data);
spastor 0:fcaad0dfa051 452 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 453 }
spastor 0:fcaad0dfa051 454
spastor 0:fcaad0dfa051 455 AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, const uint8_t * data, uint16_t len)
spastor 0:fcaad0dfa051 456 {
spastor 4:629712865107 457 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 458 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 4:629712865107 459 }
spastor 0:fcaad0dfa051 460
spastor 0:fcaad0dfa051 461 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 462 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 463
spastor 0:fcaad0dfa051 464 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param, data, len);
spastor 0:fcaad0dfa051 465 return send_at_cmd(&cmd_frame, NULL, NULL, RadioRemote);
spastor 0:fcaad0dfa051 466 }
spastor 0:fcaad0dfa051 467
spastor 0:fcaad0dfa051 468 AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint8_t * const data, uint16_t * const len)
spastor 0:fcaad0dfa051 469 {
spastor 0:fcaad0dfa051 470
spastor 4:629712865107 471 if (!remote.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 472 return AtCmdFrame::AtCmdRespInvalidAddr;
spastor 4:629712865107 473 }
spastor 0:fcaad0dfa051 474
spastor 0:fcaad0dfa051 475 const uint64_t remote64 = remote.get_addr64();
spastor 0:fcaad0dfa051 476 const uint16_t remote16 = remote.get_addr16();
spastor 0:fcaad0dfa051 477
spastor 0:fcaad0dfa051 478 AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param);
spastor 0:fcaad0dfa051 479 return send_at_cmd(&cmd_frame, data, len, RadioRemote, false);
spastor 0:fcaad0dfa051 480 }
spastor 0:fcaad0dfa051 481
spastor 0:fcaad0dfa051 482 static void get_dio_cmd(XBeeZB::IoLine line, char * const iocmd)
spastor 0:fcaad0dfa051 483 {
spastor 0:fcaad0dfa051 484 if (line >= XBeeZB::DIO10) {
spastor 0:fcaad0dfa051 485 iocmd[0] = 'P';
spastor 0:fcaad0dfa051 486 iocmd[1] = '0' + line - XBeeZB::DIO10;
spastor 0:fcaad0dfa051 487 } else {
spastor 0:fcaad0dfa051 488 iocmd[0] = 'D';
spastor 0:fcaad0dfa051 489 iocmd[1] = '0' + line;
spastor 0:fcaad0dfa051 490 }
spastor 0:fcaad0dfa051 491 iocmd[2] = '\0';
spastor 0:fcaad0dfa051 492 }
spastor 0:fcaad0dfa051 493
spastor 0:fcaad0dfa051 494 RadioStatus XBeeZB::set_pin_config(const RemoteXBee& remote, IoLine line, IoMode mode)
spastor 0:fcaad0dfa051 495 {
spastor 0:fcaad0dfa051 496 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 497 char iocmd[3];
spastor 0:fcaad0dfa051 498
spastor 0:fcaad0dfa051 499 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 500
spastor 0:fcaad0dfa051 501 cmdresp = set_param(remote, iocmd, (uint8_t)mode);
spastor 0:fcaad0dfa051 502 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 503 digi_log(LogLevelError, "set_pin_config: set_param returned %d\r\n", cmdresp);
spastor 0:fcaad0dfa051 504 return Failure;
spastor 0:fcaad0dfa051 505 }
spastor 0:fcaad0dfa051 506
spastor 0:fcaad0dfa051 507 return Success;
spastor 0:fcaad0dfa051 508 }
spastor 0:fcaad0dfa051 509
spastor 0:fcaad0dfa051 510 RadioStatus XBeeZB::get_pin_config(const RemoteXBee& remote, IoLine line, IoMode * const mode)
spastor 0:fcaad0dfa051 511 {
spastor 0:fcaad0dfa051 512 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 513 char iocmd[3];
spastor 0:fcaad0dfa051 514
spastor 0:fcaad0dfa051 515 get_dio_cmd(line, iocmd);
spastor 0:fcaad0dfa051 516
spastor 0:fcaad0dfa051 517 uint32_t var32;
spastor 0:fcaad0dfa051 518 cmdresp = get_param(remote, iocmd, &var32);
spastor 0:fcaad0dfa051 519 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 520 return Failure;
spastor 0:fcaad0dfa051 521 }
spastor 0:fcaad0dfa051 522 *mode = (IoMode)var32;
spastor 0:fcaad0dfa051 523
spastor 0:fcaad0dfa051 524 return Success;
spastor 0:fcaad0dfa051 525 }
spastor 0:fcaad0dfa051 526
spastor 0:fcaad0dfa051 527 RadioStatus XBeeZB::set_dio(const RemoteXBee& remote, IoLine line, DioVal val)
spastor 0:fcaad0dfa051 528 {
spastor 4:629712865107 529 return set_pin_config(remote, line, val == Low ? DigitalOutLow : DigitalOutHigh);
spastor 0:fcaad0dfa051 530 }
spastor 0:fcaad0dfa051 531
spastor 0:fcaad0dfa051 532 RadioStatus XBeeZB::get_dio(const RemoteXBee& remote, IoLine line, DioVal * const val)
spastor 0:fcaad0dfa051 533 {
spastor 3:8662ebe83570 534 return get_iosample(remote).get_dio(line, val);
spastor 0:fcaad0dfa051 535 }
spastor 0:fcaad0dfa051 536
spastor 0:fcaad0dfa051 537 RadioStatus XBeeZB::get_adc(const RemoteXBee& remote, IoLine line, uint16_t * const val)
spastor 0:fcaad0dfa051 538 {
spastor 3:8662ebe83570 539 return get_iosample(remote).get_adc(line, val);
spastor 3:8662ebe83570 540 }
spastor 0:fcaad0dfa051 541
spastor 3:8662ebe83570 542 IOSampleZB XBeeZB::get_iosample(const RemoteXBee& remote)
spastor 3:8662ebe83570 543 {
spastor 3:8662ebe83570 544 uint8_t io_sample[MAX_IO_SAMPLE_ZB_LEN];
spastor 3:8662ebe83570 545 uint16_t len = sizeof io_sample;
spastor 3:8662ebe83570 546
spastor 3:8662ebe83570 547 RadioStatus resp = _get_iosample(remote, io_sample, &len);
spastor 3:8662ebe83570 548 if (resp != Success) {
spastor 3:8662ebe83570 549 digi_log(LogLevelError, "XBeeZB::get_iosample failed to get an IOSample\r\n");
spastor 3:8662ebe83570 550 len = 0;
spastor 0:fcaad0dfa051 551 }
spastor 0:fcaad0dfa051 552
spastor 3:8662ebe83570 553 return IOSampleZB(io_sample, len);
spastor 0:fcaad0dfa051 554 }
spastor 0:fcaad0dfa051 555
spastor 0:fcaad0dfa051 556 static uint16_t get_dio_pr_mask(XBeeZB::IoLine line)
spastor 0:fcaad0dfa051 557 {
spastor 0:fcaad0dfa051 558 switch (line) {
spastor 0:fcaad0dfa051 559 case XBeeZB::DIO4:
spastor 0:fcaad0dfa051 560 return (1 << 0);
spastor 0:fcaad0dfa051 561 case XBeeZB::DIO3_AD3:
spastor 0:fcaad0dfa051 562 return (1 << 1);
spastor 0:fcaad0dfa051 563 case XBeeZB::DIO2_AD2:
spastor 0:fcaad0dfa051 564 return (1 << 2);
spastor 0:fcaad0dfa051 565 case XBeeZB::DIO1_AD1:
spastor 0:fcaad0dfa051 566 return (1 << 3);
spastor 0:fcaad0dfa051 567 case XBeeZB::DIO0_AD0:
spastor 0:fcaad0dfa051 568 return (1 << 4);
spastor 0:fcaad0dfa051 569 case XBeeZB::DIO6:
spastor 0:fcaad0dfa051 570 return (1 << 5);
spastor 0:fcaad0dfa051 571 case XBeeZB::DIO5:
spastor 0:fcaad0dfa051 572 return (1 << 8);
spastor 0:fcaad0dfa051 573 case XBeeZB::DIO12:
spastor 0:fcaad0dfa051 574 return (1 << 10);
spastor 0:fcaad0dfa051 575 case XBeeZB::DIO10:
spastor 0:fcaad0dfa051 576 return (1 << 11);
spastor 0:fcaad0dfa051 577 case XBeeZB::DIO11:
spastor 0:fcaad0dfa051 578 return (1 << 12);
spastor 0:fcaad0dfa051 579 case XBeeZB::DIO7:
spastor 0:fcaad0dfa051 580 return (1 << 13);
spastor 0:fcaad0dfa051 581 default:
spastor 0:fcaad0dfa051 582 return 0;
spastor 0:fcaad0dfa051 583 }
spastor 0:fcaad0dfa051 584 }
spastor 0:fcaad0dfa051 585
spastor 0:fcaad0dfa051 586 RadioStatus XBeeZB::set_pin_pull_up(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 587 {
spastor 0:fcaad0dfa051 588 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 589 uint32_t var32;
spastor 0:fcaad0dfa051 590 uint16_t pr;
spastor 0:fcaad0dfa051 591
spastor 0:fcaad0dfa051 592 cmdresp = get_param(remote, "PR", &var32);
spastor 0:fcaad0dfa051 593 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 594 return Failure;
spastor 0:fcaad0dfa051 595 }
spastor 0:fcaad0dfa051 596 pr = var32;
spastor 0:fcaad0dfa051 597
spastor 0:fcaad0dfa051 598 const uint16_t dio_mask = get_dio_pr_mask(line);
spastor 0:fcaad0dfa051 599 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 600 digi_log(LogLevelError, "XBeeZB::set_pin_pull_up: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 601 return Failure;
spastor 0:fcaad0dfa051 602 }
spastor 0:fcaad0dfa051 603
spastor 0:fcaad0dfa051 604 if (enable) {
spastor 0:fcaad0dfa051 605 pr |= dio_mask;
spastor 0:fcaad0dfa051 606 } else {
spastor 0:fcaad0dfa051 607 pr &= ~dio_mask;
spastor 0:fcaad0dfa051 608 }
spastor 0:fcaad0dfa051 609
spastor 0:fcaad0dfa051 610 cmdresp = set_param(remote, "PR", pr);
spastor 4:629712865107 611 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 612 return Failure;
spastor 4:629712865107 613 }
spastor 0:fcaad0dfa051 614
spastor 0:fcaad0dfa051 615 return Success;
spastor 0:fcaad0dfa051 616 }
spastor 0:fcaad0dfa051 617
spastor 0:fcaad0dfa051 618 static uint16_t get_dio_ic_mask(XBeeZB::IoLine line)
spastor 0:fcaad0dfa051 619 {
spastor 0:fcaad0dfa051 620 if (line < XBeeZB::DIO12) {
spastor 0:fcaad0dfa051 621 return (1 << line);
spastor 0:fcaad0dfa051 622 }
spastor 0:fcaad0dfa051 623 return 0;
spastor 0:fcaad0dfa051 624 }
spastor 0:fcaad0dfa051 625
spastor 0:fcaad0dfa051 626 RadioStatus XBeeZB::enable_dio_change_detection(const RemoteXBee& remote, IoLine line, bool enable)
spastor 0:fcaad0dfa051 627 {
spastor 0:fcaad0dfa051 628 if (line > DIO11) {
spastor 0:fcaad0dfa051 629 digi_log(LogLevelError, "XBeeZB::enable_dio_change_detection: pin not supported (%d)\r\n", line);
spastor 0:fcaad0dfa051 630 return Failure;
spastor 0:fcaad0dfa051 631 }
spastor 0:fcaad0dfa051 632
spastor 0:fcaad0dfa051 633 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 634 uint32_t var32;
spastor 0:fcaad0dfa051 635 uint16_t ic;
spastor 0:fcaad0dfa051 636
spastor 0:fcaad0dfa051 637 cmdresp = get_param(remote, "IC", &var32);
spastor 0:fcaad0dfa051 638 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 639 return Failure;
spastor 0:fcaad0dfa051 640 }
spastor 0:fcaad0dfa051 641 ic = var32;
spastor 0:fcaad0dfa051 642
spastor 0:fcaad0dfa051 643 const uint16_t dio_mask = get_dio_ic_mask(line);
spastor 0:fcaad0dfa051 644 if (dio_mask == 0) {
spastor 0:fcaad0dfa051 645 digi_log(LogLevelError, "XBeeZB::enable_dio_change_detection: invalid pin %d\r\n", line);
spastor 0:fcaad0dfa051 646 return Failure;
spastor 0:fcaad0dfa051 647 }
spastor 0:fcaad0dfa051 648
spastor 0:fcaad0dfa051 649 if (enable) {
spastor 0:fcaad0dfa051 650 ic |= dio_mask;
spastor 0:fcaad0dfa051 651 } else {
spastor 0:fcaad0dfa051 652 ic &= ~dio_mask;
spastor 0:fcaad0dfa051 653 }
spastor 0:fcaad0dfa051 654
spastor 0:fcaad0dfa051 655 cmdresp = set_param(remote, "IC", ic);
spastor 4:629712865107 656 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 657 return Failure;
spastor 4:629712865107 658 }
spastor 0:fcaad0dfa051 659
spastor 0:fcaad0dfa051 660 return Success;
spastor 0:fcaad0dfa051 661 }