Skip to content

Commit

Permalink
Use iteration pattern to get the fixed user label list (#16861)
Browse files Browse the repository at this point in the history
* Use iteration pattern to get the fixed user label list

* Remove the unnecessary check condition
  • Loading branch information
yufengwangca authored Mar 31, 2022
1 parent 24adfa5 commit 6a55194
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 63 deletions.
16 changes: 11 additions & 5 deletions src/app/clusters/fixed-label-server/fixed-label-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/DeviceInfoProvider.h>
#include <platform/PlatformManager.h>

using namespace chip;
Expand All @@ -53,18 +54,23 @@ class FixedLabelAttrAccess : public AttributeAccessInterface
CHIP_ERROR FixedLabelAttrAccess::ReadLabelList(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err = CHIP_NO_ERROR;
DeviceLayer::AttributeList<app::Clusters::FixedLabel::Structs::LabelStruct::Type, DeviceLayer::kMaxFixedLabels> labelList;

if (DeviceLayer::PlatformMgr().GetFixedLabelList(endpoint, labelList) == CHIP_NO_ERROR)
DeviceLayer::DeviceInfoProvider::FixedLabelIterator * it = DeviceLayer::GetDeviceInfoProvider()->IterateFixedLabel(endpoint);

if (it)
{
err = aEncoder.EncodeList([&labelList](const auto & encoder) -> CHIP_ERROR {
for (auto label : labelList)
err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
FixedLabel::Structs::LabelStruct::Type fixedlabel;

while (it->Next(fixedlabel))
{
ReturnErrorOnFailure(encoder.Encode(label));
ReturnErrorOnFailure(encoder.Encode(fixedlabel));
}

return CHIP_NO_ERROR;
});

it->Release();
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/app/clusters/user-label-server/user-label-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/DeviceInfoProvider.h>
#include <platform/PlatformManager.h>

using namespace chip;
Expand Down
109 changes: 109 additions & 0 deletions src/include/platform/DeviceInfoProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
*
* 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 <algorithm>
#include <stdint.h>
#include <sys/types.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <app/util/basic-types.h>
#include <lib/core/CHIPError.h>
#include <platform/AttributeList.h>

namespace chip {
namespace DeviceLayer {

static constexpr size_t kMaxLabelNameLength = 16;
static constexpr size_t kMaxLabelValueLength = 16;

class DeviceInfoProvider
{
public:
/**
* Template used to iterate the stored group data
*/
template <typename T>
class Iterator
{
public:
virtual ~Iterator() = default;
/**
* @retval The number of entries in total that will be iterated.
*/
virtual size_t Count() = 0;
/**
* @param[out] item Value associated with the next element in the iteration.
* @retval true if the next entry is successfully retrieved.
* @retval false if no more entries can be found.
*/
virtual bool Next(T & item) = 0;
/**
* Release the memory allocated by this iterator.
* Must be called before the pointer goes out of scope.
*/
virtual void Release() = 0;

protected:
Iterator() = default;
};

using FixedLabelType = app::Clusters::FixedLabel::Structs::LabelStruct::Type;

using FixedLabelIterator = Iterator<FixedLabelType>;

DeviceInfoProvider() = default;

virtual ~DeviceInfoProvider() = default;

// Not copyable
DeviceInfoProvider(const DeviceInfoProvider &) = delete;
DeviceInfoProvider & operator=(const DeviceInfoProvider &) = delete;

// Iterators
/**
* Creates an iterator that may be used to obtain the list of user labels associated with the given endpoint.
* In order to release the allocated memory, the Release() method must be called after the iteration is finished.
* Modifying the user label during the iteration is currently not supported, and may yield unexpected behaviour.
* @retval An instance of EndpointIterator on success
* @retval nullptr if no iterator instances are available.
*/
virtual FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) = 0;
};

/**
* Instance getter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* @return The global Device Info Provider. Assume never null.
*/
DeviceInfoProvider * GetDeviceInfoProvider();

/**
* Instance setter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* If the `provider` is nullptr, no change is done.
*
* @param[in] provider the Device Info Provider
*/
void SetDeviceInfoProvider(DeviceInfoProvider * provider);

} // namespace DeviceLayer
} // namespace chip
10 changes: 0 additions & 10 deletions src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class DiscoveryImplPlatform;

namespace DeviceLayer {

static constexpr size_t kMaxFixedLabels = 10;
static constexpr size_t kMaxUserLabels = 10;
static constexpr size_t kMaxLanguageTags = 254; // Maximum number of entry type 'ARRAY' supports
static constexpr size_t kMaxCalendarTypes = 12;
Expand Down Expand Up @@ -195,8 +194,6 @@ class PlatformManager
bool IsChipStackLockedByCurrentThread() const;
#endif

CHIP_ERROR GetFixedLabelList(EndpointId endpoint,
AttributeList<app::Clusters::FixedLabel::Structs::LabelStruct::Type, kMaxFixedLabels> & labelList);
CHIP_ERROR SetUserLabelList(EndpointId endpoint,
AttributeList<app::Clusters::UserLabel::Structs::LabelStruct::Type, kMaxUserLabels> & labelList);
CHIP_ERROR GetUserLabelList(EndpointId endpoint,
Expand Down Expand Up @@ -456,13 +453,6 @@ inline CHIP_ERROR PlatformManager::StartChipTimer(System::Clock::Timeout duratio
{
return static_cast<ImplClass *>(this)->_StartChipTimer(duration);
}

inline CHIP_ERROR PlatformManager::GetFixedLabelList(
EndpointId endpoint, AttributeList<app::Clusters::FixedLabel::Structs::LabelStruct::Type, kMaxFixedLabels> & labelList)
{
return static_cast<ImplClass *>(this)->_GetFixedLabelList(endpoint, labelList);
}

inline CHIP_ERROR
PlatformManager::SetUserLabelList(EndpointId endpoint,
AttributeList<app::Clusters::UserLabel::Structs::LabelStruct::Type, kMaxUserLabels> & labelList)
Expand Down
10 changes: 0 additions & 10 deletions src/include/platform/internal/GenericPlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ class GenericPlatformManagerImpl
void _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg);
void _DispatchEvent(const ChipDeviceEvent * event);

CHIP_ERROR
_GetFixedLabelList(EndpointId endpoint,
AttributeList<app::Clusters::FixedLabel::Structs::LabelStruct::Type, kMaxFixedLabels> & labelList);
CHIP_ERROR _SetUserLabelList(EndpointId endpoint,
AttributeList<app::Clusters::UserLabel::Structs::LabelStruct::Type, kMaxUserLabels> & labelList);
CHIP_ERROR _GetUserLabelList(EndpointId endpoint,
Expand All @@ -86,13 +83,6 @@ class GenericPlatformManagerImpl
// Instruct the compiler to instantiate the template only when explicitly told to do so.
extern template class GenericPlatformManagerImpl<PlatformManagerImpl>;

template <class ImplClass>
inline CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_GetFixedLabelList(
EndpointId endpoint, AttributeList<app::Clusters::FixedLabel::Structs::LabelStruct::Type, kMaxFixedLabels> & labelList)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

template <class ImplClass>
inline CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_SetUserLabelList(
EndpointId endpoint, AttributeList<app::Clusters::UserLabel::Structs::LabelStruct::Type, kMaxUserLabels> & labelList)
Expand Down
1 change: 1 addition & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ if (chip_device_platform != "none") {
"../include/platform/internal/testing/ConfigUnitTest.h",
"CommissionableDataProvider.cpp",
"DeviceControlServer.cpp",
"DeviceInfoProvider.cpp",
"DiagnosticDataProvider.cpp",
"Entropy.cpp",
"FailSafeContext.cpp",
Expand Down
68 changes: 68 additions & 0 deletions src/platform/DeviceInfoProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* 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
* Provides implementations for the DeviceInfoProvider APIs.
* This implementation is common across all platforms.
*/

#include <lib/support/CodeUtils.h>
#include <platform/DeviceInfoProvider.h>

namespace chip {
namespace DeviceLayer {

class DeviceInfoProvider;

namespace {

DeviceInfoProvider * gDeviceInfoProvider = nullptr;

} // namespace

/**
* Instance getter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* @return The global Device Info provider. Assume never null.
*/
DeviceInfoProvider * GetDeviceInfoProvider()
{
return gDeviceInfoProvider;
}

/**
* Instance setter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* If the `provider` is nullptr, no change is done.
*
* @param[in] provider the global Device Info Provider.
*/
void SetDeviceInfoProvider(DeviceInfoProvider * provider)
{
if (provider)
{
gDeviceInfoProvider = provider;
}
}

} // namespace DeviceLayer
} // namespace chip
2 changes: 2 additions & 0 deletions src/platform/Linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static_library("Linux") {
"ConnectivityManagerImpl.h",
"ConnectivityUtils.cpp",
"ConnectivityUtils.h",
"DeviceInfoProviderImpl.cpp",
"DeviceInfoProviderImpl.h",
"DeviceNetworkProvisioningDelegateImpl.cpp",
"DeviceNetworkProvisioningDelegateImpl.h",
"DiagnosticDataProviderImpl.cpp",
Expand Down
Loading

0 comments on commit 6a55194

Please sign in to comment.