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 : #include <transport/GroupSession.h>
18 : #include <transport/SecureSession.h>
19 : #include <transport/Session.h>
20 : #include <transport/UnauthenticatedSessionTable.h>
21 :
22 : namespace chip {
23 : namespace Transport {
24 :
25 354115 : SecureSession * Session::AsSecureSession()
26 : {
27 354115 : VerifyOrDie(GetSessionType() == SessionType::kSecure);
28 354115 : return static_cast<SecureSession *>(this);
29 : }
30 :
31 28504 : const SecureSession * Session::AsConstSecureSession() const
32 : {
33 28504 : VerifyOrDie(GetSessionType() == SessionType::kSecure);
34 28504 : return static_cast<const SecureSession *>(this);
35 : }
36 :
37 559 : UnauthenticatedSession * Session::AsUnauthenticatedSession()
38 : {
39 559 : VerifyOrDie(GetSessionType() == SessionType::kUnauthenticated);
40 559 : return static_cast<UnauthenticatedSession *>(this);
41 : }
42 :
43 0 : IncomingGroupSession * Session::AsIncomingGroupSession()
44 : {
45 0 : VerifyOrDie(GetSessionType() == SessionType::kGroupIncoming);
46 0 : return static_cast<IncomingGroupSession *>(this);
47 : }
48 :
49 0 : const IncomingGroupSession * Session::AsConstIncomingGroupSession() const
50 : {
51 0 : VerifyOrDie(GetSessionType() == SessionType::kGroupIncoming);
52 0 : return static_cast<const IncomingGroupSession *>(this);
53 : }
54 :
55 2 : OutgoingGroupSession * Session::AsOutgoingGroupSession()
56 : {
57 2 : VerifyOrDie(GetSessionType() == SessionType::kGroupOutgoing);
58 2 : return static_cast<OutgoingGroupSession *>(this);
59 : }
60 :
61 1 : const OutgoingGroupSession * Session::AsConstOutgoingGroupSession() const
62 : {
63 1 : VerifyOrDie(GetSessionType() == SessionType::kGroupOutgoing);
64 1 : return static_cast<const OutgoingGroupSession *>(this);
65 : }
66 :
67 6731 : System::Clock::Timeout Session::ComputeRoundTripTimeout(System::Clock::Timeout upperlayerProcessingTimeout,
68 : bool isFirstMessageOnExchange)
69 : {
70 6731 : if (IsGroupSession())
71 : {
72 1 : return System::Clock::kZero;
73 : }
74 :
75 : // Treat us as active for purposes of GetMessageReceiptTimeout(), pass false into GetMessageReceiptTimeout to
76 : // indicate we are processing non-initial message since the other side would be responding to our message.
77 6730 : return GetAckTimeout(isFirstMessageOnExchange) + upperlayerProcessingTimeout +
78 13460 : GetMessageReceiptTimeout(System::SystemClock().GetMonotonicTimestamp(), false /*isFirstMessageOnExchange*/);
79 : }
80 :
81 28785 : uint16_t Session::SessionIdForLogging() const
82 : {
83 28785 : switch (GetSessionType())
84 : {
85 0 : case Session::SessionType::kGroupIncoming:
86 0 : return AsConstIncomingGroupSession()->GetGroupId();
87 1 : case Session::SessionType::kGroupOutgoing:
88 1 : return AsConstOutgoingGroupSession()->GetGroupId();
89 28504 : case Session::SessionType::kSecure:
90 28504 : return AsConstSecureSession()->GetLocalSessionId();
91 280 : case Session::SessionType::kUnauthenticated:
92 280 : return 0;
93 0 : default:
94 0 : VerifyOrDie(false);
95 : return 0;
96 : }
97 : }
98 :
99 28779 : const char * GetSessionTypeString(const SessionHandle & session)
100 : {
101 28779 : switch (session->GetSessionType())
102 : {
103 1 : case Session::SessionType::kGroupIncoming:
104 : case Session::SessionType::kGroupOutgoing:
105 1 : return "G";
106 28498 : case Session::SessionType::kSecure:
107 28498 : return "S";
108 280 : case Session::SessionType::kUnauthenticated:
109 280 : return "U";
110 0 : default:
111 0 : return "?";
112 : }
113 : }
114 :
115 : } // namespace Transport
116 : } // namespace chip
|