Fork of my original MQTTGateway

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AtCommands.cpp Source File

AtCommands.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "XBeeLib.h"
00014 
00015 #define GET_CMD_RESP(fr, radio_location)    (radio_location == RadioRemote ? fr->get_data_at(REM_AT_CMD_RESP_STATUS_OFFSET) \
00016                                                                    : fr->get_data_at(ATCMD_RESP_STATUS_OFFSET))
00017 
00018 #define GET_DATA_LEN(fr, radio_location)    (radio_location == RadioRemote ? (fr->get_data_len() - REM_AT_CMD_RESP_OVERHEAD) \
00019                                                                    : (fr->get_data_len() - ATCMD_RESP_OVERHEAD))
00020 
00021 #define GET_DATA_OFF(radio_location)        (radio_location == RadioRemote ? REM_AT_CMD_RESP_CMD_DATA_OFFSET \
00022                                                                    : ATCMD_RESP_DATA_OFFSET)
00023 
00024 using namespace XBeeLib;
00025 
00026 /** Method that sends an AT command to the module and waits for the command response.
00027  *  @returns the AT command response */
00028 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame,
00029      uint8_t *const buf, uint16_t *const len, RadioLocation radio_location, bool reverse)
00030 {
00031     AtCmdFrame::AtCmdResp resp = AtCmdFrame::AtCmdRespTimeout;
00032     ApiFrame *resp_frame;
00033     ApiFrame::ApiFrameType expected_type =
00034             (frame->get_frame_type() == ApiFrame::AtCmd) ?
00035             ApiFrame::AtCmdResp : ApiFrame::RemoteCmdResp;
00036 
00037     send_api_frame(frame);
00038 
00039     /* Wait for the AT command response packet */
00040     resp_frame = get_this_api_frame(frame->get_frame_id(), expected_type);
00041     if (resp_frame == NULL) {
00042         return resp;
00043     }
00044 
00045     resp = (AtCmdFrame::AtCmdResp)GET_CMD_RESP(resp_frame, radio_location);
00046     if (resp == AtCmdFrame::AtCmdRespOk) {
00047         if (buf != NULL && len != NULL) {
00048 
00049             /* Copy the command response data */
00050             uint16_t new_len = GET_DATA_LEN(resp_frame, radio_location);
00051 
00052             *len = (*len < new_len) ? *len : new_len;
00053 
00054             /* rmemcpy makes the endian change */
00055             if (reverse) {
00056                 rmemcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len);
00057             } else {
00058                 memcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len);
00059             }
00060         }
00061     } else {
00062         digi_log(LogLevelWarning, "send_at_cmd bad response: 0x%x\r\n", resp);
00063     }
00064 
00065     /* Once processed, remove the frame from the buffer */
00066     _framebuf_syncr.free_frame(resp_frame);
00067     return resp;
00068 }
00069 
00070 /** Method that sends an AT command to the module and waits for the command response.
00071  *  @returns the AT command response */
00072 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame)
00073 {
00074     return send_at_cmd(frame, NULL, NULL);
00075 }
00076 
00077 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint8_t *data)
00078 {
00079     uint16_t len = sizeof *data;
00080     AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, data, &len);
00081 
00082     if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
00083         atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
00084     }
00085 
00086     return atCmdResponse;
00087 }
00088 
00089 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint16_t *data)
00090 {
00091     uint16_t len = sizeof *data;
00092     AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len);
00093 
00094     if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
00095         atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
00096     }
00097 
00098     return atCmdResponse;
00099 }
00100 
00101 AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint32_t *data)
00102 {
00103     uint16_t len = sizeof *data;
00104     AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len);
00105 
00106     if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) {
00107         atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
00108     }
00109 
00110     return atCmdResponse;
00111 }
00112 
00113 AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint32_t * const data)
00114 {
00115     uint16_t len = sizeof *data;
00116     AtCmdFrame cmd_frame = AtCmdFrame(param);
00117 
00118     *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. */
00119     AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len);
00120 
00121     if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) {
00122         atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch;
00123     }
00124 
00125     return atCmdResponse;
00126 }
00127 
00128 AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, uint32_t data)
00129 {
00130     AtCmdFrame cmd_frame = AtCmdFrame(param, data);
00131     return send_at_cmd(&cmd_frame, NULL, NULL);
00132 }
00133 
00134 AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, const uint8_t * data, uint16_t len)
00135 {
00136     AtCmdFrame cmd_frame = AtCmdFrame(param, data, len);
00137     return send_at_cmd(&cmd_frame, NULL, NULL);
00138 }
00139 
00140 AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint8_t * const data, uint16_t * const len)
00141 {
00142     AtCmdFrame cmd_frame = AtCmdFrame(param);
00143     return send_at_cmd(&cmd_frame, data, len, RadioLocal, false);
00144 }