1 /* 2 * Copyright (c) 2021-2024 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 DISTRIBUTED_INPUT_SOURCE_TRANSPORT_H 17 #define DISTRIBUTED_INPUT_SOURCE_TRANSPORT_H 18 19 #include <condition_variable> 20 #include <map> 21 #include <mutex> 22 #include <set> 23 #include <string> 24 #include <vector> 25 #include <thread> 26 27 #include "constants.h" 28 #include "event_handler.h" 29 #include "nlohmann/json.hpp" 30 #include "securec.h" 31 32 #include "dinput_source_trans_callback.h" 33 #include "dinput_transbase_source_callback.h" 34 35 namespace OHOS { 36 namespace DistributedHardware { 37 namespace DistributedInput { 38 class DistributedInputSourceTransport { 39 public: 40 static DistributedInputSourceTransport &GetInstance(); 41 ~DistributedInputSourceTransport(); 42 43 class DInputTransbaseSourceListener : public DInputTransbaseSourceCallback { 44 public: 45 DInputTransbaseSourceListener(DistributedInputSourceTransport *transport); 46 virtual ~DInputTransbaseSourceListener(); 47 void HandleSessionData(int32_t sessionId, const std::string &messageData) override; 48 void NotifySessionClosed() override; 49 50 private: 51 DistributedInputSourceTransport *sourceTransportObj_; 52 }; 53 54 int32_t Init(); 55 void Release(); 56 57 int32_t OpenInputSoftbus(const std::string &remoteDevId, bool isToSrc); 58 void CloseInputSoftbus(const std::string &remoteDevId, bool isToSrc); 59 60 void RegisterSourceRespCallback(std::shared_ptr<DInputSourceTransCallback> callback); 61 62 int32_t PrepareRemoteInput(const std::string &deviceId); 63 int32_t UnprepareRemoteInput(const std::string &deviceId); 64 int32_t StartRemoteInput(const std::string &deviceId, const uint32_t &inputTypes); 65 int32_t StopRemoteInput(const std::string &deviceId, const uint32_t &inputTypes); 66 int32_t LatencyCount(const std::string &deviceId); 67 void StartLatencyCount(const std::string &deviceId); 68 void StartLatencyThread(const std::string &deviceId); 69 void StopLatencyThread(); 70 71 int32_t StartRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhids); 72 int32_t StopRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhids); 73 74 int32_t SendRelayPrepareRequest(const std::string &srcId, const std::string &sinkId); 75 int32_t SendRelayUnprepareRequest(const std::string &srcId, const std::string &sinkId); 76 int32_t PrepareRemoteInput(int32_t sessionId, const std::string &deviceId); 77 int32_t UnprepareRemoteInput(int32_t sessionId, const std::string &deviceId); 78 int32_t NotifyOriginPrepareResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 79 int32_t status); 80 int32_t NotifyOriginUnprepareResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 81 int32_t status); 82 83 int32_t SendRelayStartDhidRequest(const std::string &srcId, const std::string &sinkId, 84 const std::vector<std::string> &dhids); 85 int32_t SendRelayStopDhidRequest(const std::string &srcId, const std::string &sinkId, 86 const std::vector<std::string> &dhids); 87 int32_t SendRelayStartTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes); 88 int32_t SendRelayStopTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes); 89 int32_t GetCurrentSessionId(); 90 91 private: 92 int32_t SendMessage(int32_t sessionId, std::string &message); 93 void HandleData(int32_t sessionId, const std::string &message); 94 void HandleEventFirst(int32_t sessionId, const nlohmann::json &recMsg); 95 void HandleEventSecond(int32_t sessionId, const nlohmann::json &recMsg); 96 void SessionClosed(); 97 void NotifyResponsePrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 98 void NotifyResponseUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 99 void NotifyResponseStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 100 void NotifyResponseStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 101 void NotifyResponseStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); 102 void NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); 103 void NotifyResponseKeyState(int32_t sessionId, const nlohmann::json &recMsg); 104 void NotifyResponseKeyStateBatch(int32_t sessionId, const nlohmann::json &recMsg); 105 void NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 106 void ReceiveSrcTSrcRelayPrepare(int32_t sessionId, const nlohmann::json &recMsg); 107 void ReceiveSrcTSrcRelayUnprepare(int32_t sessionId, const nlohmann::json &recMsg); 108 void NotifyResponseRelayPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 109 void NotifyResponseRelayUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 110 void ReceiveRelayPrepareResult(int32_t sessionId, const nlohmann::json &recMsg); 111 void ReceiveRelayUnprepareResult(int32_t sessionId, const nlohmann::json &recMsg); 112 113 void ReceiveSrcTSrcRelayStartDhid(int32_t sessionId, const nlohmann::json &recMsg); 114 void ReceiveSrcTSrcRelayStopDhid(int32_t sessionId, const nlohmann::json &recMsg); 115 int32_t StartRemoteInputDhids(int32_t sessionId, const std::string &deviceId, const std::string &dhids); 116 int32_t StopRemoteInputDhids(int32_t sessionId, const std::string &deviceId, const std::string &dhids); 117 void NotifyResponseRelayStartDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 118 void NotifyResponseRelayStopDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 119 int32_t NotifyOriginStartDhidResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 120 int32_t status, const std::string &dhids); 121 int32_t NotifyOriginStopDhidResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 122 int32_t status, const std::string &dhids); 123 void ReceiveRelayStartDhidResult(int32_t sessionId, const nlohmann::json &recMsg); 124 void ReceiveRelayStopDhidResult(int32_t sessionId, const nlohmann::json &recMsg); 125 126 void ReceiveSrcTSrcRelayStartType(int32_t sessionId, const nlohmann::json &recMsg); 127 void ReceiveSrcTSrcRelayStopType(int32_t sessionId, const nlohmann::json &recMsg); 128 int32_t StartRemoteInputType(int32_t sessionId, const std::string &deviceId, const uint32_t &inputTypes); 129 int32_t StopRemoteInputType(int32_t sessionId, const std::string &deviceId, const uint32_t &inputTypes); 130 void NotifyResponseRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 131 void NotifyResponseRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 132 int32_t NotifyOriginStartTypeResult(int32_t sessionId, const std::string &srcId, const std::string &sinkId, 133 int32_t status, uint32_t inputTypes); 134 int32_t NotifyOriginStopTypeResult(int32_t sessionId, const std::string &srcId, const std::string &sinkId, 135 int32_t status, uint32_t inputTypes); 136 void ReceiveRelayStartTypeResult(int32_t sessionId, const nlohmann::json &recMsg); 137 void ReceiveRelayStopTypeResult(int32_t sessionId, const nlohmann::json &recMsg); 138 139 void CalculateLatency(int32_t sessionId, const nlohmann::json &recMsg); 140 void ResetKeyboardKeyState(const std::string &deviceId, const std::vector<std::string> &dhids); 141 private: 142 std::mutex operationMutex_; 143 std::set<int32_t> sessionIdSet_; 144 std::shared_ptr<DInputSourceTransCallback> callback_; 145 std::shared_ptr<DistributedInputSourceTransport::DInputTransbaseSourceListener> statuslistener_; 146 std::string mySessionName_ = ""; 147 std::condition_variable openSessionWaitCond_; 148 uint64_t deltaTime_ = 0; 149 uint64_t deltaTimeAll_ = 0; 150 uint64_t sendTime_ = 0; 151 uint32_t sendNum_ = 0; 152 uint32_t recvNum_ = 0; 153 std::atomic<bool> isLatencyThreadRunning_ = false; 154 std::thread latencyThread_; 155 std::string eachLatencyDetails_ = ""; 156 std::atomic<int32_t> injectThreadNum = 0; 157 std::atomic<int32_t> latencyThreadNum = 0; 158 }; 159 } // namespace DistributedInput 160 } // namespace DistributedHardware 161 } // namespace OHOS 162 163 #endif // DISTRIBUTED_INPUT_SOURCE_TRANSPORT_H 164