Committer:
leothedragon
Date:
Sun Apr 18 15:20:23 2021 +0000
Revision:
0:25fa8795676b
DS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:25fa8795676b 1 /*
leothedragon 0:25fa8795676b 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:25fa8795676b 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:25fa8795676b 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:25fa8795676b 5 * not use this file except in compliance with the License.
leothedragon 0:25fa8795676b 6 * You may obtain a copy of the License at
leothedragon 0:25fa8795676b 7 *
leothedragon 0:25fa8795676b 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:25fa8795676b 9 *
leothedragon 0:25fa8795676b 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:25fa8795676b 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:25fa8795676b 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:25fa8795676b 13 * See the License for the specific language governing permissions and
leothedragon 0:25fa8795676b 14 * limitations under the License.
leothedragon 0:25fa8795676b 15 */
leothedragon 0:25fa8795676b 16 #include "mbed-client/m2mvector.h"
leothedragon 0:25fa8795676b 17 #include "mbed-client/m2mobject.h"
leothedragon 0:25fa8795676b 18 #include "mbed-client/m2mobjectinstance.h"
leothedragon 0:25fa8795676b 19 #include "mbed-client/m2mresource.h"
leothedragon 0:25fa8795676b 20
leothedragon 0:25fa8795676b 21 /**
leothedragon 0:25fa8795676b 22 * @brief M2MTLVDeserializer
leothedragon 0:25fa8795676b 23 * TLV Deserialiser get the object instances and resources as binary data and
leothedragon 0:25fa8795676b 24 * builds the <code>lwm2m</code> representation from it. See OMA-LWM2M
leothedragon 0:25fa8795676b 25 * specification, chapter 6.1 for the resource model and chapter 6.3.3 for
leothedragon 0:25fa8795676b 26 * the OMA-TLV specification.
leothedragon 0:25fa8795676b 27 */
leothedragon 0:25fa8795676b 28 class M2MTLVDeserializer {
leothedragon 0:25fa8795676b 29
leothedragon 0:25fa8795676b 30 public :
leothedragon 0:25fa8795676b 31
leothedragon 0:25fa8795676b 32 typedef enum {
leothedragon 0:25fa8795676b 33 None,
leothedragon 0:25fa8795676b 34 NotFound,
leothedragon 0:25fa8795676b 35 NotAllowed,
leothedragon 0:25fa8795676b 36 NotValid,
leothedragon 0:25fa8795676b 37 OutOfMemory
leothedragon 0:25fa8795676b 38 } Error;
leothedragon 0:25fa8795676b 39
leothedragon 0:25fa8795676b 40 typedef enum {
leothedragon 0:25fa8795676b 41 Put,
leothedragon 0:25fa8795676b 42 Post
leothedragon 0:25fa8795676b 43 } Operation;
leothedragon 0:25fa8795676b 44
leothedragon 0:25fa8795676b 45
leothedragon 0:25fa8795676b 46 /**
leothedragon 0:25fa8795676b 47 * This method checks whether the given binary encodes an object instance
leothedragon 0:25fa8795676b 48 * or something else. It returns <code>true</code> if bits 7-6 of the first
leothedragon 0:25fa8795676b 49 * byte is "00".
leothedragon 0:25fa8795676b 50 * @param tlv Binary to be checked as LWM2M object instance
leothedragon 0:25fa8795676b 51 * @return <code>true</code> or <code>false</code>.
leothedragon 0:25fa8795676b 52 */
leothedragon 0:25fa8795676b 53 static bool is_object_instance(const uint8_t *tlv);
leothedragon 0:25fa8795676b 54
leothedragon 0:25fa8795676b 55 /**
leothedragon 0:25fa8795676b 56 * This method checks whether the given binary encodes a resource or
leothedragon 0:25fa8795676b 57 * something else. It returns <code>true</code> if bits 7-6 of the first
leothedragon 0:25fa8795676b 58 * byte is "11".
leothedragon 0:25fa8795676b 59 * @param tlv Binary to be checked as LWM2M resource.
leothedragon 0:25fa8795676b 60 * @return <code>true</code> or <code>false</code>.
leothedragon 0:25fa8795676b 61 */
leothedragon 0:25fa8795676b 62 static bool is_resource(const uint8_t *tlv);
leothedragon 0:25fa8795676b 63
leothedragon 0:25fa8795676b 64 /**
leothedragon 0:25fa8795676b 65 * This method checks whether the given binary encodes a multiple resource
leothedragon 0:25fa8795676b 66 * or something else. It returns <code>true</code> if bits 7-6 of the first
leothedragon 0:25fa8795676b 67 * byte is "10".
leothedragon 0:25fa8795676b 68 * @param tlv Binary to be checked as LWM2M multiple resource.
leothedragon 0:25fa8795676b 69 * @return <code>true</code> or <code>false</code>.
leothedragon 0:25fa8795676b 70 */
leothedragon 0:25fa8795676b 71 static bool is_multiple_resource(const uint8_t *tlv);
leothedragon 0:25fa8795676b 72
leothedragon 0:25fa8795676b 73 /**
leothedragon 0:25fa8795676b 74 * This method checks whether the given binary encodes a resource instance
leothedragon 0:25fa8795676b 75 * or something else. It returns <code>true</code> if bits 7-6 of the first
leothedragon 0:25fa8795676b 76 * byte is "01".
leothedragon 0:25fa8795676b 77 * @param tlv Binary to be checked as LWM2M resource instance.
leothedragon 0:25fa8795676b 78 * @return <code>true</code> or <code>false</code>.
leothedragon 0:25fa8795676b 79 */
leothedragon 0:25fa8795676b 80 static bool is_resource_instance(const uint8_t *tlv);
leothedragon 0:25fa8795676b 81
leothedragon 0:25fa8795676b 82 /**
leothedragon 0:25fa8795676b 83 * Deserialises the given binary that must encode object instances. Binary
leothedragon 0:25fa8795676b 84 * array can be checked before invoking this method with
leothedragon 0:25fa8795676b 85 */
leothedragon 0:25fa8795676b 86 static M2MTLVDeserializer::Error deserialise_object_instances(const uint8_t* tlv,
leothedragon 0:25fa8795676b 87 uint32_t tlv_size,
leothedragon 0:25fa8795676b 88 M2MObject &object,
leothedragon 0:25fa8795676b 89 M2MTLVDeserializer::Operation operation);
leothedragon 0:25fa8795676b 90
leothedragon 0:25fa8795676b 91 /**
leothedragon 0:25fa8795676b 92 * Deserialises the given binary that must encode resources. Binary array
leothedragon 0:25fa8795676b 93 * can be checked before invoking this method.
leothedragon 0:25fa8795676b 94 */
leothedragon 0:25fa8795676b 95 static M2MTLVDeserializer::Error deserialize_resources(const uint8_t *tlv,
leothedragon 0:25fa8795676b 96 uint32_t tlv_size,
leothedragon 0:25fa8795676b 97 M2MObjectInstance &object_instance,
leothedragon 0:25fa8795676b 98 M2MTLVDeserializer::Operation operation);
leothedragon 0:25fa8795676b 99
leothedragon 0:25fa8795676b 100 /**
leothedragon 0:25fa8795676b 101 * Deserialises the given binary that must encode resource instances. Binary array
leothedragon 0:25fa8795676b 102 * can be checked before invoking this method.
leothedragon 0:25fa8795676b 103 */
leothedragon 0:25fa8795676b 104 static M2MTLVDeserializer::Error deserialize_resource_instances(const uint8_t *tlv,
leothedragon 0:25fa8795676b 105 uint32_t tlv_size,
leothedragon 0:25fa8795676b 106 M2MResource &resource,
leothedragon 0:25fa8795676b 107 M2MTLVDeserializer::Operation operation);
leothedragon 0:25fa8795676b 108 /**
leothedragon 0:25fa8795676b 109 * This method return object instance id or resource id.
leothedragon 0:25fa8795676b 110 * @param tlv Binary to be checked
leothedragon 0:25fa8795676b 111 * @return Object instance id or resource id.
leothedragon 0:25fa8795676b 112 */
leothedragon 0:25fa8795676b 113 static uint16_t instance_id(const uint8_t *tlv);
leothedragon 0:25fa8795676b 114
leothedragon 0:25fa8795676b 115 private:
leothedragon 0:25fa8795676b 116
leothedragon 0:25fa8795676b 117 static M2MTLVDeserializer::Error deserialize_object_instances(const uint8_t *tlv,
leothedragon 0:25fa8795676b 118 uint32_t tlv_size,
leothedragon 0:25fa8795676b 119 uint32_t offset,
leothedragon 0:25fa8795676b 120 M2MObject &object,
leothedragon 0:25fa8795676b 121 M2MTLVDeserializer::Operation operation,
leothedragon 0:25fa8795676b 122 bool update_value);
leothedragon 0:25fa8795676b 123
leothedragon 0:25fa8795676b 124 static M2MTLVDeserializer::Error deserialize_resources(const uint8_t *tlv,
leothedragon 0:25fa8795676b 125 uint32_t tlv_size,
leothedragon 0:25fa8795676b 126 uint32_t offset,
leothedragon 0:25fa8795676b 127 M2MObjectInstance &object_instance,
leothedragon 0:25fa8795676b 128 M2MTLVDeserializer::Operation operation,
leothedragon 0:25fa8795676b 129 bool update_value);
leothedragon 0:25fa8795676b 130
leothedragon 0:25fa8795676b 131 static M2MTLVDeserializer::Error deserialize_resource_instances(const uint8_t *tlv,
leothedragon 0:25fa8795676b 132 uint32_t tlv_size,
leothedragon 0:25fa8795676b 133 uint32_t offset,
leothedragon 0:25fa8795676b 134 M2MResource &resource,
leothedragon 0:25fa8795676b 135 M2MObjectInstance &object_instance,
leothedragon 0:25fa8795676b 136 M2MTLVDeserializer::Operation operation,
leothedragon 0:25fa8795676b 137 bool update_value);
leothedragon 0:25fa8795676b 138
leothedragon 0:25fa8795676b 139 static M2MTLVDeserializer::Error deserialize_resource_instances(const uint8_t *tlv,
leothedragon 0:25fa8795676b 140 uint32_t tlv_size,
leothedragon 0:25fa8795676b 141 uint32_t offset,
leothedragon 0:25fa8795676b 142 M2MResource &resource,
leothedragon 0:25fa8795676b 143 M2MTLVDeserializer::Operation operation,
leothedragon 0:25fa8795676b 144 bool update_value);
leothedragon 0:25fa8795676b 145
leothedragon 0:25fa8795676b 146 static bool is_object_instance(const uint8_t *tlv, uint32_t offset);
leothedragon 0:25fa8795676b 147
leothedragon 0:25fa8795676b 148 static bool is_resource(const uint8_t *tlv, uint32_t offset);
leothedragon 0:25fa8795676b 149
leothedragon 0:25fa8795676b 150 static bool is_multiple_resource(const uint8_t *tlv, uint32_t offset);
leothedragon 0:25fa8795676b 151
leothedragon 0:25fa8795676b 152 static bool is_resource_instance(const uint8_t *tlv, uint32_t offset);
leothedragon 0:25fa8795676b 153
leothedragon 0:25fa8795676b 154 static bool set_resource_instance_value(M2MResourceBase *res, const uint8_t *tlv, const uint32_t size);
leothedragon 0:25fa8795676b 155
leothedragon 0:25fa8795676b 156 static void remove_resources(const uint8_t *tlv,
leothedragon 0:25fa8795676b 157 uint32_t tlv_size,
leothedragon 0:25fa8795676b 158 M2MObjectInstance &object_instance,
leothedragon 0:25fa8795676b 159 uint32_t offset_size);
leothedragon 0:25fa8795676b 160
leothedragon 0:25fa8795676b 161 static void remove_resource_instances(const uint8_t *tlv,
leothedragon 0:25fa8795676b 162 uint32_t tlv_size,
leothedragon 0:25fa8795676b 163 M2MResource &resource,
leothedragon 0:25fa8795676b 164 uint32_t offset_size);
leothedragon 0:25fa8795676b 165 };
leothedragon 0:25fa8795676b 166
leothedragon 0:25fa8795676b 167 class TypeIdLength {
leothedragon 0:25fa8795676b 168
leothedragon 0:25fa8795676b 169 public:
leothedragon 0:25fa8795676b 170 TypeIdLength(const uint8_t *tlv, uint32_t offset);
leothedragon 0:25fa8795676b 171
leothedragon 0:25fa8795676b 172 void deserialize();
leothedragon 0:25fa8795676b 173
leothedragon 0:25fa8795676b 174 void deserialiseID(uint32_t idLength);
leothedragon 0:25fa8795676b 175
leothedragon 0:25fa8795676b 176 void deserialiseLength(uint32_t lengthType);
leothedragon 0:25fa8795676b 177
leothedragon 0:25fa8795676b 178 const uint8_t *_tlv;
leothedragon 0:25fa8795676b 179 uint32_t _offset;
leothedragon 0:25fa8795676b 180 uint32_t _type;
leothedragon 0:25fa8795676b 181 uint16_t _id;
leothedragon 0:25fa8795676b 182 uint32_t _length;
leothedragon 0:25fa8795676b 183
leothedragon 0:25fa8795676b 184 friend class Test_M2MTLVDeserializer;
leothedragon 0:25fa8795676b 185 };