Changeset 441 for EcnlProtoTool


Ignore:
Timestamp:
Jul 10, 2020, 9:09:25 PM (4 years ago)
Author:
coas-nagasima
Message:

NTShellタスクを更新

Location:
EcnlProtoTool/trunk
Files:
13 added
1 deleted
29 edited

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/asp3_dcre/Debug/Makefile

    r429 r441  
    246246#  システムサービスに関する定義
    247247#
    248 ifdef TCINSTALL
    249 GCC_PATH = $(TCINSTALL)
    250 else
    251 GCC_PATH = $(shell which $(CC))/../../
    252 endif
    253 MALLOC_LIB = "${GCC_PATH}arm-none-eabi/lib/thumb/v7-ar/fpv3/hard/libc.a"
    254 MALLOC_OBJS = lib_a-reent.o lib_a-impure.o lib_a-malloc.o lib_a-mallocr.o lib_a-calloc.o lib_a-callocr.o lib_a-realloc.o lib_a-reallocr.o lib_a-sbrkr.o lib_a-freer.o
    255 
    256248SYSSVC_DIRS := $(TECSGENDIR) $(SRCDIR)/tecs_kernel \
    257249                                $(SYSSVC_DIRS) $(SRCDIR)/syssvc
     
    380372CFG_COBJS      := $(addprefix $(OBJDIR)/, $(CFG_COBJS))
    381373CFG_DMY        := $(addprefix $(OBJDIR)/, $(CFG_DMY))
    382 MALLOC_OBJS    := $(addprefix $(OBJDIR)/, $(MALLOC_OBJS))
    383374
    384375#
     
    387378APPL_OBJS = $(APPL_ASMOBJS) $(APPL_COBJS) $(APPL_CXXOBJS)
    388379SYSSVC_OBJS = $(SYSSVC_ASMOBJS) $(SYSSVC_COBJS) $(HIDDEN_OBJS)
    389 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) $(MALLOC_OBJS)
     380KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS)
    390381CFG_OBJS = $(CFG_ASMOBJS) $(CFG_COBJS)
    391382ALL_OBJS = $(START_OBJS) $(APPL_OBJS) $(SYSSVC_OBJS) $(CFG_OBJS) \
     
    554545        rm -f libsyssvc.a
    555546endif
    556         rm -f $(MALLOC_OBJS)
    557547
    558548.PHONY: cleankernel
     
    678668        $(CC) -c -o $@ -MD -MP -MF $(DEPDIR)/$*.d $(CFLAGS) $(APPL_CFLAGS) $<
    679669
    680 $(MALLOC_OBJS): $(OBJDIR)/%.o:
    681         $(AR) x $(MALLOC_LIB) $(notdir $@)
    682         mv $(notdir $@) $@
    683 
    684670#
    685671#  デフォルトコンパイルルールを上書き
  • EcnlProtoTool/trunk/ntshell/Debug/Makefile

    r429 r441  
    199199        CDEFS := -DTOPPERS_OMIT_TECS $(CDEFS)
    200200endif
    201 CDEFS := $(CDEFS) -DFD_SETSIZE=32
     201CDEFS := $(CDEFS) -DFD_SETSIZE=32 -DDHCP4_CLI_CFG -DUSE_PING -DUSE_RESOLVER -DUSE_NETAPP_SUBR
    202202INCLUDES := -I. -I$(SRCDIR)/include $(INCLUDES) -I$(SRCDIR)
    203203LDFLAGS := $(LDFLAGS) -L.
     
    213213APPL_CDL = main.cdl
    214214
    215 APPL_DIRS := $(APPLDIRS) $(SRCDIR)/library
     215APPL_DIRS := $(APPLDIRS) $(SRCDIR)/library $(SRCDIR)/$(TINETDIR)/netapp
    216216APPL_ASMOBJS :=
    217217ifdef USE_CXX
    218218        APPL_CXXOBJS := main.o
    219         APPL_COBJS := fdtable.o io_stub.o socket_stub.o ffarch.o fftime.o tlsf.o diskio.o ff.o sdfs.o ccsbcs.o base64.o http-strings.o http_parser.o httpd.o httpd-fs.o sha1.o websocket.o websocket_fbs.o ntshell.o text_editor.o text_history.o usrcmd.o vtrecv.o vtsend.o ntlibc.o ntstdio.o ntopt.o syscall.o mbedcall.o
     219        APPL_COBJS := fdtable.o stdio_stub.o pipe_stub.o io_stub.o socket_stub.o tlsf.o ffarch.o diskio.o ff.o sdfs.o ccsbcs.o base64.o http-strings.o http_parser.o httpd.o httpd-fs.o sha1.o websocket.o websocket_fbs.o ntshell.o text_editor.o text_history.o usrcmd.o vtrecv.o vtsend.o ntlibc.o ntstdio.o ntopt.o syscall.o mbedcall.o ping.o ping6.o resolver.o dhcp4_cli.o netapp_subr.o ntp_cli.o net_misc.o netcmd.o
    220220else
    221         APPL_COBJS := main.o fdtable.o io_stub.o socket_stub.o ffarch.o fftime.o tlsf.o diskio.o ff.o sdfs.o ccsbcs.o base64.o http-strings.o http_parser.o httpd.o httpd-fs.o sha1.o websocket.o websocket_fbs.o ntshell.o text_editor.o text_history.o usrcmd.o vtrecv.o vtsend.o ntlibc.o ntstdio.o ntopt.o syscall.o mbedcall.o
     221        APPL_COBJS := main.o fdtable.o stdio_stub.o pipe_stub.o io_stub.o socket_stub.o tlsf.o ffarch.o diskio.o ff.o sdfs.o ccsbcs.o base64.o http-strings.o http_parser.o httpd.o httpd-fs.o sha1.o websocket.o websocket_fbs.o ntshell.o text_editor.o text_history.o usrcmd.o vtrecv.o vtsend.o ntlibc.o ntstdio.o ntopt.o syscall.o mbedcall.o ping.o ping6.o resolver.o dhcp4_cli.o netapp_subr.o ntp_cli.o net_misc.o netcmd.o
    222222endif
    223223APPL_COBJS := $(APPL_COBJS) log_output.o vasyslog.o t_perror.o strerror.o mbed_stub.o
     
    263263#  システムサービスに関する定義
    264264#
    265 ifdef TCINSTALL
    266 GCC_PATH = $(TCINSTALL)
    267 else
    268 GCC_PATH = $(shell which $(CC))/../../
    269 endif
    270 MALLOC_LIB = "${GCC_PATH}arm-none-eabi/lib/thumb/v7-ar/fpv3/hard/libc.a"
    271 MALLOC_OBJS = lib_a-reent.o lib_a-impure.o lib_a-malloc.o lib_a-mallocr.o lib_a-calloc.o lib_a-callocr.o lib_a-realloc.o lib_a-reallocr.o lib_a-sbrkr.o lib_a-freer.o
    272 
    273265SYSSVC_DIRS := $(TECSGENDIR) $(SRCDIR)/tecs_kernel \
    274266                                $(SYSSVC_DIRS) $(SRCDIR)/syssvc
     
    398390CFG_COBJS      := $(addprefix $(OBJDIR)/, $(CFG_COBJS))
    399391CFG_DMY        := $(addprefix $(OBJDIR)/, $(CFG_DMY))
    400 MALLOC_OBJS    := $(addprefix $(OBJDIR)/, $(MALLOC_OBJS))
    401392
    402393#
     
    405396APPL_OBJS = $(APPL_ASMOBJS) $(APPL_COBJS) $(APPL_CXXOBJS) $(APPL_BINOBJS)
    406397SYSSVC_OBJS = $(SYSSVC_ASMOBJS) $(SYSSVC_COBJS) $(HIDDEN_OBJS)
    407 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) $(MALLOC_OBJS)
     398KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS)
    408399CFG_OBJS = $(CFG_ASMOBJS) $(CFG_COBJS)
    409400ALL_OBJS = $(START_OBJS) $(APPL_OBJS) $(SYSSVC_OBJS) $(CFG_OBJS) \
     
    573564        rm -f libsyssvc.a
    574565endif
    575         rm -f $(MALLOC_OBJS)
    576566
    577567.PHONY: cleankernel
     
    701691        $(OBJCOPY) -B arm -I binary -O elf32-littlearm --rename-section .data=.rodata $< $(OBJDIR)/$*.o
    702692
    703 $(MALLOC_OBJS): $(OBJDIR)/%.o:
    704         $(AR) x $(MALLOC_LIB) $(notdir $@)
    705         mv $(notdir $@) $@
    706 
    707693#
    708694#  デフォルトコンパイルルールを上書き
  • EcnlProtoTool/trunk/ntshell/fatfs/diskio.h

    r434 r441  
    4343void disk_timerproc(void);
    4444
    45 #define SZ_RAMDISK      1792    /* Size of RAM disk [kB] */
     45#define SZ_RAMDISK      1536    /* Size of RAM disk [kB] */
    4646#define SS_RAMDISK      512             /* Sector size of RAM disk [byte] */
    4747
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c

    r435 r441  
    4242#include <kernel.h>
    4343#include <stdlib.h>
    44 //#include <string.h>
     44#include <string.h>
    4545#include "t_stdlib.h"
    4646#include "syssvc/serial.h"
    4747#include "syssvc/syslog.h"
     48#include "target_syssvc.h"
    4849#include "kernel_cfg.h"
    4950#include "diskio.h"
    5051#include "sdfs.h"
    5152#include "ff.h"
    52 #include "tlsf.h"
    53 #include "util/ntstdio.h"
     53#include <stdio.h>
     54#include "hal/rtc_api.h"
     55#include "ffarch.h"
    5456
    5557#define SD_CARD_LOG_LEVEL LOG_INFO
    5658
    57 #define SIO_PORTID 1
    58 extern ntstdio_t ntstdio;
    59 
    60 static tlsf_t sys_tlsf;
    61 static pool_t sys_pool;
    62 
    63 uint8_t shell_heap[64 * 1024];
     59enum ffarch_state_t {
     60        FFS_IDLE,
     61        FFS_RETRY_WAIT,
     62};
     63
     64enum ffarch_state_t ffarch_state;
     65volatile int ffarch_timer;
     66int ffarch_retry_count;
     67uint8_t sdfs_new_status, sdfs_prev_status;
     68
     69static void ffarch_initialize();
     70
     71/*
     72 * FatFSタスク
     73 */
     74void ffarch_task(intptr_t exinf)
     75{
     76        ER ret, ret2;
     77        SYSTIM prev, now;
     78        int timer;
     79
     80        ffarch_initialize();
     81
     82        ret2 = get_tim(&now);
     83        if (ret2 != E_OK){
     84                syslog(LOG_NOTICE, "[ffarch] get_tim error: %s",
     85                        itron_strerror(ret2));
     86                return;
     87        }
     88
     89        for(;;){
     90                prev = now;
     91
     92                /* タイマー取得 */
     93                timer = ffarch_get_timer();
     94
     95                /* 待ち */
     96                ret = tslp_tsk(timer);
     97                if ((ret != E_OK) && (ret != E_TMOUT)) {
     98                        syslog(LOG_NOTICE, "[ffarch] tslp_tsk error: %s %d",
     99                                itron_strerror(ret), timer);
     100                        break;
     101                }
     102
     103                ret2 = get_tim(&now);
     104                if (ret2 != E_OK) {
     105                        syslog(LOG_NOTICE, "[ffarch] get_tim error: %s",
     106                                itron_strerror(ret2));
     107                        break;
     108                }
     109
     110                /* 時間経過 */
     111                int elapse = now - prev;
     112                ffarch_progress(elapse);
     113
     114                /* タイムアウト処理 */
     115                ffarch_timeout();
     116        }
     117}
     118
    64119FATFS RomDisk;
    65120
     
    75130SD Sd;
    76131
    77 void sys_init(void);
    78132bool_t romdisk_init();
    79133bool_t SD_begin();
    80134
    81 int mruby_arduino_init()
    82 {
    83         int result = -1;
    84 
    85         sys_init();
    86 
     135void ffarch_init(void)
     136{
    87137        /* SD_CD */
    88138        gpio_init_in(&ins, P7_8);
     
    91141        sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
    92142
    93         result = SD_begin() ? 0 : -1;
    94         if (result == 0) {
    95                 ntstdio_printf(&ntstdio, "SD card (1:) OK!\n");
     143        ffarch_state = FFS_IDLE;
     144        ffarch_timer = 0;
     145        ffarch_retry_count = 3;
     146        sdfs_new_status = 0;
     147        sdfs_prev_status = STA_NODISK;
     148
     149        ffarch_timeout();
     150
     151        act_tsk(FFARCH_TASK);
     152
     153        if (romdisk_init()) {
     154                syslog(LOG_NOTICE, "ROM disk (0:) OK!");
    96155        }
    97156        else {
    98                 ntstdio_printf(&ntstdio, "SD card (1:) NG!\n");
    99         }
     157                syslog(LOG_NOTICE, "ROM disk (0:) NG!");
     158        }
     159}
     160
     161void ffarch_initialize()
     162{
    100163        sta_cyc(SDFS_CYC);
    101 
    102         if (romdisk_init())
    103                 result = 0;
    104 
    105         if (result == 0) {
    106                 ntstdio_printf(&ntstdio, "ROM disk (0:) OK!\n");
    107         }
    108         else {
    109                 ntstdio_printf(&ntstdio, "ROM disk (0:) NG!\n");
    110         }
    111 
    112         /* uploadディレクトリを作成しておく */
    113         f_mkdir("1:/upload");
    114 
    115         serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
    116 
    117         return result;
    118 }
    119 
    120 void sys_init(void)
    121 {
    122         sys_tlsf = tlsf_create(&shell_heap);
    123         if (sys_tlsf == NULL)
    124                 return;
    125 
    126         sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&shell_heap) + tlsf_size(), sizeof(shell_heap) - tlsf_size());
    127 }
    128 
    129 void sys_fini(void)
    130 {
    131         tlsf_destroy(sys_tlsf);
    132 }
    133 
    134 void *sys_malloc(size_t size)
    135 {
    136         return tlsf_malloc(sys_tlsf, size);
    137 }
    138 
    139 void *sys_calloc(size_t size, size_t count)
    140 {
    141         void *result = tlsf_malloc(sys_tlsf, count * size);
    142         memset(result, 0, count * size);
    143         return result;
    144 }
    145 
    146 void *sys_realloc(void *ptr, size_t size)
    147 {
    148         return tlsf_realloc(sys_tlsf, ptr, size);
    149 }
    150 
    151 void sys_free(void *ptr)
    152 {
    153         tlsf_free(sys_tlsf, ptr);
    154164}
    155165
     
    219229                return ramdisk_get_status();
    220230        case 1:
    221                 return sdfs_status(&sdfs);
     231                return sdfs._is_initialized;
    222232        }
    223233        return STA_NOINIT;
     
    246256}
    247257
     258DRESULT sdfs_ioctl(BYTE ctrl, void *buff);
     259
    248260DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
    249261{
     
    252264                return RES_PARERR; /* ramdisk_ioctl(cmd, buff); */
    253265        case 1:
    254                 switch(cmd) {
    255                 case CTRL_SYNC:
    256                         return sdfs_sync(&sdfs);
    257                 case GET_SECTOR_COUNT:
    258                         return sdfs_sectors(&sdfs);
    259                 case GET_BLOCK_SIZE:
    260                         *((DWORD*)buff) = 1; // default when not known
    261                         return RES_OK;
    262                 }
     266                return sdfs_ioctl(cmd, buff);
    263267        }
    264268        return RES_PARERR;
    265269}
    266270
     271DRESULT sdfs_ioctl(BYTE ctrl, void *buff)
     272{
     273        DRESULT res;
     274        uint32_t *dp = (uint32_t *)buff;
     275        uint8_t *ptr = (uint8_t *)buff;
     276
     277        if (sdfs._is_initialized & STA_NOINIT)
     278                return RES_NOTRDY;
     279
     280        res = RES_ERROR;
     281
     282        switch (ctrl) {
     283        case CTRL_SYNC:
     284                if (sdfs_sync(&sdfs) == 0) {
     285                        res = RES_OK;
     286                }
     287                break;
     288        case GET_SECTOR_SIZE:
     289                *(WORD*)buff = 512;
     290                res = RES_OK;
     291                break;
     292        case GET_SECTOR_COUNT:
     293                *(DWORD*)buff = sdfs._capacity / 512;
     294                res = RES_OK;
     295                break;
     296        case GET_BLOCK_SIZE:
     297                *(DWORD*)buff = sdfs._block_len;
     298                res = RES_OK;
     299                break;
     300        case CTRL_TRIM:
     301                res = sdfs_trim(&sdfs, dp[0], dp[1]);
     302                break;
     303        case MMC_GET_TYPE:
     304                *ptr = sdfs._card_type;
     305                res = RES_OK;
     306                break;
     307        case MMC_GET_CSD:
     308                memcpy(ptr, sdfs._csd, sizeof(sdfs._csd));
     309                res = RES_OK;
     310                break;
     311        case MMC_GET_CID:
     312                memcpy(ptr, sdfs._cid, sizeof(sdfs._cid));
     313                res = RES_OK;
     314                break;
     315        case MMC_GET_OCR:
     316                if (sdfs_get_ocr(&sdfs, buff) == 0) {
     317                        res = RES_OK;
     318                }
     319                break;
     320        case MMC_GET_SDSTAT:
     321                if (sdfs_get_sdstat(&sdfs, buff) == 0) {
     322                        res = RES_OK;
     323                }
     324                break;
     325        default:
     326                res = RES_PARERR;
     327        }
     328
     329        return res;
     330}
     331
    267332void sdfs_cychdr(intptr_t exinf)
    268333{
    269         BYTE s;
    270 
    271         s = sdfs._is_initialized;
    272         if (WP())               /* Write protected */
     334        BYTE s = 0;
     335             
     336        /* Write protected */
     337        if (WP())
    273338                s |= STA_PROTECT;
    274         else            /* Write enabled */
     339        else
    275340                s &= ~STA_PROTECT;
    276         if (gpio_read(&ins))    /* Card is in socket */
     341
     342        /* Card is in socket */
     343        if (gpio_read(&ins))
     344                s |= (STA_NODISK | STA_NOINIT);
     345        else
    277346                s &= ~STA_NODISK;
    278         else            /* Socket empty */
    279                 s |= (STA_NODISK | STA_NOINIT);
    280         sdfs._is_initialized = s;
     347
     348        sdfs_new_status = s;
     349
     350        if ((sdfs_prev_status != sdfs_new_status) && (ffarch_state == FFS_IDLE)) {
     351                ffarch_timer = 0;
     352                wup_tsk(FFARCH_TASK);
     353        }
     354}
     355
     356int ffarch_get_timer(void)
     357{
     358        return ffarch_timer;
     359}
     360
     361void ffarch_progress(int elapse)
     362{
     363        if (ffarch_timer != TMO_FEVR) {
     364                ffarch_timer -= elapse;
     365                if (ffarch_timer < 0) {
     366                        ffarch_timer = 0;
     367                }
     368        }
     369}
     370
     371void ffarch_timeout(void)
     372{
     373        if (ffarch_timer != 0)
     374                return;
     375
     376        switch (ffarch_state) {
     377        case FFS_RETRY_WAIT:
     378                if (ffarch_retry_count == 0) {
     379                        syslog(SD_CARD_LOG_LEVEL, "SD card (1:) initialize tired...");
     380
     381                        ffarch_state = FFS_IDLE;
     382                        ffarch_timer = TMO_FEVR;
     383                        ffarch_retry_count = 3;
     384                        break;
     385                }
     386                ffarch_retry_count--;
     387                //goto case FFS_IDLE;
     388        case FFS_IDLE:
     389                /* SDカードが入れられた場合 */
     390                if ((sdfs._is_initialized & STA_NOINIT)
     391                        || (((sdfs_prev_status & STA_NODISK) != 0) && ((sdfs_new_status & STA_NODISK) == 0))) {
     392                        syslog(SD_CARD_LOG_LEVEL, "SD card initializing ...");
     393
     394                        if (SD_begin()) {
     395                                syslog(SD_CARD_LOG_LEVEL, "SD card (1:) OK!");
     396
     397                                /* uploadディレクトリを作成しておく */
     398                                f_mkdir("1:/upload");
     399
     400                                ffarch_state = FFS_IDLE;
     401                                ffarch_timer = TMO_FEVR;
     402                                ffarch_retry_count = 3;
     403                        }
     404                        else {
     405                                syslog(SD_CARD_LOG_LEVEL, "SD card (1:) NG!");
     406                                ffarch_state = FFS_RETRY_WAIT;
     407                                ffarch_timer = 1000 * 1000;
     408                        }
     409                }
     410                /* SDカードが抜かれた場合 */
     411                else if (((sdfs_prev_status & STA_NODISK) == 0) && ((sdfs_new_status & STA_NODISK) != 0)) {
     412                        syslog(SD_CARD_LOG_LEVEL, "SD card unmount");
     413
     414                        f_mount(&Sd.FatFs, "1:", 0);
     415                        ffarch_state = FFS_IDLE;
     416                        ffarch_timer = TMO_FEVR;
     417                        ffarch_retry_count = 3;
     418                }
     419                if (((sdfs_prev_status ^ sdfs_new_status) & STA_PROTECT) != 0) {
     420                        if ((sdfs_new_status & STA_PROTECT) != 0)
     421                                sdfs._is_initialized |= STA_PROTECT;
     422                        else
     423                                sdfs._is_initialized &= ~STA_PROTECT;
     424                }
     425                sdfs_prev_status = sdfs_new_status;
     426                break;
     427        }
    281428}
    282429
     
    314461void* ff_memalloc (UINT msize)
    315462{
    316         return sys_malloc(msize);
     463        return malloc(msize);
    317464}
    318465
    319466void ff_memfree (void* mblock)
    320467{
    321         sys_free(mblock);
     468        free(mblock);
    322469}
    323470
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.cfg

    r434 r441  
    3838#include "ffarch.h"
    3939
     40CRE_TSK(FFARCH_TASK, { TA_NULL, 0, ffarch_task, FFARCH_PRIORITY, FFARCH_STACK_SIZE, NULL });
    4041CRE_CYC(SDFS_CYC, { TA_NULL, { TNFY_HANDLER, 0, sdfs_cychdr }, 1000000, 0 });
    4142
    42 CRE_SEM(SEM_MALLOC, { TA_TPRI, 1, 1 });
     43CRE_SEM(SEM_FILESYSTEM, { TA_TPRI, 1, 1 });
    4344
  • EcnlProtoTool/trunk/ntshell/fatfs/ffarch.h

    r434 r441  
    3838#define _FFARCH_H_
    3939
    40 int mruby_arduino_init();
     40#define FFARCH_PRIORITY                 5
     41#define FFARCH_STACK_SIZE               512
     42
     43void ffarch_init(void);
     44void ffarch_task(intptr_t exinf);
     45int ffarch_get_timer(void);
     46void ffarch_progress(int elapse);
     47void ffarch_timeout(void);
     48
    4149void sdfs_cychdr(intptr_t exinf);
    4250
  • EcnlProtoTool/trunk/ntshell/ntshell/core/ntlibc.h

    r331 r441  
    5252int ntlibc_tolower(int c);
    5353int ntlibc_atoi(const char *nptr);
    54 unsigned long ntlibc_strtoul(const char *__restrict nptr, char **__restrict endptr, int base);
     54unsigned long ntlibc_strtoul(const char *restrict nptr, char **restrict endptr, int base);
    5555char *ntlibc_strchr(const char *s, int c);
    5656char *ntlibc_utoa(unsigned int value, char *s, int radix);
  • EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c

    r435 r441  
    3333#include "shellif.h"
    3434#include <kernel.h>
    35 //#include <string.h>
    3635#include <t_syslog.h>
    3736#include <t_stdlib.h>
    3837#include <sil.h>
    3938#include "syssvc/syslog.h"
     39#include "target_syssvc.h"
    4040#include "kernel_cfg.h"
    4141#include "main.h"
    42 #include "rza1.h"
    4342#include "ffarch.h"
    4443#include "ff.h"
     
    4847#include "util/ntstdio.h"
    4948
    50 extern ntstdio_t ntstdio;
     49extern ntstdio_t *ntstdio;
    5150extern int ntshell_exit;
    5251
     
    147146}
    148147
    149 int shell_kill(int pid, int sig)
    150 {
    151         asm("bkpt #0");
    152         return -1;
    153 }
    154 
    155 int shell_gettimeofday(struct timeval *tv, void *tzvp)
    156 {
    157         SYSTIM time;
    158         if (!tv) return 0;
    159         get_tim(&time);
    160         tv->tv_sec = time / 1000000;
    161         tv->tv_usec = time - (tv->tv_sec * 1000000);
    162         return 0;
    163 }
    164 
    165148void put_rc(const char *func, FRESULT rc)
    166149{
     
    175158                while (*p++);
    176159        }
    177         ntstdio_printf(&ntstdio, "%s() =>%u FR_%s\n", func, (UINT)rc, p);
     160        ntstdio_printf(ntstdio, "%s() =>%u FR_%s\n", func, (UINT)rc, p);
    178161}
    179162
     
    187170                while (*p++);
    188171        }
    189         ntstdio_printf(&ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);
     172        ntstdio_printf(ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);
    190173}
    191174
     
    210193        for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s;
    211194        for (; s[i] != '/'; i--) if (!i) return (s == _s) ? "." : _s;
    212         for (; s[i] == '/'; i--) if (!i) if (s == _s) return "/"; else break;
     195        for (; s[i] == '/'; i--) if (!i) { if (s == _s) return "/"; else break; }
    213196        s[i + 1] = 0;
    214197        return _s;
     
    237220
    238221        ntlibc_strlcat(path, "\n", sizeof(path));
    239         ntstdio_printf(&ntstdio, path);
     222        ntstdio_printf(ntstdio, path);
    240223
    241224        return 0;
     
    260243
    261244        if (list_option & LS_LONG) {
    262                 ntstdio_printf(&ntstdio, "%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",
     245                ntstdio_printf(ntstdio, "%c%c%c%c%c %04d/%02d/%02d %02d:%02d:%02d ",
    263246                        (fno->fattrib & AM_DIR) ? 'd' : '-',
    264247                        (fno->fattrib & AM_RDO) ? 'r' : '-',
     
    274257
    275258                if (fno->fattrib & AM_DIR) {                    /* It is a directory */
    276                         ntstdio_printf(&ntstdio, "%10S ", " ");
     259                        ntstdio_printf(ntstdio, "%10s ", " ");
    277260                }
    278261                else {
    279                         ntstdio_printf(&ntstdio, "%10d ", fno->fsize);
     262                        ntstdio_printf(ntstdio, "%10lu ", fno->fsize);
    280263                }
    281264        }
    282265
    283266        if (fno->fattrib & AM_DIR) {                    /* It is a directory */
    284                 ntstdio_printf(&ntstdio, "\x1B[32m%s\x1B[0m\n", fn);
     267                ntstdio_printf(ntstdio, "\x1B[32m%s\x1B[0m\n", fn);
    285268        }
    286269        else {
    287                 ntstdio_printf(&ntstdio, "%s\n", fn);
     270                ntstdio_printf(ntstdio, "%s\n", fn);
    288271        }
    289272}
     
    304287        path_backup = ff_memalloc(LFN_BUF_SIZE);
    305288        if (path_backup == NULL) {
    306                 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
     289                ntstdio_printf(ntstdio, "ff_memalloc err.\n");
    307290                return;
    308291        }
     
    312295        lfn = ff_memalloc(LFN_BUF_SIZE);
    313296        if (lfn == NULL) {
    314                 ntstdio_printf(&ntstdio, "ff_memalloc err.\n");
     297                ntstdio_printf(ntstdio, "ff_memalloc err.\n");
    315298                ff_memfree(path_backup);
    316299                return;
     
    340323                        }
    341324
    342                         if ((res = f_chdrive(path_p)) != RES_OK) {
     325                        if ((res = f_chdrive(path_p)) != FR_OK) {
    343326                                put_rc("f_chdrive", res);
    344327                        }
    345328
    346                         if ((res = f_chdir(path_p)) != RES_OK) {
     329                        if ((res = f_chdir(path_p)) != FR_OK) {
    347330                                put_rc("f_chdir", res);
    348331                        }
    349332
    350                         ntstdio_printf(&ntstdio, "\n%s/%s:\n", path_p, fn);
     333                        ntstdio_printf(ntstdio, "\n%s/%s:\n", path_p, fn);
    351334
    352335                        print_ls(fn, NULL, list_option);
    353336
    354                         ntstdio_printf(&ntstdio, "\n");
    355 
    356                         if ((res = f_chdrive(path_backup)) != RES_OK) {
     337                        ntstdio_printf(ntstdio, "\n");
     338
     339                        if ((res = f_chdrive(path_backup)) != FR_OK) {
    357340                                put_rc("f_chdrive", res);
    358341                        }
    359342
    360                         if ((res = f_chdir(path_backup)) != RES_OK) {
     343                        if ((res = f_chdir(path_backup)) != FR_OK) {
    361344                                put_rc("f_chdir", res);
    362345                        }
     
    449432        lfn = ff_memalloc(LFN_BUF_SIZE);
    450433        if (lfn == NULL) {
    451                 ntstdio_printf(&ntstdio, "alloc err.\n");
     434                ntstdio_printf(ntstdio, "alloc err.\n");
    452435                goto cp_end;
    453436        }
     
    459442        local_buff = ff_memalloc(64);
    460443        if (local_buff == NULL) {
    461                 ntstdio_printf(&ntstdio, "alloc err.\n");
     444                ntstdio_printf(ntstdio, "alloc err.\n");
    462445                goto cp_end;
    463446        }
     
    469452        if (res != FR_OK) {
    470453                if (res == FR_NO_FILE)
    471                         ntstdio_printf(&ntstdio, "src no file.\n", res);
     454                        ntstdio_printf(ntstdio, "src no file.\n");
    472455                else
    473                         ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
     456                        ntstdio_printf(ntstdio, "src stat err(%d).\n", res);
    474457                goto cp_end;
    475458        }
     
    480463                res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ));
    481464                if (res != FR_OK) {
    482                         ntstdio_printf(&ntstdio, "src open err(%d).\n", res);
     465                        ntstdio_printf(ntstdio, "src open err(%d).\n", res);
    483466                        goto cp_end;
    484467                }
     
    495478                                dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE);
    496479                                if (dst_mod_str_p == NULL) {
    497                                         ntstdio_printf(&ntstdio, "alloc err.\n");
     480                                        ntstdio_printf(ntstdio, "alloc err.\n");
    498481                                        goto cp_end;
    499482                                }
     
    502485                        }
    503486                        else {
    504                                 ntstdio_printf(&ntstdio, "dst file exists.\n");
     487                                ntstdio_printf(ntstdio, "dst file exists.\n");
    505488                                goto cp_end_1;
    506489                        }
    507490                }
    508491                else {
    509                         ntstdio_printf(&ntstdio, "src stat err(%d).\n", res);
     492                        ntstdio_printf(ntstdio, "src stat err(%d).\n", res);
    510493                        goto cp_end_1;
    511494                }
     
    513496        res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE));
    514497        if (res != FR_OK) {
    515                 ntstdio_printf(&ntstdio, "dst open err(%d).\n", res);
     498                ntstdio_printf(ntstdio, "dst open err(%d).\n", res);
    516499                goto cp_end_1;
    517500        }
     
    524507                res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size);
    525508                if (res != FR_OK) {
    526                         ntstdio_printf(&ntstdio, "src read err(%d).\n", res);
     509                        ntstdio_printf(ntstdio, "src read err(%d).\n", res);
    527510                        goto cp_end_2;
    528511                }
     
    531514                res = f_write(&dst_fp, local_buff, read_size, &write_size);
    532515                if (res != FR_OK) {
    533                         ntstdio_printf(&ntstdio, "dst write err(%d).\n", res);
     516                        ntstdio_printf(ntstdio, "dst write err(%d).\n", res);
    534517                        goto cp_end_2;
    535518                }
    536519                if (read_size != write_size) {
    537                         ntstdio_printf(&ntstdio, "dst write err(disk full).\n", res);
     520                        ntstdio_printf(ntstdio, "dst write err(disk full).\n");
    538521                        goto cp_end_2;
    539522                }
     
    594577}
    595578
    596 extern WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */
    597 extern void Utf16_to_Utf8(unsigned char *, int *, UINT); /* in ff.c */
    598579#define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */
    599580#define CCOLOR_RESET    0
     
    626607                switch (op) {
    627608                case 'h': /* help */
    628                         ntstdio_printf(&ntstdio, " hexdump [OPTION] file\n");
    629                         ntstdio_printf(&ntstdio, "  -h : help\n");
    630                         ntstdio_printf(&ntstdio, "  -d : print all byte with convert and color [in character area] (default)\n");
    631                         ntstdio_printf(&ntstdio, "  -u : try print UTF-8 code [in character area]\n");
    632                         ntstdio_printf(&ntstdio, "  -oOFFSET : print start offset address from top\n");
    633                         ntstdio_printf(&ntstdio, "  -sSIZE   : print size\n");
     609                        ntstdio_printf(ntstdio, " hexdump [OPTION] file\n");
     610                        ntstdio_printf(ntstdio, "  -h : help\n");
     611                        ntstdio_printf(ntstdio, "  -d : print all byte with convert and color [in character area] (default)\n");
     612                        ntstdio_printf(ntstdio, "  -u : try print UTF-8 code [in character area]\n");
     613                        ntstdio_printf(ntstdio, "  -oOFFSET : print start offset address from top\n");
     614                        ntstdio_printf(ntstdio, "  -sSIZE   : print size\n");
    634615                        break;
    635616                case 'd': /* print one byte character [in character area] (default) */
     
    662643        /* position adjusting */
    663644        if (op_offset >= fsrc.fsize) {
    664                 ntstdio_printf(&ntstdio, "error : input offset is bigger than file size(0x%X).\n", fsrc.fsize);
     645                ntstdio_printf(ntstdio, "error : input offset is bigger than file size(0x%lX).\n", fsrc.fsize);
    665646                return 0;
    666647        }
     
    689670                if (br > 16) br = 16;
    690671
    691                 char *apos = ascii;
     672                unsigned char *apos = ascii;
    692673                int arst = sizeof(ascii);
    693674                for (int j = 0; j < br; j++) {
     
    784765                rst -= len;
    785766
    786                 ntstdio_puts(&ntstdio, line);
     767                ntstdio_puts(ntstdio, line);
    787768        }
    788769
     
    799780        ret = shell_clock_gettime(CLOCK_REALTIME, &tp);
    800781        if (ret != 0) {
    801                 ntstdio_printf(&ntstdio, "clock_gettime error %d", ret);
     782                ntstdio_printf(ntstdio, "clock_gettime error %d", ret);
    802783                return 0;
    803784        }
     
    805786        memset(buf, 0, sizeof(buf));
    806787        if (ctime_r(&tp.tv_sec, buf) == NULL) {
    807                 ntstdio_printf(&ntstdio, "ctime_r error");
     788                ntstdio_printf(ntstdio, "ctime_r error");
    808789                return 0;
    809790        }
     
    813794        buf[ret - 1] = '\0';
    814795
    815         ntstdio_printf(&ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);
     796        ntstdio_printf(ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);
    816797        return 0;
    817798}
     
    820801{
    821802        if (argc != 2) {
    822                 ntstdio_printf(&ntstdio, "info sys\n");
    823                 ntstdio_printf(&ntstdio, "info ver\n");
     803                ntstdio_printf(ntstdio, "info sys\n");
     804                ntstdio_printf(ntstdio, "info ver\n");
    824805                return 0;
    825806        }
    826807        if (ntlibc_strcmp(argv[1], "sys") == 0) {
    827                 ntstdio_printf(&ntstdio, "GR-PEACH Monitor\n");
     808                ntstdio_printf(ntstdio, TARGET_NAME" Monitor\n");
    828809                return 0;
    829810        }
     
    831812                int mj, mn, bd;
    832813                ntshell_version(&mj, &mn, &bd);
    833                 ntstdio_printf(&ntstdio, "Version %d.%d.%d\n", mj, mn, bd);
    834                 return 0;
    835         }
    836         ntstdio_printf(&ntstdio, "Unknown sub command found\n");
     814                ntstdio_printf(ntstdio, "Version %d.%d.%d\n", mj, mn, bd);
     815                return 0;
     816        }
     817        ntstdio_printf(ntstdio, "Unknown sub command found\n");
    837818        return -1;
    838819}
  • EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.h

    r433 r441  
    5656int usrcmd_exit(int argc, char **argv);
    5757
    58 void shell_abort();
    59 void shell_exit(int exitcd);
    60 int shell_kill(int pid, int sig);
    61 int shell_gettimeofday(struct timeval * tp, void * tzvp);
    62 
    6358#endif
    6459
  • EcnlProtoTool/trunk/ntshell/src/fdtable.c

    r434 r441  
    4343#include "syssvc/serial.h"
    4444#include "syssvc/syslog.h"
    45 #include <tinet_config.h>
    46 #include <netinet/in.h>
    47 #include <netinet/in_itron.h>
    48 #include <tinet_nic_defs.h>
    49 #include <tinet_cfg.h>
    50 #include <netinet/in_var.h>
    51 #include <net/ethernet.h>
    52 #include <net/if6_var.h>
    53 #include <net/net.h>
    54 #include <net/if_var.h>
    55 #include <netinet/udp_var.h>
    56 //#include <ethernet_api.h>
    57 #include "ff.h"
     45#include "target_syssvc.h"
    5846#include "fdtable.h"
    5947#include "kernel_cfg.h"
    60 //#include <string.h>
    61 
    62 #define SIO_PORTID 1
    63 
    64 #define IO_TYPE_FREE    0
    65 #define IO_TYPE_SIO             1
    66 #define IO_TYPE_FILE    2
    67 #define IO_TYPE_DIR             3
    68 #define IO_TYPE_TCP             4
    69 #define IO_TYPE_UDP             5
    70 
    71 static struct _IO_FILE fd_table[8 * sizeof(FLGPTN)] = {
    72         { 0, IO_TYPE_SIO, 0, stdio_close, stdin_read, stdio_write, sio_seek, sio_ioctl },
    73         { 1, IO_TYPE_SIO, 0, stdio_close, stdio_read, stdout_write, sio_seek, sio_ioctl },
    74         { 2, IO_TYPE_SIO, 0, stdio_close, stdio_read, stderr_write, sio_seek, sio_ioctl },
     48#include <string.h>
     49#include "util/ntstdio.h"
     50#include "hal/serial_api.h"
     51
     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;
     59
     60static struct SHELL_FILE fd_table[8 * sizeof(FLGPTN)] = {
     61        { STDIN_FILENO, &IO_TYPE_STDIN, 0 },
     62        { STDOUT_FILENO, &IO_TYPE_STDOUT, 0 },
     63        { STDERR_FILENO, &IO_TYPE_STDERR, 0 },
    7564};
    7665#define fd_table_count (sizeof(fd_table) / sizeof(fd_table[0]))
    7766
    78 static int new_fd(int type, int id)
    79 {
     67struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable)
     68{
     69        struct SHELL_FILE *fp = NULL;
     70        ER ret;
     71
     72        ret = wai_sem(SEM_FILEDESC);
     73        if (ret < 0) {
     74                syslog(LOG_ERROR, "wai_sem => %d", ret);
     75        }
     76
    8077        for (int fd = 3; fd < fd_table_count; fd++) {
    81                 struct _IO_FILE *fp = &fd_table[fd];
    82                 if (fp->type != IO_TYPE_FREE)
     78                fp = &fd_table[fd];
     79                if (fp->type != NULL)
    8380                        continue;
    8481
     82                memset(fp, 0, sizeof(struct SHELL_FILE));
    8583                fp->fd = fd;
    8684                fp->type = type;
    8785                fp->handle = id;
    88                 return fd;
    89         }
    90 
    91         return -ENOMEM;
    92 }
    93 
    94 static struct _IO_FILE *id_to_fd(int type, int id)
    95 {
     86                fp->writable = writable;
     87                break;
     88        }
     89
     90        ret = sig_sem(SEM_FILEDESC);
     91        if (ret < 0) {
     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                }
     104        }
     105
     106        return fp;
     107}
     108
     109struct SHELL_FILE *id_to_fd(IO_TYPE *type, int id)
     110{
     111        struct SHELL_FILE *fp = NULL;
     112        ER ret;
     113
     114        ret = wai_sem(SEM_FILEDESC);
     115        if (ret < 0) {
     116                syslog(LOG_ERROR, "wai_sem => %d", ret);
     117        }
     118
    96119        for (int fd = 3; fd < fd_table_count; fd++) {
    97                 struct _IO_FILE *fp = &fd_table[fd];
     120                fp = &fd_table[fd];
    98121                if ((fp->type == type) && (fp->handle == id))
    99                         return fp;
    100         }
    101 
    102         return NULL;
    103 }
    104 
    105 static int delete_fd(int type, int id)
    106 {
    107         struct _IO_FILE *fp = id_to_fd(type, id);
     122                        break;
     123        }
     124
     125        ret = sig_sem(SEM_FILEDESC);
     126        if (ret < 0) {
     127                syslog(LOG_ERROR, "sig_sem => %d", ret);
     128        }
     129
     130        return fp;
     131}
     132
     133int delete_fd_by_id(IO_TYPE *type, int id)
     134{
     135        struct SHELL_FILE *fp = id_to_fd(type, id);
    108136        if (fp == NULL)
    109137                return -EBADF;
    110138
    111         memset(fp, 0, sizeof(struct _IO_FILE));
     139        return delete_fp(fp);
     140}
     141
     142int delete_fp(struct SHELL_FILE *fp)
     143{
     144        ER ret;
     145
     146        if (fp->type == NULL)
     147                return 0;
     148
     149        fp->type->delete(fp);
     150
     151        ret = wai_sem(SEM_FILEDESC);
     152        if (ret < 0) {
     153                syslog(LOG_ERROR, "wai_sem => %d", ret);
     154        }
     155
     156        memset(fp, 0, sizeof(struct SHELL_FILE));
     157
     158        ret = sig_sem(SEM_FILEDESC);
     159        if (ret < 0) {
     160                syslog(LOG_ERROR, "sig_sem => %d", ret);
     161        }
    112162
    113163        return 0;
    114164}
    115165
    116 struct _IO_FILE *fd_to_fp(int fd)
     166struct SHELL_FILE *fd_to_fp(int fd)
    117167{
    118168        if ((fd < 0) || (fd >= fd_table_count))
     
    121171}
    122172
    123 struct _IO_FILE *new_sio_fd(int sioid)
    124 {
    125         int fd = new_fd(IO_TYPE_SIO, sioid);
    126         if ((fd < 0) || (fd >= fd_table_count))
    127                 return NULL;
    128 
    129         struct _IO_FILE *fp = &fd_table[fd];
    130         fp->close = sio_close;
    131         fp->read = sio_read;
    132         fp->write = sio_write;
    133         fp->seek = sio_seek;
    134         fp->ioctl = sio_ioctl;
    135 
    136         return fp;
    137 }
    138 
    139 int delete_sio_fd(int sioid)
    140 {
    141         return delete_fd(IO_TYPE_SIO, sioid);
    142 }
    143 
    144 struct _IO_FILE *sioid_to_fd(int sioid)
    145 {
    146         return id_to_fd(IO_TYPE_SIO, sioid);
    147 }
    148 
    149 struct _IO_FILE *new_file_fd(int fileid)
    150 {
    151         int fd = new_fd(IO_TYPE_FILE, fileid);
    152         if ((fd < 0) || (fd >= fd_table_count))
    153                 return NULL;
    154 
    155         struct _IO_FILE *fp = &fd_table[fd];
    156         fp->close = file_close;
    157         fp->read = file_read;
    158         fp->write = file_write;
    159         fp->seek = file_seek;
    160         fp->ioctl = file_ioctl;
    161 
    162         return fp;
    163 }
    164 
    165 int delete_file_fd(int fileid)
    166 {
    167         return delete_fd(IO_TYPE_FILE, fileid);
    168 }
    169 
    170 struct _IO_FILE *fileid_to_fd(int fileid)
    171 {
    172         return id_to_fd(IO_TYPE_FILE, fileid);
    173 }
    174 
    175 struct _IO_FILE *new_dir_fd(int fileid)
    176 {
    177         int fd = new_fd(IO_TYPE_DIR, fileid);
    178         if ((fd < 0) || (fd >= fd_table_count))
    179                 return NULL;
    180 
    181         struct _IO_FILE *fp = &fd_table[fd];
    182         fp->close = dir_close;
    183         fp->read = dir_read;
    184         fp->write = dir_write;
    185         fp->seek = dir_seek;
    186         fp->ioctl = dir_ioctl;
    187 
    188         return fp;
    189 }
    190 
    191 int delete_dir_fd(int dirid)
    192 {
    193         return delete_fd(IO_TYPE_DIR, dirid);
    194 }
    195 
    196 struct _IO_FILE *dirid_to_fd(int dirid)
    197 {
    198         return id_to_fd(IO_TYPE_DIR, dirid);
    199 }
    200 
    201 struct _IO_FILE *new_tcp_fd(int tcpid)
    202 {
    203         int fd = new_fd(IO_TYPE_TCP, tcpid);
    204         if ((fd < 0) || (fd >= fd_table_count))
    205                 return NULL;
    206 
    207         struct _IO_FILE *fp = &fd_table[fd];
    208         fp->close = tcp_fd_close;
    209         fp->read = tcp_fd_read;
    210         fp->write = tcp_fd_write;
    211         fp->seek = tcp_fd_seek;
    212         fp->ioctl = tcp_fd_ioctl;
    213 
    214         return fp;
    215 }
    216 
    217 int delete_tcp_fd(int tcpid)
    218 {
    219         return delete_fd(IO_TYPE_TCP, tcpid);
    220 }
    221 
    222 struct _IO_FILE *tcpid_to_fd(int tcpid)
    223 {
    224         return id_to_fd(IO_TYPE_TCP, tcpid);
    225 }
    226 
    227 struct _IO_FILE *new_udp_fd(int udpid)
    228 {
    229         int fd = new_fd(IO_TYPE_UDP, udpid);
    230         if ((fd < 0) || (fd >= fd_table_count))
    231                 return NULL;
    232 
    233         struct _IO_FILE *fp = &fd_table[fd];
    234         fp->close = udp_fd_close;
    235         fp->read = udp_fd_read;
    236         fp->write = udp_fd_write;
    237         fp->seek = udp_fd_seek;
    238         fp->ioctl = udp_fd_ioctl;
    239 
    240         return fp;
    241 }
    242 
    243 int delete_udp_fd(int udpid)
    244 {
    245         return delete_fd(IO_TYPE_UDP, udpid);
    246 }
    247 
    248 struct _IO_FILE *udpid_to_fd(int udpid)
    249 {
    250         return id_to_fd(IO_TYPE_UDP, udpid);
     173void memand(void *dst, void *src, size_t len)
     174{
     175        uint8_t *d = (uint8_t *)dst;
     176        uint8_t *s = (uint8_t *)src;
     177        uint8_t *e = &s[len];
     178
     179        while (s < e) {
     180                *d++ &= *s++;
     181        }
    251182}
    252183
     
    258189};
    259190
    260 ER shell_get_evts(struct fd_events *evts, TMO tmout);
     191ER shell_get_evts(struct fd_events *evts, TMO *tmout);
    261192
    262193#define TMO_MAX INT_MAX
    263194
    264 int shell_select(int n, fd_set *__restrict rfds, fd_set *__restrict wfds, fd_set *__restrict efds, struct timeval *__restrict tv)
     195int shell_select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv)
    265196{
    266197        ER ret;
     
    275206        }
    276207
    277         memcpy(&evts.readfds, rfds, sizeof(fd_set));
    278         memcpy(&evts.writefds, wfds, sizeof(fd_set));
    279         memcpy(&evts.errorfds, efds, sizeof(fd_set));
     208        if (rfds != NULL)
     209                memcpy(&evts.readfds, rfds, sizeof(fd_set));
     210        else
     211                memset(&evts.readfds, 0, sizeof(fd_set));
     212        if (wfds != NULL)
     213                memcpy(&evts.writefds, wfds, sizeof(fd_set));
     214        else
     215                memset(&evts.writefds, 0, sizeof(fd_set));
     216        if (efds != NULL)
     217                memcpy(&evts.errorfds, efds, sizeof(fd_set));
     218        else
     219                memset(&evts.errorfds, 0, sizeof(fd_set));
    280220        evts.count = 0;
    281221
    282         ret = shell_get_evts(&evts, tmout);
     222        ret = shell_get_evts(&evts, &tmout);
    283223        if (ret == E_OK) {
    284                 memcpy(rfds, &evts.readfds, sizeof(fd_set));
    285                 memcpy(wfds, &evts.writefds, sizeof(fd_set));
    286                 memcpy(efds, &evts.errorfds, sizeof(fd_set));
     224                if (rfds != NULL)
     225                        memand(rfds, &evts.readfds, sizeof(fd_set));
     226                if (wfds != NULL)
     227                        memand(wfds, &evts.writefds, sizeof(fd_set));
     228                if (efds != NULL)
     229                        memand(efds, &evts.errorfds, sizeof(fd_set));
    287230                return evts.count;
    288231        }
    289232        if (ret == E_TMOUT) {
    290                 memset(rfds, 0, sizeof(fd_set));
    291                 memset(wfds, 0, sizeof(fd_set));
    292                 memset(efds, 0, sizeof(fd_set));
     233                if (rfds != NULL)
     234                        memset(rfds, 0, sizeof(fd_set));
     235                if (wfds != NULL)
     236                        memset(wfds, 0, sizeof(fd_set));
     237                if (efds != NULL)
     238                        memset(efds, 0, sizeof(fd_set));
    293239                return 0;
    294240        }
     
    310256                tmout = TMO_MAX;
    311257
     258retry:
    312259        memset(&evts, 0, sizeof(evts));
    313260
     
    327274        }
    328275
    329         ret = shell_get_evts(&evts, tmout);
     276        ret = shell_get_evts(&evts, &tmout);
    330277        if (ret == E_OK) {
    331278                int result = 0;
     
    345292                                result++;
    346293                }
     294                if (result == 0)
     295                        goto retry;
    347296                return result;
    348297        }
     
    354303}
    355304
    356 void stdio_update_evts()
    357 {
    358         int fd = STDIN_FILENO;
    359         struct _IO_FILE *fp = &fd_table[fd];
    360         T_SERIAL_RPOR rpor;
    361         FLGPTN flgptn = 0;
    362 
    363         ER ret = serial_ref_por(SIO_PORTID, &rpor);
    364         if (ret != E_OK)
    365                 return;
    366 
    367         if (rpor.reacnt != 0) {
    368                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    369 
    370                 FD_SET(fd, (fd_set *)&flgptn);
    371         }
    372         if (rpor.wricnt != 0) {
    373                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    374 
    375                 FD_SET(fd, (fd_set *)&flgptn);
    376         }
    377 
    378         if (flgptn != 0) {
    379                 set_flg(FLG_SELECT_WAIT, flgptn);
    380         }
    381 }
    382 
    383 void stdio_flgptn(FLGPTN *flgptn)
    384 {
    385         int fd = STDIN_FILENO;
    386         struct _IO_FILE *fp = &fd_table[fd];
    387         T_SERIAL_RPOR rpor;
    388         *flgptn = 0;
    389 
    390         ER ret = serial_ref_por(SIO_PORTID, &rpor);
    391         if (ret != E_OK)
    392                 return;
    393 
    394         if (rpor.reacnt != 0) {
    395                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    396 
    397                 FD_SET(fd, (fd_set *)flgptn);
    398         }
    399         if (rpor.wricnt != 0) {
    400                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    401 
    402                 FD_SET(fd, (fd_set *)flgptn);
    403         }
    404 }
    405 
    406 ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk)
    407 {
    408         struct _IO_FILE *fp = tcpid_to_fd(cepid);
    409         FLGPTN flgptn = 0;
    410 
    411         if (fp == NULL)
    412                 return E_PAR;
    413 
    414         int fd = fp->fd;
    415         FD_SET(fd, (fd_set *)&flgptn);
    416 
    417         switch (fncd) {
    418         case TFN_TCP_RCV_DAT:
    419                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    420 
    421                 set_flg(FLG_SELECT_WAIT, flgptn);
    422                 return E_OK;
    423 
    424         case TFN_TCP_SND_DAT:
    425                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    426 
    427                 set_flg(FLG_SELECT_WAIT, flgptn);
    428                 return E_OK;
    429 
    430         case TFN_TCP_CAN_CEP:
    431                 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
    432 
    433                 set_flg(FLG_SELECT_WAIT, flgptn);
    434                 return E_OK;
    435 
    436         case TFN_TCP_DEL_REP:
    437                 delete_tcp_rep(cepid);
    438                 return E_OK;
    439 
    440         case TFN_TCP_DEL_CEP:
    441                 delete_tcp_fd(cepid);
    442                 return E_OK;
    443 
    444         default:
    445                 return E_OK;
    446         }
    447 }
    448 
    449 ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk)
    450 {
    451         struct _IO_FILE *fp = udpid_to_fd(cepid);
    452         FLGPTN flgptn = 0;
    453 
    454         if (fp == NULL)
    455                 return E_PAR;
    456 
    457         int fd = fp->fd;
    458         FD_SET(fd, (fd_set *)&flgptn);
    459 
    460         switch (fncd) {
    461         case TFN_UDP_CRE_CEP:
    462         case TFN_UDP_RCV_DAT:
    463                 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
    464 
    465                 set_flg(FLG_SELECT_WAIT, flgptn);
    466                 return E_OK;
    467 
    468         case TFN_UDP_SND_DAT:
    469                 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
    470 
    471                 set_flg(FLG_SELECT_WAIT, flgptn);
    472                 return E_OK;
    473 
    474         case TFN_UDP_CAN_CEP:
    475                 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
    476 
    477                 set_flg(FLG_SELECT_WAIT, flgptn);
    478                 return E_OK;
    479 
    480         case TFN_UDP_DEL_CEP:
    481                 delete_udp_fd(cepid);
    482                 return E_OK;
    483 
    484         default:
    485                 return E_OK;
    486         }
    487 }
    488 
    489 ER shell_get_evts(struct fd_events *evts, TMO tmout)
    490 {
    491         ER ret;
    492         FLGPTN waitptn, flgptn = 0;
    493 
    494         stdio_update_evts();
    495 
    496         waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->writefds) | *((FLGPTN *)&evts->errorfds);
    497         memset(evts, 0, sizeof(*evts));
    498 
    499         /* イベント待ち */
    500         ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, tmout);
    501         if (ret != E_OK) {
    502                 if (ret != E_TMOUT) {
    503                         syslog(LOG_ERROR, "twai_flg => %d", ret);
    504                         return ret;
    505                 }
    506 
    507                 stdio_flgptn(&flgptn);
    508 
    509                 if (flgptn == 0)
     305ER shell_get_evts(struct fd_events *evts, TMO *tmout)
     306{
     307        int count;
     308        SYSTIM prev, now;
     309        FLGPTN flgptn;
     310
     311        get_tim(&prev);
     312
     313        for (;;) {
     314                ER ret;
     315                FLGPTN waitptn, readfds = 0, writefds = 0;
     316                struct SHELL_FILE *fp = NULL;
     317
     318#ifndef NTSHELL_NO_SOCKET
     319                waitptn = *((FLGPTN *)&evts->errorfds);
     320#else
     321                waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->errorfds);
     322#endif
     323                count = 0;
     324                for (int fd = 0; fd < fd_table_count; fd++) {
     325                        fp = &fd_table[fd];
     326
     327                        if (FD_ISSET(fd, &evts->readfds)) {
     328                                if (fp->type->readable(fp)) {
     329                                        FD_SET(fd, (fd_set *)&readfds);
     330                                        count++;
     331                                        if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
     332                                }
     333                                else {
     334                                        FD_SET(fd, (fd_set *)&waitptn);
     335                                }
     336                        }
     337
     338                        if (FD_ISSET(fd, &evts->writefds)) {
     339                                if (fp->type->writable(fp)) {
     340                                        FD_SET(fd, (fd_set *)&writefds);
     341                                        count++;
     342                                        if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_r--;
     343                                }
     344                                else {
     345                                        FD_SET(fd, (fd_set *)&waitptn);
     346                                }
     347                        }
     348                }
     349                memset(evts, 0, sizeof(*evts));
     350
     351                if (waitptn == 0) {
     352                        memcpy(&evts->readfds, &readfds, sizeof(evts->readfds));
     353                        memcpy(&evts->writefds, &writefds, sizeof(evts->writefds));
     354                        evts->count = count;
     355                        return E_OK;
     356                }
     357                else if ((readfds | writefds) != 0) {
     358                        set_flg(FLG_SELECT_WAIT, (readfds | writefds));
     359                }
     360
     361                /* イベント待ち */
     362                flgptn = 0;
     363                ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, *tmout);
     364                if (ret != E_OK) {
     365                        if (ret != E_TMOUT) {
     366                                syslog(LOG_ERROR, "twai_flg => %d", ret);
     367                                return ret;
     368                        }
     369                }
     370
     371                if (flgptn != 0) {
     372                        flgptn &= waitptn;
     373
     374                        /* 受け取ったフラグのみクリア */
     375                        ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
     376                        if (ret != E_OK) {
     377                                syslog(LOG_ERROR, "clr_flg => %d", ret);
     378                        }
     379                }
     380
     381                count = 0;
     382                for (int fd = 0; fd < fd_table_count; fd++) {
     383                        fp = &fd_table[fd];
     384
     385                        if (FD_ISSET(fd, (fd_set *)&waitptn)
     386                                && (fp->readevt_w != fp->readevt_r)) {
     387                                fp->readevt_r++;
     388                                FD_SET(fd, &evts->readfds);
     389                                count++;
     390                        }
     391                        if (FD_ISSET(fd, (fd_set *)&waitptn)
     392                                && (fp->writeevt_w != fp->writeevt_r)) {
     393                                fp->writeevt_r++;
     394                                FD_SET(fd, &evts->writefds);
     395                                count++;
     396                        }
     397                        if (FD_ISSET(fd, (fd_set *)&waitptn)
     398                                && (fp->errorevt_w != fp->errorevt_r)) {
     399                                fp->errorevt_r++;
     400                                FD_SET(fd, &evts->errorfds);
     401                                count++;
     402                        }
     403                }
     404
     405                if (count > 0)
     406                        break;
     407
     408                get_tim(&now);
     409
     410                SYSTIM elapse = now - prev;
     411                if (elapse > *tmout) {
     412                        *tmout = 0;
     413                        evts->count = 0;
    510414                        return E_TMOUT;
    511         }
    512         flgptn &= waitptn;
    513 
    514         /* 受け取ったフラグのみクリア */
    515         ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
    516         if (ret != E_OK) {
    517                 syslog(LOG_ERROR, "clr_flg => %d", ret);
    518         }
    519 
    520         struct _IO_FILE *fp = NULL;
    521         for (int fd = 0; fd < fd_table_count; fd++) {
    522                 if (!FD_ISSET(fd, (fd_set *)&flgptn))
    523                         continue;
    524 
    525                 fp = &fd_table[fd];
    526 
    527                 if (fp->readevt_w != fp->readevt_r) {
    528                         fp->readevt_r++;
    529                         FD_SET(fd, &evts->readfds);
    530                         evts->count++;
    531                 }
    532                 if (fp->writeevt_w != fp->writeevt_r) {
    533                         fp->writeevt_r++;
    534                         FD_SET(fd, &evts->writefds);
    535                         evts->count++;
    536                 }
    537                 if (fp->errorevt_w != fp->errorevt_r) {
    538                         fp->errorevt_r++;
    539                         FD_SET(fd, &evts->errorfds);
    540                         evts->count++;
    541                 }
    542         }
     415                }
     416
     417                prev = now;
     418                *tmout -= elapse;
     419        }
     420
     421        evts->count = count;
    543422
    544423        return E_OK;
     
    547426void clean_fd()
    548427{
    549         struct _IO_FILE *fp = NULL;
     428        struct SHELL_FILE *fp = NULL;
    550429        for (int fd = 3; fd < fd_table_count; fd++) {
    551430                fp = &fd_table[fd];
     
    553432                        continue;
    554433
    555                 fp->close(fp);
    556 
    557                 memset(fp, 0, sizeof(*fp));
    558         }
     434                fp->type->close(fp);
     435
     436                delete_fp(fp);
     437        }
     438}
     439
     440int shell_close(int fd)
     441{
     442        struct SHELL_FILE *fp = fd_to_fp(fd);
     443        if ((fp == NULL) || (fp->type == NULL))
     444                return -EBADF;
     445
     446        int ret = fp->type->close(fp);
     447
     448        delete_fp(fp);
     449
     450        return ret;
     451}
     452
     453ssize_t shell_read(int fd, void *data, size_t len)
     454{
     455        struct SHELL_FILE *fp = fd_to_fp(fd);
     456        if ((fp == NULL) || (fp->type == NULL))
     457                return -EBADF;
     458
     459        return fp->type->read(fp, (unsigned char *)data, len);
     460}
     461
     462int shell_readv(int fd, const struct iovec *iov, int iovcnt)
     463{
     464        int result = 0;
     465        struct SHELL_FILE *fp = fd_to_fp(fd);
     466        if ((fp == NULL) || (fp->type == NULL))
     467                return -EBADF;
     468
     469        const struct iovec *end = &iov[iovcnt];
     470        for (; iov < end; iov++) {
     471                result += fp->type->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     472        }
     473
     474        return result;
     475}
     476
     477ssize_t shell_write(int fd, const void *data, size_t len)
     478{
     479        struct SHELL_FILE *fp = fd_to_fp(fd);
     480        if ((fp == NULL) || (fp->type == NULL))
     481                return -EBADF;
     482
     483        return fp->type->write(fp, (unsigned char *)data, len);
     484}
     485
     486int shell_writev(int fd, const struct iovec *iov, int iovcnt)
     487{
     488        int result = 0;
     489        struct SHELL_FILE *fp = fd_to_fp(fd);
     490        if ((fp == NULL) || (fp->type == NULL))
     491                return -EBADF;
     492
     493        const struct iovec *end = &iov[iovcnt];
     494        for (; iov < end; iov++) {
     495                result += fp->type->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     496        }
     497
     498        return result;
     499}
     500
     501int shell_llseek(int fd, off_t ptr, off_t *result, int dir)
     502{
     503        struct SHELL_FILE *fp = fd_to_fp(fd);
     504        if ((fp == NULL) || (fp->type == NULL))
     505                return -EBADF;
     506
     507        off_t ret = fp->type->seek(fp, ptr, dir);
     508        if (ret < 0)
     509                return ret;
     510
     511        *result = ret;
     512        return 0;
    559513}
    560514
    561515int shell_ioctl(int fd, int request, void *arg)
    562516{
    563         struct _IO_FILE *fp = fd_to_fp(fd);
     517        struct SHELL_FILE *fp = fd_to_fp(fd);
    564518        if (fp == NULL)
    565519                return -EBADF;
    566520
    567         return fp->ioctl(fp, request, arg);
    568 }
     521        return fp->type->ioctl(fp, request, arg);
     522}
     523
     524#ifdef NTSHELL_NO_SOCKET
     525
     526int shell_socket(int family, int type, int protocol)
     527{
     528        return -ENOMEM;
     529}
     530
     531int shell_bind(int fd, const struct sockaddr *addr, socklen_t len)
     532{
     533        return -ENOMEM;
     534}
     535
     536int shell_listen(int fd, int backlog)
     537{
     538        return -ENOMEM;
     539}
     540
     541int shell_connect(int fd, const struct sockaddr *addr, socklen_t len)
     542{
     543        return -ENOMEM;
     544}
     545
     546int shell_accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
     547{
     548        return -ENOMEM;
     549}
     550
     551ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
     552{
     553        return -ENOMEM;
     554}
     555
     556ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags)
     557{
     558        return -ENOMEM;
     559}
     560
     561ssize_t shell_recvfrom(int fd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict addr, socklen_t *restrict alen)
     562{
     563        return -ENOMEM;
     564}
     565
     566ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags)
     567{
     568        return -ENOMEM;
     569}
     570
     571int shell_shutdown(int fd, int how)
     572{
     573        return -ENOMEM;
     574}
     575
     576int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *restrict optlen)
     577{
     578        return -ENOMEM;
     579}
     580
     581int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
     582{
     583        return -ENOMEM;
     584}
     585
     586int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
     587{
     588        return -ENOMEM;
     589}
     590
     591int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
     592{
     593        return -ENOMEM;
     594}
     595#endif
  • EcnlProtoTool/trunk/ntshell/src/fdtable.h

    r439 r441  
    3535 *  @(#) $Id$
    3636 */
    37 #ifndef SOCKET_STUB_H
    38 #define SOCKET_STUB_H
     37#ifndef _FDTABLE_H_
     38#define _FDTABLE_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 };
     40typedef const struct io_type_s IO_TYPE;
    5041
    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 } socket_t;
    102 
    103 struct _IO_FILE {
     42struct SHELL_FILE {
    10443        int fd;
    105         int type;
     44        IO_TYPE *type;
    10645        int handle;
    107         int (*close)(struct _IO_FILE *);
    108         size_t (*read)(struct _IO_FILE *, unsigned char *, size_t);
    109         size_t (*write)(struct _IO_FILE *, const unsigned char *, size_t);
    110         off_t (*seek)(struct _IO_FILE *, off_t, int);
    111         int (*ioctl)(struct _IO_FILE *, int, void *);
    11246        int readevt_r;
    11347        int readevt_w;
    11448        int writeevt_r;
    11549        int writeevt_w;
     50        int writable;
    11651        int errorevt_r;
    11752        int errorevt_w;
    118         union {
    119                 FIL file;
    120                 struct {
    121                         FATFS_DIR dir;
    122                         struct dirent dirent;
    123                 };
    124                 socket_t socket;
    125         };
     53        void *exinf;
     54};
     55
     56struct io_type_s {
     57        const int (*close)(struct SHELL_FILE *);
     58        const size_t (*read)(struct SHELL_FILE *, unsigned char *, size_t);
     59        const size_t (*write)(struct SHELL_FILE *, const unsigned char *, size_t);
     60        const off_t (*seek)(struct SHELL_FILE *, off_t, int);
     61        const int (*ioctl)(struct SHELL_FILE *, int, void *);
     62        const bool_t (*readable)(struct SHELL_FILE *);
     63        const bool_t (*writable)(struct SHELL_FILE *);
     64        const void (*delete)(struct SHELL_FILE *);
    12665};
    12766
     
    13675#endif
    13776
    138 extern int stdio_close(struct _IO_FILE *fp);
    139 extern size_t stdio_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    140 extern size_t stdio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    141 extern size_t stdin_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    142 extern size_t stdout_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    143 extern size_t stderr_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
     77struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable);
     78int delete_fd_by_id(IO_TYPE *type, int id);
     79struct SHELL_FILE *fd_to_fp(int fd);
     80struct SHELL_FILE *id_to_fd(IO_TYPE *type, int id);
    14481
    145 extern int sio_close(struct _IO_FILE *fp);
    146 extern size_t sio_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    147 extern size_t sio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    148 extern off_t sio_seek(struct _IO_FILE *fp, off_t ofs, int org);
    149 extern int sio_ioctl(struct _IO_FILE *fp, int req, void *arg);
    150 
    151 extern int file_close(struct _IO_FILE *fp);
    152 extern size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    153 extern size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    154 extern off_t file_seek(struct _IO_FILE *fp, off_t ofs, int org);
    155 extern int file_ioctl(struct _IO_FILE *fp, int req, void *arg);
    156 
    157 extern int dir_close(struct _IO_FILE *fp);
    158 extern size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    159 extern size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    160 extern off_t dir_seek(struct _IO_FILE *fp, off_t ofs, int org);
    161 extern int dir_ioctl(struct _IO_FILE *fp, int req, void *arg);
    162 
    163 extern int tcp_fd_close(struct _IO_FILE *fp);
    164 extern size_t tcp_fd_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    165 extern size_t tcp_fd_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    166 extern off_t tcp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org);
    167 extern int tcp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg);
    168 
    169 extern int udp_fd_close(struct _IO_FILE *fp);
    170 extern size_t udp_fd_read(struct _IO_FILE *fp, unsigned char *data, size_t len);
    171 extern size_t udp_fd_write(struct _IO_FILE *fp, const unsigned char *data, size_t len);
    172 extern off_t udp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org);
    173 extern int udp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg);
    174 
    175 struct _IO_FILE *fd_to_fp(int fd);
    176 
    177 struct _IO_FILE *new_sio_fd(int sioid);
    178 int delete_sio_fd(int sioid);
    179 struct _IO_FILE *sioid_to_fd(int sioid);
    180 
    181 struct _IO_FILE *new_file_fd(int fileid);
    182 int delete_file_fd(int fileid);
    183 struct _IO_FILE *fileid_to_fd(int fileid);
    184 
    185 struct _IO_FILE *new_dir_fd(int dirid);
    186 int delete_dir_fd(int dirid);
    187 struct _IO_FILE *dirid_to_fd(int dirid);
    188 
    189 struct _IO_FILE *new_tcp_fd(int tcpid);
    190 int delete_tcp_fd(int tcpid);
    191 struct _IO_FILE *tcpid_to_fd(int tcpid);
    192 
    193 struct _IO_FILE *new_udp_fd(int udpid);
    194 int delete_udp_fd(int udpid);
    195 struct _IO_FILE *udpid_to_fd(int udpid);
    196 
    197 int delete_tcp_rep(int repid);
     82int delete_fp(struct SHELL_FILE *fp);
    19883void clean_fd();
    19984
    200 ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk);
    201 ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk);
    202 
    203 #endif // !SOCKET_STUB_H
     85#endif /* _FDTABLE_H_ */
  • EcnlProtoTool/trunk/ntshell/src/io_stub.c

    r434 r441  
    11/*
    22 *  TOPPERS PROJECT Home Network Working Group Software
    3  * 
     3 *
    44 *  Copyright (C) 2017-2019 Cores Co., Ltd. Japan
    5  * 
     5 *
    66 *  上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
    77 *  ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
     
    2626 *      由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
    2727 *      免責すること.
    28  * 
     28 *
    2929 *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
    3030 *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
     
    3232 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
    3333 *  の責任を負わない.
    34  * 
     34 *
    3535 *  @(#) $Id$
    3636 */
     
    4242#include <t_stdlib.h>
    4343#include <sil.h>
    44 //#include <string.h>
    45 #include <setjmp.h>
     44#include <string.h>
     45#include "syssvc/serial.h"
    4646#include "syssvc/syslog.h"
    47 #include <tinet_config.h>
    48 #include <netinet/in.h>
    49 #include <netinet/in_itron.h>
    50 #include <tinet_nic_defs.h>
    51 #include <tinet_cfg.h>
    52 #include <netinet/in_var.h>
    53 #include <net/ethernet.h>
    54 #include <net/if6_var.h>
    55 #include <net/net.h>
    56 #include <net/if_var.h>
    57 #include <netinet/udp_var.h>
    58 //#include <ethernet_api.h>
    5947#include "fdtable.h"
    60 //#include <sys/stat.h>
    6148#include "util/ntstdio.h"
    6249#include "usrcmd.h"
    6350#include "core/ntlibc.h"
     51#include "kernel_cfg.h"
     52#include "target_syssvc.h"
     53
     54struct SHELL_DIR {
     55        FATFS_DIR dir;
     56        struct dirent dirent;
     57};
    6458
    6559int fresult2errno(FRESULT res)
     
    9084}
    9185
    92 int shell_open(const char * path, int flags, void *arg)
    93 {
    94         FRESULT res;
    95         struct _IO_FILE *fp;
     86static int file_close(struct SHELL_FILE *fp);
     87static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     88static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     89static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     90static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     91static bool_t file_readable(struct SHELL_FILE *fp);
     92static bool_t file_writable(struct SHELL_FILE *fp);
     93static void file_delete(struct SHELL_FILE *fp);
     94
     95static int dir_close(struct SHELL_FILE *fp);
     96static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     97static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     98static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     99static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     100static bool_t dir_readable(struct SHELL_FILE *fp);
     101static bool_t dir_writable(struct SHELL_FILE *fp);
     102static void dir_delete(struct SHELL_FILE *fp);
     103
     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 };
     106
     107int shell_open(const char *path, int flags, void *arg)
     108{
     109        FRESULT res;
     110        struct SHELL_FILE *fp;
    96111
    97112        if (flags & O_DIRECTORY) {
    98                 fp = new_dir_fd(0);
     113                fp = new_fp(&IO_TYPE_DIR, 0, 0);
    99114                if (fp == NULL)
    100115                        return -ENOMEM;
    101116
    102                 FATFS_DIR *dir = &fp->dir;
    103                 FRESULT res;
     117                fp->exinf = malloc(sizeof(struct SHELL_DIR));
     118                memset(fp->exinf, 0, sizeof(struct SHELL_DIR));
     119
     120                FATFS_DIR *dir = &((struct SHELL_DIR *)fp->exinf)->dir;
    104121                if ((res = f_opendir(dir, path)) != FR_OK) {
     122                        delete_fp(fp);
    105123                        return fresult2errno(res);
    106124                }
     
    108126        }
    109127
    110         fp = new_file_fd(0);
     128        fp = new_fp(&IO_TYPE_FILE, 0, 1);
    111129        if (fp == NULL)
    112130                return -ENOMEM;
     131
     132        fp->exinf = malloc(sizeof(FIL));
     133        memset(fp->exinf, 0, sizeof(FIL));
    113134
    114135        BYTE fmd = 0;
     
    146167        }
    147168
    148         if ((res = f_open(&fp->file, path, fmd)) == FR_OK) {
     169        if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) {
    149170                fp->handle = fp->fd;
    150171                return fp->fd;
    151172        }
    152173
     174        delete_fp(fp);
    153175        return fresult2errno(res);
    154176}
    155177
    156 int file_close(struct _IO_FILE *fp)
    157 {
    158         FRESULT res;
    159 
    160         if ((res = f_close(&fp->file)) == FR_OK) {
     178int file_close(struct SHELL_FILE *fp)
     179{
     180        FRESULT res;
     181
     182        if ((res = f_close((FIL *)fp->exinf)) == FR_OK) {
    161183                return 0;
    162184        }
     
    165187}
    166188
    167 size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
     189size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    168190{
    169191        unsigned int ret = 0;
    170192        FRESULT res;
    171193
    172         if ((res = f_read(&fp->file, data, len, &ret)) != FR_OK)
     194        if ((res = f_read((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
    173195                return -EIO;
    174196
     
    176198}
    177199
    178 size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
     200size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    179201{
    180202        unsigned int ret = 0;
    181203        FRESULT res;
    182204
    183         if ((res = f_write(&fp->file, data, len, &ret)) != FR_OK)
     205        if ((res = f_write((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
    184206                return -EIO;
    185207
     
    187209}
    188210
    189 off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     211off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
    190212{
    191213        switch (dir) {
     
    204226
    205227        FRESULT res;
    206         if ((res = f_seek(&fp->file, ptr, dir)) != FR_OK)
     228        if ((res = f_seek((FIL *)fp->exinf, ptr, dir)) != FR_OK)
    207229                return -EIO;
    208230
    209         return fp->file.fptr;
    210 }
    211 
    212 int file_ioctl(struct _IO_FILE *fp, int req, void *arg)
     231        return ((FIL *)fp->exinf)->fptr;
     232}
     233
     234int file_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    213235{
    214236        DRESULT res;
    215237
    216         if ((res = disk_ioctl(fp->file.fs->drv, req, arg) != RES_OK))
     238        if ((res = disk_ioctl(((FIL *)fp->exinf)->fs->drv, req, arg) != RES_OK))
    217239                return -EINVAL;
    218240
     
    220242}
    221243
    222 int shell_close(int fd)
    223 {
    224         struct _IO_FILE *fp = fd_to_fp(fd);
    225         if (fp == NULL)
    226                 return -EBADF;
    227 
    228         return fp->close(fp);
    229 }
    230 
    231 ssize_t shell_read(int fd, void *data, size_t len)
    232 {
    233         struct _IO_FILE *fp = fd_to_fp(fd);
    234         if (fp == NULL)
    235                 return -EBADF;
    236 
    237         return fp->read(fp, (unsigned char *)data, len);
    238 }
    239 
    240 int shell_readv(int fd, const struct iovec *iov, int iovcnt)
    241 {
    242         int result = 0;
    243         struct _IO_FILE *fp = fd_to_fp(fd);
    244         if (fp == NULL)
    245                 return -EBADF;
    246 
    247         const struct iovec *end = &iov[iovcnt];
    248         for (; iov < end; iov++) {
    249                 result += fp->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
    250         }
    251 
    252         return result;
    253 }
    254 
    255 ssize_t shell_write(int fd, const void *data, size_t len)
    256 {
    257         struct _IO_FILE *fp = fd_to_fp(fd);
    258         if (fp == NULL)
    259                 return -EBADF;
    260 
    261         return fp->write(fp, (unsigned char *)data, len);
    262 }
    263 
    264 int shell_writev(int fd, const struct iovec *iov, int iovcnt)
    265 {
    266         int result = 0;
    267         struct _IO_FILE *fp = fd_to_fp(fd);
    268         if (fp == NULL)
    269                 return -EBADF;
    270 
    271         const struct iovec *end = &iov[iovcnt];
    272         for (; iov < end; iov++) {
    273                 result += fp->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
    274         }
    275 
    276         return result;
    277 }
    278 
    279 int shell_llseek(int fd, off_t ptr, off_t *result, int dir)
    280 {
    281         struct _IO_FILE *fp = fd_to_fp(fd);
    282         if (fp == NULL)
    283                 return -EBADF;
    284 
    285         off_t ret = fp->seek(fp, ptr, dir);
    286         if (ret < 0)
    287                 return ret;
    288 
    289         *result = ret;
    290         return 0;
     244bool_t file_readable(struct SHELL_FILE *fp)
     245{
     246        return fp->readevt_w != fp->readevt_r;
     247}
     248
     249bool_t file_writable(struct SHELL_FILE *fp)
     250{
     251        return fp->writable && (fp->writeevt_w == fp->writeevt_r);
     252}
     253
     254void file_delete(struct SHELL_FILE *fp)
     255{
     256        free((FIL *)fp->exinf);
     257        fp->exinf = NULL;
    291258}
    292259
    293260int shell_fstat(int fd, struct stat * st)
    294261{
    295         struct _IO_FILE *fp = fd_to_fp(fd);
     262        struct SHELL_FILE *fp = fd_to_fp(fd);
    296263        if (fp == NULL)
    297264                return -EBADF;
     
    305272int shell_fsync(int fd)
    306273{
    307         struct _IO_FILE *fp = fd_to_fp(fd);
     274        struct SHELL_FILE *fp = fd_to_fp(fd);
    308275        if (fp == NULL)
    309276                return -EBADF;
     
    313280int shell_ftruncate(int fd, off_t length)
    314281{
    315         struct _IO_FILE *fp = fd_to_fp(fd);
     282        struct SHELL_FILE *fp = fd_to_fp(fd);
    316283        if (fp == NULL)
    317284                return -EBADF;
    318285
    319286        FRESULT res;
    320         if ((res = f_truncate(&fp->file)) != FR_OK)
     287        if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK)
    321288                return fresult2errno(res);
    322289
     
    329296}
    330297
    331 int sio_tcgetattr(int fd, struct termios *termios)
    332 {
    333         extern ntstdio_t ntstdio;
    334 
    335         if (fd == STDIN_FILENO) {
    336                 memset(termios, 0, sizeof(*termios));
    337 
    338                 if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) {
    339                         termios->c_lflag |= ECHO;
    340                 }
    341                 else {
    342                         termios->c_lflag &= ~ECHO;
    343                 }
    344                 if (ntstdio.option & NTSTDIO_OPTION_CANON) {
    345                         termios->c_lflag |= ICANON;
    346                 }
    347                 else {
    348                         termios->c_lflag &= ~ICANON;
    349                 }
    350                 if (ntstdio.option & NTSTDIO_OPTION_LF_CR) {
    351                         termios->c_iflag |= INLCR;
    352                 }
    353                 else {
    354                         termios->c_iflag &= ~INLCR;
    355                 }
    356                 if (ntstdio.option & NTSTDIO_OPTION_LF_CRLF) {
    357                         termios->c_oflag |= ONLCR;
    358                 }
    359                 else {
    360                         termios->c_oflag &= ~ONLCR;
    361                 }
    362                 return 0;
    363         }
    364         shell_abort();
    365         return 0;
    366 }
    367 
    368 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
    369 {
    370         extern ntstdio_t ntstdio;
    371 
    372         if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) {
    373                 if (termios->c_lflag & ECHO) {
    374                         ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO;
    375                 }
    376                 else {
    377                         ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO;
    378                 }
    379                 if (termios->c_lflag & ICANON) {
    380                         ntstdio.option |= NTSTDIO_OPTION_CANON;
    381                 }
    382                 else {
    383                         ntstdio.option &= ~NTSTDIO_OPTION_CANON;
    384                 }
    385                 if (termios->c_iflag & INLCR) {
    386                         ntstdio.option |= NTSTDIO_OPTION_LF_CR;
    387                 }
    388                 else{
    389                         ntstdio.option &= ~NTSTDIO_OPTION_LF_CR;
    390                 }
    391                 if (termios->c_oflag & ONLCR) {
    392                         ntstdio.option |= NTSTDIO_OPTION_LF_CRLF;
    393                 }
    394                 else {
    395                         ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF;
    396                 }
    397                 return 0;
    398         }
    399         shell_abort();
    400         return 0;
    401 }
    402 
    403 int shell_stat(const char *__restrict path, struct stat *__restrict st)
     298int shell_stat(const char *restrict path, struct stat *restrict st)
    404299{
    405300        FILINFO fi;
     
    434329        st->st_size = fi.fsize;
    435330        st->st_mtim.tv_nsec = 0;
    436         st->st_mtim.tv_sec = fi.fdate + fi.ftime;
    437         st->st_mode  = (S_IRUSR | S_IRGRP | S_IROTH);
     331        st->st_mtim.tv_sec = (time_t)fi.fdate + (time_t)fi.ftime;
     332        st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH);
    438333        st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH);
    439334        st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG;
     
    442337}
    443338
    444 int shell_lstat(const char *__restrict path, struct stat *__restrict st)
     339int shell_lstat(const char *restrict path, struct stat *restrict st)
    445340{
    446341        return shell_stat(path, st);
     
    494389        BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
    495390
    496         if(mode & S_IREAD) {
    497                 if((mode & S_IWRITE) == 0) {
     391        if (mode & S_IREAD) {
     392                if ((mode & S_IWRITE) == 0) {
    498393                        attr |= AM_RDO;
    499394                }
     
    503398        }
    504399
    505         if((res = f_chmod(path, attr, mask)) != FR_OK) {
     400        if ((res = f_chmod(path, attr, mask)) != FR_OK) {
    506401                return fresult2errno(res);
    507402        }
     
    516411        BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
    517412
    518         if(mode & S_IREAD) {
    519                 if((mode & S_IWRITE) == 0) {
     413        if (mode & S_IREAD) {
     414                if ((mode & S_IWRITE) == 0) {
    520415                        attr |= AM_RDO;
    521416                }
     
    525420        }
    526421
    527         if((res = f_chmod(path, attr, mask)) != FR_OK) {
     422        if ((res = f_chmod(path, attr, mask)) != FR_OK) {
    528423                return fresult2errno(res);
    529424        }
     
    535430{
    536431        FRESULT ret;
    537         if((ret = f_getcwd(buf, size)) != FR_OK) {
     432        if ((ret = f_getcwd(buf, size)) != FR_OK) {
    538433                return NULL;
    539434        }
     
    558453}
    559454
    560 int dir_close(struct _IO_FILE *fp)
    561 {
    562         FRESULT res;
    563         if ((res = f_closedir(&fp->dir)) != FR_OK) {
     455int dir_close(struct SHELL_FILE *fp)
     456{
     457        FRESULT res;
     458        if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    564459                return fresult2errno(res);
    565460        }
     
    573468                return -EINVAL;
    574469
    575         struct _IO_FILE *fp = fd_to_fp(fd);
     470        struct SHELL_FILE *fp = fd_to_fp(fd);
    576471        if (fp == NULL)
    577472                return -EBADF;
     
    584479#endif
    585480        FRESULT res;
    586         if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     481        if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
    587482                return fresult2errno(res);
    588483        }
     
    598493}
    599494
    600 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
     495size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    601496{
    602497        return -EPERM;
    603498}
    604499
    605 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
     500size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    606501{
    607502        return -EPERM;
    608503}
    609504
    610 off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     505off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
    611506{
    612507        FRESULT res;
     
    616511
    617512        if (ptr == 0) {
    618                 if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
     513                if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    619514                        return fresult2errno(res);
    620515                }
     
    627522                fno.lfsize = sizeof lfn;
    628523#endif
    629                 if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
     524                if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    630525                        return fresult2errno(res);
    631526                }
    632527
    633528                for (int i = 0; i < ptr; i++) {
    634                         if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     529                        if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
    635530                                return fresult2errno(res);
    636531                        }
     
    641536}
    642537
    643 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)
     538int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    644539{
    645540        return -EINVAL;
     541}
     542
     543bool_t dir_readable(struct SHELL_FILE *fp)
     544{
     545        return fp->readevt_w != fp->readevt_r;
     546}
     547
     548bool_t dir_writable(struct SHELL_FILE *fp)
     549{
     550        return false;
     551}
     552
     553void dir_delete(struct SHELL_FILE *fp)
     554{
     555        free((struct SHELL_DIR *)fp->exinf);
     556        fp->exinf = NULL;
    646557}
    647558
     
    662573        return 0;
    663574}
    664 
    665 //#include "../musl-1.1.18/include/bits/limits.h"
    666 #define PAGE_SIZE 4096
    667 
    668 uint32_t  __CmdBase;
    669 uint32_t  __CmdLimit;
    670 
    671 void *shell_brk(void *addr)
    672 {
    673         if (addr == 0) {
    674                 return (void *)((intptr_t)&__CmdBase + 0x20000);
    675         }
    676         if ((addr >= (intptr_t)&__CmdBase + 0x20000) && (addr < &__CmdLimit)) {
    677                 return addr;
    678         }
    679         return (void *)-1;
    680 }
    681 
    682 void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
    683 {
    684         if (fd != -1)
    685                 return -EINVAL;
    686 
    687         if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - (intptr_t)&__CmdBase - 0x20000)) {
    688                 return &__CmdBase + 0x20000;
    689         }
    690         return (void *)-1;
    691 }
    692 
    693 int shell_mprotect(void *addr, size_t len, int prot)
    694 {
    695         //if ((addr >= (intptr_t)&__CmdBase + 0x20000) && ((intptr_t)addr + len < &__CmdLimit)) {
    696                 return 0;
    697         //}
    698         //return -1;
    699 }
  • EcnlProtoTool/trunk/ntshell/src/main.c

    r434 r441  
    4848#include "syssvc/serial.h"
    4949#include "syssvc/syslog.h"
     50#include "target_syssvc.h"
    5051#include "kernel_cfg.h"
    5152#include "main.h"
    52 #include "rza1.h"
     53#include "fdtable.h"
     54#ifndef NTSHELL_NO_SOCKET
    5355#include <tinet_config.h>
    5456#include <netinet/in.h>
     
    7072#include "usrcmd.h"
    7173#include "util/ntopt.h"
    72 #include "fdtable.h"
    73 #include "target_syssvc.h"
     74#include "netapp/dhcp4_cli.h"
     75#include "ntp_cli.h"
     76#include "netcmd.h"
     77#endif
    7478
    7579ID ws_api_mailboxid = MAIN_DATAQUEUE;
    7680ID ws_mempoolid = MPF_NET_BUF_256;
     81extern ntstdio_t *ntstdio;
    7782
    7883char command[NTOPT_TEXT_MAXLEN];
    79 ntstdio_t ntstdio;
    80 
    81 unsigned char ntstdio_xi(struct ntstdio_t *handle)
    82 {
    83         char buf[1];
    84         if(serial_rea_dat(SIO_PORTID, buf, 1) != 1)
    85                 return -EIO;
    86         return buf[0];
    87 }
    88 
    89 void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
    90 {
    91         char buf[1];
    92         buf[0] = c;
    93         serial_wri_dat(SIO_PORTID, buf, 1);
    94 }
    9584
    9685const uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};
     
    10190        "3.5.0",
    10291        TARGET_NAME,
    103     "toppers.jp"
     92        "toppers.jp"
    10493};
    10594
     
    113102        main_state_start,
    114103        main_state_idle,
     104#ifndef NTSHELL_NO_SOCKET
     105        main_state_start_dhcp,
     106#endif
     107        main_state_servey_cmd,
    115108};
    116109
    117110struct main_obj_t {
    118111        ntshell_t ntshell;
    119         TMO timer;
     112        int timer;
    120113        enum main_state_t state;
    121114        SYSTIM prev, now;
    122         WS_FBS_ID rcvmsg;
    123         T_WS_FBS_QUEUE wbs_queue;
    124         WS_FBS_ID sndmsg;
     115        task_base_t **tasks;
     116        int task_count;
     117#ifndef NTSHELL_NO_SOCKET
     118        uint8_t link;
     119        uint8_t link_up;
     120        uint8_t up;
     121        uint8_t dhcp;
     122        uint8_t ntp;
     123        ntp_cli_state_t ntp_state;
     124#endif
     125        bool_t exec;
     126        int event_req, event_res;
    125127};
    126128struct main_obj_t main_obj;
    127129
    128 static void main_change_netif_link(T_IFNET *ether);
     130#ifndef NTSHELL_NO_SOCKET
     131static void netif_link_callback(T_IFNET *ether);
     132static void main_change_netif_link(uint8_t link_up, uint8_t up);
     133#endif
    129134static void main_initialize(struct main_obj_t *obj);
    130 static TMO main_get_timer(struct main_obj_t *obj);
    131 static void main_progress(struct main_obj_t *obj, TMO elapse);
    132 static void main_timeout(struct main_obj_t *obj);
    133 extern int execute_command(int wait);
     135static void main_finalize(struct main_obj_t *obj);
     136static int main_get_timer(struct main_obj_t *obj);
     137static void main_progress(struct main_obj_t *obj, int elapse);
     138static void main_timeout(struct main_obj_t *obj, bool_t wakeup);
     139static int execute_command(struct main_obj_t *obj, int wait);
    134140static int usrcmd_ntopt_callback(long *args, void *extobj);
    135 
     141#ifndef NTSHELL_NO_SOCKET
     142static void ntp_cli_state_changed(ntp_cli_state_t state);
     143#endif
     144
     145void stdio_open(ID portid);
     146void stdio_input(unsigned char c);
     147
     148bool_t dhcp_enable;
     149int ntshell_exit;
    136150int shellcmd_exit_code;
    137151volatile int shellcmd_state;
     
    139153jmp_buf shellcmd_exit;
    140154
     155void main_task_init(task_base_t **tasks, int task_count)
     156{
     157        main_obj.tasks = tasks;
     158        main_obj.task_count = task_count;
     159}
     160
    141161int uart_read(char *buf, int cnt, void *extobj)
    142162{
    143         return serial_rea_dat(SIO_PORTID, (char *)buf, cnt);
     163        struct main_obj_t *obj = (struct main_obj_t *)extobj;
     164        int result;
     165        ER ret;
     166        int timer;
     167        bool_t wakeup = false;
     168
     169        obj->prev = obj->now;
     170
     171        if (obj->event_req != obj->event_res) {
     172                obj->event_res = obj->event_req;
     173                wakeup = true;
     174#ifndef NTSHELL_NO_SOCKET
     175                if (obj->dhcp) {
     176                        obj->dhcp = 0;
     177                        obj->state = main_state_start_dhcp;
     178                }
     179#endif
     180                obj->timer = 0;
     181        }
     182
     183        /* タイマー取得 */
     184        timer = main_get_timer(obj);
     185
     186        /* 待ち */
     187        ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
     188        if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT) && (ret != E_RLWAI)) {
     189                syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
     190                ntshell_exit = 1;
     191                return -1;
     192        }
     193        if (ret != E_TMOUT)
     194                wakeup = true;
     195        result = (int)ret;
     196
     197        ret = get_tim(&obj->now);
     198        if (ret != E_OK) {
     199                syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
     200                ntshell_exit = 1;
     201                return -1;
     202        }
     203
     204        /* 時間経過 */
     205        int elapse = obj->now - obj->prev;
     206        main_progress(obj, elapse);
     207
     208        /* タイムアウト処理 */
     209        main_timeout(obj, wakeup);
     210
     211        return result;
    144212}
    145213
     
    162230        }
    163231
    164         return execute_command(tskid == MAIN_TASK);
    165 }
     232        return execute_command(&main_obj, tskid == MAIN_TASK);
     233}
     234
     235gpio_t led_blue, led_green, led_red, sw;
    166236
    167237/*
    168  * メインタスク
     238 *  メインタスク
    169239 */
    170240void main_task(intptr_t exinf)
    171241{
    172242        struct main_obj_t *obj = (struct main_obj_t *)&main_obj;
     243
     244        gpio_init_out(&led_blue, LED_BLUE);
     245        gpio_init_out(&led_green, LED_GREEN);
     246        gpio_init_out(&led_red, LED_RED);
     247        gpio_init_in(&sw, USER_BUTTON0);
     248
     249        obj->exec = gpio_read(&sw) == 1;
     250
     251        gpio_write(&led_blue, 1);
     252        gpio_write(&led_green, obj->exec ? 1 : 0);
     253        gpio_write(&led_red, 0);
     254
     255        ether_set_link_callback(main_change_netif_link);
     256
     257        /* 初期化 */
     258        ffarch_init();
     259
     260        stdio_open(SIO_PORTID);
     261
     262#ifndef NTSHELL_NO_SOCKET
     263        ntp_cli_set_state_changed_cb(ntp_cli_state_changed);
     264#endif
    173265
    174266        main_initialize(obj);
     
    177269        ntshell_set_prompt(&obj->ntshell, "NTShell>");
    178270        ntshell_execute(&obj->ntshell);
     271
     272        main_finalize(obj);
    179273}
    180274
     
    184278static void main_initialize(struct main_obj_t *obj)
    185279{
    186         FILINFO fno;
    187 #if FF_USE_LFN
    188         char lfn[FF_MAX_LFN + 1];
    189         fno.lfname = lfn;
    190         fno.lfsize = FF_MAX_LFN + 1;
    191 #endif
    192280        ER ret;
    193 
    194         ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);
    195 
    196         obj->timer = TMO_FEVR;
     281        ID tskid = 0;
     282
     283        ret = get_tid(&tskid);
     284        if (ret != E_OK) {
     285                syslog(LOG_ERROR, "get_tid %d", ret);
     286                return;
     287        }
     288
     289        obj->timer = 100000;
    197290        obj->state = main_state_start;
    198291
    199         gpio_t led_blue, led_green, led_red, sw;
    200         gpio_init_out(&led_blue, LED_BLUE);
    201         gpio_init_out(&led_green, LED_GREEN);
    202         gpio_init_out(&led_red, LED_RED);
    203         gpio_init_in(&sw, USER_BUTTON0);
    204 
    205         bool_t exec = gpio_read(&sw) == 1;
    206 
    207         gpio_write(&led_blue, 1);
    208         gpio_write(&led_green, exec ? 1 : 0);
    209         gpio_write(&led_red, 0);
    210 
    211         ether_set_link_callback(main_change_netif_link);
    212 
    213         /* 初期化 */
    214         if (mruby_arduino_init() == 0) {
    215                 gpio_write(&led_blue, 0);
    216         }
    217         else {
    218                 gpio_write(&led_blue, 0);
    219                 gpio_write(&led_red, 1);
    220         }
    221 
     292        for (int i = 0; i < obj->task_count; i++) {
     293                task_base_t *task = obj->tasks[i];
     294                task->on_start(task, tskid);
     295        }
     296
     297        gpio_write(&led_blue, 0);
    222298        gpio_write(&led_green, 0);
    223299
    224300        act_tsk(HTTPD1_TASK);
    225301        act_tsk(HTTPD2_TASK);
    226 
    227         if (f_stat("1:/upload/main.mrb", &fno) != FR_OK) {
    228                 exec = false;
    229         }
    230 
    231         if (exec) {
    232                 ntlibc_strlcpy(command, "mruby -b 1:/upload/main.mrb", sizeof(command));
    233 
    234                 execute_command(1);
    235         }
    236302
    237303        ret = get_tim(&obj->now);
     
    243309}
    244310
     311static void main_finalize(struct main_obj_t *obj)
     312{
     313        for (int i = 0; i < obj->task_count; i++) {
     314                task_base_t *task = obj->tasks[i];
     315                task->on_end(task);
     316        }
     317}
     318
    245319/*
    246320 * タイマー取得
    247321 */
    248 static TMO main_get_timer(struct main_obj_t *obj)
    249 {
    250         TMO timer = obj->timer;
     322static int main_get_timer(struct main_obj_t *obj)
     323{
     324        int timer = obj->timer;
     325
     326        for (int i = 0; i < obj->task_count; i++) {
     327                task_base_t *task = obj->tasks[i];
     328                int timer2 = task->get_timer(task);
     329                if ((timer == -1) || ((timer2 != -1) && (timer > timer2)))
     330                        timer = timer2;
     331        }
    251332
    252333        return timer;
     
    256337 * 時間経過
    257338 */
    258 static void main_progress(struct main_obj_t *obj, TMO elapse)
     339static void main_progress(struct main_obj_t *obj, int elapse)
    259340{
    260341        if (obj->timer != TMO_FEVR) {
     
    264345                }
    265346        }
     347
     348        for (int i = 0; i < obj->task_count; i++) {
     349                task_base_t *task = obj->tasks[i];
     350                task->progress(task, elapse);
     351        }
    266352}
    267353
     
    269355 * タイムアウト処理
    270356 */
    271 static void main_timeout(struct main_obj_t *obj)
    272 {
    273         //if (obj->timer == 0) {
    274         //}
    275 }
    276 
     357static void main_timeout(struct main_obj_t *obj, bool_t wakeup)
     358{
     359        ER ret;
     360        uint32_t event = wakeup ? MAIN_EVENT_WAKEUP : 0;
     361
     362        if (!wakeup && (obj->timer != 0))
     363                return;
     364
     365        switch (obj->state) {
     366        case main_state_start:
     367#ifndef NTSHELL_NO_SOCKET
     368                ether_set_link_callback(netif_link_callback);
     369#endif
     370                {
     371                        FILINFO fno;
     372#if FF_USE_LFN
     373                        char lfn[FF_MAX_LFN + 1];
     374                        fno.lfname = lfn;
     375                        fno.lfsize = FF_MAX_LFN + 1;
     376#endif
     377                        if (f_stat("1:/upload/main.mrb", &fno) != FR_OK) {
     378                                obj->exec = false;
     379                        }
     380
     381                        if (obj->exec) {
     382                                cmd_execute("mruby -b 1:/upload/main.mrb", obj);
     383                        }
     384                }
     385                obj->state = main_state_idle;
     386                obj->timer = TMO_FEVR;
     387                break;
     388#ifndef NTSHELL_NO_SOCKET
     389        case main_state_start_dhcp:
     390                ret = dhcp4c_renew_info();
     391                if (ret == E_OK) {
     392                        obj->state = main_state_idle;
     393                        obj->timer = TMO_FEVR;
     394                }
     395                else {
     396                        obj->state = main_state_start_dhcp;
     397                        obj->timer = 1000000;
     398                }
     399                break;
     400#endif
     401        default:
     402                obj->state = main_state_idle;
     403                obj->timer = TMO_FEVR;
     404                break;
     405        }
     406#ifndef NTSHELL_NO_SOCKET
     407        if (obj->link) {
     408                obj->link = 0;
     409                event |= MAIN_EVENT_NETIF_CHANGED;
     410                if (obj->link_up)
     411                        event |= MAIN_EVENT_LINK_UP;
     412                if (obj->up)
     413                        event |= MAIN_EVENT_UP;
     414
     415                if (obj->link_up && obj->up)
     416                        ntp_cli_execute();
     417
     418                main_change_netif_link(obj->link_up, obj->up);
     419        }
     420        if (obj->ntp) {
     421                obj->ntp = 0;
     422                if (obj->ntp_state != NTP_CLI_STATE_SYNC)
     423                        event |= MAIN_EVENT_NTP_ASYNC;
     424                else
     425                        event |= MAIN_EVENT_NTP_SYNC;
     426        }
     427#endif
     428        for (int i = 0; i < obj->task_count; i++) {
     429                task_base_t *task = obj->tasks[i];
     430                task->process(task, event);
     431        }
     432}
     433
     434#ifndef NTSHELL_NO_SOCKET
    277435/* MACアドレスの設定時に呼ばれる */
    278436void mbed_mac_address(char *mac)
     
    281439}
    282440
    283 static void main_change_netif_link(T_IFNET *ether)
     441void main_change_netif_link(uint8_t link_up, uint8_t up)
    284442{
    285443        FLGPTN flgptn;
     
    295453        set_flg(FLG_SELECT_WAIT, flgptn);
    296454}
     455
     456void netif_link_callback(T_IFNET *ether)
     457{
     458        struct main_obj_t *obj = (struct main_obj_t *)&main_obj;
     459        uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
     460        uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
     461        bool_t wake = false;
     462
     463        if (dhcp_enable) {
     464                if (!link_up)
     465                        dhcp4c_rel_info();
     466                else if (!up) {
     467                        wake = dhcp4c_renew_info() != E_OK;
     468                        if (wake) {
     469                                obj->dhcp = 1;
     470                        }
     471                }
     472        }
     473        else {
     474                up = link_up;
     475        }
     476
     477        if ((obj->link_up != link_up) || (obj->up != up)) {
     478                obj->link = 1;
     479                wake = true;
     480        }
     481
     482        obj->link_up = link_up;
     483        obj->up = up;
     484
     485        if (wake && (obj->event_req == obj->event_res)) {
     486                obj->event_req++;
     487                rel_wai(MAIN_TASK);
     488        }
     489}
     490
     491void ntp_cli_state_changed(ntp_cli_state_t state)
     492{
     493        struct main_obj_t *obj = (struct main_obj_t *)&main_obj;
     494        bool_t wake = obj->ntp_state != state;
     495
     496        if (wake) {
     497                obj->ntp_state = state;
     498                obj->ntp = 1;
     499
     500                if (obj->event_req == obj->event_res) {
     501                        obj->event_req++;
     502                        rel_wai(MAIN_TASK);
     503                }
     504        }
     505}
     506#endif
    297507
    298508/*
     
    302512{
    303513        shellcmd_state = 1;
    304         shellcmd_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
     514
     515        if (setjmp(shellcmd_exit) == 0) {
     516                shellcmd_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
     517        }
     518
     519        //fflush(stdout);
     520        clean_fd();
     521
    305522        shellcmd_state = 2;
    306523}
     
    315532        {"hexdump", "Hex dump", usrcmd_hexdump},
    316533        {"date", "print date and time", usrcmd_date},
     534#ifndef NTSHELL_NO_SOCKET
     535        {"ping", "ping", usrcmd_ping},
     536        {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
     537        {"dnsc", "DNS client", usrcmd_dnsc },
     538        {"ntpc", "NTP client", usrcmd_ntpc },
     539#endif
    317540        {"info", "This is a description text string for info command.", usrcmd_info},
    318541        {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
     
    330553
    331554        if (ntlibc_strcmp((const char *)args[1], "help") == 0) {
    332                 usrcmd_help(args[0], (char **)&args[1]);
     555                found = 1;
     556                result = usrcmd_help(args[0], (char **)&args[1]);
    333557        }
    334558        else for (int i = 0; i < cmd_table_info.count; i++) {
    335559                if (ntlibc_strcmp((const char *)args[1], p->cmd) == 0) {
    336                         return p->func(args[0], (char **)&args[1]);
     560                        found = 1;
     561                        result = p->func(args[0], (char **)&args[1]);
     562                        break;
    337563                }
    338564                p++;
    339565        }
    340         if (setjmp(shellcmd_exit) == 0) {
    341                 (*((PowerOn_Reset_t *)0x18220000))(args);
    342         }
    343         clean_fd();
    344         return 0;
     566
     567        if ((found == 0) && setjmp(shellcmd_exit) == 0) {
     568                found = 1;
     569                (*((PowerOn_Reset_t *)0x18200000))(args);
     570        }
     571
     572        if ((found == 0) && (((const char *)args[1])[0] != '\0'))
     573                ntstdio_printf(ntstdio, "Unknown command found. %s \n", (const char *)args[1]);
     574
     575        return result;
    345576}
    346577
     
    349580        const cmd_table_t *p = cmd_table_info.table;
    350581        for (int i = 0; i < cmd_table_info.count; i++) {
    351                 ntstdio_puts(&ntstdio, p->cmd);
    352                 ntstdio_puts(&ntstdio, "\t:");
    353                 ntstdio_puts(&ntstdio, p->desc);
    354                 ntstdio_puts(&ntstdio, "\n");
     582                ntstdio_puts(ntstdio, p->cmd);
     583                ntstdio_puts(ntstdio, "\t:");
     584                ntstdio_puts(ntstdio, p->desc);
     585                ntstdio_puts(ntstdio, "\n");
    355586                p++;
    356587        }
     
    372603void shell_abort()
    373604{
    374         asm("bkpt #0");
    375 
    376605        shellcmd_exit_code = -1;
    377606        longjmp(shellcmd_exit, 1);
     
    390619}
    391620
    392 int execute_command(int wait)
    393 {
     621int execute_command(struct main_obj_t *obj, int wait)
     622{
     623        T_RTSK rtsk;
    394624        ER ret;
    395625
     
    399629        }
    400630
    401         tslp_tsk(100000);
     631        if (ret == E_OK)
     632                tslp_tsk(100000);
    402633
    403634        clean_fd();
     
    413644
    414645        do {
    415                 tslp_tsk(100000);
     646                obj->state = main_state_servey_cmd;
     647                obj->timer = 100000;
     648
     649                char c;
     650                if (obj->ntshell.func_read(&c, sizeof(c), obj->ntshell.extobj) == 1) {
     651                        // Ctrl+Cが押された場合
     652                        if (c == 0x03) {
     653                                // コマンドタスクを終了
     654                                ret = ter_tsk(SHELLCMD_TASK);
     655                                if (ret != E_OK) {
     656                                        syslog(LOG_ERROR, "ter_tsk => %d", ret);
     657                                }
     658                                shellcmd_exit_code = -1;
     659                                shellcmd_state = 4;
     660                                if (ret == E_OK)
     661                                        tslp_tsk(100000);
     662                                break;
     663                        }
     664
     665                        stdio_input(c);
     666                }
     667
     668                ret = ref_tsk(SHELLCMD_TASK, &rtsk);
     669                if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
     670                        shellcmd_state = 3;
    416671        } while(shellcmd_state == 1);
    417672
     673        obj->state = main_state_idle;
     674        obj->timer = TMO_FEVR;
     675
     676        clean_fd();
     677
    418678        return shellcmd_exit_code;
    419679}
     
    421681int shell_clock_getres(clockid_t clk_id, struct timespec *res)
    422682{
    423         if (clk_id != CLOCK_REALTIME)
     683        if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    424684                return -EINVAL;
    425685
     
    434694        SYSTIM now = 0;
    435695
    436         if (clk_id != CLOCK_REALTIME)
     696        if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    437697                return -EINVAL;
    438698
     
    446706int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
    447707{
    448         if (clk_id != CLOCK_REALTIME)
     708        if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    449709                return -EINVAL;
    450710
    451         rtc_write(tp->tv_sec);
     711        SYSTIM time;
     712        ER ret;
     713
     714        time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
     715
     716        ret = set_tim(time);
     717        if (ret != E_OK) {
     718                return -EPERM;
     719        }
    452720
    453721        return 0;
     
    482750}
    483751
    484 struct sigaction sigtable[6];
    485 
    486 int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
    487 {
    488         struct sigaction *sat;
     752// musl-1.1.18\src\internal\ksigaction.h
     753struct k_sigaction {
     754        void(*handler)(int);
     755        unsigned long flags;
     756        void(*restorer)(void);
     757        unsigned mask[2];
     758};
     759
     760struct k_sigaction sigtable[7];
     761
     762int shell_sigaction(int sig, const struct k_sigaction *restrict sa,
     763        struct k_sigaction *restrict old, size_t size)
     764{
     765        struct k_sigaction *sat;
    489766
    490767        switch(sig){
     
    507784                sat = &sigtable[5];
    508785                break;
     786        case SIGPIPE:
     787                sat = &sigtable[6];
     788                break;
    509789        default:
    510790                return -EINVAL;
     
    512792
    513793        if (old != NULL)
    514                 memcpy(old, sat, sizeof(struct sigaction));
    515 
    516         memcpy(sat, sa, sizeof(struct sigaction));
     794                memcpy(old, sat, offsetof(struct k_sigaction, mask) + size);
     795
     796        memcpy(sat, sa, offsetof(struct k_sigaction, mask) + size);
    517797
    518798        return 0;
     
    524804}
    525805
    526 int stdio_close(struct _IO_FILE *fp)
    527 {
    528         return -EPERM;
    529 }
    530 
    531 size_t stdio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
    532 {
    533         return -EPERM;
    534 }
    535 
    536 size_t stdio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
    537 {
    538         return -EPERM;
    539 }
    540 
    541 size_t stdin_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
    542 {
    543         int i = 0;
    544         while (i < len) {
    545                 int c = ntstdio_getc(&ntstdio);
    546                 data[i++] = c;
    547                 if ((c == EOF) || (c == '\n'))
    548                         break;
    549         }
    550         return i;
    551 }
    552 
    553 size_t stdout_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
    554 {
    555         for (int i = 0; i < len; i++) {
    556                 ntstdio_putc(&ntstdio, data[i]);
    557         }
    558         return len;
    559 }
    560 
    561 size_t stderr_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
    562 {
    563         for (int i = 0; i < len; i++) {
    564                 ntstdio_putc(&ntstdio, data[i]);
    565         }
    566         return len;
    567 }
    568 
    569 int sio_close(struct _IO_FILE *fp)
    570 {
    571         return -EPERM;
    572 }
    573 
    574 size_t sio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
    575 {
    576         return -EPERM;
    577 }
    578 
    579 size_t sio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
    580 {
    581         return -EPERM;
    582 }
    583 
    584 off_t sio_seek(struct _IO_FILE *fp, off_t ofs, int org)
    585 {
    586         return -EPERM;
    587 }
    588 
    589 int sio_ioctl(struct _IO_FILE *fp, int request, void *arg)
    590 {
    591         switch (request) {
    592         case TIOCGWINSZ:
     806int shell_gettid()
     807{
     808        ID tskid;
     809        ER ret;
     810
     811        ret = get_tid(&tskid);
     812        if (ret != E_OK)
     813                return -1;
     814
     815        return tskid;
     816}
     817
     818int shell_tkill(int tid, int sig)
     819{
     820        if ((tid == SHELLCMD_TASK) && (sig == SIGABRT)) {
     821                shell_abort();
     822        }
     823
     824        no_implement("tkill");
     825        return -1;
     826}
     827
     828int shell_kill(int pid, int sig)
     829{
     830        DebugBreak();
     831        return -1;
     832}
     833
     834int shell_gettimeofday(struct timeval *tv, void *tzvp)
     835{
     836        SYSTIM time;
     837        if (!tv) return 0;
     838        get_tim(&time);
     839        tv->tv_sec = time / 1000000;
     840        tv->tv_usec = time - (tv->tv_sec * 1000000);
     841        return 0;
     842}
     843
     844int shell_nanosleep(const struct timespec *req, struct timespec *rem)
     845{
     846        ER ret;
     847        TMO tmo;
     848        SYSTIM prev, now, diff;
     849
     850        if ((req == NULL) || (req->tv_nsec < 0) || (req->tv_nsec >= 1000000000))
     851                return -EINVAL;
     852
     853        get_tim(&prev);
     854
     855        tmo = req->tv_sec * 1000000 + req->tv_nsec / 1000;
     856        ret = tslp_tsk(tmo);
     857        if (ret == E_OK) {
     858                if (rem != NULL) {
     859                        get_tim(&now);
     860                        diff = now - prev;
     861                        rem->tv_sec = diff / 1000000ll;
     862                        rem->tv_nsec = (diff - (rem->tv_sec * 1000000ll)) * 1000ll;
     863                }
    593864                return 0;
    594         case TCGETS:
    595                 return sio_tcgetattr(fp->fd, (struct termios *)arg);
    596         case TCSETS + TCSANOW:
    597         case TCSETS + TCSADRAIN:
    598         case TCSETS + TCSAFLUSH:
    599                 return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
    600         }
    601 
    602         return -EINVAL;
    603 }
    604 
     865        }
     866        else if (ret == E_TMOUT) {
     867                if (rem != NULL) {
     868                        rem->tv_sec = 0;
     869                        rem->tv_nsec = 0;
     870                }
     871                return 0;
     872        }
     873
     874        return -EFAULT;
     875}
     876
     877ssize_t shell_getrandom(void *buf, size_t buflen, unsigned int flags)
     878{
     879        SYSTIM now;
     880        int32_t i;
     881        int *output = (int *)buf;
     882        size_t sz = buflen / 4;
     883
     884        get_tim(&now);
     885        srand(now);
     886
     887        for (i = 0; i < sz; i++)
     888                output[i] = rand();
     889
     890        for (i = 4 * sz; i < buflen; i++)
     891                ((char *)buf)[i] = rand();
     892
     893        return buflen;
     894}
     895
     896extern uint32_t  __CmdBase;
     897extern uint32_t  __CmdLimit;
     898
     899void *shell_brk(void *addr)
     900{
     901        if (addr == 0) {
     902                return (void *)((intptr_t)&__CmdBase + 0x40000);
     903        }
     904        if ((addr >= (intptr_t)&__CmdBase + 0x40000) && (addr < &__CmdLimit)) {
     905                return addr;
     906        }
     907        return (void *)-1;
     908}
     909
     910void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
     911{
     912        if (fd != -1)
     913                return -EINVAL;
     914
     915        if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - ((intptr_t)&__CmdBase + 0x40000))) {
     916                return &__CmdBase + 0x40000;
     917        }
     918        return (void *)-1;
     919}
     920
     921int shell_mprotect(void *addr, size_t len, int prot)
     922{
     923        //if ((addr >= (intptr_t)&__CmdBase + 0x40000) && ((intptr_t)addr + len < &__CmdLimit)) {
     924                return 0;
     925        //}
     926        //return -1;
     927}
  • EcnlProtoTool/trunk/ntshell/src/main.cdl

    r321 r441  
    11/*
    2  *              サンプルプログラム(1)のコンポーネント記述ファイル
    3  *
     2 *  TOPPERS/ASP Kernel
     3 *      Toyohashi Open Platform for Embedded Real-Time Systems/
     4 *      Advanced Standard Profile Kernel
     5 *
     6 *  Copyright (C) 2015 by Ushio Laboratory
     7 *              Graduate School of Engineering Science, Osaka Univ., JAPAN
     8 *  Copyright (C) 2015,2016 by Embedded and Real-Time Systems Laboratory
     9 *              Graduate School of Information Science, Nagoya Univ., JAPAN
     10 *
     11 *  上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
     12 *  ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
     13 *  変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
     14 *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
     15 *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
     16 *      スコード中に含まれていること.
     17 *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
     18 *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
     19 *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
     20 *      の無保証規定を掲載すること.
     21 *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
     22 *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
     23 *      と.
     24 *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
     25 *        作権表示,この利用条件および下記の無保証規定を掲載すること.
     26 *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
     27 *        報告すること.
     28 *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
     29 *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
     30 *      また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
     31 *      由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
     32 *      免責すること.
     33 *
     34 *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
     35 *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
     36 *  に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
     37 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
     38 *  の責任を負わない.
     39 *
    440 *  $Id$
    541 */
     
    1955import("syssvc/tBanner.cdl");
    2056
     57import_C("target_syssvc.h");
     58
    2159/*
    22  *  ターゲット依存部の取り込み
     60 *  タスクのスタックサイズのデフォルト
    2361 */
    24 import("target.cdl");
     62const size_t DefaultTaskStackSize = 1024;               /* スタックサイズ(1KB)*/
     63
     64/*
     65 *  システムログタスクのスタックサイズの定義
     66 */
     67const size_t LogTaskStackSize = DefaultTaskStackSize;
     68
     69/*
     70 *  カーネル起動メッセージに関する定義
     71 */
     72const char *const BannerTargetName = C_EXP("TARGET_NAME");              /* ターゲット名 */
     73const char *const BannerCopyrightNotice = "";                   /* 著作権表示 */
     74
     75/*
     76 *  ターゲット依存のセルタイプの定義
     77 */
     78import("syssvc/tPutLogSIOPort.cdl");
     79import("tSIOPortNTShell.cdl");
     80
     81/*
     82 *  シリアルインタフェースドライバのターゲット依存部の組み上げ記述
     83 */
     84cell tSIOPortNTShell SIOPortTarget1 {
     85};
     86
     87/*
     88 *  低レベル出力の組み上げ記述
     89 */
     90cell tPutLogSIOPort PutLogTarget {
     91        /* SIOドライバとの結合 */
     92        cSIOPort = SIOPortTarget1.eSIOPort;
     93};
    2594
    2695/*
  • EcnlProtoTool/trunk/ntshell/src/main.cfg

    r434 r441  
    4848#include <net/if6_var.h>
    4949
     50ATT_INI({ TA_NULL, 0, sys_init });
     51
    5052INCLUDE("tecsgen.cfg");
    5153INCLUDE("tinet/tinet_asp.cfg");
    5254INCLUDE("tinet_main.cfg");
    53 /*#include "netapp/dhcp4_cli.h"*/
    54 /*INCLUDE("netapp/dhcp4_cli_asp.cfg");*/
     55
     56/* DHCP クライアント */
     57#include "netapp/dhcp4_cli.h"
     58//INCLUDE("netapp/dhcp4_cli_asp.cfg");
     59INCLUDE("netapp/tinet_dhcp4_cli.cfg");
     60
     61/* DNSクライアント */
     62#include "netapp/resolver.h"
     63INCLUDE("netapp/resolver_asp.cfg");
     64INCLUDE("netapp/tinet_resolver.cfg");
     65
     66/* NTPクライアント */
     67#include "ntp_cli.h"
     68//INCLUDE("ntp_cli.cfg");
     69INCLUDE("tinet_ntp_cli.cfg");
     70
     71#include "net_misc.h"
     72INCLUDE("net_misc.cfg");
     73
     74/* FatFs */
    5575INCLUDE("ffarch.cfg");
    5676INCLUDE("httpd.cfg");
     
    5979CRE_TSK(MAIN_TASK, { TA_ACT, 0, main_task, MAIN_PRIORITY, MAIN_STACK_SIZE, NULL });
    6080CRE_DTQ(MAIN_DATAQUEUE, { TA_TFIFO, NUM_MAIN_DATAQUEUE, NULL });
    61 CRE_SEM(SEM_FILESYSTEM, { TA_TPRI, 1, 1 });
    6281
    63 CRE_TSK(SHELLCMD_TASK, { TA_NULL, 0, shellcmd_task, SHELLCMD_PRIORITY, SHELLCMD_STACK_SIZE, (void *)0x20100000 });
     82CRE_TSK(SHELLCMD_TASK, { TA_FPU, 0, shellcmd_task, SHELLCMD_PRIORITY, SHELLCMD_STACK_SIZE, (void *)0x20100000 });
    6483CRE_FLG(FLG_SELECT_WAIT, { TA_WMUL, 0x00 });
     84CRE_SEM(SEM_FILEDESC, { TA_TPRI, 1, 1 });
  • EcnlProtoTool/trunk/ntshell/src/main.h

    r435 r441  
    5656
    5757#define MAIN_PRIORITY   5                       /* メインタスクの優先度 */
    58 #define SHELLCMD_PRIORITY       4               /* shellcmdタスクの優先度 */
     58#define SHELLCMD_PRIORITY       8               /* shellcmdタスクの優先度 */
    5959
    60 #define MAIN_STACK_SIZE         2048    /* メインタスクのスタック領域のサイズ */
     60#define MAIN_STACK_SIZE 2048            /* メインタスクのスタック領域のサイズ */
    6161#define SHELLCMD_STACK_SIZE     0x00800000      /* shellcmdタスクのスタック領域のサイズ */
    6262
    6363#define NUM_MAIN_DATAQUEUE      1               /* メインタスクで待ち受けているデータキューのサイズ */
    64 #define NUM_SHELLCMD_DATAQUEUE  1       /* shellcmdタスクで待ち受けているデータキューのサイズ */
     64
    6565
    6666/*
     
    6969#ifndef TOPPERS_MACRO_ONLY
    7070
     71typedef struct task_base_t {
     72        void (*on_start)(struct task_base_t *task, ID tskid);
     73        void (*on_end)(struct task_base_t *task);
     74        int (*get_timer)(struct task_base_t *task);
     75        void (*progress)(struct task_base_t *task, int elapse);
     76        void (*process)(struct task_base_t *task, uint32_t event);
     77        int timer;
     78        void *exinf;
     79} task_base_t;
     80
    7181typedef struct
    7282{
     
    7484        int count;
    7585} cmd_table_info_t;
     86
     87extern cmd_table_info_t cmd_table_info;
     88
     89/* ntshellの初期化 */
     90void sys_init(intptr_t exinf);
     91
     92#define MAIN_EVENT_WAKEUP               0x00000001
     93#define MAIN_EVENT_NETIF_CHANGED        0x00000002
     94#define MAIN_EVENT_LINK_UP              0x00000004
     95#define MAIN_EVENT_UP                   0x00000008
     96#define MAIN_EVENT_NTP_ASYNC            0x00000010
     97#define MAIN_EVENT_NTP_SYNC             0x00000020
     98
     99/* メインタスク初期化 */
     100void main_task_init(task_base_t **tasks, int task_count);
    76101
    77102/* メインタスク */
  • EcnlProtoTool/trunk/ntshell/src/mbedcall.c

    r439 r441  
    375375                break;
    376376        default:
    377                 asm("bkpt #0");
     377                DebugBreak();
    378378                break;
    379379        }
  • EcnlProtoTool/trunk/ntshell/src/shellif.h

    r439 r441  
    5050 *  @(#) $Id$
    5151 */
    52 #ifndef SHELLIF_H
    53 #define SHELLIF_H
     52#ifndef _SHELLIF_H_
     53#define _SHELLIF_H_
    5454
    5555#ifdef _MSC_VER
    56 #include <time.h>
     56#undef NULL
    5757#endif
    5858
     
    9292#include "../musl-1.1.18/include/bits/stat.h"
    9393#include "../musl-1.1.18/include/sys/select.h"
    94 #ifndef _MSC_VER
    9594#include "../musl-1.1.18/include/time.h"
    96 #endif
    9795#include "../musl-1.1.18/include/signal.h"
    9896#include "../musl-1.1.18/include/stdio.h"
     
    107105#include <stdarg.h>
    108106
     107#define DebugBreak()    asm("bkpt #0")
     108
     109#ifdef __cplusplus
     110extern "C" {
     111#endif
     112
    109113// time.h
    110114#define CLOCK_REALTIME 0
     115int shell_nanosleep(const struct timespec *req, struct timespec *rem);
    111116
    112117// dirent.h
     
    154159// signal.h
    155160int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old);
    156 int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old);
     161struct k_sigaction;
     162int shell_sigaction(int sig, const struct k_sigaction *restrict sa,
     163        struct k_sigaction *restrict old, size_t size);
    157164// socket.h
    158165int shell_socket(int, int, int);
    159166int shell_connect(int, const struct sockaddr *, socklen_t);
    160 ssize_t shell_send(int, const void *, size_t, int);
    161167ssize_t shell_sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
    162168ssize_t shell_sendmsg(int, const struct msghdr *, int);
    163169int shell_bind(int, const struct sockaddr *, socklen_t);
    164170int shell_listen(int, int);
    165 int shell_accept(int, struct sockaddr *__restrict, socklen_t *__restrict);
    166 ssize_t shell_recv(int, void *, size_t, int);
    167 ssize_t shell_recvfrom(int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict);
     171int shell_accept(int, struct sockaddr *restrict, socklen_t *restrict);
     172ssize_t shell_recvfrom(int, void *restrict, size_t, int, struct sockaddr *restrict, socklen_t *restrict);
    168173ssize_t shell_recvmsg(int, struct msghdr *, int);
    169 int shell_getsockopt(int, int, int, void *__restrict, socklen_t *__restrict);
     174int shell_getsockopt(int, int, int, void *restrict, socklen_t *restrict);
    170175int shell_setsockopt(int, int, int, const void *, socklen_t);
    171176int shell_shutdown(int, int);
     177int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len);
     178int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len);
    172179// select.h
    173 int shell_select(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict);
     180int shell_select(int, fd_set *restrict, fd_set *restrict, fd_set *restrict, struct timeval *restrict);
    174181// syslog.h
    175182void vsyslog (int, const char *, va_list);
    176183// termios.h
    177 int sio_tcgetattr (int, struct termios *);
    178 int sio_tcsetattr(int, int, const struct termios *);
     184int tcgetattr (int, struct termios *);
     185int tcsetattr(int, int, const struct termios *);
    179186// fcntl.h
    180187int shell_open(const char *, int, void *arg);
     
    196203int shell_chmod(const char *, mode_t);
    197204int shell_mkdir(const char *, mode_t);
    198 int shell_stat(const char *__restrict, struct stat *__restrict);
     205int shell_stat(const char *restrict, struct stat *restrict);
    199206int shell_fstat(int, struct stat *);
    200 int shell_lstat(const char *__restrict, struct stat *__restrict);
     207int shell_lstat(const char *restrict, struct stat *restrict);
    201208
    202209int shell_clock_getres(clockid_t clk_id, struct timespec *res);
     
    215222int shell_mprotect(void *addr, size_t len, int prot);
    216223// TODO
     224int shell_clone(int, int);
     225int shell_dup2(int, int);
     226int shell_execve(const char *, char *const [], char *const []);
     227int shell_faccessat(int, const char *, int, int);
     228int shell_fchmodat(int, const char *, mode_t, int);
     229int shell_flock(int, int);
     230int shell_fork();
     231int shell_futex(volatile int *, int, int, void *);
     232int shell_futimesat(int, const char *, struct timeval *);
     233int shell_getegid();
     234int shell_geteuid();
     235int shell_getgid();
     236int shell_gettid();
     237int shell_getuid();
    217238int shell_madvise(void *, size_t, int);
     239int shell_mremap(void *, size_t, size_t, int, void *);
     240int shell_munmap(void *, size_t);
     241int shell_pipe(int [2]);
     242int shell_pipe2(int [2], int);
     243int shell_ppoll(struct pollfd *, nfds_t, const struct timespec *,const sigset_t *);
     244int shell_pselect6(int, fd_set *restrict, fd_set *restrict,
     245        fd_set *restrict, const struct timespec *restrict, const sigset_t *restrict);
     246int shell_setpgid(pid_t, pid_t);
     247int shell_setsid();
     248int shell_symlink(const char *, const char *);
     249int shell_tkill(int, int);
     250int shell_umask(mode_t);
    218251int shell_uname(struct utsname *);
     252int shell_utimensat(int, const char *, const struct timespec [2], int);
     253int shell_utimes(const char *, struct timeval *);
     254// sys/resource.h
     255struct rusage;
     256int shell_wait4(pid_t, int *, int, struct rusage *);
     257ssize_t shell_getrandom(void *buf, size_t buflen, unsigned int flags);
    219258// ↑↑↑musl
    220259
     
    239278void __mbedcall_cp_asm();
    240279
     280#include "ff.h"
     281WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */
     282void Utf16_to_Utf8(unsigned char *, int *, UINT); /* in ff.c */
     283
    241284#ifdef __cplusplus
    242285extern "C" {
    243286#endif
    244 void Kernel_StartExternal(int argc, char **argv);
     287int Kernel_StartExternal(int argc, char **argv);
    245288#ifdef __cplusplus
    246289}
    247290#endif
    248291
    249 #endif /* SHELLIF_H */
     292void *malloc(size_t size);
     293void *calloc(size_t size, size_t count);
     294void *realloc(void *ptr, size_t size);
     295void free(void *ptr);
     296
     297#ifdef __cplusplus
     298}
     299#endif
     300
     301#endif /* _SHELLIF_H_ */
  • EcnlProtoTool/trunk/ntshell/src/socket_stub.c

    r439 r441  
    3939#include <t_syslog.h>
    4040#include <t_stdlib.h>
    41 //#include <string.h>
     41#include <string.h>
    4242#include <sil.h>
    43 #include <setjmp.h>
    4443#include "syssvc/syslog.h"
     44#include <tinet_defs.h>
    4545#include <tinet_config.h>
     46#include <net/net.h>
     47#include <net/net_endian.h>
    4648#include <netinet/in.h>
    4749#include <netinet/in_itron.h>
     
    5456#include <net/if_var.h>
    5557#include <netinet/udp_var.h>
    56 //#include <netinet/tcp_var.h>
    57 extern const ID tmax_tcp_cepid;
    58 #include "ff.h"
     58#include <net/net_buf.h>
     59#include <netinet/udp.h>
     60#include <netinet/udp_var.h>
     61#include <netinet/tcp.h>
     62#include <netinet/tcp_var.h>
     63#include <netapp/resolver.h>
    5964#include "fdtable.h"
     65#include "kernel_cfg.h"
     66
     67#ifdef _DEBUG
     68static const char THIS_FILE[] = __FILE__;
     69#endif
    6070
    6171#define SOCKET_TIMEOUT 2000000
     72#define TCP_SOCKET_BUF_SIZE 512
     73
     74struct addrinfo {
     75        int ai_flags;
     76        int ai_family;
     77        int ai_socktype;
     78        int ai_protocol;
     79        socklen_t ai_addrlen;
     80        struct sockaddr *ai_addr;
     81        char *ai_canonname;
     82        struct addrinfo *ai_next;
     83};
     84
     85typedef uint16_t in_port_t;
     86typedef uint32_t in_addr_t;
     87struct in_addr { in_addr_t s_addr; };
     88
     89struct sockaddr_in {
     90        sa_family_t sin_family;
     91        in_port_t sin_port;
     92        struct in_addr sin_addr;
     93        uint8_t sin_zero[8];
     94};
     95
     96struct in6_addr
     97{
     98        union {
     99                uint8_t __s6_addr[16];
     100                uint16_t __s6_addr16[8];
     101                uint32_t __s6_addr32[4];
     102        } __in6_union;
     103};
     104//#define s6_addr __in6_union.__s6_addr
     105//#define s6_addr16 __in6_union.__s6_addr16
     106//#define s6_addr32 __in6_union.__s6_addr32
     107
     108struct sockaddr_in6
     109{
     110        sa_family_t     sin6_family;
     111        in_port_t       sin6_port;
     112        uint32_t        sin6_flowinfo;
     113        struct in6_addr sin6_addr;
     114        uint32_t        sin6_scope_id;
     115};
     116
     117typedef struct socket_t {
     118        int family;
     119        int type;
     120        int protocol;
     121        int cepid;
     122        int repid;
     123        int backlog;
     124        unsigned int flags;
     125        union {
     126                struct sockaddr_in laddr4;
     127                struct sockaddr_in6 laddr6;
     128        };
     129        union {
     130                struct sockaddr_in raddr4;
     131                struct sockaddr_in6 raddr6;
     132        };
     133        int buf_size;
     134        unsigned char *buf;
     135        void *input;
     136        int len;
     137} socket_t;
     138
     139#define tcp6_cre_cep tcp_cre_cep
     140#define tcp6_del_cep tcp_del_cep
     141#define tcp6_del_rep tcp_del_rep
     142#define tcp6_can_cep tcp_can_cep
     143#define tcp6_sht_cep tcp_sht_cep
     144#define tcp6_cls_cep tcp_cls_cep
     145#define tcp6_snd_oob tcp_snd_oob
     146#define tcp6_snd_dat tcp_snd_dat
     147#define tcp6_rcv_oob tcp_rcv_oob
     148#define tcp6_rcv_buf tcp_rcv_buf
     149#define tcp6_rel_buf tcp_rel_buf
     150#define tcp6_get_opt tcp_get_opt
     151#define tcp6_set_opt tcp_set_opt
     152
     153#define udp6_can_cep udp_can_cep
     154
     155#ifndef SUPPORT_INET6
     156
     157ER      tcp6_cre_rep (ID repid, T_TCP6_CREP *pk_crep) { return E_SYS; }
     158ER      tcp6_acp_cep (ID cepid, ID repid, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; }
     159ER      tcp6_con_cep (ID cepid, T_IPV6EP *p_myaddr, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; }
     160
     161ER      udp6_cre_cep (ID cepid, T_UDP6_CCEP *pk_ccep) { return E_SYS; }
     162ER      udp6_del_cep (ID cepid) { return E_SYS; }
     163ER_UINT udp6_snd_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
     164ER_UINT udp6_rcv_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
     165ER      udp6_set_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
     166ER      udp6_get_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
     167
     168const T_IN6_ADDR *in6_get_ifaddr (int_t index) { return NULL; }
     169
     170#endif
     171
     172ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk);
     173ER socket_tcp6_callback(ID cepid, FN fncd, void *p_parblk);
     174ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk);
     175ER socket_udp6_callback(ID cepid, FN fncd, void *p_parblk);
     176
     177static int tcp_fd_close(struct SHELL_FILE *fp);
     178static size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     179static size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     180static off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     181static int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     182static bool_t tcp_fd_readable(struct SHELL_FILE *fp);
     183static bool_t tcp_fd_writable(struct SHELL_FILE *fp);
     184static void tcp_fd_delete(struct SHELL_FILE *fp);
     185
     186static int udp_fd_close(struct SHELL_FILE *fp);
     187static size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     188static size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     189static off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     190static int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     191static bool_t udp_fd_readable(struct SHELL_FILE *fp);
     192static bool_t udp_fd_writable(struct SHELL_FILE *fp);
     193static void udp_fd_delete(struct SHELL_FILE *fp);
     194
     195IO_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 };
     196IO_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 };
    62197
    63198typedef struct id_table_t {
     
    72207
    73208id_table_t tcp_cepid_table[] = {
    74         {0, USR_TCP_CEP1}, {0, USR_TCP_CEP2}, {0, USR_TCP_CEP3}, {0, USR_TCP_CEP4}, {0, USR_TCP_CEP5}, {0, USR_TCP_CEP6}, {0, USR_TCP_CEP7}, {0, USR_TCP_CEP8}
     209        {0, USR_TCP_CEP1}, {0, USR_TCP_CEP2}, {0, USR_TCP_CEP3}, {0, USR_TCP_CEP4},
     210#ifndef TOPPERS_GRSAKURA
     211        {0, USR_TCP_CEP5}, {0, USR_TCP_CEP6}, {0, USR_TCP_CEP7}, {0, USR_TCP_CEP8}
     212#endif
    75213};
    76214#define tcp_cepid_table_count (sizeof(tcp_cepid_table) / sizeof(tcp_cepid_table[0]))
     215
     216#ifdef SUPPORT_INET6
     217
     218id_table_t tcp6_repid_table[] = {
     219        {0, USR_TCP6_REP1}, {0, USR_TCP6_REP2}, {0, USR_TCP6_REP3}, {0, USR_TCP6_REP4}
     220};
     221#define tcp6_repid_table_count (sizeof(tcp6_repid_table) / sizeof(tcp6_repid_table[0]))
     222
     223id_table_t tcp6_cepid_table[] = {
     224        {0, USR_TCP6_CEP1}, {0, USR_TCP6_CEP2}, {0, USR_TCP6_CEP3}, {0, USR_TCP6_CEP4},
     225#ifndef TOPPERS_GRSAKURA
     226        {0, USR_TCP6_CEP5}, {0, USR_TCP6_CEP6}, {0, USR_TCP6_CEP7}, {0, USR_TCP6_CEP8}
     227#endif
     228};
     229#define tcp6_cepid_table_count (sizeof(tcp6_cepid_table) / sizeof(tcp6_cepid_table[0]))
     230
     231#else
     232
     233#define tcp6_repid_table NULL
     234#define tcp6_repid_table_count 0
     235
     236#define tcp6_cepid_table NULL
     237#define tcp6_cepid_table_count 0
     238
     239#endif
    77240
    78241id_table_t udp_cepid_table[] = {
     
    81244#define udp_cepid_table_count (sizeof(udp_cepid_table) / sizeof(udp_cepid_table[0]))
    82245
     246#ifdef SUPPORT_INET6
     247
     248id_table_t udp6_cepid_table[] = {
     249        {0, USR_UDP6_CEP1}, {0, USR_UDP6_CEP2}, {0, USR_UDP6_CEP3}, {0, USR_UDP6_CEP4}
     250};
     251#define udp6_cepid_table_count (sizeof(udp6_cepid_table) / sizeof(udp6_cepid_table[0]))
     252
     253#else
     254
     255#define udp6_cepid_table NULL
     256#define udp6_cepid_table_count 0
     257
     258#endif
     259
     260void addrcpy(void *_dst, const void *_src, int len)
     261{
     262#if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
     263        memcpy(_dst, _src, len);
     264#else
     265        uint8_t *dst = (uint8_t *)_dst, *src = &((uint8_t *)_src)[len];
     266        while (src != _src)
     267                *dst++ = *--src;
     268#endif
     269}
     270
    83271ID new_id(id_table_t *table, int count)
    84272{
     
    108296}
    109297
    110 int delete_tcp_rep(int repid)
    111 {
    112         return delete_tcp_fd(tmax_tcp_cepid + repid);
    113 }
    114 
    115 unsigned char tcp_buf[tcp_cepid_table_count][2 * 512];
    116 
    117 unsigned char *id_to_buff(ID id)
    118 {
    119         for (int i = 0; i < tcp_cepid_table_count; i++) {
    120                 id_table_t *item = &tcp_cepid_table[i];
    121                 if ((item->used == 0) || (item->id != id))
    122                         continue;
    123 
    124                 return tcp_buf[i];
    125         }
    126 
    127         return NULL;
    128 }
    129 
    130 typedef struct _IO_FILE SOCKET;
     298typedef struct SHELL_FILE SOCKET;
    131299
    132300int shell_socket(int family, int type, int protocol)
     
    148316        switch (type) {
    149317        case SOCK_STREAM:
    150                 fp = new_tcp_fd(0);
     318                fp = new_fp(&IO_TYPE_TCP, 0, 0);
     319                if (fp == NULL)
     320                        return -ENOMEM;
     321
     322                fp->exinf = malloc(sizeof(socket_t));
     323                memset(fp->exinf, 0, sizeof(socket_t));
    151324                break;
    152325        case SOCK_DGRAM:
    153                 fp = new_udp_fd(0);
     326                fp = new_fp(&IO_TYPE_UDP, 0, 1);
     327                if (fp == NULL)
     328                        return -ENOMEM;
     329
     330                fp->exinf = malloc(sizeof(socket_t));
     331                memset(fp->exinf, 0, sizeof(socket_t));
    154332                break;
    155333        default:
     
    157335        }
    158336
    159         if (fp == NULL) {
    160                 return -ENOMEM;
    161         }
    162 
    163         fp->socket.family = family;
    164         fp->socket.type = type;
    165         fp->socket.protocol = protocol;
    166         fp->socket.flags = flags;
     337        socket_t *socket = (socket_t *)fp->exinf;
     338        socket->family = family;
     339        socket->type = type;
     340        socket->protocol = protocol;
     341        socket->flags = flags;
    167342
    168343        return fp->fd;
     
    174349        if (fp == NULL)
    175350                return -EBADF;
    176         if (fp->socket.family != addr->sa_family)
     351        socket_t *socket = (socket_t *)fp->exinf;
     352        if (socket->family != addr->sa_family)
    177353                return -EINVAL;
    178354
     
    184360                }
    185361                struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
    186                 memcpy(&fp->socket.laddr4, addr, len);
    187                 switch (fp->socket.type) {
     362                memcpy(&socket->laddr4, addr, len);
     363                switch (socket->type) {
    188364                case SOCK_STREAM: {
    189365                        ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
     
    191367                                return -ENOMEM;
    192368
    193                         fp->socket.buf_size = 512 + 512;
    194                         fp->socket.buf = id_to_buff(cepid);
    195                         T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
     369                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     370                        socket->buf = malloc(socket->buf_size);
     371#ifdef _DEBUG
     372                        memset(socket->buf, 0, socket->buf_size);
     373#endif
     374                        T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
    196375                        ret = tcp_cre_cep(cepid, &ccep);
    197376                        if (ret != E_OK) {
     377                                free(socket->buf);
     378                                socket->buf = NULL;
     379                                socket->buf_size = 0;
    198380                                delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
    199381                                return -ENOMEM;
    200382                        }
    201383                        fp->handle = cepid;
    202                         fp->socket.cepid = cepid;
     384                        socket->cepid = cepid;
    203385                        break;
    204386                }
     
    215397                        }
    216398                        fp->handle = cepid;
    217                         fp->socket.cepid = cepid;
     399                        socket->cepid = cepid;
    218400                        break;
    219401                }
     
    227409                        return -EINVAL;
    228410                }
    229                 memcpy(&fp->socket.laddr4, addr, len);
    230                 break;
    231         }
     411                struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
     412                memcpy(&socket->laddr6, addr, len);
     413                switch (socket->type) {
     414                case SOCK_STREAM: {
     415                        ID cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
     416                        if (cepid < 0)
     417                                return -ENOMEM;
     418
     419                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     420                        socket->buf = malloc(socket->buf_size);
     421#ifdef _DEBUG
     422                        memset(socket->buf, 0, socket->buf_size);
     423#endif
     424                        T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
     425                        ret = tcp6_cre_cep(cepid, &ccep);
     426                        if (ret != E_OK) {
     427                                free(socket->buf);
     428                                socket->buf = NULL;
     429                                socket->buf_size = 0;
     430                                delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
     431                                return -ENOMEM;
     432                        }
     433                        fp->handle = cepid;
     434                        socket->cepid = cepid;
     435                        break;
     436                }
     437                case SOCK_DGRAM: {
     438                        ID cepid = new_id(udp6_cepid_table, udp6_cepid_table_count);
     439                        if (cepid < 0)
     440                                return -ENOMEM;
     441
     442                        T_UDP6_CCEP ccep = { 0, { { ntohl(addr_in6->sin6_addr.__in6_union.__s6_addr) }, ntohs(addr_in6->sin6_port)}, (FP)socket_udp6_callback };
     443                        ret = udp6_cre_cep(cepid, &ccep);
     444                        if (ret != E_OK) {
     445                                delete_id(udp6_cepid_table, udp6_cepid_table_count, cepid);
     446                                return -ENOMEM;
     447                        }
     448                        fp->handle = cepid;
     449                        socket->cepid = cepid;
     450                        break;
     451                }
     452                default:
     453                        return -ENOPROTOOPT;
     454                }
     455                break;
     456        }
     457        default:
     458                return -ENOPROTOOPT;
    232459        }
    233460
     
    240467        if (fp == NULL)
    241468                return -EBADF;
    242         if (fp->socket.type != SOCK_STREAM)
     469        socket_t *socket = (socket_t *)fp->exinf;
     470        if (socket->type != SOCK_STREAM)
    243471                return -EINVAL;
    244472
    245         fp->socket.backlog = backlog;
     473        socket->backlog = backlog;
    246474
    247475        ER ret;
    248         switch (fp->socket.family) {
     476        switch (socket->family) {
    249477        case AF_INET: {
    250478                ID repid = new_id(tcp_repid_table, tcp_repid_table_count);
     
    252480                        return -ENOMEM;
    253481
    254                 struct sockaddr_in *laddr = &fp->socket.laddr4;
     482                struct sockaddr_in *laddr = &socket->laddr4;
    255483                T_TCP_CREP crep = { 0, {ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port)} };
    256484                ret = tcp_cre_rep(repid, &crep);
     
    259487                        return -ENOMEM;
    260488                }
    261                 fp->socket.repid = repid;
     489                socket->repid = repid;
    262490                break;
    263491        }
    264492        case AF_INET6: {
    265                 break;
    266         }
     493                ID repid = new_id(tcp6_repid_table, tcp6_repid_table_count);
     494                if (repid < 0)
     495                        return -ENOMEM;
     496
     497                struct sockaddr_in6 *laddr = &socket->laddr6;
     498                T_TCP6_CREP crep = { 0, { { ntohl(laddr->sin6_addr.__in6_union.__s6_addr) }, ntohs(laddr->sin6_port)} };
     499                ret = tcp6_cre_rep(repid, &crep);
     500                if (ret != E_OK) {
     501                        delete_id(tcp6_repid_table, tcp6_repid_table_count, repid);
     502                        return -ENOMEM;
     503                }
     504                socket->repid = repid;
     505                break;
     506        }
     507        default:
     508                return -ENOPROTOOPT;
    267509        }
    268510
     
    270512}
    271513
    272 int shell_connect(int fd, const struct sockaddr *addr, socklen_t len)
     514int shell_connect(int fd, const struct sockaddr *addr, socklen_t alen)
    273515{
    274516        SOCKET *fp = fd_to_fp(fd);
    275517        if (fp == NULL)
    276518                return -EBADF;
    277         if (fp->socket.type != SOCK_STREAM)
     519        socket_t *socket = (socket_t *)fp->exinf;
     520        if (socket->type != SOCK_STREAM)
    278521                return -EINVAL;
    279522
    280523        ER ret;
    281         switch (fp->socket.family) {
     524        switch (socket->family) {
    282525        case AF_INET: {
    283                 if (len < 8) {
     526                if (alen < sizeof(struct sockaddr_in)) {
    284527                        return -EINVAL;
    285528                }
    286                 if (fp->socket.cepid == 0) {
     529                if (socket->cepid == 0) {
    287530                        ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
    288531                        if (cepid < 0)
    289532                                return -ENOMEM;
    290533
    291                         fp->socket.buf_size = 512 + 512;
    292                         fp->socket.buf = id_to_buff(cepid);
    293                         T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
     534                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     535                        socket->buf = malloc(socket->buf_size);
     536#ifdef _DEBUG
     537                        memset(socket->buf, 0, socket->buf_size);
     538#endif
     539                        T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
    294540                        ret = tcp_cre_cep(cepid, &ccep);
    295541                        if (ret != E_OK) {
     542                                free(socket->buf);
     543                                socket->buf = NULL;
     544                                socket->buf_size = 0;
    296545                                delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
    297546                                return -ENOMEM;
    298547                        }
    299548                        fp->handle = cepid;
    300                         fp->socket.cepid = cepid;
    301                 }
    302                 struct sockaddr_in *laddr = &fp->socket.laddr4;
    303                 struct sockaddr_in *raddr = &fp->socket.raddr4;
    304                 memset(raddr, 0, sizeof(*raddr));
    305                 memcpy(raddr, addr, len);
     549                        fp->writable = 1;
     550                        socket->cepid = cepid;
     551                }
     552                struct sockaddr_in *laddr = &socket->laddr4;
     553                struct sockaddr_in *raddr = &socket->raddr4;
     554                memcpy(raddr, addr, sizeof(struct sockaddr_in));
    306555                T_IPV4EP lep = { ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port) };
    307556                T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
    308                 ret = tcp_con_cep(fp->socket.cepid, &lep, &rep, SOCKET_TIMEOUT);
    309                 if (ret < 0) {
     557                ret = tcp_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
     558                if (ret == E_TMOUT) {
     559                        return -ETIMEDOUT;
     560                }
     561                else if (ret < 0) {
    310562                        return -EHOSTUNREACH;
    311563                }
     
    313565        }
    314566        case AF_INET6: {
    315                 break;
    316         }
    317         }
     567                if (alen < sizeof(struct sockaddr_in6)) {
     568                        return -EINVAL;
     569                }
     570                if (socket->cepid == 0) {
     571                        ID cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
     572                        if (cepid < 0)
     573                                return -ENOMEM;
     574
     575                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     576                        socket->buf = malloc(socket->buf_size);
     577#ifdef _DEBUG
     578                        memset(socket->buf, 0, socket->buf_size);
     579#endif
     580                        T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
     581                        ret = tcp6_cre_cep(cepid, &ccep);
     582                        if (ret != E_OK) {
     583                                free(socket->buf);
     584                                socket->buf = NULL;
     585                                socket->buf_size = 0;
     586                                delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
     587                                return -ENOMEM;
     588                        }
     589                        fp->handle = cepid;
     590                        fp->writable = 1;
     591                        socket->cepid = cepid;
     592                }
     593                struct sockaddr_in6 *laddr = &socket->laddr6;
     594                struct sockaddr_in6 *raddr = &socket->raddr6;
     595                memcpy(raddr, addr, sizeof(struct sockaddr_in6));
     596                T_IPV6EP lep;
     597                addrcpy(&lep.ipaddr, &laddr->sin6_addr, 16);
     598                lep.portno = ntohs(laddr->sin6_port);
     599                T_IPV6EP rep;
     600                addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
     601                rep.portno = ntohs(raddr->sin6_port);
     602                ret = tcp6_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
     603                if (ret == E_TMOUT) {
     604                        return -ETIMEDOUT;
     605                }
     606                else if (ret < 0) {
     607                        return -EHOSTUNREACH;
     608                }
     609                break;
     610        }
     611        default:
     612                return -ENOPROTOOPT;
     613        }
     614
     615        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
    318616
    319617        return 0;
    320618}
    321619
    322 int shell_accept(int fd, struct sockaddr *__restrict addr, socklen_t *__restrict len)
     620int shell_accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict alen)
    323621{
    324622        SOCKET *lfp = fd_to_fp(fd);
    325623        if (lfp == NULL)
    326624                return -EBADF;
    327         if (lfp->socket.type != SOCK_STREAM)
     625        if (((socket_t *)lfp->exinf)->type != SOCK_STREAM)
    328626                return -EINVAL;
    329627
    330         SOCKET *fp = new_tcp_fd(0);
    331         if (fp == NULL) {
     628        SOCKET *fp = new_fp(&IO_TYPE_TCP, 0, 0);
     629        if (fp == NULL)
    332630                return -ENOMEM;
    333         }
    334 
    335         memcpy(&fp->socket, &lfp->socket, sizeof(fp->socket));
     631
     632        fp->exinf = malloc(sizeof(socket_t));
     633        memset(fp->exinf, 0, sizeof(socket_t));
     634
     635        memcpy(fp->exinf, lfp->exinf, offsetof(socket_t, buf_size));
    336636
    337637        ER ret;
    338         switch (fp->socket.family) {
     638        socket_t *socket = (socket_t *)fp->exinf;
     639        switch (socket->family) {
    339640        case AF_INET: {
    340641                ID cepid;
    341                 if (fp->socket.cepid == 0) {
     642                if (socket->cepid == 0) {
    342643                        cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
    343644                        if (cepid < 0)
    344645                                return -ENOMEM;
    345646
    346                         fp->socket.buf_size = 512 + 512;
    347                         fp->socket.buf = id_to_buff(cepid);
    348                         T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
     647                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     648                        socket->buf = malloc(socket->buf_size);
     649#ifdef _DEBUG
     650                        memset(socket->buf, 0, socket->buf_size);
     651#endif
     652                        T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
    349653                        ret = tcp_cre_cep(cepid, &ccep);
    350654                        if (ret != E_OK) {
     655                                free(socket->buf);
     656                                socket->buf = NULL;
     657                                socket->buf_size = 0;
    351658                                delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
    352659                                return -ENOMEM;
    353660                        }
    354661                        fp->handle = cepid;
    355                         fp->socket.cepid = cepid;
     662                        fp->writable = 1;
     663                        socket->cepid = cepid;
    356664                }
    357665                else {
    358                         cepid = lfp->socket.cepid;
    359                         lfp->handle = tmax_tcp_cepid + lfp->socket.repid;
    360                         lfp->socket.cepid = 0;
    361                         lfp->socket.buf_size = 0;
    362                         lfp->socket.buf = 0;
     666                        cepid = ((socket_t *)lfp->exinf)->cepid;
     667                        fp->handle = cepid;
     668                        fp->writable = 1;
     669                        lfp->handle = tmax_tcp_cepid + ((socket_t *)lfp->exinf)->repid;
     670                        ((socket_t *)lfp->exinf)->cepid = 0;
     671                        ((socket_t *)lfp->exinf)->buf_size = 0;
     672                        ((socket_t *)lfp->exinf)->buf = 0;
    363673                }
    364674                T_IPV4EP rep = { 0, 0 };
    365                 ret = tcp_acp_cep(fp->socket.cepid, fp->socket.repid, &rep, TMO_FEVR);
     675                ret = tcp_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
    366676                if (ret < 0) {
    367677                        return -ENOMEM;
    368678                }
    369                 struct sockaddr_in *raddr = &fp->socket.raddr4;
    370                 memset(raddr, 0, sizeof(*raddr));
     679                struct sockaddr_in *raddr = &socket->raddr4;
     680                memset(raddr, 0, sizeof(struct sockaddr_in));
    371681                raddr->sin_family = AF_INET;
    372682                raddr->sin_port = htons(rep.portno);
    373683                raddr->sin_addr.s_addr = htonl(rep.ipaddr);
     684
     685                if (addr != NULL && alen != NULL) {
     686                        int sz = *alen;
     687                        if (sz < sizeof(struct sockaddr_in)) {
     688                                return -EINVAL;
     689                        }
     690                        if (sz > sizeof(struct sockaddr_in))
     691                                sz = sizeof(struct sockaddr_in);
     692                        memcpy(addr, raddr, sz);
     693                        *alen = sz;
     694                }
    374695                break;
    375696        }
    376697        case AF_INET6: {
    377                 return -EAFNOSUPPORT;
    378         }
    379         }
    380 
    381         if (addr != NULL && len != NULL) {
    382                 int sz = *len;
    383                 if (sz < 8) {
    384                         return -EINVAL;
    385                 }
    386                 struct sockaddr_in *raddr = &fp->socket.raddr4;
    387                 if (sz > sizeof(*raddr))
    388                         sz = sizeof(*raddr);
    389                 memcpy(addr, raddr, sz);
    390                 *len = sizeof(*raddr);
    391         }
     698                ID cepid;
     699                if (socket->cepid == 0) {
     700                        cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
     701                        if (cepid < 0)
     702                                return -ENOMEM;
     703
     704                        socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
     705                        socket->buf = malloc(socket->buf_size);
     706#ifdef _DEBUG
     707                        memset(socket->buf, 0, socket->buf_size);
     708#endif
     709                        T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
     710                        ret = tcp6_cre_cep(cepid, &ccep);
     711                        if (ret != E_OK) {
     712                                free(socket->buf);
     713                                socket->buf = NULL;
     714                                socket->buf_size = 0;
     715                                delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
     716                                return -ENOMEM;
     717                        }
     718                        fp->handle = cepid;
     719                        fp->writable = 1;
     720                        socket->cepid = cepid;
     721                }
     722                else {
     723                        cepid = ((socket_t *)lfp->exinf)->cepid;
     724                        fp->handle = cepid;
     725                        fp->writable = 1;
     726                        lfp->handle = tmax_tcp6_cepid + ((socket_t *)lfp->exinf)->repid;
     727                        ((socket_t *)lfp->exinf)->cepid = 0;
     728                        ((socket_t *)lfp->exinf)->buf_size = 0;
     729                        ((socket_t *)lfp->exinf)->buf = 0;
     730                }
     731                T_IPV6EP rep = { { 0 }, 0 };
     732                ret = tcp6_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
     733                if (ret < 0) {
     734                        return -ENOMEM;
     735                }
     736                struct sockaddr_in6 *raddr = &socket->raddr6;
     737                memset(raddr, 0, sizeof(struct sockaddr_in6));
     738                raddr->sin6_family = AF_INET;
     739                raddr->sin6_port = htons(rep.portno);
     740                addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
     741
     742                if (addr != NULL && alen != NULL) {
     743                        int sz = *alen;
     744                        if (sz < sizeof(struct sockaddr_in6)) {
     745                                return -EINVAL;
     746                        }
     747                        if (sz > sizeof(struct sockaddr_in6))
     748                                sz = sizeof(struct sockaddr_in6);
     749                        memcpy(addr, raddr, sz);
     750                        *alen = sz;
     751                }
     752                break;
     753        }
     754        default:
     755                return -ENOPROTOOPT;
     756        }
     757
     758        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
    392759
    393760        return fp->fd;
    394761}
    395762
    396 ssize_t shell_send(int fd, const void *buf, size_t len, int flags)
     763ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
    397764{
    398765        SOCKET *fp = fd_to_fp(fd);
     
    402769
    403770        int ret = 0;
    404         switch (fp->socket.family) {
     771        socket_t *socket = (socket_t *)fp->exinf;
     772        switch (socket->family) {
    405773        case AF_INET: {
    406                 switch (fp->socket.type) {
     774                switch (socket->type) {
    407775                case SOCK_STREAM: {
     776                        if ((addr != NULL) || (alen != 0)) {
     777                                return -EISCONN;
     778                        }
     779
    408780                        if (flags & MSG_OOB) {
    409                                 ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
     781                                ret = tcp_snd_oob(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
    410782                                if (ret < 0) {
    411783                                        return -ECOMM;
     
    413785                        }
    414786                        else {
    415                                 ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
     787                                int temp = len;
     788                                for (;;) {
     789                                        ret = tcp_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
     790                                        if (ret < 0) {
     791                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     792                                        }
     793                                        len -= ret;
     794                                        if (len <= 0)
     795                                                break;
     796                                        buf = (const void *)&((uint8_t *)buf)[ret];
     797                                }
     798                                ret = temp;
     799                        }
     800                        break;
     801                }
     802                case SOCK_DGRAM: {
     803                        if ((addr == NULL) || (alen < sizeof(struct sockaddr_in))) {
     804                                return -EINVAL;
     805                        }
     806                        struct sockaddr_in *raddr = &socket->raddr4;
     807                        memcpy(raddr, addr, sizeof(struct sockaddr_in));
     808                        T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
     809                        ret = udp_snd_dat(socket->cepid, &rep, (void *)buf, len,
     810                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     811                        if (ret < 0) {
     812                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     813                        }
     814                        break;
     815                }
     816                }
     817                break;
     818        }
     819        case AF_INET6: {
     820                switch (socket->type) {
     821                case SOCK_STREAM: {
     822                        if ((addr != NULL) || (alen != 0)) {
     823                                return -EISCONN;
     824                        }
     825
     826                        if (flags & MSG_OOB) {
     827                                ret = tcp6_snd_oob(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
    416828                                if (ret < 0) {
    417829                                        return -ECOMM;
    418830                                }
    419831                        }
     832                        else {
     833                                int temp = len;
     834                                for (;;) {
     835                                        ret = tcp6_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
     836                                        if (ret < 0) {
     837                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     838                                        }
     839                                        len -= ret;
     840                                        if (len <= 0)
     841                                                break;
     842                                        buf = (const void *)&((uint8_t *)buf)[ret];
     843                                }
     844                                ret = temp;
     845                        }
    420846                        break;
    421847                }
    422848                case SOCK_DGRAM: {
    423                         return -EINVAL;
    424                 }
    425                 }
    426                 break;
    427         }
    428         case AF_INET6: {
    429                 return -EAFNOSUPPORT;
    430         }
    431         }
     849                        if ((addr == NULL) || (alen < sizeof(struct sockaddr_in6))) {
     850                                return -EINVAL;
     851                        }
     852                        struct sockaddr_in6 *raddr = &socket->raddr6;
     853                        memcpy(raddr, addr, sizeof(struct sockaddr_in6));
     854                        T_IPV6EP rep;
     855                        addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
     856                        rep.portno = ntohs(raddr->sin6_port);
     857                        ret = udp6_snd_dat(socket->cepid, &rep, (void *)buf, len,
     858                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     859                        if (ret < 0) {
     860                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     861                        }
     862                        break;
     863                }
     864                }
     865                break;
     866        }
     867        default:
     868                return -ENOPROTOOPT;
     869        }
     870
     871        if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
    432872
    433873        return ret;
    434874}
    435875
    436 ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
     876ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags)
     877{
     878        no_implement("sendmsg\n");
     879        return -ENOSYS;
     880}
     881
     882ssize_t shell_recvfrom(int fd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict addr, socklen_t *restrict alen)
    437883{
    438884        SOCKET *fp = fd_to_fp(fd);
     
    442888
    443889        int ret = 0;
    444         switch (fp->socket.family) {
     890        socket_t *socket = (socket_t *)fp->exinf;
     891        switch (socket->family) {
    445892        case AF_INET: {
    446                 switch (fp->socket.type) {
     893                switch (socket->type) {
    447894                case SOCK_STREAM: {
    448                         if ((addr != NULL) && (alen != 0)) {
    449                                 return -EINVAL;
    450                         }
    451 
    452895                        if (flags & MSG_OOB) {
    453                                 ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
     896                                ret = tcp_rcv_oob(socket->cepid, buf, len);
    454897                                if (ret < 0) {
     898                                        syslog(LOG_ERROR, "tcp_rcv_oob => %d", ret);
    455899                                        return -ECOMM;
    456900                                }
    457901                        }
    458902                        else {
    459                                 ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
     903                                int rsz, tmp;
     904                                if (socket->input == NULL) {
     905                                        ret = wai_sem(SEM_FILEDESC);
     906                                        if (ret < 0) {
     907                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     908                                        }
     909                                        socket->len = 0;
     910                                        ret = sig_sem(SEM_FILEDESC);
     911                                        if (ret < 0) {
     912                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     913                                        }
     914                                        ret = tcp_rcv_buf(socket->cepid, &socket->input,
     915                                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     916                                        if (ret < 0) {
     917                                                if ((socket->flags & O_NONBLOCK) == 0)
     918                                                        syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
     919                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     920                                        }
     921                                        rsz = ret;
     922                                }
     923                                else
     924                                        rsz = socket->len;
     925                                tmp = rsz;
     926                                if (rsz > len)
     927                                        rsz = len;
     928                                if (rsz > 0) {
     929                                        memcpy(buf, socket->input, rsz);
     930                                        ret = wai_sem(SEM_FILEDESC);
     931                                        if (ret < 0) {
     932                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     933                                        }
     934                                        socket->len = tmp - rsz;
     935                                        ret = sig_sem(SEM_FILEDESC);
     936                                        if (ret < 0) {
     937                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     938                                        }
     939                                        if (tmp - rsz == 0) {
     940                                                socket->input = NULL;
     941                                        }
     942                                        else
     943                                                socket->input = (void *)&((uint8_t *)socket->input)[rsz];
     944                                        ret = tcp_rel_buf(socket->cepid, rsz);
     945                                        if ((ret != E_OBJ) && (ret < 0)) {
     946                                                syslog(LOG_ERROR, "tcp_rel_buf => %d", ret);
     947                                                //return -ECOMM;
     948                                        }
     949                                }
     950                                else {
     951                                        syslog(LOG_ERROR, "shell_recvfrom => %d", rsz);
     952                                }
     953                                ret = rsz;
     954                        }
     955                        break;
     956                }
     957                case SOCK_DGRAM: {
     958                        struct sockaddr_in *raddr = &socket->raddr4;
     959                        int rsz;
     960                        ret = wai_sem(SEM_FILEDESC);
     961                        if (ret < 0) {
     962                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     963                        }
     964                        T_NET_BUF *input = socket->input;
     965                        if (input == NULL) {
     966                                ret = sig_sem(SEM_FILEDESC);
    460967                                if (ret < 0) {
     968                                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     969                                }
     970
     971                                T_IPV4EP rep = { 0, 0 };
     972                                ret = udp_rcv_dat(socket->cepid, &rep, buf, len,
     973                                        (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     974                                if (ret < 0) {
     975                                        if ((socket->flags & O_NONBLOCK) == 0)
     976                                                syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
     977                                        return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     978                                }
     979                                rsz = ret;
     980                                if ((addr != NULL) && (alen != NULL)) {
     981                                        ret = wai_sem(SEM_FILEDESC);
     982                                        if (ret < 0) {
     983                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     984                                        }
     985                                        int sz = *alen;
     986                                        memset(raddr, 0, sizeof(struct sockaddr_in));
     987                                        raddr->sin_family = AF_INET;
     988                                        raddr->sin_port = htons(rep.portno);
     989                                        raddr->sin_addr.s_addr = htonl(rep.ipaddr);
     990                                        if (sz > sizeof(struct sockaddr_in))
     991                                                sz = sizeof(struct sockaddr_in);
     992                                        memcpy(addr, raddr, sz);
     993                                        *alen = sz;
     994                                        ret = sig_sem(SEM_FILEDESC);
     995                                        if (ret < 0) {
     996                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     997                                        }
     998                                }
     999                        }
     1000                        else {
     1001                                rsz = socket->len;
     1002                                void *pbuf = socket->buf;
     1003                                socket->input = NULL;
     1004                                socket->len = 0;
     1005                                socket->buf = NULL;
     1006                                if ((addr != NULL) && (alen != NULL)) {
     1007                                        int sz = *alen;
     1008                                        if (sz > sizeof(struct sockaddr_in))
     1009                                                sz = sizeof(struct sockaddr_in);
     1010                                        memcpy(addr, raddr, sz);
     1011                                        *alen = sz;
     1012                                }
     1013                                ret = sig_sem(SEM_FILEDESC);
     1014                                if (ret < 0) {
     1015                                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1016                                }
     1017                                if (rsz > len)
     1018                                        rsz = len;
     1019                                memcpy(buf, pbuf, rsz);
     1020                                ret = rel_net_buf(input);
     1021                                if (ret < 0) {
     1022                                        syslog(LOG_ERROR, "rel_net_buf => %d", ret);
     1023                                        //return -ECOMM;
     1024                                }
     1025                        }
     1026                        ret = rsz;
     1027                }
     1028                }
     1029                break;
     1030        }
     1031        case AF_INET6: {
     1032                switch (socket->type) {
     1033                case SOCK_STREAM: {
     1034                        if (flags & MSG_OOB) {
     1035                                ret = tcp6_rcv_oob(socket->cepid, buf, len);
     1036                                if (ret < 0) {
     1037                                        syslog(LOG_ERROR, "tcp6_rcv_oob => %d", ret);
    4611038                                        return -ECOMM;
    4621039                                }
    4631040                        }
     1041                        else {
     1042                                int rsz, tmp;
     1043                                if (socket->input == NULL) {
     1044                                        ret = wai_sem(SEM_FILEDESC);
     1045                                        if (ret < 0) {
     1046                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     1047                                        }
     1048                                        socket->len = 0;
     1049                                        ret = sig_sem(SEM_FILEDESC);
     1050                                        if (ret < 0) {
     1051                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     1052                                        }
     1053                                        ret = tcp6_rcv_buf(socket->cepid, &socket->input,
     1054                                                (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     1055                                        if (ret < 0) {
     1056                                                if ((socket->flags & O_NONBLOCK) == 0)
     1057                                                        syslog(LOG_ERROR, "tcp6_rcv_buf => %d", ret);
     1058                                                return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     1059                                        }
     1060                                        rsz = ret;
     1061                                }
     1062                                else
     1063                                        rsz = socket->len;
     1064                                tmp = rsz;
     1065                                if (rsz > len)
     1066                                        rsz = len;
     1067                                if (rsz >= 0) {
     1068                                        memcpy(buf, socket->input, rsz);
     1069                                        ret = wai_sem(SEM_FILEDESC);
     1070                                        if (ret < 0) {
     1071                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     1072                                        }
     1073                                        socket->len = tmp - rsz;
     1074                                        ret = sig_sem(SEM_FILEDESC);
     1075                                        if (ret < 0) {
     1076                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     1077                                        }
     1078                                        if (tmp - rsz == 0) {
     1079                                                socket->input = NULL;
     1080                                        }
     1081                                        else
     1082                                                socket->input = (void *)&((uint8_t *)socket->input)[rsz];
     1083                                        ret = tcp6_rel_buf(socket->cepid, rsz);
     1084                                        if ((ret != E_OBJ) && (ret < 0)) {
     1085                                                syslog(LOG_ERROR, "tcp6_rel_buf => %d", ret);
     1086                                                //return -ECOMM;
     1087                                        }
     1088                                }
     1089                                ret = rsz;
     1090                        }
    4641091                        break;
    4651092                }
    4661093                case SOCK_DGRAM: {
    467                         int sz = alen;
    468                         if (sz < 8) {
    469                                 return -EINVAL;
    470                         }
    471                         struct sockaddr_in *raddr = &fp->socket.raddr4;
    472                         memset(raddr, 0, sizeof(*raddr));
    473                         memcpy(raddr, addr, sz);
    474                         T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
    475                         ret = udp_snd_dat(fp->socket.cepid, &rep, (void *)buf, len,
    476                                 (fp->socket.flags & O_NONBLOCK) ? 0 : SOCKET_TIMEOUT);
     1094                        struct sockaddr_in6 *raddr = &socket->raddr6;
     1095                        int rsz;
     1096                        ret = wai_sem(SEM_FILEDESC);
    4771097                        if (ret < 0) {
    478                                 return -ECOMM;
    479                         }
    480                         break;
    481                 }
    482                 }
    483                 break;
    484         }
    485         case AF_INET6: {
    486                 return -EAFNOSUPPORT;
    487         }
    488         }
     1098                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     1099                        }
     1100                        T_NET_BUF *input = socket->input;
     1101                        if (input == NULL) {
     1102                                ret = sig_sem(SEM_FILEDESC);
     1103                                if (ret < 0) {
     1104                                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1105                                }
     1106
     1107                                T_IPV6EP rep = { { 0 }, 0 };
     1108                                ret = udp6_rcv_dat(socket->cepid, &rep, buf, len,
     1109                                        (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
     1110                                if (ret < 0) {
     1111                                        if ((socket->flags & O_NONBLOCK) == 0)
     1112                                                syslog(LOG_ERROR, "udp6_rcv_buf => %d", ret);
     1113                                        return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
     1114                                }
     1115                                rsz = ret;
     1116                                if ((addr != NULL) && (alen != NULL)) {
     1117                                        ret = wai_sem(SEM_FILEDESC);
     1118                                        if (ret < 0) {
     1119                                                syslog(LOG_ERROR, "wai_sem => %d", ret);
     1120                                        }
     1121                                        int sz = *alen;
     1122                                        memset(raddr, 0, sizeof(struct sockaddr_in6));
     1123                                        raddr->sin6_family = AF_INET;
     1124                                        raddr->sin6_port = htons(rep.portno);
     1125                                        addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
     1126                                        if (sz > sizeof(struct sockaddr_in6))
     1127                                                sz = sizeof(struct sockaddr_in6);
     1128                                        memcpy(addr, raddr, sz);
     1129                                        *alen = sz;
     1130                                        ret = sig_sem(SEM_FILEDESC);
     1131                                        if (ret < 0) {
     1132                                                syslog(LOG_ERROR, "sig_sem => %d", ret);
     1133                                        }
     1134                                }
     1135                        }
     1136                        else {
     1137                                rsz = socket->len;
     1138                                void *pbuf = socket->buf;
     1139                                socket->input = NULL;
     1140                                socket->len = 0;
     1141                                socket->buf = NULL;
     1142                                if ((addr != NULL) && (alen != NULL)) {
     1143                                        int sz = *alen;
     1144                                        if (sz > sizeof(struct sockaddr_in6))
     1145                                                sz = sizeof(struct sockaddr_in6);
     1146                                        memcpy(addr, raddr, sz);
     1147                                        *alen = sz;
     1148                                }
     1149                                ret = sig_sem(SEM_FILEDESC);
     1150                                if (ret < 0) {
     1151                                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1152                                }
     1153                                if (rsz > len)
     1154                                        rsz = len;
     1155                                memcpy(buf, pbuf, rsz);
     1156                                ret = rel_net_buf(input);
     1157                                if (ret < 0) {
     1158                                        syslog(LOG_ERROR, "rel_net_buf => %d", ret);
     1159                                        //return -ECOMM;
     1160                                }
     1161                        }
     1162                        ret = rsz;
     1163                }
     1164                }
     1165                break;
     1166        }
     1167        default:
     1168                return -ENOPROTOOPT;
     1169        }
     1170
     1171        if (fp->readevt_w != fp->readevt_r) fp->readevt_r++;
    4891172
    4901173        return ret;
    4911174}
    4921175
    493 ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags)
    494 {
    495         no_implement("sendmsg\n");
     1176ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags)
     1177{
     1178        no_implement("recvmsg\n");
    4961179        return -ENOSYS;
    4971180}
    4981181
    499 ssize_t shell_recv(int fd, void *buf, size_t len, int flags)
     1182int shell_shutdown(int fd, int how)
    5001183{
    5011184        SOCKET *fp = fd_to_fp(fd);
     
    5041187        }
    5051188
    506         int ret = 0;
    507         switch (fp->socket.family) {
     1189        ER ret;
     1190        socket_t *socket = (socket_t *)fp->exinf;
     1191        switch (socket->family) {
    5081192        case AF_INET: {
    509                 switch (fp->socket.type) {
     1193                switch (socket->type) {
    5101194                case SOCK_STREAM: {
    511                         if (flags & MSG_OOB) {
    512                                 ret = tcp_rcv_oob(fp->socket.cepid, buf, len);
    513                                 if (ret < 0) {
    514                                         return -ECOMM;
    515                                 }
    516                         }
    517                         else {
    518                                 ret = tcp_rcv_dat(fp->socket.cepid, buf, len, TMO_FEVR);
    519                                 if (ret < 0) {
    520                                         return -ECOMM;
    521                                 }
    522                         }
    523                         break;
    524                 }
    525                 case SOCK_DGRAM: {
    526                         T_IPV4EP rep = { 0, 0 };
    527                         ret = udp_rcv_dat(fp->socket.cepid, &rep, buf, len, (fp->socket.flags & O_NONBLOCK) ? 0 : TMO_FEVR);
     1195                        ret = tcp_sht_cep(socket->cepid);
    5281196                        if (ret < 0) {
    5291197                                return -ECOMM;
    5301198                        }
     1199                        break;
    5311200                }
    5321201                }
     
    5341203        }
    5351204        case AF_INET6: {
    536                 return -EAFNOSUPPORT;
    537         }
    538         }
    539 
    540         return ret;
    541 }
    542 
    543 ssize_t shell_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
     1205                switch (socket->type) {
     1206                case SOCK_STREAM: {
     1207                        ret = tcp6_sht_cep(socket->cepid);
     1208                        if (ret < 0) {
     1209                                return -ECOMM;
     1210                        }
     1211                        break;
     1212                }
     1213                }
     1214                break;
     1215        }
     1216        default:
     1217                return -ENOPROTOOPT;
     1218        }
     1219
     1220        return 0;
     1221}
     1222
     1223int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *restrict optlen)
    5441224{
    5451225        SOCKET *fp = fd_to_fp(fd);
     
    5481228        }
    5491229
    550         int ret = 0;
    551         switch (fp->socket.family) {
     1230        ER ret;
     1231        socket_t *socket = (socket_t *)fp->exinf;
     1232        switch (socket->family) {
    5521233        case AF_INET: {
    553                 switch (fp->socket.type) {
     1234                switch (socket->type) {
    5541235                case SOCK_STREAM: {
    555                         if (flags & MSG_OOB) {
    556                                 ret = tcp_rcv_oob(fp->socket.cepid, buf, len);
     1236                        switch (level) {
     1237                        case SOL_SOCKET:
     1238                                switch (optname) {
     1239                                case SO_REUSEADDR:
     1240                                        if (socket->flags & SO_REUSEADDR) {
     1241                                                *(bool *)optval = true;
     1242                                        }
     1243                                        else {
     1244                                                *(bool *)optval = false;
     1245                                        }
     1246                                        break;
     1247                                case SO_KEEPALIVE:
     1248                                        if (socket->flags & SO_KEEPALIVE) {
     1249                                                *(bool *)optval = true;
     1250                                        }
     1251                                        else {
     1252                                                *(bool *)optval = false;
     1253                                        }
     1254                                        break;
     1255                                case SO_ERROR:
     1256                                        *(int *)optval = 0;
     1257                                        break;
     1258                                default:
     1259                                        return -EINVAL;
     1260                                }
     1261                                break;
     1262                        case IPPROTO_TCP:
     1263                                ret = tcp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
    5571264                                if (ret < 0) {
    558                                         return -ECOMM;
    559                                 }
    560                         }
    561                         else {
    562                                 ret = tcp_rcv_dat(fp->socket.cepid, buf, len, TMO_FEVR);
     1265                                        return -EINVAL;
     1266                                }
     1267                                *optlen = ret;
     1268                                break;
     1269                        default:
     1270                                return -EINVAL;
     1271                        }
     1272                        break;
     1273                }
     1274                case SOCK_DGRAM: {
     1275                        switch (level) {
     1276                        case IPPROTO_UDP:
     1277                                ret = udp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
    5631278                                if (ret < 0) {
    564                                         return -ECOMM;
    565                                 }
     1279                                        return -EINVAL;
     1280                                }
     1281                                *optlen = ret;
     1282                                break;
     1283                        default:
     1284                                return -EINVAL;
    5661285                        }
    5671286                        break;
    5681287                }
     1288                }
     1289                break;
     1290        }
     1291        case AF_INET6: {
     1292                switch (socket->type) {
     1293                case SOCK_STREAM: {
     1294                        switch (level) {
     1295                        case SOL_SOCKET:
     1296                                switch (optname) {
     1297                                case SO_REUSEADDR:
     1298                                        if (socket->flags & SO_REUSEADDR) {
     1299                                                *(bool *)optval = true;
     1300                                        }
     1301                                        else {
     1302                                                *(bool *)optval = false;
     1303                                        }
     1304                                        break;
     1305                                case SO_KEEPALIVE:
     1306                                        if (socket->flags & SO_KEEPALIVE) {
     1307                                                *(bool *)optval = true;
     1308                                        }
     1309                                        else {
     1310                                                *(bool *)optval = false;
     1311                                        }
     1312                                        break;
     1313                                case SO_ERROR:
     1314                                        *(int *)optval = 0;
     1315                                        break;
     1316                                default:
     1317                                        return -EINVAL;
     1318                                }
     1319                                break;
     1320                        case IPPROTO_TCP:
     1321                                ret = tcp6_get_opt(socket->cepid, optname, (void *)optval, *optlen);
     1322                                if (ret < 0) {
     1323                                        return -EINVAL;
     1324                                }
     1325                                *optlen = ret;
     1326                                break;
     1327                        default:
     1328                                return -EINVAL;
     1329                        }
     1330                        break;
     1331                }
    5691332                case SOCK_DGRAM: {
    570                         T_IPV4EP rep = { 0, 0 };
    571                         ret = udp_rcv_dat(fp->socket.cepid, &rep, buf, len, TMO_FEVR);
    572                         if (ret < 0) {
    573                                 return -ECOMM;
    574                         }
    575                         int sz = *alen;
    576                         struct sockaddr_in raddr;
    577                         memset(&raddr, 0, sizeof(raddr));
    578                         raddr.sin_family = AF_INET;
    579                         raddr.sin_port = htons(rep.portno);
    580                         raddr.sin_addr.s_addr = htonl(rep.ipaddr);
    581                         if (sz > sizeof(raddr))
    582                                 sz = sizeof(raddr);
    583                         memcpy(addr, &raddr, sz);
    584                 }
    585                 }
    586                 break;
    587         }
    588         case AF_INET6: {
    589                 return -EAFNOSUPPORT;
    590         }
    591         }
    592 
    593         return ret;
    594 }
    595 
    596 ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags)
    597 {
    598         no_implement("recvmsg\n");
    599         return -ENOSYS;
    600 }
    601 
    602 int shell_shutdown(int fd, int how)
     1333                        switch (level) {
     1334                        case IPPROTO_UDP:
     1335                                ret = udp6_get_opt(socket->cepid, optname, (void *)optval, *optlen);
     1336                                if (ret < 0) {
     1337                                        return -EINVAL;
     1338                                }
     1339                                *optlen = ret;
     1340                                break;
     1341                        default:
     1342                                return -EINVAL;
     1343                        }
     1344                        break;
     1345                }
     1346                }
     1347                break;
     1348        }
     1349        default:
     1350                return -ENOPROTOOPT;
     1351        }
     1352
     1353        return 0;
     1354}
     1355
     1356int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
    6031357{
    6041358        SOCKET *fp = fd_to_fp(fd);
     
    6081362
    6091363        ER ret;
    610         switch (fp->socket.family) {
     1364        socket_t *socket = (socket_t *)fp->exinf;
     1365        switch (socket->family) {
    6111366        case AF_INET: {
    612                 switch (fp->socket.type) {
     1367                switch (socket->type) {
    6131368                case SOCK_STREAM: {
    614                         ret = tcp_sht_cep(fp->socket.cepid);
    615                         if (ret < 0) {
    616                                 return -ECOMM;
     1369                        switch (level){
     1370                        case SOL_SOCKET:
     1371                                switch (optname) {
     1372                                case SO_REUSEADDR:
     1373                                        if (*(bool *)optval) {
     1374                                                socket->flags |= SO_REUSEADDR;
     1375                                        }
     1376                                        else {
     1377                                                socket->flags &= ~SO_REUSEADDR;
     1378                                        }
     1379                                        break;
     1380                                case SO_KEEPALIVE:
     1381                                        if (*(bool *)optval) {
     1382                                                socket->flags |= SO_KEEPALIVE;
     1383                                        }
     1384                                        else {
     1385                                                socket->flags &= ~SO_KEEPALIVE;
     1386                                        }
     1387                                        break;
     1388                                default:
     1389                                        return -EINVAL;
     1390                                }
     1391                                break;
     1392                        case IPPROTO_TCP:
     1393                                ret = tcp_set_opt(socket->cepid, optname, (void *)optval, optlen);
     1394                                if (ret < 0) {
     1395                                        return -EINVAL;
     1396                                }
     1397                                break;
     1398                        default:
     1399                                return -EINVAL;
    6171400                        }
    6181401                        break;
    6191402                }
     1403                case SOCK_DGRAM: {
     1404                        switch (level){
     1405                        case IPPROTO_UDP:
     1406                                ret = udp_set_opt(socket->cepid, optname, (void *)optval, optlen);
     1407                                if (ret < 0) {
     1408                                        return -EINVAL;
     1409                                }
     1410                                break;
     1411                        default:
     1412                                return -EINVAL;
     1413                        }
     1414                        break;
     1415                }
    6201416                }
    6211417                break;
    6221418        }
    6231419        case AF_INET6: {
    624                 return -EAFNOSUPPORT;
    625         }
     1420                switch (socket->type) {
     1421                case SOCK_STREAM: {
     1422                        switch (level){
     1423                        case SOL_SOCKET:
     1424                                switch (optname) {
     1425                                case SO_REUSEADDR:
     1426                                        if (*(bool *)optval) {
     1427                                                socket->flags |= SO_REUSEADDR;
     1428                                        }
     1429                                        else {
     1430                                                socket->flags &= ~SO_REUSEADDR;
     1431                                        }
     1432                                        break;
     1433                                case SO_KEEPALIVE:
     1434                                        if (*(bool *)optval) {
     1435                                                socket->flags |= SO_KEEPALIVE;
     1436                                        }
     1437                                        else {
     1438                                                socket->flags &= ~SO_KEEPALIVE;
     1439                                        }
     1440                                        break;
     1441                                default:
     1442                                        return -EINVAL;
     1443                                }
     1444                                break;
     1445                        case IPPROTO_TCP:
     1446                                ret = tcp6_set_opt(socket->cepid, optname, (void *)optval, optlen);
     1447                                if (ret < 0) {
     1448                                        return -EINVAL;
     1449                                }
     1450                                break;
     1451                        default:
     1452                                return -EINVAL;
     1453                        }
     1454                        break;
     1455                }
     1456                case SOCK_DGRAM: {
     1457                        switch (level){
     1458                        case IPPROTO_UDP:
     1459                                ret = udp6_set_opt(socket->cepid, optname, (void *)optval, optlen);
     1460                                if (ret < 0) {
     1461                                        return -EINVAL;
     1462                                }
     1463                                break;
     1464                        default:
     1465                                return -EINVAL;
     1466                        }
     1467                        break;
     1468                }
     1469                }
     1470                break;
     1471        }
     1472        default:
     1473                return -ENOPROTOOPT;
    6261474        }
    6271475
     
    6291477}
    6301478
    631 int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *__restrict optlen)
     1479int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
    6321480{
    6331481        SOCKET *fp = fd_to_fp(fd);
     
    6351483                return -EBADF;
    6361484        }
    637 
    638         ER ret;
    639         switch (fp->socket.family) {
     1485        if (len == NULL) {
     1486                return -EINVAL;
     1487        }
     1488
     1489        socklen_t size = *len;
     1490        socket_t *socket = (socket_t *)fp->exinf;
     1491        switch (socket->family) {
    6401492        case AF_INET: {
    641                 switch (fp->socket.type) {
    642                 case SOCK_STREAM: {
    643                         switch (optname) {
    644                         case SO_REUSEADDR:
    645                                 if (fp->socket.flags & SO_REUSEADDR) {
    646                                         *(bool *)optval = true;
    647                                 }
    648                                 else {
    649                                         *(bool *)optval = false;
    650                                 }
    651                                 break;
    652                         default:
    653                                 ret = tcp_get_opt(fp->socket.cepid, optname, (void *)optval, *optlen);
    654                                 if (ret < 0) {
    655                                         return -EINVAL;
    656                                 }
    657                                 *optlen = ret;
    658                                 break;
    659                         }
    660                         break;
    661                 }
    662                 case SOCK_DGRAM: {
    663                         ret = udp_get_opt(fp->socket.cepid, optname, (void *)optval, *optlen);
    664                         if (ret < 0) {
    665                                 return -EINVAL;
    666                         }
    667                         *optlen = ret;
    668                         break;
    669                 }
    670                 }
     1493                struct sockaddr_in *raddr = &socket->raddr4;
     1494                *len = sizeof(struct sockaddr_in);
     1495                if (size > sizeof(struct sockaddr_in))
     1496                        size = sizeof(struct sockaddr_in);
     1497                memcpy(addr, raddr, size);
    6711498                break;
    6721499        }
    6731500        case AF_INET6: {
    674                 return -EAFNOSUPPORT;
    675         }
     1501                struct sockaddr_in6 *raddr = &socket->raddr6;
     1502                *len = sizeof(struct sockaddr_in6);
     1503                if (size > sizeof(struct sockaddr_in6))
     1504                        size = sizeof(struct sockaddr_in6);
     1505                memcpy(addr, raddr, size);
     1506                break;
     1507        }
     1508        default:
     1509                return -ENOPROTOOPT;
    6761510        }
    6771511
     
    6791513}
    6801514
    681 int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
     1515int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
    6821516{
    6831517        SOCKET *fp = fd_to_fp(fd);
     
    6851519                return -EBADF;
    6861520        }
    687 
    688         ER ret;
    689         switch (fp->socket.family) {
     1521        if (len == NULL) {
     1522                return -EINVAL;
     1523        }
     1524
     1525        socklen_t size = *len;
     1526        socket_t *socket = (socket_t *)fp->exinf;
     1527        switch (socket->family) {
    6901528        case AF_INET: {
    691                 switch (fp->socket.type) {
    692                 case SOCK_STREAM: {
    693                         switch (optname) {
    694                         case SO_REUSEADDR:
    695                                 if (*(bool *)optval) {
    696                                         fp->socket.flags |= SO_REUSEADDR;
    697                                 }
    698                                 else {
    699                                         fp->socket.flags &= ~SO_REUSEADDR;
    700                                 }
    701                                 break;
    702                         default:
    703                                 ret = tcp_set_opt(fp->socket.cepid, optname, (void *)optval, optlen);
    704                                 if (ret < 0) {
    705                                         return -EINVAL;
    706                                 }
    707                                 break;
    708                         }
    709                         break;
    710                 }
    711                 case SOCK_DGRAM: {
    712                         ret = udp_set_opt(fp->socket.cepid, optname, (void *)optval, optlen);
    713                         if (ret < 0) {
    714                                 return -EINVAL;
    715                         }
    716                         break;
    717                 }
    718                 }
     1529                const T_IN4_ADDR *laddr4 = in4_get_ifaddr(0);
     1530                struct sockaddr_in laddr;
     1531                laddr.sin_family = AF_INET;
     1532                laddr.sin_addr.s_addr = htonl(*laddr4);
     1533                laddr.sin_port = socket->laddr4.sin_port;
     1534                memset(&laddr.sin_zero, 0, sizeof(laddr.sin_zero));
     1535                *len = sizeof(struct sockaddr_in);
     1536                if (size > sizeof(struct sockaddr_in))
     1537                        size = sizeof(struct sockaddr_in);
     1538                memcpy(addr, &laddr, size);
    7191539                break;
    7201540        }
    7211541        case AF_INET6: {
    722                 return -EAFNOSUPPORT;
    723         }
     1542                const T_IN6_ADDR *laddr6 = in6_get_ifaddr(0);
     1543                struct sockaddr_in6 laddr;
     1544                laddr.sin6_family = AF_INET;
     1545                addrcpy(&laddr.sin6_addr, laddr6, 16);
     1546                laddr.sin6_port = socket->laddr6.sin6_port;
     1547                *len = sizeof(struct sockaddr_in6);
     1548                if (size > sizeof(struct sockaddr_in6))
     1549                        size = sizeof(struct sockaddr_in6);
     1550                memcpy(addr, &laddr, size);
     1551                break;
     1552        }
     1553        default:
     1554                return -ENOPROTOOPT;
    7241555        }
    7251556
     
    7271558}
    7281559
    729 int tcp_fd_close(struct _IO_FILE *fp)
    730 {
    731         ER ret;
    732         switch (fp->socket.family) {
     1560int tcp_fd_close(struct SHELL_FILE *fp)
     1561{
     1562        ER ret, ret2;
     1563
     1564        socket_t *socket = (socket_t *)fp->exinf;
     1565        switch (socket->family) {
    7331566        case AF_INET: {
    734                 if (fp->socket.cepid != 0) {
    735                         ID cepid = fp->socket.cepid;
     1567                if (socket->cepid != 0) {
     1568                        ID cepid = socket->cepid;
     1569                        ret = tcp_can_cep(cepid, TFN_TCP_ALL);
     1570                        if ((ret < 0) && (ret != E_OBJ)) {
     1571                                syslog(LOG_ERROR, "tcp_can_cep => %d", ret);
     1572                        }
    7361573                        ret = tcp_sht_cep(cepid);
    737                         if (ret < 0) {
    738                                 //return -1;
    739                         }
    740                         ret = tcp_cls_cep(cepid, (fp->socket.repid != 0) ? 0 : SOCKET_TIMEOUT);
    741                         if (ret < 0) {
    742                                 //return -1;
    743                         }
    744                         ret = tcp_del_cep(cepid);
    745                         delete_tcp_fd(cepid);
     1574                        if ((ret < 0) && (ret != E_OBJ)) {
     1575                                syslog(LOG_ERROR, "tcp_sht_cep => %d", ret);
     1576                        }
     1577                        ret = tcp_cls_cep(cepid, (socket->repid != 0) ? 0 : SOCKET_TIMEOUT);
     1578                        ret2 = tcp_del_cep(cepid);
     1579                        if (ret2 < 0) {
     1580                                syslog(LOG_ERROR, "tcp_del_cep => %d", ret2);
     1581                        }
     1582                        //delete_fd_by_id(&IO_TYPE_TCP, cepid);
    7461583                        delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
    747                         if (ret < 0) {
    748                                 return -EINVAL;
    749                         }
    750                 }
    751                 else if (fp->socket.repid != 0) {
    752                         ID repid = fp->socket.repid;
     1584                        if ((ret < 0) || (ret2 < 0)) {
     1585                                return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
     1586                        }
     1587                }
     1588                else if (socket->repid != 0) {
     1589                        ID repid = socket->repid;
    7531590                        ret = tcp_del_rep(repid);
    754                         delete_tcp_fd(tmax_tcp_cepid + repid);
     1591                        //delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + repid);
    7551592                        delete_id(tcp_repid_table, tcp_repid_table_count, repid);
    7561593                        if (ret < 0) {
     
    7641601        }
    7651602        case AF_INET6: {
    766                 return -EAFNOSUPPORT;
    767         }
     1603                if (socket->cepid != 0) {
     1604                        ID cepid = socket->cepid;
     1605                        ret = tcp6_can_cep(cepid, TFN_TCP_ALL);
     1606                        if ((ret < 0) && (ret != E_OBJ)) {
     1607                                syslog(LOG_ERROR, "tcp_can_cep => %d", ret);
     1608                        }
     1609                        ret = tcp6_sht_cep(cepid);
     1610                        if ((ret < 0) && (ret != E_OBJ)) {
     1611                                syslog(LOG_ERROR, "tcp6_sht_cep => %d", ret);
     1612                        }
     1613                        ret = tcp6_cls_cep(cepid, (socket->repid != 0) ? 0 : SOCKET_TIMEOUT);
     1614                        ret2 = tcp6_del_cep(cepid);
     1615                        if (ret2 < 0) {
     1616                                syslog(LOG_ERROR, "tcp6_del_cep => %d", ret2);
     1617                        }
     1618                        //delete_fd_by_id(&IO_TYPE_TCP, cepid);
     1619                        delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
     1620                        if ((ret < 0) || (ret2 < 0)) {
     1621                                return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
     1622                        }
     1623                }
     1624                else if (socket->repid != 0) {
     1625                        ID repid = socket->repid;
     1626                        ret = tcp6_del_rep(repid);
     1627                        //delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp6_cepid + repid);
     1628                        delete_id(tcp6_repid_table, tcp6_repid_table_count, repid);
     1629                        if (ret < 0) {
     1630                                return -EINVAL;
     1631                        }
     1632                }
     1633                else {
     1634                        return -EINVAL;
     1635                }
     1636                break;
     1637        }
     1638        default:
     1639                return -ENOPROTOOPT;
    7681640        }
    7691641
     
    7711643}
    7721644
    773 size_t tcp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
    774 {
    775         return shell_recv(fp->fd, dst, dstsz, 0);
    776 }
    777 
    778 size_t tcp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
    779 {
    780         return shell_send(fp->fd, src, srcsz, 0);
    781 }
    782 
    783 off_t tcp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
     1645size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
     1646{
     1647        return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
     1648}
     1649
     1650size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
     1651{
     1652        return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
     1653}
     1654
     1655off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
    7841656{
    7851657        return -EPERM;
    7861658}
    7871659
    788 int tcp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
    789 {
     1660int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
     1661{
     1662        socket_t *socket = (socket_t *)fp->exinf;
     1663
     1664        switch (req) {
     1665        case F_GETFL:
     1666                return socket->flags;
     1667        case F_SETFL:
     1668                socket->flags = (unsigned int)arg;
     1669                return 0;
     1670        }
     1671
    7901672        return -EINVAL;
    7911673}
    7921674
    793 int udp_fd_close(struct _IO_FILE *fp)
     1675bool_t tcp_fd_readable(struct SHELL_FILE *fp)
     1676{
     1677        ER ret;
     1678
     1679        socket_t *socket = (socket_t *)fp->exinf;
     1680        if (socket->cepid != 0) {
     1681                if (socket->len == 0) {
     1682                        ret = tcp_rcv_buf(socket->cepid, &socket->input, TMO_NBLK);
     1683                        if ((ret != E_WBLK) && (ret != E_OBJ) && (ret < 0)) {
     1684                                syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
     1685                                //return ret;
     1686                        }
     1687                        if (ret > 0) {
     1688                                ret = wai_sem(SEM_FILEDESC);
     1689                                if (ret < 0) {
     1690                                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     1691                                }
     1692                                socket->len += ret;
     1693                                ret = sig_sem(SEM_FILEDESC);
     1694                                if (ret < 0) {
     1695                                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1696                                }
     1697                        }
     1698                }
     1699                else ret = 1;
     1700                if (ret > 0) {
     1701                        return true;
     1702                }
     1703        }
     1704
     1705        return false;
     1706}
     1707
     1708bool_t tcp_fd_writable(struct SHELL_FILE *fp)
     1709{
     1710        //socket_t *socket = (socket_t *)fp->exinf;
     1711
     1712        return /*fp->writable &&*/ (fp->writeevt_w == fp->writeevt_r);
     1713}
     1714
     1715void tcp_fd_delete(struct SHELL_FILE *fp)
     1716{
     1717        socket_t *socket = (socket_t *)fp->exinf;
     1718        free(socket->buf);
     1719        socket->buf = NULL;
     1720        free(fp->exinf);
     1721        fp->exinf = NULL;
     1722}
     1723
     1724ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk)
     1725{
     1726        struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_TCP, cepid);
     1727        FLGPTN flgptn = 0;
     1728        ER ret;
     1729        int len;
     1730
     1731        if (fp == NULL)
     1732                return E_PAR;
     1733
     1734        socket_t *socket = (socket_t *)fp->exinf;
     1735        int fd = fp->fd;
     1736        FD_SET(fd, (fd_set *)&flgptn);
     1737
     1738        switch (fncd) {
     1739        case TFN_TCP_RCV_BUF:
     1740                len = *(int *)p_parblk;
     1741                if ((len <= 0) || (fp->exinf == NULL))
     1742                        return E_OK;
     1743
     1744                ret = wai_sem(SEM_FILEDESC);
     1745                if (ret < 0) {
     1746                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     1747                }
     1748                socket->len += len;
     1749                ret = sig_sem(SEM_FILEDESC);
     1750                if (ret < 0) {
     1751                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1752                }
     1753
     1754                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     1755
     1756                set_flg(FLG_SELECT_WAIT, flgptn);
     1757                return E_OK;
     1758
     1759        case TFN_TCP_RCV_DAT:
     1760                len = *(int *)p_parblk;
     1761                if ((len <= 0) || (fp->exinf == NULL))
     1762                        return E_OK;
     1763
     1764                ret = wai_sem(SEM_FILEDESC);
     1765                if (ret < 0) {
     1766                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     1767                }
     1768                socket->len += len;
     1769                ret = sig_sem(SEM_FILEDESC);
     1770                if (ret < 0) {
     1771                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1772                }
     1773
     1774                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     1775
     1776                set_flg(FLG_SELECT_WAIT, flgptn);
     1777                return E_OK;
     1778
     1779        case TFN_TCP_SND_DAT:
     1780                if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
     1781
     1782                set_flg(FLG_SELECT_WAIT, flgptn);
     1783                return E_OK;
     1784
     1785        case TFN_TCP_CAN_CEP:
     1786                if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
     1787
     1788                set_flg(FLG_SELECT_WAIT, flgptn);
     1789                return E_OK;
     1790
     1791        case TFN_TCP_DEL_REP:
     1792                delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + cepid);
     1793                return E_OK;
     1794
     1795        case TFN_TCP_DEL_CEP:
     1796                delete_fd_by_id(&IO_TYPE_TCP, cepid);
     1797                return E_OK;
     1798
     1799        default:
     1800                return E_OK;
     1801        }
     1802}
     1803
     1804ER socket_tcp6_callback(ID cepid, FN fncd, void *p_parblk)
     1805{
     1806        struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_TCP, cepid);
     1807        FLGPTN flgptn = 0;
     1808        ER ret;
     1809        int len;
     1810
     1811        if (fp == NULL)
     1812                return E_PAR;
     1813
     1814        socket_t *socket = (socket_t *)fp->exinf;
     1815        int fd = fp->fd;
     1816        FD_SET(fd, (fd_set *)&flgptn);
     1817
     1818        switch (fncd) {
     1819        case TFN_TCP_RCV_BUF:
     1820                len = *(int *)p_parblk;
     1821                if ((len <= 0) || (fp->exinf == NULL))
     1822                        return E_OK;
     1823
     1824                ret = wai_sem(SEM_FILEDESC);
     1825                if (ret < 0) {
     1826                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     1827                }
     1828                socket->len += len;
     1829                ret = sig_sem(SEM_FILEDESC);
     1830                if (ret < 0) {
     1831                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1832                }
     1833
     1834                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     1835
     1836                set_flg(FLG_SELECT_WAIT, flgptn);
     1837                return E_OK;
     1838
     1839        case TFN_TCP_RCV_DAT:
     1840                len = *(int *)p_parblk;
     1841                if ((len <= 0) || (fp->exinf == NULL))
     1842                        return E_OK;
     1843
     1844                ret = wai_sem(SEM_FILEDESC);
     1845                if (ret < 0) {
     1846                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     1847                }
     1848                socket->len += len;
     1849                ret = sig_sem(SEM_FILEDESC);
     1850                if (ret < 0) {
     1851                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     1852                }
     1853
     1854                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     1855
     1856                set_flg(FLG_SELECT_WAIT, flgptn);
     1857                return E_OK;
     1858
     1859        case TFN_TCP_SND_DAT:
     1860                if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
     1861
     1862                set_flg(FLG_SELECT_WAIT, flgptn);
     1863                return E_OK;
     1864
     1865        case TFN_TCP_CAN_CEP:
     1866                if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
     1867
     1868                set_flg(FLG_SELECT_WAIT, flgptn);
     1869                return E_OK;
     1870
     1871        case TFN_TCP_DEL_REP:
     1872                delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + cepid);
     1873                return E_OK;
     1874
     1875        case TFN_TCP_DEL_CEP:
     1876                delete_fd_by_id(&IO_TYPE_TCP, cepid);
     1877                return E_OK;
     1878
     1879        default:
     1880                return E_OK;
     1881        }
     1882}
     1883
     1884int udp_fd_close(struct SHELL_FILE *fp)
    7941885{
    7951886        ER ret;
    7961887        ID cepid;
    797         switch (fp->socket.family) {
     1888
     1889        socket_t *socket = (socket_t *)fp->exinf;
     1890        switch (socket->family) {
    7981891        case AF_INET: {
    799                 cepid = fp->socket.cepid;
     1892                cepid = socket->cepid;
     1893                ret = udp_can_cep(cepid, TFN_UDP_ALL);
     1894                if ((ret < 0) && (ret != E_OBJ)) {
     1895                        syslog(LOG_ERROR, "udp_can_cep => %d", ret);
     1896                }
    8001897                ret = udp_del_cep(cepid);
    801                 delete_udp_fd(cepid);
     1898                if (ret < 0) {
     1899                        syslog(LOG_ERROR, "udp_del_cep => %d", ret);
     1900                }
     1901                //delete_fd_by_id(&IO_TYPE_UDP, cepid);
    8021902                delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
    8031903                if (ret < 0) {
     
    8071907        }
    8081908        case AF_INET6: {
    809                 return -EAFNOSUPPORT;
    810         }
     1909                cepid = socket->cepid;
     1910                ret = udp6_can_cep(cepid, TFN_UDP_ALL);
     1911                if ((ret < 0) && (ret != E_OBJ)) {
     1912                        syslog(LOG_ERROR, "udp6_can_cep => %d", ret);
     1913                }
     1914                ret = udp6_del_cep(cepid);
     1915                if (ret < 0) {
     1916                        syslog(LOG_ERROR, "udp6_del_cep => %d", ret);
     1917                }
     1918                //delete_fd_by_id(&IO_TYPE_UDP, cepid);
     1919                delete_id(udp6_cepid_table, udp6_cepid_table_count, cepid);
     1920                if (ret < 0) {
     1921                        return -EINVAL;
     1922                }
     1923                break;
     1924        }
     1925        default:
     1926                return -ENOPROTOOPT;
    8111927        }
    8121928
     
    8141930}
    8151931
    816 size_t udp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
    817 {
    818         return shell_recv(fp->fd, dst, dstsz, 0);
    819 }
    820 
    821 size_t udp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
    822 {
    823         return shell_send(fp->fd, src, srcsz, 0);
    824 }
    825 
    826 off_t udp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
     1932size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
     1933{
     1934        return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
     1935}
     1936
     1937size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
     1938{
     1939        return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
     1940}
     1941
     1942off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
    8271943{
    8281944        return -EPERM;
    8291945}
    8301946
    831 int udp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
     1947int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    8321948{
    8331949        return -EINVAL;
     1950}
     1951
     1952bool_t udp_fd_readable(struct SHELL_FILE *fp)
     1953{
     1954        socket_t *socket = (socket_t *)fp->exinf;
     1955        if (socket->cepid != 0) {
     1956                if (socket->input != NULL) {
     1957                        return true;
     1958                }
     1959        }
     1960
     1961        return false;
     1962}
     1963
     1964bool_t udp_fd_writable(struct SHELL_FILE *fp)
     1965{
     1966        //socket_t *socket = (socket_t *)fp->exinf;
     1967
     1968        return fp->writable && (fp->writeevt_w == fp->writeevt_r);
     1969}
     1970
     1971void udp_fd_delete(struct SHELL_FILE *fp)
     1972{
     1973        //socket_t *socket = (socket_t *)fp->exinf;
     1974        //free(socket->buf);
     1975        //socket->buf = NULL;
     1976        free(fp->exinf);
     1977        fp->exinf = NULL;
     1978}
     1979
     1980ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk)
     1981{
     1982        struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_UDP, cepid);
     1983        FLGPTN flgptn = 0;
     1984        int len;
     1985
     1986        if (fp == NULL)
     1987                return E_PAR;
     1988
     1989        int fd = fp->fd;
     1990        FD_SET(fd, (fd_set *)&flgptn);
     1991
     1992        switch (fncd) {
     1993        case TEV_UDP_RCV_DAT:
     1994        {
     1995                T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
     1996                len = udppara->len;
     1997                if ((len <= 0) || (fp->exinf == NULL))
     1998                        return E_OK;
     1999
     2000                ER ret = wai_sem(SEM_FILEDESC);
     2001                if (ret < 0) {
     2002                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     2003                }
     2004                socket_t *socket = (socket_t *)fp->exinf;
     2005                socket->len = len;
     2006                if (socket->input != NULL) {
     2007                        ret = rel_net_buf(socket->input);
     2008                        if (ret < 0) {
     2009                                syslog(LOG_ERROR, "rel_net_buf => %d", ret);
     2010                        }
     2011                }
     2012                socket->input = udppara->input;
     2013                socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
     2014                memset(&socket->raddr4, 0, sizeof(struct sockaddr_in));
     2015                socket->raddr4.sin_family = AF_INET;
     2016                socket->raddr4.sin_port = htons(udppara->rep4.portno);
     2017                socket->raddr4.sin_addr.s_addr = htonl(udppara->rep4.ipaddr);
     2018                udppara->input->flags |= NB_FLG_NOREL_IFOUT;
     2019                ret = sig_sem(SEM_FILEDESC);
     2020                if (ret < 0) {
     2021                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     2022                }
     2023
     2024                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     2025
     2026                set_flg(FLG_SELECT_WAIT, flgptn);
     2027                return E_OK;
     2028        }
     2029        case TFN_UDP_CRE_CEP:
     2030                return E_OK;
     2031
     2032        case TFN_UDP_RCV_DAT:
     2033                len = *(int *)p_parblk;
     2034                if ((len <= 0) || (fp->exinf == NULL))
     2035                        return E_OK;
     2036
     2037                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     2038
     2039                set_flg(FLG_SELECT_WAIT, flgptn);
     2040                return E_OK;
     2041
     2042        case TFN_UDP_SND_DAT:
     2043                if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
     2044
     2045                set_flg(FLG_SELECT_WAIT, flgptn);
     2046                return E_OK;
     2047
     2048        case TFN_UDP_CAN_CEP:
     2049                if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
     2050
     2051                set_flg(FLG_SELECT_WAIT, flgptn);
     2052                return E_OK;
     2053
     2054        case TFN_UDP_DEL_CEP:
     2055                delete_fd_by_id(&IO_TYPE_UDP, cepid);
     2056                return E_OK;
     2057
     2058        default:
     2059                return E_OK;
     2060        }
     2061}
     2062
     2063ER socket_udp6_callback(ID cepid, FN fncd, void *p_parblk)
     2064{
     2065        struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_UDP, cepid);
     2066        FLGPTN flgptn = 0;
     2067        int len;
     2068
     2069        if (fp == NULL)
     2070                return E_PAR;
     2071
     2072        int fd = fp->fd;
     2073        FD_SET(fd, (fd_set *)&flgptn);
     2074
     2075        switch (fncd) {
     2076        case TEV_UDP_RCV_DAT:
     2077        {
     2078                T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
     2079                len = udppara->len;
     2080                if ((len <= 0) || (fp->exinf == NULL))
     2081                        return E_OK;
     2082
     2083                ER ret = wai_sem(SEM_FILEDESC);
     2084                if (ret < 0) {
     2085                        syslog(LOG_ERROR, "wai_sem => %d", ret);
     2086                }
     2087                socket_t *socket = (socket_t *)fp->exinf;
     2088                socket->len = len;
     2089                if (socket->input != NULL) {
     2090                        ret = rel_net_buf(socket->input);
     2091                        if (ret < 0) {
     2092                                syslog(LOG_ERROR, "rel_net_buf => %d", ret);
     2093                        }
     2094                }
     2095                socket->input = udppara->input;
     2096                socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
     2097                memset(&socket->raddr6, 0, sizeof(struct sockaddr_in6));
     2098                socket->raddr6.sin6_family = AF_INET;
     2099                socket->raddr6.sin6_port = htons(udppara->rep6.portno);
     2100                addrcpy(&socket->raddr6.sin6_addr, &udppara->rep6.ipaddr, 16);
     2101                udppara->input->flags |= NB_FLG_NOREL_IFOUT;
     2102                ret = sig_sem(SEM_FILEDESC);
     2103                if (ret < 0) {
     2104                        syslog(LOG_ERROR, "sig_sem => %d", ret);
     2105                }
     2106
     2107                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     2108
     2109                set_flg(FLG_SELECT_WAIT, flgptn);
     2110                return E_OK;
     2111        }
     2112        case TFN_UDP_CRE_CEP:
     2113                return E_OK;
     2114
     2115        case TFN_UDP_RCV_DAT:
     2116                len = *(int *)p_parblk;
     2117                if ((len <= 0) || (fp->exinf == NULL))
     2118                        return E_OK;
     2119
     2120                if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
     2121
     2122                set_flg(FLG_SELECT_WAIT, flgptn);
     2123                return E_OK;
     2124
     2125        case TFN_UDP_SND_DAT:
     2126                if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
     2127
     2128                set_flg(FLG_SELECT_WAIT, flgptn);
     2129                return E_OK;
     2130
     2131        case TFN_UDP_CAN_CEP:
     2132                if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
     2133
     2134                set_flg(FLG_SELECT_WAIT, flgptn);
     2135                return E_OK;
     2136
     2137        case TFN_UDP_DEL_CEP:
     2138                delete_fd_by_id(&IO_TYPE_UDP, cepid);
     2139                return E_OK;
     2140
     2141        default:
     2142                return E_OK;
     2143        }
    8342144}
    8352145
     
    9042214}
    9052215#endif
     2216
     2217// musl-1.1.18/network/lookup.h
     2218struct address {
     2219        int family;
     2220        unsigned scopeid;
     2221        uint8_t addr[16];
     2222        int sortkey;
     2223};
     2224
     2225#define MAXNS 3
     2226
     2227struct resolvconf {
     2228        struct address ns[MAXNS];
     2229        unsigned nns, attempts, ndots;
     2230        unsigned timeout;
     2231};
     2232
     2233// musl-1.1.18/network/resolvconf.c
     2234int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz)
     2235{
     2236        int nns = 0;
     2237
     2238        conf->ndots = 1;
     2239        conf->timeout = 5;
     2240        conf->attempts = 2;
     2241        if (search) *search = 0;
     2242
     2243#if defined(SUPPORT_INET4)
     2244        T_IN4_ADDR      in4_addr;
     2245        conf->ns[nns].family = AF_INET;
     2246        conf->ns[nns].scopeid = 0;
     2247        dns_in4_get_addr(&in4_addr);
     2248        *(uint32_t *)conf->ns[nns].addr = ntohl(in4_addr);
     2249        nns++;
     2250#endif
     2251
     2252#if defined(SUPPORT_INET6)
     2253        conf->ns[nns].family = AF_INET6;
     2254        conf->ns[nns].scopeid = 0;
     2255        dns_in6_get_addr((T_IN6_ADDR *)conf->ns[nns].addr);
     2256        nns++;
     2257#endif
     2258        conf->nns = nns;
     2259
     2260        return 0;
     2261}
  • EcnlProtoTool/trunk/ntshell/src/syscall.c

    r439 r441  
    101101        switch (r->n) {
    102102        case SYS__llseek:
    103                 return shell_llseek(r->a, (((off_t)r->b) << 32) + (off_t)r->c, (off_t *)r->d, r->e);
     103                ret = shell_llseek(r->a, (((off_t)r->b) << 32) + (off_t)r->c, (off_t *)r->d, r->e);
    104104        case SYS__newselect:
    105105                ret = shell_select(r->a, (fd_set *)r->b, (fd_set *)r->c, (fd_set *)r->d, (struct timeval *)r->e);
     
    359359                break;
    360360        case SYS_gettid:
    361                 no_implement("gettid\n");
     361                ret = shell_gettid();
    362362                break;
    363363        case SYS_gettimeofday:
     
    439439                break;
    440440        case SYS_lstat64:
    441                 ret = shell_lstat((const char *__restrict)r->a, (struct stat *__restrict)r->b);
     441                ret = shell_lstat((const char *restrict)r->a, (struct stat *restrict)r->b);
    442442                break;
    443443        case SYS_madvise:
     
    472472                break;
    473473        case SYS_mprotect:
    474                 return shell_mprotect((void *)r->a, (size_t)r->b, r->c);
     474                ret = shell_mprotect((void *)r->a, (size_t)r->b, r->c);
    475475                break;
    476476        case SYS_mq_getsetattr:
     
    520520                break;
    521521        case SYS_nanosleep:
    522                 no_implement("nanosleep\n");
     522                ret = shell_nanosleep((const struct timespec *)r->a, (struct timespec *)r->b);
    523523                break;
    524524        case SYS_nice:
     
    538538                break;
    539539        case SYS_pipe:
    540                 no_implement("pipe\n");
     540                ret = shell_pipe((int *)r->a);
    541541                break;
    542542        case SYS_pipe2:
     
    601601                break;
    602602        case SYS_recv:
    603                 ret = shell_recv(r->a, (void *)r->b, (size_t)r->c, r->d);
     603                ret = shell_recvfrom(r->a, (void *)r->b, (size_t)r->c, r->d, NULL, NULL);
    604604                break;
    605605        case SYS_recvfrom:
    606                 if (r->e == 0)
    607                         ret = shell_recv(r->a, (void *)r->b, (size_t)r->c, r->d);
    608                 else
    609                         ret = shell_recvfrom(r->a, (void *)r->b, (size_t)r->c, r->d, (struct sockaddr *)r->e, (socklen_t *)r->f);
     606                ret = shell_recvfrom(r->a, (void *)r->b, (size_t)r->c, r->d, (struct sockaddr *)r->e, (socklen_t *)r->f);
    610607                break;
    611608        case SYS_recvmsg:
     
    634631                break;
    635632        case SYS_rt_sigaction:
    636                 ret = shell_sigaction(r->a, (const struct sigaction *)r->b, (struct sigaction *)r->c);
     633                ret = shell_sigaction(r->a, (const struct sigaction *)r->b, (struct sigaction *)r->c, (size_t)r->d);
    637634                break;
    638635        case SYS_rt_sigpending:
     
    652649                break;
    653650        case SYS_send:
    654                 ret = shell_send(r->a, (const void *)r->b, (size_t)r->c, r->d);
     651                ret = shell_sendto(r->a, (const void *)r->b, (size_t)r->c, r->d, NULL, 0);
    655652                break;
    656653        case SYS_sendmsg:
     
    658655                break;
    659656        case SYS_sendto:
    660                 if (r->e == 0)
    661                         ret = shell_send(r->a, (const void *)r->b, (size_t)r->c, r->d);
    662                 else
    663                         ret = shell_sendto(r->a, (const void *)r->b, (size_t)r->c, r->d, (const struct sockaddr *)r->e, (socklen_t)r->f);
     657                ret = shell_sendto(r->a, (const void *)r->b, (size_t)r->c, r->d, (const struct sockaddr *)r->e, (socklen_t)r->f);
    664658                break;
    665659        case SYS_setsockopt:
     
    869863                break;
    870864        case SYS_tkill:
    871                 no_implement("tkill\n");
     865                ret = shell_tkill((int)r->a, (int)r->b);
    872866                break;
    873867        case SYS_truncate64:
     
    918912        case SYS_writev:
    919913                ret = shell_writev((int)r->a, (const struct iovec *)r->b, (size_t)r->c);
     914                break;
     915        case SYS_getpeername:
     916                ret = shell_getpeername((int)r->a, (struct sockaddr *)r->b, (socklen_t *)r->c);
     917                break;
     918        case SYS_getsockname:
     919                ret = shell_getsockname((int)r->a, (struct sockaddr *)r->b, (socklen_t *)r->c);
     920                break;
     921        case SYS_sigprocmask:
     922                ret = shell_sigprocmask((int)r->a, (const sigset_t *)r->b, (sigset_t *)r->c);
     923                break;
     924        case SYS_sigaction:
     925                ret = shell_sigaction((int)r->a, (const struct k_sigaction *)r->b, (struct k_sigaction *)r->c, (size_t)r->d);
    920926                break;
    921927        default:
  • EcnlProtoTool/trunk/ntshell/src/tinet_app_config.h

    r331 r441  
    285285#ifdef SUPPORT_ETHER
    286286
    287 #ifdef DHCP4_CLI_CFG
    288 
    289 #define IPV4_ADDR_LOCAL                 MAKE_IPV4_ADDR(0,0,0,0)
    290 #define IPV4_ADDR_LOCAL_MASK            MAKE_IPV4_ADDR(0,0,0,0)
    291 #define IPV4_ADDR_DEFAULT_GW            MAKE_IPV4_ADDR(0,0,0,0)
    292 
    293 #else   /* of #ifdef DHCP4_CLI_CFG */
    294 
    295287#define IPV4_ADDR_LOCAL                 MAKE_IPV4_ADDR(192,168,137,110)
    296 #define IPV4_ADDR_LOCAL_MASK            MAKE_IPV4_ADDR(255,255,255,0)
    297 #define IPV4_ADDR_DEFAULT_GW            MAKE_IPV4_ADDR(192,168,137,1)
    298 
    299 #endif  /* of #ifdef DHCP4_CLI_CFG */
     288#define IPV4_ADDR_LOCAL_MASK    MAKE_IPV4_ADDR(255,255,255,0)
     289#define IPV4_ADDR_DEFAULT_GW    MAKE_IPV4_ADDR(192,168,137,1)
    300290
    301291#define NUM_IN4_STATIC_ROUTE_ENTRY      3
  • EcnlProtoTool/trunk/ntshell/src/tinet_main.cfg

    r439 r441  
    22 *  TOPPERS PROJECT Home Network Working Group Software
    33 *
    4  *  Copyright (C) 2017-2019 Cores Co., Ltd. Japan
     4 *  Copyright (C) 2014-2019 Cores Co., Ltd. Japan
    55 *
    66 *  上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
  • EcnlProtoTool/trunk/ntshell/webserver/httpd-fs.c

    r436 r441  
    6363#endif /* NULL */
    6464
    65 extern ntstdio_t ntstdio;
     65extern ntstdio_t *ntstdio;
    6666
    6767/*-----------------------------------------------------------------------------------*/
     
    8989                        }
    9090                        else {
    91                                 ntstdio_printf(&ntstdio, "%s/%s\n", path, fn);
     91                                ntstdio_printf(ntstdio, "%s/%s\n", path, fn);
    9292                        }
    9393                }
     
    114114        if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
    115115                if ((res = f_opendir(&dir, name)) != FR_OK) {
    116                         ntstdio_printf(&ntstdio, "f_opendir(%s) => %d\n", name, res);
     116                        ntstdio_printf(ntstdio, "f_opendir(%s) => %d\n", name, res);
    117117                        return 0;
    118118                }
    119119
    120120                if ((res = f_readdir(&dir, &fno)) != FR_OK) {
    121                         ntstdio_printf(&ntstdio, "f_readdir(%s) => %d\n", name, res);
     121                        ntstdio_printf(ntstdio, "f_readdir(%s) => %d\n", name, res);
    122122                        return 0;
    123123                }
     
    132132
    133133                if (res != FR_OK) {
    134                         ntstdio_printf(&ntstdio, "f_open(%s) => %d %x\n", name, res, fno.fattrib);
     134                        ntstdio_printf(ntstdio, "f_open(%s) => %d %x\n", name, res, fno.fattrib);
    135135                        return 0;
    136136                }
     
    139139        file->len = fd->fsize;
    140140
    141         //ntstdio_printf(&ntstdio, "httpd_fs_open(%s) %d\n", name, file->len);
     141        //ntstdio_printf(ntstdio, "httpd_fs_open(%s) %d\n", name, file->len);
    142142
    143143        return 1;
     
    158158
    159159        if ((res = f_open(fd, name, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
    160                 ntstdio_printf(&ntstdio, "f_open(%s) => %d\n", name, res);
     160                ntstdio_printf(ntstdio, "f_open(%s) => %d\n", name, res);
    161161                return 0;
    162162        }
     
    165165        file->len = 0;
    166166
    167         //ntstdio_printf(&ntstdio, "httpd_fs_create(%s) %d\n", file->name, file->len);
     167        //ntstdio_printf(ntstdio, "httpd_fs_create(%s) %d\n", file->name, file->len);
    168168
    169169        return 1;
     
    179179
    180180        if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
    181                 ntstdio_printf(&ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
     181                ntstdio_printf(ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
    182182                return 0;
    183183        }
    184184
    185185        if (file->pos != fd->fptr) {
    186                 ntstdio_printf(&ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
     186                ntstdio_printf(ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
    187187        }
    188188
    189189        if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
    190                 ntstdio_printf(&ntstdio, "f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
    191                 return 0;
    192         }
    193 
    194         //ntstdio_printf(&ntstdio, "httpd_fs_read(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
     190                ntstdio_printf(ntstdio, "f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
     191                return 0;
     192        }
     193
     194        //ntstdio_printf(ntstdio, "httpd_fs_read(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
    195195
    196196        return rlen;
     
    206206
    207207        if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
    208                 ntstdio_printf(&ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
     208                ntstdio_printf(ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
    209209                return 0;
    210210        }
    211211
    212212        if (file->pos != fd->fptr) {
    213                 ntstdio_printf(&ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
     213                ntstdio_printf(ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
    214214        }
    215215
    216216        if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
    217                 ntstdio_printf(&ntstdio, "f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
     217                ntstdio_printf(ntstdio, "f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
    218218                return 0;
    219219        }
     
    222222        file->len += rlen;
    223223
    224         //ntstdio_printf(&ntstdio, "httpd_fs_write(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
     224        //ntstdio_printf(ntstdio, "httpd_fs_write(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
    225225
    226226        return rlen;
     
    234234
    235235        if ((ret = f_close(fd)) != FR_OK) {
    236                 ntstdio_printf(&ntstdio, "f_close(%s) => %d\n", file->name, ret);
     236                ntstdio_printf(ntstdio, "f_close(%s) => %d\n", file->name, ret);
    237237                return 0;
    238238        }
  • EcnlProtoTool/trunk/ntshell/webserver/httpd.c

    r436 r441  
    5959#define FALSE 0
    6060
    61 extern ntstdio_t ntstdio;
     61extern ntstdio_t *ntstdio;
    6262SYSTIM httpd_time;
    6363struct httpd_state *uploding;
     
    6565
    6666/*  TCP 送受信ウィンドバッファ  */
     67#ifdef SUPPORT_INET6
     68uint8_t tcp6_swbuf1[TCP_SWBUF_SIZE];
     69uint8_t tcp6_rwbuf1[TCP_RWBUF_SIZE];
     70uint8_t tcp6_swbuf2[TCP_SWBUF_SIZE];
     71uint8_t tcp6_rwbuf2[TCP_RWBUF_SIZE];
     72#endif
     73
     74#ifdef SUPPORT_INET4
    6775uint8_t tcp_swbuf1[TCP_SWBUF_SIZE];
    6876uint8_t tcp_rwbuf1[TCP_RWBUF_SIZE];
    6977uint8_t tcp_swbuf2[TCP_SWBUF_SIZE];
    7078uint8_t tcp_rwbuf2[TCP_RWBUF_SIZE];
     79#endif
    7180
    7281#define ISO_nl      0x0a
     
    325334                        // アップロード先はSDカード
    326335                        s->filename[0] = '1';
    327                         ntstdio_printf(&ntstdio, "create:    %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     336                        ntstdio_printf(ntstdio, "create:    %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    328337                        if (!httpd_fs_create(s->filename, &s->file)) {
    329338                                goto error;
     
    333342                }
    334343                else if (ntlibc_strcmp(s->filename, uploding->filename) == 0) {
    335                         ntstdio_printf(&ntstdio, "collision: %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     344                        ntstdio_printf(ntstdio, "collision: %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    336345                        goto error;
    337346                }
     
    365374
    366375        if (s->message.body_is_final) {
    367                 ntstdio_printf(&ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
     376                ntstdio_printf(ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
    368377                        "on last on_body callback call "
    369378                        "but it doesn't! ***\n\n");
     
    378387
    379388        if (s->message.body_is_final) {
    380                 ntstdio_printf(&ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     389                ntstdio_printf(ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    381390                httpd_fs_close(&s->file);
    382391                memset(&s->file, 0, sizeof(s->file));
     
    403412        struct httpd_state *s = get_context(p);
    404413        if (s->message.should_keep_alive != http_should_keep_alive(p)) {
    405                 ntstdio_printf(&ntstdio, "\n\n *** Error http_should_keep_alive() should have same \n"
     414                ntstdio_printf(ntstdio, "\n\n *** Error http_should_keep_alive() should have same \n"
    406415                        "value in both on_message_complete and on_headers_complete "
    407416                        "but it doesn't! ***\n\n");
     
    412421                http_body_is_final(p) &&
    413422                !s->message.body_is_final) {
    414                 ntstdio_printf(&ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
     423                ntstdio_printf(ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
    415424                        "on last on_body callback call "
    416425                        "but it doesn't! ***\n\n");
     
    484493                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    485494                if (slen < 0) {
    486                         ntstdio_printf(&ntstdio, "send_file#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     495                        ntstdio_printf(ntstdio, "send_file#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    487496                        s->state = STATE_CLOSING;
    488497                        break;
     
    497506                len = httpd_fs_read(&s->file, buf, len);
    498507                if (len <= 0) {
    499                         ntstdio_printf(&ntstdio, "send_file#httpd_fs_read(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     508                        ntstdio_printf(ntstdio, "send_file#httpd_fs_read(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    500509                        break;
    501510                }
     
    505514
    506515                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    507                         ntstdio_printf(&ntstdio, "send_file#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     516                        ntstdio_printf(ntstdio, "send_file#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    508517                        s->state = STATE_CLOSING;
    509518                        break;
     
    511520        }
    512521
    513         ntstdio_printf(&ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     522        ntstdio_printf(ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    514523        httpd_fs_close(&s->file);
    515524        s->file.len = 0;
     
    527536                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    528537                if (slen < 0) {
    529                         ntstdio_printf(&ntstdio, "send_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     538                        ntstdio_printf(ntstdio, "send_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    530539                        s->state = STATE_CLOSING;
    531540                        break;
     
    544553
    545554                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    546                         ntstdio_printf(&ntstdio, "send_data#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     555                        ntstdio_printf(ntstdio, "send_data#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    547556                        s->state = STATE_CLOSING;
    548557                        break;
     
    690699                break;
    691700        case OUT_STATE_OPEN_GET_FILE:
    692                 ntstdio_printf(&ntstdio, "open:      %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     701                ntstdio_printf(ntstdio, "open:      %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    693702                if (!httpd_fs_open(s->filename, sizeof(s->message.request_url), &s->file)) {
    694703                        s->filename = NULL;
     
    704713                break;
    705714        case OUT_STATE_WAIT_POST_BODY:
    706                 //ntstdio_printf(&ntstdio, "wait post body\n");
     715                //ntstdio_printf(ntstdio, "wait post body\n");
    707716                s->out.wait = true;
    708717                break;
    709718        case OUT_STATE_BODY_RECEIVED:
    710                 //ntstdio_printf(&ntstdio, "body received\n");
     719                //ntstdio_printf(ntstdio, "body received\n");
    711720                s->out.statushdr = http_header_200;
    712721                s->out.state = OUT_STATE_SEND_HEADER;
    713722                break;
    714723        case OUT_STATE_SEND_HEADER:
    715                 //ntstdio_printf(&ntstdio, "send header\n");
     724                //ntstdio_printf(ntstdio, "send header\n");
    716725                send_headers(s, s->out.statushdr);
    717726                break;
    718727        case OUT_STATE_SEND_FILE:
    719                 //ntstdio_printf(&ntstdio, "send file %d\n", s->file.len);
     728                //ntstdio_printf(ntstdio, "send file %d\n", s->file.len);
    720729                send_file(s);
    721730                break;
    722731        case OUT_STATE_SEND_DATA:
    723                 //ntstdio_printf(&ntstdio, "send data %d\n", s->response_len);
     732                //ntstdio_printf(ntstdio, "send data %d\n", s->response_len);
    724733                send_data(s);
    725734                break;
    726735        case OUT_STATE_SEND_END:
    727                 //ntstdio_printf(&ntstdio, "send end\n");
     736                //ntstdio_printf(ntstdio, "send end\n");
    728737                s->out.wait = true;
    729738                if (s->message.should_keep_alive && s->reset == 0) {
     
    783792        slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    784793        if (slen < 0) {
    785                 ntstdio_printf(&ntstdio, "send_ws_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     794                ntstdio_printf(ntstdio, "send_ws_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    786795                return;
    787796        }
     
    847856                                //}
    848857                        }
    849                         ntstdio_printf(&ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     858                        ntstdio_printf(ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    850859                        uploding = NULL;
    851860                        s->state = STATE_CLOSING;
     
    855864                tcp_rel_buf(s->cepid, done);
    856865                if (s->parser.http_errno != HPE_OK) {
    857                         ntstdio_printf(&ntstdio, "http_parser error %s.%d => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
     866                        ntstdio_printf(ntstdio, "http_parser error %s.%d => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
    858867                        uploding = NULL;
    859868                        s->state = STATE_CLOSING;
     
    880889                                        break;
    881890                                }
    882                                 ntstdio_printf(&ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     891                                ntstdio_printf(ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    883892                                s->state = STATE_CLOSING;
    884893                                break;
     
    918927
    919928        if (s == NULL)
    920                 ntstdio_printf(&ntstdio, "callback_nblk_tcp(%d, %d)\n", fncd, cepid);
     929                ntstdio_printf(ntstdio, "callback_nblk_tcp(%d, %d)\n", fncd, cepid);
    921930        else
    922                 ntstdio_printf(&ntstdio, "callback_nblk_tcp(%d, %s.%d)\n", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
     931                ntstdio_printf(ntstdio, "callback_nblk_tcp(%d, %s.%d)\n", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
    923932
    924933        return E_PAR;
     
    936945                ret2 = get_tim(&httpd_time);
    937946                if (ret2 != E_OK) {
    938                         ntstdio_printf(&ntstdio, "get_tim\n");
     947                        ntstdio_printf(ntstdio, "get_tim\n");
    939948                        return;
    940949                }
     
    944953                        memset(&s->dst, 0, sizeof(s->dst));
    945954                        if ((ret = tcp_acp_cep(s->cepid, TCP_REPID, (T_IPV4EP *)s->dst, TMO_FEVR)) != E_OK) {
    946                                 ntstdio_printf(&ntstdio, "tcp_acp_cep(%d) => %d\n", s->cepid, ret);
     955                                ntstdio_printf(ntstdio, "tcp_acp_cep(%d) => %d\n", s->cepid, ret);
    947956                                tslp_tsk(100 * 1000);   // TODO
    948957                                s->state = STATE_CLOSING;
     
    950959                        }
    951960                        IP2STR(s->addr, &((T_IPV4EP *)s->dst)->ipaddr);
    952                         ntstdio_printf(&ntstdio, "connected: %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     961                        ntstdio_printf(ntstdio, "connected: %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
    953962                        memset(&s->in, 0, sizeof(s->in));
    954963                        memset(&s->out, 0, sizeof(s->out));
     
    965974                        break;
    966975                case STATE_CLOSING:
    967                         ntstdio_printf(&ntstdio, "close:     %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     976                        ntstdio_printf(ntstdio, "close:     %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
    968977                        tcp_sht_cep(s->cepid);
    969978                        tcp_cls_cep(s->cepid, TMO_FEVR);
  • EcnlProtoTool/trunk/ntshell/webserver/httpd.h

    r436 r441  
    5757 /*  TCP 送受信ウィンドバッファサイズ  */
    5858
    59 #define TCP_SWBUF_SIZE  512
    60 #define TCP_RWBUF_SIZE  512
     59#define TCP_SWBUF_SIZE  1024
     60#define TCP_RWBUF_SIZE  1024
    6161
    6262/*
     
    6666
    6767 /*  TCP 送受信ウィンドバッファ  */
    68 
     68#ifdef SUPPORT_INET6
     69extern uint8_t tcp6_swbuf1[];
     70extern uint8_t tcp6_rwbuf1[];
     71extern uint8_t tcp6_swbuf2[];
     72extern uint8_t tcp6_rwbuf2[];
     73#endif
     74
     75#ifdef SUPPORT_INET4
    6976extern uint8_t tcp_swbuf1[];
    7077extern uint8_t tcp_rwbuf1[];
    7178extern uint8_t tcp_swbuf2[];
    7279extern uint8_t tcp_rwbuf2[];
     80#endif
    7381
    7482/* HTTPサーバータスク */
  • EcnlProtoTool/trunk/ntshell/webserver/tinet_httpd.cfg

    r436 r441  
    6262TCP6_CRE_CEP (TCP6_CEPID1, {
    6363        0,
    64         tcp_swbuf1,
     64        tcp6_swbuf1,
    6565        TCP_SWBUF_SIZE,
    66         tcp_rwbuf1,
     66        tcp6_rwbuf1,
    6767        TCP_RWBUF_SIZE,
    6868        (FP)callback_nblk_tcp
     
    7171TCP6_CRE_CEP (TCP6_CEPID2, {
    7272        0,
    73         tcp_swbuf2,
     73        tcp6_swbuf2,
    7474        TCP_SWBUF_SIZE,
    75         tcp_rwbuf2,
     75        tcp6_rwbuf2,
    7676        TCP_RWBUF_SIZE,
    7777        (FP)callback_nblk_tcp
  • EcnlProtoTool/trunk/prototool/src/linker_scriptDebug.ld

    r439 r441  
    88
    99MEMORY {
    10         BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x00220000
    11         ROM (rx)         : ORIGIN = 0x18220000, LENGTH = 0x005E0000
     10        BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x00200000
     11        ROM (rx)         : ORIGIN = 0x18200000, LENGTH = 0x00600000
    1212        RAM (xrw)        : ORIGIN = 0x20100000, LENGTH = 8M
    1313}
Note: See TracChangeset for help on using the changeset viewer.