1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dbinder_session_object.h"
17
18 #include "ipc_debug.h"
19 #include "ipc_process_skeleton.h"
20 #include "log_tags.h"
21
22 namespace OHOS {
23 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC_SESSION_OBJ, "dbinder_session_object" };
24
DBinderSessionObject(const std::string & serviceName,const std::string & serverDeviceId,uint64_t stubIndex,IPCObjectProxy * proxy,uint32_t tokenId)25 DBinderSessionObject::DBinderSessionObject(const std::string &serviceName, const std::string &serverDeviceId,
26 uint64_t stubIndex, IPCObjectProxy *proxy, uint32_t tokenId)
27 : serviceName_(serviceName), serverDeviceId_(serverDeviceId), stubIndex_(stubIndex), proxy_(proxy),
28 tokenId_(tokenId), pid_(0), uid_(0)
29 {
30 }
31
~DBinderSessionObject()32 DBinderSessionObject::~DBinderSessionObject()
33 {
34 buff_ = nullptr;
35 }
36
37 // LCOV_EXCL_START
CloseDatabusSession()38 void DBinderSessionObject::CloseDatabusSession()
39 {
40 std::shared_ptr<DatabusSocketListener> listener = DelayedSingleton<DatabusSocketListener>::GetInstance();
41 if (listener == nullptr) {
42 ZLOGE(LOG_LABEL, "fail to get socket listener");
43 return;
44 }
45 ZLOGI(LOG_LABEL, "Shutdown, deviceId:%{public}s socketId:%{public}d",
46 IPCProcessSkeleton::ConvertToSecureString(GetDeviceId()).c_str(), socket_);
47 listener->ShutdownSocket(socket_);
48 socket_ = SOCKET_ID_INVALID;
49 }
50 // LCOV_EXCL_STOP
51
52 // LCOV_EXCL_START
GetSessionBuff()53 std::shared_ptr<BufferObject> DBinderSessionObject::GetSessionBuff()
54 {
55 if (buff_ == nullptr) {
56 std::lock_guard<std::mutex> lockGuard(buffMutex_);
57 if (buff_ == nullptr) {
58 std::shared_ptr<BufferObject> temp = std::make_shared<BufferObject>();
59 buff_ = temp;
60 }
61 }
62
63 return buff_;
64 }
65 // LCOV_EXCL_STOP
66
SetServiceName(const std::string & serviceName)67 void DBinderSessionObject::SetServiceName(const std::string &serviceName)
68 {
69 serviceName_ = serviceName;
70 }
71
GetServiceName() const72 std::string DBinderSessionObject::GetServiceName() const
73 {
74 return serviceName_;
75 }
76
SetDeviceId(const std::string & serverDeviceId)77 void DBinderSessionObject::SetDeviceId(const std::string &serverDeviceId)
78 {
79 serverDeviceId_ = serverDeviceId;
80 }
81
GetDeviceId() const82 std::string DBinderSessionObject::GetDeviceId() const
83 {
84 return serverDeviceId_;
85 }
86
SetProxy(IPCObjectProxy * proxy)87 void DBinderSessionObject::SetProxy(IPCObjectProxy *proxy)
88 {
89 proxy_ = proxy;
90 }
91
GetProxy() const92 IPCObjectProxy *DBinderSessionObject::GetProxy() const
93 {
94 return proxy_;
95 }
96
GetStubIndex() const97 uint64_t DBinderSessionObject::GetStubIndex() const
98 {
99 return stubIndex_;
100 }
101
102 // LCOV_EXCL_START
GetFlatSessionLen()103 uint32_t DBinderSessionObject::GetFlatSessionLen()
104 {
105 auto length = sizeof(struct FlatDBinderSession);
106 ZLOGD(LOG_LABEL, "FlatDBinderSession size:%{public}zu", length);
107 return length;
108 }
109 // LCOV_EXCL_STOP
110
GetSocketId() const111 int32_t DBinderSessionObject::GetSocketId() const
112 {
113 return socket_;
114 }
115
SetSocketId(int32_t socketId)116 void DBinderSessionObject::SetSocketId(int32_t socketId)
117 {
118 socket_ = socketId;
119 }
120
GetTokenId() const121 uint32_t DBinderSessionObject::GetTokenId() const
122 {
123 return tokenId_;
124 }
125
SetPeerPid(int peerPid)126 void DBinderSessionObject::SetPeerPid(int peerPid)
127 {
128 pid_ = peerPid;
129 }
130
SetPeerUid(int peerUid)131 void DBinderSessionObject::SetPeerUid(int peerUid)
132 {
133 uid_ = peerUid;
134 }
135
136 // LCOV_EXCL_START
GetPeerPid() const137 int DBinderSessionObject::GetPeerPid() const
138 {
139 return pid_;
140 }
141 // LCOV_EXCL_STOP
142
143 // LCOV_EXCL_START
GetPeerUid() const144 int DBinderSessionObject::GetPeerUid() const
145 {
146 return uid_;
147 }
148 // LCOV_EXCL_STOP
149 } // namespace OHOS
150