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/
mbed-client/source/m2mserver.cpp@2:b894b3508057, 2017-09-05 (annotated)
- 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?
User | Revision | Line number | New 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 | |
edamame22 | 0:29983394c6b6 | 17 | #include "mbed-client/m2mserver.h" |
edamame22 | 0:29983394c6b6 | 18 | #include "mbed-client/m2mconstants.h" |
edamame22 | 0:29983394c6b6 | 19 | #include "mbed-client/m2mobject.h" |
edamame22 | 0:29983394c6b6 | 20 | #include "mbed-client/m2mobjectinstance.h" |
edamame22 | 0:29983394c6b6 | 21 | #include "mbed-client/m2mresource.h" |
edamame22 | 0:29983394c6b6 | 22 | #include "mbed-client/m2mstring.h" |
edamame22 | 0:29983394c6b6 | 23 | |
edamame22 | 0:29983394c6b6 | 24 | #define TRACE_GROUP "mClt" |
edamame22 | 0:29983394c6b6 | 25 | |
edamame22 | 0:29983394c6b6 | 26 | #define BUFFER_SIZE 21 |
edamame22 | 0:29983394c6b6 | 27 | |
edamame22 | 0:29983394c6b6 | 28 | M2MServer::M2MServer() |
edamame22 | 0:29983394c6b6 | 29 | : M2MObject(M2M_SERVER_ID, stringdup(M2M_SERVER_ID)) |
edamame22 | 0:29983394c6b6 | 30 | { |
edamame22 | 0:29983394c6b6 | 31 | M2MObject::create_object_instance(); |
edamame22 | 0:29983394c6b6 | 32 | |
edamame22 | 0:29983394c6b6 | 33 | _server_instance = object_instance(); |
edamame22 | 0:29983394c6b6 | 34 | |
edamame22 | 0:29983394c6b6 | 35 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 36 | |
edamame22 | 0:29983394c6b6 | 37 | M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID, |
edamame22 | 0:29983394c6b6 | 38 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 39 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 40 | true); |
edamame22 | 0:29983394c6b6 | 41 | if(res) { |
edamame22 | 0:29983394c6b6 | 42 | res->set_operation(M2MBase::GET_PUT_ALLOWED); |
edamame22 | 0:29983394c6b6 | 43 | } |
edamame22 | 0:29983394c6b6 | 44 | res = _server_instance->create_dynamic_resource(SERVER_LIFETIME, |
edamame22 | 0:29983394c6b6 | 45 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 46 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 47 | true); |
edamame22 | 0:29983394c6b6 | 48 | if(res) { |
edamame22 | 0:29983394c6b6 | 49 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 50 | } |
edamame22 | 0:29983394c6b6 | 51 | res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE, |
edamame22 | 0:29983394c6b6 | 52 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 53 | M2MResourceInstance::BOOLEAN, |
edamame22 | 0:29983394c6b6 | 54 | true); |
edamame22 | 0:29983394c6b6 | 55 | if(res) { |
edamame22 | 0:29983394c6b6 | 56 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 57 | } |
edamame22 | 0:29983394c6b6 | 58 | res = _server_instance->create_dynamic_resource(SERVER_BINDING, |
edamame22 | 0:29983394c6b6 | 59 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 60 | M2MResourceInstance::STRING, |
edamame22 | 0:29983394c6b6 | 61 | true); |
edamame22 | 0:29983394c6b6 | 62 | if(res) { |
edamame22 | 0:29983394c6b6 | 63 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 64 | } |
edamame22 | 0:29983394c6b6 | 65 | res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE, |
edamame22 | 0:29983394c6b6 | 66 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 67 | M2MResourceInstance::OPAQUE, |
edamame22 | 0:29983394c6b6 | 68 | false); |
edamame22 | 0:29983394c6b6 | 69 | if(res) { |
edamame22 | 0:29983394c6b6 | 70 | res->set_operation(M2MBase::POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 71 | } |
edamame22 | 0:29983394c6b6 | 72 | } |
edamame22 | 0:29983394c6b6 | 73 | } |
edamame22 | 0:29983394c6b6 | 74 | |
edamame22 | 0:29983394c6b6 | 75 | M2MServer::~M2MServer() |
edamame22 | 0:29983394c6b6 | 76 | { |
edamame22 | 0:29983394c6b6 | 77 | |
edamame22 | 0:29983394c6b6 | 78 | } |
edamame22 | 0:29983394c6b6 | 79 | |
edamame22 | 0:29983394c6b6 | 80 | M2MResource* M2MServer::create_resource(ServerResource resource, uint32_t value) |
edamame22 | 0:29983394c6b6 | 81 | { |
edamame22 | 0:29983394c6b6 | 82 | M2MResource* res = NULL; |
edamame22 | 0:29983394c6b6 | 83 | const char* server_id_ptr = ""; |
edamame22 | 0:29983394c6b6 | 84 | if(!is_resource_present(resource)) { |
edamame22 | 0:29983394c6b6 | 85 | switch(resource) { |
edamame22 | 0:29983394c6b6 | 86 | case DefaultMinPeriod: |
edamame22 | 0:29983394c6b6 | 87 | server_id_ptr = SERVER_DEFAULT_MIN_PERIOD; |
edamame22 | 0:29983394c6b6 | 88 | break; |
edamame22 | 0:29983394c6b6 | 89 | case DefaultMaxPeriod: |
edamame22 | 0:29983394c6b6 | 90 | server_id_ptr = SERVER_DEFAULT_MAX_PERIOD; |
edamame22 | 0:29983394c6b6 | 91 | break; |
edamame22 | 0:29983394c6b6 | 92 | case DisableTimeout: |
edamame22 | 0:29983394c6b6 | 93 | server_id_ptr = SERVER_DISABLE_TIMEOUT; |
edamame22 | 0:29983394c6b6 | 94 | break; |
edamame22 | 0:29983394c6b6 | 95 | default: |
edamame22 | 0:29983394c6b6 | 96 | break; |
edamame22 | 0:29983394c6b6 | 97 | } |
edamame22 | 0:29983394c6b6 | 98 | } |
edamame22 | 0:29983394c6b6 | 99 | String server_id(server_id_ptr); |
edamame22 | 0:29983394c6b6 | 100 | |
edamame22 | 0:29983394c6b6 | 101 | if(!server_id.empty()) { |
edamame22 | 0:29983394c6b6 | 102 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 103 | res = _server_instance->create_dynamic_resource(server_id, |
edamame22 | 0:29983394c6b6 | 104 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 105 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 106 | true); |
edamame22 | 0:29983394c6b6 | 107 | if(res) { |
edamame22 | 0:29983394c6b6 | 108 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 109 | |
edamame22 | 0:29983394c6b6 | 110 | res->set_value(value); |
edamame22 | 0:29983394c6b6 | 111 | } |
edamame22 | 0:29983394c6b6 | 112 | } |
edamame22 | 0:29983394c6b6 | 113 | } |
edamame22 | 0:29983394c6b6 | 114 | return res; |
edamame22 | 0:29983394c6b6 | 115 | } |
edamame22 | 0:29983394c6b6 | 116 | |
edamame22 | 0:29983394c6b6 | 117 | M2MResource* M2MServer::create_resource(ServerResource resource) |
edamame22 | 0:29983394c6b6 | 118 | { |
edamame22 | 0:29983394c6b6 | 119 | M2MResource* res = NULL; |
edamame22 | 0:29983394c6b6 | 120 | if(!is_resource_present(resource)) { |
edamame22 | 0:29983394c6b6 | 121 | if(M2MServer::Disable == resource) { |
edamame22 | 0:29983394c6b6 | 122 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 123 | res = _server_instance->create_dynamic_resource(SERVER_DISABLE, |
edamame22 | 0:29983394c6b6 | 124 | OMA_RESOURCE_TYPE, |
edamame22 | 0:29983394c6b6 | 125 | M2MResourceInstance::OPAQUE, |
edamame22 | 0:29983394c6b6 | 126 | false); |
edamame22 | 0:29983394c6b6 | 127 | if(res) { |
edamame22 | 0:29983394c6b6 | 128 | res->set_operation(M2MBase::POST_ALLOWED); |
edamame22 | 0:29983394c6b6 | 129 | } |
edamame22 | 0:29983394c6b6 | 130 | } |
edamame22 | 0:29983394c6b6 | 131 | } |
edamame22 | 0:29983394c6b6 | 132 | } |
edamame22 | 0:29983394c6b6 | 133 | return res; |
edamame22 | 0:29983394c6b6 | 134 | } |
edamame22 | 0:29983394c6b6 | 135 | |
edamame22 | 0:29983394c6b6 | 136 | bool M2MServer::delete_resource(ServerResource resource) |
edamame22 | 0:29983394c6b6 | 137 | { |
edamame22 | 0:29983394c6b6 | 138 | bool success = false; |
edamame22 | 0:29983394c6b6 | 139 | const char* server_id_ptr; |
edamame22 | 0:29983394c6b6 | 140 | switch(resource) { |
edamame22 | 0:29983394c6b6 | 141 | case DefaultMinPeriod: |
edamame22 | 0:29983394c6b6 | 142 | server_id_ptr = SERVER_DEFAULT_MIN_PERIOD; |
edamame22 | 0:29983394c6b6 | 143 | break; |
edamame22 | 0:29983394c6b6 | 144 | case DefaultMaxPeriod: |
edamame22 | 0:29983394c6b6 | 145 | server_id_ptr = SERVER_DEFAULT_MAX_PERIOD; |
edamame22 | 0:29983394c6b6 | 146 | break; |
edamame22 | 0:29983394c6b6 | 147 | case Disable: |
edamame22 | 0:29983394c6b6 | 148 | server_id_ptr = SERVER_DISABLE; |
edamame22 | 0:29983394c6b6 | 149 | break; |
edamame22 | 0:29983394c6b6 | 150 | case DisableTimeout: |
edamame22 | 0:29983394c6b6 | 151 | server_id_ptr = SERVER_DISABLE_TIMEOUT; |
edamame22 | 0:29983394c6b6 | 152 | break; |
edamame22 | 0:29983394c6b6 | 153 | default: |
edamame22 | 0:29983394c6b6 | 154 | server_id_ptr = NULL; |
edamame22 | 0:29983394c6b6 | 155 | break; |
edamame22 | 0:29983394c6b6 | 156 | } |
edamame22 | 0:29983394c6b6 | 157 | |
edamame22 | 0:29983394c6b6 | 158 | if(server_id_ptr) { |
edamame22 | 0:29983394c6b6 | 159 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 160 | success = _server_instance->remove_resource(server_id_ptr); |
edamame22 | 0:29983394c6b6 | 161 | } |
edamame22 | 0:29983394c6b6 | 162 | } |
edamame22 | 0:29983394c6b6 | 163 | return success; |
edamame22 | 0:29983394c6b6 | 164 | } |
edamame22 | 0:29983394c6b6 | 165 | |
edamame22 | 0:29983394c6b6 | 166 | bool M2MServer::set_resource_value(ServerResource resource, |
edamame22 | 0:29983394c6b6 | 167 | const String &value) |
edamame22 | 0:29983394c6b6 | 168 | { |
edamame22 | 0:29983394c6b6 | 169 | bool success = false; |
edamame22 | 0:29983394c6b6 | 170 | M2MResource* res = get_resource(resource); |
edamame22 | 0:29983394c6b6 | 171 | if(res && (M2MServer::Binding == resource)) { |
edamame22 | 0:29983394c6b6 | 172 | success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); |
edamame22 | 0:29983394c6b6 | 173 | } |
edamame22 | 0:29983394c6b6 | 174 | return success; |
edamame22 | 0:29983394c6b6 | 175 | } |
edamame22 | 0:29983394c6b6 | 176 | |
edamame22 | 0:29983394c6b6 | 177 | bool M2MServer::set_resource_value(ServerResource resource, |
edamame22 | 0:29983394c6b6 | 178 | uint32_t value) |
edamame22 | 0:29983394c6b6 | 179 | { |
edamame22 | 0:29983394c6b6 | 180 | bool success = false; |
edamame22 | 0:29983394c6b6 | 181 | M2MResource* res = get_resource(resource); |
edamame22 | 0:29983394c6b6 | 182 | if(res) { |
edamame22 | 0:29983394c6b6 | 183 | if(M2MServer::ShortServerID == resource || |
edamame22 | 0:29983394c6b6 | 184 | M2MServer::Lifetime == resource || |
edamame22 | 0:29983394c6b6 | 185 | M2MServer::DefaultMinPeriod == resource || |
edamame22 | 0:29983394c6b6 | 186 | M2MServer::DefaultMaxPeriod == resource || |
edamame22 | 0:29983394c6b6 | 187 | M2MServer::DisableTimeout == resource || |
edamame22 | 0:29983394c6b6 | 188 | M2MServer::NotificationStorage == resource) { |
edamame22 | 0:29983394c6b6 | 189 | // If it is any of the above resource |
edamame22 | 0:29983394c6b6 | 190 | // set the value of the resource. |
edamame22 | 0:29983394c6b6 | 191 | |
edamame22 | 0:29983394c6b6 | 192 | success = res->set_value(value); |
edamame22 | 0:29983394c6b6 | 193 | } |
edamame22 | 0:29983394c6b6 | 194 | } |
edamame22 | 0:29983394c6b6 | 195 | return success; |
edamame22 | 0:29983394c6b6 | 196 | } |
edamame22 | 0:29983394c6b6 | 197 | |
edamame22 | 0:29983394c6b6 | 198 | String M2MServer::resource_value_string(ServerResource resource) const |
edamame22 | 0:29983394c6b6 | 199 | { |
edamame22 | 0:29983394c6b6 | 200 | String value = ""; |
edamame22 | 0:29983394c6b6 | 201 | M2MResource* res = get_resource(resource); |
edamame22 | 0:29983394c6b6 | 202 | if(res && (M2MServer::Binding == resource)) { |
edamame22 | 0:29983394c6b6 | 203 | |
edamame22 | 0:29983394c6b6 | 204 | value = res->get_value_string(); |
edamame22 | 0:29983394c6b6 | 205 | } |
edamame22 | 0:29983394c6b6 | 206 | return value; |
edamame22 | 0:29983394c6b6 | 207 | } |
edamame22 | 0:29983394c6b6 | 208 | |
edamame22 | 0:29983394c6b6 | 209 | |
edamame22 | 0:29983394c6b6 | 210 | uint32_t M2MServer::resource_value_int(ServerResource resource) const |
edamame22 | 0:29983394c6b6 | 211 | { |
edamame22 | 0:29983394c6b6 | 212 | uint32_t value = 0; |
edamame22 | 0:29983394c6b6 | 213 | M2MResource* res = get_resource(resource); |
edamame22 | 0:29983394c6b6 | 214 | if(res) { |
edamame22 | 0:29983394c6b6 | 215 | if(M2MServer::ShortServerID == resource || |
edamame22 | 0:29983394c6b6 | 216 | M2MServer::Lifetime == resource || |
edamame22 | 0:29983394c6b6 | 217 | M2MServer::DefaultMinPeriod == resource || |
edamame22 | 0:29983394c6b6 | 218 | M2MServer::DefaultMaxPeriod == resource || |
edamame22 | 0:29983394c6b6 | 219 | M2MServer::DisableTimeout == resource || |
edamame22 | 0:29983394c6b6 | 220 | M2MServer::NotificationStorage == resource) { |
edamame22 | 0:29983394c6b6 | 221 | |
edamame22 | 0:29983394c6b6 | 222 | value = res->get_value_int(); |
edamame22 | 0:29983394c6b6 | 223 | } |
edamame22 | 0:29983394c6b6 | 224 | } |
edamame22 | 0:29983394c6b6 | 225 | return value; |
edamame22 | 0:29983394c6b6 | 226 | } |
edamame22 | 0:29983394c6b6 | 227 | |
edamame22 | 0:29983394c6b6 | 228 | bool M2MServer::is_resource_present(ServerResource resource) const |
edamame22 | 0:29983394c6b6 | 229 | { |
edamame22 | 0:29983394c6b6 | 230 | bool success = false; |
edamame22 | 0:29983394c6b6 | 231 | M2MResource *res = get_resource(resource); |
edamame22 | 0:29983394c6b6 | 232 | if(res) { |
edamame22 | 0:29983394c6b6 | 233 | success = true; |
edamame22 | 0:29983394c6b6 | 234 | } |
edamame22 | 0:29983394c6b6 | 235 | return success; |
edamame22 | 0:29983394c6b6 | 236 | } |
edamame22 | 0:29983394c6b6 | 237 | |
edamame22 | 0:29983394c6b6 | 238 | uint16_t M2MServer::total_resource_count() const |
edamame22 | 0:29983394c6b6 | 239 | { |
edamame22 | 0:29983394c6b6 | 240 | uint16_t total_count = 0; |
edamame22 | 0:29983394c6b6 | 241 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 242 | total_count = _server_instance->resources().size(); |
edamame22 | 0:29983394c6b6 | 243 | } |
edamame22 | 0:29983394c6b6 | 244 | return total_count; |
edamame22 | 0:29983394c6b6 | 245 | } |
edamame22 | 0:29983394c6b6 | 246 | |
edamame22 | 0:29983394c6b6 | 247 | M2MResource* M2MServer::get_resource(ServerResource res) const |
edamame22 | 0:29983394c6b6 | 248 | { |
edamame22 | 0:29983394c6b6 | 249 | M2MResource* res_object = NULL; |
edamame22 | 0:29983394c6b6 | 250 | const char* res_name_ptr = NULL; |
edamame22 | 0:29983394c6b6 | 251 | switch(res) { |
edamame22 | 0:29983394c6b6 | 252 | case ShortServerID: |
edamame22 | 0:29983394c6b6 | 253 | res_name_ptr = SERVER_SHORT_SERVER_ID; |
edamame22 | 0:29983394c6b6 | 254 | break; |
edamame22 | 0:29983394c6b6 | 255 | case Lifetime: |
edamame22 | 0:29983394c6b6 | 256 | res_name_ptr = SERVER_LIFETIME; |
edamame22 | 0:29983394c6b6 | 257 | break; |
edamame22 | 0:29983394c6b6 | 258 | case DefaultMinPeriod: |
edamame22 | 0:29983394c6b6 | 259 | res_name_ptr = SERVER_DEFAULT_MIN_PERIOD; |
edamame22 | 0:29983394c6b6 | 260 | break; |
edamame22 | 0:29983394c6b6 | 261 | case DefaultMaxPeriod: |
edamame22 | 0:29983394c6b6 | 262 | res_name_ptr = SERVER_DEFAULT_MAX_PERIOD; |
edamame22 | 0:29983394c6b6 | 263 | break; |
edamame22 | 0:29983394c6b6 | 264 | case Disable: |
edamame22 | 0:29983394c6b6 | 265 | res_name_ptr = SERVER_DISABLE; |
edamame22 | 0:29983394c6b6 | 266 | break; |
edamame22 | 0:29983394c6b6 | 267 | case DisableTimeout: |
edamame22 | 0:29983394c6b6 | 268 | res_name_ptr = SERVER_DISABLE_TIMEOUT; |
edamame22 | 0:29983394c6b6 | 269 | break; |
edamame22 | 0:29983394c6b6 | 270 | case NotificationStorage: |
edamame22 | 0:29983394c6b6 | 271 | res_name_ptr = SERVER_NOTIFICATION_STORAGE; |
edamame22 | 0:29983394c6b6 | 272 | break; |
edamame22 | 0:29983394c6b6 | 273 | case Binding: |
edamame22 | 0:29983394c6b6 | 274 | res_name_ptr = SERVER_BINDING; |
edamame22 | 0:29983394c6b6 | 275 | break; |
edamame22 | 0:29983394c6b6 | 276 | case RegistrationUpdate: |
edamame22 | 0:29983394c6b6 | 277 | res_name_ptr = SERVER_REGISTRATION_UPDATE; |
edamame22 | 0:29983394c6b6 | 278 | break; |
edamame22 | 0:29983394c6b6 | 279 | } |
edamame22 | 0:29983394c6b6 | 280 | |
edamame22 | 0:29983394c6b6 | 281 | if(res_name_ptr) { |
edamame22 | 0:29983394c6b6 | 282 | if(_server_instance) { |
edamame22 | 0:29983394c6b6 | 283 | res_object = _server_instance->resource(res_name_ptr); |
edamame22 | 0:29983394c6b6 | 284 | } |
edamame22 | 0:29983394c6b6 | 285 | } |
edamame22 | 0:29983394c6b6 | 286 | return res_object; |
edamame22 | 0:29983394c6b6 | 287 | } |