1 /*
2 * Copyright (c) 2016-2017, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "changed_props_set.hpp"
29
30 #include <limits.h>
31
32 #include "common/code_utils.hpp"
33
34 namespace ot {
35 namespace Ncp {
36
37 // ----------------------------------------------------------------------------
38 // MARK: ChangedPropsSet class
39 // ----------------------------------------------------------------------------
40
41 // Defines the list of properties that can support unsolicited update.
42 //
43 // Note that {`SPINEL_PROP_LAST_STATUS`, `SPINEL_STATUS_RESET_UNKNOWN`} should be first entry to ensure that RESET is
44 // reported before any other property update.
45 //
46 // Since a `uint64_t` is used as bit-mask to track which entries are in the changed set, we should ensure that the
47 // number of entries in the list is always less than or equal to 64.
48 //
49 const ChangedPropsSet::Entry ChangedPropsSet::mSupportedProps[] = {
50 // Spinel property , Status (if prop is `LAST_STATUS`), IsFilterable?
51
52 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_RESET_UNKNOWN, false},
53 {SPINEL_PROP_STREAM_DEBUG, SPINEL_STATUS_OK, true},
54 {SPINEL_PROP_IPV6_LL_ADDR, SPINEL_STATUS_OK, true},
55 {SPINEL_PROP_IPV6_ML_ADDR, SPINEL_STATUS_OK, true},
56 {SPINEL_PROP_IPV6_ADDRESS_TABLE, SPINEL_STATUS_OK, true},
57 {SPINEL_PROP_NET_ROLE, SPINEL_STATUS_OK, true},
58 {SPINEL_PROP_NET_PARTITION_ID, SPINEL_STATUS_OK, true},
59 {SPINEL_PROP_NET_KEY_SEQUENCE_COUNTER, SPINEL_STATUS_OK, true},
60 {SPINEL_PROP_THREAD_LEADER_NETWORK_DATA, SPINEL_STATUS_OK, true},
61 {SPINEL_PROP_THREAD_CHILD_TABLE, SPINEL_STATUS_OK, true},
62 {SPINEL_PROP_THREAD_ON_MESH_NETS, SPINEL_STATUS_OK, true},
63 {SPINEL_PROP_THREAD_OFF_MESH_ROUTES, SPINEL_STATUS_OK, true},
64 {SPINEL_PROP_NET_STACK_UP, SPINEL_STATUS_OK, true},
65 {SPINEL_PROP_NET_REQUIRE_JOIN_EXISTING, SPINEL_STATUS_OK, true},
66 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_NOMEM, true},
67 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_DROPPED, true},
68 #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE
69 {SPINEL_PROP_JAM_DETECTED, SPINEL_STATUS_OK, true},
70 #endif
71 #if OPENTHREAD_CONFIG_LEGACY_ENABLE
72 {SPINEL_PROP_NEST_LEGACY_ULA_PREFIX, SPINEL_STATUS_OK, true},
73 {SPINEL_PROP_NEST_LEGACY_LAST_NODE_JOINED, SPINEL_STATUS_OK, true},
74 #endif
75 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_JOIN_FAILURE, false},
76 {SPINEL_PROP_MAC_SCAN_STATE, SPINEL_STATUS_OK, false},
77 {SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE, SPINEL_STATUS_OK, true},
78 {SPINEL_PROP_PHY_CHAN, SPINEL_STATUS_OK, true},
79 {SPINEL_PROP_MAC_15_4_PANID, SPINEL_STATUS_OK, true},
80 {SPINEL_PROP_NET_NETWORK_NAME, SPINEL_STATUS_OK, true},
81 {SPINEL_PROP_NET_XPANID, SPINEL_STATUS_OK, true},
82 {SPINEL_PROP_NET_NETWORK_KEY, SPINEL_STATUS_OK, true},
83 {SPINEL_PROP_NET_PSKC, SPINEL_STATUS_OK, true},
84 {SPINEL_PROP_PHY_CHAN_SUPPORTED, SPINEL_STATUS_OK, true},
85 #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE
86 {SPINEL_PROP_CHANNEL_MANAGER_NEW_CHANNEL, SPINEL_STATUS_OK, true},
87 #endif
88 #if OPENTHREAD_CONFIG_JOINER_ENABLE
89 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_JOIN_NO_PEERS, false},
90 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_JOIN_SECURITY, false},
91 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_JOIN_RSP_TIMEOUT, false},
92 {SPINEL_PROP_LAST_STATUS, SPINEL_STATUS_JOIN_SUCCESS, false},
93 #endif
94 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
95 {SPINEL_PROP_THREAD_NETWORK_TIME, SPINEL_STATUS_OK, false},
96 #endif
97 {SPINEL_PROP_PARENT_RESPONSE_INFO, SPINEL_STATUS_OK, true},
98 };
99
GetNumEntries(void) const100 uint8_t ChangedPropsSet::GetNumEntries(void) const
101 {
102 static_assert(OT_ARRAY_LENGTH(mSupportedProps) <= sizeof(mChangedSet) * CHAR_BIT,
103 "Changed set size is smaller than number of entries in `mSupportedProps[]` array");
104
105 return OT_ARRAY_LENGTH(mSupportedProps);
106 }
107
Add(spinel_prop_key_t aPropKey,spinel_status_t aStatus)108 void ChangedPropsSet::Add(spinel_prop_key_t aPropKey, spinel_status_t aStatus)
109 {
110 uint8_t numEntries;
111 const Entry *entry;
112
113 entry = GetSupportedEntries(numEntries);
114
115 for (uint8_t index = 0; index < numEntries; index++, entry++)
116 {
117 if ((entry->mPropKey == aPropKey) && (entry->mStatus == aStatus))
118 {
119 if (!IsEntryFiltered(index))
120 {
121 SetBit(mChangedSet, index);
122 }
123
124 break;
125 }
126 }
127 }
128
EnablePropertyFilter(spinel_prop_key_t aPropKey,bool aEnable)129 otError ChangedPropsSet::EnablePropertyFilter(spinel_prop_key_t aPropKey, bool aEnable)
130 {
131 uint8_t numEntries;
132 const Entry *entry;
133 bool didFind = false;
134
135 entry = GetSupportedEntries(numEntries);
136
137 for (uint8_t index = 0; index < numEntries; index++, entry++)
138 {
139 if (entry->mFilterable && (entry->mPropKey == aPropKey))
140 {
141 if (aEnable)
142 {
143 SetBit(mFilterSet, index);
144
145 // If filter is enabled for a property, the `mChangedSet` is cleared
146 // for the same property so to ensure a pending update is also filtered.
147
148 ClearBit(mChangedSet, index);
149 }
150 else
151 {
152 ClearBit(mFilterSet, index);
153 }
154
155 didFind = true;
156
157 // Continue the search only if the prop key is `LAST_STATUS`, as
158 // we have multiple filterable `LAST_STATUS` entries in the table
159 // with different error status (DROPPED and NOMEM).
160
161 if (aPropKey != SPINEL_PROP_LAST_STATUS)
162 {
163 break;
164 }
165 }
166 }
167
168 return didFind ? OT_ERROR_NONE : OT_ERROR_INVALID_ARGS;
169 }
170
IsPropertyFiltered(spinel_prop_key_t aPropKey) const171 bool ChangedPropsSet::IsPropertyFiltered(spinel_prop_key_t aPropKey) const
172 {
173 bool isFiltered = false;
174 uint8_t numEntries;
175 const Entry *entry;
176
177 entry = GetSupportedEntries(numEntries);
178
179 for (uint8_t index = 0; index < numEntries; index++, entry++)
180 {
181 if (entry->mFilterable && (entry->mPropKey == aPropKey))
182 {
183 isFiltered = IsEntryFiltered(index);
184
185 break;
186 }
187 }
188
189 return isFiltered;
190 }
191
192 } // namespace Ncp
193 } // namespace ot
194