A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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