1 /* 2 * Copyright (c) 2021-2023 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_state.h" 33 #include "dinput_source_trans_callback.h" 34 #include "dinput_transbase_source_callback.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 namespace DistributedInput { 39 class DistributedInputSourceTransport { 40 public: 41 static DistributedInputSourceTransport &GetInstance(); 42 ~DistributedInputSourceTransport(); 43 44 class DInputTransbaseSourceListener : public DInputTransbaseSourceCallback { 45 public: 46 DInputTransbaseSourceListener(DistributedInputSourceTransport *transport); 47 virtual ~DInputTransbaseSourceListener(); 48 void HandleSessionData(int32_t sessionId, const std::string &messageData) override; 49 void NotifySessionClosed() override; 50 51 private: 52 DistributedInputSourceTransport *sourceTransportObj_; 53 }; 54 55 int32_t Init(); 56 void Release(); 57 58 int32_t OpenInputSoftbus(const std::string &remoteDevId, bool isToSrc); 59 void CloseInputSoftbus(const std::string &remoteDevId, bool isToSrc); 60 61 void RegisterSourceRespCallback(std::shared_ptr<DInputSourceTransCallback> callback); 62 63 int32_t PrepareRemoteInput(const std::string &deviceId); 64 int32_t UnprepareRemoteInput(const std::string &deviceId); 65 int32_t StartRemoteInput(const std::string &deviceId, const uint32_t &inputTypes); 66 int32_t StopRemoteInput(const std::string &deviceId, const uint32_t &inputTypes); 67 int32_t LatencyCount(const std::string &deviceId); 68 void StartLatencyCount(const std::string &deviceId); 69 void StartLatencyThread(const std::string &deviceId); 70 void StopLatencyThread(); 71 72 int32_t StartRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhids); 73 int32_t StopRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhids); 74 75 int32_t SendRelayPrepareRequest(const std::string &srcId, const std::string &sinkId); 76 int32_t SendRelayUnprepareRequest(const std::string &srcId, const std::string &sinkId); 77 int32_t PrepareRemoteInput(int32_t sessionId, const std::string &deviceId); 78 int32_t UnprepareRemoteInput(int32_t sessionId, const std::string &deviceId); 79 int32_t NotifyOriginPrepareResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 80 int32_t status); 81 int32_t NotifyOriginUnprepareResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 82 int32_t status); 83 84 int32_t SendRelayStartDhidRequest(const std::string &srcId, const std::string &sinkId, 85 const std::vector<std::string> &dhids); 86 int32_t SendRelayStopDhidRequest(const std::string &srcId, const std::string &sinkId, 87 const std::vector<std::string> &dhids); 88 int32_t SendRelayStartTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes); 89 int32_t SendRelayStopTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes); 90 int32_t GetCurrentSessionId(); 91 92 private: 93 int32_t SendMessage(int32_t sessionId, std::string &message); 94 void HandleData(int32_t sessionId, const std::string &message); 95 void SessionClosed(); 96 void NotifyResponsePrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 97 void NotifyResponseUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 98 void NotifyResponseStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 99 void NotifyResponseStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 100 void NotifyResponseStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); 101 void NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); 102 void NotifyResponseKeyState(int32_t sessionId, const nlohmann::json &recMsg); 103 void NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 104 void ReceiveSrcTSrcRelayPrepare(int32_t sessionId, const nlohmann::json &recMsg); 105 void ReceiveSrcTSrcRelayUnprepare(int32_t sessionId, const nlohmann::json &recMsg); 106 void NotifyResponseRelayPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 107 void NotifyResponseRelayUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 108 void ReceiveRelayPrepareResult(int32_t sessionId, const nlohmann::json &recMsg); 109 void ReceiveRelayUnprepareResult(int32_t sessionId, const nlohmann::json &recMsg); 110 111 void ReceiveSrcTSrcRelayStartDhid(int32_t sessionId, const nlohmann::json &recMsg); 112 void ReceiveSrcTSrcRelayStopDhid(int32_t sessionId, const nlohmann::json &recMsg); 113 int32_t StartRemoteInputDhids(int32_t sessionId, const std::string &deviceId, const std::string &dhids); 114 int32_t StopRemoteInputDhids(int32_t sessionId, const std::string &deviceId, const std::string &dhids); 115 void NotifyResponseRelayStartDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 116 void NotifyResponseRelayStopDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 117 int32_t NotifyOriginStartDhidResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 118 int32_t status, const std::string &dhids); 119 int32_t NotifyOriginStopDhidResult(int32_t srcTsrcSeId, const std::string &srcId, const std::string &sinkId, 120 int32_t status, const std::string &dhids); 121 void ReceiveRelayStartDhidResult(int32_t sessionId, const nlohmann::json &recMsg); 122 void ReceiveRelayStopDhidResult(int32_t sessionId, const nlohmann::json &recMsg); 123 124 void ReceiveSrcTSrcRelayStartType(int32_t sessionId, const nlohmann::json &recMsg); 125 void ReceiveSrcTSrcRelayStopType(int32_t sessionId, const nlohmann::json &recMsg); 126 int32_t StartRemoteInputType(int32_t sessionId, const std::string &deviceId, const uint32_t &inputTypes); 127 int32_t StopRemoteInputType(int32_t sessionId, const std::string &deviceId, const uint32_t &inputTypes); 128 void NotifyResponseRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 129 void NotifyResponseRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); 130 int32_t NotifyOriginStartTypeResult(int32_t sessionId, const std::string &srcId, const std::string &sinkId, 131 int32_t status, uint32_t inputTypes); 132 int32_t NotifyOriginStopTypeResult(int32_t sessionId, const std::string &srcId, const std::string &sinkId, 133 int32_t status, uint32_t inputTypes); 134 void ReceiveRelayStartTypeResult(int32_t sessionId, const nlohmann::json &recMsg); 135 void ReceiveRelayStopTypeResult(int32_t sessionId, const nlohmann::json &recMsg); 136 137 void CalculateLatency(int32_t sessionId, const nlohmann::json &recMsg); 138 std::string JointDhIds(const std::vector<std::string> &dhids); 139 void RegRespFunMap(); 140 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 using SourceTransportFunc = void (DistributedInputSourceTransport::*)(int32_t sessionId, 160 const nlohmann::json &recMsg); 161 std::map<int32_t, SourceTransportFunc> memberFuncMap_; 162 }; 163 } // namespace DistributedInput 164 } // namespace DistributedHardware 165 } // namespace OHOS 166 167 #endif // DISTRIBUTED_INPUT_SOURCE_TRANSPORT_H 168