1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 PANIC_REPORT_RECOVERY_H_
17 #define PANIC_REPORT_RECOVERY_H_
18 
19 #include <filesystem>
20 #include <memory>
21 #include <string>
22 
23 #include "sys_event.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace PanicReport {
28 
29 struct BboxSaveLogFlags {
30     std::string happenTime;
31     std::string factoryRecoveryTime;
32     std::string softwareVersion;
33     bool isPanicUploaded = true;
34     bool isStartUpShort = false;
35 };
36 
37 /**
38  * load bboxSaveLogFlags from file.
39  *
40  * @return bboxSaveLogFlags
41  */
42 BboxSaveLogFlags LoadBboxSaveFlagFromFile();
43 
44 /**
45  * save bboxSaveLogFlags to file.
46  *
47  * @param bboxSaveLogFlags bboxSaveLogFlags
48  * @return whether save completed.
49  */
50 bool SaveBboxLogFlagsToFile(const BboxSaveLogFlags& bboxSaveLogFlags);
51 
52 /**
53  * clearAllFiles in the path given.
54  *
55  * @param dirPath the path
56  * @return whether clear completed.
57  */
58 bool ClearFilesInDir(const std::filesystem::path& dirPath);
59 
60 /**
61  * Initialize the configuration file.
62  *
63  * @return whether init completed.
64  */
65 bool InitPanicConfigFile();
66 
67 /**
68  * Initialize the panic report.
69  *
70  * @return whether init completed.
71  */
72 bool InitPanicReport();
73 
74 /**
75  * The function to determine whether the system boot completed.
76  *
77  * @return whether whether the system starts normally.
78  */
79 bool IsBootCompleted();
80 
81 /**
82  * The function to determine whether the system is crashed within 10 minutes after boot completed.
83  *
84  * @return whether the system is crashed within 10 minutes after boot completed.
85  */
86 bool IsLastShortStartUp();
87 
88 /**
89  * The function to determine whether the sysEvent is recovery panic.
90  *
91  * @return whether the event is recovery panic event.
92  */
93 bool IsRecoveryPanicEvent(const std::shared_ptr<SysEvent>& sysEvent);
94 
95 /**
96  * The function to get current software version.
97  *
98  * @return currentVersion.
99  */
100 std::string GetCurrentVersion();
101 
102 /**
103  * The function to get the last recovery time.
104  *
105  * @return the time of last recovery.
106  */
107 std::string GetLastRecoveryTime();
108 
109 /**
110  * The function to get the absolute file path by the timeStr given.
111  *
112  * @param timeStr timeStr.
113  * @return the absolute path of bakeFile.
114  */
115 std::string GetBackupFilePath(const std::string& timeStr);
116 
117 /**
118  * The function to get a value from the content given by param name.
119  *
120  * @param content  content.
121  * @param param the param.
122  * @return the value.
123  */
124 std::string GetParamValueFromString(const std::string& content, const std::string& param);
125 
126 /**
127  * The function to get a value from the file given by param name.
128  *
129  * @param filePath the path of the file given.
130  * @param param the param.
131  * @return the value.
132  */
133 std::string GetParamValueFromFile(const std::string& filePath, const std::string& param);
134 
135 /**
136  * The function to compress log files.
137  *
138  * @param srcPath filePath.
139  * @param timeStr time in string format.
140  */
141 void CompressAndCopyLogFiles(const std::string& srcPath, const std::string& timeStr);
142 
143 /**
144  * The function to report panic system event after recovery.
145  *
146  * @param content content.
147  */
148 void ReportPanicEventAfterRecovery(const BboxSaveLogFlags& bboxSaveLogFlags);
149 
150 /**
151  * Try to report recovery panic event.
152  *
153  * @return whether the recovery panic event was reported
154  */
155 bool TryToReportRecoveryPanicEvent();
156 
157 /**
158  * Confirm the result of recovery report.
159  */
160 void ConfirmReportResult();
161 }
162 }
163 }
164 
165 #endif // PANIC_REPORT_RECOVERY_H_
166