Skip to content

Commit

Permalink
[Ameba] only trigger ota query when ota is initialized (#25207)
Browse files Browse the repository at this point in the history
* [ota] only trigger ota query when ota is initialized

* [ota] restyle

* [ota] optimize code

* [ota] optimize chef devicecallback
  • Loading branch information
pankore authored and pull[bot] committed Jul 11, 2023
1 parent 3768b6f commit 1149788
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
6 changes: 1 addition & 5 deletions examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ 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...");
Expand All @@ -145,11 +142,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
chip::app::DnssdServer::Instance().StartServer();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
if (OTAInitializer::Instance().CheckInit())
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
}
Expand Down
6 changes: 1 addition & 5 deletions examples/chef/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ 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...");
Expand All @@ -123,11 +120,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
chip::app::DnssdServer::Instance().StartServer();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
if (OTAInitializer::Instance().CheckInit())
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
}
Expand Down
6 changes: 1 addition & 5 deletions examples/light-switch-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ 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...");
Expand All @@ -145,11 +142,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
chip::app::DnssdServer::Instance().StartServer();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
if (OTAInitializer::Instance().CheckInit())
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
}
Expand Down
7 changes: 1 addition & 6 deletions examples/lighting-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ 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...");
Expand All @@ -126,11 +122,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
chip::app::DnssdServer::Instance().StartServer();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
if (OTAInitializer::Instance().CheckInit())
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
}
Expand Down
6 changes: 1 addition & 5 deletions examples/ota-requestor-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ 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...");
Expand All @@ -145,11 +142,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
chip::app::DnssdServer::Instance().StartServer();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
if (OTAInitializer::Instance().CheckInit())
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
}
Expand Down
14 changes: 11 additions & 3 deletions examples/platform/ameba/ota/OTAInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ AmebaOTAImageProcessor gImageProcessor;
extern "C" void amebaQueryImageCmdHandler()
{
ChipLogProgress(DeviceLayer, "Calling amebaQueryImageCmdHandler");
PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); });
if (OTAInitializer::Instance().CheckInit())
PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); });
}

extern "C" void amebaApplyUpdateCmdHandler()
{
ChipLogProgress(DeviceLayer, "Calling amebaApplyUpdateCmdHandler");
PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); });
if (OTAInitializer::Instance().CheckInit())
PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); });
}

void OTAInitializer::InitOTARequestor(void)
void OTAInitializer::InitOTARequestor()
{
SetRequestorInstance(&gRequestorCore);
gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
Expand All @@ -55,4 +57,10 @@ void OTAInitializer::InitOTARequestor(void)
// Connect the Downloader and Image Processor objects
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
initialized = true;
}

bool OTAInitializer::CheckInit()
{
return initialized;
}
4 changes: 4 additions & 0 deletions examples/platform/ameba/ota/OTAInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class OTAInitializer
return sInitOTA;
}
void InitOTARequestor(void);
bool CheckInit(void);

private:
bool initialized = false;
};

0 comments on commit 1149788

Please sign in to comment.