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 } HiviewFile; 94 #pragma pack() 95 96 /** 97 * Initializes the file object. 98 * 99 * @param fp The address of hivew file object. 100 * @param type file type. 101 * @param size file size. 102 * @return TRUE if success, otherwise FALSE. 103 **/ 104 boolean InitHiviewFile(HiviewFile *fp, HiviewFileType type, uint32 size); 105 106 /** 107 * Writes the file header to file. 108 * 109 * @param fp the pointer of hiview file object. 110 * @return TRUE if success, otherwise FALSE. 111 **/ 112 boolean WriteFileHeader(HiviewFile *fp); 113 114 /** 115 * Reads the file header info. 116 * 117 * @param fp the pointer of hiview file object. 118 * @return TRUE if the file contains the correct header info, otherwise FALSE. 119 **/ 120 boolean ReadFileHeader(HiviewFile *fp); 121 122 /** 123 * Writes data to file. 124 * 125 * @param fp the pointer of hiview file object. 126 * @param data bytes to be written to the file. 127 * @param len the length of the data to be written. The length should be a multiple of a structure. 128 * @return length of bytes written. 129 * @attention Avoid calling this function too frequently, the watchdog may timeout otherwise. 130 **/ 131 int32 WriteToFile(HiviewFile *fp, const uint8 *data, uint32 len); 132 133 /** 134 * Reads data from file. 135 * 136 * @param fp the pointer of hiview file object. 137 * @param data buffer for holding reading data. 138 * @param readLen the length of the data to be read. The length should be a multiple of a structure. 139 * @return length of bytes read. 140 * @attention Avoid calling this function too frequently, the watchdog may timeout otherwise. 141 **/ 142 int32 ReadFromFile(HiviewFile *fp, uint8 *data, uint32 readLen); 143 144 /** 145 * Gets used size of file, excluding the size of file header 146 * 147 * @param fp the pointer of hiview file object. 148 * @return used size. 149 **/ 150 uint32 GetFileUsedSize(HiviewFile *fp); 151 152 /** 153 * Gets free size of file, excluding the size of file header 154 * 155 * @param fp the pointer of hiview file object. 156 * @return free size. 157 **/ 158 uint32 GetFileFreeSize(HiviewFile *fp); 159 160 /** 161 * Closes the file. 162 * 163 * @param fp the pointer of hiview file object. 164 * @return 0 if success, otherwise -1. 165 **/ 166 int32 CloseHiviewFile(HiviewFile *fp); 167 168 /** 169 * Process files according to mode. 170 * 171 * @param fp the pointer of hiview file object. 172 * @param dest target file path. 173 * @param mode file processing mode. 174 * @return 0 if success, otherwise -1. 175 **/ 176 int8 ProcFile(HiviewFile *fp, const char *dest, FileProcMode mode); 177 178 /** 179 * Register a monitoring function when file is full . 180 * 181 * @param fp the pointer of file object. 182 * @param func callback function. 183 * @param dest target file path. 184 **/ 185 void RegisterFileWatcher(HiviewFile *fp, FileProc func, const char *path); 186 187 /** 188 * Unregister a monitoring function. 189 * 190 * @param fp the pointer of file object. 191 * @param func callback function. 192 * 193 **/ 194 void UnRegisterFileWatcher(HiviewFile *fp, FileProc func); 195 196 #ifdef __cplusplus 197 #if __cplusplus 198 } 199 #endif 200 #endif /* End of #ifdef __cplusplus */ 201 202 #endif /* End of #ifndef HOS_LITE_HIVIEW_FILE_H */ 203