• 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);
IsDataReady()42     bool IsDataReady()
43     {
44         return dataReady_.load();
45     }
46 
47 private:
48     // for ut
49     void SetConfig(HidumpConfig& config);
50     int SetTestCmd(const char *test_cmd);
51     std::string GetCmdArgs(const HidumpConfig& protoConfig);
52     const char *GetTestCmd();
53     char *testCmd_ = nullptr;
54 private:
55     template <typename T>
56     bool ParseHidumpInfo(T& hidumpInfoProto, char *buf, size_t len);
57 
58     HidumpConfig protoConfig_;
59     std::vector<char> buffer_;
60     WriterStruct* resultWriter_ = nullptr;
61     std::mutex mutex_;
62     std::thread writeThread_;
63     std::atomic<bool> running_ = true;
64     std::atomic<bool> dataReady_ = false;
65     std::unique_ptr<FILE, std::function<int (FILE*)>> fp_;
66     volatile pid_t childPid_ = -1;
67     int pipeFds_[2] = {-1, -1}; // 2: pipe fds
68 };
69 
70 #endif // HIDUMP_PLUGIN_H