テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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