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