Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 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 : /**
19 : * @file
20 : * General faults could be detected by the Node.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <lib/core/CHIPError.h>
26 : #include <lib/support/CodeUtils.h>
27 :
28 : namespace chip {
29 : namespace DeviceLayer {
30 :
31 : static constexpr size_t kMaxHardwareFaults = 11;
32 : static constexpr size_t kMaxRadioFaults = 7;
33 : static constexpr size_t kMaxNetworkFaults = 4;
34 :
35 : template <size_t N>
36 : class GeneralFaults
37 : {
38 : public:
39 : /* The iterator */
40 : class Iterator
41 : {
42 : public:
43 : Iterator(const GeneralFaults<N> * GeneralFaults, int index);
44 : uint8_t operator*() const;
45 : Iterator & operator++();
46 : bool operator!=(const Iterator & other) const;
47 :
48 : private:
49 : const GeneralFaults<N> * mGeneralFaultsPtr;
50 : int mIndex = -1;
51 : };
52 :
53 7 : GeneralFaults() = default;
54 7 : ~GeneralFaults() { mSize = 0; }
55 :
56 : CHIP_ERROR add(const uint8_t value);
57 : CHIP_ERROR remove(const uint8_t value);
58 :
59 5 : const uint8_t * data() const { return mData; }
60 : size_t size() const;
61 : uint8_t operator[](int index) const;
62 :
63 : Iterator begin() const;
64 : Iterator end() const;
65 :
66 : private:
67 : uint8_t mData[N];
68 : int mSize = 0;
69 : };
70 :
71 : /*
72 : * GeneralFaults methods
73 : **/
74 : template <size_t N>
75 8 : inline CHIP_ERROR GeneralFaults<N>::add(const uint8_t value)
76 : {
77 8 : if (mSize == N)
78 : {
79 1 : return CHIP_ERROR_NO_MEMORY;
80 : }
81 :
82 : // add the new element
83 7 : mData[mSize] = value;
84 7 : ++mSize;
85 7 : return CHIP_NO_ERROR;
86 : }
87 :
88 : template <size_t N>
89 4 : inline CHIP_ERROR GeneralFaults<N>::remove(const uint8_t value)
90 : {
91 4 : int originalSize = mSize;
92 4 : mSize = 0;
93 15 : for (int i = 0; i < originalSize; ++i)
94 : {
95 11 : if (mData[i] != value)
96 : {
97 7 : mData[mSize++] = mData[i];
98 : }
99 : }
100 :
101 4 : return (mSize != originalSize) ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND;
102 : }
103 :
104 : template <size_t N>
105 13 : inline size_t GeneralFaults<N>::size() const
106 : {
107 13 : return static_cast<size_t>(mSize);
108 : }
109 :
110 : template <size_t N>
111 9 : inline uint8_t GeneralFaults<N>::operator[](int index) const
112 : {
113 9 : VerifyOrDie(index < mSize);
114 9 : return mData[index];
115 : }
116 :
117 : template <size_t N>
118 1 : inline typename GeneralFaults<N>::Iterator GeneralFaults<N>::begin() const
119 : {
120 1 : return GeneralFaults<N>::Iterator{ this, 0 };
121 : }
122 :
123 : template <size_t N>
124 3 : inline typename GeneralFaults<N>::Iterator GeneralFaults<N>::end() const
125 : {
126 3 : return GeneralFaults<N>::Iterator{ this, mSize };
127 : }
128 :
129 : /*
130 : * Iterator methods
131 : **/
132 : template <size_t N>
133 4 : inline GeneralFaults<N>::Iterator::Iterator(const GeneralFaults<N> * pGeneralFaults, int index) :
134 4 : mGeneralFaultsPtr(pGeneralFaults), mIndex(index)
135 4 : {}
136 :
137 : template <size_t N>
138 2 : inline uint8_t GeneralFaults<N>::Iterator::operator*() const
139 : {
140 2 : return mGeneralFaultsPtr->operator[](mIndex);
141 : }
142 :
143 : template <size_t N>
144 2 : inline typename GeneralFaults<N>::Iterator & GeneralFaults<N>::Iterator::operator++()
145 : {
146 2 : ++mIndex;
147 2 : return *this;
148 : }
149 :
150 : template <size_t N>
151 3 : inline bool GeneralFaults<N>::Iterator::operator!=(const GeneralFaults<N>::Iterator & other) const
152 : {
153 3 : return mIndex != other.mIndex;
154 : }
155 :
156 : } // namespace DeviceLayer
157 : } // namespace chip
|