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

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

Location:
asp3_tinet_ecnl_arm/trunk/ntshell
Files:
4 added
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_arm/trunk/ntshell/fatfs/ffarch.c

    r374 r387  
    8080        ret2 = get_tim(&now);
    8181        if (ret2 != E_OK){
    82                 printf("[ffarch] get_tim error: %s",
     82                syslog(LOG_NOTICE, "[ffarch] get_tim error: %s",
    8383                        itron_strerror(ret2));
    8484                return;
     
    9494                ret = tslp_tsk(timer);
    9595                if ((ret != E_OK) && (ret != E_TMOUT)) {
    96                         printf("[ffarch] tslp_tsk error: %s %d",
     96                        syslog(LOG_NOTICE, "[ffarch] tslp_tsk error: %s %d",
    9797                                itron_strerror(ret), timer);
    9898                        break;
     
    101101                ret2 = get_tim(&now);
    102102                if (ret2 != E_OK) {
    103                         printf("[ffarch] get_tim error: %s",
     103                        syslog(LOG_NOTICE, "[ffarch] get_tim error: %s",
    104104                                itron_strerror(ret2));
    105105                        break;
     
    131131bool_t SD_begin();
    132132
    133 void ffarch_init()
     133void ffarch_init(void)
    134134{
    135135        /* SD_CD */
     
    150150
    151151        if (romdisk_init()) {
    152                 printf("ROM disk (0:) OK!\n");
     152                syslog(LOG_NOTICE, "ROM disk (0:) OK!");
    153153        }
    154154        else {
    155                 printf("ROM disk (0:) NG!\n");
     155                syslog(LOG_NOTICE, "ROM disk (0:) NG!");
    156156        }
    157157}
     
    352352}
    353353
    354 int ffarch_get_timer()
     354int ffarch_get_timer(void)
    355355{
    356356        return ffarch_timer;
     
    367367}
    368368
    369 void ffarch_timeout()
     369void ffarch_timeout(void)
    370370{
    371371        if (ffarch_timer != 0)
     
    375375        case FFS_RETRY_WAIT:
    376376                if (ffarch_retry_count == 0) {
    377                         printf("SD card (1:) initialize tired...\n");
     377                        syslog(LOG_NOTICE, "SD card (1:) initialize tired...");
    378378
    379379                        ffarch_state = FFS_IDLE;
     
    388388                if ((sdfs._is_initialized & STA_NOINIT)
    389389                        || (((sdfs_prev_status & STA_NODISK) != 0) && ((sdfs_new_status & STA_NODISK) == 0))) {
    390                         printf("SD card initializing ...\n");
     390                        syslog(LOG_NOTICE, "SD card initializing ...");
    391391
    392392                        if (SD_begin()) {
    393                                 printf("SD card (1:) OK!\n");
     393                                syslog(LOG_NOTICE, "SD card (1:) OK!");
    394394
    395395                                /* uploadディレクトリを作成しておく */
     
    401401                        }
    402402                        else {
    403                                 printf("SD card (1:) NG!\n");
     403                                syslog(LOG_NOTICE, "SD card (1:) NG!");
    404404                                ffarch_state = FFS_RETRY_WAIT;
    405405                                ffarch_timer = 1000 * 1000;
     
    408408                /* SDカードが抜かれた場合 */
    409409                else if (((sdfs_prev_status & STA_NODISK) == 0) && ((sdfs_new_status & STA_NODISK) != 0)) {
    410                         printf("SD card unmount\n");
     410                        syslog(LOG_NOTICE, "SD card unmount");
    411411
    412412                        f_mount(&Sd.FatFs, "1:", 0);
  • asp3_tinet_ecnl_arm/trunk/ntshell/ntshell/util/ntstdio.c

    r352 r387  
    224224        handle->xi = xi;
    225225        handle->xo = xo;
    226         handle->outptr = 0;
    227         handle->len = 0;
    228226        handle->pos = 0;
    229227        handle->option = option;
     
    232230static void _putc(ntstdio_t *handle, char c)
    233231{
    234         if (handle->outptr) {
    235                 if ((handle->pos + 1) >= handle->len)
    236                         return;
    237                 handle->outptr[handle->pos] = (unsigned char)c;
    238                 handle->pos++;
    239                 return;
    240         }
    241 
    242232        if (handle->xo) {
    243233                handle->xo(handle, (unsigned char)c);
     
    291281}
    292282
     283struct put_buf_t {
     284        char *outptr;
     285        int len;
     286};
     287
     288static void put_buf(struct ntstdio_t *handle, unsigned char c)
     289{
     290        struct put_buf_t *put_buf = (struct put_buf_t *)handle->exinf;
     291        if ((handle->pos + 1) >= put_buf->len)
     292                return;
     293        put_buf->outptr[handle->pos] = (char)c;
     294        handle->pos++;
     295}
     296
    293297int ntstdio_snprintf(char *buf, int len, const char *fmt, ...)
    294298{
    295299        int result;
    296300        ntstdio_t handle;
     301        struct put_buf_t exinf;
    297302        va_list arp;
    298303        /* Switch destination for memory */
     304        handle.xo = put_buf;
    299305        handle.option = 0;
    300         handle.outptr = buf;
    301         handle.len = len;
    302306        handle.pos = 0;
     307        handle.exinf = &exinf;
     308        exinf.outptr = buf;
     309        exinf.len = len;
     310
    303311        va_start(arp, fmt);
    304312        result = xvprintf(&handle, fmt, arp);
     
    306314
    307315        /* Terminate output string with a \0 */
    308         handle.outptr[handle.pos] = '\0';
     316        buf[handle.pos] = '\0';
    309317        return result;
    310318}
  • asp3_tinet_ecnl_arm/trunk/ntshell/ntshell/util/ntstdio.h

    r374 r387  
    7474        NTSTDIO_XI xi;
    7575        NTSTDIO_XO xo;
    76         char *outptr;
    77         int len;
    7876        int pos;
    7977        unsigned int option;
  • asp3_tinet_ecnl_arm/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>
     
    7554#endif
    7655
    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;
     56extern IO_TYPE IO_TYPE_STDIN;
     57extern IO_TYPE IO_TYPE_STDOUT;
     58extern IO_TYPE IO_TYPE_STDERR;
    9859
    9960static 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 },
    10364};
    10465#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 }
    22066
    22167struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable)
     
    24591        if (ret < 0) {
    24692                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                }
    247104        }
    248105
     
    440297}
    441298
    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 
    484299ER shell_get_evts(struct fd_events *evts, TMO tmout)
    485300{
    486301        int count = 0;
    487302        SYSTIM prev, now;
     303        FLGPTN flgptn;
    488304
    489305        get_tim(&prev);
     
    491307        for (;;) {
    492308                ER ret;
    493                 FLGPTN waitptn, flgptn, readfds = 0, writefds = 0;
     309                FLGPTN waitptn, readfds = 0, writefds = 0;
    494310                struct SHELL_FILE *fp = NULL;
    495 
    496                 stdio_update_evts();
    497311
    498312#ifndef NTSHELL_NO_SOCKET
     
    504318                        fp = &fd_table[fd];
    505319
    506 #ifndef NTSHELL_NO_SOCKET
    507320                        if (FD_ISSET(fd, &evts->readfds)) {
    508321                                if (fp->type->readable(fp)) {
     
    515328                                }
    516329                        }
    517 #endif
     330
    518331                        if (FD_ISSET(fd, &evts->writefds)) {
    519                                 if (fp->writeevt_w == fp->writeevt_r) {
     332                                if (fp->type->writable(fp)) {
    520333                                        FD_SET(fd, (fd_set *)&writefds);
    521334                                        count++;
     
    547360                                return ret;
    548361                        }
    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                        }
    561372                }
    562373
    563374                count = 0;
    564375                for (int fd = 0; fd < fd_table_count; fd++) {
    565                         if (!FD_ISSET(fd, (fd_set *)&waitptn))
    566                                 continue;
    567 
    568376                        fp = &fd_table[fd];
    569377
    570378                        if (fp->readevt_w != fp->readevt_r) {
    571379                                fp->readevt_r++;
    572                                 FD_SET(fd, &evts->readfds);
     380                                if (FD_ISSET(fd, (fd_set *)&waitptn))
     381                                        FD_SET(fd, &evts->readfds);
    573382                                count++;
    574383                        }
    575384                        if (fp->writeevt_w != fp->writeevt_r) {
    576385                                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);
    581388                                count++;
    582389                        }
    583390                        if (fp->errorevt_w != fp->errorevt_r) {
    584391                                fp->errorevt_r++;
    585                                 FD_SET(fd, &evts->errorfds);
     392                                if (FD_ISSET(fd, (fd_set *)&waitptn))
     393                                        FD_SET(fd, &evts->errorfds);
    586394                                count++;
    587395                        }
    588396                }
    589397
    590                 if (count > 0)
     398                if ((flgptn == 0) || (count > 0))
    591399                        break;
    592400
     
    594402
    595403                SYSTIM elapse = now - prev;
    596                 if (elapse > tmout)
    597                         return E_TMOUT;
     404                if (elapse > tmout) {
     405                        flgptn = 0;
     406                        break;
     407                }
    598408
    599409                prev = now;
     
    603413        evts->count = count;
    604414
    605         return E_OK;
     415        return (flgptn == 0) ? E_TMOUT : E_OK;
    606416}
    607417
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.h

    r386 r387  
    3838#define SOCKET_STUB_H
    3939
    40 struct addrinfo {
    41         int ai_flags;
    42         int ai_family;
    43         int ai_socktype;
    44         int ai_protocol;
    45         socklen_t ai_addrlen;
    46         struct sockaddr *ai_addr;
    47         char *ai_canonname;
    48         struct addrinfo *ai_next;
    49 };
    50 
    51 typedef uint16_t in_port_t;
    52 typedef uint32_t in_addr_t;
    53 struct in_addr { in_addr_t s_addr; };
    54 
    55 struct sockaddr_in {
    56         sa_family_t sin_family;
    57         in_port_t sin_port;
    58         struct in_addr sin_addr;
    59         uint8_t sin_zero[8];
    60 };
    61 
    62 struct in6_addr
    63 {
    64         union {
    65                 uint8_t __s6_addr[16];
    66                 uint16_t __s6_addr16[8];
    67                 uint32_t __s6_addr32[4];
    68         } __in6_union;
    69 };
    70 //#define s6_addr __in6_union.__s6_addr
    71 //#define s6_addr16 __in6_union.__s6_addr16
    72 //#define s6_addr32 __in6_union.__s6_addr32
    73 
    74 struct sockaddr_in6
    75 {
    76         sa_family_t     sin6_family;
    77         in_port_t       sin6_port;
    78         uint32_t        sin6_flowinfo;
    79         struct in6_addr sin6_addr;
    80         uint32_t        sin6_scope_id;
    81 };
    82 
    83 typedef struct socket_t {
    84         int family;
    85         int type;
    86         int protocol;
    87         int cepid;
    88         int repid;
    89         int backlog;
    90         unsigned int flags;
    91         union {
    92                 struct sockaddr_in laddr4;
    93                 struct sockaddr_in6 laddr6;
    94         };
    95         union {
    96                 struct sockaddr_in raddr4;
    97                 struct sockaddr_in6 raddr6;
    98         };
    99         int buf_size;
    100         unsigned char *buf;
    101         void *input;
    102         int len;
    103 } socket_t;
    104 
    105 struct SHELL_DIR {
    106         FATFS_DIR dir;
    107         struct dirent dirent;
    108 };
    109 
    11040typedef const struct io_type_s IO_TYPE;
    11141
     
    13161        int (*ioctl)(struct SHELL_FILE *, int, void *);
    13262        bool_t (*readable)(struct SHELL_FILE *);
     63        bool_t (*writable)(struct SHELL_FILE *);
    13364        void (*delete)(struct SHELL_FILE *);
    13465};
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/io_stub.c

    r374 r387  
    4545#include "syssvc/serial.h"
    4646#include "syssvc/syslog.h"
    47 #include "socket_stub.h"
     47#include "fdtable.h"
    4848#include "util/ntstdio.h"
    4949#include "usrcmd.h"
     
    5151#include "kernel_cfg.h"
    5252#include "target_syssvc.h"
     53
     54struct SHELL_DIR {
     55        FATFS_DIR dir;
     56        struct dirent dirent;
     57};
    5358
    5459int fresult2errno(FRESULT res)
     
    8590static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    8691static bool_t file_readable(struct SHELL_FILE *fp);
     92static bool_t file_writable(struct SHELL_FILE *fp);
    8793static void file_delete(struct SHELL_FILE *fp);
    8894
     
    9399static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    94100static bool_t dir_readable(struct SHELL_FILE *fp);
     101static bool_t dir_writable(struct SHELL_FILE *fp);
    95102static void dir_delete(struct SHELL_FILE *fp);
    96103
    97 IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_delete };
    98 IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_delete };
     104IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_writable, file_delete };
     105IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_writable, dir_delete };
    99106
    100107int shell_open(const char *path, int flags, void *arg)
     
    114121                FRESULT res;
    115122                if ((res = f_opendir(dir, path)) != FR_OK) {
     123                        delete_fp(fp);
    116124                        return fresult2errno(res);
    117125                }
     
    165173        }
    166174
     175        delete_fp(fp);
    167176        return fresult2errno(res);
    168177}
     
    239248}
    240249
     250bool_t file_writable(struct SHELL_FILE *fp)
     251{
     252        return fp->writable && (fp->writeevt_w == fp->writeevt_r);
     253}
     254
    241255void file_delete(struct SHELL_FILE *fp)
    242256{
     
    356370{
    357371        return shell_ioctl(fd, cmd, arg);
    358 }
    359 
    360 extern IO_TYPE IO_TYPE_SIO;
    361 
    362 int sio_tcgetattr(int fd, struct termios *termios)
    363 {
    364         struct SHELL_FILE *fp = fd_to_fp(fd);
    365         if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
    366                 return -EBADF;
    367 
    368         ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
    369 
    370         memset(termios, 0, sizeof(*termios));
    371 
    372         if (ntstdio->option & NTSTDIO_OPTION_LINE_ECHO) {
    373                 termios->c_lflag |= ECHO;
    374         }
    375         else {
    376                 termios->c_lflag &= ~ECHO;
    377         }
    378         if (ntstdio->option & NTSTDIO_OPTION_CANON) {
    379                 termios->c_lflag |= ICANON;
    380         }
    381         else {
    382                 termios->c_lflag &= ~ICANON;
    383         }
    384         if (ntstdio->option & NTSTDIO_OPTION_LF_CR) {
    385                 termios->c_iflag |= INLCR;
    386         }
    387         else {
    388                 termios->c_iflag &= ~INLCR;
    389         }
    390         if (ntstdio->option & NTSTDIO_OPTION_LF_CRLF) {
    391                 termios->c_oflag |= ONLCR;
    392         }
    393         else {
    394                 termios->c_oflag &= ~ONLCR;
    395         }
    396 
    397         return 0;
    398 }
    399 
    400 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
    401 {
    402         struct SHELL_FILE *fp = fd_to_fp(fd);
    403         if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
    404                 return -EBADF;
    405 
    406         ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
    407 
    408         if (optional_actions == TCSANOW) {
    409                 if (termios->c_lflag & ECHO) {
    410                         ntstdio->option |= NTSTDIO_OPTION_LINE_ECHO;
    411                 }
    412                 else {
    413                         ntstdio->option &= ~NTSTDIO_OPTION_LINE_ECHO;
    414                 }
    415                 if (termios->c_lflag & ICANON) {
    416                         ntstdio->option |= NTSTDIO_OPTION_CANON;
    417                 }
    418                 else {
    419                         ntstdio->option &= ~NTSTDIO_OPTION_CANON;
    420                 }
    421                 if (termios->c_iflag & INLCR) {
    422                         ntstdio->option |= NTSTDIO_OPTION_LF_CR;
    423                 }
    424                 else {
    425                         ntstdio->option &= ~NTSTDIO_OPTION_LF_CR;
    426                 }
    427                 if (termios->c_oflag & ONLCR) {
    428                         ntstdio->option |= NTSTDIO_OPTION_LF_CRLF;
    429                 }
    430                 else {
    431                         ntstdio->option &= ~NTSTDIO_OPTION_LF_CRLF;
    432                 }
    433                 return 0;
    434         }
    435 
    436         shell_abort();
    437         return 0;
    438372}
    439373
     
    688622}
    689623
     624bool_t dir_writable(struct SHELL_FILE *fp)
     625{
     626        return false;
     627}
     628
    690629void dir_delete(struct SHELL_FILE *fp)
    691630{
     
    710649        return 0;
    711650}
    712 
    713 #ifndef _MSC_VER
    714 extern uint32_t __HeapBase;
    715 extern uint32_t __HeapLimit;
    716 #else
    717 uint8_t __HeapBase[14 * 4096];
    718 #define __HeapLimit __HeapBase[sizeof(__HeapBase)]
    719 #endif
    720 
    721 void *shell_brk(void *addr)
    722 {
    723         if (addr == 0) {
    724                 return (void *)(&__HeapBase);
    725         }
    726         if ((addr >= (void *)&__HeapBase) && (addr < (void *)&__HeapLimit)) {
    727                 return addr;
    728         }
    729         return (void *)-1;
    730 }
    731 
    732 void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
    733 {
    734         if (fd != -1)
    735                 return (void *)-EINVAL;
    736 
    737         if ((length >= 0) && (length <= sizeof(&__HeapBase))) {
    738                 return &__HeapBase;
    739         }
    740         return (void *)-1;
    741 }
    742 
    743 int shell_mprotect(void *addr, size_t len, int prot)
    744 {
    745         //if ((addr >= (void *)&__HeapBase) && (addr + len < (void *)&__HeapLimit)) {
    746         return 0;
    747 //}
    748 //return -1;
    749 }
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/net_misc.c

    r374 r387  
    9595
    9696        get_tid(&nc->tskid);
    97         printf("[NET MISC:%d,%d] started.", nc->tskid, (ID)exinf);
     97        syslog(LOG_NOTICE, "[NET MISC:%d,%d] started.", nc->tskid, (ID)exinf);
    9898
    9999        /* 初期化 */
     
    103103        ret = get_tim(&time);
    104104        if (ret != E_OK) {
    105                 printf("[NET MISC,%d] get_tim error: %7lu,%s",
     105                syslog(LOG_NOTICE, "[NET MISC,%d] get_tim error: %7lu,%s",
    106106                        nc->cepid, time / SYSTIM_HZ, itron_strerror(ret));
    107107                return;
     
    117117                error = tslp_tsk(timer);
    118118                if ((error != E_OK) && (error != E_TMOUT)) {
    119                         printf("[NET MISC,%d] tslp_tsk error: %s %d",
     119                        syslog(LOG_NOTICE, "[NET MISC,%d] tslp_tsk error: %s %d",
    120120                                nc->cepid, itron_strerror(error), timer);
    121121                        break;
     
    124124                ret = get_tim(&time);
    125125                if (ret != E_OK) {
    126                         printf("[NET MISC,%d] get_tim error: %s",
     126                        syslog(LOG_NOTICE, "[NET MISC,%d] get_tim error: %s",
    127127                                nc->cepid, itron_strerror(ret));
    128128                        break;
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/ntp_cli.c

    r374 r387  
    277277        }
    278278
    279         printf("[NTP CLI,%d] recv time: %s .%09u\n",
     279        syslog(LOG_NOTICE, "[NTP CLI,%d] recv time: %s .%09u",
    280280                nc->cepid, nc->buf, tp.tv_nsec);
    281281}
     
    309309                line = lookup_ipaddr(&nc->ipaddr6, NTP_SRV_URL, API_PROTO_IPV4);
    310310                if (line == NULL || !in6_is_addr_ipv4mapped(&nc->ipaddr6)) {
    311                         printf("[NTP CLI,%d] sleep %d.%03u[s], unknown host.",
     311                        syslog(LOG_NOTICE, "[NTP CLI,%d] sleep %d.%03u[s], unknown host.",
    312312                                nc->cepid, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
    313313                        nc->timer = SLP_ITV;
     
    317317#else   /* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
    318318                if ((line = lookup_ipaddr(&nc->snd_rmt.ipaddr, NTP_SRV_URL, DEFAULT_API_PROTO)) == NULL) {
    319                         printf("[NTP CLI,%d] sleep %d.%03u[s], unknown host.",
     319                        syslog(LOG_NOTICE, "[NTP CLI,%d] sleep %d.%03u[s], unknown host.",
    320320                                nc->cepid, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
    321321                        nc->timer = SLP_ITV;
     
    421421
    422422        if ((error = udp_snd_dat(nc->cepid, &nc->snd_rmt, ntp, len, TMO_NBLK)) != E_WBLK) {
    423                 printf("[NTP CLI,%d] udp_snd_dat error: %s",
     423                syslog(LOG_NOTICE, "[NTP CLI,%d] udp_snd_dat error: %s",
    424424                        nc->cepid, itron_strerror(error));
    425425                return error;
    426426        }
    427427        else
    428                 syslog(LOG_DEBUG, "[NTP CLI,%d] udp_snd_dat: to: %s.%d\n",
     428                syslog(LOG_DEBUG, "[NTP CLI,%d] udp_snd_dat: to: %s.%d",
    429429                        nc->cepid, ip2str(NULL, &nc->snd_rmt.ipaddr), nc->snd_rmt.portno);
    430430
     
    439439        ret = get_tim(&time);
    440440        if (ret != E_OK) {
    441                 printf("[NTP CLI,%d] get_tim error: %s",
     441                syslog(LOG_NOTICE, "[NTP CLI,%d] get_tim error: %s",
    442442                        nc->cepid, itron_strerror(ret));
    443443                tp->tv_sec = 0;
     
    459459        ret = set_tim(time);
    460460        if (ret != E_OK) {
    461                 printf("[NTP CLI,%d] set_tim error: %s",
     461                syslog(LOG_NOTICE, "[NTP CLI,%d] set_tim error: %s",
    462462                        nc->cepid, itron_strerror(ret));
    463463        }
     
    477477        if (len < 0 && len != E_RLWAI) {
    478478                /* E_RLWAI 以外で、0 以下の場合は、エラーを意味している。*/
    479                 printf("[NTP CLI,%d] callback error: %s, fncd: %s", nc->cepid,
     479                syslog(LOG_NOTICE, "[NTP CLI,%d] callback error: %s, fncd: %s", nc->cepid,
    480480                        itron_strerror(len), in_strtfn(fncd));
    481481        }
     
    484484                        if ((len = udp_rcv_dat(nc->cepid, &nc->rcv_rmt, &nc->ntp_msg, len, TMO_POL)) < 0)
    485485                        {
    486                                 printf("[NTP CLI,%d] udp_rcv_dat error: %s", nc->cepid,
     486                                syslog(LOG_NOTICE, "[NTP CLI,%d] udp_rcv_dat error: %s", nc->cepid,
    487487                                        itron_strerror(len));
    488488                        }
     
    516516
    517517        get_tid(&nc->tskid);
    518         printf("[NTP CLI:%d,%d] started.", nc->tskid, (ID)exinf);
     518        syslog(LOG_NOTICE, "[NTP CLI:%d,%d] started.", nc->tskid, (ID)exinf);
    519519
    520520        /* 初期化 */
     
    523523        ret = get_tim(&time);
    524524        if (ret != E_OK) {
    525                 printf("[NTP CLI,%d] get_tim error: %7lu,%s",
     525                syslog(LOG_NOTICE, "[NTP CLI,%d] get_tim error: %7lu,%s",
    526526                        nc->cepid, time / SYSTIM_HZ, itron_strerror(ret));
    527527                return;
     
    537537                error = tslp_tsk(timer);
    538538                if ((error != E_OK) && (error != E_TMOUT)) {
    539                         printf("[NTP CLI,%d] tslp_tsk error: %s %d",
     539                        syslog(LOG_NOTICE, "[NTP CLI,%d] tslp_tsk error: %s %d",
    540540                                nc->cepid, itron_strerror(error), timer);
    541541                        break;
     
    544544                ret = get_tim(&time);
    545545                if (ret != E_OK) {
    546                         printf("[NTP CLI,%d] get_tim error: %s",
     546                        syslog(LOG_NOTICE, "[NTP CLI,%d] get_tim error: %s",
    547547                                nc->cepid, itron_strerror(ret));
    548548                        break;
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/ntshell_main.c

    r374 r387  
    5858#include "util/ntopt.h"
    5959#include "ntshell_main.h"
    60 #include "socket_stub.h"
     60#include "fdtable.h"
    6161
    6262char command[NTOPT_TEXT_MAXLEN];
     
    7878}
    7979
     80void stdio_open(ID portid);
    8081static int usrcmd_ntopt_callback(long *args, void *extobj);
    8182
     
    8687void ntshell_task_init(ID portid)
    8788{
    88         serial_ctl_por(portid, IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV);
     89        stdio_open(portid);
    8990}
    9091
     
    9596{
    9697        ntshell_state = 1;
    97         ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
     98
     99        if (setjmp(process_exit) == 0) {
     100                ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
     101        }
     102
     103        fflush(stdout);
     104        clean_fd();
     105
    98106        ntshell_state = 2;
    99107}
     
    139147                printf("Unknown command found.\n");
    140148
    141         clean_fd();
    142 
    143149        return result;
    144150}
     
    285291}
    286292
    287 struct sigaction sigtable[7];
    288 
    289 int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
    290 {
    291         struct sigaction *sat;
     293// musl-1.1.18\src\internal\ksigaction.h
     294struct k_sigaction {
     295        void(*handler)(int);
     296        unsigned long flags;
     297        void(*restorer)(void);
     298        unsigned mask[2];
     299};
     300
     301struct k_sigaction sigtable[7];
     302
     303int shell_sigaction(int sig, const struct k_sigaction *__restrict sa,
     304        struct k_sigaction *__restrict old, size_t size)
     305{
     306        struct k_sigaction *sat;
    292307
    293308        switch(sig){
     
    318333
    319334        if (old != NULL)
    320                 memcpy(old, sat, sizeof(struct sigaction));
    321 
    322         memcpy(sat, sa, sizeof(struct sigaction));
     335                memcpy(old, sat, offsetof(struct k_sigaction, mask) + size);
     336
     337        memcpy(sat, sa, offsetof(struct k_sigaction, mask) + size);
    323338
    324339        return 0;
     
    368383}
    369384
     385int shell_nanosleep(const struct timespec *req, struct timespec *rem)
     386{
     387        ER ret;
     388        TMO tmo;
     389        SYSTIM prev, now, diff;
     390
     391        if ((req == NULL) || (req->tv_nsec < 0) || (req->tv_nsec >= 1000000000))
     392                return -EINVAL;
     393
     394        get_tim(&prev);
     395
     396        tmo = req->tv_sec * 1000000 + req->tv_nsec / 1000;
     397        ret = tslp_tsk(tmo);
     398        if (ret == E_OK) {
     399                if (rem != NULL) {
     400                        get_tim(&now);
     401                        diff = now - prev;
     402                        rem->tv_sec = diff / 1000000ll;
     403                        rem->tv_nsec = (diff - (rem->tv_sec * 1000000ll)) * 1000ll;
     404                }
     405                return 0;
     406        }
     407        else if (ret == E_TMOUT) {
     408                if (rem != NULL) {
     409                        rem->tv_sec = 0;
     410                        rem->tv_nsec = 0;
     411                }
     412                return 0;
     413        }
     414
     415        return -EFAULT;
     416}
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/shellif.h

    r374 r387  
    7474#define NCCS 32
    7575
    76 #include <bits/fcntl.h>
    77 #include <bits/termios.h>
    78 #include <bits/stat.h>
     76#include <fcntl.h>
     77#include <termios.h>
     78#include <sys/stat.h>
    7979#include <sys/select.h>
    8080#include <time.h>
     
    8585#include <sys/utsname.h>
    8686#include <dirent.h>
    87 #include <bits/ioctl.h>
    88 #include <bits/errno.h>
     87#include <sys/ioctl.h>
     88#include <errno.h>
    8989#include <sys/socket.h>
    9090#else
     
    132132// time.h
    133133#define CLOCK_REALTIME 0
     134int shell_nanosleep(const struct timespec *req, struct timespec *rem);
    134135
    135136// dirent.h
     
    177178// signal.h
    178179int shell_sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict old);
    179 int shell_sigaction(int sig, const struct sigaction *__restrict sa, struct sigaction *__restrict old);
     180struct k_sigaction;
     181int shell_sigaction(int sig, const struct k_sigaction *__restrict sa,
     182        struct k_sigaction *__restrict old, size_t size);
    180183// socket.h
    181184int shell_socket(int, int, int);
     
    197200// syslog.h
    198201void vsyslog (int, const char *, va_list);
    199 // termios.h
    200 int sio_tcgetattr (int, struct termios *);
    201 int sio_tcsetattr(int, int, const struct termios *);
    202202// fcntl.h
    203203int shell_open(const char *, int, void *arg);
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/socket_stub.c

    r375 r387  
    6262#include <netinet/tcp_var.h>
    6363#include <netapp/resolver.h>
    64 extern const ID tmax_tcp_cepid;
    65 #include "ff.h"
    66 #include "socket_stub.h"
     64#include "fdtable.h"
    6765#include "kernel_cfg.h"
    6866
     67#ifdef _DEBUG
     68static const char THIS_FILE[] = __FILE__;
     69#endif
     70
    6971#define SOCKET_TIMEOUT 2000000
     72
     73struct addrinfo {
     74        int ai_flags;
     75        int ai_family;
     76        int ai_socktype;
     77        int ai_protocol;
     78        socklen_t ai_addrlen;
     79        struct sockaddr *ai_addr;
     80        char *ai_canonname;
     81        struct addrinfo *ai_next;
     82};
     83
     84typedef uint16_t in_port_t;
     85typedef uint32_t in_addr_t;
     86struct in_addr { in_addr_t s_addr; };
     87
     88struct sockaddr_in {
     89        sa_family_t sin_family;
     90        in_port_t sin_port;
     91        struct in_addr sin_addr;
     92        uint8_t sin_zero[8];
     93};
     94
     95struct in6_addr
     96{
     97        union {
     98                uint8_t __s6_addr[16];
     99                uint16_t __s6_addr16[8];
     100                uint32_t __s6_addr32[4];
     101        } __in6_union;
     102};
     103//#define s6_addr __in6_union.__s6_addr
     104//#define s6_addr16 __in6_union.__s6_addr16
     105//#define s6_addr32 __in6_union.__s6_addr32
     106
     107struct sockaddr_in6
     108{
     109        sa_family_t     sin6_family;
     110        in_port_t       sin6_port;
     111        uint32_t        sin6_flowinfo;
     112        struct in6_addr sin6_addr;
     113        uint32_t        sin6_scope_id;
     114};
     115
     116typedef struct socket_t {
     117        int family;
     118        int type;
     119        int protocol;
     120        int cepid;
     121        int repid;
     122        int backlog;
     123        unsigned int flags;
     124        union {
     125                struct sockaddr_in laddr4;
     126                struct sockaddr_in6 laddr6;
     127        };
     128        union {
     129                struct sockaddr_in raddr4;
     130                struct sockaddr_in6 raddr6;
     131        };
     132        int buf_size;
     133        unsigned char *buf;
     134        void *input;
     135        int len;
     136} socket_t;
    70137
    71138#define tcp6_cre_cep tcp_cre_cep
     
    82149#define tcp6_set_opt tcp_set_opt
    83150
    84 #define udp6_del_cep udp_del_cep
    85 #define udp6_get_opt udp_get_opt
    86 #define udp6_set_opt udp_set_opt
    87 
    88151#ifndef SUPPORT_INET6
    89152
     
    93156
    94157ER      udp6_cre_cep (ID cepid, T_UDP6_CCEP *pk_ccep) { return E_SYS; }
     158ER      udp6_del_cep (ID cepid) { return E_SYS; }
    95159ER_UINT udp6_snd_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
    96160ER_UINT udp6_rcv_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
     161ER      udp6_set_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
     162ER      udp6_get_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
    97163
    98164const T_IN6_ADDR *in6_get_ifaddr (int_t index) { return NULL; }
     
    111177static int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    112178static bool_t tcp_fd_readable(struct SHELL_FILE *fp);
     179static bool_t tcp_fd_writable(struct SHELL_FILE *fp);
    113180static void tcp_fd_delete(struct SHELL_FILE *fp);
    114181
     
    119186static int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    120187static bool_t udp_fd_readable(struct SHELL_FILE *fp);
     188static bool_t udp_fd_writable(struct SHELL_FILE *fp);
    121189static void udp_fd_delete(struct SHELL_FILE *fp);
    122190
    123 IO_TYPE IO_TYPE_TCP = { tcp_fd_close, tcp_fd_read, tcp_fd_write, tcp_fd_seek, tcp_fd_ioctl, tcp_fd_readable, tcp_fd_delete };
    124 IO_TYPE IO_TYPE_UDP = { udp_fd_close, udp_fd_read, udp_fd_write, udp_fd_seek, udp_fd_ioctl, udp_fd_readable, udp_fd_delete };
     191IO_TYPE IO_TYPE_TCP = { tcp_fd_close, tcp_fd_read, tcp_fd_write, tcp_fd_seek, tcp_fd_ioctl, tcp_fd_readable, tcp_fd_writable, tcp_fd_delete };
     192IO_TYPE IO_TYPE_UDP = { udp_fd_close, udp_fd_read, udp_fd_write, udp_fd_seek, udp_fd_ioctl, udp_fd_readable, udp_fd_writable, udp_fd_delete };
    125193
    126194typedef struct id_table_t {
     
    185253
    186254#endif
     255
     256void addrcpy(void *_dst, const void *_src, int len)
     257{
     258#if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
     259        memcpy(_dst, _src, len);
     260#else
     261        uint8_t *dst = (uint8_t *)_dst, *src = &((uint8_t *)_src)[len];
     262        while (src != _src)
     263                *dst++ = *--src;
     264#endif
     265}
    187266
    188267ID new_id(id_table_t *table, int count)
     
    347426                }
    348427                case SOCK_DGRAM: {
    349                         ID cepid = new_id(udp_cepid_table, udp_cepid_table_count);
     428                        ID cepid = new_id(udp6_cepid_table, udp6_cepid_table_count);
    350429                        if (cepid < 0)
    351430                                return -ENOMEM;
    352431
    353                         T_UDP6_CCEP ccep = { 0, {ntohl(addr_in6->sin6_addr.__in6_union.__s6_addr), ntohs(addr_in6->sin6_port)}, (FP)socket_udp6_callback };
     432                        T_UDP6_CCEP ccep = { 0, { { ntohl(addr_in6->sin6_addr.__in6_union.__s6_addr) }, ntohs(addr_in6->sin6_port)}, (FP)socket_udp6_callback };
    354433                        ret = udp6_cre_cep(cepid, &ccep);
    355434                        if (ret != E_OK) {
    356                                 delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
     435                                delete_id(udp6_cepid_table, udp6_cepid_table_count, cepid);
    357436                                return -ENOMEM;
    358437                        }
     
    407486
    408487                struct sockaddr_in6 *laddr = &socket->laddr6;
    409                 T_TCP6_CREP crep = { 0, {ntohl(laddr->sin6_addr.__in6_union.__s6_addr), ntohs(laddr->sin6_port)} };
     488                T_TCP6_CREP crep = { 0, { { ntohl(laddr->sin6_addr.__in6_union.__s6_addr) }, ntohs(laddr->sin6_port)} };
    410489                ret = tcp6_cre_rep(repid, &crep);
    411490                if (ret != E_OK) {
     
    423502}
    424503
    425 int shell_connect(int fd, const struct sockaddr *addr, socklen_t len)
     504int shell_connect(int fd, const struct sockaddr *addr, socklen_t alen)
    426505{
    427506        SOCKET *fp = fd_to_fp(fd);
     
    435514        switch (socket->family) {
    436515        case AF_INET: {
    437                 if (len < 8) {
     516                if (alen < sizeof(struct sockaddr_in)) {
    438517                        return -EINVAL;
    439518                }
     
    455534                        }
    456535                        fp->handle = cepid;
     536                        fp->writable = 1;
    457537                        socket->cepid = cepid;
    458538                }
    459539                struct sockaddr_in *laddr = &socket->laddr4;
    460540                struct sockaddr_in *raddr = &socket->raddr4;
    461                 memset(raddr, 0, sizeof(*raddr));
    462                 memcpy(raddr, addr, len);
     541                memcpy(raddr, addr, sizeof(struct sockaddr_in));
    463542                T_IPV4EP lep = { ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port) };
    464543                T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
    465544                ret = tcp_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
    466                 if (ret < 0) {
     545                if (ret == E_TMOUT) {
     546                        return -ETIMEDOUT;
     547                }
     548                else if (ret < 0) {
    467549                        return -EHOSTUNREACH;
    468550                }
     
    470552        }
    471553        case AF_INET6: {
    472                 if (len < 20) {
     554                if (alen < sizeof(struct sockaddr_in6)) {
    473555                        return -EINVAL;
    474556                }
     
    490572                        }
    491573                        fp->handle = cepid;
     574                        fp->writable = 1;
    492575                        socket->cepid = cepid;
    493576                }
    494577                struct sockaddr_in6 *laddr = &socket->laddr6;
    495578                struct sockaddr_in6 *raddr = &socket->raddr6;
    496                 memset(raddr, 0, sizeof(*raddr));
    497                 memcpy(raddr, addr, len);
    498                 T_IPV6EP lep = { ntohl(laddr->sin6_addr.__in6_union.__s6_addr), ntohs(laddr->sin6_port) };
    499                 T_IPV6EP rep = { ntohl(raddr->sin6_addr.__in6_union.__s6_addr), ntohs(raddr->sin6_port) };
     579                memcpy(raddr, addr, sizeof(struct sockaddr_in6));
     580                T_IPV6EP lep;
     581                addrcpy(&lep.ipaddr, &laddr->sin6_addr, 16);
     582                lep.portno = ntohs(laddr->sin6_port);
     583                T_IPV6EP rep;
     584                addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
     585                rep.portno = ntohs(raddr->sin6_port);
    500586                ret = tcp6_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
    501                 if (ret < 0) {
     587                if (ret == E_TMOUT) {
     588                        return -ETIMEDOUT;
     589                }
     590                else if (ret < 0) {
    502591                        return -EHOSTUNREACH;
    503592                }
     
    508597        }
    509598
     599        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
     600
    510601        return 0;
    511602}
    512603
    513 int shell_accept(int fd, struct sockaddr *__restrict addr, socklen_t *__restrict len)
     604int shell_accept(int fd, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
    514605{
    515606        SOCKET *lfp = fd_to_fp(fd);
     
    550641                        }
    551642                        fp->handle = cepid;
     643                        fp->writable = 1;
    552644                        socket->cepid = cepid;
    553645                }
     
    555647                        cepid = ((socket_t *)lfp->exinf)->cepid;
    556648                        fp->handle = cepid;
     649                        fp->writable = 1;
    557650                        lfp->handle = tmax_tcp_cepid + ((socket_t *)lfp->exinf)->repid;
    558651                        ((socket_t *)lfp->exinf)->cepid = 0;
     
    566659                }
    567660                struct sockaddr_in *raddr = &socket->raddr4;
    568                 memset(raddr, 0, sizeof(*raddr));
     661                memset(raddr, 0, sizeof(struct sockaddr_in));
    569662                raddr->sin_family = AF_INET;
    570663                raddr->sin_port = htons(rep.portno);
    571664                raddr->sin_addr.s_addr = htonl(rep.ipaddr);
    572665
    573                 if (addr != NULL && len != NULL) {
    574                         int sz = *len;
    575                         if (sz < 8) {
     666                if (addr != NULL && alen != NULL) {
     667                        int sz = *alen;
     668                        if (sz < sizeof(struct sockaddr_in)) {
    576669                                return -EINVAL;
    577670                        }
    578671                        struct sockaddr_in *raddr = &socket->raddr4;
    579                         if (sz > sizeof(*raddr))
    580                                 sz = sizeof(*raddr);
     672                        if (sz > sizeof(struct sockaddr_in))
     673                                sz = sizeof(struct sockaddr_in);
    581674                        memcpy(addr, raddr, sz);
    582                         *len = sizeof(*raddr);
     675                        *alen = sz;
    583676                }
    584677                break;
     
    603696                        }
    604697                        fp->handle = cepid;
     698                        fp->writable = 1;
    605699                        socket->cepid = cepid;
    606700                }
     
    608702                        cepid = ((socket_t *)lfp->exinf)->cepid;
    609703                        fp->handle = cepid;
     704                        fp->writable = 1;
    610705                        lfp->handle = tmax_tcp6_cepid + ((socket_t *)lfp->exinf)->repid;
    611706                        ((socket_t *)lfp->exinf)->cepid = 0;
     
    613708                        ((socket_t *)lfp->exinf)->buf = 0;
    614709                }
    615                 T_IPV6EP rep = { 0, 0 };
     710                T_IPV6EP rep = { { 0 }, 0 };
    616711                ret = tcp6_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
    617712                if (ret < 0) {
     
    619714                }
    620715                struct sockaddr_in6 *raddr = &socket->raddr6;
    621                 memset(raddr, 0, sizeof(*raddr));
     716                memset(raddr, 0, sizeof(struct sockaddr_in6));
    622717                raddr->sin6_family = AF_INET;
    623718                raddr->sin6_port = htons(rep.portno);
    624 #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
    625                 memcpy(raddr->sin6_addr.__in6_union.__s6_addr, rep.ipaddr.__u6_addr.__u6_addr8, 16);
    626 #else
    627                 for (int i = 0; i < 16; i++)
    628                         raddr->sin6_addr.__in6_union.__s6_addr[i] = rep.ipaddr.__u6_addr.__u6_addr8[i];
    629 #endif
    630 
    631                 if (addr != NULL && len != NULL) {
    632                         int sz = *len;
    633                         if (sz < 8) {
     719                addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
     720
     721                if (addr != NULL && alen != NULL) {
     722                        int sz = *alen;
     723                        if (sz < sizeof(struct sockaddr_in6)) {
    634724                                return -EINVAL;
    635725                        }
    636726                        struct sockaddr_in6 *raddr = &socket->raddr6;
    637                         if (sz > sizeof(*raddr))
    638                                 sz = sizeof(*raddr);
     727                        if (sz > sizeof(struct sockaddr_in6))
     728                                sz = sizeof(struct sockaddr_in6);
    639729                        memcpy(addr, raddr, sz);
    640                         *len = sizeof(*raddr);
     730                        *alen = sz;
    641731                }
    642732                break;
     
    645735                return -ENOPROTOOPT;
    646736        }
     737
     738        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
    647739
    648740        return fp->fd;
     
    673765                        }
    674766                        else {
     767                                int temp = len;
    675768                                for (;;) {
    676769                                        ret = tcp_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
    677770                                        if (ret < 0) {
    678                                                 if (ret == E_TMOUT)
    679                                                         return -ETIME;
    680                                                 return -ECOMM;
     771                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    681772                                        }
    682773                                        len -= ret;
     
    685776                                        buf = (const void *)&((uint8_t *)buf)[ret];
    686777                                }
     778                                ret = temp;
    687779                        }
    688780                        break;
    689781                }
    690782                case SOCK_DGRAM: {
    691                         int sz = alen;
    692                         if ((addr == NULL) || (sz < 8)) {
     783                        if ((addr == NULL) || (alen < sizeof(struct sockaddr_in))) {
    693784                                return -EINVAL;
    694785                        }
    695786                        struct sockaddr_in *raddr = &socket->raddr4;
    696                         memset(raddr, 0, sizeof(*raddr));
    697                         memcpy(raddr, addr, sz);
     787                        memcpy(raddr, addr, sizeof(struct sockaddr_in));
    698788                        T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
    699789                        ret = udp_snd_dat(socket->cepid, &rep, (void *)buf, len,
    700790                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
    701791                        if (ret < 0) {
    702                                 return (ret == E_TMOUT) ? -ETIME : -ECOMM;
     792                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    703793                        }
    704794                        break;
     
    721811                        }
    722812                        else {
     813                                int temp = len;
    723814                                for (;;) {
    724815                                        ret = tcp6_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
    725816                                        if (ret < 0) {
    726                                                 if (ret == E_TMOUT)
    727                                                         return -ETIME;
    728                                                 return -ECOMM;
     817                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    729818                                        }
    730819                                        len -= ret;
     
    732821                                                break;
    733822                                        buf = (const void *)&((uint8_t *)buf)[ret];
    734                                 }
     823                                }
     824                                ret = temp;
    735825                        }
    736826                        break;
    737827                }
    738828                case SOCK_DGRAM: {
    739                         int sz = alen;
    740                         if ((addr == NULL) || (sz < 8)) {
     829                        if ((addr == NULL) || (alen < sizeof(struct sockaddr_in6))) {
    741830                                return -EINVAL;
    742831                        }
    743832                        struct sockaddr_in6 *raddr = &socket->raddr6;
    744                         memset(raddr, 0, sizeof(*raddr));
    745                         memcpy(raddr, addr, sz);
    746                         T_IPV6EP rep = { ntohl(raddr->sin6_addr.__in6_union.__s6_addr), ntohs(raddr->sin6_port) };
     833                        memcpy(raddr, addr, sizeof(struct sockaddr_in6));
     834                        T_IPV6EP rep;
     835                        addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
     836                        rep.portno = ntohs(raddr->sin6_port);
    747837                        ret = udp6_snd_dat(socket->cepid, &rep, (void *)buf, len,
    748838                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
    749839                        if (ret < 0) {
    750                                 return (ret == E_TMOUT) ? -ETIME : -ECOMM;
     840                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    751841                        }
    752842                        break;
     
    758848                return -ENOPROTOOPT;
    759849        }
     850
     851        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
    760852
    761853        return ret;
     
    800892                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
    801893                                        }
    802                                         ret = tcp_rcv_buf(socket->cepid, &socket->input, TMO_FEVR);
     894                                        ret = tcp_rcv_buf(socket->cepid, &socket->input,
     895                                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
    803896                                        if (ret < 0) {
    804                                                 syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
    805                                                 return -ECOMM;
     897                                                if ((socket->flags & O_NONBLOCK) == 0)
     898                                                        syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
     899                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    806900                                        }
    807901                                        rsz = ret;
     
    858952                                        if ((socket->flags & O_NONBLOCK) == 0)
    859953                                                syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
    860                                         return (ret == E_TMOUT) ? -ETIME : -ECOMM;
     954                                        return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    861955                                }
    862956                                rsz = ret;
     
    867961                                        }
    868962                                        int sz = *alen;
    869                                         memset(raddr, 0, sizeof(socket->raddr4));
     963                                        memset(raddr, 0, sizeof(struct sockaddr_in));
    870964                                        raddr->sin_family = AF_INET;
    871965                                        raddr->sin_port = htons(rep.portno);
    872966                                        raddr->sin_addr.s_addr = htonl(rep.ipaddr);
    873                                         if (sz > sizeof(socket->raddr4))
    874                                                 sz = sizeof(socket->raddr4);
     967                                        if (sz > sizeof(struct sockaddr_in))
     968                                                sz = sizeof(struct sockaddr_in);
    875969                                        memcpy(addr, raddr, sz);
    876970                                        *alen = sz;
     
    889983                                if ((addr != NULL) && (alen != NULL)) {
    890984                                        int sz = *alen;
    891                                         if (sz > sizeof(socket->raddr4))
    892                                                 sz = sizeof(socket->raddr4);
     985                                        if (sz > sizeof(struct sockaddr_in))
     986                                                sz = sizeof(struct sockaddr_in);
    893987                                        memcpy(addr, raddr, sz);
    894988                                        *alen = sz;
     
    9341028                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
    9351029                                        }
    936                                         ret = tcp6_rcv_buf(socket->cepid, &socket->input, TMO_FEVR);
     1030                                        ret = tcp6_rcv_buf(socket->cepid, &socket->input,
     1031                                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
    9371032                                        if (ret < 0) {
    938                                                 syslog(LOG_ERROR, "tcp6_rcv_buf => %d", ret);
    939                                                 return -ECOMM;
     1033                                                if ((socket->flags & O_NONBLOCK) == 0)
     1034                                                        syslog(LOG_ERROR, "tcp6_rcv_buf => %d", ret);
     1035                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    9401036                                        }
    9411037                                        rsz = ret;
     
    9861082                                }
    9871083
    988                                 T_IPV6EP rep = { 0, 0 };
     1084                                T_IPV6EP rep = { { 0 }, 0 };
    9891085                                ret = udp6_rcv_dat(socket->cepid, &rep, buf, len,
    9901086                                        (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     
    9921088                                        if ((socket->flags & O_NONBLOCK) == 0)
    9931089                                                syslog(LOG_ERROR, "udp6_rcv_buf => %d", ret);
    994                                         return (ret == E_TMOUT) ? -ETIME : -ECOMM;
     1090                                        return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
    9951091                                }
    9961092                                rsz = ret;
     
    10011097                                        }
    10021098                                        int sz = *alen;
    1003                                         memset(raddr, 0, sizeof(socket->raddr6));
     1099                                        memset(raddr, 0, sizeof(struct sockaddr_in6));
    10041100                                        raddr->sin6_family = AF_INET;
    10051101                                        raddr->sin6_port = htons(rep.portno);
    1006 #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
    1007                                         memcpy(raddr->sin6_addr.__in6_union.__s6_addr, rep.ipaddr.__u6_addr.__u6_addr8, 16);
    1008 #else
    1009                                         for (int i = 0; i < 16; i++)
    1010                                                 raddr->sin6_addr.__in6_union.__s6_addr[i] = rep.ipaddr.__u6_addr.__u6_addr8[i];
    1011 #endif
    1012                                         if (sz > sizeof(socket->raddr6))
    1013                                                 sz = sizeof(socket->raddr6);
     1102                                        addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
     1103                                        if (sz > sizeof(struct sockaddr_in6))
     1104                                                sz = sizeof(struct sockaddr_in6);
    10141105                                        memcpy(addr, raddr, sz);
    10151106                                        *alen = sz;
     
    10281119                                if ((addr != NULL) && (alen != NULL)) {
    10291120                                        int sz = *alen;
    1030                                         if (sz > sizeof(socket->raddr6))
    1031                                                 sz = sizeof(socket->raddr6);
     1121                                        if (sz > sizeof(struct sockaddr_in6))
     1122                                                sz = sizeof(struct sockaddr_in6);
    10321123                                        memcpy(addr, raddr, sz);
    10331124                                        *alen = sz;
     
    10541145                return -ENOPROTOOPT;
    10551146        }
     1147
     1148        if (fp->readevt_w != fp->readevt_r) fp->readevt_r++;
    10561149
    10571150        return ret;
     
    14271520                struct sockaddr_in6 laddr;
    14281521                laddr.sin6_family = AF_INET;
    1429 #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
    1430                 memcpy(laddr.sin6_addr.__in6_union.__s6_addr, laddr6->__u6_addr.__u6_addr8, 16);
    1431 #else
    1432                 for (int i = 0; i < 16; i++)
    1433                         laddr.sin6_addr.__in6_union.__s6_addr[i] = laddr6->__u6_addr.__u6_addr8[i];
    1434 #endif
     1522                addrcpy(&laddr.sin6_addr, laddr6, 16);
    14351523                laddr.sin6_port = socket->laddr6.sin6_port;
    14361524                *len = sizeof(struct sockaddr_in6);
     
    14651553                        delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
    14661554                        if ((ret < 0) || (ret2 < 0)) {
    1467                                 return (ret == E_TMOUT) ? -ETIME : -EINVAL;
     1555                                return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
    14681556                        }
    14691557                }
     
    14941582                        delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
    14951583                        if ((ret < 0) || (ret2 < 0)) {
    1496                                 return (ret == E_TMOUT) ? -ETIME : -EINVAL;
     1584                                return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
    14971585                        }
    14981586                }
     
    15351623int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    15361624{
     1625        socket_t *socket = (socket_t *)fp->exinf;
     1626
     1627        switch (req) {
     1628        case F_GETFL:
     1629                return socket->flags;
     1630        case F_SETFL:
     1631                socket->flags = (unsigned int)arg;
     1632                return 0;
     1633        }
     1634
    15371635        return -EINVAL;
    15381636}
     
    15711669}
    15721670
     1671bool_t tcp_fd_writable(struct SHELL_FILE *fp)
     1672{
     1673        //socket_t *socket = (socket_t *)fp->exinf;
     1674
     1675        return /*fp->writable &&*/ (fp->writeevt_w == fp->writeevt_r);
     1676}
     1677
    15731678void tcp_fd_delete(struct SHELL_FILE *fp)
    15741679{
     
    15901695                return E_PAR;
    15911696
     1697        socket_t *socket = (socket_t *)fp->exinf;
    15921698        int fd = fp->fd;
    15931699        FD_SET(fd, (fd_set *)&flgptn);
     
    16031709                        syslog(LOG_ERROR, "wai_sem => %d", ret);
    16041710                }
    1605                 socket_t *socket = (socket_t *)fp->exinf;
    16061711                socket->len += len;
    16071712                ret = sig_sem(SEM_FILEDESC);
     
    16701775                return E_PAR;
    16711776
     1777        socket_t *socket = (socket_t *)fp->exinf;
    16721778        int fd = fp->fd;
    16731779        FD_SET(fd, (fd_set *)&flgptn);
     
    16831789                        syslog(LOG_ERROR, "wai_sem => %d", ret);
    16841790                }
    1685                 socket_t *socket = (socket_t *)fp->exinf;
    16861791                socket->len += len;
    16871792                ret = sig_sem(SEM_FILEDESC);
     
    18061911}
    18071912
     1913bool_t udp_fd_writable(struct SHELL_FILE *fp)
     1914{
     1915        //socket_t *socket = (socket_t *)fp->exinf;
     1916
     1917        return fp->writable && (fp->writeevt_w == fp->writeevt_r);
     1918}
     1919
    18081920void udp_fd_delete(struct SHELL_FILE *fp)
    18091921{
     
    18491961                socket->input = udppara->input;
    18501962                socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
    1851                 memset(&socket->raddr4, 0, sizeof(socket->raddr4));
     1963                memset(&socket->raddr4, 0, sizeof(struct sockaddr_in));
    18521964                socket->raddr4.sin_family = AF_INET;
    18531965                socket->raddr4.sin_port = htons(udppara->rep4.portno);
     
    19322044                socket->input = udppara->input;
    19332045                socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
    1934                 memset(&socket->raddr6, 0, sizeof(socket->raddr6));
     2046                memset(&socket->raddr6, 0, sizeof(struct sockaddr_in6));
    19352047                socket->raddr6.sin6_family = AF_INET;
    19362048                socket->raddr6.sin6_port = htons(udppara->rep6.portno);
    1937 #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
    1938                 memcpy(socket->raddr6.sin6_addr.__in6_union.__s6_addr, udppara->rep6.ipaddr.__u6_addr.__u6_addr8, 16);
    1939 #else
    1940                 for (int i = 0; i < 16; i++)
    1941                         socket->raddr6.sin6_addr.__in6_union.__s6_addr[i] = udppara->rep6.ipaddr.__u6_addr.__u6_addr8[i];
    1942 #endif
     2049                addrcpy(&socket->raddr6.sin6_addr, &udppara->rep6.ipaddr, 16);
    19432050                udppara->input->flags |= NB_FLG_NOREL_IFOUT;
    19442051                ret = sig_sem(SEM_FILEDESC);
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/syscall.c

    r374 r387  
    336336}
    337337
    338 long SYS_rt_sigaction(long a, long b, long c) {
    339         return shell_sigaction((int)a, (const struct sigaction *)b, (struct sigaction *)c);
     338long SYS_rt_sigaction(long a, long b, long c, long d) {
     339        return shell_sigaction((int)a, (const struct k_sigaction *)b, (struct k_sigaction *)c, (size_t)d);
    340340}
    341341
     
    429429}
    430430
     431long SYS_nanosleep(long a, long b)
     432{
     433        return shell_nanosleep((const struct timespec *)a, (struct timespec *)b);
     434}
     435
    431436long SYS_dup(long a) {
    432437        //int dup(int fd)
     
    532537{
    533538        return no_implement("tgkill\n");
    534 }
    535 
    536 long SYS_nanosleep(long a, long b) {
    537         //int nanosleep(const struct timespec *req, struct timespec *rem)
    538         return no_implement("nanosleep\n");
    539539}
    540540
Note: See TracChangeset for help on using the changeset viewer.