BLE_API_Tiny_BLE
Fork of BLE_API by
public/GattServer.h@129:fca6e5ba7d39, 2014-11-21 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 21 09:23:22 2014 +0000
- Revision:
- 129:fca6e5ba7d39
- Parent:
- 128:685897b70e4b
- Child:
- 134:49321f76753e
Synchronized with git rev 1af30a89
Author: Rohit Grover
GattServer:: 'descriptorCount' is no longer a member of the base class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
106:a20be740075d | 1 | /* mbed Microcontroller Library |
Rohit Grover |
106:a20be740075d | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
106:a20be740075d | 3 | * |
Rohit Grover |
106:a20be740075d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
106:a20be740075d | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
106:a20be740075d | 6 | * You may obtain a copy of the License at |
Rohit Grover |
106:a20be740075d | 7 | * |
Rohit Grover |
106:a20be740075d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
106:a20be740075d | 9 | * |
Rohit Grover |
106:a20be740075d | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
106:a20be740075d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
106:a20be740075d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
106:a20be740075d | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
106:a20be740075d | 14 | * limitations under the License. |
Rohit Grover |
106:a20be740075d | 15 | */ |
Rohit Grover |
106:a20be740075d | 16 | |
Rohit Grover |
106:a20be740075d | 17 | #ifndef __GATT_SERVER_H__ |
Rohit Grover |
106:a20be740075d | 18 | #define __GATT_SERVER_H__ |
Rohit Grover |
106:a20be740075d | 19 | |
Rohit Grover |
106:a20be740075d | 20 | #include "GattService.h" |
Rohit Grover |
106:a20be740075d | 21 | #include "GattServerEvents.h" |
Rohit Grover |
116:ca826083980e | 22 | #include "GattCharacteristicWriteCBParams.h" |
Rohit Grover |
118:620d28e7a1ba | 23 | #include "CallChainOfFunctionPointersWithContext.h" |
Rohit Grover |
106:a20be740075d | 24 | |
Rohit Grover |
106:a20be740075d | 25 | /**************************************************************************/ |
Rohit Grover |
106:a20be740075d | 26 | /*! |
Rohit Grover |
106:a20be740075d | 27 | \brief |
Rohit Grover |
106:a20be740075d | 28 | The base class used to abstract GATT Server functionality to a specific |
Rohit Grover |
106:a20be740075d | 29 | radio transceiver, SOC or BLE Stack. |
Rohit Grover |
106:a20be740075d | 30 | */ |
Rohit Grover |
106:a20be740075d | 31 | /**************************************************************************/ |
Rohit Grover |
106:a20be740075d | 32 | class GattServer |
Rohit Grover |
106:a20be740075d | 33 | { |
Rohit Grover |
106:a20be740075d | 34 | public: |
Rohit Grover |
106:a20be740075d | 35 | /* These functions must be defined in the sub-class */ |
Rohit Grover |
106:a20be740075d | 36 | virtual ble_error_t addService(GattService &) = 0; |
Rohit Grover |
106:a20be740075d | 37 | virtual ble_error_t readValue(uint16_t handle, uint8_t buffer[], uint16_t *const lengthP) = 0; |
Rohit Grover |
106:a20be740075d | 38 | virtual ble_error_t updateValue(uint16_t, uint8_t[], uint16_t, bool localOnly = false) = 0; |
Rohit Grover |
106:a20be740075d | 39 | |
Rohit Grover |
106:a20be740075d | 40 | // ToDo: For updateValue, check the CCCD to see if the value we are |
Rohit Grover |
106:a20be740075d | 41 | // updating has the notify or indicate bits sent, and if BOTH are set |
Rohit Grover |
106:a20be740075d | 42 | // be sure to call sd_ble_gatts_hvx() twice with notify then indicate! |
Rohit Grover |
106:a20be740075d | 43 | // Strange use case, but valid and must be covered! |
Rohit Grover |
106:a20be740075d | 44 | |
Rohit Grover |
106:a20be740075d | 45 | /* Event callback handlers. */ |
Rohit Grover |
106:a20be740075d | 46 | typedef void (*EventCallback_t)(uint16_t attributeHandle); |
Rohit Grover |
116:ca826083980e | 47 | typedef void (*ServerEventCallback_t)(void); /**< independent of any particular attribute */ |
Rohit Grover |
116:ca826083980e | 48 | typedef void (*ServerEventCallbackWithCount_t)(unsigned count); /**< independent of any particular attribute */ |
Rohit Grover |
116:ca826083980e | 49 | void setOnDataSent(ServerEventCallbackWithCount_t callback) { |
Rohit Grover |
106:a20be740075d | 50 | onDataSent = callback; |
Rohit Grover |
106:a20be740075d | 51 | } |
Rohit Grover |
118:620d28e7a1ba | 52 | void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) { |
Rohit Grover |
118:620d28e7a1ba | 53 | onDataWritten.add(callback); |
Rohit Grover |
118:620d28e7a1ba | 54 | } |
Rohit Grover |
118:620d28e7a1ba | 55 | template <typename T> |
Rohit Grover |
118:620d28e7a1ba | 56 | void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) { |
Rohit Grover |
118:620d28e7a1ba | 57 | onDataWritten.add(objPtr, memberPtr); |
Rohit Grover |
106:a20be740075d | 58 | } |
Rohit Grover |
106:a20be740075d | 59 | void setOnUpdatesEnabled(EventCallback_t callback) { |
Rohit Grover |
106:a20be740075d | 60 | onUpdatesEnabled = callback; |
Rohit Grover |
106:a20be740075d | 61 | } |
Rohit Grover |
106:a20be740075d | 62 | void setOnUpdatesDisabled(EventCallback_t callback) { |
Rohit Grover |
106:a20be740075d | 63 | onUpdatesDisabled = callback; |
Rohit Grover |
106:a20be740075d | 64 | } |
Rohit Grover |
106:a20be740075d | 65 | void setOnConfirmationReceived(EventCallback_t callback) { |
Rohit Grover |
106:a20be740075d | 66 | onConfirmationReceived = callback; |
Rohit Grover |
106:a20be740075d | 67 | } |
Rohit Grover |
106:a20be740075d | 68 | |
rgrover1 | 128:685897b70e4b | 69 | protected: |
rgrover1 | 128:685897b70e4b | 70 | GattServer() : serviceCount(0), characteristicCount(0), onDataSent(NULL), onDataWritten(), onUpdatesEnabled(NULL), onUpdatesDisabled(NULL), onConfirmationReceived(NULL) { |
rgrover1 | 128:685897b70e4b | 71 | /* empty */ |
rgrover1 | 128:685897b70e4b | 72 | } |
rgrover1 | 128:685897b70e4b | 73 | |
rgrover1 | 128:685897b70e4b | 74 | protected: |
Rohit Grover |
118:620d28e7a1ba | 75 | void handleDataWrittenEvent(const GattCharacteristicWriteCBParams *params) { |
Rohit Grover |
118:620d28e7a1ba | 76 | if (onDataWritten.hasCallbacksAttached()) { |
Rohit Grover |
118:620d28e7a1ba | 77 | onDataWritten.call(params); |
Rohit Grover |
116:ca826083980e | 78 | } |
Rohit Grover |
116:ca826083980e | 79 | } |
Rohit Grover |
116:ca826083980e | 80 | |
Rohit Grover |
106:a20be740075d | 81 | void handleEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) { |
Rohit Grover |
106:a20be740075d | 82 | switch (type) { |
Rohit Grover |
106:a20be740075d | 83 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
Rohit Grover |
106:a20be740075d | 84 | if (onUpdatesEnabled) { |
Rohit Grover |
106:a20be740075d | 85 | onUpdatesEnabled(charHandle); |
Rohit Grover |
106:a20be740075d | 86 | } |
Rohit Grover |
106:a20be740075d | 87 | break; |
Rohit Grover |
106:a20be740075d | 88 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
Rohit Grover |
106:a20be740075d | 89 | if (onUpdatesDisabled) { |
Rohit Grover |
106:a20be740075d | 90 | onUpdatesDisabled(charHandle); |
Rohit Grover |
106:a20be740075d | 91 | } |
Rohit Grover |
106:a20be740075d | 92 | break; |
Rohit Grover |
106:a20be740075d | 93 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
Rohit Grover |
106:a20be740075d | 94 | if (onConfirmationReceived) { |
Rohit Grover |
106:a20be740075d | 95 | onConfirmationReceived(charHandle); |
Rohit Grover |
106:a20be740075d | 96 | } |
Rohit Grover |
106:a20be740075d | 97 | break; |
Rohit Grover |
106:a20be740075d | 98 | } |
Rohit Grover |
106:a20be740075d | 99 | } |
Rohit Grover |
106:a20be740075d | 100 | |
Rohit Grover |
116:ca826083980e | 101 | void handleDataSentEvent(unsigned count) { |
Rohit Grover |
116:ca826083980e | 102 | if (onDataSent) { |
Rohit Grover |
116:ca826083980e | 103 | onDataSent(count); |
Rohit Grover |
106:a20be740075d | 104 | } |
Rohit Grover |
106:a20be740075d | 105 | } |
Rohit Grover |
106:a20be740075d | 106 | |
Rohit Grover |
106:a20be740075d | 107 | protected: |
Rohit Grover |
106:a20be740075d | 108 | uint8_t serviceCount; |
Rohit Grover |
106:a20be740075d | 109 | uint8_t characteristicCount; |
Rohit Grover |
106:a20be740075d | 110 | |
Rohit Grover |
106:a20be740075d | 111 | private: |
Rohit Grover |
116:ca826083980e | 112 | ServerEventCallbackWithCount_t onDataSent; |
Rohit Grover |
118:620d28e7a1ba | 113 | CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten; |
Rohit Grover |
116:ca826083980e | 114 | EventCallback_t onUpdatesEnabled; |
Rohit Grover |
116:ca826083980e | 115 | EventCallback_t onUpdatesDisabled; |
Rohit Grover |
116:ca826083980e | 116 | EventCallback_t onConfirmationReceived; |
Rohit Grover |
106:a20be740075d | 117 | }; |
Rohit Grover |
106:a20be740075d | 118 | |
rgrover1 | 126:fdebe4d5d62f | 119 | #endif // ifndef __GATT_SERVER_H__ |