blob: 5b9738c3f82b71b9cbc5965f2dc2eb5d0633d8e9 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <string.h>
7#include <errno.h>
8#include <termios.h>
9#include <unistd.h>
10#include <stdint.h>
11#include <sys/ioctl.h>
12#include <dlfcn.h>
13
14#include "gsw_uart.h"
15
16#ifndef LOG_ERR_LEVEL
17#define LOG_ERR_LEVEL 3 /* error conditions */
18#endif
19#ifndef LOG_WARN_LEVEL
20#define LOG_WARN_LEVEL 4 /* warning conditions */
21#endif
22#ifndef LOG_INFO_LEVEL
23#define LOG_INFO_LEVEL 6 /* informational */
24#endif
25#ifndef LOG_DEBUG_LEVEL
26#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
27#endif
28#ifndef LOG_VERBOSE_LEVEL
29#define LOG_VERBOSE_LEVEL 8
30#endif
31
32#define LOGV(fmt, args ...) \
33 do{ \
34 char *file_ptr_1001 = __FILE__; \
35 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
36 char line_1001[10] = {0}; \
37 sprintf(line_1001, "%d", __LINE__); \
38 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
39 if(*ptr_1001 == '/') \
40 break; \
41 ptr_1001--; \
42 } \
43 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
44 } while(0)
45
46#define LOGI(fmt, args...) \
47 do{ \
48 char *file_ptr_1001 = __FILE__; \
49 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
50 char line_1001[10] = {0}; \
51 sprintf(line_1001, "%d", __LINE__); \
52 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
53 if(*ptr_1001 == '/') \
54 break; \
55 ptr_1001--; \
56 } \
57 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
58 } while(0)
59
60#define LOGD(fmt, args...) \
61 do{ \
62 char *file_ptr_1001 = __FILE__; \
63 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
64 char line_1001[10] = {0}; \
65 sprintf(line_1001, "%d", __LINE__); \
66 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
67 if(*ptr_1001 == '/') \
68 break; \
69 ptr_1001--; \
70 } \
71 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
72 } while(0)
73
74#define LOGW(fmt, args...) \
75 do{ \
76 char *file_ptr_1001 = __FILE__; \
77 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
78 char line_1001[10] = {0}; \
79 sprintf(line_1001, "%d", __LINE__); \
80 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
81 if(*ptr_1001 == '/') \
82 break; \
83 ptr_1001--; \
84 } \
85 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
86 } while(0)
87
88#define LOGE(fmt, args...) \
89 do{ \
90 char *file_ptr_1001 = __FILE__; \
91 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
92 char line_1001[10] = {0}; \
93 sprintf(line_1001, "%d", __LINE__); \
94 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
95 if(*ptr_1001 == '/') \
96 break; \
97 ptr_1001--; \
98 } \
99 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
100 } while(0)
101
102typedef void (*mbtk_log)(int level, const char *format,...);
103static mbtk_log fun_ptr_log = NULL;
104void *dlHandle_uart = NULL;
105char *lynqLib_uart = "/lib/libmbtk_lib.so";
106
107static int handle()
108{
109 if(dlHandle_uart == NULL || fun_ptr_log == NULL)
110 {
111 dlHandle_uart = dlopen(lynqLib_uart, RTLD_NOW);
112 fun_ptr_log = (mbtk_log)dlsym(dlHandle_uart, "mbtk_log");
113 if(fun_ptr_log == NULL || dlHandle_uart == NULL)
114 {
115 return GSW_HAL_FAIL;
116 }
117 }
118 return GSW_HAL_SUCCESS;
119}
120
121
122
123
124int32_t gsw_uart_open_ex(int8_t *port, gsw_hal_uart_baudrate baudrate, uint32_t bits, int8_t parity, uint32_t stop)
125{
126 if (handle())
127 return GSW_HAL_FAIL;
128 int fd = -1;
129 if((fd = open((const char *)port, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
130 {
131 LOGE("open %s failed - %d", port, errno);
132 return -1;
133 }
134 LOGD("Open %s success.", port);
135
136 /* set newtio */
137 struct termios newtio;
138 memset(&newtio, 0, sizeof(newtio));
139 if (tcflush(fd, TCIOFLUSH) < 0) {
140 LOGE("Could not flush uart port");
141 return -1;
142 }
143
144 newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
145 newtio.c_cc[VMIN] = 0; /* blocking read until 5 chars received */
146
147 /* c_iflag 输入模式 */
148 newtio.c_iflag &= ~(ICRNL | INLCR);
149 newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
150
151 // /* c_lflag 本地模式 */
152 newtio.c_cflag &= ~ INPCK;
153 newtio.c_cflag |= (CLOCAL | CREAD);
154
155 // /* c_lflag 本地模式 */
156 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
157 /* c_oflag 输出模式 */
158 newtio.c_oflag &= ~ OPOST;
159 newtio.c_oflag &= ~(ONLCR | OCRNL);
160
161 /* c_cflag 控制模式 */
162 newtio.c_cflag &= ~ CSIZE;
163 newtio.c_cflag |= CS8;
164 newtio.c_cflag &= ~ CSTOPB;
165 newtio.c_cflag &= ~ PARENB;
166
167
168 switch(baudrate)
169 {
170 case GSW_HAL_BAUDRATE_1200:
171 cfsetospeed(&newtio, B1200);
172 cfsetispeed(&newtio, B1200);
173 break;
174 case GSW_HAL_BAUDRATE_1800:
175 cfsetospeed(&newtio, B1800);
176 cfsetispeed(&newtio, B1800);
177 break;
178 case GSW_HAL_BAUDRATE_4800:
179 cfsetospeed(&newtio, B4800);
180 cfsetispeed(&newtio, B4800);
181 break;
182 case GSW_HAL_BAUDRATE_9600:
183 cfsetospeed(&newtio, B9600);
184 cfsetispeed(&newtio, B9600);
185 break;
186 case GSW_HAL_BAUDRATE_19200:
187 cfsetospeed(&newtio, B19200);
188 cfsetispeed(&newtio, B19200);
189 break;
190 case GSW_HAL_BAUDRATE_38400:
191 cfsetospeed(&newtio, B38400);
192 cfsetispeed(&newtio, B38400);
193 break;
194 case GSW_HAL_BAUDRATE_57600:
195 cfsetospeed(&newtio, B57600);
196 cfsetispeed(&newtio, B57600);
197 break;
198 case GSW_HAL_BAUDRATE_115200:
199 cfsetospeed(&newtio, B115200);
200 cfsetispeed(&newtio, B115200);
201 break;
202 case GSW_HAL_BAUDRATE_230400:
203 cfsetospeed(&newtio, B230400);
204 cfsetispeed(&newtio, B230400);
205 break;
206 case GSW_HAL_BAUDRATE_460800:
207 cfsetospeed(&newtio, B460800);
208 cfsetispeed(&newtio, B460800);
209 break;
210 case GSW_HAL_BAUDRATE_500000:
211 cfsetospeed(&newtio, B500000);
212 cfsetispeed(&newtio, B500000);
213 break;
214 case GSW_HAL_BAUDRATE_576000:
215 cfsetospeed(&newtio, B576000);
216 cfsetispeed(&newtio, B576000);
217 break;
218 case GSW_HAL_BAUDRATE_921600:
219 cfsetospeed(&newtio, B921600);
220 cfsetispeed(&newtio, B921600);
221 break;
222 default:
223 cfsetospeed(&newtio, B115200);
224 cfsetispeed(&newtio, B115200);
225 break;
226 }
227
228 switch(bits)
229 {
230 case CS5:
231 newtio.c_cflag &= ~CSIZE;
232 newtio.c_cflag |= CS5;
233 break;
234 case CS6:
235 newtio.c_cflag &= ~CSIZE;
236 newtio.c_cflag |= CS6;
237 break;
238 case CS7:
239 newtio.c_cflag &= ~CSIZE;
240 newtio.c_cflag |= CS7;
241 break;
242 case CS8:
243 newtio.c_cflag &= ~CSIZE;
244 newtio.c_cflag |= CS8;
245 break;
246 default:
247 LOGD("No set databit.");
248 break;
249 }
250
251 if(stop == 2) {
252 newtio.c_cflag |= CSTOPB;
253 } else {
254 newtio.c_cflag &= ~CSTOPB;
255 }
256
257 switch (parity)
258 {
259 case '1':// 奇校验
260 newtio.c_cflag |= PARENB;
261 newtio.c_cflag |= PARODD;
262 break;
263 case '2':// 偶校验
264 newtio.c_cflag |= PARENB;
265 newtio.c_cflag &= ~PARODD;
266 break;
267 case '0':// 无奇偶校验
268 newtio.c_cflag &= ~PARENB;
269 break;
270 default:
271 LOGD("No set parity.");
272 break;
273 }
274
275 if (tcsetattr(fd, TCSANOW, &newtio) < 0) {
276 LOGE("Can't set port setting");
277 return -1;
278 }
279
280 return fd;
281}
282
283int32_t gsw_uart_flush(int32_t fd)
284{
285 if (tcflush(fd, TCIOFLUSH) < 0)
286 {
287 return GSW_HAL_FAIL;
288 }
289 return GSW_HAL_SUCCESS;
290}
291
292
293int32_t gsw_uart_read(int32_t fd, uint8_t *buffer, uint32_t len, int32_t timeout_ms)
294{
295 if (handle())
296 return GSW_HAL_FAIL;
297 int flags = fcntl(fd, F_GETFL); // 获取当前状态标志
298 if (flags == -1)
299 {
300 perror("fcntl get");
301 return GSW_HAL_FAIL;
302 }
303
304 struct termios newtio;
305 if (tcgetattr(fd, &newtio) < 0) {
306 perror("tcgetattr failed");
307 return -1;
308 }
309 if (timeout_ms == 0)
310 {
311 flags |= O_NONBLOCK; // 设置非阻塞标志
312 fcntl(fd, F_SETFL, flags);
313 newtio.c_cc[VMIN] = 0;
314 newtio.c_cc[VTIME] = 0;
315 }
316 else if (timeout_ms == 1)
317 {
318 flags &= ~O_NONBLOCK; // 清除非阻塞标志
319 fcntl(fd, F_SETFL, flags);
320 newtio.c_cc[VMIN] = 1;
321 newtio.c_cc[VTIME] = timeout_ms/100;
322 }
323 else
324 {
325 flags &= ~O_NONBLOCK; // 清除非阻塞标志
326 fcntl(fd, F_SETFL, flags);
327 newtio.c_cc[VMIN] = 0;
328 newtio.c_cc[VTIME] = timeout_ms/100;
329 }
330 if(timeout_ms != 0)
331 LOGI("%s :VMIN = %d ;VTIME = %d\n",__func__,newtio.c_cc[VMIN],newtio.c_cc[VTIME]);
332 if (tcsetattr(fd, TCSANOW, &newtio) != 0)
333 {
334 perror("tcsetattr");
335 return -1;
336 }
337 //gsw_uart_flush(fd);
338 int data_len = read(fd, buffer, len);
339 if (data_len < 0)
340 {
341 if(errno == EAGAIN || errno == EWOULDBLOCK)
342 return 0;
343 else
344 return GSW_HAL_FAIL;
345 }
346 return data_len;
347}
348
349
350int32_t gsw_uart_write(int32_t fd, const uint8_t *buffer, uint32_t len)
351{
352 //gsw_uart_flush(fd);
353 if (write(fd, buffer, len) < 0)
354 {
355 return GSW_HAL_FAIL;
356 }
357 return GSW_HAL_SUCCESS;
358}
359
360int32_t gsw_uart_ioctl(int32_t fd, uint32_t cmd, void *pvalue)
361{
362 if (handle())
363 return GSW_HAL_FAIL;
364 if (fd < 0 || pvalue == NULL)
365 {
366 LOGE("Invalid file descriptor");
367 return GSW_HAL_FAIL;
368 }
369 if (ioctl(fd, cmd, pvalue) < 0)
370 {
371 LOGE("Could not set DCB,error:%d, %s\n",errno, strerror(errno));
372 return GSW_HAL_FAIL;
373 }
374 return GSW_HAL_SUCCESS;
375}
376
377
378
379
380int32_t gsw_uart_close(int32_t fd)
381{
382 if (fd <= 0)
383 return GSW_HAL_FAIL;
384 //gsw_uart_flush(fd);
385 close(fd);
386 return GSW_HAL_SUCCESS;
387}
388