• 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_module.h"
17 
18 #include <dlfcn.h>
19 #include <iostream>
20 
21 #include "logging.h"
22 #include "plugin_module_api.h"
23 
PluginModule(const std::string & path)24 PluginModule::PluginModule(const std::string& path) : handle_(nullptr), running_(false), path_(path),
25                                                         structPtr_(nullptr) {}
26 
~PluginModule()27 PluginModule::~PluginModule() {}
28 
ComputeSha256()29 std::string PluginModule::ComputeSha256()
30 {
31     return "";
32 }
33 
Load()34 bool PluginModule::Load()
35 {
36     char realPath[PATH_MAX + 1] = {0};
37     if (handle_ != nullptr) {
38         HILOG_DEBUG(LOG_CORE, "%s:already open", __func__);
39         return false;
40     }
41 
42     if ((path_.length() >= PATH_MAX) || (realpath(path_.c_str(), realPath) == nullptr)) {
43         HILOG_ERROR(LOG_CORE, "%s:so filename invalid, errno=%d", __func__, errno);
44         return false;
45     }
46 
47     std::string rpath = realPath; // for SC warning
48     handle_ = dlopen(rpath.c_str(), RTLD_NOW);
49     if (handle_ == nullptr) {
50         HILOG_DEBUG(LOG_CORE, "%s:dlopen err:%s.", __func__, dlerror());
51         return false;
52     }
53     return true;
54 }
55 
Unload()56 bool PluginModule::Unload()
57 {
58     HILOG_INFO(LOG_CORE, "%s:unload ready!", __func__);
59     if (handle_ != nullptr) {
60         int ret = dlclose(handle_);
61         HILOG_INFO(LOG_CORE, "%s:unload plugin ret = %d", __func__, ret);
62         handle_ = nullptr;
63         structPtr_ = nullptr;
64         return true;
65     }
66 
67     return false;
68 }
69 
GetInfo(PluginModuleInfo & info)70 bool PluginModule::GetInfo(PluginModuleInfo& info)
71 {
72     if (handle_ != nullptr) {
73         if (structPtr_ == nullptr) {
74             return false;
75         }
76         info.bufferSizeHint = structPtr_->resultBufferSizeHint;
77         info.name.assign(structPtr_->name);
78         return true;
79     }
80     return false;
81 }
82 
GetSampleMode() const83 PluginModule::SampleMode PluginModule::GetSampleMode() const
84 {
85     if (structPtr_ && structPtr_->callbacks) {
86         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
87             return POLLING;
88         } else if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
89             return STREAMING;
90         }
91     }
92     return UNKNOWN;
93 }
94 
SetConfigData(const std::string & data)95 void PluginModule::SetConfigData(const std::string& data)
96 {
97     configData_ = data;
98 }
99 
GetConfigData() const100 std::string PluginModule::GetConfigData() const
101 {
102     return configData_;
103 }
104 
GetPluginName(std::string & pluginName)105 bool PluginModule::GetPluginName(std::string& pluginName)
106 {
107     if (handle_ != nullptr) {
108         if (structPtr_ == nullptr) {
109             return false;
110         }
111         pluginName.assign(structPtr_->name);
112         return true;
113     }
114     return false;
115 }
116 
GetBufferSizeHint(uint32_t & bufferSizeHint)117 bool PluginModule::GetBufferSizeHint(uint32_t& bufferSizeHint)
118 {
119     if (handle_ != nullptr) {
120         if (structPtr_ == nullptr) {
121             return false;
122         }
123         bufferSizeHint = structPtr_->resultBufferSizeHint;
124         return true;
125     }
126     return false;
127 }
128 
IsLoaded()129 bool PluginModule::IsLoaded()
130 {
131     return (handle_ != nullptr);
132 }
133 
IsRunning()134 bool PluginModule::IsRunning()
135 {
136     return running_;
137 }
138 
BindFunctions()139 bool PluginModule::BindFunctions()
140 {
141     if (handle_ == nullptr) {
142         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
143         return false;
144     }
145     if (structPtr_ == nullptr) {
146         structPtr_ = static_cast<PluginModuleStruct*>(dlsym(handle_, "g_pluginModule"));
147         if (structPtr_ == nullptr) {
148             HILOG_DEBUG(LOG_CORE, "%s:structPtr_ == nullptr", __func__);
149             return false;
150         }
151     }
152 
153     if (structPtr_->callbacks == nullptr) {
154         HILOG_DEBUG(LOG_CORE, "%s:structPtr_->callbacks == nullptr", __func__);
155         return false;
156     }
157 
158     if ((structPtr_->callbacks->onPluginSessionStart == nullptr) ||
159         (structPtr_->callbacks->onPluginSessionStop == nullptr)) {
160         HILOG_DEBUG(LOG_CORE, "%s:onPluginSessionStart == nullptr", __func__);
161         return false;
162     }
163 
164     return true;
165 }
166 
StartSession(const uint8_t * buffer,uint32_t size)167 bool PluginModule::StartSession(const uint8_t* buffer, uint32_t size)
168 {
169     HILOG_DEBUG(LOG_CORE, "StartSession");
170     if (handle_ == nullptr) {
171         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
172         return false;
173     }
174 
175     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
176         if (structPtr_->callbacks->onPluginSessionStart) {
177             running_ = true;
178             return (structPtr_->callbacks->onPluginSessionStart(buffer, size) == 0);
179         }
180     }
181     return false;
182 }
183 
StopSession()184 bool PluginModule::StopSession()
185 {
186     HILOG_INFO(LOG_CORE, "%s:stop Session ready!", __func__);
187     if (handle_ == nullptr) {
188         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
189         return false;
190     }
191     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
192         if (structPtr_->callbacks->onPluginSessionStop != nullptr) {
193             running_ = false;
194             return (structPtr_->callbacks->onPluginSessionStop() == 0);
195         }
196     }
197     return false;
198 }
199 
ReportResult(uint8_t * buffer,uint32_t size)200 int32_t PluginModule::ReportResult(uint8_t* buffer, uint32_t size)
201 {
202     if (handle_ == nullptr) {
203         HILOG_DEBUG(LOG_CORE, "%s:plugin not open", __func__);
204         return -1;
205     }
206     if (first_) {
207         lastTime_ = std::chrono::steady_clock::now();
208         first_ = false;
209     } else {
210         std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
211         lastTime_ = t1;
212     }
213 
214     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
215         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
216             return structPtr_->callbacks->onPluginReportResult(buffer, size);
217         }
218     }
219 
220     return -1;
221 }
222 
RegisterWriter(const BufferWriterPtr writer)223 bool PluginModule::RegisterWriter(const BufferWriterPtr writer)
224 {
225     writerAdapter_ = std::make_shared<WriterAdapter>();
226     writerAdapter_->SetWriter(writer);
227 
228     if (writer == nullptr) {
229         HILOG_INFO(LOG_CORE, "%s:bufferWriter is null, update WriterAdapter only!", __func__);
230         return true;
231     }
232     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
233         if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
234             return structPtr_->callbacks->onRegisterWriterStruct(writerAdapter_->GetStruct());
235         }
236     }
237     return true;
238 }
239 
GetWriter()240 WriterPtr PluginModule::GetWriter()
241 {
242     if (writerAdapter_ == nullptr) {
243         HILOG_DEBUG(LOG_CORE, "%s:pluginModule nullptr", __func__);
244         return nullptr;
245     }
246     return writerAdapter_->GetWriter();
247 }
248