1e0e9324cSopenharmony_ci/* 2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License. 5e0e9324cSopenharmony_ci * You may obtain a copy of the License at 6e0e9324cSopenharmony_ci * 7e0e9324cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0e9324cSopenharmony_ci * 9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and 13e0e9324cSopenharmony_ci * limitations under the License. 14e0e9324cSopenharmony_ci */ 15e0e9324cSopenharmony_ci 16e0e9324cSopenharmony_ci#include "interaction_manager.h" 17e0e9324cSopenharmony_ci#include <future> 18e0e9324cSopenharmony_ci#include <unistd.h> 19e0e9324cSopenharmony_ci#include "ability_manager_client.h" 20e0e9324cSopenharmony_ci#include "common/common_macro.h" 21e0e9324cSopenharmony_ci#include "common/sharing_log.h" 22e0e9324cSopenharmony_ci#include "event/event_manager.h" 23e0e9324cSopenharmony_ci#include "interaction/device_kit/dm_kit.h" 24e0e9324cSopenharmony_ci#include "interaction/domain/domain_manager.h" 25e0e9324cSopenharmony_ci#include "interaction/ipc_codec/ipc_msg.h" 26e0e9324cSopenharmony_ci#include "magic_enum.hpp" 27e0e9324cSopenharmony_ci#include "utils.h" 28e0e9324cSopenharmony_ci 29e0e9324cSopenharmony_cinamespace OHOS { 30e0e9324cSopenharmony_cinamespace Sharing { 31e0e9324cSopenharmony_ci 32e0e9324cSopenharmony_ciInteractionManager::InteractionManager() 33e0e9324cSopenharmony_ci{ 34e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 35e0e9324cSopenharmony_ci EventManager::GetInstance().AddListener(std::make_shared<InteractionEventListener>()); 36e0e9324cSopenharmony_ci} 37e0e9324cSopenharmony_ci 38e0e9324cSopenharmony_ciInteractionManager::~InteractionManager() 39e0e9324cSopenharmony_ci{ 40e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 41e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 42e0e9324cSopenharmony_ci interactions_.clear(); 43e0e9324cSopenharmony_ci sharedFromThis_.reset(); 44e0e9324cSopenharmony_ci} 45e0e9324cSopenharmony_ci 46e0e9324cSopenharmony_civoid InteractionManager::Init() 47e0e9324cSopenharmony_ci{ 48e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 49e0e9324cSopenharmony_ci if (sharedFromThis_ == nullptr) { 50e0e9324cSopenharmony_ci sharedFromThis_ = Ptr(this, [](InteractionManager *) { SHARING_LOGD("trace."); }); 51e0e9324cSopenharmony_ci } 52e0e9324cSopenharmony_ci DomainManager::GetInstance()->SetListener(sharedFromThis_); 53e0e9324cSopenharmony_ci} 54e0e9324cSopenharmony_ci 55e0e9324cSopenharmony_ciint32_t InteractionManager::HandleEvent(SharingEvent &event) 56e0e9324cSopenharmony_ci{ 57e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 58e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(event.eventMsg); 59e0e9324cSopenharmony_ci SHARING_LOGI("fromMgr: %{public}u, srcId: %{public}u, toMgr: %{public}u, dstId: %{public}u, event: %{public}s.", 60e0e9324cSopenharmony_ci event.eventMsg->fromMgr, event.eventMsg->srcId, event.eventMsg->toMgr, event.eventMsg->dstId, 61e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str()); 62e0e9324cSopenharmony_ci switch (event.eventMsg->type) { 63e0e9324cSopenharmony_ci case EVENT_CONFIGURE_INTERACT: 64e0e9324cSopenharmony_ci break; 65e0e9324cSopenharmony_ci case EVENT_INTERACTIONMGR_DESTROY_INTERACTION: { 66e0e9324cSopenharmony_ci auto eventMsg = std::static_pointer_cast<InteractionEventMsg>(event.eventMsg); 67e0e9324cSopenharmony_ci if (eventMsg) { 68e0e9324cSopenharmony_ci DestroyInteraction(eventMsg->dstId); 69e0e9324cSopenharmony_ci } else { 70e0e9324cSopenharmony_ci SHARING_LOGE("event msg null."); 71e0e9324cSopenharmony_ci } 72e0e9324cSopenharmony_ci break; 73e0e9324cSopenharmony_ci } 74e0e9324cSopenharmony_ci case EVENT_INTERACTIONMGR_REMOVE_INTERACTION: { 75e0e9324cSopenharmony_ci auto eventMsg = std::static_pointer_cast<InteractionEventMsg>(event.eventMsg); 76e0e9324cSopenharmony_ci if (eventMsg) { 77e0e9324cSopenharmony_ci RemoveInteraction(eventMsg->dstId); 78e0e9324cSopenharmony_ci } else { 79e0e9324cSopenharmony_ci SHARING_LOGE("event msg null."); 80e0e9324cSopenharmony_ci } 81e0e9324cSopenharmony_ci break; 82e0e9324cSopenharmony_ci } 83e0e9324cSopenharmony_ci default: { 84e0e9324cSopenharmony_ci auto eventMsg = std::static_pointer_cast<InteractionEventMsg>(event.eventMsg); 85e0e9324cSopenharmony_ci if (eventMsg) { 86e0e9324cSopenharmony_ci Interaction::Ptr interaction = GetInteraction(eventMsg->dstId); 87e0e9324cSopenharmony_ci if (interaction) { 88e0e9324cSopenharmony_ci interaction->HandleEvent(event); 89e0e9324cSopenharmony_ci } else { 90e0e9324cSopenharmony_ci SHARING_LOGE("interaction null interactionId: %{public}d.", eventMsg->dstId); 91e0e9324cSopenharmony_ci } 92e0e9324cSopenharmony_ci } else { 93e0e9324cSopenharmony_ci SHARING_LOGE("event msg null."); 94e0e9324cSopenharmony_ci } 95e0e9324cSopenharmony_ci break; 96e0e9324cSopenharmony_ci } 97e0e9324cSopenharmony_ci } 98e0e9324cSopenharmony_ci 99e0e9324cSopenharmony_ci return 0; 100e0e9324cSopenharmony_ci} 101e0e9324cSopenharmony_ci 102e0e9324cSopenharmony_ciInteraction::Ptr InteractionManager::CreateInteraction(const std::string &className) 103e0e9324cSopenharmony_ci{ 104e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 105e0e9324cSopenharmony_ci Interaction::Ptr interaction = std::make_shared<Interaction>(); 106e0e9324cSopenharmony_ci if (interaction->CreateScene(className)) { 107e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 108e0e9324cSopenharmony_ci interactions_.emplace(interaction->GetId(), interaction); 109e0e9324cSopenharmony_ci SHARING_LOGI("id: %{public}d size: %{public}zu.", interaction->GetId(), interactions_.size()); 110e0e9324cSopenharmony_ci return interaction; 111e0e9324cSopenharmony_ci } else { 112e0e9324cSopenharmony_ci SHARING_LOGE("create scene failed."); 113e0e9324cSopenharmony_ci return nullptr; 114e0e9324cSopenharmony_ci } 115e0e9324cSopenharmony_ci} 116e0e9324cSopenharmony_ci 117e0e9324cSopenharmony_civoid InteractionManager::DestroyInteraction(uint32_t interactionId) 118e0e9324cSopenharmony_ci{ 119e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 120e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 121e0e9324cSopenharmony_ci auto itr = interactions_.find(interactionId); 122e0e9324cSopenharmony_ci if (itr != interactions_.end() && itr->second != nullptr) { 123e0e9324cSopenharmony_ci itr->second->Destroy(); 124e0e9324cSopenharmony_ci } 125e0e9324cSopenharmony_ci} 126e0e9324cSopenharmony_ci 127e0e9324cSopenharmony_civoid InteractionManager::RemoveInteraction(uint32_t interactionId) 128e0e9324cSopenharmony_ci{ 129e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 130e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 131e0e9324cSopenharmony_ci auto itr = interactions_.find(interactionId); 132e0e9324cSopenharmony_ci if (itr != interactions_.end()) { 133e0e9324cSopenharmony_ci SHARING_LOGI("remove interaction key: %{public}s, id: %{public}u.", itr->second->GetRpcKey().c_str(), 134e0e9324cSopenharmony_ci interactionId); 135e0e9324cSopenharmony_ci interactionKeys_.erase(itr->second->GetRpcKey()); 136e0e9324cSopenharmony_ci interactions_.erase(itr); 137e0e9324cSopenharmony_ci } 138e0e9324cSopenharmony_ci} 139e0e9324cSopenharmony_ci 140e0e9324cSopenharmony_ciInteraction::Ptr InteractionManager::GetInteraction(uint32_t interactionId) 141e0e9324cSopenharmony_ci{ 142e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 143e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 144e0e9324cSopenharmony_ci auto itr = interactions_.find(interactionId); 145e0e9324cSopenharmony_ci if (itr != interactions_.end()) { 146e0e9324cSopenharmony_ci return itr->second; 147e0e9324cSopenharmony_ci } 148e0e9324cSopenharmony_ci 149e0e9324cSopenharmony_ci return nullptr; 150e0e9324cSopenharmony_ci} 151e0e9324cSopenharmony_ci 152e0e9324cSopenharmony_ciint32_t InteractionManager::OnDomainMsg(std::shared_ptr<BaseDomainMsg> msg) 153e0e9324cSopenharmony_ci{ 154e0e9324cSopenharmony_ci return 0; 155e0e9324cSopenharmony_ci} 156e0e9324cSopenharmony_ci 157e0e9324cSopenharmony_ciint32_t InteractionManager::SendDomainMsg(std::shared_ptr<BaseDomainMsg> msg) 158e0e9324cSopenharmony_ci{ 159e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 160e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(msg); 161e0e9324cSopenharmony_ci std::async(std::launch::async, [this, msg] { DomainManager::GetInstance()->SendDomainRequest(msg->toDevId, msg); }); 162e0e9324cSopenharmony_ci 163e0e9324cSopenharmony_ci return 0; 164e0e9324cSopenharmony_ci} 165e0e9324cSopenharmony_ci 166e0e9324cSopenharmony_ciint32_t InteractionEventListener::OnEvent(SharingEvent &event) 167e0e9324cSopenharmony_ci{ 168e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 169e0e9324cSopenharmony_ci return InteractionManager::GetInstance().HandleEvent(event); 170e0e9324cSopenharmony_ci} 171e0e9324cSopenharmony_ci 172e0e9324cSopenharmony_civoid InteractionManager::AddInteractionKey(const std::string &key, uint32_t interactionId) 173e0e9324cSopenharmony_ci{ 174e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 175e0e9324cSopenharmony_ci DelInteractionKey(key); 176e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 177e0e9324cSopenharmony_ci interactionKeys_.emplace(key, interactionId); 178e0e9324cSopenharmony_ci} 179e0e9324cSopenharmony_ci 180e0e9324cSopenharmony_civoid InteractionManager::DelInteractionKey(const std::string &key) 181e0e9324cSopenharmony_ci{ 182e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 183e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 184e0e9324cSopenharmony_ci auto itr = interactionKeys_.find(key); 185e0e9324cSopenharmony_ci if (itr != interactionKeys_.end()) { 186e0e9324cSopenharmony_ci interactionKeys_.erase(itr); 187e0e9324cSopenharmony_ci } 188e0e9324cSopenharmony_ci} 189e0e9324cSopenharmony_ci 190e0e9324cSopenharmony_ciint32_t InteractionManager::GetInteractionId(const std::string &key) 191e0e9324cSopenharmony_ci{ 192e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 193e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 194e0e9324cSopenharmony_ci auto itr = interactionKeys_.find(key); 195e0e9324cSopenharmony_ci if (itr != interactionKeys_.end()) { 196e0e9324cSopenharmony_ci return itr->second; 197e0e9324cSopenharmony_ci } 198e0e9324cSopenharmony_ci 199e0e9324cSopenharmony_ci return 0; 200e0e9324cSopenharmony_ci} 201e0e9324cSopenharmony_ci 202e0e9324cSopenharmony_ciInteraction::Ptr InteractionManager::GetInteraction(const std::string &key) 203e0e9324cSopenharmony_ci{ 204e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 205e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 206e0e9324cSopenharmony_ci if (interactionKeys_.find(key) != interactionKeys_.end()) { 207e0e9324cSopenharmony_ci auto itr = interactions_.find(interactionKeys_[key]); 208e0e9324cSopenharmony_ci if (itr != interactions_.end()) { 209e0e9324cSopenharmony_ci return itr->second; 210e0e9324cSopenharmony_ci } 211e0e9324cSopenharmony_ci } 212e0e9324cSopenharmony_ci 213e0e9324cSopenharmony_ci return nullptr; 214e0e9324cSopenharmony_ci} 215e0e9324cSopenharmony_ci 216e0e9324cSopenharmony_ci} // namespace Sharing 217e0e9324cSopenharmony_ci} // namespace OHOS