1 /* 2 * Copyright (c) 2023-2025 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 #ifndef COOPERATE_CLIENT_H 17 #define COOPERATE_CLIENT_H 18 19 #include <functional> 20 #include <list> 21 #include <map> 22 #include <mutex> 23 #include <set> 24 #ifdef ENABLE_PERFORMANCE_CHECK 25 #include <chrono> 26 #include <vector> 27 #endif // ENABLE_PERFORMANCE_CHECK 28 29 #include "nocopyable.h" 30 31 #include "coordination_message.h" 32 #include "i_coordination_listener.h" 33 #include "i_event_listener.h" 34 #include "i_hotarea_listener.h" 35 #include "net_packet.h" 36 #include "socket_client.h" 37 #include "stream_client.h" 38 39 namespace OHOS { 40 namespace Msdp { 41 namespace DeviceStatus { 42 class CooperateClient final { 43 public: 44 using CooperateMessageCallback = std::function<void(const std::string&, const CoordinationMsgInfo&)>; 45 using CooperateStateCallback = std::function<void(bool)>; 46 using CooperateListenerPtr = std::shared_ptr<ICoordinationListener>; 47 using HotAreaListenerPtr = std::shared_ptr<IHotAreaListener>; 48 using MouseLocationListenerPtr = std::shared_ptr<IEventListener>; 49 50 struct CooperateEvent { CooperateEventCooperateEvent51 CooperateEvent(CooperateMessageCallback callback) : msgCb(callback) {} CooperateEventCooperateEvent52 CooperateEvent(CooperateStateCallback callback) : stateCb(callback) {} 53 54 CooperateMessageCallback msgCb; 55 CooperateStateCallback stateCb; 56 }; 57 58 CooperateClient() = default; 59 ~CooperateClient() = default; 60 DISALLOW_COPY_AND_MOVE(CooperateClient); 61 62 int32_t RegisterListener(CooperateListenerPtr listener, bool isCheckPermission = false); 63 int32_t UnregisterListener(CooperateListenerPtr listener, bool isCheckPermission = false); 64 int32_t Enable(CooperateMessageCallback callback, bool isCheckPermission = false); 65 int32_t Disable(CooperateMessageCallback callback, bool isCheckPermission = false); 66 int32_t Start(const std::string &remoteNetworkId, int32_t startDeviceId, 67 CooperateMessageCallback callback, bool isCheckPermission = false); 68 int32_t StartWithOptions(const std::string &remoteNetworkId, 69 int32_t startDeviceId, CooperateMessageCallback callback, const CooperateOptions &options); 70 int32_t Stop(bool isUnchained, CooperateMessageCallback callback, bool isCheckPermission = false); 71 int32_t GetCooperateState(const std::string &networkId, CooperateStateCallback callback, 72 bool isCheckPermission = false); 73 int32_t GetCooperateState(const std::string &udId, bool &state); 74 int32_t RegisterEventListener(const std::string &networkId, MouseLocationListenerPtr listener); 75 int32_t UnregisterEventListener(const std::string &networkId, MouseLocationListenerPtr listener = nullptr); 76 int32_t SetDamplingCoefficient(uint32_t direction, double coefficient); 77 int32_t AddHotAreaListener(HotAreaListenerPtr listener); 78 int32_t RemoveHotAreaListener(HotAreaListenerPtr listener = nullptr); 79 80 int32_t OnCoordinationListener(const StreamClient &client, NetPacket &pkt); 81 int32_t OnCoordinationMessage(const StreamClient &client, NetPacket &pkt); 82 int32_t OnCoordinationState(const StreamClient &client, NetPacket &pkt); 83 int32_t OnHotAreaListener(const StreamClient &client, NetPacket &pkt); 84 int32_t OnMouseLocationListener(const StreamClient &client, NetPacket &pkt); 85 void OnConnected(); 86 void OnDisconnected(); 87 88 private: 89 int32_t GenerateRequestID(); 90 void OnDevCooperateListener(const std::string &networkId, CoordinationMessage msg); 91 void OnCooperateMessageEvent(int32_t userData, const std::string &networkId, const CoordinationMsgInfo &msgInfo); 92 void OnCooperateStateEvent(int32_t userData, bool state); 93 void OnDevHotAreaListener(int32_t displayX, int32_t displayY, HotAreaType type, bool isEdge); 94 void OnDevMouseLocationListener(const std::string &networkId, const Event &event); 95 #ifdef ENABLE_PERFORMANCE_CHECK 96 void StartTrace(int32_t userData); 97 void FinishTrace(int32_t userData, CoordinationMessage msg); 98 int32_t GetFirstSuccessIndex(); 99 void DumpPerformanceInfo(); 100 #endif // ENABLE_PERFORMANCE_CHECK 101 102 std::list<CooperateListenerPtr> devCooperateListener_; 103 std::map<std::string, std::set<MouseLocationListenerPtr>> eventListener_; 104 std::list<CooperateListenerPtr> connectedCooperateListeners_; 105 std::list<HotAreaListenerPtr> devHotAreaListener_; 106 std::map<int32_t, CooperateEvent> devCooperateEvent_; 107 mutable std::mutex mtx_; 108 std::atomic_bool isListeningProcess_ { false }; 109 110 #ifdef ENABLE_PERFORMANCE_CHECK 111 struct PerformanceInfo { 112 std::map<int32_t, std::chrono::time_point<std::chrono::steady_clock>> traces_; 113 int32_t activateNum { 0 }; 114 int32_t successNum { -1 }; 115 int32_t failNum { -1 }; 116 float successRate { 0.0f }; 117 int32_t averageDuration { -1 }; 118 int32_t failBeforeSuccess { -1 }; 119 int32_t firstSuccessDuration { -1 }; 120 int32_t maxDuration { std::numeric_limits<int32_t>::min() }; 121 int32_t minDuration { std::numeric_limits<int32_t>::max() }; 122 std::vector<int32_t> durationList; 123 }; 124 std::mutex performanceLock_; 125 PerformanceInfo performanceInfo_; 126 #endif // ENABLE_PERFORMANCE_CHECK 127 }; 128 } // namespace DeviceStatus 129 } // namespace Msdp 130 } // namespace OHOS 131 #endif // COOPERATE_CLIENT_H 132