Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Im/read chunking demo #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions examples/chip-tool/commands/tests/MockEmberAttributeTable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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.
*/

#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPTLVDebug.hpp>
#include <lib/core/DataModelTypes.h>
#include <lib/support/CodeUtils.h>

using namespace chip;

// Mock Functions
typedef uint8_t EmberAfClusterMask;
#define CLUSTER_MASK_SERVER (0x40)

namespace {

EndpointId endpoints[] = { 200, 201, 202 };
uint16_t clusterIndex[] = { 0, 2, 5 };
uint8_t clusterCount[] = { 2, 3, 4 };
ClusterId clusters[] = { 1, 2, 1, 2, 3, 1, 2, 3, 4 };
uint16_t attributeIndex[] = { 0, 2, 5, 7, 11, 16, 19, 25, 27 };
uint16_t attributeCount[] = { 2, 3, 2, 4, 5, 3, 6, 2, 1 };
AttributeId attributes[] = {
// clang-format off
100, 2,
100, 4, 5,
100, 2,
100, 4, 5, 6,
100, 8, 9, 10, 11,
100, 2, 3,
100, 4, 5, 6, 7, 8,
100, 8,
100,
// clang-format on
};

} // namespace

uint16_t emberAfEndpointCount(void)
{
return ArraySize(endpoints);
}

uint16_t emberAfIndexFromEndpoint(chip::EndpointId endpoint)
{
for (uint16_t i = 0; i < ArraySize(endpoints); i++)
{
if (endpoints[i] == endpoint)
{
return i;
}
}
return UINT16_MAX;
}

uint8_t emberAfClusterCount(chip::EndpointId endpoint, bool server)
{
for (uint16_t i = 0; i < ArraySize(endpoints); i++)
{
if (endpoints[i] == endpoint)
{
return clusterCount[i];
}
}
return 0;
}

uint16_t emberAfGetServerAttributeCount(chip::EndpointId endpoint, chip::ClusterId cluster)
{
uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
uint16_t clusterCountOnEndpoint = emberAfClusterCount(endpoint, true);
for (uint16_t i = 0; i < clusterCountOnEndpoint; i++)
{
if (clusters[i + clusterIndex[endpointIndex]] == cluster)
{
return attributeCount[i + clusterIndex[endpointIndex]];
}
}
return 0xFFFF;
}

uint16_t emberAfGetServerAttributeIndexByAttributeId(chip::EndpointId endpoint, chip::ClusterId cluster,
chip::AttributeId attributeId)
{
uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
uint16_t clusterCountOnEndpoint = emberAfClusterCount(endpoint, true);
for (uint16_t i = 0; i < clusterCountOnEndpoint; i++)
{
if (clusters[i + clusterIndex[endpointIndex]] == cluster)
{
uint16_t clusterAttributeOffset = attributeIndex[i + clusterIndex[endpointIndex]];
for (uint16_t j = 0; j < emberAfGetServerAttributeCount(endpoint, cluster); j++)
{
if (attributes[clusterAttributeOffset + j] == attributeId)
{
return j;
}
}
return 0xFFFF;
}
}
return 0xFFFF;
}

chip::EndpointId emberAfEndpointFromIndex(uint16_t index)
{
return index < ArraySize(endpoints) ? endpoints[index] : chip::kWildcardEndpointId;
}

chip::ClusterId emberAfGetNthClusterId(chip::EndpointId endpoint, uint8_t n, bool server)
{
if (n >= emberAfClusterCount(endpoint, server))
{
return MEI::kWildcard;
}
return clusters[clusterIndex[emberAfIndexFromEndpoint(endpoint)] + n];
}

chip::AttributeId emberAfGetServerAttributeIdByIndex(chip::EndpointId endpoint, chip::ClusterId cluster, uint16_t index)
{
uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
uint16_t clusterCountOnEndpoint = emberAfClusterCount(endpoint, true);
for (uint16_t i = 0; i < clusterCountOnEndpoint; i++)
{
if (clusters[i + clusterIndex[endpointIndex]] == cluster)
{
uint16_t clusterAttributeOffset = attributeIndex[i + clusterIndex[endpointIndex]];
if (index >= emberAfGetServerAttributeCount(endpoint, cluster))
{
return MEI::kWildcard;
}
return attributes[clusterAttributeOffset + index];
}
}
return 0xFFFF;
}

uint8_t emberAfClusterIndex(chip::EndpointId endpoint, chip::ClusterId cluster, EmberAfClusterMask mask)
{
uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
uint16_t clusterCountOnEndpoint = emberAfClusterCount(endpoint, true);
for (uint8_t i = 0; i < clusterCountOnEndpoint; i++)
{
if (clusters[i + clusterIndex[endpointIndex]] == cluster)
{
return i;
}
}
return 0xFF;
}
2 changes: 2 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

#include "Options.h"

