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