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