Skip to content

Commit

Permalink
[ameba] Ameba SNTP and observer support (#36382)
Browse files Browse the repository at this point in the history
* [ameba] Support Time Syncronization Using SNTP

* [ameba] Synchronize RTC with SNTP

* [ameba] Add CONFIG_ENABLE_AMEBA_SNTP to config/ameba/chip.cmake and run restyle-diff.sh

* [ameba] Add Ameba Observer for Ameba Examples

* [Ameba] Fix build with all-clusters-minimal-app and SystemTimeSupport.cpp

* [Ameba] Update Docker Version to 91

* [Ameba] moved AmebaObserver.h to examples/platform/ameba/observer/ and updated Ameba cmake

* [Ameba] simplified #if defined(CONFIG_ENABLE_AMEBA_SNTP) && (CONFIG_ENABLE_AMEBA_SNTP == 1) to #if CONFIG_ENABLE_AMEBA_SNTP

---------

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
pankore and andy31415 authored Nov 19, 2024
1 parent 83b75e1 commit e19ac85
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 19 deletions.
1 change: 1 addition & 0 deletions config/ameba/chip.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ list(
-DMBEDTLS_CONFIG_FILE=<mbedtls_config.h>
-D_POSIX_REALTIME_SIGNALS
-DCHIP_SHELL_MAX_TOKENS=11
-DCONFIG_ENABLE_AMEBA_SNTP=1
)

list(
Expand Down
6 changes: 6 additions & 0 deletions examples/air-purifier-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ endif (matter_enable_ota_requestor)
list(
APPEND ${list_chip_main_sources}

${chip_dir}/examples/air-purifier-app/air-purifier-common/src/air-purifier-manager.cpp
${chip_dir}/examples/air-purifier-app/air-purifier-common/src/air-quality-sensor-manager.cpp
${chip_dir}/examples/air-purifier-app/air-purifier-common/src/filter-delegates.cpp
${chip_dir}/examples/air-purifier-app/air-purifier-common/src/thermostat-manager.cpp

${chip_dir}/examples/air-purifier-app/ameba/main/chipinterface.cpp
${chip_dir}/examples/air-purifier-app/ameba/main/DeviceCallbacks.cpp
${chip_dir}/examples/air-purifier-app/ameba/main/CHIPDeviceManager.cpp
Expand Down Expand Up @@ -192,6 +197,7 @@ target_include_directories(
${chip_dir}/examples/air-purifier-app/air-purifier-common/include
${chip_dir}/examples/air-purifier-app/ameba/main/include
${chip_dir}/examples/platform/ameba
${chip_dir}/examples/platform/ameba/observer
${chip_dir}/examples/providers
${chip_dir_output}/gen/include
${chip_dir}/src/include/
Expand Down
4 changes: 4 additions & 0 deletions examples/air-purifier-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "Server.h"
Expand Down Expand Up @@ -139,6 +140,8 @@ static void InitServer(intptr_t context)
VerifyOrDie((sAmebaPersistentStorageOpKeystore.Init(initParams.persistentStorageDelegate)) == CHIP_NO_ERROR);
initParams.operationalKeystore = &sAmebaPersistentStorageOpKeystore;
#endif
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand All @@ -151,6 +154,7 @@ static void InitServer(intptr_t context)
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}

chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
InitAirPurifierManager();
}

Expand Down
1 change: 1 addition & 0 deletions examples/all-clusters-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ target_include_directories(
${chip_dir}/examples/energy-management-app/energy-management-common/energy-reporting/include
${chip_dir}/examples/all-clusters-app/ameba/main/include
${chip_dir}/examples/platform/ameba
${chip_dir}/examples/platform/ameba/observer
${chip_dir}/examples/platform/ameba/route_hook
${chip_dir}/examples/providers
${chip_dir_output}/gen/include
Expand Down
7 changes: 4 additions & 3 deletions examples/all-clusters-minimal-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ endif (matter_enable_ota_requestor)
list(
APPEND ${list_chip_main_sources}

${chip_dir}/examples/all-clusters-minimal-app/all-clusters-common/src/bridged-actions-stub.cpp
${chip_dir}/examples/all-clusters-minimal-app/all-clusters-common/src/smco-stub.cpp
${chip_dir}/examples/all-clusters-minimal-app/all-clusters-common/src/static-supported-modes-manager.cpp
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/smco-stub.cpp
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp

${chip_dir}/examples/all-clusters-minimal-app/ameba/main/chipinterface.cpp
${chip_dir}/examples/all-clusters-minimal-app/ameba/main/DeviceCallbacks.cpp
Expand Down Expand Up @@ -176,6 +176,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-minimal-app/ameba/main/include
${chip_dir}/examples/platform/ameba/observer
${chip_dir_output}/gen/include
${chip_dir}/src/include/
${chip_dir}/src/lib/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "Globals.h"
Expand Down Expand Up @@ -157,6 +158,8 @@ static void InitServer(intptr_t context)
// Init ZCL Data Model and CHIP App Server
static chip::CommonCaseDeviceServerInitParams initParams;
initParams.InitializeStaticResourcesBeforeServerInit();
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);

// Initialize device attestation config
Expand All @@ -173,6 +176,7 @@ static void InitServer(intptr_t context)
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
Clusters::ModeSelect::setSupportedModesManager(&sStaticSupportedModesManager);
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
}

extern "C" void ChipTest(void)
Expand Down
1 change: 1 addition & 0 deletions examples/chef/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_include_directories(
${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}/examples/platform/ameba/observer
${chip_dir}/examples/providers
${chip_dir_output}/gen/include
${chip_dir}/src/include/
Expand Down
4 changes: 4 additions & 0 deletions examples/chef/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "Globals.h"
Expand Down Expand Up @@ -104,6 +105,8 @@ static void InitServer(intptr_t context)
// Init ZCL Data Model and CHIP App Server
static chip::CommonCaseDeviceServerInitParams initParams;
initParams.InitializeStaticResourcesBeforeServerInit();
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand All @@ -115,6 +118,7 @@ static void InitServer(intptr_t context)
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
}

extern "C" void ChipTest(void)
Expand Down
1 change: 1 addition & 0 deletions examples/light-switch-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ target_include_directories(
${chip_dir}/zzz_generated/app-common
${chip_dir}/examples/light-switch-app/ameba/main/include
${chip_dir}/examples/platform/ameba
${chip_dir}/examples/platform/ameba/observer
${chip_dir}/examples/providers
${chip_dir_output}/gen/include
${chip_dir}/src/include/
Expand Down
4 changes: 4 additions & 0 deletions examples/light-switch-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "BindingHandler.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
Expand Down Expand Up @@ -109,6 +110,8 @@ static void InitServer(intptr_t context)
VerifyOrDie((sAmebaPersistentStorageOpKeystore.Init(initParams.persistentStorageDelegate)) == CHIP_NO_ERROR);
initParams.operationalKeystore = &sAmebaPersistentStorageOpKeystore;
#endif
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand All @@ -124,6 +127,7 @@ static void InitServer(intptr_t context)
#if CONFIG_ENABLE_CHIP_SHELL
InitBindingHandler();
#endif
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
}

extern "C" void ChipTest(void)
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ target_include_directories(
${chip_dir}/examples/lighting-app/lighting-common/include
${chip_dir}/examples/lighting-app/ameba/main/include
${chip_dir}/examples/platform/ameba
${chip_dir}/examples/platform/ameba/observer
${chip_dir}/examples/providers
${chip_dir_output}/gen/include
${chip_dir}/src/include/
Expand Down
4 changes: 4 additions & 0 deletions examples/lighting-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "Globals.h"
Expand Down Expand Up @@ -129,6 +130,8 @@ static void InitServer(intptr_t context)
VerifyOrDie((sAmebaPersistentStorageOpKeystore.Init(initParams.persistentStorageDelegate)) == CHIP_NO_ERROR);
initParams.operationalKeystore = &sAmebaPersistentStorageOpKeystore;
#endif
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand All @@ -140,6 +143,7 @@ static void InitServer(intptr_t context)
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
}

extern "C" void ChipTest(void)
Expand Down
1 change: 1 addition & 0 deletions examples/ota-requestor-app/ameba/chip_main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ target_include_directories(
${chip_dir}/src/app/clusters/ota-requestor
${chip_dir}/examples/ota-requestor-app/ameba/main/include
${chip_dir}/examples/platform/ameba
${chip_dir}/examples/platform/ameba/observer
${chip_dir}/examples/providers
${sdk_root}/component/soc/realtek/amebad/fwlib/include
)
Expand Down
4 changes: 4 additions & 0 deletions examples/ota-requestor-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <platform_stdlib.h>

#include "AmebaObserver.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "chip_porting.h"
Expand Down Expand Up @@ -76,11 +77,14 @@ static void InitServer(intptr_t context)
// Init ZCL Data Model and CHIP App Server
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
static AmebaObserver sAmebaObserver;
initParams.appDelegate = &sAmebaObserver;
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);

NetWorkCommissioningInstInit();
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAmebaObserver);
}

