XFusion API v1.3.0
载入中...
搜索中...
未找到
xf_hal_posix.c
浏览该文件的文档.
1
12/* ==================== [Includes] ========================================== */
13
15
16#if XF_HAL_POSIX_IS_ENABLE
17
18#include "xf_hal_posix.h"
19#include "xf_hal_dev.h"
20#include <stdarg.h>
21
22/* ==================== [Defines] =========================================== */
23
24#define DEV_STR_NUM (sizeof(dev_str) / sizeof(const char *))
25#define TAG "hal_posix"
26
27/* ==================== [Typedefs] ========================================== */
28
33
34/* ==================== [Static Prototypes] ================================= */
35
36static const char *is_prefix(const char *substr, const char *str);
37static bool _atoi(const char *src, uint16_t *num);
38static bool name_to_type_and_id(const char *pathname, uint16_t *type, uint16_t *id);
39
40/* ==================== [Static Variables] ================================== */
41
42static const char *dev_str[] = {
43#define XF_HAL_TABLE_STR
44#include "../device/xf_hal_reg_table.inc"
45};
46
47/* ==================== [Macros] ============================================ */
48
49#define FD_TO_ID(fd) ((fd) & 0xffff)
50#define FD_TO_TYPE(fd) (((fd) >> 16) & 0xffff)
51#define TYPE_ID_TO_FD(type, id) (((type) << 16) | (id))
52
53/* ==================== [Global Functions] ================================== */
54
55int open(const char *pathname, int flags)
56{
57 uint16_t type;
58 uint16_t id;
59 xf_hal_flag_t hal_flag;
60 int fp;
61
62 if (!name_to_type_and_id(pathname, &type, &id)) {
63 XF_LOGE(TAG, "pathname not paser to type and id");
64 XF_LOGE(TAG, "pathname: %s", pathname);
65 XF_LOGE(TAG, "type: %d", type);
66 XF_LOGE(TAG, "type: %d", id);
67 return -1;
68 }
69
70 xf_hal_dev_t *dev = xf_hal_driver_create(type, id);
71 if (dev == NULL) {
72 XF_LOGE(TAG, "open failed!");
73 XF_LOGE(TAG, "type: %d", type);
74 XF_LOGE(TAG, "type: %d", id);
75 return -1;
76 }
77
78 fp = TYPE_ID_TO_FD(type, id);
79
80 hal_flag = xf_hal_driver_get_flag(type);
81
82 if (BITS_CHECK(hal_flag, XF_HAL_FLAG_ONLY_READ) &&
84 flags == O_RDONLY) {
85 return fp;
86 }
87
88 if (!BITS_CHECK(hal_flag, XF_HAL_FLAG_ONLY_READ) &&
90 flags == O_WRONLY) {
91 return fp;
92 }
93
94 if (BITS_CHECK(hal_flag, XF_HAL_FLAG_ONLY_READ) &&
96 flags == O_RDWR) {
97
98 return fp;
99 }
100
101 XF_LOGE(TAG, "flags error!");
102 XF_LOGE(TAG, "flags:%d", flags);
103 return -1;
104}
105
106int ioctl(int fd, unsigned long request, ...)
107{
108 int ret = 0;
109 va_list args;
110 void *arg_in = NULL;
111 uint16_t type = FD_TO_ID(fd);
112 uint16_t id = FD_TO_TYPE(fd);
113 xf_hal_dev_t *dev = xf_hal_device_find(type, id);
114
115 if (dev == NULL) {
116 XF_LOGE(TAG, "not find this device");
117 XF_LOGE(TAG, "type;%d", type);
118 XF_LOGE(TAG, "id;%d", id);
119 }
120
121 va_start(args, request);
122 arg_in = va_arg(args, void *);
123 va_end(args);
124
125 ret = xf_hal_driver_ioctl(dev, request, arg_in);
126 if (ret) {
127 XF_LOGE(TAG, "ioctl failed!");
128 }
129
130 return ret;
131}
132
133size_t write(int fd, const void *buf, size_t count)
134{
135 int ret = 0;
136 uint16_t type = FD_TO_ID(fd);
137 uint16_t id = FD_TO_TYPE(fd);
138 xf_hal_dev_t *dev = xf_hal_device_find(type, id);
139
140 if (dev == NULL) {
141 XF_LOGE(TAG, "not find this device");
142 XF_LOGE(TAG, "type;%d", type);
143 XF_LOGE(TAG, "id;%d", id);
144 }
145
146 ret = xf_hal_driver_write(dev, buf, count);
147 if (ret < 0) {
148 XF_LOGE(TAG, "write failed!");
149 }
150
151 return ret;
152}
153
154size_t read(int fd, void *buf, size_t count)
155{
156 int ret = 0;
157 uint16_t type = FD_TO_ID(fd);
158 uint16_t id = FD_TO_TYPE(fd);
159 xf_hal_dev_t *dev = xf_hal_device_find(type, id);
160
161 if (dev == NULL) {
162 XF_LOGE(TAG, "not find this device");
163 XF_LOGE(TAG, "type;%d", type);
164 XF_LOGE(TAG, "id;%d", id);
165 }
166
167 ret = xf_hal_driver_read(dev, buf, count);
168 if (ret < 0) {
169 XF_LOGE(TAG, "read failed!");
170 }
171
172 return ret;
173}
174
175int close(int fd)
176{
177 int ret = 0;
178 uint16_t type = FD_TO_ID(fd);
179 uint16_t id = FD_TO_TYPE(fd);
180 xf_hal_dev_t *dev = xf_hal_device_find(type, id);
181
182 if (dev == NULL) {
183 XF_LOGE(TAG, "not find this device");
184 XF_LOGE(TAG, "type;%d", type);
185 XF_LOGE(TAG, "id;%d", id);
186 }
187
188 ret = xf_hal_driver_close(dev);
189 if (ret) {
190 XF_LOGE(TAG, "read failed!");
191 }
192
193 return ret;
194}
195
196/* ==================== [Static Functions] ================================== */
197
198static const char *is_prefix(const char *substr, const char *str)
199{
200 while (*substr != '\0') {
201 if (*substr != *str) {
202 return NULL;
203 }
204 substr++;
205 str++;
206 }
207 return str;
208}
209
210static bool _atoi(const char *src, uint16_t *num)
211{
212 *num = 0;
213
214 while (*src >= '0' && *src <= '9') {
215 *num = *num * 10 + (*src++ - '0');
216 }
217
218 if (*src != '\0') {
219 return false;
220 }
221
222 return true;
223}
224
225static bool name_to_type_and_id(const char *pathname, uint16_t *type, uint16_t *id)
226{
227 for (size_t i = 0; i < DEV_STR_NUM; i++) {
228 const char *other = is_prefix(dev_str[i], pathname);
229 if (other == NULL) {
230 continue;
231 }
232 _atoi(other, id);
233 *type = i;
234 return true;
235 }
236
237 return false;
238}
239
240#endif
#define BITS_CHECK(src, bits_mask)
检查变量 var 在 bits_mask 的位置上是否存在 1。
uint16_t id
uint16_t type
xf_err_t xf_hal_driver_close(xf_hal_dev_t *dev)
Definition xf_hal_dev.c:192
int xf_hal_driver_read(xf_hal_dev_t *dev, void *buf, size_t count)
Definition xf_hal_dev.c:168
xf_err_t xf_hal_driver_ioctl(xf_hal_dev_t *dev, uint32_t cmd, void *config)
Definition xf_hal_dev.c:154
int xf_hal_driver_write(xf_hal_dev_t *dev, const void *buf, size_t count)
Definition xf_hal_dev.c:180
uint32_t xf_hal_driver_get_flag(xf_hal_type_t type)
Definition xf_hal_dev.c:67
xf_hal_dev_t * xf_hal_device_find(xf_hal_type_t type, uint32_t id)
Definition xf_hal_dev.c:238
xf_hal_dev_t * xf_hal_driver_create(xf_hal_type_t type, uint32_t id)
Definition xf_hal_dev.c:93
xf_hal 设备抽象。
enum _xf_hal_flag_t xf_hal_flag_t
@ XF_HAL_FLAG_ONLY_WRITE
Definition xf_hal_dev.h:53
@ XF_HAL_FLAG_ONLY_READ
Definition xf_hal_dev.h:52
xf_hal 内核配置(仅 xf_hal kernel 内部使用)。
int ioctl(int fd, unsigned long request,...)
#define FD_TO_ID(fd)
static bool name_to_type_and_id(const char *pathname, uint16_t *type, uint16_t *id)
#define DEV_STR_NUM
static bool _atoi(const char *src, uint16_t *num)
static const char * is_prefix(const char *substr, const char *str)
#define FD_TO_TYPE(fd)
size_t read(int fd, void *buf, size_t count)
#define TYPE_ID_TO_FD(type, id)
int close(int fd)
size_t write(int fd, const void *buf, size_t count)
int open(const char *pathname, int flags)
static const char * dev_str[]
#define TAG
(暂未使用)基于 xf_hal_driver 的 posix 实现。
#define O_WRONLY
#define O_RDONLY
#define O_RDWR
#define XF_LOGE(tag, format,...)