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/include/smartpointer.h@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 | #ifndef SMART_POINTER_H |
edamame22 | 0:29983394c6b6 | 17 | #define SMART_POINTER_H |
edamame22 | 0:29983394c6b6 | 18 | |
edamame22 | 0:29983394c6b6 | 19 | class ReferenceCount |
edamame22 | 0:29983394c6b6 | 20 | { |
edamame22 | 0:29983394c6b6 | 21 | private: |
edamame22 | 0:29983394c6b6 | 22 | |
edamame22 | 0:29983394c6b6 | 23 | int _count; // Reference count |
edamame22 | 0:29983394c6b6 | 24 | |
edamame22 | 0:29983394c6b6 | 25 | public: |
edamame22 | 0:29983394c6b6 | 26 | |
edamame22 | 0:29983394c6b6 | 27 | void add_ref() |
edamame22 | 0:29983394c6b6 | 28 | { |
edamame22 | 0:29983394c6b6 | 29 | // Increment the reference count |
edamame22 | 0:29983394c6b6 | 30 | _count++; |
edamame22 | 0:29983394c6b6 | 31 | } |
edamame22 | 0:29983394c6b6 | 32 | |
edamame22 | 0:29983394c6b6 | 33 | int release() |
edamame22 | 0:29983394c6b6 | 34 | { |
edamame22 | 0:29983394c6b6 | 35 | // Decrement the reference count and |
edamame22 | 0:29983394c6b6 | 36 | // return the reference count. |
edamame22 | 0:29983394c6b6 | 37 | return --_count; |
edamame22 | 0:29983394c6b6 | 38 | } |
edamame22 | 0:29983394c6b6 | 39 | }; |
edamame22 | 0:29983394c6b6 | 40 | |
edamame22 | 0:29983394c6b6 | 41 | template < typename T > class SmartPointer |
edamame22 | 0:29983394c6b6 | 42 | { |
edamame22 | 0:29983394c6b6 | 43 | private: |
edamame22 | 0:29983394c6b6 | 44 | |
edamame22 | 0:29983394c6b6 | 45 | T *_data; // Generic pointer to be stored |
edamame22 | 0:29983394c6b6 | 46 | ReferenceCount *_reference; // Reference count |
edamame22 | 0:29983394c6b6 | 47 | |
edamame22 | 0:29983394c6b6 | 48 | |
edamame22 | 0:29983394c6b6 | 49 | public: |
edamame22 | 0:29983394c6b6 | 50 | |
edamame22 | 0:29983394c6b6 | 51 | SmartPointer() |
edamame22 | 0:29983394c6b6 | 52 | : _data(0), _reference(0) |
edamame22 | 0:29983394c6b6 | 53 | { |
edamame22 | 0:29983394c6b6 | 54 | // Create a new reference |
edamame22 | 0:29983394c6b6 | 55 | _reference = new ReferenceCount(); |
edamame22 | 0:29983394c6b6 | 56 | // Increment the reference count |
edamame22 | 0:29983394c6b6 | 57 | _reference->add_ref(); |
edamame22 | 0:29983394c6b6 | 58 | } |
edamame22 | 0:29983394c6b6 | 59 | |
edamame22 | 0:29983394c6b6 | 60 | SmartPointer(T* value) |
edamame22 | 0:29983394c6b6 | 61 | : _data(value), _reference(0) |
edamame22 | 0:29983394c6b6 | 62 | { |
edamame22 | 0:29983394c6b6 | 63 | // Create a new reference |
edamame22 | 0:29983394c6b6 | 64 | _reference = new ReferenceCount(); |
edamame22 | 0:29983394c6b6 | 65 | // Increment the reference count |
edamame22 | 0:29983394c6b6 | 66 | _reference->add_ref(); |
edamame22 | 0:29983394c6b6 | 67 | } |
edamame22 | 0:29983394c6b6 | 68 | |
edamame22 | 0:29983394c6b6 | 69 | SmartPointer(const SmartPointer<T>& smart_pointer) |
edamame22 | 0:29983394c6b6 | 70 | : _data(smart_pointer._data), reference(smart_pointer._reference) |
edamame22 | 0:29983394c6b6 | 71 | { |
edamame22 | 0:29983394c6b6 | 72 | // Copy constructor |
edamame22 | 0:29983394c6b6 | 73 | // Copy the data and reference pointer |
edamame22 | 0:29983394c6b6 | 74 | // and increment the reference count |
edamame22 | 0:29983394c6b6 | 75 | _reference->add_ref(); |
edamame22 | 0:29983394c6b6 | 76 | } |
edamame22 | 0:29983394c6b6 | 77 | |
edamame22 | 0:29983394c6b6 | 78 | ~SmartPointer() |
edamame22 | 0:29983394c6b6 | 79 | { |
edamame22 | 0:29983394c6b6 | 80 | if(_reference->release() == 0) { |
edamame22 | 0:29983394c6b6 | 81 | delete _data; |
edamame22 | 0:29983394c6b6 | 82 | delete _reference; |
edamame22 | 0:29983394c6b6 | 83 | } |
edamame22 | 0:29983394c6b6 | 84 | } |
edamame22 | 0:29983394c6b6 | 85 | |
edamame22 | 0:29983394c6b6 | 86 | T& operator* () |
edamame22 | 0:29983394c6b6 | 87 | { |
edamame22 | 0:29983394c6b6 | 88 | return *_data; |
edamame22 | 0:29983394c6b6 | 89 | } |
edamame22 | 0:29983394c6b6 | 90 | |
edamame22 | 0:29983394c6b6 | 91 | T* operator-> () |
edamame22 | 0:29983394c6b6 | 92 | { |
edamame22 | 0:29983394c6b6 | 93 | return _data; |
edamame22 | 0:29983394c6b6 | 94 | } |
edamame22 | 0:29983394c6b6 | 95 | |
edamame22 | 0:29983394c6b6 | 96 | SmartPointer<T>& operator = (const SmartPointer<T>& smart_pointer) |
edamame22 | 0:29983394c6b6 | 97 | { |
edamame22 | 0:29983394c6b6 | 98 | // Assignment operator |
edamame22 | 0:29983394c6b6 | 99 | if (this != &SmartPointer) { // Avoid self assignment |
edamame22 | 0:29983394c6b6 | 100 | // Decrement the old reference count |
edamame22 | 0:29983394c6b6 | 101 | // if reference become zero delete the old data |
edamame22 | 0:29983394c6b6 | 102 | if(_reference->release() == 0) { |
edamame22 | 0:29983394c6b6 | 103 | delete _data; |
edamame22 | 0:29983394c6b6 | 104 | delete _reference; |
edamame22 | 0:29983394c6b6 | 105 | } |
edamame22 | 0:29983394c6b6 | 106 | |
edamame22 | 0:29983394c6b6 | 107 | // Copy the data and reference pointer |
edamame22 | 0:29983394c6b6 | 108 | // and increment the reference count |
edamame22 | 0:29983394c6b6 | 109 | _data = SmartPointer._data; |
edamame22 | 0:29983394c6b6 | 110 | _reference = SmartPointer._reference; |
edamame22 | 0:29983394c6b6 | 111 | _reference->add_ref(); |
edamame22 | 0:29983394c6b6 | 112 | } |
edamame22 | 0:29983394c6b6 | 113 | return *this; |
edamame22 | 0:29983394c6b6 | 114 | } |
edamame22 | 0:29983394c6b6 | 115 | |
edamame22 | 0:29983394c6b6 | 116 | }; |
edamame22 | 0:29983394c6b6 | 117 | |
edamame22 | 0:29983394c6b6 | 118 | #endif // SMART_POINTER_H |