Wang Xinglu / BLE_API

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Fri May 23 08:56:31 2014 +0100
Revision:
41:7ae5c71cd44c
Parent:
40:d405c9b1419d
Child:
42:06e75fee52cf
Use composition to hide the transport functionality behind an interface object.
The underlying target BLE library must implement the function which creates this object.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 29:011e95ce78b8 1 /* mbed Microcontroller Library
ktownsend 29:011e95ce78b8 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 29:011e95ce78b8 3 *
ktownsend 29:011e95ce78b8 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 29:011e95ce78b8 5 * you may not use this file except in compliance with the License.
ktownsend 29:011e95ce78b8 6 * You may obtain a copy of the License at
ktownsend 29:011e95ce78b8 7 *
ktownsend 29:011e95ce78b8 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 29:011e95ce78b8 9 *
ktownsend 29:011e95ce78b8 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 29:011e95ce78b8 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 29:011e95ce78b8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 29:011e95ce78b8 13 * See the License for the specific language governing permissions and
ktownsend 29:011e95ce78b8 14 * limitations under the License.
ktownsend 29:011e95ce78b8 15 */
Rohit Grover 34:da2ea8cd6216 16
ktownsend 29:011e95ce78b8 17 #ifndef __BLE_DEVICE_H__
ktownsend 29:011e95ce78b8 18 #define __BLE_DEVICE_H__
ktownsend 29:011e95ce78b8 19
ktownsend 29:011e95ce78b8 20 #include "mbed.h"
ktownsend 29:011e95ce78b8 21 #include "blecommon.h"
ktownsend 29:011e95ce78b8 22 #include "hw/Gap.h"
ktownsend 29:011e95ce78b8 23 #include "hw/GattServer.h"
ktownsend 29:011e95ce78b8 24
Rohit Grover 41:7ae5c71cd44c 25 class BLEDeviceInstanceBase; /* forward declaration */
Rohit Grover 41:7ae5c71cd44c 26
Rohit Grover 41:7ae5c71cd44c 27 /**
Rohit Grover 41:7ae5c71cd44c 28 * BLEDevice uses composition to hide an interface object encapsulating the
Rohit Grover 41:7ae5c71cd44c 29 * backend transport.
Rohit Grover 41:7ae5c71cd44c 30 *
Rohit Grover 41:7ae5c71cd44c 31 * The following API is used to create the singleton interface object. An
Rohit Grover 41:7ae5c71cd44c 32 * implementation for this function must be provided by the device-specific
Rohit Grover 41:7ae5c71cd44c 33 * library, otherwise there will be a linker error.
Rohit Grover 41:7ae5c71cd44c 34 */
Rohit Grover 41:7ae5c71cd44c 35 extern BLEDeviceInstanceBase *createBLEDeviceInstance(void);
Rohit Grover 41:7ae5c71cd44c 36
Rohit Grover 41:7ae5c71cd44c 37 /**
Rohit Grover 41:7ae5c71cd44c 38 * The base class used to abstract away BLE capable radio transceivers or SOCs,
Rohit Grover 41:7ae5c71cd44c 39 * to enable this BLE API to work with any radio transparently.
Rohit Grover 41:7ae5c71cd44c 40 */
ktownsend 29:011e95ce78b8 41 class BLEDevice
ktownsend 29:011e95ce78b8 42 {
Rohit Grover 34:da2ea8cd6216 43 public:
Rohit Grover 41:7ae5c71cd44c 44 ble_error_t init();
Rohit Grover 41:7ae5c71cd44c 45 ble_error_t reset(void);
Rohit Grover 35:f3b0c1192cf7 46
Rohit Grover 35:f3b0c1192cf7 47 /* GAP specific APIs */
Rohit Grover 35:f3b0c1192cf7 48 public:
Rohit Grover 37:7e8de07642e0 49 /**
Rohit Grover 37:7e8de07642e0 50 * Set the BTLE MAC address and type.
Rohit Grover 37:7e8de07642e0 51 * @return
Rohit Grover 37:7e8de07642e0 52 */
Rohit Grover 38:02cf26a2a4db 53 ble_error_t setAddress(Gap::addr_type_t type, const uint8_t address[6]);
Rohit Grover 35:f3b0c1192cf7 54
Rohit Grover 40:d405c9b1419d 55 ble_error_t setAdvertisingData(const GapAdvertisingData &ADStructures,
Rohit Grover 40:d405c9b1419d 56 const GapAdvertisingData &scanResponse);
Rohit Grover 40:d405c9b1419d 57 ble_error_t setAdvertisingData(const GapAdvertisingData &ADStructures);
Rohit Grover 36:9ec94579bb78 58 ble_error_t startAdvertising(GapAdvertisingParams &advParams);
Rohit Grover 36:9ec94579bb78 59 ble_error_t stopAdvertising(void);
Rohit Grover 36:9ec94579bb78 60 ble_error_t disconnect(void);
Rohit Grover 35:f3b0c1192cf7 61
Rohit Grover 41:7ae5c71cd44c 62 public:
Rohit Grover 41:7ae5c71cd44c 63 BLEDevice() : transport(createBLEDeviceInstance()) {
Rohit Grover 41:7ae5c71cd44c 64 /* empty */
Rohit Grover 41:7ae5c71cd44c 65 }
Rohit Grover 41:7ae5c71cd44c 66
Rohit Grover 41:7ae5c71cd44c 67 private:
Rohit Grover 41:7ae5c71cd44c 68 BLEDeviceInstanceBase *transport;
Rohit Grover 41:7ae5c71cd44c 69 };
Rohit Grover 41:7ae5c71cd44c 70
Rohit Grover 41:7ae5c71cd44c 71 /**
Rohit Grover 41:7ae5c71cd44c 72 * The interface for the transport object to be created by the target library's
Rohit Grover 41:7ae5c71cd44c 73 * createBLEDeviceInstance().
Rohit Grover 41:7ae5c71cd44c 74 */
Rohit Grover 41:7ae5c71cd44c 75 class BLEDeviceInstanceBase
Rohit Grover 41:7ae5c71cd44c 76 {
Rohit Grover 41:7ae5c71cd44c 77 public:
Rohit Grover 34:da2ea8cd6216 78 virtual Gap& getGap() = 0;
Rohit Grover 34:da2ea8cd6216 79 virtual GattServer& getGattServer() = 0;
Rohit Grover 41:7ae5c71cd44c 80 virtual ble_error_t init(void) = 0;
Rohit Grover 41:7ae5c71cd44c 81 virtual ble_error_t reset(void) = 0;
ktownsend 29:011e95ce78b8 82 };
ktownsend 29:011e95ce78b8 83
Rohit Grover 41:7ae5c71cd44c 84
Rohit Grover 41:7ae5c71cd44c 85 /* BLEDevice methods. Most of these simply forward the calls to the underlying
Rohit Grover 41:7ae5c71cd44c 86 * transport.*/
Rohit Grover 41:7ae5c71cd44c 87
Rohit Grover 41:7ae5c71cd44c 88 inline ble_error_t
Rohit Grover 41:7ae5c71cd44c 89 BLEDevice::init() {
Rohit Grover 41:7ae5c71cd44c 90 return transport->init();
Rohit Grover 41:7ae5c71cd44c 91 }
Rohit Grover 41:7ae5c71cd44c 92
Rohit Grover 41:7ae5c71cd44c 93 inline ble_error_t
Rohit Grover 41:7ae5c71cd44c 94 BLEDevice::reset(void) {
Rohit Grover 41:7ae5c71cd44c 95 return transport->reset();
Rohit Grover 41:7ae5c71cd44c 96 }
Rohit Grover 41:7ae5c71cd44c 97
Rohit Grover 39:a57137537521 98 inline ble_error_t
Rohit Grover 39:a57137537521 99 BLEDevice::setAddress(Gap::addr_type_t type, const uint8_t address[6]) {
Rohit Grover 41:7ae5c71cd44c 100 return transport->getGap().setAddress(type, address);
Rohit Grover 36:9ec94579bb78 101 }
Rohit Grover 36:9ec94579bb78 102
Rohit Grover 39:a57137537521 103 inline ble_error_t
Rohit Grover 40:d405c9b1419d 104 BLEDevice::setAdvertisingData(const GapAdvertisingData &ADStructures,
Rohit Grover 40:d405c9b1419d 105 const GapAdvertisingData &scanResponse) {
Rohit Grover 41:7ae5c71cd44c 106 return transport->getGap().setAdvertisingData(ADStructures, scanResponse);
Rohit Grover 36:9ec94579bb78 107 }
Rohit Grover 36:9ec94579bb78 108
Rohit Grover 39:a57137537521 109 inline ble_error_t
Rohit Grover 40:d405c9b1419d 110 BLEDevice::setAdvertisingData(const GapAdvertisingData &ADStructures) {
Rohit Grover 36:9ec94579bb78 111 GapAdvertisingData scanResponse;
Rohit Grover 41:7ae5c71cd44c 112 return transport->getGap().setAdvertisingData(ADStructures, scanResponse);
Rohit Grover 36:9ec94579bb78 113 }
Rohit Grover 36:9ec94579bb78 114
Rohit Grover 39:a57137537521 115 inline ble_error_t
Rohit Grover 39:a57137537521 116 BLEDevice::startAdvertising(GapAdvertisingParams &advParams) {
Rohit Grover 41:7ae5c71cd44c 117 return transport->getGap().startAdvertising(advParams);
Rohit Grover 36:9ec94579bb78 118 }
Rohit Grover 36:9ec94579bb78 119
Rohit Grover 39:a57137537521 120 inline ble_error_t
Rohit Grover 39:a57137537521 121 BLEDevice::stopAdvertising(void) {
Rohit Grover 41:7ae5c71cd44c 122 return transport->getGap().stopAdvertising();
Rohit Grover 36:9ec94579bb78 123 }
Rohit Grover 36:9ec94579bb78 124
Rohit Grover 39:a57137537521 125 inline ble_error_t
Rohit Grover 39:a57137537521 126 BLEDevice::disconnect(void) {
Rohit Grover 41:7ae5c71cd44c 127 return transport->getGap().disconnect();
Rohit Grover 36:9ec94579bb78 128 }
Rohit Grover 36:9ec94579bb78 129
Rohit Grover 34:da2ea8cd6216 130 #endif // ifndef __BLE_DEVICE_H__