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 "mouse_location.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include "devicestatus_define.h"s
19c29fa5a6Sopenharmony_ci#include "dsoftbus_handler.h"
20c29fa5a6Sopenharmony_ci#include "utility.h"
21c29fa5a6Sopenharmony_ci
22c29fa5a6Sopenharmony_ci#undef LOG_TAG
23c29fa5a6Sopenharmony_ci#define LOG_TAG "MouseLocation"
24c29fa5a6Sopenharmony_ci
25c29fa5a6Sopenharmony_cinamespace OHOS {
26c29fa5a6Sopenharmony_cinamespace Msdp {
27c29fa5a6Sopenharmony_cinamespace DeviceStatus {
28c29fa5a6Sopenharmony_cinamespace Cooperate {
29c29fa5a6Sopenharmony_ci
30c29fa5a6Sopenharmony_ciMouseLocation::MouseLocation(IContext *context) : context_(context) {}
31c29fa5a6Sopenharmony_ci
32c29fa5a6Sopenharmony_civoid MouseLocation::AddListener(const RegisterEventListenerEvent &event)
33c29fa5a6Sopenharmony_ci{
34c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
35c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
36c29fa5a6Sopenharmony_ci    localNetworkId_ = IDSoftbusAdapter::GetLocalNetworkId();
37c29fa5a6Sopenharmony_ci    if (event.networkId == localNetworkId_) {
38c29fa5a6Sopenharmony_ci        FI_HILOGI("Add local mouse location listener");
39c29fa5a6Sopenharmony_ci        localListeners_.insert(event.pid);
40c29fa5a6Sopenharmony_ci        return;
41c29fa5a6Sopenharmony_ci    }
42c29fa5a6Sopenharmony_ci    FI_HILOGI("Add remote mouse location listener, networkId:%{public}s", Utility::Anonymize(event.networkId).c_str());
43c29fa5a6Sopenharmony_ci    DSoftbusSubscribeMouseLocation softbusEvent {
44c29fa5a6Sopenharmony_ci        .networkId = localNetworkId_,
45c29fa5a6Sopenharmony_ci        .remoteNetworkId = event.networkId,
46c29fa5a6Sopenharmony_ci    };
47c29fa5a6Sopenharmony_ci    if (SubscribeMouseLocation(softbusEvent) != RET_OK) {
48c29fa5a6Sopenharmony_ci        FI_HILOGE("SubscribeMouseLocation failed, networkId:%{public}s", Utility::Anonymize(event.networkId).c_str());
49c29fa5a6Sopenharmony_ci        return;
50c29fa5a6Sopenharmony_ci    }
51c29fa5a6Sopenharmony_ci    listeners_[event.networkId].insert(event.pid);
52c29fa5a6Sopenharmony_ci}
53c29fa5a6Sopenharmony_ci
54c29fa5a6Sopenharmony_civoid MouseLocation::RemoveListener(const UnregisterEventListenerEvent &event)
55c29fa5a6Sopenharmony_ci{
56c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
57c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
58c29fa5a6Sopenharmony_ci    localNetworkId_ = IDSoftbusAdapter::GetLocalNetworkId();
59c29fa5a6Sopenharmony_ci    if (event.networkId == localNetworkId_) {
60c29fa5a6Sopenharmony_ci        FI_HILOGI("Remove local mouse location listener");
61c29fa5a6Sopenharmony_ci        localListeners_.erase(event.pid);
62c29fa5a6Sopenharmony_ci        return;
63c29fa5a6Sopenharmony_ci    }
64c29fa5a6Sopenharmony_ci    DSoftbusUnSubscribeMouseLocation softbusEvent {
65c29fa5a6Sopenharmony_ci        .networkId = localNetworkId_,
66c29fa5a6Sopenharmony_ci        .remoteNetworkId = event.networkId,
67c29fa5a6Sopenharmony_ci    };
68c29fa5a6Sopenharmony_ci    if (UnSubscribeMouseLocation(softbusEvent) != RET_OK) {
69c29fa5a6Sopenharmony_ci        FI_HILOGE("UnSubscribeMouseLocation failed, networkId:%{public}s", Utility::Anonymize(event.networkId).c_str());
70c29fa5a6Sopenharmony_ci    }
71c29fa5a6Sopenharmony_ci    if (listeners_.find(event.networkId) == listeners_.end()) {
72c29fa5a6Sopenharmony_ci        FI_HILOGE("No listener for networkId:%{public}s", Utility::Anonymize(event.networkId).c_str());
73c29fa5a6Sopenharmony_ci        return;
74c29fa5a6Sopenharmony_ci    }
75c29fa5a6Sopenharmony_ci    listeners_[event.networkId].erase(event.pid);
76c29fa5a6Sopenharmony_ci    if (listeners_[event.networkId].empty()) {
77c29fa5a6Sopenharmony_ci        listeners_.erase(event.networkId);
78c29fa5a6Sopenharmony_ci    }
79c29fa5a6Sopenharmony_ci}
80c29fa5a6Sopenharmony_ci
81c29fa5a6Sopenharmony_civoid MouseLocation::OnClientDied(const ClientDiedEvent &event)
82c29fa5a6Sopenharmony_ci{
83c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
84c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
85c29fa5a6Sopenharmony_ci    localNetworkId_ = IDSoftbusAdapter::GetLocalNetworkId();
86c29fa5a6Sopenharmony_ci    FI_HILOGI("Remove client died listener, pid: %{public}d", event.pid);
87c29fa5a6Sopenharmony_ci    localListeners_.erase(event.pid);
88c29fa5a6Sopenharmony_ci    for (auto it = listeners_.begin(); it != listeners_.end();) {
89c29fa5a6Sopenharmony_ci        it->second.erase(event.pid);
90c29fa5a6Sopenharmony_ci        if (it->second.empty()) {
91c29fa5a6Sopenharmony_ci            DSoftbusUnSubscribeMouseLocation softbusEvent {
92c29fa5a6Sopenharmony_ci                .networkId = localNetworkId_,
93c29fa5a6Sopenharmony_ci                .remoteNetworkId = it->first,
94c29fa5a6Sopenharmony_ci            };
95c29fa5a6Sopenharmony_ci            UnSubscribeMouseLocation(softbusEvent);
96c29fa5a6Sopenharmony_ci            it = listeners_.erase(it);
97c29fa5a6Sopenharmony_ci        } else {
98c29fa5a6Sopenharmony_ci            ++it;
99c29fa5a6Sopenharmony_ci        }
100c29fa5a6Sopenharmony_ci    }
101c29fa5a6Sopenharmony_ci}
102c29fa5a6Sopenharmony_ci
103c29fa5a6Sopenharmony_civoid MouseLocation::OnSoftbusSessionClosed(const DSoftbusSessionClosed &notice)
104c29fa5a6Sopenharmony_ci{
105c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
106c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
107c29fa5a6Sopenharmony_ci    FI_HILOGI("Session to %{public}s closed", Utility::Anonymize(notice.networkId).c_str());
108c29fa5a6Sopenharmony_ci    if (remoteSubscribers_.find(notice.networkId) != remoteSubscribers_.end()) {
109c29fa5a6Sopenharmony_ci        remoteSubscribers_.erase(notice.networkId);
110c29fa5a6Sopenharmony_ci        FI_HILOGI("Remove remote subscribers from %{public}s", Utility::Anonymize(notice.networkId).c_str());
111c29fa5a6Sopenharmony_ci    }
112c29fa5a6Sopenharmony_ci    if (listeners_.find(notice.networkId) != listeners_.end()) {
113c29fa5a6Sopenharmony_ci        listeners_.erase(notice.networkId);
114c29fa5a6Sopenharmony_ci        FI_HILOGI("Remove listeners listen to %{public}s", Utility::Anonymize(notice.networkId).c_str());
115c29fa5a6Sopenharmony_ci    }
116c29fa5a6Sopenharmony_ci}
117c29fa5a6Sopenharmony_ci
118c29fa5a6Sopenharmony_civoid MouseLocation::OnSubscribeMouseLocation(const DSoftbusSubscribeMouseLocation &notice)
119c29fa5a6Sopenharmony_ci{
120c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
121c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
122c29fa5a6Sopenharmony_ci    CHKPV(context_);
123c29fa5a6Sopenharmony_ci    remoteSubscribers_.insert(notice.networkId);
124c29fa5a6Sopenharmony_ci    FI_HILOGI("Add subscriber for networkId:%{public}s successfully", Utility::Anonymize(notice.networkId).c_str());
125c29fa5a6Sopenharmony_ci    DSoftbusReplySubscribeMouseLocation event = {
126c29fa5a6Sopenharmony_ci        .networkId = notice.remoteNetworkId,
127c29fa5a6Sopenharmony_ci        .remoteNetworkId = notice.networkId,
128c29fa5a6Sopenharmony_ci        .result = true,
129c29fa5a6Sopenharmony_ci    };
130c29fa5a6Sopenharmony_ci    FI_HILOGI("ReplySubscribeMouseLocation from networkId:%{public}s to networkId:%{public}s",
131c29fa5a6Sopenharmony_ci        Utility::Anonymize(event.networkId).c_str(), Utility::Anonymize(event.remoteNetworkId).c_str());
132c29fa5a6Sopenharmony_ci    ReplySubscribeMouseLocation(event);
133c29fa5a6Sopenharmony_ci}
134c29fa5a6Sopenharmony_ci
135c29fa5a6Sopenharmony_civoid MouseLocation::OnUnSubscribeMouseLocation(const DSoftbusUnSubscribeMouseLocation &notice)
136c29fa5a6Sopenharmony_ci{
137c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
138c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
139c29fa5a6Sopenharmony_ci    localNetworkId_ = IDSoftbusAdapter::GetLocalNetworkId();
140c29fa5a6Sopenharmony_ci    if (remoteSubscribers_.find(notice.networkId) == remoteSubscribers_.end()) {
141c29fa5a6Sopenharmony_ci        FI_HILOGE("No subscriber for networkId:%{public}s stored in remote subscriber",
142c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str());
143c29fa5a6Sopenharmony_ci        return;
144c29fa5a6Sopenharmony_ci    }
145c29fa5a6Sopenharmony_ci    remoteSubscribers_.erase(notice.networkId);
146c29fa5a6Sopenharmony_ci    DSoftbusReplyUnSubscribeMouseLocation event = {
147c29fa5a6Sopenharmony_ci        .networkId = notice.remoteNetworkId,
148c29fa5a6Sopenharmony_ci        .remoteNetworkId = notice.networkId,
149c29fa5a6Sopenharmony_ci        .result = true,
150c29fa5a6Sopenharmony_ci    };
151c29fa5a6Sopenharmony_ci    FI_HILOGI("ReplyUnSubscribeMouseLocation from networkId:%{public}s to networkId:%{public}s",
152c29fa5a6Sopenharmony_ci        Utility::Anonymize(event.networkId).c_str(), Utility::Anonymize(event.remoteNetworkId).c_str());
153c29fa5a6Sopenharmony_ci    ReplyUnSubscribeMouseLocation(event);
154c29fa5a6Sopenharmony_ci}
155c29fa5a6Sopenharmony_ci
156c29fa5a6Sopenharmony_civoid MouseLocation::OnReplySubscribeMouseLocation(const DSoftbusReplySubscribeMouseLocation &notice)
157c29fa5a6Sopenharmony_ci{
158c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
159c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
160c29fa5a6Sopenharmony_ci    if (notice.result) {
161c29fa5a6Sopenharmony_ci        FI_HILOGI("SubscribeMouseLocation of networkId:%{public}s successfully, localNetworkId:%{public}s",
162c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str(), Utility::Anonymize(notice.remoteNetworkId).c_str());
163c29fa5a6Sopenharmony_ci    } else {
164c29fa5a6Sopenharmony_ci        FI_HILOGI("SubscribeMouseLocation of networkId:%{public}s failed, localNetworkId:%{public}s",
165c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str(), Utility::Anonymize(notice.remoteNetworkId).c_str());
166c29fa5a6Sopenharmony_ci    }
167c29fa5a6Sopenharmony_ci}
168c29fa5a6Sopenharmony_ci
169c29fa5a6Sopenharmony_civoid MouseLocation::OnReplyUnSubscribeMouseLocation(const DSoftbusReplyUnSubscribeMouseLocation &notice)
170c29fa5a6Sopenharmony_ci{
171c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
172c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
173c29fa5a6Sopenharmony_ci    if (notice.result) {
174c29fa5a6Sopenharmony_ci        FI_HILOGI("UnSubscribeMouseLocation of networkId:%{public}s successfully, localNetworkId:%{public}s",
175c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str(), Utility::Anonymize(notice.remoteNetworkId).c_str());
176c29fa5a6Sopenharmony_ci    } else {
177c29fa5a6Sopenharmony_ci        FI_HILOGI("UnSubscribeMouseLocation of networkId:%{public}s failed, localNetworkId:%{public}s",
178c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str(), Utility::Anonymize(notice.remoteNetworkId).c_str());
179c29fa5a6Sopenharmony_ci    }
180c29fa5a6Sopenharmony_ci}
181c29fa5a6Sopenharmony_ci
182c29fa5a6Sopenharmony_civoid MouseLocation::OnRemoteMouseLocation(const DSoftbusSyncMouseLocation &notice)
183c29fa5a6Sopenharmony_ci{
184c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
185c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
186c29fa5a6Sopenharmony_ci    if (listeners_.find(notice.networkId) == listeners_.end()) {
187c29fa5a6Sopenharmony_ci        FI_HILOGE("No listener for networkId:%{public}s stored in listeners",
188c29fa5a6Sopenharmony_ci            Utility::Anonymize(notice.networkId).c_str());
189c29fa5a6Sopenharmony_ci        return;
190c29fa5a6Sopenharmony_ci    }
191c29fa5a6Sopenharmony_ci    LocationInfo locationInfo {
192c29fa5a6Sopenharmony_ci        .displayX = notice.mouseLocation.displayX,
193c29fa5a6Sopenharmony_ci        .displayY = notice.mouseLocation.displayY,
194c29fa5a6Sopenharmony_ci        .displayWidth = notice.mouseLocation.displayWidth,
195c29fa5a6Sopenharmony_ci        .displayHeight = notice.mouseLocation.displayHeight
196c29fa5a6Sopenharmony_ci        };
197c29fa5a6Sopenharmony_ci    for (auto pid : listeners_[notice.networkId]) {
198c29fa5a6Sopenharmony_ci        ReportMouseLocationToListener(notice.networkId, locationInfo, pid);
199c29fa5a6Sopenharmony_ci    }
200c29fa5a6Sopenharmony_ci}
201c29fa5a6Sopenharmony_ci
202c29fa5a6Sopenharmony_civoid MouseLocation::ProcessData(std::shared_ptr<MMI::PointerEvent> pointerEvent)
203c29fa5a6Sopenharmony_ci{
204c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
205c29fa5a6Sopenharmony_ci    std::lock_guard<std::mutex> guard(mutex_);
206c29fa5a6Sopenharmony_ci    CHKPV(pointerEvent);
207c29fa5a6Sopenharmony_ci    if (auto sourceType = pointerEvent->GetSourceType(); sourceType != MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
208c29fa5a6Sopenharmony_ci        FI_HILOGD("Unexpected sourceType:%{public}d", static_cast<int32_t>(sourceType));
209c29fa5a6Sopenharmony_ci        return;
210c29fa5a6Sopenharmony_ci    }
211c29fa5a6Sopenharmony_ci    LocationInfo locationInfo;
212c29fa5a6Sopenharmony_ci    TransferToLocationInfo(pointerEvent, locationInfo);
213c29fa5a6Sopenharmony_ci    if (HasLocalListener()) {
214c29fa5a6Sopenharmony_ci        for (auto pid : localListeners_) {
215c29fa5a6Sopenharmony_ci            ReportMouseLocationToListener(localNetworkId_, locationInfo, pid);
216c29fa5a6Sopenharmony_ci        }
217c29fa5a6Sopenharmony_ci    }
218c29fa5a6Sopenharmony_ci    if (!HasRemoteSubscriber()) {
219c29fa5a6Sopenharmony_ci        FI_HILOGD("No remote subscriber");
220c29fa5a6Sopenharmony_ci        return;
221c29fa5a6Sopenharmony_ci    }
222c29fa5a6Sopenharmony_ci    for (const auto &networkId : remoteSubscribers_) {
223c29fa5a6Sopenharmony_ci        SyncLocationToRemote(networkId, locationInfo);
224c29fa5a6Sopenharmony_ci    }
225c29fa5a6Sopenharmony_ci}
226c29fa5a6Sopenharmony_ci
227c29fa5a6Sopenharmony_civoid MouseLocation::SyncLocationToRemote(const std::string &remoteNetworkId, const LocationInfo &locationInfo)
228c29fa5a6Sopenharmony_ci{
229c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
230c29fa5a6Sopenharmony_ci    DSoftbusSyncMouseLocation softbusEvent {
231c29fa5a6Sopenharmony_ci        .networkId = localNetworkId_,
232c29fa5a6Sopenharmony_ci        .remoteNetworkId = remoteNetworkId,
233c29fa5a6Sopenharmony_ci        .mouseLocation = {
234c29fa5a6Sopenharmony_ci            .displayX = locationInfo.displayX,
235c29fa5a6Sopenharmony_ci            .displayY = locationInfo.displayY,
236c29fa5a6Sopenharmony_ci            .displayWidth = locationInfo.displayWidth,
237c29fa5a6Sopenharmony_ci            .displayHeight = locationInfo.displayHeight,
238c29fa5a6Sopenharmony_ci        },
239c29fa5a6Sopenharmony_ci    };
240c29fa5a6Sopenharmony_ci    SyncMouseLocation(softbusEvent);
241c29fa5a6Sopenharmony_ci}
242c29fa5a6Sopenharmony_ci
243c29fa5a6Sopenharmony_ciint32_t MouseLocation::ReplySubscribeMouseLocation(const DSoftbusReplySubscribeMouseLocation &event)
244c29fa5a6Sopenharmony_ci{
245c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
246c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_REPLY_SUBSCRIBE_MOUSE_LOCATION);
247c29fa5a6Sopenharmony_ci    packet << event.networkId << event.remoteNetworkId << event.result;
248c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
249c29fa5a6Sopenharmony_ci        FI_HILOGE("Failed to write data packet");
250c29fa5a6Sopenharmony_ci        return RET_ERR;
251c29fa5a6Sopenharmony_ci    }
252c29fa5a6Sopenharmony_ci    if (SendPacket(event.remoteNetworkId, packet) != RET_OK) {
253c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed");
254c29fa5a6Sopenharmony_ci        return RET_ERR;
255c29fa5a6Sopenharmony_ci    }
256c29fa5a6Sopenharmony_ci    return RET_OK;
257c29fa5a6Sopenharmony_ci}
258c29fa5a6Sopenharmony_ci
259c29fa5a6Sopenharmony_ciint32_t MouseLocation::ReplyUnSubscribeMouseLocation(const DSoftbusReplyUnSubscribeMouseLocation &event)
260c29fa5a6Sopenharmony_ci{
261c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
262c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_REPLY_UNSUBSCRIBE_MOUSE_LOCATION);
263c29fa5a6Sopenharmony_ci    packet << event.networkId << event.remoteNetworkId << event.result;
264c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
265c29fa5a6Sopenharmony_ci        FI_HILOGE("Failed to write data packet");
266c29fa5a6Sopenharmony_ci        return RET_ERR;
267c29fa5a6Sopenharmony_ci    }
268c29fa5a6Sopenharmony_ci    if (SendPacket(event.remoteNetworkId, packet) != RET_OK) {
269c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed");
270c29fa5a6Sopenharmony_ci        return RET_ERR;
271c29fa5a6Sopenharmony_ci    }
272c29fa5a6Sopenharmony_ci    return RET_OK;
273c29fa5a6Sopenharmony_ci}
274c29fa5a6Sopenharmony_ci
275c29fa5a6Sopenharmony_ciint32_t MouseLocation::SubscribeMouseLocation(const DSoftbusSubscribeMouseLocation &event)
276c29fa5a6Sopenharmony_ci{
277c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
278c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_SUBSCRIBE_MOUSE_LOCATION);
279c29fa5a6Sopenharmony_ci    packet << event.networkId << event.remoteNetworkId;
280c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
281c29fa5a6Sopenharmony_ci        FI_HILOGE("Failed to write data packet");
282c29fa5a6Sopenharmony_ci        return RET_ERR;
283c29fa5a6Sopenharmony_ci    }
284c29fa5a6Sopenharmony_ci    if (SendPacket(event.remoteNetworkId, packet) != RET_OK) {
285c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed");
286c29fa5a6Sopenharmony_ci        return RET_ERR;
287c29fa5a6Sopenharmony_ci    }
288c29fa5a6Sopenharmony_ci    return RET_OK;
289c29fa5a6Sopenharmony_ci}
290c29fa5a6Sopenharmony_ci
291c29fa5a6Sopenharmony_ciint32_t MouseLocation::UnSubscribeMouseLocation(const DSoftbusUnSubscribeMouseLocation &event)
292c29fa5a6Sopenharmony_ci{
293c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
294c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_UNSUBSCRIBE_MOUSE_LOCATION);
295c29fa5a6Sopenharmony_ci    packet << event.networkId << event.remoteNetworkId;
296c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
297c29fa5a6Sopenharmony_ci        FI_HILOGE("Failed to write data packet");
298c29fa5a6Sopenharmony_ci        return RET_ERR;
299c29fa5a6Sopenharmony_ci    }
300c29fa5a6Sopenharmony_ci    if (SendPacket(event.remoteNetworkId, packet) != RET_OK) {
301c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed");
302c29fa5a6Sopenharmony_ci        return RET_ERR;
303c29fa5a6Sopenharmony_ci    }
304c29fa5a6Sopenharmony_ci    return RET_OK;
305c29fa5a6Sopenharmony_ci}
306c29fa5a6Sopenharmony_ci
307c29fa5a6Sopenharmony_ciint32_t MouseLocation::SyncMouseLocation(const DSoftbusSyncMouseLocation &event)
308c29fa5a6Sopenharmony_ci{
309c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
310c29fa5a6Sopenharmony_ci    NetPacket packet(MessageId::DSOFTBUS_MOUSE_LOCATION);
311c29fa5a6Sopenharmony_ci    packet << event.networkId << event.remoteNetworkId << event.mouseLocation.displayX <<
312c29fa5a6Sopenharmony_ci        event.mouseLocation.displayY << event.mouseLocation.displayWidth << event.mouseLocation.displayHeight;
313c29fa5a6Sopenharmony_ci    if (packet.ChkRWError()) {
314c29fa5a6Sopenharmony_ci        FI_HILOGE("Failed to write data packet");
315c29fa5a6Sopenharmony_ci        return RET_ERR;
316c29fa5a6Sopenharmony_ci    }
317c29fa5a6Sopenharmony_ci    if (SendPacket(event.remoteNetworkId, packet) != RET_OK) {
318c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed");
319c29fa5a6Sopenharmony_ci        return RET_ERR;
320c29fa5a6Sopenharmony_ci    }
321c29fa5a6Sopenharmony_ci    return RET_OK;
322c29fa5a6Sopenharmony_ci}
323c29fa5a6Sopenharmony_ci
324c29fa5a6Sopenharmony_civoid MouseLocation::ReportMouseLocationToListener(const std::string &networkId, const LocationInfo &locationInfo,
325c29fa5a6Sopenharmony_ci    int32_t pid)
326c29fa5a6Sopenharmony_ci{
327c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
328c29fa5a6Sopenharmony_ci    CHKPV(context_);
329c29fa5a6Sopenharmony_ci    auto session = context_->GetSocketSessionManager().FindSessionByPid(pid);
330c29fa5a6Sopenharmony_ci    CHKPV(session);
331c29fa5a6Sopenharmony_ci    NetPacket pkt(MessageId::MOUSE_LOCATION_ADD_LISTENER);
332c29fa5a6Sopenharmony_ci    pkt << networkId << locationInfo.displayX << locationInfo.displayY <<
333c29fa5a6Sopenharmony_ci        locationInfo.displayWidth << locationInfo.displayHeight;
334c29fa5a6Sopenharmony_ci    if (pkt.ChkRWError()) {
335c29fa5a6Sopenharmony_ci        FI_HILOGE("Packet write data failed");
336c29fa5a6Sopenharmony_ci        return;
337c29fa5a6Sopenharmony_ci    }
338c29fa5a6Sopenharmony_ci    if (!session->SendMsg(pkt)) {
339c29fa5a6Sopenharmony_ci        FI_HILOGE("Sending failed");
340c29fa5a6Sopenharmony_ci        return;
341c29fa5a6Sopenharmony_ci    }
342c29fa5a6Sopenharmony_ci}
343c29fa5a6Sopenharmony_ci
344c29fa5a6Sopenharmony_civoid MouseLocation::TransferToLocationInfo(std::shared_ptr<MMI::PointerEvent> pointerEvent, LocationInfo &locationInfo)
345c29fa5a6Sopenharmony_ci{
346c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
347c29fa5a6Sopenharmony_ci    CHKPV(pointerEvent);
348c29fa5a6Sopenharmony_ci    MMI::PointerEvent::PointerItem pointerItem;
349c29fa5a6Sopenharmony_ci    if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
350c29fa5a6Sopenharmony_ci        FI_HILOGE("Corrupted pointer event");
351c29fa5a6Sopenharmony_ci        return;
352c29fa5a6Sopenharmony_ci    }
353c29fa5a6Sopenharmony_ci    auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
354c29fa5a6Sopenharmony_ci    CHKPV(display);
355c29fa5a6Sopenharmony_ci    locationInfo = {
356c29fa5a6Sopenharmony_ci        .displayX = pointerItem.GetDisplayX(),
357c29fa5a6Sopenharmony_ci        .displayY = pointerItem.GetDisplayY(),
358c29fa5a6Sopenharmony_ci        .displayWidth = display->GetWidth(),
359c29fa5a6Sopenharmony_ci        .displayHeight = display->GetHeight(),
360c29fa5a6Sopenharmony_ci    };
361c29fa5a6Sopenharmony_ci}
362c29fa5a6Sopenharmony_ci
363c29fa5a6Sopenharmony_cibool MouseLocation::HasRemoteSubscriber()
364c29fa5a6Sopenharmony_ci{
365c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
366c29fa5a6Sopenharmony_ci    return !remoteSubscribers_.empty();
367c29fa5a6Sopenharmony_ci}
368c29fa5a6Sopenharmony_ci
369c29fa5a6Sopenharmony_cibool MouseLocation::HasLocalListener()
370c29fa5a6Sopenharmony_ci{
371c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
372c29fa5a6Sopenharmony_ci    return !localListeners_.empty();
373c29fa5a6Sopenharmony_ci}
374c29fa5a6Sopenharmony_ci
375c29fa5a6Sopenharmony_ciint32_t MouseLocation::SendPacket(const std::string &remoteNetworkId, NetPacket &packet)
376c29fa5a6Sopenharmony_ci{
377c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
378c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
379c29fa5a6Sopenharmony_ci    if (!context_->GetDSoftbus().HasSessionExisted(remoteNetworkId)) {
380c29fa5a6Sopenharmony_ci        FI_HILOGE("No session connected to %{public}s", Utility::Anonymize(remoteNetworkId).c_str());
381c29fa5a6Sopenharmony_ci        return RET_ERR;
382c29fa5a6Sopenharmony_ci    }
383c29fa5a6Sopenharmony_ci    if (context_->GetDSoftbus().SendPacket(remoteNetworkId, packet) != RET_OK) {
384c29fa5a6Sopenharmony_ci        FI_HILOGE("SendPacket failed to %{public}s", Utility::Anonymize(remoteNetworkId).c_str());
385c29fa5a6Sopenharmony_ci        return RET_ERR;
386c29fa5a6Sopenharmony_ci    }
387c29fa5a6Sopenharmony_ci    return RET_OK;
388c29fa5a6Sopenharmony_ci}
389c29fa5a6Sopenharmony_ci
390c29fa5a6Sopenharmony_ci} // namespace Cooperate
391c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
392c29fa5a6Sopenharmony_ci} // namespace Msdp
393c29fa5a6Sopenharmony_ci} // namespace OHOS
394