Skip to content

Commit 5240195

Browse files
authored
Merge faac4fb into e26859c
2 parents e26859c + faac4fb commit 5240195

15 files changed

+1437
-131
lines changed

.github/workflows/build.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ jobs:
204204
run: |
205205
./scripts/run_in_build_env.sh "./scripts/run_codegen_targets.sh out/sanitizers"
206206
- name: Clang-tidy validation
207+
# NOTE: clang-tidy crashes on CodegenDataModel_Write due to Nullable/std::optional check.
208+
# See https://github.com/llvm/llvm-project/issues/97426
207209
run: |
208210
./scripts/run_in_build_env.sh \
209211
"./scripts/run-clang-tidy-on-compile-commands.py \
210212
--compile-database out/sanitizers/compile_commands.json \
211-
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|-ReadImpl|-InvokeSubscribeImpl' \
213+
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|-ReadImpl|-InvokeSubscribeImpl|CodegenDataModel_Write' \
212214
check \
213215
"
214216
- name: Clean output
@@ -422,10 +424,13 @@ jobs:
422424
run: |
423425
./scripts/run_in_build_env.sh "./scripts/run_codegen_targets.sh out/default"
424426
- name: Clang-tidy validation
427+
# NOTE: clang-tidy crashes on CodegenDataModel_Write due to Nullable/std::optional check.
428+
# See https://github.com/llvm/llvm-project/issues/97426
425429
run: |
426430
./scripts/run_in_build_env.sh \
427431
"./scripts/run-clang-tidy-on-compile-commands.py \
428432
--compile-database out/default/compile_commands.json \
433+
--file-exclude-regex '/(repo|zzz_generated|lwip/standalone)/|CodegenDataModel_Write' \
429434
check \
430435
"
431436
- name: Uploading diagnostic logs

src/app/codegen-data-model/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import("//build_overrides/chip.gni")
2020
#
2121
# Use `model.gni` to get access to:
2222
# CodegenDataModel.cpp
23-
# CodegenDataModel_Read.cpp
2423
# CodegenDataModel.h
24+
# CodegenDataModel_Read.cpp
25+
# CodegenDataModel_Write.cpp
26+
# EmberMetadata.cpp
27+
# EmberMetadata.h
2528
#
2629
# The above list of files exists to satisfy the "dependency linter"
2730
# since those files should technically be "visible to gn" even though we

src/app/codegen-data-model/CodegenDataModel.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,6 @@ bool CodegenDataModel::EmberCommandListIterator::Exists(const CommandId * list,
231231
return (*mCurrentHint == toCheck);
232232
}
233233

