1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef __OAL_FILE_H__
20 #define __OAL_FILE_H__
21
22 /* ****************************************************************************
23 1 其他头文件包含
24 **************************************************************************** */
25 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
26 #include <linux/fs.h>
27 #include <linux/uaccess.h>
28 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
29 #include <fs/driver.h>
30 #include <fcntl.h>
31 #include <hi_types.h>
32 #endif
33 #include "oal_mm.h"
34
35 #ifdef __cplusplus
36 #if __cplusplus
37 extern "C" {
38 #endif
39 #endif
40
41 /* ****************************************************************************
42 2 宏定义
43 **************************************************************************** */
44 /* 文件属性 */
45 #define OAL_O_ACCMODE O_ACCMODE
46 #define OAL_O_RDONLY O_RDONLY
47 #define OAL_O_WRONLY O_WRONLY
48 #define OAL_O_RDWR O_RDWR
49 #define OAL_O_CREAT O_CREAT
50 #define OAL_O_TRUNC O_TRUNC
51 #define OAL_O_APPEND O_APPEND
52
53 #define OAL_SEEK_SET SEEK_SET /* Seek from beginning of file. */
54 #define OAL_SEEK_CUR SEEK_CUR /* Seek from current position. */
55 #define OAL_SEEK_END SEEK_END /* Set file pointer to EOF plus "offset" */
56
57 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
58 #define OAL_FILE_POS(pst_file) (pst_file->fp->f_pos)
59 #define OAL_FILE_FAIL HI_NULL
60 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
61 #define OAL_FILE_POS(pst_file) oal_get_file_pos(pst_file)
62 #endif
63
64 /* ****************************************************************************
65 3 枚举定义
66 **************************************************************************** */
67 /* ****************************************************************************
68 4 全局变量声明
69 **************************************************************************** */
70 /* ****************************************************************************
71 5 消息头定义
72 **************************************************************************** */
73 /* ****************************************************************************
74 6 消息定义
75 **************************************************************************** */
76 /* ****************************************************************************
77 7 STRUCT定义
78 **************************************************************************** */
79 typedef struct _oal_file_stru_ {
80 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
81 struct file *fp;
82 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
83 int fd;
84 #endif
85 } oal_file_stru;
86
87 /* ****************************************************************************
88 8 UNION定义
89 **************************************************************************** */
90 /* ****************************************************************************
91 9 OTHERS定义
92 **************************************************************************** */
93 /* ****************************************************************************
94 10 函数声明
95 **************************************************************************** */
96 /* ****************************************************************************
97 功能描述 : 打开文件
98 输入参数 : pc_path: 文件路径,flags:打开方式,rights:打开权限
99 输出参数 : 无
100 返 回 值 : 文件句柄
101 **************************************************************************** */
oal_file_open(const hi_char * pc_path,hi_s32 flags,hi_s32 rights)102 static inline oal_file_stru *oal_file_open(const hi_char *pc_path, hi_s32 flags, hi_s32 rights)
103 {
104 oal_file_stru *pst_file = NULL;
105 pst_file = oal_kzalloc(sizeof(oal_file_stru), OAL_GFP_KERNEL);
106 if (pst_file == NULL) {
107 return HI_NULL;
108 }
109
110 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
111 pst_file->fp = filp_open(pc_path, flags, rights);
112 if (IS_ERR_OR_NULL(pst_file->fp)) {
113 oal_free(pst_file);
114 return HI_NULL;
115 }
116 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
117 pst_file->fd = open(pc_path, flags, rights);
118 if (pst_file->fd < 0) {
119 oal_free(pst_file);
120 return HI_NULL;
121 }
122 #endif
123
124 return pst_file;
125 }
126
127 /* ****************************************************************************
128 功能描述 : 写文件
129 输入参数 : file: 文件句柄
130 : pc_string: 输入内容地址
131 : ul_length: 输入内容长度
132 输出参数 : 无
133 返 回 值 : 文件句柄
134 **************************************************************************** */
oal_file_write(const oal_file_stru * pst_file,const hi_char * pc_string,hi_u32 ul_length)135 static inline hi_u32 oal_file_write(const oal_file_stru *pst_file, const hi_char *pc_string, hi_u32 ul_length)
136 {
137 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
138 hi_u32 ul_ret;
139 mm_segment_t fs;
140 fs = get_fs();
141 set_fs(KERNEL_DS);
142 ul_ret = vfs_write(pst_file->fp, pc_string, ul_length, &(pst_file->fp->f_pos));
143 set_fs(fs);
144 return ul_ret;
145 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
146 return (hi_u32)write(pst_file->fd, pc_string, ul_length);
147 #endif
148 }
149
150 /* ****************************************************************************
151 功能描述 : 关闭文件
152 输入参数 : pc_path: 文件路径
153 输出参数 : 无
154 返 回 值 : 文件句柄
155 **************************************************************************** */
oal_file_close(oal_file_stru * pst_file)156 static inline hi_void oal_file_close(oal_file_stru *pst_file)
157 {
158 if (pst_file != HI_NULL) {
159 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
160 filp_close(pst_file->fp, NULL);
161 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
162 close(pst_file->fd);
163 #endif
164 oal_free(pst_file);
165 pst_file = NULL;
166 }
167 }
168
169 /* ****************************************************************************
170 功能描述 : 内核读文件,从头开始读
171 输入参数 : file:指向要读取的文件的指针
172 puc_buf:从文件读出数据后存放的buf
173 ul_count:指定要读取的长度
174 输出参数 : 无
175 返 回 值 :
176 **************************************************************************** */
oal_file_read(const oal_file_stru * pst_file,hi_u8 * pc_buf,hi_u32 ul_count)177 static inline hi_s32 oal_file_read(const oal_file_stru *pst_file, hi_u8 *pc_buf, hi_u32 ul_count)
178 {
179 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
180 return kernel_read(pst_file->fp, pc_buf, ul_count, &(pst_file->fp->f_pos));
181 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
182 return read(pst_file->fd, pc_buf, ul_count);
183 #endif
184 }
185
oal_file_lseek(const oal_file_stru * pst_file,hi_s64 offset,hi_s32 whence)186 static inline hi_s64 oal_file_lseek(const oal_file_stru *pst_file, hi_s64 offset, hi_s32 whence)
187 {
188 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
189 return vfs_llseek(pst_file->fp, offset, whence);
190 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
191 return lseek(pst_file->fd, offset, whence);
192 #endif
193 }
194
195 #ifdef __cplusplus
196 #if __cplusplus
197 }
198 #endif
199 #endif
200
201 #endif /* end of oal_file.h */
202