• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "plugin_manager/include/plugin.h"
17 
18 #include <dlfcn.h>
19 
20 #include "securec.h"
21 
22 #include "platform/dl_operation/include/aie_dl_operation.h"
23 #include "plugin_manager/include/plugin_label.h"
24 #include "protocol/retcode_inner/aie_retcode_inner.h"
25 #include "utils/aie_macros.h"
26 #include "utils/log/aie_log.h"
27 
28 namespace OHOS {
29 namespace AI {
30 class HandleGuard {
31 public:
HandleGuard(const std::string & aid,void * handle)32     HandleGuard(const std::string &aid, void *handle) : aid_(aid), handle_(handle)
33     {
34     }
35 
Detach()36     void Detach()
37     {
38         handle_ = nullptr;
39     }
40 
~HandleGuard()41     ~HandleGuard()
42     {
43         CHK_RET_NONE(handle_ == nullptr);
44         AieDlclose(handle_);
45         handle_ = nullptr;
46         HILOGI("[Plugin]Succeed to close library, aid=%s.", aid_.c_str());
47     }
48 private:
49     std::string aid_;
50     void *handle_ = nullptr;
51 };
52 
Plugin(const std::string & aid,long long version)53 Plugin::Plugin(const std::string &aid, long long version)
54     : pluginAlgorithm_(nullptr), aid_(aid), version_(version), handle_(nullptr)
55 {
56 }
57 
~Plugin()58 Plugin::~Plugin()
59 {
60     UnloadPluginAlgorithm();
61     PluginLabel *pluginLabel = PluginLabel::GetInstance();
62     if (pluginLabel != nullptr) {
63         pluginLabel->ReleaseInstance();
64     }
65 }
66 
LoadPluginAlgorithm()67 int Plugin::LoadPluginAlgorithm()
68 {
69     PluginLabel *pluginLabel = PluginLabel::GetInstance();
70     if (pluginLabel == nullptr) {
71         HILOGE("[Plugin]PluginLabel is nullptr.");
72         return RETCODE_FAILURE;
73     }
74     std::string libPath;
75     int retCode = pluginLabel->GetLibPath(aid_, version_, libPath);
76     if (retCode != RETCODE_SUCCESS) {
77         HILOGE("[Plugin]Failed to get lib path.");
78         return retCode;
79     }
80     void *handle = AieDlopen(libPath.c_str());
81     if (handle == nullptr) {
82 #ifdef OHOS_DEBUG
83         HILOGE("[Plugin]Failed to open lib(%s), ret: %s.", libPath.c_str(), AieDlerror());
84 #else
85         HILOGE("[Plugin]Failed to open lib(%s).", libPath.c_str());
86 #endif
87         return RETCODE_OPEN_SO_FAILED;
88     }
89 
90     HandleGuard handleGuard(aid_, handle);
91 
92     IPLUGIN_INTERFACE fp = (IPLUGIN_INTERFACE)AieDlsym(handle, PLUGIN_INTERFACE_NAME);
93     if (fp == nullptr) {
94         HILOGE("[Plugin]Failed to get symbol %s in file %s.", PLUGIN_INTERFACE_NAME, libPath.c_str());
95         return RETCODE_SO_LACK_SYMBOL;
96     }
97 
98     IPlugin *pluginAlgorithm = (*fp)();
99     if (pluginAlgorithm == nullptr) {
100         HILOGE("[Plugin]Get plugin object failed.");
101         return RETCODE_NULL_PARAM;
102     }
103 
104     handleGuard.Detach();
105     pluginAlgorithm_ = pluginAlgorithm;
106     handle_ = handle;
107 
108     return RETCODE_SUCCESS;
109 }
110 
UnloadPluginAlgorithm()111 void Plugin::UnloadPluginAlgorithm()
112 {
113     AIE_DELETE(pluginAlgorithm_);
114     if (handle_) {
115         AieDlclose(handle_);
116         handle_ = nullptr;
117     }
118     HILOGI("[Plugin]Succeed to unload plugin algorithm, aid=%s.", aid_.c_str());
119 }
120 
GetPluginAlgorithm()121 IPlugin *Plugin::GetPluginAlgorithm()
122 {
123     return pluginAlgorithm_;
124 }
125 
SetPluginAlgorithm(IPlugin * pluginAlgorithm)126 void Plugin::SetPluginAlgorithm(IPlugin *pluginAlgorithm)
127 {
128     pluginAlgorithm_ = pluginAlgorithm;
129 }
130 
GetVersion() const131 long long Plugin::GetVersion() const
132 {
133     return version_;
134 }
135 
GetAid() const136 std::string Plugin::GetAid() const
137 {
138     return aid_;
139 }
140 } // namespace AI
141 } // namespace OHOS