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 <unistd.h>
edamame22 0:29983394c6b6 17 #include "mbed-client/m2minterfacefactory.h"
edamame22 0:29983394c6b6 18 #include "mbed-client/m2minterface.h"
edamame22 0:29983394c6b6 19 #include "mbed-client/m2mdevice.h"
edamame22 0:29983394c6b6 20 #include "mbed-client/m2minterfaceobserver.h"
edamame22 0:29983394c6b6 21 #include "mbed-client/m2mobjectinstance.h"
edamame22 0:29983394c6b6 22 #include "mbed-client/m2mresource.h"
edamame22 0:29983394c6b6 23 #include "mbed-client/m2mfirmware.h"
edamame22 0:29983394c6b6 24
edamame22 0:29983394c6b6 25 class M2MLWClient: public M2MInterfaceObserver {
edamame22 0:29983394c6b6 26 public:
edamame22 0:29983394c6b6 27 M2MLWClient();
edamame22 0:29983394c6b6 28
edamame22 0:29983394c6b6 29 ~M2MLWClient();
edamame22 0:29983394c6b6 30
edamame22 0:29983394c6b6 31 bool create_interface(int32_t param_count,
edamame22 0:29983394c6b6 32 const char *endpoint,
edamame22 0:29983394c6b6 33 const char *resource_type,
edamame22 0:29983394c6b6 34 const int32_t lifetime,
edamame22 0:29983394c6b6 35 const uint16_t listen_port,
edamame22 0:29983394c6b6 36 const char *domain,
edamame22 0:29983394c6b6 37 const uint8_t binding_mode,
edamame22 0:29983394c6b6 38 const uint8_t network_interface);
edamame22 0:29983394c6b6 39
edamame22 0:29983394c6b6 40 bool create_bootstrap_object(const char *coap_bootstrap_address);
edamame22 0:29983394c6b6 41
edamame22 0:29983394c6b6 42 bool create_register_object(const char *coap_register_address, bool useSecureConn);
edamame22 0:29983394c6b6 43
edamame22 0:29983394c6b6 44 bool test_bootstrap();
edamame22 0:29983394c6b6 45
edamame22 0:29983394c6b6 46 bool create_device_object();
edamame22 0:29983394c6b6 47
edamame22 0:29983394c6b6 48 bool create_device_object(M2MDevice::DeviceResource resource,
edamame22 0:29983394c6b6 49 const char *value);
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 bool create_device_object(M2MDevice::DeviceResource resource,
edamame22 0:29983394c6b6 52 int64_t value,
edamame22 0:29983394c6b6 53 uint16_t instance_id);
edamame22 0:29983394c6b6 54
edamame22 0:29983394c6b6 55 bool create_device_object(M2MDevice::DeviceResource resource,
edamame22 0:29983394c6b6 56 int64_t value);
edamame22 0:29983394c6b6 57
edamame22 0:29983394c6b6 58 bool create_firmware_object();
edamame22 0:29983394c6b6 59
edamame22 0:29983394c6b6 60 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
edamame22 0:29983394c6b6 61 const char *value);
edamame22 0:29983394c6b6 62
edamame22 0:29983394c6b6 63 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
edamame22 0:29983394c6b6 64 int64_t value);
edamame22 0:29983394c6b6 65
edamame22 0:29983394c6b6 66 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
edamame22 0:29983394c6b6 67 const uint8_t *value,
edamame22 0:29983394c6b6 68 const uint32_t length);
edamame22 0:29983394c6b6 69
edamame22 0:29983394c6b6 70 void firmware_resource_int(int resource);
edamame22 0:29983394c6b6 71 void firmware_resource_string(int resource);
edamame22 0:29983394c6b6 72
edamame22 0:29983394c6b6 73 void firmware_resource_buffer();
edamame22 0:29983394c6b6 74
edamame22 0:29983394c6b6 75 bool create_object(const char *name,
edamame22 0:29983394c6b6 76 bool new_instance,
edamame22 0:29983394c6b6 77 uint8_t object_operation,
edamame22 0:29983394c6b6 78 uint8_t object_instance_operation,
edamame22 0:29983394c6b6 79 uint16_t object_instance_id,
edamame22 0:29983394c6b6 80 bool object_observable,
edamame22 0:29983394c6b6 81 bool object_instance_observable);
edamame22 0:29983394c6b6 82
edamame22 0:29983394c6b6 83 bool create_static_resource_string(const char *name,
edamame22 0:29983394c6b6 84 const char *value,
edamame22 0:29983394c6b6 85 bool multiple_instance,
edamame22 0:29983394c6b6 86 uint16_t object_instance = 0);
edamame22 0:29983394c6b6 87
edamame22 0:29983394c6b6 88 bool create_static_resource_int(const char *name,
edamame22 0:29983394c6b6 89 int64_t value,
edamame22 0:29983394c6b6 90 bool multiple_instance,
edamame22 0:29983394c6b6 91 uint16_t object_instance = 0);
edamame22 0:29983394c6b6 92
edamame22 0:29983394c6b6 93 bool create_dynamic_resource_int(const char *name,
edamame22 0:29983394c6b6 94 bool observable,
edamame22 0:29983394c6b6 95 bool multiple_instance,
edamame22 0:29983394c6b6 96 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 97 uint8_t resource_operation = 1);
edamame22 0:29983394c6b6 98 bool create_dynamic_resource_string(const char *name,
edamame22 0:29983394c6b6 99 bool observable,
edamame22 0:29983394c6b6 100 bool multiple_instance,
edamame22 0:29983394c6b6 101 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 102 uint8_t resource_operation = 1);
edamame22 0:29983394c6b6 103
edamame22 0:29983394c6b6 104 bool set_resource_value(const char *name,
edamame22 0:29983394c6b6 105 int32_t value,
edamame22 0:29983394c6b6 106 uint16_t object_instance = 0);
edamame22 0:29983394c6b6 107
edamame22 0:29983394c6b6 108 bool set_resource_value(const char *name,
edamame22 0:29983394c6b6 109 const char *value,
edamame22 0:29983394c6b6 110 uint16_t object_instance = 0);
edamame22 0:29983394c6b6 111
edamame22 0:29983394c6b6 112 bool create_static_resource_instance_string(const char *name,
edamame22 0:29983394c6b6 113 const char *value,
edamame22 0:29983394c6b6 114 bool multiple_instance,
edamame22 0:29983394c6b6 115 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 116 uint16_t resource_instance = 0);
edamame22 0:29983394c6b6 117
edamame22 0:29983394c6b6 118 bool create_static_resource_instance_int(const char *name,
edamame22 0:29983394c6b6 119 int32_t value,
edamame22 0:29983394c6b6 120 bool multiple_instance,
edamame22 0:29983394c6b6 121 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 122 uint16_t resource_instance = 0);
edamame22 0:29983394c6b6 123
edamame22 0:29983394c6b6 124 bool create_dynamic_resource_instance_int(const char *name,
edamame22 0:29983394c6b6 125 bool observable,
edamame22 0:29983394c6b6 126 bool multiple_instance,
edamame22 0:29983394c6b6 127 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 128 uint16_t resource_instance = 0,
edamame22 0:29983394c6b6 129 uint8_t resource_instance_operation = 1);
edamame22 0:29983394c6b6 130 bool create_dynamic_resource_instance_string(const char *name,
edamame22 0:29983394c6b6 131 bool observable,
edamame22 0:29983394c6b6 132 bool multiple_instance,
edamame22 0:29983394c6b6 133 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 134 uint16_t resource_instance = 0,
edamame22 0:29983394c6b6 135 uint8_t resource_instance_operation = 1);
edamame22 0:29983394c6b6 136
edamame22 0:29983394c6b6 137 bool set_resource_instance_value(const char *name,
edamame22 0:29983394c6b6 138 int32_t value,
edamame22 0:29983394c6b6 139 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 140 uint16_t resource_instance = 0);
edamame22 0:29983394c6b6 141
edamame22 0:29983394c6b6 142 bool set_resource_instance_value(const char *name,
edamame22 0:29983394c6b6 143 const char *value,
edamame22 0:29983394c6b6 144 uint16_t object_instance = 0,
edamame22 0:29983394c6b6 145 uint16_t resource_instance = 0);
edamame22 0:29983394c6b6 146
edamame22 0:29983394c6b6 147 bool create_object_instance();
edamame22 0:29983394c6b6 148
edamame22 0:29983394c6b6 149 bool test_register();
edamame22 0:29983394c6b6 150
edamame22 0:29983394c6b6 151 bool test_update_register(const uint32_t lifetime);
edamame22 0:29983394c6b6 152
edamame22 0:29983394c6b6 153 bool test_unregister();
edamame22 0:29983394c6b6 154
edamame22 0:29983394c6b6 155 void bootstrap_done(M2MSecurity *server_object);
edamame22 0:29983394c6b6 156
edamame22 0:29983394c6b6 157 void object_registered(M2MSecurity *security_object, const M2MServer &server_object);
edamame22 0:29983394c6b6 158
edamame22 0:29983394c6b6 159 void object_unregistered(M2MSecurity *server_object);
edamame22 0:29983394c6b6 160
edamame22 0:29983394c6b6 161 void registration_updated(M2MSecurity *security_object, const M2MServer &server_object);
edamame22 0:29983394c6b6 162
edamame22 0:29983394c6b6 163 void error(M2MInterface::Error error);
edamame22 0:29983394c6b6 164
edamame22 0:29983394c6b6 165 void value_updated(M2MBase *base, M2MBase::BaseType type);
edamame22 0:29983394c6b6 166
edamame22 0:29983394c6b6 167 void set_fw_execute_function();
edamame22 0:29983394c6b6 168
edamame22 0:29983394c6b6 169 private:
edamame22 0:29983394c6b6 170
edamame22 0:29983394c6b6 171 M2MBase::Operation int_to_operation(uint8_t operation);
edamame22 0:29983394c6b6 172 void fw_execute_function(void *argument);
edamame22 0:29983394c6b6 173
edamame22 0:29983394c6b6 174
edamame22 0:29983394c6b6 175 private:
edamame22 0:29983394c6b6 176
edamame22 0:29983394c6b6 177 M2MInterface *_interface;
edamame22 0:29983394c6b6 178 M2MSecurity *_security;
edamame22 0:29983394c6b6 179 M2MSecurity *_register_security;
edamame22 0:29983394c6b6 180 M2MDevice *_device;
edamame22 0:29983394c6b6 181 M2MFirmware *_firmware;
edamame22 0:29983394c6b6 182 M2MObject *_object;
edamame22 0:29983394c6b6 183 bool _bootstrapped;
edamame22 0:29983394c6b6 184 bool _error;
edamame22 0:29983394c6b6 185 bool _registered;
edamame22 0:29983394c6b6 186 bool _unregistered;
edamame22 0:29983394c6b6 187 bool _registration_updated;
edamame22 0:29983394c6b6 188 };
edamame22 0:29983394c6b6 189
edamame22 0:29983394c6b6 190
edamame22 0:29983394c6b6 191 const unsigned char psk[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36};
edamame22 0:29983394c6b6 192 const size_t psk_len = sizeof( psk );
edamame22 0:29983394c6b6 193 const unsigned char psk_identity[] = {0x0F,0x0F};
edamame22 0:29983394c6b6 194 const size_t psk_identity_len = sizeof( psk_identity );
edamame22 0:29983394c6b6 195
edamame22 0:29983394c6b6 196 const uint8_t server_cert[] = "-----BEGIN CERTIFICATE-----\r\n"
edamame22 0:29983394c6b6 197 "MIIBmDCCAT6gAwIBAgIEVUCA0jAKBggqhkjOPQQDAjBLMQswCQYDVQQGEwJGSTEN\r\n"
edamame22 0:29983394c6b6 198 "MAsGA1UEBwwET3VsdTEMMAoGA1UECgwDQVJNMQwwCgYDVQQLDANJb1QxETAPBgNV\r\n"
edamame22 0:29983394c6b6 199 "BAMMCEFSTSBtYmVkMB4XDTE1MDQyOTA2NTc0OFoXDTE4MDQyOTA2NTc0OFowSzEL\r\n"
edamame22 0:29983394c6b6 200 "MAkGA1UEBhMCRkkxDTALBgNVBAcMBE91bHUxDDAKBgNVBAoMA0FSTTEMMAoGA1UE\r\n"
edamame22 0:29983394c6b6 201 "CwwDSW9UMREwDwYDVQQDDAhBUk0gbWJlZDBZMBMGByqGSM49AgEGCCqGSM49AwEH\r\n"
edamame22 0:29983394c6b6 202 "A0IABLuAyLSk0mA3awgFR5mw2RHth47tRUO44q/RdzFZnLsAsd18Esxd5LCpcT9w\r\n"
edamame22 0:29983394c6b6 203 "0tvNfBv4xJxGw0wcYrPDDb8/rjujEDAOMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0E\r\n"
edamame22 0:29983394c6b6 204 "AwIDSAAwRQIhAPAonEAkwixlJiyYRQQWpXtkMZax+VlEiS201BG0PpAzAiBh2RsD\r\n"
edamame22 0:29983394c6b6 205 "NxLKWwf4O7D6JasGBYf9+ZLwl0iaRjTjytO+Kw==\r\n"
edamame22 0:29983394c6b6 206 "-----END CERTIFICATE-----\r\n";
edamame22 0:29983394c6b6 207
edamame22 0:29983394c6b6 208 const uint8_t cert[] = "-----BEGIN CERTIFICATE-----\r\n"
edamame22 0:29983394c6b6 209 "MIICTDCCAbWgAwIBAgIJAKI3S+LGklSGMA0GCSqGSIb3DQEBCwUAMD8xCzAJBgNV\r\n"
edamame22 0:29983394c6b6 210 "BAYTAkZJMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARPdWx1MQwwCgYD\r\n"
edamame22 0:29983394c6b6 211 "VQQKDANBUk0wHhcNMTUwNjExMTI0NTU1WhcNMTYwNjEwMTI0NTU1WjA/MQswCQYD\r\n"
edamame22 0:29983394c6b6 212 "VQQGEwJGSTETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwET3VsdTEMMAoG\r\n"
edamame22 0:29983394c6b6 213 "A1UECgwDQVJNMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBt8ag12ILm2pn\r\n"
edamame22 0:29983394c6b6 214 "RHUc2yMBXpdzspDwVV3VDaNTRXypVUOq/nxJc6zr8yG/Pvga2/XVijWQDLABHVhV\r\n"
edamame22 0:29983394c6b6 215 "PoeulzXVSEHRR2bR2lhqulLzdUzPYJ+yJd4+082akoxdzoom8ms2LFlgiXO7lyBg\r\n"
edamame22 0:29983394c6b6 216 "1t74xjEQapCkr7Tdl2pD9OTUrcfoEwIDAQABo1AwTjAdBgNVHQ4EFgQUHK0O4iK7\r\n"
edamame22 0:29983394c6b6 217 "Rv8AsxAqRDlY3TSHWtUwHwYDVR0jBBgwFoAUHK0O4iK7Rv8AsxAqRDlY3TSHWtUw\r\n"
edamame22 0:29983394c6b6 218 "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAPVLihYN65DEeYdf7gDAdP\r\n"
edamame22 0:29983394c6b6 219 "MNH5RxsQlHdktQkYWcUewe3hvOf8yZUBbmO5OeEQmy5Ca2H1QhUuw+kWHo1l/K9g\r\n"
edamame22 0:29983394c6b6 220 "DpR1xoDACeycBd/6tAHfaFzTEEDC9Ug839EAHDrbI4ihrPEbP6hyRIaUIM4IojXf\r\n"
edamame22 0:29983394c6b6 221 "zuqwOcobS3idv8a9Npuohw==\r\n"
edamame22 0:29983394c6b6 222 "-----END CERTIFICATE-----\r\n";
edamame22 0:29983394c6b6 223
edamame22 0:29983394c6b6 224 const uint8_t key[] = "-----BEGIN PRIVATE KEY-----\r\n"
edamame22 0:29983394c6b6 225 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMG3xqDXYgubamdE\r\n"
edamame22 0:29983394c6b6 226 "dRzbIwFel3OykPBVXdUNo1NFfKlVQ6r+fElzrOvzIb8++Brb9dWKNZAMsAEdWFU+\r\n"
edamame22 0:29983394c6b6 227 "h66XNdVIQdFHZtHaWGq6UvN1TM9gn7Il3j7TzZqSjF3OiibyazYsWWCJc7uXIGDW\r\n"
edamame22 0:29983394c6b6 228 "3vjGMRBqkKSvtN2XakP05NStx+gTAgMBAAECgYEAlRimNxsu6jIXJh4paSk8ewxb\r\n"
edamame22 0:29983394c6b6 229 "0v+n0GJT0uwpPakotofaihxOepYejIfX/IMxoDKVtmImsZUiTelRGGMFORpGomS7\r\n"
edamame22 0:29983394c6b6 230 "3Pb9awfn1yWiYja9MF7+pgFtXj6LxcB5rcxrP8s9rCs+kEq8yl/khGXmlwlpacUm\r\n"
edamame22 0:29983394c6b6 231 "Pdg1rEHriykk/5dlyKkCQQD9zpgQKN87bQNOVSgFSNsrwCjEN2MvVUC6ycqVYqQd\r\n"
edamame22 0:29983394c6b6 232 "oQZrO2lJuf57Jn7kRKJgfqqImwLYaohn5A9gKiqszo+fAkEAw2REr8L9aLoRPe5Q\r\n"
edamame22 0:29983394c6b6 233 "4+lYCmNmxJjBbJoRviSlxx+p5dxK+8H4J+bnFUWzORFJP0+Bjhii2UHGBoMjuP2s\r\n"
edamame22 0:29983394c6b6 234 "e8xDDQJADm0IrZp1Hb6TeodSkAJVGsaASq7PP2h8FmHT1uvVYi293Khy0pL0yPEm\r\n"
edamame22 0:29983394c6b6 235 "MzlHdW6dqMgOwFhFyonm6dNbu5i+BwJBALSPKfIKDMcEev5yBgo3Dga7iLJQPx4u\r\n"
edamame22 0:29983394c6b6 236 "2gqOLyvtlME3a4tu2fV1qUG9ITuxP7DjteMr3QFLiMVn1lETCpt0TNkCQGIvyjdV\r\n"
edamame22 0:29983394c6b6 237 "RzYnzLDMulXCsuRBjs0C75KoZrcPPQ416SZFtYASsOl3P0Ih5jgt/1aPmth3Yson\r\n"
edamame22 0:29983394c6b6 238 "GdnBOWyKzadLmz8=\r\n"
edamame22 0:29983394c6b6 239 "-----END PRIVATE KEY-----\r\n";
edamame22 0:29983394c6b6 240
edamame22 0:29983394c6b6 241 const size_t server_cert_len = sizeof( server_cert );
edamame22 0:29983394c6b6 242 const size_t cert_len = sizeof( cert );
edamame22 0:29983394c6b6 243 const size_t key_len = sizeof( key );
edamame22 0:29983394c6b6 244