Skip to content

BLE: Add cmake unittest fakes for BLE and events #14737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UNITTESTS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ if (VALGRIND)
endif(VALGRIND)

add_subdirectory(stubs)
add_subdirectory(fakes)

5 changes: 5 additions & 0 deletions UNITTESTS/fakes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_subdirectory(events)
add_subdirectory(ble)
163 changes: 163 additions & 0 deletions UNITTESTS/fakes/ble/BLE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* mbed Microcontroller Library
* Copyright (c) 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.
*/

#include "ble/BLE.h"
#include "GattServerImpl_mock.h"
#include "GattClientImpl_mock.h"
#include "GapImpl_mock.h"
#include "SecurityManagerImpl_mock.h"
#include "ble/GattClient.h"
#include "ble/GattServer.h"
#include "ble/SecurityManager.h"
#include "ble/Gap.h"
#include "ble_mocks.h"

namespace ble {

static GapMock *gap_impl = nullptr;
static GattServerMock *gatt_server_impl = nullptr;
static GattClientMock *gatt_client_impl = nullptr;
static SecurityManagerMock *security_manager_impl = nullptr;

static Gap *gap = nullptr;
static GattServer *gatt_server = nullptr;
static GattClient *gatt_client = nullptr;
static SecurityManager *security_manager = nullptr;

GapMock& gap_mock() {
return *ble::gap_impl;
}

GattServerMock& gatt_server_mock() {
return *ble::gatt_server_impl;
}

GattClientMock& gatt_client_mock() {
return *ble::gatt_client_impl;
}

SecurityManagerMock& security_manager_mock() {
return *ble::security_manager_impl;
}

void init_mocks() {
if (gap_impl) {
/* we are already initialised */
return;
}

/* mocks */
gap_impl = new GapMock();
gatt_server_impl = new GattServerMock();
gatt_client_impl = new GattClientMock();
security_manager_impl = new SecurityManagerMock();
/* user APIS */
gap = new Gap(gap_impl);
gatt_server = new GattServer(gatt_server_impl);
gatt_client = new GattClient(gatt_client_impl);
security_manager = new SecurityManager(security_manager_impl);
}

void delete_mocks() {
delete gap;
delete gap_impl;
delete gatt_server;
delete gatt_server_impl;
delete gatt_client;
delete gatt_client_impl;
delete security_manager;
delete security_manager_impl;

gap = nullptr;
gap_impl = nullptr;
gatt_server = nullptr;
gatt_server_impl = nullptr;
gatt_client = nullptr;
gatt_client_impl = nullptr;
security_manager = nullptr;
security_manager_impl = nullptr;
}

class BLEInstanceBase {
};

BLE::BLE(ble::BLEInstanceBase &transport) : transport(transport)
{
}

BLE& BLE::Instance()
{
static ble::BLEInstanceBase transport;
static BLE instance(transport);
init_mocks();
return instance;
}

ble::Gap &BLE::gap()
{
init_mocks();
return *ble::gap;
}

ble::GattServer &BLE::gattServer()
{
init_mocks();
return *ble::gatt_server;
}

ble::GattClient &BLE::gattClient()
{
init_mocks();
return *ble::gatt_client;
}

ble::SecurityManager &BLE::securityManager()
{
init_mocks();
return *ble::security_manager;
}

const ble::Gap &BLE::gap() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::Gap &>(self.gap());
}

const ble::GattServer &BLE::gattServer() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::GattServer &>(self.gattServer());
}

const ble::GattClient &BLE::gattClient() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::GattClient &>(self.gattClient());
}

const ble::SecurityManager &BLE::securityManager() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::SecurityManager &>(self.securityManager());
}

void BLE::processEvents()
{

}

}
41 changes: 41 additions & 0 deletions UNITTESTS/fakes/ble/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-fakes-ble)

target_include_directories(mbed-fakes-ble
PUBLIC
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include/ble
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source
PRIVATE
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
)

target_sources(mbed-fakes-ble
PRIVATE
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingDataBuilder.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingParameters.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/ConnectionParameters.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gatt/DiscoveredCharacteristic.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/Gap.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattClient.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattServer.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/SecurityManager.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/BLE.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/source/GattServerImpl_mock.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/ble_mocks.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GapImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattClientImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattServerImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/SecurityManagerImpl_mock.h
)

target_link_libraries(mbed-fakes-ble
PRIVATE
mbed-headers
mbed-stubs-headers
gcov
)
91 changes: 91 additions & 0 deletions UNITTESTS/fakes/ble/GapImpl_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* mbed Microcontroller Library
* Copyright (c) 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_GAPMOCK_H
#define BLE_GAPMOCK_H

#include "gmock/gmock.h"
#include "source/generic/GapImpl.h"

