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) 2016 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/m2mstringbufferbase.h"
edamame22 0:29983394c6b6 18
edamame22 0:29983394c6b6 19 #include "mbed-client/m2mstring.h"
edamame22 0:29983394c6b6 20
edamame22 0:29983394c6b6 21 #include <assert.h>
edamame22 0:29983394c6b6 22 #include <string.h>
edamame22 0:29983394c6b6 23
edamame22 0:29983394c6b6 24
edamame22 0:29983394c6b6 25 bool StringBufferBase::ensure_space(size_t max_size, size_t required_size) const
edamame22 0:29983394c6b6 26 {
edamame22 0:29983394c6b6 27 const size_t space_left = max_size - _curr_size;
edamame22 0:29983394c6b6 28
edamame22 0:29983394c6b6 29 bool space_available = false;
edamame22 0:29983394c6b6 30
edamame22 0:29983394c6b6 31 if (required_size <= space_left) {
edamame22 0:29983394c6b6 32
edamame22 0:29983394c6b6 33 space_available = true;
edamame22 0:29983394c6b6 34 }
edamame22 0:29983394c6b6 35 return space_available;
edamame22 0:29983394c6b6 36 }
edamame22 0:29983394c6b6 37
edamame22 0:29983394c6b6 38 bool StringBufferBase::append(char *buff, size_t max_size, char data)
edamame22 0:29983394c6b6 39 {
edamame22 0:29983394c6b6 40 bool space_available = ensure_space(max_size, 1 + 1); // there must be space for trailing zero too
edamame22 0:29983394c6b6 41 if (space_available) {
edamame22 0:29983394c6b6 42 buff[_curr_size++] = data;
edamame22 0:29983394c6b6 43 buff[_curr_size] = '\0';
edamame22 0:29983394c6b6 44 assert(_curr_size < max_size);
edamame22 0:29983394c6b6 45 }
edamame22 0:29983394c6b6 46 return space_available;
edamame22 0:29983394c6b6 47 }
edamame22 0:29983394c6b6 48
edamame22 0:29983394c6b6 49 bool StringBufferBase::append(char *buff, size_t max_size, const char *data)
edamame22 0:29983394c6b6 50 {
edamame22 0:29983394c6b6 51
edamame22 0:29983394c6b6 52 const size_t string_len = strlen(data);
edamame22 0:29983394c6b6 53 bool space_available = ensure_space(max_size, string_len + 1);
edamame22 0:29983394c6b6 54 if (space_available) {
edamame22 0:29983394c6b6 55 memcpy(buff + _curr_size, data, string_len + 1); // copy the zero terminator too
edamame22 0:29983394c6b6 56 _curr_size += string_len;
edamame22 0:29983394c6b6 57 assert(_curr_size < max_size);
edamame22 0:29983394c6b6 58 }
edamame22 0:29983394c6b6 59 return space_available;
edamame22 0:29983394c6b6 60 }
edamame22 0:29983394c6b6 61
edamame22 0:29983394c6b6 62 bool StringBufferBase::append(char *buff, size_t max_size, const char *data, size_t data_len)
edamame22 0:29983394c6b6 63 {
edamame22 0:29983394c6b6 64 bool space_available = true;
edamame22 0:29983394c6b6 65 if (data_len > 0) {
edamame22 0:29983394c6b6 66 space_available = ensure_space(max_size, data_len + 1);
edamame22 0:29983394c6b6 67 if (space_available) {
edamame22 0:29983394c6b6 68 memcpy(buff + _curr_size, data, data_len);
edamame22 0:29983394c6b6 69 _curr_size += data_len;
edamame22 0:29983394c6b6 70 // Todo: should the code actually check, if the data already contained zero or not?
edamame22 0:29983394c6b6 71 buff[_curr_size] = '\0';
edamame22 0:29983394c6b6 72 assert(_curr_size < max_size);
edamame22 0:29983394c6b6 73 }
edamame22 0:29983394c6b6 74 }
edamame22 0:29983394c6b6 75 return space_available;
edamame22 0:29983394c6b6 76 }
edamame22 0:29983394c6b6 77
edamame22 0:29983394c6b6 78 bool StringBufferBase::append_int(char *buff, size_t max_size, uint16_t data)
edamame22 0:29983394c6b6 79 {
edamame22 0:29983394c6b6 80 // max len of "-9223372036854775808" plus zero termination
edamame22 0:29983394c6b6 81 char conv_buff[20+1];
edamame22 0:29983394c6b6 82
edamame22 0:29983394c6b6 83 // re-use the String's functionality, a more optimal version would use snprintf() or int size specific converter
edamame22 0:29983394c6b6 84 int len = m2m::itoa_c(data, conv_buff);
edamame22 0:29983394c6b6 85
edamame22 0:29983394c6b6 86 return append(buff, max_size, conv_buff, len);
edamame22 0:29983394c6b6 87 }
edamame22 0:29983394c6b6 88
edamame22 0:29983394c6b6 89 int StringBufferBase::find_last_of(const char *buff, char search_char) const
edamame22 0:29983394c6b6 90 {
edamame22 0:29983394c6b6 91 int last_index = -1;
edamame22 0:29983394c6b6 92 // search from the end of string, return upon first found matching char
edamame22 0:29983394c6b6 93 for (int index = _curr_size; index >= 0; index--) {
edamame22 0:29983394c6b6 94 if (buff[index] == search_char) {
edamame22 0:29983394c6b6 95 last_index = index;
edamame22 0:29983394c6b6 96 break;
edamame22 0:29983394c6b6 97 }
edamame22 0:29983394c6b6 98 }
edamame22 0:29983394c6b6 99
edamame22 0:29983394c6b6 100 return last_index;
edamame22 0:29983394c6b6 101 }