XBee modules

Dependencies:   DigiLogger

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 "XBeeLib.h"
spastor 0:fcaad0dfa051 14 #include "Frames/ApiFrame.h"
spastor 0:fcaad0dfa051 15
spastor 0:fcaad0dfa051 16 using namespace XBeeLib;
spastor 0:fcaad0dfa051 17
spastor 0:fcaad0dfa051 18 RadioStatus XBee::write_config(void)
spastor 0:fcaad0dfa051 19 {
spastor 0:fcaad0dfa051 20 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 21
spastor 0:fcaad0dfa051 22 cmdresp = set_param("WR");
spastor 0:fcaad0dfa051 23 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 24 return Failure;
spastor 0:fcaad0dfa051 25 }
spastor 0:fcaad0dfa051 26 return Success;
spastor 0:fcaad0dfa051 27 }
spastor 0:fcaad0dfa051 28
spastor 0:fcaad0dfa051 29 RadioStatus XBee::sleep_now(void)
spastor 0:fcaad0dfa051 30 {
spastor 0:fcaad0dfa051 31 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 32
spastor 0:fcaad0dfa051 33 cmdresp = set_param("SI");
spastor 0:fcaad0dfa051 34 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 35 return Failure;
spastor 0:fcaad0dfa051 36 }
spastor 0:fcaad0dfa051 37 return Success;
spastor 0:fcaad0dfa051 38 }
spastor 0:fcaad0dfa051 39
spastor 0:fcaad0dfa051 40 RadioStatus XBee::set_power_level(uint8_t level)
spastor 0:fcaad0dfa051 41 {
spastor 0:fcaad0dfa051 42 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 43
spastor 0:fcaad0dfa051 44 if (level > 4) {
spastor 0:fcaad0dfa051 45 return Failure;
spastor 0:fcaad0dfa051 46 }
spastor 0:fcaad0dfa051 47
spastor 0:fcaad0dfa051 48 cmdresp = set_param("PL", level);
spastor 0:fcaad0dfa051 49 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 50 return Failure;
spastor 0:fcaad0dfa051 51 }
spastor 0:fcaad0dfa051 52
spastor 0:fcaad0dfa051 53 return Success;
spastor 0:fcaad0dfa051 54 }
spastor 0:fcaad0dfa051 55
spastor 0:fcaad0dfa051 56 RadioStatus XBee::get_power_level(uint8_t * const level)
spastor 0:fcaad0dfa051 57 {
spastor 0:fcaad0dfa051 58 if (level == NULL) {
spastor 0:fcaad0dfa051 59 return Failure;
spastor 0:fcaad0dfa051 60 }
spastor 0:fcaad0dfa051 61 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 62
spastor 0:fcaad0dfa051 63 uint32_t var32;
spastor 0:fcaad0dfa051 64 cmdresp = get_param("PL", &var32);
spastor 0:fcaad0dfa051 65 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 66 return Failure;
spastor 0:fcaad0dfa051 67 }
spastor 0:fcaad0dfa051 68 *level = var32;
spastor 0:fcaad0dfa051 69 return Success;
spastor 0:fcaad0dfa051 70 }
spastor 0:fcaad0dfa051 71
spastor 0:fcaad0dfa051 72 RadioStatus XBee::get_network_address(uint16_t * const addr16)
spastor 0:fcaad0dfa051 73 {
spastor 0:fcaad0dfa051 74 if (addr16 == NULL) {
spastor 0:fcaad0dfa051 75 return Failure;
spastor 0:fcaad0dfa051 76 }
spastor 0:fcaad0dfa051 77 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 78
spastor 0:fcaad0dfa051 79 uint32_t var32;
spastor 0:fcaad0dfa051 80 cmdresp = get_param("MY", &var32);
spastor 0:fcaad0dfa051 81 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 82 return Failure;
spastor 0:fcaad0dfa051 83 }
spastor 0:fcaad0dfa051 84 *addr16 = var32;
spastor 0:fcaad0dfa051 85 return Success;
spastor 0:fcaad0dfa051 86 }
spastor 0:fcaad0dfa051 87
spastor 0:fcaad0dfa051 88 RadioStatus XBee::software_reset(void)
spastor 0:fcaad0dfa051 89 {
spastor 0:fcaad0dfa051 90 volatile uint16_t * const rst_cnt_p = &_wd_reset_cnt;
spastor 0:fcaad0dfa051 91 const uint16_t init_rst_cnt = *rst_cnt_p;
spastor 0:fcaad0dfa051 92
spastor 0:fcaad0dfa051 93 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 94
spastor 0:fcaad0dfa051 95 cmdresp = set_param("FR");
spastor 0:fcaad0dfa051 96 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 1:794d1d3e4a08 97 digi_log(LogLevelError, "software_reset failed!\r\n");
spastor 0:fcaad0dfa051 98 return Failure;
spastor 0:fcaad0dfa051 99 }
spastor 0:fcaad0dfa051 100
spastor 0:fcaad0dfa051 101 return wait_for_module_to_reset(rst_cnt_p, init_rst_cnt);
spastor 0:fcaad0dfa051 102 }
spastor 0:fcaad0dfa051 103
spastor 0:fcaad0dfa051 104 RadioStatus XBee::set_node_identifier(const char * const node_id)
spastor 0:fcaad0dfa051 105 {
spastor 0:fcaad0dfa051 106 if (node_id == NULL) {
spastor 0:fcaad0dfa051 107 return Failure;
spastor 0:fcaad0dfa051 108 }
spastor 0:fcaad0dfa051 109
spastor 0:fcaad0dfa051 110 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 111 const size_t str_len = strlen(node_id);
spastor 0:fcaad0dfa051 112
spastor 0:fcaad0dfa051 113 if(str_len > 20 || str_len < 1) {
spastor 0:fcaad0dfa051 114 return Failure;
spastor 0:fcaad0dfa051 115 }
spastor 0:fcaad0dfa051 116
spastor 0:fcaad0dfa051 117 cmdresp = set_param("NI", (const uint8_t *)node_id, str_len);
spastor 0:fcaad0dfa051 118 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 119 return Failure;
spastor 0:fcaad0dfa051 120 }
spastor 0:fcaad0dfa051 121 return Success;
spastor 0:fcaad0dfa051 122 }
spastor 0:fcaad0dfa051 123
spastor 0:fcaad0dfa051 124 RadioStatus XBee::get_node_identifier(char * const node_id)
spastor 0:fcaad0dfa051 125 {
spastor 0:fcaad0dfa051 126 if (node_id == NULL) {
spastor 0:fcaad0dfa051 127 return Failure;
spastor 0:fcaad0dfa051 128 }
spastor 0:fcaad0dfa051 129
spastor 0:fcaad0dfa051 130 uint16_t max_ni_length = 20;
spastor 0:fcaad0dfa051 131 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 132
spastor 0:fcaad0dfa051 133 cmdresp = get_param("NI", (uint8_t *)node_id, &max_ni_length);
spastor 0:fcaad0dfa051 134 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 135 return Failure;
spastor 0:fcaad0dfa051 136 }
spastor 0:fcaad0dfa051 137 node_id[max_ni_length] = '\0';
spastor 0:fcaad0dfa051 138 return Success;
spastor 0:fcaad0dfa051 139 }
spastor 0:fcaad0dfa051 140
spastor 0:fcaad0dfa051 141 uint16_t XBee::get_hw_version() const
spastor 0:fcaad0dfa051 142 {
spastor 0:fcaad0dfa051 143 return _hw_version;
spastor 0:fcaad0dfa051 144 }
spastor 0:fcaad0dfa051 145
spastor 0:fcaad0dfa051 146 uint16_t XBee::get_fw_version() const
spastor 0:fcaad0dfa051 147 {
spastor 0:fcaad0dfa051 148 return _fw_version;
spastor 0:fcaad0dfa051 149 }
spastor 0:fcaad0dfa051 150
spastor 0:fcaad0dfa051 151 void XBee::set_tx_options(uint8_t options)
spastor 0:fcaad0dfa051 152 {
spastor 0:fcaad0dfa051 153 _tx_options = options;
spastor 0:fcaad0dfa051 154 }
spastor 0:fcaad0dfa051 155
spastor 0:fcaad0dfa051 156 uint8_t XBee::get_tx_options() const
spastor 0:fcaad0dfa051 157 {
spastor 0:fcaad0dfa051 158 return _tx_options;
spastor 0:fcaad0dfa051 159 }
spastor 0:fcaad0dfa051 160
spastor 0:fcaad0dfa051 161 void XBee::set_broadcast_radius(uint8_t bc_radius)
spastor 0:fcaad0dfa051 162 {
spastor 0:fcaad0dfa051 163 _bc_radius = bc_radius;
spastor 0:fcaad0dfa051 164 }
spastor 0:fcaad0dfa051 165
spastor 0:fcaad0dfa051 166 uint8_t XBee::get_bc_radius() const
spastor 0:fcaad0dfa051 167 {
spastor 0:fcaad0dfa051 168 return _bc_radius;
spastor 0:fcaad0dfa051 169 }
spastor 0:fcaad0dfa051 170
spastor 0:fcaad0dfa051 171 RadioStatus XBee::start_node_discovery()
spastor 0:fcaad0dfa051 172 {
spastor 0:fcaad0dfa051 173 AtCmdFrame cmd_frame = AtCmdFrame("ND");
spastor 0:fcaad0dfa051 174 send_api_frame(&cmd_frame);
spastor 0:fcaad0dfa051 175
spastor 0:fcaad0dfa051 176 return Success;
spastor 0:fcaad0dfa051 177 }
spastor 0:fcaad0dfa051 178
spastor 0:fcaad0dfa051 179 RadioStatus XBee::config_node_discovery(uint16_t timeout_ms, uint8_t options)
spastor 0:fcaad0dfa051 180 {
spastor 0:fcaad0dfa051 181 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 182
spastor 0:fcaad0dfa051 183 cmdresp = set_param("NT", (uint8_t)(timeout_ms / 100));
spastor 0:fcaad0dfa051 184 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 185 return Failure;
spastor 0:fcaad0dfa051 186 }
spastor 0:fcaad0dfa051 187
spastor 0:fcaad0dfa051 188 cmdresp = set_param("NO", (uint8_t)options);
spastor 0:fcaad0dfa051 189 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 190 return Failure;
spastor 0:fcaad0dfa051 191 }
spastor 0:fcaad0dfa051 192 cmdresp = set_param("AC");
spastor 0:fcaad0dfa051 193 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 194 return Failure;
spastor 0:fcaad0dfa051 195 }
spastor 0:fcaad0dfa051 196
spastor 0:fcaad0dfa051 197 return Success;
spastor 0:fcaad0dfa051 198 }
spastor 0:fcaad0dfa051 199
spastor 0:fcaad0dfa051 200 RadioStatus XBee::get_config_node_discovery(uint16_t * const timeout_ms, uint8_t * const options)
spastor 0:fcaad0dfa051 201 {
spastor 0:fcaad0dfa051 202 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 203 uint32_t var32;
spastor 0:fcaad0dfa051 204
spastor 0:fcaad0dfa051 205 cmdresp = get_param("NT", &var32);
spastor 0:fcaad0dfa051 206 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 207 return Failure;
spastor 0:fcaad0dfa051 208 }
spastor 0:fcaad0dfa051 209 *timeout_ms = var32;
spastor 0:fcaad0dfa051 210
spastor 0:fcaad0dfa051 211 cmdresp = get_param("NO", &var32);
spastor 0:fcaad0dfa051 212 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 213 return Failure;
spastor 0:fcaad0dfa051 214 }
spastor 0:fcaad0dfa051 215 *options = var32;
spastor 0:fcaad0dfa051 216 return Success;
spastor 0:fcaad0dfa051 217 }
spastor 0:fcaad0dfa051 218
spastor 0:fcaad0dfa051 219 RadioStatus XBee::get_iosample(const RemoteXBee& remote, uint8_t * const io_sample, uint16_t * const len)
spastor 0:fcaad0dfa051 220 {
spastor 0:fcaad0dfa051 221 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 222 *len = MAX_IO_SAMPLE_BUF_LEN;
spastor 0:fcaad0dfa051 223
spastor 0:fcaad0dfa051 224 /* Force a sample read */
spastor 0:fcaad0dfa051 225 cmdresp = get_param(remote, "IS", io_sample, len);
spastor 0:fcaad0dfa051 226 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 227 digi_log(LogLevelError, "get_iosample error %d:\r\n", cmdresp);
spastor 0:fcaad0dfa051 228 return Failure;
spastor 0:fcaad0dfa051 229 }
spastor 0:fcaad0dfa051 230
spastor 0:fcaad0dfa051 231 return Success;
spastor 0:fcaad0dfa051 232 }
spastor 0:fcaad0dfa051 233
spastor 0:fcaad0dfa051 234 RadioStatus XBee::config_io_sample_destination(const RemoteXBee& remote, const RemoteXBee& destination)
spastor 0:fcaad0dfa051 235 {
spastor 0:fcaad0dfa051 236 uint32_t dh;
spastor 0:fcaad0dfa051 237 uint32_t dl;
spastor 0:fcaad0dfa051 238
spastor 0:fcaad0dfa051 239 if (destination.is_valid_addr64b()) {
spastor 0:fcaad0dfa051 240 const uint64_t dest64 = destination.get_addr64();
spastor 0:fcaad0dfa051 241 dh = (uint32_t)((dest64 >> 32) & 0xFFFFFFFF);
spastor 0:fcaad0dfa051 242 dl = (uint32_t)((dest64 & 0xFFFFFFFF));
spastor 0:fcaad0dfa051 243 } else if (destination.is_valid_addr16b()) {
spastor 0:fcaad0dfa051 244 const uint16_t destAddr16 = destination.get_addr16();
spastor 0:fcaad0dfa051 245 dh = 0;
spastor 0:fcaad0dfa051 246 dl = destAddr16;
spastor 0:fcaad0dfa051 247 } else {
spastor 0:fcaad0dfa051 248 digi_log(LogLevelError, "send_io_sample_to: Invalid destination");
spastor 0:fcaad0dfa051 249 return Failure;
spastor 0:fcaad0dfa051 250 }
spastor 0:fcaad0dfa051 251
spastor 0:fcaad0dfa051 252 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 253
spastor 0:fcaad0dfa051 254 cmdresp = set_param(remote, "DH", dh);
spastor 0:fcaad0dfa051 255 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 256 digi_log(LogLevelError, "send_io_sample_to error %d:\r\n", cmdresp);
spastor 0:fcaad0dfa051 257 return Failure;
spastor 0:fcaad0dfa051 258 }
spastor 0:fcaad0dfa051 259
spastor 0:fcaad0dfa051 260 cmdresp = set_param(remote, "DL", dl);
spastor 0:fcaad0dfa051 261 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 262 digi_log(LogLevelError, "send_io_sample_to error %d:\r\n", cmdresp);
spastor 0:fcaad0dfa051 263 return Failure;
spastor 0:fcaad0dfa051 264 }
spastor 0:fcaad0dfa051 265
spastor 0:fcaad0dfa051 266 return Success;
spastor 0:fcaad0dfa051 267 }
spastor 0:fcaad0dfa051 268
spastor 0:fcaad0dfa051 269 RadioStatus XBee::set_io_sample_rate(const RemoteXBee& remote, const float seconds)
spastor 0:fcaad0dfa051 270 {
spastor 0:fcaad0dfa051 271 const float max_seconds = 65.535;
spastor 0:fcaad0dfa051 272
spastor 0:fcaad0dfa051 273 if (seconds > max_seconds) {
spastor 0:fcaad0dfa051 274 digi_log(LogLevelError, "XBee::set_io_sample_rate error seconds rate exceeds maximum %d:\r\n", max_seconds);
spastor 0:fcaad0dfa051 275 return Failure;
spastor 0:fcaad0dfa051 276 }
spastor 0:fcaad0dfa051 277
spastor 0:fcaad0dfa051 278 AtCmdFrame::AtCmdResp cmdresp;
spastor 0:fcaad0dfa051 279 const uint16_t milliseconds = seconds * 1000;
spastor 0:fcaad0dfa051 280
spastor 0:fcaad0dfa051 281 cmdresp = set_param(remote, "IR", milliseconds);
spastor 0:fcaad0dfa051 282 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 283 digi_log(LogLevelError, "XBee::set_io_sample_rate error %d:\r\n", cmdresp);
spastor 0:fcaad0dfa051 284 return Failure;
spastor 0:fcaad0dfa051 285 }
spastor 0:fcaad0dfa051 286
spastor 0:fcaad0dfa051 287 return Success;
spastor 0:fcaad0dfa051 288 }