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