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 "XBee/XBee.h"
vpcola 0:f1d3878b8dd9 15 #include "ApiFrame.h"
vpcola 0:f1d3878b8dd9 16
vpcola 0:f1d3878b8dd9 17 using namespace XBeeLib;
vpcola 0:f1d3878b8dd9 18
vpcola 0:f1d3878b8dd9 19 uint8_t ApiFrame::last_frame_id = 0;
vpcola 0:f1d3878b8dd9 20
vpcola 0:f1d3878b8dd9 21 ApiFrame::ApiFrame(void)
vpcola 0:f1d3878b8dd9 22 {
vpcola 0:f1d3878b8dd9 23 this->_type = Invalid;
vpcola 0:f1d3878b8dd9 24 this->_data = NULL;
vpcola 0:f1d3878b8dd9 25 this->_data_frame_len = 0;
vpcola 0:f1d3878b8dd9 26 this->_alloc_data = false;
vpcola 0:f1d3878b8dd9 27 _frame_id = get_next_frame_id();
vpcola 0:f1d3878b8dd9 28 }
vpcola 0:f1d3878b8dd9 29
vpcola 0:f1d3878b8dd9 30 ApiFrame::ApiFrame(uint16_t len)
vpcola 0:f1d3878b8dd9 31 {
vpcola 0:f1d3878b8dd9 32 this->_type = Invalid;
vpcola 0:f1d3878b8dd9 33 this->_data = new uint8_t[len];
vpcola 0:f1d3878b8dd9 34 this->_alloc_data = true;
vpcola 0:f1d3878b8dd9 35 this->_data_frame_len = len;
vpcola 0:f1d3878b8dd9 36 this->_frame_id = get_next_frame_id();
vpcola 0:f1d3878b8dd9 37 }
vpcola 0:f1d3878b8dd9 38
vpcola 0:f1d3878b8dd9 39 uint8_t ApiFrame::get_next_frame_id(void)
vpcola 0:f1d3878b8dd9 40 {
vpcola 0:f1d3878b8dd9 41 last_frame_id++;
vpcola 0:f1d3878b8dd9 42 if (last_frame_id == 0) {
vpcola 0:f1d3878b8dd9 43 last_frame_id++;
vpcola 0:f1d3878b8dd9 44 }
vpcola 0:f1d3878b8dd9 45
vpcola 0:f1d3878b8dd9 46 return last_frame_id;
vpcola 0:f1d3878b8dd9 47 }
vpcola 0:f1d3878b8dd9 48
vpcola 0:f1d3878b8dd9 49 ApiFrame::ApiFrame(ApiFrameType type, const uint8_t *data, uint16_t len)
vpcola 0:f1d3878b8dd9 50 {
vpcola 0:f1d3878b8dd9 51 this->_data = NULL;
vpcola 0:f1d3878b8dd9 52 set_api_frame(type, data, len);
vpcola 0:f1d3878b8dd9 53 }
vpcola 0:f1d3878b8dd9 54
vpcola 0:f1d3878b8dd9 55 void ApiFrame::set_api_frame(ApiFrameType type, const uint8_t *data, uint16_t len)
vpcola 0:f1d3878b8dd9 56 {
vpcola 0:f1d3878b8dd9 57 this->_type = type;
vpcola 0:f1d3878b8dd9 58 this->_data_frame_len = len;
vpcola 0:f1d3878b8dd9 59 if (this->_data) {
vpcola 0:f1d3878b8dd9 60 delete _data;
vpcola 0:f1d3878b8dd9 61 }
vpcola 0:f1d3878b8dd9 62 this->_data = new uint8_t[len];
vpcola 0:f1d3878b8dd9 63 this->_alloc_data = true;
vpcola 0:f1d3878b8dd9 64 assert(this->_data != NULL);
vpcola 0:f1d3878b8dd9 65 memcpy((void *)this->_data, data, len);
vpcola 0:f1d3878b8dd9 66 }
vpcola 0:f1d3878b8dd9 67
vpcola 0:f1d3878b8dd9 68 ApiFrame::~ApiFrame()
vpcola 0:f1d3878b8dd9 69 {
vpcola 0:f1d3878b8dd9 70 if (this->_data != NULL && this->_alloc_data) {
vpcola 0:f1d3878b8dd9 71 delete[] this->_data;
vpcola 0:f1d3878b8dd9 72 }
vpcola 0:f1d3878b8dd9 73 }
vpcola 0:f1d3878b8dd9 74
vpcola 0:f1d3878b8dd9 75 void ApiFrame::dump(void) const
vpcola 0:f1d3878b8dd9 76 {
vpcola 0:f1d3878b8dd9 77 #if defined(ENABLE_LOGGING)
vpcola 0:f1d3878b8dd9 78 digi_log(LogLevelFrameData, "API frame: type %02x, len %d\r\n", this->_type, this->_data_frame_len);
vpcola 0:f1d3878b8dd9 79 for (int i = 0; i < this->_data_frame_len; i++)
vpcola 0:f1d3878b8dd9 80 digi_log(LogLevelFrameData, "%02x ", this->_data[i]);
vpcola 0:f1d3878b8dd9 81 digi_log(LogLevelFrameData, "\r\n");
vpcola 0:f1d3878b8dd9 82 #endif
vpcola 0:f1d3878b8dd9 83 }
vpcola 0:f1d3878b8dd9 84
vpcola 0:f1d3878b8dd9 85 void ApiFrame::dump_if(ApiFrameType type)
vpcola 0:f1d3878b8dd9 86 {
vpcola 0:f1d3878b8dd9 87 if (_type != type) {
vpcola 0:f1d3878b8dd9 88 return;
vpcola 0:f1d3878b8dd9 89 }
vpcola 0:f1d3878b8dd9 90 dump();
vpcola 0:f1d3878b8dd9 91 }
vpcola 0:f1d3878b8dd9 92
vpcola 0:f1d3878b8dd9 93 ApiFrame::ApiFrameType ApiFrame::get_frame_type() const
vpcola 0:f1d3878b8dd9 94 {
vpcola 0:f1d3878b8dd9 95 return _type;
vpcola 0:f1d3878b8dd9 96 }
vpcola 0:f1d3878b8dd9 97
vpcola 0:f1d3878b8dd9 98 void ApiFrame::set_frame_type(ApiFrameType type)
vpcola 0:f1d3878b8dd9 99 {
vpcola 0:f1d3878b8dd9 100 _type = type;
vpcola 0:f1d3878b8dd9 101 }
vpcola 0:f1d3878b8dd9 102
vpcola 0:f1d3878b8dd9 103 uint16_t ApiFrame::get_data_len() const
vpcola 0:f1d3878b8dd9 104 {
vpcola 0:f1d3878b8dd9 105 return _data_frame_len;
vpcola 0:f1d3878b8dd9 106 }
vpcola 0:f1d3878b8dd9 107
vpcola 0:f1d3878b8dd9 108 void ApiFrame::set_data_len(uint16_t len)
vpcola 0:f1d3878b8dd9 109 {
vpcola 0:f1d3878b8dd9 110 _data_frame_len = len;
vpcola 0:f1d3878b8dd9 111 }
vpcola 0:f1d3878b8dd9 112
vpcola 0:f1d3878b8dd9 113 const uint8_t *ApiFrame::get_data() const
vpcola 0:f1d3878b8dd9 114 {
vpcola 0:f1d3878b8dd9 115 return _data;
vpcola 0:f1d3878b8dd9 116 }
vpcola 0:f1d3878b8dd9 117
vpcola 0:f1d3878b8dd9 118 uint8_t ApiFrame::get_data_at(uint16_t index) const
vpcola 0:f1d3878b8dd9 119 {
vpcola 0:f1d3878b8dd9 120 return *(_data + index);
vpcola 0:f1d3878b8dd9 121 }
vpcola 0:f1d3878b8dd9 122
vpcola 0:f1d3878b8dd9 123 void ApiFrame::set_data(uint8_t d, uint16_t index)
vpcola 0:f1d3878b8dd9 124 {
vpcola 0:f1d3878b8dd9 125 *(_data + index) = d;
vpcola 0:f1d3878b8dd9 126 }
vpcola 0:f1d3878b8dd9 127
vpcola 0:f1d3878b8dd9 128 /* Returns the frame_id of this frame */
vpcola 0:f1d3878b8dd9 129 uint8_t ApiFrame::get_frame_id() const
vpcola 0:f1d3878b8dd9 130 {
vpcola 0:f1d3878b8dd9 131 return _frame_id;
vpcola 0:f1d3878b8dd9 132 }