XBee modules

Dependencies:   DigiLogger

Fork of XBeeLib by Digi International Inc.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ApiFrame.cpp Source File

ApiFrame.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBee/XBee.h"
00015 #include "ApiFrame.h"
00016 
00017 using namespace XBeeLib;
00018 
00019 uint8_t ApiFrame::last_frame_id = 0;
00020 
00021 ApiFrame::ApiFrame(void)
00022 {
00023     this->_type = Invalid;
00024     this->_data = NULL;
00025     this->_data_frame_len = 0;
00026     this->_alloc_data = false;
00027     _frame_id = get_next_frame_id();
00028 }
00029 
00030 ApiFrame::ApiFrame(uint16_t len)
00031 {
00032     this->_type = Invalid;
00033     this->_data = new uint8_t[len];
00034     this->_alloc_data = true;
00035     this->_data_frame_len = len;
00036     this->_frame_id = get_next_frame_id();
00037 }
00038 
00039 uint8_t ApiFrame::get_next_frame_id(void)
00040 {
00041     last_frame_id++;
00042     if (last_frame_id == 0) {
00043         last_frame_id++;
00044     }
00045 
00046     return last_frame_id;
00047 }
00048 
00049 ApiFrame::ApiFrame(ApiFrameType type, const uint8_t *data, uint16_t len)
00050 {
00051     this->_data = NULL;
00052     set_api_frame(type, data, len);
00053 }
00054 
00055 void ApiFrame::set_api_frame(ApiFrameType type, const uint8_t *data, uint16_t len)
00056 {
00057     this->_type = type;
00058     this->_data_frame_len = len;
00059     if (this->_data) {
00060         delete _data;
00061     }
00062     this->_data = new uint8_t[len];
00063     this->_alloc_data = true;
00064     assert(this->_data != NULL);
00065     memcpy((void *)this->_data, data, len);
00066 }
00067 
00068 ApiFrame::~ApiFrame()
00069 {
00070     if (this->_data != NULL && this->_alloc_data) {
00071         delete[] this->_data;
00072     }
00073 }
00074 
00075 void ApiFrame::dump(void) const
00076 {
00077 #if defined(ENABLE_LOGGING)
00078     digi_log(LogLevelFrameData, "API frame: type %02x, len %d\r\n", this->_type, this->_data_frame_len);
00079     for (int i = 0; i < this->_data_frame_len; i++)
00080         digi_log(LogLevelFrameData, "%02x ", this->_data[i]);
00081     digi_log(LogLevelFrameData, "\r\n");
00082 #endif
00083 }
00084 
00085 void ApiFrame::dump_if(ApiFrameType type)
00086 {
00087     if (_type != type) {
00088         return;
00089     }
00090     dump();
00091 }
00092 
00093 ApiFrame::ApiFrameType ApiFrame::get_frame_type() const
00094 {
00095     return _type;
00096 }
00097 
00098 void ApiFrame::set_frame_type(ApiFrameType type)
00099 {
00100     _type = type;
00101 }
00102 
00103 uint16_t ApiFrame::get_data_len() const
00104 {
00105     return _data_frame_len;
00106 }
00107 
00108 void ApiFrame::set_data_len(uint16_t len)
00109 {
00110     _data_frame_len = len;
00111 }
00112 
00113 const uint8_t *ApiFrame::get_data() const
00114 {
00115     return _data;
00116 }
00117 
00118 uint8_t ApiFrame::get_data_at(uint16_t index) const
00119 {
00120     return *(_data + index);
00121 }
00122 
00123 void ApiFrame::set_data(uint8_t d, uint16_t index)
00124 {
00125     *(_data + index) = d;
00126 }
00127 
00128 /* Returns the frame_id of this frame */
00129 uint8_t ApiFrame::get_frame_id() const
00130 {
00131     return _frame_id;
00132 }