From 973b2d1fa26d2f67e940ec3f6789dae433a14746 Mon Sep 17 00:00:00 2001 From: wyhong <30567533+wy-hh@users.noreply.github.com> Date: Tue, 21 Mar 2023 04:40:20 +0800 Subject: [PATCH] [Bouffalolab] Update application lighting operation in lighting-app (#25640) * update application lighting operation * Fix restyle * add more comments --- .../bouffalolab/common/AppTask.cpp | 286 +++++------------- .../lighting-app/bouffalolab/common/AppTask.h | 18 +- .../bouffalolab/bl602/ldscripts/flash_rom.ld | 4 - .../bouffalolab/common/plat/platform.cpp | 137 ++++++--- .../BL602/NetworkCommissioningDriver.cpp | 19 +- .../bouffalolab/BL602/PlatformManagerImpl.h | 2 +- .../bouffalolab/BL702/PlatformManagerImpl.h | 2 +- third_party/bouffalolab/bl602/bl_iot_sdk.gni | 2 +- 8 files changed, 187 insertions(+), 283 deletions(-) diff --git a/examples/lighting-app/bouffalolab/common/AppTask.cpp b/examples/lighting-app/bouffalolab/common/AppTask.cpp index 9d514cb76cca39..8b88e6892d0ca2 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.cpp +++ b/examples/lighting-app/bouffalolab/common/AppTask.cpp @@ -34,14 +34,6 @@ #include #include #include -#ifdef OTA_ENABLED -#include "OTAConfig.h" -#endif // OTA_ENABLED - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI -#include -#include -#endif #if HEAP_MONITORING #include "MemMonitoring.h" @@ -146,21 +138,32 @@ void AppTask::PostEvent(app_event_t event) void AppTask::AppTaskMain(void * pvParameter) { app_event_t appEvent; - bool isStateReady = false; + bool onoff = false; sLightLED.Init(); #ifdef LED_BTN_RESET ButtonInit(); #else - + /** Without RESET PIN defined, factory reset will be executed if power cycle count(resetCnt) >= APP_REBOOT_RESET_COUNT */ uint32_t resetCnt = 0; size_t saved_value_len = 0; ef_get_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt), &saved_value_len); resetCnt++; ef_set_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt)); + + /** To share with RESET PIN logic, mButtonPressedTime is used to recorded resetCnt increased. + * +1 makes sure mButtonPressedTime is not zero */ + GetAppTask().mButtonPressedTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count() + 1; #endif + GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(1000), false, NULL, AppTask::TimerCallback); + if (GetAppTask().sTimer == NULL) + { + ChipLogError(NotSpecified, "Failed to create timer task"); + appError(APP_ERROR_EVENT_QUEUE_FAILED); + } + ChipLogProgress(NotSpecified, "Starting Platform Manager Event Loop"); CHIP_ERROR ret = PlatformMgr().StartEventLoopTask(); if (ret != CHIP_NO_ERROR) @@ -172,27 +175,10 @@ void AppTask::AppTaskMain(void * pvParameter) GetAppTask().PostEvent(AppTask::APP_EVENT_TIMER); vTaskSuspend(NULL); -#ifndef LED_BTN_RESET - GetAppTask().mButtonPressedTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count() + 1; - if (ConnectivityMgr().IsThreadProvisioned()) - { - GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED); - } -#endif - GetAppTask().mIsConnected = false; - GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(1000), false, NULL, AppTask::TimerCallback); - if (GetAppTask().sTimer == NULL) - { - ChipLogError(NotSpecified, "Failed to create timer task"); - appError(APP_ERROR_EVENT_QUEUE_FAILED); - } - ChipLogProgress(NotSpecified, "App Task started, with heap %d left\r\n", xPortGetFreeHeapSize()); - StartTimer(); - while (true) { appEvent = APP_EVENT_NONE; @@ -201,29 +187,21 @@ void AppTask::AppTaskMain(void * pvParameter) if (eventReceived) { PlatformMgr().LockChipStack(); - if (APP_EVENT_SYS_BLE_ADV & appEvent) - { - LightingSetStatus(APP_EVENT_SYS_BLE_ADV); - LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH); - - isStateReady = false; - } if (APP_EVENT_SYS_PROVISIONED & appEvent) { - LightingSetStatus(APP_EVENT_SYS_PROVISIONED); - LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH); - - isStateReady = true; + LightingUpdate(APP_EVENT_LIGHTING_MASK); } if (APP_EVENT_BTN_SHORT & appEvent) { - LightingSetStatus(APP_EVENT_SYS_LIGHT_TOGGLE); - LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH); + Clusters::OnOff::Attributes::OnOff::Get(GetAppTask().GetEndpointId(), &onoff); + onoff = !onoff; + Clusters::OnOff::Attributes::OnOff::Set(GetAppTask().GetEndpointId(), onoff); + LightingUpdate((app_event_t)(APP_EVENT_LIGHTING_MASK & appEvent)); } - if ((APP_EVENT_LIGHTING_MASK & appEvent) && isStateReady) + if (APP_EVENT_LIGHTING_MASK & appEvent) { LightingUpdate((app_event_t)(APP_EVENT_LIGHTING_MASK & appEvent)); } @@ -233,6 +211,11 @@ void AppTask::AppTaskMain(void * pvParameter) IdentifyHandleOp(appEvent); } + if (APP_EVENT_SYS_BLE_ADV & appEvent) + { + LightingUpdate(APP_EVENT_SYS_BLE_ADV); + } + if (APP_EVENT_FACTORY_RESET & appEvent) { DeviceLayer::ConfigurationMgr().InitiateFactoryReset(); @@ -245,195 +228,67 @@ void AppTask::AppTaskMain(void * pvParameter) } } -void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - switch (event->Type) - { - case DeviceEventType::kCHIPoBLEAdvertisingChange: - -#ifndef LED_BTN_RESET - if (ConnectivityMgr().IsThreadProvisioned()) - { - GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED); - break; - } -#endif - - if (ConnectivityMgr().NumBLEConnections()) - { - GetAppTask().PostEvent(APP_EVENT_SYS_BLE_CONN); - } - else - { - GetAppTask().PostEvent(APP_EVENT_SYS_BLE_ADV); - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - GetAppTask().mIsConnected = ConnectivityMgr().IsWiFiStationConnected(); -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - GetAppTask().mIsConnected = ConnectivityMgr().IsThreadAttached(); -#endif - } - ChipLogProgress(NotSpecified, "BLE adv changed, connection number: %d\r\n", ConnectivityMgr().NumBLEConnections()); - break; -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - case DeviceEventType::kThreadStateChange: - - ChipLogProgress(NotSpecified, "Thread state changed, IsThreadAttached: %d\r\n", ConnectivityMgr().IsThreadAttached()); - if (!GetAppTask().mIsConnected && ConnectivityMgr().IsThreadAttached()) - { - GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED); - GetAppTask().mIsConnected = true; - } - break; -#endif - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - case DeviceEventType::kWiFiConnectivityChange: - - ChipLogProgress(NotSpecified, "Wi-Fi state changed to %s.\r\n", - ConnectivityMgr().IsWiFiStationConnected() ? "connected" : "disconnected"); - - chip::app::DnssdServer::Instance().StartServer(); - NetworkCommissioning::BLWiFiDriver::GetInstance().SaveConfiguration(); - if (!GetAppTask().mIsConnected && ConnectivityMgr().IsWiFiStationConnected()) - { - GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED); - GetAppTask().mIsConnected = true; - } - break; - - case DeviceEventType::kInterfaceIpAddressChanged: - if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) || - (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)) - { - // MDNS server restart on any ip assignment: if link local ipv6 is configured, that - // will not trigger a 'internet connectivity change' as there is no internet - // connectivity. MDNS still wants to refresh its listening interfaces to include the - // newly selected address. - chip::app::DnssdServer::Instance().StartServer(); - } - - if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned) - { - ChipLogProgress(NotSpecified, "Initializing route hook..."); - bl_route_hook_init(); - } - break; -#endif - case DeviceEventType::kFailSafeTimerExpired: - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - GetAppTask().mIsConnected = ConnectivityMgr().IsWiFiStationConnected(); -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - GetAppTask().mIsConnected = ConnectivityMgr().IsThreadAttached(); -#endif - - break; - default: - break; - } -} - -void AppTask::LightingUpdate(app_event_t event) +void AppTask::LightingUpdate(app_event_t status) { uint8_t hue, sat; bool onoff; - DataModel::Nullable v; + DataModel::Nullable v(0); EndpointId endpoint = GetAppTask().GetEndpointId(); - do + if (APP_EVENT_LIGHTING_MASK & status) { - if (EMBER_ZCL_STATUS_SUCCESS != Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff)) + do { - break; - } + if (EMBER_ZCL_STATUS_SUCCESS != Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff)) + { + break; + } - if (EMBER_ZCL_STATUS_SUCCESS != Clusters::LevelControl::Attributes::CurrentLevel::Get(endpoint, v)) - { - break; - } + if (EMBER_ZCL_STATUS_SUCCESS != Clusters::LevelControl::Attributes::CurrentLevel::Get(endpoint, v)) + { + break; + } - if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentHue::Get(endpoint, &hue)) - { - break; - } + if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentHue::Get(endpoint, &hue)) + { + break; + } - if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentSaturation::Get(endpoint, &sat)) - { - break; - } + if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentSaturation::Get(endpoint, &sat)) + { + break; + } - if (!onoff) - { - sLightLED.SetLevel(0); - } - else - { - if (v.IsNull()) + if (!onoff) { - // Just pick something. - v.SetNonNull(254); + sLightLED.SetLevel(0); } + else + { + if (v.IsNull()) + { + // Just pick something. + v.SetNonNull(254); + } #if defined(BL706_NIGHT_LIGHT) || defined(BL602_NIGHT_LIGHT) - sLightLED.SetColor(v.Value(), hue, sat); + sLightLED.SetColor(v.Value(), hue, sat); #else - sLightLED.SetLevel(v.Value()); + sLightLED.SetLevel(v.Value()); #endif - } - - } while (0); -} - -void AppTask::LightingSetOnoff(uint8_t bonoff) -{ - uint8_t newValue = bonoff; - EndpointId endpoint = GetAppTask().GetEndpointId(); - - // write the new on/off value - Clusters::OnOff::Attributes::OnOff::Set(endpoint, newValue); - newValue = 254; - Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, newValue); -} - -void AppTask::LightingSetStatus(app_event_t status) -{ - uint8_t level, hue, sat; - bool onoff = true; - EndpointId endpoint = GetAppTask().GetEndpointId(); - static bool isProvisioned = false; + } - if (APP_EVENT_SYS_LIGHT_TOGGLE == status) - { - Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff); - onoff = !onoff; - } - else if (APP_EVENT_SYS_BLE_ADV == status) - { - hue = 35; - Clusters::ColorControl::Attributes::CurrentHue::Set(endpoint, hue); - sat = 254; - Clusters::ColorControl::Attributes::CurrentSaturation::Set(endpoint, sat); - level = 254; - Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, level); - - isProvisioned = false; + } while (0); } - else if (APP_EVENT_SYS_PROVISIONED == status) + else if (APP_EVENT_SYS_BLE_ADV & status) { - if (isProvisioned) - { - return; - } - isProvisioned = true; - sat = 0; - Clusters::ColorControl::Attributes::CurrentSaturation::Set(endpoint, sat); - level = 254; - Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, level); +#if defined(BL706_NIGHT_LIGHT) || defined(BL602_NIGHT_LIGHT) + /** show yellow to indicate BLE advertisement */ + sLightLED.SetColor(254, 35, 254); +#else + /** show 30% brightness to indicate BLE advertisement */ + sLightLED.SetLevel(25); +#endif } - - Clusters::OnOff::Attributes::OnOff::Set(endpoint, onoff); } bool AppTask::StartTimer(void) @@ -502,7 +357,14 @@ void AppTask::TimerEventHandler(app_event_t event) StartTimer(); if (GetAppTask().mIsFactoryResetIndicat) { - LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH); + if (GetAppTask().mIsConnected) + { + LightingUpdate(APP_EVENT_LIGHTING_MASK); + } + else + { + LightingUpdate(APP_EVENT_SYS_BLE_ADV); + } } GetAppTask().mIsFactoryResetIndicat = false; GetAppTask().mButtonPressedTime = 0; @@ -610,7 +472,7 @@ void AppTask::IdentifyHandleOp(app_event_t event) if (APP_EVENT_IDENTIFY_STOP & event) { identifyState = 0; - LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH); + LightingUpdate(APP_EVENT_LIGHTING_MASK); ChipLogProgress(NotSpecified, "identify stop"); } } diff --git a/examples/lighting-app/bouffalolab/common/AppTask.h b/examples/lighting-app/bouffalolab/common/AppTask.h index 622c1e93ed0001..fd69a0ab14a2aa 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.h +++ b/examples/lighting-app/bouffalolab/common/AppTask.h @@ -51,6 +51,8 @@ struct Identify; class AppTask { public: + friend AppTask & GetAppTask(void); + enum app_event_t { APP_EVENT_NONE = 0x00000000, @@ -73,11 +75,10 @@ class AppTask APP_EVENT_SYS_ALL_MASK = APP_EVENT_SYS_BLE_ADV | APP_EVENT_SYS_BLE_CONN | APP_EVENT_SYS_PROVISIONED | APP_EVENT_FACTORY_RESET, - APP_EVENT_LIGHTING_ONOFF = 0x00010000, - APP_EVENT_LIGHTING_LEVEL = 0x00020000, - APP_EVENT_LIGHTING_COLOR = 0x00040000, - APP_EVENT_LIGHTING_GO_THROUGH = 0x00100000, - APP_EVENT_LIGHTING_MASK = APP_EVENT_LIGHTING_ONOFF | APP_EVENT_LIGHTING_LEVEL | APP_EVENT_LIGHTING_COLOR, + APP_EVENT_LIGHTING_ONOFF = 0x00010000, + APP_EVENT_LIGHTING_LEVEL = 0x00020000, + APP_EVENT_LIGHTING_COLOR = 0x00040000, + APP_EVENT_LIGHTING_MASK = APP_EVENT_LIGHTING_ONOFF | APP_EVENT_LIGHTING_LEVEL | APP_EVENT_LIGHTING_COLOR, APP_EVENT_IDENTIFY_START = 0x01000000, APP_EVENT_IDENTIFY_IDENTIFY = 0x02000000, @@ -101,18 +102,14 @@ class AppTask static void IdentifyStartHandler(Identify *); static void IdentifyStopHandler(Identify *); static void IdentifyHandleOp(app_event_t event); + bool mIsConnected; private: - friend AppTask & GetAppTask(void); friend void StartAppTask(void); friend PlatformManagerImpl; - static void ChipEventHandler(const ChipDeviceEvent * event, intptr_t arg); static uint32_t AppRebootCheck(uint32_t time = 0); - static void LightingSetOnoff(uint8_t bonoff); - static void LightingSetStatus(app_event_t status); - static void LightingSetBleAdv(void); static void LightingSetProvisioned(void); static void LightingSetFactoryReset(void); @@ -143,7 +140,6 @@ class AppTask uint32_t mTimerIntvl; uint64_t mButtonPressedTime; bool mIsFactoryResetIndicat; - bool mIsConnected; static StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)]; static StaticTask_t appTaskStruct; diff --git a/examples/platform/bouffalolab/bl602/ldscripts/flash_rom.ld b/examples/platform/bouffalolab/bl602/ldscripts/flash_rom.ld index 0ed6b74099b710..5ad35f1b9832b8 100644 --- a/examples/platform/bouffalolab/bl602/ldscripts/flash_rom.ld +++ b/examples/platform/bouffalolab/bl602/ldscripts/flash_rom.ld @@ -18,10 +18,6 @@ MEMORY { rom (rxai!w) : ORIGIN = 0x21015000, LENGTH = 44K flash (rxai!w) : ORIGIN = 0x23000000, LENGTH = 4M - - /* ram_tcm (wxa) : ORIGIN = 0x4200C000, LENGTH = (16K + 16K + 48K + 64K + 64K + 8K - __EM_SIZE) put itcm with dtam and also OCRAM*/ - /* ram_wifi (wxa) : ORIGIN = 0x42042000 - __EM_SIZE, LENGTH = (8K + 104K - 64K - 8K) leave 8K left for BLE*/ - ram_tcm (wxa) : ORIGIN = __RAM_START, LENGTH = __RAM_TCM_LEN ram_wifi (wxa) : ORIGIN = __RAM_START + __RAM_TCM_LEN, LENGTH = __RAM_WIFI_LEN } diff --git a/examples/platform/bouffalolab/common/plat/platform.cpp b/examples/platform/bouffalolab/common/plat/platform.cpp index 75dc55b3ec781c..a6d142a1a75575 100644 --- a/examples/platform/bouffalolab/common/plat/platform.cpp +++ b/examples/platform/bouffalolab/common/plat/platform.cpp @@ -24,13 +24,11 @@ #include #include #include -#ifdef OTA_ENABLED -#include "OTAConfig.h" -#endif // OTA_ENABLED #if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include #include +#include #endif #include @@ -46,6 +44,10 @@ #include #endif +#ifdef OTA_ENABLED +#include "OTAConfig.h" +#endif // OTA_ENABLED + #if CONFIG_ENABLE_CHIP_SHELL #include #include @@ -75,7 +77,82 @@ chip::app::Clusters::NetworkCommissioning::Instance } #endif -void PlatformManagerImpl::PlatformInit(void) +void ChipEventHandler(const ChipDeviceEvent * event, intptr_t arg) +{ + switch (event->Type) + { + case DeviceEventType::kCHIPoBLEAdvertisingChange: + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + GetAppTask().mIsConnected = ConnectivityMgr().IsWiFiStationConnected(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + GetAppTask().mIsConnected = ConnectivityMgr().IsThreadAttached(); +#endif + + if (ConnectivityMgr().NumBLEConnections()) + { + GetAppTask().PostEvent(AppTask::APP_EVENT_SYS_BLE_CONN); + } + else + { + GetAppTask().PostEvent(AppTask::APP_EVENT_SYS_BLE_ADV); + } + ChipLogProgress(NotSpecified, "BLE adv changed, connection number: %d", ConnectivityMgr().NumBLEConnections()); + break; +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + case DeviceEventType::kThreadStateChange: + + ChipLogProgress(NotSpecified, "Thread state changed, IsThreadAttached: %d", ConnectivityMgr().IsThreadAttached()); + if (!GetAppTask().mIsConnected && ConnectivityMgr().IsThreadAttached()) + { + GetAppTask().PostEvent(AppTask::APP_EVENT_SYS_PROVISIONED); + GetAppTask().mIsConnected = true; + } + break; +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + case DeviceEventType::kWiFiConnectivityChange: + + ChipLogProgress(NotSpecified, "Wi-Fi state changed to %s.", + ConnectivityMgr().IsWiFiStationConnected() ? "connected" : "disconnected"); + + chip::app::DnssdServer::Instance().StartServer(); + NetworkCommissioning::BLWiFiDriver::GetInstance().SaveConfiguration(); + if (!GetAppTask().mIsConnected && ConnectivityMgr().IsWiFiStationConnected()) + { + GetAppTask().PostEvent(AppTask::APP_EVENT_SYS_PROVISIONED); + GetAppTask().mIsConnected = true; + } + break; + + case DeviceEventType::kInterfaceIpAddressChanged: + if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) || + (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)) + { + // MDNS server restart on any ip assignment: if link local ipv6 is configured, that + // will not trigger a 'internet connectivity change' as there is no internet + // connectivity. MDNS still wants to refresh its listening interfaces to include the + // newly selected address. + chip::app::DnssdServer::Instance().StartServer(); + } + + if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned) + { + ChipLogProgress(NotSpecified, "Initializing route hook..."); + bl_route_hook_init(); + } + break; +#endif + + break; + default: + break; + } +} + +CHIP_ERROR PlatformManagerImpl::PlatformInit(void) { #if CONFIG_ENABLE_CHIP_SHELL || PW_RPC_ENABLED uartInit(); @@ -92,12 +169,7 @@ void PlatformManagerImpl::PlatformInit(void) #endif ChipLogProgress(NotSpecified, "Initializing CHIP stack"); - CHIP_ERROR ret = PlatformMgr().InitChipStack(); - if (ret != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "PlatformMgr().InitChipStack() failed"); - appError(ret); - } + ReturnLogErrorOnFailure(PlatformMgr().InitChipStack()); chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(CHIP_BLE_DEVICE_NAME); @@ -108,31 +180,16 @@ void PlatformManagerImpl::PlatformInit(void) #endif ChipLogProgress(NotSpecified, "Initializing OpenThread stack"); - ret = ThreadStackMgr().InitThreadStack(); - if (ret != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "ThreadStackMgr().InitThreadStack() failed"); - appError(ret); - } + ReturnLogErrorOnFailure(ThreadStackMgr().InitThreadStack()); #if CHIP_DEVICE_CONFIG_THREAD_FTD - ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); + ReturnLogErrorOnFailure(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router)); #else - ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); + ReturnLogErrorOnFailure(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice)); #endif - if (ret != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "ConnectivityMgr().SetThreadDeviceType() failed"); - appError(ret); - } #elif CHIP_DEVICE_CONFIG_ENABLE_WIFI - - ret = sWiFiNetworkCommissioningInstance.Init(); - if (CHIP_NO_ERROR != ret) - { - ChipLogError(NotSpecified, "sWiFiNetworkCommissioningInstance.Init() failed"); - } + ReturnLogErrorOnFailure(sWiFiNetworkCommissioningInstance.Init()); #endif chip::DeviceLayer::PlatformMgr().LockChipStack(); @@ -144,33 +201,23 @@ void PlatformManagerImpl::PlatformInit(void) chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(EXT_DISCOVERY_TIMEOUT_SECS); #endif - // Init ZCL Data Model - static chip::CommonCaseDeviceServerInitParams initParams; + static CommonCaseDeviceServerInitParams initParams; (void) initParams.InitializeStaticResourcesBeforeServerInit(); - ret = chip::Server::GetInstance().Init(initParams); - if (ret != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "chip::Server::GetInstance().Init(initParams) failed"); - appError(ret); - } + ReturnLogErrorOnFailure(chip::Server::GetInstance().Init(initParams)); + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); #if CHIP_ENABLE_OPENTHREAD ChipLogProgress(NotSpecified, "Starting OpenThread task"); // Start OpenThread task - ret = ThreadStackMgrImpl().StartThreadTask(); - if (ret != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "ThreadStackMgr().StartThreadTask() failed"); - appError(ret); - } + ReturnLogErrorOnFailure(ThreadStackMgrImpl().StartThreadTask()); #endif ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); - PlatformMgr().AddEventHandler(AppTask::ChipEventHandler, 0); + PlatformMgr().AddEventHandler(ChipEventHandler, 0); #ifdef OTA_ENABLED chip::DeviceLayer::PlatformMgr().LockChipStack(); @@ -183,4 +230,6 @@ void PlatformManagerImpl::PlatformInit(void) #endif vTaskResume(GetAppTask().sAppTaskHandle); + + return CHIP_NO_ERROR; } diff --git a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp index 7cb2bfaad1cc20..f4ba42ff5adcc2 100644 --- a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp +++ b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp @@ -59,16 +59,10 @@ CHIP_ERROR BLWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeC err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials, sizeof(mSavedNetwork.credentials), &credentialsLen); - if (err == CHIP_ERROR_NOT_FOUND) - { - return CHIP_NO_ERROR; - } - + SuccessOrExit(err); err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), &ssidLen); - if (err == CHIP_ERROR_NOT_FOUND) - { - return CHIP_NO_ERROR; - } + SuccessOrExit(err); + mSavedNetwork.credentialsLen = credentialsLen; mSavedNetwork.ssidLen = ssidLen; @@ -77,6 +71,13 @@ CHIP_ERROR BLWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeC mpConnectCallback = nullptr; mpStatusChangeCallback = networkStatusChangeCallback; +exit: + if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) + { + /** Not commissioned, SSID and Network Password not found. */ + return CHIP_NO_ERROR; + } + return err; } diff --git a/src/platform/bouffalolab/BL602/PlatformManagerImpl.h b/src/platform/bouffalolab/BL602/PlatformManagerImpl.h index 861e446f0beec3..4e6641b6e1e66f 100644 --- a/src/platform/bouffalolab/BL602/PlatformManagerImpl.h +++ b/src/platform/bouffalolab/BL602/PlatformManagerImpl.h @@ -70,7 +70,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener using Internal::GenericPlatformManagerImpl_FreeRTOS::PostEventFromISR; - void PlatformInit(void); + CHIP_ERROR PlatformInit(void); }; /** diff --git a/src/platform/bouffalolab/BL702/PlatformManagerImpl.h b/src/platform/bouffalolab/BL702/PlatformManagerImpl.h index 31cc14ebc97168..9dd8e14c87d293 100644 --- a/src/platform/bouffalolab/BL702/PlatformManagerImpl.h +++ b/src/platform/bouffalolab/BL702/PlatformManagerImpl.h @@ -57,7 +57,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener using Internal::GenericPlatformManagerImpl_FreeRTOS::PostEventFromISR; // ===== Methods that implement the PlatformManager Implementation dedicated. - void PlatformInit(void); + CHIP_ERROR PlatformInit(void); }; /** diff --git a/third_party/bouffalolab/bl602/bl_iot_sdk.gni b/third_party/bouffalolab/bl602/bl_iot_sdk.gni index e6d933e501df93..2376a32a7441e7 100644 --- a/third_party/bouffalolab/bl602/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl602/bl_iot_sdk.gni @@ -838,7 +838,7 @@ template("bl_iot_sdk") { "LWIP_ND6_RDNSS_MAX_DNS_SERVERS=1", "MEMP_NUM_MLD6_GROUP=10", "PBUF_POOL_SIZE=20", - "PBUF_POOL_BUFSIZE=(1600)", + "PBUF_POOL_BUFSIZE=1600", "CONFIG_ENABLE_IPV6_ADDR_CALLBACK", "CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT", "CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT",