feat: capability list to enable greater flexibility of devices
[lunaix-os.git] / lunaix-os / includes / hal / term.h
1 #ifndef __LUNAIX_TERM_H
2 #define __LUNAIX_TERM_H
3
4 #include <lunaix/device.h>
5 #include <lunaix/ds/rbuffer.h>
6 #include <lunaix/signal_defs.h>
7
8 #include <usr/lunaix/term.h>
9
10 struct term;
11
12 struct linebuffer
13 {
14     struct rbuffer* next;
15     struct rbuffer* current;
16     short sflags;
17     short sz_hlf;
18 };
19 #define LSTATE_EOL (1)
20 #define LSTATE_EOF (1 << 1)
21 #define LSTATE_SIGRAISE (1 << 2)
22
23 typedef struct rbuffer** lbuf_ref_t;
24 #define ref_current(lbuf) (&(lbuf)->current)
25 #define ref_next(lbuf) (&(lbuf)->next)
26 #define deref(bref) (*(bref))
27
28 struct term_lcntl
29 {
30     struct term* term;
31     int (*process_and_put)(struct term*, struct linebuffer*, char);
32 };
33
34 /**
35  * @brief Communication port capability that a device is supported natively, 
36  *          or able to emulate low level serial transmission behaviour specify 
37  *          by POSIX1-2008, section 11.
38  * 
39  */
40 #define TERMPORT_CAP 0x4d524554U
41
42 /**
43  * @brief A termios capability that a device provide interfaces which is 
44  *          compliant to POSIX1-2008
45  * 
46  */
47 #define TERMIOS_CAP 0x534f4954U
48
49 struct termport_capability
50 {
51     CAPABILITY_META;
52
53     void (*set_speed)(struct device*, speed_t);
54     void (*set_cntrl_mode)(struct device*, tcflag_t);
55 };
56
57 struct term
58 {
59     struct device* dev;
60     struct device* chdev;
61     struct term_lcntl* lcntl;
62     struct linebuffer line_out;
63     struct linebuffer line_in;
64     pid_t fggrp;
65
66     struct termport_capability* tp_cap;
67
68     /* -- POSIX.1-2008 compliant fields -- */
69     tcflag_t iflags;
70     tcflag_t oflags;
71     tcflag_t lflags;
72     tcflag_t cflags;
73     cc_t cc[_NCCS];
74     speed_t iospeed;
75 };
76
77 extern struct device* sysconsole;
78
79 struct term*
80 term_create(struct device* chardev, char* suffix);
81
82 int
83 term_bind(struct term* tdev, struct device* chdev);
84
85 int
86 term_push_lcntl(struct term* tdev, struct term_lcntl* lcntl);
87
88 int
89 term_pop_lcntl(struct term* tdev);
90
91 struct term_lcntl*
92 term_get_lcntl(u32_t lcntl_index);
93
94 static inline void
95 line_flip(struct linebuffer* lbf)
96 {
97     struct rbuffer* tmp = lbf->current;
98     lbf->current = lbf->next;
99     lbf->next = tmp;
100 }
101
102 void
103 line_alloc(struct linebuffer* lbf, size_t sz_hlf);
104
105 void
106 line_free(struct linebuffer* lbf, size_t sz_hlf);
107
108 void
109 term_sendsig(struct term* tdev, int signal);
110
111 int
112 term_flush(struct term* tdev);
113
114 int
115 term_read(struct term* tdev);
116
117 int
118 lcntl_transform_inseq(struct term* tdev);
119
120 int
121 lcntl_transform_outseq(struct term* tdev);
122
123 #endif /* __LUNAIX_TERM_H */