From e8eae9cc5eeed449202c29982d096e61c77c3bca Mon Sep 17 00:00:00 2001 From: xxxx Date: Thu, 27 Oct 2022 15:23:35 +0800 Subject: [PATCH] Description:fix full-duplex session Match-id-e1e364652cce610b42c19ef3a81f40c5f81de1d7 --- 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 | 393 ++++----- services/sink/transport/BUILD.gn | 2 + .../distributed_input_sink_transport.h | 28 +- .../src/distributed_input_sink_transport.cpp | 325 ++------ 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 | 747 +++++------------- services/transportbase/BUILD.gn | 80 ++ .../distributed_input_transport_base.h | 101 +++ .../src/distributed_input_transport_base.cpp | 510 ++++++++++++ 22 files changed, 1495 insertions(+), 1192 deletions(-) create mode 100644 services/common/include/dinput_transbase_sink_callback.h create mode 100644 services/common/include/dinput_transbase_source_callback.h create mode 100644 services/transportbase/BUILD.gn create mode 100644 services/transportbase/include/distributed_input_transport_base.h create mode 100644 services/transportbase/src/distributed_input_transport_base.cpp diff --git a/bundle.json b/bundle.json index 68c8d98..34f9420 100755 --- a/bundle.json +++ b/bundle.json @@ -56,6 +56,7 @@ "//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 c1c7351..2583ca2 100644 --- a/services/common/include/dinput_softbus_define.h +++ b/services/common/include/dinput_softbus_define.h @@ -42,8 +42,7 @@ namespace DistributedInput { const uint32_t INTERCEPT_STRING_LENGTH = 20; const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; - const std::string SESSION_NAME_SOURCE = "ohos.dhardware.dinput.so_"; - const std::string SESSION_NAME_SINK = "ohos.dhardware.dinput.si_"; + const std::string SESSION_NAME = "ohos.dhardware.dinput.session"; const std::string GROUP_ID = "input_softbus_group_id"; #define DINPUT_SOFTBUS_KEY_CMD_TYPE "dinput_softbus_key_cmd_type" @@ -61,7 +60,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" - // json value + // src will receive const uint32_t TRANS_SINK_MSG_ONPREPARE = 1; const uint32_t TRANS_SINK_MSG_ONUNPREPARE = 2; const uint32_t TRANS_SINK_MSG_ONSTART = 3; @@ -71,45 +70,42 @@ 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; - // 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; + 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; - 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; + // src or sink + const uint32_t TRANS_MSG_SRC_SINK_SPLIT = 30; - 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; + // 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; } // 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 23a1e64..25f1346 100644 --- a/services/common/include/dinput_source_trans_callback.h +++ b/services/common/include/dinput_source_trans_callback.h @@ -25,19 +25,22 @@ 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, const std::string &object) = 0; + virtual void onResponsePrepareRemoteInput(const std::string deviceId, bool result) = 0; virtual void onResponseUnprepareRemoteInput(const std::string deviceId, bool result) = 0; - virtual void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result) = 0; + virtual void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result, + const std::string& object) = 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) = 0; + virtual void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result, + const std::string& object) = 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, - const std::string &object) = 0; + virtual void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result) = 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 new file mode 100644 index 0000000..fe6b2ae --- /dev/null +++ b/services/common/include/dinput_transbase_sink_callback.h @@ -0,0 +1,32 @@ +/* + * 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 new file mode 100644 index 0000000..e45dcc4 --- /dev/null +++ b/services/common/include/dinput_transbase_source_callback.h @@ -0,0 +1,32 @@ +/* + * 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 3d97076..64fc63c 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 6e65034..7ebb2d6 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -31,6 +31,7 @@ #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" @@ -48,6 +49,8 @@ class DistributedInputSinkManager : public SystemAbility, public DistributedInpu DECLARE_SYSTEM_ABILITY(DistributedInputSinkManager) public: + static DistributedInputSinkManager &GetInstance(); + DistributedInputSinkManager() = default; DistributedInputSinkManager(int32_t saId, bool runOnCreate); ~DistributedInputSinkManager(); @@ -153,6 +156,8 @@ 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 62523ce..ee559b4 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -26,7 +26,6 @@ #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" @@ -60,6 +59,12 @@ DistributedInputSinkManager::~DistributedInputSinkManager() projectWindowListener_ = nullptr; } +DistributedInputSinkManager &DistributedInputSinkManager::GetInstance() +{ + static DistributedInputSinkManager instance; + return instance; +} + DistributedInputSinkManager::DInputSinkListener::DInputSinkListener(DistributedInputSinkManager *manager) { sinkManagerObj_ = manager; @@ -74,6 +79,28 @@ 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) { @@ -81,41 +108,8 @@ 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; - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object; - smsg = jsonStr.dump(); + std::string smsg = jsonStr.dump(); DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); } @@ -123,7 +117,6 @@ 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; @@ -135,56 +128,23 @@ void DistributedInputSinkManager::DInputSinkListener::onUnprepareRemoteInput(con void DistributedInputSinkManager::DInputSinkListener::onRelayPrepareRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, const std::string &deviceId) { - DHLOGI("toSinkSessionId: %s, devId: %s", GetAnonyInt32(toSinkSessionId).c_str(), GetAnonyString(deviceId).c_str()); + DHLOGI("onRelayPrepareRemoteInput called, toSinkSessionId: %d, devId: %s", toSinkSessionId, + 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; - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = object; - smsg = jsonStr.dump(); + std::string 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("toSinkSessionId: %d, devId: %s", toSinkSessionId, GetAnonyString(deviceId).c_str()); - DistributedInputCollector::GetInstance().SetSharingTypes(false, static_cast(DInputDeviceType::ALL)); - DistributedInputSinkSwitch::GetInstance().StopAllSwitch(); - DistributedInputSinkSwitch::GetInstance().RemoveSession(toSinkSessionId); + DHLOGI("onRelayUnprepareRemoteInput called, toSinkSessionId: %d, devId: %s", toSinkSessionId, + GetAnonyString(deviceId).c_str()); + onStopRemoteInput(toSrcSessionId, static_cast(DInputDeviceType::ALL)); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_ON_RELAY_UNPREPARE; @@ -197,41 +157,57 @@ void DistributedInputSinkManager::DInputSinkListener::onRelayUnprepareRemoteInpu void DistributedInputSinkManager::DInputSinkListener::onStartRemoteInput( const int32_t& sessionId, const uint32_t& inputTypes) { - 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) ? 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 smsgSta = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsgSta); - - // 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); + 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 startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(sessionId); + sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE + : DInputServerType::NULL_SERVER_TYPE); + bool result = (startRes == DH_SUCCESS); + jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; + jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; + std::string smsg = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); + + if (startRes != DH_SUCCESS) { + DHLOGE("onStartRemoteInput startSwitch error."); + return; + } + + 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) { - 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) { + 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(); } } @@ -240,6 +216,10 @@ 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)); @@ -266,48 +246,63 @@ 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) { - 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; + DHLOGI("onStartRemoteInputDhid called, : sessionId: %d", sessionId); + 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_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 smsgSta = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsgSta); + std::string smsg = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(sessionId, smsg); if (startRes != DH_SUCCESS) { DHLOGE("onStartRemoteInputDhid StartSwitch error."); return; } - if (curSessionId == ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) { - DHLOGW("onStartRemoteInputDhid called, this is the only session."); - } - 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); - } + + 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); @@ -335,54 +330,69 @@ 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("all session is stop."); + DHLOGI("onStartRemoteInput called, 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) { - 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; - jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; - jsonStrSta[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = strDhids; - jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; - std::string smsgSta = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsgSta); - - if (startRes != DH_SUCCESS) { - DHLOGE("StartSwitch error."); + DHLOGI("onRelayStartDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); + if (sinkManagerObj_ == nullptr) { + DHLOGE("sinkManagerObj is null."); return; } - if (curSessionId == ERR_DH_INPUT_SERVER_SINK_GET_OPEN_SESSION_FAIL) { - DHLOGW("this is the only session."); + int32_t ret = DistributedInputCollector::GetInstance().Init( + DistributedInputSinkTransport::GetInstance().GetEventHandler()); + 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); + + if (startRes != DH_SUCCESS) { + DHLOGE("onRelayStartDhidRemoteInput startSwitch error."); + return; + } + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, toSinkSessionId, 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(toSinkSessionId, affDhIds.sharingDhIds); - DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); - } + 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("toSinkSessionId: %d", toSinkSessionId); + DHLOGI("onRelayStopDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); + if (sinkManagerObj_ == nullptr) { + DHLOGE("sinkManagerObj is null."); + return; + } + std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; StringSplit(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); @@ -415,55 +425,75 @@ 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) { - 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); + DHLOGI("onRelayStartTypeRemoteInput called, toSinkSessionId: %d", toSinkSessionId); + if (sinkManagerObj_ == nullptr) { + DHLOGE("sinkManagerObj is null."); + return; + } - sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE - : DInputServerType::NULL_SERVER_TYPE); - - bool result = (startRes == DH_SUCCESS) ? true : false; + 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 startRes = DistributedInputSinkSwitch::GetInstance().StartSwitch(toSinkSessionId); + sinkManagerObj_->SetStartTransFlag((startRes == DH_SUCCESS) ? DInputServerType::SINK_SERVER_TYPE + : DInputServerType::NULL_SERVER_TYPE); + bool result = (startRes == DH_SUCCESS); jsonStrSta[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; jsonStrSta[DINPUT_SOFTBUS_KEY_RESP_VALUE] = result; jsonStrSta[DINPUT_SOFTBUS_KEY_SESSION_ID] = toSrcSessionId; - std::string smsgSta = jsonStrSta.dump(); - DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsgSta); + std::string smsg = jsonStrSta.dump(); + DistributedInputSinkTransport::GetInstance().RespStartRemoteInput(toSinkSessionId, smsg); if (startRes != DH_SUCCESS) { - DHLOGE("StartSwitch error."); return; } - if (sinkManagerObj_->GetEventHandler() == nullptr) { - DHLOGE("eventhandler is null."); - return; - } + sinkManagerObj_->SetInputTypes(sinkManagerObj_->GetInputTypes() | inputTypes); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingTypes(true, + sinkManagerObj_->GetInputTypes()); + sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); + DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast(DInputDeviceType::MOUSE)) != 0; - 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(); - } + 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(); } } 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()); @@ -493,6 +523,7 @@ 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, @@ -1043,7 +1074,7 @@ int32_t DistributedInputSinkManager::Dump(int32_t fd, const std::vector callback); @@ -48,10 +59,6 @@ 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); @@ -63,12 +70,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& messageData); + void HandleSessionData(int32_t sessionId, const std::string& message); + void HandleData(int32_t sessionId, const std::string& message); + void RegRespFunMap(); 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); @@ -84,11 +91,14 @@ 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_; }; } // 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 1dac7b4..7458d00 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -32,10 +32,12 @@ #include "session.h" #include "softbus_bus_center.h" +#include "distributed_input_transport_base.h" + namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -DistributedInputSinkTransport::DistributedInputSinkTransport() : sessionDevMap_({}), mySessionName_("") +DistributedInputSinkTransport::DistributedInputSinkTransport() : mySessionName_("") { std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); eventHandler_ = std::make_shared(runner); @@ -45,7 +47,6 @@ DistributedInputSinkTransport::DistributedInputSinkTransport() : sessionDevMap_( DistributedInputSinkTransport::~DistributedInputSinkTransport() { DHLOGI("~DistributedInputSinkTransport"); - sessionDevMap_.clear(); (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); } @@ -54,38 +55,6 @@ 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; @@ -99,10 +68,10 @@ void DistributedInputSinkTransport::DInputSinkEventHandler::ProcessEvent(const A switch (eventId) { case EHandlerMsgType::DINPUT_SINK_EVENT_HANDLER_MSG: { std::shared_ptr innerMsg = event->GetSharedObject(); - 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(); + 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(); RecordEventLog(innerMsg); int32_t sessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession(); if (sessionId > 0) { @@ -120,30 +89,18 @@ void DistributedInputSinkTransport::DInputSinkEventHandler::ProcessEvent(const A int32_t DistributedInputSinkTransport::Init() { - // 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); + DHLOGI("Init Sink Transport"); - int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + int32_t ret = DistributedInputTransportBase::GetInstance().Init(); if (ret != DH_SUCCESS) { - DHLOGE("Init CreateSessionServer failed, error code %d.", ret); - return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_INIT_FAIL; + DHLOGE("Init Sink Transport Failed"); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; } + + statuslistener_ = std::make_shared(this); + DistributedInputTransportBase::GetInstance().RegisterSinkHandleSessionCallback(statuslistener_); + + RegRespFunMap(); return DH_SUCCESS; } @@ -176,7 +133,8 @@ 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()); @@ -192,7 +150,8 @@ int32_t DistributedInputSinkTransport::RespUnprepareRemoteInput(const int32_t se } } -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()); @@ -264,141 +223,20 @@ void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, int32_t DistributedInputSinkTransport::SendMessage(int32_t sessionId, std::string &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; + return DistributedInputTransportBase::GetInstance().SendMsg(sessionId, message); } -int32_t DistributedInputSinkTransport::GetSessionIdByNetId(const std::string &srcId) +DistributedInputSinkTransport::DInputTransbaseSinkListener::DInputTransbaseSinkListener( + DistributedInputSinkTransport *transport) { - 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; + sinkTransportObj_ = transport; + DHLOGI("DInputTransbaseSinkListener init."); } -void DistributedInputSinkTransport::GetDeviceIdBySessionId(int32_t sessionId, std::string &srcId) +DistributedInputSinkTransport::DInputTransbaseSinkListener::~DInputTransbaseSinkListener() { - 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; + sinkTransportObj_ = nullptr; + DHLOGI("DInputTransbaseSinkListener destory."); } void DistributedInputSinkTransport::NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) @@ -630,7 +468,35 @@ void DistributedInputSinkTransport::NotifyRelayStopTypeRemoteInput(int32_t sessi callback_->onRelayStopTypeRemoteInput(toSrcSessionId, sessionId, deviceId, inputTypes); } -void DistributedInputSinkTransport::HandleSessionData(int32_t sessionId, const std::string& message) +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) { if (callback_ == nullptr) { DHLOGE("OnBytesReceived the callback_ is null, the message:%s abort.", SetAnonyId(message).c_str()); @@ -638,79 +504,14 @@ void DistributedInputSinkTransport::HandleSessionData(int32_t sessionId, const s } nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false); - if (recMsg.is_discarded()) { - DHLOGE("OnBytesReceived jsonStr error."); + 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); return; } - - 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; - } + SinkTransportFunc &func = iter->second; + (this->*func)(sessionId, recMsg); } void DistributedInputSinkTransport::CloseAllSession() diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 62c77a1..4f0c1cf 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -22,6 +22,7 @@ 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 5644541..acf9f96 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -63,6 +63,7 @@ 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 93841d0..0b78cca 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -38,14 +38,15 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadRunning_(false), - inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) +DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), + 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(); @@ -179,30 +180,37 @@ int32_t DistributedInputNodeManager::getDevice(const std::string& dhId, VirtualD { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.find(dhId); - if (iter != virtualDeviceMap_.end()) { - device = iter->second.get(); - return DH_SUCCESS; + if (iter == virtualDeviceMap_.end()) { + return ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL; } - return ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL; + device = iter->second.get(); + return DH_SUCCESS; } void DistributedInputNodeManager::StartInjectThread() { - DHLOGI("start"); + if (isInjectThreadCreated_.load()) { + DHLOGI("InjectThread has been created."); + return; + } + DHLOGI("InjectThread does not created"); + isInjectThreadCreated_.store(true); isInjectThreadRunning_.store(true); eventInjectThread_ = std::thread(&DistributedInputNodeManager::InjectEvent, this); - DHLOGI("end"); } void DistributedInputNodeManager::StopInjectThread() { - DHLOGI("start"); + if (!isInjectThreadCreated_.load()) { + DHLOGI("InjectThread does not created, and not need to stop."); + } + DHLOGI("InjectThread has been created, and soon will be stopped."); 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 8bd8c52..5770e8d 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -29,6 +29,7 @@ #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" @@ -73,6 +74,7 @@ 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"; @@ -188,18 +190,21 @@ 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, const std::string &object); + void onResponsePrepareRemoteInput(const std::string deviceId, bool result); void onResponseUnprepareRemoteInput(const std::string deviceId, bool result); - void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result); + void onResponseStartRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result, + const std::string &object); void onResponseStopRemoteInput(const std::string deviceId, const uint32_t inputTypes, bool result); - void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result); + void onResponseStartRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result, + const std::string &object); 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, - const std::string &object); + void onResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result); 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); @@ -304,26 +309,16 @@ 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, 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 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 RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, const int32_t &status); + void RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, const int32_t &status, + const std::string& object); 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 f78889d..c4e1401 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -44,7 +44,6 @@ #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 { @@ -83,7 +82,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) { @@ -106,27 +105,25 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRegisterDist } void DistributedInputSourceManager::DInputSourceListener::onResponsePrepareRemoteInput(const std::string deviceId, - bool result, const std::string &object) + bool result) { 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, object); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL); 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); @@ -140,7 +137,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) { @@ -161,11 +158,11 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseUnprepareRem } void DistributedInputSourceManager::DInputSourceListener::onResponseRelayPrepareRemoteInput(int32_t toSrcSessionId, - const std::string &deviceId, bool result, const std::string &object) + const std::string &deviceId, bool result) { - DHLOGI("deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); + DHLOGI("onResponseRelayPrepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj_ is null."); + DHLOGE("onResponseRelayPrepareRemoteInput sourceManagerObj is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -176,7 +173,6 @@ 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( @@ -187,9 +183,9 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseRelayPrepare void DistributedInputSourceManager::DInputSourceListener::onResponseRelayUnprepareRemoteInput(int32_t toSrcSessionId, const std::string &deviceId, bool result) { - DHLOGI("deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); + DHLOGI("onResponseRelayUnprepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result); if (sourceManagerObj_ == nullptr) { - DHLOGE("sourceManagerObj_ is null."); + DHLOGE("onResponseRelayUnprepareRemoteInput sourceManagerObj is null."); return; } if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) { @@ -207,19 +203,41 @@ 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 deviceId, const uint32_t inputTypes, bool result, const std::string &object) { 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); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object); DHLOGE("onResponseStartRemoteInput GetCallbackEventHandler is null."); return; } @@ -231,6 +249,7 @@ 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( @@ -245,7 +264,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) { @@ -267,19 +286,19 @@ void DistributedInputSourceManager::DInputSourceListener::onResponseStopRemoteIn } void DistributedInputSourceManager::DInputSourceListener::onResponseStartRemoteInputDhid( - const std::string deviceId, const std::string &dhids, bool result) + const std::string deviceId, const std::string &dhids, bool result, const std::string &object) { 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); + ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object); return; } if (result) { @@ -290,6 +309,7 @@ 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 = @@ -304,7 +324,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) { @@ -330,7 +350,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) { @@ -385,7 +405,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) { @@ -408,7 +428,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) { @@ -431,7 +451,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) { @@ -455,7 +475,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) { @@ -479,7 +499,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) { @@ -503,7 +523,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) { @@ -608,6 +628,11 @@ 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); @@ -635,6 +660,11 @@ 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); @@ -651,20 +681,29 @@ 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, object); + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_MSG_IS_BAD); } 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); @@ -680,11 +719,17 @@ 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); @@ -695,12 +740,17 @@ 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); + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, object); } 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); @@ -733,6 +783,11 @@ 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(); @@ -740,16 +795,20 @@ 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]; - if (sourceManagerObj_ != nullptr) { - sourceManagerObj_->RunStartDhidCallback(deviceId, dhidStr, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD); - } + sourceManagerObj_->RunStartDhidCallback(deviceId, dhidStr, + result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD, object); } 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(); @@ -758,15 +817,18 @@ void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopD std::string dhidStr = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID]; bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT]; - if (sourceManagerObj_ != nullptr) { - sourceManagerObj_->RunStopDhidCallback(deviceId, dhidStr, - result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_MSG_IS_BAD); - } + 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(); @@ -776,19 +838,24 @@ 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]; - if (sourceManagerObj_ != nullptr) { - sourceManagerObj_->RunKeyStateCallback(deviceId, dhid, keyType, keyCode, keyValue); - } + + 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); } @@ -796,6 +863,11 @@ 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); @@ -809,6 +881,11 @@ 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); @@ -827,9 +904,7 @@ 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; @@ -842,7 +917,6 @@ 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( @@ -871,6 +945,11 @@ 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); @@ -885,6 +964,11 @@ 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); @@ -899,6 +983,11 @@ 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); @@ -913,6 +1002,11 @@ 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); @@ -1584,7 +1678,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; @@ -1613,7 +1707,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; @@ -2038,7 +2132,7 @@ int32_t DistributedInputSourceManager::RelayPrepareRemoteInput(const std::string } } - int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbusForRelay(srcId); + int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(srcId); if (ret != DH_SUCCESS) { DHLOGE("Open softbus session fail."); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; @@ -2049,7 +2143,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; @@ -2078,7 +2172,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; @@ -2107,7 +2201,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; @@ -2136,7 +2230,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; @@ -2181,7 +2275,7 @@ void DistributedInputSourceManager::RunUnregisterCallback( } void DistributedInputSourceManager::RunPrepareCallback( - const std::string& devId, const int32_t& status, const std::string& object) + const std::string& devId, const int32_t& status) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK); for (auto iter = preCallbacks_.begin(); iter != preCallbacks_.end(); ++iter) { @@ -2189,7 +2283,6 @@ void DistributedInputSourceManager::RunPrepareCallback( DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_PREPARE_MSG"); iter->preCallback->OnResult(devId, status); preCallbacks_.erase(iter); - RunWhiteListCallback(devId, object); return; } } @@ -2213,7 +2306,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); @@ -2228,7 +2321,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); @@ -2263,7 +2356,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& devId, const uint32_t& inputTypes, const int32_t& status, const std::string& object) { FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); for (auto iter = staCallbacks_.begin(); iter != staCallbacks_.end(); ++iter) { @@ -2271,6 +2364,7 @@ void DistributedInputSourceManager::RunStartCallback( DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_MSG"); iter->callback->OnResult(devId, inputTypes, status); staCallbacks_.erase(iter); + RunWhiteListCallback(devId, object); break; } } @@ -2291,7 +2385,7 @@ void DistributedInputSourceManager::RunStopCallback( } void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds, - const int32_t &status) + const int32_t &status, const std::string& object) { std::vector dhidsVec; StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); @@ -2308,6 +2402,7 @@ 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 8ff351e..e73eb66 100755 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -38,6 +38,7 @@ 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" ] @@ -60,6 +61,7 @@ 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 1c39e1d..7e69eb4 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,6 +30,7 @@ #include "securec.h" #include "dinput_source_trans_callback.h" +#include "dinput_transbase_source_callback.h" namespace OHOS { namespace DistributedHardware { @@ -39,23 +40,28 @@ 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 int32_t sessionId); - // this open deviceA.sourceSa ----> deviceB.sourceSa softbus. - int32_t OpenInputSoftbusForRelay(const std::string &srcId); + void CloseInputSoftbus(const std::string &remoteDevId); + 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); @@ -82,12 +88,9 @@ public: int32_t GetCurrentSessionId(); private: - 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); + int32_t SendMessage(int32_t sessionId, std::string &message); + void HandleData(int32_t sessionId, const std::string& message); 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); @@ -128,13 +131,6 @@ 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(); @@ -142,19 +138,11 @@ 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 a22fde6..2fff68c 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -36,20 +36,11 @@ #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() { @@ -57,38 +48,6 @@ 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; @@ -97,30 +56,16 @@ DistributedInputSourceTransport &DistributedInputSourceTransport::GetInstance() int32_t DistributedInputSourceTransport::Init() { - DHLOGI("Init"); - ISessionListener iSessionListener = { - .OnSessionOpened = SessionOpened, - .OnSessionClosed = SessionClosed, - .OnBytesReceived = BytesReceived, - .OnMessageReceived = MessageReceived, - .OnStreamReceived = StreamReceived - }; + DHLOGI("Init Source Transport"); - 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); - - int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + int32_t ret = DistributedInputTransportBase::GetInstance().Init(); if (ret != DH_SUCCESS) { - DHLOGE("Init CreateSessionServer failed, error code %d.", ret); - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; + DHLOGE("Init Source Transport failed."); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; } + + statuslistener_ = std::make_shared(this); + DistributedInputTransportBase::GetInstance().RegisterSrcHandleSessionCallback(statuslistener_); RegRespFunMap(); return DH_SUCCESS; } @@ -132,7 +77,6 @@ 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; @@ -175,180 +119,35 @@ void DistributedInputSourceTransport::RegRespFunMap() void DistributedInputSourceTransport::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(); -} + DHLOGI("Release Source Transport"); -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; + DistributedInputTransportBase::GetInstance().Release(); + + DistributedInputInject::GetInstance().StopInjectThread(); } int32_t DistributedInputSourceTransport::OpenInputSoftbus(const std::string &remoteDevId) { - 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 int32_t sessionId) -{ - 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(); + int32_t ret = DistributedInputTransportBase::GetInstance().StartSession(remoteDevId); if (ret != DH_SUCCESS) { - return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; + DHLOGE("StartSession fail! remoteDevId:%s.", GetAnonyString(remoteDevId).c_str()); + return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_PREPARE_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::CloseInputSoftbus(const std::string &remoteDevId) +{ + DistributedInputTransportBase::GetInstance().StopSession(remoteDevId); +} + void DistributedInputSourceTransport::RegisterSourceRespCallback(std::shared_ptr callback) { DHLOGI("RegisterSourceRespCallback"); 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 @@ -356,7 +155,7 @@ int32_t DistributedInputSourceTransport::FindSessionIdByDevId(bool isToSrc, cons */ int32_t DistributedInputSourceTransport::PrepareRemoteInput(const std::string& deviceId) { - int32_t sessionId = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -368,20 +167,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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, smsg:%s.", + DHLOGI("PrepareRemoteInput devId:%s, sessionId:%d, msg:%s.", GetAnonyString(deviceId).c_str(), sessionId, SetAnonyId(smsg).c_str()); return DH_SUCCESS; } int32_t DistributedInputSourceTransport::UnprepareRemoteInput(const std::string& deviceId) { - int32_t sessionId = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -393,7 +192,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -404,61 +203,60 @@ int32_t DistributedInputSourceTransport::UnprepareRemoteInput(const std::string& return DH_SUCCESS; } - -int32_t DistributedInputSourceTransport::PrepareRemoteInput(int32_t srcTsrcSeId, const std::string &deviceId) +int32_t DistributedInputSourceTransport::PrepareRemoteInput(int32_t srcTsrcSeId, const std::string &peerDevId) { - int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(peerDevId); if (sinkSessionId < 0) { - DHLOGE("PrepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); + DHLOGE("PrepareRemoteInput error, not find this device:%s.", GetAnonyString(peerDevId).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] = deviceId; + jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = peerDevId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(sinkSessionId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("PrepareRemoteInput deviceId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("PrepareRemoteInput peerDevId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyString(peerDevId).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(deviceId).c_str(), SetAnonyId(smsg).c_str()); + GetAnonyString(peerDevId).c_str(), SetAnonyId(smsg).c_str()); return DH_SUCCESS; } - -int32_t DistributedInputSourceTransport::UnprepareRemoteInput(int32_t srcTsrcSeId, const std::string &deviceId) +int32_t DistributedInputSourceTransport::UnprepareRemoteInput(int32_t srcTsrcSeId, const std::string &peerDevId) { - int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(peerDevId); if (sinkSessionId < 0) { - DHLOGE("UnprepareRemoteInput error, not find this device:%s.", GetAnonyString(deviceId).c_str()); + DHLOGE("UnprepareRemoteInput error, not find this device:%s.", GetAnonyString(peerDevId).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] = deviceId; + jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = peerDevId; jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = srcTsrcSeId; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(sinkSessionId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("UnprepareRemoteInput deviceId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); + DHLOGE("UnprepareRemoteInput peerDevId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyString(peerDevId).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(deviceId).c_str(), SetAnonyId(smsg).c_str()); + GetAnonyString(peerDevId).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 = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -471,7 +269,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 = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(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); @@ -479,13 +277,15 @@ 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 = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -498,7 +298,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 = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(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); @@ -512,7 +312,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 = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -525,21 +325,23 @@ 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 = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(sinkSessionId, smsg); if (ret != DH_SUCCESS) { DHLOGE("StartRemoteInputType deviceId:%s, smsg:%s, SendMsg error, ret:%d.", - GetAnonyString(deviceId).c_str(), smsg.c_str(), ret); + GetAnonyString(deviceId).c_str(), SetAnonyId(smsg).c_str(), ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_START_FAIL; } - DHLOGI("StartRemoteInputType send success, devId:%s, smsg:%s.", GetAnonyString(deviceId).c_str(), + DHLOGI("StartRemoteInputType 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::StopRemoteInputType(int32_t srcTsrcSeId, const std::string &deviceId, const uint32_t& inputTypes) { - int32_t sinkSessionId = FindSessionIdByDevId(false, deviceId); + int32_t sinkSessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -552,7 +354,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 = SendMsg(sinkSessionId, smsg); + int32_t ret = SendMessage(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); @@ -565,7 +367,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputType(int32_t srcTsrcSeId int32_t DistributedInputSourceTransport::SendRelayPrepareRequest(const std::string &srcId, const std::string &sinkId) { - int32_t sessionId = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -576,7 +378,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -589,7 +391,7 @@ int32_t DistributedInputSourceTransport::SendRelayPrepareRequest(const std::stri int32_t DistributedInputSourceTransport::SendRelayUnprepareRequest(const std::string &srcId, const std::string &sinkId) { - int32_t sessionId = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -600,14 +402,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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:%s, smsg:%s.", - GetAnonyString(srcId).c_str(), GetAnonyInt32(sessionId).c_str(), SetAnonyId(smsg).c_str()); + DHLOGI("SendRelayUnprepareRequest srcId:%s, sessionId:%d, smsg:%s.", + GetAnonyString(srcId).c_str(), sessionId, SetAnonyId(smsg).c_str()); return DH_SUCCESS; } @@ -622,10 +424,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginPrepareResult(int32_t srcTs jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = status; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginPrepareResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginPrepareResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -643,10 +445,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginUnprepareResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = status; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginUnprepareResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginUnprepareResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -665,10 +467,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStartDhidResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStartDhidResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStartDhidResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -687,10 +489,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStopDhidResult(int32_t srcT jsonStr[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = dhids; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStopDhidResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStopDhidResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -709,10 +511,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStartTypeResult(int32_t src jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStartTypeResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStartTypeResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -731,10 +533,10 @@ int32_t DistributedInputSourceTransport::NotifyOriginStopTypeResult(int32_t srcT jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(srcTsrcSeId, smsg); + int32_t ret = SendMessage(srcTsrcSeId, smsg); if (ret != DH_SUCCESS) { - DHLOGE("NotifyOriginStopTypeResult srcTsrcSeId:%d, smsg:%s, SendMsg error, ret:%d.", - srcTsrcSeId, SetAnonyId(smsg).c_str(), ret); + DHLOGE("NotifyOriginStopTypeResult srcTsrcSeId:%s, smsg:%s, SendMsg error, ret:%d.", + GetAnonyInt32(srcTsrcSeId).c_str(), 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()); @@ -742,10 +544,9 @@ 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 = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -758,7 +559,7 @@ int32_t DistributedInputSourceTransport::StartRemoteInput( jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = sessionId; jsonStr[DINPUT_SOFTBUS_KEY_INPUT_TYPE] = inputTypes; std::string smsg = jsonStr.dump(); - int32_t ret = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -766,13 +567,17 @@ int32_t DistributedInputSourceTransport::StartRemoteInput( } 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 = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -785,7 +590,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -796,74 +601,10 @@ 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 = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -876,7 +617,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -884,13 +625,17 @@ 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 = FindSessionIdByDevId(false, deviceId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -903,7 +648,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -917,7 +662,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 = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -929,7 +674,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -943,7 +688,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 = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -955,7 +700,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -969,7 +714,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 = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -981,7 +726,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -995,7 +740,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 = FindSessionIdByDevId(true, srcId); + int32_t sessionId = DistributedInputTransportBase::GetInstance().GetSessionIdByDeviceId(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; @@ -1007,7 +752,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 = SendMsg(sessionId, smsg); + int32_t ret = SendMessage(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); @@ -1027,100 +772,43 @@ 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; + return DistributedInputTransportBase::GetInstance().SendMsg(sessionId, message); } -int32_t DistributedInputSourceTransport::OnSessionOpened(int32_t sessionId, int32_t result) +int32_t DistributedInputSourceTransport::GetCurrentSessionId() { - 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; + return sessionId_; } -void DistributedInputSourceTransport::OnSessionClosed(int32_t sessionId) +DistributedInputSourceTransport::DInputTransbaseSourceListener::DInputTransbaseSourceListener( + DistributedInputSourceTransport *transport) { - 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(); + sourceTransportObj_ = transport; + DHLOGI("DInputTransbaseSourceListener init."); +} + +DistributedInputSourceTransport::DInputTransbaseSourceListener::~DInputTransbaseSourceListener() +{ + sourceTransportObj_ = nullptr; + DHLOGI("DInputTransbaseSourceListener destory."); } 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() || - !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONPREPARE, data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().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], - recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); + callback_->onResponsePrepareRemoteInput(deviceId, recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); } void DistributedInputSourceTransport::NotifyResponseUnprepareRemoteInput(int32_t sessionId, @@ -1131,29 +819,31 @@ void DistributedInputSourceTransport::NotifyResponseUnprepareRemoteInput(int32_t DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONUNPREPARE data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().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(sessionId); + CloseInputSoftbus(deviceId); } 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()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ONSTART, data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().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]); + deviceId, recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], + recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) @@ -1163,7 +853,7 @@ void DistributedInputSourceTransport::NotifyResponseStopRemoteInput(int32_t sess DHLOGE("OnBytesReceived cmdType TRANS_SINK_MSG_ONSTOP data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType TRANS_SINK_MSG_ONSTOP, deviceId is error."); return; @@ -1176,17 +866,19 @@ 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()) { + if (!recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE].is_boolean() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTART, data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().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]); + deviceId, recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID], recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE], + recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST]); } void DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg) @@ -1196,7 +888,7 @@ void DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid(int32_t DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTOP, data type is error."); return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_DHID_ONSTOP, deviceId is error."); return; @@ -1208,7 +900,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 = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE, deviceId is error."); return; @@ -1226,7 +918,7 @@ void DistributedInputSourceTransport::NotifyReceivedEventRemoteInput(int32_t ses return; } - std::string deviceId = FindDeviceBySession(sessionId); + std::string deviceId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (deviceId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_BODY_DATA, deviceId is error."); return; @@ -1235,21 +927,6 @@ 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."); @@ -1257,18 +934,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]; - // continue notify to A_sink_trans - int32_t ret = OpenInputSoftbus(deviceId); + std::string peerDevId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; + + int32_t ret = OpenInputSoftbus(peerDevId); if (ret != DH_SUCCESS) { - callback_->onResponseRelayPrepareRemoteInput(sessionId, deviceId, false, ""); + callback_->onResponseRelayPrepareRemoteInput(sessionId, peerDevId, false); return; } - ret = PrepareRemoteInput(sessionId, deviceId); + ret = PrepareRemoteInput(sessionId, peerDevId); if (ret != DH_SUCCESS) { - callback_->onResponseRelayPrepareRemoteInput(sessionId, deviceId, false, ""); + callback_->onResponseRelayPrepareRemoteInput(sessionId, peerDevId, false); return; } } @@ -1281,11 +958,11 @@ void DistributedInputSourceTransport::ReceiveSrcTSrcRelayUnprepare(int32_t sessi return; } - std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; - int32_t ret = UnprepareRemoteInput(sessionId, deviceId); + std::string peerDevId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID]; + int32_t ret = UnprepareRemoteInput(sessionId, peerDevId); if (ret != DH_SUCCESS) { DHLOGE("Can not send message by softbus, unprepare fail."); - callback_->onResponseRelayUnprepareRemoteInput(sessionId, deviceId, false); + callback_->onResponseRelayUnprepareRemoteInput(sessionId, peerDevId, false); return; } } @@ -1295,18 +972,17 @@ 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_WHITE_LIST].is_string()) { + !recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID].is_number()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_PREPARE, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().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_WHITE_LIST]); + recMsg[DINPUT_SOFTBUS_KEY_RESP_VALUE]); } void DistributedInputSourceTransport::NotifyResponseRelayUnprepareRemoteInput(int32_t sessionId, @@ -1318,14 +994,19 @@ void DistributedInputSourceTransport::NotifyResponseRelayUnprepareRemoteInput(in DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_UNPREPARE, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().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]); - CloseInputSoftbus(sessionId); + + int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID]; + if (toSrcSessionId != sessionId) { + DHLOGE("Close to sink session."); + CloseInputSoftbus(sinkDevId); + } } void DistributedInputSourceTransport::ReceiveRelayPrepareResult(int32_t sessionId, const nlohmann::json &recMsg) @@ -1358,7 +1039,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(sessionId); + CloseInputSoftbus(srcId); } void DistributedInputSourceTransport::ReceiveSrcTSrcRelayStartDhid(int32_t sessionId, const nlohmann::json &recMsg) @@ -1413,11 +1094,12 @@ 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_VECTOR_DHID].is_string() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTDHID, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTDHID, sinkDevId is error."); return; @@ -1432,6 +1114,7 @@ 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, @@ -1444,7 +1127,7 @@ void DistributedInputSourceTransport::NotifyResponseRelayStopDhidRemoteInput(int DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPDHID, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPDHID, sinkDevId is error."); return; @@ -1549,11 +1232,12 @@ 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_INPUT_TYPE].is_number() || + !recMsg[DINPUT_SOFTBUS_KEY_WHITE_LIST].is_string()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTTYPE, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STARTTYPE, sinkDevId is error."); return; @@ -1568,6 +1252,7 @@ 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, @@ -1580,7 +1265,7 @@ void DistributedInputSourceTransport::NotifyResponseRelayStopTypeRemoteInput(int DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPTYPE, data type is error."); return; } - std::string sinkDevId = FindDeviceBySession(sessionId); + std::string sinkDevId = DistributedInputTransportBase::GetInstance().FindDeviceBySession(sessionId); if (sinkDevId.empty()) { DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_ON_RELAY_STOPTYPE, sinkDevId is error."); return; @@ -1633,18 +1318,21 @@ void DistributedInputSourceTransport::ReceiveRelayStopTypeResult(int32_t session callback_->onReceiveRelayStopTypeResult(status, srcId, sinkId, inputTypes); } -void DistributedInputSourceTransport::HandleSessionData(int32_t sessionId, const std::string& message) +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) { 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); - if (CheckRecivedData(message) != true) { - return; - } - int cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE]; + 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); @@ -1654,85 +1342,6 @@ void DistributedInputSourceTransport::HandleSessionData(int32_t sessionId, const (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 new file mode 100644 index 0000000..a4ee241 --- /dev/null +++ b/services/transportbase/BUILD.gn @@ -0,0 +1,80 @@ +# 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 new file mode 100644 index 0000000..5f93e93 --- /dev/null +++ b/services/transportbase/include/distributed_input_transport_base.h @@ -0,0 +1,101 @@ +/* + * 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 new file mode 100644 index 0000000..6d60920 --- /dev/null +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -0,0 +1,510 @@ +/* + * 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