1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 <sys/types.h>
17 
18 #include <mutex>
19 
20 #include "plugin_module_api.h"
21 #include "xpower_plugin.h"
22 
23 namespace {
24 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
25 std::unique_ptr<XpowerPlugin> g_plugin = nullptr;
26 std::mutex g_taskMutex;
27 } // namespace
28 
XpowerPluginSessionStart(const uint8_t* configData, uint32_t configSize)29 static int XpowerPluginSessionStart(const uint8_t* configData, uint32_t configSize)
30 {
31     std::lock_guard<std::mutex> guard(g_taskMutex);
32     if (g_plugin == nullptr) {
33         return -1;
34     }
35     return g_plugin->Start(configData, configSize);
36 }
37 
XpowerPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)38 static int XpowerPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
39 {
40     std::lock_guard<std::mutex> guard(g_taskMutex);
41     if (g_plugin == nullptr) {
42         return -1;
43     }
44     return g_plugin->Report(bufferData, bufferSize);
45 }
46 
XpowerPluginSessionStop()47 static int XpowerPluginSessionStop()
48 {
49     std::lock_guard<std::mutex> guard(g_taskMutex);
50     CHECK_NOTNULL(g_plugin, -1, "g_plugin is nullptr");
51     g_plugin->Stop();
52     return 0;
53 }
54 
XpowerRegisterWriterStruct(WriterStruct* writer)55 static int XpowerRegisterWriterStruct(WriterStruct* writer)
56 {
57     std::lock_guard<std::mutex> guard(g_taskMutex);
58     g_plugin = std::make_unique<XpowerPlugin>();
59     g_plugin->SetWriter(writer);
60     return 0;
61 }
62 
63 static PluginModuleCallbacks g_callbacks = {
64     .onPluginSessionStart = XpowerPluginSessionStart,
65     .onPluginReportResult = XpowerPluginReportResult,
66     .onPluginSessionStop = XpowerPluginSessionStop,
67     .onRegisterWriterStruct = (RegisterWriterStructCallback)XpowerRegisterWriterStruct,
68 };
69 
70 EXPORT_API PluginModuleStruct g_pluginModule = {
71     .callbacks = &g_callbacks,
72     .name = "xpower-plugin",
73     .version = "1.02",
74     .resultBufferSizeHint = MAX_BUFFER_SIZE,
75 };
76