106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1106f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1206f6ba60Sopenharmony_ci * limitations under the License.
1306f6ba60Sopenharmony_ci */
1406f6ba60Sopenharmony_ci
1506f6ba60Sopenharmony_ci#ifndef HISYSEVENT_PLUGIN_H
1606f6ba60Sopenharmony_ci#define HISYSEVENT_PLUGIN_H
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include "hisysevent_plugin_config.pb.h"
1906f6ba60Sopenharmony_ci#include "hisysevent_plugin_result.pb.h"
2006f6ba60Sopenharmony_ci#include "common.h"
2106f6ba60Sopenharmony_ci#include "logging.h"
2206f6ba60Sopenharmony_ci#include "plugin_module_api.h"
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_ci#include <atomic>
2506f6ba60Sopenharmony_ci#include <cstdio>
2606f6ba60Sopenharmony_ci#include <mutex>
2706f6ba60Sopenharmony_ci#include <thread>
2806f6ba60Sopenharmony_ci#include <vector>
2906f6ba60Sopenharmony_ci
3006f6ba60Sopenharmony_ciclass HisyseventPlugin {
3106f6ba60Sopenharmony_cipublic:
3206f6ba60Sopenharmony_ci    HisyseventPlugin();
3306f6ba60Sopenharmony_ci    ~HisyseventPlugin();
3406f6ba60Sopenharmony_ci    int Start(const uint8_t* configData, uint32_t configSize);
3506f6ba60Sopenharmony_ci    int Stop();
3606f6ba60Sopenharmony_ci    void Run(void);
3706f6ba60Sopenharmony_ci
3806f6ba60Sopenharmony_ci    int SetWriter(WriterStruct* writer);
3906f6ba60Sopenharmony_ci
4006f6ba60Sopenharmony_ciprivate:
4106f6ba60Sopenharmony_ci    std::string GetFullCmd();
4206f6ba60Sopenharmony_ci    bool InitHisyseventCmd();
4306f6ba60Sopenharmony_ci
4406f6ba60Sopenharmony_ci    template <typename T> bool ParseSyseventLineInfo(const char* data, size_t len, T hisyseventInfoProto);
4506f6ba60Sopenharmony_ci
4606f6ba60Sopenharmony_ci    template <typename T> bool WriteResult(const T hisyseventInfoProto);
4706f6ba60Sopenharmony_ci
4806f6ba60Sopenharmony_ci    template <typename T> void FlushDataOptimize(const T hisyseventInfoProto);
4906f6ba60Sopenharmony_ci
5006f6ba60Sopenharmony_ciprivate:
5106f6ba60Sopenharmony_ci    std::atomic<uint64_t> id_;
5206f6ba60Sopenharmony_ci    std::vector<std::string> fullCmd_;
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_ci    std::thread workThread_;
5506f6ba60Sopenharmony_ci    std::atomic<bool> running_ = true;
5606f6ba60Sopenharmony_ci
5706f6ba60Sopenharmony_ci    std::unique_ptr<FILE, std::function<int (FILE*)>> fp_;
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ci    HisyseventConfig protoConfig_;
6006f6ba60Sopenharmony_ci    std::vector<char> protoBuffer_;
6106f6ba60Sopenharmony_ci    WriterStruct* resultWriter_ = nullptr;
6206f6ba60Sopenharmony_ci
6306f6ba60Sopenharmony_ci    volatile pid_t childPid_ = -1;
6406f6ba60Sopenharmony_ci    int pipeFds_[2] = {-1, -1};
6506f6ba60Sopenharmony_ci};
6606f6ba60Sopenharmony_ci#endif // !HISYSEVENT_PLUGIN_H
67