1 /*
2 * Copyright (c) 2022-2023 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 "coordination_state_out.h"
17
18 #include "coordination_device_manager.h"
19 #include "coordination_event_manager.h"
20 #include "coordination_message.h"
21 #include "coordination_sm.h"
22 #include "coordination_softbus_adapter.h"
23 #include "coordination_util.h"
24 #include "distributed_input_adapter.h"
25
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CoordinationStateOut" };
31 } // namespace
32
CoordinationStateOut(const std::string & startDeviceDhid)33 CoordinationStateOut::CoordinationStateOut(const std::string &startDeviceDhid)
34 : startDeviceDhid_(startDeviceDhid)
35 {}
36
DeactivateCoordination(const std::string & remoteNetworkId,bool isUnchained,const std::pair<std::string,std::string> & preparedNetworkId)37 int32_t CoordinationStateOut::DeactivateCoordination(const std::string &remoteNetworkId, bool isUnchained,
38 const std::pair<std::string, std::string> &preparedNetworkId)
39 {
40 CALL_INFO_TRACE;
41 std::string tempRemoteNetworkId = remoteNetworkId;
42 if (tempRemoteNetworkId.empty()) {
43 tempRemoteNetworkId = preparedNetworkId.first;
44 }
45 int32_t ret = COOR_SOFTBUS_ADAPTER->StopRemoteCoordination(tempRemoteNetworkId, isUnchained);
46 if (ret != RET_OK) {
47 FI_HILOGE("Stop coordination failed");
48 return RET_ERR;
49 }
50 std::string taskName = "process_stop_task";
51 std::function<void()> handleProcessStopFunc =
52 std::bind(&CoordinationStateOut::ProcessStop, this, tempRemoteNetworkId);
53 CHKPR(eventHandler_, RET_ERR);
54 eventHandler_->ProxyPostTask(handleProcessStopFunc, taskName, 0);
55 return RET_OK;
56 }
57
ProcessStop(const std::string & remoteNetworkId)58 void CoordinationStateOut::ProcessStop(const std::string &remoteNetworkId)
59 {
60 CALL_DEBUG_ENTER;
61 std::string localNetworkId = COORDINATION::GetLocalNetworkId();
62 std::vector<std::string> inputDeviceDhids = COOR_DEV_MGR->GetCoordinationDhids(startDeviceDhid_, false);
63 if (inputDeviceDhids.empty()) {
64 COOR_SM->OnStopFinish(false, remoteNetworkId);
65 }
66 int32_t ret = D_INPUT_ADAPTER->StopRemoteInput(remoteNetworkId, localNetworkId, inputDeviceDhids,
67 [this, remoteNetworkId](bool isSuccess) {
68 this->OnStopRemoteInput(isSuccess, remoteNetworkId);
69 });
70 if (ret != RET_OK) {
71 COOR_SM->OnStopFinish(false, remoteNetworkId);
72 }
73 }
74
OnStopRemoteInput(bool isSuccess,const std::string & remoteNetworkId)75 void CoordinationStateOut::OnStopRemoteInput(bool isSuccess, const std::string &remoteNetworkId)
76 {
77 CALL_DEBUG_ENTER;
78 std::string taskName = "stop_finish_task";
79 std::function<void()> handleStopFinishFunc =
80 std::bind(&CoordinationSM::OnStopFinish, COOR_SM, isSuccess, remoteNetworkId);
81 CHKPV(eventHandler_);
82 eventHandler_->ProxyPostTask(handleStopFinishFunc, taskName, 0);
83 }
84
OnKeyboardOnline(const std::string & dhid,const std::pair<std::string,std::string> & networkIds)85 void CoordinationStateOut::OnKeyboardOnline(const std::string &dhid,
86 const std::pair<std::string, std::string> &networkIds)
87 {
88 std::vector<std::string> inputDeviceDhids;
89 inputDeviceDhids.push_back(dhid);
90 D_INPUT_ADAPTER->StartRemoteInput(networkIds.first, networkIds.second, inputDeviceDhids, [](bool isSuccess) {});
91 }
92
SetStartDeviceDhid(const std::string & startDeviceDhid)93 void CoordinationStateOut::SetStartDeviceDhid(const std::string &startDeviceDhid)
94 {
95 startDeviceDhid_ = startDeviceDhid;
96 }
97 } // namespace DeviceStatus
98 } // namespace Msdp
99 } // namespace OHOS
100