From 450610985ab8293fbd015c1a714af6bd32f71348 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Mon, 4 Apr 2022 19:31:20 -0700 Subject: [PATCH] Use iterator to read supported calendar types --- .../time-format-localization-server.cpp | 63 +++++++++++----- src/include/platform/DeviceInfoProvider.h | 16 +++- src/include/platform/PlatformManager.h | 9 --- .../internal/GenericPlatformManagerImpl.h | 10 --- src/platform/Darwin/PlatformManagerImpl.cpp | 21 ------ src/platform/Darwin/PlatformManagerImpl.h | 3 - src/platform/Linux/DeviceInfoProviderImpl.cpp | 75 +++++++++++++++++++ src/platform/Linux/DeviceInfoProviderImpl.h | 13 ++++ src/platform/Linux/PlatformManagerImpl.cpp | 21 ------ src/platform/Linux/PlatformManagerImpl.h | 2 - src/platform/fake/PlatformManagerImpl.h | 6 -- 11 files changed, 144 insertions(+), 95 deletions(-) diff --git a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp index 8a68136829faa6..42065e6fd1ad97 100644 --- a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp +++ b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include using namespace chip; @@ -57,18 +58,32 @@ TimeFormatLocalizationAttrAccess gAttrAccess; CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder) { CHIP_ERROR err = CHIP_NO_ERROR; - DeviceLayer::AttributeList supportedCalendarTypes; - if (DeviceLayer::PlatformMgr().GetSupportedCalendarTypes(supportedCalendarTypes) == CHIP_NO_ERROR) + DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider(); + + if (provider) { - err = aEncoder.EncodeList([&supportedCalendarTypes](const auto & encoder) -> CHIP_ERROR { - for (auto type : supportedCalendarTypes) - { - ReturnErrorOnFailure(encoder.Encode(type)); - } + DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes(); + + if (it) + { + err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR { + CalendarType type; - return CHIP_NO_ERROR; - }); + while (it->Next(type)) + { + ReturnErrorOnFailure(encoder.Encode(type)); + } + + return CHIP_NO_ERROR; + }); + + it->Release(); + } + else + { + err = aEncoder.EncodeEmptyList(); + } } else { @@ -96,25 +111,33 @@ CHIP_ERROR TimeFormatLocalizationAttrAccess::Read(const ConcreteReadAttributePat // if there are any, and to kBuddhist if there are not. bool IsSupportedCalendarType(CalendarType newType, CalendarType & validType) { - DeviceLayer::AttributeList supportedCalendarTypes; + // Reset valid type if no supported calendar types found. + validType = CalendarType::kBuddhist; + + DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider(); - if (DeviceLayer::PlatformMgr().GetSupportedCalendarTypes(supportedCalendarTypes) == CHIP_NO_ERROR) + if (provider) { - for (auto type : supportedCalendarTypes) + DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes(); + + if (it) { - validType = type; + CalendarType type; - if (validType == newType) + while (it->Next(type)) { - return true; + validType = type; + + if (validType == newType) + { + it->Release(); + return true; + } } + + it->Release(); } } - else - { - // Reset valid type if no supported calendar types found. - validType = CalendarType::kBuddhist; - } return false; } diff --git a/src/include/platform/DeviceInfoProvider.h b/src/include/platform/DeviceInfoProvider.h index cfe371e48aa97b..eb1b26e1053c14 100644 --- a/src/include/platform/DeviceInfoProvider.h +++ b/src/include/platform/DeviceInfoProvider.h @@ -66,10 +66,12 @@ class DeviceInfoProvider using FixedLabelType = app::Clusters::FixedLabel::Structs::LabelStruct::Type; using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type; + using CalendarType = app::Clusters::TimeFormatLocalization::CalendarType; - using FixedLabelIterator = Iterator; - using UserLabelIterator = Iterator; - using SupportedLocalesIterator = Iterator; + using FixedLabelIterator = Iterator; + using UserLabelIterator = Iterator; + using SupportedLocalesIterator = Iterator; + using SupportedCalendarTypesIterator = Iterator; DeviceInfoProvider() = default; @@ -101,6 +103,14 @@ class DeviceInfoProvider */ virtual SupportedLocalesIterator * IterateSupportedLocales() = 0; + /** + * Creates an iterator that may be used to obtain the list of supported calendar types of the device. + * In order to release the allocated memory, the Release() method must be called after the iteration is finished. + * @retval An instance of EndpointIterator on success + * @retval nullptr if no iterator instances are available. + */ + virtual SupportedCalendarTypesIterator * IterateSupportedCalendarTypes() = 0; + protected: /** * @brief Set the UserLabel at the specified index of the UserLabelList on a given endpoint diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index b657a3c1d63ff1..0d09b6719b81ef 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -192,9 +192,6 @@ class PlatformManager bool IsChipStackLockedByCurrentThread() const; #endif - CHIP_ERROR GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes); - /* * PostEvent can be called safely on any thread without locking the stack. * When called from a thread that is not doing the stack work item @@ -447,11 +444,5 @@ inline CHIP_ERROR PlatformManager::StartChipTimer(System::Clock::Timeout duratio return static_cast(this)->_StartChipTimer(duration); } -inline CHIP_ERROR PlatformManager::GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes) -{ - return static_cast(this)->_GetSupportedCalendarTypes(supportedCalendarTypes); -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericPlatformManagerImpl.h b/src/include/platform/internal/GenericPlatformManagerImpl.h index c89b2bc9ebdebd..b217a6c7c057af 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl.h @@ -60,9 +60,6 @@ class GenericPlatformManagerImpl void _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg); void _DispatchEvent(const ChipDeviceEvent * event); - CHIP_ERROR _GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes); - // ===== Support methods that can be overridden by the implementation subclass. void DispatchEventToDeviceLayer(const ChipDeviceEvent * event); @@ -78,13 +75,6 @@ class GenericPlatformManagerImpl // Instruct the compiler to instantiate the template only when explicitly told to do so. extern template class GenericPlatformManagerImpl; -template -inline CHIP_ERROR GenericPlatformManagerImpl::_GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes) -{ - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; -} - } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Darwin/PlatformManagerImpl.cpp b/src/platform/Darwin/PlatformManagerImpl.cpp index 1b340e5e0fc875..c423b87f46ddf6 100644 --- a/src/platform/Darwin/PlatformManagerImpl.cpp +++ b/src/platform/Darwin/PlatformManagerImpl.cpp @@ -136,26 +136,5 @@ CHIP_ERROR PlatformManagerImpl::_PostEvent(const ChipDeviceEvent * event) return CHIP_NO_ERROR; } -CHIP_ERROR -PlatformManagerImpl::_GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes) -{ - // In Darwin simulation, return following supported Calendar Types - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kBuddhist); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kChinese); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kCoptic); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kEthiopian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kGregorian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kHebrew); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kIndian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kIslamic); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kJapanese); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kKorean); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kPersian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kTaiwanese); - - return CHIP_NO_ERROR; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Darwin/PlatformManagerImpl.h b/src/platform/Darwin/PlatformManagerImpl.h index 642f68cc3e3a89..0a5e03a65e36d3 100644 --- a/src/platform/Darwin/PlatformManagerImpl.h +++ b/src/platform/Darwin/PlatformManagerImpl.h @@ -64,9 +64,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener CHIP_ERROR _StartEventLoopTask(); CHIP_ERROR _StopEventLoopTask(); - CHIP_ERROR _GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes); - void _RunEventLoop(); void _LockChipStack(){}; bool _TryLockChipStack() { return false; }; diff --git a/src/platform/Linux/DeviceInfoProviderImpl.cpp b/src/platform/Linux/DeviceInfoProviderImpl.cpp index 32987b0b1f962e..e16598ab2502e5 100644 --- a/src/platform/Linux/DeviceInfoProviderImpl.cpp +++ b/src/platform/Linux/DeviceInfoProviderImpl.cpp @@ -267,5 +267,80 @@ bool DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Next(CharSpan & outpu } } +DeviceInfoProvider::SupportedCalendarTypesIterator * DeviceInfoProviderImpl::IterateSupportedCalendarTypes() +{ + return new SupportedCalendarTypesIteratorImpl(); +} + +size_t DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Count() +{ + // In Linux Simulation, return the size of the hardcoded list of Strings that are valid values for the Calendar Types. + // {("kBuddhist"), ("kChinese"), ("kCoptic"), ("kEthiopian"), ("kGregorian"), ("kHebrew"), ("kIndian"), ("kJapanese"), + // ("kKorean"), ("kPersian"), ("kTaiwanese"), ("kIslamic")} + + return 12; +} + +bool DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Next(CalendarType & output) +{ + // In Linux Simulation, return following hardcoded list of Strings that are valid values for the Calendar Types. + CHIP_ERROR err = CHIP_NO_ERROR; + + VerifyOrReturnError(mIndex < 12, false); + + switch (mIndex) + { + case 0: + output = app::Clusters::TimeFormatLocalization::CalendarType::kBuddhist; + break; + case 1: + output = app::Clusters::TimeFormatLocalization::CalendarType::kChinese; + break; + case 2: + output = app::Clusters::TimeFormatLocalization::CalendarType::kCoptic; + break; + case 3: + output = app::Clusters::TimeFormatLocalization::CalendarType::kEthiopian; + break; + case 4: + output = app::Clusters::TimeFormatLocalization::CalendarType::kGregorian; + break; + case 5: + output = app::Clusters::TimeFormatLocalization::CalendarType::kHebrew; + break; + case 6: + output = app::Clusters::TimeFormatLocalization::CalendarType::kIndian; + break; + case 7: + output = app::Clusters::TimeFormatLocalization::CalendarType::kJapanese; + break; + case 8: + output = app::Clusters::TimeFormatLocalization::CalendarType::kKorean; + break; + case 9: + output = app::Clusters::TimeFormatLocalization::CalendarType::kPersian; + break; + case 10: + output = app::Clusters::TimeFormatLocalization::CalendarType::kTaiwanese; + break; + case 11: + output = app::Clusters::TimeFormatLocalization::CalendarType::kIslamic; + break; + default: + err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; + break; + } + + if (err == CHIP_NO_ERROR) + { + mIndex++; + return true; + } + else + { + return false; + } +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Linux/DeviceInfoProviderImpl.h b/src/platform/Linux/DeviceInfoProviderImpl.h index 19d19c198625f6..ddf9a2201b6cac 100644 --- a/src/platform/Linux/DeviceInfoProviderImpl.h +++ b/src/platform/Linux/DeviceInfoProviderImpl.h @@ -31,6 +31,7 @@ class DeviceInfoProviderImpl : public DeviceInfoProvider FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) override; UserLabelIterator * IterateUserLabel(EndpointId endpoint) override; SupportedLocalesIterator * IterateSupportedLocales() override; + SupportedCalendarTypesIterator * IterateSupportedCalendarTypes() override; static DeviceInfoProviderImpl & GetDefaultInstance(); @@ -80,6 +81,18 @@ class DeviceInfoProviderImpl : public DeviceInfoProvider char mActiveLocaleBuf[kMaxActiveLocaleLength + 1]; }; + class SupportedCalendarTypesIteratorImpl : public SupportedCalendarTypesIterator + { + public: + SupportedCalendarTypesIteratorImpl() = default; + size_t Count() override; + bool Next(CalendarType & output) override; + void Release() override { delete this; } + + private: + size_t mIndex = 0; + }; + CHIP_ERROR SetUserLabelLength(EndpointId endpoint, size_t val) override; CHIP_ERROR GetUserLabelLength(EndpointId endpoint, size_t & val) override; CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) override; diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 97db3058c9fd64..5a1d17da87c10e 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -242,27 +242,6 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() return Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); } -CHIP_ERROR -PlatformManagerImpl::_GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes) -{ - // In Linux simulation, return following supported Calendar Types - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kBuddhist); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kChinese); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kCoptic); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kEthiopian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kGregorian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kHebrew); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kIndian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kIslamic); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kJapanese); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kKorean); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kPersian); - supportedCalendarTypes.add(app::Clusters::TimeFormatLocalization::CalendarType::kTaiwanese); - - return CHIP_NO_ERROR; -} - void PlatformManagerImpl::HandleGeneralFault(uint32_t EventId) { GeneralDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetGeneralDiagnosticsDelegate(); diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index b3f1a8cde0a688..9628e4f2bcabb6 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -65,8 +65,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener CHIP_ERROR _InitChipStack(); CHIP_ERROR _Shutdown(); - CHIP_ERROR _GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes); // ===== Members for internal use by the following friends. diff --git a/src/platform/fake/PlatformManagerImpl.h b/src/platform/fake/PlatformManagerImpl.h index ff2be4162fc6d7..d4181bfd9ef691 100644 --- a/src/platform/fake/PlatformManagerImpl.h +++ b/src/platform/fake/PlatformManagerImpl.h @@ -100,12 +100,6 @@ class PlatformManagerImpl final : public PlatformManager CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration) { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR _GetSupportedCalendarTypes( - AttributeList & supportedCalendarTypes) - { - return CHIP_ERROR_NOT_IMPLEMENTED; - } - void _LockChipStack() {} bool _TryLockChipStack() { return true; } void _UnlockChipStack() {}