Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 "mbed.h"
vpcola 0:f1d3878b8dd9 14 #include "Utils/Debug.h"
vpcola 0:f1d3878b8dd9 15 #include "AtCmdFrame.h"
vpcola 0:f1d3878b8dd9 16
vpcola 0:f1d3878b8dd9 17 #define AT_CMD_LEN 2
vpcola 0:f1d3878b8dd9 18 #define AT_CMD_ID_LEN 1
vpcola 0:f1d3878b8dd9 19
vpcola 0:f1d3878b8dd9 20 void AtCmdFrame::build_at_cmd_frame(const char *cmd, const uint8_t *cmd_params, uint8_t payload_len, bool reverse)
vpcola 0:f1d3878b8dd9 21 {
vpcola 0:f1d3878b8dd9 22 uint8_t frame_data[AT_CMD_LEN + AT_CMD_ID_LEN + payload_len];
vpcola 0:f1d3878b8dd9 23
vpcola 0:f1d3878b8dd9 24 frame_data[0] = _frame_id;
vpcola 0:f1d3878b8dd9 25 frame_data[1] = cmd[0];
vpcola 0:f1d3878b8dd9 26 frame_data[2] = cmd[1];
vpcola 0:f1d3878b8dd9 27 if (payload_len) {
vpcola 0:f1d3878b8dd9 28 if (reverse) {
vpcola 0:f1d3878b8dd9 29 rmemcpy(&frame_data[3], cmd_params, payload_len);
vpcola 0:f1d3878b8dd9 30 } else {
vpcola 0:f1d3878b8dd9 31 memcpy(&frame_data[3], cmd_params, payload_len);
vpcola 0:f1d3878b8dd9 32 }
vpcola 0:f1d3878b8dd9 33 }
vpcola 0:f1d3878b8dd9 34
vpcola 0:f1d3878b8dd9 35 set_api_frame(AtCmd, frame_data, AT_CMD_LEN + AT_CMD_ID_LEN + payload_len);
vpcola 0:f1d3878b8dd9 36 }
vpcola 0:f1d3878b8dd9 37
vpcola 0:f1d3878b8dd9 38 AtCmdFrame::AtCmdFrame(const char * const cmd, uint32_t cmd_param)
vpcola 0:f1d3878b8dd9 39 {
vpcola 0:f1d3878b8dd9 40 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 41 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 42
vpcola 0:f1d3878b8dd9 43 uint8_t len;
vpcola 0:f1d3878b8dd9 44 if (cmd_param <= 0xFF) {
vpcola 0:f1d3878b8dd9 45 len = 1;
vpcola 0:f1d3878b8dd9 46 } else if (cmd_param <= 0xFFFF) {
vpcola 0:f1d3878b8dd9 47 len = 2;
vpcola 0:f1d3878b8dd9 48 } else if (cmd_param <= 0xFFFFFF) {
vpcola 0:f1d3878b8dd9 49 len = 3;
vpcola 0:f1d3878b8dd9 50 } else {
vpcola 0:f1d3878b8dd9 51 len = 4;
vpcola 0:f1d3878b8dd9 52 }
vpcola 0:f1d3878b8dd9 53 build_at_cmd_frame(cmd, (uint8_t *)&cmd_param, len);
vpcola 0:f1d3878b8dd9 54 }
vpcola 0:f1d3878b8dd9 55
vpcola 0:f1d3878b8dd9 56 AtCmdFrame::AtCmdFrame(const char * const cmd, const uint8_t * cmd_param, uint16_t param_len)
vpcola 0:f1d3878b8dd9 57 {
vpcola 0:f1d3878b8dd9 58 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 59 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 60
vpcola 0:f1d3878b8dd9 61 build_at_cmd_frame(cmd, cmd_param, param_len, false);
vpcola 0:f1d3878b8dd9 62 }
vpcola 0:f1d3878b8dd9 63
vpcola 0:f1d3878b8dd9 64 AtCmdFrame::AtCmdFrame(uint64_t remote, const char * const cmd, uint32_t cmd_param)
vpcola 0:f1d3878b8dd9 65 {
vpcola 0:f1d3878b8dd9 66 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 67 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 68
vpcola 0:f1d3878b8dd9 69 build_at_cmd_remote_frame(remote, ADDR16_UNKNOWN, cmd, (uint8_t *)&cmd_param, 4);
vpcola 0:f1d3878b8dd9 70 }
vpcola 0:f1d3878b8dd9 71
vpcola 0:f1d3878b8dd9 72 AtCmdFrame::AtCmdFrame(uint64_t remote, const char * const cmd, const uint8_t * cmd_param, uint16_t param_len)
vpcola 0:f1d3878b8dd9 73 {
vpcola 0:f1d3878b8dd9 74 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 75 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 76
vpcola 0:f1d3878b8dd9 77 build_at_cmd_remote_frame(remote, ADDR16_UNKNOWN, cmd, cmd_param, param_len, false);
vpcola 0:f1d3878b8dd9 78 }
vpcola 0:f1d3878b8dd9 79
vpcola 0:f1d3878b8dd9 80 AtCmdFrame::AtCmdFrame(uint16_t remote, const char * const cmd, uint32_t cmd_param)
vpcola 0:f1d3878b8dd9 81 {
vpcola 0:f1d3878b8dd9 82 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 83 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 84
vpcola 0:f1d3878b8dd9 85 build_at_cmd_remote_frame(ADDR64_UNASSIGNED, remote, cmd, (uint8_t *)&cmd_param, 4);
vpcola 0:f1d3878b8dd9 86 }
vpcola 0:f1d3878b8dd9 87
vpcola 0:f1d3878b8dd9 88 AtCmdFrame::AtCmdFrame(uint16_t remote, const char * const cmd, const uint8_t * cmd_param, uint16_t param_len)
vpcola 0:f1d3878b8dd9 89 {
vpcola 0:f1d3878b8dd9 90 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 91 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 92
vpcola 0:f1d3878b8dd9 93 build_at_cmd_remote_frame(ADDR64_UNASSIGNED, remote, cmd, cmd_param, param_len, false);
vpcola 0:f1d3878b8dd9 94 }
vpcola 0:f1d3878b8dd9 95
vpcola 0:f1d3878b8dd9 96 AtCmdFrame::AtCmdFrame(uint64_t remote64, uint16_t remote16, const char * const cmd, uint32_t cmd_param)
vpcola 0:f1d3878b8dd9 97 {
vpcola 0:f1d3878b8dd9 98 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 99 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 100
vpcola 0:f1d3878b8dd9 101 build_at_cmd_remote_frame(remote64, remote16, cmd, (uint8_t *)&cmd_param, 4);
vpcola 0:f1d3878b8dd9 102 }
vpcola 0:f1d3878b8dd9 103
vpcola 0:f1d3878b8dd9 104 AtCmdFrame::AtCmdFrame(uint64_t remote64, uint16_t remote16, const char * const cmd,
vpcola 0:f1d3878b8dd9 105 const uint8_t * cmd_param, uint16_t param_len)
vpcola 0:f1d3878b8dd9 106 {
vpcola 0:f1d3878b8dd9 107 assert(cmd != NULL);
vpcola 0:f1d3878b8dd9 108 assert(strlen(cmd) == AT_CMD_LEN);
vpcola 0:f1d3878b8dd9 109
vpcola 0:f1d3878b8dd9 110 build_at_cmd_remote_frame(remote64, remote16, cmd, cmd_param, param_len, false);
vpcola 0:f1d3878b8dd9 111 }
vpcola 0:f1d3878b8dd9 112
vpcola 0:f1d3878b8dd9 113 #define FRAME_ID_LEN 1
vpcola 0:f1d3878b8dd9 114 #define ADDR64_LEN 8
vpcola 0:f1d3878b8dd9 115 #define ADDR16_LEN 2
vpcola 0:f1d3878b8dd9 116 #define OPTIONS_LEN 1
vpcola 0:f1d3878b8dd9 117 #define AT_CMD_LEN 2
vpcola 0:f1d3878b8dd9 118 #define REM_AT_CMD_OVERHEAD (FRAME_ID_LEN + ADDR64_LEN + \
vpcola 0:f1d3878b8dd9 119 ADDR16_LEN + OPTIONS_LEN + \
vpcola 0:f1d3878b8dd9 120 AT_CMD_LEN)
vpcola 0:f1d3878b8dd9 121
vpcola 0:f1d3878b8dd9 122 void AtCmdFrame::build_at_cmd_remote_frame(uint64_t remote64, uint16_t remote16,
vpcola 0:f1d3878b8dd9 123 const char *const cmd, const uint8_t *const cmd_params, uint8_t params_len, bool reverse)
vpcola 0:f1d3878b8dd9 124 {
vpcola 0:f1d3878b8dd9 125 uint8_t frame_data[REM_AT_CMD_OVERHEAD + params_len];
vpcola 0:f1d3878b8dd9 126
vpcola 0:f1d3878b8dd9 127 /* copy the frame id, the 64bit remote address, the 16bit network address,
vpcola 0:f1d3878b8dd9 128 * the options byte, the command and the command params */
vpcola 0:f1d3878b8dd9 129
vpcola 0:f1d3878b8dd9 130 frame_data[0] = _frame_id;
vpcola 0:f1d3878b8dd9 131 rmemcpy(&frame_data[1], (const uint8_t *)&remote64, sizeof remote64);
vpcola 0:f1d3878b8dd9 132 frame_data[9] = (uint8_t)(remote16 >> 8);
vpcola 0:f1d3878b8dd9 133 frame_data[10] = (uint8_t)remote16;
vpcola 0:f1d3878b8dd9 134 frame_data[11] = 0x02; /* TODO Options */
vpcola 0:f1d3878b8dd9 135 frame_data[12] = cmd[0];
vpcola 0:f1d3878b8dd9 136 frame_data[13] = cmd[1];
vpcola 0:f1d3878b8dd9 137
vpcola 0:f1d3878b8dd9 138 if (params_len) {
vpcola 0:f1d3878b8dd9 139 if (reverse) {
vpcola 0:f1d3878b8dd9 140 rmemcpy(&frame_data[14], cmd_params, params_len);
vpcola 0:f1d3878b8dd9 141 } else {
vpcola 0:f1d3878b8dd9 142 memcpy(&frame_data[14], cmd_params, params_len);
vpcola 0:f1d3878b8dd9 143 }
vpcola 0:f1d3878b8dd9 144 }
vpcola 0:f1d3878b8dd9 145
vpcola 0:f1d3878b8dd9 146 set_api_frame(RemoteCmdReq, frame_data, REM_AT_CMD_OVERHEAD + params_len);
vpcola 0:f1d3878b8dd9 147 }