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