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