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

Implement the codegen-data-model Write support #34139

Merged
merged 98 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
ba43ab7
Initial copy/merge of the codegendatamodel::write support
andy31415 Jun 28, 2024
c13f3ce
Restyle
andy31415 Jun 28, 2024
79f9603
Remove the error translation for ACL checks for attribute writes
andy31415 Jun 28, 2024
3648820
Comment correction after special access error code guarantees were re…
andy31415 Jun 28, 2024
f2911ef
Set the namespace for DataModel to resolve nameclash for android builds
andy31415 Jun 28, 2024
fedfc79
Restyle
andy31415 Jun 28, 2024
2a632f3
Some changes to make darwin builds happy
andy31415 Jun 28, 2024
e22cbf9
Merge branch 'master' into imdm/4-ember-write-attribute
andreilitvin Jul 2, 2024
97490f9
Do not clang-tidy on CodegenDataModel_Write
andreilitvin Jul 2, 2024
a0bdfd2
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
7f30755
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
8a158ef
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
6566f31
Merge branch 'master' into imdm/4-ember-write-attribute
andy31415 Jul 3, 2024
55e45d4
Use little endian encoding for pascal long strings, since this is wha…
andy31415 Jul 3, 2024
1b54c1a
Restyle
andy31415 Jul 3, 2024
53458e4
Merge branch 'imdm/4-ember-write-attribute' of github.com:andy31415/c…
andy31415 Jul 3, 2024
2518cba
Fix code to compile and pass tests
andy31415 Jul 3, 2024
4fd538a
Code review comments
andy31415 Jul 3, 2024
77bc4fe
Comment update
andy31415 Jul 3, 2024
929b5f6
Update based on code review feedback
andy31415 Jul 3, 2024
1c15644
Wrong condition. Fixed
andy31415 Jul 3, 2024
b61b3aa
Return invalid value to match ember-compatibility-functions
andy31415 Jul 3, 2024
0992f6d
switch invalid data point to constraint error for return codes
andy31415 Jul 3, 2024
5b7d536
Fix code review comments: comments and return unsupportedaccess
andy31415 Jul 3, 2024
65baf80
Remove useless comment - error check should be clear enough
andy31415 Jul 3, 2024
76b7e7d
Comment update
andy31415 Jul 3, 2024
1f79a52
Re-arrange code for read only and timed
andy31415 Jul 3, 2024
a629992
Re-format the read only checks a bit
andy31415 Jul 3, 2024
3741c61
Use CHIP_ERROR_NOT_FOUND
andy31415 Jul 3, 2024
e252bb0
Separate out variable names
andy31415 Jul 3, 2024
2b0bc7a
Slight updated code layout
andy31415 Jul 3, 2024
3296766
Updated return value for chip error
andy31415 Jul 3, 2024
16cc088
Updated test to verifyordie instead of just logging errors
andy31415 Jul 3, 2024
682b73a
Update src/app/codegen-data-model/tests/TestCodegenModelViaMocks.cpp
andy31415 Jul 3, 2024
37f4298
Update based on review feedback
andy31415 Jul 3, 2024
d50ac27
Merge branch 'imdm/4-ember-write-attribute' of github.com:andy31415/c…
andy31415 Jul 3, 2024
03bb45f
Fix endianess and copying in test code
andy31415 Jul 3, 2024
8b630b0
Restyle
andy31415 Jul 3, 2024
412d681
Updated comment
andy31415 Jul 3, 2024
413b5e7
Update src/app/codegen-data-model/tests/TestCodegenModelViaMocks.cpp
andy31415 Jul 3, 2024
8b1a1b1
Add unit test for "lowest signed value write"
andy31415 Jul 3, 2024
516a1c9
Restyle
andy31415 Jul 3, 2024
2aed85d
A constraint error update and better tests for AAI returning errors
andy31415 Jul 3, 2024
1a48fc9
One more test for invalid ember usage
andy31415 Jul 3, 2024
6fc8d17
Restyle
andy31415 Jul 3, 2024
e93665b
more tests for more coverage
andy31415 Jul 3, 2024
0e4398b
Comment update
andy31415 Jul 3, 2024
41403a4
Fix comment
andy31415 Jul 3, 2024
39da04f
One more test for more coverage
andy31415 Jul 3, 2024
c5feac7
Also cover writing non-null value to nullable attribute
andy31415 Jul 3, 2024
6b05337
Fix the ember string usage
andy31415 Jul 3, 2024
46a5ad2
Update src/app/codegen-data-model/CodegenDataModel_Read.cpp
andy31415 Jul 3, 2024
8631b41
Update src/app/codegen-data-model/CodegenDataModel_Read.cpp
andy31415 Jul 3, 2024
5a4bd3c
Remove duplicate code
andy31415 Jul 3, 2024
6e0d9d1
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
4bdc2ee
Remove chip::app:: prefix in unit test since we have a top level using
andy31415 Jul 3, 2024
e6e5b19
Merge branch 'imdm/4-ember-write-attribute' of github.com:andy31415/c…
andy31415 Jul 3, 2024
f6e1bb9
Fix copy & paste encode to decode
andy31415 Jul 3, 2024
4bd7e28
Replace decoded with converted
andy31415 Jul 3, 2024
8e2383c
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
e921ead
Merge branch 'imdm/4-ember-write-attribute' of github.com:andy31415/c…
andy31415 Jul 3, 2024
575b492
Start using Failure for invalid data type instead of unsupported read…
andy31415 Jul 3, 2024
7b5bb9f
Fix comments
andy31415 Jul 3, 2024
da1aae4
Updated encode/decode comment
andy31415 Jul 3, 2024
dc1c658
Use failure instead of constraint error
andy31415 Jul 3, 2024
4dab89b
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
e39cb80
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
f58fee8
Update src/app/codegen-data-model/CodegenDataModel_Write.cpp
andy31415 Jul 3, 2024
380ac9f
Merge branch 'imdm/4-ember-write-attribute' of github.com:andy31415/c…
andy31415 Jul 3, 2024
8e87177
Use dataversion mismatch for write without a version
andy31415 Jul 3, 2024
572a7f3
Add extra IsGlobalAttribute check
andy31415 Jul 3, 2024
84fb496
use external writes for the ember write logic, so that we have extra …
andy31415 Jul 3, 2024
db487b4
Updated comments
andy31415 Jul 3, 2024
2d216ab
more comments
andy31415 Jul 3, 2024
865b80a
Restyle
andy31415 Jul 3, 2024
e542231
Use emberAfWriteAttribute
andy31415 Jul 4, 2024
5664e7b
Add comment about ember-string
andy31415 Jul 4, 2024
1df88a2
Restyle
andy31415 Jul 4, 2024
1475193
Add context to unit tests, make write do the marking of dirty paths
andy31415 Jul 4, 2024
b26edf1
Add some unit tests for dirty path handling
andy31415 Jul 4, 2024
faac4fb
Move the change callback around a bit
andy31415 Jul 4, 2024
56fe685
Restyle
andy31415 Jul 4, 2024
94392d6
Fixed unit tests to support size checks
andy31415 Jul 4, 2024
2ab7dd7
Add unit test for invalid data
andy31415 Jul 4, 2024
ecd4fd4
Restyle
andy31415 Jul 4, 2024
73ff5ae
Fix linter errors
andy31415 Jul 4, 2024
c36c64e
Update to make size enforcement and guarantees clearer
andy31415 Jul 4, 2024
9f1b7de
use size_t for getlength sizes
andy31415 Jul 4, 2024
0eb94b5
Review comments and updated code to compile for android
andy31415 Jul 4, 2024
e5fffd6
make datamodel unambiguous
andy31415 Jul 5, 2024
a379908
More fixes for clang compilation for DataModel scoping
andy31415 Jul 5, 2024
b78f27a
Restyle
andy31415 Jul 5, 2024
e10fefc
Try to make darwin compiler happy ... ssize_t vs size_t
andy31415 Jul 5, 2024
40ae352
Fix typo
andy31415 Jul 5, 2024
a6e2ddb
Restyle
andy31415 Jul 5, 2024
9bbe92a
Merge branch 'master' into imdm/4-ember-write-attribute
andy31415 Jul 5, 2024
1c04a69
Code review updates
andy31415 Jul 5, 2024
08c4fbd
Undo submodule update
andy31415 Jul 5, 2024
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
7 changes: 6 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ jobs:
run: |
./scripts/run_in_build_env.sh "./scripts/run_codegen_targets.sh out/sanitizers"
- name: Clang-tidy validation
# NOTE: clang-tidy crashes on CodegenDataModel_Write due to Nullable/std::optional check.
# See https://github.com/llvm/llvm-project/issues/97426
run: |
./scripts/run_in_build_env.sh \
"./scripts/run-clang-tidy-on-compile-commands.py \
--compile-database out/sanitizers/compile_commands.json \
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|-ReadImpl|-InvokeSubscribeImpl' \
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|-ReadImpl|-InvokeSubscribeImpl|CodegenDataModel_Write' \
check \
"
- name: Clean output
Expand Down Expand Up @@ -422,10 +424,13 @@ jobs:
run: |
./scripts/run_in_build_env.sh "./scripts/run_codegen_targets.sh out/default"
- name: Clang-tidy validation
# NOTE: clang-tidy crashes on CodegenDataModel_Write due to Nullable/std::optional check.
# See https://github.com/llvm/llvm-project/issues/97426
run: |
./scripts/run_in_build_env.sh \
"./scripts/run-clang-tidy-on-compile-commands.py \
--compile-database out/default/compile_commands.json \
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|CodegenDataModel_Write' \
check \
"
- name: Uploading diagnostic logs
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,17 @@ jobs:
type-safe setters
if: always()
run: |
git grep -I -n 'emberAfWriteAttribute' -- './*' ':(exclude).github/workflows/lint.yml' ':(exclude)zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp' ':(exclude)src/app/zap-templates/templates/app/attributes/Accessors-src.zapt' ':(exclude)src/app/util/attribute-table.cpp' ':(exclude)examples/common/pigweed/rpc_services/Attributes.h' ':(exclude)src/app/util/attribute-table.h' ':(exclude)src/app/util/ember-compatibility-functions.cpp' && exit 1 || exit 0
git grep -I -n 'emberAfWriteAttribute' -- './*' \
':(exclude).github/workflows/lint.yml' \
':(exclude)examples/common/pigweed/rpc_services/Attributes.h' \
':(exclude)src/app/codegen-data-model/CodegenDataModel_Write.cpp' \
':(exclude)src/app/codegen-data-model/tests/EmberReadWriteOverride.cpp' \
':(exclude)src/app/util/attribute-table.cpp' \
':(exclude)src/app/util/attribute-table.h' \
':(exclude)src/app/util/ember-compatibility-functions.cpp' \
':(exclude)src/app/zap-templates/templates/app/attributes/Accessors-src.zapt' \
':(exclude)zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp' \
&& exit 1 || exit 0

