Maiko Matsumoto / Mbed 2 deprecated BLE_WallbotBLE_Challenge2_2

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge2 by Maiko Matsumoto

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 "mbed.h"
00021 #include "blecommon.h"
00022 #include "GattService.h"
00023 #include "GattServerEvents.h"
00024 #include "GattCharacteristicWriteCBParams.h"
00025 #include "CallChainOfFunctionPointersWithContext.h"
00026 
00027 /**************************************************************************/
00028 /*!
00029     \brief
00030     The base class used to abstract GATT Server functionality to a specific
00031     radio transceiver, SOC or BLE Stack.
00032 */
00033 /**************************************************************************/
00034 class GattServer
00035 {
00036 public:
00037     /* These functions must be defined in the sub-class */
00038     virtual ble_error_t addService(GattService &) = 0;
00039     virtual ble_error_t readValue(uint16_t handle, uint8_t buffer[], uint16_t *const lengthP) = 0;
00040     virtual ble_error_t updateValue(uint16_t, uint8_t[], uint16_t, bool localOnly = false) = 0;
00041 
00042     // ToDo: For updateValue, check the CCCD to see if the value we are
00043     // updating has the notify or indicate bits sent, and if BOTH are set
00044     // be sure to call sd_ble_gatts_hvx() twice with notify then indicate!
00045     // Strange use case, but valid and must be covered!
00046 
00047     /* Event callback handlers. */
00048     typedef void (*EventCallback_t)(uint16_t attributeHandle);
00049     typedef void (*ServerEventCallback_t)(void);                    /**< independent of any particular attribute */
00050     typedef void (*ServerEventCallbackWithCount_t)(unsigned count); /**< independent of any particular attribute */
00051     void setOnDataSent(ServerEventCallbackWithCount_t callback) {
00052         onDataSent = callback;
00053     }
00054     void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {
00055         onDataWritten.add(callback);
00056     }
00057     template <typename T>
00058     void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
00059         onDataWritten.add(objPtr, memberPtr);
00060     }
00061     void setOnUpdatesEnabled(EventCallback_t callback) {
00062         onUpdatesEnabled = callback;
00063     }
00064     void setOnUpdatesDisabled(EventCallback_t callback) {
00065         onUpdatesDisabled = callback;
00066     }
00067     void setOnConfirmationReceived(EventCallback_t callback) {
00068         onConfirmationReceived = callback;
00069     }
00070 
00071     void handleDataWrittenEvent(const GattCharacteristicWriteCBParams *params) {
00072         if (onDataWritten.hasCallbacksAttached()) {
00073             onDataWritten.call(params);
00074         }
00075     }
00076 
00077     void handleEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
00078         switch (type) {
00079             case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
00080                 if (onUpdatesEnabled) {
00081                     onUpdatesEnabled(charHandle);
00082                 }
00083                 break;
00084             case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
00085                 if (onUpdatesDisabled) {
00086                     onUpdatesDisabled(charHandle);
00087                 }
00088                 break;
00089             case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
00090                 if (onConfirmationReceived) {
00091                     onConfirmationReceived(charHandle);
00092                 }
00093                 break;
00094         }
00095     }
00096 
00097     void handleDataSentEvent(unsigned count) {
00098         if (onDataSent) {
00099             onDataSent(count);
00100         }
00101     }
00102 
00103 protected:
00104     GattServer() : serviceCount(0), characteristicCount(0), onDataSent(NULL), onDataWritten(), onUpdatesEnabled(NULL), onUpdatesDisabled(NULL), onConfirmationReceived(NULL) {
00105         /* empty */
00106     }
00107 
00108 protected:
00109     uint8_t serviceCount;
00110     uint8_t characteristicCount;
00111     uint8_t descriptorCount;
00112 
00113 private:
00114     ServerEventCallbackWithCount_t onDataSent;
00115     CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten;
00116     EventCallback_t                onUpdatesEnabled;
00117     EventCallback_t                onUpdatesDisabled;
00118     EventCallback_t                onConfirmationReceived;
00119 };
00120 
00121 #endif // ifndef __GATT_SERVER_H__