Line data Source code
1 : /*
2 : * Copyright (c) 2021 Project CHIP Authors
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 :
17 : /**
18 : * @file
19 : * This file defines the Matter Group message counters of remote nodes for groups.
20 : *
21 : */
22 : #pragma once
23 :
24 : #include <array>
25 : #include <bitset>
26 :
27 : #include <lib/core/CHIPPersistentStorageDelegate.h>
28 : #include <lib/core/DataModelTypes.h>
29 : #include <lib/core/NodeId.h>
30 : #include <lib/core/PeerId.h>
31 : #include <lib/support/Span.h>
32 : #include <transport/PeerMessageCounter.h>
33 :
34 : #define GROUP_MSG_COUNTER_MIN_INCREMENT 1000
35 :
36 : namespace chip {
37 : namespace Transport {
38 :
39 : class GroupSender
40 : {
41 : public:
42 : NodeId mNodeId = kUndefinedNodeId;
43 : PeerMessageCounter msgCounter;
44 : };
45 :
46 : class GroupFabric
47 : {
48 : public:
49 : FabricIndex mFabricIndex = kUndefinedFabricIndex;
50 : uint8_t mControlPeerCount = 0;
51 : uint8_t mDataPeerCount = 0;
52 :
53 : // The calls to Find/Add/Remove GroupSender entries for the GroupFabric are made through the GroupPeerTable.
54 : // The group peer table implements the Find/Add/Remove logic by treating these arrays as LRU caches,
55 : // and handles the movemenet of GroupSender elements to maintain order accordingly. The most recently used
56 : // GroupSender entry will be at index 0 (and least recently used will be at the end of the list).
57 : GroupSender mDataGroupSenders[CHIP_CONFIG_MAX_GROUP_DATA_PEERS];
58 : GroupSender mControlGroupSenders[CHIP_CONFIG_MAX_GROUP_CONTROL_PEERS];
59 : };
60 :
61 : class GroupPeerTable
62 : {
63 : public:
64 : CHIP_ERROR FindOrAddPeer(FabricIndex fabricIndex, NodeId nodeId, bool isControl,
65 : chip::Transport::PeerMessageCounter *& counter);
66 :
67 : // Used in case of MCSP failure
68 : CHIP_ERROR RemovePeer(FabricIndex fabricIndex, NodeId nodeId, bool isControl);
69 :
70 : CHIP_ERROR FabricRemoved(FabricIndex fabricIndex);
71 :
72 : // Protected for Unit Tests inheritance
73 : protected:
74 : bool RemoveSpecificPeer(GroupSender * list, NodeId nodeId, uint32_t size);
75 : void CompactPeers(GroupSender * list, uint32_t size);
76 : void RemoveAndCompactFabric(uint32_t tableIndex);
77 :
78 : GroupFabric mGroupFabrics[CHIP_CONFIG_MAX_FABRICS];
79 : };
80 :
81 : // Might want to rename this so that it is explicitly the sending side of counters
82 : class GroupOutgoingCounters
83 : {
84 : public:
85 : static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask
86 :
87 693 : GroupOutgoingCounters(){};
88 : GroupOutgoingCounters(chip::PersistentStorageDelegate * storage_delegate);
89 : CHIP_ERROR Init(chip::PersistentStorageDelegate * storage_delegate);
90 : uint32_t GetCounter(bool isControl);
91 : CHIP_ERROR IncrementCounter(bool isControl);
92 :
93 : // Protected for Unit Tests inheritance
94 : protected:
95 : // TODO Initialize those to random value
96 : uint32_t mGroupDataCounter = 0;
97 : uint32_t mGroupControlCounter = 0;
98 : chip::PersistentStorageDelegate * mStorage = nullptr;
99 : };
100 :
101 : } // namespace Transport
102 : } // namespace chip
|