# Run ruff python linter
- name: Check for errors using ruff Python linter
Expand Down
5 changes: 4 additions & 1 deletion src/app/codegen-data-model/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import("//build_overrides/chip.gni")
#
# Use `model.gni` to get access to:
# CodegenDataModel.cpp
# CodegenDataModel_Read.cpp
# CodegenDataModel.h
# CodegenDataModel_Read.cpp
# CodegenDataModel_Write.cpp
# EmberMetadata.cpp
# EmberMetadata.h
#
# The above list of files exists to satisfy the "dependency linter"
# since those files should technically be "visible to gn" even though we
Expand Down
7 changes: 0 additions & 7 deletions src/app/codegen-data-model/CodegenDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,6 @@ bool CodegenDataModel::EmberCommandListIterator::Exists(const CommandId * list,
return (*mCurrentHint == toCheck);
}

CHIP_ERROR CodegenDataModel::WriteAttribute(const InteractionModel::WriteAttributeRequest & request,
AttributeValueDecoder & decoder)
{
// TODO: this needs an implementation
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR CodegenDataModel::Invoke(const InteractionModel::InvokeRequest & request, TLV::TLVReader & input_arguments,
InteractionModel::InvokeReply & reply)
{
Expand Down
97 changes: 33 additions & 64 deletions src/app/codegen-data-model/CodegenDataModel_Read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/core/CHIPError.h"
#include <app/codegen-data-model/CodegenDataModel.h>

#include <optional>
Expand All @@ -29,6 +28,7 @@
#include <app/AttributeValueEncoder.h>
#include <app/GlobalAttributes.h>
#include <app/RequiredPrivilege.h>
#include <app/codegen-data-model/EmberMetadata.h>
#include <app/data-model/FabricScoped.h>
#include <app/util/af-types.h>
#include <app/util/attribute-metadata.h>
Expand All @@ -49,56 +49,6 @@ namespace app {
namespace {
using namespace chip::app::Compatibility::Internal;

// Fetch the source for the given attribute path: either a cluster (for global ones) or attribute
// path.
//
// if returning a CHIP_ERROR, it will NEVER be CHIP_NO_ERROR.
std::variant<const EmberAfCluster *, // global attribute, data from a cluster
const EmberAfAttributeMetadata *, // a specific attribute stored by ember
CHIP_ERROR // error, this will NEVER be CHIP_NO_ERROR
>
FindAttributeMetadata(const ConcreteAttributePath & aPath)
{
for (auto & attr : GlobalAttributesNotInMetadata)
{

if (attr == aPath.mAttributeId)
{
const EmberAfCluster * cluster = emberAfFindServerCluster(aPath.mEndpointId, aPath.mClusterId);
if (cluster == nullptr)
{
return (emberAfFindEndpointType(aPath.mEndpointId) == nullptr) ? CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint)
: CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
}

return cluster;
}
}
const EmberAfAttributeMetadata * metadata =
emberAfLocateAttributeMetadata(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);

if (metadata == nullptr)
{
const EmberAfEndpointType * type = emberAfFindEndpointType(aPath.mEndpointId);
if (type == nullptr)
{
return CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint);
}

const EmberAfCluster * cluster = emberAfFindClusterInType(type, aPath.mClusterId, CLUSTER_MASK_SERVER);
if (cluster == nullptr)
{
return CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
}

// Since we know the attribute is unsupported and the endpoint/cluster are
// OK, this is the only option left.
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}

return metadata;
}

/// Attempts to read via an attribute access interface (AAI)
///
/// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success).
Expand Down Expand Up @@ -138,13 +88,30 @@ struct ShortPascalString
{
using LengthType = uint8_t;
static constexpr LengthType kNullLength = 0xFF;

static size_t GetLength(ByteSpan buffer)
{
VerifyOrDie(buffer.size() >= 1);
// NOTE: we do NOT use emberAfStringLength from ember-strings.h because that will result in 0
// length for null sizes (i.e. 0xFF is translated to 0 and we do not want that here)
return buffer[0];
}
};

/// Metadata of what a ember/pascal LONG string means (prepended by a u16 length)
struct LongPascalString
{
using LengthType = uint16_t;
static constexpr LengthType kNullLength = 0xFFFF;

static size_t GetLength(ByteSpan buffer)
{
// NOTE: we do NOT use emberAfLongStringLength from ember-strings.h because that will result in 0
// length for null sizes (i.e. 0xFFFF is translated to 0 and we do not want that here)
VerifyOrDie(buffer.size() >= 2);
const uint8_t * data = buffer.data();
return Encoding::LittleEndian::Read16(data);
}
};

// ember assumptions ... should just work
Expand All @@ -157,20 +124,17 @@ static_assert(sizeof(LongPascalString::LengthType) == 2);
template <class OUT, class ENCODING>
std::optional<OUT> ExtractEmberString(ByteSpan data)
{
typename ENCODING::LengthType len;

// Ember storage format for pascal-prefix data is specifically "native byte order",
// hence the use of memcpy.
VerifyOrDie(sizeof(len) <= data.size());
memcpy(&len, data.data(), sizeof(len));
constexpr size_t kLengthTypeSize = sizeof(typename ENCODING::LengthType);
VerifyOrDie(kLengthTypeSize <= data.size());
auto len = ENCODING::GetLength(data);

if (len == ENCODING::kNullLength)
{
return std::nullopt;
}

VerifyOrDie(static_cast<size_t>(len + sizeof(len)) <= data.size());
return std::make_optional<OUT>(reinterpret_cast<typename OUT::pointer>(data.data() + sizeof(len)), len);
VerifyOrDie(len + sizeof(len) <= data.size());
return std::make_optional<OUT>(reinterpret_cast<typename OUT::pointer>(data.data() + kLengthTypeSize), len);
}

/// Encode a value inside `encoder`
Expand Down Expand Up @@ -282,7 +246,7 @@ CHIP_ERROR EncodeEmberValue(ByteSpan data, const EmberAfAttributeMetadata * meta
return EncodeStringLike<ByteSpan, LongPascalString>(data, isNullable, encoder);
default:
ChipLogError(DataManagement, "Attribute type 0x%x not handled", static_cast<int>(metadata->attributeType));
return CHIP_IM_GLOBAL_STATUS(UnsupportedRead);
return CHIP_IM_GLOBAL_STATUS(Failure);
}
}

Expand Down Expand Up @@ -311,21 +275,26 @@ CHIP_ERROR CodegenDataModel::ReadAttribute(const InteractionModel::ReadAttribute
RequiredPrivilege::ForReadAttribute(request.path));
if (err != CHIP_NO_ERROR)
{
ReturnErrorCodeIf(err != CHIP_ERROR_ACCESS_DENIED, err);

// Implementation of 8.4.3.2 of the spec for path expansion
if (request.path.mExpanded && (err == CHIP_ERROR_ACCESS_DENIED))
if (request.path.mExpanded)
{
return CHIP_NO_ERROR;
}
return err;
// access denied has a specific code for IM
return CHIP_IM_GLOBAL_STATUS(UnsupportedAccess);
}
}

auto metadata = FindAttributeMetadata(request.path);
auto metadata = Ember::FindAttributeMetadata(request.path);

// Explicit failure in finding a suitable metadata
if (const CHIP_ERROR * err = std::get_if<CHIP_ERROR>(&metadata))
{
VerifyOrDie(*err != CHIP_NO_ERROR);
VerifyOrDie((*err == CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint)) || //
(*err == CHIP_IM_GLOBAL_STATUS(UnsupportedCluster)) || //
(*err == CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute)));
return *err;
}

Expand Down
Loading
Loading