From 23085162e3556660090f264e97913ec1d84ac526 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 17 Feb 2023 18:05:46 +0100 Subject: [PATCH] [Tizen] Enable WiFi management from example app (#23633) * [Tizen] Simplify WiFiManager::IsActivated usage * Apply clang-tidy modernize-redundant-void-arg * Fix incorrect feature flag for enabling WiFi * [Tizen] Implement WiFi started check function The IsWiFiManagementStarted is used by the example Linux applications to check whether CHIP applications is connected with wpa_supplicant WiFi manager. Since Tizen examples are based on Linux code and Tizen uses wpa_supplicant as well, Tizen should also implement such check function. * Activate/deactivate WiFi without async wrapper * Add network.set privilege required for WiFi management * Fix compilation failure after syncing with master --- .../all-clusters-app/tizen/tizen-manifest.xml | 1 + .../tizen/tizen-manifest.xml | 1 + .../lighting-app/tizen/tizen-manifest.xml | 1 + examples/platform/tizen/OptionsProxy.cpp | 2 +- src/platform/BUILD.gn | 2 - .../Tizen/ConnectivityManagerImpl.cpp | 14 ++- src/platform/Tizen/ConnectivityManagerImpl.h | 8 +- src/platform/Tizen/WiFiManager.cpp | 86 +++++++------------ src/platform/Tizen/WiFiManager.h | 22 ++--- 9 files changed, 57 insertions(+), 80 deletions(-) diff --git a/examples/all-clusters-app/tizen/tizen-manifest.xml b/examples/all-clusters-app/tizen/tizen-manifest.xml index dfd08655700512..c96e765d907f09 100644 --- a/examples/all-clusters-app/tizen/tizen-manifest.xml +++ b/examples/all-clusters-app/tizen/tizen-manifest.xml @@ -8,6 +8,7 @@ http://tizen.org/privilege/bluetooth http://tizen.org/privilege/internet http://tizen.org/privilege/network.get + http://tizen.org/privilege/network.set true true diff --git a/examples/all-clusters-minimal-app/tizen/tizen-manifest.xml b/examples/all-clusters-minimal-app/tizen/tizen-manifest.xml index ca1b4edc263bdc..f97f57a53591fe 100644 --- a/examples/all-clusters-minimal-app/tizen/tizen-manifest.xml +++ b/examples/all-clusters-minimal-app/tizen/tizen-manifest.xml @@ -8,6 +8,7 @@ http://tizen.org/privilege/bluetooth http://tizen.org/privilege/internet http://tizen.org/privilege/network.get + http://tizen.org/privilege/network.set true true diff --git a/examples/lighting-app/tizen/tizen-manifest.xml b/examples/lighting-app/tizen/tizen-manifest.xml index 86aa047bba3390..68b034c9fd66cc 100644 --- a/examples/lighting-app/tizen/tizen-manifest.xml +++ b/examples/lighting-app/tizen/tizen-manifest.xml @@ -8,6 +8,7 @@ http://tizen.org/privilege/bluetooth http://tizen.org/privilege/internet http://tizen.org/privilege/network.get + http://tizen.org/privilege/network.set true true diff --git a/examples/platform/tizen/OptionsProxy.cpp b/examples/platform/tizen/OptionsProxy.cpp index 590580d48a50ba..158d0343f2b066 100644 --- a/examples/platform/tizen/OptionsProxy.cpp +++ b/examples/platform/tizen/OptionsProxy.cpp @@ -38,7 +38,7 @@ static constexpr Option sOptions[] = { #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE { "ble-device", false }, #endif -#if CHIP_DEVICE_CONFIG_ENABLE_WPA +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI { "wifi", true }, #endif #if CHIP_ENABLE_OPENTHREAD diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 1d2a9665f97abf..ac478a14d0ce52 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -191,8 +191,6 @@ if (chip_device_platform != "none" && chip_device_platform != "external") { "CHIP_DEVICE_LAYER_TARGET=Tizen", "CHIP_DEVICE_CONFIG_ENABLE_WIFI=${chip_enable_wifi}", ] - defines -= - [ "CHIP_DEVICE_CONFIG_ENABLE_WPA=${chip_device_config_enable_wpa}" ] } else if (chip_device_platform == "nrfconnect") { defines += [ "CHIP_DEVICE_LAYER_TARGET_NRFCONNECT=1", diff --git a/src/platform/Tizen/ConnectivityManagerImpl.cpp b/src/platform/Tizen/ConnectivityManagerImpl.cpp index 75a5fb5e5473e0..a954b6b7e0fbfc 100644 --- a/src/platform/Tizen/ConnectivityManagerImpl.cpp +++ b/src/platform/Tizen/ConnectivityManagerImpl.cpp @@ -209,23 +209,21 @@ void ConnectivityManagerImpl::_SetWiFiAPIdleTimeout(System::Clock::Timeout val) void ConnectivityManagerImpl::StartWiFiManagement() { - SystemLayer().ScheduleWork(ActivateWiFiManager, nullptr); + Internal::WiFiMgr().Activate(); } void ConnectivityManagerImpl::StopWiFiManagement() { - SystemLayer().ScheduleWork(DeactivateWiFiManager, nullptr); + Internal::WiFiMgr().Deactivate(); } -void ConnectivityManagerImpl::ActivateWiFiManager(System::Layer * aLayer, void * aAppState) +bool ConnectivityManagerImpl::IsWiFiManagementStarted() { - Internal::WiFiMgr().Activate(); + bool isActivated = false; + Internal::WiFiMgr().IsActivated(&isActivated); + return isActivated; } -void ConnectivityManagerImpl::DeactivateWiFiManager(System::Layer * aLayer, void * aAppState) -{ - Internal::WiFiMgr().Deactivate(); -} #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI } // namespace DeviceLayer diff --git a/src/platform/Tizen/ConnectivityManagerImpl.h b/src/platform/Tizen/ConnectivityManagerImpl.h index b469b631347b3d..32edbc146c9b0e 100644 --- a/src/platform/Tizen/ConnectivityManagerImpl.h +++ b/src/platform/Tizen/ConnectivityManagerImpl.h @@ -84,8 +84,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, public: #if CHIP_DEVICE_CONFIG_ENABLE_WIFI - void StartWiFiManagement(void); - void StopWiFiManagement(void); + void StartWiFiManagement(); + void StopWiFiManagement(); + bool IsWiFiManagementStarted(); #endif private: @@ -115,9 +116,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void _MaintainOnDemandWiFiAP(void); System::Clock::Timeout _GetWiFiAPIdleTimeout(void); void _SetWiFiAPIdleTimeout(System::Clock::Timeout val); - - static void ActivateWiFiManager(System::Layer * aLayer, void * aAppState); - static void DeactivateWiFiManager(System::Layer * aLayer, void * aAppState); #endif // ===== Members for internal use by the following friends. diff --git a/src/platform/Tizen/WiFiManager.cpp b/src/platform/Tizen/WiFiManager.cpp index 7731ffcb26aeb3..10678d271dbdfd 100644 --- a/src/platform/Tizen/WiFiManager.cpp +++ b/src/platform/Tizen/WiFiManager.cpp @@ -601,41 +601,26 @@ void WiFiManager::Deinit() CHIP_ERROR WiFiManager::IsActivated(bool * isWiFiActivated) { - CHIP_ERROR err = CHIP_NO_ERROR; - int wifiErr = WIFI_MANAGER_ERROR_NONE; - - wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, isWiFiActivated); - if (wifiErr == WIFI_MANAGER_ERROR_NONE) - { - ChipLogProgress(DeviceLayer, "WiFi is %s", *isWiFiActivated ? "activated" : "deactivated"); - } - else - { - err = CHIP_ERROR_INCORRECT_STATE; - ChipLogError(DeviceLayer, "FAIL: check whether WiFi is activated [%s]", get_error_message(wifiErr)); - } - - return err; + int wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, isWiFiActivated); + VerifyOrReturnError(wifiErr == WIFI_MANAGER_ERROR_NONE, CHIP_ERROR_INCORRECT_STATE, + ChipLogError(DeviceLayer, "FAIL: Check whether WiFi is activated: %s", get_error_message(wifiErr))); + return CHIP_NO_ERROR; } CHIP_ERROR WiFiManager::Activate() { CHIP_ERROR err = CHIP_NO_ERROR; - int wifiErr = WIFI_MANAGER_ERROR_NONE; bool isWiFiActivated = false; bool dbusAsyncErr = false; - wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, &isWiFiActivated); - VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, err = CHIP_ERROR_INCORRECT_STATE; - ChipLogError(DeviceLayer, "FAIL: check whether WiFi is activated [%s]", get_error_message(wifiErr))); - + VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, ); VerifyOrExit(isWiFiActivated == false, ChipLogProgress(DeviceLayer, "WiFi is already activated")); dbusAsyncErr = MainLoop::Instance().AsyncRequest(_WiFiActivate); - if (dbusAsyncErr == false) - { - err = CHIP_ERROR_INCORRECT_STATE; - } + VerifyOrExit(dbusAsyncErr == true, { + ChipLogError(DeviceLayer, "FAIL: Async request: Activate WiFi"); + err = CHIP_ERROR_INTERNAL; + }); exit: return err; @@ -644,21 +629,17 @@ CHIP_ERROR WiFiManager::Activate() CHIP_ERROR WiFiManager::Deactivate() { CHIP_ERROR err = CHIP_NO_ERROR; - int wifiErr = WIFI_MANAGER_ERROR_NONE; bool isWiFiActivated = false; bool dbusAsyncErr = false; - wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, &isWiFiActivated); - VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, err = CHIP_ERROR_INCORRECT_STATE; - ChipLogError(DeviceLayer, "FAIL: check whether WiFi is activated [%s]", get_error_message(wifiErr))); - + VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, ); VerifyOrExit(isWiFiActivated == true, ChipLogProgress(DeviceLayer, "WiFi is already deactivated")); dbusAsyncErr = MainLoop::Instance().AsyncRequest(_WiFiDeactivate); - if (dbusAsyncErr == false) - { - err = CHIP_ERROR_INCORRECT_STATE; - } + VerifyOrExit(dbusAsyncErr == true, { + ChipLogError(DeviceLayer, "FAIL: Async request: Deactivate WiFi"); + err = CHIP_ERROR_INTERNAL; + }); exit: return err; @@ -668,7 +649,6 @@ CHIP_ERROR WiFiManager::Connect(const char * ssid, const char * key, DeviceLayer::NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * apCallback) { CHIP_ERROR err = CHIP_NO_ERROR; - int wifiErr = WIFI_MANAGER_ERROR_NONE; bool isWiFiActivated = false; bool dbusAsyncErr = false; wifi_manager_ap_h foundAp = nullptr; @@ -676,30 +656,30 @@ CHIP_ERROR WiFiManager::Connect(const char * ssid, const char * key, g_strlcpy(sInstance.mWiFiSSID, ssid, sizeof(sInstance.mWiFiSSID)); g_strlcpy(sInstance.mWiFiKey, key, sizeof(sInstance.mWiFiKey)); - wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, &isWiFiActivated); - VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, err = CHIP_ERROR_INCORRECT_STATE; - ChipLogError(DeviceLayer, "FAIL: check whether WiFi is activated [%s]", get_error_message(wifiErr))); - - VerifyOrExit(isWiFiActivated == true, ChipLogProgress(DeviceLayer, "WiFi is deactivated")); + VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, ); + VerifyOrExit(isWiFiActivated == true, { + ChipLogProgress(DeviceLayer, "WiFi is not activated"); + err = CHIP_ERROR_INCORRECT_STATE; + }); sInstance.mpConnectCallback = apCallback; foundAp = sInstance._WiFiGetFoundAP(); if (foundAp != nullptr) { - dbusAsyncErr = MainLoop::Instance().AsyncRequest(_WiFiConnect, static_cast(foundAp)); - if (dbusAsyncErr == false) - { - err = CHIP_ERROR_INCORRECT_STATE; - } + dbusAsyncErr = MainLoop::Instance().AsyncRequest(_WiFiConnect, foundAp); + VerifyOrExit(dbusAsyncErr == true, { + ChipLogError(DeviceLayer, "FAIL: Async request: Connect WiFi"); + err = CHIP_ERROR_INTERNAL; + }); } else { dbusAsyncErr = MainLoop::Instance().AsyncRequest(_WiFiScan); - if (dbusAsyncErr == false) - { - err = CHIP_ERROR_INCORRECT_STATE; - } + VerifyOrExit(dbusAsyncErr == true, { + ChipLogError(DeviceLayer, "FAIL: Async request: Scan WiFi"); + err = CHIP_ERROR_INTERNAL; + }); } exit: @@ -715,11 +695,11 @@ CHIP_ERROR WiFiManager::Disconnect(const char * ssid) g_strlcpy(sInstance.mWiFiSSID, ssid, sizeof(sInstance.mWiFiSSID)); - wifiErr = wifi_manager_is_activated(sInstance.mWiFiManagerHandle, &isWiFiActivated); - VerifyOrExit(wifiErr == WIFI_MANAGER_ERROR_NONE, err = CHIP_ERROR_INCORRECT_STATE; - ChipLogError(DeviceLayer, "FAIL: check whether WiFi is activated [%s]", get_error_message(wifiErr))); - - VerifyOrExit(isWiFiActivated == true, ChipLogProgress(DeviceLayer, "WiFi is deactivated")); + VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, ); + VerifyOrExit(isWiFiActivated == true, { + ChipLogProgress(DeviceLayer, "WiFi is not activated"); + err = CHIP_ERROR_INCORRECT_STATE; + }); foundAp = sInstance._WiFiGetFoundAP(); VerifyOrExit(foundAp != nullptr, ); diff --git a/src/platform/Tizen/WiFiManager.h b/src/platform/Tizen/WiFiManager.h index f37e2f1240b7b8..87da73aafc16a9 100644 --- a/src/platform/Tizen/WiFiManager.h +++ b/src/platform/Tizen/WiFiManager.h @@ -37,16 +37,16 @@ class WiFiManager friend class ConnectivityManagerImpl; public: - void Init(void); - void Deinit(void); + void Init(); + void Deinit(); CHIP_ERROR IsActivated(bool * isWiFiActivated); - CHIP_ERROR Activate(void); - CHIP_ERROR Deactivate(void); + CHIP_ERROR Activate(); + CHIP_ERROR Deactivate(); CHIP_ERROR Connect(const char * ssid, const char * key, NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * apCallback = nullptr); CHIP_ERROR Disconnect(const char * ssid); - CHIP_ERROR RemoveAllConfigs(void); + CHIP_ERROR RemoveAllConfigs(); CHIP_ERROR GetDeviceMACAddress(uint8_t * macAddress, size_t macAddressLen); CHIP_ERROR GetDeviceState(wifi_manager_device_state_e * deviceState); @@ -75,16 +75,16 @@ class WiFiManager static gboolean _WiFiScan(GMainLoop * mainLoop, gpointer userData); static gboolean _WiFiConnect(GMainLoop * mainLoop, gpointer userData); - void _WiFiDeinitialize(void); - void _WiFiSetStates(void); - void _WiFiSetCallbacks(void); - void _WiFiUnsetCallbacks(void); + void _WiFiDeinitialize(); + void _WiFiSetStates(); + void _WiFiSetCallbacks(); + void _WiFiUnsetCallbacks(); void _WiFiSetDeviceState(wifi_manager_device_state_e deviceState); void _WiFiSetModuleState(wifi_manager_module_state_e moduleState); void _WiFiSetConnectionState(wifi_manager_connection_state_e connectionState); - wifi_manager_ap_h _WiFiGetFoundAP(void); + wifi_manager_ap_h _WiFiGetFoundAP(); - friend WiFiManager & WiFiMgr(void); + friend WiFiManager & WiFiMgr(); static WiFiManager sInstance;