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