Microbug / BLE_API_FOTA

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)(uint16_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 updateValue(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0;
00052     virtual ble_error_t initializeGATTDatabase(void)                                                            = 0;
00053 
00054     // ToDo: For updateValue, check the CCCD to see if the value we are
00055     // updating has the notify or indicate bits sent, and if BOTH are set
00056     // be sure to call sd_ble_gatts_hvx() twice with notify then indicate!
00057     // Strange use case, but valid and must be covered!
00058 
00059     void setOnDataSent(void (*callback)(unsigned count)) {onDataSent.add(callback);}
00060     template <typename T>
00061     void setOnDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
00062         onDataSent.add(objPtr, memberPtr);
00063     }
00064     void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {onDataWritten.add(callback);}
00065     template <typename T>
00066     void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
00067         onDataWritten.add(objPtr, memberPtr);
00068     }
00069 
00070     /**
00071      * A virtual function to allow underlying stacks to indicate if they support
00072      * onDataRead(). It should be overridden to return true as applicable.
00073      */
00074     virtual bool isOnDataReadAvailable() const {
00075         return false;
00076     }
00077     ble_error_t setOnDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {
00078         if (!isOnDataReadAvailable()) {
00079             return BLE_ERROR_NOT_IMPLEMENTED;
00080         }
00081 
00082         onDataRead.add(callback);
00083         return BLE_ERROR_NONE;
00084     }
00085     template <typename T>
00086     ble_error_t setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
00087         if (!isOnDataReadAvailable()) {
00088             return BLE_ERROR_NOT_IMPLEMENTED;
00089         }
00090 
00091         onDataRead.add(objPtr, memberPtr);
00092         return BLE_ERROR_NONE;
00093     }
00094     void setOnUpdatesEnabled(EventCallback_t callback) {onUpdatesEnabled = callback;}
00095     void setOnUpdatesDisabled(EventCallback_t callback) {onUpdatesDisabled = callback;}
00096     void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;}
00097 
00098 protected:
00099     void handleDataWrittenEvent(const GattCharacteristicWriteCBParams *params) {
00100         if (onDataWritten.hasCallbacksAttached()) {
00101             onDataWritten.call(params);
00102         }
00103     }
00104 
00105     void handleDataReadEvent(const GattCharacteristicReadCBParams *params) {
00106         if (onDataRead.hasCallbacksAttached()) {
00107             onDataRead.call(params);
00108         }
00109     }
00110 
00111     void handleEvent(GattServerEvents::gattEvent_e  type, uint16_t charHandle) {
00112         switch (type) {
00113             case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
00114                 if (onUpdatesEnabled) {
00115                     onUpdatesEnabled(charHandle);
00116                 }
00117                 break;
00118             case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
00119                 if (onUpdatesDisabled) {
00120                     onUpdatesDisabled(charHandle);
00121                 }
00122                 break;
00123             case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
00124                 if (onConfirmationReceived) {
00125                     onConfirmationReceived(charHandle);
00126                 }
00127                 break;
00128             default:
00129                 break;
00130         }
00131     }
00132 
00133     void handleDataSentEvent(unsigned count) {
00134         if (onDataSent.hasCallbacksAttached()) {
00135             onDataSent.call(count);
00136         }
00137     }
00138 
00139 protected:
00140     uint8_t serviceCount;
00141     uint8_t characteristicCount;
00142 
00143 private:
00144     CallChainOfFunctionPointersWithContext<unsigned>  onDataSent;
00145     CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *>  onDataWritten;
00146     CallChainOfFunctionPointersWithContext<const GattCharacteristicReadCBParams *>  onDataRead;
00147     EventCallback_t onUpdatesEnabled;
00148     EventCallback_t onUpdatesDisabled;
00149     EventCallback_t onConfirmationReceived;
00150 
00151 private:
00152     /* disallow copy and assignment */
00153     GattServer(const GattServer &);
00154     GattServer& operator=(const GattServer &);
00155 };
00156 
00157 #endif // ifndef __GATT_SERVER_H__