1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-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#ifndef PLUGIN_MODULE_H
17#define PLUGIN_MODULE_H
18
19#include <chrono>
20#include <memory>
21#include <string>
22
23#include "buffer_writer.h"
24#include "writer_adapter.h"
25
26struct PluginModuleInfo {
27    std::string name;
28    uint32_t bufferSizeHint = 0;
29    bool isStandaloneFileData = false;
30    std::string outFileName = "";
31    std::string pluginVersion = "";
32};
33
34struct PluginModuleStruct;
35struct WriterStruct;
36
37using BufferWriterPtr = STD_PTR(shared, BufferWriter);
38
39class PluginModule {
40public:
41    enum class SampleMode {
42        UNKNOWN,
43        POLLING,
44        STREAMING,
45    };
46    explicit PluginModule(const std::string& path = "");
47    ~PluginModule();
48    std::string ComputeSha256();
49
50    bool Load();
51    bool Unload();
52
53    SampleMode GetSampleMode() const;
54    bool GetInfo(PluginModuleInfo& info);
55    bool GetPluginName(std::string& pluginName);
56    bool GetBufferSizeHint(uint32_t& bufferSizeHint);
57    bool GetStandaloneFileData();
58    bool GetOutFileName(std::string& outFileName);
59    bool GetPluginVersion(std::string& pluginVersion);
60    std::string GetPath();
61    std::string GetPluginName();
62    bool IsRunning();
63    bool IsLoaded();
64    bool BindFunctions();
65
66    bool StartSession(const uint8_t* buffer, uint32_t size);
67    bool StopSession();
68    int32_t ReportResult(uint8_t* buffer, uint32_t size);
69    PluginReportResultOptimizeCallback ReportResultOptimize();
70    bool ReportBasicData();
71
72    bool RegisterWriter(const BufferWriterPtr writer, bool isProtobufSerialize = true);
73    WriterPtr GetWriter();
74
75    void SetConfigData(const std::string& data);
76    std::string GetConfigData() const;
77
78    void SetClockId(clockid_t clockId);
79    clockid_t GetClockId() const;
80
81private:
82    void* handle_;
83    bool running_;
84    std::string path_;
85    std::string pluginName_;
86    std::string configData_;
87    clockid_t clockId_ = CLOCK_REALTIME;
88    PluginModuleStruct* structPtr_;
89    std::shared_ptr<WriterAdapter> writerAdapter_;
90    std::chrono::steady_clock::time_point lastTime_;
91    bool first_ = true;
92    bool isProtobufSerialize_ = true;
93};
94
95#endif // !PLUGIN_MODULE_H
96