180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "collaboration_manager.h"
1780922886Sopenharmony_ci
1880922886Sopenharmony_cinamespace OHOS::AVSession {
1980922886Sopenharmony_ciCollaborationManager& CollaborationManager::GetInstance()
2080922886Sopenharmony_ci{
2180922886Sopenharmony_ci    static CollaborationManager collaborationManager;
2280922886Sopenharmony_ci    return collaborationManager;
2380922886Sopenharmony_ci}
2480922886Sopenharmony_ci
2580922886Sopenharmony_ciCollaborationManager::CollaborationManager()
2680922886Sopenharmony_ci{
2780922886Sopenharmony_ci    localHardwareList_ = {
2880922886Sopenharmony_ci        .hardWareType = ServiceCollaborationManagerHardwareType::SCM_UNKNOWN_TYPE,
2980922886Sopenharmony_ci        .canShare = false
3080922886Sopenharmony_ci    };
3180922886Sopenharmony_ci    remoteHardwareList_[0] = {
3280922886Sopenharmony_ci        .hardWareType = ServiceCollaborationManagerHardwareType::SCM_DISPLAY,
3380922886Sopenharmony_ci        .canShare = false
3480922886Sopenharmony_ci    };
3580922886Sopenharmony_ci    remoteHardwareList_[1] = {
3680922886Sopenharmony_ci        .hardWareType = ServiceCollaborationManagerHardwareType::SCM_SPEAKER,
3780922886Sopenharmony_ci        .canShare = false
3880922886Sopenharmony_ci    };
3980922886Sopenharmony_ci    communicationRequest_ = {
4080922886Sopenharmony_ci        .minBandwidth = 80 * 1024 * 1024,
4180922886Sopenharmony_ci        .maxLatency = 5000,
4280922886Sopenharmony_ci        .minLatency = 500,
4380922886Sopenharmony_ci        .maxWaitTime = 60000,
4480922886Sopenharmony_ci        .dataType = dataType_.c_str()
4580922886Sopenharmony_ci    };
4680922886Sopenharmony_ci}
4780922886Sopenharmony_ci
4880922886Sopenharmony_ciCollaborationManager::~CollaborationManager()
4980922886Sopenharmony_ci{
5080922886Sopenharmony_ci    SLOGI("enter ~CollaborationManager");
5180922886Sopenharmony_ci    delete resourceRequest_;
5280922886Sopenharmony_ci    resourceRequest_ = nullptr;
5380922886Sopenharmony_ci}
5480922886Sopenharmony_ci
5580922886Sopenharmony_civoid CollaborationManager::SendRejectStateToStopCast(const std::function<
5680922886Sopenharmony_ci    void(const int32_t code)>& callback)
5780922886Sopenharmony_ci{
5880922886Sopenharmony_ci    sendRejectStateToStopCast_ = callback;
5980922886Sopenharmony_ci}
6080922886Sopenharmony_ci
6180922886Sopenharmony_ci__attribute__((no_sanitize("cfi")))static int32_t OnStop(const char* peerNetworkId)
6280922886Sopenharmony_ci{
6380922886Sopenharmony_ci    SLOGE("Onstop to stop cast");
6480922886Sopenharmony_ci    CollaborationManager::GetInstance().sendRejectStateToStopCast_(
6580922886Sopenharmony_ci        ServiceCollaborationManagerResultCode::ONSTOP);
6680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
6780922886Sopenharmony_ci}
6880922886Sopenharmony_ci
6980922886Sopenharmony_ci__attribute__((no_sanitize("cfi")))static int32_t ApplyResult(int32_t errorcode,
7080922886Sopenharmony_ci    int32_t result, const char* reason)
7180922886Sopenharmony_ci{
7280922886Sopenharmony_ci    if (result == ServiceCollaborationManagerResultCode::REJECT) {
7380922886Sopenharmony_ci        SLOGE("return connect reject and reson:%{public}s", reason);
7480922886Sopenharmony_ci    }
7580922886Sopenharmony_ci    CollaborationManager::GetInstance().sendRejectStateToStopCast_(result);
7680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
7780922886Sopenharmony_ci}
7880922886Sopenharmony_ci
7980922886Sopenharmony_cistatic ServiceCollaborationManager_Callback serviceCollaborationCallback {
8080922886Sopenharmony_ci    .OnStop = OnStop,
8180922886Sopenharmony_ci    .ApplyResult = ApplyResult
8280922886Sopenharmony_ci};
8380922886Sopenharmony_ci
8480922886Sopenharmony_ci__attribute__((no_sanitize("cfi"))) int32_t CollaborationManager::ReadCollaborationManagerSo()
8580922886Sopenharmony_ci{
8680922886Sopenharmony_ci    SLOGI("enter ReadCollaborationManagerSo");
8780922886Sopenharmony_ci    void *collaborationManagerExport = pluginLib_.LoadSymbol("ServiceCollaborationManager_Export");
8880922886Sopenharmony_ci    if (collaborationManagerExport == nullptr) {
8980922886Sopenharmony_ci        SLOGE("load libcfwk_allconnect_client.z.so failed");
9080922886Sopenharmony_ci        return AVSESSION_ERROR;
9180922886Sopenharmony_ci    }
9280922886Sopenharmony_ci    collaborationManagerExportFun_ = (reinterpret_cast<CollaborationManagerExportFunType>(
9380922886Sopenharmony_ci        collaborationManagerExport));
9480922886Sopenharmony_ci    (*collaborationManagerExportFun_)(&exportapi_);
9580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
9680922886Sopenharmony_ci}
9780922886Sopenharmony_ci
9880922886Sopenharmony_ciint32_t CollaborationManager::RegisterLifecycleCallback()
9980922886Sopenharmony_ci{
10080922886Sopenharmony_ci    SLOGI("enter RegisterLifecycleCallback");
10180922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback == nullptr) {
10280922886Sopenharmony_ci        SLOGE("RegisterLifecycleCallback function sptr nullptr");
10380922886Sopenharmony_ci        return AVSESSION_ERROR;
10480922886Sopenharmony_ci    }
10580922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_RegisterLifecycleCallback(serviceName_.c_str(),
10680922886Sopenharmony_ci        &serviceCollaborationCallback)) {
10780922886Sopenharmony_ci        return AVSESSION_ERROR;
10880922886Sopenharmony_ci    }
10980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
11080922886Sopenharmony_ci}
11180922886Sopenharmony_ci
11280922886Sopenharmony_ciint32_t CollaborationManager::UnRegisterLifecycleCallback()
11380922886Sopenharmony_ci{
11480922886Sopenharmony_ci    SLOGI("enter UnRegisterLifecycleCallback");
11580922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback == nullptr) {
11680922886Sopenharmony_ci        SLOGE("UnRegisterLifecycleCallback function sptr nullptr");
11780922886Sopenharmony_ci        return AVSESSION_ERROR;
11880922886Sopenharmony_ci    }
11980922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_UnRegisterLifecycleCallback(serviceName_.c_str())) {
12080922886Sopenharmony_ci        return AVSESSION_ERROR;
12180922886Sopenharmony_ci    }
12280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
12380922886Sopenharmony_ci}
12480922886Sopenharmony_ci
12580922886Sopenharmony_ciint32_t CollaborationManager::PublishServiceState(const char* peerNetworkId,
12680922886Sopenharmony_ci    ServiceCollaborationManagerBussinessStatus state)
12780922886Sopenharmony_ci{
12880922886Sopenharmony_ci    SLOGI("enter PublishServiceState");
12980922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_PublishServiceState == nullptr) {
13080922886Sopenharmony_ci        SLOGE("PublishServiceState function sptr nullptr");
13180922886Sopenharmony_ci        return AVSESSION_ERROR;
13280922886Sopenharmony_ci    }
13380922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_PublishServiceState(peerNetworkId,
13480922886Sopenharmony_ci        serviceName_.c_str(), "NULL", state)) {
13580922886Sopenharmony_ci        return AVSESSION_ERROR;
13680922886Sopenharmony_ci    }
13780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
13880922886Sopenharmony_ci}
13980922886Sopenharmony_ci
14080922886Sopenharmony_ciint32_t CollaborationManager::ApplyAdvancedResource(const char* peerNetworkId)
14180922886Sopenharmony_ci{
14280922886Sopenharmony_ci    SLOGI("enter ApplyAdvancedResource");
14380922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource == nullptr) {
14480922886Sopenharmony_ci        SLOGE("ApplyAdvancedResource function sptr nullptr");
14580922886Sopenharmony_ci        return AVSESSION_ERROR;
14680922886Sopenharmony_ci    }
14780922886Sopenharmony_ci    if (resourceRequest_ == nullptr) {
14880922886Sopenharmony_ci        SLOGE("resourceRequest_ is nullptr");
14980922886Sopenharmony_ci        return AVSESSION_ERROR;
15080922886Sopenharmony_ci    }
15180922886Sopenharmony_ci    resourceRequest_->localHardwareListSize = localHardwareListSize_;
15280922886Sopenharmony_ci    resourceRequest_->localHardwareList = &localHardwareList_;
15380922886Sopenharmony_ci    resourceRequest_->remoteHardwareListSize = remoteHardwareListSize_;
15480922886Sopenharmony_ci    resourceRequest_->remoteHardwareList = remoteHardwareList_;
15580922886Sopenharmony_ci    resourceRequest_->communicationRequest = &communicationRequest_;
15680922886Sopenharmony_ci    if (exportapi_.ServiceCollaborationManager_ApplyAdvancedResource(peerNetworkId,
15780922886Sopenharmony_ci        serviceName_.c_str(), resourceRequest_, &serviceCollaborationCallback)) {
15880922886Sopenharmony_ci        return AVSESSION_ERROR;
15980922886Sopenharmony_ci    }
16080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
16180922886Sopenharmony_ci}
16280922886Sopenharmony_ci}   // namespace OHOS::AVSession