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 #include "ipc_process_skeleton.h"
18 #include "ISessionService.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, "dbinder_session_object" };
24
DBinderSessionObject(std::shared_ptr<Session> session,const std::string & serviceName,const std::string & serverDeviceId)25 DBinderSessionObject::DBinderSessionObject(std::shared_ptr<Session> session, const std::string &serviceName,
26 const std::string &serverDeviceId)
27 : session_(session), serviceName_(serviceName), serverDeviceId_(serverDeviceId)
28 {}
29
~DBinderSessionObject()30 DBinderSessionObject::~DBinderSessionObject()
31 {
32 session_ = nullptr;
33 buff_ = nullptr;
34 }
35
CloseDatabusSession()36 void DBinderSessionObject::CloseDatabusSession()
37 {
38 std::shared_ptr<ISessionService> manager = ISessionService::GetInstance();
39 if (session_ != nullptr && manager != nullptr) {
40 ZLOGI(LOG_LABEL, "close softbus session:%{public}" PRIu64 "", session_->GetChannelId());
41 (void)manager->CloseSession(session_);
42 }
43 }
44
SetBusSession(std::shared_ptr<Session> session)45 void DBinderSessionObject::SetBusSession(std::shared_ptr<Session> session)
46 {
47 session_ = session;
48 }
49
GetBusSession() const50 std::shared_ptr<Session> DBinderSessionObject::GetBusSession() const
51 {
52 return session_;
53 }
54
GetSessionBuff()55 std::shared_ptr<BufferObject> DBinderSessionObject::GetSessionBuff()
56 {
57 if (buff_ == nullptr) {
58 std::lock_guard<std::mutex> lockGuard(buffMutex_);
59 if (buff_ == nullptr) {
60 std::shared_ptr<BufferObject> temp = std::make_shared<BufferObject>();
61 buff_ = temp;
62 }
63 }
64
65 return buff_;
66 }
67
SetServiceName(const std::string & serviceName)68 void DBinderSessionObject::SetServiceName(const std::string &serviceName)
69 {
70 serviceName_ = serviceName;
71 }
72
GetServiceName() const73 std::string DBinderSessionObject::GetServiceName() const
74 {
75 return serviceName_;
76 }
77
SetDeviceId(const std::string & serverDeviceId)78 void DBinderSessionObject::SetDeviceId(const std::string &serverDeviceId)
79 {
80 serverDeviceId_ = serverDeviceId;
81 }
82
SetFeatureSet(std::shared_ptr<FeatureSetData> rpcFeatureSet)83 void DBinderSessionObject::SetFeatureSet(std::shared_ptr<FeatureSetData> rpcFeatureSet)
84 {
85 rpcFeatureSet_ = rpcFeatureSet;
86 }
87
GetDeviceId() const88 std::string DBinderSessionObject::GetDeviceId() const
89 {
90 return serverDeviceId_;
91 }
92
GetFeatureSet() const93 std::shared_ptr<FeatureSetData> DBinderSessionObject::GetFeatureSet() const
94 {
95 return rpcFeatureSet_;
96 }
97
GetFlatSessionLen()98 uint32_t DBinderSessionObject::GetFlatSessionLen()
99 {
100 return sizeof(struct FlatDBinderSession);
101 }
102
GetSessionHandle() const103 uint32_t DBinderSessionObject::GetSessionHandle() const
104 {
105 if (session_ != nullptr) {
106 return IPCProcessSkeleton::ConvertChannelID2Int(session_->GetChannelId());
107 }
108 return 0;
109 }
110 } // namespace OHOS
111