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 g_plugin->Stop();
51 return 0;
52 }
53
XpowerRegisterWriterStruct(WriterStruct * writer)54 static int XpowerRegisterWriterStruct(WriterStruct* writer)
55 {
56 std::lock_guard<std::mutex> guard(g_taskMutex);
57 g_plugin = std::make_unique<XpowerPlugin>();
58 g_plugin->SetWriter(writer);
59 return 0;
60 }
61
62 static PluginModuleCallbacks g_callbacks = {
63 .onPluginSessionStart = XpowerPluginSessionStart,
64 .onPluginReportResult = XpowerPluginReportResult,
65 .onPluginSessionStop = XpowerPluginSessionStop,
66 .onRegisterWriterStruct = (RegisterWriterStructCallback)XpowerRegisterWriterStruct,
67 };
68
69 EXPORT_API PluginModuleStruct g_pluginModule = {
70 .callbacks = &g_callbacks,
71 .name = "xpower-plugin",
72 .version = "1.02",
73 .resultBufferSizeHint = MAX_BUFFER_SIZE,
74 };
75