diff --git a/examples/all-clusters-app/ameba/chip_main.cmake b/examples/all-clusters-app/ameba/chip_main.cmake index 52a77e443857f3..1c51d88663b886 100755 --- a/examples/all-clusters-app/ameba/chip_main.cmake +++ b/examples/all-clusters-app/ameba/chip_main.cmake @@ -121,6 +121,7 @@ list( ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp ${chip_dir}/src/app/clusters/ota-requestor/ota-requestor-server.cpp + ${chip_dir}/examples/platform/ameba/ota/OTAInitializer.cpp ) endif (matter_enable_ota_requestor) @@ -178,6 +179,7 @@ target_include_directories( ${chip_dir}/examples/all-clusters-app/all-clusters-common ${chip_dir}/examples/all-clusters-app/all-clusters-common/include ${chip_dir}/examples/all-clusters-app/ameba/main/include + ${chip_dir}/examples/platform/ameba ${chip_dir_output}/gen/include ${chip_dir}/src/include/ ${chip_dir}/src/lib/ @@ -240,14 +242,6 @@ list( ) endif (matter_enable_rpc) -if (matter_enable_ota_requestor) -list( - APPEND chip_main_flags - - -DCONFIG_ENABLE_OTA_REQUESTOR=1 -) -endif (matter_enable_ota_requestor) - list( APPEND chip_main_cpp_flags diff --git a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp index 8f18f730999cde..2db5f21cee2c85 100644 --- a/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp @@ -36,6 +36,9 @@ #include #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +#include +#endif #include "Globals.h" #include "LEDWidget.h" @@ -50,7 +53,15 @@ using namespace ::chip::DeviceManager; using namespace ::chip::Logging; uint32_t identifyTimerCount; -constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kInitOTARequestorDelaySec = 3; + +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + OTAInitializer::Instance().InitOTARequestor(); +} +#endif void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { @@ -97,6 +108,9 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + static bool isOTAInitialized = false; +#endif if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ChipLogProgress(DeviceLayer, "IPv4 Server ready..."); @@ -110,6 +124,15 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { ChipLogProgress(DeviceLayer, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + // Init OTA requestor only when we have gotten IPv6 address + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } +#endif } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index 306d9c36717e00..8dc7e1e9edb1ea 100644 --- a/examples/all-clusters-app/ameba/main/chipinterface.cpp +++ b/examples/all-clusters-app/ameba/main/chipinterface.cpp @@ -39,16 +39,6 @@ #include #include -#if CONFIG_ENABLE_OTA_REQUESTOR -#include "app/clusters/ota-requestor/DefaultOTARequestorStorage.h" -#include -#include -#include -#include -#include -#include -#endif - #if CONFIG_ENABLE_PW_RPC #include "Rpc.h" #endif @@ -100,55 +90,6 @@ Identify gIdentify1 = { static DeviceCallbacks EchoCallbacks; -#if CONFIG_ENABLE_OTA_REQUESTOR -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -ExtendedOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -AmebaOTAImageProcessor gImageProcessor; -chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider; -static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted; -#endif - -#if CONFIG_ENABLE_OTA_REQUESTOR -extern "C" void amebaQueryImageCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaQueryImageCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); }); -} - -extern "C" void amebaApplyUpdateCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaApplyUpdateCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); }); -} - -static void InitOTARequestor(void) -{ - // Initialize and interconnect the Requestor and Image Processor objects -- START - SetRequestorInstance(&gRequestorCore); - - gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage()); - - // Set server instance used for session establishment - gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - - gImageProcessor.SetOTADownloader(&gDownloader); - - // Connect the Downloader and Image Processor objects - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - - if (gUserConsentState != chip::ota::UserConsentState::kUnknown) - { - gUserConsentProvider.SetUserConsentState(gUserConsentState); - gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); - } - - // Initialize and interconnect the Requestor and Image Processor objects -- END -} -#endif // CONFIG_ENABLE_OTA_REQUESTOR - static void InitServer(intptr_t context) { // Init ZCL Data Model and CHIP App Server @@ -160,10 +101,6 @@ static void InitServer(intptr_t context) SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); NetWorkCommissioningInstInit(); -#if CONFIG_ENABLE_OTA_REQUESTOR - InitOTARequestor(); -#endif - if (RTW_SUCCESS != wifi_is_connected_to_ap()) { // QR code will be used with CHIP Tool diff --git a/examples/lighting-app/ameba/chip_main.cmake b/examples/lighting-app/ameba/chip_main.cmake index 6ba0531e97f357..3d17b4aebd3194 100755 --- a/examples/lighting-app/ameba/chip_main.cmake +++ b/examples/lighting-app/ameba/chip_main.cmake @@ -19,6 +19,7 @@ list( ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp ${chip_dir}/src/app/clusters/ota-requestor/ota-requestor-server.cpp + ${chip_dir}/examples/platform/ameba/ota/OTAInitializer.cpp ) endif (matter_enable_ota_requestor) @@ -59,6 +60,7 @@ target_include_directories( ${chip_dir}/examples/lighting-app/lighting-common ${chip_dir}/examples/lighting-app/lighting-common/include ${chip_dir}/examples/lighting-app/ameba/main/include + ${chip_dir}/examples/platform/ameba ${chip_dir_output}/gen/include ${chip_dir}/src/include/ ${chip_dir}/src/lib/ @@ -84,14 +86,6 @@ list( -DMATTER_LIGHTING_APP=1 ) -if (matter_enable_ota_requestor) -list( - APPEND chip_main_flags - - -DCONFIG_ENABLE_OTA_REQUESTOR=1 -) -endif (matter_enable_ota_requestor) - list( APPEND chip_main_cpp_flags diff --git a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp index cb023f73b6ea87..754a79651b7063 100644 --- a/examples/lighting-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/ameba/main/DeviceCallbacks.cpp @@ -37,6 +37,10 @@ #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +#include +#endif + static const char * TAG = "app-devicecallbacks"; using namespace ::chip; @@ -47,7 +51,15 @@ using namespace ::chip::DeviceManager; using namespace ::chip::Logging; uint32_t identifyTimerCount; -constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kInitOTARequestorDelaySec = 3; + +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + OTAInitializer::Instance().InitOTARequestor(); +} +#endif void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { @@ -76,6 +88,10 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + static bool isOTAInitialized = false; +#endif + if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { printf("IPv4 Server ready..."); @@ -89,6 +105,15 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { printf("IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + // Init OTA requestor only when we have gotten IPv6 address + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } +#endif } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/lighting-app/ameba/main/chipinterface.cpp b/examples/lighting-app/ameba/main/chipinterface.cpp index 6ef8940aaacfc3..ecede097cb7441 100644 --- a/examples/lighting-app/ameba/main/chipinterface.cpp +++ b/examples/lighting-app/ameba/main/chipinterface.cpp @@ -42,16 +42,6 @@ #include -#if CONFIG_ENABLE_OTA_REQUESTOR -#include "app/clusters/ota-requestor/DefaultOTARequestorStorage.h" -#include -#include -#include -#include -#include -#include -#endif - using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; @@ -85,55 +75,6 @@ void NetWorkCommissioningInstInit() static DeviceCallbacks EchoCallbacks; -#if CONFIG_ENABLE_OTA_REQUESTOR -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -ExtendedOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -AmebaOTAImageProcessor gImageProcessor; -chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider; -static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted; -#endif - -#if CONFIG_ENABLE_OTA_REQUESTOR -extern "C" void amebaQueryImageCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaQueryImageCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); }); -} - -extern "C" void amebaApplyUpdateCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaApplyUpdateCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); }); -} - -static void InitOTARequestor(void) -{ - // Initialize and interconnect the Requestor and Image Processor objects -- START - SetRequestorInstance(&gRequestorCore); - - gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - - // Set server instance used for session establishment - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - - gImageProcessor.SetOTADownloader(&gDownloader); - - // Connect the Downloader and Image Processor objects - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - - if (gUserConsentState != chip::ota::UserConsentState::kUnknown) - { - gUserConsentProvider.SetUserConsentState(gUserConsentState); - gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); - } - - // Initialize and interconnect the Requestor and Image Processor objects -- END -} -#endif // CONFIG_ENABLE_OTA_REQUESTOR - void OnIdentifyStart(Identify *) { ChipLogProgress(Zcl, "OnIdentifyStart"); @@ -181,10 +122,6 @@ static void InitServer(intptr_t context) SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); NetWorkCommissioningInstInit(); -#if CONFIG_ENABLE_OTA_REQUESTOR - InitOTARequestor(); -#endif - if (RTW_SUCCESS != wifi_is_connected_to_ap()) { // QR code will be used with CHIP Tool diff --git a/examples/ota-requestor-app/ameba/chip_main.cmake b/examples/ota-requestor-app/ameba/chip_main.cmake index bebe861e8f8809..7bd00165edecec 100644 --- a/examples/ota-requestor-app/ameba/chip_main.cmake +++ b/examples/ota-requestor-app/ameba/chip_main.cmake @@ -27,6 +27,7 @@ list( ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorDriver.cpp ${chip_dir}/src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp ${chip_dir}/src/app/clusters/ota-requestor/ota-requestor-server.cpp + ${chip_dir}/examples/platform/ameba/ota/OTAInitializer.cpp ) add_library( @@ -58,10 +59,9 @@ target_include_directories( ${chip_dir}/src/controller/data_model ${chip_dir}/third_party/nlio/repo/include/ ${chip_dir}/third_party/nlunit-test/repo/src - ${chip_dir}/src/app/clusters/ota-requestor ${chip_dir}/examples/ota-requestor-app/ameba/main/include - + ${chip_dir}/examples/platform/ameba ${sdk_root}/component/soc/realtek/amebad/fwlib/include ) diff --git a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp index 8f18f730999cde..2db5f21cee2c85 100644 --- a/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp @@ -36,6 +36,9 @@ #include #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +#include +#endif #include "Globals.h" #include "LEDWidget.h" @@ -50,7 +53,15 @@ using namespace ::chip::DeviceManager; using namespace ::chip::Logging; uint32_t identifyTimerCount; -constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kIdentifyTimerDelayMS = 250; +constexpr uint32_t kInitOTARequestorDelaySec = 3; + +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR +void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ + OTAInitializer::Instance().InitOTARequestor(); +} +#endif void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { @@ -97,6 +108,9 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) { +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + static bool isOTAInitialized = false; +#endif if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { ChipLogProgress(DeviceLayer, "IPv4 Server ready..."); @@ -110,6 +124,15 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event { ChipLogProgress(DeviceLayer, "IPv6 Server ready..."); chip::app::DnssdServer::Instance().StartServer(); +#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR + // Init OTA requestor only when we have gotten IPv6 address + if (!isOTAInitialized) + { + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), + InitOTARequestorHandler, nullptr); + isOTAInitialized = true; + } +#endif } else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) { diff --git a/examples/ota-requestor-app/ameba/main/chipinterface.cpp b/examples/ota-requestor-app/ameba/main/chipinterface.cpp index 983a340a5ebaef..89a99340cceda1 100644 --- a/examples/ota-requestor-app/ameba/main/chipinterface.cpp +++ b/examples/ota-requestor-app/ameba/main/chipinterface.cpp @@ -33,27 +33,14 @@ #include #include -#include -#include -#include -#include -#include -#include -#include - void * __dso_handle = 0; -using chip::AmebaOTAImageProcessor; -using chip::BDXDownloader; using chip::ByteSpan; -using chip::DefaultOTARequestor; using chip::EndpointId; using chip::FabricIndex; -using chip::GetRequestorInstance; using chip::NodeId; using chip::OnDeviceConnected; using chip::OnDeviceConnectionFailure; -using chip::OTADownloader; using chip::PeerId; using chip::Server; using chip::VendorId; @@ -87,51 +74,6 @@ void NetWorkCommissioningInstInit() static DeviceCallbacks EchoCallbacks; -DefaultOTARequestor gRequestorCore; -DefaultOTARequestorStorage gRequestorStorage; -ExtendedOTARequestorDriver gRequestorUser; -BDXDownloader gDownloader; -AmebaOTAImageProcessor gImageProcessor; -chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider; -static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted; - -extern "C" void amebaQueryImageCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaQueryImageCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); }); -} - -extern "C" void amebaApplyUpdateCmdHandler() -{ - ChipLogProgress(DeviceLayer, "Calling amebaApplyUpdateCmdHandler"); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); }); -} - -static void InitOTARequestor(void) -{ - // Initialize and interconnect the Requestor and Image Processor objects -- START - SetRequestorInstance(&gRequestorCore); - - gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage()); - - // Set server instance used for session establishment - gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - - gImageProcessor.SetOTADownloader(&gDownloader); - - // Connect the Downloader and Image Processor objects - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - - if (gUserConsentState != chip::ota::UserConsentState::kUnknown) - { - gUserConsentProvider.SetUserConsentState(gUserConsentState); - gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); - } - - // Initialize and interconnect the Requestor and Image Processor objects -- END -} - static void InitServer(intptr_t context) { // Init ZCL Data Model and CHIP App Server @@ -142,8 +84,6 @@ static void InitServer(intptr_t context) // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); NetWorkCommissioningInstInit(); - - InitOTARequestor(); } extern "C" void ChipTest(void) diff --git a/examples/platform/ameba/ota/OTAInitializer.cpp b/examples/platform/ameba/ota/OTAInitializer.cpp new file mode 100644 index 00000000000000..50dd3d050a7db2 --- /dev/null +++ b/examples/platform/ameba/ota/OTAInitializer.cpp @@ -0,0 +1,67 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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 "OTAInitializer.h" +#include "app/clusters/ota-requestor/DefaultOTARequestorStorage.h" +#include +#include +#include +#include +#include +#include + +using namespace chip; +using namespace chip::DeviceLayer; + +namespace { +DefaultOTARequestor gRequestorCore; +DefaultOTARequestorStorage gRequestorStorage; +ExtendedOTARequestorDriver gRequestorUser; +BDXDownloader gDownloader; +AmebaOTAImageProcessor gImageProcessor; +chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider; +static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted; +} // namespace + +extern "C" void amebaQueryImageCmdHandler() +{ + ChipLogProgress(DeviceLayer, "Calling amebaQueryImageCmdHandler"); + PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); }); +} + +extern "C" void amebaApplyUpdateCmdHandler() +{ + ChipLogProgress(DeviceLayer, "Calling amebaApplyUpdateCmdHandler"); + PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); }); +} + +void OTAInitializer::InitOTARequestor(void) +{ + SetRequestorInstance(&gRequestorCore); + gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage()); + // Set server instance used for session establishment + gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + // Connect the Downloader and Image Processor objects + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); + if (gUserConsentState != chip::ota::UserConsentState::kUnknown) + { + gUserConsentProvider.SetUserConsentState(gUserConsentState); + gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider); + } +} diff --git a/examples/platform/ameba/ota/OTAInitializer.h b/examples/platform/ameba/ota/OTAInitializer.h new file mode 100644 index 00000000000000..f678c80a780c00 --- /dev/null +++ b/examples/platform/ameba/ota/OTAInitializer.h @@ -0,0 +1,27 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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. + */ + +class OTAInitializer +{ +public: + static OTAInitializer & Instance(void) + { + static OTAInitializer sInitOTA; + return sInitOTA; + } + void InitOTARequestor(void); +}; diff --git a/src/platform/Ameba/AmebaConfig.cpp b/src/platform/Ameba/AmebaConfig.cpp index d9a34c92f5cd6c..9ed767a4262e17 100644 --- a/src/platform/Ameba/AmebaConfig.cpp +++ b/src/platform/Ameba/AmebaConfig.cpp @@ -42,20 +42,25 @@ namespace Internal { // *** CAUTION ***: Changing the names or namespaces of these values will *break* existing devices. // NVS namespaces used to store device configuration information. -const char AmebaConfig::kConfigNamespace_ChipFactory[] = "chip-factory"; -const char AmebaConfig::kConfigNamespace_ChipConfig[] = "chip-config"; -const char AmebaConfig::kConfigNamespace_ChipCounters[] = "chip-counters"; -const char AmebaConfig::kConfigNamespace_ChipFabric1[] = "chip-fabric-1"; -const char AmebaConfig::kConfigNamespace_ChipFabric2[] = "chip-fabric-2"; -const char AmebaConfig::kConfigNamespace_ChipFabric3[] = "chip-fabric-3"; -const char AmebaConfig::kConfigNamespace_ChipFabric4[] = "chip-fabric-4"; -const char AmebaConfig::kConfigNamespace_ChipFabric5[] = "chip-fabric-5"; -const char AmebaConfig::kConfigNamespace_ChipACL[] = "chip-acl"; -const char AmebaConfig::kConfigNamespace_ChipAttributes[] = "chip-attributes"; -const char AmebaConfig::kConfigNamespace_ChipBindingTable[] = "chip-bindingtable"; -const char AmebaConfig::kConfigNamespace_ChipOTA[] = "chip-ota"; -const char AmebaConfig::kConfigNamespace_ChipDNS[] = "chip-dns"; -const char AmebaConfig::kConfigNamespace_ChipOthers[] = "chip-others"; +const char AmebaConfig::kConfigNamespace_ChipFactory[] = "chip-factory"; +const char AmebaConfig::kConfigNamespace_ChipConfig[] = "chip-config"; +const char AmebaConfig::kConfigNamespace_ChipCounters[] = "chip-counters"; +const char AmebaConfig::kConfigNamespace_ChipFabric1[] = "chip-fabric-1"; +const char AmebaConfig::kConfigNamespace_ChipFabric2[] = "chip-fabric-2"; +const char AmebaConfig::kConfigNamespace_ChipFabric3[] = "chip-fabric-3"; +const char AmebaConfig::kConfigNamespace_ChipFabric4[] = "chip-fabric-4"; +const char AmebaConfig::kConfigNamespace_ChipFabric5[] = "chip-fabric-5"; +const char AmebaConfig::kConfigNamespace_ChipACL[] = "chip-acl"; +const char AmebaConfig::kConfigNamespace_ChipGroupMessageCounters[] = "chip-groupmsgcounters"; +const char AmebaConfig::kConfigNamespace_ChipAttributes[] = "chip-attributes"; +const char AmebaConfig::kConfigNamespace_ChipBindingTable[] = "chip-bindingtable"; +const char AmebaConfig::kConfigNamespace_ChipOTA[] = "chip-ota"; +const char AmebaConfig::kConfigNamespace_ChipFailSafe[] = "chip-failsafe"; +const char AmebaConfig::kConfigNamespace_ChipSessionResumptionIndex[] = "chip-sessionresumptionindex"; +const char AmebaConfig::kConfigNamespace_ChipSessionResumption[] = "chip-sessionresumption"; +const char AmebaConfig::kConfigNamespace_ChipDeviceInfoProvider[] = "chip-deviceinfoprovider"; +const char AmebaConfig::kConfigNamespace_ChipOthers[] = "chip-others"; +const char AmebaConfig::kConfigNamespace_ChipOthers2[] = "chip-others2"; // Keys stored in the chip-factory namespace const AmebaConfig::Key AmebaConfig::kConfigKey_SerialNum = { kConfigNamespace_ChipFactory, "serial-num" }; diff --git a/src/platform/Ameba/AmebaConfig.h b/src/platform/Ameba/AmebaConfig.h index e2af3ea97826c7..4d1cb2645b3fca 100755 --- a/src/platform/Ameba/AmebaConfig.h +++ b/src/platform/Ameba/AmebaConfig.h @@ -46,11 +46,16 @@ class AmebaConfig static const char kConfigNamespace_ChipFabric4[]; static const char kConfigNamespace_ChipFabric5[]; static const char kConfigNamespace_ChipACL[]; + static const char kConfigNamespace_ChipGroupMessageCounters[]; static const char kConfigNamespace_ChipAttributes[]; static const char kConfigNamespace_ChipBindingTable[]; static const char kConfigNamespace_ChipOTA[]; - static const char kConfigNamespace_ChipDNS[]; + static const char kConfigNamespace_ChipFailSafe[]; + static const char kConfigNamespace_ChipSessionResumptionIndex[]; + static const char kConfigNamespace_ChipSessionResumption[]; + static const char kConfigNamespace_ChipDeviceInfoProvider[]; static const char kConfigNamespace_ChipOthers[]; + static const char kConfigNamespace_ChipOthers2[]; // Key definitions for well-known keys. static const Key kConfigKey_SerialNum; diff --git a/src/platform/Ameba/AmebaUtils.cpp b/src/platform/Ameba/AmebaUtils.cpp new file mode 100644 index 00000000000000..b5f3b95f13454b --- /dev/null +++ b/src/platform/Ameba/AmebaUtils.cpp @@ -0,0 +1,142 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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. + */ + +/** + * @file + * General utility methods for the Ameba platform. + */ +/* this file behaves like a config.h, comes first */ +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace ::chip::DeviceLayer::Internal; +using chip::DeviceLayer::Internal::DeviceNetworkInfo; + +namespace { +constexpr char kWiFiSSIDKeyName[] = "wifi-ssid"; +constexpr char kWiFiCredentialsKeyName[] = "wifi-pass"; +} // namespace + +CHIP_ERROR AmebaUtils::StartWiFi(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + // Ensure that the WiFi layer is started. + wifi_on(RTW_MODE_STA); + return err; +} + +CHIP_ERROR AmebaUtils::IsStationEnabled(bool & staEnabled) +{ + staEnabled = (wifi_mode == RTW_MODE_STA || wifi_mode == RTW_MODE_STA_AP); + return CHIP_NO_ERROR; +} + +bool AmebaUtils::IsStationProvisioned(void) +{ + rtw_wifi_config_t WiFiConfig = { 0 }; + return ((GetWiFiConfig(&WiFiConfig) == CHIP_NO_ERROR) && (WiFiConfig.ssid[0] != 0)); +} + +CHIP_ERROR AmebaUtils::IsStationConnected(bool & connected) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + connected = (wifi_is_connected_to_ap() == RTW_SUCCESS) ? 1 : 0; + return err; +} + +CHIP_ERROR AmebaUtils::EnableStationMode(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + // Ensure that station mode is enabled in the WiFi layer. + wifi_set_mode(RTW_MODE_STA); + return err; +} + +CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + /* Store Wi-Fi Configurations in Storage */ + err = PersistedStorage::KeyValueStoreMgr().Put(kWiFiSSIDKeyName, config->ssid, sizeof(config->ssid)); + SuccessOrExit(err); + + err = PersistedStorage::KeyValueStoreMgr().Put(kWiFiCredentialsKeyName, config->password, sizeof(config->password)); + SuccessOrExit(err); + +exit: + return err; +} + +CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + size_t ssidLen = 0; + size_t credentialsLen = 0; + + /* Retrieve Wi-Fi Configurations from Storage */ + err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, config->ssid, sizeof(config->ssid), &ssidLen); + SuccessOrExit(err); + + err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, config->password, sizeof(config->password), + &credentialsLen); + SuccessOrExit(err); + + config->ssid_len = ssidLen; + config->password_len = credentialsLen; + +exit: + return err; +} + +CHIP_ERROR AmebaUtils::ClearWiFiConfig() +{ + // Clear Ameba WiFi station config + CHIP_ERROR err = CHIP_NO_ERROR; + rtw_wifi_config_t wifiConfig; + memset(&wifiConfig, 0, sizeof(wifiConfig)); + err = SetWiFiConfig(&wifiConfig); + return err; +} + +CHIP_ERROR AmebaUtils::WiFiDisconnect(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + ChipLogProgress(DeviceLayer, "wifi_disconnect"); + err = (wifi_disconnect() == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL; + return err; +} + +CHIP_ERROR AmebaUtils::WiFiConnect(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + rtw_wifi_config_t * config = (rtw_wifi_config_t *) pvPortMalloc(sizeof(rtw_wifi_config_t)); + memset(config, 0, sizeof(rtw_wifi_config_t)); + GetWiFiConfig(config); + ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) config->ssid); + int ameba_err = wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password, + strlen((const char *) config->ssid), strlen((const char *) config->password), 0, NULL); + + vPortFree(config); + err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL; + return err; +} diff --git a/src/platform/Ameba/AmebaUtils.h b/src/platform/Ameba/AmebaUtils.h new file mode 100644 index 00000000000000..b2f68f38dad1ef --- /dev/null +++ b/src/platform/Ameba/AmebaUtils.h @@ -0,0 +1,44 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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. + */ + +#pragma once + +#include "platform/internal/DeviceNetworkInfo.h" +#include + +namespace chip { +namespace DeviceLayer { +namespace Internal { + +class AmebaUtils +{ +public: + static CHIP_ERROR StartWiFi(void); + static CHIP_ERROR IsStationEnabled(bool & staEnabled); + static bool IsStationProvisioned(void); + static CHIP_ERROR IsStationConnected(bool & connected); + static CHIP_ERROR EnableStationMode(void); + static CHIP_ERROR SetWiFiConfig(rtw_wifi_config_t * config); + static CHIP_ERROR GetWiFiConfig(rtw_wifi_config_t * config); + static CHIP_ERROR ClearWiFiConfig(void); + static CHIP_ERROR WiFiDisconnect(void); + static CHIP_ERROR WiFiConnect(void); +}; + +} // namespace Internal +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Ameba/BUILD.gn b/src/platform/Ameba/BUILD.gn index ec2349745c5329..f9524f21487989 100755 --- a/src/platform/Ameba/BUILD.gn +++ b/src/platform/Ameba/BUILD.gn @@ -23,6 +23,8 @@ static_library("Ameba") { "../SingletonConfigurationManager.cpp", "AmebaConfig.cpp", "AmebaConfig.h", + "AmebaUtils.cpp", + "AmebaUtils.h", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "CHIPDevicePlatformConfig.h", diff --git a/src/platform/Ameba/ConfigurationManagerImpl.cpp b/src/platform/Ameba/ConfigurationManagerImpl.cpp index 18a05566248367..24f4e254b4ecb3 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.cpp +++ b/src/platform/Ameba/ConfigurationManagerImpl.cpp @@ -67,16 +67,26 @@ CHIP_ERROR ConfigurationManagerImpl::Init() SuccessOrExit(err); err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipACL); SuccessOrExit(err); + err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipGroupMessageCounters); + SuccessOrExit(err); err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipAttributes); SuccessOrExit(err); err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipBindingTable); SuccessOrExit(err); err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipOTA); SuccessOrExit(err); - err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipDNS); + err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipFailSafe); + SuccessOrExit(err); + err = AmebaConfig::EnsureNamespace2(AmebaConfig::kConfigNamespace_ChipSessionResumptionIndex); + SuccessOrExit(err); + err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipSessionResumption); + SuccessOrExit(err); + err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipDeviceInfoProvider); SuccessOrExit(err); err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipOthers); SuccessOrExit(err); + err = AmebaConfig::EnsureNamespace2(AmebaConfig::kConfigNamespace_ChipOthers2); + SuccessOrExit(err); if (AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_RebootCount)) { diff --git a/src/platform/Ameba/ConnectivityManagerImpl.cpp b/src/platform/Ameba/ConnectivityManagerImpl.cpp index ee294acb78d665..ce2ff544c66d34 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.cpp +++ b/src/platform/Ameba/ConnectivityManagerImpl.cpp @@ -32,6 +32,7 @@ #include #endif +#include #include #include #include @@ -59,6 +60,7 @@ ConnectivityManagerImpl ConnectivityManagerImpl::sInstance; CHIP_ERROR ConnectivityManagerImpl::_Init() { + CHIP_ERROR err = CHIP_NO_ERROR; #if CHIP_DEVICE_CONFIG_ENABLE_WIFI mLastStationConnectFailTime = System::Clock::kZero; mLastAPDemandTime = System::Clock::kZero; @@ -77,39 +79,32 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL); wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL); - // Ensure that station mode is enabled. - wifi_on(RTW_MODE_STA); - - // Ensure that station mode is enabled in the WiFi layer. - wifi_set_mode(RTW_MODE_STA); - ; + err = Internal::AmebaUtils::StartWiFi(); + SuccessOrExit(err); + err = Internal::AmebaUtils::EnableStationMode(); + SuccessOrExit(err); // If there is no persistent station provision... if (!IsWiFiStationProvisioned()) { // If the code has been compiled with a default WiFi station provision, configure that now. #if !defined(CONFIG_DEFAULT_WIFI_SSID) - printf("%s %d pls define CONFIG_DEFAULT_WIFI_SSID\r\n", __func__, __LINE__); + ChipLogProgress(DeviceLayer, "Please define CONFIG_DEFAULT_WIFI_SSID"); #else if (CONFIG_DEFAULT_WIFI_SSID[0] != 0) { ChipLogProgress(DeviceLayer, "Setting default WiFi station configuration (SSID: %s)", CONFIG_DEFAULT_WIFI_SSID); // Set a default station configuration. - rtw_wifi_setting_t wifiConfig; - - // Set the wifi configuration + rtw_wifi_config_t wifiConfig; memset(&wifiConfig, 0, sizeof(wifiConfig)); memcpy(wifiConfig.ssid, CONFIG_DEFAULT_WIFI_SSID, strlen(CONFIG_DEFAULT_WIFI_SSID) + 1); memcpy(wifiConfig.password, CONFIG_DEFAULT_WIFI_PASSWORD, strlen(CONFIG_DEFAULT_WIFI_PASSWORD) + 1); wifiConfig.mode = RTW_MODE_STA; // Configure the WiFi interface. - int err = CHIP_SetWiFiConfig(&wifiConfig); - if (err != 0) - { - ChipLogError(DeviceLayer, "_Init _SetWiFiConfig() failed: %d", err); - } + err = Internal::AmebaUtils::SetWiFiConfig(&wifiConfig); + SuccessOrExit(err); // Enable WiFi station mode. ReturnErrorOnFailure(SetWiFiStationMode(kWiFiStationMode_Enabled)); @@ -131,7 +126,8 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI - return CHIP_NO_ERROR; +exit: + return err; } void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) @@ -149,8 +145,8 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) { ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded); } - DriveStationState(); DHCPProcess(); + DriveStationState(); } if (event->Type == DeviceEventType::kRtkWiFiStationDisconnectedEvent) { @@ -208,14 +204,15 @@ CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(WiFiStationMode val) bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void) { - rtw_wifi_setting_t mWiFiSetting; - CHIP_GetWiFiConfig(&mWiFiSetting); - return mWiFiSetting.ssid[0] != 0; + return Internal::AmebaUtils::IsStationProvisioned(); } void ConnectivityManagerImpl::_ClearWiFiStationProvision(void) { - // TBD + // Clear Ameba WiFi station config + rtw_wifi_config_t wifiConfig; + memset(&wifiConfig, 0, sizeof(wifiConfig)); + Internal::AmebaUtils::SetWiFiConfig(&wifiConfig); } CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val) @@ -468,6 +465,7 @@ void ConnectivityManagerImpl::_OnWiFiStationProvisionChange() void ConnectivityManagerImpl::DriveStationState() { + CHIP_ERROR err = CHIP_NO_ERROR; bool stationConnected; GetWiFiStationMode(); @@ -475,16 +473,14 @@ void ConnectivityManagerImpl::DriveStationState() // If the station interface is NOT under application control... if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled) { - // Ensure that the WiFi layer is started. - wifi_on(RTW_MODE_STA); - - // Ensure that station mode is enabled in the WiFi layer. - wifi_set_mode(RTW_MODE_STA); - ; + err = Internal::AmebaUtils::StartWiFi(); + SuccessOrExit(err); + err = Internal::AmebaUtils::EnableStationMode(); + SuccessOrExit(err); } // Determine if the WiFi layer thinks the station interface is currently connected. - stationConnected = (wifi_is_connected_to_ap() == RTW_SUCCESS) ? 1 : 0; + err = Internal::AmebaUtils::IsStationConnected(stationConnected); // If the station interface is currently connected ... if (stationConnected) @@ -506,10 +502,10 @@ void ConnectivityManagerImpl::DriveStationState() (mWiFiStationMode != kWiFiStationMode_Enabled || !IsWiFiStationProvisioned())) { ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface"); - int err = wifi_disconnect(); - if (err != 0) + err = Internal::AmebaUtils::WiFiDisconnect(); + if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "wifi_disconnect() failed: %s", err); + ChipLogError(DeviceLayer, "WiFiDisconnect() failed: %s", err); return; } @@ -551,10 +547,13 @@ void ConnectivityManagerImpl::DriveStationState() now >= mLastStationConnectFailTime + mWiFiStationReconnectInterval) { ChipLogProgress(DeviceLayer, "Attempting to connect WiFi station interface"); - rtw_wifi_setting_t wifi_info; - CHIP_GetWiFiConfig(&wifi_info); - wifi_connect((char *) wifi_info.ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) wifi_info.password, - strlen((const char *) wifi_info.ssid), strlen((const char *) wifi_info.password), 0, NULL); + err = Internal::AmebaUtils::WiFiConnect(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "WiFiConnect() failed: %s", chip::ErrorStr(err)); + } + SuccessOrExit(err); + ChangeWiFiStationState(kWiFiStationState_Connecting); } @@ -571,6 +570,7 @@ void ConnectivityManagerImpl::DriveStationState() } } +exit: ChipLogProgress(DeviceLayer, "Done driving station state, nothing else to do..."); // Kick-off any pending network scan that might have been deferred due to the activity // of the WiFi station. @@ -674,16 +674,9 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void) // address (2000::/3) that is in the valid state. If such an address is found... for (uint8_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isglobal(netif_ip6_addr(netif, i)) && ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) { - // Determine if there is a default IPv6 router that is currently reachable - // via the station interface. If so, presume for now that the device has - // IPv6 connectivity. - struct netif * found_if = nd6_find_route(IP6_ADDR_ANY6); - if (found_if && netif->num == found_if->num) - { - haveIPv6Conn = true; - } + haveIPv6Conn = true; } } } diff --git a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp index 142d2b2daa7e5b..e5754f447c7e45 100644 --- a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -30,11 +31,6 @@ namespace chip { namespace DeviceLayer { namespace NetworkCommissioning { -namespace { -constexpr char kWiFiSSIDKeyName[] = "wifi-ssid"; -constexpr char kWiFiCredentialsKeyName[] = "wifi-pass"; -} // namespace - CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback) { CHIP_ERROR err; @@ -44,20 +40,17 @@ CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChan mpConnectCallback = nullptr; mpStatusChangeCallback = networkStatusChangeCallback; - err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials, - sizeof(mSavedNetwork.credentials), &credentialsLen); + rtw_wifi_config_t config = { 0 }; + err = chip::DeviceLayer::Internal::AmebaUtils::GetWiFiConfig(&config); if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) { return CHIP_NO_ERROR; } - err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), &ssidLen); - if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) - { - return CHIP_NO_ERROR; - } - mSavedNetwork.credentialsLen = credentialsLen; - mSavedNetwork.ssidLen = ssidLen; + memcpy(mSavedNetwork.ssid, config.ssid, sizeof(config.ssid)); + memcpy(mSavedNetwork.credentials, config.password, sizeof(config.password)); + mSavedNetwork.ssidLen = config.ssid_len; + mSavedNetwork.credentialsLen = config.password_len; mStagingNetwork = mSavedNetwork; return err; @@ -71,9 +64,11 @@ CHIP_ERROR AmebaWiFiDriver::Shutdown() CHIP_ERROR AmebaWiFiDriver::CommitConfiguration() { - ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiSSIDKeyName, mStagingNetwork.ssid, mStagingNetwork.ssidLen)); - ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiCredentialsKeyName, mStagingNetwork.credentials, - mStagingNetwork.credentialsLen)); + rtw_wifi_config_t config = { 0 }; + memcpy(config.ssid, mStagingNetwork.ssid, mStagingNetwork.ssidLen); + memcpy(config.password, mStagingNetwork.credentials, mStagingNetwork.credentialsLen); + ReturnErrorOnFailure(chip::DeviceLayer::Internal::AmebaUtils::SetWiFiConfig(&config)); + mSavedNetwork = mStagingNetwork; return CHIP_NO_ERROR; } @@ -129,22 +124,41 @@ Status AmebaWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, Mutabl CHIP_ERROR AmebaWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen) { + CHIP_ERROR err = CHIP_NO_ERROR; + // If device is already connected to WiFi, then disconnect the WiFi, + // clear the WiFi configurations and add the newly provided WiFi configurations. + if (chip::DeviceLayer::Internal::AmebaUtils::IsStationProvisioned()) + { + ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface"); + err = chip::DeviceLayer::Internal::AmebaUtils::WiFiDisconnect(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "WiFiDisconnect() failed"); + return err; + } + err = chip::DeviceLayer::Internal::AmebaUtils::ClearWiFiConfig(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "ClearWiFiStationProvision failed"); + return err; + } + } + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); - rtw_wifi_setting_t wifiConfig; + rtw_wifi_config_t wifiConfig; // Set the wifi configuration memset(&wifiConfig, 0, sizeof(wifiConfig)); memcpy(wifiConfig.ssid, ssid, ssidLen + 1); memcpy(wifiConfig.password, key, keyLen + 1); - wifiConfig.mode = RTW_MODE_STA; // Configure the WiFi interface. - int err = CHIP_SetWiFiConfig(&wifiConfig); - if (err != 0) + err = chip::DeviceLayer::Internal::AmebaUtils::SetWiFiConfig(&wifiConfig); + if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "_SetWiFiConfig() failed: %d", err); - return CHIP_ERROR_INCORRECT_STATE; + ChipLogError(DeviceLayer, "SetWiFiConfig() failed"); + return err; } ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); @@ -249,7 +263,7 @@ void AmebaWiFiDriver::OnScanWiFiNetworkDone() CHIP_ERROR GetConfiguredNetwork(Network & network) { rtw_wifi_setting_t wifi_setting; - CHIP_GetWiFiConfig(&wifi_setting); + wifi_get_setting(WLAN0_NAME, &wifi_setting); uint8_t length = strnlen(reinterpret_cast(wifi_setting.ssid), DeviceLayer::Internal::kMaxWiFiSSIDLength); if (length > sizeof(network.networkID)) @@ -266,10 +280,9 @@ CHIP_ERROR GetConfiguredNetwork(Network & network) void AmebaWiFiDriver::OnNetworkStatusChange() { Network configuredNetwork; - rtw_wifi_setting_t mWiFiSetting; - CHIP_GetWiFiConfig(&mWiFiSetting); - bool staEnable = (mWiFiSetting.mode == RTW_MODE_STA || mWiFiSetting.mode == RTW_MODE_STA_AP); - bool staConnected = (mWiFiSetting.ssid[0] != 0); + bool staEnabled = false, staConnected = false; + VerifyOrReturn(chip::DeviceLayer::Internal::AmebaUtils::IsStationEnabled(staEnabled) == CHIP_NO_ERROR); + VerifyOrReturn(staEnabled && mpStatusChangeCallback != nullptr); CHIP_ERROR err = GetConfiguredNetwork(configuredNetwork); if (err != CHIP_NO_ERROR) @@ -278,6 +291,7 @@ void AmebaWiFiDriver::OnNetworkStatusChange() return; } + VerifyOrReturn(chip::DeviceLayer::Internal::AmebaUtils::IsStationConnected(staConnected) == CHIP_NO_ERROR); if (staConnected) { mpStatusChangeCallback->OnNetworkingStatusChange( @@ -334,9 +348,8 @@ bool AmebaWiFiDriver::WiFiNetworkIterator::Next(Network & item) CHIP_ERROR err = GetConfiguredNetwork(configuredNetwork); if (err == CHIP_NO_ERROR) { - rtw_wifi_setting_t mWiFiSetting; - CHIP_GetWiFiConfig(&mWiFiSetting); - bool isConnected = (mWiFiSetting.ssid[0] != 0); + bool isConnected = false; + err = chip::DeviceLayer::Internal::AmebaUtils::IsStationConnected(isConnected); if (isConnected && configuredNetwork.networkIDLen == item.networkIDLen && memcmp(configuredNetwork.networkID, item.networkID, item.networkIDLen) == 0)