diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index b1af519..e782a37 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -113,6 +113,32 @@ namespace DistributedInput { const std::string DINPUT_SPLIT_COMMA = ", "; + const std::string SOURCE_DEVICE_ID = "sourceDevId"; + + const std::string SINK_DEVICE_ID = "sinkDevId"; + + const std::string SOURCE_WINDOW_ID = "sourceWinId"; + + const std::string SINK_SHOW_WINDOW_ID = "sinkShowWinId"; + + const std::string SOURCE_WINDOW_WIDTH = "sourceWinWidth"; + + const std::string SOURCE_WINDOW_HEIGHT = "sourceWinHeight"; + + const std::string SINK_PROJECT_SHOW_WIDTH = "sinkProjShowWidth"; + + const std::string SINK_PROJECT_SHOW_HEIGHT = "sinkProjShowHeight"; + + const std::string SINK_WINDOW_SHOW_X = "sinkWinShowX"; + + const std::string SINK_WINDOW_SHOW_Y = "sinkWinShowY"; + + constexpr uint32_t SCREEN_ID_DEFAULT = 0; + + constexpr uint32_t DEFAULT_VALUE = 0; + + constexpr int32_t UNINIT_FD_VALUE = -1; + enum class EHandlerMsgType { DINPUT_SINK_EVENT_HANDLER_MSG = 1, DINPUT_SOURCE_EVENT_HANDLER_MSG = 2 diff --git a/common/include/dinput_errcode.h b/common/include/dinput_errcode.h index f86b079..3638c60 100644 --- a/common/include/dinput_errcode.h +++ b/common/include/dinput_errcode.h @@ -37,6 +37,7 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_LOW_LATENCY_LIB_NULL = -60011; constexpr int32_t ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST = -60012; constexpr int32_t ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL = -60013; + constexpr int32_t ERR_DH_INPUT_JSON_PARSE_FAIL = -60014; // whilte list error code constexpr int32_t ERR_DH_INPUT_WHILTELIST_INIT_FAIL = -61001; @@ -57,6 +58,7 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPSTOP_FAIL = -64008; constexpr int32_t ERR_DH_INPUT_SERVER_SINK_TRANSPORT_SENDMESSAGE_FAIL = -64009; constexpr int32_t ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESP_LATENCY_FAIL = -64010; + constexpr int32_t ERR_DH_INPUT_SERVER_SINK_SCREEN_INFO_IS_EMPTY = -64011; // service source error code constexpr int32_t ERR_DH_INPUT_SERVER_SOURCE_INJECT_REGISTER_FAIL = -65000; @@ -131,6 +133,11 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_SOURCE_STUB_ON_REMOTE_REQUEST_FAIL = -67028; constexpr int32_t ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL = -67029; constexpr int32_t ERR_DH_INPUT_IPC_READ_TOKEN_VALID_FAIL = -67030; + constexpr int32_t ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL = -67035; + constexpr int32_t ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL = -67036; + constexpr int32_t ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL = -67037; + constexpr int32_t ERR_DH_INPUT_RPC_REPLY_FAIL = -67038; + constexpr int32_t ERR_DH_INPUT_SA_REQUEST_CODE_INVALID = -67039; // Hidump Helper error code constexpr int32_t ERR_DH_INPUT_HIDUMP_INVALID_ARGS = -68000; diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index f6d2ddc..56607b8 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -35,6 +34,7 @@ #include "constants_dinput.h" #include "dinput_context.h" #include "dinput_errcode.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -209,7 +209,7 @@ size_t InputHub::CollectEvent(RawEvent* buffer, size_t& capacity, size_t bufferS std::vector needFilted(bufferSize, false); if (device->classes == INPUT_DEVICE_CLASS_TOUCH_MT) { - HandleTouchScreenEvent(readBuffer, count, needFilted); + HandleTouchScreenEvent(readBuffer, count, needFilted, device); } RawEvent* event = buffer; @@ -670,21 +670,6 @@ std::string InputHub::StringPrintf(const char* format, ...) const return result; } -std::string InputHub::Sha256(const std::string& in) const -{ - SHA256_CTX ctx; - SHA256_Init(&ctx); - SHA256_Update(&ctx, reinterpret_cast(in.c_str()), in.size()); - u_char digest[SHA_DIGEST_LENGTH]; - SHA256_Final(digest, &ctx); - - std::string out; - for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) { - out += StringPrintf("%02x", digest[i]); - } - return out; -} - void InputHub::GenerateDescriptor(InputDevice& identifier) const { std::string rawDescriptor; @@ -964,7 +949,6 @@ void InputHub::RecordEventLog(const RawEvent* event) case EV_ABS: eventType = "EV_ABS"; break; - break; case EV_SYN: eventType = "EV_SYN"; break; @@ -972,60 +956,59 @@ void InputHub::RecordEventLog(const RawEvent* event) eventType = "other type " + std::to_string(event->type); break; } - DHLOGD("1.E2E-Test Sink collect event, EventType: %s, Code: %d, Value: %d, Path: %s, When:%" PRId64 "", - eventType.c_str(), event->code, event->value, event->path.c_str(), event->when); + DHLOGD("1.E2E-Test Sink collect event, EventType: %s, Code: %d, Value: %d, Path: %s, descriptor: %s," + "When:%" PRId64 "", eventType.c_str(), event->code, event->value, event->path.c_str(), + GetAnonyString(event->descriptor).c_str(), event->when); } void InputHub::HandleTouchScreenEvent(struct input_event readBuffer[], const size_t count, - std::vector& needFilted) + std::vector& needFilted, Device* device) { std::vector> absIndexs; - uint32_t typePre = 0; - uint32_t typeCurr = 0; - size_t firstIndex = 0; - size_t lastIndex = 0; + int32_t firstIndex = -1; + int32_t lastIndex = -1; for (size_t i = 0; i < count; i++) { struct input_event& iev = readBuffer[i]; - typePre = typeCurr; - typeCurr = iev.type; - if ((typePre != EV_ABS) && (typeCurr == EV_ABS)) { - firstIndex = i; + if ((iev.type == EV_ABS) && (iev.code == ABS_MT_POSITION_X || iev.code == ABS_X)) { + firstIndex = (int32_t)i; + } else if (iev.type == EV_SYN) { + lastIndex = (int32_t)i; } - if (typeCurr == EV_SYN) { - lastIndex = i; - absIndexs.emplace_back(std::make_pair(firstIndex, lastIndex)); + if ((firstIndex >= 0) && (lastIndex > 0)) { + absIndexs.emplace_back(std::make_pair((size_t)firstIndex, (size_t)lastIndex)); } } - int32_t absXIndex = -1; - int32_t absYIndex = -1; - uint32_t absX = 0; - uint32_t absY = 0; + AbsInfo absInfo = { + .absX = 0, + .absY = 0, + .absXIndex = -1, + .absYIndex = -1, + }; for (auto iter : absIndexs) { - absXIndex = -1; - absYIndex = -1; + absInfo.absXIndex = -1; + absInfo.absYIndex = -1; for (size_t j = iter.first; j <= iter.second; j++) { struct input_event &iev = readBuffer[j]; - if (iev.code == ABS_MT_POSITION_X) { - absX = (uint32_t)iev.value; - absXIndex = (int32_t)j; + if (iev.code == ABS_MT_POSITION_X || iev.code == ABS_X) { + absInfo.absX = iev.value; + absInfo.absXIndex = j; } - if (iev.code == ABS_MT_POSITION_Y) { - absY = (uint32_t)iev.value; - absYIndex = (int32_t)j; + if (iev.code == ABS_MT_POSITION_Y || iev.code == ABS_Y) { + absInfo.absY = iev.value; + absInfo.absYIndex = j; } } - if ((absXIndex < 0) || (absYIndex < 0)) { + if ((absInfo.absXIndex < 0) || (absInfo.absYIndex < 0)) { for (size_t j = iter.first; j <= iter.second; j++) { needFilted[j] = true; } continue; } - - if (!CheckTouchPointRegion(readBuffer, absX, absY, absXIndex, absYIndex)) { + if (!CheckTouchPointRegion(readBuffer, absInfo, device)) { for (size_t j = iter.first; j <= iter.second; j++) { needFilted[j] = true; } @@ -1033,17 +1016,17 @@ void InputHub::HandleTouchScreenEvent(struct input_event readBuffer[], const siz } } -bool InputHub::CheckTouchPointRegion(struct input_event readBuffer[], uint32_t absX, uint32_t absY, - int32_t absXIndex, int32_t absYIndex) +bool InputHub::CheckTouchPointRegion(struct input_event readBuffer[], const AbsInfo& absInfo, Device* device) { auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo(); for (const auto& [id, sinkInfo] : sinkInfos) { auto info = sinkInfo.transformInfo; - if ((absX >= info.sinkWinPhyX) && (absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth)) - && (absY >= info.sinkWinPhyY) && (absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) { - readBuffer[absXIndex].value = (absX - info.sinkWinPhyX) * info.coeffWidth; - readBuffer[absYIndex].value = (absY - info.sinkWinPhyY) * info.coeffHeight; + if ((absInfo.absX >= info.sinkWinPhyX) && (absInfo.absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth)) + && (absInfo.absY >= info.sinkWinPhyY) && (absInfo.absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) { + device->identifier.descriptor = sinkInfo.srcScreenInfo.sourcePhyId; + readBuffer[absInfo.absXIndex].value = (absInfo.absX - info.sinkWinPhyX) * info.coeffWidth; + readBuffer[absInfo.absYIndex].value = (absInfo.absY - info.sinkWinPhyY) * info.coeffHeight; return true; } } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 3cfd035..2367acc 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -65,6 +65,13 @@ private: const bool isVirtual; // set if fd < 0 is passed to constructor }; + struct AbsInfo { + uint32_t absX; + uint32_t absY; + int32_t absXIndex; + int32_t absYIndex; + }; + int32_t Initialize(); int32_t Release(); @@ -78,7 +85,6 @@ private: int32_t MakeDevice(int fd, std::unique_ptr device); void GenerateDescriptor(InputDevice& identifier) const; std::string StringPrintf(const char* format, ...) const; - std::string Sha256(const std::string& in) const; int32_t RegisterFdForEpoll(int fd); int32_t RegisterDeviceForEpollLocked(const Device& device); @@ -108,10 +114,10 @@ private: uint32_t SizeofBitArray(uint32_t bit); bool IsSupportInputTypes(uint32_t classes); void RecordEventLog(const RawEvent* event); - void HandleTouchScreenEvent(struct input_event readBuffer[], const size_t count, std::vector& needFilted); + void HandleTouchScreenEvent(struct input_event readBuffer[], const size_t count, std::vector& needFilted, + Device* device); int32_t QueryLocalTouchScreenInfo(int fd); - bool CheckTouchPointRegion(struct input_event readBuffer[], uint32_t absX, uint32_t absY, - int32_t absXIndex, int32_t absYIndex); + bool CheckTouchPointRegion(struct input_event readBuffer[], const AbsInfo& absInfo, Device* device); size_t CollectEvent(RawEvent* buffer, size_t& capacity, size_t bufferSize, Device* device); int epollFd_; diff --git a/distributedinput.gni b/distributedinput.gni index 61c3e68..ee79e67 100644 --- a/distributedinput.gni +++ b/distributedinput.gni @@ -39,6 +39,8 @@ fwk_common_path = "${distributedhardwarefwk_path}/common" fwk_utils_path = "${distributedhardwarefwk_path}/utils" +fwk_interfaces_path = "${distributedhardwarefwk_path}/interfaces/inner_kits" + distributedinput_ldflags = [ "-lpthread" ] declare_args() { diff --git a/frameworks/include/i_distributed_sink_input.h b/frameworks/include/i_distributed_sink_input.h index cddb558..8734b5b 100644 --- a/frameworks/include/i_distributed_sink_input.h +++ b/frameworks/include/i_distributed_sink_input.h @@ -21,8 +21,9 @@ #include "iremote_broker.h" #include "iremote_object.h" -#include "i_start_d_input_server_call_back.h" #include "constants_dinput.h" +#include "dinput_context.h" +#include "i_start_d_input_server_call_back.h" namespace OHOS { namespace DistributedHardware { @@ -31,6 +32,10 @@ class IDistributedSinkInput : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedInput.IDistributedSinkInput"); + /* + * Init, Release and IsStartDistributedInput are IPC interface, + * which are used for interacting by dhareware and dinput + */ virtual int32_t Init() = 0; virtual int32_t Release() = 0; @@ -38,10 +43,20 @@ public: virtual int32_t IsStartDistributedInput( const uint32_t& inputType, sptr callback) = 0; - enum class MessageCode { - INIT = 0xf011, - RELEASE = 0xf012, - ISSTART_REMOTE_INPUT = 0xf013, + /* + * NotifyStartDScreen and NotifyStopDScreen are RPC interface, + * which are used for interacting by dinput source and dinput sink + */ + virtual int32_t NotifyStartDScreen(const SrcScreenInfo& remoteCtrlInfo) = 0; + + virtual int32_t NotifyStopDScreen(const std::string &srcScreenInfoKey) = 0; + + enum { + INIT = 0xf011U, + RELEASE = 0xf012U, + IS_START_REMOTE_INPUT = 0xf013U, + NOTIFY_START_DSCREEN = 0xf014U, + NOTIFY_STOP_DSCREEN = 0xf015U, }; }; } // namespace DistributedInput diff --git a/frameworks/include/i_distributed_source_input.h b/frameworks/include/i_distributed_source_input.h index 5194fce..3b92d43 100644 --- a/frameworks/include/i_distributed_source_input.h +++ b/frameworks/include/i_distributed_source_input.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef I_DISTRIBUTED_INPUT_H -#define I_DISTRIBUTED_INPUT_H +#ifndef I_DISTRIBUTED_SOURCE_INPUT_H +#define I_DISTRIBUTED_SOURCE_INPUT_H #include @@ -84,4 +84,4 @@ public: } // namespace DistributedHardware } // namespace OHOS -#endif // DISTRIBUTED_INPUT_INNERKITS_INCLUDE_I_DISTRIBUTED_INPUT_H +#endif // I_DISTRIBUTED_SOURCE_INPUT_H diff --git a/inputdevicehandler/BUILD.gn b/inputdevicehandler/BUILD.gn index a7522db..a2eb9bf 100755 --- a/inputdevicehandler/BUILD.gn +++ b/inputdevicehandler/BUILD.gn @@ -28,6 +28,8 @@ ohos_shared_library("libdinput_handler") { "${frameworks_path}/include", "${utils_path}/include", "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -46,6 +48,7 @@ ohos_shared_library("libdinput_handler") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn old mode 100755 new mode 100644 index b74dad6..7131c3f --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -31,6 +31,8 @@ ohos_shared_library("libdinput_sdk") { "${fwk_utils_path}/include", "${utils_path}/include", "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ "${common_path}/include/white_list_util.cpp", @@ -64,6 +66,7 @@ ohos_shared_library("libdinput_sdk") { deps = [ "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] defines = [ diff --git a/interfaces/inner_kits/test/unittest/BUILD.gn b/interfaces/inner_kits/test/unittest/BUILD.gn old mode 100755 new mode 100644 index e59c0dc..b72a904 --- a/interfaces/inner_kits/test/unittest/BUILD.gn +++ b/interfaces/inner_kits/test/unittest/BUILD.gn @@ -43,6 +43,8 @@ ohos_unittest("distributed_input_inner_test") { "${fwk_utils_path}/include", "//third_party/json/include", "${utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -87,6 +89,7 @@ ohos_unittest("distributed_input_inner_test") { "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/interfaces/ipc/include/distributed_input_client.h b/interfaces/ipc/include/distributed_input_client.h index 6a6a750..ed2174f 100644 --- a/interfaces/ipc/include/distributed_input_client.h +++ b/interfaces/ipc/include/distributed_input_client.h @@ -70,6 +70,10 @@ public: DInputServerType IsStartDistributedInput(const uint32_t& inputType); + int32_t NotifyStartDScreen(const std::string &networkId, const std::string& srcDevId, const uint64_t srcWinId); + + int32_t NotifyStopDScreen(const std::string &networkId, const std::string& srcScreenInfoKey); + public: class RegisterDInputCb : public OHOS::DistributedHardware::DistributedInput::RegisterDInputCallbackStub { public: @@ -111,6 +115,7 @@ private: bool IsJsonData(std::string strData) const; void AddWhiteListInfos(const std::string &deviceId, const std::string &strJson) const; void DelWhiteListInfos(const std::string &deviceId) const; + sptr GetRemoteDInput(const std::string &networkId) const; private: static std::shared_ptr instance; diff --git a/interfaces/ipc/include/distributed_input_sink_proxy.h b/interfaces/ipc/include/distributed_input_sink_proxy.h index f35b764..3e6c8b8 100644 --- a/interfaces/ipc/include/distributed_input_sink_proxy.h +++ b/interfaces/ipc/include/distributed_input_sink_proxy.h @@ -39,8 +39,12 @@ public: virtual int32_t IsStartDistributedInput( const uint32_t& inputType, sptr callback) override; + virtual int32_t NotifyStartDScreen(const SrcScreenInfo& srcScreenRemoteCtrlInfo) override; + + virtual int32_t NotifyStopDScreen(const std::string& srcScreenInfoKey) override; + private: - bool SendRequest(IDistributedSinkInput::MessageCode code, MessageParcel &data, MessageParcel &reply); + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); static inline BrokerDelegator delegator_; }; diff --git a/interfaces/ipc/include/distributed_input_sink_stub.h b/interfaces/ipc/include/distributed_input_sink_stub.h index 92d2cb8..dc806ba 100644 --- a/interfaces/ipc/include/distributed_input_sink_stub.h +++ b/interfaces/ipc/include/distributed_input_sink_stub.h @@ -33,8 +33,21 @@ public: virtual int32_t OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + int32_t InitInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t ReleaseInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t IsStartDistributedInputInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t NotifyStartDScreenInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t NotifyStopDScreenInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); + private: DISALLOW_COPY_AND_MOVE(DistributedInputSinkStub); + using DistributedInputSinkFunc = int32_t (DistributedInputSinkStub::*)(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + std::map memberFuncMap_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 5cfc632..7bd7c04 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -363,6 +363,64 @@ void DistributedInputClient::DelWhiteListInfos(const std::string &deviceId) cons { WhiteListUtil::GetInstance().ClearWhiteList(deviceId); } + +int32_t DistributedInputClient::NotifyStartDScreen(const std::string &sinkDevId, const std::string& srcDevId, + const uint64_t srcWinId) +{ + sptr remoteDInput = GetRemoteDInput(sinkDevId); + if (remoteDInput == nullptr || !remoteDInput->AsObject()) { + DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(sinkDevId).c_str()); + return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL; + } + std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, srcWinId); + SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(srcScreenInfoKey); + DHLOGI("DistributedInputSinkProxy the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d," + "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d", + GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, + srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd, + srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight); + auto ret = remoteDInput->NotifyStartDScreen(srcScreenInfo); + DHLOGI("NotifyStartDScreen, retCode = %d", ret); + if (ret != DH_SUCCESS) { + DHLOGE("NotifyStartDScreen failed, errCode = %d", ret); + } + return ret; +} + +int32_t DistributedInputClient::NotifyStopDScreen(const std::string &networkId, const std::string& srcScreenInfoKey) +{ + sptr remoteDInput = GetRemoteDInput(networkId); + if (remoteDInput == nullptr || !remoteDInput->AsObject()) { + DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(networkId).c_str()); + return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL; + } + auto ret = remoteDInput->NotifyStopDScreen(srcScreenInfoKey); + DHLOGI("NotifyStopDScreen, retCode = %d", ret); + if (ret != DH_SUCCESS) { + DHLOGE("NotifyStopDScreen failed, errCode = %d", ret); + } + return ret; +} + +sptr DistributedInputClient::GetRemoteDInput(const std::string &networkId) const +{ + DHLOGI("start, networkId = %s", GetAnonyString(networkId).c_str()); + if (networkId.empty()) { + DHLOGE("networkId is empty"); + return nullptr; + } + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + DHLOGE("GetSystemAbilityManager failed"); + return nullptr; + } + auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, networkId); + if (object == nullptr) { + DHLOGE("CheckSystemAbility failed"); + return nullptr; + } + return iface_cast(object); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/ipc/src/distributed_input_sink_proxy.cpp b/interfaces/ipc/src/distributed_input_sink_proxy.cpp index c7e3002..d84e4a1 100644 --- a/interfaces/ipc/src/distributed_input_sink_proxy.cpp +++ b/interfaces/ipc/src/distributed_input_sink_proxy.cpp @@ -15,6 +15,7 @@ #include "distributed_input_sink_proxy.h" +#include "anonymous_string.h" #include "distributed_hardware_log.h" #include "dinput_errcode.h" @@ -38,10 +39,12 @@ int32_t DistributedInputSinkProxy::Init() return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; } int32_t result = ERR_DH_INPUT_SINK_PROXY_INIT_FAIL; - bool ret = SendRequest(IDistributedSinkInput::MessageCode::INIT, data, reply); - if (ret) { - result = reply.ReadInt32(); + bool ret = SendRequest(INIT, data, reply); + if (!ret) { + DHLOGE("SendRequest fail!"); + return ERR_DH_INPUT_SINK_PROXY_INIT_FAIL; } + result = reply.ReadInt32(); return result; } @@ -54,10 +57,12 @@ int32_t DistributedInputSinkProxy::Release() return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; } int32_t result = ERR_DH_INPUT_SINK_PROXY_RELEASE_FAIL; - bool ret = SendRequest(IDistributedSinkInput::MessageCode::RELEASE, data, reply); - if (ret) { - result = reply.ReadInt32(); + bool ret = SendRequest(RELEASE, data, reply); + if (!ret) { + DHLOGE("SendRequest fail!"); + return ERR_DH_INPUT_SINK_PROXY_RELEASE_FAIL; } + result = reply.ReadInt32(); return result; } @@ -79,22 +84,75 @@ int32_t DistributedInputSinkProxy::IsStartDistributedInput( } MessageParcel reply; int32_t result = ERR_DH_INPUT_SINK_PROXY_IS_START_INPUT_FAIL; - bool ret = SendRequest(IDistributedSinkInput::MessageCode::ISSTART_REMOTE_INPUT, data, reply); - if (ret) { - result = reply.ReadInt32(); + bool ret = SendRequest(IS_START_REMOTE_INPUT, data, reply); + if (!ret) { + DHLOGE("SendRequest fail!"); + return ERR_DH_INPUT_SINK_PROXY_IS_START_INPUT_FAIL; } + result = reply.ReadInt32(); return result; } -bool DistributedInputSinkProxy::SendRequest( - IDistributedSinkInput::MessageCode code, MessageParcel &data, MessageParcel &reply) +int32_t DistributedInputSinkProxy::NotifyStartDScreen(const SrcScreenInfo &remoteCtrlInfo) +{ + MessageParcel data; + MessageParcel reply; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + DHLOGI("DistributedInputSinkProxy the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d, " + "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d", + GetAnonyString(remoteCtrlInfo.devId).c_str(), remoteCtrlInfo.sourceWinId, remoteCtrlInfo.sourceWinWidth, + remoteCtrlInfo.sourceWinHeight, GetAnonyString(remoteCtrlInfo.sourcePhyId).c_str(), + remoteCtrlInfo.sourcePhyFd, remoteCtrlInfo.sourcePhyWidth, remoteCtrlInfo.sourcePhyHeight); + if (!data.WriteString(remoteCtrlInfo.devId) || !data.WriteUint64(remoteCtrlInfo.sourceWinId) || + !data.WriteUint32(remoteCtrlInfo.sourceWinWidth) || !data.WriteUint32(remoteCtrlInfo.sourceWinHeight) || + !data.WriteString(remoteCtrlInfo.sourcePhyId) || !data.WriteUint32(remoteCtrlInfo.sourcePhyFd) || + !data.WriteUint32(remoteCtrlInfo.sourcePhyWidth) || !data.WriteUint32(remoteCtrlInfo.sourcePhyHeight)) { + DHLOGE("DistributedInputSinkProxy write params failed"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + int32_t result = ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL; + bool ret = SendRequest(NOTIFY_START_DSCREEN, data, reply); + if (!ret) { + DHLOGE("SendRequest fail!"); + return ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL; + } + result = reply.ReadInt32(); + return result; +} + +int32_t DistributedInputSinkProxy::NotifyStopDScreen(const std::string &srcScreenInfoKey) +{ + MessageParcel data; + MessageParcel reply; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + if (!data.WriteString(srcScreenInfoKey)) { + DHLOGE("DistributedInputSinkProxy write params failed"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + int32_t result = ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL; + bool ret = SendRequest(NOTIFY_STOP_DSCREEN, data, reply); + if (!ret) { + DHLOGE("SendRequest fail!"); + return ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL; + } + result = reply.ReadInt32(); + return result; +} + +bool DistributedInputSinkProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) { sptr remote = Remote(); if (remote == nullptr) { return false; } MessageOption option(MessageOption::TF_SYNC); - int32_t result = remote->SendRequest(static_cast(code), data, reply, option); + int32_t result = remote->SendRequest(code, data, reply, option); if (result != DH_SUCCESS) { return false; } diff --git a/interfaces/ipc/src/distributed_input_sink_stub.cpp b/interfaces/ipc/src/distributed_input_sink_stub.cpp index 0e1a554..6a5ac56 100644 --- a/interfaces/ipc/src/distributed_input_sink_stub.cpp +++ b/interfaces/ipc/src/distributed_input_sink_stub.cpp @@ -15,6 +15,7 @@ #include "distributed_input_sink_stub.h" +#include "anonymous_string.h" #include "distributed_hardware_log.h" #include "constants_dinput.h" @@ -24,10 +25,20 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { DistributedInputSinkStub::DistributedInputSinkStub() -{} +{ + DHLOGI("DistributedInputSinkStub ctor!"); + memberFuncMap_[INIT] = &DistributedInputSinkStub::InitInner; + memberFuncMap_[RELEASE] = &DistributedInputSinkStub::ReleaseInner; + memberFuncMap_[IS_START_REMOTE_INPUT] = &DistributedInputSinkStub::IsStartDistributedInputInner; + memberFuncMap_[NOTIFY_START_DSCREEN] = &DistributedInputSinkStub::NotifyStartDScreenInner; + memberFuncMap_[NOTIFY_STOP_DSCREEN] = &DistributedInputSinkStub::NotifyStopDScreenInner; +} DistributedInputSinkStub::~DistributedInputSinkStub() -{} +{ + DHLOGI("DistributedInputSinkStub dtor!"); + memberFuncMap_.clear(); +} int32_t DistributedInputSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -36,40 +47,94 @@ int32_t DistributedInputSinkStub::OnRemoteRequest(uint32_t code, MessageParcel & DHLOGE("DistributedInputSinkStub read token valid failed"); return ERR_DH_INPUT_IPC_READ_TOKEN_VALID_FAIL; } - switch (code) { - case static_cast(IDistributedSinkInput::MessageCode::INIT): { - int32_t ret = Init(); - if (!reply.WriteInt32(ret)) { - DHLOGE("DistributedInputSinkStub write ret failed"); - return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; - } - break; - } - - case static_cast(IDistributedSinkInput::MessageCode::RELEASE): { - int32_t ret = Release(); - if (!reply.WriteInt32(ret)) { - DHLOGE("DistributedInputSinkStub write ret failed"); - return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; - } - break; - } - - case static_cast(IDistributedSinkInput::MessageCode::ISSTART_REMOTE_INPUT): { - uint32_t inputType = data.ReadUint32(); - sptr callback = - iface_cast(data.ReadRemoteObject()); - int32_t ret = IsStartDistributedInput(inputType, callback); - if (!reply.WriteInt32(ret)) { - DHLOGE("DistributedInputSinkStub write ret failed"); - return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; - } - break; - } - default: - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + auto iter = memberFuncMap_.find(code); + if (iter == memberFuncMap_.end()) { + DHLOGE("invalid request code is %d.", code); + return ERR_DH_INPUT_SA_REQUEST_CODE_INVALID; } - return DH_SUCCESS; + DistributedInputSinkFunc &func = iter->second; + return (this->*func)(data, reply, option); +} + +int32_t DistributedInputSinkStub::InitInner(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + DHLOGI("start"); + int32_t ret = Init(); + if (!reply.WriteInt32(ret)) { + DHLOGE("DistributedInputSinkStub write ret failed"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + return ret; +} + +int32_t DistributedInputSinkStub::ReleaseInner(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t ret = Release(); + if (!reply.WriteInt32(ret)) { + DHLOGE("DistributedInputSinkStub write ret failed"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + return ret; +} + +int32_t DistributedInputSinkStub::IsStartDistributedInputInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + uint32_t inputType = data.ReadUint32(); + sptr callback = + iface_cast(data.ReadRemoteObject()); + int32_t ret = IsStartDistributedInput(inputType, callback); + if (!reply.WriteInt32(ret)) { + DHLOGE("DistributedInputSinkStub write ret failed"); + return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL; + } + return ret; +} + +int32_t DistributedInputSinkStub::NotifyStartDScreenInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + std::string devId = data.ReadString(); + uint64_t sourceWinId = data.ReadUint64(); + uint32_t sourceWinWidth = data.ReadUint32(); + uint32_t sourceWinHeight = data.ReadUint32(); + std::string sourcePhyId = data.ReadString(); + uint32_t sourcePhyFd = data.ReadUint32(); + uint32_t sourcePhyWidth = data.ReadUint32(); + uint32_t sourcePhyHeight = data.ReadUint32(); + DHLOGI("OnRemoteRequest the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d," + "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d", GetAnonyString(devId).c_str(), + sourceWinId, sourceWinWidth, sourceWinHeight, GetAnonyString(sourcePhyId).c_str(), sourcePhyFd, sourcePhyWidth, + sourcePhyHeight); + SrcScreenInfo srcScreenInfo = { + .devId = devId, + .sourceWinId = sourceWinId, + .sourceWinWidth = sourceWinWidth, + .sourceWinHeight = sourceWinHeight, + .sourcePhyId = sourcePhyId, + .sourcePhyFd = sourcePhyFd, + .sourcePhyWidth = sourcePhyWidth, + .sourcePhyHeight = sourcePhyHeight, + }; + int32_t ret = NotifyStartDScreen(srcScreenInfo); + if (!reply.WriteInt32(ret)) { + DHLOGE("write reply failed"); + return ERR_DH_INPUT_RPC_REPLY_FAIL; + } + return ret; +} + +int32_t DistributedInputSinkStub::NotifyStopDScreenInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + std::string srcScreenInfoKey = data.ReadString(); + DHLOGI("OnRemoteRequest srcScreenInfoKey: %s", GetAnonyString(srcScreenInfoKey).c_str()); + int ret = NotifyStopDScreen(srcScreenInfoKey); + if (!reply.WriteInt32(ret)) { + DHLOGE("write version failed"); + return ERR_DH_INPUT_RPC_REPLY_FAIL; + } + return ret; } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/sink/inputcollector/BUILD.gn b/services/sink/inputcollector/BUILD.gn index 2e935ed..9f48d20 100755 --- a/services/sink/inputcollector/BUILD.gn +++ b/services/sink/inputcollector/BUILD.gn @@ -27,6 +27,8 @@ ohos_shared_library("libdinput_collector") { "${fwk_utils_path}/include", "${utils_path}/include", "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -45,6 +47,7 @@ ohos_shared_library("libdinput_collector") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/services/sink/sinkmanager/BUILD.gn b/services/sink/sinkmanager/BUILD.gn old mode 100755 new mode 100644 index 2e2387c..4663802 --- a/services/sink/sinkmanager/BUILD.gn +++ b/services/sink/sinkmanager/BUILD.gn @@ -37,6 +37,10 @@ ohos_shared_library("libdinput_sink") { "${dfx_utils_path}/include", "${utils_path}/include", "${low_latency_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "//foundation/window/window_manager/interfaces/innerkits/dm", + "//foundation/window/window_manager/utils/include/", ] sources = [ @@ -68,6 +72,9 @@ ohos_shared_library("libdinput_sink") { "//foundation/distributedhardware/distributed_input/interfaces/inner_kits:libdinput_sdk", "//foundation/distributedhardware/distributed_input/services/sink/inputcollector:libdinput_collector", "//foundation/distributedhardware/distributed_input/services/sink/transport:libdinput_sink_trans", + "${fwk_interfaces_path}:libdhfwk_sdk", + "//foundation/window/window_manager/dm:libdm", + "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", ] external_deps = [ diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index c741ef8..ee595dc 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef DISRIBUTED_INPUT_SINK_MANAGER_SERVICE_H -#define DISRIBUTED_INPUT_SINK_MANAGER_SERVICE_H +#ifndef DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H +#define DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H #include #include @@ -23,6 +23,9 @@ #include #include "event_handler.h" +#include "ipublisher_listener.h" +#include "publisher_listener_stub.h" +#include "screen.h" #include "singleton.h" #include "system_ability.h" @@ -41,7 +44,7 @@ class DistributedInputSinkManager : public SystemAbility, public DistributedInpu public: DistributedInputSinkManager(int32_t saId, bool runOnCreate); - ~DistributedInputSinkManager() = default; + ~DistributedInputSinkManager(); class DInputSinkListener : public DInputSinkTransCallback { public: @@ -55,6 +58,25 @@ public: DistributedInputSinkManager *sinkManagerObj_; }; + class ProjectWindowListener : public PublisherListenerStub { + public: + ProjectWindowListener(); + ~ProjectWindowListener(); + void OnMessage(const DHTopic topic, const std::string& message) override; + + private: + int32_t ParseMessage(const std::string& message, std::string& srcDeviceId, uint64_t& srcWinId, + SinkScreenInfo& sinkScreenInfo); + int32_t UpdateSinkScreenInfoCache(const std::string& srcDevId, const uint64_t srcWinId, + const SinkScreenInfo& sinkScreenInfoTmp); + uint32_t GetScreenWidth(); + uint32_t GetScreenHeight(); + + private: + sptr screen_; + std::mutex handleScreenMutex_; + }; + public: void OnStart() override; @@ -84,7 +106,12 @@ public: */ std::shared_ptr GetEventHandler(); + int32_t NotifyStartDScreen(const SrcScreenInfo& srcScreenInfo) override; + + int32_t NotifyStopDScreen(const std::string& srcScreenInfoKey) override; + int32_t Dump(int32_t fd, const std::vector& args) override; + private: ServiceSinkRunningState serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START; DInputServerType isStartTrans_ = DInputServerType::NULL_SERVER_TYPE; @@ -95,9 +122,10 @@ private: std::shared_ptr handler_; bool InitAuto(); DInputDeviceType inputTypes_; + sptr projectWindowListener_ = nullptr; }; } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS -#endif // DISRIBUTED_INPUT_SINK_MANAGER_SERVICE_H +#endif // DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index b574258..ce7c33f 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -16,12 +16,14 @@ #include "distributed_input_sink_manager.h" #include "anonymous_string.h" +#include "distributed_hardware_fwk_kit.h" #include "distributed_hardware_log.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "nlohmann/json.hpp" -#include "system_ability_definition.h" +#include "screen_manager.h" #include "string_ex.h" +#include "system_ability_definition.h" #include "distributed_input_collector.h" #include "distributed_input_sink_switch.h" @@ -41,9 +43,16 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSinkManager, DISTRIBUTED_HARDWARE_ DistributedInputSinkManager::DistributedInputSinkManager(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) { + DHLOGI("DistributedInputSinkManager ctor!"); inputTypes_ = DInputDeviceType::NONE; } +DistributedInputSinkManager::~DistributedInputSinkManager() +{ + DHLOGI("DistributedInputSinkManager dtor!"); + projectWindowListener_ = nullptr; +} + DistributedInputSinkManager::DInputSinkListener::DInputSinkListener(DistributedInputSinkManager *manager) { sinkManagerObj_ = manager; @@ -276,6 +285,13 @@ int32_t DistributedInputSinkManager::Init() serviceRunningState_ = ServiceSinkRunningState::STATE_RUNNING; + std::shared_ptr dhFwkKit = DInputContext::GetInstance().GetDHFwkKit(); + if (dhFwkKit == nullptr) { + DHLOGE("dhFwkKit obtain fail!"); + return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; + } + projectWindowListener_ = new ProjectWindowListener; + dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, projectWindowListener_); return DH_SUCCESS; } @@ -301,6 +317,11 @@ int32_t DistributedInputSinkManager::Release() DistributedInputCollector::GetInstance().Release(); serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START; + std::shared_ptr dhFwkKit = DInputContext::GetInstance().GetDHFwkKit(); + if (dhFwkKit != nullptr && projectWindowListener_ != nullptr) { + DHLOGI("UnPublish ProjectWindowListener"); + dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, projectWindowListener_); + } DHLOGI("exit dinput sink sa."); SetSinkProcessExit(); @@ -349,6 +370,154 @@ void DistributedInputSinkManager::SetInputTypes(const uint32_t& inputTypes) inputTypes_ = static_cast(inputTypes); } +DistributedInputSinkManager::ProjectWindowListener::ProjectWindowListener() +{ + DHLOGI("ProjectWindowListener ctor!"); + std::lock_guard lock(handleScreenMutex_); + if (screen_ == nullptr) { + std::vector> screens = Rosen::ScreenManager::GetInstance().GetAllScreens(); + screen_ = screens[SCREEN_ID_DEFAULT]; + } +} + +DistributedInputSinkManager::ProjectWindowListener::~ProjectWindowListener() +{ + DHLOGI("ProjectWindowListener dtor!"); + std::lock_guard lock(handleScreenMutex_); + screen_ = nullptr; +} + +void DistributedInputSinkManager::ProjectWindowListener::OnMessage(const DHTopic topic, const std::string& message) +{ + std::string srcDeviceId = ""; + uint64_t srcWinId = 0; + SinkScreenInfo sinkScreenInfo = {}; + int32_t parseRes = ParseMessage(message, srcDeviceId, srcWinId, sinkScreenInfo); + if (parseRes != DH_SUCCESS) { + DHLOGE("message parse failed!"); + return; + } + int32_t saveRes = UpdateSinkScreenInfoCache(srcDeviceId, srcWinId, sinkScreenInfo); + if (saveRes != DH_SUCCESS) { + DHLOGE("Save sink screen info failed!"); + return; + } +} + +int32_t DistributedInputSinkManager::ProjectWindowListener::ParseMessage(const std::string& message, + std::string& srcDeviceId, uint64_t& srcWinId, SinkScreenInfo& sinkScreenInfo) +{ + nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false); + if (jsonObj.is_discarded()) { + DHLOGE("jsonObj parse failed!"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + if (!IsString(jsonObj, SOURCE_DEVICE_ID)) { + DHLOGE("sourceDevId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + srcDeviceId = jsonObj[SOURCE_DEVICE_ID].get(); + if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + DHLOGE("sourceWinId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + srcWinId = jsonObj[SOURCE_WINDOW_ID].get(); + if (!IsUint64(jsonObj, SINK_SHOW_WINDOW_ID)) { + DHLOGE("sinkWinId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkScreenInfo.sinkShowWinId = jsonObj[SINK_SHOW_WINDOW_ID].get(); + if (!IsUint32(jsonObj, SINK_PROJECT_SHOW_WIDTH)) { + DHLOGE("sourceWinHeight key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkScreenInfo.sinkProjShowWidth = jsonObj[SINK_PROJECT_SHOW_WIDTH].get(); + if (!IsUint32(jsonObj, SINK_PROJECT_SHOW_HEIGHT)) { + DHLOGE("sourceWinHeight key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkScreenInfo.sinkProjShowHeight = jsonObj[SINK_PROJECT_SHOW_HEIGHT].get(); + if (!IsUint32(jsonObj, SINK_WINDOW_SHOW_X)) { + DHLOGE("sourceWinHeight key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkScreenInfo.sinkWinShowX = jsonObj[SINK_WINDOW_SHOW_X].get(); + if (!IsUint32(jsonObj, SINK_WINDOW_SHOW_Y)) { + DHLOGE("sourceWinHeight key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkScreenInfo.sinkWinShowY = jsonObj[SINK_WINDOW_SHOW_Y].get(); + return DH_SUCCESS; +} + +int32_t DistributedInputSinkManager::ProjectWindowListener::UpdateSinkScreenInfoCache(const std::string& srcDevId, + const uint64_t srcWinId, const SinkScreenInfo& sinkScreenInfoTmp) +{ + std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, srcWinId); + SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(srcScreenInfoKey); + sinkScreenInfo.sinkShowWinId = sinkScreenInfoTmp.sinkShowWinId; + sinkScreenInfo.sinkProjShowWidth = sinkScreenInfoTmp.sinkProjShowWidth; + sinkScreenInfo.sinkProjShowHeight = sinkScreenInfoTmp.sinkProjShowHeight; + sinkScreenInfo.sinkWinShowX = sinkScreenInfoTmp.sinkWinShowX; + sinkScreenInfo.sinkWinShowY = sinkScreenInfoTmp.sinkWinShowY; + sinkScreenInfo.sinkShowWidth = GetScreenWidth(); + sinkScreenInfo.sinkShowHeight = GetScreenHeight(); + LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo; + sinkScreenInfo.sinkPhyWidth = info.absMtPositionXMax + 1; + sinkScreenInfo.sinkPhyHeight = info.absMtPositionYMax + 1; + DHLOGI("sinkShowWinId: %d, sinkProjShowWidth: %d, sinkProjShowHeight: %d, sinkWinShowX: %d, sinkWinShowY: %d," + "sinkShowWidth: %d, sinkShowHeight: %d, sinkPhyWidth: %d, sinkPhyHeight: %d", sinkScreenInfo.sinkShowWinId, + sinkScreenInfo.sinkProjShowWidth, sinkScreenInfo.sinkProjShowHeight, sinkScreenInfo.sinkWinShowX, + sinkScreenInfo.sinkWinShowY, sinkScreenInfo.sinkShowWidth, sinkScreenInfo.sinkShowHeight, + sinkScreenInfo.sinkPhyWidth, sinkScreenInfo.sinkPhyHeight); + return DInputContext::GetInstance().UpdateSinkScreenInfo(srcScreenInfoKey, sinkScreenInfo); +} + +uint32_t DistributedInputSinkManager::ProjectWindowListener::GetScreenWidth() +{ + std::lock_guard lock(handleScreenMutex_); + if (screen_ == nullptr) { + DHLOGE("screen is nullptr!"); + return DEFAULT_VALUE; + } + return screen_->GetWidth(); +} + +uint32_t DistributedInputSinkManager::ProjectWindowListener::GetScreenHeight() +{ + std::lock_guard lock(handleScreenMutex_); + if (screen_ == nullptr) { + DHLOGE("screen is nullptr!"); + return DEFAULT_VALUE; + } + return screen_->GetHeight(); +} + +int32_t DistributedInputSinkManager::NotifyStartDScreen(const SrcScreenInfo& srcScreenInfo) +{ + DHLOGI("NotifyStartDScreen start!"); + std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcScreenInfo.devId, + srcScreenInfo.sourceWinId); + SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(screenInfoKey); + sinkScreenInfo.srcScreenInfo = srcScreenInfo; + DHLOGI("OnRemoteRequest the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d," + "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d", + GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, + srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd, + srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight); + return DInputContext::GetInstance().UpdateSinkScreenInfo(screenInfoKey, sinkScreenInfo); +} + +int32_t DistributedInputSinkManager::NotifyStopDScreen(const std::string& srcScreenInfoKey) +{ + DHLOGI("NotifyStopDScreen start, srcScreenInfoKey: %s", GetAnonyString(srcScreenInfoKey).c_str()); + if (srcScreenInfoKey.empty()) { + DHLOGE("srcScreenInfoKey is empty, srcScreenInfoKey: %s", GetAnonyString(srcScreenInfoKey).c_str()); + return ERR_DH_INPUT_SERVER_SINK_SCREEN_INFO_IS_EMPTY; + } + return DInputContext::GetInstance().RemoveSinkScreenInfo(srcScreenInfoKey); +} + int32_t DistributedInputSinkManager::Dump(int32_t fd, const std::vector& args) { DHLOGI("DistributedInputSinkManager Dump."); diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn index 3fdce06..f40245c 100755 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn @@ -48,6 +48,10 @@ ohos_unittest("distributed_input_sinkmanager_test") { "${dfx_utils_path}/include", "${utils_path}/include", "${low_latency_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "//foundation/window/window_manager/interfaces/innerkits/dm", + "//foundation/window/window_manager/utils/include/", ] sources = [ @@ -91,6 +95,9 @@ ohos_unittest("distributed_input_sinkmanager_test") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", + "//foundation/window/window_manager/dm:libdm", + "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", ] external_deps = [ diff --git a/services/sink/transport/BUILD.gn b/services/sink/transport/BUILD.gn old mode 100755 new mode 100644 index 50ae5d6..5a777e5 --- a/services/sink/transport/BUILD.gn +++ b/services/sink/transport/BUILD.gn @@ -31,6 +31,8 @@ ohos_shared_library("libdinput_sink_trans") { "${dfx_utils_path}/include", "${utils_path}/include", "${low_latency_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -56,6 +58,7 @@ ohos_shared_library("libdinput_sink_trans") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn old mode 100755 new mode 100644 index 7d82544..1b3ac65 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -28,6 +28,8 @@ ohos_shared_library("libdinput_inject") { "//third_party/json/include", "${dfx_utils_path}/include", "${utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -53,6 +55,7 @@ ohos_shared_library("libdinput_inject") { "${utils_path}:libdinput_utils", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 27f38d2..6c3f980 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -36,12 +36,19 @@ public: int32_t StructTransJson(const InputDevice& pBuf, std::string& strDescriptor); void StartInjectThread(); void StopInjectThread(); + std::string GenerateVirtualTouchScreenDHId(const uint64_t sourceWinId, const uint32_t sourceWinWidth, + const uint32_t sourceWinHeight); + int32_t CreateVirtualTouchScreenNode(const std::string& devId, const std::string& dhId, const uint64_t srcWinId, + const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); + int32_t RemoveVirtualTouchScreenNode(const std::string& dhId); + int32_t GetVirtualTouchScreenFd(); private: DistributedInputInject(); ~DistributedInputInject(); std::unique_ptr inputNodeManager_; + std::mutex inputNodeManagerMutex_; // The event queue. static const int EVENT_BUFFER_SIZE = 16; diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 95c7e40..6720790 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -44,6 +44,10 @@ public: int32_t CloseDeviceLocked(const std::string& dhId); void StartInjectThread(); void StopInjectThread(); + int32_t CreateVirtualTouchScreenNode(const std::string& devId, const std::string& dhId, const uint64_t srcWinId, + const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); + int32_t RemoveVirtualTouchScreenNode(const std::string& dhId); + int32_t GetVirtualTouchScreenFd(); private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); @@ -62,6 +66,7 @@ private: std::condition_variable conditionVariable_; std::queue> injectQueue_; std::unique_ptr inputHub_; + int32_t virtualTouchScreenFd_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/include/virtual_device.h b/services/source/inputinject/include/virtual_device.h index a8a6c04..94861e6 100644 --- a/services/source/inputinject/include/virtual_device.h +++ b/services/source/inputinject/include/virtual_device.h @@ -42,6 +42,7 @@ public: bool SetUp(const std::string& devId, const std::string& dhId); bool InjectInputEvent(const input_event& event); void SetNetWorkId(const std::string netWorkId); + int32_t GetFd(); protected: VirtualDevice(); diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 2cc11bd..aa42122 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -19,6 +19,7 @@ #include "anonymous_string.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" #include "nlohmann/json.hpp" #include "dinput_errcode.h" @@ -29,12 +30,16 @@ namespace DistributedHardware { namespace DistributedInput { DistributedInputInject::DistributedInputInject() { + std::lock_guard lock(inputNodeManagerMutex_); inputNodeManager_ = std::make_unique(); } DistributedInputInject::~DistributedInputInject() { DHLOGI("~DistributedInputInject"); + std::lock_guard lock(inputNodeManagerMutex_); + inputNodeManager_.reset(); + inputNodeManager_ = nullptr; } DistributedInputInject &DistributedInputInject::GetInstance() @@ -48,7 +53,7 @@ int32_t DistributedInputInject::RegisterDistributedHardware(const std::string& d { DHLOGI("%s called, deviceId: %s, dhId: %s, parameters: %s", __func__, GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str()); - + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("the DistributedInputNodeManager is null\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; @@ -64,6 +69,7 @@ int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string& { DHLOGI("%s called, deviceId: %s, dhId: %s", __func__, GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("the DistributedInputNodeManager is null\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; @@ -98,8 +104,9 @@ int32_t DistributedInputInject::StructTransJson(const InputDevice& pBuf, std::st int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent* buffer, size_t bufferSize) { + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { - DHLOGE("the DistributedInputNodeManager is null\n"); + DHLOGE("the DistributedInputNodeManager is null"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } DHLOGI("RegisterDistributedEvent start %zu\n", bufferSize); @@ -111,6 +118,7 @@ int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent* buffer, size_ void DistributedInputInject::StartInjectThread() { + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ != nullptr) { inputNodeManager_->StartInjectThread(); } @@ -118,10 +126,50 @@ void DistributedInputInject::StartInjectThread() void DistributedInputInject::StopInjectThread() { + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ != nullptr) { inputNodeManager_->StopInjectThread(); } } + +std::string DistributedInputInject::GenerateVirtualTouchScreenDHId(const uint64_t sourceWinId, + const uint32_t sourceWinWidth, const uint32_t sourceWinHeight) +{ + std::string uniqueInfo = GetLocalNetworkId() + std::to_string(sourceWinId) + + std::to_string(sourceWinWidth) + std::to_string(sourceWinHeight); + return DH_ID_PREFIX + Sha256(uniqueInfo); +} + +int32_t DistributedInputInject::CreateVirtualTouchScreenNode(const std::string& devId, const std::string& dhId, + const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; + } + return inputNodeManager_->CreateVirtualTouchScreenNode(devId, dhId, srcWinId, sourcePhyWidth, sourcePhyHeight); +} + +int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string& dhId) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; + } + return inputNodeManager_->RemoveVirtualTouchScreenNode(dhId); +} + +int32_t DistributedInputInject::GetVirtualTouchScreenFd() +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return UNINIT_FD_VALUE; + } + return inputNodeManager_->GetVirtualTouchScreenFd(); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index c471cdf..1d741d7 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -114,8 +114,43 @@ int32_t DistributedInputNodeManager::CreateHandle(InputDevice event, const std:: return DH_SUCCESS; } +int32_t DistributedInputNodeManager::CreateVirtualTouchScreenNode(const std::string& devId, const std::string& dhId, + const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight) +{ + std::unique_lock my_lock(operationMutex_); + std::unique_ptr device; + LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo; + DHLOGI("CreateVirtualTouchScreenNode start, dhId: %s, sourcePhyWidth: %d, sourcePhyHeight: %d", + GetAnonyString(dhId).c_str(), sourcePhyWidth, sourcePhyHeight); + device = std::make_unique(info.deviceInfo, info, sourcePhyWidth - 1, sourcePhyHeight - 1); + if (device == nullptr) { + DHLOGE("could not create new virtual touch Screen"); + return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; + } + if (!device->SetUp(devId, dhId)) { + DHLOGE("Virtual touch Screen setUp fail, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), + GetAnonyString(dhId).c_str()); + return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; + } + virtualTouchScreenFd_ = device->GetFd(); + AddDeviceLocked(dhId, std::move(device)); + DHLOGI("CreateVirtualTouchScreenNode end, dhId: %s", GetAnonyString(dhId).c_str()); + return DH_SUCCESS; +} + +int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string& dhId) +{ + return CloseDeviceLocked(dhId); +} + +int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd() +{ + return virtualTouchScreenFd_; +} + void DistributedInputNodeManager::AddDeviceLocked(const std::string& dhId, std::unique_ptr device) { + DHLOGI("dhId=%s", GetAnonyString(dhId).c_str()); std::lock_guard lock(virtualDeviceMapMutex_); auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign(dhId, std::move(device)); if (!inserted) { @@ -129,6 +164,7 @@ int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &dhId) std::lock_guard lock(virtualDeviceMapMutex_); std::map>::iterator iter = virtualDeviceMap_.find(dhId); if (iter != virtualDeviceMap_.end()) { + DHLOGI("%s called success, dhId=%s", __func__, GetAnonyString(dhId).c_str()); virtualDeviceMap_.erase(iter); return DH_SUCCESS; } diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index 5c0c816..eb6de3b 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -96,6 +96,7 @@ bool VirtualDevice::SetPhys(const std::string deviceName) bool VirtualDevice::SetUp(const std::string& devId, const std::string& dhId) { fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + DHLOGI("open fd %d", fd_); if (fd_ < 0) { DHLOGE("Failed to open uinput %s", __func__); return false; @@ -133,12 +134,13 @@ bool VirtualDevice::SetUp(const std::string& devId, const std::string& dhId) DHLOGE("Unable to create input device : %s", __func__); return false; } + DHLOGI("create fd %d", fd_); char sysfs_device_name[16]; if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfs_device_name)), sysfs_device_name) < 0) { DHLOGE("Unable to get input device name: %s", __func__); } - DHLOGI("get input device name: %s", GetAnonyString(sysfs_device_name).c_str()); + DHLOGI("get input device name: %s, fd: %d", GetAnonyString(sysfs_device_name).c_str(), fd_); return true; } @@ -162,6 +164,11 @@ void VirtualDevice::SetNetWorkId(const std::string netWorkId) netWorkId_.append(netWorkId); } +int32_t VirtualDevice::GetFd() +{ + return fd_; +} + void VirtualDevice::RecordEventLog(const input_event& event) { std::string eventType = ""; diff --git a/services/source/inputinject/test/sourceinjectunittest/BUILD.gn b/services/source/inputinject/test/sourceinjectunittest/BUILD.gn index 3de37b6..95e402b 100755 --- a/services/source/inputinject/test/sourceinjectunittest/BUILD.gn +++ b/services/source/inputinject/test/sourceinjectunittest/BUILD.gn @@ -45,6 +45,8 @@ ohos_unittest("distributed_input_inner_sourceinject_test") { "//foundation/communication/dsoftbus/interfaces/kits/transport", "${dfx_utils_path}/include", "${utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -80,6 +82,7 @@ ohos_unittest("distributed_input_inner_sourceinject_test") { "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", "//foundation/communication/dsoftbus/sdk:softbus_client", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/services/source/sourcemanager/BUILD.gn b/services/source/sourcemanager/BUILD.gn old mode 100755 new mode 100644 index 2988f1e..7c94b85 --- a/services/source/sourcemanager/BUILD.gn +++ b/services/source/sourcemanager/BUILD.gn @@ -20,6 +20,7 @@ ohos_shared_library("libdinput_source") { "include", "${frameworks_path}/include", "${innerkits_path}/include", + "${innerkits_path}/include/ipc", "${innerkits_path}/src", "${ipc_path}/include", "${ipc_path}/src", @@ -38,6 +39,8 @@ ohos_shared_library("libdinput_source") { "//third_party/json/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "${low_latency_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -88,6 +91,7 @@ ohos_shared_library("libdinput_source") { "//foundation/distributedhardware/distributed_input/interfaces/inner_kits:libdinput_sdk", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", "//foundation/distributedhardware/distributed_input/services/source/transport:libdinput_source_trans", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index d8e9607..a46de8c 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef DISRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H -#define DISRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H +#ifndef DISTRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H +#define DISTRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H #include #include @@ -23,12 +23,16 @@ #include #include +#include "dinput_context.h" #include "event_handler.h" +#include "ipublisher_listener.h" +#include "publisher_listener_stub.h" #include "singleton.h" #include "system_ability.h" #include "constants_dinput.h" #include "dinput_source_trans_callback.h" +#include "distributed_input_node_manager.h" #include "distributed_input_source_stub.h" #include "distributed_input_source_event_handler.h" @@ -69,7 +73,7 @@ typedef struct InputDeviceId { public: DistributedInputSourceManager(int32_t saId, bool runOnCreate); - ~DistributedInputSourceManager() = default; + ~DistributedInputSourceManager(); void OnStart() override; @@ -137,6 +141,27 @@ public: DistributedInputSourceManager *sourceManagerObj_; }; + class StartDScreenListener : public PublisherListenerStub { + public: + StartDScreenListener(); + ~StartDScreenListener(); + void OnMessage(const DHTopic topic, const std::string& message) override; + + private: + int32_t ParseMessage(const std::string& message, std::string& sinkDevId, SrcScreenInfo& srcScreenInfo); + int32_t UpdateSrcScreenInfoCache(const SrcScreenInfo& TmpInfo); + }; + + class StopDScreenListener : public PublisherListenerStub { + public: + StopDScreenListener(); + ~StopDScreenListener(); + void OnMessage(const DHTopic topic, const std::string& message) override; + + private: + int32_t ParseMessage(const std::string& message, std::string& sinkDevId, uint64_t& sourceWinId); + }; + std::shared_ptr GetCallbackEventHandler() { return callBackHandler_; @@ -237,9 +262,11 @@ private: bool InitAuto(); void handleStartServerCallback(const std::string& devId); std::mutex operationMutex_; + sptr startDScreenListener_ = nullptr; + sptr stopDScreenListener_ = nullptr; }; } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS -#endif // DISRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H +#endif // DISTRIBUTED_INPUT_SOURCE_MANAGER_SERVICE_H diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 6aeff0a..7407664 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -29,11 +29,15 @@ #include "dinput_errcode.h" #include "dinput_hitrace.h" #include "dinput_sa_process_state.h" +#include "dinput_utils_tool.h" +#include "distributed_hardware_fwk_kit.h" +#include "distributed_input_client.h" #include "distributed_input_inject.h" #include "distributed_input_source_transport.h" #include "dinput_utils_tool.h" #include "hisysevent_util.h" #include "hidumper.h" +#include "ipublisher_listener.h" #include "white_list_util.h" namespace OHOS { @@ -44,6 +48,14 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSourceManager, DISTRIBUTED_HARDWAR DistributedInputSourceManager::DistributedInputSourceManager(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) { + DHLOGI("DistributedInputSourceManager ctor!"); +} + +DistributedInputSourceManager::~DistributedInputSourceManager() +{ + DHLOGI("DistributedInputSourceManager dtor!"); + startDScreenListener_ = nullptr; + stopDScreenListener_ = nullptr; } DistributedInputSourceManager::DInputSourceListener::DInputSourceListener(DistributedInputSourceManager *manager) @@ -495,6 +507,16 @@ int32_t DistributedInputSourceManager::Init() DistributedInputSourceTransport::GetInstance().RegisterSourceRespCallback(statuslistener_); serviceRunningState_ = ServiceSourceRunningState::STATE_RUNNING; + + std::shared_ptr dhFwkKit = DInputContext::GetInstance().GetDHFwkKit(); + if (dhFwkKit == nullptr) { + DHLOGE("dhFwkKit obtain fail!"); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL; + } + startDScreenListener_ = new StartDScreenListener; + stopDScreenListener_ = new StopDScreenListener; + dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, startDScreenListener_); + dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_); return DH_SUCCESS; } @@ -538,6 +560,12 @@ int32_t DistributedInputSourceManager::Release() callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); serviceRunningState_ = ServiceSourceRunningState::STATE_NOT_START; + std::shared_ptr dhFwkKit = DInputContext::GetInstance().GetDHFwkKit(); + if (dhFwkKit != nullptr && startDScreenListener_ != nullptr && stopDScreenListener_ != nullptr) { + DHLOGI("UnPublish StartDScreenListener and StopDScreenListener"); + dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, startDScreenListener_); + dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_); + } DHLOGI("exit dinput source sa."); SetSourceProcessExit(); return DH_SUCCESS; @@ -1179,6 +1207,172 @@ void DistributedInputSourceManager::DInputSourceListener::RecordEventLog(int64_t eventType.c_str(), code, value, path.c_str(), when); } +DistributedInputSourceManager::StartDScreenListener::StartDScreenListener() +{ + DHLOGI("StartDScreenListener ctor!"); +} + +DistributedInputSourceManager::StartDScreenListener::~StartDScreenListener() +{ + DHLOGI("StartDScreenListener dtor!"); +} + +void DistributedInputSourceManager::StartDScreenListener::OnMessage(const DHTopic topic, const std::string& message) +{ + DHLOGI("StartDScreenListener OnMessage!"); + std::string sinkDevId = ""; + SrcScreenInfo srcScreenInfo = {}; + int32_t parseRes = ParseMessage(message, sinkDevId, srcScreenInfo); + if (parseRes != DH_SUCCESS) { + DHLOGE("Parse message failed!"); + return; + } + + std::string srcDevId = GetLocalNetworkId(); + std::string virtualTouchScreenDHId = DistributedInputInject::GetInstance().GenerateVirtualTouchScreenDHId( + srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, srcScreenInfo.sourceWinHeight); + int32_t createNodeRes = DistributedInputInject::GetInstance().CreateVirtualTouchScreenNode(srcDevId, + virtualTouchScreenDHId, srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, + srcScreenInfo.sourceWinHeight); + if (createNodeRes != DH_SUCCESS) { + DHLOGE("Create virtual touch screen Node failed!"); + return; + } + + int32_t cacheRes = UpdateSrcScreenInfoCache(srcScreenInfo); + if (cacheRes != DH_SUCCESS) { + DHLOGE("Update SrcScreenInfo cache failed!"); + return; + } + + int32_t rpcRes = DistributedInputClient::GetInstance().NotifyStartDScreen(sinkDevId, srcDevId, + srcScreenInfo.sourceWinId); + if (rpcRes != DH_SUCCESS) { + DHLOGE("Rpc invoke failed!"); + return; + } +} + +int32_t DistributedInputSourceManager::StartDScreenListener::ParseMessage(const std::string& message, + std::string& sinkDevId, SrcScreenInfo& srcScreenInfo) +{ + nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false); + if (jsonObj.is_discarded()) { + DHLOGE("jsonObj parse failed!"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + if (!IsString(jsonObj, SINK_DEVICE_ID)) { + DHLOGE("devId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkDevId = jsonObj[SINK_DEVICE_ID].get(); + if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + DHLOGE("sourceWinId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + srcScreenInfo.sourceWinId = jsonObj[SOURCE_WINDOW_ID].get(); + if (!IsUint32(jsonObj, SOURCE_WINDOW_WIDTH)) { + DHLOGE("sourceWinWidth key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + srcScreenInfo.sourceWinWidth = jsonObj[SOURCE_WINDOW_WIDTH].get(); + if (!IsUint32(jsonObj, SOURCE_WINDOW_HEIGHT)) { + DHLOGE("sourceWinHeight key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + srcScreenInfo.sourceWinHeight = jsonObj[SOURCE_WINDOW_HEIGHT].get(); + return DH_SUCCESS; +} + +int32_t DistributedInputSourceManager::StartDScreenListener::UpdateSrcScreenInfoCache(const SrcScreenInfo& TmpInfo) +{ + std::string srcDevId = GetLocalNetworkId(); + std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, TmpInfo.sourceWinId); + SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(srcScreenInfoKey); + srcScreenInfo.devId = srcDevId; + srcScreenInfo.sourceWinId = TmpInfo.sourceWinId; + srcScreenInfo.sourceWinWidth = TmpInfo.sourceWinWidth; + srcScreenInfo.sourceWinHeight = TmpInfo.sourceWinHeight; + srcScreenInfo.sourcePhyId = DistributedInputInject::GetInstance().GenerateVirtualTouchScreenDHId( + srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, srcScreenInfo.sourceWinHeight); + srcScreenInfo.sourcePhyFd = DistributedInputInject::GetInstance().GetVirtualTouchScreenFd(); + srcScreenInfo.sourcePhyWidth = TmpInfo.sourceWinWidth; + srcScreenInfo.sourcePhyHeight = TmpInfo.sourceWinHeight; + DHLOGI("StartDScreenListener UpdateSrcScreenInfo the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d," + "sourceWinHeight: %d, sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d", + GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, + srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd, + srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight); + return DInputContext::GetInstance().UpdateSrcScreenInfo(srcScreenInfoKey, srcScreenInfo); +} + +DistributedInputSourceManager::StopDScreenListener::StopDScreenListener() +{ + DHLOGI("StopDScreenListener ctor!"); +} + +DistributedInputSourceManager::StopDScreenListener::~StopDScreenListener() +{ + DHLOGI("StopDScreenListener dtor!"); +} + +void DistributedInputSourceManager::StopDScreenListener::OnMessage(const DHTopic topic, const std::string& message) +{ + DHLOGI("StopDScreenListener OnMessage!"); + std::string sinkDevId = ""; + uint64_t sourceWinId = 0; + int32_t parseRes = ParseMessage(message, sinkDevId, sourceWinId); + if (parseRes != DH_SUCCESS) { + DHLOGE("Message parse failed!"); + return; + } + + std::string sourceDevId = GetLocalNetworkId(); + std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(sourceDevId, sourceWinId); + DHLOGI("screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); + SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey); + + int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode( + srcScreenInfo.sourcePhyId); + if (removeNodeRes != DH_SUCCESS) { + DHLOGE("Remove virtual touch screen node failed!"); + return; + } + + int32_t removeCacheRes = DInputContext::GetInstance().RemoveSrcScreenInfo(screenInfoKey); + if (removeCacheRes != DH_SUCCESS) { + DHLOGE("Remove src cache failed!"); + return; + } + + int32_t rpcRes = DistributedInputClient::GetInstance().NotifyStopDScreen(sinkDevId, screenInfoKey); + if (rpcRes != DH_SUCCESS) { + DHLOGE("Rpc invoke failed!"); + return; + } +} + +int32_t DistributedInputSourceManager::StopDScreenListener::ParseMessage(const std::string& message, + std::string& sinkDevId, uint64_t& sourceWinId) +{ + nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false); + if (jsonObj.is_discarded()) { + DHLOGE("jsonObj parse failed!"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + if (!IsString(jsonObj, SINK_DEVICE_ID)) { + DHLOGE("sourceWinId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sinkDevId = jsonObj[SINK_DEVICE_ID].get(); + if (!IsUint64(jsonObj, SOURCE_WINDOW_ID)) { + DHLOGE("sourceWinId key is invalid"); + return ERR_DH_INPUT_JSON_PARSE_FAIL; + } + sourceWinId = jsonObj[SOURCE_WINDOW_ID].get(); + return DH_SUCCESS; +} + int32_t DistributedInputSourceManager::Dump(int32_t fd, const std::vector& args) { DHLOGI("DistributedInputSourceManager Dump."); diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn index aeead22..b98a138 100755 --- a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn +++ b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn @@ -28,6 +28,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { include_dirs = [ "${innerkits_path}/include", + "${innerkits_path}/ipc/include", "${innerkits_path}/src", "${ipc_path}/include", "${ipc_path}/src", @@ -50,6 +51,8 @@ ohos_unittest("distributed_input_sourcemanager_test") { "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "${utils_path}/include", "${low_latency_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -117,6 +120,8 @@ ohos_unittest("distributed_input_sourcemanager_test") { "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "//third_party/openssl:libcrypto_static", + "${fwk_interfaces_path}:libdhfwk_sdk", + "${innerkits_path}:libdinput_sdk", ] external_deps = [ diff --git a/sinkhandler/BUILD.gn b/sinkhandler/BUILD.gn old mode 100755 new mode 100644 index 02641dc..eb5f5ba --- a/sinkhandler/BUILD.gn +++ b/sinkhandler/BUILD.gn @@ -33,6 +33,9 @@ ohos_shared_library("libdinput_sink_handler") { "${fwk_utils_path}/include/log", "${fwk_utils_path}/include", "${dfx_utils_path}/include", + "${utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -52,6 +55,9 @@ ohos_shared_library("libdinput_sink_handler") { "${innerkits_path}:libdinput_sdk", "${services_sink_path}/sinkmanager:libdinput_sink", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/sinkhandler/test/unittest/BUILD.gn b/sinkhandler/test/unittest/BUILD.gn index 9a60f7b..34db5e2 100755 --- a/sinkhandler/test/unittest/BUILD.gn +++ b/sinkhandler/test/unittest/BUILD.gn @@ -44,6 +44,10 @@ ohos_unittest("distributed_input_sink_handler_test") { "${fwk_utils_path}/include", "//third_party/json/include", "${dfx_utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", ] sources = [ @@ -73,6 +77,8 @@ ohos_unittest("distributed_input_sink_handler_test") { "${innerkits_path}:libdinput_sdk", "${services_source_path}/sourcemanager:libdinput_source", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/sourcehandler/BUILD.gn b/sourcehandler/BUILD.gn old mode 100755 new mode 100644 index 2351bba..5a093f9 --- a/sourcehandler/BUILD.gn +++ b/sourcehandler/BUILD.gn @@ -33,6 +33,10 @@ ohos_shared_library("libdinput_source_handler") { "${fwk_utils_path}/include/log", "${fwk_utils_path}/include", "${dfx_utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${utils_path}/include", + "${services_source_path}/inputinject/include", ] sources = [ @@ -52,6 +56,10 @@ ohos_shared_library("libdinput_source_handler") { "${innerkits_path}:libdinput_sdk", "${services_source_path}/sourcemanager:libdinput_source", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${dfx_utils_path}:libdinput_dfx_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", + "${utils_path}:libdinput_utils", + "${distributedinput_path}/services/source/inputinject:libdinput_inject", ] external_deps = [ diff --git a/sourcehandler/test/unittest/BUILD.gn b/sourcehandler/test/unittest/BUILD.gn index 0e93c65..f6315e8 100755 --- a/sourcehandler/test/unittest/BUILD.gn +++ b/sourcehandler/test/unittest/BUILD.gn @@ -44,6 +44,10 @@ ohos_unittest("distributed_input_source_handler_test") { "${fwk_utils_path}/include", "//third_party/json/include", "${dfx_utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", ] sources = [ @@ -73,6 +77,8 @@ ohos_unittest("distributed_input_source_handler_test") { "${innerkits_path}:libdinput_sdk", "${services_source_path}/sourcemanager:libdinput_source", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn index 376ae6f..d41417e 100755 --- a/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputclient_fuzzer/BUILD.gn @@ -43,6 +43,10 @@ ohos_fuzztest("DistributedInputClientFuzzTest") { "//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include/log", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", ] cflags = [ @@ -66,6 +70,8 @@ ohos_fuzztest("DistributedInputClientFuzzTest") { "${distributedinput_path}/sinkhandler:libdinput_sink_handler", "${distributedinput_path}/sourcehandler:libdinput_source_handler", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn index cb20359..87a75e9 100755 --- a/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputkit_fuzzer/BUILD.gn @@ -43,6 +43,10 @@ ohos_fuzztest("DistributedInputKitFuzzTest") { "//foundation/distributedhardware/distributed_hardware_fwk/common/utils/include", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include/log", "//foundation/distributedhardware/distributed_hardware_fwk/utils/include", + "${utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", ] cflags = [ @@ -66,6 +70,8 @@ ohos_fuzztest("DistributedInputKitFuzzTest") { "${distributedinput_path}/sinkhandler:libdinput_sink_handler", "${distributedinput_path}/sourcehandler:libdinput_source_handler", "//base/notification/eventhandler/frameworks/eventhandler:libeventhandler", + "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ diff --git a/test/fuzztest/distributedinputsourcetransport_fuzzer/distributed_input_source_transport_fuzzer.cpp b/test/fuzztest/distributedinputsourcetransport_fuzzer/distributed_input_source_transport_fuzzer.cpp index 36c95c5..170e099 100644 --- a/test/fuzztest/distributedinputsourcetransport_fuzzer/distributed_input_source_transport_fuzzer.cpp +++ b/test/fuzztest/distributedinputsourcetransport_fuzzer/distributed_input_source_transport_fuzzer.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -28,6 +29,9 @@ namespace OHOS { namespace DistributedHardware { +namespace { + const uint32_t SLEEP_TIME_US = 100 * 1000; +} void OpenInputSoftbusFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size <= 0)) { @@ -36,6 +40,7 @@ void OpenInputSoftbusFuzzTest(const uint8_t* data, size_t size) std::string remoteDevId(reinterpret_cast(data), size); + usleep(SLEEP_TIME_US); DistributedInput::DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(remoteDevId); DistributedInput::DistributedInputSourceTransport::GetInstance().CloseInputSoftbus(remoteDevId); } diff --git a/utils/BUILD.gn b/utils/BUILD.gn index a2c6599..0ef6f3e 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -25,6 +25,9 @@ ohos_shared_library("libdinput_utils") { "${utils_path}/include", "${common_path}/include", "${service_common}/include", + "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ @@ -38,11 +41,16 @@ ohos_shared_library("libdinput_utils") { "LOG_DOMAIN=0xD004100", ] - deps = [ "${fwk_utils_path}:distributedhardwareutils" ] + deps = [ + "${fwk_utils_path}:distributedhardwareutils", + "${fwk_interfaces_path}:libdhfwk_sdk", + "//third_party/openssl:libcrypto_static", + ] external_deps = [ "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] subsystem_name = "distributedhardware" diff --git a/utils/include/dinput_context.h b/utils/include/dinput_context.h index 0de1896..452c568 100644 --- a/utils/include/dinput_context.h +++ b/utils/include/dinput_context.h @@ -18,12 +18,14 @@ #include #include +#include #include #include #include #include "single_instance.h" +#include "distributed_hardware_fwk_kit.h" #include "distributed_hardware_log.h" #include "input_hub.h" @@ -67,7 +69,7 @@ struct LocalTouchScreenInfo { struct SrcScreenInfo { std::string devId = ""; // source device id - std::string sourceWinId = ""; // source projection window id + uint64_t sourceWinId = 0; // source projection window id uint32_t sourceWinWidth = 0; // source projection window width uint32_t sourceWinHeight = 0; // source projection window height std::string sourcePhyId = ""; // source virtual screen driver id @@ -90,7 +92,7 @@ struct SinkScreenInfo { uint32_t sinkShowHeight = 0; // sink show height uint32_t sinkPhyWidth = 0; // sink touch screen width uint32_t sinkPhyHeight = 0; // sink touch screen height - std::string sinkShowWinId = ""; // sink show window id + uint64_t sinkShowWinId = 0; // sink show window id uint32_t sinkWinShowX = 0; // sink show window x coordinate uint32_t sinkWinShowY = 0; // sink show window y coordinate uint32_t sinkProjShowWidth = 0; // sink show window width @@ -102,18 +104,20 @@ struct SinkScreenInfo { class DInputContext { DECLARE_SINGLE_INSTANCE_BASE(DInputContext); public: - std::string GetScreenInfoKey(const std::string &devId, const std::string &sourceWinId); - int32_t RemoveSinkScreenInfo(const std::string &sourceWinId); - int32_t UpdateSinkScreenInfo(const std::string &sourceWinId, const SinkScreenInfo &sinkScreenInfo); - SinkScreenInfo GetSinkScreenInfo(const std::string &sourceWinId); + std::string GetScreenInfoKey(const std::string &devId, const uint64_t sourceWinId); + int32_t RemoveSinkScreenInfo(const std::string &screenInfoKey); + int32_t UpdateSinkScreenInfo(const std::string &screenInfoKey, const SinkScreenInfo &sinkScreenInfo); + SinkScreenInfo GetSinkScreenInfo(const std::string &screenInfoKey); const std::unordered_map& GetAllSinkScreenInfo(); - int32_t RemoveSrcScreenInfo(const std::string &sourceWinId); - int32_t UpdateSrcScreenInfo(const std::string &sourceWinId, const SrcScreenInfo &srcScreenInfo); - SrcScreenInfo GetSrcScreenInfo(const std::string &sourceWinId); + int32_t RemoveSrcScreenInfo(const std::string &screenInfoKey); + int32_t UpdateSrcScreenInfo(const std::string &screenInfoKey, const SrcScreenInfo &srcScreenInfo); + SrcScreenInfo GetSrcScreenInfo(const std::string &screenInfoKey); void SetLocalTouchScreenInfo(const LocalTouchScreenInfo &localTouchScreenInfo); LocalTouchScreenInfo GetLocalTouchScreenInfo(); + std::shared_ptr GetDHFwkKit(); + private: int32_t CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo); @@ -121,12 +125,17 @@ private: DInputContext() = default; ~DInputContext(); + /* the key is Combination of sink's localDeviceId and windowId, the value is sinkScreenInfo */ std::unordered_map sinkScreenInfoMap_; + + /* the key is Combination of source's localDeviceId and windowId, the value is sourceScreenInfo */ std::unordered_map srcScreenInfoMap_; LocalTouchScreenInfo localTouchScreenInfo_; std::mutex srcMapMutex_; std::mutex sinkMapMutex_; std::mutex localTouchScreenInfoMutex_; + std::shared_ptr dhFwkKit_; + std::mutex dhFwkKitMutex_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index 26605f3..292f833 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -20,6 +20,7 @@ #include #include "distributed_hardware_log.h" +#include "nlohmann/json.hpp" namespace OHOS { namespace DistributedHardware { @@ -31,8 +32,14 @@ struct DevInfo { }; DevInfo GetLocalDeviceInfo(); +std::string GetLocalNetworkId(); uint64_t GetCurrentTime(); std::string SetAnonyId(const std::string &message); +/* IsString, IsUint32 and IsUint64 are used to valid json key */ +bool IsString(const nlohmann::json& jsonObj, const std::string& key); +bool IsInt32(const nlohmann::json& jsonObj, const std::string& key); +bool IsUint32(const nlohmann::json& jsonObj, const std::string& key); +bool IsUint64(const nlohmann::json& jsonObj, const std::string& key); } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dinput_context.cpp b/utils/src/dinput_context.cpp index 17066ad..206eba1 100644 --- a/utils/src/dinput_context.cpp +++ b/utils/src/dinput_context.cpp @@ -15,6 +15,7 @@ #include "dinput_context.h" +#include "anonymous_string.h" #include "constants.h" #include "dinput_errcode.h" @@ -30,22 +31,25 @@ DInputContext::~DInputContext() srcScreenInfoMap_.clear(); } -std::string DInputContext::GetScreenInfoKey(const std::string &devId, const std::string &sourceWinId) +std::string DInputContext::GetScreenInfoKey(const std::string &devId, const uint64_t sourceWinId) { - return devId + RESOURCE_SEPARATOR + sourceWinId; + DHLOGI("GetScreenInfoKey screenInfoKey: %s, sourceWinId: %d", GetAnonyString(devId).c_str(), sourceWinId); + return devId + RESOURCE_SEPARATOR + std::to_string(sourceWinId); } -int32_t DInputContext::RemoveSinkScreenInfo(const std::string &sourceWinId) +int32_t DInputContext::RemoveSinkScreenInfo(const std::string &screenInfoKey) { + DHLOGI("RemoveSinkScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); std::lock_guard lock(sinkMapMutex_); - sinkScreenInfoMap_.erase(sourceWinId); + sinkScreenInfoMap_.erase(screenInfoKey); return DH_SUCCESS; } -int32_t DInputContext::UpdateSinkScreenInfo(const std::string &sourceWinId, const SinkScreenInfo &sinkScreenInfo) +int32_t DInputContext::UpdateSinkScreenInfo(const std::string &screenInfoKey, const SinkScreenInfo &sinkScreenInfo) { + DHLOGI("UpdateSinkScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); std::lock_guard lock(sinkMapMutex_); - if (sinkScreenInfoMap_.count(sourceWinId) <= 0) { + if (sinkScreenInfoMap_.count(screenInfoKey) <= 0) { DHLOGE("source window id not exist"); return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST; } @@ -55,20 +59,21 @@ int32_t DInputContext::UpdateSinkScreenInfo(const std::string &sourceWinId, cons DHLOGE("calculate transform infomation failed"); } - sinkScreenInfoMap_[sourceWinId] = tmp; + sinkScreenInfoMap_[screenInfoKey] = tmp; return DH_SUCCESS; } -SinkScreenInfo DInputContext::GetSinkScreenInfo(const std::string &sourceWinId) +SinkScreenInfo DInputContext::GetSinkScreenInfo(const std::string &screenInfoKey) { + DHLOGI("GetSinkScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); std::lock_guard lock(sinkMapMutex_); - if (sinkScreenInfoMap_.count(sourceWinId) <= 0) { - DHLOGE("source window id not exist"); + if (sinkScreenInfoMap_.count(screenInfoKey) <= 0) { + DHLOGE("screenInfoKey not exist"); SinkScreenInfo sinkScreenInfo; - sinkScreenInfoMap_[sourceWinId] = sinkScreenInfo; + sinkScreenInfoMap_[screenInfoKey] = sinkScreenInfo; } - return sinkScreenInfoMap_[sourceWinId]; + return sinkScreenInfoMap_[screenInfoKey]; } const std::unordered_map& DInputContext::GetAllSinkScreenInfo() @@ -77,35 +82,38 @@ const std::unordered_map& DInputContext::GetAllSink return sinkScreenInfoMap_; } -int32_t DInputContext::RemoveSrcScreenInfo(const std::string &sourceWinId) +int32_t DInputContext::RemoveSrcScreenInfo(const std::string &screenInfoKey) { + DHLOGI("RemoveSrcScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); std::lock_guard lock(srcMapMutex_); - srcScreenInfoMap_.erase(sourceWinId); + srcScreenInfoMap_.erase(screenInfoKey); return DH_SUCCESS; } -int32_t DInputContext::UpdateSrcScreenInfo(const std::string &sourceWinId, const SrcScreenInfo &srcScreenInfo) +int32_t DInputContext::UpdateSrcScreenInfo(const std::string &screenInfoKey, const SrcScreenInfo &srcScreenInfo) { std::lock_guard lock(srcMapMutex_); - if (srcScreenInfoMap_.count(sourceWinId) <= 0) { + DHLOGI("UpdateSrcScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); + if (srcScreenInfoMap_.count(screenInfoKey) <= 0) { DHLOGE("source window id not exist"); return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST; } - srcScreenInfoMap_[sourceWinId] = srcScreenInfo; + srcScreenInfoMap_[screenInfoKey] = srcScreenInfo; return DH_SUCCESS; } -SrcScreenInfo DInputContext::GetSrcScreenInfo(const std::string &sourceWinId) +SrcScreenInfo DInputContext::GetSrcScreenInfo(const std::string &screenInfoKey) { + DHLOGI("GetSrcScreenInfo screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str()); std::lock_guard lock(srcMapMutex_); - if (srcScreenInfoMap_.count(sourceWinId) <= 0) { + if (srcScreenInfoMap_.count(screenInfoKey) <= 0) { DHLOGE("source window id not exist"); SrcScreenInfo srcScreenInfo; - srcScreenInfoMap_[sourceWinId] = srcScreenInfo; + srcScreenInfoMap_[screenInfoKey] = srcScreenInfo; } - return srcScreenInfoMap_[sourceWinId]; + return srcScreenInfoMap_[screenInfoKey]; } void DInputContext::SetLocalTouchScreenInfo(const LocalTouchScreenInfo &localTouchScreenInfo) @@ -153,6 +161,15 @@ int32_t DInputContext::CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo) sinkScreenInfo.transformInfo = transformInfo; return DH_SUCCESS; } + +std::shared_ptr DInputContext::GetDHFwkKit() +{ + std::lock_guard lock(dhFwkKitMutex_); + if (dhFwkKit_ == nullptr) { + dhFwkKit_ = std::make_shared(); + } + return dhFwkKit_; +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index 9ec51b7..5fb582f 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -15,13 +15,20 @@ #include "dinput_utils_tool.h" -#include "anonymous_string.h" +#include +#include #include -#include "nlohmann/json.hpp" -#include "dinput_softbus_define.h" +#include + +#include "anonymous_string.h" +#include "nlohmann/json.hpp" #include "softbus_bus_center.h" +#include "constants_dinput.h" +#include "dinput_errcode.h" +#include "dinput_softbus_define.h" + namespace OHOS { namespace DistributedHardware { namespace DistributedInput { @@ -47,6 +54,14 @@ DevInfo GetLocalDeviceInfo() return devInfo; } +std::string GetLocalNetworkId() +{ + if (GetLocalDeviceInfo().networkId.empty()) { + DHLOGE("local networkId is empty!"); + } + return GetLocalDeviceInfo().networkId; +} + uint64_t GetCurrentTime() { struct timeval tv; @@ -61,18 +76,45 @@ std::string SetAnonyId(const std::string &message) DHLOGE("jsonObj parse failed!"); return ""; } + if (IsString(jsonObj, DINPUT_SOFTBUS_KEY_DEVICE_ID)) { + jsonObj[DINPUT_SOFTBUS_KEY_DEVICE_ID] = GetAnonyString(jsonObj[DINPUT_SOFTBUS_KEY_DEVICE_ID]); + } + if (IsInt32(jsonObj, DINPUT_SOFTBUS_KEY_SESSION_ID)) { + jsonObj[DINPUT_SOFTBUS_KEY_SESSION_ID] = GetAnonyInt32(jsonObj[DINPUT_SOFTBUS_KEY_SESSION_ID]); + } + if (IsString(jsonObj, DESCRIPTOR)) { + jsonObj[DESCRIPTOR] = GetAnonyString(jsonObj[DESCRIPTOR]); + } + return jsonObj.dump(); +} - nlohmann::json jsonStr = nlohmann::json::parse(message); - if (jsonStr.contains(DINPUT_SOFTBUS_KEY_DEVICE_ID)) { - jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID] = GetAnonyString(jsonStr[DINPUT_SOFTBUS_KEY_DEVICE_ID]); - } - if (jsonStr.contains(DINPUT_SOFTBUS_KEY_SESSION_ID)) { - jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID] = GetAnonyInt32(jsonStr[DINPUT_SOFTBUS_KEY_SESSION_ID]); - } - if (jsonStr.contains(DESCRIPTOR)) { - jsonStr[DESCRIPTOR] = GetAnonyString(jsonStr[DESCRIPTOR]); - } - return jsonStr.dump(); +bool IsString(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_string(); + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; +} + +bool IsInt32(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && INT32_MIN <= jsonObj[key] && + jsonObj[key] <= INT32_MAX; + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; +} + +bool IsUint32(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; +} + +bool IsUint64(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT64_MAX; + DHLOGI("the key %s in json is %s", key.c_str(), res ? "valid" : "invalid"); + return res; } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/utils/test/unittest/BUILD.gn b/utils/test/unittest/BUILD.gn index 86b000d..06bca02 100644 --- a/utils/test/unittest/BUILD.gn +++ b/utils/test/unittest/BUILD.gn @@ -34,6 +34,8 @@ ohos_unittest("distributed_input_utils_test") { "${utils_path}/include", "${utils_path}/test/unittest", "${common_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", ] sources = [ "dinput_context_test.cpp" ] @@ -55,11 +57,13 @@ ohos_unittest("distributed_input_utils_test") { deps = [ "${fwk_utils_path}:distributedhardwareutils", "${utils_path}:libdinput_utils", + "${fwk_interfaces_path}:libdhfwk_sdk", ] external_deps = [ "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/utils/test/unittest/dinput_context_test.cpp b/utils/test/unittest/dinput_context_test.cpp index a13b94d..16cf5f5 100644 --- a/utils/test/unittest/dinput_context_test.cpp +++ b/utils/test/unittest/dinput_context_test.cpp @@ -47,9 +47,9 @@ void DInputContextTest::TearDownTestCase() HWTEST_F(DInputContextTest, GetSourceWindId001, testing::ext::TestSize.Level0) { std::string devId = "hello"; - std::string sourceWinId = "hello"; + uint64_t sourceWinId = 1; std::string ret = DInputContext::GetInstance().GetScreenInfoKey(devId, sourceWinId); - EXPECT_EQ("hello###hello", ret); + EXPECT_EQ("hello###1", ret); } HWTEST_F(DInputContextTest, RemoveSinkScreenInfo001, testing::ext::TestSize.Level0)