leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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