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 "FrameHandlers/FH_AtCmdResp.h"
vpcola 0:f1d3878b8dd9 14 #include "Frames/ApiFrame.h"
vpcola 0:f1d3878b8dd9 15
vpcola 0:f1d3878b8dd9 16 using namespace XBeeLib;
vpcola 0:f1d3878b8dd9 17
vpcola 0:f1d3878b8dd9 18 /** Class constructor */
vpcola 0:f1d3878b8dd9 19 FH_AtCmdResp::FH_AtCmdResp() :
vpcola 0:f1d3878b8dd9 20 FrameHandler(ApiFrame::AtCmdResp), at_cmd_resp_cb(NULL)
vpcola 0:f1d3878b8dd9 21 {
vpcola 0:f1d3878b8dd9 22 }
vpcola 0:f1d3878b8dd9 23
vpcola 0:f1d3878b8dd9 24 FH_AtCmdResp::FH_AtCmdResp(ApiFrame::ApiFrameType type) :
vpcola 0:f1d3878b8dd9 25 FrameHandler(type), at_cmd_resp_cb(NULL)
vpcola 0:f1d3878b8dd9 26 {
vpcola 0:f1d3878b8dd9 27 }
vpcola 0:f1d3878b8dd9 28
vpcola 0:f1d3878b8dd9 29 /** Class destructor */
vpcola 0:f1d3878b8dd9 30 FH_AtCmdResp::~FH_AtCmdResp()
vpcola 0:f1d3878b8dd9 31 {
vpcola 0:f1d3878b8dd9 32 }
vpcola 0:f1d3878b8dd9 33
vpcola 0:f1d3878b8dd9 34 void FH_AtCmdResp::register_at_cmd_resp_cb(at_cmd_resp_cb_t function)
vpcola 0:f1d3878b8dd9 35 {
vpcola 0:f1d3878b8dd9 36 at_cmd_resp_cb = function;
vpcola 0:f1d3878b8dd9 37 }
vpcola 0:f1d3878b8dd9 38
vpcola 0:f1d3878b8dd9 39 void FH_AtCmdResp::unregister_at_cmd_resp_cb()
vpcola 0:f1d3878b8dd9 40 {
vpcola 0:f1d3878b8dd9 41 at_cmd_resp_cb = NULL;
vpcola 0:f1d3878b8dd9 42 }
vpcola 0:f1d3878b8dd9 43
vpcola 0:f1d3878b8dd9 44 void FH_AtCmdResp::process_frame_data(const ApiFrame * const frame)
vpcola 0:f1d3878b8dd9 45 {
vpcola 0:f1d3878b8dd9 46 /* The caller checks that the type matches, so no need to check it here again */
vpcola 0:f1d3878b8dd9 47
vpcola 0:f1d3878b8dd9 48 if (at_cmd_resp_cb == NULL) {
vpcola 0:f1d3878b8dd9 49 return;
vpcola 0:f1d3878b8dd9 50 }
vpcola 0:f1d3878b8dd9 51
vpcola 0:f1d3878b8dd9 52 at_cmd_resp_cb(frame->get_data(), frame->get_data_len());
vpcola 0:f1d3878b8dd9 53 }
vpcola 0:f1d3878b8dd9 54
vpcola 0:f1d3878b8dd9 55
vpcola 0:f1d3878b8dd9 56 /** Class constructor */
vpcola 0:f1d3878b8dd9 57 FH_NodeDiscoveryZB::FH_NodeDiscoveryZB() :
vpcola 0:f1d3878b8dd9 58 FH_AtCmdResp(ApiFrame::AtCmdResp), node_discovery_cb(NULL)
vpcola 0:f1d3878b8dd9 59 {
vpcola 0:f1d3878b8dd9 60 }
vpcola 0:f1d3878b8dd9 61
vpcola 0:f1d3878b8dd9 62 /** Class destructor */
vpcola 0:f1d3878b8dd9 63 FH_NodeDiscoveryZB::~FH_NodeDiscoveryZB()
vpcola 0:f1d3878b8dd9 64 {
vpcola 0:f1d3878b8dd9 65 }
vpcola 0:f1d3878b8dd9 66
vpcola 0:f1d3878b8dd9 67 void FH_NodeDiscoveryZB::register_node_discovery_cb(node_discovery_zb_cb_t function)
vpcola 0:f1d3878b8dd9 68 {
vpcola 0:f1d3878b8dd9 69 node_discovery_cb = function;
vpcola 0:f1d3878b8dd9 70 }
vpcola 0:f1d3878b8dd9 71
vpcola 0:f1d3878b8dd9 72 void FH_NodeDiscoveryZB::unregister_node_discovery_cb()
vpcola 0:f1d3878b8dd9 73 {
vpcola 0:f1d3878b8dd9 74 node_discovery_cb = NULL;
vpcola 0:f1d3878b8dd9 75 }
vpcola 0:f1d3878b8dd9 76
vpcola 0:f1d3878b8dd9 77
vpcola 0:f1d3878b8dd9 78 void FH_NodeDiscoveryZB::process_frame_data(const ApiFrame *const frame)
vpcola 0:f1d3878b8dd9 79 {
vpcola 0:f1d3878b8dd9 80 /* The caller checks that the type matches, so no need to check it here again */
vpcola 0:f1d3878b8dd9 81
vpcola 0:f1d3878b8dd9 82 if (node_discovery_cb == NULL) {
vpcola 0:f1d3878b8dd9 83 return;
vpcola 0:f1d3878b8dd9 84 }
vpcola 0:f1d3878b8dd9 85
vpcola 0:f1d3878b8dd9 86 if (frame->get_data_at(ATCMD_RESP_CMD_LOW_OFFSET) != 'N' ||
vpcola 0:f1d3878b8dd9 87 frame->get_data_at(ATCMD_RESP_CMD_HIGH_OFFSET) != 'D') {
vpcola 0:f1d3878b8dd9 88 return;
vpcola 0:f1d3878b8dd9 89 }
vpcola 0:f1d3878b8dd9 90
vpcola 0:f1d3878b8dd9 91 if (frame->get_data_at(ATCMD_RESP_STATUS_OFFSET) != AtCmdFrame::AtCmdRespOk) {
vpcola 0:f1d3878b8dd9 92 return;
vpcola 0:f1d3878b8dd9 93 }
vpcola 0:f1d3878b8dd9 94
vpcola 0:f1d3878b8dd9 95 const uint8_t * const data = frame->get_data(); /* The data payload we get here is the full AT command response payload, excluding the frameid. Keep that in mind for the offsets */
vpcola 0:f1d3878b8dd9 96 const uint16_t addr16 = UINT16(data[ATCMD_RESP_NW_ADDR_H_OFFSET], data[ATCMD_RESP_NW_ADDR_L_OFFSET]);
vpcola 0:f1d3878b8dd9 97 const uint64_t addr64 = addr64_from_uint8_t(&data[ATCMD_RESP_SH_ADDR_L_OFFSET]);
vpcola 0:f1d3878b8dd9 98 const char * const node_id = (const char *)&data[ATCMD_RESP_NI_OFFSET];
vpcola 0:f1d3878b8dd9 99 RemoteXBeeZB remote = RemoteXBeeZB(addr64, addr16);
vpcola 0:f1d3878b8dd9 100
vpcola 0:f1d3878b8dd9 101 node_discovery_cb(remote, node_id);
vpcola 0:f1d3878b8dd9 102 }
vpcola 0:f1d3878b8dd9 103
vpcola 0:f1d3878b8dd9 104
vpcola 0:f1d3878b8dd9 105
vpcola 0:f1d3878b8dd9 106 /** Class constructor */
vpcola 0:f1d3878b8dd9 107 FH_NodeDiscovery802::FH_NodeDiscovery802() :
vpcola 0:f1d3878b8dd9 108 FH_AtCmdResp(ApiFrame::AtCmdResp), node_discovery_cb(NULL)
vpcola 0:f1d3878b8dd9 109 {
vpcola 0:f1d3878b8dd9 110 }
vpcola 0:f1d3878b8dd9 111
vpcola 0:f1d3878b8dd9 112 /** Class destructor */
vpcola 0:f1d3878b8dd9 113 FH_NodeDiscovery802::~FH_NodeDiscovery802()
vpcola 0:f1d3878b8dd9 114 {
vpcola 0:f1d3878b8dd9 115 }
vpcola 0:f1d3878b8dd9 116
vpcola 0:f1d3878b8dd9 117 void FH_NodeDiscovery802::register_node_discovery_cb(node_discovery_802_cb_t function)
vpcola 0:f1d3878b8dd9 118 {
vpcola 0:f1d3878b8dd9 119 node_discovery_cb = function;
vpcola 0:f1d3878b8dd9 120 }
vpcola 0:f1d3878b8dd9 121
vpcola 0:f1d3878b8dd9 122 void FH_NodeDiscovery802::unregister_node_discovery_cb()
vpcola 0:f1d3878b8dd9 123 {
vpcola 0:f1d3878b8dd9 124 node_discovery_cb = NULL;
vpcola 0:f1d3878b8dd9 125 }
vpcola 0:f1d3878b8dd9 126
vpcola 0:f1d3878b8dd9 127
vpcola 0:f1d3878b8dd9 128 void FH_NodeDiscovery802::process_frame_data(const ApiFrame *const frame)
vpcola 0:f1d3878b8dd9 129 {
vpcola 0:f1d3878b8dd9 130 /* The caller checks that the type matches, so no need to check it here again */
vpcola 0:f1d3878b8dd9 131
vpcola 0:f1d3878b8dd9 132 if (node_discovery_cb == NULL) {
vpcola 0:f1d3878b8dd9 133 return;
vpcola 0:f1d3878b8dd9 134 }
vpcola 0:f1d3878b8dd9 135
vpcola 0:f1d3878b8dd9 136 if (frame->get_data_at(ATCMD_RESP_CMD_LOW_OFFSET) != 'N' ||
vpcola 0:f1d3878b8dd9 137 frame->get_data_at(ATCMD_RESP_CMD_HIGH_OFFSET) != 'D') {
vpcola 0:f1d3878b8dd9 138 return;
vpcola 0:f1d3878b8dd9 139 }
vpcola 0:f1d3878b8dd9 140
vpcola 0:f1d3878b8dd9 141 if (frame->get_data_at(ATCMD_RESP_STATUS_OFFSET) != AtCmdFrame::AtCmdRespOk) {
vpcola 0:f1d3878b8dd9 142 return;
vpcola 0:f1d3878b8dd9 143 }
vpcola 0:f1d3878b8dd9 144
vpcola 0:f1d3878b8dd9 145 const uint16_t min_atnd_response_with_data = sizeof (uint16_t) + sizeof(uint64_t);
vpcola 0:f1d3878b8dd9 146 if (frame->get_data_len() < min_atnd_response_with_data) {
vpcola 0:f1d3878b8dd9 147 /* Do not process the ATND "OK" response */
vpcola 0:f1d3878b8dd9 148 return;
vpcola 0:f1d3878b8dd9 149 }
vpcola 0:f1d3878b8dd9 150
vpcola 0:f1d3878b8dd9 151 const uint8_t * const data = frame->get_data();
vpcola 0:f1d3878b8dd9 152 const uint16_t addr16 = UINT16(data[ATCMD_RESP_NW_ADDR_H_OFFSET], data[ATCMD_RESP_NW_ADDR_L_OFFSET]);
vpcola 0:f1d3878b8dd9 153 const uint64_t addr64 = addr64_from_uint8_t(&data[ATCMD_RESP_SH_ADDR_L_OFFSET]);
vpcola 0:f1d3878b8dd9 154 #if 0
vpcola 0:f1d3878b8dd9 155 const uint8_t signal_strength = data[ATCMD_802_RESP_SIGN_STR_OFFSET];
vpcola 0:f1d3878b8dd9 156 #endif
vpcola 0:f1d3878b8dd9 157 const RemoteXBee802 remote = RemoteXBee802(addr64, addr16);
vpcola 0:f1d3878b8dd9 158 const char * const nodeid = (const char *)(&data[ATCMD_802_RESP_NI_OFFSET]);
vpcola 0:f1d3878b8dd9 159
vpcola 0:f1d3878b8dd9 160 node_discovery_cb(remote, nodeid);
vpcola 0:f1d3878b8dd9 161 }
vpcola 0:f1d3878b8dd9 162
vpcola 0:f1d3878b8dd9 163 /** Class constructor */
vpcola 0:f1d3878b8dd9 164 FH_NodeDiscoveryDM::FH_NodeDiscoveryDM() :
vpcola 0:f1d3878b8dd9 165 FH_AtCmdResp(ApiFrame::AtCmdResp), node_discovery_cb(NULL)
vpcola 0:f1d3878b8dd9 166 {
vpcola 0:f1d3878b8dd9 167 }
vpcola 0:f1d3878b8dd9 168
vpcola 0:f1d3878b8dd9 169 /** Class destructor */
vpcola 0:f1d3878b8dd9 170 FH_NodeDiscoveryDM::~FH_NodeDiscoveryDM()
vpcola 0:f1d3878b8dd9 171 {
vpcola 0:f1d3878b8dd9 172 }
vpcola 0:f1d3878b8dd9 173
vpcola 0:f1d3878b8dd9 174 void FH_NodeDiscoveryDM::register_node_discovery_cb(node_discovery_dm_cb_t function)
vpcola 0:f1d3878b8dd9 175 {
vpcola 0:f1d3878b8dd9 176 node_discovery_cb = function;
vpcola 0:f1d3878b8dd9 177 }
vpcola 0:f1d3878b8dd9 178
vpcola 0:f1d3878b8dd9 179 void FH_NodeDiscoveryDM::unregister_node_discovery_cb()
vpcola 0:f1d3878b8dd9 180 {
vpcola 0:f1d3878b8dd9 181 node_discovery_cb = NULL;
vpcola 0:f1d3878b8dd9 182 }
vpcola 0:f1d3878b8dd9 183
vpcola 0:f1d3878b8dd9 184
vpcola 0:f1d3878b8dd9 185 void FH_NodeDiscoveryDM::process_frame_data(const ApiFrame *const frame)
vpcola 0:f1d3878b8dd9 186 {
vpcola 0:f1d3878b8dd9 187 /* The caller checks that the type matches, so no need to check it here again */
vpcola 0:f1d3878b8dd9 188
vpcola 0:f1d3878b8dd9 189 if (node_discovery_cb == NULL) {
vpcola 0:f1d3878b8dd9 190 return;
vpcola 0:f1d3878b8dd9 191 }
vpcola 0:f1d3878b8dd9 192
vpcola 0:f1d3878b8dd9 193 if (frame->get_data_at(ATCMD_RESP_CMD_LOW_OFFSET) != 'N' ||
vpcola 0:f1d3878b8dd9 194 frame->get_data_at(ATCMD_RESP_CMD_HIGH_OFFSET) != 'D') {
vpcola 0:f1d3878b8dd9 195 return;
vpcola 0:f1d3878b8dd9 196 }
vpcola 0:f1d3878b8dd9 197
vpcola 0:f1d3878b8dd9 198 if (frame->get_data_at(ATCMD_RESP_STATUS_OFFSET) != AtCmdFrame::AtCmdRespOk) {
vpcola 0:f1d3878b8dd9 199 return;
vpcola 0:f1d3878b8dd9 200 }
vpcola 0:f1d3878b8dd9 201
vpcola 0:f1d3878b8dd9 202 const uint8_t * const data = frame->get_data(); /* The data payload we get here is the full AT command response payload, excluding the frameid. Keep that in mind for the offsets */
vpcola 0:f1d3878b8dd9 203 const uint64_t addr64 = addr64_from_uint8_t(&data[ATCMD_RESP_SH_ADDR_L_OFFSET]);
vpcola 0:f1d3878b8dd9 204 const char * const node_id = (const char *)&data[ATCMD_RESP_NI_OFFSET];
vpcola 0:f1d3878b8dd9 205 RemoteXBeeDM remote = RemoteXBeeDM(addr64);
vpcola 0:f1d3878b8dd9 206
vpcola 0:f1d3878b8dd9 207 node_discovery_cb(remote, node_id);
vpcola 0:f1d3878b8dd9 208 }