Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include <stdio.h>
nexpaq 0:6c56fb4bc5f0 17 #include "include/m2mtlvdeserializer.h"
nexpaq 0:6c56fb4bc5f0 18 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 19 #include "include/nsdllinker.h"
nexpaq 0:6c56fb4bc5f0 20 #include "mbed-trace/mbed_trace.h"
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 23 #define BUFFER_SIZE 10
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25 M2MTLVDeserializer::M2MTLVDeserializer()
nexpaq 0:6c56fb4bc5f0 26 {
nexpaq 0:6c56fb4bc5f0 27 }
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 M2MTLVDeserializer::~M2MTLVDeserializer()
nexpaq 0:6c56fb4bc5f0 30 {
nexpaq 0:6c56fb4bc5f0 31 }
nexpaq 0:6c56fb4bc5f0 32
nexpaq 0:6c56fb4bc5f0 33 bool M2MTLVDeserializer::is_object_instance(uint8_t *tlv)
nexpaq 0:6c56fb4bc5f0 34 {
nexpaq 0:6c56fb4bc5f0 35 return is_object_instance(tlv, 0);
nexpaq 0:6c56fb4bc5f0 36 }
nexpaq 0:6c56fb4bc5f0 37
nexpaq 0:6c56fb4bc5f0 38 bool M2MTLVDeserializer::is_resource(uint8_t *tlv)
nexpaq 0:6c56fb4bc5f0 39 {
nexpaq 0:6c56fb4bc5f0 40 return is_resource(tlv, 0);
nexpaq 0:6c56fb4bc5f0 41 }
nexpaq 0:6c56fb4bc5f0 42
nexpaq 0:6c56fb4bc5f0 43 bool M2MTLVDeserializer::is_multiple_resource(uint8_t *tlv)
nexpaq 0:6c56fb4bc5f0 44 {
nexpaq 0:6c56fb4bc5f0 45 return is_multiple_resource(tlv, 0);
nexpaq 0:6c56fb4bc5f0 46 }
nexpaq 0:6c56fb4bc5f0 47
nexpaq 0:6c56fb4bc5f0 48 bool M2MTLVDeserializer::is_resource_instance(uint8_t *tlv)
nexpaq 0:6c56fb4bc5f0 49 {
nexpaq 0:6c56fb4bc5f0 50 return is_resource_instance(tlv, 0);
nexpaq 0:6c56fb4bc5f0 51 }
nexpaq 0:6c56fb4bc5f0 52
nexpaq 0:6c56fb4bc5f0 53 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialise_object_instances(uint8_t* tlv,
nexpaq 0:6c56fb4bc5f0 54 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 55 M2MObject &object,
nexpaq 0:6c56fb4bc5f0 56 M2MTLVDeserializer::Operation operation)
nexpaq 0:6c56fb4bc5f0 57 {
nexpaq 0:6c56fb4bc5f0 58 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 59 if (is_object_instance(tlv) ) {
nexpaq 0:6c56fb4bc5f0 60 tr_debug("M2MTLVDeserializer::deserialise_object_instances");
nexpaq 0:6c56fb4bc5f0 61 error = deserialize_object_instances(tlv, tlv_size, 0, object,operation,false);
nexpaq 0:6c56fb4bc5f0 62 if(M2MTLVDeserializer::None == error) {
nexpaq 0:6c56fb4bc5f0 63 error = deserialize_object_instances(tlv, tlv_size, 0, object,operation,true);
nexpaq 0:6c56fb4bc5f0 64 }
nexpaq 0:6c56fb4bc5f0 65 } else {
nexpaq 0:6c56fb4bc5f0 66 tr_debug("M2MTLVDeserializer::deserialise_object_instances ::NotValid");
nexpaq 0:6c56fb4bc5f0 67 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 68 }
nexpaq 0:6c56fb4bc5f0 69 return error;
nexpaq 0:6c56fb4bc5f0 70 }
nexpaq 0:6c56fb4bc5f0 71
nexpaq 0:6c56fb4bc5f0 72 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resources(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 73 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 74 M2MObjectInstance &object_instance,
nexpaq 0:6c56fb4bc5f0 75 M2MTLVDeserializer::Operation operation)
nexpaq 0:6c56fb4bc5f0 76 {
nexpaq 0:6c56fb4bc5f0 77 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 78 if (!is_resource(tlv) && !is_multiple_resource(tlv)) {
nexpaq 0:6c56fb4bc5f0 79 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 80 } else {
nexpaq 0:6c56fb4bc5f0 81 error = deserialize_resources(tlv, tlv_size, 0, object_instance, operation,false);
nexpaq 0:6c56fb4bc5f0 82 if(M2MTLVDeserializer::None == error) {
nexpaq 0:6c56fb4bc5f0 83 error = deserialize_resources(tlv, tlv_size, 0, object_instance, operation,true);
nexpaq 0:6c56fb4bc5f0 84 }
nexpaq 0:6c56fb4bc5f0 85 }
nexpaq 0:6c56fb4bc5f0 86 return error;
nexpaq 0:6c56fb4bc5f0 87 }
nexpaq 0:6c56fb4bc5f0 88
nexpaq 0:6c56fb4bc5f0 89 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 90 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 91 M2MResource &resource,
nexpaq 0:6c56fb4bc5f0 92 M2MTLVDeserializer::Operation operation)
nexpaq 0:6c56fb4bc5f0 93 {
nexpaq 0:6c56fb4bc5f0 94 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 95 if (!is_multiple_resource(tlv)) {
nexpaq 0:6c56fb4bc5f0 96 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 97 } else {
nexpaq 0:6c56fb4bc5f0 98 tr_debug("M2MTLVDeserializer::deserialize_resource_instances()");
nexpaq 0:6c56fb4bc5f0 99 uint8_t offset = 2;
nexpaq 0:6c56fb4bc5f0 100
nexpaq 0:6c56fb4bc5f0 101 ((tlv[0] & 0x20) == 0) ? offset : offset++;
nexpaq 0:6c56fb4bc5f0 102
nexpaq 0:6c56fb4bc5f0 103 uint8_t length = tlv[0] & 0x18;
nexpaq 0:6c56fb4bc5f0 104 if(length == 0x08) {
nexpaq 0:6c56fb4bc5f0 105 offset+= 1;
nexpaq 0:6c56fb4bc5f0 106 } else if(length == 0x10) {
nexpaq 0:6c56fb4bc5f0 107 offset+= 2;
nexpaq 0:6c56fb4bc5f0 108 } else if(length == 0x18) {
nexpaq 0:6c56fb4bc5f0 109 offset+= 3;
nexpaq 0:6c56fb4bc5f0 110 }
nexpaq 0:6c56fb4bc5f0 111
nexpaq 0:6c56fb4bc5f0 112 tr_debug("M2MTLVDeserializer::deserialize_resource_instances() Offset %d", offset);
nexpaq 0:6c56fb4bc5f0 113 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, operation,false);
nexpaq 0:6c56fb4bc5f0 114 if(M2MTLVDeserializer::None == error) {
nexpaq 0:6c56fb4bc5f0 115 error = deserialize_resource_instances(tlv, tlv_size, offset, resource, operation,true);
nexpaq 0:6c56fb4bc5f0 116 }
nexpaq 0:6c56fb4bc5f0 117 }
nexpaq 0:6c56fb4bc5f0 118 return error;
nexpaq 0:6c56fb4bc5f0 119 }
nexpaq 0:6c56fb4bc5f0 120
nexpaq 0:6c56fb4bc5f0 121 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_object_instances(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 122 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 123 uint32_t offset,
nexpaq 0:6c56fb4bc5f0 124 M2MObject &object,
nexpaq 0:6c56fb4bc5f0 125 M2MTLVDeserializer::Operation operation,
nexpaq 0:6c56fb4bc5f0 126 bool update_value)
nexpaq 0:6c56fb4bc5f0 127 {
nexpaq 0:6c56fb4bc5f0 128 tr_debug("M2MTLVDeserializer::deserialize_object_instances()");
nexpaq 0:6c56fb4bc5f0 129 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 130 TypeIdLength *til = TypeIdLength::createTypeIdLength(tlv, offset)->deserialize();
nexpaq 0:6c56fb4bc5f0 131 offset = til->_offset;
nexpaq 0:6c56fb4bc5f0 132
nexpaq 0:6c56fb4bc5f0 133 M2MObjectInstanceList list = object.instances();
nexpaq 0:6c56fb4bc5f0 134 M2MObjectInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 135 it = list.begin();
nexpaq 0:6c56fb4bc5f0 136
nexpaq 0:6c56fb4bc5f0 137 if (TYPE_OBJECT_INSTANCE == til->_type) {
nexpaq 0:6c56fb4bc5f0 138 for (; it!=list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 139 if((*it)->instance_id() == til->_id) {
nexpaq 0:6c56fb4bc5f0 140 error = deserialize_resources(tlv, tlv_size, offset, (**it),operation, update_value);
nexpaq 0:6c56fb4bc5f0 141 }
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143 offset += til->_length;
nexpaq 0:6c56fb4bc5f0 144
nexpaq 0:6c56fb4bc5f0 145 if(offset < tlv_size) {
nexpaq 0:6c56fb4bc5f0 146 error = deserialize_object_instances(tlv, tlv_size, offset, object, operation, update_value);
nexpaq 0:6c56fb4bc5f0 147 }
nexpaq 0:6c56fb4bc5f0 148 delete til;
nexpaq 0:6c56fb4bc5f0 149 }
nexpaq 0:6c56fb4bc5f0 150 return error;
nexpaq 0:6c56fb4bc5f0 151 }
nexpaq 0:6c56fb4bc5f0 152
nexpaq 0:6c56fb4bc5f0 153 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resources(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 154 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 155 uint32_t offset,
nexpaq 0:6c56fb4bc5f0 156 M2MObjectInstance &object_instance,
nexpaq 0:6c56fb4bc5f0 157 M2MTLVDeserializer::Operation operation,
nexpaq 0:6c56fb4bc5f0 158 bool update_value)
nexpaq 0:6c56fb4bc5f0 159 {
nexpaq 0:6c56fb4bc5f0 160 tr_debug("M2MTLVDeserializer::deserialize_resources()");
nexpaq 0:6c56fb4bc5f0 161 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 162 TypeIdLength *til = TypeIdLength::createTypeIdLength(tlv, offset)->deserialize();
nexpaq 0:6c56fb4bc5f0 163 offset = til->_offset;
nexpaq 0:6c56fb4bc5f0 164
nexpaq 0:6c56fb4bc5f0 165 M2MResourceList list = object_instance.resources();
nexpaq 0:6c56fb4bc5f0 166 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 167 it = list.begin();
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 if (TYPE_RESOURCE == til->_type || TYPE_RESOURCE_INSTANCE == til->_type) {
nexpaq 0:6c56fb4bc5f0 170 bool found = false;
nexpaq 0:6c56fb4bc5f0 171 for (; it!=list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 172 if((*it)->name_id() == til->_id){
nexpaq 0:6c56fb4bc5f0 173 tr_debug("M2MTLVDeserializer::deserialize_resources() - Resource ID %d ", til->_id);
nexpaq 0:6c56fb4bc5f0 174 found = true;
nexpaq 0:6c56fb4bc5f0 175 if(update_value) {
nexpaq 0:6c56fb4bc5f0 176 if(til->_length > 0) {
nexpaq 0:6c56fb4bc5f0 177 tr_debug("M2MTLVDeserializer::deserialize_resources() - Update value");
nexpaq 0:6c56fb4bc5f0 178 (*it)->set_value(tlv+offset, til->_length);
nexpaq 0:6c56fb4bc5f0 179 } else {
nexpaq 0:6c56fb4bc5f0 180 tr_debug("M2MTLVDeserializer::deserialize_resources() - Clear Value");
nexpaq 0:6c56fb4bc5f0 181 (*it)->clear_value();
nexpaq 0:6c56fb4bc5f0 182 }
nexpaq 0:6c56fb4bc5f0 183 break;
nexpaq 0:6c56fb4bc5f0 184 } else if(0 == ((*it)->operation() & SN_GRS_PUT_ALLOWED)) {
nexpaq 0:6c56fb4bc5f0 185 tr_debug("M2MTLVDeserializer::deserialize_resources() - NOT_ALLOWED");
nexpaq 0:6c56fb4bc5f0 186 error = M2MTLVDeserializer::NotAllowed;
nexpaq 0:6c56fb4bc5f0 187 break;
nexpaq 0:6c56fb4bc5f0 188 }
nexpaq 0:6c56fb4bc5f0 189 }
nexpaq 0:6c56fb4bc5f0 190 }
nexpaq 0:6c56fb4bc5f0 191 if(!found) {
nexpaq 0:6c56fb4bc5f0 192 if(M2MTLVDeserializer::Post == operation) {
nexpaq 0:6c56fb4bc5f0 193 //Create a new Resource
nexpaq 0:6c56fb4bc5f0 194 String id;
nexpaq 0:6c56fb4bc5f0 195 id.append_int(til->_id);
nexpaq 0:6c56fb4bc5f0 196 M2MResource *resource = object_instance.create_dynamic_resource(id,"",M2MResourceInstance::INTEGER,true,false);
nexpaq 0:6c56fb4bc5f0 197 if(resource) {
nexpaq 0:6c56fb4bc5f0 198 resource->set_operation(M2MBase::GET_PUT_POST_DELETE_ALLOWED);
nexpaq 0:6c56fb4bc5f0 199 }
nexpaq 0:6c56fb4bc5f0 200 } else if(M2MTLVDeserializer::Put == operation) {
nexpaq 0:6c56fb4bc5f0 201 error = M2MTLVDeserializer::NotFound;
nexpaq 0:6c56fb4bc5f0 202 }
nexpaq 0:6c56fb4bc5f0 203 }
nexpaq 0:6c56fb4bc5f0 204 } else if (TYPE_MULTIPLE_RESOURCE == til->_type) {
nexpaq 0:6c56fb4bc5f0 205 for (; it!=list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 206 if((*it)->supports_multiple_instances()) {
nexpaq 0:6c56fb4bc5f0 207 error = deserialize_resource_instances(tlv, tlv_size-offset, offset, (**it), object_instance, operation, update_value);
nexpaq 0:6c56fb4bc5f0 208 }
nexpaq 0:6c56fb4bc5f0 209 }
nexpaq 0:6c56fb4bc5f0 210
nexpaq 0:6c56fb4bc5f0 211 } else {
nexpaq 0:6c56fb4bc5f0 212 delete til;
nexpaq 0:6c56fb4bc5f0 213 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 214 return error;
nexpaq 0:6c56fb4bc5f0 215 }
nexpaq 0:6c56fb4bc5f0 216 offset += til->_length;
nexpaq 0:6c56fb4bc5f0 217
nexpaq 0:6c56fb4bc5f0 218 delete til;
nexpaq 0:6c56fb4bc5f0 219 if(offset < tlv_size) {
nexpaq 0:6c56fb4bc5f0 220 error = deserialize_resources(tlv, tlv_size, offset, object_instance, operation, update_value);
nexpaq 0:6c56fb4bc5f0 221 }
nexpaq 0:6c56fb4bc5f0 222 return error;
nexpaq 0:6c56fb4bc5f0 223 }
nexpaq 0:6c56fb4bc5f0 224
nexpaq 0:6c56fb4bc5f0 225 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 226 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 227 uint32_t offset,
nexpaq 0:6c56fb4bc5f0 228 M2MResource &resource,
nexpaq 0:6c56fb4bc5f0 229 M2MObjectInstance &object_instance,
nexpaq 0:6c56fb4bc5f0 230 M2MTLVDeserializer::Operation operation,
nexpaq 0:6c56fb4bc5f0 231 bool update_value)
nexpaq 0:6c56fb4bc5f0 232 {
nexpaq 0:6c56fb4bc5f0 233 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 234 TypeIdLength *til = TypeIdLength::createTypeIdLength(tlv, offset)->deserialize();
nexpaq 0:6c56fb4bc5f0 235 offset = til->_offset;
nexpaq 0:6c56fb4bc5f0 236
nexpaq 0:6c56fb4bc5f0 237 if (TYPE_MULTIPLE_RESOURCE == til->_type || TYPE_RESOURCE_INSTANCE == til->_type) {
nexpaq 0:6c56fb4bc5f0 238 M2MResourceInstanceList list = resource.resource_instances();
nexpaq 0:6c56fb4bc5f0 239 M2MResourceInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 240 it = list.begin();
nexpaq 0:6c56fb4bc5f0 241 bool found = false;
nexpaq 0:6c56fb4bc5f0 242 for (; it!=list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 243 if((*it)->instance_id() == til->_id) {
nexpaq 0:6c56fb4bc5f0 244 found = true;
nexpaq 0:6c56fb4bc5f0 245 if(update_value) {
nexpaq 0:6c56fb4bc5f0 246 if(til->_length > 0) {
nexpaq 0:6c56fb4bc5f0 247 (*it)->set_value(tlv+offset, til->_length);
nexpaq 0:6c56fb4bc5f0 248 } else {
nexpaq 0:6c56fb4bc5f0 249 (*it)->clear_value();
nexpaq 0:6c56fb4bc5f0 250 }
nexpaq 0:6c56fb4bc5f0 251 break;
nexpaq 0:6c56fb4bc5f0 252 } else if(0 == ((*it)->operation() & SN_GRS_PUT_ALLOWED)) {
nexpaq 0:6c56fb4bc5f0 253 error = M2MTLVDeserializer::NotAllowed;
nexpaq 0:6c56fb4bc5f0 254 break;
nexpaq 0:6c56fb4bc5f0 255 }
nexpaq 0:6c56fb4bc5f0 256 }
nexpaq 0:6c56fb4bc5f0 257 }
nexpaq 0:6c56fb4bc5f0 258 if(!found) {
nexpaq 0:6c56fb4bc5f0 259 if(M2MTLVDeserializer::Post == operation) {
nexpaq 0:6c56fb4bc5f0 260 // Create a new Resource Instance
nexpaq 0:6c56fb4bc5f0 261 M2MResourceInstance *res_instance = object_instance.create_dynamic_resource_instance(resource.name(),"",
nexpaq 0:6c56fb4bc5f0 262 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 263 true,
nexpaq 0:6c56fb4bc5f0 264 til->_id);
nexpaq 0:6c56fb4bc5f0 265 if(res_instance) {
nexpaq 0:6c56fb4bc5f0 266 res_instance->set_operation(M2MBase::GET_PUT_POST_DELETE_ALLOWED);
nexpaq 0:6c56fb4bc5f0 267 }
nexpaq 0:6c56fb4bc5f0 268 } else if(M2MTLVDeserializer::Put == operation) {
nexpaq 0:6c56fb4bc5f0 269 error = M2MTLVDeserializer::NotFound;
nexpaq 0:6c56fb4bc5f0 270 }
nexpaq 0:6c56fb4bc5f0 271 }
nexpaq 0:6c56fb4bc5f0 272 } else {
nexpaq 0:6c56fb4bc5f0 273 delete til;
nexpaq 0:6c56fb4bc5f0 274 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 275 return error;
nexpaq 0:6c56fb4bc5f0 276 }
nexpaq 0:6c56fb4bc5f0 277
nexpaq 0:6c56fb4bc5f0 278 offset += til->_length;
nexpaq 0:6c56fb4bc5f0 279
nexpaq 0:6c56fb4bc5f0 280 delete til;
nexpaq 0:6c56fb4bc5f0 281 if(offset < tlv_size) {
nexpaq 0:6c56fb4bc5f0 282 error = deserialize_resource_instances(tlv, tlv_size-offset, offset, resource, object_instance, operation, update_value);
nexpaq 0:6c56fb4bc5f0 283 }
nexpaq 0:6c56fb4bc5f0 284 return error;
nexpaq 0:6c56fb4bc5f0 285 }
nexpaq 0:6c56fb4bc5f0 286
nexpaq 0:6c56fb4bc5f0 287 M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(uint8_t *tlv,
nexpaq 0:6c56fb4bc5f0 288 uint32_t tlv_size,
nexpaq 0:6c56fb4bc5f0 289 uint32_t offset,
nexpaq 0:6c56fb4bc5f0 290 M2MResource &resource,
nexpaq 0:6c56fb4bc5f0 291 M2MTLVDeserializer::Operation operation,
nexpaq 0:6c56fb4bc5f0 292 bool update_value)
nexpaq 0:6c56fb4bc5f0 293 {
nexpaq 0:6c56fb4bc5f0 294 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 295 TypeIdLength *til = TypeIdLength::createTypeIdLength(tlv, offset)->deserialize();
nexpaq 0:6c56fb4bc5f0 296 offset = til->_offset;
nexpaq 0:6c56fb4bc5f0 297
nexpaq 0:6c56fb4bc5f0 298 if (TYPE_RESOURCE_INSTANCE == til->_type) {
nexpaq 0:6c56fb4bc5f0 299 M2MResourceInstanceList list = resource.resource_instances();
nexpaq 0:6c56fb4bc5f0 300 M2MResourceInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 301 it = list.begin();
nexpaq 0:6c56fb4bc5f0 302 bool found = false;
nexpaq 0:6c56fb4bc5f0 303 for (; it!=list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 304 if((*it)->instance_id() == til->_id) {
nexpaq 0:6c56fb4bc5f0 305 found = true;
nexpaq 0:6c56fb4bc5f0 306 if(update_value) {
nexpaq 0:6c56fb4bc5f0 307 if(til->_length > 0) {
nexpaq 0:6c56fb4bc5f0 308 (*it)->set_value(tlv+offset, til->_length);
nexpaq 0:6c56fb4bc5f0 309 } else {
nexpaq 0:6c56fb4bc5f0 310 (*it)->clear_value();
nexpaq 0:6c56fb4bc5f0 311 }
nexpaq 0:6c56fb4bc5f0 312 break;
nexpaq 0:6c56fb4bc5f0 313 } else if(0 == ((*it)->operation() & SN_GRS_PUT_ALLOWED)) {
nexpaq 0:6c56fb4bc5f0 314 error = M2MTLVDeserializer::NotAllowed;
nexpaq 0:6c56fb4bc5f0 315 break;
nexpaq 0:6c56fb4bc5f0 316 }
nexpaq 0:6c56fb4bc5f0 317 }
nexpaq 0:6c56fb4bc5f0 318 }
nexpaq 0:6c56fb4bc5f0 319 if(!found) {
nexpaq 0:6c56fb4bc5f0 320 if(M2MTLVDeserializer::Post == operation) {
nexpaq 0:6c56fb4bc5f0 321 error = M2MTLVDeserializer::NotAllowed;
nexpaq 0:6c56fb4bc5f0 322 } else if(M2MTLVDeserializer::Put == operation) {
nexpaq 0:6c56fb4bc5f0 323 error = M2MTLVDeserializer::NotFound;
nexpaq 0:6c56fb4bc5f0 324 }
nexpaq 0:6c56fb4bc5f0 325 }
nexpaq 0:6c56fb4bc5f0 326 } else {
nexpaq 0:6c56fb4bc5f0 327 delete til;
nexpaq 0:6c56fb4bc5f0 328 error = M2MTLVDeserializer::NotValid;
nexpaq 0:6c56fb4bc5f0 329 return error;
nexpaq 0:6c56fb4bc5f0 330 }
nexpaq 0:6c56fb4bc5f0 331
nexpaq 0:6c56fb4bc5f0 332 offset += til->_length;
nexpaq 0:6c56fb4bc5f0 333
nexpaq 0:6c56fb4bc5f0 334 delete til;
nexpaq 0:6c56fb4bc5f0 335 if(offset < tlv_size) {
nexpaq 0:6c56fb4bc5f0 336 error = deserialize_resource_instances(tlv, tlv_size-offset, offset, resource, operation, update_value);
nexpaq 0:6c56fb4bc5f0 337 }
nexpaq 0:6c56fb4bc5f0 338 return error;
nexpaq 0:6c56fb4bc5f0 339 }
nexpaq 0:6c56fb4bc5f0 340
nexpaq 0:6c56fb4bc5f0 341 bool M2MTLVDeserializer::is_object_instance(uint8_t *tlv, uint32_t offset)
nexpaq 0:6c56fb4bc5f0 342 {
nexpaq 0:6c56fb4bc5f0 343 bool ret = false;
nexpaq 0:6c56fb4bc5f0 344 if (tlv) {
nexpaq 0:6c56fb4bc5f0 345 uint8_t value = tlv[offset];
nexpaq 0:6c56fb4bc5f0 346 ret = (TYPE_OBJECT_INSTANCE == (value & TYPE_RESOURCE));
nexpaq 0:6c56fb4bc5f0 347 }
nexpaq 0:6c56fb4bc5f0 348 return ret;
nexpaq 0:6c56fb4bc5f0 349 }
nexpaq 0:6c56fb4bc5f0 350
nexpaq 0:6c56fb4bc5f0 351 uint16_t M2MTLVDeserializer::instance_id(uint8_t *tlv)
nexpaq 0:6c56fb4bc5f0 352 {
nexpaq 0:6c56fb4bc5f0 353 TypeIdLength *til = TypeIdLength::createTypeIdLength(tlv, 0)->deserialize();
nexpaq 0:6c56fb4bc5f0 354 uint16_t id = til->_id;
nexpaq 0:6c56fb4bc5f0 355 delete til;
nexpaq 0:6c56fb4bc5f0 356 return id;
nexpaq 0:6c56fb4bc5f0 357 }
nexpaq 0:6c56fb4bc5f0 358
nexpaq 0:6c56fb4bc5f0 359 bool M2MTLVDeserializer::is_resource(uint8_t *tlv, uint32_t offset)
nexpaq 0:6c56fb4bc5f0 360 {
nexpaq 0:6c56fb4bc5f0 361 bool ret = false;
nexpaq 0:6c56fb4bc5f0 362 if (tlv) {
nexpaq 0:6c56fb4bc5f0 363 ret = (TYPE_RESOURCE == (tlv[offset] & TYPE_RESOURCE));
nexpaq 0:6c56fb4bc5f0 364 }
nexpaq 0:6c56fb4bc5f0 365 return ret;
nexpaq 0:6c56fb4bc5f0 366 }
nexpaq 0:6c56fb4bc5f0 367
nexpaq 0:6c56fb4bc5f0 368 bool M2MTLVDeserializer::is_multiple_resource(uint8_t *tlv, uint32_t offset)
nexpaq 0:6c56fb4bc5f0 369 {
nexpaq 0:6c56fb4bc5f0 370 bool ret = false;
nexpaq 0:6c56fb4bc5f0 371 if (tlv) {
nexpaq 0:6c56fb4bc5f0 372 ret = (TYPE_MULTIPLE_RESOURCE == (tlv[offset] & TYPE_RESOURCE));
nexpaq 0:6c56fb4bc5f0 373 }
nexpaq 0:6c56fb4bc5f0 374 return ret;
nexpaq 0:6c56fb4bc5f0 375 }
nexpaq 0:6c56fb4bc5f0 376
nexpaq 0:6c56fb4bc5f0 377 bool M2MTLVDeserializer::is_resource_instance(uint8_t *tlv, uint32_t offset)
nexpaq 0:6c56fb4bc5f0 378 {
nexpaq 0:6c56fb4bc5f0 379 bool ret = false;
nexpaq 0:6c56fb4bc5f0 380 if (tlv) {
nexpaq 0:6c56fb4bc5f0 381 ret = (TYPE_RESOURCE_INSTANCE == (tlv[offset] & TYPE_RESOURCE));
nexpaq 0:6c56fb4bc5f0 382 }
nexpaq 0:6c56fb4bc5f0 383 return ret;
nexpaq 0:6c56fb4bc5f0 384 }
nexpaq 0:6c56fb4bc5f0 385
nexpaq 0:6c56fb4bc5f0 386 TypeIdLength* TypeIdLength::createTypeIdLength(uint8_t *tlv, uint32_t offset)
nexpaq 0:6c56fb4bc5f0 387 {
nexpaq 0:6c56fb4bc5f0 388 TypeIdLength *til = new TypeIdLength();
nexpaq 0:6c56fb4bc5f0 389 til->_tlv = tlv;
nexpaq 0:6c56fb4bc5f0 390 til->_offset = offset;
nexpaq 0:6c56fb4bc5f0 391 til->_type = tlv[offset] & 0xC0;
nexpaq 0:6c56fb4bc5f0 392 til->_id = 0;
nexpaq 0:6c56fb4bc5f0 393 til->_length = 0;
nexpaq 0:6c56fb4bc5f0 394 return til;
nexpaq 0:6c56fb4bc5f0 395 }
nexpaq 0:6c56fb4bc5f0 396
nexpaq 0:6c56fb4bc5f0 397 TypeIdLength* TypeIdLength::deserialize()
nexpaq 0:6c56fb4bc5f0 398 {
nexpaq 0:6c56fb4bc5f0 399 uint32_t idLength = _tlv[_offset] & ID16;
nexpaq 0:6c56fb4bc5f0 400 uint32_t lengthType = _tlv[_offset] & LENGTH24;
nexpaq 0:6c56fb4bc5f0 401 if (0 == lengthType) {
nexpaq 0:6c56fb4bc5f0 402 _length = _tlv[_offset] & 0x07;
nexpaq 0:6c56fb4bc5f0 403 }
nexpaq 0:6c56fb4bc5f0 404 _offset++;
nexpaq 0:6c56fb4bc5f0 405
nexpaq 0:6c56fb4bc5f0 406 deserialiseID(idLength);
nexpaq 0:6c56fb4bc5f0 407 deserialiseLength(lengthType);
nexpaq 0:6c56fb4bc5f0 408
nexpaq 0:6c56fb4bc5f0 409 return this;
nexpaq 0:6c56fb4bc5f0 410 }
nexpaq 0:6c56fb4bc5f0 411
nexpaq 0:6c56fb4bc5f0 412 void TypeIdLength::deserialiseID(uint32_t idLength)
nexpaq 0:6c56fb4bc5f0 413 {
nexpaq 0:6c56fb4bc5f0 414 _id = _tlv[_offset++] & 0xFF;
nexpaq 0:6c56fb4bc5f0 415 if (ID16 == idLength) {
nexpaq 0:6c56fb4bc5f0 416 _id = (_id << 8) + (_tlv[_offset++] & 0xFF);
nexpaq 0:6c56fb4bc5f0 417 }
nexpaq 0:6c56fb4bc5f0 418 }
nexpaq 0:6c56fb4bc5f0 419
nexpaq 0:6c56fb4bc5f0 420 void TypeIdLength::deserialiseLength(uint32_t lengthType)
nexpaq 0:6c56fb4bc5f0 421 {
nexpaq 0:6c56fb4bc5f0 422 if (lengthType > 0) {
nexpaq 0:6c56fb4bc5f0 423 _length = _tlv[_offset++] & 0xFF;
nexpaq 0:6c56fb4bc5f0 424 }
nexpaq 0:6c56fb4bc5f0 425 if (lengthType > LENGTH8) {
nexpaq 0:6c56fb4bc5f0 426 _length = (_length << 8) + (_tlv[_offset++] & 0xFF);
nexpaq 0:6c56fb4bc5f0 427 }
nexpaq 0:6c56fb4bc5f0 428 if (lengthType > LENGTH16) {
nexpaq 0:6c56fb4bc5f0 429 _length = (_length << 8) + (_tlv[_offset++] & 0xFF);
nexpaq 0:6c56fb4bc5f0 430 }
nexpaq 0:6c56fb4bc5f0 431 }