Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020 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 API declarations for a binding table.
20 : */
21 :
22 : #pragma once
23 :
24 : #include <app/util/af-types.h>
25 : #include <app/util/config.h>
26 : #include <lib/core/CHIPPersistentStorageDelegate.h>
27 : #include <lib/core/TLV.h>
28 : #include <lib/support/DefaultStorageKeyAllocator.h>
29 :
30 : namespace chip {
31 :
32 : class BindingTable
33 : {
34 : friend class Iterator;
35 :
36 : public:
37 : BindingTable();
38 :
39 : class Iterator
40 : {
41 : friend class BindingTable;
42 :
43 : public:
44 34 : EmberBindingTableEntry & operator*() { return mTable->mBindingTable[mIndex]; }
45 :
46 : const EmberBindingTableEntry & operator*() const { return mTable->mBindingTable[mIndex]; }
47 :
48 1883 : EmberBindingTableEntry * operator->() { return &(mTable->mBindingTable[mIndex]); }
49 :
50 : const EmberBindingTableEntry * operator->() const { return &(mTable->mBindingTable[mIndex]); }
51 :
52 : Iterator operator++();
53 :
54 17 : bool operator==(const Iterator & rhs) const { return mIndex == rhs.mIndex; }
55 :
56 586 : bool operator!=(const Iterator & rhs) const { return mIndex != rhs.mIndex; }
57 :
58 143 : uint8_t GetIndex() const { return mIndex; }
59 :
60 : private:
61 : BindingTable * mTable;
62 : uint8_t mPrevIndex;
63 : uint8_t mIndex;
64 : };
65 :
66 : CHIP_ERROR Add(const EmberBindingTableEntry & entry);
67 :
68 : const EmberBindingTableEntry & GetAt(uint8_t index);
69 :
70 : // The iter will be moved to the next item in the table after calling RemoveAt.
71 : CHIP_ERROR RemoveAt(Iterator & iter);
72 :
73 : // Returns the number of active entries in the binding table.
74 : // *NOTE* The function does not return the capacity of the binding table.
75 16 : uint8_t Size() const { return mSize; }
76 :
77 : Iterator begin();
78 :
79 : Iterator end();
80 :
81 11 : void SetPersistentStorage(PersistentStorageDelegate * storage) { mStorage = storage; }
82 :
83 : CHIP_ERROR LoadFromStorage();
84 :
85 668 : static BindingTable & GetInstance() { return sInstance; }
86 :
87 : private:
88 : static BindingTable sInstance;
89 :
90 : static constexpr uint32_t kStorageVersion = 1;
91 : static constexpr uint8_t kEntryStorageSize = TLV::EstimateStructOverhead(
92 : sizeof(FabricIndex), sizeof(EndpointId), sizeof(ClusterId), sizeof(EndpointId), sizeof(NodeId), sizeof(uint8_t));
93 : static constexpr uint8_t kListInfoStorageSize = TLV::EstimateStructOverhead(sizeof(kStorageVersion), sizeof(uint8_t));
94 :
95 : static constexpr uint8_t kTagStorageVersion = 1;
96 : static constexpr uint8_t kTagHead = 2;
97 : static constexpr uint8_t kTagFabricIndex = 1;
98 : static constexpr uint8_t kTagLocalEndpoint = 2;
99 : static constexpr uint8_t kTagCluster = 3;
100 : static constexpr uint8_t kTagRemoteEndpoint = 4;
101 : static constexpr uint8_t kTagNodeId = 5;
102 : static constexpr uint8_t kTagGroupId = 6;
103 : static constexpr uint8_t kTagNextEntry = 7;
104 : static constexpr uint8_t kNextNullIndex = 255;
105 :
106 : uint8_t GetNextAvaiableIndex();
107 :
108 : CHIP_ERROR SaveEntryToStorage(uint8_t index, uint8_t nextIndex);
109 : CHIP_ERROR SaveListInfo(uint8_t head);
110 :
111 : CHIP_ERROR LoadEntryFromStorage(uint8_t index, uint8_t & nextIndex);
112 :
113 : EmberBindingTableEntry mBindingTable[MATTER_BINDING_TABLE_SIZE];
114 : uint8_t mNextIndex[MATTER_BINDING_TABLE_SIZE];
115 :
116 : uint8_t mHead = kNextNullIndex;
117 : uint8_t mTail = kNextNullIndex;
118 : uint8_t mSize = 0;
119 :
120 : PersistentStorageDelegate * mStorage;
121 : };
122 :
123 : } // namespace chip
|