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 384053 : SecureSession * Session::AsSecureSession()
26 : {
27 384053 : VerifyOrDie(GetSessionType() == SessionType::kSecure);
28 384053 : return static_cast<SecureSession *>(this);
29 : }
30 :
31 42402 : const SecureSession * Session::AsConstSecureSession() const
32 : {
33 42402 : VerifyOrDie(GetSessionType() == SessionType::kSecure);
34 42402 : return static_cast<const SecureSession *>(this);
35 : }
36 :
37 607 : UnauthenticatedSession * Session::AsUnauthenticatedSession()
38 : {
39 607 : VerifyOrDie(GetSessionType() == SessionType::kUnauthenticated);
40 607 : 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 4 : OutgoingGroupSession * Session::AsOutgoingGroupSession()
56 : {
57 4 : VerifyOrDie(GetSessionType() == SessionType::kGroupOutgoing);
58 4 : return static_cast<OutgoingGroupSession *>(this);
59 : }
60 :
61 2 : const OutgoingGroupSession * Session::AsConstOutgoingGroupSession() const
62 : {
63 2 : VerifyOrDie(GetSessionType() == SessionType::kGroupOutgoing);
64 2 : return static_cast<const OutgoingGroupSession *>(this);
65 : }
66 :
67 9459 : System::Clock::Timeout Session::ComputeRoundTripTimeout(System::Clock::Timeout upperlayerProcessingTimeout,
68 : bool isFirstMessageOnExchange)
69 : {
70 9459 : if (IsGroupSession())
71 : {
72 2 : 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 9457 : return GetAckTimeout(isFirstMessageOnExchange) + upperlayerProcessingTimeout +
78 18914 : GetMessageReceiptTimeout(System::SystemClock().GetMonotonicTimestamp(), false /*isFirstMessageOnExchange*/);
79 : }
80 :
81 42709 : uint16_t Session::SessionIdForLogging() const
82 : {
83 42709 : switch (GetSessionType())
84 : {
85 0 : case Session::SessionType::kGroupIncoming:
86 0 : return AsConstIncomingGroupSession()->GetGroupId();
87 2 : case Session::SessionType::kGroupOutgoing:
88 2 : return AsConstOutgoingGroupSession()->GetGroupId();
89 42402 : case Session::SessionType::kSecure:
90 42402 : return AsConstSecureSession()->GetLocalSessionId();
91 305 : case Session::SessionType::kUnauthenticated:
92 305 : return 0;
93 0 : default:
94 0 : VerifyOrDie(false);
95 : return 0;
96 : }
97 : }
98 :
99 42703 : const char * GetSessionTypeString(const SessionHandle & session)
100 : {
101 42703 : switch (session->GetSessionType())
102 : {
103 2 : case Session::SessionType::kGroupIncoming:
104 : case Session::SessionType::kGroupOutgoing:
105 2 : return "G";
106 42396 : case Session::SessionType::kSecure:
107 42396 : return "S";
108 305 : case Session::SessionType::kUnauthenticated:
109 305 : return "U";
110 0 : default:
111 0 : return "?";
112 : }
113 : }
114 :
115 : } // namespace Transport
116 : } // namespace chip
|