Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mtlvserializer.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /* |
leothedragon | 0:8f0bb79ddd48 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:8f0bb79ddd48 | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:8f0bb79ddd48 | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 6 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 7 | * |
leothedragon | 0:8f0bb79ddd48 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 9 | * |
leothedragon | 0:8f0bb79ddd48 | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:8f0bb79ddd48 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 14 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 15 | */ |
leothedragon | 0:8f0bb79ddd48 | 16 | #include "include/m2mtlvserializer.h" |
leothedragon | 0:8f0bb79ddd48 | 17 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:8f0bb79ddd48 | 18 | |
leothedragon | 0:8f0bb79ddd48 | 19 | #include <stdlib.h> |
leothedragon | 0:8f0bb79ddd48 | 20 | #include "common_functions.h" |
leothedragon | 0:8f0bb79ddd48 | 21 | |
leothedragon | 0:8f0bb79ddd48 | 22 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 23 | |
leothedragon | 0:8f0bb79ddd48 | 24 | #define MAX_TLV_LENGTH_SIZE 3 |
leothedragon | 0:8f0bb79ddd48 | 25 | #define MAX_TLV_ID_SIZE 2 |
leothedragon | 0:8f0bb79ddd48 | 26 | #define TLV_TYPE_SIZE 1 |
leothedragon | 0:8f0bb79ddd48 | 27 | |
leothedragon | 0:8f0bb79ddd48 | 28 | uint8_t* M2MTLVSerializer::serialize(const M2MObjectInstanceList &object_instance_list, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 29 | { |
leothedragon | 0:8f0bb79ddd48 | 30 | return serialize_object_instances(object_instance_list, size); |
leothedragon | 0:8f0bb79ddd48 | 31 | } |
leothedragon | 0:8f0bb79ddd48 | 32 | |
leothedragon | 0:8f0bb79ddd48 | 33 | uint8_t* M2MTLVSerializer::serialize(const M2MResourceList &resource_list, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 34 | { |
leothedragon | 0:8f0bb79ddd48 | 35 | bool valid = true; |
leothedragon | 0:8f0bb79ddd48 | 36 | return serialize_resources(resource_list, size,valid); |
leothedragon | 0:8f0bb79ddd48 | 37 | } |
leothedragon | 0:8f0bb79ddd48 | 38 | |
leothedragon | 0:8f0bb79ddd48 | 39 | uint8_t* M2MTLVSerializer::serialize(const M2MResource *resource, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 40 | { |
leothedragon | 0:8f0bb79ddd48 | 41 | uint8_t* data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 42 | serialize(resource, data, size); |
leothedragon | 0:8f0bb79ddd48 | 43 | return data; |
leothedragon | 0:8f0bb79ddd48 | 44 | } |
leothedragon | 0:8f0bb79ddd48 | 45 | |
leothedragon | 0:8f0bb79ddd48 | 46 | uint8_t* M2MTLVSerializer::serialize_object_instances(const M2MObjectInstanceList &object_instance_list, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 47 | { |
leothedragon | 0:8f0bb79ddd48 | 48 | uint8_t *data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 49 | |
leothedragon | 0:8f0bb79ddd48 | 50 | if(!object_instance_list.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 51 | M2MObjectInstanceList::const_iterator it; |
leothedragon | 0:8f0bb79ddd48 | 52 | it = object_instance_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 53 | for (; it!=object_instance_list.end(); it++) { |
leothedragon | 0:8f0bb79ddd48 | 54 | uint16_t id = (*it)->instance_id(); |
leothedragon | 0:8f0bb79ddd48 | 55 | serialize(id, *it, data, size); |
leothedragon | 0:8f0bb79ddd48 | 56 | } |
leothedragon | 0:8f0bb79ddd48 | 57 | } |
leothedragon | 0:8f0bb79ddd48 | 58 | return data; |
leothedragon | 0:8f0bb79ddd48 | 59 | } |
leothedragon | 0:8f0bb79ddd48 | 60 | |
leothedragon | 0:8f0bb79ddd48 | 61 | uint8_t* M2MTLVSerializer::serialize_resources(const M2MResourceList &resource_list, uint32_t &size, bool &valid) |
leothedragon | 0:8f0bb79ddd48 | 62 | { |
leothedragon | 0:8f0bb79ddd48 | 63 | uint8_t *data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 64 | |
leothedragon | 0:8f0bb79ddd48 | 65 | if(!resource_list.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 66 | M2MResourceList::const_iterator it; |
leothedragon | 0:8f0bb79ddd48 | 67 | it = resource_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 68 | for (; it!=resource_list.end(); it++) { |
leothedragon | 0:8f0bb79ddd48 | 69 | if((*it)->name_id() == -1) { |
leothedragon | 0:8f0bb79ddd48 | 70 | valid = false; |
leothedragon | 0:8f0bb79ddd48 | 71 | break; |
leothedragon | 0:8f0bb79ddd48 | 72 | } |
leothedragon | 0:8f0bb79ddd48 | 73 | } |
leothedragon | 0:8f0bb79ddd48 | 74 | if(valid) { |
leothedragon | 0:8f0bb79ddd48 | 75 | it = resource_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 76 | for (; it!=resource_list.end(); it++) { |
leothedragon | 0:8f0bb79ddd48 | 77 | if (((*it)->operation() & M2MBase::GET_ALLOWED) == M2MBase::GET_ALLOWED) { |
leothedragon | 0:8f0bb79ddd48 | 78 | if(!serialize(*it, data, size)) { |
leothedragon | 0:8f0bb79ddd48 | 79 | /* serializing has failed */ |
leothedragon | 0:8f0bb79ddd48 | 80 | /* free data so far */ |
leothedragon | 0:8f0bb79ddd48 | 81 | free(data); |
leothedragon | 0:8f0bb79ddd48 | 82 | /* invalidate */ |
leothedragon | 0:8f0bb79ddd48 | 83 | valid = false; |
leothedragon | 0:8f0bb79ddd48 | 84 | /* return NULL immediately */ |
leothedragon | 0:8f0bb79ddd48 | 85 | return NULL; |
leothedragon | 0:8f0bb79ddd48 | 86 | } |
leothedragon | 0:8f0bb79ddd48 | 87 | } |
leothedragon | 0:8f0bb79ddd48 | 88 | } |
leothedragon | 0:8f0bb79ddd48 | 89 | } |
leothedragon | 0:8f0bb79ddd48 | 90 | } |
leothedragon | 0:8f0bb79ddd48 | 91 | return data; |
leothedragon | 0:8f0bb79ddd48 | 92 | } |
leothedragon | 0:8f0bb79ddd48 | 93 | |
leothedragon | 0:8f0bb79ddd48 | 94 | bool M2MTLVSerializer::serialize(uint16_t id, const M2MObjectInstance *object_instance, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 95 | { |
leothedragon | 0:8f0bb79ddd48 | 96 | uint8_t *resource_data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 97 | uint32_t resource_size = 0; |
leothedragon | 0:8f0bb79ddd48 | 98 | bool success; |
leothedragon | 0:8f0bb79ddd48 | 99 | |
leothedragon | 0:8f0bb79ddd48 | 100 | bool valid = true; |
leothedragon | 0:8f0bb79ddd48 | 101 | resource_data = serialize_resources(object_instance->resources(),resource_size,valid); |
leothedragon | 0:8f0bb79ddd48 | 102 | if(valid) { |
leothedragon | 0:8f0bb79ddd48 | 103 | if(serialize_TILV(TYPE_OBJECT_INSTANCE, id, resource_data, resource_size, data, size)) { |
leothedragon | 0:8f0bb79ddd48 | 104 | success = true; |
leothedragon | 0:8f0bb79ddd48 | 105 | } else { |
leothedragon | 0:8f0bb79ddd48 | 106 | /* serializing object instance failed */ |
leothedragon | 0:8f0bb79ddd48 | 107 | success = false; |
leothedragon | 0:8f0bb79ddd48 | 108 | } |
leothedragon | 0:8f0bb79ddd48 | 109 | free(resource_data); |
leothedragon | 0:8f0bb79ddd48 | 110 | } else { |
leothedragon | 0:8f0bb79ddd48 | 111 | /* serializing resources failed */ |
leothedragon | 0:8f0bb79ddd48 | 112 | success = false; |
leothedragon | 0:8f0bb79ddd48 | 113 | } |
leothedragon | 0:8f0bb79ddd48 | 114 | return success; |
leothedragon | 0:8f0bb79ddd48 | 115 | } |
leothedragon | 0:8f0bb79ddd48 | 116 | |
leothedragon | 0:8f0bb79ddd48 | 117 | bool M2MTLVSerializer::serialize(const M2MResource *resource, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 118 | { |
leothedragon | 0:8f0bb79ddd48 | 119 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 120 | if(resource->name_id() != -1) { |
leothedragon | 0:8f0bb79ddd48 | 121 | success = resource->supports_multiple_instances() ? |
leothedragon | 0:8f0bb79ddd48 | 122 | serialize_multiple_resource(resource, data, size) : |
leothedragon | 0:8f0bb79ddd48 | 123 | serialize_resource(resource, data, size); |
leothedragon | 0:8f0bb79ddd48 | 124 | } |
leothedragon | 0:8f0bb79ddd48 | 125 | return success; |
leothedragon | 0:8f0bb79ddd48 | 126 | } |
leothedragon | 0:8f0bb79ddd48 | 127 | |
leothedragon | 0:8f0bb79ddd48 | 128 | bool M2MTLVSerializer::serialize_resource(const M2MResource *resource, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 129 | { |
leothedragon | 0:8f0bb79ddd48 | 130 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 131 | if(resource->name_id() != -1) { |
leothedragon | 0:8f0bb79ddd48 | 132 | if ( (resource->resource_instance_type() == M2MResourceBase::INTEGER) || |
leothedragon | 0:8f0bb79ddd48 | 133 | (resource->resource_instance_type() == M2MResourceBase::BOOLEAN) || |
leothedragon | 0:8f0bb79ddd48 | 134 | (resource->resource_instance_type() == M2MResourceBase::TIME) ) { |
leothedragon | 0:8f0bb79ddd48 | 135 | success = serialize_TLV_binary_int(resource, TYPE_RESOURCE, resource->name_id(), data, size); |
leothedragon | 0:8f0bb79ddd48 | 136 | } |
leothedragon | 0:8f0bb79ddd48 | 137 | else if (resource->resource_instance_type() == M2MResourceBase::FLOAT) { |
leothedragon | 0:8f0bb79ddd48 | 138 | success = serialize_TLV_binary_float(resource, TYPE_RESOURCE, resource->name_id(), data, size); |
leothedragon | 0:8f0bb79ddd48 | 139 | } |
leothedragon | 0:8f0bb79ddd48 | 140 | else { |
leothedragon | 0:8f0bb79ddd48 | 141 | success = serialize_TILV(TYPE_RESOURCE, resource->name_id(), |
leothedragon | 0:8f0bb79ddd48 | 142 | resource->value(), resource->value_length(), data, size); |
leothedragon | 0:8f0bb79ddd48 | 143 | } |
leothedragon | 0:8f0bb79ddd48 | 144 | } |
leothedragon | 0:8f0bb79ddd48 | 145 | return success; |
leothedragon | 0:8f0bb79ddd48 | 146 | } |
leothedragon | 0:8f0bb79ddd48 | 147 | |
leothedragon | 0:8f0bb79ddd48 | 148 | bool M2MTLVSerializer::serialize_multiple_resource(const M2MResource *resource, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 149 | { |
leothedragon | 0:8f0bb79ddd48 | 150 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 151 | uint8_t *nested_data = NULL; |
leothedragon | 0:8f0bb79ddd48 | 152 | uint32_t nested_data_size = 0; |
leothedragon | 0:8f0bb79ddd48 | 153 | |
leothedragon | 0:8f0bb79ddd48 | 154 | const M2MResourceInstanceList &instance_list = resource->resource_instances(); |
leothedragon | 0:8f0bb79ddd48 | 155 | if(!instance_list.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 156 | M2MResourceInstanceList::const_iterator it; |
leothedragon | 0:8f0bb79ddd48 | 157 | it = instance_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 158 | for (; it!=instance_list.end(); it++) { |
leothedragon | 0:8f0bb79ddd48 | 159 | uint16_t id = (*it)->instance_id(); |
leothedragon | 0:8f0bb79ddd48 | 160 | if (((*it)->operation() & M2MBase::GET_ALLOWED) == M2MBase::GET_ALLOWED) { |
leothedragon | 0:8f0bb79ddd48 | 161 | if(!serialize_resource_instance(id, (*it), nested_data, nested_data_size)) { |
leothedragon | 0:8f0bb79ddd48 | 162 | /* serializing instance has failed */ |
leothedragon | 0:8f0bb79ddd48 | 163 | /* free data so far allocated */ |
leothedragon | 0:8f0bb79ddd48 | 164 | free(nested_data); |
leothedragon | 0:8f0bb79ddd48 | 165 | /* return fail immediately*/ |
leothedragon | 0:8f0bb79ddd48 | 166 | success = false; |
leothedragon | 0:8f0bb79ddd48 | 167 | return success; |
leothedragon | 0:8f0bb79ddd48 | 168 | } |
leothedragon | 0:8f0bb79ddd48 | 169 | } |
leothedragon | 0:8f0bb79ddd48 | 170 | } |
leothedragon | 0:8f0bb79ddd48 | 171 | } |
leothedragon | 0:8f0bb79ddd48 | 172 | if(resource->name_id() != -1 && |
leothedragon | 0:8f0bb79ddd48 | 173 | (resource->operation() & M2MBase::GET_ALLOWED) == M2MBase::GET_ALLOWED) { |
leothedragon | 0:8f0bb79ddd48 | 174 | success = serialize_TILV(TYPE_MULTIPLE_RESOURCE, resource->name_id(), |
leothedragon | 0:8f0bb79ddd48 | 175 | nested_data, nested_data_size, data, size); |
leothedragon | 0:8f0bb79ddd48 | 176 | } |
leothedragon | 0:8f0bb79ddd48 | 177 | |
leothedragon | 0:8f0bb79ddd48 | 178 | free(nested_data); |
leothedragon | 0:8f0bb79ddd48 | 179 | return success; |
leothedragon | 0:8f0bb79ddd48 | 180 | } |
leothedragon | 0:8f0bb79ddd48 | 181 | |
leothedragon | 0:8f0bb79ddd48 | 182 | bool M2MTLVSerializer::serialize_resource_instance(uint16_t id, const M2MResourceInstance *resource, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 183 | { |
leothedragon | 0:8f0bb79ddd48 | 184 | bool success; |
leothedragon | 0:8f0bb79ddd48 | 185 | |
leothedragon | 0:8f0bb79ddd48 | 186 | if ( (resource->resource_instance_type() == M2MResourceBase::INTEGER) || |
leothedragon | 0:8f0bb79ddd48 | 187 | (resource->resource_instance_type() == M2MResourceBase::BOOLEAN) || |
leothedragon | 0:8f0bb79ddd48 | 188 | (resource->resource_instance_type() == M2MResourceBase::TIME) ) { |
leothedragon | 0:8f0bb79ddd48 | 189 | success=serialize_TLV_binary_int(resource, TYPE_RESOURCE_INSTANCE, id, data, size); |
leothedragon | 0:8f0bb79ddd48 | 190 | } |
leothedragon | 0:8f0bb79ddd48 | 191 | else if (resource->resource_instance_type() == M2MResourceBase::FLOAT) { |
leothedragon | 0:8f0bb79ddd48 | 192 | success=serialize_TLV_binary_float(resource, TYPE_RESOURCE_INSTANCE, id, data, size); |
leothedragon | 0:8f0bb79ddd48 | 193 | } |
leothedragon | 0:8f0bb79ddd48 | 194 | else { |
leothedragon | 0:8f0bb79ddd48 | 195 | success=serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size); |
leothedragon | 0:8f0bb79ddd48 | 196 | } |
leothedragon | 0:8f0bb79ddd48 | 197 | |
leothedragon | 0:8f0bb79ddd48 | 198 | return success; |
leothedragon | 0:8f0bb79ddd48 | 199 | } |
leothedragon | 0:8f0bb79ddd48 | 200 | |
leothedragon | 0:8f0bb79ddd48 | 201 | /* See, OMA-TS-LightweightM2M-V1_0-20170208-A, Appendix C, |
leothedragon | 0:8f0bb79ddd48 | 202 | * Data Types, Integer, Boolean and Time TLV Format */ |
leothedragon | 0:8f0bb79ddd48 | 203 | bool M2MTLVSerializer::serialize_TLV_binary_int(const M2MResourceBase *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 204 | { |
leothedragon | 0:8f0bb79ddd48 | 205 | int64_t valueInt = resource->get_value_int(); |
leothedragon | 0:8f0bb79ddd48 | 206 | uint32_t buffer_size; |
leothedragon | 0:8f0bb79ddd48 | 207 | /* max len 8 bytes */ |
leothedragon | 0:8f0bb79ddd48 | 208 | uint8_t buffer[8]; |
leothedragon | 0:8f0bb79ddd48 | 209 | |
leothedragon | 0:8f0bb79ddd48 | 210 | if (resource->resource_instance_type() == M2MResourceBase::BOOLEAN) { |
leothedragon | 0:8f0bb79ddd48 | 211 | buffer_size = 1; |
leothedragon | 0:8f0bb79ddd48 | 212 | buffer[0] = valueInt; |
leothedragon | 0:8f0bb79ddd48 | 213 | } else { |
leothedragon | 0:8f0bb79ddd48 | 214 | buffer_size = 8; |
leothedragon | 0:8f0bb79ddd48 | 215 | common_write_64_bit(valueInt, buffer); |
leothedragon | 0:8f0bb79ddd48 | 216 | } |
leothedragon | 0:8f0bb79ddd48 | 217 | |
leothedragon | 0:8f0bb79ddd48 | 218 | return serialize_TILV(type, id, buffer, buffer_size, data, size); |
leothedragon | 0:8f0bb79ddd48 | 219 | } |
leothedragon | 0:8f0bb79ddd48 | 220 | |
leothedragon | 0:8f0bb79ddd48 | 221 | /* See, OMA-TS-LightweightM2M-V1_0-20170208-A, Appendix C, |
leothedragon | 0:8f0bb79ddd48 | 222 | * Data Type Float (32 bit only) TLV Format */ |
leothedragon | 0:8f0bb79ddd48 | 223 | bool M2MTLVSerializer::serialize_TLV_binary_float(const M2MResourceBase *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 224 | { |
leothedragon | 0:8f0bb79ddd48 | 225 | float valueFloat = resource->get_value_float(); |
leothedragon | 0:8f0bb79ddd48 | 226 | /* max len 8 bytes */ |
leothedragon | 0:8f0bb79ddd48 | 227 | uint8_t buffer[4]; |
leothedragon | 0:8f0bb79ddd48 | 228 | |
leothedragon | 0:8f0bb79ddd48 | 229 | common_write_32_bit(*(uint32_t*)&valueFloat, buffer); |
leothedragon | 0:8f0bb79ddd48 | 230 | |
leothedragon | 0:8f0bb79ddd48 | 231 | return serialize_TILV(type, id, buffer, 4, data, size); |
leothedragon | 0:8f0bb79ddd48 | 232 | } |
leothedragon | 0:8f0bb79ddd48 | 233 | |
leothedragon | 0:8f0bb79ddd48 | 234 | |
leothedragon | 0:8f0bb79ddd48 | 235 | bool M2MTLVSerializer::serialize_TILV(uint8_t type, uint16_t id, uint8_t *value, uint32_t value_length, uint8_t *&data, uint32_t &size) |
leothedragon | 0:8f0bb79ddd48 | 236 | { |
leothedragon | 0:8f0bb79ddd48 | 237 | uint8_t *tlv = 0; |
leothedragon | 0:8f0bb79ddd48 | 238 | const uint32_t type_length = TLV_TYPE_SIZE; |
leothedragon | 0:8f0bb79ddd48 | 239 | type += id < 256 ? 0 : ID16; |
leothedragon | 0:8f0bb79ddd48 | 240 | type += value_length < 8 ? value_length : |
leothedragon | 0:8f0bb79ddd48 | 241 | value_length < 256 ? LENGTH8 : |
leothedragon | 0:8f0bb79ddd48 | 242 | value_length < 65536 ? LENGTH16 : LENGTH24; |
leothedragon | 0:8f0bb79ddd48 | 243 | uint8_t tlv_type; |
leothedragon | 0:8f0bb79ddd48 | 244 | tlv_type = type & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 245 | |
leothedragon | 0:8f0bb79ddd48 | 246 | uint32_t id_size; |
leothedragon | 0:8f0bb79ddd48 | 247 | uint8_t id_array[MAX_TLV_ID_SIZE]; |
leothedragon | 0:8f0bb79ddd48 | 248 | serialize_id(id, id_size, id_array); |
leothedragon | 0:8f0bb79ddd48 | 249 | |
leothedragon | 0:8f0bb79ddd48 | 250 | uint32_t length_size; |
leothedragon | 0:8f0bb79ddd48 | 251 | uint8_t length_array[MAX_TLV_LENGTH_SIZE]; |
leothedragon | 0:8f0bb79ddd48 | 252 | serialize_length(value_length, length_size, length_array); |
leothedragon | 0:8f0bb79ddd48 | 253 | |
leothedragon | 0:8f0bb79ddd48 | 254 | tlv = (uint8_t*)malloc(size + type_length + id_size + length_size + value_length); |
leothedragon | 0:8f0bb79ddd48 | 255 | if (!tlv) { |
leothedragon | 0:8f0bb79ddd48 | 256 | /* memory allocation has failed */ |
leothedragon | 0:8f0bb79ddd48 | 257 | /* return failure immediately */ |
leothedragon | 0:8f0bb79ddd48 | 258 | return false; |
leothedragon | 0:8f0bb79ddd48 | 259 | /* eventually NULL will be returned to serializer public method caller */ |
leothedragon | 0:8f0bb79ddd48 | 260 | } |
leothedragon | 0:8f0bb79ddd48 | 261 | if(data) { |
leothedragon | 0:8f0bb79ddd48 | 262 | memcpy(tlv, data, size); |
leothedragon | 0:8f0bb79ddd48 | 263 | free(data); |
leothedragon | 0:8f0bb79ddd48 | 264 | } |
leothedragon | 0:8f0bb79ddd48 | 265 | memcpy(tlv+size, &tlv_type, type_length); |
leothedragon | 0:8f0bb79ddd48 | 266 | memcpy(tlv+size+type_length, id_array, id_size); |
leothedragon | 0:8f0bb79ddd48 | 267 | memcpy(tlv+size+type_length+id_size, length_array, length_size); |
leothedragon | 0:8f0bb79ddd48 | 268 | memcpy(tlv+size+type_length+id_size+length_size, value, value_length); |
leothedragon | 0:8f0bb79ddd48 | 269 | |
leothedragon | 0:8f0bb79ddd48 | 270 | data = tlv; |
leothedragon | 0:8f0bb79ddd48 | 271 | size += type_length + id_size + length_size + value_length; |
leothedragon | 0:8f0bb79ddd48 | 272 | return true; |
leothedragon | 0:8f0bb79ddd48 | 273 | } |
leothedragon | 0:8f0bb79ddd48 | 274 | |
leothedragon | 0:8f0bb79ddd48 | 275 | void M2MTLVSerializer::serialize_id(uint16_t id, uint32_t &size, uint8_t *id_ptr) |
leothedragon | 0:8f0bb79ddd48 | 276 | { |
leothedragon | 0:8f0bb79ddd48 | 277 | if(id > 255) { |
leothedragon | 0:8f0bb79ddd48 | 278 | size=2; |
leothedragon | 0:8f0bb79ddd48 | 279 | id_ptr[0] = (id & 0xFF00) >> 8; |
leothedragon | 0:8f0bb79ddd48 | 280 | id_ptr[1] = id & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 281 | } else { |
leothedragon | 0:8f0bb79ddd48 | 282 | size=1; |
leothedragon | 0:8f0bb79ddd48 | 283 | id_ptr[0] = id & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 284 | } |
leothedragon | 0:8f0bb79ddd48 | 285 | } |
leothedragon | 0:8f0bb79ddd48 | 286 | |
leothedragon | 0:8f0bb79ddd48 | 287 | void M2MTLVSerializer::serialize_length(uint32_t length, uint32_t &size, uint8_t *length_ptr) |
leothedragon | 0:8f0bb79ddd48 | 288 | { |
leothedragon | 0:8f0bb79ddd48 | 289 | if (length > 65535) { |
leothedragon | 0:8f0bb79ddd48 | 290 | size = 3; |
leothedragon | 0:8f0bb79ddd48 | 291 | length_ptr[0] = (length & 0xFF0000) >> 16; |
leothedragon | 0:8f0bb79ddd48 | 292 | length_ptr[1] = (length & 0xFF00) >> 8; |
leothedragon | 0:8f0bb79ddd48 | 293 | length_ptr[2] = length & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 294 | } else if (length > 255) { |
leothedragon | 0:8f0bb79ddd48 | 295 | size = 2; |
leothedragon | 0:8f0bb79ddd48 | 296 | length_ptr[0] = (length & 0xFF00) >> 8; |
leothedragon | 0:8f0bb79ddd48 | 297 | length_ptr[1] = length & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 298 | } else if (length > 7) { |
leothedragon | 0:8f0bb79ddd48 | 299 | size = 1; |
leothedragon | 0:8f0bb79ddd48 | 300 | length_ptr[0] = length & 0xFF; |
leothedragon | 0:8f0bb79ddd48 | 301 | } else { |
leothedragon | 0:8f0bb79ddd48 | 302 | size=0; |
leothedragon | 0:8f0bb79ddd48 | 303 | } |
leothedragon | 0:8f0bb79ddd48 | 304 | } |
leothedragon | 0:8f0bb79ddd48 | 305 |