1 /* 2 * Copyright (c) 2020 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 HOS_LITE_HIVIEW_FILE_H 17 #define HOS_LITE_HIVIEW_FILE_H 18 19 #include "hiview_util.h" 20 #include "ohos_types.h" 21 22 #ifdef __cplusplus 23 #if __cplusplus 24 extern "C" { 25 #endif 26 #endif /* End of #ifdef __cplusplus */ 27 28 #define HIVIEW_FILE_HEADER_PREFIX_LOG 0x48565701 /* HVW ASCii + 0x01 */ 29 #define HIVIEW_FILE_HEADER_PREFIX_EVENT 0x48565702 /* HVW ASCii + 0x02 */ 30 #define HIVIEW_FILE_HEADER_PREFIX_TEXT 0x48565703 /* HVW ASCii + 0x03 */ 31 #define HIVIEW_FILE_HEADER_PREFIX_MASK 0x48565700 /* HVW ASCii + 0x00 */ 32 #define HIVIEW_FILE_HEADER_MAIN_VERSION 1 /* Main version:1 */ 33 #define HIVIEW_FILE_HEADER_SUB_VERSION 10 /* Sub version:10 (lite) */ 34 #define HIVIEW_UE_EVENT_VER 991231100 /* UE event version */ 35 #define HIVIEW_FAULT_EVENT_VER 991231000 /* Fault event version */ 36 #define HIVIEW_STATIC_EVENT_VER 991231000 /* Static event version */ 37 #define HIVIEW_CONF_PRODUCT_VER_STR "1.0.0" 38 39 #ifndef FILE_PROC_DEFINED 40 #define FILE_PROC_DEFINED 41 /** 42 * Callback function prototype for file processing . 43 * 44 * @param path the path of the file to be processed. 45 * @param type the type of the file to be processed. 46 * @param event the type of event that triggered the function. 0 for file full. 47 **/ 48 typedef void (*FileProc)(const char *path, uint8 type, uint8 event); 49 #endif 50 51 typedef enum { 52 HIVIEW_LOG_TEXT_FILE = 0, 53 HIVIEW_LOG_BIN_FILE, 54 HIVIEW_DUMP_FILE, 55 HIVIEW_FAULT_EVENT_FILE, 56 HIVIEW_UE_EVENT_FILE, 57 HIVIEW_STAT_EVENT_FILE, 58 } HiviewFileType; 59 60 typedef enum { 61 HIVIEW_FILE_COPY = 0, 62 HIVIEW_FILE_RENAME 63 } FileProcMode; 64 65 typedef enum { 66 HIVIEW_FILE_FULL = 0 67 } HiviewFileEvent; 68 69 #pragma pack(1) 70 typedef struct { 71 uint32 prefix; 72 uint8 type; /* HiviewFileType */ 73 uint8 codeMainVersion; 74 uint8 codeSubVersion; 75 uint32 defineFileVersion; 76 } FileHeaderCommon; 77 78 typedef struct { 79 FileHeaderCommon common; 80 uint32 createTime; 81 uint32 wCursor; 82 uint32 rCursor; 83 uint32 size; /* Max size. Include the file header. */ 84 } HiviewFileHeader; 85 86 typedef struct { 87 HiviewFileHeader header; 88 const char *path; 89 char *outPath; 90 FileProc pFunc; 91 HiviewMutexId_t mutex; /* file lock of outPath */ 92 int32 fhandle; /* Circular file */ 93 uint32 configSize; /* Config size */ 94 } HiviewFile; 95 #pragma pack() 96 97 /** 98 * Initializes the file object. 99 * 100 * @param fp The address of hivew file object. 101 * @param type file type. 102 * @param size file size. 103 * @return TRUE if success, otherwise FALSE. 104 **/ 105 boolean InitHiviewFile(HiviewFile *fp, HiviewFileType type, uint32 size); 106 107 /** 108 * Writes the file header to file. 109 * 110 * @param fp the pointer of hiview file object. 111 * @return TRUE if success, otherwise FALSE. 112 **/ 113 boolean WriteFileHeader(HiviewFile *fp); 114 115 /** 116 * Reads the file header info. 117 * 118 * @param fp the pointer of hiview file object. 119 * @return TRUE if the file contains the correct header info, otherwise FALSE. 120 **/ 121 boolean ReadFileHeader(HiviewFile *fp); 122 123 /** 124 * Writes data to file. 125 * 126 * @param fp the pointer of hiview file object. 127 * @param data bytes to be written to the file. 128 * @param len the length of the data to be written. The length should be a multiple of a structure. 129 * @return length of bytes written. 130 * @attention Avoid calling this function too frequently, the watchdog may timeout otherwise. 131 **/ 132 int32 WriteToFile(HiviewFile *fp, const uint8 *data, uint32 len); 133 134 /** 135 * Reads data from file. 136 * 137 * @param fp the pointer of hiview file object. 138 * @param data buffer for holding reading data. 139 * @param readLen the length of the data to be read. The length should be a multiple of a structure. 140 * @return length of bytes read. 141 * @attention Avoid calling this function too frequently, the watchdog may timeout otherwise. 142 **/ 143 int32 ReadFromFile(HiviewFile *fp, uint8 *data, uint32 readLen); 144 145 /** 146 * Gets used size of file, excluding the size of file header 147 * 148 * @param fp the pointer of hiview file object. 149 * @return used size. 150 **/ 151 uint32 GetFileUsedSize(HiviewFile *fp); 152 153 /** 154 * Gets free size of file, excluding the size of file header 155 * 156 * @param fp the pointer of hiview file object. 157 * @return free size. 158 **/ 159 uint32 GetFileFreeSize(HiviewFile *fp); 160 161 /** 162 * Closes the file. 163 * 164 * @param fp the pointer of hiview file object. 165 * @return 0 if success, otherwise -1. 166 **/ 167 int32 CloseHiviewFile(HiviewFile *fp); 168 169 /** 170 * Process files according to mode. 171 * 172 * @param fp the pointer of hiview file object. 173 * @param dest target file path. 174 * @param mode file processing mode. 175 * @return 0 if success, otherwise -1. 176 **/ 177 int8 ProcFile(HiviewFile *fp, const char *dest, FileProcMode mode); 178 179 /** 180 * Register a monitoring function when file is full . 181 * 182 * @param fp the pointer of file object. 183 * @param func callback function. 184 * @param dest target file path. 185 **/ 186 void RegisterFileWatcher(HiviewFile *fp, FileProc func, const char *path); 187 188 /** 189 * Unregister a monitoring function. 190 * 191 * @param fp the pointer of file object. 192 * @param func callback function. 193 * 194 **/ 195 void UnRegisterFileWatcher(HiviewFile *fp, FileProc func); 196 197 #ifdef __cplusplus 198 #if __cplusplus 199 } 200 #endif 201 #endif /* End of #ifdef __cplusplus */ 202 203 #endif /* End of #ifndef HOS_LITE_HIVIEW_FILE_H */ 204