• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: offline log file saved to the storage
15  */
16 
17 #ifndef LOG_FILE_COMMON_H
18 #define LOG_FILE_COMMON_H
19 
20 #include <stdint.h>
21 #include "errcode.h"
22 #include "log_file.h"
23 #include "soc_osal.h"
24 
25 #define MAX_FILE_PATH_LEN           128
26 #define MAX_SUFFIX_LEN              8
27 #define RECORD_HEAD_MAGIC           0x4B3C
28 #ifndef LOGFILE_SAVE_TASK_SIZE
29 #define LOGFILE_SAVE_TASK_SIZE      0x800
30 #endif
31 #define LOGFILE_SAVE_EVENT_MASK     0x7
32 #define LOGFILE_TIMER_PERIOD        10000
33 #define MAX_MUTIFILE_NAME_NUM       9999
34 #define MAX_MUTIFILE_NUM            100
35 #define FILE_HEAD_START_FLAG        0xaabbccdd
36 #define CLOSE_SLEEP_PERIOD          10
37 #define THREAD_PRIORITY_NUM         24
38 
39 typedef struct {
40     uint16_t magic;
41     uint16_t len;           /* 记录头 + 数据的总长度 */
42     uint8_t type;           /* 类型 */
43     uint8_t rev;            /* 是否有效 */
44 #if CONFIG_DFX_SUPPORT_FILE_SYSTEM == DFX_NO
45     uint16_t index;
46 #endif
47     uint16_t crc;           /* CRC16 校验 */
48     signed char data[0];
49 } store_record_info_t;      /* 每条记录的头 */
50 
51 typedef struct {
52     uint32_t cache_write_pos;
53     uint32_t cache_read_pos;
54     uint32_t cache_size;
55     uint32_t threshold_size; /* 门限,cache剩余空间小于门限时,触发保存 */
56     signed char data[0];     /* 存储record */
57 } store_cache_t;
58 
59 typedef struct {
60     uint32_t oldest_file_idx;   /* 多文件存储时,最早的文件序列 */
61     uint32_t cur_file_idx;      /* 多文件存储时,当前打开的文件序列 */
62     uint32_t file_count;        /* 多文件存储时,当前文件个数 */
63 } store_muti_file_idx_t;
64 
65 typedef struct {
66     store_file_head_t file_head;
67     store_file_cfg_t file_cfg;
68     store_service_t type;
69     store_muti_file_idx_t muti_file_idx;
70     int32_t fd;                     /* 当前打开的文件句柄 */
71     int32_t idx_fd;                 /* 索引文件的文件句柄 */
72     uint32_t last_save_time;        /* 此文件上次写的时间 */
73 #if CONFIG_DFX_SUPPORT_FILE_SYSTEM == DFX_NO
74     uint32_t index;                 /* 当前flash中最新的标识数字 */
75     uint32_t flash_cur_pos;         /* 当前文件写入位置 */
76 #endif
77     store_cache_t *cache;
78     bool run_flag;
79     bool finish_flag;
80 } store_file_info_t;
81 
82 typedef struct {
83     osal_timer timer_handle;        /* 缓存延后写入文件的定时器 */
84     osal_task *task_handle;;        /* 负责文件写入的线程 */
85     osal_event event;               /* 线程空闲时等待事件 */
86     osal_mutex file_write_mutex;    /* 文件写入的互斥锁 */
87     store_file_info_t file_info[STORE_MAX];
88 } store_file_manage_t;
89 
90 store_file_manage_t* logfile_get_manage(void);
91 
92 bool logfile_check_record_head_valid(store_record_info_t *record_header);
93 
94 void logfile_init_file_head(store_file_info_t *file_info);
95 
96 #endif /* LOG_FILE_COMMON_H */
97