• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "intell_voice_engine_manager_impl.h"
16 
17 #include <dlfcn.h>
18 #include <cinttypes>
19 #include "hdf_base.h"
20 #include "intell_voice_log.h"
21 #include "intell_voice_engine_adapter_impl.h"
22 
23 #undef HDF_LOG_TAG
24 #define HDF_LOG_TAG "IntelligentVoiceEngineManagerImpl"
25 
26 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
27 
28 namespace OHOS {
29 namespace IntelligentVoice {
30 namespace Engine {
IntellVoiceEngineManagerImplGetInstance(void)31 extern "C" IIntellVoiceEngineManager *IntellVoiceEngineManagerImplGetInstance(void)
32 {
33     return new (std::nothrow) IntellVoiceEngineManagerImpl();
34 }
35 
LoadVendorLib()36 int32_t IntellVoiceEngineManagerImpl::LoadVendorLib()
37 {
38     std::string error;
39     const char *vendorLibPath = HDF_LIBRARY_FULL_PATH("libvendor_intell_voice_engine");
40     engineManagerPriv_.handle = dlopen(vendorLibPath, RTLD_LAZY);
41     if (engineManagerPriv_.handle == nullptr) {
42         error = dlerror();
43         INTELLIGENT_VOICE_LOGE("load path%{public}s, dlopen err=%{public}s", vendorLibPath, error.c_str());
44         return HDF_FAILURE;
45     }
46 
47     (void)dlerror(); // clear existing error
48 
49     engineManagerPriv_.getEngineManagerHalInst = reinterpret_cast<GetEngineManagerHalInstFunc>(dlsym(
50         engineManagerPriv_.handle, "GetIntellVoiceEngineManagerHalInst"));
51     if (engineManagerPriv_.getEngineManagerHalInst == nullptr) {
52         error = dlerror();
53         INTELLIGENT_VOICE_LOGE("dlsym GetIntellVoiceEngineManagerHalInst err=%{public}s", error.c_str());
54         dlclose(engineManagerPriv_.handle);
55         engineManagerPriv_.handle = nullptr;
56         return HDF_FAILURE;
57     }
58 
59     INTELLIGENT_VOICE_LOGI("load vendor lib success");
60 
61     return HDF_SUCCESS;
62 }
63 
UnloadVendorLib()64 void IntellVoiceEngineManagerImpl::UnloadVendorLib()
65 {
66     engineManagerPriv_.handle = nullptr;
67 }
68 
IntellVoiceEngineManagerImpl()69 IntellVoiceEngineManagerImpl::IntellVoiceEngineManagerImpl()
70 {
71     if (LoadVendorLib() == static_cast<int32_t>(HDF_SUCCESS)) {
72         inst_ = engineManagerPriv_.getEngineManagerHalInst();
73     }
74 }
75 
~IntellVoiceEngineManagerImpl()76 IntellVoiceEngineManagerImpl::~IntellVoiceEngineManagerImpl()
77 {
78     adapters_.clear();
79     inst_ = nullptr;
80     UnloadVendorLib();
81 }
82 
GetAdapterDescriptors(std::vector<IntellVoiceEngineAdapterDescriptor> & descs)83 int32_t IntellVoiceEngineManagerImpl::GetAdapterDescriptors(std::vector<IntellVoiceEngineAdapterDescriptor>& descs)
84 {
85     return HDF_SUCCESS;
86 }
87 
CreateAdapter(const IntellVoiceEngineAdapterDescriptor & descriptor,sptr<IIntellVoiceEngineAdapter> & adapter)88 int32_t IntellVoiceEngineManagerImpl::CreateAdapter(
89     const IntellVoiceEngineAdapterDescriptor &descriptor, sptr<IIntellVoiceEngineAdapter> &adapter)
90 {
91     std::lock_guard<std::mutex> lock(mutex_);
92 
93     if (inst_ == nullptr) {
94         INTELLIGENT_VOICE_LOGE("inst is nullptr");
95         return HDF_FAILURE;
96     }
97 
98     std::unique_ptr<IEngine> engine = nullptr;
99     inst_->CreateAdapter(descriptor, engine);
100     if (engine == nullptr) {
101         INTELLIGENT_VOICE_LOGE("get adapter device from hal failed");
102         return HDF_FAILURE;
103     }
104 
105     adapter = sptr<IIntellVoiceEngineAdapter>(new (std::nothrow) IntellVoiceEngineAdapterImpl(std::move(engine)));
106     if (adapter == nullptr) {
107         INTELLIGENT_VOICE_LOGE("malloc intell voice adapter server failed ");
108         return HDF_ERR_MALLOC_FAIL;
109     }
110 
111     adapters_.insert(std::make_pair(descriptor.adapterType, adapter));
112     return HDF_SUCCESS;
113 }
114 
ReleaseAdapter(const IntellVoiceEngineAdapterDescriptor & descriptor)115 int32_t IntellVoiceEngineManagerImpl::ReleaseAdapter(const IntellVoiceEngineAdapterDescriptor &descriptor)
116 {
117     std::lock_guard<std::mutex> lock(mutex_);
118 
119     if (inst_ == nullptr) {
120         INTELLIGENT_VOICE_LOGE("inst is nullptr");
121         return HDF_FAILURE;
122     }
123 
124     auto it = adapters_.find(descriptor.adapterType);
125     if (it == adapters_.end()) {
126         INTELLIGENT_VOICE_LOGW("can not find adapter, %{public}d", descriptor.adapterType);
127         return HDF_SUCCESS;
128     }
129 
130     inst_->ReleaseAdapter(descriptor);
131 
132     it->second = nullptr;
133     adapters_.erase(it);
134     return HDF_SUCCESS;
135 }
136 }
137 }
138 }
139