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