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
23namespace {
24constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
25std::unique_ptr<XpowerPlugin> g_plugin = nullptr;
26std::mutex g_taskMutex;
27} // namespace
28
29static 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
38static 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
47static 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
55static 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
63static PluginModuleCallbacks g_callbacks = {
64    .onPluginSessionStart = XpowerPluginSessionStart,
65    .onPluginReportResult = XpowerPluginReportResult,
66    .onPluginSessionStop = XpowerPluginSessionStop,
67    .onRegisterWriterStruct = (RegisterWriterStructCallback)XpowerRegisterWriterStruct,
68};
69
70EXPORT_API PluginModuleStruct g_pluginModule = {
71    .callbacks = &g_callbacks,
72    .name = "xpower-plugin",
73    .version = "1.02",
74    .resultBufferSizeHint = MAX_BUFFER_SIZE,
75};
76