diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/BLEInstanceBaseImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/BLEInstanceBaseImpl.h deleted file mode 100644 index e256f4e..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/BLEInstanceBaseImpl.h +++ /dev/null @@ -1,202 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IMPL_BLE_INSTANCE_BASE_H_ -#define IMPL_BLE_INSTANCE_BASE_H_ - -#include "ble/BLE.h" -#include "ble/common/blecommon.h" -#include "source/BLEInstanceBase.h" - -#include "ble/driver/CordioHCIDriver.h" -#include "ble/GattServer.h" -#include "source/pal/PalAttClient.h" -#include "source/pal/PalGattClient.h" -#include "ble/GattClient.h" -#include "source/pal/PalGap.h" -#include "ble/Gap.h" -#include "source/pal/PalGenericAccessService.h" -#include "ble/SecurityManager.h" -#include "source/pal/PalEventQueue.h" -#include "drivers/LowPowerTimer.h" -#include "source/pal/PalSecurityManager.h" - -#include "source/generic/GapImpl.h" -#include "source/generic/GattClientImpl.h" -#include "source/GattServerImpl.h" -#include "source/generic/SecurityManagerImpl.h" -#include "internal/PalEventQueueImpl.h" - -namespace ble { - -class PalSigningMonitor; - -namespace impl { - -/** - * @see BLEInstanceBase - */ -class BLEInstanceBase : public ble::BLEInstanceBase { - friend PalSigningMonitor; - - /** - * Construction with an HCI driver. - */ - BLEInstanceBase(CordioHCIDriver &hci_driver); - - /** - * Destructor - */ - virtual ~BLEInstanceBase(); - -public: - - /** - * Access to the singleton containing the implementation of BLEInstanceBase - * for the Cordio stack. - */ - static BLEInstanceBase &deviceInstance(); - - /** - * @see BLEInstanceBase::init - */ - virtual ble_error_t init( - FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback - ); - - /** - * @see BLEInstanceBase::hasInitialized - */ - virtual bool hasInitialized() const; - - /** - * @see BLEInstanceBase::shutdown - */ - virtual ble_error_t shutdown(); - - /** - * @see BLEInstanceBase::getVersion - */ - virtual const char *getVersion(); - - ble::impl::Gap &getGapImpl(); - - /** - * @see BLEInstanceBase::getGap - */ - virtual ble::Gap &getGap(); - - /** - * @see BLEInstanceBase::getGap - */ - virtual const ble::Gap &getGap() const; - -#if BLE_FEATURE_GATT_SERVER - - ble::impl::GattServer &getGattServerImpl(); - - /** - * @see BLEInstanceBase::getGattServer - */ - virtual ble::GattServer &getGattServer(); - - /** - * @see BLEInstanceBase::getGattServer - */ - virtual const ble::GattServer &getGattServer() const; - -#endif // BLE_FEATURE_GATT_SERVER - -#if BLE_FEATURE_GATT_CLIENT - - ble::impl::GattClient &getGattClientImpl(); - - /** - * @see BLEInstanceBase::getGattClient - */ - virtual ble::GattClient &getGattClient(); - - /** - * Get the PAL Gatt Client. - * - * @return PAL Gatt Client. - */ - PalGattClient &getPalGattClient(); - -#endif // BLE_FEATURE_GATT_CLIENT - -#if BLE_FEATURE_SECURITY - - ble::impl::SecurityManager &getSecurityManagerImpl(); - - /** - * @see BLEInstanceBase::getSecurityManager - */ - virtual ble::SecurityManager &getSecurityManager(); - - /** - * @see BLEInstanceBase::getSecurityManager - */ - virtual const ble::SecurityManager &getSecurityManager() const; - -#endif // BLE_FEATURE_SECURITY - - /** - * @see BLEInstanceBase::waitForEvent - */ - virtual void waitForEvent(); - - /** - * @see BLEInstanceBase::processEvents - */ - virtual void processEvents(); - -private: - static void stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg); - - static void device_manager_cb(dmEvt_t *dm_event); - - static void connection_handler(dmEvt_t *dm_event); - - static void timeoutCallback(); - - void stack_setup(); - - void start_stack_reset(); - - void callDispatcher(); - - static CordioHCIDriver *_hci_driver; - static FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> _init_callback; - - enum { - NOT_INITIALIZED, - INITIALIZING, - INITIALIZED - } initialization_status; - - mutable ble::impl::PalEventQueue _event_queue; - mbed::LowPowerTimer _timer; - uint64_t _last_update_us; -}; - -} // namespace impl -} // namespace ble - - -#endif /* IMPL_BLE_INSTANCE_BASE_H_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalAttClientImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalAttClientImpl.h deleted file mode 100644 index 02f6923..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalAttClientImpl.h +++ /dev/null @@ -1,481 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IMPL_PAL_ATT_CLIENT_ -#define IMPL_PAL_ATT_CLIENT_ - -#include "source/pal/PalSimpleAttServerMessage.h" -#include "source/pal/PalAttClient.h" - -#include "att_api.h" - -namespace ble { -namespace impl { - -class PalAttClient final : public ble::PalAttClient { - -public: - PalAttClient(); - - ~PalAttClient(); - - /** - * @see ble::PalAttClient::exchange_mtu_request - */ - ble_error_t exchange_mtu_request(connection_handle_t connection) final; - - /** - * @see ble::PalGattClient::get_mtu_size - */ - ble_error_t get_mtu_size( - connection_handle_t connection_handle, - uint16_t &mtu_size - ) final; - - /** - * @see ble::PalAttClient::find_information_request - */ - ble_error_t find_information_request( - connection_handle_t connection_handle, - attribute_handle_range_t discovery_range - ) final; - - /** - * @see ble::PalAttClient::find_by_type_value_request - */ - ble_error_t find_by_type_value_request( - connection_handle_t connection_handle, - attribute_handle_range_t discovery_range, - uint16_t type, - const Span &value - ) final; - - /** - * @see ble::PalAttClient::read_by_type_request - */ - ble_error_t read_by_type_request( - connection_handle_t connection_handle, - attribute_handle_range_t read_range, - const UUID &type - ) final; - - /** - * @see ble::PalAttClient::read_request - */ - ble_error_t read_request( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle - ) final; - - /** - * @see ble::PalAttClient::read_blob_request - */ - ble_error_t read_blob_request( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle, - uint16_t offset - ) final; - - /** - * @see ble::PalAttClient::read_multiple_request - */ - ble_error_t read_multiple_request( - connection_handle_t connection_handle, - const Span &attribute_handles - ) final; - - /** - * @see ble::PalAttClient::read_by_group_type_request - */ - ble_error_t read_by_group_type_request( - connection_handle_t connection_handle, - attribute_handle_range_t read_range, - const UUID &group_type - ) final; - - /** - * @see ble::PalAttClient::write_request - */ - ble_error_t write_request( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle, - const Span &value - ) final; - - /** - * @see ble::PalAttClient::write_command - */ - ble_error_t write_command( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle, - const Span &value - ) final; - - /** - * @see ble::PalAttClient::signed_write_command - */ - ble_error_t signed_write_command( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle, - const Span &value - ) final; - - /** - * Initialises the counter used to sign messages. Counter will be incremented every - * time a message is signed. - * - * @param sign_counter initialise the signing counter to this value - */ - void set_sign_counter( - sign_count_t sign_counter - ); - - /** - * @see ble::PalAttClient::prepare_write_request - */ - ble_error_t prepare_write_request( - connection_handle_t connection_handle, - attribute_handle_t attribute_handle, - uint16_t offset, - const Span &value - ) final; - - /** - * @see ble::PalAttClient::execute_write_request - */ - ble_error_t execute_write_request( - connection_handle_t connection_handle, - bool execute - ) final; - - /** - * @see ble::PalAttClient::initialize - */ - ble_error_t initialize() final; - - /** - * @see ble::PalAttClient::terminate - */ - ble_error_t terminate() final; - - // singleton of the ARM Cordio client - static PalAttClient &get_client(); - - void when_server_message_received( - mbed::Callback cb - ) final; - - void when_transaction_timeout( - mbed::Callback cb - ) final; - -private: - // convert an array of byte to an uint16_t - static uint16_t to_uint16_t(const uint8_t *array); - - /** - * Type of an event handler. - * @param The event to handle - * @return true if the event has been handled and false otherwise. - */ - typedef bool (*event_handler_t)(const attEvt_t *); - -public: - /** - * Callback which handle attEvt_t and forward them to on_server_event. - */ - static void att_client_handler(const attEvt_t *event); - -private: - /** - * Event handler generator. - * @tparam Description of an event converter. It contains two static member - * function: - * - bool can_convert(const attEvt_t* event): return true if the event can - * be converted by the converter - * - convert(const attEvt_t* event): return the - * AttServerMessage converted from event. - * @param event - * @return - */ - template - static bool event_handler(const attEvt_t *event); - - static bool timeout_event_handler(const attEvt_t *event); - - template - static void generated_handler( - const attEvt_t *event, ResultType (*convert)(const attEvt_t *) - ); - - /** - * Traits defining can_convert for non ErrorResponse events. - */ - template - struct ResponseConverter { - static bool can_convert(const attEvt_t *event) - { - if (event->hdr.status == ATT_SUCCESS && event->hdr.event == RequestID) { - return true; - } - return false; - } - }; - - /** - * Converter for an AttErrorResponse. - */ - struct ErrorResponseConverter { - static bool can_convert(const attEvt_t *event) - { - if (event->hdr.status != ATT_SUCCESS) { - return true; - } - return false; - } - - static AttErrorResponse convert(const attEvt_t *event) - { - return AttErrorResponse( - static_cast(event->hdr.event * 2), - event->handle, - event->hdr.status - ); - } - }; - - /** - * Converter for a PalSimpleAttFindInformationResponse. - */ - struct FindInformationResponseConverter : ResponseConverter { - static PalSimpleAttFindInformationResponse convert(const attEvt_t *event) - { - return PalSimpleAttFindInformationResponse( - static_cast(event->pValue[0]), - make_const_Span( - event->pValue + 1, - event->valueLen - 1 - ) - ); - } - }; - - /** - * Converter for a PalSimpleAttFindByTypeValueResponse. - */ - struct FindByTypeValueResponseConverter : ResponseConverter { - static PalSimpleAttFindByTypeValueResponse convert(const attEvt_t *event) - { - return PalSimpleAttFindByTypeValueResponse( - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a PalSimpleAttReadByTypeResponse. - */ - struct ReadByTypeResponseConverter : ResponseConverter { - static PalSimpleAttReadByTypeResponse convert(const attEvt_t *event) - { - return PalSimpleAttReadByTypeResponse( - event->pValue[0], - make_const_Span( - event->pValue + 1, - event->valueLen - 1 - ) - ); - } - }; - - /** - * Converter for a AttReadResponse. - */ - struct ReadResponseConverter : ResponseConverter { - static AttReadResponse convert(const attEvt_t *event) - { - return AttReadResponse( - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a AttReadBlobResponse. - */ - struct ReadBlobResponseConverter : ResponseConverter { - static AttReadBlobResponse convert(const attEvt_t *event) - { - return AttReadBlobResponse( - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a AttReadMultipleResponse. - */ - struct ReadMultipleResponseConverter : ResponseConverter { - static AttReadMultipleResponse convert(const attEvt_t *event) - { - return AttReadMultipleResponse( - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a PalSimpleAttReadByGroupTypeResponse. - */ - struct ReadBygroupTypeResponseConverter : ResponseConverter { - static PalSimpleAttReadByGroupTypeResponse convert(const attEvt_t *event) - { - return PalSimpleAttReadByGroupTypeResponse( - event->pValue[0], - make_const_Span( - event->pValue + 1, - event->valueLen - 1 - ) - ); - } - }; - - /** - * Converter for a AttWriteResponse. - */ - struct WriteResponseConverter : ResponseConverter { - static AttWriteResponse convert(const attEvt_t *event) - { - return AttWriteResponse(); - } - }; - - /** - * Converter for a AttPrepareWriteResponse. - */ - struct PrepareWriteResponseConverter : ResponseConverter { - static AttPrepareWriteResponse convert(const attEvt_t *event) - { - return AttPrepareWriteResponse( - event->handle, - 0, /* offset is lost */ - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a AttExecuteWriteResponse. - */ - struct ExecuteWriteResponseConverter : ResponseConverter { - static AttExecuteWriteResponse convert(const attEvt_t *event) - { - return AttExecuteWriteResponse(); - } - }; - - /** - * Converter for a AttHandleValueNotification. - */ - struct HandleValueNotificationConverter : ResponseConverter { - static AttHandleValueNotification convert(const attEvt_t *event) - { - return AttHandleValueNotification( - event->handle, - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - - /** - * Converter for a AttHandleValueIndication. - */ - struct HandleValueIndicationConverter : ResponseConverter { - static AttHandleValueIndication convert(const attEvt_t *event) - { - return AttHandleValueIndication( - event->handle, - make_const_Span( - event->pValue, - event->valueLen - ) - ); - } - }; - -private: - /** - * Upon server message reception an implementation shall call this function. - * - * @param connection_handle The handle of the connection which has received - * the server message. - * @param server_message The message received from the server. - */ - void on_server_event( - connection_handle_t connection_handle, - const AttServerMessage &server_message - ); - - /** - * Upon transaction timeout an implementation shall call this function. - * - * @param connection_handle The handle of the connection of the transaction - * which has times out. - * - * @note see BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part F Section 3.3.3 - */ - void on_transaction_timeout( - connection_handle_t connection_handle - ); - -private: - sign_count_t _local_sign_counter; - - /** - * Callback called when the client receive a message from the server. - */ - mbed::Callback _server_message_cb; - - /** - * Callback called when a transaction times out. - */ - mbed::Callback _transaction_timeout_cb; -}; - -} // namespace impl -} // namespace ble - -#endif /* IMPL_PAL_ATT_CLIENT_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalEventQueueImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalEventQueueImpl.h deleted file mode 100644 index 507b7f0..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalEventQueueImpl.h +++ /dev/null @@ -1,146 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BLE_PAL_SIMPLE_EVENT_QUEUE_H_ -#define BLE_PAL_SIMPLE_EVENT_QUEUE_H_ - -#include -#include "source/pal/PalEventQueue.h" - -#include "wsf_buf.h" - -namespace ble { - -class BLEInstanceBase; - -namespace impl { - -/** - * Simple implementation of the EventQueue. - */ -class PalEventQueue final : public ble::PalEventQueue { -public: - typedef mbed::Callback event_t; - - /** - * Construct an empty event queue. - * - * @attention a call to initialize is mandatory before any other call. - */ - PalEventQueue() : - _ble_base(nullptr), _events(nullptr) - { - } - - /** - * Initialize the event queue with a BLEInstanceBase and a ble id. - * - * @param ble_base the instance which will be used to signal the presence - * of new events. - */ - void initialize(ble::BLEInstanceBase *ble_base) - { - _ble_base = ble_base; - } - - /** - * @see ble::EventQueue - */ - ~PalEventQueue() - { - clear(); - } - - /** - * @see ble::post - */ - bool post(const mbed::Callback &event) final - { - if (_ble_base == nullptr) { - return false; - } - void *event_buf = WsfBufAlloc(sizeof(EventNode)); - MBED_ASSERT(event_buf != nullptr); - if (event_buf == nullptr) { - return false; - } - auto *next = new(event_buf) EventNode(event); - - if (_events == nullptr) { - _events = next; - } else { - EventNode *previous = _events; - while (previous->next) { - previous = previous->next; - } - - previous->next = next; - } - - signal_event(); - - return true; - } - - /** - * Clear the event queue from all its events. - */ - void clear() - { - while (_events) { - EventNode *next = _events->next; - _events->~EventNode(); - WsfBufFree(_events); - _events = next; - } - } - - /** - * Process the event queue. - */ - void process() - { - while (_events) { - EventNode *next = _events->next; - _events->event(); - _events->~EventNode(); - WsfBufFree(_events); - _events = next; - } - } - -private: - struct EventNode { - EventNode(const event_t event) : event(event), next(nullptr) - { - } - - event_t event; - EventNode *next; - }; - - void signal_event(); - - ble::BLEInstanceBase *_ble_base; - EventNode *_events; -}; - -} // namespace impl -} // namespace ble - -#endif /* BLE_PAL_SIMPLE_EVENT_QUEUE_H_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGapImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGapImpl.h deleted file mode 100644 index 50829fa..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGapImpl.h +++ /dev/null @@ -1,554 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IMPL_PAL_GAP_ -#define IMPL_PAL_GAP_ - -#include "source/pal/PalGap.h" -#include "dm_api.h" - -namespace ble { -namespace impl { - -/** - * Implementation of ble::PalGap for the Cordio stack. - */ -class PalGap final : public ble::PalGap { -public: - PalGap() : _pal_event_handler(nullptr), use_active_scanning(false) - { - }; - - ~PalGap() = default; - - bool is_feature_supported( - ble::controller_supported_features_t feature - ) final; - - ble_error_t initialize() final; - - ble_error_t terminate() final; - - address_t get_device_address() final; - - address_t get_random_address() final; - - ble_error_t set_random_address(const address_t &address) final; - - ble_error_t set_advertising_parameters( - uint16_t advertising_interval_min, - uint16_t advertising_interval_max, - advertising_type_t advertising_type, - own_address_type_t own_address_type, - advertising_peer_address_type_t peer_address_type, - const address_t &peer_address, - advertising_channel_map_t advertising_channel_map, - advertising_filter_policy_t advertising_filter_policy - ) final; - - ble_error_t set_advertising_data( - uint8_t advertising_data_length, - const advertising_data_t &advertising_data - ) final; - - ble_error_t set_scan_response_data( - uint8_t scan_response_data_length, - const advertising_data_t &scan_response_data - ) final; - - ble_error_t advertising_enable(bool enable) final; - - ble_error_t set_scan_parameters( - bool active_scanning, - uint16_t scan_interval, - uint16_t scan_window, - own_address_type_t own_address_type, - scanning_filter_policy_t filter_policy - ) final; - - ble_error_t scan_enable( - bool enable, - bool filter_duplicates - ) final; - - ble_error_t create_connection( - uint16_t scan_interval, - uint16_t scan_window, - initiator_policy_t initiator_policy, - connection_peer_address_type_t peer_address_type, - const address_t &peer_address, - own_address_type_t own_address_type, - uint16_t connection_interval_min, - uint16_t connection_interval_max, - uint16_t connection_latency, - uint16_t supervision_timeout, - uint16_t minimum_connection_event_length, - uint16_t maximum_connection_event_length - ) final; - - ble_error_t cancel_connection_creation(); - - uint8_t read_white_list_capacity() final; - - ble_error_t clear_whitelist() final; - - ble_error_t add_device_to_whitelist( - whitelist_address_type_t address_type, - address_t address - ) final; - - ble_error_t remove_device_from_whitelist( - whitelist_address_type_t address_type, - address_t address - ) final; - - ble_error_t connection_parameters_update( - connection_handle_t connection, - uint16_t connection_interval_min, - uint16_t connection_interval_max, - uint16_t connection_latency, - uint16_t supervision_timeout, - uint16_t minimum_connection_event_length, - uint16_t maximum_connection_event_length - ) final; - - ble_error_t accept_connection_parameter_request( - connection_handle_t connection_handle, - uint16_t interval_min, - uint16_t interval_max, - uint16_t latency, - uint16_t supervision_timeout, - uint16_t minimum_connection_event_length, - uint16_t maximum_connection_event_length - ) final; - - ble_error_t reject_connection_parameter_request( - connection_handle_t connection_handle, - hci_error_code_t rejection_reason - ) final; - - ble_error_t disconnect( - connection_handle_t connection, - local_disconnection_reason_t disconnection_reason - ) final; - - bool is_privacy_supported() final; - - ble_error_t set_address_resolution( - bool enable - ) final; - - ble_error_t read_phy(connection_handle_t connection) final; - - ble_error_t set_preferred_phys( - const phy_set_t &tx_phys, - const phy_set_t &rx_phys - ) final; - - ble_error_t set_phy( - connection_handle_t connection, - const phy_set_t &tx_phys, - const phy_set_t &rx_phys, - coded_symbol_per_bit_t coded_symbol - ) final; - - // singleton of the ARM Cordio client - static PalGap &get_gap(); - - /** - * Callback which handle wsfMsgHdr_t and forward them to emit_gap_event. - */ - static void gap_handler(const wsfMsgHdr_t *msg); - - ble_error_t set_advertising_set_random_address( - advertising_handle_t advertising_handle, - const address_t &address - ) final; - - ble_error_t set_extended_advertising_parameters( - advertising_handle_t advertising_handle, - advertising_event_properties_t event_properties, - advertising_interval_t primary_advertising_interval_min, - advertising_interval_t primary_advertising_interval_max, - advertising_channel_map_t primary_advertising_channel_map, - own_address_type_t own_address_type, - advertising_peer_address_type_t peer_address_type, - const address_t &peer_address, - advertising_filter_policy_t advertising_filter_policy, - advertising_power_t advertising_power, - phy_t primary_advertising_phy, - uint8_t secondary_advertising_max_skip, - phy_t secondary_phy, - uint8_t advertising_sid, - bool scan_request_notification - ) final; - - ble_error_t set_periodic_advertising_parameters( - advertising_handle_t advertising_handle, - periodic_advertising_interval_t periodic_advertising_min, - periodic_advertising_interval_t periodic_advertising_max, - bool advertise_power - ) final; - - ble_error_t set_extended_advertising_data( - advertising_handle_t advertising_handle, - advertising_fragment_description_t operation, - bool minimize_fragmentation, - uint8_t advertising_data_size, - const uint8_t *advertising_data - ) final; - - ble_error_t set_periodic_advertising_data( - advertising_handle_t advertising_handle, - advertising_fragment_description_t fragment_description, - uint8_t advertising_data_size, - const uint8_t *advertising_data - ) final; - - ble_error_t set_extended_scan_response_data( - advertising_handle_t advertising_handle, - advertising_fragment_description_t operation, - bool minimize_fragmentation, - uint8_t scan_response_data_size, - const uint8_t *scan_response_data - ) final; - - ble_error_t extended_advertising_enable( - bool enable, - uint8_t number_of_sets, - const advertising_handle_t *handles, - const uint16_t *durations, - const uint8_t *max_extended_advertising_events - ) final; - - ble_error_t periodic_advertising_enable( - bool enable, - advertising_handle_t advertising_handle - ) final; - - uint16_t get_maximum_advertising_data_length() final; - - uint16_t get_maximum_connectable_advertising_data_length() final; - - uint8_t get_maximum_hci_advertising_data_length() final; - - uint8_t get_max_number_of_advertising_sets() final; - - ble_error_t remove_advertising_set( - advertising_handle_t advertising_handle - ) final; - - ble_error_t clear_advertising_sets() final; - - ble_error_t set_extended_scan_parameters( - own_address_type_t own_address_type, - scanning_filter_policy_t filter_policy, - phy_set_t scanning_phys, - const bool *active_scanning, - const uint16_t *scan_interval, - const uint16_t *scan_window - ) final; - - ble_error_t extended_scan_enable( - bool enable, - duplicates_filter_t filter_duplicates, - uint16_t duration, - uint16_t period - ) final; - - ble_error_t periodic_advertising_create_sync( - bool use_periodic_advertiser_list, - uint8_t advertising_sid, - peer_address_type_t peer_address_type, - const address_t &peer_address, - uint16_t allowed_skip, - uint16_t sync_timeout - ) final; - - ble_error_t cancel_periodic_advertising_create_sync() final; - - ble_error_t periodic_advertising_terminate_sync( - sync_handle_t sync_handle - ) final; - - ble_error_t add_device_to_periodic_advertiser_list( - advertising_peer_address_type_t advertiser_address_type, - const address_t &advertiser_address, - uint8_t advertising_sid - ) final; - - ble_error_t remove_device_from_periodic_advertiser_list( - advertising_peer_address_type_t advertiser_address_type, - const address_t &advertiser_address, - uint8_t advertising_sid - ) final; - - ble_error_t clear_periodic_advertiser_list() final; - - uint8_t read_periodic_advertiser_list_size() final; - - ble_error_t extended_create_connection( - initiator_policy_t initiator_policy, - own_address_type_t own_address_type, - peer_address_type_t peer_address_type, - const address_t &peer_address, - phy_set_t initiating_phys, - const uint16_t *scan_intervals, - const uint16_t *scan_windows, - const uint16_t *connection_intervals_min, - const uint16_t *connection_intervals_max, - const uint16_t *connection_latencies, - const uint16_t *supervision_timeouts, - const uint16_t *minimum_connection_event_lengths, - const uint16_t *maximum_connection_event_lengths - ) final; - - void when_gap_event_received(mbed::Callback cb) final; - - /** - * Sets the event handler that us called by the PAL porters to notify the stack of events - * which will in turn be passed onto the user application when appropriate. - * - * @param[in] event_handler the new event handler interface implementation. Memory - * owned by caller who is responsible for updating this pointer if interface changes. - */ - void set_event_handler(PalGapEventHandler *event_handler) final; - - PalGapEventHandler *get_event_handler() final; - -private: - /** - * Called this function whenever the LE subsystem - * generate a PalGap event. - * - * @param gap_event The event to emit to higher layer. - */ - void emit_gap_event(const GapEvent &gap_event) - { - if (_gap_event_cb) { - _gap_event_cb(gap_event); - } - } - - /** - * T shall define a can_convert and convert function and a type - */ - template - static bool event_handler(const wsfMsgHdr_t *msg); - - /** - * Traits defining can_convert for events. - */ - template - struct MessageConverter { - static bool can_convert(const wsfMsgHdr_t *msg) - { - if (msg->event == EventID) { - return true; - } - return false; - } - }; - - struct ConnectionCompleteMessageConverter final : public MessageConverter { - typedef hciLeConnCmplEvt_t type; - - static GapConnectionCompleteEvent convert(const hciLeConnCmplEvt_t *conn_evt) - { - return GapConnectionCompleteEvent( - conn_evt->status, - // note the usage of the stack handle, not the HCI handle - conn_evt->hdr.param, - (connection_role_t::type) conn_evt->role, - (peer_address_type_t::type) conn_evt->addrType, - conn_evt->peerAddr, - conn_evt->connInterval, - conn_evt->connLatency, - conn_evt->supTimeout, - conn_evt->localRpa, - conn_evt->peerRpa - ); - } - }; - - struct GapAdvertisingReportMessageConverter final : public MessageConverter { - typedef hciLeAdvReportEvt_t type; - - struct CordioGapAdvertisingReportEvent final : public GapAdvertisingReportEvent { - CordioGapAdvertisingReportEvent(const advertising_t &advertising) : - GapAdvertisingReportEvent(), advertising(advertising) - { - } - - uint8_t size() const final - { - return 1; - } - - advertising_t operator[](uint8_t i) const final - { - return advertising; - } - - advertising_t advertising; - }; - - static CordioGapAdvertisingReportEvent convert(const hciLeAdvReportEvt_t *scan_report) - { - GapAdvertisingReportEvent::advertising_t advertising = { - (received_advertising_type_t::type) scan_report->eventType, - (connection_peer_address_type_t::type) scan_report->addrType, - scan_report->addr, - make_const_Span(scan_report->pData, scan_report->len), - scan_report->rssi - }; - return CordioGapAdvertisingReportEvent(advertising); - } - }; - - struct DisconnectionMessageConverter final : public MessageConverter { - typedef hciDisconnectCmplEvt_t type; - - static GapDisconnectionCompleteEvent convert(const hciDisconnectCmplEvt_t *disc_evt) - { - return GapDisconnectionCompleteEvent( - disc_evt->status, - // note the usage of the stack handle, not the HCI handle - disc_evt->hdr.param, - disc_evt->reason - ); - } - }; - - struct ConnectionUpdateMessageConverter final : public MessageConverter { - typedef hciLeConnUpdateCmplEvt_t type; - - static GapConnectionUpdateEvent convert(const hciLeConnUpdateCmplEvt_t *evt) - { - return GapConnectionUpdateEvent( - evt->status, - evt->hdr.param, - evt->connInterval, - evt->connLatency, - evt->supTimeout - ); - } - }; - - struct RemoteConnectionParameterRequestMessageConverter final : public MessageConverter { - typedef hciLeRemConnParamReqEvt_t type; - - static GapRemoteConnectionParameterRequestEvent convert(const hciLeRemConnParamReqEvt_t *evt) - { - return GapRemoteConnectionParameterRequestEvent( - evt->hdr.param, - evt->intervalMin, - evt->intervalMax, - evt->latency, - evt->timeout - ); - } - }; - - /* Cordio requires the call to DmConnAccept to properly catch direct advertising timeout. - * This call returns a connection ID and expect the peer address and the peer address type. - * This PAL following HCI expectations, these information are not present when the higher - * level calls the function to enable advertising. - * This data structure caches these informations and use them in case of direct advertising. - * Similarly, direct advertising timeout doesn't trigger an advertising timeout instead it - * triggers a disconnection with the status 60. Information on running direct advertising - * is stored here. - */ - struct direct_adv_cb_t { - direct_adv_cb_t() : - peer_address_type(advertising_peer_address_type_t::PUBLIC), - state(free) - { - } - - address_t peer_address; - advertising_peer_address_type_t peer_address_type; - advertising_handle_t advertising_handle; - connection_handle_t connection_handle; - bool low_duty_cycle; - enum { - free = 0, - pending, - running - } state; - }; - - /* Predicate expect a const reference to a direct_adv_cb_t - * It must returns true if the criteria are met and false otherwise. */ - template - direct_adv_cb_t *get_adv_cb(const Predicate &predicate); - - direct_adv_cb_t *get_running_direct_adv_cb(advertising_handle_t adv_handle); - - direct_adv_cb_t *get_running_conn_direct_adv_cb(connection_handle_t adv_handle); - - direct_adv_cb_t *get_pending_direct_adv_cb(advertising_handle_t adv_handle); - - direct_adv_cb_t *get_free_direct_adv_cb(); - - ble_error_t update_direct_advertising_parameters( - advertising_handle_t advertising_handle, - uint8_t advertising_type, - address_t peer_address, - advertising_peer_address_type_t peer_address_type - ); - - /** - * Create an ALL_PHYS parameter used in LE Set PHY Command - * and LE Set Default PHY Command. - * @see BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E - 7.8.49 - */ - static uint8_t create_all_phys_value( - const phy_set_t &tx_phys, - const phy_set_t &rx_phys - ) - { - /* if phy set is empty set corresponding all_phys bit to 1 */ - uint8_t all_phys = 0; - if (tx_phys.value() == 0) { - all_phys |= 0x01; - } - if (rx_phys.value() == 0) { - all_phys |= 0x02; - } - return all_phys; - } - -private: - PalGapEventHandler *_pal_event_handler; - address_t device_random_address; - bool use_active_scanning; - uint8_t extended_scan_type[3]; - phy_set_t scanning_phys; - direct_adv_cb_t direct_adv_cb[DM_NUM_ADV_SETS]; - - /** - * Callback called when an event is emitted by the LE subsystem. - */ - mbed::Callback _gap_event_cb; -}; - -} // namespace impl -} // ble - -#endif /* IMPL_PAL_GAP_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGenericAccessServiceImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGenericAccessServiceImpl.h deleted file mode 100644 index 86b3531..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalGenericAccessServiceImpl.h +++ /dev/null @@ -1,67 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IMPL_PAL_GENERIC_ACCESS_SERVICE_ -#define IMPL_PAL_GENERIC_ACCESS_SERVICE_ - -#include "source/pal/PalGenericAccessService.h" -#include "ble/Gap.h" - -namespace ble { -namespace impl { -class GattServer; -} - -namespace impl { -/** - * Implementation of ble::PalGenericAccessService for the Cordio stack. - */ -class PalGenericAccessService final : public ble::PalGenericAccessService { -public: - PalGenericAccessService() = default; - - ~PalGenericAccessService() = default; - -#if 0 // Disabled until reworked and reintroduced to GattServer API - virtual ble_error_t get_device_name_length(uint8_t& length); - virtual ble_error_t get_device_name(Span& array); - virtual ble_error_t set_device_name(const uint8_t* device_name); - virtual ble_error_t get_appearance(GapAdvertisingData::Appearance& appearance); - virtual ble_error_t set_appearance(GapAdvertisingData::Appearance appearance); -#endif // Disabled until reworked and reintroduced to GattServer API - - ble_error_t get_peripheral_preferred_connection_parameters( - ble::Gap::PreferredConnectionParams_t ¶meters - ) final; - - ble_error_t set_peripheral_preferred_connection_parameters( - const ble::Gap::PreferredConnectionParams_t ¶meters - ) final; - -private: -#if BLE_FEATURE_GATT_SERVER - - ble::impl::GattServer &gatt_server(); - -#endif // BLE_FEATURE_GATT_SERVER -}; - -} // namespace impl -} // namespace ble - -#endif /* IMPL_PAL_GENERIC_ACCESS_SERVICE_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalSecurityManagerImpl.h b/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalSecurityManagerImpl.h deleted file mode 100644 index 95cca71..0000000 --- a/connectivity/FEATURE_BLE/source/cordio/include/ble/internal/PalSecurityManagerImpl.h +++ /dev/null @@ -1,400 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2020 ARM Limited - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IMPL_PAL_SECURITY_MANAGER_ -#define IMPL_PAL_SECURITY_MANAGER_ - -#include "source/pal/PalSecurityManager.h" -#include "ble/common/blecommon.h" -#include "wsf_types.h" -#include "wsf_os.h" -#include "sec_api.h" -#include "smp_defs.h" -#include "cfg_stack.h" - -namespace ble { -namespace impl { - -class BLEInstanceBase; - -class PalSecurityManager final : public ble::PalSecurityManager { - friend BLEInstanceBase; -public: - - PalSecurityManager(); - - ~PalSecurityManager(); - - //////////////////////////////////////////////////////////////////////////// - // SM lifecycle management - // - - /** - * @see ::ble::PalSecurityManager::initialize - */ - ble_error_t initialize() final; - - /** - * @see ::ble::PalSecurityManager::terminate - */ - ble_error_t terminate() final; - - /** - * @see ::ble::PalSecurityManager::reset - */ - ble_error_t reset() final; - - //////////////////////////////////////////////////////////////////////////// - // Resolving list management - // - - /** - * @see ::ble::PalSecurityManager::read_resolving_list_capacity - */ - uint8_t read_resolving_list_capacity() final; - - /** - * @see ::ble::PalSecurityManager::add_device_to_resolving_list - */ - ble_error_t add_device_to_resolving_list( - advertising_peer_address_type_t peer_identity_address_type, - const address_t &peer_identity_address, - const irk_t &peer_irk - ) final; - - /** - * @see ::ble::PalSecurityManager::remove_device_from_resolving_list - */ - ble_error_t remove_device_from_resolving_list( - advertising_peer_address_type_t peer_identity_address_type, - const address_t &peer_identity_address - ) final; - - /** - * @see ::ble::PalSecurityManager::clear_resolving_list - */ - ble_error_t clear_resolving_list() final; - - //////////////////////////////////////////////////////////////////////////// - // Pairing - // - - /** - * @see ::ble::PalSecurityManager::send_pairing_request - */ - ble_error_t send_pairing_request( - connection_handle_t connection, - bool oob_data_flag, - AuthenticationMask authentication_requirements, - KeyDistribution initiator_dist, - KeyDistribution responder_dist - ) final; - - /** - * @see ::ble::PalSecurityManager::send_pairing_response - */ - ble_error_t send_pairing_response( - connection_handle_t connection, - bool oob_data_flag, - AuthenticationMask authentication_requirements, - KeyDistribution initiator_dist, - KeyDistribution responder_dist - ) final; - - /** - * @see ::ble::PalSecurityManager::cancel_pairing - */ - ble_error_t cancel_pairing( - connection_handle_t connection, pairing_failure_t reason - ) final; - - //////////////////////////////////////////////////////////////////////////// - // Feature support - // - - /** - * @see ::ble::PalSecurityManager::get_secure_connections_support - */ - ble_error_t get_secure_connections_support( - bool &enabled - ) final; - - /** - * @see ::ble::PalSecurityManager::set_io_capability - */ - ble_error_t set_io_capability(io_capability_t io_capability) final; - - //////////////////////////////////////////////////////////////////////////// - // Security settings - // - - /** - * @see ::ble::PalSecurityManager::set_authentication_timeout - */ - ble_error_t set_authentication_timeout( - connection_handle_t, uint16_t timeout_in_10ms - ) final; - - /** - * @see ::ble::PalSecurityManager::get_authentication_timeout - */ - ble_error_t get_authentication_timeout( - connection_handle_t, uint16_t &timeout_in_10ms - ) final; - - /** - * @see ::ble::PalSecurityManager::set_encryption_key_requirements - */ - ble_error_t set_encryption_key_requirements( - uint8_t min_encryption_key_size, - uint8_t max_encryption_key_size - ) final; - - /** - * @see ::ble::PalSecurityManager::slave_security_request - */ - ble_error_t slave_security_request( - connection_handle_t connection, - AuthenticationMask authentication - ) final; - - //////////////////////////////////////////////////////////////////////////// - // Encryption - // - - /** - * @see ::ble::PalSecurityManager::enable_encryption - */ - ble_error_t enable_encryption( - connection_handle_t connection, - const ltk_t <k, - const rand_t &rand, - const ediv_t &ediv, - bool mitm - ) final; - - /** - * @see ::ble::PalSecurityManager::enable_encryption - */ - ble_error_t enable_encryption( - connection_handle_t connection, - const ltk_t <k, - bool mitm - ) final; - - /** - * @see ::ble::PalSecurityManager::encrypt_data - */ - ble_error_t encrypt_data( - const byte_array_t<16> &key, - encryption_block_t &data - ) final; - - //////////////////////////////////////////////////////////////////////////// - // Privacy - // - - /** - * @see ::ble::PalSecurityManager::set_private_address_timeout - */ - ble_error_t set_private_address_timeout(uint16_t timeout_in_seconds) final; - - /** - * @see ::ble::PalSecurityManager::get_identity_address - */ - ble_error_t get_identity_address(address_t &address, bool &public_address) final; - - //////////////////////////////////////////////////////////////////////////// - // Keys - // - - /** - * @see ::ble::PalSecurityManager::set_ltk - */ - ble_error_t set_ltk( - connection_handle_t connection, - const ltk_t <k, - bool mitm, - bool secure_connections - ) final; - - /** - * @see ::ble::PalSecurityManager::set_ltk_not_found - */ - ble_error_t set_ltk_not_found( - connection_handle_t connection - ) final; - - /** - * @see ::ble::PalSecurityManager::set_irk - */ - ble_error_t set_irk(const irk_t &irk) final; - - /** - * @see ::ble::PalSecurityManager::set_csrk - */ - ble_error_t set_csrk( - const csrk_t &csrk, - sign_count_t sign_counter - ) final; - - /** - * @see ::ble::PalSecurityManager::set_peer_csrk - */ - ble_error_t set_peer_csrk( - connection_handle_t connection, - const csrk_t &csrk, - bool authenticated, - sign_count_t sign_counter - ) final; - - ble_error_t remove_peer_csrk(connection_handle_t connection) final; - - //////////////////////////////////////////////////////////////////////////// - // Authentication - // - - /** - * @see ::ble::PalSecurityManager::get_random_data - */ - ble_error_t get_random_data(byte_array_t<8> &random_data) final; - - //////////////////////////////////////////////////////////////////////////// - // MITM - // - - /** - * @see ::ble::PalSecurityManager::set_display_passkey - */ - ble_error_t set_display_passkey(passkey_num_t passkey) final; - - /** - * @see ::ble::PalSecurityManager::passkey_request_reply - */ - ble_error_t passkey_request_reply( - connection_handle_t connection, - passkey_num_t passkey - ) final; - - /** - * @see ::ble::PalSecurityManager::secure_connections_oob_request_reply - */ - ble_error_t secure_connections_oob_request_reply( - connection_handle_t connection, - const oob_lesc_value_t &local_random, - const oob_lesc_value_t &peer_random, - const oob_confirm_t &peer_confirm - ) final; - - /** - * @see ::ble::PalSecurityManager::legacy_pairing_oob_request_reply - */ - ble_error_t legacy_pairing_oob_request_reply( - connection_handle_t connection, - const oob_tk_t &oob_data - ) final; - - /** - * @see ::ble::PalSecurityManager::confirmation_entered - */ - ble_error_t confirmation_entered( - connection_handle_t connection, bool confirmation - ) final; - - /** - * @see ::ble::PalSecurityManager::send_keypress_notification - */ - ble_error_t send_keypress_notification( - connection_handle_t connection, ble::Keypress_t keypress - ) final; - - /** - * @see ::ble::PalSecurityManager::generate_secure_connections_oob - */ - ble_error_t generate_secure_connections_oob() final; - - /** - * @see ::ble::PalSecurityManager::set_event_handler - */ - void set_event_handler(PalSecurityManagerEventHandler *event_handler) final; - - /** - * @see ::ble::PalSecurityManager::get_event_handler - */ - PalSecurityManagerEventHandler *get_event_handler() final; - -public: - /* used by the PAL to get the singleton */ - static PalSecurityManager &get_security_manager(); - - /* used by PAL to handle security messages coming from the stack Event handler */ - static bool sm_handler(const wsfMsgHdr_t *msg); - -private: - - struct PrivacyControlBlock; - struct PrivacyClearResListControlBlock; - struct PrivacyAddDevToResListControlBlock; - struct PrivacyRemoveDevFromResListControlBlock; - - // Queue control block to add device to resolving list - void queue_add_device_to_resolving_list( - advertising_peer_address_type_t peer_identity_address_type, - const address_t &peer_identity_address, - const irk_t &peer_irk - ); - - // Queue control block to remove device from resolving list - void queue_remove_device_from_resolving_list( - advertising_peer_address_type_t peer_identity_address_type, - const address_t &peer_identity_address - ); - - // Queue control block to clear resolving list - void queue_clear_resolving_list(); - - // Clear all control blocks - void clear_privacy_control_blocks(); - - // Queue a control block - void queue_privacy_control_block(PrivacyControlBlock *block); - - // Try to dequeue and process the next control block - // cb_completed is set when the previous block has completed - void process_privacy_control_blocks(bool cb_completed); - - void cleanup_peer_csrks(); - - PalSecurityManagerEventHandler *_pal_event_handler; - - bool _use_default_passkey; - passkey_num_t _default_passkey; - bool _lesc_keys_generated; - uint8_t _public_key_x[SEC_ECC_KEY_LEN]; - - PrivacyControlBlock *_pending_privacy_control_blocks; - bool _processing_privacy_control_block; - irk_t _irk; - csrk_t _csrk; - csrk_t *_peer_csrks[DM_CONN_MAX]; -}; - -} // namespace impl -} // namespace ble - -#endif /* IMPL_PAL_SECURITY_MANAGER_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp index 5dc06f2..0e16c67 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.cpp @@ -17,9 +17,26 @@ */ #include "mbed.h" -#include "us_ticker_api.h" +#include "platform/CriticalSectionLock.h" +#include "hal/us_ticker_api.h" +#include "platform/mbed_assert.h" + #include "ble/BLE.h" -#include "CriticalSectionLock.h" +#include "ble/driver/CordioHCIDriver.h" + +#include "source/pal/PalAttClient.h" +#include "source/pal/PalSecurityManager.h" +#include "source/pal/PalGap.h" +#include "source/pal/PalSigningMonitor.h" +#include "source/pal/PalAttClientToGattClient.h" + +#include "source/BLEInstanceBaseImpl.h" +#include "source/GattServerImpl.h" +#include "source/PalSecurityManagerImpl.h" +#include "source/PalAttClientImpl.h" +#include "source/PalGenericAccessServiceImpl.h" +#include "source/PalGapImpl.h" + #include "wsf_types.h" #include "wsf_msg.h" #include "wsf_os.h" @@ -34,24 +51,8 @@ #include "att_api.h" #include "smp_api.h" #include "hci_drv.h" -#include "mbed_assert.h" #include "bstream.h" -#include "source/pal/PalAttClient.h" -#include "source/pal/PalSecurityManager.h" -#include "source/pal/PalGap.h" -#include "source/pal/PalSigningMonitor.h" -#include "source/pal/PalAttClientToGattClient.h" -#include "source/BLEInstanceBase.h" -#include "ble/driver/CordioHCIDriver.h" -#include "GattServerImpl.h" -#include "PalSecurityManagerImpl.h" - -#include "internal/PalAttClientImpl.h" -#include "internal/PalGenericAccessServiceImpl.h" -#include "PalGapImpl.h" -#include "internal/BLEInstanceBaseImpl.h" - using namespace std::chrono; @@ -123,9 +124,7 @@ stack_setup(); } -BLEInstanceBase::~BLEInstanceBase() -{ -} +BLEInstanceBase::~BLEInstanceBase() = default; /** * The singleton which represents the BLE transport for the BLE. @@ -217,7 +216,7 @@ const ble::Gap &BLEInstanceBase::getGap() const { - BLEInstanceBase &self = const_cast(*this); + auto &self = const_cast(*this); return const_cast(self.getGap()); }; @@ -237,7 +236,7 @@ const ble::GattServer &BLEInstanceBase::getGattServer() const { - BLEInstanceBase &self = const_cast(*this); + auto &self = const_cast(*this); return const_cast(self.getGattServer()); } @@ -273,8 +272,8 @@ { // Creation of a proxy monitor to let the security manager register to // the gatt client and gatt server. - static struct : PalSigningMonitor { - void set_signing_event_handler(PalSigningMonitorEventHandler *handler) + static struct : ble::PalSigningMonitor { + void set_signing_event_handler(PalSigningMonitorEventHandler *handler) final { #if BLE_FEATURE_GATT_CLIENT BLEInstanceBase::deviceInstance().getGattClientImpl().set_signing_event_handler(handler); diff --git a/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.h new file mode 100644 index 0000000..b91e9e3 --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/BLEInstanceBaseImpl.h @@ -0,0 +1,205 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMPL_BLE_INSTANCE_BASE_H_ +#define IMPL_BLE_INSTANCE_BASE_H_ + +#include "ble/BLE.h" + +#include "ble/GattServer.h" +#include "ble/GattClient.h" +#include "ble/Gap.h" +#include "ble/SecurityManager.h" + +#include "ble/common/blecommon.h" +#include "ble/driver/CordioHCIDriver.h" + +#include "source/BLEInstanceBase.h" +#include "source/pal/PalAttClient.h" +#include "source/pal/PalGattClient.h" +#include "source/pal/PalGap.h" +#include "source/pal/PalGenericAccessService.h" +#include "source/pal/PalEventQueue.h" +#include "source/pal/PalSecurityManager.h" + +#include "source/generic/GapImpl.h" +#include "source/generic/GattClientImpl.h" +#include "source/generic/SecurityManagerImpl.h" +#include "source/GattServerImpl.h" +#include "source/PalEventQueueImpl.h" + +#include "drivers/LowPowerTimer.h" + +namespace ble { + +class PalSigningMonitor; + +namespace impl { + +/** + * @see BLEInstanceBase + */ +class BLEInstanceBase final : public ble::BLEInstanceBase { + friend PalSigningMonitor; + + /** + * Construction with an HCI driver. + */ + BLEInstanceBase(CordioHCIDriver &hci_driver); + + /** + * Destructor + */ + virtual ~BLEInstanceBase(); + +public: + + /** + * Access to the singleton containing the implementation of BLEInstanceBase + * for the Cordio stack. + */ + static BLEInstanceBase &deviceInstance(); + + /** + * @see BLEInstanceBase::init + */ + ble_error_t init( + FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> initCallback + ) final; + + /** + * @see BLEInstanceBase::hasInitialized + */ + bool hasInitialized() const final; + + /** + * @see BLEInstanceBase::shutdown + */ + ble_error_t shutdown() final; + + /** + * @see BLEInstanceBase::getVersion + */ + const char *getVersion() final; + + ble::impl::Gap &getGapImpl(); + + /** + * @see BLEInstanceBase::getGap + */ + ble::Gap &getGap() final; + + /** + * @see BLEInstanceBase::getGap + */ + const ble::Gap &getGap() const final; + +#if BLE_FEATURE_GATT_SERVER + + ble::impl::GattServer &getGattServerImpl(); + + /** + * @see BLEInstanceBase::getGattServer + */ + ble::GattServer &getGattServer() final; + + /** + * @see BLEInstanceBase::getGattServer + */ + const ble::GattServer &getGattServer() const final; + +#endif // BLE_FEATURE_GATT_SERVER + +#if BLE_FEATURE_GATT_CLIENT + + ble::impl::GattClient &getGattClientImpl(); + + /** + * @see BLEInstanceBase::getGattClient + */ + ble::GattClient &getGattClient() final; + + /** + * Get the PAL Gatt Client. + * + * @return PAL Gatt Client. + */ + PalGattClient &getPalGattClient(); + +#endif // BLE_FEATURE_GATT_CLIENT + +#if BLE_FEATURE_SECURITY + + ble::impl::SecurityManager &getSecurityManagerImpl(); + + /** + * @see BLEInstanceBase::getSecurityManager + */ + ble::SecurityManager &getSecurityManager() final; + + /** + * @see BLEInstanceBase::getSecurityManager + */ + const ble::SecurityManager &getSecurityManager() const final; + +#endif // BLE_FEATURE_SECURITY + + /** + * @see BLEInstanceBase::waitForEvent + */ + void waitForEvent(); + + /** + * @see BLEInstanceBase::processEvents + */ + void processEvents() final; + +private: + static void stack_handler(wsfEventMask_t event, wsfMsgHdr_t *msg); + + static void device_manager_cb(dmEvt_t *dm_event); + + static void connection_handler(dmEvt_t *dm_event); + + static void timeoutCallback(); + + void stack_setup(); + + void start_stack_reset(); + + void callDispatcher(); + + static CordioHCIDriver *_hci_driver; + static FunctionPointerWithContext<::BLE::InitializationCompleteCallbackContext *> _init_callback; + + enum { + NOT_INITIALIZED, + INITIALIZING, + INITIALIZED + } initialization_status; + + mutable ble::impl::PalEventQueue _event_queue; + mbed::LowPowerTimer _timer; + uint64_t _last_update_us; +}; + +} // namespace impl +} // namespace ble + + +#endif /* IMPL_BLE_INSTANCE_BASE_H_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp index 434ee52..8cb7d4b 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.cpp @@ -18,8 +18,8 @@ #include "ble/common/BLERoles.h" #include -#include "GattServerImpl.h" -#include "BLEInstanceBaseImpl.h" +#include "source/GattServerImpl.h" +#include "source/BLEInstanceBaseImpl.h" #include "wsf_types.h" #include "att_api.h" diff --git a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h index ad91e42..32d4400 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/GattServerImpl.h @@ -25,7 +25,6 @@ #include "wsf_types.h" #include "att_api.h" - #include "ble/GattServer.h" #include "ble/Gap.h" #include "ble/SecurityManager.h" @@ -38,7 +37,6 @@ #include "ble/common/CallChainOfFunctionPointersWithContext.h" #include "ble/common/blecommon.h" - #include "source/BLEInstanceBase.h" #include "source/generic/GattServerEvents.h" #include "source/pal/PalSigningMonitor.h" diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.cpp index 817535f..35c299d 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.cpp @@ -17,11 +17,12 @@ */ #include "source/pal/PalAttClient.h" -#include "GattServerImpl.h" #include "source/pal/PalSimpleAttServerMessage.h" #include "source/pal/PalGattClient.h" -#include "BLEInstanceBaseImpl.h" -#include "internal/PalAttClientImpl.h" + +#include "source/GattServerImpl.h" +#include "source/BLEInstanceBaseImpl.h" +#include "source/PalAttClientImpl.h" #include "att_api.h" #include "att_defs.h" diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.h new file mode 100644 index 0000000..02f6923 --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.h @@ -0,0 +1,481 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMPL_PAL_ATT_CLIENT_ +#define IMPL_PAL_ATT_CLIENT_ + +#include "source/pal/PalSimpleAttServerMessage.h" +#include "source/pal/PalAttClient.h" + +#include "att_api.h" + +namespace ble { +namespace impl { + +class PalAttClient final : public ble::PalAttClient { + +public: + PalAttClient(); + + ~PalAttClient(); + + /** + * @see ble::PalAttClient::exchange_mtu_request + */ + ble_error_t exchange_mtu_request(connection_handle_t connection) final; + + /** + * @see ble::PalGattClient::get_mtu_size + */ + ble_error_t get_mtu_size( + connection_handle_t connection_handle, + uint16_t &mtu_size + ) final; + + /** + * @see ble::PalAttClient::find_information_request + */ + ble_error_t find_information_request( + connection_handle_t connection_handle, + attribute_handle_range_t discovery_range + ) final; + + /** + * @see ble::PalAttClient::find_by_type_value_request + */ + ble_error_t find_by_type_value_request( + connection_handle_t connection_handle, + attribute_handle_range_t discovery_range, + uint16_t type, + const Span &value + ) final; + + /** + * @see ble::PalAttClient::read_by_type_request + */ + ble_error_t read_by_type_request( + connection_handle_t connection_handle, + attribute_handle_range_t read_range, + const UUID &type + ) final; + + /** + * @see ble::PalAttClient::read_request + */ + ble_error_t read_request( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle + ) final; + + /** + * @see ble::PalAttClient::read_blob_request + */ + ble_error_t read_blob_request( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle, + uint16_t offset + ) final; + + /** + * @see ble::PalAttClient::read_multiple_request + */ + ble_error_t read_multiple_request( + connection_handle_t connection_handle, + const Span &attribute_handles + ) final; + + /** + * @see ble::PalAttClient::read_by_group_type_request + */ + ble_error_t read_by_group_type_request( + connection_handle_t connection_handle, + attribute_handle_range_t read_range, + const UUID &group_type + ) final; + + /** + * @see ble::PalAttClient::write_request + */ + ble_error_t write_request( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle, + const Span &value + ) final; + + /** + * @see ble::PalAttClient::write_command + */ + ble_error_t write_command( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle, + const Span &value + ) final; + + /** + * @see ble::PalAttClient::signed_write_command + */ + ble_error_t signed_write_command( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle, + const Span &value + ) final; + + /** + * Initialises the counter used to sign messages. Counter will be incremented every + * time a message is signed. + * + * @param sign_counter initialise the signing counter to this value + */ + void set_sign_counter( + sign_count_t sign_counter + ); + + /** + * @see ble::PalAttClient::prepare_write_request + */ + ble_error_t prepare_write_request( + connection_handle_t connection_handle, + attribute_handle_t attribute_handle, + uint16_t offset, + const Span &value + ) final; + + /** + * @see ble::PalAttClient::execute_write_request + */ + ble_error_t execute_write_request( + connection_handle_t connection_handle, + bool execute + ) final; + + /** + * @see ble::PalAttClient::initialize + */ + ble_error_t initialize() final; + + /** + * @see ble::PalAttClient::terminate + */ + ble_error_t terminate() final; + + // singleton of the ARM Cordio client + static PalAttClient &get_client(); + + void when_server_message_received( + mbed::Callback cb + ) final; + + void when_transaction_timeout( + mbed::Callback cb + ) final; + +private: + // convert an array of byte to an uint16_t + static uint16_t to_uint16_t(const uint8_t *array); + + /** + * Type of an event handler. + * @param The event to handle + * @return true if the event has been handled and false otherwise. + */ + typedef bool (*event_handler_t)(const attEvt_t *); + +public: + /** + * Callback which handle attEvt_t and forward them to on_server_event. + */ + static void att_client_handler(const attEvt_t *event); + +private: + /** + * Event handler generator. + * @tparam Description of an event converter. It contains two static member + * function: + * - bool can_convert(const attEvt_t* event): return true if the event can + * be converted by the converter + * - convert(const attEvt_t* event): return the + * AttServerMessage converted from event. + * @param event + * @return + */ + template + static bool event_handler(const attEvt_t *event); + + static bool timeout_event_handler(const attEvt_t *event); + + template + static void generated_handler( + const attEvt_t *event, ResultType (*convert)(const attEvt_t *) + ); + + /** + * Traits defining can_convert for non ErrorResponse events. + */ + template + struct ResponseConverter { + static bool can_convert(const attEvt_t *event) + { + if (event->hdr.status == ATT_SUCCESS && event->hdr.event == RequestID) { + return true; + } + return false; + } + }; + + /** + * Converter for an AttErrorResponse. + */ + struct ErrorResponseConverter { + static bool can_convert(const attEvt_t *event) + { + if (event->hdr.status != ATT_SUCCESS) { + return true; + } + return false; + } + + static AttErrorResponse convert(const attEvt_t *event) + { + return AttErrorResponse( + static_cast(event->hdr.event * 2), + event->handle, + event->hdr.status + ); + } + }; + + /** + * Converter for a PalSimpleAttFindInformationResponse. + */ + struct FindInformationResponseConverter : ResponseConverter { + static PalSimpleAttFindInformationResponse convert(const attEvt_t *event) + { + return PalSimpleAttFindInformationResponse( + static_cast(event->pValue[0]), + make_const_Span( + event->pValue + 1, + event->valueLen - 1 + ) + ); + } + }; + + /** + * Converter for a PalSimpleAttFindByTypeValueResponse. + */ + struct FindByTypeValueResponseConverter : ResponseConverter { + static PalSimpleAttFindByTypeValueResponse convert(const attEvt_t *event) + { + return PalSimpleAttFindByTypeValueResponse( + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a PalSimpleAttReadByTypeResponse. + */ + struct ReadByTypeResponseConverter : ResponseConverter { + static PalSimpleAttReadByTypeResponse convert(const attEvt_t *event) + { + return PalSimpleAttReadByTypeResponse( + event->pValue[0], + make_const_Span( + event->pValue + 1, + event->valueLen - 1 + ) + ); + } + }; + + /** + * Converter for a AttReadResponse. + */ + struct ReadResponseConverter : ResponseConverter { + static AttReadResponse convert(const attEvt_t *event) + { + return AttReadResponse( + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a AttReadBlobResponse. + */ + struct ReadBlobResponseConverter : ResponseConverter { + static AttReadBlobResponse convert(const attEvt_t *event) + { + return AttReadBlobResponse( + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a AttReadMultipleResponse. + */ + struct ReadMultipleResponseConverter : ResponseConverter { + static AttReadMultipleResponse convert(const attEvt_t *event) + { + return AttReadMultipleResponse( + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a PalSimpleAttReadByGroupTypeResponse. + */ + struct ReadBygroupTypeResponseConverter : ResponseConverter { + static PalSimpleAttReadByGroupTypeResponse convert(const attEvt_t *event) + { + return PalSimpleAttReadByGroupTypeResponse( + event->pValue[0], + make_const_Span( + event->pValue + 1, + event->valueLen - 1 + ) + ); + } + }; + + /** + * Converter for a AttWriteResponse. + */ + struct WriteResponseConverter : ResponseConverter { + static AttWriteResponse convert(const attEvt_t *event) + { + return AttWriteResponse(); + } + }; + + /** + * Converter for a AttPrepareWriteResponse. + */ + struct PrepareWriteResponseConverter : ResponseConverter { + static AttPrepareWriteResponse convert(const attEvt_t *event) + { + return AttPrepareWriteResponse( + event->handle, + 0, /* offset is lost */ + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a AttExecuteWriteResponse. + */ + struct ExecuteWriteResponseConverter : ResponseConverter { + static AttExecuteWriteResponse convert(const attEvt_t *event) + { + return AttExecuteWriteResponse(); + } + }; + + /** + * Converter for a AttHandleValueNotification. + */ + struct HandleValueNotificationConverter : ResponseConverter { + static AttHandleValueNotification convert(const attEvt_t *event) + { + return AttHandleValueNotification( + event->handle, + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + + /** + * Converter for a AttHandleValueIndication. + */ + struct HandleValueIndicationConverter : ResponseConverter { + static AttHandleValueIndication convert(const attEvt_t *event) + { + return AttHandleValueIndication( + event->handle, + make_const_Span( + event->pValue, + event->valueLen + ) + ); + } + }; + +private: + /** + * Upon server message reception an implementation shall call this function. + * + * @param connection_handle The handle of the connection which has received + * the server message. + * @param server_message The message received from the server. + */ + void on_server_event( + connection_handle_t connection_handle, + const AttServerMessage &server_message + ); + + /** + * Upon transaction timeout an implementation shall call this function. + * + * @param connection_handle The handle of the connection of the transaction + * which has times out. + * + * @note see BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part F Section 3.3.3 + */ + void on_transaction_timeout( + connection_handle_t connection_handle + ); + +private: + sign_count_t _local_sign_counter; + + /** + * Callback called when the client receive a message from the server. + */ + mbed::Callback _server_message_cb; + + /** + * Callback called when a transaction times out. + */ + mbed::Callback _transaction_timeout_cb; +}; + +} // namespace impl +} // namespace ble + +#endif /* IMPL_PAL_ATT_CLIENT_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.cpp index 29c9c8e..e1079df 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.cpp @@ -16,8 +16,8 @@ * limitations under the License. */ -#include "internal/PalEventQueueImpl.h" #include "source/BLEInstanceBase.h" +#include "source/PalEventQueueImpl.h" namespace ble { namespace impl { diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.h new file mode 100644 index 0000000..507b7f0 --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalEventQueueImpl.h @@ -0,0 +1,146 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BLE_PAL_SIMPLE_EVENT_QUEUE_H_ +#define BLE_PAL_SIMPLE_EVENT_QUEUE_H_ + +#include +#include "source/pal/PalEventQueue.h" + +#include "wsf_buf.h" + +namespace ble { + +class BLEInstanceBase; + +namespace impl { + +/** + * Simple implementation of the EventQueue. + */ +class PalEventQueue final : public ble::PalEventQueue { +public: + typedef mbed::Callback event_t; + + /** + * Construct an empty event queue. + * + * @attention a call to initialize is mandatory before any other call. + */ + PalEventQueue() : + _ble_base(nullptr), _events(nullptr) + { + } + + /** + * Initialize the event queue with a BLEInstanceBase and a ble id. + * + * @param ble_base the instance which will be used to signal the presence + * of new events. + */ + void initialize(ble::BLEInstanceBase *ble_base) + { + _ble_base = ble_base; + } + + /** + * @see ble::EventQueue + */ + ~PalEventQueue() + { + clear(); + } + + /** + * @see ble::post + */ + bool post(const mbed::Callback &event) final + { + if (_ble_base == nullptr) { + return false; + } + void *event_buf = WsfBufAlloc(sizeof(EventNode)); + MBED_ASSERT(event_buf != nullptr); + if (event_buf == nullptr) { + return false; + } + auto *next = new(event_buf) EventNode(event); + + if (_events == nullptr) { + _events = next; + } else { + EventNode *previous = _events; + while (previous->next) { + previous = previous->next; + } + + previous->next = next; + } + + signal_event(); + + return true; + } + + /** + * Clear the event queue from all its events. + */ + void clear() + { + while (_events) { + EventNode *next = _events->next; + _events->~EventNode(); + WsfBufFree(_events); + _events = next; + } + } + + /** + * Process the event queue. + */ + void process() + { + while (_events) { + EventNode *next = _events->next; + _events->event(); + _events->~EventNode(); + WsfBufFree(_events); + _events = next; + } + } + +private: + struct EventNode { + EventNode(const event_t event) : event(event), next(nullptr) + { + } + + event_t event; + EventNode *next; + }; + + void signal_event(); + + ble::BLEInstanceBase *_ble_base; + EventNode *_events; +}; + +} // namespace impl +} // namespace ble + +#endif /* BLE_PAL_SIMPLE_EVENT_QUEUE_H_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp index 38c985d..07af679 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "PalGapImpl.h" +#include "source/PalGapImpl.h" #include "hci_api.h" #include "dm_api.h" #include "dm_main.h" diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h new file mode 100644 index 0000000..50829fa --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h @@ -0,0 +1,554 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMPL_PAL_GAP_ +#define IMPL_PAL_GAP_ + +#include "source/pal/PalGap.h" +#include "dm_api.h" + +namespace ble { +namespace impl { + +/** + * Implementation of ble::PalGap for the Cordio stack. + */ +class PalGap final : public ble::PalGap { +public: + PalGap() : _pal_event_handler(nullptr), use_active_scanning(false) + { + }; + + ~PalGap() = default; + + bool is_feature_supported( + ble::controller_supported_features_t feature + ) final; + + ble_error_t initialize() final; + + ble_error_t terminate() final; + + address_t get_device_address() final; + + address_t get_random_address() final; + + ble_error_t set_random_address(const address_t &address) final; + + ble_error_t set_advertising_parameters( + uint16_t advertising_interval_min, + uint16_t advertising_interval_max, + advertising_type_t advertising_type, + own_address_type_t own_address_type, + advertising_peer_address_type_t peer_address_type, + const address_t &peer_address, + advertising_channel_map_t advertising_channel_map, + advertising_filter_policy_t advertising_filter_policy + ) final; + + ble_error_t set_advertising_data( + uint8_t advertising_data_length, + const advertising_data_t &advertising_data + ) final; + + ble_error_t set_scan_response_data( + uint8_t scan_response_data_length, + const advertising_data_t &scan_response_data + ) final; + + ble_error_t advertising_enable(bool enable) final; + + ble_error_t set_scan_parameters( + bool active_scanning, + uint16_t scan_interval, + uint16_t scan_window, + own_address_type_t own_address_type, + scanning_filter_policy_t filter_policy + ) final; + + ble_error_t scan_enable( + bool enable, + bool filter_duplicates + ) final; + + ble_error_t create_connection( + uint16_t scan_interval, + uint16_t scan_window, + initiator_policy_t initiator_policy, + connection_peer_address_type_t peer_address_type, + const address_t &peer_address, + own_address_type_t own_address_type, + uint16_t connection_interval_min, + uint16_t connection_interval_max, + uint16_t connection_latency, + uint16_t supervision_timeout, + uint16_t minimum_connection_event_length, + uint16_t maximum_connection_event_length + ) final; + + ble_error_t cancel_connection_creation(); + + uint8_t read_white_list_capacity() final; + + ble_error_t clear_whitelist() final; + + ble_error_t add_device_to_whitelist( + whitelist_address_type_t address_type, + address_t address + ) final; + + ble_error_t remove_device_from_whitelist( + whitelist_address_type_t address_type, + address_t address + ) final; + + ble_error_t connection_parameters_update( + connection_handle_t connection, + uint16_t connection_interval_min, + uint16_t connection_interval_max, + uint16_t connection_latency, + uint16_t supervision_timeout, + uint16_t minimum_connection_event_length, + uint16_t maximum_connection_event_length + ) final; + + ble_error_t accept_connection_parameter_request( + connection_handle_t connection_handle, + uint16_t interval_min, + uint16_t interval_max, + uint16_t latency, + uint16_t supervision_timeout, + uint16_t minimum_connection_event_length, + uint16_t maximum_connection_event_length + ) final; + + ble_error_t reject_connection_parameter_request( + connection_handle_t connection_handle, + hci_error_code_t rejection_reason + ) final; + + ble_error_t disconnect( + connection_handle_t connection, + local_disconnection_reason_t disconnection_reason + ) final; + + bool is_privacy_supported() final; + + ble_error_t set_address_resolution( + bool enable + ) final; + + ble_error_t read_phy(connection_handle_t connection) final; + + ble_error_t set_preferred_phys( + const phy_set_t &tx_phys, + const phy_set_t &rx_phys + ) final; + + ble_error_t set_phy( + connection_handle_t connection, + const phy_set_t &tx_phys, + const phy_set_t &rx_phys, + coded_symbol_per_bit_t coded_symbol + ) final; + + // singleton of the ARM Cordio client + static PalGap &get_gap(); + + /** + * Callback which handle wsfMsgHdr_t and forward them to emit_gap_event. + */ + static void gap_handler(const wsfMsgHdr_t *msg); + + ble_error_t set_advertising_set_random_address( + advertising_handle_t advertising_handle, + const address_t &address + ) final; + + ble_error_t set_extended_advertising_parameters( + advertising_handle_t advertising_handle, + advertising_event_properties_t event_properties, + advertising_interval_t primary_advertising_interval_min, + advertising_interval_t primary_advertising_interval_max, + advertising_channel_map_t primary_advertising_channel_map, + own_address_type_t own_address_type, + advertising_peer_address_type_t peer_address_type, + const address_t &peer_address, + advertising_filter_policy_t advertising_filter_policy, + advertising_power_t advertising_power, + phy_t primary_advertising_phy, + uint8_t secondary_advertising_max_skip, + phy_t secondary_phy, + uint8_t advertising_sid, + bool scan_request_notification + ) final; + + ble_error_t set_periodic_advertising_parameters( + advertising_handle_t advertising_handle, + periodic_advertising_interval_t periodic_advertising_min, + periodic_advertising_interval_t periodic_advertising_max, + bool advertise_power + ) final; + + ble_error_t set_extended_advertising_data( + advertising_handle_t advertising_handle, + advertising_fragment_description_t operation, + bool minimize_fragmentation, + uint8_t advertising_data_size, + const uint8_t *advertising_data + ) final; + + ble_error_t set_periodic_advertising_data( + advertising_handle_t advertising_handle, + advertising_fragment_description_t fragment_description, + uint8_t advertising_data_size, + const uint8_t *advertising_data + ) final; + + ble_error_t set_extended_scan_response_data( + advertising_handle_t advertising_handle, + advertising_fragment_description_t operation, + bool minimize_fragmentation, + uint8_t scan_response_data_size, + const uint8_t *scan_response_data + ) final; + + ble_error_t extended_advertising_enable( + bool enable, + uint8_t number_of_sets, + const advertising_handle_t *handles, + const uint16_t *durations, + const uint8_t *max_extended_advertising_events + ) final; + + ble_error_t periodic_advertising_enable( + bool enable, + advertising_handle_t advertising_handle + ) final; + + uint16_t get_maximum_advertising_data_length() final; + + uint16_t get_maximum_connectable_advertising_data_length() final; + + uint8_t get_maximum_hci_advertising_data_length() final; + + uint8_t get_max_number_of_advertising_sets() final; + + ble_error_t remove_advertising_set( + advertising_handle_t advertising_handle + ) final; + + ble_error_t clear_advertising_sets() final; + + ble_error_t set_extended_scan_parameters( + own_address_type_t own_address_type, + scanning_filter_policy_t filter_policy, + phy_set_t scanning_phys, + const bool *active_scanning, + const uint16_t *scan_interval, + const uint16_t *scan_window + ) final; + + ble_error_t extended_scan_enable( + bool enable, + duplicates_filter_t filter_duplicates, + uint16_t duration, + uint16_t period + ) final; + + ble_error_t periodic_advertising_create_sync( + bool use_periodic_advertiser_list, + uint8_t advertising_sid, + peer_address_type_t peer_address_type, + const address_t &peer_address, + uint16_t allowed_skip, + uint16_t sync_timeout + ) final; + + ble_error_t cancel_periodic_advertising_create_sync() final; + + ble_error_t periodic_advertising_terminate_sync( + sync_handle_t sync_handle + ) final; + + ble_error_t add_device_to_periodic_advertiser_list( + advertising_peer_address_type_t advertiser_address_type, + const address_t &advertiser_address, + uint8_t advertising_sid + ) final; + + ble_error_t remove_device_from_periodic_advertiser_list( + advertising_peer_address_type_t advertiser_address_type, + const address_t &advertiser_address, + uint8_t advertising_sid + ) final; + + ble_error_t clear_periodic_advertiser_list() final; + + uint8_t read_periodic_advertiser_list_size() final; + + ble_error_t extended_create_connection( + initiator_policy_t initiator_policy, + own_address_type_t own_address_type, + peer_address_type_t peer_address_type, + const address_t &peer_address, + phy_set_t initiating_phys, + const uint16_t *scan_intervals, + const uint16_t *scan_windows, + const uint16_t *connection_intervals_min, + const uint16_t *connection_intervals_max, + const uint16_t *connection_latencies, + const uint16_t *supervision_timeouts, + const uint16_t *minimum_connection_event_lengths, + const uint16_t *maximum_connection_event_lengths + ) final; + + void when_gap_event_received(mbed::Callback cb) final; + + /** + * Sets the event handler that us called by the PAL porters to notify the stack of events + * which will in turn be passed onto the user application when appropriate. + * + * @param[in] event_handler the new event handler interface implementation. Memory + * owned by caller who is responsible for updating this pointer if interface changes. + */ + void set_event_handler(PalGapEventHandler *event_handler) final; + + PalGapEventHandler *get_event_handler() final; + +private: + /** + * Called this function whenever the LE subsystem + * generate a PalGap event. + * + * @param gap_event The event to emit to higher layer. + */ + void emit_gap_event(const GapEvent &gap_event) + { + if (_gap_event_cb) { + _gap_event_cb(gap_event); + } + } + + /** + * T shall define a can_convert and convert function and a type + */ + template + static bool event_handler(const wsfMsgHdr_t *msg); + + /** + * Traits defining can_convert for events. + */ + template + struct MessageConverter { + static bool can_convert(const wsfMsgHdr_t *msg) + { + if (msg->event == EventID) { + return true; + } + return false; + } + }; + + struct ConnectionCompleteMessageConverter final : public MessageConverter { + typedef hciLeConnCmplEvt_t type; + + static GapConnectionCompleteEvent convert(const hciLeConnCmplEvt_t *conn_evt) + { + return GapConnectionCompleteEvent( + conn_evt->status, + // note the usage of the stack handle, not the HCI handle + conn_evt->hdr.param, + (connection_role_t::type) conn_evt->role, + (peer_address_type_t::type) conn_evt->addrType, + conn_evt->peerAddr, + conn_evt->connInterval, + conn_evt->connLatency, + conn_evt->supTimeout, + conn_evt->localRpa, + conn_evt->peerRpa + ); + } + }; + + struct GapAdvertisingReportMessageConverter final : public MessageConverter { + typedef hciLeAdvReportEvt_t type; + + struct CordioGapAdvertisingReportEvent final : public GapAdvertisingReportEvent { + CordioGapAdvertisingReportEvent(const advertising_t &advertising) : + GapAdvertisingReportEvent(), advertising(advertising) + { + } + + uint8_t size() const final + { + return 1; + } + + advertising_t operator[](uint8_t i) const final + { + return advertising; + } + + advertising_t advertising; + }; + + static CordioGapAdvertisingReportEvent convert(const hciLeAdvReportEvt_t *scan_report) + { + GapAdvertisingReportEvent::advertising_t advertising = { + (received_advertising_type_t::type) scan_report->eventType, + (connection_peer_address_type_t::type) scan_report->addrType, + scan_report->addr, + make_const_Span(scan_report->pData, scan_report->len), + scan_report->rssi + }; + return CordioGapAdvertisingReportEvent(advertising); + } + }; + + struct DisconnectionMessageConverter final : public MessageConverter { + typedef hciDisconnectCmplEvt_t type; + + static GapDisconnectionCompleteEvent convert(const hciDisconnectCmplEvt_t *disc_evt) + { + return GapDisconnectionCompleteEvent( + disc_evt->status, + // note the usage of the stack handle, not the HCI handle + disc_evt->hdr.param, + disc_evt->reason + ); + } + }; + + struct ConnectionUpdateMessageConverter final : public MessageConverter { + typedef hciLeConnUpdateCmplEvt_t type; + + static GapConnectionUpdateEvent convert(const hciLeConnUpdateCmplEvt_t *evt) + { + return GapConnectionUpdateEvent( + evt->status, + evt->hdr.param, + evt->connInterval, + evt->connLatency, + evt->supTimeout + ); + } + }; + + struct RemoteConnectionParameterRequestMessageConverter final : public MessageConverter { + typedef hciLeRemConnParamReqEvt_t type; + + static GapRemoteConnectionParameterRequestEvent convert(const hciLeRemConnParamReqEvt_t *evt) + { + return GapRemoteConnectionParameterRequestEvent( + evt->hdr.param, + evt->intervalMin, + evt->intervalMax, + evt->latency, + evt->timeout + ); + } + }; + + /* Cordio requires the call to DmConnAccept to properly catch direct advertising timeout. + * This call returns a connection ID and expect the peer address and the peer address type. + * This PAL following HCI expectations, these information are not present when the higher + * level calls the function to enable advertising. + * This data structure caches these informations and use them in case of direct advertising. + * Similarly, direct advertising timeout doesn't trigger an advertising timeout instead it + * triggers a disconnection with the status 60. Information on running direct advertising + * is stored here. + */ + struct direct_adv_cb_t { + direct_adv_cb_t() : + peer_address_type(advertising_peer_address_type_t::PUBLIC), + state(free) + { + } + + address_t peer_address; + advertising_peer_address_type_t peer_address_type; + advertising_handle_t advertising_handle; + connection_handle_t connection_handle; + bool low_duty_cycle; + enum { + free = 0, + pending, + running + } state; + }; + + /* Predicate expect a const reference to a direct_adv_cb_t + * It must returns true if the criteria are met and false otherwise. */ + template + direct_adv_cb_t *get_adv_cb(const Predicate &predicate); + + direct_adv_cb_t *get_running_direct_adv_cb(advertising_handle_t adv_handle); + + direct_adv_cb_t *get_running_conn_direct_adv_cb(connection_handle_t adv_handle); + + direct_adv_cb_t *get_pending_direct_adv_cb(advertising_handle_t adv_handle); + + direct_adv_cb_t *get_free_direct_adv_cb(); + + ble_error_t update_direct_advertising_parameters( + advertising_handle_t advertising_handle, + uint8_t advertising_type, + address_t peer_address, + advertising_peer_address_type_t peer_address_type + ); + + /** + * Create an ALL_PHYS parameter used in LE Set PHY Command + * and LE Set Default PHY Command. + * @see BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E - 7.8.49 + */ + static uint8_t create_all_phys_value( + const phy_set_t &tx_phys, + const phy_set_t &rx_phys + ) + { + /* if phy set is empty set corresponding all_phys bit to 1 */ + uint8_t all_phys = 0; + if (tx_phys.value() == 0) { + all_phys |= 0x01; + } + if (rx_phys.value() == 0) { + all_phys |= 0x02; + } + return all_phys; + } + +private: + PalGapEventHandler *_pal_event_handler; + address_t device_random_address; + bool use_active_scanning; + uint8_t extended_scan_type[3]; + phy_set_t scanning_phys; + direct_adv_cb_t direct_adv_cb[DM_NUM_ADV_SETS]; + + /** + * Callback called when an event is emitted by the LE subsystem. + */ + mbed::Callback _gap_event_cb; +}; + +} // namespace impl +} // ble + +#endif /* IMPL_PAL_GAP_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.cpp index abf7cd5..0e2fe68 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.cpp @@ -16,8 +16,8 @@ * limitations under the License. */ -#include "internal/PalGenericAccessServiceImpl.h" -#include "GattServerImpl.h" +#include "source/PalGenericAccessServiceImpl.h" +#include "source/GattServerImpl.h" namespace ble { namespace impl { diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.h new file mode 100644 index 0000000..86b3531 --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGenericAccessServiceImpl.h @@ -0,0 +1,67 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMPL_PAL_GENERIC_ACCESS_SERVICE_ +#define IMPL_PAL_GENERIC_ACCESS_SERVICE_ + +#include "source/pal/PalGenericAccessService.h" +#include "ble/Gap.h" + +namespace ble { +namespace impl { +class GattServer; +} + +namespace impl { +/** + * Implementation of ble::PalGenericAccessService for the Cordio stack. + */ +class PalGenericAccessService final : public ble::PalGenericAccessService { +public: + PalGenericAccessService() = default; + + ~PalGenericAccessService() = default; + +#if 0 // Disabled until reworked and reintroduced to GattServer API + virtual ble_error_t get_device_name_length(uint8_t& length); + virtual ble_error_t get_device_name(Span& array); + virtual ble_error_t set_device_name(const uint8_t* device_name); + virtual ble_error_t get_appearance(GapAdvertisingData::Appearance& appearance); + virtual ble_error_t set_appearance(GapAdvertisingData::Appearance appearance); +#endif // Disabled until reworked and reintroduced to GattServer API + + ble_error_t get_peripheral_preferred_connection_parameters( + ble::Gap::PreferredConnectionParams_t ¶meters + ) final; + + ble_error_t set_peripheral_preferred_connection_parameters( + const ble::Gap::PreferredConnectionParams_t ¶meters + ) final; + +private: +#if BLE_FEATURE_GATT_SERVER + + ble::impl::GattServer &gatt_server(); + +#endif // BLE_FEATURE_GATT_SERVER +}; + +} // namespace impl +} // namespace ble + +#endif /* IMPL_PAL_GENERIC_ACCESS_SERVICE_ */ diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp index 59aa223..83094d0 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.cpp @@ -20,8 +20,8 @@ #include "ble/common/BLERoles.h" #include "ble/common/blecommon.h" -#include "internal/PalSecurityManagerImpl.h" -#include "internal/PalAttClientImpl.h" +#include "source/PalSecurityManagerImpl.h" +#include "source/PalAttClientImpl.h" #include "dm_api.h" #include "att_api.h" #include "smp_api.h" diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.h new file mode 100644 index 0000000..95cca71 --- /dev/null +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalSecurityManagerImpl.h @@ -0,0 +1,400 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMPL_PAL_SECURITY_MANAGER_ +#define IMPL_PAL_SECURITY_MANAGER_ + +#include "source/pal/PalSecurityManager.h" +#include "ble/common/blecommon.h" +#include "wsf_types.h" +#include "wsf_os.h" +#include "sec_api.h" +#include "smp_defs.h" +#include "cfg_stack.h" + +namespace ble { +namespace impl { + +class BLEInstanceBase; + +class PalSecurityManager final : public ble::PalSecurityManager { + friend BLEInstanceBase; +public: + + PalSecurityManager(); + + ~PalSecurityManager(); + + //////////////////////////////////////////////////////////////////////////// + // SM lifecycle management + // + + /** + * @see ::ble::PalSecurityManager::initialize + */ + ble_error_t initialize() final; + + /** + * @see ::ble::PalSecurityManager::terminate + */ + ble_error_t terminate() final; + + /** + * @see ::ble::PalSecurityManager::reset + */ + ble_error_t reset() final; + + //////////////////////////////////////////////////////////////////////////// + // Resolving list management + // + + /** + * @see ::ble::PalSecurityManager::read_resolving_list_capacity + */ + uint8_t read_resolving_list_capacity() final; + + /** + * @see ::ble::PalSecurityManager::add_device_to_resolving_list + */ + ble_error_t add_device_to_resolving_list( + advertising_peer_address_type_t peer_identity_address_type, + const address_t &peer_identity_address, + const irk_t &peer_irk + ) final; + + /** + * @see ::ble::PalSecurityManager::remove_device_from_resolving_list + */ + ble_error_t remove_device_from_resolving_list( + advertising_peer_address_type_t peer_identity_address_type, + const address_t &peer_identity_address + ) final; + + /** + * @see ::ble::PalSecurityManager::clear_resolving_list + */ + ble_error_t clear_resolving_list() final; + + //////////////////////////////////////////////////////////////////////////// + // Pairing + // + + /** + * @see ::ble::PalSecurityManager::send_pairing_request + */ + ble_error_t send_pairing_request( + connection_handle_t connection, + bool oob_data_flag, + AuthenticationMask authentication_requirements, + KeyDistribution initiator_dist, + KeyDistribution responder_dist + ) final; + + /** + * @see ::ble::PalSecurityManager::send_pairing_response + */ + ble_error_t send_pairing_response( + connection_handle_t connection, + bool oob_data_flag, + AuthenticationMask authentication_requirements, + KeyDistribution initiator_dist, + KeyDistribution responder_dist + ) final; + + /** + * @see ::ble::PalSecurityManager::cancel_pairing + */ + ble_error_t cancel_pairing( + connection_handle_t connection, pairing_failure_t reason + ) final; + + //////////////////////////////////////////////////////////////////////////// + // Feature support + // + + /** + * @see ::ble::PalSecurityManager::get_secure_connections_support + */ + ble_error_t get_secure_connections_support( + bool &enabled + ) final; + + /** + * @see ::ble::PalSecurityManager::set_io_capability + */ + ble_error_t set_io_capability(io_capability_t io_capability) final; + + //////////////////////////////////////////////////////////////////////////// + // Security settings + // + + /** + * @see ::ble::PalSecurityManager::set_authentication_timeout + */ + ble_error_t set_authentication_timeout( + connection_handle_t, uint16_t timeout_in_10ms + ) final; + + /** + * @see ::ble::PalSecurityManager::get_authentication_timeout + */ + ble_error_t get_authentication_timeout( + connection_handle_t, uint16_t &timeout_in_10ms + ) final; + + /** + * @see ::ble::PalSecurityManager::set_encryption_key_requirements + */ + ble_error_t set_encryption_key_requirements( + uint8_t min_encryption_key_size, + uint8_t max_encryption_key_size + ) final; + + /** + * @see ::ble::PalSecurityManager::slave_security_request + */ + ble_error_t slave_security_request( + connection_handle_t connection, + AuthenticationMask authentication + ) final; + + //////////////////////////////////////////////////////////////////////////// + // Encryption + // + + /** + * @see ::ble::PalSecurityManager::enable_encryption + */ + ble_error_t enable_encryption( + connection_handle_t connection, + const ltk_t <k, + const rand_t &rand, + const ediv_t &ediv, + bool mitm + ) final; + + /** + * @see ::ble::PalSecurityManager::enable_encryption + */ + ble_error_t enable_encryption( + connection_handle_t connection, + const ltk_t <k, + bool mitm + ) final; + + /** + * @see ::ble::PalSecurityManager::encrypt_data + */ + ble_error_t encrypt_data( + const byte_array_t<16> &key, + encryption_block_t &data + ) final; + + //////////////////////////////////////////////////////////////////////////// + // Privacy + // + + /** + * @see ::ble::PalSecurityManager::set_private_address_timeout + */ + ble_error_t set_private_address_timeout(uint16_t timeout_in_seconds) final; + + /** + * @see ::ble::PalSecurityManager::get_identity_address + */ + ble_error_t get_identity_address(address_t &address, bool &public_address) final; + + //////////////////////////////////////////////////////////////////////////// + // Keys + // + + /** + * @see ::ble::PalSecurityManager::set_ltk + */ + ble_error_t set_ltk( + connection_handle_t connection, + const ltk_t <k, + bool mitm, + bool secure_connections + ) final; + + /** + * @see ::ble::PalSecurityManager::set_ltk_not_found + */ + ble_error_t set_ltk_not_found( + connection_handle_t connection + ) final; + + /** + * @see ::ble::PalSecurityManager::set_irk + */ + ble_error_t set_irk(const irk_t &irk) final; + + /** + * @see ::ble::PalSecurityManager::set_csrk + */ + ble_error_t set_csrk( + const csrk_t &csrk, + sign_count_t sign_counter + ) final; + + /** + * @see ::ble::PalSecurityManager::set_peer_csrk + */ + ble_error_t set_peer_csrk( + connection_handle_t connection, + const csrk_t &csrk, + bool authenticated, + sign_count_t sign_counter + ) final; + + ble_error_t remove_peer_csrk(connection_handle_t connection) final; + + //////////////////////////////////////////////////////////////////////////// + // Authentication + // + + /** + * @see ::ble::PalSecurityManager::get_random_data + */ + ble_error_t get_random_data(byte_array_t<8> &random_data) final; + + //////////////////////////////////////////////////////////////////////////// + // MITM + // + + /** + * @see ::ble::PalSecurityManager::set_display_passkey + */ + ble_error_t set_display_passkey(passkey_num_t passkey) final; + + /** + * @see ::ble::PalSecurityManager::passkey_request_reply + */ + ble_error_t passkey_request_reply( + connection_handle_t connection, + passkey_num_t passkey + ) final; + + /** + * @see ::ble::PalSecurityManager::secure_connections_oob_request_reply + */ + ble_error_t secure_connections_oob_request_reply( + connection_handle_t connection, + const oob_lesc_value_t &local_random, + const oob_lesc_value_t &peer_random, + const oob_confirm_t &peer_confirm + ) final; + + /** + * @see ::ble::PalSecurityManager::legacy_pairing_oob_request_reply + */ + ble_error_t legacy_pairing_oob_request_reply( + connection_handle_t connection, + const oob_tk_t &oob_data + ) final; + + /** + * @see ::ble::PalSecurityManager::confirmation_entered + */ + ble_error_t confirmation_entered( + connection_handle_t connection, bool confirmation + ) final; + + /** + * @see ::ble::PalSecurityManager::send_keypress_notification + */ + ble_error_t send_keypress_notification( + connection_handle_t connection, ble::Keypress_t keypress + ) final; + + /** + * @see ::ble::PalSecurityManager::generate_secure_connections_oob + */ + ble_error_t generate_secure_connections_oob() final; + + /** + * @see ::ble::PalSecurityManager::set_event_handler + */ + void set_event_handler(PalSecurityManagerEventHandler *event_handler) final; + + /** + * @see ::ble::PalSecurityManager::get_event_handler + */ + PalSecurityManagerEventHandler *get_event_handler() final; + +public: + /* used by the PAL to get the singleton */ + static PalSecurityManager &get_security_manager(); + + /* used by PAL to handle security messages coming from the stack Event handler */ + static bool sm_handler(const wsfMsgHdr_t *msg); + +private: + + struct PrivacyControlBlock; + struct PrivacyClearResListControlBlock; + struct PrivacyAddDevToResListControlBlock; + struct PrivacyRemoveDevFromResListControlBlock; + + // Queue control block to add device to resolving list + void queue_add_device_to_resolving_list( + advertising_peer_address_type_t peer_identity_address_type, + const address_t &peer_identity_address, + const irk_t &peer_irk + ); + + // Queue control block to remove device from resolving list + void queue_remove_device_from_resolving_list( + advertising_peer_address_type_t peer_identity_address_type, + const address_t &peer_identity_address + ); + + // Queue control block to clear resolving list + void queue_clear_resolving_list(); + + // Clear all control blocks + void clear_privacy_control_blocks(); + + // Queue a control block + void queue_privacy_control_block(PrivacyControlBlock *block); + + // Try to dequeue and process the next control block + // cb_completed is set when the previous block has completed + void process_privacy_control_blocks(bool cb_completed); + + void cleanup_peer_csrks(); + + PalSecurityManagerEventHandler *_pal_event_handler; + + bool _use_default_passkey; + passkey_num_t _default_passkey; + bool _lesc_keys_generated; + uint8_t _public_key_x[SEC_ECC_KEY_LEN]; + + PrivacyControlBlock *_pending_privacy_control_blocks; + bool _processing_privacy_control_block; + irk_t _irk; + csrk_t _csrk; + csrk_t *_peer_csrks[DM_CONN_MAX]; +}; + +} // namespace impl +} // namespace ble + +#endif /* IMPL_PAL_SECURITY_MANAGER_ */ diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 4daa2d5..02c7c3d 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -53,17 +53,17 @@ class PalGap; -class BLEInstanceBase; - namespace impl { +class BLEInstanceBase; + class Gap : - public PalConnectionMonitor, + public ble::PalConnectionMonitor, public PalGapEventHandler { friend PalConnectionMonitor; friend PalGapEventHandler; friend PalGap; - friend BLEInstanceBase; + friend impl::BLEInstanceBase; using EventHandler = ::ble::Gap::EventHandler; using GapShutdownCallback_t = ::ble::Gap::GapShutdownCallback_t; diff --git a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h index b93d471..00ff378 100644 --- a/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h @@ -41,7 +41,6 @@ namespace ble { class PalGenericAccessService; -class BLEInstanceBase; namespace impl { @@ -450,9 +449,9 @@ public: SecurityManager( - PalSecurityManager &palImpl, - PalConnectionMonitor &connMonitorImpl, - PalSigningMonitor &signingMonitorImpl + ble::PalSecurityManager &palImpl, + ble::PalConnectionMonitor &connMonitorImpl, + ble::PalSigningMonitor &signingMonitorImpl ) : eventHandler(nullptr), _pal(palImpl), _connection_monitor(connMonitorImpl),