BLE_API_Tiny_BLE

Dependents:   CSSE4011_BLE_IMU

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 21 09:23:24 2014 +0000
Revision:
143:8bdf577b1598
Parent:
140:407d134c179d
Child:
258:85de85adfac7
Synchronized with git rev d0dbe29f
Author: Rohit Grover
minor white space diffs

Who changed what in which revision?

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