Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2024 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <app/ConcreteAttributePath.h>
21 : #include <app/util/af-types.h>
22 : #include <app/util/attribute-metadata.h>
23 : #include <lib/core/DataModelTypes.h>
24 : #include <protocols/interaction_model/StatusCode.h>
25 :
26 : #include <cstdint>
27 :
28 : struct EmberAfWriteDataInput
29 : {
30 : // Where the input data originates from.
31 : //
32 : // NOTE: at this time there is no information regarding
33 : // input buffer size and it is assumed "correct" for the given data type.
34 : //
35 : // NOTE: technically this should be `const uint8_t*`, however ember internal logic uses a
36 : // `emAfReadOrWriteAttribute` method and because of the duality of read/write it needs
37 : // a non-const input. This is odd and should probably be fixed.
38 : uint8_t * dataPtr;
39 :
40 : // The data type that dataPtr points to
41 : EmberAfAttributeType dataType;
42 :
43 : // Controls when `changeListener` is called to flag an attribute dirty. It allows for things like:
44 : // kIfChanged - only if the dataPtr contains a different value than what currenty exists
45 : // kNo - never called
46 : // kYes - always called
47 : chip::app::MarkAttributeDirty markDirty = chip::app::MarkAttributeDirty::kIfChanged;
48 :
49 : // Listener called when when the written data is consided changed/dirty.
50 : // This being called depends on settings of `markDirty` combined with the actual contents of dataPtr
51 : // vs the contents of the current attribute storage.
52 : chip::app::AttributesChangedListener * changeListener = nullptr;
53 :
54 118 : EmberAfWriteDataInput(uint8_t * data, EmberAfAttributeType type) : dataPtr(data), dataType(type) {}
55 :
56 : EmberAfWriteDataInput & SetMarkDirty(chip::app::MarkAttributeDirty value)
57 : {
58 : markDirty = value;
59 : return *this;
60 : }
61 :
62 118 : EmberAfWriteDataInput & SetChangeListener(chip::app::AttributesChangedListener * listener)
63 : {
64 118 : changeListener = listener;
65 118 : return *this;
66 : }
67 : };
68 :
69 : /**
70 : * @brief write an attribute, performing all the checks.
71 : *
72 : * TODO: what are "all the checks"? There are limitations below
73 : * and generally input data pointer does not even have a size,
74 : * hence data validity check capabilities seem limited.
75 : *
76 : * This function will attempt to write the attribute value from
77 : * the provided pointer. This function will only check that the
78 : * attribute exists. If it does it will write the value into
79 : * the attribute table for the given attribute.
80 : *
81 : * This function will not check to see if the attribute is
82 : * writable since the read only / writable characteristic
83 : * of an attribute only pertains to external devices writing
84 : * over the air. Because this function is being called locally
85 : * it assumes that the device knows what it is doing and has permission
86 : * to perform the given operation.
87 : *
88 : * This function also does NOT check that the input dataType matches the expected
89 : * data type (as Accessors.h/cpp have this correct by default).
90 : * TODO: this not checking seems off - what if this is run without Accessors.h ?
91 : */
92 : chip::Protocols::InteractionModel::Status emberAfWriteAttribute(const chip::app::ConcreteAttributePath & path,
93 : const EmberAfWriteDataInput & input);
94 :
95 : /**
96 : * Override of emberAfWriteAttribute to reduce code size for calls that default
97 : * markDirty/changeListener and any other future customization calls.
98 : */
99 : chip::Protocols::InteractionModel::Status emberAfWriteAttribute(chip::EndpointId endpoint, chip::ClusterId cluster,
100 : chip::AttributeId attributeID, uint8_t * dataPtr,
101 : EmberAfAttributeType dataType);
102 :
103 : /**
104 : * @brief Read the attribute value, performing all the checks.
105 : *
106 : * This function will attempt to read the attribute and store it into the
107 : * pointer.
108 : *
109 : * dataPtr may be NULL, signifying that we don't need the value, just the status
110 : * (i.e. whether the attribute can be read).
111 : */
112 : chip::Protocols::InteractionModel::Status emberAfReadAttribute(chip::EndpointId endpoint, chip::ClusterId cluster,
113 : chip::AttributeId attributeID, uint8_t * dataPtr,
114 : uint16_t readLength);
|