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 : GroupSender mDataGroupSenders[CHIP_CONFIG_MAX_GROUP_DATA_PEERS]; 53 : GroupSender mControlGroupSenders[CHIP_CONFIG_MAX_GROUP_CONTROL_PEERS]; 54 : }; 55 : 56 : class GroupPeerTable 57 : { 58 : public: 59 : CHIP_ERROR FindOrAddPeer(FabricIndex fabricIndex, NodeId nodeId, bool isControl, 60 : chip::Transport::PeerMessageCounter *& counter); 61 : 62 : // Used in case of MCSP failure 63 : CHIP_ERROR RemovePeer(FabricIndex fabricIndex, NodeId nodeId, bool isControl); 64 : 65 : CHIP_ERROR FabricRemoved(FabricIndex fabricIndex); 66 : 67 : // Protected for Unit Tests inheritance 68 : protected: 69 : bool RemoveSpecificPeer(GroupSender * list, NodeId nodeId, uint32_t size); 70 : void CompactPeers(GroupSender * list, uint32_t size); 71 : void RemoveAndCompactFabric(uint32_t tableIndex); 72 : 73 : GroupFabric mGroupFabrics[CHIP_CONFIG_MAX_FABRICS]; 74 : }; 75 : 76 : // Might want to rename this so that it is explicitly the sending side of counters 77 : class GroupOutgoingCounters 78 : { 79 : public: 80 : static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask 81 : 82 101 : GroupOutgoingCounters(){}; 83 : GroupOutgoingCounters(chip::PersistentStorageDelegate * storage_delegate); 84 : CHIP_ERROR Init(chip::PersistentStorageDelegate * storage_delegate); 85 : uint32_t GetCounter(bool isControl); 86 : CHIP_ERROR IncrementCounter(bool isControl); 87 : 88 : // Protected for Unit Tests inheritance 89 : protected: 90 : // TODO Initialize those to random value 91 : uint32_t mGroupDataCounter = 0; 92 : uint32_t mGroupControlCounter = 0; 93 : chip::PersistentStorageDelegate * mStorage = nullptr; 94 : }; 95 : 96 : } // namespace Transport 97 : } // namespace chip