High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
public/GattServer.h@715:6d415ac147aa, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 715:6d415ac147aa
- Parent:
- 714:a6130aaa0fd9
Synchronized with git rev 69726547
Author: Rohit Grover
Release 0.3.9
=============
A minor patch to fix a build error introduced by the previous
release. This has to do with certain declarations being made members
of class UUID.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 714:a6130aaa0fd9 | 1 | /* mbed Microcontroller Library |
rgrover1 | 714:a6130aaa0fd9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 714:a6130aaa0fd9 | 3 | * |
rgrover1 | 714:a6130aaa0fd9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 714:a6130aaa0fd9 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 714:a6130aaa0fd9 | 6 | * You may obtain a copy of the License at |
rgrover1 | 714:a6130aaa0fd9 | 7 | * |
rgrover1 | 714:a6130aaa0fd9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 714:a6130aaa0fd9 | 9 | * |
rgrover1 | 714:a6130aaa0fd9 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 714:a6130aaa0fd9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 714:a6130aaa0fd9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 714:a6130aaa0fd9 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 714:a6130aaa0fd9 | 14 | * limitations under the License. |
rgrover1 | 714:a6130aaa0fd9 | 15 | */ |
rgrover1 | 714:a6130aaa0fd9 | 16 | |
rgrover1 | 714:a6130aaa0fd9 | 17 | #ifndef __GATT_SERVER_H__ |
rgrover1 | 714:a6130aaa0fd9 | 18 | #define __GATT_SERVER_H__ |
rgrover1 | 714:a6130aaa0fd9 | 19 | |
rgrover1 | 714:a6130aaa0fd9 | 20 | #include "Gap.h" |
rgrover1 | 714:a6130aaa0fd9 | 21 | #include "GattService.h" |
rgrover1 | 714:a6130aaa0fd9 | 22 | #include "GattAttribute.h" |
rgrover1 | 714:a6130aaa0fd9 | 23 | #include "GattServerEvents.h" |
rgrover1 | 714:a6130aaa0fd9 | 24 | #include "GattCharacteristicCallbackParams.h" |
rgrover1 | 714:a6130aaa0fd9 | 25 | #include "CallChainOfFunctionPointersWithContext.h" |
rgrover1 | 714:a6130aaa0fd9 | 26 | |
rgrover1 | 714:a6130aaa0fd9 | 27 | class GattServer { |
rgrover1 | 714:a6130aaa0fd9 | 28 | public: |
rgrover1 | 714:a6130aaa0fd9 | 29 | /* Event callback handlers. */ |
rgrover1 | 714:a6130aaa0fd9 | 30 | typedef void (*EventCallback_t)(GattAttribute::Handle_t attributeHandle); |
rgrover1 | 714:a6130aaa0fd9 | 31 | typedef void (*ServerEventCallback_t)(void); /**< independent of any particular attribute */ |
rgrover1 | 714:a6130aaa0fd9 | 32 | |
rgrover1 | 714:a6130aaa0fd9 | 33 | protected: |
rgrover1 | 714:a6130aaa0fd9 | 34 | GattServer() : |
rgrover1 | 714:a6130aaa0fd9 | 35 | serviceCount(0), |
rgrover1 | 714:a6130aaa0fd9 | 36 | characteristicCount(0), |
rgrover1 | 714:a6130aaa0fd9 | 37 | onDataSent(), |
rgrover1 | 714:a6130aaa0fd9 | 38 | onDataWritten(), |
rgrover1 | 714:a6130aaa0fd9 | 39 | onDataRead(), |
rgrover1 | 714:a6130aaa0fd9 | 40 | onUpdatesEnabled(NULL), |
rgrover1 | 714:a6130aaa0fd9 | 41 | onUpdatesDisabled(NULL), |
rgrover1 | 714:a6130aaa0fd9 | 42 | onConfirmationReceived(NULL) { |
rgrover1 | 714:a6130aaa0fd9 | 43 | /* empty */ |
rgrover1 | 714:a6130aaa0fd9 | 44 | } |
rgrover1 | 714:a6130aaa0fd9 | 45 | |
rgrover1 | 714:a6130aaa0fd9 | 46 | friend class BLEDevice; |
rgrover1 | 714:a6130aaa0fd9 | 47 | private: |
rgrover1 | 714:a6130aaa0fd9 | 48 | /* These functions must be defined in the sub-class */ |
rgrover1 | 714:a6130aaa0fd9 | 49 | virtual ble_error_t addService(GattService &) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 50 | virtual ble_error_t readValue(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 51 | virtual ble_error_t readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 52 | virtual ble_error_t updateValue(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 53 | virtual ble_error_t updateValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 54 | virtual ble_error_t initializeGATTDatabase(void) = 0; |
rgrover1 | 714:a6130aaa0fd9 | 55 | |
rgrover1 | 714:a6130aaa0fd9 | 56 | // ToDo: For updateValue, check the CCCD to see if the value we are |
rgrover1 | 714:a6130aaa0fd9 | 57 | // updating has the notify or indicate bits sent, and if BOTH are set |
rgrover1 | 714:a6130aaa0fd9 | 58 | // be sure to call sd_ble_gatts_hvx() twice with notify then indicate! |
rgrover1 | 714:a6130aaa0fd9 | 59 | // Strange use case, but valid and must be covered! |
rgrover1 | 714:a6130aaa0fd9 | 60 | |
rgrover1 | 714:a6130aaa0fd9 | 61 | void setOnDataSent(void (*callback)(unsigned count)) {onDataSent.add(callback);} |
rgrover1 | 714:a6130aaa0fd9 | 62 | template <typename T> |
rgrover1 | 714:a6130aaa0fd9 | 63 | void setOnDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { |
rgrover1 | 714:a6130aaa0fd9 | 64 | onDataSent.add(objPtr, memberPtr); |
rgrover1 | 714:a6130aaa0fd9 | 65 | } |
rgrover1 | 714:a6130aaa0fd9 | 66 | void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {onDataWritten.add(callback);} |
rgrover1 | 714:a6130aaa0fd9 | 67 | template <typename T> |
rgrover1 | 714:a6130aaa0fd9 | 68 | void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) { |
rgrover1 | 714:a6130aaa0fd9 | 69 | onDataWritten.add(objPtr, memberPtr); |
rgrover1 | 714:a6130aaa0fd9 | 70 | } |
rgrover1 | 714:a6130aaa0fd9 | 71 | |
rgrover1 | 714:a6130aaa0fd9 | 72 | /** |
rgrover1 | 714:a6130aaa0fd9 | 73 | * A virtual function to allow underlying stacks to indicate if they support |
rgrover1 | 714:a6130aaa0fd9 | 74 | * onDataRead(). It should be overridden to return true as applicable. |
rgrover1 | 714:a6130aaa0fd9 | 75 | */ |
rgrover1 | 714:a6130aaa0fd9 | 76 | virtual bool isOnDataReadAvailable() const { |
rgrover1 | 714:a6130aaa0fd9 | 77 | return false; |
rgrover1 | 714:a6130aaa0fd9 | 78 | } |
rgrover1 | 714:a6130aaa0fd9 | 79 | ble_error_t setOnDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) { |
rgrover1 | 714:a6130aaa0fd9 | 80 | if (!isOnDataReadAvailable()) { |
rgrover1 | 714:a6130aaa0fd9 | 81 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 714:a6130aaa0fd9 | 82 | } |
rgrover1 | 714:a6130aaa0fd9 | 83 | |
rgrover1 | 714:a6130aaa0fd9 | 84 | onDataRead.add(callback); |
rgrover1 | 714:a6130aaa0fd9 | 85 | return BLE_ERROR_NONE; |
rgrover1 | 714:a6130aaa0fd9 | 86 | } |
rgrover1 | 714:a6130aaa0fd9 | 87 | template <typename T> |
rgrover1 | 714:a6130aaa0fd9 | 88 | ble_error_t setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) { |
rgrover1 | 714:a6130aaa0fd9 | 89 | if (!isOnDataReadAvailable()) { |
rgrover1 | 714:a6130aaa0fd9 | 90 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 714:a6130aaa0fd9 | 91 | } |
rgrover1 | 714:a6130aaa0fd9 | 92 | |
rgrover1 | 714:a6130aaa0fd9 | 93 | onDataRead.add(objPtr, memberPtr); |
rgrover1 | 714:a6130aaa0fd9 | 94 | return BLE_ERROR_NONE; |
rgrover1 | 714:a6130aaa0fd9 | 95 | } |
rgrover1 | 714:a6130aaa0fd9 | 96 | void setOnUpdatesEnabled(EventCallback_t callback) {onUpdatesEnabled = callback;} |
rgrover1 | 714:a6130aaa0fd9 | 97 | void setOnUpdatesDisabled(EventCallback_t callback) {onUpdatesDisabled = callback;} |
rgrover1 | 714:a6130aaa0fd9 | 98 | void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;} |
rgrover1 | 714:a6130aaa0fd9 | 99 | |
rgrover1 | 714:a6130aaa0fd9 | 100 | protected: |
rgrover1 | 714:a6130aaa0fd9 | 101 | void handleDataWrittenEvent(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 714:a6130aaa0fd9 | 102 | if (onDataWritten.hasCallbacksAttached()) { |
rgrover1 | 714:a6130aaa0fd9 | 103 | onDataWritten.call(params); |
rgrover1 | 714:a6130aaa0fd9 | 104 | } |
rgrover1 | 714:a6130aaa0fd9 | 105 | } |
rgrover1 | 714:a6130aaa0fd9 | 106 | |
rgrover1 | 714:a6130aaa0fd9 | 107 | void handleDataReadEvent(const GattCharacteristicReadCBParams *params) { |
rgrover1 | 714:a6130aaa0fd9 | 108 | if (onDataRead.hasCallbacksAttached()) { |
rgrover1 | 714:a6130aaa0fd9 | 109 | onDataRead.call(params); |
rgrover1 | 714:a6130aaa0fd9 | 110 | } |
rgrover1 | 714:a6130aaa0fd9 | 111 | } |
rgrover1 | 714:a6130aaa0fd9 | 112 | |
rgrover1 | 714:a6130aaa0fd9 | 113 | void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t charHandle) { |
rgrover1 | 714:a6130aaa0fd9 | 114 | switch (type) { |
rgrover1 | 714:a6130aaa0fd9 | 115 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
rgrover1 | 714:a6130aaa0fd9 | 116 | if (onUpdatesEnabled) { |
rgrover1 | 714:a6130aaa0fd9 | 117 | onUpdatesEnabled(charHandle); |
rgrover1 | 714:a6130aaa0fd9 | 118 | } |
rgrover1 | 714:a6130aaa0fd9 | 119 | break; |
rgrover1 | 714:a6130aaa0fd9 | 120 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
rgrover1 | 714:a6130aaa0fd9 | 121 | if (onUpdatesDisabled) { |
rgrover1 | 714:a6130aaa0fd9 | 122 | onUpdatesDisabled(charHandle); |
rgrover1 | 714:a6130aaa0fd9 | 123 | } |
rgrover1 | 714:a6130aaa0fd9 | 124 | break; |
rgrover1 | 714:a6130aaa0fd9 | 125 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
rgrover1 | 714:a6130aaa0fd9 | 126 | if (onConfirmationReceived) { |
rgrover1 | 714:a6130aaa0fd9 | 127 | onConfirmationReceived(charHandle); |
rgrover1 | 714:a6130aaa0fd9 | 128 | } |
rgrover1 | 714:a6130aaa0fd9 | 129 | break; |
rgrover1 | 714:a6130aaa0fd9 | 130 | default: |
rgrover1 | 714:a6130aaa0fd9 | 131 | break; |
rgrover1 | 714:a6130aaa0fd9 | 132 | } |
rgrover1 | 714:a6130aaa0fd9 | 133 | } |
rgrover1 | 714:a6130aaa0fd9 | 134 | |
rgrover1 | 714:a6130aaa0fd9 | 135 | void handleDataSentEvent(unsigned count) { |
rgrover1 | 714:a6130aaa0fd9 | 136 | if (onDataSent.hasCallbacksAttached()) { |
rgrover1 | 714:a6130aaa0fd9 | 137 | onDataSent.call(count); |
rgrover1 | 714:a6130aaa0fd9 | 138 | } |
rgrover1 | 714:a6130aaa0fd9 | 139 | } |
rgrover1 | 714:a6130aaa0fd9 | 140 | |
rgrover1 | 714:a6130aaa0fd9 | 141 | protected: |
rgrover1 | 714:a6130aaa0fd9 | 142 | uint8_t serviceCount; |
rgrover1 | 714:a6130aaa0fd9 | 143 | uint8_t characteristicCount; |
rgrover1 | 714:a6130aaa0fd9 | 144 | |
rgrover1 | 714:a6130aaa0fd9 | 145 | private: |
rgrover1 | 714:a6130aaa0fd9 | 146 | CallChainOfFunctionPointersWithContext<unsigned> onDataSent; |
rgrover1 | 714:a6130aaa0fd9 | 147 | CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten; |
rgrover1 | 714:a6130aaa0fd9 | 148 | CallChainOfFunctionPointersWithContext<const GattCharacteristicReadCBParams *> onDataRead; |
rgrover1 | 714:a6130aaa0fd9 | 149 | EventCallback_t onUpdatesEnabled; |
rgrover1 | 714:a6130aaa0fd9 | 150 | EventCallback_t onUpdatesDisabled; |
rgrover1 | 714:a6130aaa0fd9 | 151 | EventCallback_t onConfirmationReceived; |
rgrover1 | 714:a6130aaa0fd9 | 152 | |
rgrover1 | 714:a6130aaa0fd9 | 153 | private: |
rgrover1 | 714:a6130aaa0fd9 | 154 | /* disallow copy and assignment */ |
rgrover1 | 714:a6130aaa0fd9 | 155 | GattServer(const GattServer &); |
rgrover1 | 714:a6130aaa0fd9 | 156 | GattServer& operator=(const GattServer &); |
rgrover1 | 714:a6130aaa0fd9 | 157 | }; |
rgrover1 | 714:a6130aaa0fd9 | 158 | |
rgrover1 | 714:a6130aaa0fd9 | 159 | #endif // ifndef __GATT_SERVER_H__ |