project de marde si tu touch le no-homo s'applique pas
Fork of XBeeLib by
XBee/AtCommands.cpp@0:fcaad0dfa051, 2015-05-08 (annotated)
- Committer:
- spastor
- Date:
- Fri May 08 11:50:56 2015 +0200
- Revision:
- 0:fcaad0dfa051
- Child:
- 4:629712865107
Automatic upload
Who changed what in which revision?
User | Revision | Line number | New 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 | |
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 | 0:fcaad0dfa051 | 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 | 0:fcaad0dfa051 | 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 | 0:fcaad0dfa051 | 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 | 0:fcaad0dfa051 | 41 | if (resp_frame == NULL) |
spastor | 0:fcaad0dfa051 | 42 | return resp; |
spastor | 0:fcaad0dfa051 | 43 | |
spastor | 0:fcaad0dfa051 | 44 | resp = (AtCmdFrame::AtCmdResp)GET_CMD_RESP(resp_frame, radio_location); |
spastor | 0:fcaad0dfa051 | 45 | if (resp == AtCmdFrame::AtCmdRespOk) { |
spastor | 0:fcaad0dfa051 | 46 | if (buf != NULL && len != NULL) { |
spastor | 0:fcaad0dfa051 | 47 | |
spastor | 0:fcaad0dfa051 | 48 | /* Copy the command response data */ |
spastor | 0:fcaad0dfa051 | 49 | uint16_t new_len = GET_DATA_LEN(resp_frame, radio_location); |
spastor | 0:fcaad0dfa051 | 50 | |
spastor | 0:fcaad0dfa051 | 51 | *len = (*len < new_len) ? *len : new_len; |
spastor | 0:fcaad0dfa051 | 52 | |
spastor | 0:fcaad0dfa051 | 53 | /* rmemcpy makes the endian change */ |
spastor | 0:fcaad0dfa051 | 54 | if (reverse) { |
spastor | 0:fcaad0dfa051 | 55 | rmemcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len); |
spastor | 0:fcaad0dfa051 | 56 | } else { |
spastor | 0:fcaad0dfa051 | 57 | memcpy(buf, resp_frame->get_data() + GET_DATA_OFF(radio_location), *len); |
spastor | 0:fcaad0dfa051 | 58 | } |
spastor | 0:fcaad0dfa051 | 59 | } |
spastor | 0:fcaad0dfa051 | 60 | } else { |
spastor | 0:fcaad0dfa051 | 61 | digi_log(LogLevelWarning, "send_at_cmd bad response: 0x%x\r\n", resp); |
spastor | 0:fcaad0dfa051 | 62 | } |
spastor | 0:fcaad0dfa051 | 63 | |
spastor | 0:fcaad0dfa051 | 64 | /* Once processed, remove the frame from the buffer */ |
spastor | 0:fcaad0dfa051 | 65 | _framebuf.free_frame(resp_frame); |
spastor | 0:fcaad0dfa051 | 66 | return resp; |
spastor | 0:fcaad0dfa051 | 67 | } |
spastor | 0:fcaad0dfa051 | 68 | |
spastor | 0:fcaad0dfa051 | 69 | /** Method that sends an AT command to the module and waits for the command response. |
spastor | 0:fcaad0dfa051 | 70 | * @returns the AT command response */ |
spastor | 0:fcaad0dfa051 | 71 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame) |
spastor | 0:fcaad0dfa051 | 72 | { |
spastor | 0:fcaad0dfa051 | 73 | return send_at_cmd(frame, NULL, NULL); |
spastor | 0:fcaad0dfa051 | 74 | } |
spastor | 0:fcaad0dfa051 | 75 | |
spastor | 0:fcaad0dfa051 | 76 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint8_t *data) |
spastor | 0:fcaad0dfa051 | 77 | { |
spastor | 0:fcaad0dfa051 | 78 | uint16_t len = sizeof *data; |
spastor | 0:fcaad0dfa051 | 79 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, data, &len); |
spastor | 0:fcaad0dfa051 | 80 | |
spastor | 0:fcaad0dfa051 | 81 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) |
spastor | 0:fcaad0dfa051 | 82 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
spastor | 0:fcaad0dfa051 | 83 | |
spastor | 0:fcaad0dfa051 | 84 | return atCmdResponse; |
spastor | 0:fcaad0dfa051 | 85 | } |
spastor | 0:fcaad0dfa051 | 86 | |
spastor | 0:fcaad0dfa051 | 87 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint16_t *data) |
spastor | 0:fcaad0dfa051 | 88 | { |
spastor | 0:fcaad0dfa051 | 89 | uint16_t len = sizeof *data; |
spastor | 0:fcaad0dfa051 | 90 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len); |
spastor | 0:fcaad0dfa051 | 91 | |
spastor | 0:fcaad0dfa051 | 92 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) |
spastor | 0:fcaad0dfa051 | 93 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
spastor | 0:fcaad0dfa051 | 94 | |
spastor | 0:fcaad0dfa051 | 95 | return atCmdResponse; |
spastor | 0:fcaad0dfa051 | 96 | } |
spastor | 0:fcaad0dfa051 | 97 | |
spastor | 0:fcaad0dfa051 | 98 | AtCmdFrame::AtCmdResp XBee::send_at_cmd(AtCmdFrame *frame, uint32_t *data) |
spastor | 0:fcaad0dfa051 | 99 | { |
spastor | 0:fcaad0dfa051 | 100 | uint16_t len = sizeof *data; |
spastor | 0:fcaad0dfa051 | 101 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(frame, (uint8_t *)data, &len); |
spastor | 0:fcaad0dfa051 | 102 | |
spastor | 0:fcaad0dfa051 | 103 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len != sizeof *data) |
spastor | 0:fcaad0dfa051 | 104 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
spastor | 0:fcaad0dfa051 | 105 | |
spastor | 0:fcaad0dfa051 | 106 | return atCmdResponse; |
spastor | 0:fcaad0dfa051 | 107 | } |
spastor | 0:fcaad0dfa051 | 108 | |
spastor | 0:fcaad0dfa051 | 109 | AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint32_t * const data) |
spastor | 0:fcaad0dfa051 | 110 | { |
spastor | 0:fcaad0dfa051 | 111 | uint16_t len = sizeof *data; |
spastor | 0:fcaad0dfa051 | 112 | AtCmdFrame cmd_frame = AtCmdFrame(param); |
spastor | 0:fcaad0dfa051 | 113 | |
spastor | 0:fcaad0dfa051 | 114 | *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 | 115 | AtCmdFrame::AtCmdResp atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len); |
spastor | 0:fcaad0dfa051 | 116 | |
spastor | 0:fcaad0dfa051 | 117 | if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) |
spastor | 0:fcaad0dfa051 | 118 | atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; |
spastor | 0:fcaad0dfa051 | 119 | |
spastor | 0:fcaad0dfa051 | 120 | return atCmdResponse; |
spastor | 0:fcaad0dfa051 | 121 | } |
spastor | 0:fcaad0dfa051 | 122 | |
spastor | 0:fcaad0dfa051 | 123 | AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, uint32_t data) |
spastor | 0:fcaad0dfa051 | 124 | { |
spastor | 0:fcaad0dfa051 | 125 | AtCmdFrame cmd_frame = AtCmdFrame(param, data); |
spastor | 0:fcaad0dfa051 | 126 | return send_at_cmd(&cmd_frame, NULL, NULL); |
spastor | 0:fcaad0dfa051 | 127 | } |
spastor | 0:fcaad0dfa051 | 128 | |
spastor | 0:fcaad0dfa051 | 129 | AtCmdFrame::AtCmdResp XBee::set_param(const char * const param, const uint8_t * data, uint16_t len) |
spastor | 0:fcaad0dfa051 | 130 | { |
spastor | 0:fcaad0dfa051 | 131 | AtCmdFrame cmd_frame = AtCmdFrame(param, data, len); |
spastor | 0:fcaad0dfa051 | 132 | return send_at_cmd(&cmd_frame, NULL, NULL); |
spastor | 0:fcaad0dfa051 | 133 | } |
spastor | 0:fcaad0dfa051 | 134 | |
spastor | 0:fcaad0dfa051 | 135 | AtCmdFrame::AtCmdResp XBee::get_param(const char * const param, uint8_t * const data, uint16_t * const len) |
spastor | 0:fcaad0dfa051 | 136 | { |
spastor | 0:fcaad0dfa051 | 137 | AtCmdFrame cmd_frame = AtCmdFrame(param); |
spastor | 0:fcaad0dfa051 | 138 | return send_at_cmd(&cmd_frame, data, len, RadioLocal, false); |
spastor | 0:fcaad0dfa051 | 139 | } |