From cc896f1b52cd3b2b3db745bbc00f1388d4223b1f Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 16:43:02 +0000 Subject: [PATCH 1/6] BLE: Add mock for the security db. --- features/FEATURE_BLE/ble/pal/SecurityDb.h | 1 + features/FEATURE_BLE/tests/CMakeLists.txt | 1 + .../mock/MockPalSecurityDb.cpp | 35 ++++ .../SecurityManager/mock/MockPalSecurityDb.h | 192 ++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.cpp create mode 100644 features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.h diff --git a/features/FEATURE_BLE/ble/pal/SecurityDb.h b/features/FEATURE_BLE/ble/pal/SecurityDb.h index 703f4177a8..d89328852c 100644 --- a/features/FEATURE_BLE/ble/pal/SecurityDb.h +++ b/features/FEATURE_BLE/ble/pal/SecurityDb.h @@ -20,6 +20,7 @@ #include "platform/Callback.h" #include "ble/pal/GapTypes.h" #include "ble/BLETypes.h" +#include "ble/Gap.h" #include namespace ble { diff --git a/features/FEATURE_BLE/tests/CMakeLists.txt b/features/FEATURE_BLE/tests/CMakeLists.txt index 45ab4313ef..b36daad240 100644 --- a/features/FEATURE_BLE/tests/CMakeLists.txt +++ b/features/FEATURE_BLE/tests/CMakeLists.txt @@ -93,6 +93,7 @@ add_test(NAME GattClientTests COMMAND gatt-client-tests) add_executable(security-manager-tests mbed_os_stub/mbed_assert.c generic/SecurityManager/mock/MockPalSecurityManager.cpp + generic/SecurityManager/mock/MockPalSecurityDb.cpp ${PROJECT_SOURCE_DIR}/../source/generic/GenericSecurityManager.cpp ) diff --git a/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.cpp b/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.cpp new file mode 100644 index 0000000000..ac37e2aab2 --- /dev/null +++ b/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.cpp @@ -0,0 +1,35 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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. + */ + +#include "MockPalSecurityDb.h" + +namespace ble { +namespace pal { +namespace vendor { +namespace mock { + +//////////////////////////////////////////////////////////////////////////////// +// Constructor implementation of the mocked pal security manager +// +// WARNING: Do not remove; it speedup compile time. + +MockPalSecurityDb::MockPalSecurityDb() { } +MockPalSecurityDb::~MockPalSecurityDb() { } + +} // namespace ble +} // namespace pal +} // namespace vendor +} // namespace mock diff --git a/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.h b/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.h new file mode 100644 index 0000000000..088b328def --- /dev/null +++ b/features/FEATURE_BLE/tests/generic/SecurityManager/mock/MockPalSecurityDb.h @@ -0,0 +1,192 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 TESTS_GENERIC_SECURITYMANAGER_MOCK_MOCKPALSECURITYDB_H_ +#define TESTS_GENERIC_SECURITYMANAGER_MOCK_MOCKPALSECURITYDB_H_ + +#include "gmock/gmock.h" +#include "ble/pal/SecurityDB.h" + +namespace ble { +namespace pal { +namespace vendor { +namespace mock { + +/* + * Mock of ble::pal::SecurityDB + */ +class MockPalSecurityDb : public ble::pal::SecurityDb { +public: + MockPalSecurityDb(); + + virtual ~MockPalSecurityDb(); + + MOCK_METHOD1(get_entry, SecurityEntry_t*(connection_handle_t)); + + MOCK_METHOD1(get_entry, SecurityEntry_t*(const address_t &)); + + MOCK_METHOD4( + get_entry_local_keys, + void( + SecurityEntryKeysDbCb_t, + connection_handle_t, + const ediv_t &, + const rand_t & + ) + ); + + MOCK_METHOD2( + get_entry_local_keys, + void(SecurityEntryKeysDbCb_t, connection_handle_t) + ); + + MOCK_METHOD2( + set_entry_local_ltk, + void(connection_handle_t, const ltk_t &) + ); + + MOCK_METHOD3( + set_entry_local_ediv_rand, + void(connection_handle_t, const ediv_t &, const rand_t &) + ); + + MOCK_METHOD2( + get_entry_peer_csrk, + void(SecurityEntryCsrkDbCb_t, connection_handle_t) + ); + + MOCK_METHOD2( + get_entry_peer_keys, + void(SecurityEntryKeysDbCb_t, connection_handle_t) + ); + + MOCK_METHOD2( + set_entry_peer_ltk, + void(connection_handle_t, const ltk_t &) + ); + + MOCK_METHOD3( + set_entry_peer_ediv_rand, + void(connection_handle_t, const ediv_t &, const rand_t &) + ); + + MOCK_METHOD2( + set_entry_peer_irk, + void(connection_handle_t, const irk_t &) + ); + + MOCK_METHOD3( + set_entry_peer_bdaddr, + void(connection_handle_t, bool, const address_t &) + ); + + MOCK_METHOD2( + set_entry_peer_csrk, + void(connection_handle_t, const csrk_t &) + ); + + MOCK_METHOD0(get_local_csrk, const csrk_t*()); + + MOCK_METHOD1(set_local_csrk, void(const csrk_t &)); + + MOCK_METHOD0(get_public_key_x, const public_key_t&()); + + MOCK_METHOD0(get_public_key_y, const public_key_t&()); + + MOCK_METHOD2(set_public_key, void(const public_key_t &, const public_key_t &)); + + MOCK_METHOD0(get_peer_sc_oob_address, const address_t&()); + + MOCK_METHOD0(get_peer_sc_oob_random, const oob_rand_t&()); + + MOCK_METHOD0(get_peer_sc_oob_confirm, const oob_confirm_t&()); + + MOCK_METHOD4( + get_sc_oob_data, + void( + address_t &, + oob_rand_t &, + oob_confirm_t &, + oob_rand_t & + ) + ); + + MOCK_METHOD0(get_local_sc_oob_random, const oob_rand_t&()); + + MOCK_METHOD3( + set_peer_sc_oob_data, + void( + const address_t &, + const oob_rand_t &, + const oob_confirm_t & + ) + ); + + MOCK_METHOD1(set_local_sc_oob_random, void(const oob_rand_t&)); + + MOCK_METHOD4( + connect_entry, + SecurityEntry_t*( + connection_handle_t, + BLEProtocol::AddressType_t, + const address_t &, + const address_t & + ) + ); + + MOCK_METHOD1(disconnect_entry, void(connection_handle_t)); + + MOCK_METHOD1(remove_entry, void(const address_t)); + + MOCK_METHOD0(clear_entries, void()); + + MOCK_METHOD2(get_whitelist, void(WhitelistDbCb_t, Gap::Whitelist_t *)); + + MOCK_METHOD2( + generate_whitelist_from_bond_table, + void(WhitelistDbCb_t, Gap::Whitelist_t *) + ); + + MOCK_METHOD2( + set_whitelist, + void(WhitelistDbCb_t, const Gap::Whitelist_t &) + ); + + MOCK_METHOD1( + add_whitelist_entry, + void(const address_t &) + ); + + MOCK_METHOD1( + remove_whitelist_entry, + void(const address_t &) + ); + + MOCK_METHOD0(clear_whitelist, void()); + + MOCK_METHOD0(restore, void()); + + MOCK_METHOD0(sync, void()); + + MOCK_METHOD1(set_restore, void(bool)); +}; + +} // namespace ble +} // namespace pal +} // namespace vendor +} // namespace mock + +#endif /* TESTS_GENERIC_SECURITYMANAGER_MOCK_MOCKPALSECURITYDB_H_ */ From 8b957f5018f41a699bb57ac1eb5c1f65ed02219f Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 17:04:06 +0000 Subject: [PATCH 2/6] plaform/sleep: Fix inclusion and inline declaration. --- platform/mbed_sleep.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/platform/mbed_sleep.h b/platform/mbed_sleep.h index 23fa5150f4..791d83d03a 100644 --- a/platform/mbed_sleep.h +++ b/platform/mbed_sleep.h @@ -5,7 +5,7 @@ * \defgroup platform_sleep Sleep functions * @{ */ - + /* mbed Microcontroller Library * Copyright (c) 2006-2017 ARM Limited * @@ -24,7 +24,7 @@ #ifndef MBED_SLEEP_H #define MBED_SLEEP_H -#include "sleep_api.h" +#include "hal/sleep_api.h" #include "mbed_toolchain.h" #include @@ -128,7 +128,7 @@ void sleep_manager_sleep_auto(void); * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be * able to access the LocalFileSystem */ -__INLINE static void sleep(void) +static inline void sleep(void) { #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) #if DEVICE_SLEEP @@ -158,7 +158,7 @@ __INLINE static void sleep(void) */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()") -__INLINE static void deepsleep(void) +static inline void deepsleep(void) { #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) #if DEVICE_SLEEP From bcea06fb3211e4f49d40f0ca522ad6855a8c02be Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 17:04:54 +0000 Subject: [PATCH 3/6] BLE: Fix inclusion of mbed_error.h --- features/FEATURE_BLE/ble/BLE.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/FEATURE_BLE/ble/BLE.h b/features/FEATURE_BLE/ble/BLE.h index 20eedb4e08..66bb545c3e 100644 --- a/features/FEATURE_BLE/ble/BLE.h +++ b/features/FEATURE_BLE/ble/BLE.h @@ -28,7 +28,7 @@ #ifdef YOTTA_CFG_MBED_OS #include "mbed-drivers/mbed_error.h" #else -#include "mbed_error.h" +#include "platform/mbed_error.h" #endif #include "platform/mbed_toolchain.h" From 8b9e08e128dfe73fb8130542e95b4a6df30f4984 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 17:05:32 +0000 Subject: [PATCH 4/6] BLE tests: Add stub for device.h . --- features/FEATURE_BLE/tests/CMakeLists.txt | 1 + features/FEATURE_BLE/tests/mbed_os_stub/device.h | 0 2 files changed, 1 insertion(+) create mode 100644 features/FEATURE_BLE/tests/mbed_os_stub/device.h diff --git a/features/FEATURE_BLE/tests/CMakeLists.txt b/features/FEATURE_BLE/tests/CMakeLists.txt index b36daad240..8774040100 100644 --- a/features/FEATURE_BLE/tests/CMakeLists.txt +++ b/features/FEATURE_BLE/tests/CMakeLists.txt @@ -100,6 +100,7 @@ add_executable(security-manager-tests target_include_directories(security-manager-tests PRIVATE "${PROJECT_SOURCE_DIR}/.." "${PROJECT_SOURCE_DIR}/../../.." + "${PROJECT_SOURCE_DIR}/mbed_os_stub" "${PROJECT_SOURCE_DIR}/generic/SecurityManager" ) diff --git a/features/FEATURE_BLE/tests/mbed_os_stub/device.h b/features/FEATURE_BLE/tests/mbed_os_stub/device.h new file mode 100644 index 0000000000..e69de29bb2 From 28f2f46974704d159d9deae41bad95df767c5c1c Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 17:41:18 +0000 Subject: [PATCH 5/6] BLE: Hide event monitor inside pal. --- features/FEATURE_BLE/ble/generic/GenericGap.h | 8 ++++---- .../FEATURE_BLE/ble/generic/GenericSecurityManager.h | 2 +- .../FEATURE_BLE/ble/{ => pal}/ConnectionEventMonitor.h | 10 ++++++---- features/FEATURE_BLE/source/generic/GenericGap.cpp | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) rename features/FEATURE_BLE/ble/{ => pal}/ConnectionEventMonitor.h (90%) diff --git a/features/FEATURE_BLE/ble/generic/GenericGap.h b/features/FEATURE_BLE/ble/generic/GenericGap.h index cdc8d5a2b9..95ba900da1 100644 --- a/features/FEATURE_BLE/ble/generic/GenericGap.h +++ b/features/FEATURE_BLE/ble/generic/GenericGap.h @@ -28,7 +28,7 @@ #include "ble/BLETypes.h" #include "ble/pal/GenericAccessService.h" #include "ble/pal/EventQueue.h" -#include "ble/ConnectionEventMonitor.h" +#include "ble/pal/ConnectionEventMonitor.h" #include "drivers/Timeout.h" @@ -42,7 +42,7 @@ namespace generic { * @attention: Not part of the public interface of BLE API. */ class GenericGap : public ::Gap, - public ConnectionEventMonitor { + public pal::ConnectionEventMonitor { public: /** @@ -275,7 +275,7 @@ public: /** @note Implements ConnectionEventMonitor. * @copydoc ConnectionEventMonitor::set_connection_event_handler */ - virtual void set_connection_event_handler(ConnectionEventHandler *_connection_event_handler); + void set_connection_event_handler(pal::ConnectionEventHandler *_connection_event_handler); private: void on_scan_timeout(); @@ -317,7 +317,7 @@ private: mutable Whitelist_t _whitelist; mbed::Timeout _advertising_timeout; mbed::Timeout _scan_timeout; - ConnectionEventHandler *_connection_event_handler; + pal::ConnectionEventHandler *_connection_event_handler; }; } diff --git a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h index 2e742e12af..48deb30cb9 100644 --- a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h +++ b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h @@ -39,7 +39,7 @@ class GenericSecurityManagerEventHandler; class GenericSecurityManager : public SecurityManager, public pal::SecurityManagerEventHandler, - public ConnectionEventHandler { + public pal::ConnectionEventHandler { public: typedef ble::pal::SecurityEntry_t SecurityEntry_t; typedef ble::pal::SecurityEntryKeys_t SecurityEntryKeys_t; diff --git a/features/FEATURE_BLE/ble/ConnectionEventMonitor.h b/features/FEATURE_BLE/ble/pal/ConnectionEventMonitor.h similarity index 90% rename from features/FEATURE_BLE/ble/ConnectionEventMonitor.h rename to features/FEATURE_BLE/ble/pal/ConnectionEventMonitor.h index daa3ae4e14..0067d4d580 100644 --- a/features/FEATURE_BLE/ble/ConnectionEventMonitor.h +++ b/features/FEATURE_BLE/ble/pal/ConnectionEventMonitor.h @@ -30,22 +30,23 @@ #include "ble/pal/EventQueue.h" namespace ble { +namespace pal { class ConnectionEventHandler { public: virtual void on_connected( connection_handle_t connection, - Gap::Role_t role, + ::Gap::Role_t role, BLEProtocol::AddressType_t peer_address_type, const BLEProtocol::AddressBytes_t peer_address, BLEProtocol::AddressType_t local_address_type, const BLEProtocol::AddressBytes_t local_address, - const Gap::ConnectionParams_t *connection_params + const ::Gap::ConnectionParams_t *connection_params ) = 0; virtual void on_disconnected( connection_handle_t connection, - Gap::DisconnectionReason_t reason + ::Gap::DisconnectionReason_t reason ) = 0; }; @@ -59,6 +60,7 @@ public: virtual void set_connection_event_handler(ConnectionEventHandler *connection_event_handler) = 0; }; -} +} // namespace pal +} // namespace ble #endif /* MBED_BLE_CONNECTION_EVENT_MONITOR */ diff --git a/features/FEATURE_BLE/source/generic/GenericGap.cpp b/features/FEATURE_BLE/source/generic/GenericGap.cpp index ac3388d502..61744d115d 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.cpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.cpp @@ -1149,7 +1149,7 @@ bool GenericGap::initialize_whitelist() const return true; } -void GenericGap::set_connection_event_handler(ConnectionEventHandler *connection_event_handler) +void GenericGap::set_connection_event_handler(pal::ConnectionEventHandler *connection_event_handler) { _connection_event_handler = connection_event_handler; } From 4ce0ed9c0cf7401473debd10026c2eeb1b27a55f Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 19 Feb 2018 17:46:41 +0000 Subject: [PATCH 6/6] BLE: Remove dependency of GenericSecurityManager towards gap. --- features/FEATURE_BLE/ble/generic/GenericSecurityManager.h | 8 ++++---- .../FEATURE_BLE/source/generic/GenericSecurityManager.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h index 48deb30cb9..4de2c787eb 100644 --- a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h +++ b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h @@ -21,7 +21,7 @@ #include "ble/BLETypes.h" #include "ble/pal/SecurityDb.h" #include "platform/Callback.h" -#include "ble/Gap.h" +#include "ble/pal/ConnectionEventMonitor.h" #include "ble/generic/GenericGap.h" #include "ble/pal/PalSecurityManager.h" @@ -237,10 +237,10 @@ public: GenericSecurityManager( pal::SecurityManager &palImpl, pal::SecurityDb &dbImpl, - GenericGap &gapImpl + pal::ConnectionEventMonitor &connMonitorImpl ) : _pal(palImpl), _db(dbImpl), - _gap(gapImpl), + _connection_monitor(connMonitorImpl), _default_authentication(0), _default_key_distribution(KeyDistribution::KEY_DISTRIBUTION_ALL), _pairing_authorisation_required(false), @@ -433,7 +433,7 @@ private: private: pal::SecurityManager &_pal; pal::SecurityDb &_db; - GenericGap &_gap; + pal::ConnectionEventMonitor &_connection_monitor; AuthenticationMask _default_authentication; KeyDistribution _default_key_distribution; diff --git a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp index b8f1c10edc..6d38ddc793 100644 --- a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp +++ b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp @@ -55,7 +55,7 @@ ble_error_t GenericSecurityManager::init( init_signing(); } - _gap.set_connection_event_handler(this); + _connection_monitor.set_connection_event_handler(this); _pal.generate_public_key();