Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GenericGattClient.h Source File

GenericGattClient.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017-2017 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 #ifndef MBED_BLE_GENERIC_GATT_CLIENT
00018 #define MBED_BLE_GENERIC_GATT_CLIENT
00019 
00020 #include <algorithm>
00021 #include "ble/GattClient.h"
00022 #include "ble/pal/PalGattClient.h"
00023 #include "ble/pal/SigningEventMonitor.h"
00024 
00025 // IMPORTANT: private header. Not part of the public interface.
00026 
00027 namespace ble {
00028 namespace generic {
00029 
00030 /**
00031  * Generic implementation of the GattClient.
00032  * It requires a pal::GattClient injected at construction site.
00033  * @attention: Not part of the public interface of BLE API.
00034  */
00035 template<template<class> class TPalGattClient, class SigningMonitorEventHandler>
00036 class GenericGattClient :
00037     public interface::GattClient<GenericGattClient<TPalGattClient, SigningMonitorEventHandler> >,
00038     public pal::SigningEventMonitor<GenericGattClient<TPalGattClient, SigningMonitorEventHandler>, SigningMonitorEventHandler>,
00039     public pal::GattClientEventHandler<GenericGattClient<TPalGattClient, SigningMonitorEventHandler> > {
00040 
00041     using interface::GattClient<GenericGattClient<TPalGattClient, SigningMonitorEventHandler> >::eventHandler;
00042 
00043 public:
00044 
00045     typedef interface::GattClient<GenericGattClient<TPalGattClient, SigningMonitorEventHandler> > Base;
00046     typedef TPalGattClient<GenericGattClient> PalGattClient;
00047     typedef typename Base::WriteOp_t WriteOp_t;
00048 
00049     /**
00050      * Create a GenericGattClient from a pal::GattClient
00051      */
00052     GenericGattClient(PalGattClient* pal_client);
00053 
00054     /**
00055      * @see GattClient::launchServiceDiscovery
00056      */
00057     ble_error_t launchServiceDiscovery_ (
00058         connection_handle_t connection_handle,
00059         ServiceDiscovery::ServiceCallback_t  service_callback,
00060         ServiceDiscovery::CharacteristicCallback_t  characteristic_callback,
00061         const UUID& matching_service_uuid,
00062         const UUID& matching_characteristic_uuid
00063     );
00064 
00065     /**
00066      * @see GattClient::isServiceDiscoveryActive
00067      */
00068     bool isServiceDiscoveryActive_ () const;
00069 
00070     /**
00071      * @see GattClient::terminateServiceDiscovery
00072      */
00073     void terminateServiceDiscovery_ ();
00074 
00075     /**
00076      * @see GattClient::read
00077      */
00078     ble_error_t read_ (
00079         connection_handle_t connection_handle,
00080         GattAttribute::Handle_t attribute_handle,
00081         uint16_t offset
00082     ) const;
00083 
00084     /**
00085      * @see GattClient::write
00086      */
00087     ble_error_t write_ (
00088         WriteOp_t cmd,
00089         connection_handle_t connection_handle,
00090         GattAttribute::Handle_t attribute_handle,
00091         size_t length,
00092         const uint8_t* value
00093     ) const;
00094 
00095     /**
00096      * @see GattClient::onServiceDiscoveryTermination
00097      */
00098     void onServiceDiscoveryTermination_ (
00099         ServiceDiscovery::TerminationCallback_t  callback
00100     );
00101 
00102     /**
00103      * @see GattClient::discoverCharacteristicDescriptors
00104      */
00105     ble_error_t discoverCharacteristicDescriptors_ (
00106         const DiscoveredCharacteristic& characteristic,
00107         const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
00108         const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
00109     );
00110 
00111     /**
00112      * @see GattClient::isCharacteristicDescriptorDiscoveryActive
00113      */
00114     bool isCharacteristicDescriptorDiscoveryActive_ (
00115         const DiscoveredCharacteristic& characteristic
00116     ) const;
00117 
00118     /**
00119      * @see GattClient::terminateCharacteristicDescriptorDiscovery
00120      */
00121     void terminateCharacteristicDescriptorDiscovery_ (
00122         const DiscoveredCharacteristic& characteristic
00123     );
00124 
00125     /**
00126      * @see GattClient::negotiateAttMtu
00127      */
00128     ble_error_t negotiateAttMtu_ (
00129         connection_handle_t connection
00130     );
00131 
00132     /**
00133      * @see GattClient::reset
00134      */
00135     ble_error_t reset_ (void);
00136 
00137     /**
00138      * @see ble::pal::SigningEventMonitor::set_signing_event_handler
00139      */
00140     void set_signing_event_handler_ (SigningMonitorEventHandler *signing_event_handler);
00141 
00142     /**
00143      * @see pal::GattClient::EventHandler::on_att_mtu_change
00144      */
00145     void on_att_mtu_change_ (
00146         ble::connection_handle_t connection_handle,
00147         uint16_t att_mtu_size
00148     );
00149 
00150     /**
00151      * @see pal::GattClient::EventHandler::on_write_command_sent
00152      */
00153     void on_write_command_sent_ (
00154         ble::connection_handle_t connection_handle,
00155         ble::attribute_handle_t attribute_handle,
00156         uint8_t status
00157     );
00158 
00159 private:
00160     struct ProcedureControlBlock;
00161     struct DiscoveryControlBlock;
00162     struct ReadControlBlock;
00163     struct WriteControlBlock;
00164     struct DescriptorDiscoveryControlBlock;
00165 
00166     ProcedureControlBlock* get_control_block(connection_handle_t connection);
00167     const ProcedureControlBlock* get_control_block(connection_handle_t connection) const;
00168     void insert_control_block(ProcedureControlBlock* cb) const;
00169     void remove_control_block(ProcedureControlBlock* cb) const;
00170 
00171     void on_termination(connection_handle_t connection_handle);
00172     void on_server_message_received(connection_handle_t, const pal::AttServerMessage&);
00173     void on_server_response(connection_handle_t, const pal::AttServerMessage&);
00174     void on_server_event(connection_handle_t, const pal::AttServerMessage&);
00175     void on_transaction_timeout(connection_handle_t);
00176 
00177     uint16_t get_mtu(connection_handle_t connection) const;
00178 
00179     PalGattClient* const _pal_client;
00180     ServiceDiscovery::TerminationCallback_t  _termination_callback;
00181     SigningMonitorEventHandler* _signing_event_handler;
00182     mutable ProcedureControlBlock* control_blocks;
00183     bool _is_reseting;
00184 };
00185 
00186 } // generic
00187 } // ble
00188 
00189 #endif /* MBED_BLE_GENERIC_GATT_CLIENT */