namespace ble {

class GapMock : public ble::impl::Gap {
public:
GapMock() {};
GapMock(const GapMock&) = delete;
GapMock& operator=(const GapMock&) = delete;
virtual ~GapMock() {};

MOCK_METHOD((ble_error_t), reset, (), (override));
MOCK_METHOD(void, setEventHandler, (EventHandler *handler), (override));
MOCK_METHOD(bool, isFeatureSupported, (controller_supported_features_t feature), (override));
MOCK_METHOD(uint8_t, getMaxAdvertisingSetNumber, (), (override));
MOCK_METHOD(uint16_t, getMaxAdvertisingDataLength, (), (override));
MOCK_METHOD(uint16_t, getMaxConnectableAdvertisingDataLength, (), (override));
MOCK_METHOD(uint16_t, getMaxActiveSetAdvertisingDataLength, (), (override));
MOCK_METHOD(ble_error_t, createAdvertisingSet, (advertising_handle_t *handle, const AdvertisingParameters &parameters), (override));
MOCK_METHOD(ble_error_t, destroyAdvertisingSet, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setAdvertisingParameters, (advertising_handle_t handle, const AdvertisingParameters &params), (override));
MOCK_METHOD(ble_error_t, setAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
MOCK_METHOD(ble_error_t, setAdvertisingScanResponse, (advertising_handle_t handle, mbed::Span<const uint8_t> response), (override));
MOCK_METHOD(ble_error_t, startAdvertising, (advertising_handle_t handle, adv_duration_t maxDuration, uint8_t maxEvents), (override));
MOCK_METHOD(ble_error_t, stopAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(bool, isAdvertisingActive, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingParameters, (advertising_handle_t handle, periodic_interval_t periodicAdvertisingIntervalMin, periodic_interval_t periodicAdvertisingIntervalMax, bool advertiseTxPower), (override));
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
MOCK_METHOD(ble_error_t, startPeriodicAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, stopPeriodicAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(bool, isPeriodicAdvertisingActive, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setScanParameters, (const ScanParameters &params), (override));
MOCK_METHOD(ble_error_t, startScan, (scan_duration_t duration, duplicates_filter_t filtering, scan_period_t period), (override));
MOCK_METHOD(ble_error_t, initiate_scan, (), (override));
MOCK_METHOD(ble_error_t, stopScan, (), (override));
MOCK_METHOD(ble_error_t, createSync, (peer_address_type_t peerAddressType, const address_t &peerAddress, uint8_t sid, slave_latency_t maxPacketSkip, sync_timeout_t timeout), (override));
MOCK_METHOD(ble_error_t, createSync, (slave_latency_t maxPacketSkip, sync_timeout_t timeout), (override));
MOCK_METHOD(ble_error_t, cancelCreateSync, (), (override));
MOCK_METHOD(ble_error_t, terminateSync, (periodic_sync_handle_t handle), (override));
MOCK_METHOD(ble_error_t, addDeviceToPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
MOCK_METHOD(ble_error_t, removeDeviceFromPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
MOCK_METHOD(ble_error_t, clearPeriodicAdvertiserList, (), (override));
MOCK_METHOD(ble_error_t, connect, (peer_address_type_t peerAddressType, const address_t &peerAddress, const ConnectionParameters &connectionParams), (override));
MOCK_METHOD(ble_error_t, cancelConnect, (), (override));
MOCK_METHOD(ble_error_t, updateConnectionParameters, (connection_handle_t connectionHandle, conn_interval_t minConnectionInterval, conn_interval_t maxConnectionInterval, slave_latency_t slaveLatency, supervision_timeout_t supervision_timeout, conn_event_length_t minConnectionEventLength, conn_event_length_t maxConnectionEventLength), (override));
MOCK_METHOD(ble_error_t, manageConnectionParametersUpdateRequest, (bool userManageConnectionUpdateRequest), (override));
MOCK_METHOD(ble_error_t, acceptConnectionParametersUpdate, (connection_handle_t connectionHandle, conn_interval_t minConnectionInterval, conn_interval_t maxConnectionInterval, slave_latency_t slaveLatency, supervision_timeout_t supervision_timeout, conn_event_length_t minConnectionEventLength, conn_event_length_t maxConnectionEventLength), (override));
MOCK_METHOD(ble_error_t, rejectConnectionParametersUpdate, (connection_handle_t connectionHandle), (override));
MOCK_METHOD(ble_error_t, disconnect, (connection_handle_t connectionHandle, local_disconnection_reason_t reason), (override));
MOCK_METHOD(ble_error_t, readPhy, (connection_handle_t connection), (override));
MOCK_METHOD(ble_error_t, setPreferredPhys, (const phy_set_t *txPhys, const phy_set_t *rxPhys), (override));
MOCK_METHOD(ble_error_t, setPhy, (connection_handle_t connection, const phy_set_t *txPhys, const phy_set_t *rxPhys, coded_symbol_per_bit_t codedSymbol), (override));
MOCK_METHOD(ble_error_t, enablePrivacy, (bool enable), (override));
MOCK_METHOD(ble_error_t, setPeripheralPrivacyConfiguration, (const peripheral_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, getPeripheralPrivacyConfiguration, (peripheral_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, setCentralPrivacyConfiguration, (const central_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, getCentralPrivacyConfiguration, (central_privacy_configuration_t *configuration), (override));
MOCK_METHOD(uint8_t, getMaxWhitelistSize, (), (const, override));
MOCK_METHOD(ble_error_t, getWhitelist, (whitelist_t &whitelist), (const, override));
MOCK_METHOD(ble_error_t, setWhitelist, (const whitelist_t &whitelist), (override));
MOCK_METHOD(ble_error_t, getAddress, (own_address_type_t &typeP, address_t &address), (override));
MOCK_METHOD(void, onShutdown, (const GapShutdownCallback_t &callback), (override));
MOCK_METHOD(GapShutdownCallbackChain_t&, onShutdown, (), (override));
MOCK_METHOD(ble_error_t, setRandomStaticAddress, (const ble::address_t &address), (override));
MOCK_METHOD(ble::address_t, getRandomStaticAddress, (), (override));
};

}

#endif //BLE_GAPMOCK_H
62 changes: 62 additions & 0 deletions UNITTESTS/fakes/ble/GattClientImpl_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* mbed Microcontroller Library
* Copyright (c) 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_GATTCLIENTMOCK_H
#define BLE_GATTCLIENTMOCK_H

#include "gmock/gmock.h"
#include "source/generic/GattClientImpl.h"

namespace ble {

class GattClientMock : public ble::impl::GattClient {
public:
GattClientMock() {};
GattClientMock(const GattClientMock&) = delete;
GattClientMock& operator=(const GattClientMock&) = delete;
virtual ~GattClientMock() {};

MOCK_METHOD(ble_error_t, reset, (), (override));
MOCK_METHOD(void, setEventHandler, (EventHandler *handler), (override));
MOCK_METHOD(ble_error_t, launchServiceDiscovery, (ble::connection_handle_t connectionHandle, ServiceDiscovery::ServiceCallback_t sc, ServiceDiscovery::CharacteristicCallback_t cc, const UUID &matchingServiceUUID, const UUID &matchingCharacteristicUUIDIn), (override));
MOCK_METHOD(ble_error_t, discoverServices, (ble::connection_handle_t connectionHandle, ServiceDiscovery::ServiceCallback_t callback, const UUID &matchingServiceUUID), (override));
MOCK_METHOD(ble_error_t, discoverServices, (ble::connection_handle_t connectionHandle, ServiceDiscovery::ServiceCallback_t callback, GattAttribute::Handle_t startHandle, GattAttribute::Handle_t endHandle), (override));
MOCK_METHOD(bool, isServiceDiscoveryActive, (), (const, override));
MOCK_METHOD(void, terminateServiceDiscovery, (), (override));
MOCK_METHOD(ble_error_t, read, (ble::connection_handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset), (const, override));
MOCK_METHOD(ble_error_t, write, (GattClient::WriteOp_t cmd, ble::connection_handle_t connHandle, GattAttribute::Handle_t attributeHandle, size_t length, const uint8_t *value), (const, override));
MOCK_METHOD(void, onDataRead, (ReadCallback_t callback), (override));
MOCK_METHOD(ReadCallbackChain_t&, onDataRead, (), (override));
MOCK_METHOD(void, onDataWritten, (WriteCallback_t callback), (override));
MOCK_METHOD(WriteCallbackChain_t&, onDataWritten, (), (override));
MOCK_METHOD(void, onServiceDiscoveryTermination, (ServiceDiscovery::TerminationCallback_t callback), (override));
MOCK_METHOD(ble_error_t, discoverCharacteristicDescriptors, (const DiscoveredCharacteristic &characteristic, const CharacteristicDescriptorDiscovery::DiscoveryCallback_t &discoveryCallback, const CharacteristicDescriptorDiscovery::TerminationCallback_t &terminationCallback), (override));
MOCK_METHOD(bool, isCharacteristicDescriptorDiscoveryActive, (const DiscoveredCharacteristic &characteristic), (const, override));
MOCK_METHOD(void, terminateCharacteristicDescriptorDiscovery, (const DiscoveredCharacteristic &characteristic), (override));
MOCK_METHOD(ble_error_t, negotiateAttMtu, (ble::connection_handle_t connection), (override));
MOCK_METHOD(void, onHVX, (HVXCallback_t callback), (override));
MOCK_METHOD(void, onShutdown, (const GattClientShutdownCallback_t &callback), (override));
MOCK_METHOD(GattClientShutdownCallbackChain_t&, onShutdown, (), (override));
MOCK_METHOD(HVXCallbackChain_t&, onHVX, (), (override));
MOCK_METHOD(void, processReadResponse, (const GattReadCallbackParams *params), (override));
MOCK_METHOD(void, processWriteResponse, (const GattWriteCallbackParams *params), (override));
MOCK_METHOD(void, processHVXEvent, (const GattHVXCallbackParams *params), (override));
};

}

#endif //BLE_GATTCLIENTMOCK_H
Loading