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_in.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, "CoordinationStateIn" };
31 } // namespace
32
CoordinationStateIn(const std::string & startDeviceDhid)33 CoordinationStateIn::CoordinationStateIn(const std::string &startDeviceDhid) : startDeviceDhid_(startDeviceDhid) {}
34
ActivateCoordination(const std::string & remoteNetworkId,int32_t startDeviceId)35 int32_t CoordinationStateIn::ActivateCoordination(const std::string &remoteNetworkId,
36 int32_t startDeviceId)
37 {
38 CALL_INFO_TRACE;
39 if (remoteNetworkId.empty()) {
40 FI_HILOGE("remoteNetworkId is empty");
41 return COMMON_PARAMETER_ERROR;
42 }
43 std::string localNetworkId = COORDINATION::GetLocalNetworkId();
44 if (localNetworkId.empty() || remoteNetworkId == localNetworkId) {
45 FI_HILOGE("Input Parameters error");
46 return COMMON_PARAMETER_ERROR;
47 }
48 int32_t ret = COOR_SOFTBUS_ADAPTER->StartRemoteCoordination(localNetworkId, remoteNetworkId, false);
49 if (ret != RET_OK) {
50 FI_HILOGE("Start coordination failed");
51 return COOPERATOR_FAIL;
52 }
53 std::string taskName = "process_start_task";
54 std::function<void()> handleProcessStartFunc =
55 std::bind(&CoordinationStateIn::ProcessStart, this, remoteNetworkId, startDeviceId);
56 CHKPR(eventHandler_, RET_ERR);
57 eventHandler_->ProxyPostTask(handleProcessStartFunc, taskName, 0);
58 return RET_OK;
59 }
60
ProcessStart(const std::string & remoteNetworkId,int32_t startDeviceId)61 int32_t CoordinationStateIn::ProcessStart(const std::string &remoteNetworkId, int32_t startDeviceId)
62 {
63 CALL_INFO_TRACE;
64 IContext* context = COOR_EVENT_MGR->GetIContext();
65 CHKPR(context, RET_ERR);
66 std::string originNetworkId = COOR_DEV_MGR->GetOriginNetworkId(startDeviceId);
67 if (remoteNetworkId == originNetworkId) {
68 ComeBack(remoteNetworkId, startDeviceId);
69 return RET_OK;
70 }
71 return RelayComeBack(remoteNetworkId, startDeviceId);
72 }
73
DeactivateCoordination(const std::string & remoteNetworkId,bool isUnchained,const std::pair<std::string,std::string> & preparedNetworkId)74 int32_t CoordinationStateIn::DeactivateCoordination(const std::string &remoteNetworkId, bool isUnchained,
75 const std::pair<std::string, std::string> &preparedNetworkId)
76 {
77 CALL_INFO_TRACE;
78 int32_t ret = COOR_SOFTBUS_ADAPTER->StopRemoteCoordination(remoteNetworkId, isUnchained);
79 if (ret != RET_OK) {
80 FI_HILOGE("Stop coordination failed");
81 return ret;
82 }
83 (void)(preparedNetworkId);
84 std::string taskName = "process_stop_task";
85 std::function<void()> handleProcessStopFunc = std::bind(&CoordinationStateIn::ProcessStop, this);
86 CHKPR(eventHandler_, RET_ERR);
87 eventHandler_->ProxyPostTask(handleProcessStopFunc, taskName, 0);
88 return RET_OK;
89 }
90
ProcessStop()91 int32_t CoordinationStateIn::ProcessStop()
92 {
93 CALL_DEBUG_ENTER;
94 std::vector<std::string> inputDeviceDhids = COOR_DEV_MGR->GetCoordinationDhids(startDeviceDhid_, true);
95 std::string originNetworkId = COOR_DEV_MGR->GetOriginNetworkId(startDeviceDhid_);
96 int32_t ret = D_INPUT_ADAPTER->StopRemoteInput(
97 originNetworkId, inputDeviceDhids, [this, originNetworkId](bool isSuccess) {
98 this->OnStopRemoteInput(isSuccess, originNetworkId, -1);
99 });
100 if (ret != RET_OK) {
101 FI_HILOGE("Failed to stop remote input");
102 COOR_SM->OnStopFinish(false, originNetworkId);
103 }
104 return RET_OK;
105 }
106
OnStartRemoteInput(bool isSuccess,const std::string & remoteNetworkId,int32_t startDeviceId)107 void CoordinationStateIn::OnStartRemoteInput(bool isSuccess, const std::string &remoteNetworkId, int32_t startDeviceId)
108 {
109 CALL_DEBUG_ENTER;
110 if (!isSuccess) {
111 ICoordinationState::OnStartRemoteInput(isSuccess, remoteNetworkId, startDeviceId);
112 return;
113 }
114 std::string originNetworkId = COOR_DEV_MGR->GetOriginNetworkId(startDeviceId);
115 std::vector<std::string> dhid = COOR_DEV_MGR->GetCoordinationDhids(startDeviceId);
116 std::string taskName = "relay_stop_task";
117 std::function<void()> handleRelayStopFunc = std::bind(&CoordinationStateIn::StopRemoteInput,
118 this, originNetworkId, remoteNetworkId, dhid, startDeviceId);
119 CHKPV(eventHandler_);
120 eventHandler_->ProxyPostTask(handleRelayStopFunc, taskName, 0);
121 }
122
StopRemoteInput(const std::string & originNetworkId,const std::string & remoteNetworkId,const std::vector<std::string> & dhid,int32_t startDeviceId)123 void CoordinationStateIn::StopRemoteInput(const std::string &originNetworkId,
124 const std::string &remoteNetworkId, const std::vector<std::string> &dhid, int32_t startDeviceId)
125 {
126 int32_t ret = D_INPUT_ADAPTER->StopRemoteInput(originNetworkId, dhid,
127 [this, remoteNetworkId, startDeviceId](bool isSuccess) {
128 this->OnStopRemoteInput(isSuccess, remoteNetworkId, startDeviceId);
129 });
130 if (ret != RET_OK) {
131 COOR_SM->OnStartFinish(false, originNetworkId, startDeviceId);
132 }
133 }
134
OnStopRemoteInput(bool isSuccess,const std::string & remoteNetworkId,int32_t startDeviceId)135 void CoordinationStateIn::OnStopRemoteInput(bool isSuccess,
136 const std::string &remoteNetworkId, int32_t startDeviceId)
137 {
138 FI_HILOGD("In, remoteNetworkId:%{public}s, startDeviceId:%{public}d",
139 remoteNetworkId.substr(0, SUBSTR_NETWORKID_LEN).c_str(), startDeviceId);
140 if (COOR_SM->IsStarting()) {
141 FI_HILOGD("Is starting in");
142 std::string taskName = "start_finish_task";
143 std::function<void()> handleStartFinishFunc = std::bind(&CoordinationSM::OnStartFinish,
144 COOR_SM, isSuccess, remoteNetworkId, startDeviceId);
145 CHKPV(eventHandler_);
146 eventHandler_->ProxyPostTask(handleStartFinishFunc, taskName, 0);
147 } else if (COOR_SM->IsStopping()) {
148 FI_HILOGD("Is stopping in");
149 std::string taskName = "stop_finish_task";
150 std::function<void()> handleStopFinishFunc =
151 std::bind(&CoordinationSM::OnStopFinish, COOR_SM, isSuccess, remoteNetworkId);
152 CHKPV(eventHandler_);
153 eventHandler_->ProxyPostTask(handleStopFinishFunc, taskName, 0);
154 }
155 }
156
ComeBack(const std::string & remoteNetworkId,int32_t startDeviceId)157 void CoordinationStateIn::ComeBack(const std::string &remoteNetworkId, int32_t startDeviceId)
158 {
159 CALL_INFO_TRACE;
160 std::vector<std::string> inputDeviceDhids = COOR_DEV_MGR->GetCoordinationDhids(startDeviceId);
161 if (inputDeviceDhids.empty()) {
162 COOR_SM->OnStartFinish(false, remoteNetworkId, startDeviceId);
163 }
164 int32_t ret = D_INPUT_ADAPTER->StopRemoteInput(remoteNetworkId, inputDeviceDhids,
165 [this, remoteNetworkId, startDeviceId](bool isSuccess) {
166 this->OnStopRemoteInput(isSuccess, remoteNetworkId, startDeviceId);
167 });
168 if (ret != RET_OK) {
169 COOR_SM->OnStartFinish(false, remoteNetworkId, startDeviceId);
170 }
171 }
172
RelayComeBack(const std::string & remoteNetworkId,int32_t startDeviceId)173 int32_t CoordinationStateIn::RelayComeBack(const std::string &remoteNetworkId, int32_t startDeviceId)
174 {
175 CALL_DEBUG_ENTER;
176 return PrepareAndStart(remoteNetworkId, startDeviceId);
177 }
178
SetStartDeviceDhid(const std::string & startDeviceDhid)179 void CoordinationStateIn::SetStartDeviceDhid(const std::string &startDeviceDhid)
180 {
181 startDeviceDhid_ = startDeviceDhid;
182 }
183 } // namespace DeviceStatus
184 } // namespace Msdp
185 } // namespace OHOS
186