• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 class HidumpPlugin {
34 public:
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 
43 private:
44     // for ut
45     void SetConfig(HidumpConfig& config);
46     int SetTestCmd(const char *test_cmd);
47     const char *GetTestCmd();
48     char *testCmd_ = nullptr;
49 private:
50     static FILE* CustomPopen(const char* command, const char* type);
51     static int CustomPclose(FILE* fp);
52 
53     template <typename T>
54     bool ParseHidumpInfo(T& hidumpInfoProto, char *buf);
55 
56     HidumpConfig protoConfig_;
57     std::vector<char> buffer_;
58     WriterStruct* resultWriter_ = nullptr;
59     std::mutex mutex_;
60     std::thread writeThread_;
61     std::atomic<bool> running_ = true;
62     std::unique_ptr<FILE, int (*)(FILE*)> fp_;
63 };
64 
65 #endif // HIDUMP_PLUGIN_H