Skip to content

Commit df12718

Browse files
authored
Merge pull request #14737 from paul-szczepanek-arm/cmake-ble-rebased
BLE: Add cmake unittest fakes for BLE and events
2 parents aeaac0e + 8642610 commit df12718

File tree

19 files changed

+1870
-0
lines changed

19 files changed

+1870
-0
lines changed

UNITTESTS/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ if (VALGRIND)
5858
endif(VALGRIND)
5959

6060
add_subdirectory(stubs)
61+
add_subdirectory(fakes)
62+

UNITTESTS/fakes/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_subdirectory(events)
5+
add_subdirectory(ble)

UNITTESTS/fakes/ble/BLE.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2020 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "ble/BLE.h"
19+
#include "GattServerImpl_mock.h"
20+
#include "GattClientImpl_mock.h"
21+
#include "GapImpl_mock.h"
22+
#include "SecurityManagerImpl_mock.h"
23+
#include "ble/GattClient.h"
24+
#include "ble/GattServer.h"
25+
#include "ble/SecurityManager.h"
26+
#include "ble/Gap.h"
27+
#include "ble_mocks.h"
28+
29+
namespace ble {
30+
31+
static GapMock *gap_impl = nullptr;
32+
static GattServerMock *gatt_server_impl = nullptr;
33+
static GattClientMock *gatt_client_impl = nullptr;
34+
static SecurityManagerMock *security_manager_impl = nullptr;
35+
36+
static Gap *gap = nullptr;
37+
static GattServer *gatt_server = nullptr;
38+
static GattClient *gatt_client = nullptr;
39+
static SecurityManager *security_manager = nullptr;
40+
41+
GapMock& gap_mock() {
42+
return *ble::gap_impl;
43+
}
44+
45+
GattServerMock& gatt_server_mock() {
46+
return *ble::gatt_server_impl;
47+
}
48+
49+
GattClientMock& gatt_client_mock() {
50+
return *ble::gatt_client_impl;
51+
}
52+
53+
SecurityManagerMock& security_manager_mock() {
54+
return *ble::security_manager_impl;
55+
}
56+
57+
void init_mocks() {
58+
if (gap_impl) {
59+
/* we are already initialised */
60+
return;
61+
}
62+
63+
/* mocks */
64+
gap_impl = new GapMock();
65+
gatt_server_impl = new GattServerMock();
66+
gatt_client_impl = new GattClientMock();
67+
security_manager_impl = new SecurityManagerMock();
68+
/* user APIS */
69+
gap = new Gap(gap_impl);
70+
gatt_server = new GattServer(gatt_server_impl);
71+
gatt_client = new GattClient(gatt_client_impl);
72+
security_manager = new SecurityManager(security_manager_impl);
73+
}
74+
75+
void delete_mocks() {
76+
delete gap;
77+
delete gap_impl;
78+
delete gatt_server;
79+
delete gatt_server_impl;
80+
delete gatt_client;
81+
delete gatt_client_impl;
82+
delete security_manager;
83+
delete security_manager_impl;
84+
85+
gap = nullptr;
86+
gap_impl = nullptr;
87+
gatt_server = nullptr;
88+
gatt_server_impl = nullptr;
89+
gatt_client = nullptr;
90+
gatt_client_impl = nullptr;
91+
security_manager = nullptr;
92+
security_manager_impl = nullptr;
93+
}
94+
95+
class BLEInstanceBase {
96+
};
97+
98+
BLE::BLE(ble::BLEInstanceBase &transport) : transport(transport)
99+
{
100+
}
101+
102+
BLE& BLE::Instance()
103+
{
104+
static ble::BLEInstanceBase transport;
105+
static BLE instance(transport);
106+
init_mocks();
107+
return instance;
108+
}
109+
110+
ble::Gap &BLE::gap()
111+
{
112+
init_mocks();
113+
return *ble::gap;
114+
}
115+
116+
ble::GattServer &BLE::gattServer()
117+
{
118+
init_mocks();
119+
return *ble::gatt_server;
120+
}
121+
122+
ble::GattClient &BLE::gattClient()
123+
{
124+
init_mocks();
125+
return *ble::gatt_client;
126+
}
127+
128+
ble::SecurityManager &BLE::securityManager()
129+
{
130+
init_mocks();
131+
return *ble::security_manager;
132+
}
133+
134+
const ble::Gap &BLE::gap() const
135+
{
136+
auto &self = const_cast<BLE &>(*this);
137+
return const_cast<const ble::Gap &>(self.gap());
138+
}
139+
140+
const ble::GattServer &BLE::gattServer() const
141+
{
142+
auto &self = const_cast<BLE &>(*this);
143+
return const_cast<const ble::GattServer &>(self.gattServer());
144+
}
145+
146+
const ble::GattClient &BLE::gattClient() const
147+
{
148+
auto &self = const_cast<BLE &>(*this);
149+
return const_cast<const ble::GattClient &>(self.gattClient());
150+
}
151+
152+
const ble::SecurityManager &BLE::securityManager() const
153+
{
154+
auto &self = const_cast<BLE &>(*this);
155+
return const_cast<const ble::SecurityManager &>(self.securityManager());
156+
}
157+
158+
void BLE::processEvents()
159+
{
160+
161+
}
162+
163+
}

