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