22
22
#include < access/Privilege.h>
23
23
#include < lib/core/DataModelTypes.h>
24
24
#include < lib/support/BitFlags.h>
25
+ #include < assert.h>
25
26
26
27
namespace chip {
27
28
namespace app {
@@ -84,48 +85,102 @@ enum class AttributeQualityFlags : uint32_t
84
85
kTimed = 0x0040 , // `T` quality on attributes (writes require timed interactions)
85
86
};
86
87
88
+
89
+ struct attribute_entry_mask_t {
90
+
91
+ // attribute quality flags
92
+ //
93
+ std::underlying_type_t <AttributeQualityFlags> flags : 7 ; // generally defaults to View if readable
94
+
95
+ // read/write access privilege variables
96
+ //
97
+ std::underlying_type_t <Access::Privilege> readPrivilege : 5 ; // generally defaults to View if readable
98
+ std::underlying_type_t <Access::Privilege> writePrivilege : 5 ; // generally defaults to Operate if writable
99
+
100
+ };
101
+
102
+
103
+ // Static ASSERT to check size of mask type.
104
+ static_assert (sizeof (attribute_entry_mask_t ) <= 4 , " Size of attribute_entry_mask_t is not as expected." );
105
+
106
+ // zero is not a valid privilege, just a default initial value for privileges
107
+ constexpr std::underlying_type_t <Access::Privilege> kNoPrivilege (0 ) ;
108
+
109
+
87
110
struct AttributeEntry
88
111
{
89
112
AttributeId attributeId;
90
- BitFlags<AttributeQualityFlags> flags;
91
-
92
113
93
114
// Constructor
94
- AttributeEntry () : readPrivilege (0 ), writePrivilege( 0 ) {}
115
+ constexpr AttributeEntry () : attributeId (0 ), mask{ 0 , kNoPrivilege , kNoPrivilege } {}
95
116
117
+
96
118
void SetReadPrivilege (Access::Privilege r)
97
119
{
98
- readPrivilege = chip::to_underlying (r);
120
+ // Static ASSERT to check size of readPrivilege type vs entry parameter.
121
+ static_assert (sizeof (std::underlying_type_t <Access::Privilege>) >=
122
+ sizeof (chip::to_underlying (r)),
123
+ " Size of readPrivilege is not able to accomodate parameter (r)." );
124
+
125
+ // ASSERT to validate entry parameter value.
126
+ assert (kNoPrivilege != chip::to_underlying (r));
127
+
128
+
129
+ mask.readPrivilege = chip::to_underlying (r);
99
130
}
100
131
101
132
Access::Privilege GetReadPrivilege () const
102
133
{
103
- return static_cast < Access::Privilege >(readPrivilege);
134
+ return static_cast < Access::Privilege >(mask. readPrivilege );
104
135
}
105
136
106
137
void SetWritePrivilege (Access::Privilege w)
107
138
{
108
- writePrivilege = chip::to_underlying (w);
139
+ // Static ASSERT to check size of writePrivilege type vs entry parameter.
140
+ static_assert (sizeof (std::underlying_type_t <Access::Privilege>) >=
141
+ sizeof (chip::to_underlying (w)),
142
+ " Size of writePrivilege is not able to accomodate parameter (w)." );
143
+
144
+ // ASSERT to validate entry parameter value.
145
+ assert (kNoPrivilege != chip::to_underlying (w));
146
+
147
+ mask.writePrivilege = chip::to_underlying (w);
109
148
}
110
149
111
150
Access::Privilege GetWritePrivilege () const
112
151
{
113
- return static_cast < Access::Privilege >(writePrivilege);
152
+ return static_cast < Access::Privilege >(mask. writePrivilege );
114
153
}
115
154
116
- bool readPrivilegeHasValue () {return readPrivilege;}
117
- bool writePrivilegeHasValue () {return writePrivilege;}
155
+
156
+ AttributeQualityFlags SetFlags (AttributeQualityFlags f)
157
+ {
158
+ return static_cast < AttributeQualityFlags >( mask.flags |= chip::to_underlying (f) ) ;
159
+ }
160
+
161
+
162
+ AttributeQualityFlags SetFlags (AttributeQualityFlags f, bool isSet)
163
+ {
164
+ return isSet ?
165
+ SetFlags (f) :
166
+ static_cast < AttributeQualityFlags >( mask.flags &= ~chip::to_underlying (f) );
167
+ }
168
+
169
+
170
+ constexpr bool FlagsHas (AttributeQualityFlags f) const { return (mask.flags & chip::to_underlying (f)) != 0 ; }
171
+
172
+ bool readPrivilegeHasValue () {return mask.readPrivilege ;}
173
+ bool writePrivilegeHasValue () {return mask.writePrivilege ;}
118
174
119
175
120
176
private:
121
177
122
- // read/write access will be missing if read/write is NOT allowed
123
- //
124
- // NOTE: this should be compacted for size
125
- std::underlying_type_t <Access::Privilege> readPrivilege : 5 ; // generally defaults to View if readable
126
- std::underlying_type_t <Access::Privilege> writePrivilege : 5 ; // generally defaults to Operate if writable
178
+ attribute_entry_mask_t mask;
179
+
127
180
};
128
181
182
+
183
+
129
184
// Bitmask values for different Command qualities.
130
185
enum class CommandQualityFlags : uint32_t
131
186
{
@@ -134,33 +189,77 @@ enum class CommandQualityFlags : uint32_t
134
189
kLargeMessage = 0x0004 , // `L` quality on commands
135
190
};
136
191
192
+
193
+
194
+ struct accepted_command_entry_mask_t {
195
+
196
+ // command quality flags
197
+ //
198
+ std::underlying_type_t <CommandQualityFlags> flags : 3 ; // generally defaults to View if readable
199
+
200
+ // invoke privilege variable
201
+ //
202
+ std::underlying_type_t <Access::Privilege> invokePrivilege : 5 ;
203
+
204
+ };
205
+
206
+
207
+ // Static ASSERT to check size of mask type.
208
+ static_assert (sizeof (accepted_command_entry_mask_t ) <= 4 , " Size of accepted_command_entry_mask_t is not as expected." );
209
+
210
+
211
+
137
212
struct AcceptedCommandEntry
138
213
{
139
214
CommandId commandId;
140
215
141
- // TODO: this can be more compact (use some flags for privilege)
142
- // to make this compact, add a compact enum and make flags/invokePrivilege getters (to still be type safe)
143
- BitFlags<CommandQualityFlags> flags;
144
-
145
216
146
217
// Constructor
147
- AcceptedCommandEntry () :
148
- invokePrivilege ( chip::to_underlying(Access::Privilege::kOperate )) {}
218
+ constexpr AcceptedCommandEntry () : commandId( 0 ),
219
+ mask{ 0 , chip::to_underlying (Access::Privilege::kOperate ) } {}
149
220
150
-
221
+
222
+ void SetInvokePrivilege (Access::Privilege i)
223
+ {
224
+ // Static ASSERT to check size of invokePrivilege type vs entry parameter.
225
+ static_assert (sizeof (std::underlying_type_t <Access::Privilege>) >=
226
+ sizeof (chip::to_underlying (i)),
227
+ " Size of invokePrivilege is not able to accomodate parameter (i)." );
228
+
229
+ // ASSERT to validate entry parameter value.
230
+ assert (kNoPrivilege != chip::to_underlying (i));
231
+
232
+ mask.invokePrivilege = chip::to_underlying (i);
233
+ }
234
+
235
+
151
236
Access::Privilege GetInvokePrivilege () const
152
237
{
153
- return static_cast < Access::Privilege >(invokePrivilege);
238
+ return static_cast < Access::Privilege >(mask. invokePrivilege );
154
239
}
155
240
156
- void SetInvokePrivilege (Access::Privilege i)
241
+
242
+ CommandQualityFlags SetFlags (CommandQualityFlags f)
243
+ {
244
+ return static_cast < CommandQualityFlags >( mask.flags |= chip::to_underlying (f) ) ;
245
+ }
246
+
247
+
248
+ CommandQualityFlags SetFlags (CommandQualityFlags f, bool isSet)
157
249
{
158
- invokePrivilege = chip::to_underlying (i);
250
+ return isSet ?
251
+ SetFlags (f) :
252
+ static_cast < CommandQualityFlags >( mask.flags &= ~chip::to_underlying (f) );
159
253
}
254
+
255
+
256
+ constexpr bool FlagsHas (CommandQualityFlags f) const { return (mask.flags & chip::to_underlying (f)) != 0 ; }
160
257
161
258
162
259
private:
163
- std::underlying_type_t <Access::Privilege> invokePrivilege : 5 ;
260
+
261
+ accepted_command_entry_mask_t mask;
262
+
164
263
};
165
264
166
265
// / Represents a device type that resides on an endpoint
0 commit comments