From 976dc9cbe66adc79cb2e7a45d14642808f2f99ce Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Sat, 23 Dec 2023 09:04:15 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!1267=20?= =?UTF-8?q?:=20add=20files'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- intention/cooperate/server/src/cooperate.cpp | 104 +++++ .../server/src/cooperate_event_handler.cpp | 50 +++ .../server/src/cooperate_event_manager.cpp | 175 ++++++++ .../server/src/cooperate_state_out.cpp | 98 +++++ .../cooperate/server/src/cooperate_util.cpp | 46 ++ .../src/device_profile_adapter.cpp | 0 .../src/distributed_input_adapter.cpp} | 195 ++++----- .../server/src/i_cooperate_state.cpp | 121 ++++++ .../state_machine/src/device_manager.cpp | 411 ------------------ 9 files changed, 681 insertions(+), 519 deletions(-) create mode 100644 intention/cooperate/server/src/cooperate.cpp create mode 100644 intention/cooperate/server/src/cooperate_event_handler.cpp create mode 100644 intention/cooperate/server/src/cooperate_event_manager.cpp create mode 100644 intention/cooperate/server/src/cooperate_state_out.cpp create mode 100644 intention/cooperate/server/src/cooperate_util.cpp rename intention/cooperate/{state_machine => server}/src/device_profile_adapter.cpp (100%) rename intention/cooperate/{state_machine/src/dinput_adapter.cpp => server/src/distributed_input_adapter.cpp} (41%) create mode 100644 intention/cooperate/server/src/i_cooperate_state.cpp delete mode 100644 intention/cooperate/state_machine/src/device_manager.cpp diff --git a/intention/cooperate/server/src/cooperate.cpp b/intention/cooperate/server/src/cooperate.cpp new file mode 100644 index 000000000..f2e0d9baf --- /dev/null +++ b/intention/cooperate/server/src/cooperate.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cooperate.h" + +#include "devicestatus_define.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "Cooperate" }; +} // namespace + +Cooperate::Cooperate(IContext *context) + : context_(context) +{} + +int32_t Cooperate::RegisterListener(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::UnregisterListener(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::RegisterHotAreaListener(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::UnregisterHotAreaListener(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::Enable(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::Disable(int32_t pid) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::Start(int32_t pid, int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId) +{ + CALL_DEBUG_ENTER; + CHKPR(context_, RET_ERR); + return RET_ERR; +} + +int32_t Cooperate::Stop(int32_t pid, int32_t userData, bool isUnchained) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +int32_t Cooperate::GetCooperateState(int32_t pid, int32_t userData, const std::string &networkId) +{ + CALL_DEBUG_ENTER; + return RET_ERR; +} + +void Cooperate::Dump(int32_t fd) +{ + CALL_DEBUG_ENTER; +} + +extern "C" ICooperate* CreateInstance(IContext *context) +{ + return new (std::nothrow) Cooperate(context); +} + +extern "C" void DestroyInstance(ICooperate *instance) +{ + if (instance != nullptr) { + delete instance; + } +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS \ No newline at end of file diff --git a/intention/cooperate/server/src/cooperate_event_handler.cpp b/intention/cooperate/server/src/cooperate_event_handler.cpp new file mode 100644 index 000000000..c8da998a6 --- /dev/null +++ b/intention/cooperate/server/src/cooperate_event_handler.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cooperate_event_handler.h" + +#include "fi_log.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CooperateEventHandler" }; +} // namespace +CooperateEventHandler::CooperateEventHandler( + const std::shared_ptr &runner) + : AppExecFwk::EventHandler(runner) {} + +bool CooperateEventHandler::ProxyPostTask(const Callback &callback, int64_t delayTime) +{ + CALL_DEBUG_ENTER; + return AppExecFwk::EventHandler::PostTask(callback, delayTime); +} + +bool CooperateEventHandler::ProxyPostTask(const Callback &callback, const std::string &name, + int64_t delayTime) +{ + CALL_DEBUG_ENTER; + return AppExecFwk::EventHandler::PostTask(callback, name, delayTime); +} + +void CooperateEventHandler::ProxyRemoveTask(const std::string &name) +{ + CALL_DEBUG_ENTER; + AppExecFwk::EventHandler::RemoveTask(name); +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS diff --git a/intention/cooperate/server/src/cooperate_event_manager.cpp b/intention/cooperate/server/src/cooperate_event_manager.cpp new file mode 100644 index 000000000..5072388aa --- /dev/null +++ b/intention/cooperate/server/src/cooperate_event_manager.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cooperate_event_manager.h" +#include "devicestatus_define.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CooperateEventManager" }; +} // namespace + +CooperateEventManager::CooperateEventManager() {} +CooperateEventManager::~CooperateEventManager() {} + +void CooperateEventManager::AddCooperateEvent(sptr event) +{ + CALL_DEBUG_ENTER; + CHKPV(event); + std::lock_guard guard(lock_); + if (event->type == EventType::LISTENER) { + auto it = std::find_if(remoteCooperateCallbacks_.begin(), remoteCooperateCallbacks_.end(), + [event] (auto info) { + return (*info).sess == event->sess; + }); + if (it != remoteCooperateCallbacks_.end()) { + *it = event; + } else { + remoteCooperateCallbacks_.emplace_back(event); + } + } else { + cooperateCallbacks_[event->type] = event; + } +} + +void CooperateEventManager::RemoveCooperateEvent(sptr event) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + if (remoteCooperateCallbacks_.empty() || event == nullptr) { + FI_HILOGE("Remove listener failed"); + return; + } + for (auto it = remoteCooperateCallbacks_.begin(); it != remoteCooperateCallbacks_.end(); ++it) { + if ((*it)->sess == event->sess) { + remoteCooperateCallbacks_.erase(it); + return; + } + } +} + +int32_t CooperateEventManager::OnCooperateMessage(CoordinationMessage msg, const std::string &networkId) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + if (remoteCooperateCallbacks_.empty()) { + FI_HILOGW("The current listener is empty, unable to invoke the listening interface"); + return RET_ERR; + } + for (auto it = remoteCooperateCallbacks_.begin(); it != remoteCooperateCallbacks_.end(); ++it) { + sptr info = *it; + CHKPC(info); + NotifyCooperateMessage(info->sess, info->msgId, info->userData, networkId, msg); + } + return RET_OK; +} + +void CooperateEventManager::OnEnable(CoordinationMessage msg, const std::string &networkId) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + sptr info = cooperateCallbacks_[EventType::ENABLE]; + CHKPV(info); + NotifyCooperateMessage(info->sess, info->msgId, info->userData, networkId, msg); + cooperateCallbacks_[EventType::ENABLE] = nullptr; +} + +void CooperateEventManager::OnStart(CoordinationMessage msg, const std::string &networkId) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + sptr info = cooperateCallbacks_[EventType::START]; + CHKPV(info); + NotifyCooperateMessage(info->sess, info->msgId, info->userData, networkId, msg); + cooperateCallbacks_[EventType::START] = nullptr; +} + +void CooperateEventManager::OnStop(CoordinationMessage msg, const std::string &networkId) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + sptr info = cooperateCallbacks_[EventType::STOP]; + CHKPV(info); + NotifyCooperateMessage(info->sess, info->msgId, info->userData, networkId, msg); + cooperateCallbacks_[EventType::STOP] = nullptr; +} + +void CooperateEventManager::OnGetCrossingSwitchState(bool state) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(lock_); + sptr info = cooperateCallbacks_[EventType::STATE]; + CHKPV(info); + NotifyCooperateState(info->sess, info->msgId, info->userData, state); + cooperateCallbacks_[EventType::STATE] = nullptr; +} + +void CooperateEventManager::OnErrorMessage(EventType type, CoordinationMessage msg) +{ + std::lock_guard guard(lock_); + sptr info = cooperateCallbacks_[type]; + CHKPV(info); + NotifyCooperateMessage(info->sess, info->msgId, info->userData, "", msg); + cooperateCallbacks_[type] = nullptr; +} + +void CooperateEventManager::SetIContext(IContext *context) +{ + context_ = context; +} + +IContext* CooperateEventManager::GetIContext() const +{ + return context_; +} + +void CooperateEventManager::NotifyCooperateMessage(std::shared_ptr sess, + MessageId msgId, int32_t userData, const std::string &networkId, CoordinationMessage msg) +{ + CALL_DEBUG_ENTER; + CHKPV(sess); + NetPacket pkt(msgId); + pkt << userData << networkId << static_cast(msg); + if (pkt.ChkRWError()) { + FI_HILOGE("Packet write data failed"); + return; + } + if (!sess->SendMsg(pkt)) { + FI_HILOGE("Sending failed"); + return; + } +} + +void CooperateEventManager::NotifyCooperateState(std::shared_ptr sess, + MessageId msgId, int32_t userData, bool state) +{ + CALL_DEBUG_ENTER; + CHKPV(sess); + NetPacket pkt(msgId); + pkt << userData << state; + if (pkt.ChkRWError()) { + FI_HILOGE("Packet write data failed"); + return; + } + if (!sess->SendMsg(pkt)) { + FI_HILOGE("Sending failed"); + return; + } +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS diff --git a/intention/cooperate/server/src/cooperate_state_out.cpp b/intention/cooperate/server/src/cooperate_state_out.cpp new file mode 100644 index 000000000..4f4970dba --- /dev/null +++ b/intention/cooperate/server/src/cooperate_state_out.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cooperate_state_out.h" + +#include "cooperate_device_manager.h" +#include "cooperate_event_manager.h" +#include "cooperate_sm.h" +#include "cooperate_softbus_adapter.h" +#include "cooperate_util.h" +#include "distributed_input_adapter.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CooperateStateOut" }; +} // namespace + +CooperateStateOut::CooperateStateOut(const std::string& startDeviceDhid) + : startDeviceDhid_(startDeviceDhid) +{} + +int32_t CooperateStateOut::DeactivateCooperate(const std::string &remoteNetworkId, bool isUnchained, + const std::pair &preparedNetworkId) +{ + CALL_DEBUG_ENTER; + std::string tempRemoteNetworkId = remoteNetworkId; + if (tempRemoteNetworkId.empty()) { + tempRemoteNetworkId = preparedNetworkId.first; + } + int32_t ret = COOR_SOFTBUS_ADAPTER->StopRemoteCooperate(tempRemoteNetworkId, isUnchained); + if (ret != RET_OK) { + FI_HILOGE("Stop cooperate failed"); + return RET_ERR; + } + std::string taskName = "process_stop_task"; + std::function handleProcessStopFunc = + std::bind(&CooperateStateOut::ProcessStop, this, tempRemoteNetworkId); + CHKPR(eventHandler_, RET_ERR); + eventHandler_->ProxyPostTask(handleProcessStopFunc, taskName, 0); + return RET_OK; +} + +void CooperateStateOut::ProcessStop(const std::string& remoteNetworkId) +{ + CALL_DEBUG_ENTER; + std::string localNetworkId = COOPERATE::GetLocalNetworkId(); + std::vector inputDeviceDhids = COOR_DEV_MGR->GetCooperateDhids(startDeviceDhid_, false); + if (inputDeviceDhids.empty()) { + COOR_SM->OnStopFinish(false, remoteNetworkId); + } + int32_t ret = D_INPUT_ADAPTER->StopRemoteInput(remoteNetworkId, localNetworkId, inputDeviceDhids, + [this, remoteNetworkId](bool isSuccess) { + this->OnStopRemoteInput(isSuccess, remoteNetworkId); + }); + if (ret != RET_OK) { + COOR_SM->OnStopFinish(false, remoteNetworkId); + } +} + +void CooperateStateOut::OnStopRemoteInput(bool isSuccess, const std::string &remoteNetworkId) +{ + CALL_DEBUG_ENTER; + std::string taskName = "stop_finish_task"; + std::function handleStopFinishFunc = + std::bind(&CooperateSM::OnStopFinish, COOR_SM, isSuccess, remoteNetworkId); + CHKPV(eventHandler_); + eventHandler_->ProxyPostTask(handleStopFinishFunc, taskName, 0); +} + +void CooperateStateOut::OnKeyboardOnline(const std::string &dhid, + const std::pair &networkIds) +{ + std::vector inputDeviceDhids; + inputDeviceDhids.push_back(dhid); + D_INPUT_ADAPTER->StartRemoteInput(networkIds.first, networkIds.second, inputDeviceDhids, [](bool isSuccess) {}); +} + +void CooperateStateOut::SetStartDeviceDhid(const std::string &startDeviceDhid) +{ + startDeviceDhid_ = startDeviceDhid; +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS diff --git a/intention/cooperate/server/src/cooperate_util.cpp b/intention/cooperate/server/src/cooperate_util.cpp new file mode 100644 index 000000000..e1ac55406 --- /dev/null +++ b/intention/cooperate/server/src/cooperate_util.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cooperate_util.h" + +#include "cooperate_sm.h" +#include "softbus_bus_center.h" + +#include "devicestatus_define.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace COOPERATE { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CooperateUtil" }; +} // namespace +std::string GetLocalNetworkId() +{ + CALL_DEBUG_ENTER; + auto localNode = std::make_unique(); + int32_t ret = GetLocalNodeDeviceInfo(FI_PKG_NAME, localNode.get()); + if (ret != RET_OK) { + FI_HILOGE("GetLocalNodeDeviceInfo ret:%{public}d", ret); + return {}; + } + std::string networkId(localNode->networkId, sizeof(localNode->networkId)); + FI_HILOGD("GetLocalNodeDeviceInfo networkId:%{public}s", networkId.substr(0, SUBSTR_NETWORKID_LEN).c_str()); + return localNode->networkId; +} +} // namespace COOPERATE +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS diff --git a/intention/cooperate/state_machine/src/device_profile_adapter.cpp b/intention/cooperate/server/src/device_profile_adapter.cpp similarity index 100% rename from intention/cooperate/state_machine/src/device_profile_adapter.cpp rename to intention/cooperate/server/src/device_profile_adapter.cpp diff --git a/intention/cooperate/state_machine/src/dinput_adapter.cpp b/intention/cooperate/server/src/distributed_input_adapter.cpp similarity index 41% rename from intention/cooperate/state_machine/src/dinput_adapter.cpp rename to intention/cooperate/server/src/distributed_input_adapter.cpp index 766262984..c4f6c9b5a 100644 --- a/intention/cooperate/state_machine/src/dinput_adapter.cpp +++ b/intention/cooperate/server/src/distributed_input_adapter.cpp @@ -13,119 +13,122 @@ * limitations under the License. */ -#include "dinput_adapter.h" +#include "distributed_input_adapter.h" #include #include #include +#include "cooperate_event_manager.h" #include "devicestatus_define.h" -#include "event_manager.h" namespace OHOS { namespace Msdp { namespace DeviceStatus { -namespace Cooperate { using namespace DistributedHardware::DistributedInput; namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DInputAdapter" }; +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DistributedInputAdapter" }; constexpr int32_t DEFAULT_DELAY_TIME { 4000 }; constexpr int32_t RETRY_TIME { 2 }; } // namespace +DistributedInputAdapter::DistributedInputAdapter() = default; -DInputAdapter::DInputAdapter(IContext *env) - : env_(env) -{} +DistributedInputAdapter::~DistributedInputAdapter() +{ + CALL_INFO_TRACE; + std::lock_guard guard(adapterLock_); + callbacks_.clear(); +} -bool DInputAdapter::IsNeedFilterOut(const std::string &networkId, const BusinessEvent &event) +bool DistributedInputAdapter::IsNeedFilterOut(const std::string &networkId, const BusinessEvent &event) { - CALL_DEBUG_ENTER; + CALL_INFO_TRACE; return DistributedInputKit::IsNeedFilterOut(networkId, event); } -int32_t DInputAdapter::StartRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, - const std::vector &inputDeviceDhids, DInputCallback callback) +int32_t DistributedInputAdapter::StartRemoteInput(const std::string &remoteNetworkId, + const std::string &originNetworkId, const std::vector &inputDeviceDhids, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) StartDInputCallbackSink(); CHKPR(cb, ERROR_NULL_POINTER); SaveCallback(CallbackType::StartDInputCallbackSink, callback); return DistributedInputKit::StartRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb); } -int32_t DInputAdapter::StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, +int32_t DistributedInputAdapter::StopRemoteInput(const std::string &originNetworkId, const std::vector &inputDeviceDhids, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) StopDInputCallbackDHIds(); CHKPR(cb, ERROR_NULL_POINTER); - SaveCallback(CallbackType::StopDInputCallbackSink, callback); - return DistributedInputKit::StopRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb); + SaveCallback(CallbackType::StopDInputCallbackDHIds, callback); + return DistributedInputKit::StopRemoteInput(originNetworkId, inputDeviceDhids, cb); } -int32_t DInputAdapter::StopRemoteInput(const std::string &originNetworkId, +int32_t DistributedInputAdapter::StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, const std::vector &inputDeviceDhids, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) StopDInputCallbackSink(); CHKPR(cb, ERROR_NULL_POINTER); - SaveCallback(CallbackType::StopDInputCallbackDHIds, callback); - return DistributedInputKit::StopRemoteInput(originNetworkId, inputDeviceDhids, cb); + SaveCallback(CallbackType::StopDInputCallbackSink, callback); + return DistributedInputKit::StopRemoteInput(remoteNetworkId, originNetworkId, inputDeviceDhids, cb); } -int32_t DInputAdapter::PrepareRemoteInput(const std::string &remoteNetworkId, - const std::string &originNetworkId, DInputCallback callback) +int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &remoteNetworkId, + const std::string &originNetworkId, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) PrepareStartDInputCallbackSink(); CHKPR(cb, ERROR_NULL_POINTER); SaveCallback(CallbackType::PrepareStartDInputCallbackSink, callback); return DistributedInputKit::PrepareRemoteInput(remoteNetworkId, originNetworkId, cb); } -int32_t DInputAdapter::UnPrepareRemoteInput(const std::string &remoteNetworkId, - const std::string &originNetworkId, DInputCallback callback) +int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &remoteNetworkId, + const std::string &originNetworkId, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) UnPrepareStopDInputCallbackSink(); CHKPR(cb, ERROR_NULL_POINTER); SaveCallback(CallbackType::UnPrepareStopDInputCallbackSink, callback); return DistributedInputKit::UnprepareRemoteInput(remoteNetworkId, originNetworkId, cb); } -int32_t DInputAdapter::PrepareRemoteInput(const std::string &networkId, DInputCallback callback) +int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &networkId, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) PrepareStartDInputCallback(); CHKPR(cb, ERROR_NULL_POINTER); - SaveCallback(CallbackType::PrepareDInputCallback, callback); + SaveCallback(CallbackType::PrepareStartDInputCallback, callback); return DistributedInputKit::PrepareRemoteInput(networkId, cb); } -int32_t DInputAdapter::UnPrepareRemoteInput(const std::string &networkId, DInputCallback callback) +int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &networkId, DInputCallback callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(shared_from_this()); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) UnPrepareStopDInputCallback(); CHKPR(cb, ERROR_NULL_POINTER); - SaveCallback(CallbackType::UnprepareDInputCallback, callback); + SaveCallback(CallbackType::UnPrepareStopDInputCallback, callback); return DistributedInputKit::UnprepareRemoteInput(networkId, cb); } -int32_t DInputAdapter::RegisterSessionStateCb(std::function callback) +int32_t DistributedInputAdapter::RegisterSessionStateCb(std::function callback) { - CALL_DEBUG_ENTER; - sptr cb = sptr::MakeSptr(callback); + CALL_INFO_TRACE; + sptr cb = new (std::nothrow) SessionStateCallback(callback); CHKPR(callback, ERROR_NULL_POINTER); return DistributedInputKit::RegisterSessionStateCb(cb); } -int32_t DInputAdapter::UnregisterSessionStateCb() +int32_t DistributedInputAdapter::UnregisterSessionStateCb() { - CALL_DEBUG_ENTER; + CALL_INFO_TRACE; return DistributedInputKit::UnregisterSessionStateCb(); } -void DInputAdapter::SaveCallback(CallbackType type, DInputCallback callback) +void DistributedInputAdapter::SaveCallback(CallbackType type, DInputCallback callback) { std::lock_guard guard(adapterLock_); CHKPV(callback); @@ -133,11 +136,12 @@ void DInputAdapter::SaveCallback(CallbackType type, DInputCallback callback) AddTimer(type); } -void DInputAdapter::AddTimer(const CallbackType &type) +void DistributedInputAdapter::AddTimer(const CallbackType &type) { FI_HILOGD("AddTimer type:%{public}d", type); - CHKPV(env_); - int32_t timerId = env_->GetTimerManager().AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, type]() { + auto context = COOR_EVENT_MGR->GetIContext(); + CHKPV(context); + int32_t timerId = context->GetTimerManager().AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, type]() { if ((callbacks_.find(type) == callbacks_.end()) || (watchings_.find(type) == watchings_.end())) { FI_HILOGE("Callback or watching is not exist"); return; @@ -158,19 +162,20 @@ void DInputAdapter::AddTimer(const CallbackType &type) watchings_[type].times = 0; } -void DInputAdapter::RemoveTimer(const CallbackType &type) +void DistributedInputAdapter::RemoveTimer(const CallbackType &type) { FI_HILOGD("Remove timer type:%{public}d", type); if (watchings_.find(type) != watchings_.end()) { - CHKPV(env_); - env_->GetTimerManager().RemoveTimer(watchings_[type].timerId); + auto context = COOR_EVENT_MGR->GetIContext(); + CHKPV(context); + context->GetTimerManager().RemoveTimer(watchings_[type].timerId); watchings_.erase(type); } } -void DInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status) +void DistributedInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status) { - CALL_DEBUG_ENTER; + CALL_INFO_TRACE; std::lock_guard guard(adapterLock_); RemoveTimer(type); auto it = callbacks_.find(type); @@ -182,89 +187,63 @@ void DInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status) callbacks_.erase(it); } -DInputAdapter::StopDInputCallbackDHIds::StopDInputCallbackDHIds(std::shared_ptr dinput) - : dinput_(dinput) -{} - -void DInputAdapter::StopDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::StartDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes, + const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::StopDInputCallbackDHIds, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallback, status); } -DInputAdapter::StartDInputCallbackSink::StartDInputCallbackSink(std::shared_ptr dinput) - : dinput_(dinput) -{} - -void DInputAdapter::StartDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::StopDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes, + const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::StartDInputCallbackSink, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallback, status); } -DInputAdapter::StopDInputCallbackSink::StopDInputCallbackSink(std::shared_ptr dinput) - : dinput_(dinput) -{} - -void DInputAdapter::StopDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::StartDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::StopDInputCallbackSink, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallbackDHIds, status); } -DInputAdapter::PrepareDInputCallback::PrepareDInputCallback(std::shared_ptr dinput) - : dinput_(dinput) -{} - -void DInputAdapter::PrepareDInputCallback::OnResult(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::StopDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::PrepareDInputCallback, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallbackDHIds, status); } -DInputAdapter::UnprepareDInputCallback::UnprepareDInputCallback(std::shared_ptr dinput) - : dinput_(dinput) -{} +void DistributedInputAdapter::StartDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status) +{ + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StartDInputCallbackSink, status); +} -void DInputAdapter::UnprepareDInputCallback::OnResult(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::StopDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::UnprepareDInputCallback, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::StopDInputCallbackSink, status); } -DInputAdapter::PrepareStartDInputCallbackSink::PrepareStartDInputCallbackSink(std::shared_ptr dinput) - : dinput_(dinput) -{} +void DistributedInputAdapter::PrepareStartDInputCallback::OnResult(const std::string &devId, const int32_t &status) +{ + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::PrepareStartDInputCallback, status); +} -void DInputAdapter::PrepareStartDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::UnPrepareStopDInputCallback::OnResult(const std::string &devId, const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::PrepareStartDInputCallbackSink, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallback, status); } -DInputAdapter::UnPrepareStopDInputCallbackSink::UnPrepareStopDInputCallbackSink(std::shared_ptr dinput) - : dinput_(dinput) -{} +void DistributedInputAdapter::PrepareStartDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status) +{ + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::PrepareStartDInputCallbackSink, status); +} -void DInputAdapter::UnPrepareStopDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status) +void DistributedInputAdapter::UnPrepareStopDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status) { - std::shared_ptr dinput = dinput_.lock(); - CHKPV(dinput); - dinput->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallbackSink, status); + D_INPUT_ADAPTER->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallbackSink, status); } -void DInputAdapter::SessionStateCallback::OnResult(const std::string &devId, const uint32_t status) +void DistributedInputAdapter::SessionStateCallback::OnResult(const std::string &devId, const uint32_t status) { CHKPV(callback_); callback_(status); } -} // namespace Cooperate } // namespace DeviceStatus } // namespace Msdp } // namespace OHOS diff --git a/intention/cooperate/server/src/i_cooperate_state.cpp b/intention/cooperate/server/src/i_cooperate_state.cpp new file mode 100644 index 000000000..fbaa19863 --- /dev/null +++ b/intention/cooperate/server/src/i_cooperate_state.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "i_cooperate_state.h" + +#include "cooperate_device_manager.h" +#include "cooperate_event_manager.h" +#include "cooperate_sm.h" +#include "devicestatus_define.h" +#include "distributed_input_adapter.h" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "ICooperateState" }; +const std::string THREAD_NAME { "coordination_state" }; +} // namespace + +ICooperateState::ICooperateState() +{ + runner_ = AppExecFwk::EventRunner::Create(THREAD_NAME); + CHKPL(runner_); + eventHandler_ = std::make_shared(runner_); +} + +int32_t ICooperateState::PrepareAndStart(const std::string &remoteNetworkId, int32_t startDeviceId) +{ + CALL_INFO_TRACE; + std::string originNetworkId = COOR_DEV_MGR->GetOriginNetworkId(startDeviceId); + int32_t ret = RET_ERR; + if (NeedPrepare(remoteNetworkId, originNetworkId)) { + COOR_SM->UpdatePreparedDevices(remoteNetworkId, originNetworkId); + ret = D_INPUT_ADAPTER->PrepareRemoteInput(remoteNetworkId, originNetworkId, + [this, remoteNetworkId, startDeviceId](bool isSuccess) { + this->OnPrepareDistributedInput(isSuccess, remoteNetworkId, startDeviceId); + }); + if (ret != RET_OK) { + FI_HILOGE("Prepare remote input failed"); + COOR_SM->OnStartFinish(false, originNetworkId, startDeviceId); + COOR_SM->UpdatePreparedDevices("", ""); + } + } else { + ret = StartRemoteInput(startDeviceId); + if (ret != RET_OK) { + FI_HILOGE("Start remoteNetworkId input failed"); + COOR_SM->OnStartFinish(false, originNetworkId, startDeviceId); + } + } + return ret; +} + +void ICooperateState::OnPrepareDistributedInput(bool isSuccess, const std::string &remoteNetworkId, + int32_t startDeviceId) +{ + FI_HILOGI("isSuccess:%{public}s", isSuccess ? "true" : "false"); + if (!isSuccess) { + COOR_SM->UpdatePreparedDevices("", ""); + COOR_SM->OnStartFinish(false, remoteNetworkId, startDeviceId); + return; + } + std::string taskName = "start_dinput_task"; + std::function handleStartDinputFunc = + std::bind(&ICooperateState::StartRemoteInput, this, startDeviceId); + CHKPV(eventHandler_); + eventHandler_->ProxyPostTask(handleStartDinputFunc, taskName, 0); +} + +int32_t ICooperateState::StartRemoteInput(int32_t startDeviceId) +{ + CALL_DEBUG_ENTER; + std::pair networkIds = COOR_SM->GetPreparedDevices(); + std::vector inputDeviceDhids = COOR_DEV_MGR->GetCooperateDhids(startDeviceId); + if (inputDeviceDhids.empty()) { + COOR_SM->OnStartFinish(false, networkIds.first, startDeviceId); + return static_cast(CoordinationMessage::COORDINATION_FAIL); + } + int32_t ret = D_INPUT_ADAPTER->StartRemoteInput(networkIds.first, networkIds.second, inputDeviceDhids, + [this, remoteNetworkId = networkIds.first, startDeviceId](bool isSuccess) { + this->OnStartRemoteInput(isSuccess, remoteNetworkId, startDeviceId); + }); + if (ret != RET_OK) { + COOR_SM->OnStartFinish(false, networkIds.first, startDeviceId); + return static_cast(CoordinationMessage::COORDINATION_FAIL); + } + return RET_OK; +} + +void ICooperateState::OnStartRemoteInput(bool isSuccess, const std::string &remoteNetworkId, int32_t startDeviceId) +{ + CALL_DEBUG_ENTER; + std::string taskName = "start_finish_task"; + std::function handleStartFinishFunc = + std::bind(&CooperateSM::OnStartFinish, COOR_SM, isSuccess, remoteNetworkId, startDeviceId); + CHKPV(eventHandler_); + eventHandler_->ProxyPostTask(handleStartFinishFunc, taskName, 0); +} + +bool ICooperateState::NeedPrepare(const std::string &remoteNetworkId, const std::string &originNetworkId) +{ + CALL_DEBUG_ENTER; + std::pair prepared = COOR_SM->GetPreparedDevices(); + bool isNeed = !(remoteNetworkId == prepared.first && originNetworkId == prepared.second); + FI_HILOGI("NeedPrepare?:%{public}s", isNeed ? "true" : "false"); + return isNeed; +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS diff --git a/intention/cooperate/state_machine/src/device_manager.cpp b/intention/cooperate/state_machine/src/device_manager.cpp deleted file mode 100644 index 4e0dd1768..000000000 --- a/intention/cooperate/state_machine/src/device_manager.cpp +++ /dev/null @@ -1,411 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device_manager.h" - -#include -#include - -#include "devicestatus_define.h" -#include "dsoftbus_adapter.h" -#include "event_manager.h" -#include "napi_constants.h" - -namespace OHOS { -namespace Msdp { -namespace DeviceStatus { -namespace Cooperate { -namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceManager" }; -constexpr size_t NETWORK_ID_NUMS { 3 }; -constexpr size_t DESCRIPTOR_INDEX { 2 }; -} // namespace - -DeviceManager::Device::Device(std::shared_ptr dev) - : device_(dev) -{ - Populate(); -} - -int32_t DeviceManager::Device::GetId() const -{ - CHKPR(device_, RET_ERR); - return device_->GetId(); -} - -std::string DeviceManager::Device::GetName() const -{ - CHKPS(device_); - return device_->GetName(); -} - -std::string DeviceManager::Device::GetDhid() const -{ - return dhid_; -} - -std::string DeviceManager::Device::GetNetworkId() const -{ - return networkId_; -} - -int32_t DeviceManager::Device::GetProduct() const -{ - CHKPR(device_, RET_ERR); - return device_->GetProduct(); -} - -int32_t DeviceManager::Device::GetVendor() const -{ - CHKPR(device_, RET_ERR); - return device_->GetVendor(); -} - -std::string DeviceManager::Device::GetPhys() const -{ - CHKPS(device_); - return device_->GetPhys(); -} - -std::string DeviceManager::Device::GetUniq() const -{ - CHKPS(device_); - return device_->GetUniq(); -} - -bool DeviceManager::Device::IsPointerDevice() const -{ - CHKPF(device_); - return device_->IsPointerDevice(); -} - -IDevice::KeyboardType DeviceManager::Device::GetKeyboardType() const -{ - CHKPR(device_, IDevice::KeyboardType::KEYBOARD_TYPE_NONE); - return device_->GetKeyboardType(); -} - -bool DeviceManager::Device::IsKeyboard() const -{ - CHKPF(device_); - return device_->IsKeyboard(); -} - -void DeviceManager::Device::Populate() -{ - CALL_DEBUG_ENTER; - if (IsRemote()) { - networkId_ = MakeNetworkId(GetPhys()); - } - dhid_ = GenerateDescriptor(); -} - -bool DeviceManager::Device::IsRemote() -{ - const std::string INPUT_VIRTUAL_DEVICE_NAME { "DistributedInput " }; - return (GetName().find(INPUT_VIRTUAL_DEVICE_NAME) != std::string::npos); -} - -std::string DeviceManager::Device::MakeNetworkId(const std::string &phys) const -{ - std::vector idParts; - const std::string SPLIT_SYMBOL { "|" }; - StringSplit(phys, SPLIT_SYMBOL, idParts); - if (idParts.size() == NETWORK_ID_NUMS) { - return idParts[1]; - } - return {}; -} - -std::string DeviceManager::Device::GenerateDescriptor() -{ - const std::string phys = GetPhys(); - const std::string SPLIT_SYMBOL { "|" }; - const std::string DH_ID_PREFIX { "Input_" }; - std::string descriptor; - if (IsRemote() && !phys.empty()) { - FI_HILOGD("physicalPath:%{public}s", phys.c_str()); - std::vector idParts; - StringSplit(phys.c_str(), SPLIT_SYMBOL, idParts); - if (idParts.size() == NETWORK_ID_NUMS) { - descriptor = idParts[DESCRIPTOR_INDEX]; - } - return descriptor; - } - - const std::string name = GetName(); - const std::string uniq = GetUniq(); - std::string rawDescriptor = StringPrintf(":%04x:%04x:", GetVendor(), GetProduct()); - - if (!uniq.empty()) { - rawDescriptor += "uniqueId:" + uniq; - } - if (!phys.empty()) { - rawDescriptor += "physicalPath:" + phys; - } - if (!name.empty()) { - rawDescriptor += "name:" + std::regex_replace(name, std::regex(" "), ""); - } - descriptor = DH_ID_PREFIX + Sha256(rawDescriptor); - FI_HILOGD("Created descriptor raw:%{public}s", rawDescriptor.c_str()); - return descriptor; -} - -std::string DeviceManager::Device::Sha256(const std::string &in) const -{ - unsigned char out[SHA256_DIGEST_LENGTH * 2 + 1] = { 0 }; // 2:coefficient - SHA256_CTX ctx; - SHA256_Init(&ctx); - SHA256_Update(&ctx, in.data(), in.size()); - SHA256_Final(&out[SHA256_DIGEST_LENGTH], &ctx); - - constexpr int32_t width = 4; - constexpr unsigned char mask = 0x0F; - const char* hexCode = "0123456789abcdef"; - constexpr int32_t DOUBLE_TIMES = 2; - for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) { - unsigned char value = out[SHA256_DIGEST_LENGTH + i]; - out[i * DOUBLE_TIMES] = hexCode[(value >> width) & mask]; - out[i * DOUBLE_TIMES + 1] = hexCode[value & mask]; - } - out[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; - return reinterpret_cast(out); -} - -DeviceManager::DeviceManager(IContext *env) - : env_(env) -{} - -DeviceManager::~DeviceManager() -{ - Stop(); -} - -void DeviceManager::AttachSender(Channel::Sender sender) -{ - sender_ = sender; -} - -bool DeviceManager::Start() -{ - CALL_DEBUG_ENTER; - if (observer_ != nullptr) { - return true; - } - observer_ = std::make_shared(shared_from_this()); - int32_t ret = env_->GetDeviceManager().AddDeviceObserver(observer_); - if (ret != RET_OK) { - FI_HILOGE("Failed to add observer"); - observer_.reset(); - return false; - } - env_->GetDeviceManager().RetriggerHotplug(observer_); - return true; -} - -void DeviceManager::Stop() -{ - CALL_DEBUG_ENTER; - CHKPV(observer_); - env_->GetDeviceManager().RemoveDeviceObserver(observer_); - observer_.reset(); - devices_.clear(); -} - -bool DeviceManager::IsRemote(int32_t id) -{ - if (auto devIter = devices_.find(id); devIter != devices_.end()) { - CHKPF(devIter->second); - return devIter->second->IsRemote(); - } - return false; -} - -std::vector DeviceManager::GetCooperateDhids(int32_t deviceId) const -{ - CALL_INFO_TRACE; - std::vector inputDeviceDhids; - auto devIter = devices_.find(deviceId); - if (devIter == devices_.end()) { - FI_HILOGW("Cannot find pointer id:%{public}d", deviceId); - return inputDeviceDhids; - } - if (devIter->second == nullptr) { - FI_HILOGW("Device is nullptr"); - return inputDeviceDhids; - } - std::shared_ptr dev = devIter->second; - if (!dev->IsPointerDevice()) { - FI_HILOGD("Not pointer device"); - return inputDeviceDhids; - } - inputDeviceDhids.push_back(dev->GetDhid()); - FI_HILOGD("unq:%{public}s, type:%{public}s", inputDeviceDhids.back().c_str(), "pointer"); - - const std::string localNetworkId { DSoftbusAdapter::GetLocalNetworkId() }; - const std::string pointerNetworkId { dev->IsRemote() ? dev->GetNetworkId() : localNetworkId }; - - for (const auto &[id, dev] : devices_) { - CHKPC(dev); - const std::string networkId { dev->IsRemote() ? dev->GetNetworkId() : localNetworkId }; - if (networkId != pointerNetworkId) { - continue; - } - if (dev->GetKeyboardType() == IDevice::KEYBOARD_TYPE_ALPHABETICKEYBOARD) { - inputDeviceDhids.push_back(dev->GetDhid()); - FI_HILOGD("unq:%{public}s, type:%{public}s", inputDeviceDhids.back().c_str(), "supportkey"); - } - } - return inputDeviceDhids; -} - -std::vector DeviceManager::GetCooperateDhids(const std::string &dhid, bool isRemote) const -{ - int32_t deviceId { -1 }; - for (const auto &[id, dev] : devices_) { - CHKPC(dev); - if (dev->GetDhid() == dhid && dev->IsRemote() == isRemote) { - deviceId = id; - break; - } - } - return GetCooperateDhids(deviceId); -} - -std::string DeviceManager::GetOriginNetworkId(int32_t id) const -{ - CALL_INFO_TRACE; - auto devIter = devices_.find(id); - if (devIter == devices_.end()) { - FI_HILOGE("Failed to search for the device, id:%{public}d", id); - return {}; - } - CHKPS(devIter->second); - auto OriginNetworkId = devIter->second->GetNetworkId(); - if (OriginNetworkId.empty()) { - OriginNetworkId = DSoftbusAdapter::GetLocalNetworkId(); - } - return OriginNetworkId; -} - -std::string DeviceManager::GetOriginNetworkId(const std::string &dhid) const -{ - CALL_INFO_TRACE; - if (dhid.empty()) { - FI_HILOGD("The current netWorkId is an empty string"); - return {}; - } - for (const auto &[id, dev] : devices_) { - CHKPC(dev); - if (dev->IsRemote() && dev->GetDhid() == dhid) { - return dev->GetNetworkId(); - } - } - FI_HILOGD("The current netWorkId is an empty string"); - return {}; -} - -std::string DeviceManager::GetDhid(int32_t deviceId) const -{ - CALL_INFO_TRACE; - if (auto devIter = devices_.find(deviceId); devIter != devices_.end()) { - if (devIter->second != nullptr) { - return devIter->second->GetDhid(); - } - FI_HILOGW("Device is nullptr"); - } - return {}; -} - -bool DeviceManager::HasLocalPointerDevice() const -{ - for (const auto &[id, dev] : devices_) { - CHKPC(dev); - if (!dev->IsRemote() && dev->IsPointerDevice()) { - FI_HILOGD("It is currently a mouse device"); - return true; - } - } - FI_HILOGD("Not currently a mouse device"); - return false; -} - -void DeviceManager::OnDeviceAdded(std::shared_ptr device) -{ - CALL_INFO_TRACE; - CHKPV(device); - auto dev = std::make_shared(device); - devices_.insert_or_assign(dev->GetId(), dev); - if (dev->IsKeyboard()) { - sender_.Send(CooperateEvent(CooperateEventType::PLUG_KEYBOARD, HotplugEvent { - .dhid = dev->GetDhid() - })); - } - FI_HILOGD("Add device %{public}d:%{public}s, Dhid:\"%{public}s\", Network id:\"%{public}s\", " - "local/remote:\"%{public}s\"", device->GetId(), device->GetDevPath().c_str(), dev->GetDhid().c_str(), - dev->GetNetworkId().c_str(), dev->IsRemote() ? "Remote Device" : "Local Device"); -} - -void DeviceManager::OnDeviceRemoved(std::shared_ptr device) -{ - CALL_INFO_TRACE; - CHKPV(device); - auto iter = devices_.find(device->GetId()); - if (iter == devices_.end()) { - FI_HILOGE("The device corresponding to the current id:%{public}d cannot be found", device->GetId()); - return; - } - std::shared_ptr dev = iter->second; - devices_.erase(iter); - CHKPV(dev); - if (dev->IsPointerDevice()) { - sender_.Send(CooperateEvent(CooperateEventType::UNPLUG_POINTER, HotplugEvent { - .dhid = dev->GetDhid() - })); - } else if (dev->IsKeyboard() && !dev->IsRemote() && - (dev->GetKeyboardType() == IDevice::KEYBOARD_TYPE_ALPHABETICKEYBOARD)) { - sender_.Send(CooperateEvent(CooperateEventType::UNPLUG_KEYBOARD, HotplugEvent { - .dhid = dev->GetDhid() - })); - } -} - -DeviceManager::DeviceObserver::DeviceObserver(std::shared_ptr devMgr) - : devMgr_(devMgr) -{} - -void DeviceManager::DeviceObserver::OnDeviceAdded(std::shared_ptr device) -{ - std::shared_ptr devMgr = devMgr_.lock(); - if (devMgr == nullptr) { - FI_HILOGE("Reference has expired"); - return; - } - devMgr->OnDeviceAdded(device); -} - -void DeviceManager::DeviceObserver::OnDeviceRemoved(std::shared_ptr device) -{ - std::shared_ptr devMgr = devMgr_.lock(); - if (devMgr == nullptr) { - FI_HILOGE("Reference has expired"); - return; - } - devMgr->OnDeviceRemoved(device); -} -} // namespace Cooperate -} // namespace DeviceStatus -} // namespace Msdp -} // namespace OHOS \ No newline at end of file -- Gitee