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