Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include <stdio.h>
nexpaq 0:6c56fb4bc5f0 17 #include "include/m2mtlvserializer.h"
nexpaq 0:6c56fb4bc5f0 18 #include "include/nsdllinker.h"
nexpaq 0:6c56fb4bc5f0 19 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 20
nexpaq 0:6c56fb4bc5f0 21 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 22
nexpaq 0:6c56fb4bc5f0 23 M2MTLVSerializer::M2MTLVSerializer()
nexpaq 0:6c56fb4bc5f0 24 {
nexpaq 0:6c56fb4bc5f0 25 }
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27 M2MTLVSerializer::~M2MTLVSerializer()
nexpaq 0:6c56fb4bc5f0 28 {
nexpaq 0:6c56fb4bc5f0 29 }
nexpaq 0:6c56fb4bc5f0 30
nexpaq 0:6c56fb4bc5f0 31 uint8_t* M2MTLVSerializer::serialize(M2MObjectInstanceList object_instance_list, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 32 {
nexpaq 0:6c56fb4bc5f0 33 return serialize_object_instances(object_instance_list, size);
nexpaq 0:6c56fb4bc5f0 34 }
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 uint8_t* M2MTLVSerializer::serialize(M2MResourceList resource_list, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 37 {
nexpaq 0:6c56fb4bc5f0 38 bool valid = true;
nexpaq 0:6c56fb4bc5f0 39 return serialize_resources(resource_list, size,valid);
nexpaq 0:6c56fb4bc5f0 40 }
nexpaq 0:6c56fb4bc5f0 41
nexpaq 0:6c56fb4bc5f0 42 uint8_t* M2MTLVSerializer::serialize(M2MResource *resource, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 43 {
nexpaq 0:6c56fb4bc5f0 44 uint8_t* data = NULL;
nexpaq 0:6c56fb4bc5f0 45 serialize(resource, data, size);
nexpaq 0:6c56fb4bc5f0 46 return data;
nexpaq 0:6c56fb4bc5f0 47 }
nexpaq 0:6c56fb4bc5f0 48
nexpaq 0:6c56fb4bc5f0 49 uint8_t* M2MTLVSerializer::serialize_object_instances(M2MObjectInstanceList object_instance_list, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 50 {
nexpaq 0:6c56fb4bc5f0 51 uint8_t *data = NULL;
nexpaq 0:6c56fb4bc5f0 52
nexpaq 0:6c56fb4bc5f0 53 if(!object_instance_list.empty()) {
nexpaq 0:6c56fb4bc5f0 54 M2MObjectInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 55 it = object_instance_list.begin();
nexpaq 0:6c56fb4bc5f0 56 for (; it!=object_instance_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 57 uint16_t id = (*it)->instance_id();
nexpaq 0:6c56fb4bc5f0 58 serialize(id, *it, data, size);
nexpaq 0:6c56fb4bc5f0 59 }
nexpaq 0:6c56fb4bc5f0 60 }
nexpaq 0:6c56fb4bc5f0 61 return data;
nexpaq 0:6c56fb4bc5f0 62 }
nexpaq 0:6c56fb4bc5f0 63
nexpaq 0:6c56fb4bc5f0 64 uint8_t* M2MTLVSerializer::serialize_resources(M2MResourceList resource_list, uint32_t &size, bool &valid)
nexpaq 0:6c56fb4bc5f0 65 {
nexpaq 0:6c56fb4bc5f0 66 uint8_t *data = NULL;
nexpaq 0:6c56fb4bc5f0 67
nexpaq 0:6c56fb4bc5f0 68 if(!resource_list.empty()) {
nexpaq 0:6c56fb4bc5f0 69 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 70 it = resource_list.begin();
nexpaq 0:6c56fb4bc5f0 71 for (; it!=resource_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 72 if((*it)->name_id() == -1) {
nexpaq 0:6c56fb4bc5f0 73 valid = false;
nexpaq 0:6c56fb4bc5f0 74 break;
nexpaq 0:6c56fb4bc5f0 75 }
nexpaq 0:6c56fb4bc5f0 76 }
nexpaq 0:6c56fb4bc5f0 77 if(valid) {
nexpaq 0:6c56fb4bc5f0 78 it = resource_list.begin();
nexpaq 0:6c56fb4bc5f0 79 for (; it!=resource_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 80 serialize(*it, data, size);
nexpaq 0:6c56fb4bc5f0 81 }
nexpaq 0:6c56fb4bc5f0 82 }
nexpaq 0:6c56fb4bc5f0 83 }
nexpaq 0:6c56fb4bc5f0 84 return data;
nexpaq 0:6c56fb4bc5f0 85 }
nexpaq 0:6c56fb4bc5f0 86
nexpaq 0:6c56fb4bc5f0 87 void M2MTLVSerializer::serialize(uint16_t id, M2MObjectInstance *object_instance, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 88 {
nexpaq 0:6c56fb4bc5f0 89 uint8_t *resource_data = NULL;
nexpaq 0:6c56fb4bc5f0 90 uint32_t resource_size = 0;
nexpaq 0:6c56fb4bc5f0 91
nexpaq 0:6c56fb4bc5f0 92 bool valid = true;
nexpaq 0:6c56fb4bc5f0 93 resource_data = serialize_resources(object_instance->resources(),resource_size,valid);
nexpaq 0:6c56fb4bc5f0 94 if(valid) {
nexpaq 0:6c56fb4bc5f0 95 serialize_TILV(TYPE_OBJECT_INSTANCE, id, resource_data, resource_size, data, size);
nexpaq 0:6c56fb4bc5f0 96 }
nexpaq 0:6c56fb4bc5f0 97 free(resource_data);
nexpaq 0:6c56fb4bc5f0 98 }
nexpaq 0:6c56fb4bc5f0 99
nexpaq 0:6c56fb4bc5f0 100 bool M2MTLVSerializer::serialize(M2MResource *resource, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 101 {
nexpaq 0:6c56fb4bc5f0 102 bool success = false;
nexpaq 0:6c56fb4bc5f0 103 if(resource->name_id() != -1) {
nexpaq 0:6c56fb4bc5f0 104 success = resource->supports_multiple_instances() ?
nexpaq 0:6c56fb4bc5f0 105 serialize_multiple_resource(resource, data, size) :
nexpaq 0:6c56fb4bc5f0 106 serialize_resource(resource, data, size);
nexpaq 0:6c56fb4bc5f0 107 }
nexpaq 0:6c56fb4bc5f0 108 return success;
nexpaq 0:6c56fb4bc5f0 109 }
nexpaq 0:6c56fb4bc5f0 110
nexpaq 0:6c56fb4bc5f0 111 bool M2MTLVSerializer::serialize_resource(M2MResource *resource, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 112 {
nexpaq 0:6c56fb4bc5f0 113 bool success = false;
nexpaq 0:6c56fb4bc5f0 114 if(resource->name_id() != -1) {
nexpaq 0:6c56fb4bc5f0 115 success = true;
nexpaq 0:6c56fb4bc5f0 116 serialize_TILV(TYPE_RESOURCE, resource->name_id(), resource->value(), resource->value_length(), data, size);
nexpaq 0:6c56fb4bc5f0 117 }
nexpaq 0:6c56fb4bc5f0 118 return success;
nexpaq 0:6c56fb4bc5f0 119 }
nexpaq 0:6c56fb4bc5f0 120
nexpaq 0:6c56fb4bc5f0 121 bool M2MTLVSerializer::serialize_multiple_resource(M2MResource *resource, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 122 {
nexpaq 0:6c56fb4bc5f0 123 bool success = false;
nexpaq 0:6c56fb4bc5f0 124 uint8_t *nested_data = NULL;
nexpaq 0:6c56fb4bc5f0 125 uint32_t nested_data_size = 0;
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 M2MResourceInstanceList instance_list = resource->resource_instances();
nexpaq 0:6c56fb4bc5f0 128 if(!instance_list.empty()) {
nexpaq 0:6c56fb4bc5f0 129 M2MResourceInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 130 it = instance_list.begin();
nexpaq 0:6c56fb4bc5f0 131 for (; it!=instance_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 132 uint16_t id = (*it)->instance_id();
nexpaq 0:6c56fb4bc5f0 133 serialize_resource_instance(id, (*it), nested_data, nested_data_size);
nexpaq 0:6c56fb4bc5f0 134 }
nexpaq 0:6c56fb4bc5f0 135 }
nexpaq 0:6c56fb4bc5f0 136 if(resource->name_id() != -1) {
nexpaq 0:6c56fb4bc5f0 137 success = true;
nexpaq 0:6c56fb4bc5f0 138 serialize_TILV(TYPE_MULTIPLE_RESOURCE, resource->name_id(), nested_data, nested_data_size, data, size);
nexpaq 0:6c56fb4bc5f0 139 }
nexpaq 0:6c56fb4bc5f0 140
nexpaq 0:6c56fb4bc5f0 141 free(nested_data);
nexpaq 0:6c56fb4bc5f0 142 nested_data = NULL;
nexpaq 0:6c56fb4bc5f0 143 return success;
nexpaq 0:6c56fb4bc5f0 144 }
nexpaq 0:6c56fb4bc5f0 145
nexpaq 0:6c56fb4bc5f0 146 void M2MTLVSerializer::serialize_resource_instance(uint16_t id, M2MResourceInstance *resource, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 147 {
nexpaq 0:6c56fb4bc5f0 148 serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size);
nexpaq 0:6c56fb4bc5f0 149 }
nexpaq 0:6c56fb4bc5f0 150
nexpaq 0:6c56fb4bc5f0 151 void M2MTLVSerializer::serialize_TILV(uint8_t type, uint16_t id, uint8_t *value, uint32_t value_length, uint8_t *&data, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 152 {
nexpaq 0:6c56fb4bc5f0 153 uint8_t *tlv = 0;
nexpaq 0:6c56fb4bc5f0 154 uint32_t type_length = 1;
nexpaq 0:6c56fb4bc5f0 155 type += id < 256 ? 0 : ID16;
nexpaq 0:6c56fb4bc5f0 156 type += value_length < 8 ? value_length :
nexpaq 0:6c56fb4bc5f0 157 value_length < 256 ? LENGTH8 :
nexpaq 0:6c56fb4bc5f0 158 value_length < 65536 ? LENGTH16 : LENGTH24;
nexpaq 0:6c56fb4bc5f0 159 uint8_t *tlv_type = (uint8_t*)malloc(type_length+1);
nexpaq 0:6c56fb4bc5f0 160 memset(tlv_type,0,type_length+1);
nexpaq 0:6c56fb4bc5f0 161 *tlv_type = type & 0xFF;
nexpaq 0:6c56fb4bc5f0 162
nexpaq 0:6c56fb4bc5f0 163 uint32_t id_size = 0;
nexpaq 0:6c56fb4bc5f0 164 uint8_t* id_ptr = serialize_id(id, id_size);
nexpaq 0:6c56fb4bc5f0 165
nexpaq 0:6c56fb4bc5f0 166 uint32_t length_size = 0;
nexpaq 0:6c56fb4bc5f0 167 uint8_t* length_ptr = serialize_length(value_length, length_size);
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 tlv = (uint8_t*)malloc(size + type_length + id_size + length_size + value_length+1);
nexpaq 0:6c56fb4bc5f0 170 memset(tlv,0,size + type_length + id_size + length_size + value_length+1);
nexpaq 0:6c56fb4bc5f0 171 if(data) {
nexpaq 0:6c56fb4bc5f0 172 memcpy(tlv, data, size);
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174 memcpy(tlv+size, tlv_type, type_length);
nexpaq 0:6c56fb4bc5f0 175 memcpy(tlv+size+type_length, id_ptr, id_size);
nexpaq 0:6c56fb4bc5f0 176 memcpy(tlv+size+type_length+id_size, length_ptr, length_size);
nexpaq 0:6c56fb4bc5f0 177 memcpy(tlv+size+type_length+id_size+length_size, value, value_length);
nexpaq 0:6c56fb4bc5f0 178
nexpaq 0:6c56fb4bc5f0 179 free(tlv_type) ;
nexpaq 0:6c56fb4bc5f0 180 free(length_ptr);
nexpaq 0:6c56fb4bc5f0 181 free(id_ptr);
nexpaq 0:6c56fb4bc5f0 182 free(data);
nexpaq 0:6c56fb4bc5f0 183
nexpaq 0:6c56fb4bc5f0 184 data = tlv;
nexpaq 0:6c56fb4bc5f0 185 size += type_length + id_size + length_size + value_length;
nexpaq 0:6c56fb4bc5f0 186 }
nexpaq 0:6c56fb4bc5f0 187
nexpaq 0:6c56fb4bc5f0 188 uint8_t* M2MTLVSerializer::serialize_id(uint16_t id, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 189 {
nexpaq 0:6c56fb4bc5f0 190 uint32_t id_size = id > 255 ? 2 : 1;
nexpaq 0:6c56fb4bc5f0 191 uint8_t *id_ptr = (uint8_t*)malloc(id_size);
nexpaq 0:6c56fb4bc5f0 192 memset(id_ptr, 0 , id_size);
nexpaq 0:6c56fb4bc5f0 193 size += id_size;
nexpaq 0:6c56fb4bc5f0 194 if(id > 255) {
nexpaq 0:6c56fb4bc5f0 195 *id_ptr = (id & 0xFF00) >> 8;
nexpaq 0:6c56fb4bc5f0 196 id_ptr++;
nexpaq 0:6c56fb4bc5f0 197 *id_ptr = id & 0xFF;
nexpaq 0:6c56fb4bc5f0 198 id_ptr--;
nexpaq 0:6c56fb4bc5f0 199 } else {
nexpaq 0:6c56fb4bc5f0 200 *id_ptr = id & 0xFF;
nexpaq 0:6c56fb4bc5f0 201 }
nexpaq 0:6c56fb4bc5f0 202 return id_ptr;
nexpaq 0:6c56fb4bc5f0 203 }
nexpaq 0:6c56fb4bc5f0 204
nexpaq 0:6c56fb4bc5f0 205 uint8_t* M2MTLVSerializer::serialize_length(uint32_t length, uint32_t &size)
nexpaq 0:6c56fb4bc5f0 206 {
nexpaq 0:6c56fb4bc5f0 207 uint8_t *length_ptr = 0;
nexpaq 0:6c56fb4bc5f0 208 uint32_t length_size = 0;
nexpaq 0:6c56fb4bc5f0 209 if (length > 65535) {
nexpaq 0:6c56fb4bc5f0 210 length_size = 3;
nexpaq 0:6c56fb4bc5f0 211 length_ptr = (uint8_t*)malloc(length_size+1);
nexpaq 0:6c56fb4bc5f0 212 memset(length_ptr,0,length_size+1);
nexpaq 0:6c56fb4bc5f0 213 *length_ptr = (length & 0xFF0000) >> 16;
nexpaq 0:6c56fb4bc5f0 214 length_ptr++;
nexpaq 0:6c56fb4bc5f0 215 *length_ptr = (length & 0xFF00) >> 8;
nexpaq 0:6c56fb4bc5f0 216 length_ptr++;
nexpaq 0:6c56fb4bc5f0 217 *length_ptr = length & 0xFF;
nexpaq 0:6c56fb4bc5f0 218 length_ptr--;
nexpaq 0:6c56fb4bc5f0 219 length_ptr--;
nexpaq 0:6c56fb4bc5f0 220 } else if (length > 255) {
nexpaq 0:6c56fb4bc5f0 221 length_size = 2;
nexpaq 0:6c56fb4bc5f0 222 length_ptr = (uint8_t*)malloc(length_size+1);
nexpaq 0:6c56fb4bc5f0 223 memset(length_ptr,0,length_size+1);
nexpaq 0:6c56fb4bc5f0 224 *length_ptr = (length & 0xFF00) >> 8;
nexpaq 0:6c56fb4bc5f0 225 length_ptr++;
nexpaq 0:6c56fb4bc5f0 226 *length_ptr = length & 0xFF;
nexpaq 0:6c56fb4bc5f0 227 length_ptr--;
nexpaq 0:6c56fb4bc5f0 228 } else if (length > 7) {
nexpaq 0:6c56fb4bc5f0 229 length_size = 1;
nexpaq 0:6c56fb4bc5f0 230 length_ptr = (uint8_t*)malloc(length_size+1);
nexpaq 0:6c56fb4bc5f0 231 memset(length_ptr,0,length_size+1);
nexpaq 0:6c56fb4bc5f0 232 *length_ptr = length & 0xFF;
nexpaq 0:6c56fb4bc5f0 233 }
nexpaq 0:6c56fb4bc5f0 234 size += length_size;
nexpaq 0:6c56fb4bc5f0 235 return length_ptr;
nexpaq 0:6c56fb4bc5f0 236 }
nexpaq 0:6c56fb4bc5f0 237