234-
CHIP_ERROR CodegenDataModel::WriteAttribute(const InteractionModel::WriteAttributeRequest & request,
235-
AttributeValueDecoder & decoder)
236-
{
237-
// TODO: this needs an implementation
238-
return CHIP_ERROR_NOT_IMPLEMENTED;
239-
}
240-
241234
CHIP_ERROR CodegenDataModel::Invoke(const InteractionModel::InvokeRequest & request, TLV::TLVReader & input_arguments,
242235
InteractionModel::InvokeReply & reply)
243236
{

src/app/codegen-data-model/CodegenDataModel_Read.cpp

+27-62
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#include "lib/core/CHIPError.h"
1817
#include <app/codegen-data-model/CodegenDataModel.h>
1918

2019
#include <optional>
@@ -29,6 +28,7 @@
2928
#include <app/AttributeValueEncoder.h>
3029
#include <app/GlobalAttributes.h>
3130
#include <app/RequiredPrivilege.h>
31+
#include <app/codegen-data-model/EmberMetadata.h>
3232
#include <app/data-model/FabricScoped.h>
3333
#include <app/util/af-types.h>
3434
#include <app/util/attribute-metadata.h>
@@ -49,56 +49,6 @@ namespace app {
4949
namespace {
5050
using namespace chip::app::Compatibility::Internal;
5151

52-
// Fetch the source for the given attribute path: either a cluster (for global ones) or attribute
53-
// path.
54-
//
55-
// if returning a CHIP_ERROR, it will NEVER be CHIP_NO_ERROR.
56-
std::variant<const EmberAfCluster *, // global attribute, data from a cluster
57-
const EmberAfAttributeMetadata *, // a specific attribute stored by ember
58-
CHIP_ERROR // error, this will NEVER be CHIP_NO_ERROR
59-
>
60-
FindAttributeMetadata(const ConcreteAttributePath & aPath)
61-
{
62-
for (auto & attr : GlobalAttributesNotInMetadata)
63-
{
64-
65-
if (attr == aPath.mAttributeId)
66-
{
67-
const EmberAfCluster * cluster = emberAfFindServerCluster(aPath.mEndpointId, aPath.mClusterId);
68-
if (cluster == nullptr)
69-
{
70-
return (emberAfFindEndpointType(aPath.mEndpointId) == nullptr) ? CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint)
71-
: CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
72-
}
73-
74-
return cluster;
75-
}
76-
}
77-
const EmberAfAttributeMetadata * metadata =
78-
emberAfLocateAttributeMetadata(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);
79-
80-
if (metadata == nullptr)
81-
{
82-
const EmberAfEndpointType * type = emberAfFindEndpointType(aPath.mEndpointId);
83-
if (type == nullptr)
84-
{
85-
return CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint);
86-
}
87-
88-
const EmberAfCluster * cluster = emberAfFindClusterInType(type, aPath.mClusterId, CLUSTER_MASK_SERVER);
89-
if (cluster == nullptr)
90-
{
91-
return CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
92-
}
93-
94-
// Since we know the attribute is unsupported and the endpoint/cluster are
95-
// OK, this is the only option left.
96-
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
97-
}
98-
99-
return metadata;
100-
}
101-
10252
/// Attempts to read via an attribute access interface (AAI)
10353
///
10454
/// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success).
@@ -138,13 +88,27 @@ struct ShortPascalString
13888
{
13989
using LengthType = uint8_t;
14090
static constexpr LengthType kNullLength = 0xFF;
91+
92+
static LengthType GetLength(const uint8_t * buffer)
93+
{
94+
// NOTE: we do NOT use emberAfLongStringLength because that will result in 0 length
95+
// for null strings
96+
return *buffer;
97+
}
14198
};
14299

143100
/// Metadata of what a ember/pascal LONG string means (prepended by a u16 length)
144101
struct LongPascalString
145102
{
146103
using LengthType = uint16_t;
147104
static constexpr LengthType kNullLength = 0xFFFF;
105+
106+
static LengthType GetLength(const uint8_t * buffer)
107+
{
108+
// NOTE: we do NOT use emberAfLongStringLength because that will result in 0 length
109+
// for null strings
110+
return Encoding::LittleEndian::Read16(buffer);
111+
}
148112
};
149113

150114
// ember assumptions ... should just work
@@ -157,12 +121,8 @@ static_assert(sizeof(LongPascalString::LengthType) == 2);
157121
template <class OUT, class ENCODING>
158122
std::optional<OUT> ExtractEmberString(ByteSpan data)
159123
{
160-
typename ENCODING::LengthType len;
161-
162-
// Ember storage format for pascal-prefix data is specifically "native byte order",
163-
// hence the use of memcpy.
164-
VerifyOrDie(sizeof(len) <= data.size());
165-
memcpy(&len, data.data(), sizeof(len));
124+
VerifyOrDie(sizeof(typename ENCODING::LengthType) <= data.size());
125+
auto len = ENCODING::GetLength(data.data());
166126

167127
if (len == ENCODING::kNullLength)
168128
{
@@ -282,7 +242,7 @@ CHIP_ERROR EncodeEmberValue(ByteSpan data, const EmberAfAttributeMetadata * meta
282242
return EncodeStringLike<ByteSpan, LongPascalString>(data, isNullable, encoder);
283243
default:
284244
ChipLogError(DataManagement, "Attribute type 0x%x not handled", static_cast<int>(metadata->attributeType));
285-
return CHIP_IM_GLOBAL_STATUS(UnsupportedRead);
245+
return CHIP_IM_GLOBAL_STATUS(Failure);
286246
}
287247
}
288248

@@ -311,21 +271,26 @@ CHIP_ERROR CodegenDataModel::ReadAttribute(const InteractionModel::ReadAttribute
311271
RequiredPrivilege::ForReadAttribute(request.path));
312272
if (err != CHIP_NO_ERROR)
313273
{
274+
ReturnErrorCodeIf(err != CHIP_ERROR_ACCESS_DENIED, err);
275+
314276
// Implementation of 8.4.3.2 of the spec for path expansion
315-
if (request.path.mExpanded && (err == CHIP_ERROR_ACCESS_DENIED))
277+
if (request.path.mExpanded)
316278
{
317279
return CHIP_NO_ERROR;
318280
}
319-
return err;
281+
// access denied has a specific code for IM
282+
return CHIP_IM_GLOBAL_STATUS(UnsupportedAccess);
320283
}
321284
}
322285

323-
auto metadata = FindAttributeMetadata(request.path);
286+
auto metadata = Ember::FindAttributeMetadata(request.path);
324287

325288
// Explicit failure in finding a suitable metadata
326289
if (const CHIP_ERROR * err = std::get_if<CHIP_ERROR>(&metadata))
327290
{
328-
VerifyOrDie(*err != CHIP_NO_ERROR);
291+
VerifyOrDie((*err == CHIP_IM_GLOBAL_STATUS(UnsupportedEndpoint)) || //
292+
(*err == CHIP_IM_GLOBAL_STATUS(UnsupportedCluster)) || //
293+
(*err == CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute)));
329294
return *err;
330295
}
331296

0 commit comments

Comments
 (0)