From c885df0af3d52a7cf307440fc7b75337e1b45e3c Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 21 May 2019 14:49:05 -0700 Subject: [PATCH] wifi(tests): Unit tests for iface event cb's Few class refactors to help with unit tests for ag/7567226. Bug: 132943959 Test: ./hardware/interfaces/wifi/1.3/default/tests/runtests.sh Change-Id: I62909f3f94899bc1593824684b93d29f44954eae --- wifi/1.3/default/Android.mk | 2 + wifi/1.3/default/service.cpp | 6 +- .../1.3/default/tests/mock_interface_tool.cpp | 29 ++++ wifi/1.3/default/tests/mock_interface_tool.h | 44 ++++++ .../default/tests/mock_wifi_iface_util.cpp | 4 +- wifi/1.3/default/tests/mock_wifi_iface_util.h | 6 +- .../default/tests/mock_wifi_legacy_hal.cpp | 4 +- wifi/1.3/default/tests/mock_wifi_legacy_hal.h | 3 +- .../tests/wifi_ap_iface_unit_tests.cpp | 7 +- .../default/tests/wifi_chip_unit_tests.cpp | 7 +- .../tests/wifi_iface_util_unit_tests.cpp | 46 +++++- .../tests/wifi_nan_iface_unit_tests.cpp | 148 ++++++++++++++++++ wifi/1.3/default/wifi.cpp | 4 +- wifi/1.3/default/wifi.h | 4 +- wifi/1.3/default/wifi_iface_util.cpp | 15 +- wifi/1.3/default/wifi_iface_util.h | 4 +- wifi/1.3/default/wifi_legacy_hal.cpp | 10 +- wifi/1.3/default/wifi_legacy_hal.h | 4 +- 18 files changed, 317 insertions(+), 30 deletions(-) create mode 100644 wifi/1.3/default/tests/mock_interface_tool.cpp create mode 100644 wifi/1.3/default/tests/mock_interface_tool.h create mode 100644 wifi/1.3/default/tests/wifi_nan_iface_unit_tests.cpp diff --git a/wifi/1.3/default/Android.mk b/wifi/1.3/default/Android.mk index 199ea3b9f8..1f918618f7 100644 --- a/wifi/1.3/default/Android.mk +++ b/wifi/1.3/default/Android.mk @@ -142,12 +142,14 @@ LOCAL_CPPFLAGS := -Wall -Werror -Wextra LOCAL_SRC_FILES := \ tests/hidl_struct_util_unit_tests.cpp \ tests/main.cpp \ + tests/mock_interface_tool.cpp \ tests/mock_wifi_feature_flags.cpp \ tests/mock_wifi_iface_util.cpp \ tests/mock_wifi_legacy_hal.cpp \ tests/mock_wifi_mode_controller.cpp \ tests/ringbuffer_unit_tests.cpp \ tests/wifi_ap_iface_unit_tests.cpp \ + tests/wifi_nan_iface_unit_tests.cpp \ tests/wifi_chip_unit_tests.cpp \ tests/wifi_iface_util_unit_tests.cpp LOCAL_STATIC_LIBRARIES := \ diff --git a/wifi/1.3/default/service.cpp b/wifi/1.3/default/service.cpp index 73015cf054..fcbc37c12b 100644 --- a/wifi/1.3/default/service.cpp +++ b/wifi/1.3/default/service.cpp @@ -48,12 +48,14 @@ int main(int /*argc*/, char** argv) { configureRpcThreadpool(1, true /* callerWillJoin */); + const auto iface_tool = + std::make_shared(); // Setup hwbinder service android::sp service = new android::hardware::wifi::V1_3::implementation::Wifi( - std::make_shared(), + iface_tool, std::make_shared(iface_tool), std::make_shared(), - std::make_shared(), + std::make_shared(iface_tool), std::make_shared()); if (kLazyService) { LazyServiceRegistrar registrar; diff --git a/wifi/1.3/default/tests/mock_interface_tool.cpp b/wifi/1.3/default/tests/mock_interface_tool.cpp new file mode 100644 index 0000000000..b99a16446c --- /dev/null +++ b/wifi/1.3/default/tests/mock_interface_tool.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * 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 +#include +#include + +#undef NAN // This is weird, NAN is defined in bionic/libc/include/math.h:38 +#include "mock_interface_tool.h" + +namespace android { +namespace wifi_system { + +MockInterfaceTool::MockInterfaceTool() {} + +} // namespace wifi_system +} // namespace android diff --git a/wifi/1.3/default/tests/mock_interface_tool.h b/wifi/1.3/default/tests/mock_interface_tool.h new file mode 100644 index 0000000000..0f17551f96 --- /dev/null +++ b/wifi/1.3/default/tests/mock_interface_tool.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * 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 MOCK_INTERFACE_TOOL_H +#define MOCK_INTERFACE_TOOL_H + +#include +#include + +namespace android { +namespace wifi_system { + +class MockInterfaceTool : public InterfaceTool { + public: + MockInterfaceTool(); + + MOCK_METHOD1(GetUpState, bool(const char* if_name)); + MOCK_METHOD2(SetUpState, bool(const char* if_name, bool request_up)); + MOCK_METHOD1(SetWifiUpState, bool(bool request_up)); + MOCK_METHOD2(SetMacAddress, + bool(const char* if_name, + const std::array& address)); + MOCK_METHOD1(GetFactoryMacAddress, + std::array(const char* if_name)); + +}; // class MockInterfaceTool + +} // namespace wifi_system +} // namespace android + +#endif // MOCK_INTERFACE_TOOL_H diff --git a/wifi/1.3/default/tests/mock_wifi_iface_util.cpp b/wifi/1.3/default/tests/mock_wifi_iface_util.cpp index 706cb6a5ef..3d877c0cbf 100644 --- a/wifi/1.3/default/tests/mock_wifi_iface_util.cpp +++ b/wifi/1.3/default/tests/mock_wifi_iface_util.cpp @@ -28,7 +28,9 @@ namespace V1_3 { namespace implementation { namespace iface_util { -MockWifiIfaceUtil::MockWifiIfaceUtil() : WifiIfaceUtil() {} +MockWifiIfaceUtil::MockWifiIfaceUtil( + const std::weak_ptr iface_tool) + : WifiIfaceUtil(iface_tool) {} } // namespace iface_util } // namespace implementation } // namespace V1_3 diff --git a/wifi/1.3/default/tests/mock_wifi_iface_util.h b/wifi/1.3/default/tests/mock_wifi_iface_util.h index 87ab5db412..8ec93eb471 100644 --- a/wifi/1.3/default/tests/mock_wifi_iface_util.h +++ b/wifi/1.3/default/tests/mock_wifi_iface_util.h @@ -30,12 +30,16 @@ namespace iface_util { class MockWifiIfaceUtil : public WifiIfaceUtil { public: - MockWifiIfaceUtil(); + MockWifiIfaceUtil( + const std::weak_ptr iface_tool); MOCK_METHOD1(getFactoryMacAddress, std::array(const std::string&)); MOCK_METHOD2(setMacAddress, bool(const std::string&, const std::array&)); MOCK_METHOD0(getOrCreateRandomMacAddress, std::array()); + MOCK_METHOD2(registerIfaceEventHandlers, + void(const std::string&, IfaceEventHandlers)); + MOCK_METHOD1(unregisterIfaceEventHandlers, void(const std::string&)); }; } // namespace iface_util } // namespace implementation diff --git a/wifi/1.3/default/tests/mock_wifi_legacy_hal.cpp b/wifi/1.3/default/tests/mock_wifi_legacy_hal.cpp index 4cd279d543..0a202c42d2 100644 --- a/wifi/1.3/default/tests/mock_wifi_legacy_hal.cpp +++ b/wifi/1.3/default/tests/mock_wifi_legacy_hal.cpp @@ -28,7 +28,9 @@ namespace V1_3 { namespace implementation { namespace legacy_hal { -MockWifiLegacyHal::MockWifiLegacyHal() : WifiLegacyHal() {} +MockWifiLegacyHal::MockWifiLegacyHal( + const std::weak_ptr iface_tool) + : WifiLegacyHal(iface_tool) {} } // namespace legacy_hal } // namespace implementation } // namespace V1_3 diff --git a/wifi/1.3/default/tests/mock_wifi_legacy_hal.h b/wifi/1.3/default/tests/mock_wifi_legacy_hal.h index 53fa8d6f4c..81cb1ded86 100644 --- a/wifi/1.3/default/tests/mock_wifi_legacy_hal.h +++ b/wifi/1.3/default/tests/mock_wifi_legacy_hal.h @@ -30,7 +30,8 @@ namespace legacy_hal { class MockWifiLegacyHal : public WifiLegacyHal { public: - MockWifiLegacyHal(); + MockWifiLegacyHal( + const std::weak_ptr iface_tool); MOCK_METHOD0(initialize, wifi_error()); MOCK_METHOD0(start, wifi_error()); MOCK_METHOD2(stop, wifi_error(std::unique_lock*, diff --git a/wifi/1.3/default/tests/wifi_ap_iface_unit_tests.cpp b/wifi/1.3/default/tests/wifi_ap_iface_unit_tests.cpp index 28e028b675..680f534c41 100644 --- a/wifi/1.3/default/tests/wifi_ap_iface_unit_tests.cpp +++ b/wifi/1.3/default/tests/wifi_ap_iface_unit_tests.cpp @@ -22,6 +22,7 @@ #undef NAN // This is weird, NAN is defined in bionic/libc/include/math.h:38 #include "wifi_ap_iface.h" +#include "mock_interface_tool.h" #include "mock_wifi_feature_flags.h" #include "mock_wifi_iface_util.h" #include "mock_wifi_legacy_hal.h" @@ -42,10 +43,12 @@ namespace implementation { class WifiApIfaceTest : public Test { protected: + std::shared_ptr> iface_tool_{ + new NiceMock}; std::shared_ptr> legacy_hal_{ - new NiceMock}; + new NiceMock(iface_tool_)}; std::shared_ptr> iface_util_{ - new NiceMock}; + new NiceMock(iface_tool_)}; std::shared_ptr> feature_flags_{new NiceMock}; }; diff --git a/wifi/1.3/default/tests/wifi_chip_unit_tests.cpp b/wifi/1.3/default/tests/wifi_chip_unit_tests.cpp index 22359e956c..d8ce2785b7 100644 --- a/wifi/1.3/default/tests/wifi_chip_unit_tests.cpp +++ b/wifi/1.3/default/tests/wifi_chip_unit_tests.cpp @@ -22,6 +22,7 @@ #undef NAN // This is weird, NAN is defined in bionic/libc/include/math.h:38 #include "wifi_chip.h" +#include "mock_interface_tool.h" #include "mock_wifi_feature_flags.h" #include "mock_wifi_iface_util.h" #include "mock_wifi_legacy_hal.h" @@ -263,12 +264,14 @@ class WifiChipTest : public Test { sp chip_; ChipId chip_id_ = kFakeChipId; + std::shared_ptr> iface_tool_{ + new NiceMock}; std::shared_ptr> legacy_hal_{ - new NiceMock}; + new NiceMock(iface_tool_)}; std::shared_ptr> mode_controller_{new NiceMock}; std::shared_ptr> iface_util_{ - new NiceMock}; + new NiceMock(iface_tool_)}; std::shared_ptr> feature_flags_{new NiceMock}; diff --git a/wifi/1.3/default/tests/wifi_iface_util_unit_tests.cpp b/wifi/1.3/default/tests/wifi_iface_util_unit_tests.cpp index e5758facdb..28d23ffa71 100644 --- a/wifi/1.3/default/tests/wifi_iface_util_unit_tests.cpp +++ b/wifi/1.3/default/tests/wifi_iface_util_unit_tests.cpp @@ -21,10 +21,15 @@ #undef NAN #include "wifi_iface_util.h" +#include "mock_interface_tool.h" + +using testing::NiceMock; using testing::Test; namespace { constexpr uint8_t kValidUnicastLocallyAssignedMacAddressMask = 0x02; +constexpr uint8_t kMacAddress[] = {0x02, 0x12, 0x45, 0x56, 0xab, 0xcc}; +constexpr char kIfaceName[] = "test-wlan0"; bool isValidUnicastLocallyAssignedMacAddress( const std::array& mac_address) { @@ -38,19 +43,52 @@ namespace hardware { namespace wifi { namespace V1_3 { namespace implementation { +namespace iface_util { class WifiIfaceUtilTest : public Test { protected: - iface_util::WifiIfaceUtil iface_util_; + std::shared_ptr> iface_tool_{ + new NiceMock}; + WifiIfaceUtil* iface_util_ = new WifiIfaceUtil(iface_tool_); }; TEST_F(WifiIfaceUtilTest, GetOrCreateRandomMacAddress) { - auto mac_address = iface_util_.getOrCreateRandomMacAddress(); + auto mac_address = iface_util_->getOrCreateRandomMacAddress(); ASSERT_TRUE(isValidUnicastLocallyAssignedMacAddress(mac_address)); // All further calls should return the same MAC address. - ASSERT_EQ(mac_address, iface_util_.getOrCreateRandomMacAddress()); - ASSERT_EQ(mac_address, iface_util_.getOrCreateRandomMacAddress()); + ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress()); + ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress()); } + +TEST_F(WifiIfaceUtilTest, IfaceEventHandlers_SetMacAddress) { + std::array mac_address = {}; + std::copy(std::begin(kMacAddress), std::end(kMacAddress), + std::begin(mac_address)); + EXPECT_CALL(*iface_tool_, SetMacAddress(testing::_, testing::_)) + .WillRepeatedly(testing::Return(true)); + EXPECT_CALL(*iface_tool_, SetUpState(testing::_, testing::_)) + .WillRepeatedly(testing::Return(true)); + + // Register for iface state toggle events. + bool callback_invoked = false; + iface_util::IfaceEventHandlers event_handlers = {}; + event_handlers.on_state_toggle_off_on = + [&callback_invoked](const std::string& /* iface_name */) { + callback_invoked = true; + }; + iface_util_->registerIfaceEventHandlers(kIfaceName, event_handlers); + // Invoke setMacAddress and ensure that the cb is invoked. + ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address)); + ASSERT_TRUE(callback_invoked); + + // Unregister for iface state toggle events. + callback_invoked = false; + iface_util_->unregisterIfaceEventHandlers(kIfaceName); + // Invoke setMacAddress and ensure that the cb is not invoked. + ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address)); + ASSERT_FALSE(callback_invoked); +} +} // namespace iface_util } // namespace implementation } // namespace V1_3 } // namespace wifi diff --git a/wifi/1.3/default/tests/wifi_nan_iface_unit_tests.cpp b/wifi/1.3/default/tests/wifi_nan_iface_unit_tests.cpp new file mode 100644 index 0000000000..eb6c61065d --- /dev/null +++ b/wifi/1.3/default/tests/wifi_nan_iface_unit_tests.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2019, The Android Open Source Project + * + * 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 +#include +#include +#include + +#undef NAN // This is weird, NAN is defined in bionic/libc/include/math.h:38 +#include "wifi_nan_iface.h" + +#include "mock_interface_tool.h" +#include "mock_wifi_feature_flags.h" +#include "mock_wifi_iface_util.h" +#include "mock_wifi_legacy_hal.h" + +using testing::NiceMock; +using testing::Return; +using testing::Test; + +namespace { +constexpr char kIfaceName[] = "mockWlan0"; +} // namespace + +namespace android { +namespace hardware { +namespace wifi { +namespace V1_3 { +namespace implementation { + +bool CaptureIfaceEventHandlers( + const std::string& /* iface_name*/, + iface_util::IfaceEventHandlers in_iface_event_handlers, + iface_util::IfaceEventHandlers* out_iface_event_handlers) { + *out_iface_event_handlers = in_iface_event_handlers; + return true; +} + +class MockNanIfaceEventCallback : public IWifiNanIfaceEventCallback { + public: + MockNanIfaceEventCallback() = default; + + MOCK_METHOD3(notifyCapabilitiesResponse, + Return(uint16_t, const WifiNanStatus&, + const NanCapabilities&)); + MOCK_METHOD2(notifyEnableResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyConfigResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyDisableResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD3(notifyStartPublishResponse, + Return(uint16_t, const WifiNanStatus&, uint8_t)); + MOCK_METHOD2(notifyStopPublishResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD3(notifyStartSubscribeResponse, + Return(uint16_t, const WifiNanStatus&, uint8_t)); + MOCK_METHOD2(notifyStopSubscribeResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyTransmitFollowupResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyCreateDataInterfaceResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyDeleteDataInterfaceResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD3(notifyInitiateDataPathResponse, + Return(uint16_t, const WifiNanStatus&, uint32_t)); + MOCK_METHOD2(notifyRespondToDataPathIndicationResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD2(notifyTerminateDataPathResponse, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD1(eventClusterEvent, Return(const NanClusterEventInd&)); + MOCK_METHOD1(eventDisabled, Return(const WifiNanStatus&)); + MOCK_METHOD2(eventPublishTerminated, + Return(uint8_t, const WifiNanStatus&)); + MOCK_METHOD2(eventSubscribeTerminated, + Return(uint8_t, const WifiNanStatus&)); + MOCK_METHOD1(eventMatch, Return(const NanMatchInd&)); + MOCK_METHOD2(eventMatchExpired, Return(uint8_t, uint32_t)); + MOCK_METHOD1(eventFollowupReceived, + Return(const NanFollowupReceivedInd&)); + MOCK_METHOD2(eventTransmitFollowup, + Return(uint16_t, const WifiNanStatus&)); + MOCK_METHOD1(eventDataPathRequest, + Return(const NanDataPathRequestInd&)); + MOCK_METHOD1(eventDataPathConfirm, + Return(const NanDataPathConfirmInd&)); + MOCK_METHOD1(eventDataPathTerminated, Return(uint32_t)); +}; + +class WifiNanIfaceTest : public Test { + protected: + std::shared_ptr> iface_tool_{ + new NiceMock}; + std::shared_ptr> legacy_hal_{ + new NiceMock(iface_tool_)}; + std::shared_ptr> iface_util_{ + new NiceMock(iface_tool_)}; +}; + +TEST_F(WifiNanIfaceTest, IfacEventHandlers_OnStateToggleOffOn) { + iface_util::IfaceEventHandlers captured_iface_event_handlers = {}; + EXPECT_CALL(*legacy_hal_, + nanRegisterCallbackHandlers(testing::_, testing::_)) + .WillOnce(testing::Return(legacy_hal::WIFI_SUCCESS)); + EXPECT_CALL(*iface_util_, + registerIfaceEventHandlers(testing::_, testing::_)) + .WillOnce(testing::Invoke( + bind(CaptureIfaceEventHandlers, std::placeholders::_1, + std::placeholders::_2, &captured_iface_event_handlers))); + sp nan_iface = + new WifiNanIface(kIfaceName, legacy_hal_, iface_util_); + + // Register a mock nan event callback. + sp> mock_event_callback{ + new NiceMock}; + nan_iface->registerEventCallback( + mock_event_callback, [](const WifiStatus& status) { + ASSERT_EQ(WifiStatusCode::SUCCESS, status.code); + }); + // Ensure that the eventDisabled() function in mock callback will be + // invoked. + WifiNanStatus expected_nan_status = { + NanStatusType::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""}; + EXPECT_CALL(*mock_event_callback, eventDisabled(expected_nan_status)) + .Times(1); + + // Trigger the iface state toggle callback. + captured_iface_event_handlers.on_state_toggle_off_on(kIfaceName); +} +} // namespace implementation +} // namespace V1_3 +} // namespace wifi +} // namespace hardware +} // namespace android diff --git a/wifi/1.3/default/wifi.cpp b/wifi/1.3/default/wifi.cpp index 1f640856c2..2f21819dbc 100644 --- a/wifi/1.3/default/wifi.cpp +++ b/wifi/1.3/default/wifi.cpp @@ -34,11 +34,13 @@ using hidl_return_util::validateAndCall; using hidl_return_util::validateAndCallWithLock; Wifi::Wifi( + const std::shared_ptr iface_tool, const std::shared_ptr legacy_hal, const std::shared_ptr mode_controller, const std::shared_ptr iface_util, const std::shared_ptr feature_flags) - : legacy_hal_(legacy_hal), + : iface_tool_(iface_tool), + legacy_hal_(legacy_hal), mode_controller_(mode_controller), iface_util_(iface_util), feature_flags_(feature_flags), diff --git a/wifi/1.3/default/wifi.h b/wifi/1.3/default/wifi.h index c4ab7732bd..1c2a15409e 100644 --- a/wifi/1.3/default/wifi.h +++ b/wifi/1.3/default/wifi.h @@ -40,7 +40,8 @@ namespace implementation { */ class Wifi : public V1_3::IWifi { public: - Wifi(const std::shared_ptr legacy_hal, + Wifi(const std::shared_ptr iface_tool, + const std::shared_ptr legacy_hal, const std::shared_ptr mode_controller, const std::shared_ptr iface_util, @@ -77,6 +78,7 @@ class Wifi : public V1_3::IWifi { // Instance is created in this root level |IWifi| HIDL interface object // and shared with all the child HIDL interface objects. + std::shared_ptr iface_tool_; std::shared_ptr legacy_hal_; std::shared_ptr mode_controller_; std::shared_ptr iface_util_; diff --git a/wifi/1.3/default/wifi_iface_util.cpp b/wifi/1.3/default/wifi_iface_util.cpp index 7042af0b87..34bc02d3f9 100644 --- a/wifi/1.3/default/wifi_iface_util.cpp +++ b/wifi/1.3/default/wifi_iface_util.cpp @@ -39,25 +39,28 @@ namespace V1_3 { namespace implementation { namespace iface_util { -WifiIfaceUtil::WifiIfaceUtil() - : random_mac_address_(nullptr), event_handlers_map_() {} +WifiIfaceUtil::WifiIfaceUtil( + const std::weak_ptr iface_tool) + : iface_tool_(iface_tool), + random_mac_address_(nullptr), + event_handlers_map_() {} std::array WifiIfaceUtil::getFactoryMacAddress( const std::string& iface_name) { - return iface_tool_.GetFactoryMacAddress(iface_name.c_str()); + return iface_tool_.lock()->GetFactoryMacAddress(iface_name.c_str()); } bool WifiIfaceUtil::setMacAddress(const std::string& iface_name, const std::array& mac) { - if (!iface_tool_.SetUpState(iface_name.c_str(), false)) { + if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), false)) { LOG(ERROR) << "SetUpState(false) failed."; return false; } - if (!iface_tool_.SetMacAddress(iface_name.c_str(), mac)) { + if (!iface_tool_.lock()->SetMacAddress(iface_name.c_str(), mac)) { LOG(ERROR) << "SetMacAddress failed."; return false; } - if (!iface_tool_.SetUpState(iface_name.c_str(), true)) { + if (!iface_tool_.lock()->SetUpState(iface_name.c_str(), true)) { LOG(ERROR) << "SetUpState(true) failed."; return false; } diff --git a/wifi/1.3/default/wifi_iface_util.h b/wifi/1.3/default/wifi_iface_util.h index 325fbe8390..98073e0763 100644 --- a/wifi/1.3/default/wifi_iface_util.h +++ b/wifi/1.3/default/wifi_iface_util.h @@ -40,7 +40,7 @@ struct IfaceEventHandlers { */ class WifiIfaceUtil { public: - WifiIfaceUtil(); + WifiIfaceUtil(const std::weak_ptr iface_tool); virtual ~WifiIfaceUtil() = default; virtual std::array getFactoryMacAddress( @@ -60,7 +60,7 @@ class WifiIfaceUtil { private: std::array createRandomMacAddress(); - wifi_system::InterfaceTool iface_tool_; + std::weak_ptr iface_tool_; std::unique_ptr> random_mac_address_; std::map event_handlers_map_; }; diff --git a/wifi/1.3/default/wifi_legacy_hal.cpp b/wifi/1.3/default/wifi_legacy_hal.cpp index 7f9b63575e..485bd16634 100644 --- a/wifi/1.3/default/wifi_legacy_hal.cpp +++ b/wifi/1.3/default/wifi_legacy_hal.cpp @@ -333,10 +333,12 @@ void onAsyncNanEventScheduleUpdate(NanDataPathScheduleUpdateInd* event) { } // End of the free-standing "C" style callbacks. -WifiLegacyHal::WifiLegacyHal() +WifiLegacyHal::WifiLegacyHal( + const std::weak_ptr iface_tool) : global_handle_(nullptr), awaiting_event_loop_termination_(false), - is_started_(false) {} + is_started_(false), + iface_tool_(iface_tool) {} wifi_error WifiLegacyHal::initialize() { LOG(DEBUG) << "Initialize legacy HAL"; @@ -371,7 +373,7 @@ wifi_error WifiLegacyHal::start() { property_set(kDriverPropName, "ok"); LOG(DEBUG) << "Starting legacy HAL"; - if (!iface_tool_.SetWifiUpState(true)) { + if (!iface_tool_.lock()->SetWifiUpState(true)) { LOG(ERROR) << "Failed to set WiFi interface up"; return WIFI_ERROR_UNKNOWN; } @@ -407,7 +409,7 @@ wifi_error WifiLegacyHal::stop( // Invalidate all the internal pointers now that the HAL is // stopped. invalidate(); - iface_tool_.SetWifiUpState(false); + iface_tool_.lock()->SetWifiUpState(false); on_stop_complete_user_callback(); is_started_ = false; }; diff --git a/wifi/1.3/default/wifi_legacy_hal.h b/wifi/1.3/default/wifi_legacy_hal.h index a3ed460b11..9cfa172402 100644 --- a/wifi/1.3/default/wifi_legacy_hal.h +++ b/wifi/1.3/default/wifi_legacy_hal.h @@ -170,7 +170,7 @@ using on_radio_mode_change_callback = */ class WifiLegacyHal { public: - WifiLegacyHal(); + WifiLegacyHal(const std::weak_ptr iface_tool); virtual ~WifiLegacyHal() = default; // Initialize the legacy HAL function table. @@ -391,7 +391,7 @@ class WifiLegacyHal { std::condition_variable_any stop_wait_cv_; // Flag to indicate if the legacy HAL has been started. bool is_started_; - wifi_system::InterfaceTool iface_tool_; + std::weak_ptr iface_tool_; }; } // namespace legacy_hal