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