leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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