BLE_API wrapper library for STMicroelectronics' BlueNRG Bluetooth Low Energy expansion board shield (Component)

Dependents:   Nucleo_Zumo_BLE_IDB04A1 contest_IOT5 contest_IOT6 contest_IOT_10 ... more

Fork of X_NUCLEO_IDB0XA1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlueNRGGattClient.h Source File

BlueNRGGattClient.h

00001 /* mbed Microcontroller Library
00002 * Copyright (c) 2006-2013 ARM Limited
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 /**
00017   ******************************************************************************
00018   * @file    BlueNRGGattClient.cpp 
00019   * @author  STMicroelectronics
00020   * @brief   Header file for BLE_API GattClient Class
00021   ******************************************************************************
00022   * @copy
00023   *
00024   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00025   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00026   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00027   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00028   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00029   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00030   *
00031   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
00032   */
00033  
00034 #ifndef __BLUENRG_GATT_CLIENT_H__
00035 #define __BLUENRG_GATT_CLIENT_H__
00036 
00037 #ifdef YOTTA_CFG_MBED_OS
00038     #include "mbed-drivers/mbed.h"
00039 #else
00040     #include "mbed.h"
00041 #endif 
00042 #include "ble/blecommon.h"
00043 #include "btle.h"
00044 #include "ble/GattClient.h"
00045 #include "ble/DiscoveredService.h"
00046 #include "ble/CharacteristicDescriptorDiscovery.h"
00047 #include "BlueNRGDiscoveredCharacteristic.h"
00048 
00049 using namespace std;
00050 
00051 #define BLE_TOTAL_DISCOVERED_SERVICES 10
00052 #define BLE_TOTAL_DISCOVERED_CHARS 10
00053 
00054 class BlueNRGGattClient : public GattClient
00055 {
00056 public:
00057     static BlueNRGGattClient &getInstance() {
00058         static BlueNRGGattClient m_instance;
00059         return m_instance;
00060     }
00061     
00062     enum {
00063       GATT_IDLE,
00064       GATT_SERVICE_DISCOVERY,
00065       GATT_CHAR_DESC_DISCOVERY,
00066       //GATT_CHARS_DISCOVERY_COMPLETE,
00067       //GATT_DISCOVERY_TERMINATED,
00068       GATT_READ_CHAR,
00069       GATT_WRITE_CHAR
00070     };
00071     
00072     /* Functions that must be implemented from GattClient */
00073     virtual ble_error_t launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
00074                                                ServiceDiscovery::ServiceCallback_t         sc                           = NULL,
00075                                                ServiceDiscovery::CharacteristicCallback_t  cc                           = NULL,
00076                                                const UUID                                 &matchingServiceUUID          = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
00077                                                const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00078 
00079     virtual ble_error_t discoverServices(Gap::Handle_t                        connectionHandle,
00080                                          ServiceDiscovery::ServiceCallback_t  callback,
00081                                          const UUID                          &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00082 
00083     virtual ble_error_t discoverServices(Gap::Handle_t                        connectionHandle,
00084                                          ServiceDiscovery::ServiceCallback_t  callback,
00085                                          GattAttribute::Handle_t              startHandle,
00086                                          GattAttribute::Handle_t              endHandle);
00087 
00088     virtual bool isServiceDiscoveryActive(void) const;
00089     virtual void terminateServiceDiscovery(void);
00090     virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
00091       terminationCallback = callback;
00092     }
00093     virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const;
00094     virtual ble_error_t write(GattClient::WriteOp_t    cmd,
00095                               Gap::Handle_t            connHandle,
00096                               GattAttribute::Handle_t  attributeHandle,
00097                               size_t                   length,
00098                               const uint8_t           *value) const;
00099     virtual ble_error_t discoverCharacteristicDescriptors(
00100         const DiscoveredCharacteristic& characteristic,
00101         const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
00102         const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback);
00103 
00104     virtual ble_error_t reset(void);
00105 
00106     void gattProcedureCompleteCB(Gap::Handle_t connectionHandle, uint8_t error_code);
00107 
00108     void primaryServicesCB(Gap::Handle_t connectionHandle,
00109                            uint8_t event_data_length,
00110                            uint8_t attribute_data_length,
00111                            uint8_t *attribute_data_list);
00112     
00113     void primaryServiceCB(Gap::Handle_t connectionHandle,
00114                           uint8_t event_data_length,
00115                           uint8_t *handles_info_list);
00116     
00117     ble_error_t findServiceChars(Gap::Handle_t connectionHandle);
00118     
00119     void serviceCharsCB(Gap::Handle_t connectionHandle,
00120                         uint8_t event_data_length,
00121                         uint8_t handle_value_pair_length,
00122                         uint8_t *handle_value_pair);
00123     
00124     void  serviceCharByUUIDCB(Gap::Handle_t connectionHandle,
00125                               uint8_t event_data_length,
00126                               uint16_t attr_handle,
00127                               uint8_t *attr_value);
00128 
00129     void discAllCharacDescCB(Gap::Handle_t connHandle,
00130                              uint8_t event_data_length,
00131                              uint8_t format,
00132                              uint8_t *handle_uuid_pair);
00133 
00134     void charReadCB(Gap::Handle_t connHandle,
00135                     uint8_t event_data_length,
00136                     uint8_t* attribute_value);
00137 
00138     void charWritePrepareCB(Gap::Handle_t connHandle,
00139                             uint8_t event_data_length,
00140                             uint16_t attribute_handle,
00141                             uint16_t offset,
00142                             uint8_t *part_attr_value);
00143     
00144     void charWriteExecCB(Gap::Handle_t connHandle,
00145                          uint8_t event_data_length);
00146 
00147 protected:
00148 
00149     BlueNRGGattClient() {
00150       _currentState = GATT_IDLE;
00151       _matchingServiceUUID = BLE_UUID_UNKNOWN;
00152       _matchingCharacteristicUUIDIn = BLE_UUID_UNKNOWN;
00153     }
00154 
00155     ServiceDiscovery::ServiceCallback_t  serviceDiscoveryCallback;
00156     ServiceDiscovery::CharacteristicCallback_t characteristicDiscoveryCallback;
00157     ServiceDiscovery::TerminationCallback_t terminationCallback;
00158     CharacteristicDescriptorDiscovery::DiscoveryCallback_t charDescDiscoveryCallback;
00159     CharacteristicDescriptorDiscovery::TerminationCallback_t charDescTerminationCallback;
00160 
00161 private:
00162 
00163   BlueNRGGattClient(BlueNRGGattClient const &);
00164   void operator=(BlueNRGGattClient const &);
00165 
00166   Gap::Handle_t _connectionHandle;
00167   DiscoveredService discoveredService[BLE_TOTAL_DISCOVERED_SERVICES];
00168   BlueNRGDiscoveredCharacteristic discoveredChar[BLE_TOTAL_DISCOVERED_CHARS];
00169   
00170   GattReadCallbackParams readCBParams;
00171   GattWriteCallbackParams writeCBParams;
00172 
00173   // The char for which the descriptor discovery has been launched  
00174   DiscoveredCharacteristic _characteristic;
00175 
00176   UUID _matchingServiceUUID;
00177   UUID _matchingCharacteristicUUIDIn;
00178   uint8_t _currentState;
00179   uint8_t _numServices, _servIndex;
00180   uint8_t _numChars;
00181   uint8_t _numCharDesc;
00182   
00183 };
00184 
00185 #endif /* __BLUENRG_GATT_CLIENT_H__ */