1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "input_device_mgr.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include "device.h"
19c29fa5a6Sopenharmony_ci#include "devicestatus_define.h"
20c29fa5a6Sopenharmony_ci#include "utility.h"
21c29fa5a6Sopenharmony_ci
22c29fa5a6Sopenharmony_ci#undef LOG_TAG
23c29fa5a6Sopenharmony_ci#define LOG_TAG "InputDeviceMgr"
24c29fa5a6Sopenharmony_ci
25c29fa5a6Sopenharmony_cinamespace OHOS {
26c29fa5a6Sopenharmony_cinamespace Msdp {
27c29fa5a6Sopenharmony_cinamespace DeviceStatus {
28c29fa5a6Sopenharmony_cinamespace Cooperate {
29c29fa5a6Sopenharmony_ciconstexpr size_t MAX_INPUT_DEV_PER_DEVICE { 10 };
30c29fa5a6Sopenharmony_ci
31c29fa5a6Sopenharmony_ciInputDeviceMgr::InputDeviceMgr(IContext *context) : env_(context) {}
32c29fa5a6Sopenharmony_ci
33c29fa5a6Sopenharmony_civoid InputDeviceMgr::Enable()
34c29fa5a6Sopenharmony_ci{
35c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
36c29fa5a6Sopenharmony_ci    if (enable_) {
37c29fa5a6Sopenharmony_ci        return;
38c29fa5a6Sopenharmony_ci    }
39c29fa5a6Sopenharmony_ci    FI_HILOGI("Enable InputDeviceMgr");
40c29fa5a6Sopenharmony_ci    enable_ = true;
41c29fa5a6Sopenharmony_ci}
42c29fa5a6Sopenharmony_ci
43c29fa5a6Sopenharmony_civoid InputDeviceMgr::Disable()
44c29fa5a6Sopenharmony_ci{
45c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
46c29fa5a6Sopenharmony_ci    if (enable_) {
47c29fa5a6Sopenharmony_ci        enable_ = false;
48c29fa5a6Sopenharmony_ci    }
49c29fa5a6Sopenharmony_ci    FI_HILOGI("Disable InputDeviceMgr");
50c29fa5a6Sopenharmony_ci}
51c29fa5a6Sopenharmony_ci
52c29fa5a6Sopenharmony_civoid InputDeviceMgr::OnSoftbusSessionOpened(const DSoftbusSessionOpened &notice)
53c29fa5a6Sopenharmony_ci{
54c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
55c29fa5a6Sopenharmony_ci    NotifyInputDeviceToRemote(notice.networkId);
56c29fa5a6Sopenharmony_ci}
57c29fa5a6Sopenharmony_ci
58c29fa5a6Sopenharmony_civoid InputDeviceMgr::OnSoftbusSessionClosed(const DSoftbusSessionClosed &notice)
59c29fa5a6Sopenharmony_ci{
60c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
61c29fa5a6Sopenharmony_ci    RemoveAllRemoteInputDevice(notice.networkId);
62c29fa5a6Sopenharmony_ci}
63c29fa5a6Sopenharmony_ci
64c29fa5a6Sopenharmony_civoid InputDeviceMgr::OnLocalHotPlug(const InputHotplugEvent &notice)
65c29fa5a6Sopenharmony_ci{
66c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
67c29fa5a6Sopenharmony_ci    BroadcastHotPlugToRemote(notice);
68c29fa5a6Sopenharmony_ci}
69c29fa5a6Sopenharmony_ci
70c29fa5a6Sopenharmony_civoid InputDeviceMgr::OnRemoteInputDevice(const DSoftbusSyncInputDevice &notice)
71c29fa5a6Sopenharmony_ci{
72c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
73c29fa5a6Sopenharmony_ci    std::string networkId = notice.networkId;
74c29fa5a6Sopenharmony_ci    for (const auto &device : notice.devices) {
75c29fa5a6Sopenharmony_ci        DispDeviceInfo(device);
76c29fa5a6Sopenharmony_ci        AddRemoteInputDevice(networkId, device);
77c29fa5a6Sopenharmony_ci    }
78c29fa5a6Sopenharmony_ci}
79c29fa5a6Sopenharmony_ci
80c29fa5a6Sopenharmony_civoid InputDeviceMgr::OnRemoteHotPlug(const DSoftbusHotPlugEvent &notice)
81c29fa5a6Sopenharmony_ci{
82c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
83c29fa5a6Sopenharmony_ci}
84c29fa5a6Sopenharmony_ci
85c29fa5a6Sopenharmony_civoid InputDeviceMgr::AddVirtualInputDevice(const std::string &networkId)
86c29fa5a6Sopenharmony_ci{
87c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
88c29fa5a6Sopenharmony_ci    FI_HILOGI("Add virtual device from %{public}s", Utility::Anonymize(networkId).c_str());
89c29fa5a6Sopenharmony_ci    for (const auto &device : remoteDevices_[networkId]) {
90c29fa5a6Sopenharmony_ci        CHKPC(device);
91c29fa5a6Sopenharmony_ci        AddVirtualInputDevice(networkId, device->GetId());
92c29fa5a6Sopenharmony_ci    }
93c29fa5a6Sopenharmony_ci}
94c29fa5a6Sopenharmony_ci
95c29fa5a6Sopenharmony_civoid InputDeviceMgr::RemoveVirtualInputDevice(const std::string &networkId)
96c29fa5a6Sopenharmony_ci{
97c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
98c29fa5a6Sopenharmony_ci    FI_HILOGI("Remove virtual device from %{public}s", Utility::Anonymize(networkId).c_str());
99c29fa5a6Sopenharmony_ci    for (const auto &device : remoteDevices_[networkId]) {
100c29fa5a6Sopenharmony_ci        CHKPC(device);
101c29fa5a6Sopenharmony_ci        RemoveVirtualInputDevice(networkId, device->GetId());
102c29fa5a6Sopenharmony_ci    }
103c29fa5a6Sopenharmony_ci}
104c29fa5a6Sopenharmony_ci
105c29fa5a6Sopenharmony_civoid InputDeviceMgr::HandleRemoteHotPlug(const DSoftbusHotPlugEvent &notice)
106c29fa5a6Sopenharmony_ci{
107c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
108c29fa5a6Sopenharmony_ci    CHKPV(notice.device);
109c29fa5a6Sopenharmony_ci    auto remoteDeviceId = notice.device->GetId();
110c29fa5a6Sopenharmony_ci    if (notice.type == InputHotplugType::UNPLUG) {
111c29fa5a6Sopenharmony_ci        if (remote2VirtualIds_.find(remoteDeviceId) == remote2VirtualIds_.end()) {
112c29fa5a6Sopenharmony_ci            FI_HILOGI("No virtual matches remote deviceId:%{public}d", remoteDeviceId);
113c29fa5a6Sopenharmony_ci            return;
114c29fa5a6Sopenharmony_ci        }
115c29fa5a6Sopenharmony_ci        RemoveVirtualInputDevice(notice.networkId, remoteDeviceId);
116c29fa5a6Sopenharmony_ci    }
117c29fa5a6Sopenharmony_ci    if (notice.type == InputHotplugType::PLUG) {
118c29fa5a6Sopenharmony_ci        AddVirtualInputDevice(notice.networkId, remoteDeviceId);
119c29fa5a6Sopenharmony_ci    }
120c29fa5a6Sopenharmony_ci}
121c29fa5a6Sopenharmony_ci
122c29fa5a6Sopenharmony_civoid InputDeviceMgr::NotifyInputDeviceToRemote(const std::string &remoteNetworkId)
123c29fa5a6Sopenharmony_ci{
124c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
125c29fa5a6Sopenharmony_ci    if (!env_->GetDeviceManager().HasKeyboard()) {
126c29fa5a6Sopenharmony_ci        FI_HILOGE("Local device have no keyboard, skip");
127c29fa5a6Sopenharmony_ci        return;
128c29fa5a6Sopenharmony_ci    }
129c29fa5a6Sopenharmony_ci    auto keyboards = env_->GetDeviceManager().GetKeyboard();
130c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_INPUT_DEV_SYNC);
131c29fa5a6Sopenharmony_ci    int32_t keyboardNum = static_cast<int32_t>(keyboards.size());
132c29fa5a6Sopenharmony_ci    packet << keyboardNum;
133c29fa5a6Sopenharmony_ci    for (const auto &keyboard : keyboards) {
134c29fa5a6Sopenharmony_ci        if (SerializeDevice(keyboard, packet) != RET_OK) {
135c29fa5a6Sopenharmony_ci            FI_HILOGE("SerializeDevice failed");
136c29fa5a6Sopenharmony_ci            return;
137c29fa5a6Sopenharmony_ci        }
138c29fa5a6Sopenharmony_ci        DispDeviceInfo(keyboard);
139c29fa5a6Sopenharmony_ci    }
140c29fa5a6Sopenharmony_ci    if (int32_t ret = env_->GetDSoftbus().SendPacket(remoteNetworkId, packet); ret != RET_OK) {
141c29fa5a6Sopenharmony_ci        FI_HILOGE("SenPacket to networkId:%{public}s failed, ret:%{public}d",
142c29fa5a6Sopenharmony_ci            Utility::Anonymize(remoteNetworkId).c_str(), ret);
143c29fa5a6Sopenharmony_ci        return;
144c29fa5a6Sopenharmony_ci    }
145c29fa5a6Sopenharmony_ci    FI_HILOGI("NotifyInputDeviceToRemote networkId:%{public}s", Utility::Anonymize(remoteNetworkId).c_str());
146c29fa5a6Sopenharmony_ci}
147c29fa5a6Sopenharmony_ci
148c29fa5a6Sopenharmony_civoid InputDeviceMgr::BroadcastHotPlugToRemote(const InputHotplugEvent &notice)
149c29fa5a6Sopenharmony_ci{
150c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
151c29fa5a6Sopenharmony_ci    FI_HILOGI("HotplugType%{public}d deviceId:%{public}d", static_cast<int32_t>(notice.type), notice.deviceId);
152c29fa5a6Sopenharmony_ci    if (!notice.isKeyboard) {
153c29fa5a6Sopenharmony_ci        FI_HILOGI("Not keyboard, skip");
154c29fa5a6Sopenharmony_ci        return;
155c29fa5a6Sopenharmony_ci    }
156c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_INPUT_DEV_HOT_PLUG);
157c29fa5a6Sopenharmony_ci    packet << static_cast<int32_t>(notice.type);
158c29fa5a6Sopenharmony_ci    if (notice.type == InputHotplugType::PLUG) {
159c29fa5a6Sopenharmony_ci        auto device = env_->GetDeviceManager().GetDevice(notice.deviceId);
160c29fa5a6Sopenharmony_ci        CHKPV(device);
161c29fa5a6Sopenharmony_ci        DispDeviceInfo(device);
162c29fa5a6Sopenharmony_ci        if (SerializeDevice(device, packet) != RET_OK) {
163c29fa5a6Sopenharmony_ci            FI_HILOGE("SerializeDevice failed");
164c29fa5a6Sopenharmony_ci            return;
165c29fa5a6Sopenharmony_ci        }
166c29fa5a6Sopenharmony_ci    }
167c29fa5a6Sopenharmony_ci    if (notice.type == InputHotplugType::UNPLUG) {
168c29fa5a6Sopenharmony_ci        packet << notice.deviceId;
169c29fa5a6Sopenharmony_ci        if (packet.ChkRWError()) {
170c29fa5a6Sopenharmony_ci            FI_HILOGE("Write packet failed");
171c29fa5a6Sopenharmony_ci            return;
172c29fa5a6Sopenharmony_ci        }
173c29fa5a6Sopenharmony_ci    }
174c29fa5a6Sopenharmony_ci    if (int32_t ret = env_->GetDSoftbus().BroadcastPacket(packet); ret != RET_OK) {
175c29fa5a6Sopenharmony_ci        FI_HILOGE("BroadcastPacket failed");
176c29fa5a6Sopenharmony_ci        return;
177c29fa5a6Sopenharmony_ci    }
178c29fa5a6Sopenharmony_ci}
179c29fa5a6Sopenharmony_ci
180c29fa5a6Sopenharmony_civoid InputDeviceMgr::RemoveRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device)
181c29fa5a6Sopenharmony_ci{
182c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
183c29fa5a6Sopenharmony_ci    if (remoteDevices_.find(networkId) == remoteDevices_.end()) {
184c29fa5a6Sopenharmony_ci        FI_HILOGE("NetworkId:%{public}s have no device existed", Utility::Anonymize(networkId).c_str());
185c29fa5a6Sopenharmony_ci        return;
186c29fa5a6Sopenharmony_ci    }
187c29fa5a6Sopenharmony_ci    DispDeviceInfo(device);
188c29fa5a6Sopenharmony_ci    remoteDevices_[networkId].erase(device);
189c29fa5a6Sopenharmony_ci}
190c29fa5a6Sopenharmony_ci
191c29fa5a6Sopenharmony_civoid InputDeviceMgr::AddRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device)
192c29fa5a6Sopenharmony_ci{
193c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
194c29fa5a6Sopenharmony_ci    DispDeviceInfo(device);
195c29fa5a6Sopenharmony_ci    if (remoteDevices_.find(networkId) != remoteDevices_.end() &&
196c29fa5a6Sopenharmony_ci        remoteDevices_[networkId].size() >= MAX_INPUT_DEV_PER_DEVICE) {
197c29fa5a6Sopenharmony_ci        FI_HILOGE("Input device num from networkId:%{public}s exceeds limit", Utility::Anonymize(networkId).c_str());
198c29fa5a6Sopenharmony_ci        return;
199c29fa5a6Sopenharmony_ci    }
200c29fa5a6Sopenharmony_ci    remoteDevices_[networkId].insert(device);
201c29fa5a6Sopenharmony_ci}
202c29fa5a6Sopenharmony_ci
203c29fa5a6Sopenharmony_civoid InputDeviceMgr::RemoveAllRemoteInputDevice(const std::string &networkId)
204c29fa5a6Sopenharmony_ci{
205c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
206c29fa5a6Sopenharmony_ci    if (remoteDevices_.find(networkId) == remoteDevices_.end()) {
207c29fa5a6Sopenharmony_ci        FI_HILOGE("NetworkId:%{public}s have no device existed", Utility::Anonymize(networkId).c_str());
208c29fa5a6Sopenharmony_ci        return;
209c29fa5a6Sopenharmony_ci    }
210c29fa5a6Sopenharmony_ci    remoteDevices_.erase(networkId);
211c29fa5a6Sopenharmony_ci}
212c29fa5a6Sopenharmony_ci
213c29fa5a6Sopenharmony_civoid InputDeviceMgr::DispDeviceInfo(std::shared_ptr<IDevice> device)
214c29fa5a6Sopenharmony_ci{
215c29fa5a6Sopenharmony_ci    CHKPV(device);
216c29fa5a6Sopenharmony_ci    FI_HILOGI("  device %{public}d:%{public}s", device->GetId(), device->GetDevPath().c_str());
217c29fa5a6Sopenharmony_ci    FI_HILOGI("  sysPath:       \"%{private}s\"", device->GetSysPath().c_str());
218c29fa5a6Sopenharmony_ci    FI_HILOGI("  bus:           %{public}04x", device->GetBus());
219c29fa5a6Sopenharmony_ci    FI_HILOGI("  vendor:        %{public}04x", device->GetVendor());
220c29fa5a6Sopenharmony_ci    FI_HILOGI("  product:       %{public}04x", device->GetProduct());
221c29fa5a6Sopenharmony_ci    FI_HILOGI("  version:       %{public}04x", device->GetVersion());
222c29fa5a6Sopenharmony_ci    FI_HILOGI("  name:          \"%{public}s\"", device->GetName().c_str());
223c29fa5a6Sopenharmony_ci    FI_HILOGI("  location:      \"%{public}s\"", device->GetPhys().c_str());
224c29fa5a6Sopenharmony_ci    FI_HILOGI("  unique id:     \"%{public}s\"", device->GetUniq().c_str());
225c29fa5a6Sopenharmony_ci    FI_HILOGI("  is pointer:    %{public}s, is keyboard:%{public}s",
226c29fa5a6Sopenharmony_ci        device->IsPointerDevice() ? "True" : "False", device->IsKeyboard() ? "True" : "False");
227c29fa5a6Sopenharmony_ci}
228c29fa5a6Sopenharmony_ci
229c29fa5a6Sopenharmony_civoid InputDeviceMgr::DumpRemoteInputDevice(const std::string &networkId)
230c29fa5a6Sopenharmony_ci{
231c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
232c29fa5a6Sopenharmony_ci    if (remoteDevices_.find(networkId) == remoteDevices_.end()) {
233c29fa5a6Sopenharmony_ci        FI_HILOGE("NetworkId:%{public}s have no device existed", Utility::Anonymize(networkId).c_str());
234c29fa5a6Sopenharmony_ci        return;
235c29fa5a6Sopenharmony_ci    }
236c29fa5a6Sopenharmony_ci    FI_HILOGI("NetworkId%{public}s, device mount:%{public}zu", Utility::Anonymize(networkId).c_str(),
237c29fa5a6Sopenharmony_ci        remoteDevices_.size());
238c29fa5a6Sopenharmony_ci    for (const auto &elem : remoteDevices_[networkId]) {
239c29fa5a6Sopenharmony_ci        FI_HILOGI("DeviceId:%{public}d, deviceName:%{public}s", elem->GetId(), elem->GetName().c_str());
240c29fa5a6Sopenharmony_ci    }
241c29fa5a6Sopenharmony_ci}
242c29fa5a6Sopenharmony_ci
243c29fa5a6Sopenharmony_ciint32_t InputDeviceMgr::SerializeDevice(std::shared_ptr<IDevice> device, NetPacket &packet)
244c29fa5a6Sopenharmony_ci{
245c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
246c29fa5a6Sopenharmony_ci    packet << device->GetId() << device->GetDevPath() << device->GetSysPath() << device->GetBus() <<
247c29fa5a6Sopenharmony_ci    device->GetVendor() << device->GetProduct() << device->GetVersion() << device->GetName() <<
248c29fa5a6Sopenharmony_ci    device->GetPhys() << device->GetUniq() << device->IsPointerDevice() << device->IsKeyboard() <<
249c29fa5a6Sopenharmony_ci    static_cast<int32_t> (device->GetKeyboardType());
250c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
251c29fa5a6Sopenharmony_ci        FI_HILOGE("Write packet failed");
252c29fa5a6Sopenharmony_ci        return RET_ERR;
253c29fa5a6Sopenharmony_ci    }
254c29fa5a6Sopenharmony_ci    return RET_OK;
255c29fa5a6Sopenharmony_ci}
256c29fa5a6Sopenharmony_ci
257c29fa5a6Sopenharmony_cistd::shared_ptr<MMI::InputDevice> InputDeviceMgr::Transform(std::shared_ptr<IDevice> device)
258c29fa5a6Sopenharmony_ci{
259c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
260c29fa5a6Sopenharmony_ci    CHKPP(device);
261c29fa5a6Sopenharmony_ci    auto inputDevice = std::make_shared<MMI::InputDevice>();
262c29fa5a6Sopenharmony_ci    inputDevice->SetId(device->GetId());
263c29fa5a6Sopenharmony_ci    inputDevice->SetName(device->GetName());
264c29fa5a6Sopenharmony_ci    inputDevice->SetBus(device->GetBus());
265c29fa5a6Sopenharmony_ci    inputDevice->SetVersion(device->GetVersion());
266c29fa5a6Sopenharmony_ci    inputDevice->SetProduct(device->GetProduct());
267c29fa5a6Sopenharmony_ci    inputDevice->SetVendor(device->GetVendor());
268c29fa5a6Sopenharmony_ci    inputDevice->SetPhys(device->GetPhys());
269c29fa5a6Sopenharmony_ci    inputDevice->SetUniq(device->GetUniq());
270c29fa5a6Sopenharmony_ci    if (device->IsKeyboard()) {
271c29fa5a6Sopenharmony_ci        inputDevice->AddCapability(MMI::InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD);
272c29fa5a6Sopenharmony_ci    }
273c29fa5a6Sopenharmony_ci    if (device->IsPointerDevice()) {
274c29fa5a6Sopenharmony_ci        inputDevice->AddCapability(MMI::InputDeviceCapability::INPUT_DEV_CAP_POINTER);
275c29fa5a6Sopenharmony_ci    }
276c29fa5a6Sopenharmony_ci    return inputDevice;
277c29fa5a6Sopenharmony_ci}
278c29fa5a6Sopenharmony_ci
279c29fa5a6Sopenharmony_civoid InputDeviceMgr::AddVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId)
280c29fa5a6Sopenharmony_ci{
281c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
282c29fa5a6Sopenharmony_ci    if (remote2VirtualIds_.find(remoteDeviceId) != remote2VirtualIds_.end()) {
283c29fa5a6Sopenharmony_ci        FI_HILOGW("Remote device:%{public}d already added as virtual device:%{public}d",
284c29fa5a6Sopenharmony_ci            remoteDeviceId, remote2VirtualIds_[remoteDeviceId]);
285c29fa5a6Sopenharmony_ci            return;
286c29fa5a6Sopenharmony_ci    }
287c29fa5a6Sopenharmony_ci    auto device = GetRemoteDeviceById(networkId, remoteDeviceId);
288c29fa5a6Sopenharmony_ci    CHKPV(device);
289c29fa5a6Sopenharmony_ci    int32_t virtualDeviceId = -1;
290c29fa5a6Sopenharmony_ci    if (env_->GetInput().AddVirtualInputDevice(Transform(device), virtualDeviceId) != RET_OK) {
291c29fa5a6Sopenharmony_ci        FI_HILOGE("Add virtual device failed, remoteDeviceId:%{public}d, name:%{public}s", remoteDeviceId,
292c29fa5a6Sopenharmony_ci            device->GetName().c_str());
293c29fa5a6Sopenharmony_ci        return;
294c29fa5a6Sopenharmony_ci    }
295c29fa5a6Sopenharmony_ci    virtualInputDevicesAdded_[networkId].insert(virtualDeviceId);
296c29fa5a6Sopenharmony_ci    remote2VirtualIds_[remoteDeviceId] = virtualDeviceId;
297c29fa5a6Sopenharmony_ci    FI_HILOGI("Add virtual device success, virtualDeviceId:%{public}d", virtualDeviceId);
298c29fa5a6Sopenharmony_ci}
299c29fa5a6Sopenharmony_ci
300c29fa5a6Sopenharmony_civoid InputDeviceMgr::RemoveVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId)
301c29fa5a6Sopenharmony_ci{
302c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
303c29fa5a6Sopenharmony_ci    if (remote2VirtualIds_.find(remoteDeviceId) == remote2VirtualIds_.end()) {
304c29fa5a6Sopenharmony_ci        FI_HILOGE("No remote device from networkId%{public}s with id:%{public}d",
305c29fa5a6Sopenharmony_ci            Utility::Anonymize(networkId).c_str(), remoteDeviceId);
306c29fa5a6Sopenharmony_ci        return;
307c29fa5a6Sopenharmony_ci    }
308c29fa5a6Sopenharmony_ci    auto virtualDeviceId = remote2VirtualIds_[remoteDeviceId];
309c29fa5a6Sopenharmony_ci    if (env_->GetInput().RemoveVirtualInputDevice(virtualDeviceId) != RET_OK) {
310c29fa5a6Sopenharmony_ci        FI_HILOGE("Remove virtual device failed, virtualDeviceId:%{public}d", virtualDeviceId);
311c29fa5a6Sopenharmony_ci        return;
312c29fa5a6Sopenharmony_ci    }
313c29fa5a6Sopenharmony_ci    virtualInputDevicesAdded_[networkId].erase(virtualDeviceId);
314c29fa5a6Sopenharmony_ci    remote2VirtualIds_.erase(remoteDeviceId);
315c29fa5a6Sopenharmony_ci    FI_HILOGI("Remove virtual device success, virtualDeviceId:%{public}d", virtualDeviceId);
316c29fa5a6Sopenharmony_ci}
317c29fa5a6Sopenharmony_ci
318c29fa5a6Sopenharmony_cistd::shared_ptr<IDevice> InputDeviceMgr::GetRemoteDeviceById(const std::string &networkId, int32_t remoteDeviceId)
319c29fa5a6Sopenharmony_ci{
320c29fa5a6Sopenharmony_ci    std::shared_ptr<IDevice> dev = std::make_shared<Device>(remoteDeviceId);
321c29fa5a6Sopenharmony_ci    if (remoteDevices_.find(networkId) == remoteDevices_.end()) {
322c29fa5a6Sopenharmony_ci        FI_HILOGE("No remoteDevice from networkId:%{public}s", Utility::Anonymize(networkId).c_str());
323c29fa5a6Sopenharmony_ci        return nullptr;
324c29fa5a6Sopenharmony_ci    }
325c29fa5a6Sopenharmony_ci    if (auto iter = remoteDevices_[networkId].find(dev); iter != remoteDevices_[networkId].end()) {
326c29fa5a6Sopenharmony_ci        return *iter;
327c29fa5a6Sopenharmony_ci    }
328c29fa5a6Sopenharmony_ci    FI_HILOGW("No remote device with deviceId:%{public}d", remoteDeviceId);
329c29fa5a6Sopenharmony_ci    return nullptr;
330c29fa5a6Sopenharmony_ci}
331c29fa5a6Sopenharmony_ci
332c29fa5a6Sopenharmony_ci} // namespace Cooperate
333c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
334c29fa5a6Sopenharmony_ci} // namespace Msdp
335c29fa5a6Sopenharmony_ci} // namespace OHOS
336