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_process_skeleton.h"
19 #include "ipc_debug.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,
26     const std::string &serverDeviceId, uint64_t stubIndex, IPCObjectProxy *proxy, uint32_t tokenId)
27     :serviceName_(serviceName), serverDeviceId_(serverDeviceId),
28     stubIndex_(stubIndex), proxy_(proxy), tokenId_(tokenId), pid_(0), uid_(0)
29 {}
30 
~DBinderSessionObject()31 DBinderSessionObject::~DBinderSessionObject()
32 {
33     buff_ = nullptr;
34 }
35 
CloseDatabusSession()36 void DBinderSessionObject::CloseDatabusSession()
37 {
38     std::shared_ptr<DatabusSocketListener> listener =
39         DelayedSingleton<DatabusSocketListener>::GetInstance();
40     if (listener == nullptr) {
41         ZLOGE(LOG_LABEL, "fail to get socket listener");
42         return;
43     }
44     ZLOGI(LOG_LABEL, "Shutdown, deviceId:%{public}s socketId:%{public}d",
45         IPCProcessSkeleton::ConvertToSecureString(GetDeviceId()).c_str(), socket_);
46     listener->ShutdownSocket(socket_);
47     socket_ = SOCKET_ID_INVALID;
48 }
49 
GetSessionBuff()50 std::shared_ptr<BufferObject> DBinderSessionObject::GetSessionBuff()
51 {
52     if (buff_ == nullptr) {
53         std::lock_guard<std::mutex> lockGuard(buffMutex_);
54         if (buff_ == nullptr) {
55             std::shared_ptr<BufferObject> temp = std::make_shared<BufferObject>();
56             buff_ = temp;
57         }
58     }
59 
60     return buff_;
61 }
62 
SetServiceName(const std::string & serviceName)63 void DBinderSessionObject::SetServiceName(const std::string &serviceName)
64 {
65     serviceName_ = serviceName;
66 }
67 
GetServiceName() const68 std::string DBinderSessionObject::GetServiceName() const
69 {
70     return serviceName_;
71 }
72 
SetDeviceId(const std::string & serverDeviceId)73 void DBinderSessionObject::SetDeviceId(const std::string &serverDeviceId)
74 {
75     serverDeviceId_ = serverDeviceId;
76 }
77 
GetDeviceId() const78 std::string DBinderSessionObject::GetDeviceId() const
79 {
80     return serverDeviceId_;
81 }
82 
SetProxy(IPCObjectProxy * proxy)83 void DBinderSessionObject::SetProxy(IPCObjectProxy *proxy)
84 {
85     proxy_ = proxy;
86 }
87 
GetProxy() const88 IPCObjectProxy *DBinderSessionObject::GetProxy() const
89 {
90     return proxy_;
91 }
92 
GetStubIndex() const93 uint64_t DBinderSessionObject::GetStubIndex() const
94 {
95     return stubIndex_;
96 }
97 
GetFlatSessionLen()98 uint32_t DBinderSessionObject::GetFlatSessionLen()
99 {
100     auto length = sizeof(struct FlatDBinderSession);
101     ZLOGD(LOG_LABEL, "FlatDBinderSession size:%{public}zu", length);
102     return length;
103 }
104 
GetSocketId() const105 int32_t DBinderSessionObject::GetSocketId() const
106 {
107     return socket_;
108 }
109 
SetSocketId(int32_t socketId)110 void DBinderSessionObject::SetSocketId(int32_t socketId)
111 {
112     socket_ = socketId;
113 }
114 
GetTokenId() const115 uint32_t DBinderSessionObject::GetTokenId() const
116 {
117     return tokenId_;
118 }
119 
SetPeerPid(int peerPid)120 void DBinderSessionObject::SetPeerPid(int peerPid)
121 {
122     pid_ = peerPid;
123 }
124 
SetPeerUid(int peerUid)125 void DBinderSessionObject::SetPeerUid(int peerUid)
126 {
127     uid_ = peerUid;
128 }
129 
GetPeerPid() const130 int DBinderSessionObject::GetPeerPid() const
131 {
132     return pid_;
133 }
134 
GetPeerUid() const135 int DBinderSessionObject::GetPeerUid() const
136 {
137     return uid_;
138 }
139 } // namespace OHOS
140