Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
XbeeMonitor/XBeeLib/XBee/AtCommands.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | /** |
vpcola | 0:f1d3878b8dd9 | 2 | * Copyright (c) 2015 Digi International Inc., |
vpcola | 0:f1d3878b8dd9 | 3 | * All rights not expressly granted are reserved. |
vpcola | 0:f1d3878b8dd9 | 4 | * |
vpcola | 0:f1d3878b8dd9 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
vpcola | 0:f1d3878b8dd9 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
vpcola | 0:f1d3878b8dd9 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
vpcola | 0:f1d3878b8dd9 | 8 | * |
vpcola | 0:f1d3878b8dd9 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
vpcola | 0:f1d3878b8dd9 | 10 | * ======================================================================= |
vpcola | 0:f1d3878b8dd9 | 11 | */ |
vpcola | 0:f1d3878b8dd9 | 12 | |
vpcola | 0:f1d3878b8dd9 | 13 | #include "XBeeLib.h" |
vpcola | 0:f1d3878b8dd9 | 14 | |
vpcola | 0:f1d3878b8dd9 | 15 | #define GET_CMD_RESP(fr, radio_location) (radio_location == RadioRemote ? fr->get_data_at(REM_AT_CMD_RESP_STATUS_OFFSET) \ |
vpcola | 0:f1d3878b8dd9 | 16 | : fr->get_data_at(ATCMD_RESP_STATUS_OFFSET)) |
vpcola | 0:f1d3878b8dd9 | 17 | |
vpcola | 0:f1d3878b8dd9 | 18 | #define GET_DATA_LEN(fr, radio_location) (radio_location == RadioRemote ? (fr->get_data_len() - REM_AT_CMD_RESP_OVERHEAD) \ |
vpcola | 0:f1d3878b8dd9 | 19 | : (fr->get_data_len() - ATCMD_RESP_OVERHEAD)) |
vpcola | 0:f1d3878b8dd9 | 20 | |
vpcola | 0:f1d3878b8dd9 | 21 | #define GET_DATA_OFF(radio_location) (radio_location == RadioRemote ? REM_AT_CMD_RESP_CMD_DATA_OFFSET \ |
vpcola | 0:f1d3878b8dd9 | 22 | : ATCMD_RESP_DATA_OFFSET) |
vpcola | 0:f1d3878b8dd9 | 23 | |
vpcola | 0:f1d3878b8dd9 | 24 | using namespace XBeeLib; |
vpcola | 0:f1d3878b8dd9 | 25 | |
vpcola | 0:f1d3878b8dd9 | 26 | /** Method that sends an AT command to the module and waits for the command response. |
vpcola | 0:f1d3878b8dd9 | 27 | * @returns the AT command response */ |
vpcola | 0:f1d3878b8dd9 | 28 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, |
vpcola | 0:f1d3878b8dd9 | 29 | uint8_t *const buf, uint16_t *const len, RadioLocation radio_location, bool reverse) |
vpcola | 0:f1d3878b8dd9 | 30 | { |
vpcola | 0:f1d3878b8dd9 | 31 | AtCmdFrame::AtCmdResp resp = AtCmdFrame::AtCmdRespTimeout; |
vpcola | 0:f1d3878b8dd9 | 32 | ApiFrame *resp_frame; |
vpcola | 0:f1d3878b8dd9 | 33 | ApiFrame::ApiFrameType expected_type = |
vpcola | 0:f1d3878b8dd9 | 34 | (frame->get_frame_type() == ApiFrame::AtCmd) ? |
vpcola | 0:f1d3878b8dd9 | 35 | ApiFrame::AtCmdResp : ApiFrame::RemoteCmdResp; |
vpcola | 0:f1d3878b8dd9 | 36 | |
vpcola | 0:f1d3878b8dd9 | 37 | send_api_frame(frame); |
vpcola | 0:f1d3878b8dd9 | 38 | |
vpcola | 0:f1d3878b8dd9 | 39 | /* Wait for the AT command response packet */ |
vpcola | 0:f1d3878b8dd9 | 40 | resp_frame = get_this_api_frame(frame->get_frame_id(), expected_type); |
vpcola | 0:f1d3878b8dd9 | 41 | if (resp_frame == NULL) { |
vpcola | 0:f1d3878b8dd9 | 42 | return resp; |
vpcola | 0:f1d3878b8dd9 | 43 | } |
vpcola | 0:f1d3878b8dd9 | 44 | |
vpcola | 0:f1d3878b8dd9 | 45 | resp = (AtCmdFrame::AtCmdResp)GET_CMD_RESP(resp_frame, radio_location); |
vpcola | 0:f1d3878b8dd9 | 46 | if (resp == AtCmdFrame::AtCmdRespOk) { |
vpcola | 0:f1d3878b8dd9 | 47 | if (buf != NULL && len != NULL) { |
vpcola | 0:f1d3878b8dd9 | 48 | |
vpcola | 0:f1d3878b8dd9 | 49 | /* Copy the command response data */ |
vpcola | 0:f1d3878b8dd9 | 50 | uint16_t new_len = GET_DATA_LEN(resp_frame, radio_location); |
vpcola | 0:f1d3878b8dd9 | 51 | |
vpcola | 0:f1d3878b8dd9 | 52 | *len = (*len < new_len) ? *len : new_len; |
vpcola | 0:f1d3878b8dd9 | 53 | |
vpcola | 0:f1d3878b8dd9 | 54 | /* rmemcpy makes the endian change */ |
vpcola | 0:f1d3878b8dd9 | 55 | if (reverse) { |
vpcola | 0:f1d3878b8dd9 | 56 | rmemcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len); |
vpcola | 0:f1d3878b8dd9 | 57 | } else { |
vpcola | 0:f1d3878b8dd9 | 58 | memcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len); |
vpcola | 0:f1d3878b8dd9 | 59 | } |
vpcola | 0:f1d3878b8dd9 | 60 | } |
vpcola | 0:f1d3878b8dd9 | 61 | } else { |
vpcola | 0:f1d3878b8dd9 | 62 | digi_log(LogLevelWarning, "send_at_cmd bad response: 0x%x\r\n", resp); |
vpcola | 0:f1d3878b8dd9 | 63 | } |
vpcola | 0:f1d3878b8dd9 | 64 | |
vpcola | 0:f1d3878b8dd9 | 65 | /* Once processed, remove the frame from the buffer */ |
vpcola | 0:f1d3878b8dd9 | 66 | _framebuf_syncr.free_frame(resp_frame); |
vpcola | 0:f1d3878b8dd9 | 67 | return resp; |
vpcola | 0:f1d3878b8dd9 | 68 | } |
vpcola | 0:f1d3878b8dd9 | 69 | |
vpcola | 0:f1d3878b8dd9 | 70 | /** Method that sends an AT command to the module and waits for the command response. |
vpcola | 0:f1d3878b8dd9 | 71 | * @returns the AT command response */ |
vpcola | 0:f1d3878b8dd9 | 72 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame) |
vpcola | 0:f1d3878b8dd9 | 73 | { |
vpcola | 0:f1d3878b8dd9 | 74 | return send_at_cmd(frame, NULL, NULL); |
vpcola | 0:f1d3878b8dd9 | 75 | } |
vpcola | 0:f1d3878b8dd9 | 76 | |
vpcola | 0:f1d3878b8dd9 | 77 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint8_t *data) |
vpcola | 0:f1d3878b8dd9 | 78 | { |
vpcola | 0:f1d3878b8dd9 | 79 | uint16_t len = sizeof *data; |
vpcola | 0:f1d3878b8dd9 | 80 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, data, &len); |
vpcola | 0:f1d3878b8dd9 | 81 | |
vpcola | 0:f1d3878b8dd9 | 82 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) { |
vpcola | 0:f1d3878b8dd9 | 83 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
vpcola | 0:f1d3878b8dd9 | 84 | } |
vpcola | 0:f1d3878b8dd9 | 85 | |
vpcola | 0:f1d3878b8dd9 | 86 | return atCmdResponse; |
vpcola | 0:f1d3878b8dd9 | 87 | } |
vpcola | 0:f1d3878b8dd9 | 88 | |
vpcola | 0:f1d3878b8dd9 | 89 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint16_t *data) |
vpcola | 0:f1d3878b8dd9 | 90 | { |
vpcola | 0:f1d3878b8dd9 | 91 | uint16_t len = sizeof *data; |
vpcola | 0:f1d3878b8dd9 | 92 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len); |
vpcola | 0:f1d3878b8dd9 | 93 | |
vpcola | 0:f1d3878b8dd9 | 94 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) { |
vpcola | 0:f1d3878b8dd9 | 95 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
vpcola | 0:f1d3878b8dd9 | 96 | } |
vpcola | 0:f1d3878b8dd9 | 97 | |
vpcola | 0:f1d3878b8dd9 | 98 | return atCmdResponse; |
vpcola | 0:f1d3878b8dd9 | 99 | } |
vpcola | 0:f1d3878b8dd9 | 100 | |
vpcola | 0:f1d3878b8dd9 | 101 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint32_t *data) |
vpcola | 0:f1d3878b8dd9 | 102 | { |
vpcola | 0:f1d3878b8dd9 | 103 | uint16_t len = sizeof *data; |
vpcola | 0:f1d3878b8dd9 | 104 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len); |
vpcola | 0:f1d3878b8dd9 | 105 | |
vpcola | 0:f1d3878b8dd9 | 106 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) { |
vpcola | 0:f1d3878b8dd9 | 107 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
vpcola | 0:f1d3878b8dd9 | 108 | } |
vpcola | 0:f1d3878b8dd9 | 109 | |
vpcola | 0:f1d3878b8dd9 | 110 | return atCmdResponse; |
vpcola | 0:f1d3878b8dd9 | 111 | } |
vpcola | 0:f1d3878b8dd9 | 112 | |
vpcola | 0:f1d3878b8dd9 | 113 | AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint32_t * const data) |
vpcola | 0:f1d3878b8dd9 | 114 | { |
vpcola | 0:f1d3878b8dd9 | 115 | uint16_t len = sizeof *data; |
vpcola | 0:f1d3878b8dd9 | 116 | AtCmdFrame cmd_frame = AtCmdFrame(param); |
vpcola | 0:f1d3878b8dd9 | 117 | |
vpcola | 0:f1d3878b8dd9 | 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. */ |
vpcola | 0:f1d3878b8dd9 | 119 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len); |
vpcola | 0:f1d3878b8dd9 | 120 | |
vpcola | 0:f1d3878b8dd9 | 121 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) { |
vpcola | 0:f1d3878b8dd9 | 122 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
vpcola | 0:f1d3878b8dd9 | 123 | } |
vpcola | 0:f1d3878b8dd9 | 124 | |
vpcola | 0:f1d3878b8dd9 | 125 | return atCmdResponse; |
vpcola | 0:f1d3878b8dd9 | 126 | } |
vpcola | 0:f1d3878b8dd9 | 127 | |
vpcola | 0:f1d3878b8dd9 | 128 | AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, uint32_t data) |
vpcola | 0:f1d3878b8dd9 | 129 | { |
vpcola | 0:f1d3878b8dd9 | 130 | AtCmdFrame cmd_frame = AtCmdFrame(param, data); |
vpcola | 0:f1d3878b8dd9 | 131 | return send_at_cmd(&cmd_frame, NULL, NULL); |
vpcola | 0:f1d3878b8dd9 | 132 | } |
vpcola | 0:f1d3878b8dd9 | 133 | |
vpcola | 0:f1d3878b8dd9 | 134 | AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, const uint8_t * data, uint16_t len) |
vpcola | 0:f1d3878b8dd9 | 135 | { |
vpcola | 0:f1d3878b8dd9 | 136 | AtCmdFrame cmd_frame = AtCmdFrame(param, data, len); |
vpcola | 0:f1d3878b8dd9 | 137 | return send_at_cmd(&cmd_frame, NULL, NULL); |
vpcola | 0:f1d3878b8dd9 | 138 | } |
vpcola | 0:f1d3878b8dd9 | 139 | |
vpcola | 0:f1d3878b8dd9 | 140 | AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint8_t * const data, uint16_t * const len) |
vpcola | 0:f1d3878b8dd9 | 141 | { |
vpcola | 0:f1d3878b8dd9 | 142 | AtCmdFrame cmd_frame = AtCmdFrame(param); |
vpcola | 0:f1d3878b8dd9 | 143 | return send_at_cmd(&cmd_frame, data, len, RadioLocal, false); |
vpcola | 0:f1d3878b8dd9 | 144 | } |