Ignore:
Timestamp:
May 22, 2019, 4:09:18 PM (5 years ago)
Author:
coas-nagasima
Message:

ファイルディスクリプタ処理を更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_rx/trunk/ntshell/src/fdtable.c

    r374 r387  
    4444#include "syssvc/syslog.h"
    4545#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"
    6847#include "kernel_cfg.h"
    6948#include <string.h>
     
    7150#include "hal/serial_api.h"
    7251
    73 static int stdio_close(struct SHELL_FILE *fp);
    74 static size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
    75 static size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
    76 static size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
    77 static size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
    78 static size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
    79 static void stdio_delete(struct SHELL_FILE *fp);
    80 
    81 static int sio_close(struct SHELL_FILE *fp);
    82 static size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
    83 static size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
    84 static off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org);
    85 static int sio_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    86 static bool_t sio_readable(struct SHELL_FILE *fp);
    87 static void sio_delete(struct SHELL_FILE *fp);
    88 
    89 IO_TYPE IO_TYPE_STDIN = { stdio_close, stdin_read, stdio_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
    90 IO_TYPE IO_TYPE_STDOUT = { stdio_close, stdio_read, stdout_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
    91 IO_TYPE IO_TYPE_STDERR = { stdio_close, stdio_read, stderr_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
    92 IO_TYPE IO_TYPE_SIO = { sio_close, sio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_delete };
    93 ntstdio_t ntstdio;
     52#ifdef _DEBUG
     53static const char THIS_FILE[] = __FILE__;
     54#endif
     55
     56extern IO_TYPE IO_TYPE_STDIN;
     57extern IO_TYPE IO_TYPE_STDOUT;
     58extern IO_TYPE IO_TYPE_STDERR;
    9459
    9560static struct SHELL_FILE fd_table[8 * sizeof(FLGPTN)] = {
    96         { 0, &IO_TYPE_STDIN, 0, .exinf = &ntstdio },
    97         { 1, &IO_TYPE_STDOUT, 0, .exinf = &ntstdio },
    98         { 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 },
    9964};
    10065#define fd_table_count (sizeof(fd_table) / sizeof(fd_table[0]))
    101 
    102 extern ntstdio_t ntstdio;
    103 serial_t stdio_uart;
    104 
    105 unsigned char ntstdio_xi(struct ntstdio_t *handle)
    106 {
    107         return serial_getc((serial_t *)handle->exinf);
    108 }
    109 
    110 void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
    111 {
    112         serial_putc((serial_t *)handle->exinf, c);
    113 }
    114 
    115 void sys_init(intptr_t exinf)
    116 {
    117         sys_tlsf_init();
    118 
    119         serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
    120         serial_baud(&stdio_uart, UART_BAUDRATE);
    121         serial_format(&stdio_uart, 8, ParityNone, 1);
    122 
    123         ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);
    124         ntstdio.exinf = (void *)&stdio_uart;
    125 }
    126 
    127 int stdio_close(struct SHELL_FILE *fp)
    128 {
    129         return -EPERM;
    130 }
    131 
    132 size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    133 {
    134         return -EPERM;
    135 }
    136 
    137 size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    138 {
    139         return -EPERM;
    140 }
    141 
    142 size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    143 {
    144         int i = 0;
    145         while (i < len) {
    146                 int c = ntstdio_getc((struct ntstdio_t *)fp->exinf);
    147                 data[i++] = c;
    148                 if ((c == EOF) || (c == '\n'))
    149                         break;
    150         }
    151         return i;
    152 }
    153 
    154 size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    155 {
    156         for (int i = 0; i < len; i++) {
    157                 ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);
    158         }
    159         return len;
    160 }
    161 
    162 size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    163 {
    164         for (int i = 0; i < len; i++) {
    165                 ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);
    166         }
    167         return len;
    168 }
    169 
    170 void stdio_delete(struct SHELL_FILE *fp)
    171 {
    172 }
    173 
    174 int sio_close(struct SHELL_FILE *fp)
    175 {
    176         return -EPERM;
    177 }
    178 
    179 size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    180 {
    181         return -EPERM;
    182 }
    183 
    184 size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    185 {
    186         return -EPERM;
    187 }
    188 
    189 off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org)
    190 {
    191         return -EPERM;
    192 }
    193 
    194 int sio_ioctl(struct SHELL_FILE *fp, int request, void *arg)
    195 {
    196         switch (request) {
    197         case TIOCGWINSZ:
    198                 return 0;
    199         case TCGETS:
    200                 return sio_tcgetattr(fp->fd, (struct termios *)arg);
    201         case TCSETS + TCSANOW:
    202         case TCSETS + TCSADRAIN:
    203         case TCSETS + TCSAFLUSH:
    204                 return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
    205         }
    206 
    207         return -EINVAL;
    208 }
    209 
    210 bool_t sio_readable(struct SHELL_FILE *fp)
    211 {
    212         return fp->readevt_w != fp->readevt_r;
    213 }
    214 
    215 void sio_delete(struct SHELL_FILE *fp)
    216 {
    217         free((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf);
    218         ((struct ntstdio_t *)fp->exinf)->exinf = NULL;
    219         free((struct ntstdio_t *)fp->exinf);
    220         fp->exinf = NULL;
    221 }
    22266
    22367struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable)
     
    24791        if (ret < 0) {
    24892                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                }
    249104        }
    250105
     
    442297}
    443298
    444 /* TODO:コールバック化したい */
    445 void stdio_update_evts()
    446 {
    447         int fd = STDIN_FILENO;
    448         struct SHELL_FILE *fp = &fd_table[fd];
    449         FLGPTN flgptn = 0;
    450 
    451         if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
    452                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    453 
    454                 FD_SET(fd, (fd_set *)&flgptn);
    455         }
    456         if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
    457                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    458 
    459                 FD_SET(fd, (fd_set *)&flgptn);
    460         }
    461 
    462         if (flgptn != 0) {
    463                 set_flg(FLG_SELECT_WAIT, flgptn);
    464         }
    465 }
    466 
    467 /* TODO:コールバック化したい */
    468 void stdio_flgptn(FLGPTN *flgptn)
    469 {
    470         int fd = STDIN_FILENO;
    471         struct SHELL_FILE *fp = &fd_table[fd];
    472         *flgptn = 0;
    473 
    474         if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
    475                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    476 
    477                 FD_SET(fd, (fd_set *)flgptn);
    478         }
    479         if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
    480                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    481 
    482                 FD_SET(fd, (fd_set *)flgptn);
    483         }
    484 }
    485 
    486299ER shell_get_evts(struct fd_events *evts, TMO tmout)
    487300{
    488301        int count = 0;
    489302        SYSTIM prev, now;
     303        FLGPTN flgptn;
    490304
    491305        get_tim(&prev);
     
    493307        for (;;) {
    494308                ER ret;
    495                 FLGPTN waitptn, flgptn, readfds = 0, writefds = 0;
     309                FLGPTN waitptn, readfds = 0, writefds = 0;
    496310                struct SHELL_FILE *fp = NULL;
    497 
    498                 stdio_update_evts();
    499311
    500312#ifndef NTSHELL_NO_SOCKET
     
    506318                        fp = &fd_table[fd];
    507319
    508 #ifndef NTSHELL_NO_SOCKET
    509320                        if (FD_ISSET(fd, &evts->readfds)) {
    510321                                if (fp->type->readable(fp)) {
     
    517328                                }
    518329                        }
    519 #endif
     330
    520331                        if (FD_ISSET(fd, &evts->writefds)) {
    521                                 if (fp->writeevt_w == fp->writeevt_r) {
     332                                if (fp->type->writable(fp)) {
    522333                                        FD_SET(fd, (fd_set *)&writefds);
    523334                                        count++;
     
    549360                                return ret;
    550361                        }
    551 
    552                         stdio_flgptn(&flgptn);
    553 
    554                         if (flgptn == 0)
    555                                 return E_TMOUT;
    556                 }
    557                 flgptn &= waitptn;
    558 
    559                 /* 受け取ったフラグのみクリア */
    560                 ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
    561                 if (ret != E_OK) {
    562                         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                        }
    563372                }
    564373
    565374                count = 0;
    566375                for (int fd = 0; fd < fd_table_count; fd++) {
    567                         if (!FD_ISSET(fd, (fd_set *)&waitptn))
    568                                 continue;
    569 
    570376                        fp = &fd_table[fd];
    571377
    572378                        if (fp->readevt_w != fp->readevt_r) {
    573379                                fp->readevt_r++;
    574                                 FD_SET(fd, &evts->readfds);
     380                                if (FD_ISSET(fd, (fd_set *)&waitptn))
     381                                        FD_SET(fd, &evts->readfds);
    575382                                count++;
    576383                        }
    577384                        if (fp->writeevt_w != fp->writeevt_r) {
    578385                                fp->writeevt_r++;
    579                                 fp->writable = 1;
    580                         }
    581                         if (fp->writable) {
    582                                 FD_SET(fd, &evts->writefds);
     386                                if (FD_ISSET(fd, (fd_set *)&waitptn))
     387                                        FD_SET(fd, &evts->writefds);
    583388                                count++;
    584389                        }
    585390                        if (fp->errorevt_w != fp->errorevt_r) {
    586391                                fp->errorevt_r++;
    587                                 FD_SET(fd, &evts->errorfds);
     392                                if (FD_ISSET(fd, (fd_set *)&waitptn))
     393                                        FD_SET(fd, &evts->errorfds);
    588394                                count++;
    589395                        }
    590396                }
    591397
    592                 if (count > 0)
     398                if ((flgptn == 0) || (count > 0))
    593399                        break;
    594400
     
    596402
    597403                SYSTIM elapse = now - prev;
    598                 if (elapse > tmout)
    599                         return E_TMOUT;
     404                if (elapse > tmout) {
     405                        flgptn = 0;
     406                        break;
     407                }
    600408
    601409                prev = now;
     
    605413        evts->count = count;
    606414
    607         return E_OK;
     415        return (flgptn == 0) ? E_TMOUT : E_OK;
    608416}
    609417
Note: See TracChangeset for help on using the changeset viewer.