1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "trigger_connector_internal_impl.h" 16 #include <vector> 17 #include <iservmgr_hdi.h> 18 #include "intell_voice_log.h" 19 #include "v1_0/iintell_voice_trigger_manager.h" 20 #include "memory_guard.h" 21 22 #define LOG_TAG "TriggerConnectorInternalImpl" 23 24 using namespace OHOS::HDI::IntelligentVoice::Trigger::V1_0; 25 26 namespace OHOS { 27 namespace IntellVoiceTrigger { TriggerConnectorInternalImpl(OnServiceStartCb cb)28TriggerConnectorInternalImpl::TriggerConnectorInternalImpl(OnServiceStartCb cb) : servmgr_(IServiceManager::Get()) 29 { 30 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard; 31 IntellVoiceTriggerAdapterDsecriptor descriptor; 32 descriptor.adapterName = "primary"; 33 34 if (servmgr_ != nullptr) { 35 auto connector = sptr<TriggerConnector>(new (std::nothrow) TriggerConnector(cb, descriptor)); 36 if (connector != nullptr) { 37 servmgr_->RegisterServiceStatusListener(connector, DEVICE_CLASS_DEFAULT); 38 connectors_[descriptor.adapterName] = connector; 39 } else { 40 INTELL_VOICE_LOG_ERROR("failed to malloc connector"); 41 } 42 } else { 43 INTELL_VOICE_LOG_ERROR("failed to get hdf service manager"); 44 } 45 } 46 ~TriggerConnectorInternalImpl()47TriggerConnectorInternalImpl::~TriggerConnectorInternalImpl() 48 { 49 INTELL_VOICE_LOG_DEBUG("TriggerConnectorInternalImpl destructor"); 50 if (servmgr_ != nullptr) { 51 for (auto it = connectors_.begin(); it != connectors_.end(); ++it) { 52 servmgr_->UnregisterServiceStatusListener(it->second); 53 } 54 } 55 } 56 ListModuleDescriptors()57std::vector<TriggerConnectorModuleDesc> TriggerConnectorInternalImpl::ListModuleDescriptors() 58 { 59 std::vector<TriggerConnectorModuleDesc> ret; 60 for (auto it = connectors_.begin(); it != connectors_.end(); ++it) { 61 TriggerConnectorModuleDesc item; 62 item.adapterName = it->first; 63 item.properties = it->second->GetProperties(); 64 ret.emplace_back(item); 65 } 66 return ret; 67 } 68 GetModule(const std::string & adapterName,std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback)69std::shared_ptr<IIntellVoiceTriggerConnectorModule> TriggerConnectorInternalImpl::GetModule( 70 const std::string &adapterName, std::shared_ptr<IIntellVoiceTriggerConnectorCallback> callback) 71 { 72 auto it = connectors_.find(adapterName); 73 if ((it == connectors_.end()) || (it->second == nullptr)) { 74 INTELL_VOICE_LOG_ERROR("failed to find connector"); 75 return nullptr; 76 } 77 78 return it->second->GetModule(callback); 79 } 80 } 81 } 82