#include <app/PathIterator.h>

using namespace chip;
using namespace chip::Credentials;
using namespace chip::DeviceLayer;
Expand Down
2 changes: 2 additions & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ static_library("app") {
"MessageDef/TimedRequest.cpp",
"MessageDef/WriteRequest.cpp",
"MessageDef/WriteResponse.cpp",
"PathIterator.cpp",
"PathIterator.h",
"ReadClient.cpp",
"ReadHandler.cpp",
"WriteClient.cpp",
Expand Down
103 changes: 22 additions & 81 deletions src/app/ClusterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,96 +25,37 @@

namespace chip {
namespace app {
static constexpr AttributeId kRootAttributeId = 0xFFFFFFFF;

/**
* ClusterInfo is the representation of an attribute path or an event path used by ReacHandler, ReadClient, WriteHandler,
* Report::Engine etc, it contains a mpNext field so it can be used as a linked list. It uses some invalid values for representing
* the wildcard value for its field.
*/
// TODO: The cluster info should be breaked into AttributeInfo and EventInfo.
// Note: The change will happen after #11171 with a better linked list.
struct ClusterInfo
{
enum class Flags : uint8_t
{
kFieldIdValid = 0x01,
kListIndexValid = 0x02,
kEventIdValid = 0x03,
};

bool IsAttributePathSupersetOf(const ClusterInfo & other) const
{
if ((other.mEndpointId != mEndpointId) || (other.mClusterId != mClusterId))
{
return false;
}

Optional<AttributeId> myFieldId =
mFlags.Has(Flags::kFieldIdValid) ? Optional<AttributeId>::Value(mFieldId) : Optional<AttributeId>::Missing();

Optional<AttributeId> otherFieldId = other.mFlags.Has(Flags::kFieldIdValid) ? Optional<AttributeId>::Value(other.mFieldId)
: Optional<AttributeId>::Missing();

Optional<ListIndex> myListIndex =
mFlags.Has(Flags::kListIndexValid) ? Optional<ListIndex>::Value(mListIndex) : Optional<ListIndex>::Missing();

Optional<ListIndex> otherListIndex = other.mFlags.Has(Flags::kListIndexValid) ? Optional<ListIndex>::Value(other.mListIndex)
: Optional<ListIndex>::Missing();
VerifyOrReturnError(!mEndpointId.HasValue() || mEndpointId == other.mEndpointId, false);
VerifyOrReturnError(!mClusterId.HasValue() || mClusterId == other.mClusterId, false);
VerifyOrReturnError(!mFieldId.HasValue() || mFieldId == other.mFieldId, false);
VerifyOrReturnError(!mListIndex.HasValue() || mListIndex == other.mListIndex, false);

// If list index exists, field index must exist
// Field 0xFFFFFFF (any) & listindex set is invalid
assert(!(myListIndex.HasValue() && !myFieldId.HasValue()));
assert(!(otherListIndex.HasValue() && !otherFieldId.HasValue()));
assert(!(myFieldId == Optional<AttributeId>::Value(kRootAttributeId) && myListIndex.HasValue()));
assert(!(otherFieldId == Optional<AttributeId>::Value(kRootAttributeId) && otherListIndex.HasValue()));

if (myFieldId == Optional<AttributeId>::Value(kRootAttributeId))
{
return true;
}

if (myFieldId != otherFieldId)
{
return false;
}

// We only support top layer for attribute representation, either FieldId or FieldId + ListIndex
// Combination: if myFieldId == otherFieldId, ListIndex cannot exist without FieldId
// 1. myListIndex and otherListIndex both missing or both exactly the same, then current is superset of other
// 2. myListIndex is missing, no matter if otherListIndex is missing or not, then current is superset of other
if (myListIndex == otherListIndex)
{
// either both missing or both exactly the same
return true;
}

if (!myListIndex.HasValue())
{
// difference is ok only if myListIndex is missing
return true;
}

return false;
return true;
}

bool HasWildcard() const { return !mEndpointId.HasValue() || !mClusterId.HasValue() || !mFieldId.HasValue(); }

ClusterInfo() {}
NodeId mNodeId = 0;
ClusterId mClusterId = 0;
ListIndex mListIndex = 0;
AttributeId mFieldId = 0;
EndpointId mEndpointId = 0;
BitFlags<Flags> mFlags;
ClusterInfo * mpNext = nullptr;
EventId mEventId = 0;
/* For better structure alignment
* Above ordering is by bit-size to ensure least amount of memory alignment padding.
* Changing order to something more natural (e.g. clusterid before nodeid) will result
* in extra memory alignment padding.
* uint64 mNodeId
* uint16_t mClusterId
* uint16_t mListIndex
* uint8_t FieldId
* uint8_t EndpointId
* uint8_t mDirty
* uint8_t mType
* uint32_t mpNext
* uint16_t EventId
* padding 2 bytes
*/

Optional<NodeId> mNodeId;
Optional<EndpointId> mEndpointId;
Optional<ClusterId> mClusterId;
Optional<AttributeId> mFieldId;
Optional<EventId> mEventId;
Optional<ListIndex> mListIndex;
ClusterInfo * mpNext = nullptr; // pointer width (uint32 or uint64)
};
} // namespace app
} // namespace chip
11 changes: 8 additions & 3 deletions src/app/ConcreteAttributePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ namespace app {
*/
struct ConcreteAttributePath
{
static constexpr EndpointId kInvalidEndpointId = 0xFFFF;
static constexpr ClusterId kInvalidClusterId = 0xFFFF'FFFF;
static constexpr AttributeId kInvalidAttributeId = 0xFFFF'FFFF;

ConcreteAttributePath() {}
ConcreteAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
mEndpointId(aEndpointId), mClusterId(aClusterId), mAttributeId(aAttributeId)
{}
Expand All @@ -37,9 +42,9 @@ struct ConcreteAttributePath
return mEndpointId == other.mEndpointId && mClusterId == other.mClusterId && mAttributeId == other.mAttributeId;
}

EndpointId mEndpointId = 0;
ClusterId mClusterId = 0;
AttributeId mAttributeId = 0;
EndpointId mEndpointId = kInvalidEndpointId;
ClusterId mClusterId = kInvalidClusterId;
AttributeId mAttributeId = kInvalidAttributeId;
};
} // namespace app
} // namespace chip
6 changes: 4 additions & 2 deletions src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,10 @@ static bool IsInterestedEventPaths(EventLoadOutContext * eventLoadOutContext, co
}
while (interestedEventPaths != nullptr)
{
if (interestedEventPaths->mNodeId == event.mNodeId && interestedEventPaths->mEndpointId == event.mEndpointId &&
interestedEventPaths->mClusterId == event.mClusterId && interestedEventPaths->mEventId == event.mEventId)
if (interestedEventPaths->mNodeId == Optional<NodeId>(event.mNodeId) &&
interestedEventPaths->mEndpointId == Optional<EndpointId>(event.mEndpointId) &&
interestedEventPaths->mClusterId == Optional<ClusterId>(event.mClusterId) &&
interestedEventPaths->mEventId == Optional<EventId>(event.mEventId))
{
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ void InteractionModelEngine::ReleaseClusterInfoList(ClusterInfo *& aClusterInfo)
{
lastClusterInfo = lastClusterInfo->mpNext;
}
lastClusterInfo->mFlags.ClearAll();
lastClusterInfo->mpNext = mpNextAvailableClusterInfo;
mpNextAvailableClusterInfo = aClusterInfo;
aClusterInfo = nullptr;
Expand Down Expand Up @@ -508,7 +507,6 @@ bool InteractionModelEngine::MergeOverlappedAttributePath(ClusterInfo * apAttrib
{
runner->mListIndex = aAttributePath.mListIndex;
runner->mFieldId = aAttributePath.mFieldId;
runner->mFlags = aAttributePath.mFlags;
return true;
}
runner = runner->mpNext;
Expand Down
26 changes: 26 additions & 0 deletions src/app/MessageDef/AttributePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>

#include <app/AppBuildConfig.h>
#include <lib/core/Optional.h>

using namespace chip;
using namespace chip::TLV;
Expand Down Expand Up @@ -193,6 +194,31 @@ CHIP_ERROR AttributePath::Parser::GetListIndex(chip::ListIndex * const apListInd
return GetUnsignedInteger(kCsTag_ListIndex, apListIndex);
}

CHIP_ERROR AttributePath::Parser::GetNodeId(chip::Optional<NodeId> & aNodeId) const
{
return GetUnsignedInteger(kCsTag_NodeId, aNodeId);
}

CHIP_ERROR AttributePath::Parser::GetEndpointId(chip::Optional<EndpointId> & aEndpointId) const
{
return GetUnsignedInteger(kCsTag_EndpointId, aEndpointId);
}

CHIP_ERROR AttributePath::Parser::GetClusterId(chip::Optional<ClusterId> & aClusterId) const
{
return GetUnsignedInteger(kCsTag_ClusterId, aClusterId);
}

CHIP_ERROR AttributePath::Parser::GetFieldId(chip::Optional<FieldId> & aFieldId) const
{
return GetUnsignedInteger(kCsTag_FieldId, aFieldId);
}

CHIP_ERROR AttributePath::Parser::GetListIndex(chip::Optional<ListIndex> & aListIndex) const
{
return GetUnsignedInteger(kCsTag_ListIndex, aListIndex);
}

CHIP_ERROR AttributePath::Builder::_Init(chip::TLV::TLVWriter * const apWriter, const Tag aTag)
{
mpWriter = apWriter;
Expand Down
Loading