Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of XBeeLib by
AtCmdFrame.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 "mbed.h" 00014 #include "Utils/Debug.h" 00015 #include "AtCmdFrame.h" 00016 00017 #define AT_CMD_LEN 2 00018 #define AT_CMD_ID_LEN 1 00019 00020 void AtCmdFrame::build_at_cmd_frame(const char *cmd, const uint8_t *cmd_params, uint8_t payload_len, bool reverse) 00021 { 00022 uint8_t frame_data[AT_CMD_LEN + AT_CMD_ID_LEN + payload_len]; 00023 00024 frame_data[0] = _frame_id; 00025 frame_data[1] = cmd[0]; 00026 frame_data[2] = cmd[1]; 00027 if (payload_len) { 00028 if (reverse) { 00029 rmemcpy(&frame_data[3], cmd_params, payload_len); 00030 } else { 00031 memcpy(&frame_data[3], cmd_params, payload_len); 00032 } 00033 } 00034 00035 set_api_frame(AtCmd, frame_data, AT_CMD_LEN + AT_CMD_ID_LEN + payload_len); 00036 } 00037 00038 AtCmdFrame::AtCmdFrame(const char * const cmd, uint32_t cmd_param) 00039 { 00040 assert(cmd != NULL); 00041 assert(strlen(cmd) == AT_CMD_LEN); 00042 00043 uint8_t len; 00044 if (cmd_param <= 0xFF) { 00045 len = 1; 00046 } else if (cmd_param <= 0xFFFF) { 00047 len = 2; 00048 } else if (cmd_param <= 0xFFFFFF) { 00049 len = 3; 00050 } else { 00051 len = 4; 00052 } 00053 build_at_cmd_frame(cmd, (uint8_t *)&cmd_param, len); 00054 } 00055 00056 AtCmdFrame::AtCmdFrame(const char * const cmd, const uint8_t * cmd_param, uint16_t param_len) 00057 { 00058 assert(cmd != NULL); 00059 assert(strlen(cmd) == AT_CMD_LEN); 00060 00061 build_at_cmd_frame(cmd, cmd_param, param_len, false); 00062 } 00063 00064 AtCmdFrame::AtCmdFrame(uint64_t remote, const char * const cmd, uint32_t cmd_param) 00065 { 00066 assert(cmd != NULL); 00067 assert(strlen(cmd) == AT_CMD_LEN); 00068 00069 build_at_cmd_remote_frame(remote, ADDR16_UNKNOWN, cmd, (uint8_t *)&cmd_param, 4); 00070 } 00071 00072 AtCmdFrame::AtCmdFrame(uint64_t remote, const char * const cmd, const uint8_t * cmd_param, uint16_t param_len) 00073 { 00074 assert(cmd != NULL); 00075 assert(strlen(cmd) == AT_CMD_LEN); 00076 00077 build_at_cmd_remote_frame(remote, ADDR16_UNKNOWN, cmd, cmd_param, param_len, false); 00078 } 00079 00080 AtCmdFrame::AtCmdFrame(uint16_t remote, const char * const cmd, uint32_t cmd_param) 00081 { 00082 assert(cmd != NULL); 00083 assert(strlen(cmd) == AT_CMD_LEN); 00084 00085 build_at_cmd_remote_frame(ADDR64_UNASSIGNED, remote, cmd, (uint8_t *)&cmd_param, 4); 00086 } 00087 00088 AtCmdFrame::AtCmdFrame(uint16_t remote, const char * const cmd, const uint8_t * cmd_param, uint16_t param_len) 00089 { 00090 assert(cmd != NULL); 00091 assert(strlen(cmd) == AT_CMD_LEN); 00092 00093 build_at_cmd_remote_frame(ADDR64_UNASSIGNED, remote, cmd, cmd_param, param_len, false); 00094 } 00095 00096 AtCmdFrame::AtCmdFrame(uint64_t remote64, uint16_t remote16, const char * const cmd, uint32_t cmd_param) 00097 { 00098 assert(cmd != NULL); 00099 assert(strlen(cmd) == AT_CMD_LEN); 00100 00101 build_at_cmd_remote_frame(remote64, remote16, cmd, (uint8_t *)&cmd_param, 4); 00102 } 00103 00104 AtCmdFrame::AtCmdFrame(uint64_t remote64, uint16_t remote16, const char * const cmd, 00105 const uint8_t * cmd_param, uint16_t param_len) 00106 { 00107 assert(cmd != NULL); 00108 assert(strlen(cmd) == AT_CMD_LEN); 00109 00110 build_at_cmd_remote_frame(remote64, remote16, cmd, cmd_param, param_len, false); 00111 } 00112 00113 #define FRAME_ID_LEN 1 00114 #define ADDR64_LEN 8 00115 #define ADDR16_LEN 2 00116 #define OPTIONS_LEN 1 00117 #define AT_CMD_LEN 2 00118 #define REM_AT_CMD_OVERHEAD (FRAME_ID_LEN + ADDR64_LEN + \ 00119 ADDR16_LEN + OPTIONS_LEN + \ 00120 AT_CMD_LEN) 00121 00122 void AtCmdFrame::build_at_cmd_remote_frame(uint64_t remote64, uint16_t remote16, 00123 const char *const cmd, const uint8_t *const cmd_params, uint8_t params_len, bool reverse) 00124 { 00125 uint8_t frame_data[REM_AT_CMD_OVERHEAD + params_len]; 00126 00127 /* copy the frame id, the 64bit remote address, the 16bit network address, 00128 * the options byte, the command and the command params */ 00129 00130 frame_data[0] = _frame_id; 00131 rmemcpy(&frame_data[1], (const uint8_t *)&remote64, sizeof remote64); 00132 frame_data[9] = (uint8_t)(remote16 >> 8); 00133 frame_data[10] = (uint8_t)remote16; 00134 frame_data[11] = 0x02; /* TODO Options */ 00135 frame_data[12] = cmd[0]; 00136 frame_data[13] = cmd[1]; 00137 00138 if (params_len) { 00139 if (reverse) { 00140 rmemcpy(&frame_data[14], cmd_params, params_len); 00141 } else { 00142 memcpy(&frame_data[14], cmd_params, params_len); 00143 } 00144 } 00145 00146 set_api_frame(RemoteCmdReq, frame_data, REM_AT_CMD_OVERHEAD + params_len); 00147 }
Generated on Tue Jul 12 2022 20:40:23 by
