From 4030836b4a0be9efdecff1ebaae709e69747f0dc Mon Sep 17 00:00:00 2001 From: xxxx Date: Wed, 9 Nov 2022 09:57:38 +0800 Subject: [PATCH] Revert " Match-id-6a58479f091fc31017b473c27085fcc3c97cac81 --- bundle.json | 1 - .../common/include/dinput_softbus_define.h | 76 +- .../include/dinput_source_trans_callback.h | 13 +- .../include/dinput_transbase_sink_callback.h | 32 - .../dinput_transbase_source_callback.h | 32 - .../src/distributed_input_collector.cpp | 2 +- .../include/distributed_input_sink_manager.h | 5 - .../src/distributed_input_sink_manager.cpp | 369 ++++----- services/sink/transport/BUILD.gn | 2 - .../distributed_input_sink_transport.h | 30 +- .../src/distributed_input_sink_transport.cpp | 327 ++++++-- services/source/inputinject/BUILD.gn | 1 - .../include/distributed_input_node_manager.h | 1 - .../src/distributed_input_node_manager.cpp | 28 +- .../distributed_input_source_manager.h | 43 +- .../src/distributed_input_source_manager.cpp | 213 ++--- services/source/transport/BUILD.gn | 2 - .../distributed_input_source_transport.h | 52 +- .../distributed_input_source_transport.cpp | 743 +++++++++++++----- services/transportbase/BUILD.gn | 80 -- .../distributed_input_transport_base.h | 101 --- .../src/distributed_input_transport_base.cpp | 510 ------------ 22 files changed, 1180 insertions(+), 1483 deletions(-) delete mode 100644 services/common/include/dinput_transbase_sink_callback.h delete mode 100644 services/common/include/dinput_transbase_source_callback.h delete mode 100644 services/transportbase/BUILD.gn delete mode 100644 services/transportbase/include/distributed_input_transport_base.h delete mode 100644 services/transportbase/src/distributed_input_transport_base.cpp diff --git a/bundle.json b/bundle.json index 34f9420..68c8d98 100755 --- a/bundle.json +++ b/bundle.json @@ -56,7 +56,6 @@ "//foundation/distributedhardware/distributed_input/services/sink/sinkmanager:libdinput_sink", "//foundation/distributedhardware/distributed_input/services/sink/transport:libdinput_sink_trans", "//foundation/distributedhardware/distributed_input/services/sink/inputcollector:libdinput_collector", - "//foundation/distributedhardware/distributed_input/services/transportbase:libdinput_trans_base", "//foundation/distributedhardware/distributed_input/sourcehandler:libdinput_source_handler", "//foundation/distributedhardware/distributed_input/sinkhandler:libdinput_sink_handler", "//foundation/distributedhardware/distributed_input/inputdevicehandler:libdinput_handler", diff --git a/services/common/include/dinput_softbus_define.h b/services/common/include/dinput_softbus_define.h index 2583ca2..c1c7351 100644 --- a/services/common/include/dinput_softbus_define.h +++ b/services/common/include/dinput_softbus_define.h @@ -42,7 +42,8 @@ namespace DistributedInput { const uint32_t INTERCEPT_STRING_LENGTH = 20; const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; - const std::string SESSION_NAME = "ohos.dhardware.dinput.session"; + const std::string SESSION_NAME_SOURCE = "ohos.dhardware.dinput.so_"; + const std::string SESSION_NAME_SINK = "ohos.dhardware.dinput.si_"; const std::string GROUP_ID = "input_softbus_group_id"; #define DINPUT_SOFTBUS_KEY_CMD_TYPE "dinput_softbus_key_cmd_type" @@ -60,7 +61,7 @@ namespace DistributedInput { #define DINPUT_SOFTBUS_KEY_SRC_DEV_ID "dinput_softbus_key_src_dev_id" #define DINPUT_SOFTBUS_KEY_SINK_DEV_ID "dinput_softbus_key_sink_dev_id" - // src will receive + // json value const uint32_t TRANS_SINK_MSG_ONPREPARE = 1; const uint32_t TRANS_SINK_MSG_ONUNPREPARE = 2; const uint32_t TRANS_SINK_MSG_ONSTART = 3; @@ -70,42 +71,45 @@ namespace DistributedInput { const uint32_t TRANS_SINK_MSG_DHID_ONSTART = 7; const uint32_t TRANS_SINK_MSG_DHID_ONSTOP = 8; const uint32_t TRANS_SINK_MSG_KEY_STATE = 9; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_PREPARE = 10; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_UNPREPARE = 11; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_PREPARE_RESULT = 12; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_UNPREPARE_RESULT = 13; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_DHID = 14; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_DHID = 15; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_DHID_RESULT = 16; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_DHID_RESULT = 17; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_TYPE = 18; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_TYPE = 19; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_TYPE_RESULT = 20; - const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_TYPE_RESULT = 21; - const uint32_t TRANS_SINK_MSG_ON_RELAY_PREPARE = 22; - const uint32_t TRANS_SINK_MSG_ON_RELAY_UNPREPARE = 23; - const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTDHID = 24; - const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPDHID = 25; - const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTTYPE = 26; - const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPTYPE = 27; + // A device is sinkdev, B device is source + const uint32_t TRANS_SINK_MSG_ON_RELAY_PREPARE = 50; // A sink send prepare result to B souce + const uint32_t TRANS_SINK_MSG_ON_RELAY_UNPREPARE = 51; + const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTDHID = 52; + const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPDHID = 53; + const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTTYPE = 54; + const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPTYPE = 55; - // src or sink - const uint32_t TRANS_MSG_SRC_SINK_SPLIT = 30; + const uint32_t TRANS_SOURCE_MSG_PREPARE = 11; + const uint32_t TRANS_SOURCE_MSG_UNPREPARE = 12; + const uint32_t TRANS_SOURCE_MSG_START_TYPE = 13; + const uint32_t TRANS_SOURCE_MSG_STOP_TYPE = 14; + const uint32_t TRANS_SOURCE_MSG_START_DHID = 15; + const uint32_t TRANS_SOURCE_MSG_STOP_DHID = 16; + // A mmi call prepare, A souce will relay prepare to B source + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_PREPARE = 17; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_UNPREPARE = 18; + // B source relay send prepare to A sink + const uint32_t TRANS_SOURCE_MSG_PREPARE_FOR_REL = 19; + const uint32_t TRANS_SOURCE_MSG_UNPREPARE_FOR_REL = 20; + // B source receive A sink result,then notify A source result + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_PREPARE_RESULT = 21; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_UNPREPARE_RESULT = 22; - // sink will receive - const uint32_t TRANS_SOURCE_MSG_PREPARE = 31; - const uint32_t TRANS_SOURCE_MSG_UNPREPARE = 32; - const uint32_t TRANS_SOURCE_MSG_START_TYPE = 33; - const uint32_t TRANS_SOURCE_MSG_STOP_TYPE = 34; - const uint32_t TRANS_SOURCE_MSG_START_DHID = 35; - const uint32_t TRANS_SOURCE_MSG_STOP_DHID = 36; - const uint32_t TRANS_SOURCE_MSG_LATENCY = 37; - const uint32_t TRANS_SOURCE_MSG_START_DHID_FOR_REL = 38; - const uint32_t TRANS_SOURCE_MSG_STOP_DHID_FOR_REL = 39; - const uint32_t TRANS_SOURCE_MSG_START_TYPE_FOR_REL = 40; - const uint32_t TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL = 41; - const uint32_t TRANS_SOURCE_MSG_PREPARE_FOR_REL = 42; - const uint32_t TRANS_SOURCE_MSG_UNPREPARE_FOR_REL = 43; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_DHID = 23; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_DHID = 24; + const uint32_t TRANS_SOURCE_MSG_START_DHID_FOR_REL = 25; + const uint32_t TRANS_SOURCE_MSG_STOP_DHID_FOR_REL = 26; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_DHID_RESULT = 27; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_DHID_RESULT = 28; + + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_TYPE = 29; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_TYPE = 30; + const uint32_t TRANS_SOURCE_MSG_START_TYPE_FOR_REL = 31; + const uint32_t TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL = 32; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_START_TYPE_RESULT = 33; + const uint32_t TRANS_SOURCE_TO_SOURCE_MSG_STOP_TYPE_RESULT = 34; + + const uint32_t TRANS_SOURCE_MSG_LATENCY = 40; } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/common/include/dinput_source_trans_callback.h b/services/common/include/dinput_source_trans_callback.h index 25f1346..23a1e64 100644 --- a/services/common/include/dinput_source_trans_callback.h +++ b/services/common/include/dinput_source_trans_callback.h @@ -25,22 +25,19 @@ class DInputSourceTransCallback { public: virtual void onResponseRegisterDistributedHardware(const std::string deviceId, const std::string dhId, bool result) = 0; - virtual void onResponsePrepareRemoteInput(const std::string deviceId, bool result) = 0; + virtual void onResponsePrepareRemoteInput(const std::string deviceId, bool result, const std::string &object) = 0; virtual void onResponseUnprepareRemoteInput(const std::string deviceId, bool result) = 0; - virtual void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result, - const std::string& object) = 0; + virtual void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result) = 0; virtual void onResponseStopRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result) = 0; - virtual void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result, - const std::string& object) = 0; + virtual void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) = 0; virtual void onResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) = 0; virtual void onResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type, const uint32_t code, const uint32_t value) = 0; virtual void onReceivedEventRemoteInput(const std::string deviceId, const std::string &object) = 0; - virtual void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result) = 0; + virtual void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result, + const std::string &object) = 0; virtual void onResponseRelayUnprepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result) = 0; - virtual void onResponseRelayStartDhidRemoteInput(const std::string &deviceId, const std::string &object) = 0; - virtual void onResponseRelayStartTypeRemoteInput(const std::string &deviceId, const std::string &object) = 0; virtual void onReceiveRelayPrepareResult(int32_t status, const std::string &srcId, const std::string &sinkId) = 0; virtual void onReceiveRelayUnprepareResult(int32_t status, const std::string &srcId, const std::string &sinkId) = 0; diff --git a/services/common/include/dinput_transbase_sink_callback.h b/services/common/include/dinput_transbase_sink_callback.h deleted file mode 100644 index fe6b2ae..0000000 --- a/services/common/include/dinput_transbase_sink_callback.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISTRIBUTED_INPUT_TRANSBASE_SINK_H -#define DISTRIBUTED_INPUT_TRANSBASE_SINK_H - -#include - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class DInputTransbaseSinkCallback { -public: - virtual void HandleSessionData(int32_t sessionId, const std::string& messageData) = 0; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // DISTRIBUTED_INPUT_TRANSBASE_SINK_H diff --git a/services/common/include/dinput_transbase_source_callback.h b/services/common/include/dinput_transbase_source_callback.h deleted file mode 100644 index e45dcc4..0000000 --- a/services/common/include/dinput_transbase_source_callback.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISTRIBUTED_INPUT_TRANSBASE_SOURCE_H -#define DISTRIBUTED_INPUT_TRANSBASE_SOURCE_H - -#include - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class DInputTransbaseSourceCallback { -public: - virtual void HandleSessionData(int32_t sessionId, const std::string& messageData) = 0; -}; -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // DISTRIBUTED_INPUT_TRANSBASE_SOURCE_H diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 64fc63c..3d97076 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -76,7 +76,7 @@ bool DistributedInputCollector::InitCollectEventsThread() collectThreadID_ = -1; int32_t ret = pthread_create(&collectThreadID_, &attr, CollectEventsThread, this); if (ret != 0) { - DHLOGE("DistributedInputCollector::InitCollectEventsThread create thread failed:%d \n", ret); + DHLOGE("DistributedInputCollector::InitCollectEventsThread create thread failed:%d \n", ret); pthread_attr_destroy(&attr); collectThreadID_ = -1; isCollectingEvents_ = false; diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index 7ebb2d6..6e65034 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -31,7 +31,6 @@ #include "singleton.h" #include "system_ability.h" #include "system_ability_status_change_stub.h" -#include "nlohmann/json.hpp" #include "constants_dinput.h" #include "dinput_sink_trans_callback.h" @@ -49,8 +48,6 @@ class DistributedInputSinkManager : public SystemAbility, public DistributedInpu DECLARE_SYSTEM_ABILITY(DistributedInputSinkManager) public: - static DistributedInputSinkManager &GetInstance(); - DistributedInputSinkManager() = default; DistributedInputSinkManager(int32_t saId, bool runOnCreate); ~DistributedInputSinkManager(); @@ -156,8 +153,6 @@ public: int32_t Dump(int32_t fd, const std::vector& args) override; - void AddWhiteList(nlohmann::json &jsonStr); - private: void CleanExceptionalInfo(const SrcScreenInfo& srcScreenInfo); void CallBackScreenInfoChange(); diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index ee559b4..62523ce 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -26,6 +26,7 @@ #include "distributed_hardware_log.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include "nlohmann/json.hpp" #include "screen_manager.h" #include "string_ex.h" #include "system_ability_definition.h" @@ -59,12 +60,6 @@ DistributedInputSinkManager::~DistributedInputSinkManager() projectWindowListener_ = nullptr; } -DistributedInputSinkManager &DistributedInputSinkManager::GetInstance() -{ - static DistributedInputSinkManager instance; - return instance; -} - DistributedInputSinkManager::DInputSinkListener::DInputSinkListener(DistributedInputSinkManager *manager) { sinkManagerObj_ = manager; @@ -79,28 +74,6 @@ DistributedInputSinkManager::DInputSinkListener::~DInputSinkListener() DHLOGI("DInputSinkListener destory."); } -void DistributedInputSinkManager::AddWhiteList(nlohmann::json &jsonStr) -{ - TYPE_WHITE_LIST_VEC vecFilter; - std::string localNetworkId = GetLocalDeviceInfo().networkId; - if (!localNetworkId.empty()) { - WhiteListUtil::GetInstance().GetWhiteList(localNetworkId, vecFilter); - } else { - DHLOGE("query local network id from softbus failed"); - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - return; - } - - if (vecFilter.empty() || vecFilter[0].empty() || vecFilter[0][0].empty()) { - DHLOGE("onStartRemoteInput called, white list is null."); - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - return; - } - nlohmann::json filterMsg(vecFilter); - std::string object = filterMsg.dump(); - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object; -} - void DistributedInputSinkManager::DInputSinkListener::onPrepareRemoteInput( const int32_t& sessionId, const std::string &deviceId) { @@ -108,8 +81,41 @@ void DistributedInputSinkManager::DInputSinkListener::onPrepareRemoteInput( nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONPREPARE; + std::string smsg = ""; + int ret = DistributedInputCollector::GetInstance().Init( + DistributedInputSinkTransport::GetInstance().GetEventHandler()); + if (ret != DH_SUCCESS) { + DHLOGE("DInputSinkListener init InputCollector error."); + jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; + smsg = jsonStr.dump(); + DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); + return; + } + + DistributedInputSinkSwitch::GetInstance().AddSession(sessionId); + + // send prepare result and if result ok, send white list + TYPE_WHITE_LIST_VEC vecFilter; + std::string localNetworkId = GetLocalDeviceInfo().networkId; + if (!localNetworkId.empty()) { + WhiteListUtil::GetInstance().GetWhiteList(localNetworkId, vecFilter); + } else { + DHLOGE("query local network id from softbus failed"); + } + if (vecFilter.empty() || vecFilter[0].empty() || vecFilter[0][0].empty()) { + DHLOGE("onPrepareRemoteInput called, white list is null."); + jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true; + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; + smsg = jsonStr.dump(); + DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); + return; + } + nlohmann::json filterMsg(vecFilter); + std::string object = filterMsg.dump(); jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true; - std::string smsg = jsonStr.dump(); + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object; + smsg = jsonStr.dump(); DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); } @@ -117,6 +123,7 @@ void DistributedInputSinkManager::DInputSinkListener::onUnprepareRemoteInput(con { DHLOGI("onUnprepareRemoteInput called, sessionId: %d", sessionId); onStopRemoteInput(sessionId, static_cast(DInputDeviceType::ALL)); + DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONUNPREPARE; @@ -128,23 +135,56 @@ void DistributedInputSinkManager::DInputSinkListener::onUnprepareRemoteInput(con void DistributedInputSinkManager::DInputSinkListener::onRelayPrepareRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId) { - DHLOGI("onRelayPrepareRemoteInput called, toSinkSessionId: %d, devId: %s", toSinkSessionId, - GetAnonyString(deviceId).c_str()); + DHLOGI("toSinkSessionId: %s, devId: %s", GetAnonyInt32(toSinkSessionId).c_str(), GetAnonyString(deviceId).c_str()); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_PREPARE; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; + std::string smsg = ""; + int ret = DistributedInputCollector::GetInstance().Init( + DistributedInputSinkTransport::GetInstance().GetEventHandler()); + if (ret != DH_SUCCESS) { + DHLOGE("DInputSinkListener init InputCollector error."); + jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; + smsg = jsonStr.dump(); + DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(toSinkSessionId, smsg); + return; + } + + DistributedInputSinkSwitch::GetInstance().AddSession(toSinkSessionId); + + // send prepare result and if result ok, send white list + TYPE_WHITE_LIST_VEC vecFilter; + std::string localNetworkId = GetLocalNetworkId(); + if (!localNetworkId.empty()) { + WhiteListUtil::GetInstance().GetWhiteList(localNetworkId, vecFilter); + } else { + DHLOGE("query local network id from softbus failed"); + } + if (vecFilter.empty() || vecFilter[0].empty() || vecFilter[0][0].empty()) { + DHLOGE("white list is null."); + jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true; + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; + smsg = jsonStr.dump(); + DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(toSinkSessionId, smsg); + return; + } + nlohmann::json filterMsg(vecFilter); + std::string object = filterMsg.dump(); jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true; - std::string smsg = jsonStr.dump(); + jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object; + smsg = jsonStr.dump(); DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(toSinkSessionId, smsg); } void DistributedInputSinkManager::DInputSinkListener::onRelayUnprepareRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId) { - DHLOGI("onRelayUnprepareRemoteInput called, toSinkSessionId: %d, devId: %s", toSinkSessionId, - GetAnonyString(deviceId).c_str()); - onStopRemoteInput(toSrcSessionId, static_cast(DInputDeviceType::ALL)); + DHLOGI("toSinkSessionId: %d, devId: %s", toSinkSessionId, GetAnonyString(deviceId).c_str()); + DistributedInputCollector::GetInstance().SetSharingTypes(false, static_cast(DInputDeviceType::ALL)); + DistributedInputSinkSwitch::GetInstance().StopAllSwitch(); + DistributedInputSinkSwitch::GetInstance().RemoveSession(toSinkSessionId); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_UNPREPARE; @@ -157,57 +197,41 @@ void DistributedInputSinkManager::DInputSinkListener::onRelayUnprepareRemoteInpu void DistributedInputSinkManager::DInputSinkListener::onStartRemoteInput( const int32_t& sessionId, const uint32_t& inputTypes) { - DHLOGI("onStartRemoteInput called, sessionId: %d, inputTypes: %u.", sessionId, inputTypes); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - - int32_t ret = DistributedInputCollector::GetInstance().Init( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); - nlohmann::json jsonStrSta; - jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONSTART; - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStrSta[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); - return; - } - DistributedInputSinkManager::GetInstance().AddWhiteList(jsonStrSta); - DistributedInputSinkSwitch::GetInstance().AddSession(sessionId); + int32_t curSessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); + DHLOGI("onStartRemoteInput called, cursessionId: %d, new sessionId: %d", curSessionId, sessionId); + // set new session int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId); + sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE : DInputServerType::NULL_SERVER_TYPE); - bool result = (startRes == DH_SUCCESS); + + bool result = (startRes == DH_SUCCESS) ? true : false; + nlohmann::json jsonStrSta; + jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ONSTART; jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); + std::string smsgSta = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsgSta); - if (startRes != DH_SUCCESS) { - DHLOGE("onStartRemoteInput startSwitch error."); - return; + // add the input type + if (startRes == DH_SUCCESS) { + sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true, + sinkManagerObj_->GetInputTypes()); + sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } - sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true, - sinkManagerObj_->GetInputTypes()); - sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast(DInputDeviceType::MOUSE)) != 0; - if (!isMouse) { - return; - } - std::map deviceInfos; - DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), - deviceInfos); - for (auto deviceInfo : deviceInfos) { - DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, - deviceInfo.second).detach(); + if (isMouse) { + std::map deviceInfos; + DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), + deviceInfos); + for (auto deviceInfo : deviceInfos) { + DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, + deviceInfo.second).detach(); + } } } @@ -216,10 +240,6 @@ void DistributedInputSinkManager::DInputSinkListener::onStopRemoteInput( { DHLOGI("onStopRemoteInput called, sessionId: %d, inputTypes: %d, curInputTypes: %d", sessionId, inputTypes, sinkManagerObj_->GetInputTypes()); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() - (sinkManagerObj_->GetInputTypes() & inputTypes)); @@ -246,63 +266,48 @@ void DistributedInputSinkManager::DInputSinkListener::onStopRemoteInput( sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE); } } - DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId); } void DistributedInputSinkManager::DInputSinkListener::onStartRemoteInputDhid(const int32_t &sessionId, const std::string &strDhids) { - DHLOGI("onStartRemoteInputDhid called, : sessionId: %d", sessionId); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - - int32_t ret = DistributedInputCollector::GetInstance().Init( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); + int32_t curSessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); + DHLOGE("onStartRemoteInputDhid called, cursessionId: %d, new sessionId: %d", + curSessionId, sessionId); + // set new session + int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId); + bool result = (startRes == DH_SUCCESS) ? true : false; nlohmann::json jsonStrSta; jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_DHID_ONSTART; - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStrSta[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); - return; - } - - DistributedInputSinkManager::GetInstance().AddWhiteList(jsonStrSta); - DistributedInputSinkSwitch::GetInstance().AddSession(sessionId); - int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId); - bool result = (startRes == DH_SUCCESS); jsonStrSta[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids; jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); + std::string smsgSta = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsgSta); if (startRes != DH_SUCCESS) { DHLOGE("onStartRemoteInputDhid StartSwitch error."); return; } - std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, strDhids).detach(); + if (curSessionId == ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) { + DHLOGW("onStartRemoteInputDhid called, this is the only session."); + } - std::vector vecStr; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); - sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, strDhids).detach(); + // add the dhids + if (startRes == DH_SUCCESS) { + std::vector vecStr; + StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + } } void DistributedInputSinkManager::DInputSinkListener::onStopRemoteInputDhid(const int32_t &sessionId, const std::string &strDhids) { DHLOGI("onStopRemoteInputDhid called, sessionId: %d", sessionId); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); @@ -330,69 +335,54 @@ void DistributedInputSinkManager::DInputSinkListener::onStopRemoteInputDhid(cons sinkManagerObj_->SetInputTypes(static_cast(DInputDeviceType::NONE)); if (DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession() == ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) { - DHLOGI("onStartRemoteInput called, all session is stop."); + DHLOGI("all session is stop."); sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE); } } - DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId); } void DistributedInputSinkManager::DInputSinkListener::onRelayStartDhidRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId, const std::string &strDhids) { - DHLOGI("onRelayStartDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - - int32_t ret = DistributedInputCollector::GetInstance().Init( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); + int32_t curSessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); + DHLOGI("curSessionId:%s, new sessionId: %s", GetAnonyInt32(curSessionId).c_str(), + GetAnonyInt32(toSinkSessionId).c_str()); + // set new session + int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId); + bool result = (startRes == DH_SUCCESS) ? true : false; nlohmann::json jsonStrSta; jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STARTDHID; - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStrSta[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg); - return; - } - - DistributedInputSinkManager::GetInstance().AddWhiteList(jsonStrSta); - DistributedInputSinkSwitch::GetInstance().AddSession(toSinkSessionId); - int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId); - bool result = (startRes == DH_SUCCESS); jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; jsonStrSta[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids; jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg); + std::string smsgSta = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsgSta); if (startRes != DH_SUCCESS) { - DHLOGE("onRelayStartDhidRemoteInput startSwitch error."); + DHLOGE("StartSwitch error."); return; } + if (curSessionId == ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) { + DHLOGW("this is the only session."); + } std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, toSinkSessionId, strDhids).detach(); - std::vector vecStr; - StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); - sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + // add the dhids + if (startRes == DH_SUCCESS) { + std::vector vecStr; + StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + } } void DistributedInputSinkManager::DInputSinkListener::onRelayStopDhidRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId, const std::string &strDhids) { - DHLOGI("onRelayStopDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - + DHLOGI("toSinkSessionId: %d", toSinkSessionId); std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); @@ -425,75 +415,55 @@ void DistributedInputSinkManager::DInputSinkListener::onRelayStopDhidRemoteInput sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE); } } - DistributedInputSinkSwitch::GetInstance().RemoveSession(toSinkSessionId); } void DistributedInputSinkManager::DInputSinkListener::onRelayStartTypeRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId, uint32_t inputTypes) { - DHLOGI("onRelayStartTypeRemoteInput called, toSinkSessionId: %d", toSinkSessionId); - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - - int32_t ret = DistributedInputCollector::GetInstance().Init( - DistributedInputSinkTransport::GetInstance().GetEventHandler()); - nlohmann::json jsonStrSta; - jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STARTTYPE; - if (ret != DH_SUCCESS) { - DHLOGE("DInputSinkListener init InputCollector error."); - jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = false; - jsonStrSta[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg); - return; - } - - DistributedInputSinkManager::GetInstance().AddWhiteList(jsonStrSta); - DistributedInputSinkSwitch::GetInstance().AddSession(toSinkSessionId); + int32_t curSessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); + DHLOGI("curSessionId:%s, new sessionId: %s", + GetAnonyInt32(curSessionId).c_str(), GetAnonyInt32(toSinkSessionId).c_str()); + // set new session int32_t startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId); + sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE : DInputServerType::NULL_SERVER_TYPE); - bool result = (startRes == DH_SUCCESS); + + bool result = (startRes == DH_SUCCESS) ? true : false; + nlohmann::json jsonStrSta; + jsonStrSta[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_STARTTYPE; jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; - std::string smsg = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg); + std::string smsgSta = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsgSta); if (startRes != DH_SUCCESS) { + DHLOGE("StartSwitch error."); return; } - sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true, - sinkManagerObj_->GetInputTypes()); - sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); + if (sinkManagerObj_->GetEventHandler() == nullptr) { + DHLOGE("eventhandler is null."); + return; + } bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast(DInputDeviceType::MOUSE)) != 0; - if (!isMouse) { - return; - } - std::map deviceInfos; - DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), - deviceInfos); - for (auto deviceInfo : deviceInfos) { - DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, toSinkSessionId, - deviceInfo.second).detach(); + if (isMouse) { + std::map deviceInfos; + DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), + deviceInfos); + for (auto deviceInfo : deviceInfos) { + DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, toSinkSessionId, + deviceInfo.second).detach(); + } } } void DistributedInputSinkManager::DInputSinkListener::onRelayStopTypeRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId, uint32_t inputTypes) { - if (sinkManagerObj_ == nullptr) { - DHLOGE("sinkManagerObj is null."); - return; - } - DHLOGI("onStopRemoteInput called, sessionId: %d, inputTypes: %d, curInputTypes: %d", toSinkSessionId, inputTypes, sinkManagerObj_->GetInputTypes()); @@ -523,7 +493,6 @@ void DistributedInputSinkManager::DInputSinkListener::onRelayStopTypeRemoteInput sinkManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE); } } - DistributedInputSinkSwitch::GetInstance().RemoveSession(toSinkSessionId); } void DistributedInputSinkManager::DInputSinkListener::StringSplit(const std::string &str, const char split, @@ -1074,7 +1043,7 @@ int32_t DistributedInputSinkManager::Dump(int32_t fd, const std::vector callback); @@ -59,6 +48,10 @@ public: int32_t RespLatency(const int32_t sessionId, std::string &smsg); void SendKeyStateNodeMsg(const int32_t sessionId, const std::string& dhId, const uint32_t btnCode); + int32_t OnSessionOpened(int32_t sessionId, int32_t result); + void OnSessionClosed(int32_t sessionId); + void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); + class DInputSinkEventHandler : public AppExecFwk::EventHandler { public: DInputSinkEventHandler(const std::shared_ptr &runner); @@ -70,12 +63,12 @@ public: std::shared_ptr GetEventHandler(); void CloseAllSession(); + int32_t GetSessionIdByNetId(const std::string &srcId); + void GetDeviceIdBySessionId(int32_t sessionId, std::string &srcId); private: int32_t SendMessage(int32_t sessionId, std::string &message); - void HandleSessionData(int32_t sessionId, const std::string& message); - void HandleData(int32_t sessionId, const std::string& message); - void RegRespFunMap(); + void HandleSessionData(int32_t sessionId, const std::string& messageData); void NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void NotifyUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void NotifyStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); @@ -91,14 +84,11 @@ private: void NotifyRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); private: + std::map sessionDevMap_; // source networkId, sessionId std::string mySessionName_; - std::shared_ptr eventHandler_; - std::shared_ptr statuslistener_; - std::shared_ptr callback_; - using SinkTransportFunc = void (DistributedInputSinkTransport::*)(int32_t sessionId, - const nlohmann::json &recMsg); - std::map memberFuncMap_; + std::shared_ptr eventHandler_; + std::shared_ptr callback_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index 7458d00..1dac7b4 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -32,12 +32,10 @@ #include "session.h" #include "softbus_bus_center.h" -#include "distributed_input_transport_base.h" - namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -DistributedInputSinkTransport::DistributedInputSinkTransport() : mySessionName_("") +DistributedInputSinkTransport::DistributedInputSinkTransport() : sessionDevMap_({}), mySessionName_("") { std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); eventHandler_ = std::make_shared(runner); @@ -47,6 +45,7 @@ DistributedInputSinkTransport::DistributedInputSinkTransport() : mySessionName_( DistributedInputSinkTransport::~DistributedInputSinkTransport() { DHLOGI("~DistributedInputSinkTransport"); + sessionDevMap_.clear(); (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); } @@ -55,6 +54,38 @@ DistributedInputSinkTransport::DInputSinkEventHandler::DInputSinkEventHandler( { } +static int32_t SessionOpened(int32_t sessionId, int32_t result) +{ + return DistributedInput::DistributedInputSinkTransport::GetInstance().OnSessionOpened(sessionId, result); +} + +static void SessionClosed(int32_t sessionId) +{ + DistributedInput::DistributedInputSinkTransport::GetInstance().OnSessionClosed(sessionId); +} + +static void BytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + DistributedInput::DistributedInputSinkTransport::GetInstance().OnBytesReceived(sessionId, data, dataLen); +} + +static void MessageReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + (void)sessionId; + (void)data; + (void)dataLen; + DHLOGI("sessionId: %d, dataLen:%d", sessionId, dataLen); +} + +static void StreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) +{ + (void)sessionId; + (void)data; + (void)ext; + (void)param; + DHLOGI("sessionId: %d", sessionId); +} DistributedInputSinkTransport &DistributedInputSinkTransport::GetInstance() { static DistributedInputSinkTransport instance; @@ -68,10 +99,10 @@ void DistributedInputSinkTransport::DInputSinkEventHandler::ProcessEvent(const A switch (eventId) { case EHandlerMsgType::DINPUT_SINK_EVENT_HANDLER_MSG: { std::shared_ptr innerMsg = event->GetSharedObject(); - nlohmann::json jsonStr; - jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_BODY_DATA; - jsonStr[DINPUT_SOFTBUS_KEY_INPUT_DATA] = innerMsg->dump(); - std::string smsg = jsonStr.dump(); + nlohmann::json sendMsg; + sendMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_BODY_DATA; + sendMsg[DINPUT_SOFTBUS_KEY_INPUT_DATA] = innerMsg->dump(); + std::string smsg = sendMsg.dump(); RecordEventLog(innerMsg); int32_t sessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); if (sessionId > 0) { @@ -89,18 +120,30 @@ void DistributedInputSinkTransport::DInputSinkEventHandler::ProcessEvent(const A int32_t DistributedInputSinkTransport::Init() { - DHLOGI("Init Sink Transport"); - - int32_t ret = DistributedInputTransportBase::GetInstance().Init(); - if (ret != DH_SUCCESS) { - DHLOGE("Init Sink Transport Failed"); - return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; + // 1.create session + DHLOGI("Init"); + ISessionListener iSessionListener = { + .OnSessionOpened = SessionOpened, + .OnSessionClosed = SessionClosed, + .OnBytesReceived = BytesReceived, + .OnMessageReceived = MessageReceived, + .OnStreamReceived = StreamReceived + }; + auto localNode = std::make_unique(); + int32_t retCode = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), localNode.get()); + if (retCode != DH_SUCCESS) { + DHLOGE("Init could not get local device id."); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_INIT_FAIL; } + std::string networkId = localNode->networkId; + DHLOGI("Init device networkId is %s", GetAnonyString(networkId).c_str()); + mySessionName_ = SESSION_NAME_SINK + networkId.substr(0, INTERCEPT_STRING_LENGTH); - statuslistener_ = std::make_shared(this); - DistributedInputTransportBase::GetInstance().RegisterSinkHandleSessionCallback(statuslistener_); - - RegRespFunMap(); + int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + if (ret != DH_SUCCESS) { + DHLOGE("Init CreateSessionServer failed, error code %d.", ret); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_INIT_FAIL; + } return DH_SUCCESS; } @@ -133,8 +176,7 @@ int32_t DistributedInputSinkTransport::RespPrepareRemoteInput( } } -int32_t DistributedInputSinkTransport::RespUnprepareRemoteInput( - const int32_t sessionId, std::string &smsg) +int32_t DistributedInputSinkTransport::RespUnprepareRemoteInput(const int32_t sessionId, std::string &smsg) { if (sessionId > 0) { DHLOGI("RespUnprepareRemoteInput sessionId: %d, smsg:%s.", sessionId, SetAnonyId(smsg).c_str()); @@ -150,8 +192,7 @@ int32_t DistributedInputSinkTransport::RespUnprepareRemoteInput( } } -int32_t DistributedInputSinkTransport::RespStartRemoteInput( - const int32_t sessionId, std::string &smsg) +int32_t DistributedInputSinkTransport::RespStartRemoteInput(const int32_t sessionId, std::string &smsg) { if (sessionId > 0) { DHLOGI("RespStartRemoteInput sessionId: %d, smsg:%s.", sessionId, SetAnonyId(smsg).c_str()); @@ -223,20 +264,141 @@ void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, int32_t DistributedInputSinkTransport::SendMessage(int32_t sessionId, std::string &message) { - return DistributedInputTransportBase::GetInstance().SendMsg(sessionId, message); + DHLOGD("start SendMessage"); + if (message.size() > MSG_MAX_SIZE) { + DHLOGE("SendMessage error: message.size() > MSG_MAX_SIZE"); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_SENDMESSAGE_FAIL; + } + uint8_t *buf = reinterpret_cast(calloc((MSG_MAX_SIZE), sizeof(uint8_t))); + if (buf == nullptr) { + DHLOGE("SendMessage: malloc memory failed"); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_SENDMESSAGE_FAIL; + } + int32_t outLen = 0; + if (memcpy_s(buf, MSG_MAX_SIZE, reinterpret_cast(message.c_str()), message.size()) != EOK) { + DHLOGE("SendMessage: memcpy memory failed"); + free(buf); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_SENDMESSAGE_FAIL; + } + outLen = static_cast(message.size()); + int32_t ret = SendBytes(sessionId, buf, outLen); + free(buf); + return ret; } -DistributedInputSinkTransport::DInputTransbaseSinkListener::DInputTransbaseSinkListener( - DistributedInputSinkTransport *transport) +int32_t DistributedInputSinkTransport::GetSessionIdByNetId(const std::string &srcId) { - sinkTransportObj_ = transport; - DHLOGI("DInputTransbaseSinkListener init."); + std::map::iterator it = sessionDevMap_.find(srcId); + if (it != sessionDevMap_.end()) { + return it->second; + } + DHLOGE("get session id failed, srcId = %s", GetAnonyString(srcId).c_str()); + return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_GET_SESSIONID_FAIL; } -DistributedInputSinkTransport::DInputTransbaseSinkListener::~DInputTransbaseSinkListener() +void DistributedInputSinkTransport::GetDeviceIdBySessionId(int32_t sessionId, std::string &srcId) { - sinkTransportObj_ = nullptr; - DHLOGI("DInputTransbaseSinkListener destory."); + for (auto iter = sessionDevMap_.begin(); iter != sessionDevMap_.end(); iter++) { + if (sessionId == iter->second) { + srcId = iter->first; + return; + } + } + srcId = ""; +} + +int32_t DistributedInputSinkTransport::OnSessionOpened(int32_t sessionId, int32_t result) +{ + if (result != DH_SUCCESS) { + DHLOGE("session open failed, sessionId: %d", sessionId); + return DH_SUCCESS; + } + +#ifdef DINPUT_LOW_LATENCY + DInputLowLatency::GetInstance().EnableSinkLowLatency(); +#endif + + // return 1 is client + int32_t sessionSide = GetSessionSide(sessionId); + DHLOGI("session open succeed, sessionId: %d, sessionSide %d", sessionId, sessionSide); + + char mySessionName[SESSION_NAME_SIZE_MAX] = ""; + char peerSessionName[SESSION_NAME_SIZE_MAX] = ""; + char peerDevId[DEVICE_ID_SIZE_MAX] = ""; + int ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName)); + if (ret != DH_SUCCESS) { + DHLOGE("get my session name failed, session id is %d", sessionId); + } + // get other device session name + ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName)); + if (ret != DH_SUCCESS) { + DHLOGE("get my peer session name failed, session id is %d", sessionId); + } + + ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); + if (ret != DH_SUCCESS) { + DHLOGE("get my peer device id failed, session id is %d", sessionId); + } else { + sessionDevMap_[peerDevId] = sessionId; + } + DHLOGI("mySessionName:%s, peerSessionName:%s, peerDevId:%s", + mySessionName, peerSessionName, GetAnonyString(peerDevId).c_str()); + HiDumper::GetInstance().CreateSessionInfo(std::string(peerDevId), sessionId, mySessionName, peerSessionName, + SessionStatus::OPENED); + return DH_SUCCESS; +} + +void DistributedInputSinkTransport::OnSessionClosed(int32_t sessionId) +{ + DHLOGI("OnSessionClosed, sessionId: %d", sessionId); + +#ifdef DINPUT_LOW_LATENCY + DInputLowLatency::GetInstance().DisableSinkLowLatency(); +#endif + + char peerDevId[DEVICE_ID_SIZE_MAX] = ""; + int ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); + if (ret != DH_SUCCESS) { + DHLOGI("get my peer device id failed, session id is %d", sessionId); + } + for (auto iter = sessionDevMap_.begin(); iter != sessionDevMap_.end(); iter++) { + if (iter->second == sessionId) { + sessionDevMap_.erase(iter); + break; + } + } + DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId); + HiDumper::GetInstance().SetSessionStatus(std::string(peerDevId), SessionStatus::CLOSED); + HiDumper::GetInstance().DeleteSessionInfo(std::string(peerDevId)); +} + +void DistributedInputSinkTransport::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + DHLOGI("OnBytesReceived, sessionId: %d, dataLen:%d", sessionId, dataLen); + if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > MSG_MAX_SIZE) { + DHLOGE("OnBytesReceived param check failed"); + return; + } + + uint8_t *buf = reinterpret_cast(calloc(dataLen + 1, sizeof(uint8_t))); + if (buf == nullptr) { + DHLOGE("OnBytesReceived: malloc memory failed"); + return; + } + + if (memcpy_s(buf, dataLen + 1, reinterpret_cast(data), dataLen) != EOK) { + DHLOGE("OnBytesReceived: memcpy memory failed"); + free(buf); + return; + } + + std::string message(buf, buf + dataLen); + DHLOGI("OnBytesReceived message:%s.", SetAnonyId(message).c_str()); + HandleSessionData(sessionId, message); + + free(buf); + DHLOGI("OnBytesReceived completed"); + return; } void DistributedInputSinkTransport::NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) @@ -468,35 +630,7 @@ void DistributedInputSinkTransport::NotifyRelayStopTypeRemoteInput(int32_t sessi callback_->onRelayStopTypeRemoteInput(toSrcSessionId, sessionId, deviceId, inputTypes); } -void DistributedInputSinkTransport::DInputTransbaseSinkListener::HandleSessionData(int32_t sessionId, - const std::string& message) -{ - DistributedInputSinkTransport::GetInstance().HandleData(sessionId, message); -} - -void DistributedInputSinkTransport::RegRespFunMap() -{ - memberFuncMap_[TRANS_SOURCE_MSG_PREPARE] = &DistributedInputSinkTransport::NotifyPrepareRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_UNPREPARE] = &DistributedInputSinkTransport::NotifyUnprepareRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_START_TYPE] = &DistributedInputSinkTransport::NotifyStartRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_STOP_TYPE] = &DistributedInputSinkTransport::NotifyStopRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_LATENCY] = &DistributedInputSinkTransport::NotifyLatency; - memberFuncMap_[TRANS_SOURCE_MSG_START_DHID] = &DistributedInputSinkTransport::NotifyStartRemoteInputDhid; - memberFuncMap_[TRANS_SOURCE_MSG_STOP_DHID] = &DistributedInputSinkTransport::NotifyStopRemoteInputDhid; - memberFuncMap_[TRANS_SOURCE_MSG_PREPARE_FOR_REL] = &DistributedInputSinkTransport::NotifyRelayPrepareRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_UNPREPARE_FOR_REL] = - &DistributedInputSinkTransport::NotifyRelayUnprepareRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_START_DHID_FOR_REL] = - &DistributedInputSinkTransport::NotifyRelayStartDhidRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_STOP_DHID_FOR_REL] = - &DistributedInputSinkTransport::NotifyRelayStopDhidRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_START_TYPE_FOR_REL] = - &DistributedInputSinkTransport::NotifyRelayStartTypeRemoteInput; - memberFuncMap_[TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL] = - &DistributedInputSinkTransport::NotifyRelayStopTypeRemoteInput; -} - -void DistributedInputSinkTransport::HandleData(int32_t sessionId, const std::string& message) +void DistributedInputSinkTransport::HandleSessionData(int32_t sessionId, const std::string& message) { if (callback_ == nullptr) { DHLOGE("OnBytesReceived the callback_ is null, the message:%s abort.", SetAnonyId(message).c_str()); @@ -504,14 +638,79 @@ void DistributedInputSinkTransport::HandleData(int32_t sessionId, const std::str } nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); - uint32_t cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; - auto iter = memberFuncMap_.find(cmdType); - if (iter == memberFuncMap_.end()) { - DHLOGE("OnBytesReceived cmdType %d is undefined.", cmdType); + if (recMsg.is_discarded()) { + DHLOGE("OnBytesReceived jsonStr error."); return; } - SinkTransportFunc &func = iter->second; - (this->*func)(sessionId, recMsg); + + if (recMsg.contains(DINPUT_SOFTBUS_KEY_CMD_TYPE) != true) { + DHLOGE("OnBytesReceived message:%s is error, not contain cmdType.", SetAnonyId(message).c_str()); + return; + } + + if (recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE].is_number() != true) { + DHLOGE("OnBytesReceived cmdType is not number type."); + return; + } + + int cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; + switch (cmdType) { + case TRANS_SOURCE_MSG_PREPARE: { + NotifyPrepareRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_UNPREPARE: { + NotifyUnprepareRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_START_TYPE: { + NotifyStartRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_STOP_TYPE: { + NotifyStopRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_LATENCY: { + NotifyLatency(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_START_DHID: { + NotifyStartRemoteInputDhid(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_STOP_DHID: { + NotifyStopRemoteInputDhid(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_PREPARE_FOR_REL: { + NotifyRelayPrepareRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_UNPREPARE_FOR_REL: { + NotifyRelayUnprepareRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_START_DHID_FOR_REL: { + NotifyRelayStartDhidRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_STOP_DHID_FOR_REL: { + NotifyRelayStopDhidRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_START_TYPE_FOR_REL: { + NotifyRelayStartTypeRemoteInput(sessionId, recMsg); + break; + } + case TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL: { + NotifyRelayStopTypeRemoteInput(sessionId, recMsg); + break; + } + default: + DHLOGE("OnBytesReceived cmdType is undefined."); + break; + } } void DistributedInputSinkTransport::CloseAllSession() diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 4f0c1cf..62c77a1 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -22,7 +22,6 @@ ohos_shared_library("libdinput_inject") { "${frameworks_path}/include", "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", - "${distributedinput_path}/inputdevicehandler/include", "${fwk_common_path}/log/include", "${fwk_common_path}/utils/include", "${fwk_utils_path}/include/log", diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index acf9f96..5644541 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -63,7 +63,6 @@ private: /* the key is dhId, and the value is virtualDevice */ std::map> virtualDeviceMap_; std::mutex virtualDeviceMapMutex_; - std::atomic isInjectThreadCreated_; std::atomic isInjectThreadRunning_; std::mutex operationMutex_; std::thread eventInjectThread_; diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 0b78cca..93841d0 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -38,15 +38,14 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), - isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) +DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadRunning_(false), + inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { } DistributedInputNodeManager::~DistributedInputNodeManager() { DHLOGI("destructor start"); - isInjectThreadCreated_.store(false); isInjectThreadRunning_.store(false); if (eventInjectThread_.joinable()) { eventInjectThread_.join(); @@ -180,37 +179,30 @@ int32_t DistributedInputNodeManager::getDevice(const std::string& dhId, VirtualD { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.find(dhId); - if (iter == virtualDeviceMap_.end()) { - return ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL; + if (iter != virtualDeviceMap_.end()) { + device = iter->second.get(); + return DH_SUCCESS; } - device = iter->second.get(); - return DH_SUCCESS; + return ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL; } void DistributedInputNodeManager::StartInjectThread() { - if (isInjectThreadCreated_.load()) { - DHLOGI("InjectThread has been created."); - return; - } - DHLOGI("InjectThread does not created"); - isInjectThreadCreated_.store(true); + DHLOGI("start"); isInjectThreadRunning_.store(true); eventInjectThread_ = std::thread(&DistributedInputNodeManager::InjectEvent, this); + DHLOGI("end"); } void DistributedInputNodeManager::StopInjectThread() { - if (!isInjectThreadCreated_.load()) { - DHLOGI("InjectThread does not created, and not need to stop."); - } - DHLOGI("InjectThread has been created, and soon will be stopped."); + DHLOGI("start"); isInjectThreadRunning_.store(false); - isInjectThreadCreated_.store(false); conditionVariable_.notify_all(); if (eventInjectThread_.joinable()) { eventInjectThread_.join(); } + DHLOGI("end"); } void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent) diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index 5770e8d..8bd8c52 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -29,7 +29,6 @@ #include "singleton.h" #include "system_ability.h" #include "system_ability_status_change_stub.h" -#include "input_check_param.h" #include "constants_dinput.h" #include "dinput_context.h" @@ -74,7 +73,6 @@ const std::string INPUT_SOURCEMANAGER_KEY_DHID = "dhids"; const std::string INPUT_SOURCEMANAGER_KEY_TYPE = "type"; const std::string INPUT_SOURCEMANAGER_KEY_CODE = "code"; const std::string INPUT_SOURCEMANAGER_KEY_VALUE = "value"; -const std::string INPUT_SOURCEMANAGER_KEY_FROM_START_DHID = "fromStartDhid"; const std::string INPUT_SOURCEMANAGER_KEY_RESULT = "result"; const std::string INPUT_SOURCEMANAGER_KEY_WHITELIST = "whitelist"; const std::string INPUT_SOURCEMANAGER_KEY_SRC_DEVID = "srcId"; @@ -190,21 +188,18 @@ public: DInputSourceListener(DistributedInputSourceManager *manager); virtual ~DInputSourceListener(); void onResponseRegisterDistributedHardware(const std::string deviceId, const std::string dhId, bool result); - void onResponsePrepareRemoteInput(const std::string deviceId, bool result); + void onResponsePrepareRemoteInput(const std::string deviceId, bool result, const std::string &object); void onResponseUnprepareRemoteInput(const std::string deviceId, bool result); - void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result, - const std::string &object); + void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result); void onResponseStopRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result); - void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result, - const std::string &object); + void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result); void onResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result); void onResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type, const uint32_t code, const uint32_t value); void onReceivedEventRemoteInput(const std::string deviceId, const std::string &event); - void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result); + void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result, + const std::string &object); void onResponseRelayUnprepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result); - void onResponseRelayStartDhidRemoteInput(const std::string &deviceId, const std::string &object); - void onResponseRelayStartTypeRemoteInput(const std::string &deviceId, const std::string &object); void onReceiveRelayPrepareResult(int32_t status, const std::string &srcId, const std::string &sinkId); void onReceiveRelayUnprepareResult(int32_t status, const std::string &srcId, const std::string &sinkId); @@ -309,16 +304,26 @@ public: }; public: - void RunRegisterCallback(const std::string& devId, const std::string& dhId, const int32_t& status); - void RunUnregisterCallback(const std::string& devId, const std::string& dhId, const int32_t& status); - void RunPrepareCallback(const std::string& devId, const int32_t& status); - void RunUnprepareCallback(const std::string& devId, const int32_t& status); - void RunStartCallback(const std::string& devId, const uint32_t& inputTypes, const int32_t& status, - const std::string& object); - void RunStopCallback(const std::string& devId, const uint32_t& inputTypes, const int32_t& status); + void RunRegisterCallback( + const std::string& devId, const std::string& dhId, const int32_t& status + ); + void RunUnregisterCallback( + const std::string& devId, const std::string& dhId, const int32_t& status + ); + void RunPrepareCallback( + const std::string& devId, const int32_t& status, const std::string& object + ); + void RunUnprepareCallback( + const std::string& devId, const int32_t& status + ); + void RunStartCallback( + const std::string& devId, const uint32_t& inputTypes, const int32_t& status + ); + void RunStopCallback( + const std::string& devId, const uint32_t& inputTypes, const int32_t& status + ); - void RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, const int32_t &status, - const std::string& object); + void RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, const int32_t &status); void RunStopDhidCallback(const std::string &sinkId, const std::string &dhIds, const int32_t &status); void RunKeyStateCallback(const std::string &sinkId, const std::string &dhId, const uint32_t type, const uint32_t code, const uint32_t value); diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index c4e1401..f78889d 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -44,6 +44,7 @@ #include "distributed_input_source_transport.h" #include "hisysevent_util.h" #include "hidumper.h" +#include "input_check_param.h" #include "white_list_util.h" namespace OHOS { @@ -82,7 +83,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRegisterDist DHLOGI("onResponseRegisterDistributedHardware called, deviceId: %s, " "result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseRegisterDistributedHardware sourceManagerObj is null."); + DHLOGE("onResponseRegisterDistributedHardware sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -105,25 +106,27 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRegisterDist } void DistributedInputSourceManager::DInputSourceListener::onResponsePrepareRemoteInput(const std::string deviceId, - bool result) + bool result, const std::string &object) { DHLOGI("onResponsePrepareRemoteInput called, deviceId: %s, result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponsePrepareRemoteInput sourceManagerObj is null."); + DHLOGE("onResponsePrepareRemoteInput sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { sourceManagerObj_->RunPrepareCallback(deviceId, - ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object); DHLOGE("onResponsePrepareRemoteInput GetCallbackEventHandler is null."); return; } std::shared_ptr jsonArrayMsg = std::make_shared(); + nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId; tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result; + tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( DINPUT_SOURCE_MANAGER_PREPARE_MSG, jsonArrayMsg, 0); @@ -137,7 +140,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseUnprepareRem "result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseUnprepareRemoteInput sourceManagerObj is null."); + DHLOGE("onResponseUnprepareRemoteInput sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -158,11 +161,11 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseUnprepareRem } void DistributedInputSourceManager::DInputSourceListener::onResponseRelayPrepareRemoteInput(int32_t toSrcSessionId, - const std::string &deviceId, bool result) + const std::string &deviceId, bool result, const std::string &object) { - DHLOGI("onResponseRelayPrepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); + DHLOGI("deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseRelayPrepareRemoteInput sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -173,6 +176,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRelayPrepare nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId; tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result; + tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object; tmpJson[INPUT_SOURCEMANAGER_KEY_SESSIONID] = toSrcSessionId; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( @@ -183,9 +187,9 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRelayPrepare void DistributedInputSourceManager::DInputSourceListener::onResponseRelayUnprepareRemoteInput(int32_t toSrcSessionId, const std::string &deviceId, bool result) { - DHLOGI("onResponseRelayUnprepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); + DHLOGI("deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseRelayUnprepareRemoteInput sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -203,41 +207,19 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRelayUnprepa sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); } -void DistributedInputSourceManager::DInputSourceListener::onResponseRelayStartDhidRemoteInput( - const std::string &deviceId, const std::string &object) -{ - DHLOGI("Device whitelist object: %s", object.c_str()); - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - sourceManagerObj_->RunWhiteListCallback(deviceId, object); -} - -void DistributedInputSourceManager::DInputSourceListener::onResponseRelayStartTypeRemoteInput( - const std::string &deviceId, const std::string &object) -{ - DHLOGI("Device whitelist object: %s", object.c_str()); - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - sourceManagerObj_->RunWhiteListCallback(deviceId, object); -} - void DistributedInputSourceManager::DInputSourceListener::onResponseStartRemoteInput( - const std::string deviceId, const uint32_t inputTypes, bool result, const std::string &object) + const std::string deviceId, const uint32_t inputTypes, bool result) { DHLOGI("onResponseStartRemoteInput called, deviceId: %s, inputTypes: %d, result: %s.", GetAnonyString(deviceId).c_str(), inputTypes, result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseStartRemoteInput sourceManagerObj is null."); + DHLOGE("onResponseStartRemoteInput sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { sourceManagerObj_->RunStartCallback(deviceId, inputTypes, - ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL); DHLOGE("onResponseStartRemoteInput GetCallbackEventHandler is null."); return; } @@ -249,7 +231,6 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStartRemoteI nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId; tmpJson[INPUT_SOURCEMANAGER_KEY_ITP] = inputTypes; - tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object; tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( @@ -264,7 +245,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStopRemoteIn GetAnonyString(deviceId).c_str(), inputTypes, result ? "true" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseStopRemoteInput sourceManagerObj is null."); + DHLOGE("onResponseStopRemoteInput sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -286,19 +267,19 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStopRemoteIn } void DistributedInputSourceManager::DInputSourceListener::onResponseStartRemoteInputDhid( - const std::string deviceId, const std::string &dhids, bool result, const std::string &object) + const std::string deviceId, const std::string &dhids, bool result) { DHLOGI("onResponseStartRemoteInputDhid called, deviceId: %s, result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseStartRemoteInputDhid sourceManagerObj is null."); + DHLOGE("onResponseStartRemoteInputDhid sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { DHLOGE("onResponseStartRemoteInputDhid GetCallbackEventHandler is null."); sourceManagerObj_->RunStartDhidCallback(deviceId, dhids, - ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL); return; } if (result) { @@ -309,7 +290,6 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStartRemoteI nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId; tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhids; - tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object; tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = @@ -324,7 +304,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStopRemoteIn GetAnonyString(deviceId).c_str(), result ? "success" : "failed"); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseStopRemoteInputDhid sourceManagerObj is null."); + DHLOGE("onResponseStopRemoteInputDhid sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -350,7 +330,7 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseKeyState(con DHLOGI("onResponseMouseDown called, deviceId: %s, dhid: %s.", GetAnonyString(deviceId).c_str(), GetAnonyString(dhid).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("onResponseMouseDown sourceManagerObj is null."); + DHLOGE("onResponseMouseDown sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -405,7 +385,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayPrepareR { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -428,7 +408,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayUnprepar { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -451,7 +431,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayStartDhi { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -475,7 +455,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayStopDhid { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -499,7 +479,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayStartTyp { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -523,7 +503,7 @@ void DistributedInputSourceManager::DInputSourceListener::onReceiveRelayStopType { DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); + DHLOGE("sourceManagerObj_ is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -628,11 +608,6 @@ DistributedInputSourceManager::DInputSourceManagerEventHandler::~DInputSourceMan void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRegisterCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -660,11 +635,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRegis void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnregisterCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -681,29 +651,20 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnreg void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyPrepareCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; + std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST]; sourceManagerObj_->RunPrepareCallback(deviceId, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_MSG_IS_BAD); + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_MSG_IS_BAD, object); } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnprepareCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -719,17 +680,11 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnpre void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID]; uint32_t inputTypes = innerMsg[INPUT_SOURCEMANAGER_KEY_ITP]; - std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; DHLOGI("Start DInput Recv Callback ret: %s, devId: %s, inputTypes: %d", result ? "true" : "false", GetAnonyString(deviceId).c_str(), inputTypes); @@ -740,17 +695,12 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStart sourceManagerObj_->SetStartTransFlag((result && (sourceManagerObj_->GetInputTypesMap(deviceId) > 0)) ? DInputServerType::SOURCE_SERVER_TYPE : DInputServerType::NULL_SERVER_TYPE); sourceManagerObj_->RunStartCallback(deviceId, inputTypes, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, object); + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD); } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -783,11 +733,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopC void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartDhidCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - DHLOGI("ProcessEvent NotifyStartDhidCallback()"); std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); @@ -795,20 +740,16 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStart std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID]; std::string dhidStr = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; - std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST]; - sourceManagerObj_->RunStartDhidCallback(deviceId, dhidStr, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, object); + if (sourceManagerObj_ != nullptr) { + sourceManagerObj_->RunStartDhidCallback(deviceId, dhidStr, + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD); + } } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopDhidCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - DHLOGI("ProcessEvent NotifyStopDhidCallback()"); std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); @@ -817,18 +758,15 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopD std::string dhidStr = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; - sourceManagerObj_->RunStopDhidCallback(deviceId, dhidStr, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_MSG_IS_BAD); + if (sourceManagerObj_ != nullptr) { + sourceManagerObj_->RunStopDhidCallback(deviceId, dhidStr, + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_MSG_IS_BAD); + } } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyKeyStateCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - DHLOGI("ProcessEvent NotifyKeyStateCallback()"); std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); @@ -838,24 +776,19 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyKeySt uint32_t keyType = innerMsg[INPUT_SOURCEMANAGER_KEY_TYPE]; uint32_t keyCode = innerMsg[INPUT_SOURCEMANAGER_KEY_CODE]; uint32_t keyValue = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE]; - - sourceManagerObj_->RunKeyStateCallback(deviceId, dhid, keyType, keyCode, keyValue); + if (sourceManagerObj_ != nullptr) { + sourceManagerObj_->RunKeyStateCallback(deviceId, dhid, keyType, keyCode, keyValue); + } } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartServerCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); int32_t serType = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; DInputServerType startTransFlag = DInputServerType(serType); - sourceManagerObj_->SetStartTransFlag(startTransFlag); } @@ -863,11 +796,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStart void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayPrepareCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -881,11 +809,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayUnprepareCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -904,7 +827,9 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay nlohmann::json innerMsg = *(it); std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; + std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST]; int32_t toSrcSessionId = innerMsg[INPUT_SOURCEMANAGER_KEY_SESSIONID]; + DHLOGI("Device whitelist object: %s", object.c_str()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { return; @@ -917,6 +842,7 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_TO_ORIGIN notify is fail."); return; } + sourceManagerObj_->RunWhiteListCallback(deviceId, object); } void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayUnprepareRemoteInput( @@ -945,11 +871,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStartDhidCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -964,11 +885,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStopDhidCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -983,11 +899,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStartTypeCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -1002,11 +913,6 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelay void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStopTypeCallback( const AppExecFwk::InnerEvent::Pointer &event) { - if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj is null."); - return; - } - std::shared_ptr dataMsg = event->GetSharedObject(); nlohmann::json::iterator it = dataMsg->begin(); nlohmann::json innerMsg = *(it); @@ -1678,7 +1584,7 @@ int32_t DistributedInputSourceManager::RelayStartRemoteInputByType(const std::st int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStartTypeRequest(srcId, sinkId, inputTypes); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, start fail."); - for (auto iter = relayStaTypeCallbacks_.begin(); iter != relayStaTypeCallbacks_.end(); ++iter) { + for (auto iter = relayStaTypeCallbacks_.begin(); iter != relayStaTypeCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) { relayStaTypeCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; @@ -1707,7 +1613,7 @@ int32_t DistributedInputSourceManager::RelayStopRemoteInputByType( int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStopTypeRequest(srcId, sinkId, inputTypes); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, start fail."); - for (auto iter = relayStpTypeCallbacks_.begin(); iter != relayStpTypeCallbacks_.end(); ++iter) { + for (auto iter = relayStpTypeCallbacks_.begin(); iter != relayStpTypeCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) { relayStpTypeCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; @@ -2132,7 +2038,7 @@ int32_t DistributedInputSourceManager::RelayPrepareRemoteInput(const std::string } } - int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(srcId); + int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbusForRelay(srcId); if (ret != DH_SUCCESS) { DHLOGE("Open softbus session fail."); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; @@ -2143,7 +2049,7 @@ int32_t DistributedInputSourceManager::RelayPrepareRemoteInput(const std::string ret = DistributedInputSourceTransport::GetInstance().SendRelayPrepareRequest(srcId, sinkId); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, prepare fail."); - for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); ++iter) { + for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId) { relayPreCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; @@ -2172,7 +2078,7 @@ int32_t DistributedInputSourceManager::RelayUnprepareRemoteInput(const std::stri int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayUnprepareRequest(srcId, sinkId); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, prepare fail."); - for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); ++iter) { + for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId) { relayUnpreCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL; @@ -2201,7 +2107,7 @@ int32_t DistributedInputSourceManager::RelayStartRemoteInputByDhid(const std::st int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStartDhidRequest(srcId, sinkId, dhIds); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, start fail."); - for (auto iter = relayStaDhidCallbacks_.begin(); iter != relayStaDhidCallbacks_.end(); ++iter) { + for (auto iter = relayStaDhidCallbacks_.begin(); iter != relayStaDhidCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) { relayStaDhidCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; @@ -2230,7 +2136,7 @@ int32_t DistributedInputSourceManager::RelayStopRemoteInputByDhid(const std::str int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStopDhidRequest(srcId, sinkId, dhIds); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, stop fail."); - for (auto iter = relayStpDhidCallbacks_.begin(); iter != relayStpDhidCallbacks_.end(); ++iter) { + for (auto iter = relayStpDhidCallbacks_.begin(); iter != relayStpDhidCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) { relayStpDhidCallbacks_.erase(iter); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; @@ -2275,7 +2181,7 @@ void DistributedInputSourceManager::RunUnregisterCallback( } void DistributedInputSourceManager::RunPrepareCallback( - const std::string& devId, const int32_t& status) + const std::string& devId, const int32_t& status, const std::string& object) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK); for (auto iter = preCallbacks_.begin(); iter != preCallbacks_.end(); ++iter) { @@ -2283,6 +2189,7 @@ void DistributedInputSourceManager::RunPrepareCallback( DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_PREPARE_MSG"); iter->preCallback->OnResult(devId, status); preCallbacks_.erase(iter); + RunWhiteListCallback(devId, object); return; } } @@ -2306,7 +2213,7 @@ void DistributedInputSourceManager::RunRelayPrepareCallback(const std::string &s const int32_t &status) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK); - for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); ++iter) { + for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId) { DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_MMI"); iter->preCallback->OnResult(sinkId, status); @@ -2321,7 +2228,7 @@ void DistributedInputSourceManager::RunRelayUnprepareCallback(const std::string const int32_t &status) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK); - for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); ++iter) { + for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); iter++) { if (iter->srcId == srcId && iter->sinkId == sinkId) { DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_MMI"); iter->unpreCallback->OnResult(sinkId, status); @@ -2356,7 +2263,7 @@ void DistributedInputSourceManager::RunUnprepareCallback(const std::string &devI } void DistributedInputSourceManager::RunStartCallback( - const std::string& devId, const uint32_t& inputTypes, const int32_t& status, const std::string& object) + const std::string& devId, const uint32_t& inputTypes, const int32_t& status) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); for (auto iter = staCallbacks_.begin(); iter != staCallbacks_.end(); ++iter) { @@ -2364,7 +2271,6 @@ void DistributedInputSourceManager::RunStartCallback( DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_MSG"); iter->callback->OnResult(devId, inputTypes, status); staCallbacks_.erase(iter); - RunWhiteListCallback(devId, object); break; } } @@ -2385,7 +2291,7 @@ void DistributedInputSourceManager::RunStopCallback( } void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, - const int32_t &status, const std::string& object) + const int32_t &status) { std::vector dhidsVec; StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); @@ -2402,7 +2308,6 @@ void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sink } iter->callback->OnResultDhids(sinkId, status); staStringCallbacks_.erase(iter); - RunWhiteListCallback(sinkId, object); break; } } diff --git a/services/source/transport/BUILD.gn b/services/source/transport/BUILD.gn index e73eb66..8ff351e 100755 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -38,7 +38,6 @@ ohos_shared_library("libdinput_source_trans") { "${distributedinput_path}/inputdevicehandler/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", - "//foundation/distributedhardware/distributed_input/services/transportbase/include", ] sources = [ "src/distributed_input_source_transport.cpp" ] @@ -61,7 +60,6 @@ ohos_shared_library("libdinput_source_trans") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", - "//foundation/distributedhardware/distributed_input/services/transportbase:libdinput_trans_base", ] external_deps = [ diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 7e69eb4..1c39e1d 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -17,10 +17,10 @@ #define DISTRIBUTED_INPUT_SOURCE_TRANSPORT_H #include -#include +#include #include #include -#include +#include #include #include @@ -30,7 +30,6 @@ #include "securec.h" #include "dinput_source_trans_callback.h" -#include "dinput_transbase_source_callback.h" namespace OHOS { namespace DistributedHardware { @@ -40,28 +39,23 @@ public: static DistributedInputSourceTransport &GetInstance(); ~DistributedInputSourceTransport(); - class DInputTransbaseSourceListener : public DInputTransbaseSourceCallback { - public: - DInputTransbaseSourceListener(DistributedInputSourceTransport *transport); - virtual ~DInputTransbaseSourceListener(); - void HandleSessionData(int32_t sessionId, const std::string& messageData); - - private: - DistributedInputSourceTransport *sourceTransportObj_; - }; - int32_t Init(); void Release(); - + // this open deviceA.sourceSa ----> deviceB.sinkSa softbus. int32_t OpenInputSoftbus(const std::string &remoteDevId); - void CloseInputSoftbus(const std::string &remoteDevId); - + void CloseInputSoftbus(const int32_t sessionId); + // this open deviceA.sourceSa ----> deviceB.sourceSa softbus. + int32_t OpenInputSoftbusForRelay(const std::string &srcId); void RegisterSourceRespCallback(std::shared_ptr callback); int32_t PrepareRemoteInput(const std::string& deviceId); int32_t UnprepareRemoteInput(const std::string& deviceId); int32_t StartRemoteInput(const std::string& deviceId, const uint32_t& inputTypes); int32_t StopRemoteInput(const std::string& deviceId, const uint32_t& inputTypes); + int32_t LatencyCount(const std::string& deviceId); + void StartLatencyCount(const std::string& deviceId); + void StartLatencyThread(const std::string& deviceId); + void StopLatencyThread(); int32_t StartRemoteInput(const std::string &deviceId, const std::vector &dhids); int32_t StopRemoteInput(const std::string &deviceId, const std::vector &dhids); @@ -88,9 +82,12 @@ public: int32_t GetCurrentSessionId(); private: - int32_t SendMessage(int32_t sessionId, std::string &message); - void HandleData(int32_t sessionId, const std::string& message); + std::string FindDeviceBySession(int32_t sessionId); + int32_t FindSessionIdByDevId(bool isToSrc, const std::string &deviceId); + int32_t SendMsg(int32_t sessionId, std::string &message); + int32_t CheckDeviceSessionState(bool isToSrcSa, const std::string &remoteDevId); void HandleSessionData(int32_t sessionId, const std::string& messageData); + bool CheckRecivedData(const std::string& messageData); void NotifyResponsePrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void NotifyResponseUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void NotifyResponseStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); @@ -131,6 +128,13 @@ private: int32_t status, uint32_t inputTypes); void ReceiveRelayStartTypeResult(int32_t sessionId, const nlohmann::json &recMsg); void ReceiveRelayStopTypeResult(int32_t sessionId, const nlohmann::json &recMsg); + + struct DInputSessionInfo { + bool isToSrcSa; // [true] is session to source_sa, [false] is session to sink_sa + std::string remoteId; // networkId + }; + + void CalculateLatency(int32_t sessionId, const nlohmann::json &recMsg); std::string JointDhIds(const std::vector &dhids); void RegRespFunMap(); @@ -138,11 +142,19 @@ private: std::mutex operationMutex_; std::set sessionIdSet_; std::shared_ptr callback_; - std::shared_ptr statuslistener_; std::string mySessionName_ = ""; std::condition_variable openSessionWaitCond_; + std::map sessionDevMap_; // [sessionId, DInputSessionInfo] + std::map channelStatusMap_; // [sessionId, bool] + uint64_t deltaTime_ = 0; + uint64_t deltaTimeAll_ = 0; + uint64_t sendTime_ = 0; + uint32_t sendNum_ = 0; + uint32_t recvNum_ = 0; + std::atomic isLatencyThreadRunning_ = false; + std::thread latencyThread_; + std::string eachLatencyDetails_ = ""; int32_t sessionId_ = 0; - using SourceTransportFunc = void (DistributedInputSourceTransport::*)(int32_t sessionId, const nlohmann::json &recMsg); std::map memberFuncMap_; diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index 2fff68c..a22fde6 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -36,11 +36,20 @@ #include "softbus_bus_center.h" #include "softbus_common.h" -#include "distributed_input_transport_base.h" - namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +const int32_t DINPUT_LINK_TYPE_MAX = 4; +static SessionAttribute g_sessionAttr = { + .dataType = SessionType::TYPE_BYTES, + .linkTypeNum = DINPUT_LINK_TYPE_MAX, + .linkType = { + LINK_TYPE_WIFI_P2P, + LINK_TYPE_WIFI_WLAN_2G, + LINK_TYPE_WIFI_WLAN_5G, + LINK_TYPE_BR + } +}; DistributedInputSourceTransport::~DistributedInputSourceTransport() { @@ -48,6 +57,38 @@ DistributedInputSourceTransport::~DistributedInputSourceTransport() Release(); } +static int32_t SessionOpened(int32_t sessionId, int32_t result) +{ + return DistributedInput::DistributedInputSourceTransport::GetInstance().OnSessionOpened(sessionId, result); +} + +static void SessionClosed(int32_t sessionId) +{ + DistributedInput::DistributedInputSourceTransport::GetInstance().OnSessionClosed(sessionId); +} + +static void BytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + DistributedInput::DistributedInputSourceTransport::GetInstance().OnBytesReceived(sessionId, data, dataLen); +} + +static void MessageReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + (void)sessionId; + (void)data; + (void)dataLen; + DHLOGI("sessionId: %d, dataLen:%d", sessionId, dataLen); +} + +static void StreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) +{ + (void)sessionId; + (void)data; + (void)ext; + (void)param; + DHLOGI("sessionId: %d", sessionId); +} DistributedInputSourceTransport &DistributedInputSourceTransport::GetInstance() { static DistributedInputSourceTransport instance; @@ -56,16 +97,30 @@ DistributedInputSourceTransport &DistributedInputSourceTransport::GetInstance() int32_t DistributedInputSourceTransport::Init() { - DHLOGI("Init Source Transport"); + DHLOGI("Init"); + ISessionListener iSessionListener = { + .OnSessionOpened = SessionOpened, + .OnSessionClosed = SessionClosed, + .OnBytesReceived = BytesReceived, + .OnMessageReceived = MessageReceived, + .OnStreamReceived = StreamReceived + }; - int32_t ret = DistributedInputTransportBase::GetInstance().Init(); - if (ret != DH_SUCCESS) { - DHLOGE("Init Source Transport failed."); - return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; + auto localNode = std::make_unique(); + int32_t retCode = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), localNode.get()); + if (retCode != DH_SUCCESS) { + DHLOGE("Init Could not get local device id."); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; } + std::string networkId = localNode->networkId; + DHLOGI("Init device local networkId is %s", GetAnonyString(networkId).c_str()); + mySessionName_ = SESSION_NAME_SOURCE + networkId.substr(0, INTERCEPT_STRING_LENGTH); - statuslistener_ = std::make_shared(this); - DistributedInputTransportBase::GetInstance().RegisterSrcHandleSessionCallback(statuslistener_); + int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + if (ret != DH_SUCCESS) { + DHLOGE("Init CreateSessionServer failed, error code %d.", ret); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; + } RegRespFunMap(); return DH_SUCCESS; } @@ -77,6 +132,7 @@ void DistributedInputSourceTransport::RegRespFunMap() memberFuncMap_[TRANS_SINK_MSG_ONSTART] = &DistributedInputSourceTransport::NotifyResponseStartRemoteInput; memberFuncMap_[TRANS_SINK_MSG_ONSTOP] = &DistributedInputSourceTransport::NotifyResponseStopRemoteInput; memberFuncMap_[TRANS_SINK_MSG_BODY_DATA] = &DistributedInputSourceTransport::NotifyReceivedEventRemoteInput; + memberFuncMap_[TRANS_SINK_MSG_LATENCY] = &DistributedInputSourceTransport::CalculateLatency; memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTART] = &DistributedInputSourceTransport::NotifyResponseStartRemoteInputDhid; memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTOP] = &DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid; memberFuncMap_[TRANS_SINK_MSG_KEY_STATE] = &DistributedInputSourceTransport::NotifyResponseKeyState; @@ -119,27 +175,159 @@ void DistributedInputSourceTransport::RegRespFunMap() void DistributedInputSourceTransport::Release() { - DHLOGI("Release Source Transport"); - - DistributedInputTransportBase::GetInstance().Release(); - + std::unique_lock sessionLock(operationMutex_); + std::for_each(sessionDevMap_.begin(), sessionDevMap_.end(), [](auto item) { CloseSession(item.first); }); + (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); + sessionDevMap_.clear(); + channelStatusMap_.clear(); + memberFuncMap_.clear(); DistributedInputInject::GetInstance().StopInjectThread(); } +int32_t DistributedInputSourceTransport::CheckDeviceSessionState(bool isToSrcSa, const std::string &devId) +{ + std::unique_lock sessionLock(operationMutex_); + for (auto tmp : sessionDevMap_) { + if (tmp.second.isToSrcSa == isToSrcSa && tmp.second.remoteId == devId) { + return DH_SUCCESS; + } + } + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_DEVICE_SESSION_STATE; +} + int32_t DistributedInputSourceTransport::OpenInputSoftbus(const std::string &remoteDevId) { - int32_t ret = DistributedInputTransportBase::GetInstance().StartSession(remoteDevId); - if (ret != DH_SUCCESS) { - DHLOGE("StartSession fail! remoteDevId:%s.", GetAnonyString(remoteDevId).c_str()); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; + int32_t ret = CheckDeviceSessionState(false, remoteDevId); + if (ret == DH_SUCCESS) { + DHLOGE("Softbus session has already opened, deviceId: %s", GetAnonyString(remoteDevId).c_str()); + return DH_SUCCESS; } + std::string peerSessionName = SESSION_NAME_SINK + remoteDevId.substr(0, INTERCEPT_STRING_LENGTH); + DHLOGI("OpenInputSoftbus peerSessionName:%s", peerSessionName.c_str()); + + StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); + int sessionId = OpenSession(mySessionName_.c_str(), peerSessionName.c_str(), remoteDevId.c_str(), + GROUP_ID.c_str(), &g_sessionAttr); + if (sessionId < 0) { + DHLOGE("OpenSession fail, remoteDevId: %s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); + FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; + } + + HiDumper::GetInstance().CreateSessionInfo(remoteDevId, sessionId, mySessionName_, peerSessionName, + SessionStatus::OPENING); + { + std::unique_lock sessionLock(operationMutex_); + DInputSessionInfo sessionInfo{false, remoteDevId}; + sessionDevMap_[sessionId] = sessionInfo; + } + + DHLOGI("Wait for channel session opened."); + { + std::unique_lock waitLock(operationMutex_); + auto status = openSessionWaitCond_.wait_for(waitLock, std::chrono::seconds(SESSION_WAIT_TIMEOUT_SECOND), + [this, sessionId] () { return channelStatusMap_[sessionId]; }); + if (!status) { + DHLOGE("OpenSession timeout, remoteDevId: %s, sessionId: %d", + GetAnonyString(remoteDevId).c_str(), sessionId); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_TIMEOUT; + } + } + + StartLatencyThread(remoteDevId); + + DistributedInputInject::GetInstance().StartInjectThread(); + DHLOGI("OpenSession success, remoteDevId:%s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); + sessionId_ = sessionId; + +#ifdef DINPUT_LOW_LATENCY + DInputLowLatency::GetInstance().EnableSourceLowLatency(); +#endif + + HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::OPENED); return DH_SUCCESS; } -void DistributedInputSourceTransport::CloseInputSoftbus(const std::string &remoteDevId) +void DistributedInputSourceTransport::CloseInputSoftbus(const int32_t sessionId) { - DistributedInputTransportBase::GetInstance().StopSession(remoteDevId); + std::unique_lock sessionLock(operationMutex_); + // check this device's all hd is close,this device session close. + + if (sessionDevMap_.count(sessionId) == 0) { + DHLOGI("sessionDevMap_ Not find sessionId: %d", sessionId); + return; + } + + StopLatencyThread(); + + DHLOGI("RemoteDevId: %s, sessionId: %d", GetAnonyString(sessionDevMap_[sessionId].remoteId).c_str(), sessionId); + HiDumper::GetInstance().SetSessionStatus(sessionDevMap_[sessionId].remoteId, SessionStatus::CLOSING); + CloseSession(sessionId); + sessionDevMap_.erase(sessionId); + channelStatusMap_.erase(sessionId); + DistributedInputInject::GetInstance().StopInjectThread(); + +#ifdef DINPUT_LOW_LATENCY + DInputLowLatency::GetInstance().DisableSourceLowLatency(); +#endif + + HiDumper::GetInstance().SetSessionStatus(sessionDevMap_[sessionId].remoteId, SessionStatus::CLOSED); + HiDumper::GetInstance().DeleteSessionInfo(sessionDevMap_[sessionId].remoteId); +} + + +int32_t DistributedInputSourceTransport::OpenInputSoftbusForRelay(const std::string &srcId) +{ + int32_t ret = CheckDeviceSessionState(true, srcId); + if (ret == DH_SUCCESS) { + DHLOGE("Softbus session has already opened, deviceId: %s", GetAnonyString(srcId).c_str()); + return DH_SUCCESS; + } + + ret = Init(); + if (ret != DH_SUCCESS) { + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; + } + + std::string peerSessionName = SESSION_NAME_SOURCE + srcId.substr(0, INTERCEPT_STRING_LENGTH); + DHLOGI("OpenInputSoftbus peerSessionName:%s", peerSessionName.c_str()); + + StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); + int sessionId = OpenSession(mySessionName_.c_str(), peerSessionName.c_str(), srcId.c_str(), GROUP_ID.c_str(), + &g_sessionAttr); + if (sessionId < 0) { + DHLOGE("OpenSession fail, remoteDevId: %s, sessionId: %d", GetAnonyString(srcId).c_str(), sessionId); + FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; + } + + HiDumper::GetInstance().CreateSessionInfo(srcId, sessionId, mySessionName_, peerSessionName, + SessionStatus::OPENING); + { + std::unique_lock sessionLock(operationMutex_); + DInputSessionInfo sessionInfo{true, srcId}; + sessionDevMap_[sessionId] = sessionInfo; + } + + DHLOGI("Wait for channel session opened."); + { + std::unique_lock waitLock(operationMutex_); + auto status = openSessionWaitCond_.wait_for(waitLock, std::chrono::seconds(SESSION_WAIT_TIMEOUT_SECOND), + [this, sessionId] () { return channelStatusMap_[sessionId]; }); + if (!status) { + DHLOGE("OpenSession timeout, remoteDevId: %s, sessionId: %d", GetAnonyString(srcId).c_str(), sessionId); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_TIMEOUT; + } + } + +#ifdef DINPUT_LOW_LATENCY + DInputLowLatency::GetInstance().EnableSourceLowLatency(); +#endif + + DHLOGI("OpenSession success, remoteDevId:%s, sessionId:%d", GetAnonyString(srcId).c_str(), sessionId); + HiDumper::GetInstance().SetSessionStatus(srcId, SessionStatus::OPENED); + return DH_SUCCESS; } void DistributedInputSourceTransport::RegisterSourceRespCallback(std::shared_ptr callback) @@ -148,6 +336,19 @@ void DistributedInputSourceTransport::RegisterSourceRespCallback(std::shared_ptr callback_ = callback; } +int32_t DistributedInputSourceTransport::FindSessionIdByDevId(bool isToSrc, const std::string &deviceId) +{ + std::unique_lock sessionLock(operationMutex_); + int32_t sessionId = -1; + for (auto tmp : sessionDevMap_) { + if (tmp.second.isToSrcSa == isToSrc && tmp.second.remoteId == deviceId) { + sessionId = tmp.first; + break; + } + } + return sessionId; +} + /* * PrepareRemoteInput. * @param deviceId is remote device @@ -155,7 +356,7 @@ void DistributedInputSourceTransport::RegisterSourceRespCallback(std::shared_ptr */ int32_t DistributedInputSourceTransport::PrepareRemoteInput(const std::string& deviceId) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("PrepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; @@ -167,20 +368,20 @@ int32_t DistributedInputSourceTransport::PrepareRemoteInput(const std::string& d jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("PrepareRemoteInput deviceId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; } - DHLOGI("PrepareRemoteInput devId:%s, sessionId:%d, msg:%s.", + DHLOGI("PrepareRemoteInput devId:%s, sessionId:%d, smsg:%s.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); return DH_SUCCESS; } int32_t DistributedInputSourceTransport::UnprepareRemoteInput(const std::string& deviceId) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("UnprepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_UNPREPARE_FAIL; @@ -192,7 +393,7 @@ int32_t DistributedInputSourceTransport::UnprepareRemoteInput(const std::string& jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("UnprepareRemoteInput deviceId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -203,60 +404,61 @@ int32_t DistributedInputSourceTransport::UnprepareRemoteInput(const std::string& return DH_SUCCESS; } -int32_t DistributedInputSourceTransport::PrepareRemoteInput(int32_t srcTsrcSeId, const std::string &peerDevId) + +int32_t DistributedInputSourceTransport::PrepareRemoteInput(int32_t srcTsrcSeId, const std::string &deviceId) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(peerDevId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { - DHLOGE("PrepareRemoteInput error, not find this device:%s.", GetAnonyString(peerDevId).c_str()); + DHLOGE("PrepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; } - DHLOGI("PrepareRemoteInput srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_PREPARE_FOR_REL; - jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = peerDevId; + jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("PrepareRemoteInput peerDevId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(peerDevId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("PrepareRemoteInput deviceId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; } DHLOGI("PrepareRemoteInput send success, devId:%s, msg:%s.", - GetAnonyString(peerDevId).c_str(), SetAnonyId(smsg).c_str()); + GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str()); return DH_SUCCESS; } -int32_t DistributedInputSourceTransport::UnprepareRemoteInput(int32_t srcTsrcSeId, const std::string &peerDevId) + +int32_t DistributedInputSourceTransport::UnprepareRemoteInput(int32_t srcTsrcSeId, const std::string &deviceId) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(peerDevId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { - DHLOGE("UnprepareRemoteInput error, not find this device:%s.", GetAnonyString(peerDevId).c_str()); + DHLOGE("UnprepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_UNPREPARE_FAIL; } DHLOGI("UnprepareRemoteInput srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_UNPREPARE_FOR_REL; - jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = peerDevId; + jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("UnprepareRemoteInput peerDevId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(peerDevId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("UnprepareRemoteInput deviceId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_UNPREPARE_FAIL; } DHLOGI("UnprepareRemoteInput send success, devId:%s, msg:%s.", - GetAnonyString(peerDevId).c_str(), SetAnonyId(smsg).c_str()); + GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str()); return DH_SUCCESS; } int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSeId, const std::string &deviceId, const std::string &dhids) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { DHLOGE("StartRemoteInputDhids error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -269,7 +471,7 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StartRemoteInputDhids deviceId:%s, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); @@ -277,15 +479,13 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe } DHLOGI("StartRemoteInputDhids send success, devId:%s, msg:%s.", GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str()); - DistributedInputInject::GetInstance().StartInjectThread(); - DHLOGI("StartInjectThread successed"); return DH_SUCCESS; } int32_t DistributedInputSourceTransport::StopRemoteInputDhids(int32_t srcTsrcSeId, const std::string &deviceId, const std::string &dhids) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { DHLOGE("StopRemoteInputDhids error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -298,7 +498,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputDhids(int32_t srcTsrcSeI jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StopRemoteInputDhids deviceId:%s, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); @@ -312,7 +512,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputDhids(int32_t srcTsrcSeI int32_t DistributedInputSourceTransport::StartRemoteInputType(int32_t srcTsrcSeId, const std::string &deviceId, const uint32_t& inputTypes) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { DHLOGE("StartRemoteInputType error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -325,23 +525,21 @@ int32_t DistributedInputSourceTransport::StartRemoteInputType(int32_t srcTsrcSeI jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StartRemoteInputType deviceId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); + GetAnonyString(deviceId).c_str(), smsg.c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; } - DHLOGI("StartRemoteInputType send success, devId:%s, msg:%s.", GetAnonyString(deviceId).c_str(), + DHLOGI("StartRemoteInputType send success, devId:%s, smsg:%s.", GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str()); - DistributedInputInject::GetInstance().StartInjectThread(); - DHLOGI("StartInjectThread successed"); return DH_SUCCESS; } int32_t DistributedInputSourceTransport::StopRemoteInputType(int32_t srcTsrcSeId, const std::string &deviceId, const uint32_t& inputTypes) { - int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); if (sinkSessionId < 0) { DHLOGE("StopRemoteInputType error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -354,7 +552,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputType(int32_t srcTsrcSeId jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sinkSessionId, smsg); + int32_t ret = SendMsg(sinkSessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StopRemoteInputType deviceId:%s, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); @@ -367,7 +565,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputType(int32_t srcTsrcSeId int32_t DistributedInputSourceTransport::SendRelayPrepareRequest(const std::string &srcId, const std::string &sinkId) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayPrepareRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; @@ -378,7 +576,7 @@ int32_t DistributedInputSourceTransport::SendRelayPrepareRequest(const std::stri jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_TO_SOURCE_MSG_PREPARE; jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayPrepareRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -391,7 +589,7 @@ int32_t DistributedInputSourceTransport::SendRelayPrepareRequest(const std::stri int32_t DistributedInputSourceTransport::SendRelayUnprepareRequest(const std::string &srcId, const std::string &sinkId) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayUnprepareRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; @@ -402,14 +600,14 @@ int32_t DistributedInputSourceTransport::SendRelayUnprepareRequest(const std::st jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_TO_SOURCE_MSG_UNPREPARE; jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayUnprepareRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; } - DHLOGI("SendRelayUnprepareRequest srcId:%s, sessionId:%d, smsg:%s.", - GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str()); + DHLOGI("SendRelayUnprepareRequest srcId:%s, sessionId:%s, smsg:%s.", + GetAnonyString(srcId).c_str(), GetAnonyInt32(sessionId).c_str(), SetAnonyId(smsg).c_str()); return DH_SUCCESS; } @@ -424,10 +622,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginPrepareResult(int32_t srcTs jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = status; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginPrepareResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginPrepareResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_FAIL; } DHLOGI("NotifyOriginPrepareResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -445,10 +643,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginUnprepareResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = status; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginUnprepareResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginUnprepareResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_UNPREPARE_FAIL; } DHLOGI("NotifyOriginUnprepareResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -467,10 +665,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStartDhidResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStartDhidResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStartDhidResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; } DHLOGI("NotifyOriginStartDhidResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -489,10 +687,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStopDhidResult(int32_t srcT jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStopDhidResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStopDhidResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; } DHLOGI("NotifyOriginStopDhidResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -511,10 +709,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStartTypeResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStartTypeResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStartTypeResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; } DHLOGI("NotifyOriginStartTypeResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -533,10 +731,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStopTypeResult(int32_t srcT jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(srcTsrcSeId, smsg); + int32_t ret = SendMsg(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStopTypeResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyInt32(srcTsrcSeId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStopTypeResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", + srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; } DHLOGI("NotifyOriginStopTypeResult srcTsrcSeId:%d, smsg:%s.", srcTsrcSeId, SetAnonyId(smsg).c_str()); @@ -544,9 +742,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStopTypeResult(int32_t srcT } -int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string& deviceId, const uint32_t& inputTypes) +int32_t DistributedInputSourceTransport::StartRemoteInput( + const std::string& deviceId, const uint32_t& inputTypes) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("StartRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -559,7 +758,7 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string& dev jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StartRemoteInput deviceId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -567,17 +766,13 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string& dev } DHLOGI("StartRemoteInput deviceId:%s, sessionId:%d, smsg:%s.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); - - DistributedInputInject::GetInstance().StartInjectThread(); - DHLOGI("StartInjectThread successed"); - return DH_SUCCESS; } int32_t DistributedInputSourceTransport::StopRemoteInput( const std::string& deviceId, const uint32_t& inputTypes) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("StopRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -590,7 +785,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput( jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StopRemoteInput deviceId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -601,10 +796,74 @@ int32_t DistributedInputSourceTransport::StopRemoteInput( return DH_SUCCESS; } +int32_t DistributedInputSourceTransport::LatencyCount(const std::string& deviceId) +{ + int32_t sessionId = FindSessionIdByDevId(false, deviceId); + if (sessionId < 0) { + DHLOGE("LatencyCount error, not find this device:%s.", GetAnonyString(deviceId).c_str()); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_LATENCY_FAIL; + } + + nlohmann::json jsonStr; + jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_LATENCY; + jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; + jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; + std::string smsg = jsonStr.dump(); + int32_t ret = SendMsg(sessionId, smsg); + if (ret != DH_SUCCESS) { + DHLOGE("LatencyCount deviceId:%s, sessionId: %d, smsg:%s, SendMsg error, ret:%d.", + GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_LATENCY_FAIL; + } + + DHLOGI("LatencyCount deviceId:%s, sessionId: %d, smsg:%s.", + GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); + return DH_SUCCESS; +} + +void DistributedInputSourceTransport::StartLatencyCount(const std::string& deviceId) +{ + DHLOGI("start"); + while (isLatencyThreadRunning_.load()) { + if (sendNum_ >= INPUT_LATENCY_DELAY_TIMES) { + uint64_t latency = (uint64_t)(deltaTimeAll_ / 2 / INPUT_LATENCY_DELAY_TIMES); + DHLOGI("LatencyCount average single-channel latency is %d us, send times is %d, recive times is %d, " + + "each RTT latency details is %s", latency, sendNum_, recvNum_, eachLatencyDetails_.c_str()); + deltaTimeAll_ = 0; + sendNum_ = 0; + recvNum_ = 0; + eachLatencyDetails_ = ""; + } + sendTime_ = GetCurrentTime(); + LatencyCount(deviceId); + sendNum_ += 1; + usleep(INPUT_LATENCY_DELAYTIME_US); + } + DHLOGI("end"); +} + +void DistributedInputSourceTransport::StartLatencyThread(const std::string& deviceId) +{ + DHLOGI("start"); + isLatencyThreadRunning_.store(true); + latencyThread_ = std::thread(&DistributedInputSourceTransport::StartLatencyCount, this, deviceId); + DHLOGI("end"); +} + +void DistributedInputSourceTransport::StopLatencyThread() +{ + DHLOGI("start"); + isLatencyThreadRunning_.store(false); + if (latencyThread_.joinable()) { + latencyThread_.join(); + } + DHLOGI("end"); +} + int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string &deviceId, const std::vector &dhids) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("StartRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -617,7 +876,7 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string &dev jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = JointDhIds(dhids); std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StartRemoteInput deviceId:%s, sessionId: %d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -625,17 +884,13 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string &dev } DHLOGI("StartRemoteInput deviceId:%s, sessionId: %d, smsg:%s.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); - - DistributedInputInject::GetInstance().StartInjectThread(); - DHLOGI("StartInjectThread successed"); - return DH_SUCCESS; } int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &deviceId, const std::vector &dhids) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(deviceId); + int32_t sessionId = FindSessionIdByDevId(false, deviceId); if (sessionId < 0) { DHLOGE("StopRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -648,7 +903,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = JointDhIds(dhids); std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StopRemoteInput deviceId:%s, sessionId: %d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -662,7 +917,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi int32_t DistributedInputSourceTransport::SendRelayStartDhidRequest(const std::string &srcId, const std::string &sinkId, const std::vector &dhids) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayStartDhidRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -674,7 +929,7 @@ int32_t DistributedInputSourceTransport::SendRelayStartDhidRequest(const std::st jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = JointDhIds(dhids); std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayStartDhidRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -688,7 +943,7 @@ int32_t DistributedInputSourceTransport::SendRelayStartDhidRequest(const std::st int32_t DistributedInputSourceTransport::SendRelayStopDhidRequest(const std::string &srcId, const std::string &sinkId, const std::vector &dhids) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayStopDhidRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -700,7 +955,7 @@ int32_t DistributedInputSourceTransport::SendRelayStopDhidRequest(const std::str jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = JointDhIds(dhids); std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayStopDhidRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -714,7 +969,7 @@ int32_t DistributedInputSourceTransport::SendRelayStopDhidRequest(const std::str int32_t DistributedInputSourceTransport::SendRelayStartTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t& inputTypes) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayStartTypeRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; @@ -726,7 +981,7 @@ int32_t DistributedInputSourceTransport::SendRelayStartTypeRequest(const std::st jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayStartTypeRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -740,7 +995,7 @@ int32_t DistributedInputSourceTransport::SendRelayStartTypeRequest(const std::st int32_t DistributedInputSourceTransport::SendRelayStopTypeRequest(const std::string &srcId, const std::string &sinkId, const uint32_t& inputTypes) { - int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(srcId); + int32_t sessionId = FindSessionIdByDevId(true, srcId); if (sessionId < 0) { DHLOGE("SendRelayStopTypeRequest error, not find this device:%s.", GetAnonyString(srcId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; @@ -752,7 +1007,7 @@ int32_t DistributedInputSourceTransport::SendRelayStopTypeRequest(const std::str jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = sinkId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMessage(sessionId, smsg); + int32_t ret = SendMsg(sessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("SendRelayStopTypeRequest srcId:%s, sessionId:%d, smsg:%s, SendMsg error, ret:%d.", GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); @@ -772,43 +1027,100 @@ std::string DistributedInputSourceTransport::JointDhIds(const std::vector sessionLock(operationMutex_); + std::string devId = ""; + if (sessionDevMap_.count(sessionId) == 0) { + DHLOGE("FindDeviceBySession error, has no this sessionId."); + return devId; + } + devId = sessionDevMap_[sessionId].remoteId; + return devId; } -int32_t DistributedInputSourceTransport::GetCurrentSessionId() +int32_t DistributedInputSourceTransport::OnSessionOpened(int32_t sessionId, int32_t result) { - return sessionId_; + FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); + if (result != DH_SUCCESS) { + DHLOGE("session open failed, sessionId:%d, result:%d, deviceId:%s", sessionId, result, + GetAnonyString(sessionDevMap_[sessionId].remoteId).c_str()); + std::unique_lock sessionLock(operationMutex_); + if (sessionDevMap_.count(sessionId) > 0) { + sessionDevMap_.erase(sessionId); + } + return DH_SUCCESS; + } + + std::string deviceId = FindDeviceBySession(sessionId); + int32_t sessionSide = GetSessionSide(sessionId); + DHLOGI("session open succeed, sessionId: %d, sessionSide:%d(1 is " + "client side), deviceId:%s", sessionId, sessionSide, GetAnonyString(deviceId).c_str()); + + char mySessionName[SESSION_NAME_SIZE_MAX] = ""; + char peerSessionName[SESSION_NAME_SIZE_MAX] = ""; + char peerDevId[DEVICE_ID_SIZE_MAX] = ""; + int ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName)); + if (ret != DH_SUCCESS) { + DHLOGI("get my session name failed, session id is %d", sessionId); + } + ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName)); + if (ret != DH_SUCCESS) { + DHLOGI("get peer session name failed, session id is %d", sessionId); + } + ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); + if (ret != DH_SUCCESS) { + DHLOGI("get peer device id failed, session id is %d", sessionId); + } + DHLOGI("mySessionName:%s, peerSessionName:%s, peerDevId:%s", + mySessionName, peerSessionName, GetAnonyString(peerDevId).c_str()); + + if (sessionSide == AUTH_SESSION_SIDE_SERVER) { + DHLOGI("session open succeed, sessionId:%d, sessionSide:service", sessionId); + std::lock_guard notifyLock(operationMutex_); + DInputSessionInfo sessionInfo{true, peerDevId}; + sessionDevMap_[sessionId] = sessionInfo; + } else { + DHLOGI("session open succeed, sessionId:%d, sessionSide:client", sessionId); + std::lock_guard notifyLock(operationMutex_); + channelStatusMap_[sessionId] = true; + openSessionWaitCond_.notify_all(); + } + + return DH_SUCCESS; } -DistributedInputSourceTransport::DInputTransbaseSourceListener::DInputTransbaseSourceListener( - DistributedInputSourceTransport *transport) +void DistributedInputSourceTransport::OnSessionClosed(int32_t sessionId) { - sourceTransportObj_ = transport; - DHLOGI("DInputTransbaseSourceListener init."); -} - -DistributedInputSourceTransport::DInputTransbaseSourceListener::~DInputTransbaseSourceListener() -{ - sourceTransportObj_ = nullptr; - DHLOGI("DInputTransbaseSourceListener destory."); + std::string deviceId = FindDeviceBySession(sessionId); + DHLOGI("OnSessionClosed, sessionId: %d, deviceId:%s", sessionId, GetAnonyString(deviceId).c_str()); + std::unique_lock sessionLock(operationMutex_); + if (sessionDevMap_.count(sessionId) > 0) { + sessionDevMap_.erase(sessionId); + } + if (channelStatusMap_.count(sessionId) > 0) { + channelStatusMap_.erase(sessionId); + } + StopLatencyThread(); + DistributedInputInject::GetInstance().StopInjectThread(); } void DistributedInputSourceTransport::NotifyResponsePrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE."); - if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, deviceId is error."); return; } - callback_->onResponsePrepareRemoteInput(deviceId, recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); + callback_->onResponsePrepareRemoteInput(deviceId, recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], + recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseUnprepareRemoteInput(int32_t sessionId, @@ -819,31 +1131,29 @@ void DistributedInputSourceTransport::NotifyResponseUnprepareRemoteInput(int32_t DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONUNPREPARE data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONUNPREPARE, deviceId is error."); return; } callback_->onResponseUnprepareRemoteInput(deviceId, recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); - CloseInputSoftbus(deviceId); + CloseInputSoftbus(sessionId); } void DistributedInputSourceTransport::NotifyResponseStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_ONSTART."); - if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || - !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONSTART, data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONSTART, deviceId is error."); return; } callback_->onResponseStartRemoteInput( - deviceId, recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], - recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); + deviceId, recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); } void DistributedInputSourceTransport::NotifyResponseStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) @@ -853,7 +1163,7 @@ void DistributedInputSourceTransport::NotifyResponseStopRemoteInput(int32_t sess DHLOGE("OnBytesReceived cmdType TRANS_SINK_MSG_ONSTOP data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType TRANS_SINK_MSG_ONSTOP, deviceId is error."); return; @@ -866,19 +1176,17 @@ void DistributedInputSourceTransport::NotifyResponseStartRemoteInputDhid(int32_t const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTART."); - if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || - !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTART, data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTART, deviceId is error."); return; } callback_->onResponseStartRemoteInputDhid( - deviceId, recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], - recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); + deviceId, recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); } void DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg) @@ -888,7 +1196,7 @@ void DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid(int32_t DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTOP, data type is error."); return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTOP, deviceId is error."); return; @@ -900,7 +1208,7 @@ void DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid(int32_t void DistributedInputSourceTransport::NotifyResponseKeyState(int32_t sessionId, const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE."); - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE, deviceId is error."); return; @@ -918,7 +1226,7 @@ void DistributedInputSourceTransport::NotifyReceivedEventRemoteInput(int32_t ses return; } - std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string deviceId = FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_BODY_DATA, deviceId is error."); return; @@ -927,6 +1235,21 @@ void DistributedInputSourceTransport::NotifyReceivedEventRemoteInput(int32_t ses callback_->onReceivedEventRemoteInput(deviceId, inputDataStr); } +void DistributedInputSourceTransport::CalculateLatency(int32_t sessionId, const nlohmann::json &recMsg) +{ + DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_LATENCY."); + std::string deviceId = FindDeviceBySession(sessionId); + if (deviceId.empty()) { + DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_LATENCY, deviceId is error."); + return; + } + + deltaTime_ = GetCurrentTime() - sendTime_; + deltaTimeAll_ += deltaTime_; + recvNum_ += 1; + eachLatencyDetails_ += (std::to_string(deltaTime_) + DINPUT_SPLIT_COMMA); +} + void DistributedInputSourceTransport::ReceiveSrcTSrcRelayPrepare(int32_t sessionId, const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_TO_SOURCE_MSG_PREPARE."); @@ -934,18 +1257,18 @@ void DistributedInputSourceTransport::ReceiveSrcTSrcRelayPrepare(int32_t session DHLOGE("OnBytesReceived cmdType is TRANS_SOURCE_TO_SOURCE_MSG_PREPARE, data type is error."); return; } + std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; - std::string peerDevId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; - - int32_t ret = OpenInputSoftbus(peerDevId); + // continue notify to A_sink_trans + int32_t ret = OpenInputSoftbus(deviceId); if (ret != DH_SUCCESS) { - callback_->onResponseRelayPrepareRemoteInput(sessionId, peerDevId, false); + callback_->onResponseRelayPrepareRemoteInput(sessionId, deviceId, false, ""); return; } - ret = PrepareRemoteInput(sessionId, peerDevId); + ret = PrepareRemoteInput(sessionId, deviceId); if (ret != DH_SUCCESS) { - callback_->onResponseRelayPrepareRemoteInput(sessionId, peerDevId, false); + callback_->onResponseRelayPrepareRemoteInput(sessionId, deviceId, false, ""); return; } } @@ -958,11 +1281,11 @@ void DistributedInputSourceTransport::ReceiveSrcTSrcRelayUnprepare(int32_t sessi return; } - std::string peerDevId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; - int32_t ret = UnprepareRemoteInput(sessionId, peerDevId); + std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; + int32_t ret = UnprepareRemoteInput(sessionId, deviceId); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, unprepare fail."); - callback_->onResponseRelayUnprepareRemoteInput(sessionId, peerDevId, false); + callback_->onResponseRelayUnprepareRemoteInput(sessionId, deviceId, false); return; } } @@ -972,17 +1295,18 @@ void DistributedInputSourceTransport::NotifyResponseRelayPrepareRemoteInput(int3 { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_PREPARE."); if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || - !recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID].is_number()) { + !recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID].is_number() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_PREPARE, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_PREPARE, sinkDevId is error."); return; } callback_->onResponseRelayPrepareRemoteInput(recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID], sinkDevId, - recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); + recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseRelayUnprepareRemoteInput(int32_t sessionId, @@ -994,19 +1318,14 @@ void DistributedInputSourceTransport::NotifyResponseRelayUnprepareRemoteInput(in DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_UNPREPARE, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_UNPREPARE, sinkDevId is error."); return; } callback_->onResponseRelayUnprepareRemoteInput(recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID], sinkDevId, recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); - - int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID]; - if (toSrcSessionId != sessionId) { - DHLOGE("Close to sink session."); - CloseInputSoftbus(sinkDevId); - } + CloseInputSoftbus(sessionId); } void DistributedInputSourceTransport::ReceiveRelayPrepareResult(int32_t sessionId, const nlohmann::json &recMsg) @@ -1039,7 +1358,7 @@ void DistributedInputSourceTransport::ReceiveRelayUnprepareResult(int32_t sessio std::string sinkId = recMsg[DINPUT_SOFTBUS_KEY_SINK_DEV_ID]; int32_t status = recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]; callback_->onReceiveRelayUnprepareResult(status, srcId, sinkId); - CloseInputSoftbus(srcId); + CloseInputSoftbus(sessionId); } void DistributedInputSourceTransport::ReceiveSrcTSrcRelayStartDhid(int32_t sessionId, const nlohmann::json &recMsg) @@ -1094,12 +1413,11 @@ void DistributedInputSourceTransport::NotifyResponseRelayStartDhidRemoteInput(in DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTDHID."); if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || !recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID].is_number() || - !recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID].is_string() || - !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { + !recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTDHID, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTDHID, sinkDevId is error."); return; @@ -1114,7 +1432,6 @@ void DistributedInputSourceTransport::NotifyResponseRelayStartDhidRemoteInput(in std::string dhids = recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID]; NotifyOriginStartDhidResult(srcTsrcSeId, localNetworkId, sinkDevId, result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, dhids); - callback_->onResponseRelayStartDhidRemoteInput(sinkDevId, recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseRelayStopDhidRemoteInput(int32_t sessionId, @@ -1127,7 +1444,7 @@ void DistributedInputSourceTransport::NotifyResponseRelayStopDhidRemoteInput(int DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPDHID, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPDHID, sinkDevId is error."); return; @@ -1232,12 +1549,11 @@ void DistributedInputSourceTransport::NotifyResponseRelayStartTypeRemoteInput(in DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTTYPE."); if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || !recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID].is_number() || - !recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE].is_number() || - !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { + !recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE].is_number()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTTYPE, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTTYPE, sinkDevId is error."); return; @@ -1252,7 +1568,6 @@ void DistributedInputSourceTransport::NotifyResponseRelayStartTypeRemoteInput(in uint32_t inputTypes = recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE]; NotifyOriginStartTypeResult(srcTsrcSeId, localNetworkId, sinkDevId, result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, inputTypes); - callback_->onResponseRelayStartTypeRemoteInput(sinkDevId, recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseRelayStopTypeRemoteInput(int32_t sessionId, @@ -1265,7 +1580,7 @@ void DistributedInputSourceTransport::NotifyResponseRelayStopTypeRemoteInput(int DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPTYPE, data type is error."); return; } - std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); + std::string sinkDevId = FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPTYPE, sinkDevId is error."); return; @@ -1318,21 +1633,18 @@ void DistributedInputSourceTransport::ReceiveRelayStopTypeResult(int32_t session callback_->onReceiveRelayStopTypeResult(status, srcId, sinkId, inputTypes); } -void DistributedInputSourceTransport::DInputTransbaseSourceListener::HandleSessionData(int32_t sessionId, - const std::string& message) -{ - DistributedInputSourceTransport::GetInstance().HandleData(sessionId, message); -} - -void DistributedInputSourceTransport::HandleData(int32_t sessionId, const std::string& message) +void DistributedInputSourceTransport::HandleSessionData(int32_t sessionId, const std::string& message) { if (callback_ == nullptr) { DHLOGE("OnBytesReceived the callback_ is null, the message:%s abort.", SetAnonyId(message).c_str()); return; } - nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); - uint32_t cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; + if (CheckRecivedData(message) != true) { + return; + } + + int cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; auto iter = memberFuncMap_.find(cmdType); if (iter == memberFuncMap_.end()) { DHLOGE("OnBytesReceived cmdType %d is undefined.", cmdType); @@ -1342,6 +1654,85 @@ void DistributedInputSourceTransport::HandleData(int32_t sessionId, const std::s (this->*func)(sessionId, recMsg); } +bool DistributedInputSourceTransport::CheckRecivedData(const std::string& message) +{ + nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); + if (recMsg.is_discarded()) { + DHLOGE("OnBytesReceived jsonStr error."); + return false; + } + + if (recMsg.contains(DINPUT_SOFTBUS_KEY_CMD_TYPE) != true) { + DHLOGE("OnBytesReceived message:%s is error, not contain cmdType.", SetAnonyId(message).c_str()); + return false; + } + + if (recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE].is_number() != true) { + DHLOGE("OnBytesReceived cmdType is not number type."); + return false; + } + + return true; +} + +void DistributedInputSourceTransport::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + DHLOGI("OnBytesReceived, sessionId: %d, dataLen:%d", sessionId, dataLen); + if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > MSG_MAX_SIZE) { + DHLOGE("OnBytesReceived param check failed"); + return; + } + + uint8_t *buf = reinterpret_cast(calloc(dataLen + 1, sizeof(uint8_t))); + if (buf == nullptr) { + DHLOGE("OnBytesReceived: malloc memory failed"); + return; + } + + if (memcpy_s(buf, dataLen + 1, reinterpret_cast(data), dataLen) != EOK) { + DHLOGE("OnBytesReceived: memcpy memory failed"); + free(buf); + return; + } + + std::string message(buf, buf + dataLen); + DHLOGI("OnBytesReceived message:%s.", SetAnonyId(message).c_str()); + HandleSessionData(sessionId, message); + + free(buf); + DHLOGI("OnBytesReceived completed"); + return; +} + +int32_t DistributedInputSourceTransport::GetCurrentSessionId() +{ + return sessionId_; +} + +// send message by sessionId (channel opened) +int32_t DistributedInputSourceTransport::SendMsg(int32_t sessionId, std::string &message) +{ + DHLOGD("start SendMsg"); + if (message.size() > MSG_MAX_SIZE) { + DHLOGE("SendMessage error: message.size() > MSG_MAX_SIZE"); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; + } + uint8_t *buf = reinterpret_cast(calloc((MSG_MAX_SIZE), sizeof(uint8_t))); + if (buf == nullptr) { + DHLOGE("SendMsg: malloc memory failed"); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; + } + int32_t outLen = 0; + if (memcpy_s(buf, MSG_MAX_SIZE, reinterpret_cast(message.c_str()), message.size()) != EOK) { + DHLOGE("SendMsg: memcpy memory failed"); + free(buf); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; + } + outLen = static_cast(message.size()); + int32_t ret = SendBytes(sessionId, buf, outLen); + free(buf); + return ret; +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/transportbase/BUILD.gn b/services/transportbase/BUILD.gn deleted file mode 100644 index a4ee241..0000000 --- a/services/transportbase/BUILD.gn +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") -import( - "//foundation/distributedhardware/distributed_input/distributedinput.gni") - -ohos_shared_library("libdinput_trans_base") { - include_dirs = [ - "include", - "${common_path}/include", - "//commonlibrary/c_utils/base/include", - "//utils/system/safwk/native/include", - "${fwk_common_path}/log/include", - "${fwk_common_path}/utils/include", - "${fwk_utils_path}/include/log", - "${fwk_utils_path}/include", - "//third_party/json/include", - "//foundation/communication/dsoftbus/interfaces/kits/transport", - "${service_common}/include", - "${dfx_utils_path}/include", - "${utils_path}/include", - "${low_latency_path}/include", - "${fwk_interfaces_path}/include", - "${fwk_interfaces_path}/include/ipc", - "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk", - "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", - "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", - "//foundation/distributedhardware/distributed_input/services/transportbase/include", - "${distributedinput_path}/services/source/inputinject/include", - "${distributedinput_path}/inputdevicehandler/include", - "${frameworks_path}/include", - ] - - sources = [ - "src/distributed_input_transport_base.cpp", - ] - - defines = [ - "HI_LOG_ENABLE", - "DH_LOG_TAG=\"distributedinputtransportbase\"", - "LOG_DOMAIN=0xD004100", - ] - - if (distributed_input_low_latency) { - defines += [ "DINPUT_LOW_LATENCY" ] - } - - deps = [ - "${dfx_utils_path}:libdinput_dfx_utils", - "${fwk_utils_path}:distributedhardwareutils", - "${low_latency_path}:libdinput_low_latency", - "${utils_path}:libdinput_utils", - "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", - "//foundation/communication/dsoftbus/sdk:softbus_client", - "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", - "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - ] - - external_deps = [ - "c_utils:utils", - "hitrace_native:hitrace_meter", - ] - - cflags_cc = [ "-DHILOG_ENABLE" ] - - subsystem_name = "distributedhardware" - - part_name = "distributed_input" -} diff --git a/services/transportbase/include/distributed_input_transport_base.h b/services/transportbase/include/distributed_input_transport_base.h deleted file mode 100644 index 5f93e93..0000000 --- a/services/transportbase/include/distributed_input_transport_base.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISTRIBUTED_INPUT_TRANSPORT_BASE_H -#define DISTRIBUTED_INPUT_TRANSPORT_BASE_H - -#include -#include -#include -#include -#include -#include -#include - -#include "constants.h" -#include "event_handler.h" -#include "nlohmann/json.hpp" -#include "securec.h" - -#include "dinput_transbase_source_callback.h" -#include "dinput_transbase_sink_callback.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -class DistributedInputTransportBase { -public: - static DistributedInputTransportBase &GetInstance(); - ~DistributedInputTransportBase(); - - int32_t Init(); - void Release(); - - int32_t StartSession(const std::string &remoteDevId); - void StopSession(const std::string &remoteDevId); - - void RegisterSrcHandleSessionCallback(std::shared_ptr callback); - void RegisterSinkHandleSessionCallback(std::shared_ptr callback); - - int32_t OnSessionOpened(int32_t sessionId, int32_t result); - void OnSessionClosed(int32_t sessionId); - void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); - - int32_t GetCurrentSessionId(); - int32_t CountSession(const std::string &remoteDevId); - void EraseSessionId(const std::string &remoteDevId); - int32_t GetSessionIdByDeviceId(const std::string &srcId); - std::string FindDeviceBySession(int32_t sessionId); - int32_t SendMsg(int32_t sessionId, std::string &message); - - int32_t LatencyCount(const std::string& deviceId); - void StartLatencyCount(const std::string& deviceId); - void StartLatencyThread(const std::string& deviceId); - void StopLatencyThread(); - void CalculateLatency(int32_t sessionId); - -private: - int32_t CheckDeviceSessionState(const std::string &remoteDevId); - bool CheckRecivedData(const std::string& message); - void HandleSession(int32_t sessionId, const std::string& message); - -private: - std::mutex operationMutex_; - std::string remoteDeviceId_; - std::map remoteDevSessionMap_; - std::map channelStatusMap_; - std::condition_variable openSessionWaitCond_; - std::string mySessionName_ = ""; - int32_t sessionId_ = 0; - - std::atomic isLatencyThreadRunning_ = false; - std::thread latencyThread_; - std::string eachLatencyDetails_ = ""; - - uint64_t deltaTime_ = 0; - uint64_t deltaTimeAll_ = 0; - uint64_t sendTime_ = 0; - uint32_t sendNum_ = 0; - uint32_t recvNum_ = 0; - - std::shared_ptr srcCallback_; - std::shared_ptr sinkCallback_; -}; - -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS - -#endif // DISTRIBUTED_INPUT_TRANSPORT_BASE_H \ No newline at end of file diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp deleted file mode 100644 index 6d60920..0000000 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ /dev/null @@ -1,510 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "distributed_input_transport_base.h" - -#include -#include - -#include "anonymous_string.h" -#include "distributed_hardware_log.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" - -#include "constants_dinput.h" -#include "dinput_errcode.h" -#include "dinput_hitrace.h" -#include "dinput_low_latency.h" -#include "dinput_softbus_define.h" -#include "dinput_utils_tool.h" -#include "distributed_input_inject.h" -#include "hidumper.h" -#include "session.h" -#include "softbus_bus_center.h" -#include "softbus_common.h" - -namespace OHOS { -namespace DistributedHardware { -namespace DistributedInput { -const int32_t DINPUT_LINK_TYPE_MAX = 4; -static SessionAttribute g_sessionAttr = { - .dataType = SessionType::TYPE_BYTES, - .linkTypeNum = DINPUT_LINK_TYPE_MAX, - .linkType = { - LINK_TYPE_WIFI_P2P, - LINK_TYPE_WIFI_WLAN_2G, - LINK_TYPE_WIFI_WLAN_5G, - LINK_TYPE_BR - } -}; - -DistributedInputTransportBase &DistributedInputTransportBase::GetInstance() -{ - static DistributedInputTransportBase instance; - return instance; -} - -DistributedInputTransportBase::~DistributedInputTransportBase() -{ - DHLOGI("Release Transport Session"); - Release(); -} - -static int32_t SessionOpened(int32_t sessionId, int32_t result) -{ - return DistributedInput::DistributedInputTransportBase::GetInstance().OnSessionOpened(sessionId, result); -} - -static void SessionClosed(int32_t sessionId) -{ - DistributedInput::DistributedInputTransportBase::GetInstance().OnSessionClosed(sessionId); -} - -static void BytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) -{ - DistributedInput::DistributedInputTransportBase::GetInstance().OnBytesReceived(sessionId, data, dataLen); -} - -static void MessageReceived(int32_t sessionId, const void *data, uint32_t dataLen) -{ - (void)sessionId; - (void)data; - (void)dataLen; - DHLOGI("sessionId: %d, dataLen:%d", sessionId, dataLen); -} - -static void StreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, - const StreamFrameInfo *param) -{ - (void)sessionId; - (void)data; - (void)ext; - (void)param; - DHLOGI("sessionId: %d", sessionId); -} - -int32_t DistributedInputTransportBase::Init() -{ - DHLOGI("Init Transport Base Session"); - ISessionListener iSessionListener = { - .OnSessionOpened = SessionOpened, - .OnSessionClosed = SessionClosed, - .OnBytesReceived = BytesReceived, - .OnMessageReceived = MessageReceived, - .OnStreamReceived = StreamReceived - }; - - auto localNode = std::make_unique(); - int32_t retCode = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), localNode.get()); - if (retCode != DH_SUCCESS) { - DHLOGE("Init Could not get local device id."); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; - } - std::string networkId = localNode->networkId; - DHLOGI("Init device local networkId is %s", GetAnonyString(networkId).c_str()); - - mySessionName_ = SESSION_NAME + networkId.substr(0, INTERCEPT_STRING_LENGTH); - - int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); - if (ret != DH_SUCCESS) { - DHLOGE("Init CreateSessionServer failed, error code %d.", ret); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; - } - return DH_SUCCESS; -} - -void DistributedInputTransportBase::Release() -{ - std::unique_lock sessionLock(operationMutex_); - auto iter = remoteDevSessionMap_.begin(); - for (; iter != remoteDevSessionMap_.end(); ++iter) { - CloseSession(iter->second); - } - (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); - remoteDevSessionMap_.clear(); - channelStatusMap_.clear(); -} - -int32_t DistributedInputTransportBase::CheckDeviceSessionState(const std::string &remoteDevId) -{ - std::unique_lock sessionLock(operationMutex_); - if (remoteDevSessionMap_.find(remoteDevId) == remoteDevSessionMap_.end()) { - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_DEVICE_SESSION_STATE; - } - DHLOGI("CheckDeviceSessionState has opened %s", GetAnonyString(remoteDevId).c_str()); - return DH_SUCCESS; -} - -std::string DistributedInputTransportBase::FindDeviceBySession(int32_t sessionId) -{ - std::unique_lock sessionLock(operationMutex_); - std::string devId = ""; - auto iter = remoteDevSessionMap_.begin(); - for (; iter != remoteDevSessionMap_.end(); ++iter) { - if (iter->second == sessionId) { - devId = iter->first; - break; - } - } - if (iter == remoteDevSessionMap_.end()) { - DHLOGE("findKeyByValue error."); - } - return devId; -} - -int32_t DistributedInputTransportBase::StartSession(const std::string &remoteDevId) -{ - int32_t ret = CheckDeviceSessionState(remoteDevId); - if (ret == DH_SUCCESS) { - DHLOGE("Softbus session has already opened, deviceId: %s", GetAnonyString(remoteDevId).c_str()); - return DH_SUCCESS; - } - - std::string peerSessionName = SESSION_NAME + remoteDevId.substr(0, INTERCEPT_STRING_LENGTH); - DHLOGI("OpenInputSoftbus peerSessionName:%s", peerSessionName.c_str()); - - StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); - int32_t sessionId = OpenSession(mySessionName_.c_str(), peerSessionName.c_str(), remoteDevId.c_str(), - GROUP_ID.c_str(), &g_sessionAttr); - if (sessionId < 0) { - DHLOGE("OpenSession fail, remoteDevId: %s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; - } - - HiDumper::GetInstance().CreateSessionInfo(remoteDevId, sessionId, mySessionName_, peerSessionName, - SessionStatus::OPENING); - - DHLOGI("Wait for channel session opened."); - { - std::unique_lock waitLock(operationMutex_); - auto status = openSessionWaitCond_.wait_for(waitLock, std::chrono::seconds(SESSION_WAIT_TIMEOUT_SECOND), - [this, remoteDevId] () { return channelStatusMap_[remoteDevId]; }); - if (!status) { - DHLOGE("OpenSession timeout, remoteDevId: %s, sessionId: %d", - GetAnonyString(remoteDevId).c_str(), sessionId); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_TIMEOUT; - } - } - - DHLOGI("OpenSession success, remoteDevId:%s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); - sessionId_ = sessionId; - - StartLatencyThread(remoteDevId); - -#ifdef DINPUT_LOW_LATENCY - DInputLowLatency::GetInstance().EnableSourceLowLatency(); -#endif - - HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::OPENED); - return DH_SUCCESS; -} - -void DistributedInputTransportBase::StopSession(const std::string &remoteDevId) -{ - std::unique_lock sessionLock(operationMutex_); - // check this device's all hd is close,this device session close. - - if (remoteDevSessionMap_.count(remoteDevId) == 0) { - DHLOGI("SessionDevIdMap Not find remoteDevId: %s", GetAnonyString(remoteDevId).c_str()); - return; - } - int32_t sessionId = remoteDevSessionMap_[remoteDevId]; - - StopLatencyThread(); - - DHLOGI("RemoteDevId: %s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); - HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::CLOSING); - CloseSession(sessionId); - remoteDevSessionMap_.erase(remoteDevId); - channelStatusMap_.erase(remoteDevId); - -#ifdef DINPUT_LOW_LATENCY - DInputLowLatency::GetInstance().DisableSourceLowLatency(); -#endif - - HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::CLOSED); - HiDumper::GetInstance().DeleteSessionInfo(remoteDevId); -} - -void DistributedInputTransportBase::RegisterSrcHandleSessionCallback( - std::shared_ptr callback) -{ - DHLOGI("RegisterTransbaseSourceRespCallback"); - srcCallback_ = callback; -} - -void DistributedInputTransportBase::RegisterSinkHandleSessionCallback( - std::shared_ptr callback) -{ - DHLOGI("RegisterTransbaseSinkRespCallback"); - sinkCallback_ = callback; -} - -int32_t DistributedInputTransportBase::CountSession(const std::string &remoteDevId) -{ - return remoteDevSessionMap_.count(remoteDevId); -} - -void DistributedInputTransportBase::EraseSessionId(const std::string &remoteDevId) -{ - remoteDevSessionMap_.erase(remoteDevId); -} - -int32_t DistributedInputTransportBase::OnSessionOpened(int32_t sessionId, int32_t result) -{ - if (result != DH_SUCCESS) { - DHLOGE("session open failed, sessionId: %d", sessionId); - return DH_SUCCESS; - } - - char mySessionName[SESSION_NAME_SIZE_MAX] = {0}; - char peerSessionName[SESSION_NAME_SIZE_MAX] = {0}; - char peerDevId[DEVICE_ID_SIZE_MAX] = {0}; - int32_t ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName)); - if (ret != DH_SUCCESS) { - DHLOGI("get my session name failed, session id is %d", sessionId); - } - ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName)); - if (ret != DH_SUCCESS) { - DHLOGI("get peer session name failed, session id is %d", sessionId); - } - ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); - if (ret != DH_SUCCESS) { - DHLOGI("get peer device id failed, session id is %d", sessionId); - } - - { - std::unique_lock sessionLock(operationMutex_); - remoteDevSessionMap_[peerDevId] = sessionId; - } - - int32_t sessionSide = GetSessionSide(sessionId); - DHLOGI("session open succeed, sessionId: %d, sessionSide:%d(1 is " - "client side), deviceId:%s", sessionId, sessionSide, GetAnonyString(peerDevId).c_str()); - - DHLOGI("mySessionName:%s, peerSessionName:%s, peerDevId:%s", - mySessionName, peerSessionName, GetAnonyString(peerDevId).c_str()); - { - std::lock_guard notifyLock(operationMutex_); - channelStatusMap_[peerDevId] = true; - openSessionWaitCond_.notify_all(); - } - - return DH_SUCCESS; -} - -void DistributedInputTransportBase::OnSessionClosed(int32_t sessionId) -{ - std::string deviceId = FindDeviceBySession(sessionId); - DHLOGI("OnSessionClosed, sessionId: %d, deviceId:%s", sessionId, GetAnonyString(deviceId).c_str()); - { - std::unique_lock sessionLock(operationMutex_); - if (CountSession(deviceId) > 0) { - EraseSessionId(deviceId); - } - } - channelStatusMap_.erase(deviceId); - DistributedInputInject::GetInstance().StopInjectThread(); - DHLOGI("StopInjectThread successed"); -} - -bool DistributedInputTransportBase::CheckRecivedData(const std::string& message) -{ - nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); - if (recMsg.is_discarded()) { - DHLOGE("OnBytesReceived jsonStr error."); - return false; - } - - if (recMsg.contains(DINPUT_SOFTBUS_KEY_CMD_TYPE) != true) { - DHLOGE("OnBytesReceived message:%s is error, not contain cmdType.", SetAnonyId(message).c_str()); - return false; - } - - if (recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE].is_number() != true) { - DHLOGE("OnBytesReceived cmdType is not number type."); - return false; - } - - return true; -} - -void DistributedInputTransportBase::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) -{ - DHLOGI("OnBytesReceived, sessionId: %d, dataLen:%d", sessionId, dataLen); - if (sessionId < 0 || data == nullptr || dataLen <= 0) { - DHLOGE("OnBytesReceived param check failed"); - return; - } - - uint8_t *buf = reinterpret_cast(calloc(dataLen + 1, sizeof(uint8_t))); - if (buf == nullptr) { - DHLOGE("OnBytesReceived: malloc memory failed"); - return; - } - - if (memcpy_s(buf, dataLen + 1, reinterpret_cast(data), dataLen) != EOK) { - DHLOGE("OnBytesReceived: memcpy memory failed"); - free(buf); - return; - } - - std::string message(buf, buf + dataLen); - DHLOGI("OnBytesReceived message:%s.", SetAnonyId(message).c_str()); - HandleSession(sessionId, message); - - free(buf); - DHLOGI("OnBytesReceived completed"); - return; -} - -void DistributedInputTransportBase::HandleSession(int32_t sessionId, const std::string& message) -{ - if (CheckRecivedData(message) != true) { - return; - } - nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); - uint32_t cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; - if (cmdType == TRANS_SINK_MSG_LATENCY) { - CalculateLatency(sessionId); - return; - } - if (cmdType < TRANS_MSG_SRC_SINK_SPLIT) { - srcCallback_->HandleSessionData(sessionId, message); - return; - } - if (cmdType > TRANS_MSG_SRC_SINK_SPLIT) { - sinkCallback_->HandleSessionData(sessionId, message); - } -} - -int32_t DistributedInputTransportBase::LatencyCount(const std::string& deviceId) -{ - std::unique_lock sessionLock(operationMutex_); - if (CountSession(deviceId) <= 0) { - DHLOGE("LatencyCount error, not find this device:%s.", GetAnonyString(deviceId).c_str()); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_LATENCY_FAIL; - } - - int32_t sessionId = GetSessionIdByDeviceId(deviceId); - nlohmann::json jsonStr; - jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_LATENCY; - jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId; - jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; - std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(sessionId, smsg); - if (ret != DH_SUCCESS) { - DHLOGE("LatencyCount deviceId:%s, sessionId: %d, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str(), ret); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_LATENCY_FAIL; - } - - DHLOGI("LatencyCount deviceId:%s, sessionId: %d, smsg:%s.", - GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); - return DH_SUCCESS; -} - -void DistributedInputTransportBase::StartLatencyCount(const std::string& deviceId) -{ - DHLOGI("start"); - while (isLatencyThreadRunning_.load()) { - if (sendNum_ >= INPUT_LATENCY_DELAY_TIMES) { - uint64_t latency = (uint64_t)(deltaTimeAll_ / 2 / INPUT_LATENCY_DELAY_TIMES); - DHLOGI("LatencyCount average single-channel latency is %d us, send times is %d, recive times is %d, " + - "each RTT latency details is %s", latency, sendNum_, recvNum_, eachLatencyDetails_.c_str()); - deltaTimeAll_ = 0; - sendNum_ = 0; - recvNum_ = 0; - eachLatencyDetails_ = ""; - } - sendTime_ = GetCurrentTime(); - LatencyCount(deviceId); - sendNum_ += 1; - usleep(INPUT_LATENCY_DELAYTIME_US); - } - DHLOGI("end"); -} - -void DistributedInputTransportBase::StartLatencyThread(const std::string& deviceId) -{ - DHLOGI("start"); - isLatencyThreadRunning_.store(true); - latencyThread_ = std::thread(&DistributedInputTransportBase::StartLatencyCount, this, deviceId); - DHLOGI("end"); -} - -void DistributedInputTransportBase::StopLatencyThread() -{ - DHLOGI("start"); - isLatencyThreadRunning_.store(false); - if (latencyThread_.joinable()) { - latencyThread_.join(); - } - DHLOGI("end"); -} - -void DistributedInputTransportBase::CalculateLatency(int32_t sessionId) -{ - DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_LATENCY."); - std::string deviceId = FindDeviceBySession(sessionId); - if (deviceId.empty()) { - DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_LATENCY, deviceId is error."); - return; - } - - deltaTime_ = GetCurrentTime() - sendTime_; - deltaTimeAll_ += deltaTime_; - recvNum_ += 1; - eachLatencyDetails_ += (std::to_string(deltaTime_) + DINPUT_SPLIT_COMMA); -} - -// send message by sessionId (channel opened) -int32_t DistributedInputTransportBase::SendMsg(int32_t sessionId, std::string &message) -{ - DHLOGD("start SendMsg"); - if (message.size() > MSG_MAX_SIZE) { - DHLOGE("SendMessage error: message.size() > MSG_MAX_SIZE"); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; - } - uint8_t *buf = reinterpret_cast(calloc((MSG_MAX_SIZE), sizeof(uint8_t))); - if (buf == nullptr) { - DHLOGE("SendMsg: malloc memory failed"); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; - } - int32_t outLen = 0; - if (memcpy_s(buf, MSG_MAX_SIZE, reinterpret_cast(message.c_str()), message.size()) != EOK) { - DHLOGE("SendMsg: memcpy memory failed"); - free(buf); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_SENDMESSSAGE; - } - outLen = static_cast(message.size()); - int32_t ret = SendBytes(sessionId, buf, outLen); - free(buf); - return ret; -} - -int32_t DistributedInputTransportBase::GetSessionIdByDeviceId(const std::string &srcId) -{ - std::map::iterator it = remoteDevSessionMap_.find(srcId); - if (it != remoteDevSessionMap_.end()) { - return it->second; - } - DHLOGE("get session id failed, srcId = %s", GetAnonyString(srcId).c_str()); - return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_GET_SESSIONID_FAIL; -} -} // namespace DistributedInput -} // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file