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/SessionHolder.h>
18 :
19 : #include <transport/SecureSession.h>
20 : #include <transport/Session.h>
21 :
22 : namespace chip {
23 :
24 9079 : SessionHolder::~SessionHolder()
25 : {
26 9079 : Release();
27 9079 : }
28 :
29 0 : SessionHolder::SessionHolder(const SessionHolder & that) : IntrusiveListNodeBase()
30 : {
31 0 : mSession = that.mSession;
32 0 : if (mSession.HasValue())
33 : {
34 0 : mSession.Value()->AddHolder(*this);
35 : }
36 0 : }
37 :
38 0 : SessionHolder::SessionHolder(SessionHolder && that) : IntrusiveListNodeBase()
39 : {
40 0 : mSession = that.mSession;
41 0 : if (mSession.HasValue())
42 : {
43 0 : mSession.Value()->AddHolder(*this);
44 : }
45 :
46 0 : that.Release();
47 0 : }
48 :
49 302 : SessionHolder & SessionHolder::operator=(const SessionHolder & that)
50 : {
51 302 : Release();
52 :
53 302 : mSession = that.mSession;
54 302 : if (mSession.HasValue())
55 : {
56 302 : mSession.Value()->AddHolder(*this);
57 : }
58 :
59 302 : return *this;
60 : }
61 :
62 10 : SessionHolder & SessionHolder::operator=(SessionHolder && that)
63 : {
64 10 : Release();
65 :
66 10 : mSession = that.mSession;
67 10 : if (mSession.HasValue())
68 : {
69 10 : mSession.Value()->AddHolder(*this);
70 : }
71 :
72 10 : that.Release();
73 :
74 10 : return *this;
75 : }
76 :
77 56 : bool SessionHolder::GrabPairingSession(const SessionHandle & session)
78 : {
79 56 : Release();
80 :
81 56 : if (!session->IsSecureSession())
82 0 : return false;
83 :
84 56 : if (!session->AsSecureSession()->IsEstablishing())
85 0 : return false;
86 :
87 56 : GrabUnchecked(session);
88 56 : return true;
89 : }
90 :
91 9864 : bool SessionHolder::Grab(const SessionHandle & session)
92 : {
93 9864 : Release();
94 :
95 9864 : if (!session->IsActiveSession())
96 0 : return false;
97 :
98 9864 : GrabUnchecked(session);
99 9864 : return true;
100 : }
101 :
102 9922 : void SessionHolder::GrabUnchecked(const SessionHandle & session)
103 : {
104 9922 : VerifyOrDie(!mSession.HasValue());
105 9922 : mSession.Emplace(session.mSession);
106 9922 : session->AddHolder(*this);
107 9922 : }
108 :
109 21164 : void SessionHolder::Release()
110 : {
111 21164 : if (mSession.HasValue())
112 : {
113 10234 : mSession.Value()->RemoveHolder(*this);
114 10234 : mSession.ClearValue();
115 : }
116 21164 : }
117 :
118 : } // namespace chip
|