UNITTESTS/fakes/ble/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_library(mbed-fakes-ble)
5+
6+
target_include_directories(mbed-fakes-ble
7+
PUBLIC
8+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/
9+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include
10+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include/ble
11+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source
12+
PRIVATE
13+
${gtest_SOURCE_DIR}/include
14+
${gmock_SOURCE_DIR}/include
15+
)
16+
17+
target_sources(mbed-fakes-ble
18+
PRIVATE
19+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingDataBuilder.cpp
20+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingParameters.cpp
21+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/ConnectionParameters.cpp
22+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gatt/DiscoveredCharacteristic.cpp
23+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/Gap.cpp
24+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattClient.cpp
25+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattServer.cpp
26+
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/SecurityManager.cpp
27+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/BLE.cpp
28+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/source/GattServerImpl_mock.cpp
29+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/ble_mocks.h
30+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GapImpl_mock.h
31+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattClientImpl_mock.h
32+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattServerImpl_mock.h
33+
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/SecurityManagerImpl_mock.h
34+
)
35+
36+
target_link_libraries(mbed-fakes-ble
37+
PRIVATE
38+
mbed-headers
39+
mbed-stubs-headers
40+
gcov
41+
)

UNITTESTS/fakes/ble/GapImpl_mock.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2020 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef BLE_GAPMOCK_H
19+
#define BLE_GAPMOCK_H
20+
21+
#include "gmock/gmock.h"
22+
#include "source/generic/GapImpl.h"
23+
24+
namespace ble {
25+
26+
class GapMock : public ble::impl::Gap {
27+
public:
28+
GapMock() {};
29+
GapMock(const GapMock&) = delete;
30+
GapMock& operator=(const GapMock&) = delete;
31+
virtual ~GapMock() {};
32+
33+
MOCK_METHOD((ble_error_t), reset, (), (override));
34+
MOCK_METHOD(void, setEventHandler, (EventHandler *handler), (override));
35+
MOCK_METHOD(bool, isFeatureSupported, (controller_supported_features_t feature), (override));
36+
MOCK_METHOD(uint8_t, getMaxAdvertisingSetNumber, (), (override));
37+
MOCK_METHOD(uint16_t, getMaxAdvertisingDataLength, (), (override));
38+
MOCK_METHOD(uint16_t, getMaxConnectableAdvertisingDataLength, (), (override));
39+
MOCK_METHOD(uint16_t, getMaxActiveSetAdvertisingDataLength, (), (override));
40+
MOCK_METHOD(ble_error_t, createAdvertisingSet, (advertising_handle_t *handle, const AdvertisingParameters &parameters), (override));
41+
MOCK_METHOD(ble_error_t, destroyAdvertisingSet, (advertising_handle_t handle), (override));
42+
MOCK_METHOD(ble_error_t, setAdvertisingParameters, (advertising_handle_t handle, const AdvertisingParameters &params), (override));
43+
MOCK_METHOD(ble_error_t, setAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
44+
MOCK_METHOD(ble_error_t, setAdvertisingScanResponse, (advertising_handle_t handle, mbed::Span<const uint8_t> response), (override));
45+
MOCK_METHOD(ble_error_t, startAdvertising, (advertising_handle_t handle, adv_duration_t maxDuration, uint8_t maxEvents), (override));
46+
MOCK_METHOD(ble_error_t, stopAdvertising, (advertising_handle_t handle), (override));
47+
MOCK_METHOD(bool, isAdvertisingActive, (advertising_handle_t handle), (override));
48+
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingParameters, (advertising_handle_t handle, periodic_interval_t periodicAdvertisingIntervalMin, periodic_interval_t periodicAdvertisingIntervalMax, bool advertiseTxPower), (override));
49+
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
50+
MOCK_METHOD(ble_error_t, startPeriodicAdvertising, (advertising_handle_t handle), (override));
51+
MOCK_METHOD(ble_error_t, stopPeriodicAdvertising, (advertising_handle_t handle), (override));
52+
MOCK_METHOD(bool, isPeriodicAdvertisingActive, (advertising_handle_t handle), (override));
53+
MOCK_METHOD(ble_error_t, setScanParameters, (const ScanParameters &params), (override));
54+
MOCK_METHOD(ble_error_t, startScan, (scan_duration_t duration, duplicates_filter_t filtering, scan_period_t period), (override));
55+
MOCK_METHOD(ble_error_t, initiate_scan, (), (override));
56+
MOCK_METHOD(ble_error_t, stopScan, (), (override));
57+
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));
58+
MOCK_METHOD(ble_error_t, createSync, (slave_latency_t maxPacketSkip, sync_timeout_t timeout), (override));
59+
MOCK_METHOD(ble_error_t, cancelCreateSync, (), (override));
60+
MOCK_METHOD(ble_error_t, terminateSync, (periodic_sync_handle_t handle), (override));
61+
MOCK_METHOD(ble_error_t, addDeviceToPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
62+
MOCK_METHOD(ble_error_t, removeDeviceFromPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
63+
MOCK_METHOD(ble_error_t, clearPeriodicAdvertiserList, (), (override));
64+
MOCK_METHOD(ble_error_t, connect, (peer_address_type_t peerAddressType, const address_t &peerAddress, const ConnectionParameters &connectionParams), (override));
65+
MOCK_METHOD(ble_error_t, cancelConnect, (), (override));
66+
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));
67+
MOCK_METHOD(ble_error_t, manageConnectionParametersUpdateRequest, (bool userManageConnectionUpdateRequest), (override));
68+
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));
69+
MOCK_METHOD(ble_error_t, rejectConnectionParametersUpdate, (connection_handle_t connectionHandle), (override));
70+
MOCK_METHOD(ble_error_t, disconnect, (connection_handle_t connectionHandle, local_disconnection_reason_t reason), (override));
71+
MOCK_METHOD(ble_error_t, readPhy, (connection_handle_t connection), (override));
72+
MOCK_METHOD(ble_error_t, setPreferredPhys, (const phy_set_t *txPhys, const phy_set_t *rxPhys), (override));
73+
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));
74+
MOCK_METHOD(ble_error_t, enablePrivacy, (bool enable), (override));
75+
MOCK_METHOD(ble_error_t, setPeripheralPrivacyConfiguration, (const peripheral_privacy_configuration_t *configuration), (override));
76+
MOCK_METHOD(ble_error_t, getPeripheralPrivacyConfiguration, (peripheral_privacy_configuration_t *configuration), (override));
77+
MOCK_METHOD(ble_error_t, setCentralPrivacyConfiguration, (const central_privacy_configuration_t *configuration), (override));
78+
MOCK_METHOD(ble_error_t, getCentralPrivacyConfiguration, (central_privacy_configuration_t *configuration), (override));
79+
MOCK_METHOD(uint8_t, getMaxWhitelistSize, (), (const, override));
80+
MOCK_METHOD(ble_error_t, getWhitelist, (whitelist_t &whitelist), (const, override));
81+
MOCK_METHOD(ble_error_t, setWhitelist, (const whitelist_t &whitelist), (override));
82+
MOCK_METHOD(ble_error_t, getAddress, (own_address_type_t &typeP, address_t &address), (override));
83+
MOCK_METHOD(void, onShutdown, (const GapShutdownCallback_t &callback), (override));
84+
MOCK_METHOD(GapShutdownCallbackChain_t&, onShutdown, (), (override));
85+
MOCK_METHOD(ble_error_t, setRandomStaticAddress, (const ble::address_t &address), (override));
86+
MOCK_METHOD(ble::address_t, getRandomStaticAddress, (), (override));
87+
};
88+
89+
}
90+
91+
#endif //BLE_GAPMOCK_H
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2020 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef BLE_GATTCLIENTMOCK_H
19+
#define BLE_GATTCLIENTMOCK_H
20+
21+
#include "gmock/gmock.h"
22+
#include "source/generic/GattClientImpl.h"
23+
24+
namespace ble {
25+
26+
class GattClientMock : public ble::impl::GattClient {
27+
public:
28+
GattClientMock() {};
29+
GattClientMock(const GattClientMock&) = delete;
30+
GattClientMock& operator=(const GattClientMock&) = delete;
31+
virtual ~GattClientMock() {};
32+
33+
MOCK_METHOD(ble_error_t, reset, (), (override));
34+
MOCK_METHOD(void, setEventHandler, (EventHandler *handler), (override));
35+
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));
36+
MOCK_METHOD(ble_error_t, discoverServices, (ble::connection_handle_t connectionHandle, ServiceDiscovery::ServiceCallback_t callback, const UUID &matchingServiceUUID), (override));
37+
MOCK_METHOD(ble_error_t, discoverServices, (ble::connection_handle_t connectionHandle, ServiceDiscovery::ServiceCallback_t callback, GattAttribute::Handle_t startHandle, GattAttribute::Handle_t endHandle), (override));
38+
MOCK_METHOD(bool, isServiceDiscoveryActive, (), (const, override));
39+
MOCK_METHOD(void, terminateServiceDiscovery, (), (override));
40+
MOCK_METHOD(ble_error_t, read, (ble::connection_handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset), (const, override));
41+
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));
42+
MOCK_METHOD(void, onDataRead, (ReadCallback_t callback), (override));
43+
MOCK_METHOD(ReadCallbackChain_t&, onDataRead, (), (override));
44+
MOCK_METHOD(void, onDataWritten, (WriteCallback_t callback), (override));
45+
MOCK_METHOD(WriteCallbackChain_t&, onDataWritten, (), (override));
46+
MOCK_METHOD(void, onServiceDiscoveryTermination, (ServiceDiscovery::TerminationCallback_t callback), (override));
47+
MOCK_METHOD(ble_error_t, discoverCharacteristicDescriptors, (const DiscoveredCharacteristic &characteristic, const CharacteristicDescriptorDiscovery::DiscoveryCallback_t &discoveryCallback, const CharacteristicDescriptorDiscovery::TerminationCallback_t &terminationCallback), (override));
48+
MOCK_METHOD(bool, isCharacteristicDescriptorDiscoveryActive, (const DiscoveredCharacteristic &characteristic), (const, override));
49+
MOCK_METHOD(void, terminateCharacteristicDescriptorDiscovery, (const DiscoveredCharacteristic &characteristic), (override));
50+
MOCK_METHOD(ble_error_t, negotiateAttMtu, (ble::connection_handle_t connection), (override));
51+
MOCK_METHOD(void, onHVX, (HVXCallback_t callback), (override));
52+
MOCK_METHOD(void, onShutdown, (const GattClientShutdownCallback_t &callback), (override));
53+
MOCK_METHOD(GattClientShutdownCallbackChain_t&, onShutdown, (), (override));
54+
MOCK_METHOD(HVXCallbackChain_t&, onHVX, (), (override));
55+
MOCK_METHOD(void, processReadResponse, (const GattReadCallbackParams *params), (override));
56+
MOCK_METHOD(void, processWriteResponse, (const GattWriteCallbackParams *params), (override));
57+
MOCK_METHOD(void, processHVXEvent, (const GattHVXCallbackParams *params), (override));
58+
};
59+
60+
}
61+
62+
#endif //BLE_GATTCLIENTMOCK_H

0 commit comments

Comments
 (0)