Library to easily communicate with XBee modules.

Fork of XBeeLib by Digi International Inc.

Committer:
basvuyk
Date:
Tue Mar 27 14:13:12 2018 +0000
Revision:
10:273032d9299a
Parent:
4:629712865107
Slave V1.0

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 4:629712865107 12
spastor 0:fcaad0dfa051 13 #include "XBeeLib.h"
spastor 0:fcaad0dfa051 14
spastor 0:fcaad0dfa051 15 #define GET_CMD_RESP(fr, radio_location) (radio_location == RadioRemote ? fr->get_data_at(REM_AT_CMD_RESP_STATUS_OFFSET) \
spastor 0:fcaad0dfa051 16 : fr->get_data_at(ATCMD_RESP_STATUS_OFFSET))
spastor 0:fcaad0dfa051 17
spastor 0:fcaad0dfa051 18 #define GET_DATA_LEN(fr, radio_location) (radio_location == RadioRemote ? (fr->get_data_len() - REM_AT_CMD_RESP_OVERHEAD) \
spastor 0:fcaad0dfa051 19 : (fr->get_data_len() - ATCMD_RESP_OVERHEAD))
spastor 4:629712865107 20
spastor 0:fcaad0dfa051 21 #define GET_DATA_OFF(radio_location) (radio_location == RadioRemote ? REM_AT_CMD_RESP_CMD_DATA_OFFSET \
spastor 0:fcaad0dfa051 22 : ATCMD_RESP_DATA_OFFSET)
spastor 0:fcaad0dfa051 23
spastor 4:629712865107 24 using namespace XBeeLib;
spastor 0:fcaad0dfa051 25
spastor 0:fcaad0dfa051 26 /** Method that sends an AT command to the module and waits for the command response.
spastor 0:fcaad0dfa051 27 * @returns the AT command response */
spastor 0:fcaad0dfa051 28 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame,
spastor 0:fcaad0dfa051 29 uint8_t *const buf, uint16_t *const len, RadioLocation radio_location, bool reverse)
spastor 0:fcaad0dfa051 30 {
spastor 0:fcaad0dfa051 31 AtCmdFrame::AtCmdResp resp = AtCmdFrame::AtCmdRespTimeout;
spastor 0:fcaad0dfa051 32 ApiFrame *resp_frame;
spastor 4:629712865107 33 ApiFrame::ApiFrameType expected_type =
spastor 0:fcaad0dfa051 34 (frame->get_frame_type() == ApiFrame::AtCmd) ?
spastor 0:fcaad0dfa051 35 ApiFrame::AtCmdResp : ApiFrame::RemoteCmdResp;
spastor 0:fcaad0dfa051 36
spastor 0:fcaad0dfa051 37 send_api_frame(frame);
spastor 0:fcaad0dfa051 38
spastor 0:fcaad0dfa051 39 /* Wait for the AT command response packet */
spastor 0:fcaad0dfa051 40 resp_frame = get_this_api_frame(frame->get_frame_id(), expected_type);
spastor 4:629712865107 41 if (resp_frame == NULL) {
spastor 0:fcaad0dfa051 42 return resp;
spastor 4:629712865107 43 }
spastor 4:629712865107 44
spastor 0:fcaad0dfa051 45 resp = (AtCmdFrame::AtCmdResp)GET_CMD_RESP(resp_frame, radio_location);
spastor 0:fcaad0dfa051 46 if (resp == AtCmdFrame::AtCmdRespOk) {
spastor 0:fcaad0dfa051 47 if (buf != NULL && len != NULL) {
spastor 0:fcaad0dfa051 48
spastor 0:fcaad0dfa051 49 /* Copy the command response data */
spastor 0:fcaad0dfa051 50 uint16_t new_len = GET_DATA_LEN(resp_frame, radio_location);
spastor 0:fcaad0dfa051 51
spastor 0:fcaad0dfa051 52 *len = (*len < new_len) ? *len : new_len;
spastor 0:fcaad0dfa051 53
spastor 0:fcaad0dfa051 54 /* rmemcpy makes the endian change */
spastor 0:fcaad0dfa051 55 if (reverse) {
spastor 0:fcaad0dfa051 56 rmemcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len);
spastor 0:fcaad0dfa051 57 } else {
spastor 0:fcaad0dfa051 58 memcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len);
spastor 0:fcaad0dfa051 59 }
spastor 0:fcaad0dfa051 60 }
spastor 0:fcaad0dfa051 61 } else {
spastor 0:fcaad0dfa051 62 digi_log(LogLevelWarning, "send_at_cmd bad response: 0x%x\r\n", resp);
spastor 0:fcaad0dfa051 63 }
spastor 0:fcaad0dfa051 64
spastor 0:fcaad0dfa051 65 /* Once processed, remove the frame from the buffer */
spastor 4:629712865107 66 _framebuf_syncr.free_frame(resp_frame);
spastor 0:fcaad0dfa051 67 return resp;
spastor 0:fcaad0dfa051 68 }
spastor 0:fcaad0dfa051 69
spastor 0:fcaad0dfa051 70 /** Method that sends an AT command to the module and waits for the command response.
spastor 0:fcaad0dfa051 71 * @returns the AT command response */
spastor 0:fcaad0dfa051 72 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame)
spastor 0:fcaad0dfa051 73 {
spastor 0:fcaad0dfa051 74 return send_at_cmd(frame, NULL, NULL);
spastor 0:fcaad0dfa051 75 }
spastor 0:fcaad0dfa051 76
spastor 0:fcaad0dfa051 77 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint8_t *data)
spastor 0:fcaad0dfa051 78 {
spastor 0:fcaad0dfa051 79 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 80 AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, data, &len);
spastor 0:fcaad0dfa051 81
spastor 4:629712865107 82 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
spastor 0:fcaad0dfa051 83 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 4:629712865107 84 }
spastor 0:fcaad0dfa051 85
spastor 0:fcaad0dfa051 86 return atCmdResponse;
spastor 0:fcaad0dfa051 87 }
spastor 0:fcaad0dfa051 88
spastor 0:fcaad0dfa051 89 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint16_t *data)
spastor 0:fcaad0dfa051 90 {
spastor 0:fcaad0dfa051 91 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 92 AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len);
spastor 0:fcaad0dfa051 93
spastor 4:629712865107 94 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
spastor 0:fcaad0dfa051 95 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 4:629712865107 96 }
spastor 0:fcaad0dfa051 97
spastor 0:fcaad0dfa051 98 return atCmdResponse;
spastor 0:fcaad0dfa051 99 }
spastor 0:fcaad0dfa051 100
spastor 0:fcaad0dfa051 101 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint32_t *data)
spastor 0:fcaad0dfa051 102 {
spastor 0:fcaad0dfa051 103 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 104 AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len);
spastor 0:fcaad0dfa051 105
spastor 4:629712865107 106 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
spastor 0:fcaad0dfa051 107 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 4:629712865107 108 }
spastor 0:fcaad0dfa051 109
spastor 0:fcaad0dfa051 110 return atCmdResponse;
spastor 0:fcaad0dfa051 111 }
spastor 0:fcaad0dfa051 112
spastor 0:fcaad0dfa051 113 AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint32_t * const data)
spastor 0:fcaad0dfa051 114 {
spastor 0:fcaad0dfa051 115 uint16_t len = sizeof *data;
spastor 0:fcaad0dfa051 116 AtCmdFrame cmd_frame = AtCmdFrame(param);
spastor 0:fcaad0dfa051 117
spastor 0:fcaad0dfa051 118 *data = 0; /* Set to zero, send_at_cmd() only writes the necessary bytes, so if only 1 is written all the remaining 3 should be 0. */
spastor 0:fcaad0dfa051 119 AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len);
spastor 0:fcaad0dfa051 120
spastor 4:629712865107 121 if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) {
spastor 0:fcaad0dfa051 122 atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
spastor 4:629712865107 123 }
spastor 0:fcaad0dfa051 124
spastor 0:fcaad0dfa051 125 return atCmdResponse;
spastor 0:fcaad0dfa051 126 }
spastor 0:fcaad0dfa051 127
spastor 0:fcaad0dfa051 128 AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, uint32_t data)
spastor 0:fcaad0dfa051 129 {
spastor 0:fcaad0dfa051 130 AtCmdFrame cmd_frame = AtCmdFrame(param, data);
spastor 0:fcaad0dfa051 131 return send_at_cmd(&cmd_frame, NULL, NULL);
spastor 0:fcaad0dfa051 132 }
spastor 0:fcaad0dfa051 133
spastor 0:fcaad0dfa051 134 AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, const uint8_t * data, uint16_t len)
spastor 0:fcaad0dfa051 135 {
spastor 0:fcaad0dfa051 136 AtCmdFrame cmd_frame = AtCmdFrame(param, data, len);
spastor 0:fcaad0dfa051 137 return send_at_cmd(&cmd_frame, NULL, NULL);
spastor 0:fcaad0dfa051 138 }
spastor 0:fcaad0dfa051 139
spastor 0:fcaad0dfa051 140 AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint8_t * const data, uint16_t * const len)
spastor 0:fcaad0dfa051 141 {
spastor 0:fcaad0dfa051 142 AtCmdFrame cmd_frame = AtCmdFrame(param);
spastor 0:fcaad0dfa051 143 return send_at_cmd(&cmd_frame, data, len, RadioLocal, false);
spastor 0:fcaad0dfa051 144 }