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