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 : #pragma once
18 :
19 : #include <app/util/basic-types.h>
20 : #include <lib/core/GroupId.h>
21 : #include <lib/core/ReferenceCounted.h>
22 : #include <lib/support/Pool.h>
23 : #include <transport/Session.h>
24 :
25 : namespace chip {
26 : namespace Transport {
27 :
28 : class IncomingGroupSession : public Session, public ReferenceCounted<IncomingGroupSession, NoopDeletor<IncomingGroupSession>, 0>
29 : {
30 : public:
31 0 : IncomingGroupSession(GroupId group, FabricIndex fabricIndex, NodeId peerNodeId) : mGroupId(group), mPeerNodeId(peerNodeId)
32 : {
33 0 : SetFabricIndex(fabricIndex);
34 0 : }
35 0 : ~IncomingGroupSession() override
36 0 : {
37 0 : NotifySessionReleased();
38 0 : VerifyOrDie(GetReferenceCount() == 0);
39 0 : }
40 :
41 0 : void Retain() override { ReferenceCounted<IncomingGroupSession, NoopDeletor<IncomingGroupSession>, 0>::Retain(); }
42 0 : void Release() override { ReferenceCounted<IncomingGroupSession, NoopDeletor<IncomingGroupSession>, 0>::Release(); }
43 :
44 0 : bool IsActiveSession() const override { return true; }
45 :
46 0 : Session::SessionType GetSessionType() const override { return Session::SessionType::kGroupIncoming; }
47 :
48 0 : ScopedNodeId GetPeer() const override { return ScopedNodeId(mPeerNodeId, GetFabricIndex()); }
49 0 : ScopedNodeId GetLocalScopedNodeId() const override { return ScopedNodeId(kUndefinedNodeId, GetFabricIndex()); }
50 :
51 0 : Access::SubjectDescriptor GetSubjectDescriptor() const override
52 : {
53 0 : Access::SubjectDescriptor subjectDescriptor;
54 0 : subjectDescriptor.authMode = Access::AuthMode::kGroup;
55 0 : subjectDescriptor.subject = NodeIdFromGroupId(mGroupId);
56 0 : subjectDescriptor.fabricIndex = GetFabricIndex();
57 0 : return subjectDescriptor;
58 : }
59 :
60 0 : bool AllowsMRP() const override { return false; }
61 0 : bool AllowsLargePayload() const override { return false; }
62 :
63 0 : const SessionParameters & GetRemoteSessionParameters() const override
64 : {
65 0 : static const SessionParameters cfg(GetDefaultMRPConfig());
66 0 : VerifyOrDie(false);
67 : return cfg;
68 : }
69 :
70 0 : System::Clock::Timestamp GetMRPBaseTimeout() const override { return System::Clock::kZero; }
71 :
72 0 : System::Clock::Milliseconds32 GetAckTimeout(bool isFirstMessageOnExchange) const override
73 : {
74 0 : VerifyOrDie(false);
75 : return System::Clock::Timeout();
76 : }
77 :
78 0 : System::Clock::Milliseconds32 GetMessageReceiptTimeout(System::Clock::Timestamp ourLastActivity,
79 : bool isFirstMessageOnExchange) const override
80 : {
81 : // There are no timeouts for group sessions.
82 0 : VerifyOrDie(false);
83 : return System::Clock::Timeout();
84 : }
85 :
86 0 : GroupId GetGroupId() const { return mGroupId; }
87 :
88 : private:
89 : const GroupId mGroupId;
90 : const NodeId mPeerNodeId;
91 : };
92 :
93 : class OutgoingGroupSession : public Session, public ReferenceCounted<OutgoingGroupSession, NoopDeletor<OutgoingGroupSession>, 0>
94 : {
95 : public:
96 : OutgoingGroupSession(GroupId group, FabricIndex fabricIndex) : mGroupId(group) { SetFabricIndex(fabricIndex); }
97 : ~OutgoingGroupSession() override
98 : {
99 : NotifySessionReleased();
100 : VerifyOrDie(GetReferenceCount() == 0);
101 : }
102 :
103 : void Retain() override { ReferenceCounted<OutgoingGroupSession, NoopDeletor<OutgoingGroupSession>, 0>::Retain(); }
104 : void Release() override { ReferenceCounted<OutgoingGroupSession, NoopDeletor<OutgoingGroupSession>, 0>::Release(); }
105 :
106 : bool IsActiveSession() const override { return true; }
107 :
108 : Session::SessionType GetSessionType() const override { return Session::SessionType::kGroupOutgoing; }
109 :
110 : // Peer node ID is unused: users care about the group, not the node
111 : ScopedNodeId GetPeer() const override { return ScopedNodeId(); }
112 : // Local node ID is unused: users care about the group, not the node
113 : ScopedNodeId GetLocalScopedNodeId() const override { return ScopedNodeId(); }
114 :
115 : Access::SubjectDescriptor GetSubjectDescriptor() const override
116 : {
117 : return Access::SubjectDescriptor(); // no subject exists for outgoing group session.
118 : }
119 :
120 : bool AllowsMRP() const override { return false; }
121 : bool AllowsLargePayload() const override { return false; }
122 :
123 : const SessionParameters & GetRemoteSessionParameters() const override
124 : {
125 : static const SessionParameters cfg(GetDefaultMRPConfig());
126 : VerifyOrDie(false);
127 : return cfg;
128 : }
129 :
130 : System::Clock::Timestamp GetMRPBaseTimeout() const override { return System::Clock::kZero; }
131 :
132 : System::Clock::Milliseconds32 GetAckTimeout(bool isFirstMessageOnExchange) const override
133 : {
134 : VerifyOrDie(false);
135 : return System::Clock::Timeout();
136 : }
137 :
138 : System::Clock::Milliseconds32 GetMessageReceiptTimeout(System::Clock::Timestamp ourLastActivity,
139 : bool isFirstMessageOnExchange) const override
140 : {
141 : // There are no timeouts for group sessions.
142 : VerifyOrDie(false);
143 : return System::Clock::Timeout();
144 : }
145 :
146 6 : GroupId GetGroupId() const { return mGroupId; }
147 :
148 : private:
149 : const GroupId mGroupId;
150 : };
151 :
152 : } // namespace Transport
153 : } // namespace chip
|