Minor temporary patch to allow DFU packet callback

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GattServer.h Source File

GattServer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __GATT_SERVER_H__
00018 #define __GATT_SERVER_H__
00019 
00020 #include "Gap.h"
00021 #include "GattService.h"
00022 #include "GattAttribute.h"
00023 #include "GattServerEvents.h"
00024 #include "GattCharacteristicCallbackParams.h"
00025 #include "CallChainOfFunctionPointersWithContext.h"
00026 
00027 class GattServer {
00028 public:
00029     /* Event callback handlers. */
00030     typedef void (*EventCallback_t)(GattAttribute::Handle_t attributeHandle);
00031     typedef void (*ServerEventCallback_t)(void);                    /**< independent of any particular attribute */
00032 
00033 protected:
00034     GattServer() :
00035         serviceCount(0),
00036         characteristicCount(0),
00037         onDataSent(),
00038         onDataWritten(),
00039         onDataRead(),
00040         onUpdatesEnabled(NULL),
00041         onUpdatesDisabled(NULL),
00042         onConfirmationReceived(NULL) {
00043         /* empty */
00044     }
00045 
00046     friend class BLEDevice;
00047 private:
00048     /* These functions must be defined in the sub-class */
00049     virtual ble_error_t addService(GattService &)                                                               = 0;
00050     virtual ble_error_t readValue(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0;
00051     virtual ble_error_t readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0;
00052     virtual ble_error_t updateValue(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0;
00053     virtual ble_error_t updateValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0;
00054     virtual ble_error_t initializeGATTDatabase(void)                                                            = 0;
00055 
00056     // ToDo: For updateValue, check the CCCD to see if the value we are
00057     // updating has the notify or indicate bits sent, and if BOTH are set
00058     // be sure to call sd_ble_gatts_hvx() twice with notify then indicate!
00059     // Strange use case, but valid and must be covered!
00060 
00061     void setOnDataSent(void (*callback)(unsigned count)) {onDataSent.add(callback);}
00062     template <typename T>
00063     void setOnDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
00064         onDataSent.add(objPtr, memberPtr);
00065     }
00066     void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {onDataWritten.add(callback);}
00067     template <typename T>
00068     void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
00069         onDataWritten.add(objPtr, memberPtr);
00070     }
00071 
00072     /**
00073      * A virtual function to allow underlying stacks to indicate if they support
00074      * onDataRead(). It should be overridden to return true as applicable.
00075      */
00076     virtual bool isOnDataReadAvailable() const {
00077         return false;
00078     }
00079     ble_error_t setOnDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {
00080         if (!isOnDataReadAvailable()) {
00081             return BLE_ERROR_NOT_IMPLEMENTED;
00082         }
00083 
00084         onDataRead.add(callback);
00085         return BLE_ERROR_NONE;
00086     }
00087     template <typename T>
00088     ble_error_t setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
00089         if (!isOnDataReadAvailable()) {
00090             return BLE_ERROR_NOT_IMPLEMENTED;
00091         }
00092 
00093         onDataRead.add(objPtr, memberPtr);
00094         return BLE_ERROR_NONE;
00095     }
00096     void setOnUpdatesEnabled(EventCallback_t callback)       {onUpdatesEnabled       = callback;}
00097     void setOnUpdatesDisabled(EventCallback_t callback)      {onUpdatesDisabled      = callback;}
00098     void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;}
00099 
00100 protected:
00101     void handleDataWrittenEvent(const GattCharacteristicWriteCBParams *params) {
00102         if (onDataWritten.hasCallbacksAttached()) {
00103             onDataWritten.call(params);
00104         }
00105     }
00106 
00107     void handleDataReadEvent(const GattCharacteristicReadCBParams *params) {
00108         if (onDataRead.hasCallbacksAttached()) {
00109             onDataRead.call(params);
00110         }
00111     }
00112 
00113     void handleEvent(GattServerEvents::gattEvent_e  type, GattAttribute::Handle_t charHandle) {
00114         switch (type) {
00115             case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
00116                 if (onUpdatesEnabled) {
00117                     onUpdatesEnabled(charHandle);
00118                 }
00119                 break;
00120             case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
00121                 if (onUpdatesDisabled) {
00122                     onUpdatesDisabled(charHandle);
00123                 }
00124                 break;
00125             case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
00126                 if (onConfirmationReceived) {
00127                     onConfirmationReceived(charHandle);
00128                 }
00129                 break;
00130             default:
00131                 break;
00132         }
00133     }
00134 
00135     void handleDataSentEvent(unsigned count) {
00136         if (onDataSent.hasCallbacksAttached()) {
00137             onDataSent.call(count);
00138         }
00139     }
00140 
00141 protected:
00142     uint8_t serviceCount;
00143     uint8_t characteristicCount;
00144 
00145 private:
00146     CallChainOfFunctionPointersWithContext<unsigned>                                 onDataSent;
00147     CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *>  onDataWritten;
00148     CallChainOfFunctionPointersWithContext<const GattCharacteristicReadCBParams *>   onDataRead;
00149     EventCallback_t                                                                 onUpdatesEnabled;
00150     EventCallback_t                                                                 onUpdatesDisabled;
00151     EventCallback_t                                                                 onConfirmationReceived;
00152 
00153 private:
00154     /* disallow copy and assignment */
00155     GattServer(const GattServer &);
00156     GattServer& operator=(const GattServer &);
00157 };
00158 
00159 #endif // ifndef __GATT_SERVER_H__