mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Tue Sep 26 16:01:31 2017 +0000
Revision:
127:b4a661ff6fb9
Parent:
38:bb6d2be4d54c
minor re-ordering of FCC init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 33:1d0b855df5a5 1 /**
ansond 33:1d0b855df5a5 2 * @file DataWrapper.cpp
ansond 33:1d0b855df5a5 3 * @brief mbed CoAP Endpoint Resource Data Wrapper
ansond 33:1d0b855df5a5 4 * @author Doug Anson
ansond 33:1d0b855df5a5 5 * @version 1.0
ansond 33:1d0b855df5a5 6 * @see
ansond 33:1d0b855df5a5 7 *
ansond 33:1d0b855df5a5 8 * Copyright (c) 2014
ansond 33:1d0b855df5a5 9 *
ansond 33:1d0b855df5a5 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 33:1d0b855df5a5 11 * you may not use this file except in compliance with the License.
ansond 33:1d0b855df5a5 12 * You may obtain a copy of the License at
ansond 33:1d0b855df5a5 13 *
ansond 33:1d0b855df5a5 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 33:1d0b855df5a5 15 *
ansond 33:1d0b855df5a5 16 * Unless required by applicable law or agreed to in writing, software
ansond 33:1d0b855df5a5 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 33:1d0b855df5a5 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 33:1d0b855df5a5 19 * See the License for the specific language governing permissions and
ansond 33:1d0b855df5a5 20 * limitations under the License.
ansond 33:1d0b855df5a5 21 */
ansond 33:1d0b855df5a5 22
ansond 33:1d0b855df5a5 23 // Class Support
ansond 33:1d0b855df5a5 24 #include "mbed-connector-interface/DataWrapper.h"
ansond 33:1d0b855df5a5 25
ansond 33:1d0b855df5a5 26 // constructor
ansond 33:1d0b855df5a5 27 DataWrapper::DataWrapper(uint8_t *data,int data_length) {
ansond 33:1d0b855df5a5 28 this->m_data = data;
ansond 33:1d0b855df5a5 29 this->m_data_length = 0;
ansond 33:1d0b855df5a5 30 this->m_data_length_max = data_length;
ansond 33:1d0b855df5a5 31 this->m_alloced = false;
ansond 33:1d0b855df5a5 32 this->reset();
ansond 33:1d0b855df5a5 33 }
ansond 33:1d0b855df5a5 34
ansond 33:1d0b855df5a5 35 // constructor (alloc)
ansond 33:1d0b855df5a5 36 DataWrapper::DataWrapper(int data_length) {
ansond 33:1d0b855df5a5 37 this->m_data = (uint8_t *)malloc(data_length+1);
ansond 33:1d0b855df5a5 38 memset(this->m_data,0,data_length+1);
ansond 33:1d0b855df5a5 39 this->m_data_length = 0;
ansond 33:1d0b855df5a5 40 this->m_data_length_max = data_length;
ansond 33:1d0b855df5a5 41 this->m_alloced = true;
ansond 33:1d0b855df5a5 42 }
ansond 0:1f1f55e73248 43
ansond 33:1d0b855df5a5 44 // copy constructor
ansond 33:1d0b855df5a5 45 DataWrapper::DataWrapper(const DataWrapper &data) {
ansond 33:1d0b855df5a5 46 this->m_data = data.m_data;
ansond 33:1d0b855df5a5 47 this->m_data_length = data.m_data_length;
ansond 33:1d0b855df5a5 48 this->m_data_length_max = data.m_data_length_max;
ansond 33:1d0b855df5a5 49 this->m_alloced = data.m_alloced;
ansond 33:1d0b855df5a5 50 }
ansond 33:1d0b855df5a5 51
ansond 33:1d0b855df5a5 52 // destructor
ansond 33:1d0b855df5a5 53 DataWrapper::~DataWrapper() {
ansond 33:1d0b855df5a5 54 if (this->m_alloced && this->m_data != NULL) free(this->m_data);
ansond 33:1d0b855df5a5 55 }
ansond 33:1d0b855df5a5 56
ansond 33:1d0b855df5a5 57 // wrap
ansond 33:1d0b855df5a5 58 void DataWrapper::wrap(uint8_t *data,int data_length) {
ansond 33:1d0b855df5a5 59 this->reset();
ansond 33:1d0b855df5a5 60 if (data != NULL && data_length > 0) {
ansond 33:1d0b855df5a5 61 int length = data_length;
ansond 33:1d0b855df5a5 62 if (length > this->m_data_length_max) length = this->m_data_length_max;
ansond 33:1d0b855df5a5 63 memcpy(this->m_data,data,length);
ansond 33:1d0b855df5a5 64 this->m_data_length = length;
ansond 33:1d0b855df5a5 65 }
ansond 33:1d0b855df5a5 66 }
ansond 33:1d0b855df5a5 67
ansond 33:1d0b855df5a5 68 // unwrap
ansond 33:1d0b855df5a5 69 void DataWrapper::unwrap(uint8_t *data,int data_length) {
ansond 33:1d0b855df5a5 70 this->reset();
ansond 33:1d0b855df5a5 71 if (data != NULL && data_length > 0) {
ansond 33:1d0b855df5a5 72 int length = data_length;
ansond 33:1d0b855df5a5 73 if (length > this->m_data_length_max) length = this->m_data_length_max;
ansond 33:1d0b855df5a5 74 memcpy(this->m_data,data,length);
ansond 33:1d0b855df5a5 75 this->m_data_length = length;
ansond 33:1d0b855df5a5 76 }
ansond 33:1d0b855df5a5 77 }
ansond 33:1d0b855df5a5 78
ansond 33:1d0b855df5a5 79 // reset
ansond 33:1d0b855df5a5 80 void DataWrapper::reset() {
ansond 33:1d0b855df5a5 81 if (this->m_data != NULL && this->m_data_length_max > 0)
ansond 33:1d0b855df5a5 82 memset(this->m_data,0,this->m_data_length_max);
ansond 33:1d0b855df5a5 83 this->m_data_length = 0;
ansond 33:1d0b855df5a5 84 }
ansond 33:1d0b855df5a5 85
ansond 33:1d0b855df5a5 86 // set the app key
ansond 38:bb6d2be4d54c 87 void DataWrapper::setAppKey(uint8_t * /* appkey */,int /* appkey_length */) {
ansond 33:1d0b855df5a5 88 // do nothing in the base class
ansond 33:1d0b855df5a5 89 ;
ansond 33:1d0b855df5a5 90 }
ansond 33:1d0b855df5a5 91
ansond 33:1d0b855df5a5 92