1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2017-2019 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * ä¸è¨èä½æ¨©è
|
---|
7 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
8 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
9 | * å¤ã»åé
|
---|
10 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
11 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
12 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
13 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
14 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
15 | * ç¨ã§ããå½¢ã§åé
|
---|
16 | å¸ããå ´åã«ã¯ï¼åé
|
---|
17 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
18 | * è
|
---|
19 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
20 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
21 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
22 | * ç¨ã§ããªãå½¢ã§åé
|
---|
23 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
24 | * ã¨ï¼
|
---|
25 | * (a) åé
|
---|
26 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
27 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
28 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
29 | * (b) åé
|
---|
30 | å¸ã®å½¢æ
|
---|
31 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
32 | * å ±åãããã¨ï¼
|
---|
33 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
34 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
35 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
36 | 責ãããã¨ï¼
|
---|
37 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
38 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
40 | * å
|
---|
41 | 責ãããã¨ï¼
|
---|
42 | *
|
---|
43 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
44 | ã
|
---|
45 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
46 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
47 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
48 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: stdio_stub.c 388 2019-05-22 11:25:18Z coas-nagasima $
|
---|
51 | */
|
---|
52 | #include "shellif.h"
|
---|
53 | #include <stdint.h>
|
---|
54 | #include <kernel.h>
|
---|
55 | #include <t_syslog.h>
|
---|
56 | #include <t_stdlib.h>
|
---|
57 | #include <sil.h>
|
---|
58 | #include "syssvc/serial.h"
|
---|
59 | #include "syssvc/syslog.h"
|
---|
60 | #include "target_syssvc.h"
|
---|
61 | #include "target_serial.h"
|
---|
62 | #include "fdtable.h"
|
---|
63 | #include "kernel_cfg.h"
|
---|
64 | #include <string.h>
|
---|
65 | #include <errno.h>
|
---|
66 | #include "util/ntstdio.h"
|
---|
67 | #include "hal/serial_api.h"
|
---|
68 | #include "target_rename.h"
|
---|
69 | #include "target_kernel_impl.h"
|
---|
70 | #include "syssvc/siofd.h"
|
---|
71 |
|
---|
72 | #ifdef _DEBUG
|
---|
73 | static const char THIS_FILE[] = __FILE__;
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | static unsigned char stdio_xi(struct ntstdio_t *handle);
|
---|
77 | static void stdio_xo(struct ntstdio_t *handle, unsigned char c);
|
---|
78 | static void stdio_serial_irq_handler(uint32_t id, SerialIrq event);
|
---|
79 |
|
---|
80 | static int stdio_close(struct SHELL_FILE *fp);
|
---|
81 | static size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
|
---|
82 | static size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
|
---|
83 | static void stdio_delete(struct SHELL_FILE *fp);
|
---|
84 |
|
---|
85 | static unsigned char sio_xi(struct ntstdio_t *handle);
|
---|
86 | static void sio_xo(struct ntstdio_t *handle, unsigned char c);
|
---|
87 | static void sio_serial_irq_handler(uint32_t id, SerialIrq event);
|
---|
88 |
|
---|
89 | static int sio_close(struct SHELL_FILE *fp);
|
---|
90 | static size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
|
---|
91 | static size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
|
---|
92 | static off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org);
|
---|
93 | static int sio_ioctl(struct SHELL_FILE *fp, int req, void *arg);
|
---|
94 | static bool_t sio_readable(struct SHELL_FILE *fp);
|
---|
95 | static bool_t sio_writable(struct SHELL_FILE *fp);
|
---|
96 | static void sio_delete(struct SHELL_FILE *fp);
|
---|
97 |
|
---|
98 | IO_TYPE IO_TYPE_STDIN = { stdio_close, sio_read, stdio_write, sio_seek, sio_ioctl, sio_readable, sio_writable, stdio_delete };
|
---|
99 | IO_TYPE IO_TYPE_STDOUT = { stdio_close, stdio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_writable, stdio_delete };
|
---|
100 | IO_TYPE IO_TYPE_STDERR = { stdio_close, stdio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_writable, stdio_delete };
|
---|
101 | IO_TYPE IO_TYPE_SIO = { sio_close, sio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_writable, sio_delete };
|
---|
102 |
|
---|
103 | #define STDIO_UART_BUF_SZ 256
|
---|
104 |
|
---|
105 | typedef struct stdio_sio_t {
|
---|
106 | serial_t serial;
|
---|
107 | ntstdio_t ntstdio;
|
---|
108 | struct SHELL_FILE *fp;
|
---|
109 | unsigned char tx_buf[STDIO_UART_BUF_SZ];
|
---|
110 | int tx_pos_w;
|
---|
111 | int tx_pos_r;
|
---|
112 | unsigned char rx_buf[STDIO_UART_BUF_SZ];
|
---|
113 | int rx_pos_w;
|
---|
114 | int rx_pos_r;
|
---|
115 | } stdio_sio_t;
|
---|
116 |
|
---|
117 | stdio_sio_t stdio_sio;
|
---|
118 |
|
---|
119 | void sys_init(intptr_t exinf)
|
---|
120 | {
|
---|
121 | serial_t *serial = &stdio_sio.serial;
|
---|
122 | ntstdio_t *ntstdio = &stdio_sio.ntstdio;
|
---|
123 |
|
---|
124 | serial_init(serial, STDIO_UART_TX, STDIO_UART_RX);
|
---|
125 | serial_baud(serial, UART_BAUDRATE);
|
---|
126 | serial_format(serial, 8, ParityNone, 1);
|
---|
127 |
|
---|
128 | ntstdio_init(ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, stdio_xi, stdio_xo);
|
---|
129 | ntstdio->exinf = (void *)&stdio_sio;
|
---|
130 |
|
---|
131 | struct SHELL_FILE *fp;
|
---|
132 |
|
---|
133 | fp = fd_to_fp(STDIN_FILENO);
|
---|
134 | fp->handle = SIO_PORTID;
|
---|
135 | fp->exinf = (void *)ntstdio;
|
---|
136 |
|
---|
137 | fp = fd_to_fp(STDOUT_FILENO);
|
---|
138 | fp->handle = SIO_PORTID;
|
---|
139 | fp->exinf = (void *)ntstdio;
|
---|
140 |
|
---|
141 | fp = fd_to_fp(STDERR_FILENO);
|
---|
142 | fp->handle = SIO_PORTID;
|
---|
143 | fp->exinf = (void *)ntstdio;
|
---|
144 |
|
---|
145 | serial_irq_handler(&stdio_sio.serial, stdio_serial_irq_handler, 0);
|
---|
146 |
|
---|
147 | serial_irq_set(serial, RxIrq, true);
|
---|
148 | }
|
---|
149 |
|
---|
150 | ID serial_portid;
|
---|
151 |
|
---|
152 | void stdio_open(ID portid)
|
---|
153 | {
|
---|
154 | struct SHELL_FILE *fp;
|
---|
155 |
|
---|
156 | serial_portid = portid;
|
---|
157 | serial_ctl_por(portid, IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV);
|
---|
158 |
|
---|
159 | fp = fd_to_fp(STDIN_FILENO);
|
---|
160 | fp->handle = portid;
|
---|
161 |
|
---|
162 | fp = fd_to_fp(STDOUT_FILENO);
|
---|
163 | fp->handle = portid;
|
---|
164 |
|
---|
165 | fp = fd_to_fp(STDERR_FILENO);
|
---|
166 | fp->handle = portid;
|
---|
167 | }
|
---|
168 |
|
---|
169 | int stdio_close(struct SHELL_FILE *fp)
|
---|
170 | {
|
---|
171 | return -EPERM;
|
---|
172 | }
|
---|
173 |
|
---|
174 | size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
|
---|
175 | {
|
---|
176 | return -EPERM;
|
---|
177 | }
|
---|
178 |
|
---|
179 | size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
|
---|
180 | {
|
---|
181 | return -EPERM;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void stdio_delete(struct SHELL_FILE *fp)
|
---|
185 | {
|
---|
186 | }
|
---|
187 |
|
---|
188 | int sio_close(struct SHELL_FILE *fp)
|
---|
189 | {
|
---|
190 | return -EPERM;
|
---|
191 | }
|
---|
192 |
|
---|
193 | size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
|
---|
194 | {
|
---|
195 | int i = 0;
|
---|
196 | while (i < len) {
|
---|
197 | int c = ntstdio_getc((struct ntstdio_t *)fp->exinf);
|
---|
198 | data[i++] = c;
|
---|
199 | if ((c == EOF) || (c == '\n'))
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | return i;
|
---|
203 | }
|
---|
204 |
|
---|
205 | size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
|
---|
206 | {
|
---|
207 | for (int i = 0; i < len; i++) {
|
---|
208 | ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);
|
---|
209 | }
|
---|
210 | return len;
|
---|
211 | }
|
---|
212 |
|
---|
213 | off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org)
|
---|
214 | {
|
---|
215 | return -EPERM;
|
---|
216 | }
|
---|
217 |
|
---|
218 | int sio_tcgetattr(struct SHELL_FILE *fp, struct termios *termios)
|
---|
219 | {
|
---|
220 | ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
|
---|
221 |
|
---|
222 | memset(termios, 0, sizeof(*termios));
|
---|
223 |
|
---|
224 | if (ntstdio->option & NTSTDIO_OPTION_LINE_ECHO) {
|
---|
225 | termios->c_lflag |= ECHO;
|
---|
226 | }
|
---|
227 | else {
|
---|
228 | termios->c_lflag &= ~ECHO;
|
---|
229 | }
|
---|
230 | if (ntstdio->option & NTSTDIO_OPTION_CANON) {
|
---|
231 | termios->c_lflag |= ICANON;
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | termios->c_lflag &= ~ICANON;
|
---|
235 | }
|
---|
236 | if (ntstdio->option & NTSTDIO_OPTION_LF_CR) {
|
---|
237 | termios->c_iflag |= INLCR;
|
---|
238 | }
|
---|
239 | else {
|
---|
240 | termios->c_iflag &= ~INLCR;
|
---|
241 | }
|
---|
242 | if (ntstdio->option & NTSTDIO_OPTION_LF_CRLF) {
|
---|
243 | termios->c_oflag |= ONLCR;
|
---|
244 | }
|
---|
245 | else {
|
---|
246 | termios->c_oflag &= ~ONLCR;
|
---|
247 | }
|
---|
248 |
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | int sio_tcsetattr(struct SHELL_FILE *fp, int optional_actions, const struct termios *termios)
|
---|
253 | {
|
---|
254 | ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
|
---|
255 |
|
---|
256 | if (optional_actions == TCSANOW) {
|
---|
257 | if (termios->c_lflag & ECHO) {
|
---|
258 | ntstdio->option |= NTSTDIO_OPTION_LINE_ECHO;
|
---|
259 | }
|
---|
260 | else {
|
---|
261 | ntstdio->option &= ~NTSTDIO_OPTION_LINE_ECHO;
|
---|
262 | }
|
---|
263 | if (termios->c_lflag & ICANON) {
|
---|
264 | ntstdio->option |= NTSTDIO_OPTION_CANON;
|
---|
265 | }
|
---|
266 | else {
|
---|
267 | ntstdio->option &= ~NTSTDIO_OPTION_CANON;
|
---|
268 | }
|
---|
269 | if (termios->c_iflag & INLCR) {
|
---|
270 | ntstdio->option |= NTSTDIO_OPTION_LF_CR;
|
---|
271 | }
|
---|
272 | else {
|
---|
273 | ntstdio->option &= ~NTSTDIO_OPTION_LF_CR;
|
---|
274 | }
|
---|
275 | if (termios->c_oflag & ONLCR) {
|
---|
276 | ntstdio->option |= NTSTDIO_OPTION_LF_CRLF;
|
---|
277 | }
|
---|
278 | else {
|
---|
279 | ntstdio->option &= ~NTSTDIO_OPTION_LF_CRLF;
|
---|
280 | }
|
---|
281 | return 0;
|
---|
282 | }
|
---|
283 |
|
---|
284 | //shell_abort();
|
---|
285 | return 0;
|
---|
286 | }
|
---|
287 |
|
---|
288 | int sio_ioctl(struct SHELL_FILE *fp, int request, void *arg)
|
---|
289 | {
|
---|
290 | if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
|
---|
291 | return -EBADF;
|
---|
292 |
|
---|
293 | switch (request) {
|
---|
294 | case TIOCGWINSZ:
|
---|
295 | return 0;
|
---|
296 | case TCGETS:
|
---|
297 | return sio_tcgetattr(fp, (struct termios *)arg);
|
---|
298 | case TCSETS + TCSANOW:
|
---|
299 | case TCSETS + TCSADRAIN:
|
---|
300 | case TCSETS + TCSAFLUSH:
|
---|
301 | return sio_tcsetattr(fp, request - TCSETS, (const struct termios *)arg);
|
---|
302 | }
|
---|
303 |
|
---|
304 | return -EINVAL;
|
---|
305 | }
|
---|
306 |
|
---|
307 | bool_t sio_readable(struct SHELL_FILE *fp)
|
---|
308 | {
|
---|
309 | return fp->readevt_w != fp->readevt_r;
|
---|
310 | }
|
---|
311 |
|
---|
312 | bool_t sio_writable(struct SHELL_FILE *fp)
|
---|
313 | {
|
---|
314 | return fp->writable && (fp->writeevt_w == fp->writeevt_r);
|
---|
315 | }
|
---|
316 |
|
---|
317 | void sio_delete(struct SHELL_FILE *fp)
|
---|
318 | {
|
---|
319 | free((stdio_sio_t *)((struct ntstdio_t *)fp->exinf)->exinf);
|
---|
320 | ((struct ntstdio_t *)fp->exinf)->exinf = NULL;
|
---|
321 | free((struct ntstdio_t *)fp->exinf);
|
---|
322 | fp->exinf = NULL;
|
---|
323 | }
|
---|
324 |
|
---|
325 | static unsigned char ntstdio_xi(struct ntstdio_t *handle, struct SHELL_FILE *fp)
|
---|
326 | {
|
---|
327 | stdio_sio_t *uart = (stdio_sio_t *)handle->exinf;
|
---|
328 | ER ret;
|
---|
329 | unsigned char c;
|
---|
330 |
|
---|
331 | bool_t lock = sense_lock();
|
---|
332 | if (lock)
|
---|
333 | unl_cpu();
|
---|
334 |
|
---|
335 | while (uart->rx_pos_w == uart->rx_pos_r) {
|
---|
336 | FLGPTN flgptn = 0, waitptn = 0;
|
---|
337 | FD_SET(fp->fd, (fd_set *)&waitptn);
|
---|
338 |
|
---|
339 | ret = wai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn);
|
---|
340 | if (ret != E_OK) {
|
---|
341 | if (lock)
|
---|
342 | loc_cpu();
|
---|
343 | syslog(LOG_ERROR, "wai_flg => %d", ret);
|
---|
344 | return 0;
|
---|
345 | }
|
---|
346 |
|
---|
347 | ret = clr_flg(FLG_SELECT_WAIT, ~(flgptn & waitptn));
|
---|
348 | if (ret != E_OK) {
|
---|
349 | syslog(LOG_ERROR, "clr_flg => %d", ret);
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (lock)
|
---|
354 | unl_cpu();
|
---|
355 |
|
---|
356 | if (fp->readevt_w != fp->readevt_r) fp->readevt_r++;
|
---|
357 |
|
---|
358 | c = uart->rx_buf[uart->rx_pos_r++];
|
---|
359 | if (uart->rx_pos_r >= sizeof(uart->rx_buf))
|
---|
360 | uart->rx_pos_r = 0;
|
---|
361 |
|
---|
362 | return c;
|
---|
363 | }
|
---|
364 |
|
---|
365 | extern volatile int ntshell_state;
|
---|
366 | extern ID serial_portid;
|
---|
367 |
|
---|
368 | static void serial_rx_irq_handler(int fd)
|
---|
369 | {
|
---|
370 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
371 | stdio_sio_t *uart = (stdio_sio_t *)((struct ntstdio_t *)fp->exinf)->exinf;
|
---|
372 | serial_t *serial = (serial_t *)&uart->serial;
|
---|
373 |
|
---|
374 | if (serial_readable(serial)) {
|
---|
375 | unsigned char c = (unsigned char)serial_getc(serial);
|
---|
376 |
|
---|
377 | if (ntshell_state == 1) {
|
---|
378 | ER ret;
|
---|
379 | FLGPTN flgptn = 0;
|
---|
380 |
|
---|
381 | uart->rx_buf[uart->rx_pos_w++] = c;
|
---|
382 | if (uart->rx_pos_w >= sizeof(uart->rx_buf))
|
---|
383 | uart->rx_pos_w = 0;
|
---|
384 |
|
---|
385 | if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
|
---|
386 |
|
---|
387 | FD_SET(fd, (fd_set *)&flgptn);
|
---|
388 |
|
---|
389 | ret = set_flg(FLG_SELECT_WAIT, flgptn);
|
---|
390 | if (ret != E_OK) {
|
---|
391 | syslog(LOG_ERROR, "set_flg => %d", ret);
|
---|
392 | }
|
---|
393 | }
|
---|
394 | else
|
---|
395 | sio_isr_rcv(fp->handle, c);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | static void ntstdio_xo(struct ntstdio_t *handle, struct SHELL_FILE *fp, unsigned char c)
|
---|
400 | {
|
---|
401 | stdio_sio_t *uart = (stdio_sio_t *)handle->exinf;
|
---|
402 | serial_t *serial = (serial_t *)&uart->serial;
|
---|
403 | ER ret;
|
---|
404 |
|
---|
405 | // ã¿ã¹ã¯ã³ã³ããã¹ãã§ãªãå ´å
|
---|
406 | if (sense_context()) {
|
---|
407 | // éä¿¡å¯è½ã«ãªãã¾ã§å¾
|
---|
408 | ã¡
|
---|
409 | while (!serial_writable(serial));
|
---|
410 | // éä¿¡ãã
|
---|
411 | serial_putc(serial, c);
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | // éä¿¡ãããã¡ã空ã§
|
---|
416 | if (uart->tx_pos_w == uart->tx_pos_r) {
|
---|
417 | // éä¿¡å¯è½ãªã
|
---|
418 | if (serial_writable(serial)) {
|
---|
419 | // éä¿¡ãã
|
---|
420 | serial_putc(serial, c);
|
---|
421 | return;
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | // éä¿¡ãããã¡ã«è¿½å ãã
|
---|
426 | uart->tx_buf[uart->tx_pos_w++] = c;
|
---|
427 | if (uart->tx_pos_w >= sizeof(uart->tx_buf))
|
---|
428 | uart->tx_pos_w = 0;
|
---|
429 |
|
---|
430 | // éä¿¡å¯è½ã«ãªã£ããå²ãè¾¼ã¿ããããããè¨å®
|
---|
431 | serial_irq_set(serial, TxIrq, true);
|
---|
432 |
|
---|
433 | bool_t lock = sense_lock();
|
---|
434 | if (lock)
|
---|
435 | unl_cpu();
|
---|
436 |
|
---|
437 | do {
|
---|
438 | FLGPTN flgptn = 0, waitptn = 0;
|
---|
439 | FD_SET(fp->fd, (fd_set *)&waitptn);
|
---|
440 |
|
---|
441 | ret = wai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn);
|
---|
442 | if (ret != E_OK) {
|
---|
443 | syslog(LOG_ERROR, "wai_flg => %d", ret);
|
---|
444 | break;
|
---|
445 | }
|
---|
446 |
|
---|
447 | ret = clr_flg(FLG_SELECT_WAIT, ~(flgptn & waitptn));
|
---|
448 | if (ret != E_OK) {
|
---|
449 | syslog(LOG_ERROR, "clr_flg => %d", ret);
|
---|
450 | }
|
---|
451 | } while (fp->writeevt_w == fp->writeevt_r);
|
---|
452 |
|
---|
453 | fp->writeevt_r++;
|
---|
454 |
|
---|
455 | if (lock)
|
---|
456 | loc_cpu();
|
---|
457 | }
|
---|
458 |
|
---|
459 | static void serial_tx_irq_handler(int fd)
|
---|
460 | {
|
---|
461 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
462 | stdio_sio_t *uart = (stdio_sio_t *)((struct ntstdio_t *)fp->exinf)->exinf;
|
---|
463 | serial_t *serial = (serial_t *)&uart->serial;
|
---|
464 | ER ret;
|
---|
465 | unsigned char c;
|
---|
466 |
|
---|
467 | if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
|
---|
468 |
|
---|
469 | FLGPTN flgptn = 0;
|
---|
470 | FD_SET(fd, (fd_set *)&flgptn);
|
---|
471 |
|
---|
472 | ret = set_flg(FLG_SELECT_WAIT, flgptn);
|
---|
473 | if (ret != E_OK) {
|
---|
474 | syslog(LOG_ERROR, "set_flg => %d", ret);
|
---|
475 | }
|
---|
476 |
|
---|
477 | // éä¿¡ãããã¡ã空ãªã
|
---|
478 | if (uart->tx_pos_w == uart->tx_pos_r) {
|
---|
479 | // SIOãã©ã¤ãã®éä¿¡
|
---|
480 | sio_isr_snd(fp->handle);
|
---|
481 | // ãã£ã±ãéä¿¡ãããã¡ã空ãªã
|
---|
482 | if (uart->tx_pos_w == uart->tx_pos_r) {
|
---|
483 | // éä¿¡å²ãè¾¼ã¿åæ¢
|
---|
484 | serial_irq_set(serial, TxIrq, false);
|
---|
485 | return;
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | c = uart->tx_buf[uart->tx_pos_r++];
|
---|
490 | if (uart->tx_pos_r >= sizeof(uart->tx_buf))
|
---|
491 | uart->tx_pos_r = 0;
|
---|
492 |
|
---|
493 | // éä¿¡ãã
|
---|
494 | serial_putc(serial, c);
|
---|
495 | }
|
---|
496 |
|
---|
497 | void stdio_serial_irq_handler(uint32_t id, SerialIrq event)
|
---|
498 | {
|
---|
499 | switch (event) {
|
---|
500 | case RxIrq:
|
---|
501 | serial_rx_irq_handler(STDIN_FILENO);
|
---|
502 | break;
|
---|
503 | case TxIrq:
|
---|
504 | serial_tx_irq_handler(STDOUT_FILENO);
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | void sio_serial_irq_handler(uint32_t id, SerialIrq event)
|
---|
510 | {
|
---|
511 | struct SHELL_FILE *fp = fd_to_fp(id);
|
---|
512 | if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
|
---|
513 | return;
|
---|
514 |
|
---|
515 | switch (event) {
|
---|
516 | case RxIrq:
|
---|
517 | serial_rx_irq_handler(id);
|
---|
518 | break;
|
---|
519 | case TxIrq:
|
---|
520 | serial_tx_irq_handler(id);
|
---|
521 | break;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | unsigned char stdio_xi(struct ntstdio_t *handle)
|
---|
526 | {
|
---|
527 | return ntstdio_xi(handle, fd_to_fp(STDIN_FILENO));
|
---|
528 | }
|
---|
529 |
|
---|
530 | void stdio_xo(struct ntstdio_t *handle, unsigned char c)
|
---|
531 | {
|
---|
532 | ntstdio_xo(handle, fd_to_fp(STDOUT_FILENO), c);
|
---|
533 | }
|
---|
534 |
|
---|
535 | unsigned char sio_xi(struct ntstdio_t *handle)
|
---|
536 | {
|
---|
537 | return ntstdio_xi(handle, ((stdio_sio_t *)handle->exinf)->fp);
|
---|
538 | }
|
---|
539 |
|
---|
540 | void sio_xo(struct ntstdio_t *handle, unsigned char c)
|
---|
541 | {
|
---|
542 | ntstdio_xo(handle, ((stdio_sio_t *)handle->exinf)->fp, c);
|
---|
543 | }
|
---|
544 |
|
---|
545 | int siofd_open(const char *path, int flags, ...)
|
---|
546 | {
|
---|
547 | return -1;
|
---|
548 | }
|
---|
549 |
|
---|
550 | int siofd_close(int fd)
|
---|
551 | {
|
---|
552 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
553 | if ((fp == NULL) || (fp->type != &IO_TYPE_SIO)) {
|
---|
554 | errno = EBADF;
|
---|
555 | return -1;
|
---|
556 | }
|
---|
557 |
|
---|
558 | errno = sio_close(fp);
|
---|
559 | if (errno != 0)
|
---|
560 | return 1;
|
---|
561 | return 0;
|
---|
562 | }
|
---|
563 |
|
---|
564 | ssize_t siofd_read(int fd, void *data, size_t len)
|
---|
565 | {
|
---|
566 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
567 | if ((fp == NULL) || ((fp->type != &IO_TYPE_STDIN) && (fp->type != &IO_TYPE_SIO))) {
|
---|
568 | errno = EBADF;
|
---|
569 | return -1;
|
---|
570 | }
|
---|
571 | char *pos, *end;
|
---|
572 |
|
---|
573 | for (pos = (char *)data, end = &pos[len]; pos < end; pos++) {
|
---|
574 | *pos = ntstdio_xi((struct ntstdio_t *)fp->exinf, fp);
|
---|
575 | }
|
---|
576 |
|
---|
577 | return pos - (char *)data;
|
---|
578 | }
|
---|
579 |
|
---|
580 | ssize_t siofd_write(int fd, const void *data, size_t len)
|
---|
581 | {
|
---|
582 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
583 | if ((fp == NULL) || ((fp->type != &IO_TYPE_STDOUT) && (fp->type != &IO_TYPE_STDERR)
|
---|
584 | && (fp->type != &IO_TYPE_SIO))) {
|
---|
585 | errno = EBADF;
|
---|
586 | return -1;
|
---|
587 | }
|
---|
588 | const char *pos, *end;
|
---|
589 |
|
---|
590 | for (pos = (const char *)data, end = &pos[len]; pos < end; pos++) {
|
---|
591 | ntstdio_xo((struct ntstdio_t *)fp->exinf, fp, *pos);
|
---|
592 | }
|
---|
593 |
|
---|
594 | return pos - (char *)data;
|
---|
595 | }
|
---|
596 |
|
---|
597 | int siofd_tcsetattr(int fd, int act, const struct termios *tio)
|
---|
598 | {
|
---|
599 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
600 | if ((fp == NULL) || ((fp->type != &IO_TYPE_STDIN) && (fp->type != &IO_TYPE_STDOUT)
|
---|
601 | && (fp->type != &IO_TYPE_STDERR) && (fp->type != &IO_TYPE_SIO))) {
|
---|
602 | errno = EBADF;
|
---|
603 | return -1;
|
---|
604 | }
|
---|
605 |
|
---|
606 | errno = sio_tcsetattr(fp, act, tio);
|
---|
607 | if (errno != 0)
|
---|
608 | return 1;
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | int siofd_tcgetattr(int fd, struct termios *tio)
|
---|
613 | {
|
---|
614 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
615 | if ((fp == NULL) || ((fp->type != &IO_TYPE_STDIN) && (fp->type != &IO_TYPE_STDOUT)
|
---|
616 | && (fp->type != &IO_TYPE_STDERR) && (fp->type != &IO_TYPE_SIO))) {
|
---|
617 | errno = EBADF;
|
---|
618 | return -1;
|
---|
619 | }
|
---|
620 |
|
---|
621 | errno = sio_tcgetattr(fp, tio);
|
---|
622 | if (errno != 0)
|
---|
623 | return 1;
|
---|
624 | return 0;
|
---|
625 | }
|
---|
626 |
|
---|
627 | int siofd_fcntl(int fd, int cmd, ...)
|
---|
628 | {
|
---|
629 | struct SHELL_FILE *fp = fd_to_fp(fd);
|
---|
630 | if ((fp == NULL) || ((fp->type != &IO_TYPE_STDIN) && (fp->type != &IO_TYPE_STDOUT)
|
---|
631 | && (fp->type != &IO_TYPE_STDERR) && (fp->type != &IO_TYPE_SIO))) {
|
---|
632 | errno = EBADF;
|
---|
633 | return -1;
|
---|
634 | }
|
---|
635 | void *arg;
|
---|
636 | va_list ap;
|
---|
637 | va_start(ap, cmd);
|
---|
638 | arg = va_arg(ap, void *);
|
---|
639 | va_end(ap);
|
---|
640 |
|
---|
641 | errno = sio_ioctl(fp, cmd, arg);
|
---|
642 | if (errno != 0)
|
---|
643 | return 1;
|
---|
644 | return 0;
|
---|
645 | }
|
---|