Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16 // Needed for PRIu64 on FreeRTOS
vithyat 0:977e87915078 17 #include <stdio.h>
vithyat 0:977e87915078 18 // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX
vithyat 0:977e87915078 19 #ifndef __STDC_LIMIT_MACROS
vithyat 0:977e87915078 20 #define __STDC_LIMIT_MACROS
vithyat 0:977e87915078 21 #endif
vithyat 0:977e87915078 22
vithyat 0:977e87915078 23 // Note: this macro is needed on armcc to get the the PRI*32 macros
vithyat 0:977e87915078 24 // from inttypes.h in a C++ code.
vithyat 0:977e87915078 25 #ifndef __STDC_FORMAT_MACROS
vithyat 0:977e87915078 26 #define __STDC_FORMAT_MACROS
vithyat 0:977e87915078 27 #endif
vithyat 0:977e87915078 28
vithyat 0:977e87915078 29 #include "include/m2mtlvdeserializer.h"
vithyat 0:977e87915078 30 #include "mbed-client/m2mconstants.h"
vithyat 0:977e87915078 31 #include "mbed-trace/mbed_trace.h"
vithyat 0:977e87915078 32 #include "common_functions.h"
vithyat 0:977e87915078 33
vithyat 0:977e87915078 34 #define TRACE_GROUP "mClt"
vithyat 0:977e87915078 35 #define BUFFER_SIZE 10
vithyat 0:977e87915078 36
vithyat 0:977e87915078 37 bool M2MTLVDeserializer::is_object_instance(const uint8_t *tlv)
vithyat 0:977e87915078 38 {
vithyat 0:977e87915078 39 return is_object_instance(tlv, 0);
vithyat 0:977e87915078 40 }
vithyat 0:977e87915078 41
vithyat 0:977e87915078 42 bool M2MTLVDeserializer::is_resource(const uint8_t *tlv)
vithyat 0:977e87915078 43 {
vithyat 0:977e87915078 44 return is_resource(tlv, 0);
vithyat 0:977e87915078 45 }
vithyat 0:977e87915078 46
vithyat 0:977e87915078 47 bool M2MTLVDeserializer::is_multiple_resource(const uint8_t *tlv)
vithyat 0:977e87915078 48 {
vithyat 0:977e87915078 49 return is_multiple_resource(tlv, 0);
vithyat 0:977e87915078 50 }
vithyat 0:977e87915078 51
vithyat 0:977e87915078 52 bool M2MTLVDeserializer::is_resource_instance(const uint8_t *tlv)
vithyat 0:977e87915078 53 {
vithyat 0:977e87915078 54 return is_resource_instance(tlv, 0);
vithyat 0:977e87915078 55 }
vithyat 0:977e87915078 56
vithyat 0:977e87915078 57 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialise_object_instances(const uint8_t* tlv,
vithyat 0:977e87915078 58 uint32_t tlv_size,
vithyat 0:977e87915078 59 M2MObject &object,
vithyat 0:977e87915078 60 M2MTLVDeserializer::Operation operation)
vithyat 0:977e87915078 61 {
vithyat 0:977e87915078 62 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 63 if (is_object_instance(tlv) ) {
vithyat 0:977e87915078 64 tr_debug("M2MTLVDeserializer::deserialise_object_instances");
vithyat 0:977e87915078 65 error = deserialize_object_instances(tlv, tlv_size, 0, object,operation,false);
vithyat 0:977e87915078 66 if(M2MTLVDeserializer::None == error) {
vithyat 0:977e87915078 67 error = deserialize_object_instances(tlv, tlv_size, 0, object,operation,true);
vithyat 0:977e87915078 68 }
vithyat 0:977e87915078 69 } else {
vithyat 0:977e87915078 70 tr_debug("M2MTLVDeserializer::deserialise_object_instances ::NotValid");
vithyat 0:977e87915078 71 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 72 }
vithyat 0:977e87915078 73 return error;
vithyat 0:977e87915078 74 }
vithyat 0:977e87915078 75
vithyat 0:977e87915078 76 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resources(const uint8_t *tlv,
vithyat 0:977e87915078 77 uint32_t tlv_size,
vithyat 0:977e87915078 78 M2MObjectInstance &object_instance,
vithyat 0:977e87915078 79 M2MTLVDeserializer::Operation operation)
vithyat 0:977e87915078 80 {
vithyat 0:977e87915078 81 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 82 if (!is_resource(tlv) && !is_multiple_resource(tlv)) {
vithyat 0:977e87915078 83 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 84 } else {
vithyat 0:977e87915078 85 error = deserialize_resources(tlv, tlv_size, 0, object_instance, operation,false);
vithyat 0:977e87915078 86 if(M2MTLVDeserializer::None == error) {
vithyat 0:977e87915078 87 if (M2MTLVDeserializer::Put == operation) {
vithyat 0:977e87915078 88 remove_resources(tlv, tlv_size, object_instance, 0);
vithyat 0:977e87915078 89 }
vithyat 0:977e87915078 90 error = deserialize_resources(tlv, tlv_size, 0, object_instance, operation,true);
vithyat 0:977e87915078 91 }
vithyat 0:977e87915078 92 }
vithyat 0:977e87915078 93 return error;
vithyat 0:977e87915078 94 }
vithyat 0:977e87915078 95
vithyat 0:977e87915078 96 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(const uint8_t *tlv,
vithyat 0:977e87915078 97 uint32_t tlv_size,
vithyat 0:977e87915078 98 M2MResource &resource,
vithyat 0:977e87915078 99 M2MTLVDeserializer::Operation operation)
vithyat 0:977e87915078 100 {
vithyat 0:977e87915078 101 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 102 if (!is_multiple_resource(tlv)) {
vithyat 0:977e87915078 103 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 104 } else {
vithyat 0:977e87915078 105 tr_debug("M2MTLVDeserializer::deserialize_resource_instances()");
vithyat 0:977e87915078 106 uint8_t offset = 2;
vithyat 0:977e87915078 107
vithyat 0:977e87915078 108 ((tlv[0] & 0x20) == 0) ? offset : offset++;
vithyat 0:977e87915078 109
vithyat 0:977e87915078 110 uint8_t length = tlv[0] & 0x18;
vithyat 0:977e87915078 111 if(length == 0x08) {
vithyat 0:977e87915078 112 offset += 1;
vithyat 0:977e87915078 113 } else if(length == 0x10) {
vithyat 0:977e87915078 114 offset += 2;
vithyat 0:977e87915078 115 } else if(length == 0x18) {
vithyat 0:977e87915078 116 offset += 3;
vithyat 0:977e87915078 117 }
vithyat 0:977e87915078 118
vithyat 0:977e87915078 119 tr_debug("M2MTLVDeserializer::deserialize_resource_instances() Offset %d", offset);
vithyat 0:977e87915078 120 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, operation,false);
vithyat 0:977e87915078 121 if(M2MTLVDeserializer::None == error) {
vithyat 0:977e87915078 122 if (M2MTLVDeserializer::Put == operation) {
vithyat 0:977e87915078 123 remove_resource_instances(tlv, tlv_size, resource, offset);
vithyat 0:977e87915078 124 }
vithyat 0:977e87915078 125 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, operation,true);
vithyat 0:977e87915078 126 }
vithyat 0:977e87915078 127 }
vithyat 0:977e87915078 128 return error;
vithyat 0:977e87915078 129 }
vithyat 0:977e87915078 130
vithyat 0:977e87915078 131 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_object_instances(const uint8_t *tlv,
vithyat 0:977e87915078 132 uint32_t tlv_size,
vithyat 0:977e87915078 133 uint32_t offset,
vithyat 0:977e87915078 134 M2MObject &object,
vithyat 0:977e87915078 135 M2MTLVDeserializer::Operation operation,
vithyat 0:977e87915078 136 bool update_value)
vithyat 0:977e87915078 137 {
vithyat 0:977e87915078 138 tr_debug("M2MTLVDeserializer::deserialize_object_instances()");
vithyat 0:977e87915078 139 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 140 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 141 til.deserialize();
vithyat 0:977e87915078 142 offset = til._offset;
vithyat 0:977e87915078 143
vithyat 0:977e87915078 144 const M2MObjectInstanceList &list = object.instances();
vithyat 0:977e87915078 145 M2MObjectInstanceList::const_iterator it;
vithyat 0:977e87915078 146 it = list.begin();
vithyat 0:977e87915078 147
vithyat 0:977e87915078 148 if (TYPE_OBJECT_INSTANCE == til._type) {
vithyat 0:977e87915078 149 for (; it!=list.end(); it++) {
vithyat 0:977e87915078 150 if((*it)->instance_id() == til._id) {
vithyat 0:977e87915078 151 error = deserialize_resources(tlv, tlv_size, offset, (**it),operation, update_value);
vithyat 0:977e87915078 152 }
vithyat 0:977e87915078 153 }
vithyat 0:977e87915078 154 offset += til._length;
vithyat 0:977e87915078 155
vithyat 0:977e87915078 156 if(offset < tlv_size) {
vithyat 0:977e87915078 157 error = deserialize_object_instances(tlv, tlv_size, offset, object, operation, update_value);
vithyat 0:977e87915078 158 }
vithyat 0:977e87915078 159 }
vithyat 0:977e87915078 160 return error;
vithyat 0:977e87915078 161 }
vithyat 0:977e87915078 162
vithyat 0:977e87915078 163 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resources(const uint8_t *tlv,
vithyat 0:977e87915078 164 uint32_t tlv_size,
vithyat 0:977e87915078 165 uint32_t offset,
vithyat 0:977e87915078 166 M2MObjectInstance &object_instance,
vithyat 0:977e87915078 167 M2MTLVDeserializer::Operation operation,
vithyat 0:977e87915078 168 bool update_value)
vithyat 0:977e87915078 169 {
vithyat 0:977e87915078 170 tr_debug("M2MTLVDeserializer::deserialize_resources()");
vithyat 0:977e87915078 171 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 172 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 173 til.deserialize();
vithyat 0:977e87915078 174 offset = til._offset;
vithyat 0:977e87915078 175
vithyat 0:977e87915078 176 const M2MResourceList &list = object_instance.resources();
vithyat 0:977e87915078 177 M2MResourceList::const_iterator it;
vithyat 0:977e87915078 178 it = list.begin();
vithyat 0:977e87915078 179
vithyat 0:977e87915078 180 bool found = false;
vithyat 0:977e87915078 181 bool multi = false;
vithyat 0:977e87915078 182 if (TYPE_RESOURCE == til._type || TYPE_RESOURCE_INSTANCE == til._type) {
vithyat 0:977e87915078 183 multi = false;
vithyat 0:977e87915078 184 for (; it!=list.end(); it++) {
vithyat 0:977e87915078 185 if((*it)->name_id() == til._id){
vithyat 0:977e87915078 186 tr_debug("M2MTLVDeserializer::deserialize_resources() - Resource ID %d ", til._id);
vithyat 0:977e87915078 187 found = true;
vithyat 0:977e87915078 188 if(update_value) {
vithyat 0:977e87915078 189 if(til._length > 0) {
vithyat 0:977e87915078 190 tr_debug("M2MTLVDeserializer::deserialize_resources() - Update value");
vithyat 0:977e87915078 191 if (!set_resource_instance_value((*it), tlv+offset, til._length)) {
vithyat 0:977e87915078 192 error = M2MTLVDeserializer::OutOfMemory;
vithyat 0:977e87915078 193 break;
vithyat 0:977e87915078 194 }
vithyat 0:977e87915078 195 } else {
vithyat 0:977e87915078 196 tr_debug("M2MTLVDeserializer::deserialize_resources() - Clear Value");
vithyat 0:977e87915078 197 (*it)->clear_value();
vithyat 0:977e87915078 198 }
vithyat 0:977e87915078 199 break;
vithyat 0:977e87915078 200 } else if(0 == ((*it)->operation() & M2MBase::PUT_ALLOWED)) {
vithyat 0:977e87915078 201 tr_debug("M2MTLVDeserializer::deserialize_resources() - NOT_ALLOWED");
vithyat 0:977e87915078 202 error = M2MTLVDeserializer::NotAllowed;
vithyat 0:977e87915078 203 break;
vithyat 0:977e87915078 204 }
vithyat 0:977e87915078 205 }
vithyat 0:977e87915078 206 }
vithyat 0:977e87915078 207 } else if (TYPE_MULTIPLE_RESOURCE == til._type) {
vithyat 0:977e87915078 208 multi = true;
vithyat 0:977e87915078 209 for (; it!=list.end(); it++) {
vithyat 0:977e87915078 210 if((*it)->supports_multiple_instances() &&
vithyat 0:977e87915078 211 (*it)->name_id() == til._id) {
vithyat 0:977e87915078 212 found = true;
vithyat 0:977e87915078 213 error = deserialize_resource_instances(tlv, tlv_size, offset, (**it), object_instance, operation, update_value);
vithyat 0:977e87915078 214 }
vithyat 0:977e87915078 215 }
vithyat 0:977e87915078 216 } else {
vithyat 0:977e87915078 217 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 218 return error;
vithyat 0:977e87915078 219 }
vithyat 0:977e87915078 220
vithyat 0:977e87915078 221 if (!found) {
vithyat 0:977e87915078 222 if (M2MTLVDeserializer::Post == operation) {
vithyat 0:977e87915078 223 //Create a new Resource
vithyat 0:977e87915078 224 String id;
vithyat 0:977e87915078 225 id.append_int(til._id);
vithyat 0:977e87915078 226 M2MResource *resource = object_instance.create_dynamic_resource(id, "", M2MResourceInstance::OPAQUE, true, multi);
vithyat 0:977e87915078 227 if (resource) {
vithyat 0:977e87915078 228 resource->set_operation(M2MBase::GET_PUT_POST_DELETE_ALLOWED);
vithyat 0:977e87915078 229 if (TYPE_MULTIPLE_RESOURCE == til._type) {
vithyat 0:977e87915078 230 error = deserialize_resource_instances(tlv, tlv_size, offset, (*resource), object_instance, operation, update_value);
vithyat 0:977e87915078 231 }
vithyat 0:977e87915078 232 }
vithyat 0:977e87915078 233 } else if (M2MTLVDeserializer::Put == operation) {
vithyat 0:977e87915078 234 error = M2MTLVDeserializer::NotFound;
vithyat 0:977e87915078 235 }
vithyat 0:977e87915078 236 }
vithyat 0:977e87915078 237
vithyat 0:977e87915078 238 offset += til._length;
vithyat 0:977e87915078 239
vithyat 0:977e87915078 240 if (offset < tlv_size) {
vithyat 0:977e87915078 241 error = deserialize_resources(tlv, tlv_size, offset, object_instance, operation, update_value);
vithyat 0:977e87915078 242 }
vithyat 0:977e87915078 243
vithyat 0:977e87915078 244 return error;
vithyat 0:977e87915078 245 }
vithyat 0:977e87915078 246
vithyat 0:977e87915078 247 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(const uint8_t *tlv,
vithyat 0:977e87915078 248 uint32_t tlv_size,
vithyat 0:977e87915078 249 uint32_t offset,
vithyat 0:977e87915078 250 M2MResource &resource,
vithyat 0:977e87915078 251 M2MObjectInstance &object_instance,
vithyat 0:977e87915078 252 M2MTLVDeserializer::Operation operation,
vithyat 0:977e87915078 253 bool update_value)
vithyat 0:977e87915078 254 {
vithyat 0:977e87915078 255 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 256 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 257 til.deserialize();
vithyat 0:977e87915078 258 offset = til._offset;
vithyat 0:977e87915078 259
vithyat 0:977e87915078 260 if (TYPE_MULTIPLE_RESOURCE == til._type || TYPE_RESOURCE_INSTANCE == til._type) {
vithyat 0:977e87915078 261 const M2MResourceInstanceList &list = resource.resource_instances();
vithyat 0:977e87915078 262 M2MResourceInstanceList::const_iterator it;
vithyat 0:977e87915078 263 it = list.begin();
vithyat 0:977e87915078 264 bool found = false;
vithyat 0:977e87915078 265 for (; it!=list.end(); it++) {
vithyat 0:977e87915078 266 if((*it)->instance_id() == til._id && TYPE_RESOURCE_INSTANCE == til._type) {
vithyat 0:977e87915078 267 found = true;
vithyat 0:977e87915078 268 if(update_value) {
vithyat 0:977e87915078 269 if(til._length > 0) {
vithyat 0:977e87915078 270 if (!set_resource_instance_value((*it), tlv+offset, til._length)) {
vithyat 0:977e87915078 271 error = M2MTLVDeserializer::OutOfMemory;
vithyat 0:977e87915078 272 break;
vithyat 0:977e87915078 273 }
vithyat 0:977e87915078 274 } else {
vithyat 0:977e87915078 275 (*it)->clear_value();
vithyat 0:977e87915078 276 }
vithyat 0:977e87915078 277 break;
vithyat 0:977e87915078 278 } else if(0 == ((*it)->operation() & M2MBase::PUT_ALLOWED)) {
vithyat 0:977e87915078 279 error = M2MTLVDeserializer::NotAllowed;
vithyat 0:977e87915078 280 break;
vithyat 0:977e87915078 281 }
vithyat 0:977e87915078 282 }
vithyat 0:977e87915078 283 }
vithyat 0:977e87915078 284
vithyat 0:977e87915078 285 if(!found) {
vithyat 0:977e87915078 286 if(M2MTLVDeserializer::Post == operation) {
vithyat 0:977e87915078 287 // Create a new Resource Instance
vithyat 0:977e87915078 288 M2MResourceInstance *res_instance = object_instance.create_dynamic_resource_instance(resource.name(),"",
vithyat 0:977e87915078 289 resource.resource_instance_type(),
vithyat 0:977e87915078 290 true,
vithyat 0:977e87915078 291 til._id);
vithyat 0:977e87915078 292 if(res_instance) {
vithyat 0:977e87915078 293 res_instance->set_operation(M2MBase::GET_PUT_POST_DELETE_ALLOWED);
vithyat 0:977e87915078 294 }
vithyat 0:977e87915078 295 } else if(M2MTLVDeserializer::Put == operation) {
vithyat 0:977e87915078 296 error = M2MTLVDeserializer::NotFound;
vithyat 0:977e87915078 297 }
vithyat 0:977e87915078 298 }
vithyat 0:977e87915078 299 } else {
vithyat 0:977e87915078 300 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 301 return error;
vithyat 0:977e87915078 302 }
vithyat 0:977e87915078 303
vithyat 0:977e87915078 304 offset += til._length;
vithyat 0:977e87915078 305
vithyat 0:977e87915078 306 if(offset < tlv_size) {
vithyat 0:977e87915078 307 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, object_instance, operation, update_value);
vithyat 0:977e87915078 308 }
vithyat 0:977e87915078 309 return error;
vithyat 0:977e87915078 310 }
vithyat 0:977e87915078 311
vithyat 0:977e87915078 312 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(const uint8_t *tlv,
vithyat 0:977e87915078 313 uint32_t tlv_size,
vithyat 0:977e87915078 314 uint32_t offset,
vithyat 0:977e87915078 315 M2MResource &resource,
vithyat 0:977e87915078 316 M2MTLVDeserializer::Operation operation,
vithyat 0:977e87915078 317 bool update_value)
vithyat 0:977e87915078 318 {
vithyat 0:977e87915078 319 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 320 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 321 til.deserialize();
vithyat 0:977e87915078 322 offset = til._offset;
vithyat 0:977e87915078 323
vithyat 0:977e87915078 324 if (TYPE_RESOURCE_INSTANCE == til._type) {
vithyat 0:977e87915078 325 const M2MResourceInstanceList &list = resource.resource_instances();
vithyat 0:977e87915078 326 M2MResourceInstanceList::const_iterator it;
vithyat 0:977e87915078 327 it = list.begin();
vithyat 0:977e87915078 328 bool found = false;
vithyat 0:977e87915078 329 for (; it!=list.end(); it++) {
vithyat 0:977e87915078 330 if((*it)->instance_id() == til._id) {
vithyat 0:977e87915078 331 found = true;
vithyat 0:977e87915078 332 if(update_value) {
vithyat 0:977e87915078 333 if(til._length > 0) {
vithyat 0:977e87915078 334 if (!set_resource_instance_value((*it),tlv+offset, til._length)) {
vithyat 0:977e87915078 335 error = M2MTLVDeserializer::OutOfMemory;
vithyat 0:977e87915078 336 break;
vithyat 0:977e87915078 337 }
vithyat 0:977e87915078 338 } else {
vithyat 0:977e87915078 339 (*it)->clear_value();
vithyat 0:977e87915078 340 }
vithyat 0:977e87915078 341 break;
vithyat 0:977e87915078 342 } else if(0 == ((*it)->operation() & M2MBase::PUT_ALLOWED)) {
vithyat 0:977e87915078 343 error = M2MTLVDeserializer::NotAllowed;
vithyat 0:977e87915078 344 break;
vithyat 0:977e87915078 345 }
vithyat 0:977e87915078 346 }
vithyat 0:977e87915078 347 }
vithyat 0:977e87915078 348 if(!found) {
vithyat 0:977e87915078 349 if(M2MTLVDeserializer::Post == operation) {
vithyat 0:977e87915078 350 error = M2MTLVDeserializer::NotAllowed;
vithyat 0:977e87915078 351 } else if(M2MTLVDeserializer::Put == operation) {
vithyat 0:977e87915078 352 error = M2MTLVDeserializer::NotFound;
vithyat 0:977e87915078 353 }
vithyat 0:977e87915078 354 }
vithyat 0:977e87915078 355 } else {
vithyat 0:977e87915078 356 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 357 return error;
vithyat 0:977e87915078 358 }
vithyat 0:977e87915078 359
vithyat 0:977e87915078 360 offset += til._length;
vithyat 0:977e87915078 361
vithyat 0:977e87915078 362 if(offset < tlv_size) {
vithyat 0:977e87915078 363 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, operation, update_value);
vithyat 0:977e87915078 364 }
vithyat 0:977e87915078 365 return error;
vithyat 0:977e87915078 366 }
vithyat 0:977e87915078 367
vithyat 0:977e87915078 368 bool M2MTLVDeserializer::is_object_instance(const uint8_t *tlv, uint32_t offset)
vithyat 0:977e87915078 369 {
vithyat 0:977e87915078 370 bool ret = false;
vithyat 0:977e87915078 371 if (tlv) {
vithyat 0:977e87915078 372 uint8_t value = tlv[offset];
vithyat 0:977e87915078 373 ret = (TYPE_OBJECT_INSTANCE == (value & TYPE_RESOURCE));
vithyat 0:977e87915078 374 }
vithyat 0:977e87915078 375 return ret;
vithyat 0:977e87915078 376 }
vithyat 0:977e87915078 377
vithyat 0:977e87915078 378 uint16_t M2MTLVDeserializer::instance_id(const uint8_t *tlv)
vithyat 0:977e87915078 379 {
vithyat 0:977e87915078 380 TypeIdLength til(tlv, 0);
vithyat 0:977e87915078 381 til.deserialize();
vithyat 0:977e87915078 382 uint16_t id = til._id;
vithyat 0:977e87915078 383 return id;
vithyat 0:977e87915078 384 }
vithyat 0:977e87915078 385
vithyat 0:977e87915078 386 bool M2MTLVDeserializer::is_resource(const uint8_t *tlv, uint32_t offset)
vithyat 0:977e87915078 387 {
vithyat 0:977e87915078 388 bool ret = false;
vithyat 0:977e87915078 389 if (tlv) {
vithyat 0:977e87915078 390 ret = (TYPE_RESOURCE == (tlv[offset] & TYPE_RESOURCE));
vithyat 0:977e87915078 391 }
vithyat 0:977e87915078 392 return ret;
vithyat 0:977e87915078 393 }
vithyat 0:977e87915078 394
vithyat 0:977e87915078 395 bool M2MTLVDeserializer::is_multiple_resource(const uint8_t *tlv, uint32_t offset)
vithyat 0:977e87915078 396 {
vithyat 0:977e87915078 397 bool ret = false;
vithyat 0:977e87915078 398 if (tlv) {
vithyat 0:977e87915078 399 ret = (TYPE_MULTIPLE_RESOURCE == (tlv[offset] & TYPE_RESOURCE));
vithyat 0:977e87915078 400 }
vithyat 0:977e87915078 401 return ret;
vithyat 0:977e87915078 402 }
vithyat 0:977e87915078 403
vithyat 0:977e87915078 404 bool M2MTLVDeserializer::is_resource_instance(const uint8_t *tlv, uint32_t offset)
vithyat 0:977e87915078 405 {
vithyat 0:977e87915078 406 bool ret = false;
vithyat 0:977e87915078 407 if (tlv) {
vithyat 0:977e87915078 408 ret = (TYPE_RESOURCE_INSTANCE == (tlv[offset] & TYPE_RESOURCE));
vithyat 0:977e87915078 409 }
vithyat 0:977e87915078 410 return ret;
vithyat 0:977e87915078 411 }
vithyat 0:977e87915078 412
vithyat 0:977e87915078 413 bool M2MTLVDeserializer::set_resource_instance_value(M2MResourceBase *res, const uint8_t *tlv, const uint32_t size)
vithyat 0:977e87915078 414 {
vithyat 0:977e87915078 415 bool success = true;
vithyat 0:977e87915078 416 switch (res->resource_instance_type()) {
vithyat 0:977e87915078 417 case M2MResourceBase::INTEGER:
vithyat 0:977e87915078 418 case M2MResourceBase::BOOLEAN:
vithyat 0:977e87915078 419 case M2MResourceBase::TIME:
vithyat 0:977e87915078 420 {
vithyat 0:977e87915078 421 int64_t value = String::convert_array_to_integer(tlv, size);
vithyat 0:977e87915078 422 if (!res->set_value(value)) {
vithyat 0:977e87915078 423 success = false;
vithyat 0:977e87915078 424 }
vithyat 0:977e87915078 425 break;
vithyat 0:977e87915078 426 // Todo! implement conversion for other types as well
vithyat 0:977e87915078 427 }
vithyat 0:977e87915078 428 case M2MResourceBase::STRING:
vithyat 0:977e87915078 429 case M2MResourceBase::OPAQUE:
vithyat 0:977e87915078 430 case M2MResourceBase::OBJLINK:
vithyat 0:977e87915078 431 if (!res->set_value(tlv, size)) {
vithyat 0:977e87915078 432 success = false;
vithyat 0:977e87915078 433 }
vithyat 0:977e87915078 434 break;
vithyat 0:977e87915078 435 case M2MResourceBase::FLOAT:
vithyat 0:977e87915078 436 {
vithyat 0:977e87915078 437 uint32_t value = common_read_32_bit(tlv);
vithyat 0:977e87915078 438 if (!res->set_value_float(*(float*)&value)) {
vithyat 0:977e87915078 439 success = false;
vithyat 0:977e87915078 440 }
vithyat 0:977e87915078 441 break;
vithyat 0:977e87915078 442 }
vithyat 0:977e87915078 443 default:
vithyat 0:977e87915078 444 success = false;
vithyat 0:977e87915078 445 break;
vithyat 0:977e87915078 446 }
vithyat 0:977e87915078 447
vithyat 0:977e87915078 448 return success;
vithyat 0:977e87915078 449 }
vithyat 0:977e87915078 450
vithyat 0:977e87915078 451 void M2MTLVDeserializer::remove_resources(const uint8_t *tlv,
vithyat 0:977e87915078 452 uint32_t tlv_size,
vithyat 0:977e87915078 453 M2MObjectInstance &object_instance,
vithyat 0:977e87915078 454 uint32_t offset_size)
vithyat 0:977e87915078 455 {
vithyat 0:977e87915078 456 tr_debug("M2MTLVDeserializer::remove_resources");
vithyat 0:977e87915078 457 uint32_t offset = offset_size;
vithyat 0:977e87915078 458 const M2MResourceList &list = object_instance.resources();
vithyat 0:977e87915078 459 M2MResourceList::const_iterator it;
vithyat 0:977e87915078 460
vithyat 0:977e87915078 461 it = list.begin();
vithyat 0:977e87915078 462 for (; it!=list.end();) {
vithyat 0:977e87915078 463 bool found = false;
vithyat 0:977e87915078 464 while(offset < tlv_size) {
vithyat 0:977e87915078 465 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 466 til.deserialize();
vithyat 0:977e87915078 467 offset = til._offset;
vithyat 0:977e87915078 468 offset += til._length;
vithyat 0:977e87915078 469 if((*it)->name_id() == til._id){
vithyat 0:977e87915078 470 offset = offset_size;
vithyat 0:977e87915078 471 found = true;
vithyat 0:977e87915078 472 break;
vithyat 0:977e87915078 473 }
vithyat 0:977e87915078 474 }
vithyat 0:977e87915078 475 offset = offset_size;
vithyat 0:977e87915078 476
vithyat 0:977e87915078 477 // Remove resource if not part of the TLV message
vithyat 0:977e87915078 478 if (!found) {
vithyat 0:977e87915078 479 tr_debug("M2MTLVDeserializer::remove_resources - remove resource %" PRId32, (*it)->name_id());
vithyat 0:977e87915078 480 object_instance.remove_resource((*it)->name());
vithyat 0:977e87915078 481 } else {
vithyat 0:977e87915078 482 ++it;
vithyat 0:977e87915078 483 }
vithyat 0:977e87915078 484 }
vithyat 0:977e87915078 485 }
vithyat 0:977e87915078 486
vithyat 0:977e87915078 487 void M2MTLVDeserializer::remove_resource_instances(const uint8_t *tlv,
vithyat 0:977e87915078 488 uint32_t tlv_size,
vithyat 0:977e87915078 489 M2MResource &resource,
vithyat 0:977e87915078 490 uint32_t offset_size)
vithyat 0:977e87915078 491 {
vithyat 0:977e87915078 492 tr_debug("M2MTLVDeserializer::remove_resource_instances");
vithyat 0:977e87915078 493 uint32_t offset = offset_size;
vithyat 0:977e87915078 494 const M2MResourceInstanceList &list = resource.resource_instances();
vithyat 0:977e87915078 495 M2MResourceInstanceList::const_iterator it;
vithyat 0:977e87915078 496 it = list.begin();
vithyat 0:977e87915078 497
vithyat 0:977e87915078 498 for (; it!=list.end();) {
vithyat 0:977e87915078 499 bool found = false;
vithyat 0:977e87915078 500 while (offset < tlv_size) {
vithyat 0:977e87915078 501 TypeIdLength til(tlv, offset);
vithyat 0:977e87915078 502 til.deserialize();
vithyat 0:977e87915078 503 offset = til._offset;
vithyat 0:977e87915078 504 offset += til._length;
vithyat 0:977e87915078 505 if ((*it)->instance_id() == til._id){
vithyat 0:977e87915078 506 offset = offset_size;
vithyat 0:977e87915078 507 found = true;
vithyat 0:977e87915078 508 break;
vithyat 0:977e87915078 509 }
vithyat 0:977e87915078 510 }
vithyat 0:977e87915078 511 offset = offset_size;
vithyat 0:977e87915078 512
vithyat 0:977e87915078 513 // Remove resource instance if not part of the TLV message
vithyat 0:977e87915078 514 if (!found) {
vithyat 0:977e87915078 515 tr_debug("M2MTLVDeserializer::remove_resource_instances - remove resource instance %d", (*it)->instance_id());
vithyat 0:977e87915078 516 resource.remove_resource_instance((*it)->instance_id());
vithyat 0:977e87915078 517 } else {
vithyat 0:977e87915078 518 ++it;
vithyat 0:977e87915078 519 }
vithyat 0:977e87915078 520 }
vithyat 0:977e87915078 521 }
vithyat 0:977e87915078 522
vithyat 0:977e87915078 523 TypeIdLength::TypeIdLength(const uint8_t *tlv, uint32_t offset)
vithyat 0:977e87915078 524 : _tlv(tlv), _offset(offset), _type(tlv[offset] & 0xC0), _id(0), _length(0)
vithyat 0:977e87915078 525 {
vithyat 0:977e87915078 526 }
vithyat 0:977e87915078 527
vithyat 0:977e87915078 528 void TypeIdLength::deserialize()
vithyat 0:977e87915078 529 {
vithyat 0:977e87915078 530 uint32_t idLength = _tlv[_offset] & ID16;
vithyat 0:977e87915078 531 uint32_t lengthType = _tlv[_offset] & LENGTH24;
vithyat 0:977e87915078 532 if (0 == lengthType) {
vithyat 0:977e87915078 533 _length = _tlv[_offset] & 0x07;
vithyat 0:977e87915078 534 }
vithyat 0:977e87915078 535 _offset++;
vithyat 0:977e87915078 536
vithyat 0:977e87915078 537 deserialiseID(idLength);
vithyat 0:977e87915078 538 deserialiseLength(lengthType);
vithyat 0:977e87915078 539 }
vithyat 0:977e87915078 540
vithyat 0:977e87915078 541 void TypeIdLength::deserialiseID(uint32_t idLength)
vithyat 0:977e87915078 542 {
vithyat 0:977e87915078 543 _id = _tlv[_offset++] & 0xFF;
vithyat 0:977e87915078 544 if (ID16 == idLength) {
vithyat 0:977e87915078 545 _id = (_id << 8) + (_tlv[_offset++] & 0xFF);
vithyat 0:977e87915078 546 }
vithyat 0:977e87915078 547 }
vithyat 0:977e87915078 548
vithyat 0:977e87915078 549 void TypeIdLength::deserialiseLength(uint32_t lengthType)
vithyat 0:977e87915078 550 {
vithyat 0:977e87915078 551 if (lengthType > 0) {
vithyat 0:977e87915078 552 _length = _tlv[_offset++] & 0xFF;
vithyat 0:977e87915078 553 }
vithyat 0:977e87915078 554 if (lengthType > LENGTH8) {
vithyat 0:977e87915078 555 _length = (_length << 8) + (_tlv[_offset++] & 0xFF);
vithyat 0:977e87915078 556 }
vithyat 0:977e87915078 557 if (lengthType > LENGTH16) {
vithyat 0:977e87915078 558 _length = (_length << 8) + (_tlv[_offset++] & 0xFF);
vithyat 0:977e87915078 559 }
vithyat 0:977e87915078 560 }