Library to easily communicate with XBee modules.

Fork of XBeeLib by Digi International Inc.

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?

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