Fork of ble-x-nucleo-idb0xa1 with changes required by BleStarMbed

Dependents:   ble-star-mbed

Committer:
lorevee
Date:
Tue Feb 20 11:07:16 2018 +0000
Revision:
0:ac0b0725c6fa
Initial commit

Who changed what in which revision?

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