Changeset 441 for EcnlProtoTool/trunk
- Timestamp:
- Jul 10, 2020, 9:09:25 PM (4 years ago)
- Location:
- EcnlProtoTool/trunk
- Files:
-
- 13 added
- 1 deleted
- 29 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/asp3_dcre/Debug/Makefile
r429 r441 246 246 # システムサービスに関する定義 247 247 # 248 ifdef TCINSTALL249 GCC_PATH = $(TCINSTALL)250 else251 GCC_PATH = $(shell which $(CC))/../../252 endif253 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.o255 256 248 SYSSVC_DIRS := $(TECSGENDIR) $(SRCDIR)/tecs_kernel \ 257 249 $(SYSSVC_DIRS) $(SRCDIR)/syssvc … … 380 372 CFG_COBJS := $(addprefix $(OBJDIR)/, $(CFG_COBJS)) 381 373 CFG_DMY := $(addprefix $(OBJDIR)/, $(CFG_DMY)) 382 MALLOC_OBJS := $(addprefix $(OBJDIR)/, $(MALLOC_OBJS))383 374 384 375 # … … 387 378 APPL_OBJS = $(APPL_ASMOBJS) $(APPL_COBJS) $(APPL_CXXOBJS) 388 379 SYSSVC_OBJS = $(SYSSVC_ASMOBJS) $(SYSSVC_COBJS) $(HIDDEN_OBJS) 389 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) $(MALLOC_OBJS)380 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) 390 381 CFG_OBJS = $(CFG_ASMOBJS) $(CFG_COBJS) 391 382 ALL_OBJS = $(START_OBJS) $(APPL_OBJS) $(SYSSVC_OBJS) $(CFG_OBJS) \ … … 554 545 rm -f libsyssvc.a 555 546 endif 556 rm -f $(MALLOC_OBJS)557 547 558 548 .PHONY: cleankernel … … 678 668 $(CC) -c -o $@ -MD -MP -MF $(DEPDIR)/$*.d $(CFLAGS) $(APPL_CFLAGS) $< 679 669 680 $(MALLOC_OBJS): $(OBJDIR)/%.o:681 $(AR) x $(MALLOC_LIB) $(notdir $@)682 mv $(notdir $@) $@683 684 670 # 685 671 # デフォルトコンパイルルールを上書き -
EcnlProtoTool/trunk/ntshell/Debug/Makefile
r429 r441 199 199 CDEFS := -DTOPPERS_OMIT_TECS $(CDEFS) 200 200 endif 201 CDEFS := $(CDEFS) -DFD_SETSIZE=32 201 CDEFS := $(CDEFS) -DFD_SETSIZE=32 -DDHCP4_CLI_CFG -DUSE_PING -DUSE_RESOLVER -DUSE_NETAPP_SUBR 202 202 INCLUDES := -I. -I$(SRCDIR)/include $(INCLUDES) -I$(SRCDIR) 203 203 LDFLAGS := $(LDFLAGS) -L. … … 213 213 APPL_CDL = main.cdl 214 214 215 APPL_DIRS := $(APPLDIRS) $(SRCDIR)/library 215 APPL_DIRS := $(APPLDIRS) $(SRCDIR)/library $(SRCDIR)/$(TINETDIR)/netapp 216 216 APPL_ASMOBJS := 217 217 ifdef USE_CXX 218 218 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.o219 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 220 220 else 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.o221 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 222 222 endif 223 223 APPL_COBJS := $(APPL_COBJS) log_output.o vasyslog.o t_perror.o strerror.o mbed_stub.o … … 263 263 # ã·ã¹ãã ãµã¼ãã¹ã«é¢ããå®ç¾© 264 264 # 265 ifdef TCINSTALL266 GCC_PATH = $(TCINSTALL)267 else268 GCC_PATH = $(shell which $(CC))/../../269 endif270 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.o272 273 265 SYSSVC_DIRS := $(TECSGENDIR) $(SRCDIR)/tecs_kernel \ 274 266 $(SYSSVC_DIRS) $(SRCDIR)/syssvc … … 398 390 CFG_COBJS := $(addprefix $(OBJDIR)/, $(CFG_COBJS)) 399 391 CFG_DMY := $(addprefix $(OBJDIR)/, $(CFG_DMY)) 400 MALLOC_OBJS := $(addprefix $(OBJDIR)/, $(MALLOC_OBJS))401 392 402 393 # … … 405 396 APPL_OBJS = $(APPL_ASMOBJS) $(APPL_COBJS) $(APPL_CXXOBJS) $(APPL_BINOBJS) 406 397 SYSSVC_OBJS = $(SYSSVC_ASMOBJS) $(SYSSVC_COBJS) $(HIDDEN_OBJS) 407 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) $(MALLOC_OBJS)398 KERNEL_LIB_OBJS = $(KERNEL_ASMOBJS) $(KERNEL_COBJS) $(KERNEL_LCOBJS) 408 399 CFG_OBJS = $(CFG_ASMOBJS) $(CFG_COBJS) 409 400 ALL_OBJS = $(START_OBJS) $(APPL_OBJS) $(SYSSVC_OBJS) $(CFG_OBJS) \ … … 573 564 rm -f libsyssvc.a 574 565 endif 575 rm -f $(MALLOC_OBJS)576 566 577 567 .PHONY: cleankernel … … 701 691 $(OBJCOPY) -B arm -I binary -O elf32-littlearm --rename-section .data=.rodata $< $(OBJDIR)/$*.o 702 692 703 $(MALLOC_OBJS): $(OBJDIR)/%.o:704 $(AR) x $(MALLOC_LIB) $(notdir $@)705 mv $(notdir $@) $@706 707 693 # 708 694 # ããã©ã«ãã³ã³ãã¤ã«ã«ã¼ã«ãä¸æ¸ã -
EcnlProtoTool/trunk/ntshell/fatfs/diskio.h
r434 r441 43 43 void disk_timerproc(void); 44 44 45 #define SZ_RAMDISK 1 792/* Size of RAM disk [kB] */45 #define SZ_RAMDISK 1536 /* Size of RAM disk [kB] */ 46 46 #define SS_RAMDISK 512 /* Sector size of RAM disk [byte] */ 47 47 -
EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c
r435 r441 42 42 #include <kernel.h> 43 43 #include <stdlib.h> 44 //#include <string.h>44 #include <string.h> 45 45 #include "t_stdlib.h" 46 46 #include "syssvc/serial.h" 47 47 #include "syssvc/syslog.h" 48 #include "target_syssvc.h" 48 49 #include "kernel_cfg.h" 49 50 #include "diskio.h" 50 51 #include "sdfs.h" 51 52 #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" 54 56 55 57 #define SD_CARD_LOG_LEVEL LOG_INFO 56 58 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]; 59 enum ffarch_state_t { 60 FFS_IDLE, 61 FFS_RETRY_WAIT, 62 }; 63 64 enum ffarch_state_t ffarch_state; 65 volatile int ffarch_timer; 66 int ffarch_retry_count; 67 uint8_t sdfs_new_status, sdfs_prev_status; 68 69 static void ffarch_initialize(); 70 71 /* 72 * FatFSタスク 73 */ 74 void 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 64 119 FATFS RomDisk; 65 120 … … 75 130 SD Sd; 76 131 77 void sys_init(void);78 132 bool_t romdisk_init(); 79 133 bool_t SD_begin(); 80 134 81 int mruby_arduino_init() 82 { 83 int result = -1; 84 85 sys_init(); 86 135 void ffarch_init(void) 136 { 87 137 /* SD_CD */ 88 138 gpio_init_in(&ins, P7_8); … … 91 141 sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd"); 92 142 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!"); 96 155 } 97 156 else { 98 ntstdio_printf(&ntstdio, "SD card (1:) NG!\n"); 99 } 157 syslog(LOG_NOTICE, "ROM disk (0:) NG!"); 158 } 159 } 160 161 void ffarch_initialize() 162 { 100 163 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);154 164 } 155 165 … … 219 229 return ramdisk_get_status(); 220 230 case 1: 221 return sdfs _status(&sdfs);231 return sdfs._is_initialized; 222 232 } 223 233 return STA_NOINIT; … … 246 256 } 247 257 258 DRESULT sdfs_ioctl(BYTE ctrl, void *buff); 259 248 260 DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) 249 261 { … … 252 264 return RES_PARERR; /* ramdisk_ioctl(cmd, buff); */ 253 265 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); 263 267 } 264 268 return RES_PARERR; 265 269 } 266 270 271 DRESULT 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 267 332 void sdfs_cychdr(intptr_t exinf) 268 333 { 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()) 273 338 s |= STA_PROTECT; 274 else /* Write enabled */339 else 275 340 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 277 346 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 356 int ffarch_get_timer(void) 357 { 358 return ffarch_timer; 359 } 360 361 void 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 371 void 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 } 281 428 } 282 429 … … 314 461 void* ff_memalloc (UINT msize) 315 462 { 316 return sys_malloc(msize);463 return malloc(msize); 317 464 } 318 465 319 466 void ff_memfree (void* mblock) 320 467 { 321 sys_free(mblock);468 free(mblock); 322 469 } 323 470 -
EcnlProtoTool/trunk/ntshell/fatfs/ffarch.cfg
r434 r441 38 38 #include "ffarch.h" 39 39 40 CRE_TSK(FFARCH_TASK, { TA_NULL, 0, ffarch_task, FFARCH_PRIORITY, FFARCH_STACK_SIZE, NULL }); 40 41 CRE_CYC(SDFS_CYC, { TA_NULL, { TNFY_HANDLER, 0, sdfs_cychdr }, 1000000, 0 }); 41 42 42 CRE_SEM(SEM_ MALLOC, { TA_TPRI, 1, 1 });43 CRE_SEM(SEM_FILESYSTEM, { TA_TPRI, 1, 1 }); 43 44 -
EcnlProtoTool/trunk/ntshell/fatfs/ffarch.h
r434 r441 38 38 #define _FFARCH_H_ 39 39 40 int mruby_arduino_init(); 40 #define FFARCH_PRIORITY 5 41 #define FFARCH_STACK_SIZE 512 42 43 void ffarch_init(void); 44 void ffarch_task(intptr_t exinf); 45 int ffarch_get_timer(void); 46 void ffarch_progress(int elapse); 47 void ffarch_timeout(void); 48 41 49 void sdfs_cychdr(intptr_t exinf); 42 50 -
EcnlProtoTool/trunk/ntshell/ntshell/core/ntlibc.h
r331 r441 52 52 int ntlibc_tolower(int c); 53 53 int ntlibc_atoi(const char *nptr); 54 unsigned long ntlibc_strtoul(const char * __restrict nptr, char **__restrict endptr, int base);54 unsigned long ntlibc_strtoul(const char *restrict nptr, char **restrict endptr, int base); 55 55 char *ntlibc_strchr(const char *s, int c); 56 56 char *ntlibc_utoa(unsigned int value, char *s, int radix); -
EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
r435 r441 33 33 #include "shellif.h" 34 34 #include <kernel.h> 35 //#include <string.h>36 35 #include <t_syslog.h> 37 36 #include <t_stdlib.h> 38 37 #include <sil.h> 39 38 #include "syssvc/syslog.h" 39 #include "target_syssvc.h" 40 40 #include "kernel_cfg.h" 41 41 #include "main.h" 42 #include "rza1.h"43 42 #include "ffarch.h" 44 43 #include "ff.h" … … 48 47 #include "util/ntstdio.h" 49 48 50 extern ntstdio_t ntstdio;49 extern ntstdio_t *ntstdio; 51 50 extern int ntshell_exit; 52 51 … … 147 146 } 148 147 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 165 148 void put_rc(const char *func, FRESULT rc) 166 149 { … … 175 158 while (*p++); 176 159 } 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); 178 161 } 179 162 … … 187 170 while (*p++); 188 171 } 189 ntstdio_printf( &ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p);172 ntstdio_printf(ntstdio, "%s() =>%u %s\n", func, (UINT)rc, p); 190 173 } 191 174 … … 210 193 for (; s[i] == '/'; i--) if (!i) return (s == _s) ? "/" : _s; 211 194 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; } 213 196 s[i + 1] = 0; 214 197 return _s; … … 237 220 238 221 ntlibc_strlcat(path, "\n", sizeof(path)); 239 ntstdio_printf( &ntstdio, path);222 ntstdio_printf(ntstdio, path); 240 223 241 224 return 0; … … 260 243 261 244 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 ", 263 246 (fno->fattrib & AM_DIR) ? 'd' : '-', 264 247 (fno->fattrib & AM_RDO) ? 'r' : '-', … … 274 257 275 258 if (fno->fattrib & AM_DIR) { /* It is a directory */ 276 ntstdio_printf( &ntstdio, "%10S", " ");259 ntstdio_printf(ntstdio, "%10s ", " "); 277 260 } 278 261 else { 279 ntstdio_printf( &ntstdio, "%10d", fno->fsize);262 ntstdio_printf(ntstdio, "%10lu ", fno->fsize); 280 263 } 281 264 } 282 265 283 266 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); 285 268 } 286 269 else { 287 ntstdio_printf( &ntstdio, "%s\n", fn);270 ntstdio_printf(ntstdio, "%s\n", fn); 288 271 } 289 272 } … … 304 287 path_backup = ff_memalloc(LFN_BUF_SIZE); 305 288 if (path_backup == NULL) { 306 ntstdio_printf( &ntstdio, "ff_memalloc err.\n");289 ntstdio_printf(ntstdio, "ff_memalloc err.\n"); 307 290 return; 308 291 } … … 312 295 lfn = ff_memalloc(LFN_BUF_SIZE); 313 296 if (lfn == NULL) { 314 ntstdio_printf( &ntstdio, "ff_memalloc err.\n");297 ntstdio_printf(ntstdio, "ff_memalloc err.\n"); 315 298 ff_memfree(path_backup); 316 299 return; … … 340 323 } 341 324 342 if ((res = f_chdrive(path_p)) != RES_OK) {325 if ((res = f_chdrive(path_p)) != FR_OK) { 343 326 put_rc("f_chdrive", res); 344 327 } 345 328 346 if ((res = f_chdir(path_p)) != RES_OK) {329 if ((res = f_chdir(path_p)) != FR_OK) { 347 330 put_rc("f_chdir", res); 348 331 } 349 332 350 ntstdio_printf( &ntstdio, "\n%s/%s:\n", path_p, fn);333 ntstdio_printf(ntstdio, "\n%s/%s:\n", path_p, fn); 351 334 352 335 print_ls(fn, NULL, list_option); 353 336 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) { 357 340 put_rc("f_chdrive", res); 358 341 } 359 342 360 if ((res = f_chdir(path_backup)) != RES_OK) {343 if ((res = f_chdir(path_backup)) != FR_OK) { 361 344 put_rc("f_chdir", res); 362 345 } … … 449 432 lfn = ff_memalloc(LFN_BUF_SIZE); 450 433 if (lfn == NULL) { 451 ntstdio_printf( &ntstdio, "alloc err.\n");434 ntstdio_printf(ntstdio, "alloc err.\n"); 452 435 goto cp_end; 453 436 } … … 459 442 local_buff = ff_memalloc(64); 460 443 if (local_buff == NULL) { 461 ntstdio_printf( &ntstdio, "alloc err.\n");444 ntstdio_printf(ntstdio, "alloc err.\n"); 462 445 goto cp_end; 463 446 } … … 469 452 if (res != FR_OK) { 470 453 if (res == FR_NO_FILE) 471 ntstdio_printf( &ntstdio, "src no file.\n", res);454 ntstdio_printf(ntstdio, "src no file.\n"); 472 455 else 473 ntstdio_printf( &ntstdio, "src stat err(%d).\n", res);456 ntstdio_printf(ntstdio, "src stat err(%d).\n", res); 474 457 goto cp_end; 475 458 } … … 480 463 res = f_open(&src_fp, src_str_p, (FA_OPEN_EXISTING | FA_READ)); 481 464 if (res != FR_OK) { 482 ntstdio_printf( &ntstdio, "src open err(%d).\n", res);465 ntstdio_printf(ntstdio, "src open err(%d).\n", res); 483 466 goto cp_end; 484 467 } … … 495 478 dst_mod_str_p = ff_memalloc(LFN_BUF_SIZE); 496 479 if (dst_mod_str_p == NULL) { 497 ntstdio_printf( &ntstdio, "alloc err.\n");480 ntstdio_printf(ntstdio, "alloc err.\n"); 498 481 goto cp_end; 499 482 } … … 502 485 } 503 486 else { 504 ntstdio_printf( &ntstdio, "dst file exists.\n");487 ntstdio_printf(ntstdio, "dst file exists.\n"); 505 488 goto cp_end_1; 506 489 } 507 490 } 508 491 else { 509 ntstdio_printf( &ntstdio, "src stat err(%d).\n", res);492 ntstdio_printf(ntstdio, "src stat err(%d).\n", res); 510 493 goto cp_end_1; 511 494 } … … 513 496 res = f_open(&dst_fp, dst_str_p, (FA_CREATE_NEW | FA_WRITE)); 514 497 if (res != FR_OK) { 515 ntstdio_printf( &ntstdio, "dst open err(%d).\n", res);498 ntstdio_printf(ntstdio, "dst open err(%d).\n", res); 516 499 goto cp_end_1; 517 500 } … … 524 507 res = f_read(&src_fp, local_buff, sizeof(local_buff), &read_size); 525 508 if (res != FR_OK) { 526 ntstdio_printf( &ntstdio, "src read err(%d).\n", res);509 ntstdio_printf(ntstdio, "src read err(%d).\n", res); 527 510 goto cp_end_2; 528 511 } … … 531 514 res = f_write(&dst_fp, local_buff, read_size, &write_size); 532 515 if (res != FR_OK) { 533 ntstdio_printf( &ntstdio, "dst write err(%d).\n", res);516 ntstdio_printf(ntstdio, "dst write err(%d).\n", res); 534 517 goto cp_end_2; 535 518 } 536 519 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"); 538 521 goto cp_end_2; 539 522 } … … 594 577 } 595 578 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 */598 579 #define HEXDUMP_EXTRA_FOR_UTF8 3 /* need extra buffer size */ 599 580 #define CCOLOR_RESET 0 … … 626 607 switch (op) { 627 608 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"); 634 615 break; 635 616 case 'd': /* print one byte character [in character area] (default) */ … … 662 643 /* position adjusting */ 663 644 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); 665 646 return 0; 666 647 } … … 689 670 if (br > 16) br = 16; 690 671 691 char *apos = ascii;672 unsigned char *apos = ascii; 692 673 int arst = sizeof(ascii); 693 674 for (int j = 0; j < br; j++) { … … 784 765 rst -= len; 785 766 786 ntstdio_puts( &ntstdio, line);767 ntstdio_puts(ntstdio, line); 787 768 } 788 769 … … 799 780 ret = shell_clock_gettime(CLOCK_REALTIME, &tp); 800 781 if (ret != 0) { 801 ntstdio_printf( &ntstdio, "clock_gettime error %d", ret);782 ntstdio_printf(ntstdio, "clock_gettime error %d", ret); 802 783 return 0; 803 784 } … … 805 786 memset(buf, 0, sizeof(buf)); 806 787 if (ctime_r(&tp.tv_sec, buf) == NULL) { 807 ntstdio_printf( &ntstdio, "ctime_r error");788 ntstdio_printf(ntstdio, "ctime_r error"); 808 789 return 0; 809 790 } … … 813 794 buf[ret - 1] = '\0'; 814 795 815 ntstdio_printf( &ntstdio, "%s .%09ld\n", buf, tp.tv_nsec);796 ntstdio_printf(ntstdio, "%s .%09ld\n", buf, tp.tv_nsec); 816 797 return 0; 817 798 } … … 820 801 { 821 802 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"); 824 805 return 0; 825 806 } 826 807 if (ntlibc_strcmp(argv[1], "sys") == 0) { 827 ntstdio_printf( &ntstdio, "GR-PEACHMonitor\n");808 ntstdio_printf(ntstdio, TARGET_NAME" Monitor\n"); 828 809 return 0; 829 810 } … … 831 812 int mj, mn, bd; 832 813 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"); 837 818 return -1; 838 819 } -
EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.h
r433 r441 56 56 int usrcmd_exit(int argc, char **argv); 57 57 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 63 58 #endif 64 59 -
EcnlProtoTool/trunk/ntshell/src/fdtable.c
r434 r441 43 43 #include "syssvc/serial.h" 44 44 #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" 58 46 #include "fdtable.h" 59 47 #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 53 static const char THIS_FILE[] = __FILE__; 54 #endif 55 56 extern IO_TYPE IO_TYPE_STDIN; 57 extern IO_TYPE IO_TYPE_STDOUT; 58 extern IO_TYPE IO_TYPE_STDERR; 59 60 static 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 }, 75 64 }; 76 65 #define fd_table_count (sizeof(fd_table) / sizeof(fd_table[0])) 77 66 78 static int new_fd(int type, int id) 79 { 67 struct 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 80 77 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) 83 80 continue; 84 81 82 memset(fp, 0, sizeof(struct SHELL_FILE)); 85 83 fp->fd = fd; 86 84 fp->type = type; 87 85 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 109 struct 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 96 119 for (int fd = 3; fd < fd_table_count; fd++) { 97 struct _IO_FILE *fp = &fd_table[fd];120 fp = &fd_table[fd]; 98 121 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 133 int delete_fd_by_id(IO_TYPE *type, int id) 134 { 135 struct SHELL_FILE *fp = id_to_fd(type, id); 108 136 if (fp == NULL) 109 137 return -EBADF; 110 138 111 memset(fp, 0, sizeof(struct _IO_FILE)); 139 return delete_fp(fp); 140 } 141 142 int 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 } 112 162 113 163 return 0; 114 164 } 115 165 116 struct _IO_FILE *fd_to_fp(int fd)166 struct SHELL_FILE *fd_to_fp(int fd) 117 167 { 118 168 if ((fd < 0) || (fd >= fd_table_count)) … … 121 171 } 122 172 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); 173 void 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 } 251 182 } 252 183 … … 258 189 }; 259 190 260 ER shell_get_evts(struct fd_events *evts, TMO tmout);191 ER shell_get_evts(struct fd_events *evts, TMO *tmout); 261 192 262 193 #define TMO_MAX INT_MAX 263 194 264 int shell_select(int n, fd_set * __restrict rfds, fd_set *__restrict wfds, fd_set *__restrict efds, struct timeval *__restrict tv)195 int shell_select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv) 265 196 { 266 197 ER ret; … … 275 206 } 276 207 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)); 280 220 evts.count = 0; 281 221 282 ret = shell_get_evts(&evts, tmout);222 ret = shell_get_evts(&evts, &tmout); 283 223 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)); 287 230 return evts.count; 288 231 } 289 232 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)); 293 239 return 0; 294 240 } … … 310 256 tmout = TMO_MAX; 311 257 258 retry: 312 259 memset(&evts, 0, sizeof(evts)); 313 260 … … 327 274 } 328 275 329 ret = shell_get_evts(&evts, tmout);276 ret = shell_get_evts(&evts, &tmout); 330 277 if (ret == E_OK) { 331 278 int result = 0; … … 345 292 result++; 346 293 } 294 if (result == 0) 295 goto retry; 347 296 return result; 348 297 } … … 354 303 } 355 304 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) 305 ER 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; 510 414 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; 543 422 544 423 return E_OK; … … 547 426 void clean_fd() 548 427 { 549 struct _IO_FILE *fp = NULL;428 struct SHELL_FILE *fp = NULL; 550 429 for (int fd = 3; fd < fd_table_count; fd++) { 551 430 fp = &fd_table[fd]; … … 553 432 continue; 554 433 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 440 int 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 453 ssize_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 462 int 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 477 ssize_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 486 int 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 501 int 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; 559 513 } 560 514 561 515 int shell_ioctl(int fd, int request, void *arg) 562 516 { 563 struct _IO_FILE *fp = fd_to_fp(fd);517 struct SHELL_FILE *fp = fd_to_fp(fd); 564 518 if (fp == NULL) 565 519 return -EBADF; 566 520 567 return fp->ioctl(fp, request, arg); 568 } 521 return fp->type->ioctl(fp, request, arg); 522 } 523 524 #ifdef NTSHELL_NO_SOCKET 525 526 int shell_socket(int family, int type, int protocol) 527 { 528 return -ENOMEM; 529 } 530 531 int shell_bind(int fd, const struct sockaddr *addr, socklen_t len) 532 { 533 return -ENOMEM; 534 } 535 536 int shell_listen(int fd, int backlog) 537 { 538 return -ENOMEM; 539 } 540 541 int shell_connect(int fd, const struct sockaddr *addr, socklen_t len) 542 { 543 return -ENOMEM; 544 } 545 546 int shell_accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) 547 { 548 return -ENOMEM; 549 } 550 551 ssize_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 556 ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags) 557 { 558 return -ENOMEM; 559 } 560 561 ssize_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 566 ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags) 567 { 568 return -ENOMEM; 569 } 570 571 int shell_shutdown(int fd, int how) 572 { 573 return -ENOMEM; 574 } 575 576 int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *restrict optlen) 577 { 578 return -ENOMEM; 579 } 580 581 int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) 582 { 583 return -ENOMEM; 584 } 585 586 int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) 587 { 588 return -ENOMEM; 589 } 590 591 int 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 35 35 * @(#) $Id$ 36 36 */ 37 #ifndef SOCKET_STUB_H38 #define SOCKET_STUB_H37 #ifndef _FDTABLE_H_ 38 #define _FDTABLE_H_ 39 39 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 }; 40 typedef const struct io_type_s IO_TYPE; 50 41 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 { 42 struct SHELL_FILE { 104 43 int fd; 105 inttype;44 IO_TYPE *type; 106 45 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 *);112 46 int readevt_r; 113 47 int readevt_w; 114 48 int writeevt_r; 115 49 int writeevt_w; 50 int writable; 116 51 int errorevt_r; 117 52 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 56 struct 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 *); 126 65 }; 127 66 … … 136 75 #endif 137 76 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); 77 struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable); 78 int delete_fd_by_id(IO_TYPE *type, int id); 79 struct SHELL_FILE *fd_to_fp(int fd); 80 struct SHELL_FILE *id_to_fd(IO_TYPE *type, int id); 144 81 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); 82 int delete_fp(struct SHELL_FILE *fp); 198 83 void clean_fd(); 199 84 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 1 1 /* 2 2 * TOPPERS PROJECT Home Network Working Group Software 3 * 3 * 4 4 * Copyright (C) 2017-2019 Cores Co., Ltd. Japan 5 * 5 * 6 6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ 7 7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改 … … 26 26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを 27 27 * 免責すること. 28 * 28 * 29 29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お 30 30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的 … … 32 32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ 33 33 * の責任を負わない. 34 * 34 * 35 35 * @(#) $Id$ 36 36 */ … … 42 42 #include <t_stdlib.h> 43 43 #include <sil.h> 44 //#include <string.h>45 #include <setjmp.h>44 #include <string.h> 45 #include "syssvc/serial.h" 46 46 #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>59 47 #include "fdtable.h" 60 //#include <sys/stat.h>61 48 #include "util/ntstdio.h" 62 49 #include "usrcmd.h" 63 50 #include "core/ntlibc.h" 51 #include "kernel_cfg.h" 52 #include "target_syssvc.h" 53 54 struct SHELL_DIR { 55 FATFS_DIR dir; 56 struct dirent dirent; 57 }; 64 58 65 59 int fresult2errno(FRESULT res) … … 90 84 } 91 85 92 int shell_open(const char * path, int flags, void *arg) 93 { 94 FRESULT res; 95 struct _IO_FILE *fp; 86 static int file_close(struct SHELL_FILE *fp); 87 static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 88 static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 89 static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org); 90 static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg); 91 static bool_t file_readable(struct SHELL_FILE *fp); 92 static bool_t file_writable(struct SHELL_FILE *fp); 93 static void file_delete(struct SHELL_FILE *fp); 94 95 static int dir_close(struct SHELL_FILE *fp); 96 static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 97 static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 98 static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org); 99 static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg); 100 static bool_t dir_readable(struct SHELL_FILE *fp); 101 static bool_t dir_writable(struct SHELL_FILE *fp); 102 static void dir_delete(struct SHELL_FILE *fp); 103 104 IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_writable, file_delete }; 105 IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_writable, dir_delete }; 106 107 int shell_open(const char *path, int flags, void *arg) 108 { 109 FRESULT res; 110 struct SHELL_FILE *fp; 96 111 97 112 if (flags & O_DIRECTORY) { 98 fp = new_ dir_fd(0);113 fp = new_fp(&IO_TYPE_DIR, 0, 0); 99 114 if (fp == NULL) 100 115 return -ENOMEM; 101 116 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; 104 121 if ((res = f_opendir(dir, path)) != FR_OK) { 122 delete_fp(fp); 105 123 return fresult2errno(res); 106 124 } … … 108 126 } 109 127 110 fp = new_f ile_fd(0);128 fp = new_fp(&IO_TYPE_FILE, 0, 1); 111 129 if (fp == NULL) 112 130 return -ENOMEM; 131 132 fp->exinf = malloc(sizeof(FIL)); 133 memset(fp->exinf, 0, sizeof(FIL)); 113 134 114 135 BYTE fmd = 0; … … 146 167 } 147 168 148 if ((res = f_open( &fp->file, path, fmd)) == FR_OK) {169 if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) { 149 170 fp->handle = fp->fd; 150 171 return fp->fd; 151 172 } 152 173 174 delete_fp(fp); 153 175 return fresult2errno(res); 154 176 } 155 177 156 int file_close(struct _IO_FILE *fp)157 { 158 FRESULT res; 159 160 if ((res = f_close( &fp->file)) == FR_OK) {178 int file_close(struct SHELL_FILE *fp) 179 { 180 FRESULT res; 181 182 if ((res = f_close((FIL *)fp->exinf)) == FR_OK) { 161 183 return 0; 162 184 } … … 165 187 } 166 188 167 size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)189 size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 168 190 { 169 191 unsigned int ret = 0; 170 192 FRESULT res; 171 193 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) 173 195 return -EIO; 174 196 … … 176 198 } 177 199 178 size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)200 size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 179 201 { 180 202 unsigned int ret = 0; 181 203 FRESULT res; 182 204 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) 184 206 return -EIO; 185 207 … … 187 209 } 188 210 189 off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)211 off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 190 212 { 191 213 switch (dir) { … … 204 226 205 227 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) 207 229 return -EIO; 208 230 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 234 int file_ioctl(struct SHELL_FILE *fp, int req, void *arg) 213 235 { 214 236 DRESULT res; 215 237 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)) 217 239 return -EINVAL; 218 240 … … 220 242 } 221 243 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; 244 bool_t file_readable(struct SHELL_FILE *fp) 245 { 246 return fp->readevt_w != fp->readevt_r; 247 } 248 249 bool_t file_writable(struct SHELL_FILE *fp) 250 { 251 return fp->writable && (fp->writeevt_w == fp->writeevt_r); 252 } 253 254 void file_delete(struct SHELL_FILE *fp) 255 { 256 free((FIL *)fp->exinf); 257 fp->exinf = NULL; 291 258 } 292 259 293 260 int shell_fstat(int fd, struct stat * st) 294 261 { 295 struct _IO_FILE *fp = fd_to_fp(fd);262 struct SHELL_FILE *fp = fd_to_fp(fd); 296 263 if (fp == NULL) 297 264 return -EBADF; … … 305 272 int shell_fsync(int fd) 306 273 { 307 struct _IO_FILE *fp = fd_to_fp(fd);274 struct SHELL_FILE *fp = fd_to_fp(fd); 308 275 if (fp == NULL) 309 276 return -EBADF; … … 313 280 int shell_ftruncate(int fd, off_t length) 314 281 { 315 struct _IO_FILE *fp = fd_to_fp(fd);282 struct SHELL_FILE *fp = fd_to_fp(fd); 316 283 if (fp == NULL) 317 284 return -EBADF; 318 285 319 286 FRESULT res; 320 if ((res = f_truncate( &fp->file)) != FR_OK)287 if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK) 321 288 return fresult2errno(res); 322 289 … … 329 296 } 330 297 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) 298 int shell_stat(const char *restrict path, struct stat *restrict st) 404 299 { 405 300 FILINFO fi; … … 434 329 st->st_size = fi.fsize; 435 330 st->st_mtim.tv_nsec = 0; 436 st->st_mtim.tv_sec = fi.fdate +fi.ftime;437 st->st_mode 331 st->st_mtim.tv_sec = (time_t)fi.fdate + (time_t)fi.ftime; 332 st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH); 438 333 st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH); 439 334 st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG; … … 442 337 } 443 338 444 int shell_lstat(const char * __restrict path, struct stat *__restrict st)339 int shell_lstat(const char *restrict path, struct stat *restrict st) 445 340 { 446 341 return shell_stat(path, st); … … 494 389 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 495 390 496 if (mode & S_IREAD) {497 if ((mode & S_IWRITE) == 0) {391 if (mode & S_IREAD) { 392 if ((mode & S_IWRITE) == 0) { 498 393 attr |= AM_RDO; 499 394 } … … 503 398 } 504 399 505 if ((res = f_chmod(path, attr, mask)) != FR_OK) {400 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 506 401 return fresult2errno(res); 507 402 } … … 516 411 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 517 412 518 if (mode & S_IREAD) {519 if ((mode & S_IWRITE) == 0) {413 if (mode & S_IREAD) { 414 if ((mode & S_IWRITE) == 0) { 520 415 attr |= AM_RDO; 521 416 } … … 525 420 } 526 421 527 if ((res = f_chmod(path, attr, mask)) != FR_OK) {422 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 528 423 return fresult2errno(res); 529 424 } … … 535 430 { 536 431 FRESULT ret; 537 if ((ret = f_getcwd(buf, size)) != FR_OK) {432 if ((ret = f_getcwd(buf, size)) != FR_OK) { 538 433 return NULL; 539 434 } … … 558 453 } 559 454 560 int dir_close(struct _IO_FILE *fp)561 { 562 FRESULT res; 563 if ((res = f_closedir(& fp->dir)) != FR_OK) {455 int dir_close(struct SHELL_FILE *fp) 456 { 457 FRESULT res; 458 if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 564 459 return fresult2errno(res); 565 460 } … … 573 468 return -EINVAL; 574 469 575 struct _IO_FILE *fp = fd_to_fp(fd);470 struct SHELL_FILE *fp = fd_to_fp(fd); 576 471 if (fp == NULL) 577 472 return -EBADF; … … 584 479 #endif 585 480 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') { 587 482 return fresult2errno(res); 588 483 } … … 598 493 } 599 494 600 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)495 size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 601 496 { 602 497 return -EPERM; 603 498 } 604 499 605 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)500 size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 606 501 { 607 502 return -EPERM; 608 503 } 609 504 610 off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)505 off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 611 506 { 612 507 FRESULT res; … … 616 511 617 512 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) { 619 514 return fresult2errno(res); 620 515 } … … 627 522 fno.lfsize = sizeof lfn; 628 523 #endif 629 if ((res = f_rewinddir(& fp->dir)) != FR_OK) {524 if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 630 525 return fresult2errno(res); 631 526 } 632 527 633 528 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') { 635 530 return fresult2errno(res); 636 531 } … … 641 536 } 642 537 643 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)538 int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg) 644 539 { 645 540 return -EINVAL; 541 } 542 543 bool_t dir_readable(struct SHELL_FILE *fp) 544 { 545 return fp->readevt_w != fp->readevt_r; 546 } 547 548 bool_t dir_writable(struct SHELL_FILE *fp) 549 { 550 return false; 551 } 552 553 void dir_delete(struct SHELL_FILE *fp) 554 { 555 free((struct SHELL_DIR *)fp->exinf); 556 fp->exinf = NULL; 646 557 } 647 558 … … 662 573 return 0; 663 574 } 664 665 //#include "../musl-1.1.18/include/bits/limits.h"666 #define PAGE_SIZE 4096667 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 48 48 #include "syssvc/serial.h" 49 49 #include "syssvc/syslog.h" 50 #include "target_syssvc.h" 50 51 #include "kernel_cfg.h" 51 52 #include "main.h" 52 #include "rza1.h" 53 #include "fdtable.h" 54 #ifndef NTSHELL_NO_SOCKET 53 55 #include <tinet_config.h> 54 56 #include <netinet/in.h> … … 70 72 #include "usrcmd.h" 71 73 #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 74 78 75 79 ID ws_api_mailboxid = MAIN_DATAQUEUE; 76 80 ID ws_mempoolid = MPF_NET_BUF_256; 81 extern ntstdio_t *ntstdio; 77 82 78 83 char 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 }95 84 96 85 const uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC}; … … 101 90 "3.5.0", 102 91 TARGET_NAME, 103 92 "toppers.jp" 104 93 }; 105 94 … … 113 102 main_state_start, 114 103 main_state_idle, 104 #ifndef NTSHELL_NO_SOCKET 105 main_state_start_dhcp, 106 #endif 107 main_state_servey_cmd, 115 108 }; 116 109 117 110 struct main_obj_t { 118 111 ntshell_t ntshell; 119 TMOtimer;112 int timer; 120 113 enum main_state_t state; 121 114 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; 125 127 }; 126 128 struct main_obj_t main_obj; 127 129 128 static void main_change_netif_link(T_IFNET *ether); 130 #ifndef NTSHELL_NO_SOCKET 131 static void netif_link_callback(T_IFNET *ether); 132 static void main_change_netif_link(uint8_t link_up, uint8_t up); 133 #endif 129 134 static 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); 135 static void main_finalize(struct main_obj_t *obj); 136 static int main_get_timer(struct main_obj_t *obj); 137 static void main_progress(struct main_obj_t *obj, int elapse); 138 static void main_timeout(struct main_obj_t *obj, bool_t wakeup); 139 static int execute_command(struct main_obj_t *obj, int wait); 134 140 static int usrcmd_ntopt_callback(long *args, void *extobj); 135 141 #ifndef NTSHELL_NO_SOCKET 142 static void ntp_cli_state_changed(ntp_cli_state_t state); 143 #endif 144 145 void stdio_open(ID portid); 146 void stdio_input(unsigned char c); 147 148 bool_t dhcp_enable; 149 int ntshell_exit; 136 150 int shellcmd_exit_code; 137 151 volatile int shellcmd_state; … … 139 153 jmp_buf shellcmd_exit; 140 154 155 void 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 141 161 int uart_read(char *buf, int cnt, void *extobj) 142 162 { 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; 144 212 } 145 213 … … 162 230 } 163 231 164 return execute_command(tskid == MAIN_TASK); 165 } 232 return execute_command(&main_obj, tskid == MAIN_TASK); 233 } 234 235 gpio_t led_blue, led_green, led_red, sw; 166 236 167 237 /* 168 * メインタスク238 * メインタスク 169 239 */ 170 240 void main_task(intptr_t exinf) 171 241 { 172 242 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 173 265 174 266 main_initialize(obj); … … 177 269 ntshell_set_prompt(&obj->ntshell, "NTShell>"); 178 270 ntshell_execute(&obj->ntshell); 271 272 main_finalize(obj); 179 273 } 180 274 … … 184 278 static void main_initialize(struct main_obj_t *obj) 185 279 { 186 FILINFO fno;187 #if FF_USE_LFN188 char lfn[FF_MAX_LFN + 1];189 fno.lfname = lfn;190 fno.lfsize = FF_MAX_LFN + 1;191 #endif192 280 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; 197 290 obj->state = main_state_start; 198 291 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); 222 298 gpio_write(&led_green, 0); 223 299 224 300 act_tsk(HTTPD1_TASK); 225 301 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 }236 302 237 303 ret = get_tim(&obj->now); … … 243 309 } 244 310 311 static 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 245 319 /* 246 320 * タイマー取得 247 321 */ 248 static TMO main_get_timer(struct main_obj_t *obj) 249 { 250 TMO timer = obj->timer; 322 static 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 } 251 332 252 333 return timer; … … 256 337 * 時間経過 257 338 */ 258 static void main_progress(struct main_obj_t *obj, TMOelapse)339 static void main_progress(struct main_obj_t *obj, int elapse) 259 340 { 260 341 if (obj->timer != TMO_FEVR) { … … 264 345 } 265 346 } 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 } 266 352 } 267 353 … … 269 355 * タイムアウト処理 270 356 */ 271 static void main_timeout(struct main_obj_t *obj) 272 { 273 //if (obj->timer == 0) { 274 //} 275 } 276 357 static 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 277 435 /* MACアドレスの設定時に呼ばれる */ 278 436 void mbed_mac_address(char *mac) … … 281 439 } 282 440 283 static void main_change_netif_link(T_IFNET *ether)441 void main_change_netif_link(uint8_t link_up, uint8_t up) 284 442 { 285 443 FLGPTN flgptn; … … 295 453 set_flg(FLG_SELECT_WAIT, flgptn); 296 454 } 455 456 void 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 491 void 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 297 507 298 508 /* … … 302 512 { 303 513 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 305 522 shellcmd_state = 2; 306 523 } … … 315 532 {"hexdump", "Hex dump", usrcmd_hexdump}, 316 533 {"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 317 540 {"info", "This is a description text string for info command.", usrcmd_info}, 318 541 {"exit", "Exit Natural Tiny Shell", usrcmd_exit}, … … 330 553 331 554 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]); 333 557 } 334 558 else for (int i = 0; i < cmd_table_info.count; i++) { 335 559 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; 337 563 } 338 564 p++; 339 565 } 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; 345 576 } 346 577 … … 349 580 const cmd_table_t *p = cmd_table_info.table; 350 581 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"); 355 586 p++; 356 587 } … … 372 603 void shell_abort() 373 604 { 374 asm("bkpt #0");375 376 605 shellcmd_exit_code = -1; 377 606 longjmp(shellcmd_exit, 1); … … 390 619 } 391 620 392 int execute_command(int wait) 393 { 621 int execute_command(struct main_obj_t *obj, int wait) 622 { 623 T_RTSK rtsk; 394 624 ER ret; 395 625 … … 399 629 } 400 630 401 tslp_tsk(100000); 631 if (ret == E_OK) 632 tslp_tsk(100000); 402 633 403 634 clean_fd(); … … 413 644 414 645 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; 416 671 } while(shellcmd_state == 1); 417 672 673 obj->state = main_state_idle; 674 obj->timer = TMO_FEVR; 675 676 clean_fd(); 677 418 678 return shellcmd_exit_code; 419 679 } … … 421 681 int shell_clock_getres(clockid_t clk_id, struct timespec *res) 422 682 { 423 if ( clk_id != CLOCK_REALTIME)683 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC)) 424 684 return -EINVAL; 425 685 … … 434 694 SYSTIM now = 0; 435 695 436 if ( clk_id != CLOCK_REALTIME)696 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC)) 437 697 return -EINVAL; 438 698 … … 446 706 int shell_clock_settime(clockid_t clk_id, const struct timespec *tp) 447 707 { 448 if ( clk_id != CLOCK_REALTIME)708 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC)) 449 709 return -EINVAL; 450 710 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 } 452 720 453 721 return 0; … … 482 750 } 483 751 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 753 struct k_sigaction { 754 void(*handler)(int); 755 unsigned long flags; 756 void(*restorer)(void); 757 unsigned mask[2]; 758 }; 759 760 struct k_sigaction sigtable[7]; 761 762 int 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; 489 766 490 767 switch(sig){ … … 507 784 sat = &sigtable[5]; 508 785 break; 786 case SIGPIPE: 787 sat = &sigtable[6]; 788 break; 509 789 default: 510 790 return -EINVAL; … … 512 792 513 793 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); 517 797 518 798 return 0; … … 524 804 } 525 805 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: 806 int 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 818 int 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 828 int shell_kill(int pid, int sig) 829 { 830 DebugBreak(); 831 return -1; 832 } 833 834 int 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 844 int 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 } 593 864 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 877 ssize_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 896 extern uint32_t __CmdBase; 897 extern uint32_t __CmdLimit; 898 899 void *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 910 void *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 921 int 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 1 1 /* 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 * 4 40 * $Id$ 5 41 */ … … 19 55 import("syssvc/tBanner.cdl"); 20 56 57 import_C("target_syssvc.h"); 58 21 59 /* 22 * タ ーゲット依存部の取り込み60 * タスクのスタックサイズのデフォルト 23 61 */ 24 import("target.cdl"); 62 const size_t DefaultTaskStackSize = 1024; /* スタックサイズ(1KB)*/ 63 64 /* 65 * システムログタスクのスタックサイズの定義 66 */ 67 const size_t LogTaskStackSize = DefaultTaskStackSize; 68 69 /* 70 * カーネル起動メッセージに関する定義 71 */ 72 const char *const BannerTargetName = C_EXP("TARGET_NAME"); /* ターゲット名 */ 73 const char *const BannerCopyrightNotice = ""; /* 著作権表示 */ 74 75 /* 76 * ターゲット依存のセルタイプの定義 77 */ 78 import("syssvc/tPutLogSIOPort.cdl"); 79 import("tSIOPortNTShell.cdl"); 80 81 /* 82 * シリアルインタフェースドライバのターゲット依存部の組み上げ記述 83 */ 84 cell tSIOPortNTShell SIOPortTarget1 { 85 }; 86 87 /* 88 * 低レベル出力の組み上げ記述 89 */ 90 cell tPutLogSIOPort PutLogTarget { 91 /* SIOドライバとの結合 */ 92 cSIOPort = SIOPortTarget1.eSIOPort; 93 }; 25 94 26 95 /* -
EcnlProtoTool/trunk/ntshell/src/main.cfg
r434 r441 48 48 #include <net/if6_var.h> 49 49 50 ATT_INI({ TA_NULL, 0, sys_init }); 51 50 52 INCLUDE("tecsgen.cfg"); 51 53 INCLUDE("tinet/tinet_asp.cfg"); 52 54 INCLUDE("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"); 59 INCLUDE("netapp/tinet_dhcp4_cli.cfg"); 60 61 /* DNSクライアント */ 62 #include "netapp/resolver.h" 63 INCLUDE("netapp/resolver_asp.cfg"); 64 INCLUDE("netapp/tinet_resolver.cfg"); 65 66 /* NTPクライアント */ 67 #include "ntp_cli.h" 68 //INCLUDE("ntp_cli.cfg"); 69 INCLUDE("tinet_ntp_cli.cfg"); 70 71 #include "net_misc.h" 72 INCLUDE("net_misc.cfg"); 73 74 /* FatFs */ 55 75 INCLUDE("ffarch.cfg"); 56 76 INCLUDE("httpd.cfg"); … … 59 79 CRE_TSK(MAIN_TASK, { TA_ACT, 0, main_task, MAIN_PRIORITY, MAIN_STACK_SIZE, NULL }); 60 80 CRE_DTQ(MAIN_DATAQUEUE, { TA_TFIFO, NUM_MAIN_DATAQUEUE, NULL }); 61 CRE_SEM(SEM_FILESYSTEM, { TA_TPRI, 1, 1 });62 81 63 CRE_TSK(SHELLCMD_TASK, { TA_ NULL, 0, shellcmd_task, SHELLCMD_PRIORITY, SHELLCMD_STACK_SIZE, (void *)0x20100000 });82 CRE_TSK(SHELLCMD_TASK, { TA_FPU, 0, shellcmd_task, SHELLCMD_PRIORITY, SHELLCMD_STACK_SIZE, (void *)0x20100000 }); 64 83 CRE_FLG(FLG_SELECT_WAIT, { TA_WMUL, 0x00 }); 84 CRE_SEM(SEM_FILEDESC, { TA_TPRI, 1, 1 }); -
EcnlProtoTool/trunk/ntshell/src/main.h
r435 r441 56 56 57 57 #define MAIN_PRIORITY 5 /* メインタスクの優先度 */ 58 #define SHELLCMD_PRIORITY 4/* shellcmdタスクの優先度 */58 #define SHELLCMD_PRIORITY 8 /* shellcmdタスクの優先度 */ 59 59 60 #define MAIN_STACK_SIZE 2048/* メインタスクのスタック領域のサイズ */60 #define MAIN_STACK_SIZE 2048 /* メインタスクのスタック領域のサイズ */ 61 61 #define SHELLCMD_STACK_SIZE 0x00800000 /* shellcmdタスクのスタック領域のサイズ */ 62 62 63 63 #define NUM_MAIN_DATAQUEUE 1 /* メインタスクで待ち受けているデータキューのサイズ */ 64 #define NUM_SHELLCMD_DATAQUEUE 1 /* shellcmdタスクで待ち受けているデータキューのサイズ */ 64 65 65 66 66 /* … … 69 69 #ifndef TOPPERS_MACRO_ONLY 70 70 71 typedef 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 71 81 typedef struct 72 82 { … … 74 84 int count; 75 85 } cmd_table_info_t; 86 87 extern cmd_table_info_t cmd_table_info; 88 89 /* ntshellの初期化 */ 90 void 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 /* メインタスク初期化 */ 100 void main_task_init(task_base_t **tasks, int task_count); 76 101 77 102 /* メインタスク */ -
EcnlProtoTool/trunk/ntshell/src/mbedcall.c
r439 r441 375 375 break; 376 376 default: 377 asm("bkpt #0");377 DebugBreak(); 378 378 break; 379 379 } -
EcnlProtoTool/trunk/ntshell/src/shellif.h
r439 r441 50 50 * @(#) $Id$ 51 51 */ 52 #ifndef SHELLIF_H53 #define SHELLIF_H52 #ifndef _SHELLIF_H_ 53 #define _SHELLIF_H_ 54 54 55 55 #ifdef _MSC_VER 56 # include <time.h>56 #undef NULL 57 57 #endif 58 58 … … 92 92 #include "../musl-1.1.18/include/bits/stat.h" 93 93 #include "../musl-1.1.18/include/sys/select.h" 94 #ifndef _MSC_VER95 94 #include "../musl-1.1.18/include/time.h" 96 #endif97 95 #include "../musl-1.1.18/include/signal.h" 98 96 #include "../musl-1.1.18/include/stdio.h" … … 107 105 #include <stdarg.h> 108 106 107 #define DebugBreak() asm("bkpt #0") 108 109 #ifdef __cplusplus 110 extern "C" { 111 #endif 112 109 113 // time.h 110 114 #define CLOCK_REALTIME 0 115 int shell_nanosleep(const struct timespec *req, struct timespec *rem); 111 116 112 117 // dirent.h … … 154 159 // signal.h 155 160 int 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); 161 struct k_sigaction; 162 int shell_sigaction(int sig, const struct k_sigaction *restrict sa, 163 struct k_sigaction *restrict old, size_t size); 157 164 // socket.h 158 165 int shell_socket(int, int, int); 159 166 int shell_connect(int, const struct sockaddr *, socklen_t); 160 ssize_t shell_send(int, const void *, size_t, int);161 167 ssize_t shell_sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t); 162 168 ssize_t shell_sendmsg(int, const struct msghdr *, int); 163 169 int shell_bind(int, const struct sockaddr *, socklen_t); 164 170 int 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); 171 int shell_accept(int, struct sockaddr *restrict, socklen_t *restrict); 172 ssize_t shell_recvfrom(int, void *restrict, size_t, int, struct sockaddr *restrict, socklen_t *restrict); 168 173 ssize_t shell_recvmsg(int, struct msghdr *, int); 169 int shell_getsockopt(int, int, int, void * __restrict, socklen_t *__restrict);174 int shell_getsockopt(int, int, int, void *restrict, socklen_t *restrict); 170 175 int shell_setsockopt(int, int, int, const void *, socklen_t); 171 176 int shell_shutdown(int, int); 177 int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len); 178 int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len); 172 179 // select.h 173 int shell_select(int, fd_set * __restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict);180 int shell_select(int, fd_set *restrict, fd_set *restrict, fd_set *restrict, struct timeval *restrict); 174 181 // syslog.h 175 182 void vsyslog (int, const char *, va_list); 176 183 // termios.h 177 int sio_tcgetattr (int, struct termios *);178 int sio_tcsetattr(int, int, const struct termios *);184 int tcgetattr (int, struct termios *); 185 int tcsetattr(int, int, const struct termios *); 179 186 // fcntl.h 180 187 int shell_open(const char *, int, void *arg); … … 196 203 int shell_chmod(const char *, mode_t); 197 204 int shell_mkdir(const char *, mode_t); 198 int shell_stat(const char * __restrict, struct stat *__restrict);205 int shell_stat(const char *restrict, struct stat *restrict); 199 206 int shell_fstat(int, struct stat *); 200 int shell_lstat(const char * __restrict, struct stat *__restrict);207 int shell_lstat(const char *restrict, struct stat *restrict); 201 208 202 209 int shell_clock_getres(clockid_t clk_id, struct timespec *res); … … 215 222 int shell_mprotect(void *addr, size_t len, int prot); 216 223 // TODO 224 int shell_clone(int, int); 225 int shell_dup2(int, int); 226 int shell_execve(const char *, char *const [], char *const []); 227 int shell_faccessat(int, const char *, int, int); 228 int shell_fchmodat(int, const char *, mode_t, int); 229 int shell_flock(int, int); 230 int shell_fork(); 231 int shell_futex(volatile int *, int, int, void *); 232 int shell_futimesat(int, const char *, struct timeval *); 233 int shell_getegid(); 234 int shell_geteuid(); 235 int shell_getgid(); 236 int shell_gettid(); 237 int shell_getuid(); 217 238 int shell_madvise(void *, size_t, int); 239 int shell_mremap(void *, size_t, size_t, int, void *); 240 int shell_munmap(void *, size_t); 241 int shell_pipe(int [2]); 242 int shell_pipe2(int [2], int); 243 int shell_ppoll(struct pollfd *, nfds_t, const struct timespec *,const sigset_t *); 244 int shell_pselect6(int, fd_set *restrict, fd_set *restrict, 245 fd_set *restrict, const struct timespec *restrict, const sigset_t *restrict); 246 int shell_setpgid(pid_t, pid_t); 247 int shell_setsid(); 248 int shell_symlink(const char *, const char *); 249 int shell_tkill(int, int); 250 int shell_umask(mode_t); 218 251 int shell_uname(struct utsname *); 252 int shell_utimensat(int, const char *, const struct timespec [2], int); 253 int shell_utimes(const char *, struct timeval *); 254 // sys/resource.h 255 struct rusage; 256 int shell_wait4(pid_t, int *, int, struct rusage *); 257 ssize_t shell_getrandom(void *buf, size_t buflen, unsigned int flags); 219 258 // âââmusl 220 259 … … 239 278 void __mbedcall_cp_asm(); 240 279 280 #include "ff.h" 281 WCHAR Utf8_to_Utf16(const char *, int *); /* in ff.c */ 282 void Utf16_to_Utf8(unsigned char *, int *, UINT); /* in ff.c */ 283 241 284 #ifdef __cplusplus 242 285 extern "C" { 243 286 #endif 244 voidKernel_StartExternal(int argc, char **argv);287 int Kernel_StartExternal(int argc, char **argv); 245 288 #ifdef __cplusplus 246 289 } 247 290 #endif 248 291 249 #endif /* SHELLIF_H */ 292 void *malloc(size_t size); 293 void *calloc(size_t size, size_t count); 294 void *realloc(void *ptr, size_t size); 295 void free(void *ptr); 296 297 #ifdef __cplusplus 298 } 299 #endif 300 301 #endif /* _SHELLIF_H_ */ -
EcnlProtoTool/trunk/ntshell/src/socket_stub.c
r439 r441 39 39 #include <t_syslog.h> 40 40 #include <t_stdlib.h> 41 //#include <string.h>41 #include <string.h> 42 42 #include <sil.h> 43 #include <setjmp.h>44 43 #include "syssvc/syslog.h" 44 #include <tinet_defs.h> 45 45 #include <tinet_config.h> 46 #include <net/net.h> 47 #include <net/net_endian.h> 46 48 #include <netinet/in.h> 47 49 #include <netinet/in_itron.h> … … 54 56 #include <net/if_var.h> 55 57 #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> 59 64 #include "fdtable.h" 65 #include "kernel_cfg.h" 66 67 #ifdef _DEBUG 68 static const char THIS_FILE[] = __FILE__; 69 #endif 60 70 61 71 #define SOCKET_TIMEOUT 2000000 72 #define TCP_SOCKET_BUF_SIZE 512 73 74 struct 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 85 typedef uint16_t in_port_t; 86 typedef uint32_t in_addr_t; 87 struct in_addr { in_addr_t s_addr; }; 88 89 struct 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 96 struct 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 108 struct 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 117 typedef 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 157 ER tcp6_cre_rep (ID repid, T_TCP6_CREP *pk_crep) { return E_SYS; } 158 ER tcp6_acp_cep (ID cepid, ID repid, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; } 159 ER tcp6_con_cep (ID cepid, T_IPV6EP *p_myaddr, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; } 160 161 ER udp6_cre_cep (ID cepid, T_UDP6_CCEP *pk_ccep) { return E_SYS; } 162 ER udp6_del_cep (ID cepid) { return E_SYS; } 163 ER_UINT udp6_snd_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; } 164 ER_UINT udp6_rcv_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; } 165 ER udp6_set_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; } 166 ER udp6_get_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; } 167 168 const T_IN6_ADDR *in6_get_ifaddr (int_t index) { return NULL; } 169 170 #endif 171 172 ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk); 173 ER socket_tcp6_callback(ID cepid, FN fncd, void *p_parblk); 174 ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk); 175 ER socket_udp6_callback(ID cepid, FN fncd, void *p_parblk); 176 177 static int tcp_fd_close(struct SHELL_FILE *fp); 178 static size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 179 static size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 180 static off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org); 181 static int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg); 182 static bool_t tcp_fd_readable(struct SHELL_FILE *fp); 183 static bool_t tcp_fd_writable(struct SHELL_FILE *fp); 184 static void tcp_fd_delete(struct SHELL_FILE *fp); 185 186 static int udp_fd_close(struct SHELL_FILE *fp); 187 static size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 188 static size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 189 static off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org); 190 static int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg); 191 static bool_t udp_fd_readable(struct SHELL_FILE *fp); 192 static bool_t udp_fd_writable(struct SHELL_FILE *fp); 193 static void udp_fd_delete(struct SHELL_FILE *fp); 194 195 IO_TYPE IO_TYPE_TCP = { tcp_fd_close, tcp_fd_read, tcp_fd_write, tcp_fd_seek, tcp_fd_ioctl, tcp_fd_readable, tcp_fd_writable, tcp_fd_delete }; 196 IO_TYPE IO_TYPE_UDP = { udp_fd_close, udp_fd_read, udp_fd_write, udp_fd_seek, udp_fd_ioctl, udp_fd_readable, udp_fd_writable, udp_fd_delete }; 62 197 63 198 typedef struct id_table_t { … … 72 207 73 208 id_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 75 213 }; 76 214 #define tcp_cepid_table_count (sizeof(tcp_cepid_table) / sizeof(tcp_cepid_table[0])) 215 216 #ifdef SUPPORT_INET6 217 218 id_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 223 id_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 77 240 78 241 id_table_t udp_cepid_table[] = { … … 81 244 #define udp_cepid_table_count (sizeof(udp_cepid_table) / sizeof(udp_cepid_table[0])) 82 245 246 #ifdef SUPPORT_INET6 247 248 id_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 260 void 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 83 271 ID new_id(id_table_t *table, int count) 84 272 { … … 108 296 } 109 297 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; 298 typedef struct SHELL_FILE SOCKET; 131 299 132 300 int shell_socket(int family, int type, int protocol) … … 148 316 switch (type) { 149 317 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)); 151 324 break; 152 325 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)); 154 332 break; 155 333 default: … … 157 335 } 158 336 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; 167 342 168 343 return fp->fd; … … 174 349 if (fp == NULL) 175 350 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) 177 353 return -EINVAL; 178 354 … … 184 360 } 185 361 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) { 188 364 case SOCK_STREAM: { 189 365 ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count); … … 191 367 return -ENOMEM; 192 368 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 }; 196 375 ret = tcp_cre_cep(cepid, &ccep); 197 376 if (ret != E_OK) { 377 free(socket->buf); 378 socket->buf = NULL; 379 socket->buf_size = 0; 198 380 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid); 199 381 return -ENOMEM; 200 382 } 201 383 fp->handle = cepid; 202 fp->socket.cepid = cepid;384 socket->cepid = cepid; 203 385 break; 204 386 } … … 215 397 } 216 398 fp->handle = cepid; 217 fp->socket.cepid = cepid;399 socket->cepid = cepid; 218 400 break; 219 401 } … … 227 409 return -EINVAL; 228 410 } 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; 232 459 } 233 460 … … 240 467 if (fp == NULL) 241 468 return -EBADF; 242 if (fp->socket.type != SOCK_STREAM) 469 socket_t *socket = (socket_t *)fp->exinf; 470 if (socket->type != SOCK_STREAM) 243 471 return -EINVAL; 244 472 245 fp->socket.backlog = backlog;473 socket->backlog = backlog; 246 474 247 475 ER ret; 248 switch ( fp->socket.family) {476 switch (socket->family) { 249 477 case AF_INET: { 250 478 ID repid = new_id(tcp_repid_table, tcp_repid_table_count); … … 252 480 return -ENOMEM; 253 481 254 struct sockaddr_in *laddr = & fp->socket.laddr4;482 struct sockaddr_in *laddr = &socket->laddr4; 255 483 T_TCP_CREP crep = { 0, {ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port)} }; 256 484 ret = tcp_cre_rep(repid, &crep); … … 259 487 return -ENOMEM; 260 488 } 261 fp->socket.repid = repid;489 socket->repid = repid; 262 490 break; 263 491 } 264 492 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; 267 509 } 268 510 … … 270 512 } 271 513 272 int shell_connect(int fd, const struct sockaddr *addr, socklen_t len)514 int shell_connect(int fd, const struct sockaddr *addr, socklen_t alen) 273 515 { 274 516 SOCKET *fp = fd_to_fp(fd); 275 517 if (fp == NULL) 276 518 return -EBADF; 277 if (fp->socket.type != SOCK_STREAM) 519 socket_t *socket = (socket_t *)fp->exinf; 520 if (socket->type != SOCK_STREAM) 278 521 return -EINVAL; 279 522 280 523 ER ret; 281 switch ( fp->socket.family) {524 switch (socket->family) { 282 525 case AF_INET: { 283 if ( len < 8) {526 if (alen < sizeof(struct sockaddr_in)) { 284 527 return -EINVAL; 285 528 } 286 if ( fp->socket.cepid == 0) {529 if (socket->cepid == 0) { 287 530 ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count); 288 531 if (cepid < 0) 289 532 return -ENOMEM; 290 533 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 }; 294 540 ret = tcp_cre_cep(cepid, &ccep); 295 541 if (ret != E_OK) { 542 free(socket->buf); 543 socket->buf = NULL; 544 socket->buf_size = 0; 296 545 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid); 297 546 return -ENOMEM; 298 547 } 299 548 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)); 306 555 T_IPV4EP lep = { ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port) }; 307 556 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) { 310 562 return -EHOSTUNREACH; 311 563 } … … 313 565 } 314 566 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++; 318 616 319 617 return 0; 320 618 } 321 619 322 int shell_accept(int fd, struct sockaddr * __restrict addr, socklen_t *__restrictlen)620 int shell_accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict alen) 323 621 { 324 622 SOCKET *lfp = fd_to_fp(fd); 325 623 if (lfp == NULL) 326 624 return -EBADF; 327 if ( lfp->socket.type != SOCK_STREAM)625 if (((socket_t *)lfp->exinf)->type != SOCK_STREAM) 328 626 return -EINVAL; 329 627 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) 332 630 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)); 336 636 337 637 ER ret; 338 switch (fp->socket.family) { 638 socket_t *socket = (socket_t *)fp->exinf; 639 switch (socket->family) { 339 640 case AF_INET: { 340 641 ID cepid; 341 if ( fp->socket.cepid == 0) {642 if (socket->cepid == 0) { 342 643 cepid = new_id(tcp_cepid_table, tcp_cepid_table_count); 343 644 if (cepid < 0) 344 645 return -ENOMEM; 345 646 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 }; 349 653 ret = tcp_cre_cep(cepid, &ccep); 350 654 if (ret != E_OK) { 655 free(socket->buf); 656 socket->buf = NULL; 657 socket->buf_size = 0; 351 658 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid); 352 659 return -ENOMEM; 353 660 } 354 661 fp->handle = cepid; 355 fp->socket.cepid = cepid; 662 fp->writable = 1; 663 socket->cepid = cepid; 356 664 } 357 665 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; 363 673 } 364 674 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); 366 676 if (ret < 0) { 367 677 return -ENOMEM; 368 678 } 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)); 371 681 raddr->sin_family = AF_INET; 372 682 raddr->sin_port = htons(rep.portno); 373 683 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 } 374 695 break; 375 696 } 376 697 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++; 392 759 393 760 return fp->fd; 394 761 } 395 762 396 ssize_t shell_send (int fd, const void *buf, size_t len, int flags)763 ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen) 397 764 { 398 765 SOCKET *fp = fd_to_fp(fd); … … 402 769 403 770 int ret = 0; 404 switch (fp->socket.family) { 771 socket_t *socket = (socket_t *)fp->exinf; 772 switch (socket->family) { 405 773 case AF_INET: { 406 switch ( fp->socket.type) {774 switch (socket->type) { 407 775 case SOCK_STREAM: { 776 if ((addr != NULL) || (alen != 0)) { 777 return -EISCONN; 778 } 779 408 780 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); 410 782 if (ret < 0) { 411 783 return -ECOMM; … … 413 785 } 414 786 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); 416 828 if (ret < 0) { 417 829 return -ECOMM; 418 830 } 419 831 } 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 } 420 846 break; 421 847 } 422 848 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++; 432 872 433 873 return ret; 434 874 } 435 875 436 ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen) 876 ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags) 877 { 878 no_implement("sendmsg\n"); 879 return -ENOSYS; 880 } 881 882 ssize_t shell_recvfrom(int fd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict addr, socklen_t *restrict alen) 437 883 { 438 884 SOCKET *fp = fd_to_fp(fd); … … 442 888 443 889 int ret = 0; 444 switch (fp->socket.family) { 890 socket_t *socket = (socket_t *)fp->exinf; 891 switch (socket->family) { 445 892 case AF_INET: { 446 switch ( fp->socket.type) {893 switch (socket->type) { 447 894 case SOCK_STREAM: { 448 if ((addr != NULL) && (alen != 0)) {449 return -EINVAL;450 }451 452 895 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); 454 897 if (ret < 0) { 898 syslog(LOG_ERROR, "tcp_rcv_oob => %d", ret); 455 899 return -ECOMM; 456 900 } 457 901 } 458 902 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); 460 967 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); 461 1038 return -ECOMM; 462 1039 } 463 1040 } 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 } 464 1091 break; 465 1092 } 466 1093 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); 477 1097 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++; 489 1172 490 1173 return ret; 491 1174 } 492 1175 493 ssize_t shell_ sendmsg(int fd, conststruct msghdr *msg, int flags)494 { 495 no_implement(" sendmsg\n");1176 ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags) 1177 { 1178 no_implement("recvmsg\n"); 496 1179 return -ENOSYS; 497 1180 } 498 1181 499 ssize_t shell_recv(int fd, void *buf, size_t len, int flags)1182 int shell_shutdown(int fd, int how) 500 1183 { 501 1184 SOCKET *fp = fd_to_fp(fd); … … 504 1187 } 505 1188 506 int ret = 0; 507 switch (fp->socket.family) { 1189 ER ret; 1190 socket_t *socket = (socket_t *)fp->exinf; 1191 switch (socket->family) { 508 1192 case AF_INET: { 509 switch ( fp->socket.type) {1193 switch (socket->type) { 510 1194 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); 528 1196 if (ret < 0) { 529 1197 return -ECOMM; 530 1198 } 1199 break; 531 1200 } 532 1201 } … … 534 1203 } 535 1204 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 1223 int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *restrict optlen) 544 1224 { 545 1225 SOCKET *fp = fd_to_fp(fd); … … 548 1228 } 549 1229 550 int ret = 0; 551 switch (fp->socket.family) { 1230 ER ret; 1231 socket_t *socket = (socket_t *)fp->exinf; 1232 switch (socket->family) { 552 1233 case AF_INET: { 553 switch ( fp->socket.type) {1234 switch (socket->type) { 554 1235 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); 557 1264 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); 563 1278 if (ret < 0) { 564 return -ECOMM; 565 } 1279 return -EINVAL; 1280 } 1281 *optlen = ret; 1282 break; 1283 default: 1284 return -EINVAL; 566 1285 } 567 1286 break; 568 1287 } 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 } 569 1332 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 1356 int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) 603 1357 { 604 1358 SOCKET *fp = fd_to_fp(fd); … … 608 1362 609 1363 ER ret; 610 switch (fp->socket.family) { 1364 socket_t *socket = (socket_t *)fp->exinf; 1365 switch (socket->family) { 611 1366 case AF_INET: { 612 switch ( fp->socket.type) {1367 switch (socket->type) { 613 1368 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; 617 1400 } 618 1401 break; 619 1402 } 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 } 620 1416 } 621 1417 break; 622 1418 } 623 1419 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; 626 1474 } 627 1475 … … 629 1477 } 630 1478 631 int shell_get sockopt(int fd, int level, int optname, void *optval, socklen_t *__restrict optlen)1479 int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) 632 1480 { 633 1481 SOCKET *fp = fd_to_fp(fd); … … 635 1483 return -EBADF; 636 1484 } 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) { 640 1492 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); 671 1498 break; 672 1499 } 673 1500 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; 676 1510 } 677 1511 … … 679 1513 } 680 1514 681 int shell_ setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)1515 int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) 682 1516 { 683 1517 SOCKET *fp = fd_to_fp(fd); … … 685 1519 return -EBADF; 686 1520 } 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) { 690 1528 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); 719 1539 break; 720 1540 } 721 1541 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; 724 1555 } 725 1556 … … 727 1558 } 728 1559 729 int tcp_fd_close(struct _IO_FILE *fp) 730 { 731 ER ret; 732 switch (fp->socket.family) { 1560 int 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) { 733 1566 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 } 736 1573 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); 746 1583 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; 753 1590 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); 755 1592 delete_id(tcp_repid_table, tcp_repid_table_count, repid); 756 1593 if (ret < 0) { … … 764 1601 } 765 1602 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; 768 1640 } 769 1641 … … 771 1643 } 772 1644 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)1645 size_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 1650 size_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 1655 off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org) 784 1656 { 785 1657 return -EPERM; 786 1658 } 787 1659 788 int tcp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg) 789 { 1660 int 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 790 1672 return -EINVAL; 791 1673 } 792 1674 793 int udp_fd_close(struct _IO_FILE *fp) 1675 bool_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 1708 bool_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 1715 void 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 1724 ER 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 1804 ER 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 1884 int udp_fd_close(struct SHELL_FILE *fp) 794 1885 { 795 1886 ER ret; 796 1887 ID cepid; 797 switch (fp->socket.family) { 1888 1889 socket_t *socket = (socket_t *)fp->exinf; 1890 switch (socket->family) { 798 1891 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 } 800 1897 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); 802 1902 delete_id(udp_cepid_table, udp_cepid_table_count, cepid); 803 1903 if (ret < 0) { … … 807 1907 } 808 1908 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; 811 1927 } 812 1928 … … 814 1930 } 815 1931 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)1932 size_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 1937 size_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 1942 off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org) 827 1943 { 828 1944 return -EPERM; 829 1945 } 830 1946 831 int udp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)1947 int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg) 832 1948 { 833 1949 return -EINVAL; 1950 } 1951 1952 bool_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 1964 bool_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 1971 void 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 1980 ER 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 2063 ER 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 } 834 2144 } 835 2145 … … 904 2214 } 905 2215 #endif 2216 2217 // musl-1.1.18/network/lookup.h 2218 struct address { 2219 int family; 2220 unsigned scopeid; 2221 uint8_t addr[16]; 2222 int sortkey; 2223 }; 2224 2225 #define MAXNS 3 2226 2227 struct resolvconf { 2228 struct address ns[MAXNS]; 2229 unsigned nns, attempts, ndots; 2230 unsigned timeout; 2231 }; 2232 2233 // musl-1.1.18/network/resolvconf.c 2234 int __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 101 101 switch (r->n) { 102 102 case SYS__llseek: 103 ret urnshell_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); 104 104 case SYS__newselect: 105 105 ret = shell_select(r->a, (fd_set *)r->b, (fd_set *)r->c, (fd_set *)r->d, (struct timeval *)r->e); … … 359 359 break; 360 360 case SYS_gettid: 361 no_implement("gettid\n");361 ret = shell_gettid(); 362 362 break; 363 363 case SYS_gettimeofday: … … 439 439 break; 440 440 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); 442 442 break; 443 443 case SYS_madvise: … … 472 472 break; 473 473 case SYS_mprotect: 474 ret urnshell_mprotect((void *)r->a, (size_t)r->b, r->c);474 ret = shell_mprotect((void *)r->a, (size_t)r->b, r->c); 475 475 break; 476 476 case SYS_mq_getsetattr: … … 520 520 break; 521 521 case SYS_nanosleep: 522 no_implement("nanosleep\n");522 ret = shell_nanosleep((const struct timespec *)r->a, (struct timespec *)r->b); 523 523 break; 524 524 case SYS_nice: … … 538 538 break; 539 539 case SYS_pipe: 540 no_implement("pipe\n");540 ret = shell_pipe((int *)r->a); 541 541 break; 542 542 case SYS_pipe2: … … 601 601 break; 602 602 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); 604 604 break; 605 605 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); 610 607 break; 611 608 case SYS_recvmsg: … … 634 631 break; 635 632 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); 637 634 break; 638 635 case SYS_rt_sigpending: … … 652 649 break; 653 650 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); 655 652 break; 656 653 case SYS_sendmsg: … … 658 655 break; 659 656 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); 664 658 break; 665 659 case SYS_setsockopt: … … 869 863 break; 870 864 case SYS_tkill: 871 no_implement("tkill\n");865 ret = shell_tkill((int)r->a, (int)r->b); 872 866 break; 873 867 case SYS_truncate64: … … 918 912 case SYS_writev: 919 913 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); 920 926 break; 921 927 default: -
EcnlProtoTool/trunk/ntshell/src/tinet_app_config.h
r331 r441 285 285 #ifdef SUPPORT_ETHER 286 286 287 #ifdef DHCP4_CLI_CFG288 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 295 287 #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) 300 290 301 291 #define NUM_IN4_STATIC_ROUTE_ENTRY 3 -
EcnlProtoTool/trunk/ntshell/src/tinet_main.cfg
r439 r441 2 2 * TOPPERS PROJECT Home Network Working Group Software 3 3 * 4 * Copyright (C) 201 7-2019 Cores Co., Ltd. Japan4 * Copyright (C) 2014-2019 Cores Co., Ltd. Japan 5 5 * 6 6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ -
EcnlProtoTool/trunk/ntshell/webserver/httpd-fs.c
r436 r441 63 63 #endif /* NULL */ 64 64 65 extern ntstdio_t ntstdio;65 extern ntstdio_t *ntstdio; 66 66 67 67 /*-----------------------------------------------------------------------------------*/ … … 89 89 } 90 90 else { 91 ntstdio_printf( &ntstdio, "%s/%s\n", path, fn);91 ntstdio_printf(ntstdio, "%s/%s\n", path, fn); 92 92 } 93 93 } … … 114 114 if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) { 115 115 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); 117 117 return 0; 118 118 } 119 119 120 120 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); 122 122 return 0; 123 123 } … … 132 132 133 133 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); 135 135 return 0; 136 136 } … … 139 139 file->len = fd->fsize; 140 140 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); 142 142 143 143 return 1; … … 158 158 159 159 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); 161 161 return 0; 162 162 } … … 165 165 file->len = 0; 166 166 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); 168 168 169 169 return 1; … … 179 179 180 180 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); 182 182 return 0; 183 183 } 184 184 185 185 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); 187 187 } 188 188 189 189 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); 195 195 196 196 return rlen; … … 206 206 207 207 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); 209 209 return 0; 210 210 } 211 211 212 212 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); 214 214 } 215 215 216 216 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); 218 218 return 0; 219 219 } … … 222 222 file->len += rlen; 223 223 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); 225 225 226 226 return rlen; … … 234 234 235 235 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); 237 237 return 0; 238 238 } -
EcnlProtoTool/trunk/ntshell/webserver/httpd.c
r436 r441 59 59 #define FALSE 0 60 60 61 extern ntstdio_t ntstdio;61 extern ntstdio_t *ntstdio; 62 62 SYSTIM httpd_time; 63 63 struct httpd_state *uploding; … … 65 65 66 66 /* TCP 送受信ウィンドバッファ */ 67 #ifdef SUPPORT_INET6 68 uint8_t tcp6_swbuf1[TCP_SWBUF_SIZE]; 69 uint8_t tcp6_rwbuf1[TCP_RWBUF_SIZE]; 70 uint8_t tcp6_swbuf2[TCP_SWBUF_SIZE]; 71 uint8_t tcp6_rwbuf2[TCP_RWBUF_SIZE]; 72 #endif 73 74 #ifdef SUPPORT_INET4 67 75 uint8_t tcp_swbuf1[TCP_SWBUF_SIZE]; 68 76 uint8_t tcp_rwbuf1[TCP_RWBUF_SIZE]; 69 77 uint8_t tcp_swbuf2[TCP_SWBUF_SIZE]; 70 78 uint8_t tcp_rwbuf2[TCP_RWBUF_SIZE]; 79 #endif 71 80 72 81 #define ISO_nl 0x0a … … 325 334 // アップロード先はSDカード 326 335 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); 328 337 if (!httpd_fs_create(s->filename, &s->file)) { 329 338 goto error; … … 333 342 } 334 343 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); 336 345 goto error; 337 346 } … … 365 374 366 375 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" 368 377 "on last on_body callback call " 369 378 "but it doesn't! ***\n\n"); … … 378 387 379 388 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); 381 390 httpd_fs_close(&s->file); 382 391 memset(&s->file, 0, sizeof(s->file)); … … 403 412 struct httpd_state *s = get_context(p); 404 413 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" 406 415 "value in both on_message_complete and on_headers_complete " 407 416 "but it doesn't! ***\n\n"); … … 412 421 http_body_is_final(p) && 413 422 !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" 415 424 "on last on_body callback call " 416 425 "but it doesn't! ***\n\n"); … … 484 493 slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR); 485 494 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); 487 496 s->state = STATE_CLOSING; 488 497 break; … … 497 506 len = httpd_fs_read(&s->file, buf, len); 498 507 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); 500 509 break; 501 510 } … … 505 514 506 515 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); 508 517 s->state = STATE_CLOSING; 509 518 break; … … 511 520 } 512 521 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); 514 523 httpd_fs_close(&s->file); 515 524 s->file.len = 0; … … 527 536 slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR); 528 537 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); 530 539 s->state = STATE_CLOSING; 531 540 break; … … 544 553 545 554 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); 547 556 s->state = STATE_CLOSING; 548 557 break; … … 690 699 break; 691 700 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); 693 702 if (!httpd_fs_open(s->filename, sizeof(s->message.request_url), &s->file)) { 694 703 s->filename = NULL; … … 704 713 break; 705 714 case OUT_STATE_WAIT_POST_BODY: 706 //ntstdio_printf( &ntstdio, "wait post body\n");715 //ntstdio_printf(ntstdio, "wait post body\n"); 707 716 s->out.wait = true; 708 717 break; 709 718 case OUT_STATE_BODY_RECEIVED: 710 //ntstdio_printf( &ntstdio, "body received\n");719 //ntstdio_printf(ntstdio, "body received\n"); 711 720 s->out.statushdr = http_header_200; 712 721 s->out.state = OUT_STATE_SEND_HEADER; 713 722 break; 714 723 case OUT_STATE_SEND_HEADER: 715 //ntstdio_printf( &ntstdio, "send header\n");724 //ntstdio_printf(ntstdio, "send header\n"); 716 725 send_headers(s, s->out.statushdr); 717 726 break; 718 727 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); 720 729 send_file(s); 721 730 break; 722 731 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); 724 733 send_data(s); 725 734 break; 726 735 case OUT_STATE_SEND_END: 727 //ntstdio_printf( &ntstdio, "send end\n");736 //ntstdio_printf(ntstdio, "send end\n"); 728 737 s->out.wait = true; 729 738 if (s->message.should_keep_alive && s->reset == 0) { … … 783 792 slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR); 784 793 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); 786 795 return; 787 796 } … … 847 856 //} 848 857 } 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); 850 859 uploding = NULL; 851 860 s->state = STATE_CLOSING; … … 855 864 tcp_rel_buf(s->cepid, done); 856 865 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); 858 867 uploding = NULL; 859 868 s->state = STATE_CLOSING; … … 880 889 break; 881 890 } 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); 883 892 s->state = STATE_CLOSING; 884 893 break; … … 918 927 919 928 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); 921 930 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); 923 932 924 933 return E_PAR; … … 936 945 ret2 = get_tim(&httpd_time); 937 946 if (ret2 != E_OK) { 938 ntstdio_printf( &ntstdio, "get_tim\n");947 ntstdio_printf(ntstdio, "get_tim\n"); 939 948 return; 940 949 } … … 944 953 memset(&s->dst, 0, sizeof(s->dst)); 945 954 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); 947 956 tslp_tsk(100 * 1000); // TODO 948 957 s->state = STATE_CLOSING; … … 950 959 } 951 960 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); 953 962 memset(&s->in, 0, sizeof(s->in)); 954 963 memset(&s->out, 0, sizeof(s->out)); … … 965 974 break; 966 975 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); 968 977 tcp_sht_cep(s->cepid); 969 978 tcp_cls_cep(s->cepid, TMO_FEVR); -
EcnlProtoTool/trunk/ntshell/webserver/httpd.h
r436 r441 57 57 /* TCP 送受信ウィンドバッファサイズ */ 58 58 59 #define TCP_SWBUF_SIZE 51260 #define TCP_RWBUF_SIZE 51259 #define TCP_SWBUF_SIZE 1024 60 #define TCP_RWBUF_SIZE 1024 61 61 62 62 /* … … 66 66 67 67 /* TCP 送受信ウィンドバッファ */ 68 68 #ifdef SUPPORT_INET6 69 extern uint8_t tcp6_swbuf1[]; 70 extern uint8_t tcp6_rwbuf1[]; 71 extern uint8_t tcp6_swbuf2[]; 72 extern uint8_t tcp6_rwbuf2[]; 73 #endif 74 75 #ifdef SUPPORT_INET4 69 76 extern uint8_t tcp_swbuf1[]; 70 77 extern uint8_t tcp_rwbuf1[]; 71 78 extern uint8_t tcp_swbuf2[]; 72 79 extern uint8_t tcp_rwbuf2[]; 80 #endif 73 81 74 82 /* HTTPサーバータスク */ -
EcnlProtoTool/trunk/ntshell/webserver/tinet_httpd.cfg
r436 r441 62 62 TCP6_CRE_CEP (TCP6_CEPID1, { 63 63 0, 64 tcp _swbuf1,64 tcp6_swbuf1, 65 65 TCP_SWBUF_SIZE, 66 tcp _rwbuf1,66 tcp6_rwbuf1, 67 67 TCP_RWBUF_SIZE, 68 68 (FP)callback_nblk_tcp … … 71 71 TCP6_CRE_CEP (TCP6_CEPID2, { 72 72 0, 73 tcp _swbuf2,73 tcp6_swbuf2, 74 74 TCP_SWBUF_SIZE, 75 tcp _rwbuf2,75 tcp6_rwbuf2, 76 76 TCP_RWBUF_SIZE, 77 77 (FP)callback_nblk_tcp -
EcnlProtoTool/trunk/prototool/src/linker_scriptDebug.ld
r439 r441 8 8 9 9 MEMORY { 10 BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x002 2000011 ROM (rx) : ORIGIN = 0x182 20000, LENGTH = 0x005E000010 BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x00200000 11 ROM (rx) : ORIGIN = 0x18200000, LENGTH = 0x00600000 12 12 RAM (xrw) : ORIGIN = 0x20100000, LENGTH = 8M 13 13 }
Note:
See TracChangeset
for help on using the changeset viewer.