0611

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge2 by Maiko Matsumoto

Committer:
mmmmmmmmma
Date:
Mon Jun 11 04:55:58 2018 +0000
Revision:
4:53eceb750885
Parent:
0:76dfa9657d9d
0611

Who changed what in which revision?

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