Changeset 387 for asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c
- Timestamp:
- May 22, 2019, 4:09:18 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c
r374 r387 44 44 #include "syssvc/syslog.h" 45 45 #include "target_syssvc.h" 46 #ifndef NTSHELL_NO_SOCKET 47 #include <tinet_defs.h> 48 #include <tinet_config.h> 49 #include <net/net.h> 50 #include <net/net_endian.h> 51 #include <netinet/in.h> 52 #include <netinet/in_itron.h> 53 #include <tinet_nic_defs.h> 54 #include <tinet_cfg.h> 55 #include <netinet/in_var.h> 56 #include <net/ethernet.h> 57 #include <net/if6_var.h> 58 #include <net/net.h> 59 #include <net/if_var.h> 60 #include <netinet/udp.h> 61 #include <netinet/udp_var.h> 62 #include <netinet/tcp.h> 63 #include <netinet/tcp_var.h> 64 #include <net/net_buf.h> 65 #endif 66 #include "ff.h" 67 #include "socket_stub.h" 46 #include "fdtable.h" 68 47 #include "kernel_cfg.h" 69 48 #include <string.h> … … 75 54 #endif 76 55 77 static int stdio_close(struct SHELL_FILE *fp); 78 static size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 79 static size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 80 static size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 81 static size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 82 static size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 83 static void stdio_delete(struct SHELL_FILE *fp); 84 85 static int sio_close(struct SHELL_FILE *fp); 86 static size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 87 static size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 88 static off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org); 89 static int sio_ioctl(struct SHELL_FILE *fp, int req, void *arg); 90 static bool_t sio_readable(struct SHELL_FILE *fp); 91 static void sio_delete(struct SHELL_FILE *fp); 92 93 IO_TYPE IO_TYPE_STDIN = { stdio_close, stdin_read, stdio_write, sio_seek, sio_ioctl, sio_readable, stdio_delete }; 94 IO_TYPE IO_TYPE_STDOUT = { stdio_close, stdio_read, stdout_write, sio_seek, sio_ioctl, sio_readable, stdio_delete }; 95 IO_TYPE IO_TYPE_STDERR = { stdio_close, stdio_read, stderr_write, sio_seek, sio_ioctl, sio_readable, stdio_delete }; 96 IO_TYPE IO_TYPE_SIO = { sio_close, sio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_delete }; 97 ntstdio_t ntstdio; 56 extern IO_TYPE IO_TYPE_STDIN; 57 extern IO_TYPE IO_TYPE_STDOUT; 58 extern IO_TYPE IO_TYPE_STDERR; 98 59 99 60 static struct SHELL_FILE fd_table[8 * sizeof(FLGPTN)] = { 100 { 0, &IO_TYPE_STDIN, 0, .exinf = &ntstdio},101 { 1, &IO_TYPE_STDOUT, 0, .exinf = &ntstdio},102 { 2, &IO_TYPE_STDERR, 0,.exinf = &ntstdio},61 { STDIN_FILENO, &IO_TYPE_STDIN, 0 }, 62 { STDOUT_FILENO, &IO_TYPE_STDOUT, 0 }, 63 { STDERR_FILENO, &IO_TYPE_STDERR, 0 }, 103 64 }; 104 65 #define fd_table_count (sizeof(fd_table) / sizeof(fd_table[0])) 105 106 extern ntstdio_t ntstdio;107 extern serial_t stdio_uart;108 109 unsigned char ntstdio_xi(struct ntstdio_t *handle)110 {111 return serial_getc((serial_t *)handle->exinf);112 }113 114 void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)115 {116 serial_putc((serial_t *)handle->exinf, c);117 }118 119 void sys_init(intptr_t exinf)120 {121 ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);122 ntstdio.exinf = (void *)&stdio_uart;123 }124 125 int stdio_close(struct SHELL_FILE *fp)126 {127 return -EPERM;128 }129 130 size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)131 {132 return -EPERM;133 }134 135 size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)136 {137 return -EPERM;138 }139 140 size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)141 {142 int i = 0;143 while (i < len) {144 int c = ntstdio_getc((struct ntstdio_t *)fp->exinf);145 data[i++] = c;146 if ((c == EOF) || (c == '\n'))147 break;148 }149 return i;150 }151 152 size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)153 {154 for (int i = 0; i < len; i++) {155 ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);156 }157 return len;158 }159 160 size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)161 {162 for (int i = 0; i < len; i++) {163 ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);164 }165 return len;166 }167 168 void stdio_delete(struct SHELL_FILE *fp)169 {170 }171 172 int sio_close(struct SHELL_FILE *fp)173 {174 return -EPERM;175 }176 177 size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)178 {179 return -EPERM;180 }181 182 size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)183 {184 return -EPERM;185 }186 187 off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org)188 {189 return -EPERM;190 }191 192 int sio_ioctl(struct SHELL_FILE *fp, int request, void *arg)193 {194 switch (request) {195 case TIOCGWINSZ:196 return 0;197 case TCGETS:198 return sio_tcgetattr(fp->fd, (struct termios *)arg);199 case TCSETS + TCSANOW:200 case TCSETS + TCSADRAIN:201 case TCSETS + TCSAFLUSH:202 return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);203 }204 205 return -EINVAL;206 }207 208 bool_t sio_readable(struct SHELL_FILE *fp)209 {210 return fp->readevt_w != fp->readevt_r;211 }212 213 void sio_delete(struct SHELL_FILE *fp)214 {215 free((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf);216 ((struct ntstdio_t *)fp->exinf)->exinf = NULL;217 free((struct ntstdio_t *)fp->exinf);218 fp->exinf = NULL;219 }220 66 221 67 struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable) … … 245 91 if (ret < 0) { 246 92 syslog(LOG_ERROR, "sig_sem => %d", ret); 93 } 94 95 if (fp != NULL) { 96 FLGPTN flgptn = 0; 97 98 FD_SET(fp->fd, (fd_set *)&flgptn); 99 100 ret = clr_flg(FLG_SELECT_WAIT, ~flgptn); 101 if (ret != E_OK) { 102 syslog(LOG_ERROR, "clr_flg => %d", ret); 103 } 247 104 } 248 105 … … 440 297 } 441 298 442 /* TODO:コールバック化したい */443 void stdio_update_evts()444 {445 int fd = STDIN_FILENO;446 struct SHELL_FILE *fp = &fd_table[fd];447 FLGPTN flgptn = 0;448 449 if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {450 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;451 452 FD_SET(fd, (fd_set *)&flgptn);453 }454 if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {455 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;456 457 FD_SET(fd, (fd_set *)&flgptn);458 }459 460 if (flgptn != 0) {461 set_flg(FLG_SELECT_WAIT, flgptn);462 }463 }464 465 /* TODO:コールバック化したい */466 void stdio_flgptn(FLGPTN *flgptn)467 {468 int fd = STDIN_FILENO;469 struct SHELL_FILE *fp = &fd_table[fd];470 *flgptn = 0;471 472 if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {473 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;474 475 FD_SET(fd, (fd_set *)flgptn);476 }477 if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {478 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;479 480 FD_SET(fd, (fd_set *)flgptn);481 }482 }483 484 299 ER shell_get_evts(struct fd_events *evts, TMO tmout) 485 300 { 486 301 int count = 0; 487 302 SYSTIM prev, now; 303 FLGPTN flgptn; 488 304 489 305 get_tim(&prev); … … 491 307 for (;;) { 492 308 ER ret; 493 FLGPTN waitptn, flgptn,readfds = 0, writefds = 0;309 FLGPTN waitptn, readfds = 0, writefds = 0; 494 310 struct SHELL_FILE *fp = NULL; 495 496 stdio_update_evts();497 311 498 312 #ifndef NTSHELL_NO_SOCKET … … 504 318 fp = &fd_table[fd]; 505 319 506 #ifndef NTSHELL_NO_SOCKET507 320 if (FD_ISSET(fd, &evts->readfds)) { 508 321 if (fp->type->readable(fp)) { … … 515 328 } 516 329 } 517 #endif 330 518 331 if (FD_ISSET(fd, &evts->writefds)) { 519 if (fp-> writeevt_w == fp->writeevt_r) {332 if (fp->type->writable(fp)) { 520 333 FD_SET(fd, (fd_set *)&writefds); 521 334 count++; … … 547 360 return ret; 548 361 } 549 550 stdio_flgptn(&flgptn); 551 552 if (flgptn == 0) 553 return E_TMOUT; 554 } 555 flgptn &= waitptn; 556 557 /* 受け取ったフラグのみクリア */ 558 ret = clr_flg(FLG_SELECT_WAIT, ~flgptn); 559 if (ret != E_OK) { 560 syslog(LOG_ERROR, "clr_flg => %d", ret); 362 } 363 364 if (flgptn != 0) { 365 flgptn &= waitptn; 366 367 /* 受け取ったフラグのみクリア */ 368 ret = clr_flg(FLG_SELECT_WAIT, ~flgptn); 369 if (ret != E_OK) { 370 syslog(LOG_ERROR, "clr_flg => %d", ret); 371 } 561 372 } 562 373 563 374 count = 0; 564 375 for (int fd = 0; fd < fd_table_count; fd++) { 565 if (!FD_ISSET(fd, (fd_set *)&waitptn))566 continue;567 568 376 fp = &fd_table[fd]; 569 377 570 378 if (fp->readevt_w != fp->readevt_r) { 571 379 fp->readevt_r++; 572 FD_SET(fd, &evts->readfds); 380 if (FD_ISSET(fd, (fd_set *)&waitptn)) 381 FD_SET(fd, &evts->readfds); 573 382 count++; 574 383 } 575 384 if (fp->writeevt_w != fp->writeevt_r) { 576 385 fp->writeevt_r++; 577 fp->writable = 1; 578 } 579 if (fp->writable) { 580 FD_SET(fd, &evts->writefds); 386 if (FD_ISSET(fd, (fd_set *)&waitptn)) 387 FD_SET(fd, &evts->writefds); 581 388 count++; 582 389 } 583 390 if (fp->errorevt_w != fp->errorevt_r) { 584 391 fp->errorevt_r++; 585 FD_SET(fd, &evts->errorfds); 392 if (FD_ISSET(fd, (fd_set *)&waitptn)) 393 FD_SET(fd, &evts->errorfds); 586 394 count++; 587 395 } 588 396 } 589 397 590 if ( count > 0)398 if ((flgptn == 0) || (count > 0)) 591 399 break; 592 400 … … 594 402 595 403 SYSTIM elapse = now - prev; 596 if (elapse > tmout) 597 return E_TMOUT; 404 if (elapse > tmout) { 405 flgptn = 0; 406 break; 407 } 598 408 599 409 prev = now; … … 603 413 evts->count = count; 604 414 605 return E_OK;415 return (flgptn == 0) ? E_TMOUT : E_OK; 606 416 } 607 417
Note:
See TracChangeset
for help on using the changeset viewer.