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