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 BlueNRGGattClient.cpp
iv123 0:4946262d6030 19 * @author STMicroelectronics
iv123 0:4946262d6030 20 * @brief Header file for BLE_API GattClient 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 2015 STMicroelectronics</center></h2>
iv123 0:4946262d6030 32 */
iv123 0:4946262d6030 33
iv123 0:4946262d6030 34 #ifndef __BLUENRG_GATT_CLIENT_H__
iv123 0:4946262d6030 35 #define __BLUENRG_GATT_CLIENT_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/GattClient.h"
iv123 0:4946262d6030 45 #include "ble/DiscoveredService.h"
iv123 0:4946262d6030 46 #include "ble/CharacteristicDescriptorDiscovery.h"
iv123 0:4946262d6030 47 #include "BlueNRGDiscoveredCharacteristic.h"
iv123 0:4946262d6030 48
iv123 0:4946262d6030 49 using namespace std;
iv123 0:4946262d6030 50
iv123 0:4946262d6030 51 #define BLE_TOTAL_DISCOVERED_SERVICES 10
iv123 0:4946262d6030 52 #define BLE_TOTAL_DISCOVERED_CHARS 10
iv123 0:4946262d6030 53
iv123 0:4946262d6030 54 class BlueNRGGattClient : public GattClient
iv123 0:4946262d6030 55 {
iv123 0:4946262d6030 56 public:
iv123 0:4946262d6030 57 static BlueNRGGattClient &getInstance() {
iv123 0:4946262d6030 58 static BlueNRGGattClient m_instance;
iv123 0:4946262d6030 59 return m_instance;
iv123 0:4946262d6030 60 }
iv123 0:4946262d6030 61
iv123 0:4946262d6030 62 enum {
iv123 0:4946262d6030 63 GATT_IDLE,
iv123 0:4946262d6030 64 GATT_SERVICE_DISCOVERY,
iv123 0:4946262d6030 65 GATT_CHAR_DESC_DISCOVERY,
iv123 0:4946262d6030 66 //GATT_CHARS_DISCOVERY_COMPLETE,
iv123 0:4946262d6030 67 //GATT_DISCOVERY_TERMINATED,
iv123 0:4946262d6030 68 GATT_READ_CHAR,
iv123 0:4946262d6030 69 GATT_WRITE_CHAR
iv123 0:4946262d6030 70 };
iv123 0:4946262d6030 71
iv123 0:4946262d6030 72 /* Functions that must be implemented from GattClient */
iv123 0:4946262d6030 73 virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 74 ServiceDiscovery::ServiceCallback_t sc = NULL,
iv123 0:4946262d6030 75 ServiceDiscovery::CharacteristicCallback_t cc = NULL,
iv123 0:4946262d6030 76 const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
iv123 0:4946262d6030 77 const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
iv123 0:4946262d6030 78
iv123 0:4946262d6030 79 virtual ble_error_t discoverServices(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 80 ServiceDiscovery::ServiceCallback_t callback,
iv123 0:4946262d6030 81 const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
iv123 0:4946262d6030 82
iv123 0:4946262d6030 83 virtual ble_error_t discoverServices(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 84 ServiceDiscovery::ServiceCallback_t callback,
iv123 0:4946262d6030 85 GattAttribute::Handle_t startHandle,
iv123 0:4946262d6030 86 GattAttribute::Handle_t endHandle);
iv123 0:4946262d6030 87
iv123 0:4946262d6030 88 virtual bool isServiceDiscoveryActive(void) const;
iv123 0:4946262d6030 89 virtual void terminateServiceDiscovery(void);
iv123 0:4946262d6030 90 virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
iv123 0:4946262d6030 91 terminationCallback = callback;
iv123 0:4946262d6030 92 }
iv123 0:4946262d6030 93 virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const;
iv123 0:4946262d6030 94 virtual ble_error_t write(GattClient::WriteOp_t cmd,
iv123 0:4946262d6030 95 Gap::Handle_t connHandle,
iv123 0:4946262d6030 96 GattAttribute::Handle_t attributeHandle,
iv123 0:4946262d6030 97 size_t length,
iv123 0:4946262d6030 98 const uint8_t *value) const;
iv123 0:4946262d6030 99 virtual ble_error_t discoverCharacteristicDescriptors(
iv123 0:4946262d6030 100 const DiscoveredCharacteristic& characteristic,
iv123 0:4946262d6030 101 const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
iv123 0:4946262d6030 102 const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback);
iv123 0:4946262d6030 103
iv123 0:4946262d6030 104 virtual ble_error_t reset(void);
iv123 0:4946262d6030 105
iv123 0:4946262d6030 106 void gattProcedureCompleteCB(Gap::Handle_t connectionHandle, uint8_t error_code);
iv123 0:4946262d6030 107
iv123 0:4946262d6030 108 void primaryServicesCB(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 109 uint8_t event_data_length,
iv123 0:4946262d6030 110 uint8_t attribute_data_length,
iv123 0:4946262d6030 111 uint8_t *attribute_data_list);
iv123 0:4946262d6030 112
iv123 0:4946262d6030 113 void primaryServiceCB(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 114 uint8_t event_data_length,
iv123 0:4946262d6030 115 uint8_t *handles_info_list);
iv123 0:4946262d6030 116
iv123 0:4946262d6030 117 ble_error_t findServiceChars(Gap::Handle_t connectionHandle);
iv123 0:4946262d6030 118
iv123 0:4946262d6030 119 void serviceCharsCB(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 120 uint8_t event_data_length,
iv123 0:4946262d6030 121 uint8_t handle_value_pair_length,
iv123 0:4946262d6030 122 uint8_t *handle_value_pair);
iv123 0:4946262d6030 123
iv123 0:4946262d6030 124 void serviceCharByUUIDCB(Gap::Handle_t connectionHandle,
iv123 0:4946262d6030 125 uint8_t event_data_length,
iv123 0:4946262d6030 126 uint16_t attr_handle,
iv123 0:4946262d6030 127 uint8_t *attr_value);
iv123 0:4946262d6030 128
iv123 0:4946262d6030 129 void discAllCharacDescCB(Gap::Handle_t connHandle,
iv123 0:4946262d6030 130 uint8_t event_data_length,
iv123 0:4946262d6030 131 uint8_t format,
iv123 0:4946262d6030 132 uint8_t *handle_uuid_pair);
iv123 0:4946262d6030 133
iv123 0:4946262d6030 134 void charReadCB(Gap::Handle_t connHandle,
iv123 0:4946262d6030 135 uint8_t event_data_length,
iv123 0:4946262d6030 136 uint8_t* attribute_value);
iv123 0:4946262d6030 137
iv123 0:4946262d6030 138 void charWritePrepareCB(Gap::Handle_t connHandle,
iv123 0:4946262d6030 139 uint8_t event_data_length,
iv123 0:4946262d6030 140 uint16_t attribute_handle,
iv123 0:4946262d6030 141 uint16_t offset,
iv123 0:4946262d6030 142 uint8_t *part_attr_value);
iv123 0:4946262d6030 143
iv123 0:4946262d6030 144 void charWriteExecCB(Gap::Handle_t connHandle,
iv123 0:4946262d6030 145 uint8_t event_data_length);
iv123 0:4946262d6030 146
iv123 0:4946262d6030 147 protected:
iv123 0:4946262d6030 148
iv123 0:4946262d6030 149 BlueNRGGattClient() {
iv123 0:4946262d6030 150 _currentState = GATT_IDLE;
iv123 0:4946262d6030 151 _matchingServiceUUID = BLE_UUID_UNKNOWN;
iv123 0:4946262d6030 152 _matchingCharacteristicUUIDIn = BLE_UUID_UNKNOWN;
iv123 0:4946262d6030 153 }
iv123 0:4946262d6030 154
iv123 0:4946262d6030 155 ServiceDiscovery::ServiceCallback_t serviceDiscoveryCallback;
iv123 0:4946262d6030 156 ServiceDiscovery::CharacteristicCallback_t characteristicDiscoveryCallback;
iv123 0:4946262d6030 157 ServiceDiscovery::TerminationCallback_t terminationCallback;
iv123 0:4946262d6030 158 CharacteristicDescriptorDiscovery::DiscoveryCallback_t charDescDiscoveryCallback;
iv123 0:4946262d6030 159 CharacteristicDescriptorDiscovery::TerminationCallback_t charDescTerminationCallback;
iv123 0:4946262d6030 160
iv123 0:4946262d6030 161 private:
iv123 0:4946262d6030 162
iv123 0:4946262d6030 163 BlueNRGGattClient(BlueNRGGattClient const &);
iv123 0:4946262d6030 164 void operator=(BlueNRGGattClient const &);
iv123 0:4946262d6030 165
iv123 0:4946262d6030 166 Gap::Handle_t _connectionHandle;
iv123 0:4946262d6030 167 DiscoveredService discoveredService[BLE_TOTAL_DISCOVERED_SERVICES];
iv123 0:4946262d6030 168 BlueNRGDiscoveredCharacteristic discoveredChar[BLE_TOTAL_DISCOVERED_CHARS];
iv123 0:4946262d6030 169
iv123 0:4946262d6030 170 GattReadCallbackParams readCBParams;
iv123 0:4946262d6030 171 GattWriteCallbackParams writeCBParams;
iv123 0:4946262d6030 172
iv123 0:4946262d6030 173 // The char for which the descriptor discovery has been launched
iv123 0:4946262d6030 174 DiscoveredCharacteristic _characteristic;
iv123 0:4946262d6030 175
iv123 0:4946262d6030 176 UUID _matchingServiceUUID;
iv123 0:4946262d6030 177 UUID _matchingCharacteristicUUIDIn;
iv123 0:4946262d6030 178 uint8_t _currentState;
iv123 0:4946262d6030 179 uint8_t _numServices, _servIndex;
iv123 0:4946262d6030 180 uint8_t _numChars;
iv123 0:4946262d6030 181 uint8_t _numCharDesc;
iv123 0:4946262d6030 182
iv123 0:4946262d6030 183 };
iv123 0:4946262d6030 184
iv123 0:4946262d6030 185 #endif /* __BLUENRG_GATT_CLIENT_H__ */