1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-11-20     zhujiale     the first version
9  */
10 
11 #ifndef __RTT_BYPASS_H__
12 #define __RTT_BYPASS_H__
13 #include <rtthread.h>
14 #include <rttypes.h>
15 #include <rtdevice.h>
16 typedef rt_err_t(*bypass_function_t)(struct rt_serial_device* serial, char buf, void* data);
17 
18 #define RT_BYPASS_LEVEL_MAX 4
19 #define RT_BYPASS_LEVEL_1 0
20 #define RT_BYPASS_LEVEL_2 1
21 #define RT_BYPASS_LEVEL_3 2
22 #define RT_BYPASS_LEVEL_4 3
23 #define RT_BYPASS_MAX_LEVEL 4
24 
25 /*The protect level can be register but can not be unregister we should use it carefully*/
26 #define RT_BYPASS_PROTECT_LEVEL_1 10
27 #define RT_BYPASS_PROTECT_LEVEL_2 11
28 #define RT_BYPASS_PROTECT_LEVEL_3 12
29 #define RT_BYPASS_PROTECT_LEVEL_4 13
30 
31 struct rt_serial_bypass_func {
32     /*The function pointer of the bypassed data processing*/
33     bypass_function_t bypass;
34     /*The smaller the array of levels, the higher the priority of execution*/
35     rt_uint8_t level;
36     rt_list_t node;
37     char name[RT_NAME_MAX];
38     void* data;
39 };
40 
41 struct rt_serial_bypass_head
42 {
43     rt_list_t head;
44     struct rt_spinlock spinlock;
45 };
46 
47 struct rt_serial_bypass {
48     struct rt_work work;
49 
50     struct rt_spinlock spinlock;
51     struct rt_workqueue* lower_workq;
52     struct rt_serial_bypass_head* upper_h;
53     struct rt_serial_bypass_head* lower_h;
54     rt_mutex_t  mutex;
55     struct rt_ringbuffer* pipe;
56 };
57 
58 int serial_bypass_list(int argc, char** argv);
59 
60 void rt_bypass_work_straight(struct rt_serial_device* serial);
61 void rt_bypass_putchar(struct rt_serial_device* serial, rt_uint8_t ch);
62 rt_size_t rt_bypass_getchar(struct rt_serial_device* serial, rt_uint8_t* ch);
63 
64 rt_err_t rt_bypass_upper_unregister(struct rt_serial_device* serial, rt_uint8_t level);
65 rt_err_t rt_bypass_lower_unregister(struct rt_serial_device* serial, rt_uint8_t level);
66 
67 rt_err_t rt_bypass_upper_register(struct rt_serial_device* serial, const char* name, rt_uint8_t level, bypass_function_t func, void* data);
68 rt_err_t rt_bypass_lower_register(struct rt_serial_device* serial, const char* name, rt_uint8_t level, bypass_function_t func, void* data);
69 #endif
70