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