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