1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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 HIDUMP_PLUGIN_H
17#define HIDUMP_PLUGIN_H
18
19#include "logging.h"
20#include "plugin_module_api.h"
21#include "hidump_plugin_config.pb.h"
22#include "hidump_plugin_result.pb.h"
23
24#include <atomic>
25#include <mutex>
26#include <thread>
27#include <vector>
28
29#include <unistd.h>
30#include <sys/wait.h>
31#include <csignal>
32
33class HidumpPlugin {
34public:
35    HidumpPlugin();
36    ~HidumpPlugin();
37    int Start(const uint8_t* configData, uint32_t configSize);
38    int Stop();
39
40    int SetWriter(WriterStruct* writer);
41    void Loop(void);
42
43private:
44    // for ut
45    void SetConfig(HidumpConfig& config);
46    int SetTestCmd(const char *test_cmd);
47    const char *GetTestCmd();
48    char *testCmd_ = nullptr;
49private:
50    template <typename T>
51    bool ParseHidumpInfo(T& hidumpInfoProto, char *buf, size_t len);
52
53    HidumpConfig protoConfig_;
54    std::vector<char> buffer_;
55    WriterStruct* resultWriter_ = nullptr;
56    std::mutex mutex_;
57    std::thread writeThread_;
58    std::atomic<bool> running_ = true;
59    std::unique_ptr<FILE, std::function<int (FILE*)>> fp_;
60    volatile pid_t childPid_ = -1;
61    int pipeFds_[2] = {-1, -1}; // 2: pipe fds
62};
63
64#endif // HIDUMP_PLUGIN_H