xref: /aosp_15_r20/external/openthread/src/ncp/changed_props_set.hpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *    Copyright (c) 2016-2017, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *    All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *    Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *    modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *    1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *    2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *       documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *    3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *       names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *       derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17*cfb92d14SAndroid Build Coastguard Worker  *    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*cfb92d14SAndroid Build Coastguard Worker  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*cfb92d14SAndroid Build Coastguard Worker  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20*cfb92d14SAndroid Build Coastguard Worker  *    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*cfb92d14SAndroid Build Coastguard Worker  *    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*cfb92d14SAndroid Build Coastguard Worker  *    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23*cfb92d14SAndroid Build Coastguard Worker  *    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*cfb92d14SAndroid Build Coastguard Worker  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25*cfb92d14SAndroid Build Coastguard Worker  *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*cfb92d14SAndroid Build Coastguard Worker  */
27*cfb92d14SAndroid Build Coastguard Worker 
28*cfb92d14SAndroid Build Coastguard Worker /**
29*cfb92d14SAndroid Build Coastguard Worker  * @file
30*cfb92d14SAndroid Build Coastguard Worker  *   This file contains definitions a spinel interface to the OpenThread stack.
31*cfb92d14SAndroid Build Coastguard Worker  */
32*cfb92d14SAndroid Build Coastguard Worker 
33*cfb92d14SAndroid Build Coastguard Worker #ifndef CHANGED_PROPS_SET_HPP_
34*cfb92d14SAndroid Build Coastguard Worker #define CHANGED_PROPS_SET_HPP_
35*cfb92d14SAndroid Build Coastguard Worker 
36*cfb92d14SAndroid Build Coastguard Worker #include "openthread-core-config.h"
37*cfb92d14SAndroid Build Coastguard Worker 
38*cfb92d14SAndroid Build Coastguard Worker #include <stddef.h>
39*cfb92d14SAndroid Build Coastguard Worker 
40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/error.h>
41*cfb92d14SAndroid Build Coastguard Worker 
42*cfb92d14SAndroid Build Coastguard Worker #include "lib/spinel/spinel.h"
43*cfb92d14SAndroid Build Coastguard Worker 
44*cfb92d14SAndroid Build Coastguard Worker namespace ot {
45*cfb92d14SAndroid Build Coastguard Worker namespace Ncp {
46*cfb92d14SAndroid Build Coastguard Worker 
47*cfb92d14SAndroid Build Coastguard Worker /**
48*cfb92d14SAndroid Build Coastguard Worker  * Defines a class to track a set of property/status changes that require update to host. The properties that can
49*cfb92d14SAndroid Build Coastguard Worker  * be added to this set must support sending unsolicited updates. This class also provides mechanism for user
50*cfb92d14SAndroid Build Coastguard Worker  * to block certain filterable properties disallowing the unsolicited update from them.
51*cfb92d14SAndroid Build Coastguard Worker  *
52*cfb92d14SAndroid Build Coastguard Worker  */
53*cfb92d14SAndroid Build Coastguard Worker class ChangedPropsSet
54*cfb92d14SAndroid Build Coastguard Worker {
55*cfb92d14SAndroid Build Coastguard Worker public:
56*cfb92d14SAndroid Build Coastguard Worker     /**
57*cfb92d14SAndroid Build Coastguard Worker      * Defines an entry in the set/list.
58*cfb92d14SAndroid Build Coastguard Worker      *
59*cfb92d14SAndroid Build Coastguard Worker      */
60*cfb92d14SAndroid Build Coastguard Worker     struct Entry
61*cfb92d14SAndroid Build Coastguard Worker     {
62*cfb92d14SAndroid Build Coastguard Worker         spinel_prop_key_t mPropKey;    ///< The spinel property key.
63*cfb92d14SAndroid Build Coastguard Worker         spinel_status_t   mStatus;     ///< The spinel status (used only if prop key is `LAST_STATUS`).
64*cfb92d14SAndroid Build Coastguard Worker         bool              mFilterable; ///< Indicates whether the entry can be filtered
65*cfb92d14SAndroid Build Coastguard Worker     };
66*cfb92d14SAndroid Build Coastguard Worker 
67*cfb92d14SAndroid Build Coastguard Worker     /**
68*cfb92d14SAndroid Build Coastguard Worker      * Initializes the set.
69*cfb92d14SAndroid Build Coastguard Worker      *
70*cfb92d14SAndroid Build Coastguard Worker      */
ChangedPropsSet(void)71*cfb92d14SAndroid Build Coastguard Worker     ChangedPropsSet(void)
72*cfb92d14SAndroid Build Coastguard Worker         : mChangedSet(0)
73*cfb92d14SAndroid Build Coastguard Worker         , mFilterSet(0)
74*cfb92d14SAndroid Build Coastguard Worker     {
75*cfb92d14SAndroid Build Coastguard Worker     }
76*cfb92d14SAndroid Build Coastguard Worker 
77*cfb92d14SAndroid Build Coastguard Worker     /**
78*cfb92d14SAndroid Build Coastguard Worker      * Clears the set.
79*cfb92d14SAndroid Build Coastguard Worker      *
80*cfb92d14SAndroid Build Coastguard Worker      */
Clear(void)81*cfb92d14SAndroid Build Coastguard Worker     void Clear(void) { mChangedSet = 0; }
82*cfb92d14SAndroid Build Coastguard Worker 
83*cfb92d14SAndroid Build Coastguard Worker     /**
84*cfb92d14SAndroid Build Coastguard Worker      * Indicates if the set is empty or not.
85*cfb92d14SAndroid Build Coastguard Worker      *
86*cfb92d14SAndroid Build Coastguard Worker      * @returns TRUE if the set if empty, FALSE otherwise.
87*cfb92d14SAndroid Build Coastguard Worker      *
88*cfb92d14SAndroid Build Coastguard Worker      */
IsEmpty(void) const89*cfb92d14SAndroid Build Coastguard Worker     bool IsEmpty(void) const { return (mChangedSet == 0); }
90*cfb92d14SAndroid Build Coastguard Worker 
91*cfb92d14SAndroid Build Coastguard Worker     /**
92*cfb92d14SAndroid Build Coastguard Worker      * Adds a property to the set. The property added must be in the list of supported properties
93*cfb92d14SAndroid Build Coastguard Worker      * capable of sending unsolicited update, otherwise the input is ignored.
94*cfb92d14SAndroid Build Coastguard Worker      *
95*cfb92d14SAndroid Build Coastguard Worker      * Note that if the property is already in the set, adding it again does not change the set.
96*cfb92d14SAndroid Build Coastguard Worker      *
97*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aPropKey    The spinel property key to be added to the set
98*cfb92d14SAndroid Build Coastguard Worker      *
99*cfb92d14SAndroid Build Coastguard Worker      */
AddProperty(spinel_prop_key_t aPropKey)100*cfb92d14SAndroid Build Coastguard Worker     void AddProperty(spinel_prop_key_t aPropKey) { Add(aPropKey, SPINEL_STATUS_OK); }
101*cfb92d14SAndroid Build Coastguard Worker 
102*cfb92d14SAndroid Build Coastguard Worker     /**
103*cfb92d14SAndroid Build Coastguard Worker      * Adds a `LAST_STATUS` update to the set. The update must be in list of supported entries.
104*cfb92d14SAndroid Build Coastguard Worker      *
105*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aStatus     The spinel status update to be added to set.
106*cfb92d14SAndroid Build Coastguard Worker      *
107*cfb92d14SAndroid Build Coastguard Worker      */
AddLastStatus(spinel_status_t aStatus)108*cfb92d14SAndroid Build Coastguard Worker     void AddLastStatus(spinel_status_t aStatus) { Add(SPINEL_PROP_LAST_STATUS, aStatus); }
109*cfb92d14SAndroid Build Coastguard Worker 
110*cfb92d14SAndroid Build Coastguard Worker     /**
111*cfb92d14SAndroid Build Coastguard Worker      * Returns a pointer to array of entries of supported property/status updates. The list includes
112*cfb92d14SAndroid Build Coastguard Worker      * all properties that can generate unsolicited update.
113*cfb92d14SAndroid Build Coastguard Worker      *
114*cfb92d14SAndroid Build Coastguard Worker      * @param[out]  aNumEntries  A reference to output the number of entries in the list.
115*cfb92d14SAndroid Build Coastguard Worker      *
116*cfb92d14SAndroid Build Coastguard Worker      * @returns A pointer to the supported entries array.
117*cfb92d14SAndroid Build Coastguard Worker      *
118*cfb92d14SAndroid Build Coastguard Worker      */
GetSupportedEntries(uint8_t & aNumEntries) const119*cfb92d14SAndroid Build Coastguard Worker     const Entry *GetSupportedEntries(uint8_t &aNumEntries) const
120*cfb92d14SAndroid Build Coastguard Worker     {
121*cfb92d14SAndroid Build Coastguard Worker         aNumEntries = GetNumEntries();
122*cfb92d14SAndroid Build Coastguard Worker         return &mSupportedProps[0];
123*cfb92d14SAndroid Build Coastguard Worker     }
124*cfb92d14SAndroid Build Coastguard Worker 
125*cfb92d14SAndroid Build Coastguard Worker     /**
126*cfb92d14SAndroid Build Coastguard Worker      * Returns a pointer to the entry associated with a given index.
127*cfb92d14SAndroid Build Coastguard Worker      *
128*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aIndex     The index to an entry.
129*cfb92d14SAndroid Build Coastguard Worker      *
130*cfb92d14SAndroid Build Coastguard Worker      * @returns A pointer to the entry associated with @p aIndex, or nullptr if the index is beyond end of array.
131*cfb92d14SAndroid Build Coastguard Worker      *
132*cfb92d14SAndroid Build Coastguard Worker      */
GetEntry(uint8_t aIndex) const133*cfb92d14SAndroid Build Coastguard Worker     const Entry *GetEntry(uint8_t aIndex) const
134*cfb92d14SAndroid Build Coastguard Worker     {
135*cfb92d14SAndroid Build Coastguard Worker         return (aIndex < GetNumEntries()) ? &mSupportedProps[aIndex] : nullptr;
136*cfb92d14SAndroid Build Coastguard Worker     }
137*cfb92d14SAndroid Build Coastguard Worker 
138*cfb92d14SAndroid Build Coastguard Worker     /**
139*cfb92d14SAndroid Build Coastguard Worker      * Indicates if the entry associated with an index is in the set (i.e., it has been changed and
140*cfb92d14SAndroid Build Coastguard Worker      * requires an unsolicited update).
141*cfb92d14SAndroid Build Coastguard Worker      *
142*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aIndex     The index to an entry.
143*cfb92d14SAndroid Build Coastguard Worker      *
144*cfb92d14SAndroid Build Coastguard Worker      * @returns TRUE if the entry is in the set, FALSE otherwise.
145*cfb92d14SAndroid Build Coastguard Worker      *
146*cfb92d14SAndroid Build Coastguard Worker      */
IsEntryChanged(uint8_t aIndex) const147*cfb92d14SAndroid Build Coastguard Worker     bool IsEntryChanged(uint8_t aIndex) const { return IsBitSet(mChangedSet, aIndex); }
148*cfb92d14SAndroid Build Coastguard Worker 
149*cfb92d14SAndroid Build Coastguard Worker     /**
150*cfb92d14SAndroid Build Coastguard Worker      * Removes an entry associated with an index in the set.
151*cfb92d14SAndroid Build Coastguard Worker      *
152*cfb92d14SAndroid Build Coastguard Worker      * Note that if the property/entry is not in the set, removing it simply does nothing.
153*cfb92d14SAndroid Build Coastguard Worker      *
154*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aIndex               Index of entry to be removed.
155*cfb92d14SAndroid Build Coastguard Worker      *
156*cfb92d14SAndroid Build Coastguard Worker      */
RemoveEntry(uint8_t aIndex)157*cfb92d14SAndroid Build Coastguard Worker     void RemoveEntry(uint8_t aIndex) { ClearBit(mChangedSet, aIndex); }
158*cfb92d14SAndroid Build Coastguard Worker 
159*cfb92d14SAndroid Build Coastguard Worker     /**
160*cfb92d14SAndroid Build Coastguard Worker      * Enables/disables filtering of a given property.
161*cfb92d14SAndroid Build Coastguard Worker      *
162*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aPropKey             The property key to filter.
163*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aEnable              TRUE to enable filtering, FALSE to disable.
164*cfb92d14SAndroid Build Coastguard Worker      *
165*cfb92d14SAndroid Build Coastguard Worker      * @retval OT_ERROR_NONE           Filter state for given property updated successfully.
166*cfb92d14SAndroid Build Coastguard Worker      * @retval OT_ERROR_INVALID_ARGS   The given property is not valid (i.e., not capable of unsolicited update).
167*cfb92d14SAndroid Build Coastguard Worker      *
168*cfb92d14SAndroid Build Coastguard Worker      */
169*cfb92d14SAndroid Build Coastguard Worker     otError EnablePropertyFilter(spinel_prop_key_t aPropKey, bool aEnable);
170*cfb92d14SAndroid Build Coastguard Worker 
171*cfb92d14SAndroid Build Coastguard Worker     /**
172*cfb92d14SAndroid Build Coastguard Worker      * Determines whether filtering is enabled for an entry associated with an index.
173*cfb92d14SAndroid Build Coastguard Worker      *
174*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aIndex               Index of entry to be checked.
175*cfb92d14SAndroid Build Coastguard Worker      *
176*cfb92d14SAndroid Build Coastguard Worker      * @returns TRUE if the filter is enabled for the given entry, FALSE otherwise.
177*cfb92d14SAndroid Build Coastguard Worker      *
178*cfb92d14SAndroid Build Coastguard Worker      */
IsEntryFiltered(uint8_t aIndex) const179*cfb92d14SAndroid Build Coastguard Worker     bool IsEntryFiltered(uint8_t aIndex) const { return IsBitSet(mFilterSet, aIndex); }
180*cfb92d14SAndroid Build Coastguard Worker 
181*cfb92d14SAndroid Build Coastguard Worker     /**
182*cfb92d14SAndroid Build Coastguard Worker      * Determines whether filtering is enabled for a given property key.
183*cfb92d14SAndroid Build Coastguard Worker      *
184*cfb92d14SAndroid Build Coastguard Worker      * @param[in] aPropKey             The property key to check.
185*cfb92d14SAndroid Build Coastguard Worker      *
186*cfb92d14SAndroid Build Coastguard Worker      * @returns TRUE if the filter is enabled for the given property, FALSE if the property is not filtered or if
187*cfb92d14SAndroid Build Coastguard Worker      *          it is not filterable.
188*cfb92d14SAndroid Build Coastguard Worker      *
189*cfb92d14SAndroid Build Coastguard Worker      */
190*cfb92d14SAndroid Build Coastguard Worker     bool IsPropertyFiltered(spinel_prop_key_t aPropKey) const;
191*cfb92d14SAndroid Build Coastguard Worker 
192*cfb92d14SAndroid Build Coastguard Worker     /**
193*cfb92d14SAndroid Build Coastguard Worker      * Clears the filter.
194*cfb92d14SAndroid Build Coastguard Worker      *
195*cfb92d14SAndroid Build Coastguard Worker      */
ClearFilter(void)196*cfb92d14SAndroid Build Coastguard Worker     void ClearFilter(void) { mFilterSet = 0; }
197*cfb92d14SAndroid Build Coastguard Worker 
198*cfb92d14SAndroid Build Coastguard Worker private:
199*cfb92d14SAndroid Build Coastguard Worker     uint8_t GetNumEntries(void) const;
200*cfb92d14SAndroid Build Coastguard Worker     void    Add(spinel_prop_key_t aPropKey, spinel_status_t aStatus);
201*cfb92d14SAndroid Build Coastguard Worker 
SetBit(uint64_t & aBitset,uint8_t aBitIndex)202*cfb92d14SAndroid Build Coastguard Worker     static void SetBit(uint64_t &aBitset, uint8_t aBitIndex) { aBitset |= (1ULL << aBitIndex); }
ClearBit(uint64_t & aBitset,uint8_t aBitIndex)203*cfb92d14SAndroid Build Coastguard Worker     static void ClearBit(uint64_t &aBitset, uint8_t aBitIndex) { aBitset &= ~(1ULL << aBitIndex); }
IsBitSet(uint64_t aBitset,uint8_t aBitIndex)204*cfb92d14SAndroid Build Coastguard Worker     static bool IsBitSet(uint64_t aBitset, uint8_t aBitIndex) { return (aBitset & (1ULL << aBitIndex)) != 0; }
205*cfb92d14SAndroid Build Coastguard Worker 
206*cfb92d14SAndroid Build Coastguard Worker     static const Entry mSupportedProps[];
207*cfb92d14SAndroid Build Coastguard Worker 
208*cfb92d14SAndroid Build Coastguard Worker     uint64_t mChangedSet;
209*cfb92d14SAndroid Build Coastguard Worker     uint64_t mFilterSet;
210*cfb92d14SAndroid Build Coastguard Worker };
211*cfb92d14SAndroid Build Coastguard Worker 
212*cfb92d14SAndroid Build Coastguard Worker } // namespace Ncp
213*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
214*cfb92d14SAndroid Build Coastguard Worker 
215*cfb92d14SAndroid Build Coastguard Worker #endif // CHANGED_PROPS_SET_HPP
216