Fork of ble-x-nucleo-idb0xa1 with changes required by BleStarMbed
bluenrg/BlueNRGGattServer.h@0:ac0b0725c6fa, 2018-02-20 (annotated)
- Committer:
- lorevee
- Date:
- Tue Feb 20 11:07:16 2018 +0000
- Revision:
- 0:ac0b0725c6fa
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lorevee | 0:ac0b0725c6fa | 1 | /* mbed Microcontroller Library |
lorevee | 0:ac0b0725c6fa | 2 | * Copyright (c) 2006-2013 ARM Limited |
lorevee | 0:ac0b0725c6fa | 3 | * |
lorevee | 0:ac0b0725c6fa | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lorevee | 0:ac0b0725c6fa | 5 | * you may not use this file except in compliance with the License. |
lorevee | 0:ac0b0725c6fa | 6 | * You may obtain a copy of the License at |
lorevee | 0:ac0b0725c6fa | 7 | * |
lorevee | 0:ac0b0725c6fa | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lorevee | 0:ac0b0725c6fa | 9 | * |
lorevee | 0:ac0b0725c6fa | 10 | * Unless required by applicable law or agreed to in writing, software |
lorevee | 0:ac0b0725c6fa | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lorevee | 0:ac0b0725c6fa | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lorevee | 0:ac0b0725c6fa | 13 | * See the License for the specific language governing permissions and |
lorevee | 0:ac0b0725c6fa | 14 | * limitations under the License. |
lorevee | 0:ac0b0725c6fa | 15 | */ |
lorevee | 0:ac0b0725c6fa | 16 | /** |
lorevee | 0:ac0b0725c6fa | 17 | ****************************************************************************** |
lorevee | 0:ac0b0725c6fa | 18 | * @file BlueNRGGattServer.cpp |
lorevee | 0:ac0b0725c6fa | 19 | * @author STMicroelectronics |
lorevee | 0:ac0b0725c6fa | 20 | * @brief Header file for BLE_API GattServer Class |
lorevee | 0:ac0b0725c6fa | 21 | ****************************************************************************** |
lorevee | 0:ac0b0725c6fa | 22 | * @copy |
lorevee | 0:ac0b0725c6fa | 23 | * |
lorevee | 0:ac0b0725c6fa | 24 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
lorevee | 0:ac0b0725c6fa | 25 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
lorevee | 0:ac0b0725c6fa | 26 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
lorevee | 0:ac0b0725c6fa | 27 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
lorevee | 0:ac0b0725c6fa | 28 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
lorevee | 0:ac0b0725c6fa | 29 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
lorevee | 0:ac0b0725c6fa | 30 | * |
lorevee | 0:ac0b0725c6fa | 31 | * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
lorevee | 0:ac0b0725c6fa | 32 | */ |
lorevee | 0:ac0b0725c6fa | 33 | |
lorevee | 0:ac0b0725c6fa | 34 | #ifndef __BLUENRG_GATT_SERVER_H__ |
lorevee | 0:ac0b0725c6fa | 35 | #define __BLUENRG_GATT_SERVER_H__ |
lorevee | 0:ac0b0725c6fa | 36 | |
lorevee | 0:ac0b0725c6fa | 37 | #ifdef YOTTA_CFG_MBED_OS |
lorevee | 0:ac0b0725c6fa | 38 | #include "mbed-drivers/mbed.h" |
lorevee | 0:ac0b0725c6fa | 39 | #else |
lorevee | 0:ac0b0725c6fa | 40 | #include "mbed.h" |
lorevee | 0:ac0b0725c6fa | 41 | #endif |
lorevee | 0:ac0b0725c6fa | 42 | #include "ble/blecommon.h" |
lorevee | 0:ac0b0725c6fa | 43 | #include "btle.h" |
lorevee | 0:ac0b0725c6fa | 44 | #include "ble/GattService.h" |
lorevee | 0:ac0b0725c6fa | 45 | #include "ble/GattServer.h" |
lorevee | 0:ac0b0725c6fa | 46 | #include <vector> |
lorevee | 0:ac0b0725c6fa | 47 | #include <map> |
lorevee | 0:ac0b0725c6fa | 48 | |
lorevee | 0:ac0b0725c6fa | 49 | #define BLE_TOTAL_CHARACTERISTICS 10 |
lorevee | 0:ac0b0725c6fa | 50 | |
lorevee | 0:ac0b0725c6fa | 51 | using namespace std; |
lorevee | 0:ac0b0725c6fa | 52 | |
lorevee | 0:ac0b0725c6fa | 53 | class BlueNRGGattServer : public GattServer |
lorevee | 0:ac0b0725c6fa | 54 | { |
lorevee | 0:ac0b0725c6fa | 55 | public: |
lorevee | 0:ac0b0725c6fa | 56 | static BlueNRGGattServer &getInstance() { |
lorevee | 0:ac0b0725c6fa | 57 | static BlueNRGGattServer m_instance; |
lorevee | 0:ac0b0725c6fa | 58 | return m_instance; |
lorevee | 0:ac0b0725c6fa | 59 | } |
lorevee | 0:ac0b0725c6fa | 60 | |
lorevee | 0:ac0b0725c6fa | 61 | enum HandleEnum_t { |
lorevee | 0:ac0b0725c6fa | 62 | CHAR_HANDLE = 0, |
lorevee | 0:ac0b0725c6fa | 63 | CHAR_VALUE_HANDLE, |
lorevee | 0:ac0b0725c6fa | 64 | CHAR_DESC_HANDLE |
lorevee | 0:ac0b0725c6fa | 65 | }; |
lorevee | 0:ac0b0725c6fa | 66 | |
lorevee | 0:ac0b0725c6fa | 67 | /* Functions that must be implemented from GattServer */ |
lorevee | 0:ac0b0725c6fa | 68 | virtual ble_error_t addService(GattService &); |
lorevee | 0:ac0b0725c6fa | 69 | virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); |
lorevee | 0:ac0b0725c6fa | 70 | virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); |
lorevee | 0:ac0b0725c6fa | 71 | virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); |
lorevee | 0:ac0b0725c6fa | 72 | virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); |
lorevee | 0:ac0b0725c6fa | 73 | virtual ble_error_t initializeGATTDatabase(void); |
lorevee | 0:ac0b0725c6fa | 74 | |
lorevee | 0:ac0b0725c6fa | 75 | virtual bool isOnDataReadAvailable() const { |
lorevee | 0:ac0b0725c6fa | 76 | return true; |
lorevee | 0:ac0b0725c6fa | 77 | } |
lorevee | 0:ac0b0725c6fa | 78 | |
lorevee | 0:ac0b0725c6fa | 79 | virtual ble_error_t reset(void); |
lorevee | 0:ac0b0725c6fa | 80 | |
lorevee | 0:ac0b0725c6fa | 81 | /* BlueNRG Functions */ |
lorevee | 0:ac0b0725c6fa | 82 | void eventCallback(void); |
lorevee | 0:ac0b0725c6fa | 83 | //void hwCallback(void *pckt); |
lorevee | 0:ac0b0725c6fa | 84 | ble_error_t Read_Request_CB(uint16_t attributeHandle); |
lorevee | 0:ac0b0725c6fa | 85 | uint8_t Write_Request_CB( |
lorevee | 0:ac0b0725c6fa | 86 | uint16_t connection_handle, uint16_t attr_handle, |
lorevee | 0:ac0b0725c6fa | 87 | uint8_t data_length, const uint8_t* data |
lorevee | 0:ac0b0725c6fa | 88 | ); |
lorevee | 0:ac0b0725c6fa | 89 | GattCharacteristic* getCharacteristicFromHandle(uint16_t charHandle); |
lorevee | 0:ac0b0725c6fa | 90 | void HCIDataWrittenEvent(const GattWriteCallbackParams *params); |
lorevee | 0:ac0b0725c6fa | 91 | void HCIDataReadEvent(const GattReadCallbackParams *params); |
lorevee | 0:ac0b0725c6fa | 92 | void HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle); |
lorevee | 0:ac0b0725c6fa | 93 | void HCIDataSentEvent(unsigned count); |
lorevee | 0:ac0b0725c6fa | 94 | |
lorevee | 0:ac0b0725c6fa | 95 | private: |
lorevee | 0:ac0b0725c6fa | 96 | |
lorevee | 0:ac0b0725c6fa | 97 | // compute the number of attribute record needed by a service |
lorevee | 0:ac0b0725c6fa | 98 | static uint16_t computeAttributesRecord(GattService& service); |
lorevee | 0:ac0b0725c6fa | 99 | |
lorevee | 0:ac0b0725c6fa | 100 | |
lorevee | 0:ac0b0725c6fa | 101 | static const int MAX_SERVICE_COUNT = 10; |
lorevee | 0:ac0b0725c6fa | 102 | uint8_t serviceCount; |
lorevee | 0:ac0b0725c6fa | 103 | uint8_t characteristicCount; |
lorevee | 0:ac0b0725c6fa | 104 | uint16_t servHandle, charHandle; |
lorevee | 0:ac0b0725c6fa | 105 | |
lorevee | 0:ac0b0725c6fa | 106 | std::map<uint16_t, uint16_t> bleCharHandleMap; // 1st argument is characteristic, 2nd argument is service |
lorevee | 0:ac0b0725c6fa | 107 | GattCharacteristic *p_characteristics[BLE_TOTAL_CHARACTERISTICS]; |
lorevee | 0:ac0b0725c6fa | 108 | uint16_t bleCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; |
lorevee | 0:ac0b0725c6fa | 109 | |
lorevee | 0:ac0b0725c6fa | 110 | BlueNRGGattServer() { |
lorevee | 0:ac0b0725c6fa | 111 | serviceCount = 0; |
lorevee | 0:ac0b0725c6fa | 112 | characteristicCount = 0; |
lorevee | 0:ac0b0725c6fa | 113 | }; |
lorevee | 0:ac0b0725c6fa | 114 | |
lorevee | 0:ac0b0725c6fa | 115 | BlueNRGGattServer(BlueNRGGattServer const &); |
lorevee | 0:ac0b0725c6fa | 116 | void operator=(BlueNRGGattServer const &); |
lorevee | 0:ac0b0725c6fa | 117 | |
lorevee | 0:ac0b0725c6fa | 118 | static const int CHAR_DESC_TYPE_16_BIT=0x01; |
lorevee | 0:ac0b0725c6fa | 119 | static const int CHAR_DESC_TYPE_128_BIT=0x02; |
lorevee | 0:ac0b0725c6fa | 120 | static const int CHAR_DESC_SECURITY_PERMISSION=0x00; |
lorevee | 0:ac0b0725c6fa | 121 | static const int CHAR_DESC_ACCESS_PERMISSION=0x03; |
lorevee | 0:ac0b0725c6fa | 122 | static const int CHAR_ATTRIBUTE_LEN_IS_FIXED=0x00; |
lorevee | 0:ac0b0725c6fa | 123 | }; |
lorevee | 0:ac0b0725c6fa | 124 | |
lorevee | 0:ac0b0725c6fa | 125 | #endif |