extern "C" void ChipTest(void)
Expand Down
45 changes: 29 additions & 16 deletions src/platform/Ameba/SystemTimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,32 @@ Milliseconds64 ClockImpl::GetMonotonicMilliseconds64(void)

CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & curTime)
{
// TODO(19081): This platform does not properly error out if wall clock has
// not been set. For now, short circuit this.
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#if 0
time_t seconds;
struct rtkTimeVal tv;
#if CONFIG_ENABLE_AMEBA_SNTP
time_t seconds = 0, uSeconds = 0;

seconds = matter_rtc_read();

tv.tv_sec = (uint32_t) seconds;
tv.tv_usec = 0;
if (matter_sntp_rtc_is_sync()) // if RTC is already sync with SNTP, read directly from RTC
{
seconds = matter_rtc_read(); // ameba rtc precission is in seconds only
}
else // read from SNTP and sync RTC with SNTP
{
matter_sntp_get_current_time(&seconds, &uSeconds);
}

if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
if (seconds < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!");
curTime = Microseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec));
if (uSeconds < 0)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative uSeconds values!");
curTime = Microseconds64((static_cast<uint64_t>(seconds) * UINT64_C(1000000)) + static_cast<uint64_t>(uSeconds));

return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

Expand Down Expand Up @@ -105,9 +111,16 @@ CHIP_ERROR InitClock_RealTime()
Clock::Microseconds64((static_cast<uint64_t>(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) * UINT64_C(1000000)));
// Use CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD as the initial value of RealTime.
// Then the RealTime obtained from GetClock_RealTime will be always valid.
//
// TODO(19081): This is broken because it causes the platform to report
// that it does have wall clock time when it actually doesn't.
#if CONFIG_ENABLE_AMEBA_SNTP
time_t seconds = 0, uSeconds = 0;

matter_sntp_init();
matter_sntp_get_current_time(&seconds, &uSeconds); // try to read from SNTP and sync RTC with SNTP
if ((seconds > CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) && (uSeconds > 0))
{
curTime = Microseconds64((static_cast<uint64_t>(seconds) * UINT64_C(1000000)) + static_cast<uint64_t>(uSeconds));
}
#endif
return System::SystemClock().SetClock_RealTime(curTime);
}

Expand Down

0 comments on commit e19ac85

Please sign in to comment.