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_label.h"
17 
18 #include "plugin_manager/include/aie_plugin_info.h"
19 #include "utils/log/aie_log.h"
20 
21 namespace OHOS {
22 namespace AI {
23 namespace {
24 const std::string PLUS = "+";
25 const std::string DELIMITER = ":";
26 }
27 
28 std::mutex PluginLabel::instanceLock_;
29 PluginLabel *PluginLabel::instance_ = nullptr;
30 
GetInstance()31 PluginLabel *PluginLabel::GetInstance()
32 {
33     CHK_RET(instance_ != nullptr, instance_);
34 
35     std::lock_guard<std::mutex> lock(instanceLock_);
36     CHK_RET(instance_ != nullptr, instance_);
37 
38     PluginLabel *temp = nullptr;
39     AIE_NEW(temp, PluginLabel);
40     CHK_RET(temp == nullptr, nullptr);
41 
42     instance_ = temp;
43     return instance_;
44 }
45 
ReleaseInstance()46 void PluginLabel::ReleaseInstance()
47 {
48     std::lock_guard<std::mutex> lock(instanceLock_);
49     AIE_DELETE(instance_);
50 }
51 
52 PluginLabel::PluginLabel() = default;
53 
54 PluginLabel::~PluginLabel() = default;
55 
GetLibPath(const std::string & aid,long long & version,std::string & libPath)56 int PluginLabel::GetLibPath(const std::string &aid, long long &version, std::string &libPath)
57 {
58     // The label combined algorithm ID and algorithm version
59     std::string label = aid + PLUS + std::to_string(version);
60     auto queryKey = label + DELIMITER + ALGORITHM_INFO_TABLE_FIELD_NAME_FULLPATH;
61     if (label == "cv_card_rectification+20001001") {
62         libPath = "/usr/lib/libcv_card_rectification.so";
63     } else if (label == "sample_plugin_1+1") {
64         libPath = "/usr/lib/libsample_plugin_1.so";
65     } else if (label == "sample_plugin_2+1") {
66         libPath = "/usr/lib/libsample_plugin_2.so";
67     } else if (label == "asr_keyword_spotting+20001002") {
68         libPath = "/usr/lib/libasr_keyword_spotting.so";
69     } else if (label == "cv_image_classification+20001001") {
70         libPath = "/usr/lib/libcv_image_classification.so";
71     } else {
72         libPath = "";
73     }
74     if (libPath.empty()) {
75         HILOGE("[PluginLabel]Query lib path failed.");
76         return RETCODE_FAILURE;
77     }
78     HILOGI("[PluginLabel]Succeed to get lib path of %s.", aid.c_str());
79     return RETCODE_SUCCESS;
80 }
81 } // namespace AI
82 } // namespace OHOS