Lorenzo Invidia / target_st_bluenrg

Dependents:   ble-star-mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlueNRGGattConnectionClient.h Source File

BlueNRGGattConnectionClient.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    BlueNRGGattConnectionClient.cpp 
00019   * @author  STMicroelectronics
00020   * @brief   Header file for BlueNRGGattConnectionClient 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_CONNECTION_CLIENT_H__
00035 #define __BLUENRG_GATT_CONNECTION_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 BlueNRGGattConnectionClient
00055 {
00056 public:
00057     
00058     enum {
00059       GATT_IDLE,
00060       GATT_SERVICE_DISCOVERY,
00061       GATT_CHAR_DESC_DISCOVERY,
00062       //GATT_CHARS_DISCOVERY_COMPLETE,
00063       //GATT_DISCOVERY_TERMINATED,
00064       GATT_READ_CHAR,
00065       GATT_WRITE_CHAR
00066     };
00067     
00068     /* Functions that must be implemented from GattClient */
00069     ble_error_t launchServiceDiscovery(ServiceDiscovery::ServiceCallback_t         sc                           = NULL,
00070                                        ServiceDiscovery::CharacteristicCallback_t  cc                           = NULL,
00071                                        const UUID                                 &matchingServiceUUID          = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
00072                                        const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00073 
00074     ble_error_t discoverServices(ServiceDiscovery::ServiceCallback_t  callback,
00075                                          const UUID                          &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00076 
00077     ble_error_t discoverServices(ServiceDiscovery::ServiceCallback_t  callback,
00078                                          GattAttribute::Handle_t              startHandle,
00079                                          GattAttribute::Handle_t              endHandle);
00080 
00081     bool isServiceDiscoveryActive(void) const;
00082     void terminateServiceDiscovery(void);
00083     void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
00084       terminationCallback = callback;
00085     }
00086     ble_error_t read(GattAttribute::Handle_t attributeHandle, uint16_t offset) const;
00087     ble_error_t write(GattClient::WriteOp_t    cmd,
00088                               GattAttribute::Handle_t  attributeHandle,
00089                               size_t                   length,
00090                               const uint8_t           *value) const;
00091     ble_error_t discoverCharacteristicDescriptors(
00092         const DiscoveredCharacteristic& characteristic,
00093         const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
00094         const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback);
00095 
00096     ble_error_t reset(void);
00097 
00098     void gattProcedureCompleteCB(uint8_t error_code);
00099 
00100     void primaryServicesCB(uint8_t event_data_length,
00101                            uint8_t attribute_data_length,
00102                            uint8_t *attribute_data_list);
00103     
00104     void primaryServiceCB(uint8_t event_data_length,
00105                           uint8_t *handles_info_list);
00106     
00107     ble_error_t findServiceChars(void);
00108     
00109     void serviceCharsCB(uint8_t event_data_length,
00110                         uint8_t handle_value_pair_length,
00111                         uint8_t *handle_value_pair);
00112     
00113     void serviceCharByUUIDCB(uint8_t event_data_length,
00114                              uint16_t attr_handle,
00115                              uint8_t *attr_value);
00116 
00117     void discAllCharacDescCB(uint8_t event_data_length,
00118                              uint8_t format,
00119                              uint8_t *handle_uuid_pair);
00120 
00121     void charReadCB(uint8_t event_data_length,
00122                     uint8_t* attribute_value);
00123 
00124     void charWritePrepareCB(uint8_t event_data_length,
00125                             uint16_t attribute_handle,
00126                             uint16_t offset,
00127                             uint8_t *part_attr_value);
00128     
00129     void charWriteExecCB(uint8_t event_data_length);
00130 
00131 protected:
00132 
00133     BlueNRGGattConnectionClient(BlueNRGGattClient *gattClient, Gap::Handle_t connectionHandle):
00134                                 discoveredService(),
00135                                 discoveredChar(),
00136                                 readCBParams(),
00137                                 writeCBParams(),
00138                                 _characteristic()  {
00139 
00140      //PRINTF("BlueNRGGattConnectionClient construtor: connHandle=%d\n\r", connectionHandle);
00141 
00142      _gattClient = gattClient;
00143      _connectionHandle = connectionHandle;
00144 
00145      _currentState = GATT_IDLE;
00146      _matchingServiceUUID = BLE_UUID_UNKNOWN;
00147      _matchingCharacteristicUUIDIn = BLE_UUID_UNKNOWN;
00148 
00149     }
00150 
00151     ServiceDiscovery::ServiceCallback_t  serviceDiscoveryCallback;
00152     ServiceDiscovery::CharacteristicCallback_t characteristicDiscoveryCallback;
00153     ServiceDiscovery::TerminationCallback_t terminationCallback;
00154     CharacteristicDescriptorDiscovery::DiscoveryCallback_t charDescDiscoveryCallback;
00155     CharacteristicDescriptorDiscovery::TerminationCallback_t charDescTerminationCallback;
00156 
00157 private:
00158 
00159   BlueNRGGattConnectionClient(BlueNRGGattConnectionClient const &);
00160   void operator=(BlueNRGGattConnectionClient const &);
00161   ~BlueNRGGattConnectionClient() {};
00162 
00163   BlueNRGGattClient *_gattClient;
00164 
00165   Gap::Handle_t _connectionHandle;
00166   DiscoveredService discoveredService[BLE_TOTAL_DISCOVERED_SERVICES];
00167   BlueNRGDiscoveredCharacteristic discoveredChar[BLE_TOTAL_DISCOVERED_CHARS];
00168   
00169   GattReadCallbackParams readCBParams;
00170   GattWriteCallbackParams writeCBParams;
00171 
00172   // The char for which the descriptor discovery has been launched  
00173   DiscoveredCharacteristic _characteristic;
00174 
00175   UUID _matchingServiceUUID;
00176   UUID _matchingCharacteristicUUIDIn;
00177   uint8_t _currentState;
00178   uint8_t _numServices, _servIndex;
00179   uint8_t _numChars;
00180   uint8_t _numCharDesc;
00181   
00182   friend class BlueNRGGattClient;
00183 };
00184 
00185 #endif /* __BLUENRG_GATT_CONNECTION_CLIENT_H__ */