1 /*
2  * Copyright (C) 2020-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef AOS_TTY_CORE_H
6 #define AOS_TTY_CORE_H
7 
8 #include <aos/device_core.h>
9 #include <aos/tty.h>
10 
11 #define AOS_TTY_F_UNIQUE_REF    ((uint32_t)1 << 0)
12 
13 #ifndef AOS_TTY_RX_BUF_SIZE
14 #define AOS_TTY_RX_BUF_SIZE     1024
15 #endif
16 #ifndef AOS_TTY_TX_BUF_SIZE
17 #define AOS_TTY_TX_BUF_SIZE     1024
18 #endif
19 
20 struct aos_tty_ops;
21 
22 typedef struct {
23     aos_dev_t dev;
24 
25     /* must be initialized before registration */
26     const struct aos_tty_ops *ops;
27     uint32_t flags;
28 
29     struct termios termios;
30     uint32_t status;
31     aos_spinlock_t lock;
32     aos_event_t event;
33     size_t rx_buf_head;
34     size_t rx_buf_tail;
35     uint8_t rx_buf[AOS_TTY_RX_BUF_SIZE];
36     size_t tx_buf_head;
37     size_t tx_buf_tail;
38     uint8_t tx_buf[AOS_TTY_TX_BUF_SIZE];
39 } aos_tty_t;
40 
41 typedef struct aos_tty_ops {
42     void (*unregister)(aos_tty_t *);
43     aos_status_t (*startup)(aos_tty_t *);
44     void (*shutdown)(aos_tty_t *);
45     aos_status_t (*set_attr)(aos_tty_t *);
46     void (*enable_rx)(aos_tty_t *);
47     void (*disable_rx)(aos_tty_t *);
48     void (*start_tx)(aos_tty_t *);
49     void (*stop_tx)(aos_tty_t *);
50 } aos_tty_ops_t;
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 aos_status_t aos_tty_register(aos_tty_t *tty);
57 aos_status_t aos_tty_register_argumented(aos_tty_t *tty, uint32_t id, const aos_tty_ops_t *ops, uint32_t flags);
58 aos_status_t aos_tty_unregister(uint32_t id);
59 
60 size_t aos_tty_rx_buffer_produce(aos_tty_t *tty, const void *buf, size_t count);
61 size_t aos_tty_tx_buffer_consume(aos_tty_t *tty, void *buf, size_t count);
62 
63 #ifdef __cplusplus
64 }
65 #endif
66 
67 #endif /* AOS_TTY_CORE_H */
68