This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

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