Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AttClientToGattClientAdapter.h Source File

AttClientToGattClientAdapter.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 BLE_PAL_ATTCLIENTTOGATTCLIENTADAPTER_H_
00018 #define BLE_PAL_ATTCLIENTTOGATTCLIENTADAPTER_H_
00019 
00020 #include "AttClient.h"
00021 #include "PalGattClient.h"
00022 
00023 namespace ble {
00024 namespace pal {
00025 
00026 /**
00027  * Adapt a pal::AttClient into a pal::GattClient.
00028  *
00029  * This class let vendors define their abstraction layer in term of an AttClient
00030  * and adapt any AttClient into a GattClient.
00031  */
00032 template<class AttClient, class EventHandler>
00033 class AttClientToGattClientAdapter : public GattClient<AttClientToGattClientAdapter<AttClient, EventHandler>, EventHandler > {
00034 
00035 public:
00036     static const uint16_t END_ATTRIBUTE_HANDLE = 0xFFFF;
00037     static const uint16_t SERVICE_TYPE_UUID = 0x2800;
00038     static const uint16_t INCLUDE_TYPE_UUID = 0x2802;
00039     static const uint16_t CHARACTERISTIC_TYPE_UUID = 0x2803;
00040 
00041     /**
00042      * Construct an instance of GattClient from an instance of AttClient.
00043      * @param client The client to adapt.
00044      */
00045     AttClientToGattClientAdapter(AttClient& client) :
00046         _client(client) {
00047         _client.when_server_message_received(
00048             mbed::callback(this, &AttClientToGattClientAdapter::on_server_event)
00049         );
00050         _client.when_transaction_timeout(
00051             mbed::callback(
00052                 this, &AttClientToGattClientAdapter::on_transaction_timeout
00053             )
00054         );
00055     }
00056 
00057     /**
00058      * @see ble::pal::GattClient::exchange_mtu
00059      */
00060     ble_error_t exchange_mtu_ (connection_handle_t connection) {
00061         return _client.exchange_mtu_request(connection);
00062     }
00063 
00064     /**
00065      * @see ble::pal::GattClient::get_mtu_size
00066      */
00067     ble_error_t get_mtu_size_ (
00068         connection_handle_t connection_handle,
00069         uint16_t& mtu_size
00070     ) {
00071         return _client.get_mtu_size(connection_handle, mtu_size);
00072     }
00073 
00074     /**
00075      * @see ble::pal::GattClient::discover_primary_service
00076      */
00077     ble_error_t discover_primary_service_ (
00078         connection_handle_t connection,
00079         attribute_handle_t discovery_range_begining
00080     ) {
00081         return _client.read_by_group_type_request(
00082             connection,
00083             attribute_handle_range(discovery_range_begining, END_ATTRIBUTE_HANDLE),
00084             SERVICE_TYPE_UUID
00085         );
00086     }
00087 
00088     /**
00089      * @see ble::pal::GattClient::discover_primary_service_by_service_uuid
00090      */
00091     ble_error_t discover_primary_service_by_service_uuid_ (
00092         connection_handle_t connection_handle,
00093         attribute_handle_t discovery_range_begining,
00094         const UUID& uuid
00095     ) {
00096         return _client.find_by_type_value_request(
00097             connection_handle,
00098             attribute_handle_range(discovery_range_begining, END_ATTRIBUTE_HANDLE),
00099             SERVICE_TYPE_UUID,
00100             Span<const uint8_t>(
00101                 uuid.getBaseUUID(),
00102                 (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) ? 2 : UUID::LENGTH_OF_LONG_UUID
00103             )
00104         );
00105     }
00106 
00107     /**
00108      * @see ble::pal::GattClient::find_included_service
00109      */
00110     ble_error_t find_included_service_ (
00111         connection_handle_t connection_handle,
00112         attribute_handle_range_t service_range
00113     ) {
00114         return _client.read_by_type_request(
00115             connection_handle,
00116             service_range,
00117             INCLUDE_TYPE_UUID
00118         );
00119     }
00120 
00121     /**
00122      * @see ble::pal::GattClient::discover_characteristics_of_a_service
00123      */
00124     ble_error_t discover_characteristics_of_a_service_ (
00125         connection_handle_t connection_handle,
00126         attribute_handle_range_t discovery_range
00127     ) {
00128         return _client.read_by_type_request(
00129             connection_handle,
00130             discovery_range,
00131             CHARACTERISTIC_TYPE_UUID
00132         );
00133     }
00134 
00135     /**
00136      * @see ble::pal::GattClient::discover_characteristics_descriptors
00137      */
00138     ble_error_t discover_characteristics_descriptors_ (
00139         connection_handle_t connection_handle,
00140         attribute_handle_range_t descriptors_discovery_range
00141     ) {
00142         return _client.find_information_request(
00143             connection_handle,
00144             descriptors_discovery_range
00145         );
00146     }
00147 
00148     /**
00149      * @see ble::pal::GattClient::read_attribute_value
00150      */
00151     ble_error_t read_attribute_value_ (
00152         connection_handle_t connection_handle,
00153         attribute_handle_t attribute_handle
00154     ) {
00155         return _client.read_request(
00156             connection_handle,
00157             attribute_handle
00158         );
00159     }
00160 
00161     /**
00162      * @see ble::pal::GattClient::read_using_characteristic_uuid
00163      */
00164     ble_error_t read_using_characteristic_uuid_ (
00165         connection_handle_t connection_handle,
00166         attribute_handle_range_t read_range,
00167         const UUID& uuid
00168     ) {
00169         return _client.read_by_type_request(
00170             connection_handle,
00171             read_range,
00172             uuid
00173         );
00174     }
00175 
00176     /**
00177      * @see ble::pal::GattClient::read_attribute_blob
00178      */
00179     ble_error_t read_attribute_blob_ (
00180         connection_handle_t connection_handle,
00181         attribute_handle_t attribute_handle,
00182         uint16_t offset
00183     ) {
00184         return _client.read_blob_request(
00185             connection_handle,
00186             attribute_handle,
00187             offset
00188         );
00189     }
00190 
00191     /**
00192      * @see ble::pal::GattClient::read_multiple_characteristic_values
00193      */
00194     ble_error_t read_multiple_characteristic_values_ (
00195         connection_handle_t connection_handle,
00196         const Span<const attribute_handle_t>& characteristic_value_handles
00197     ) {
00198         return _client.read_multiple_request(
00199             connection_handle,
00200             characteristic_value_handles
00201         );
00202     }
00203 
00204     /**
00205      * @see ble::pal::GattClient::write_without_response
00206      */
00207     ble_error_t write_without_response_ (
00208         connection_handle_t connection_handle,
00209         attribute_handle_t characteristic_value_handle,
00210         const Span<const uint8_t>& value
00211     ) {
00212         return _client.write_command(
00213             connection_handle,
00214             characteristic_value_handle,
00215             value
00216         );
00217     }
00218 
00219     /**
00220      * @see ble::pal::GattClient::signed_write_without_response
00221      */
00222     ble_error_t signed_write_without_response_ (
00223         connection_handle_t connection_handle,
00224         attribute_handle_t characteristic_value_handle,
00225         const Span<const uint8_t>& value
00226     ) {
00227         return _client.signed_write_command(
00228             connection_handle,
00229             characteristic_value_handle,
00230             value
00231         );
00232     }
00233 
00234     /**
00235      * @see ble::pal::GattClient::write_attribute
00236      */
00237     ble_error_t write_attribute_ (
00238         connection_handle_t connection_handle,
00239         attribute_handle_t attribute_handle,
00240         const Span<const uint8_t>& value
00241     ) {
00242         return _client.write_request(
00243             connection_handle,
00244             attribute_handle,
00245             value
00246         );
00247     }
00248 
00249     /**
00250      * @see ble::pal::GattClient::queue_prepare_write
00251      */
00252     ble_error_t queue_prepare_write_ (
00253         connection_handle_t connection_handle,
00254         attribute_handle_t characteristic_value_handle,
00255         const Span<const uint8_t>& value,
00256         uint16_t offset
00257     ) {
00258         return _client.prepare_write_request(
00259             connection_handle,
00260             characteristic_value_handle,
00261             offset,
00262             value
00263         );
00264     }
00265 
00266     /**
00267      * @see ble::pal::GattClient::execute_write_queue
00268      */
00269     ble_error_t execute_write_queue_ (
00270         connection_handle_t connection_handle,
00271         bool execute
00272     ) {
00273         return _client.execute_write_request(connection_handle, execute);
00274     }
00275 
00276     /**
00277      * @see ble::pal::GattClient::initialize
00278      */
00279     ble_error_t initialize_ () {
00280         return _client.initialize();
00281     }
00282 
00283     /**
00284      * @see ble::pal::GattClient::terminate
00285      */
00286     ble_error_t terminate_ () {
00287         return _client.initialize();
00288     }
00289 
00290 private:
00291     AttClient& _client;
00292 };
00293 
00294 } // namespace pal
00295 } // namespace ble
00296 
00297 
00298 #endif /* BLE_PAL_ATTCLIENTTOGATTCLIENTADAPTER_H_ */