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