1 /*
2 * Copyright (c) 2024 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 "collaboration_manager.h"
17
18 namespace OHOS::AVSession {
19 std::shared_ptr<CollaborationManager> CollaborationManager::instance_;
20 std::recursive_mutex CollaborationManager::instanceLock_;
21
GetInstance()22 CollaborationManager& CollaborationManager::GetInstance()
23 {
24 std::lock_guard lockGuard(instanceLock_);
25 if (instance_ != nullptr) {
26 return *instance_;
27 }
28 SLOGI("GetInstance in");
29 instance_ = std::make_shared<CollaborationManager>();
30 instance_->exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback = nullptr;
31 instance_->exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback = nullptr;
32 instance_->exportapi_.ServiceCollaborationManager_ApplyAdvancedResource = nullptr;
33 instance_->exportapi_.ServiceCollaborationManager_PublishServiceState = nullptr;
34 return *instance_;
35 }
36
ReleaseInstance()37 void CollaborationManager::ReleaseInstance()
38 {
39 std::lock_guard lockGuard(instanceLock_);
40 SLOGI("ReleaseInstance in");
41 instance_ = nullptr;
42 }
43
CollaborationManager()44 CollaborationManager::CollaborationManager()
45 {
46 SLOGI("enter CollaborationManager construct");
47 localHardwareList_ = {
48 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_UNKNOWN_TYPE,
49 .canShare = false
50 };
51 remoteHardwareList_[0] = {
52 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_DISPLAY,
53 .canShare = false
54 };
55 remoteHardwareList_[1] = {
56 .hardWareType = ServiceCollaborationManagerHardwareType::SCM_SPEAKER,
57 .canShare = false
58 };
59 communicationRequest_ = {
60 .minBandwidth = 80 * 1024 * 1024,
61 .maxLatency = 5000,
62 .minLatency = 500,
63 .maxWaitTime = 60000,
64 .dataType = dataType_.c_str()
65 };
66 }
67
~CollaborationManager()68 CollaborationManager::~CollaborationManager()
69 {
70 SLOGI("enter ~CollaborationManager");
71 delete resourceRequest_;
72 resourceRequest_ = nullptr;
73 }
74
SendCollaborationOnStop(const std::function<void (void)> & callback)75 void CollaborationManager::SendCollaborationOnStop(const std::function<void(void)>& callback)
76 {
77 if (callback == nullptr) {
78 SLOGE("SendCollaborationOnStop callback null");
79 return;
80 }
81 sendCollaborationOnStop_ = callback;
82 }
83
OnStop(const char * peerNetworkId)84 __attribute__((no_sanitize("cfi")))static int32_t OnStop(const char* peerNetworkId)
85 {
86 SLOGE("enter onstop");
87 CollaborationManager::GetInstance().sendCollaborationOnStop_();
88 return AVSESSION_SUCCESS;
89 }
90
SendCollaborationApplyResult(const std::function<void (const int32_t code)> & callback)91 void CollaborationManager::SendCollaborationApplyResult(const std::function<
92 void(const int32_t code)>& callback)
93 {
94 if (callback == nullptr) {
95 SLOGE("SendCollaborationApplyResult callback null");
96 return;
97 }
98 sendCollaborationApplyResult_ = callback;
99 }
100
ApplyResult(int32_t errorcode,int32_t result,const char * reason)101 __attribute__((no_sanitize("cfi")))static int32_t ApplyResult(int32_t errorcode,
102 int32_t result, const char* reason)
103 {
104 SLOGI("enter ApplyResult");
105 if (result == ServiceCollaborationManagerResultCode::REJECT) {
106 SLOGE("return connect reject and reson:%{public}s", reason);
107 }
108 CollaborationManager::GetInstance().sendCollaborationApplyResult_(result);
109 return AVSESSION_SUCCESS;
110 }
111
112 static ServiceCollaborationManager_Callback serviceCollaborationCallback {
113 .OnStop = OnStop,
114 .ApplyResult = ApplyResult
115 };
116
ReadCollaborationManagerSo()117 __attribute__((no_sanitize("cfi"))) int32_t CollaborationManager::ReadCollaborationManagerSo()
118 {
119 SLOGI("enter ReadCollaborationManagerSo");
120 void *collaborationManagerExport = pluginLib_.LoadSymbol("ServiceCollaborationManager_Export");
121 if (collaborationManagerExport == nullptr) {
122 SLOGE("load libcfwk_allconnect_client.z.so failed");
123 return AVSESSION_ERROR;
124 }
125 collaborationManagerExportFun_ = (reinterpret_cast<CollaborationManagerExportFunType>(
126 collaborationManagerExport));
127 (*collaborationManagerExportFun_)(&exportapi_);
128 return AVSESSION_SUCCESS;
129 }
130
RegisterLifecycleCallback()131 int32_t CollaborationManager::RegisterLifecycleCallback()
132 {
133 SLOGI("enter RegisterLifecycleCallback");
134 if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback == nullptr) {
135 SLOGE("RegisterLifecycleCallback function sptr nullptr");
136 return AVSESSION_ERROR;
137 }
138 if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback(serviceName_.c_str(),
139 &serviceCollaborationCallback)) {
140 return AVSESSION_ERROR;
141 }
142 return AVSESSION_SUCCESS;
143 }
144
UnRegisterLifecycleCallback()145 int32_t CollaborationManager::UnRegisterLifecycleCallback()
146 {
147 SLOGI("enter UnRegisterLifecycleCallback");
148 if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback == nullptr) {
149 SLOGE("UnRegisterLifecycleCallback function sptr nullptr");
150 return AVSESSION_ERROR;
151 }
152 if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback(serviceName_.c_str())) {
153 return AVSESSION_ERROR;
154 }
155 return AVSESSION_SUCCESS;
156 }
157
PublishServiceState(const char * peerNetworkId,ServiceCollaborationManagerBussinessStatus state)158 int32_t CollaborationManager::PublishServiceState(const char* peerNetworkId,
159 ServiceCollaborationManagerBussinessStatus state)
160 {
161 SLOGI("enter PublishServiceState");
162 if (exportapi_.ServiceCollaborationManager_PublishServiceState == nullptr) {
163 SLOGE("PublishServiceState function sptr nullptr");
164 return AVSESSION_ERROR;
165 }
166 if (exportapi_.ServiceCollaborationManager_PublishServiceState(peerNetworkId,
167 serviceName_.c_str(), "NULL", state)) {
168 return AVSESSION_ERROR;
169 }
170 return AVSESSION_SUCCESS;
171 }
172
ApplyAdvancedResource(const char * peerNetworkId)173 int32_t CollaborationManager::ApplyAdvancedResource(const char* peerNetworkId)
174 {
175 SLOGI("enter ApplyAdvancedResource");
176 if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource == nullptr) {
177 SLOGE("ApplyAdvancedResource function sptr nullptr");
178 return AVSESSION_ERROR;
179 }
180 if (resourceRequest_ == nullptr) {
181 SLOGE("resourceRequest_ is nullptr");
182 return AVSESSION_ERROR;
183 }
184 resourceRequest_->localHardwareListSize = localHardwareListSize_;
185 resourceRequest_->localHardwareList = &localHardwareList_;
186 resourceRequest_->remoteHardwareListSize = remoteHardwareListSize_;
187 resourceRequest_->remoteHardwareList = remoteHardwareList_;
188 resourceRequest_->communicationRequest = &communicationRequest_;
189 if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource(peerNetworkId,
190 serviceName_.c_str(), resourceRequest_, &serviceCollaborationCallback)) {
191 return AVSESSION_ERROR;
192 }
193 return AVSESSION_SUCCESS;
194 }
195 } // namespace OHOS::AVSession