ver:init

Committer:
iv123
Date:
Sun Jun 18 16:11:03 2017 +0000
Revision:
0:4946262d6030
Initial commit

Who changed what in which revision?

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