• 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         info.isStandaloneFileData = structPtr_->isStandaloneFileData;
79         info.outFileName.assign(structPtr_->outFileName);
80         info.pluginVersion.assign(structPtr_->version);
81         return true;
82     }
83     return false;
84 }
85 
GetSampleMode() const86 PluginModule::SampleMode PluginModule::GetSampleMode() const
87 {
88     if (structPtr_ && structPtr_->callbacks) {
89         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
90             return SampleMode::POLLING;
91         } else if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
92             return SampleMode::STREAMING;
93         }
94     }
95     return SampleMode::UNKNOWN;
96 }
97 
SetConfigData(const std::string & data)98 void PluginModule::SetConfigData(const std::string& data)
99 {
100     configData_ = data;
101 }
102 
GetConfigData() const103 std::string PluginModule::GetConfigData() const
104 {
105     return configData_;
106 }
107 
GetPluginName(std::string & pluginName)108 bool PluginModule::GetPluginName(std::string& pluginName)
109 {
110     if (handle_ != nullptr) {
111         if (structPtr_ == nullptr) {
112             return false;
113         }
114         pluginName.assign(structPtr_->name);
115         return true;
116     }
117     return false;
118 }
119 
GetBufferSizeHint(uint32_t & bufferSizeHint)120 bool PluginModule::GetBufferSizeHint(uint32_t& bufferSizeHint)
121 {
122     if (handle_ != nullptr) {
123         if (structPtr_ == nullptr) {
124             return false;
125         }
126         bufferSizeHint = structPtr_->resultBufferSizeHint;
127         return true;
128     }
129     return false;
130 }
131 
GetStandaloneFileData()132 bool PluginModule::GetStandaloneFileData()
133 {
134     if (handle_ != nullptr) {
135         if (structPtr_ == nullptr) {
136             return false;
137         }
138         return structPtr_->isStandaloneFileData;
139     }
140     return false;
141 }
142 
GetOutFileName(std::string & outFileName)143 bool PluginModule::GetOutFileName(std::string& outFileName)
144 {
145     if (handle_ != nullptr) {
146         if (structPtr_ == nullptr) {
147             return false;
148         }
149         outFileName.assign(structPtr_->outFileName);
150         return true;
151     }
152     return false;
153 }
154 
GetPluginVersion(std::string & pluginVersion)155 bool PluginModule::GetPluginVersion(std::string& pluginVersion)
156 {
157     if (handle_ != nullptr) {
158         if (structPtr_ == nullptr) {
159             return false;
160         }
161         pluginVersion.assign(structPtr_->version);
162         return true;
163     }
164     return false;
165 }
166 
GetPath()167 std::string PluginModule::GetPath()
168 {
169     return path_;
170 }
171 
GetPluginName()172 std::string PluginModule::GetPluginName()
173 {
174     if (pluginName_ == "") {
175         if (handle_ != nullptr) {
176             if (structPtr_ == nullptr) {
177                 return "";
178             }
179             pluginName_.assign(structPtr_->name);
180         }
181     }
182     return pluginName_;
183 }
184 
IsLoaded()185 bool PluginModule::IsLoaded()
186 {
187     return (handle_ != nullptr);
188 }
189 
IsRunning()190 bool PluginModule::IsRunning()
191 {
192     return running_;
193 }
194 
BindFunctions()195 bool PluginModule::BindFunctions()
196 {
197     if (handle_ == nullptr) {
198         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
199         return false;
200     }
201     if (structPtr_ == nullptr) {
202         structPtr_ = static_cast<PluginModuleStruct*>(dlsym(handle_, "g_pluginModule"));
203         if (structPtr_ == nullptr) {
204             HILOG_DEBUG(LOG_CORE, "%s:structPtr_ == nullptr", __func__);
205             return false;
206         }
207     }
208 
209     if (structPtr_->callbacks == nullptr) {
210         HILOG_DEBUG(LOG_CORE, "%s:structPtr_->callbacks == nullptr", __func__);
211         return false;
212     }
213 
214     if ((structPtr_->callbacks->onPluginSessionStart == nullptr) ||
215         (structPtr_->callbacks->onPluginSessionStop == nullptr)) {
216         HILOG_DEBUG(LOG_CORE, "%s:onPluginSessionStart == nullptr", __func__);
217         return false;
218     }
219 
220     return true;
221 }
222 
StartSession(const uint8_t * buffer,uint32_t size)223 bool PluginModule::StartSession(const uint8_t* buffer, uint32_t size)
224 {
225     HILOG_DEBUG(LOG_CORE, "StartSession");
226     if (handle_ == nullptr) {
227         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
228         return false;
229     }
230 
231     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
232         if (structPtr_->callbacks->onPluginSessionStart) {
233             running_ = true;
234             return (structPtr_->callbacks->onPluginSessionStart(buffer, size) == 0);
235         }
236     }
237     return false;
238 }
239 
StopSession()240 bool PluginModule::StopSession()
241 {
242     HILOG_INFO(LOG_CORE, "%s:stop Session ready!", __func__);
243     if (handle_ == nullptr) {
244         HILOG_DEBUG(LOG_CORE, "%s:plugin not load", __func__);
245         return false;
246     }
247     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
248         if (structPtr_->callbacks->onPluginSessionStop != nullptr) {
249             running_ = false;
250             return (structPtr_->callbacks->onPluginSessionStop() == 0);
251         }
252     }
253     return false;
254 }
255 
ReportBasicData()256 bool PluginModule::ReportBasicData()
257 {
258     HILOG_INFO(LOG_CORE, "%s:report basic data ready!", __func__);
259     if (handle_ == nullptr) {
260         HILOG_ERROR(LOG_CORE, "%s:plugin not load", __func__);
261         return false;
262     }
263     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
264         if (structPtr_->callbacks->onReportBasicDataCallback != nullptr) {
265             auto write = GetWriter();
266             CHECK_NOTNULL(write, false, "%s:get write falied!", __func__);
267             write->Clear(); // clear share memory block
268             return (structPtr_->callbacks->onReportBasicDataCallback() == 0);
269         }
270     }
271     return false;
272 }
273 
ReportResult(uint8_t * buffer,uint32_t size)274 int32_t PluginModule::ReportResult(uint8_t* buffer, uint32_t size)
275 {
276     if (handle_ == nullptr) {
277         HILOG_DEBUG(LOG_CORE, "%s:plugin not open", __func__);
278         return -1;
279     }
280     if (first_) {
281         lastTime_ = std::chrono::steady_clock::now();
282         first_ = false;
283     } else {
284         std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
285         lastTime_ = t1;
286     }
287 
288     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
289         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
290             return structPtr_->callbacks->onPluginReportResult(buffer, size);
291         }
292     }
293 
294     return -1;
295 }
296 
RegisterWriter(const BufferWriterPtr writer)297 bool PluginModule::RegisterWriter(const BufferWriterPtr writer)
298 {
299     writerAdapter_ = std::make_shared<WriterAdapter>();
300     writerAdapter_->SetWriter(writer);
301 
302     if (writer == nullptr) {
303         HILOG_INFO(LOG_CORE, "%s:bufferWriter is null, update WriterAdapter only!", __func__);
304         return true;
305     }
306     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
307         if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
308             return structPtr_->callbacks->onRegisterWriterStruct(writerAdapter_->GetStruct());
309         }
310     }
311     return true;
312 }
313 
GetWriter()314 WriterPtr PluginModule::GetWriter()
315 {
316     if (writerAdapter_ == nullptr) {
317         HILOG_DEBUG(LOG_CORE, "%s:pluginModule nullptr", __func__);
318         return nullptr;
319     }
320     return writerAdapter_->GetWriter();
321 }
322