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

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

File:
1 edited

Legend:

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

    r374 r387  
    4545#include "syssvc/serial.h"
    4646#include "syssvc/syslog.h"
    47 #include "socket_stub.h"
     47#include "fdtable.h"
    4848#include "util/ntstdio.h"
    4949#include "usrcmd.h"
     
    5151#include "kernel_cfg.h"
    5252#include "target_syssvc.h"
     53
     54struct SHELL_DIR {
     55        FATFS_DIR dir;
     56        struct dirent dirent;
     57};
    5358
    5459int fresult2errno(FRESULT res)
     
    8590static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    8691static bool_t file_readable(struct SHELL_FILE *fp);
     92static bool_t file_writable(struct SHELL_FILE *fp);
    8793static void file_delete(struct SHELL_FILE *fp);
    8894
     
    9399static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg);
    94100static bool_t dir_readable(struct SHELL_FILE *fp);
     101static bool_t dir_writable(struct SHELL_FILE *fp);
    95102static void dir_delete(struct SHELL_FILE *fp);
    96103
    97 IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_delete };
    98 IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_delete };
     104IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_writable, file_delete };
     105IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_writable, dir_delete };
    99106
    100107int shell_open(const char *path, int flags, void *arg)
     
    114121                FRESULT res;
    115122                if ((res = f_opendir(dir, path)) != FR_OK) {
     123                        delete_fp(fp);
    116124                        return fresult2errno(res);
    117125                }
     
    165173        }
    166174
     175        delete_fp(fp);
    167176        return fresult2errno(res);
    168177}
     
    239248}
    240249
     250bool_t file_writable(struct SHELL_FILE *fp)
     251{
     252        return fp->writable && (fp->writeevt_w == fp->writeevt_r);
     253}
     254
    241255void file_delete(struct SHELL_FILE *fp)
    242256{
     
    356370{
    357371        return shell_ioctl(fd, cmd, arg);
    358 }
    359 
    360 extern IO_TYPE IO_TYPE_SIO;
    361 
    362 int sio_tcgetattr(int fd, struct termios *termios)
    363 {
    364         struct SHELL_FILE *fp = fd_to_fp(fd);
    365         if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
    366                 return -EBADF;
    367 
    368         ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
    369 
    370         memset(termios, 0, sizeof(*termios));
    371 
    372         if (ntstdio->option & NTSTDIO_OPTION_LINE_ECHO) {
    373                 termios->c_lflag |= ECHO;
    374         }
    375         else {
    376                 termios->c_lflag &= ~ECHO;
    377         }
    378         if (ntstdio->option & NTSTDIO_OPTION_CANON) {
    379                 termios->c_lflag |= ICANON;
    380         }
    381         else {
    382                 termios->c_lflag &= ~ICANON;
    383         }
    384         if (ntstdio->option & NTSTDIO_OPTION_LF_CR) {
    385                 termios->c_iflag |= INLCR;
    386         }
    387         else {
    388                 termios->c_iflag &= ~INLCR;
    389         }
    390         if (ntstdio->option & NTSTDIO_OPTION_LF_CRLF) {
    391                 termios->c_oflag |= ONLCR;
    392         }
    393         else {
    394                 termios->c_oflag &= ~ONLCR;
    395         }
    396 
    397         return 0;
    398 }
    399 
    400 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
    401 {
    402         struct SHELL_FILE *fp = fd_to_fp(fd);
    403         if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
    404                 return -EBADF;
    405 
    406         ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
    407 
    408         if (optional_actions == TCSANOW) {
    409                 if (termios->c_lflag & ECHO) {
    410                         ntstdio->option |= NTSTDIO_OPTION_LINE_ECHO;
    411                 }
    412                 else {
    413                         ntstdio->option &= ~NTSTDIO_OPTION_LINE_ECHO;
    414                 }
    415                 if (termios->c_lflag & ICANON) {
    416                         ntstdio->option |= NTSTDIO_OPTION_CANON;
    417                 }
    418                 else {
    419                         ntstdio->option &= ~NTSTDIO_OPTION_CANON;
    420                 }
    421                 if (termios->c_iflag & INLCR) {
    422                         ntstdio->option |= NTSTDIO_OPTION_LF_CR;
    423                 }
    424                 else {
    425                         ntstdio->option &= ~NTSTDIO_OPTION_LF_CR;
    426                 }
    427                 if (termios->c_oflag & ONLCR) {
    428                         ntstdio->option |= NTSTDIO_OPTION_LF_CRLF;
    429                 }
    430                 else {
    431                         ntstdio->option &= ~NTSTDIO_OPTION_LF_CRLF;
    432                 }
    433                 return 0;
    434         }
    435 
    436         shell_abort();
    437         return 0;
    438372}
    439373
     
    688622}
    689623
     624bool_t dir_writable(struct SHELL_FILE *fp)
     625{
     626        return false;
     627}
     628
    690629void dir_delete(struct SHELL_FILE *fp)
    691630{
     
    710649        return 0;
    711650}
    712 
    713 #ifndef _MSC_VER
    714 extern uint32_t _HeapBase;
    715 extern uint32_t _HeapLimit;
    716 #else
    717 uint8_t _HeapBase[14 * 4096];
    718 #define _HeapLimit _HeapBase[sizeof(_HeapBase)]
    719 #endif
    720 
    721 void *shell_brk(void *addr)
    722 {
    723         if (addr == 0) {
    724                 return (void *)(&_HeapBase);
    725         }
    726         if ((addr >= (void *)&_HeapBase) && (addr < (void *)&_HeapLimit)) {
    727                 return addr;
    728         }
    729         return (void *)-1;
    730 }
    731 
    732 void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
    733 {
    734         if (fd != -1)
    735                 return (void *)-EINVAL;
    736 
    737         if ((length >= 0) && (length <= sizeof(&_HeapBase))) {
    738                 return &_HeapBase;
    739         }
    740         return (void *)-1;
    741 }
    742 
    743 int shell_mprotect(void *addr, size_t len, int prot)
    744 {
    745         //if ((addr >= (void *)&_HeapBase) && (addr + len < (void *)&_HeapLimit)) {
    746         return 0;
    747 //}
    748 //return -1;
    749 }
    750 
    751 #include "tlsf.h"
    752 
    753 static tlsf_t sys_tlsf;
    754 static pool_t sys_pool;
    755 
    756 void sys_tlsf_init(void)
    757 {
    758         sys_tlsf = tlsf_create(&_HeapBase);
    759         if (sys_tlsf == NULL)
    760                 return;
    761 
    762         sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&_HeapBase) + tlsf_size(), ((intptr_t)&_HeapLimit - (intptr_t)&_HeapBase) - tlsf_size());
    763 }
    764 
    765 void sys_fini(void)
    766 {
    767         tlsf_destroy(sys_tlsf);
    768 }
    769 
    770 void *malloc(size_t size)
    771 {
    772         void *result;
    773         wai_sem(SEM_MALLOC);
    774         result = tlsf_malloc(sys_tlsf, size);
    775         sig_sem(SEM_MALLOC);
    776         if (result == NULL)
    777                 tlsf_check_pool(sys_pool);
    778         return result;
    779 }
    780 
    781 void *calloc(size_t size, size_t count)
    782 {
    783         void *result;
    784         wai_sem(SEM_MALLOC);
    785         result = tlsf_malloc(sys_tlsf, count * size);
    786         sig_sem(SEM_MALLOC);
    787         if (result != NULL)
    788                 memset(result, 0, count * size);
    789         else
    790                 tlsf_check_pool(sys_pool);
    791         return result;
    792 }
    793 
    794 void *realloc(void *ptr, size_t size)
    795 {
    796         void *result;
    797         wai_sem(SEM_MALLOC);
    798         result = tlsf_realloc(sys_tlsf, ptr, size);
    799         sig_sem(SEM_MALLOC);
    800         if (result == NULL)
    801                 tlsf_check_pool(sys_pool);
    802         return result;
    803 }
    804 
    805 void free(void *ptr)
    806 {
    807         wai_sem(SEM_MALLOC);
    808         tlsf_free(sys_tlsf, ptr);
    809         sig_sem(SEM_MALLOC);
    810 }
    811 
Note: See TracChangeset for help on using the changeset viewer.