Changeset 374 for asp3_tinet_ecnl_rx/trunk/ntshell/src/io_stub.c
- Timestamp:
- Apr 5, 2019, 9:26:53 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_rx/trunk/ntshell/src/io_stub.c
r337 r374 1 1 /* 2 2 * TOPPERS ECHONET Lite Communication Middleware 3 * 3 * 4 4 * Copyright (C) 2017 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 */ … … 43 43 #include <sil.h> 44 44 #include <string.h> 45 #include "syssvc/serial.h" 45 46 #include "syssvc/syslog.h" 46 47 #include "socket_stub.h" … … 49 50 #include "core/ntlibc.h" 50 51 #include "kernel_cfg.h" 52 #include "target_syssvc.h" 51 53 52 54 int fresult2errno(FRESULT res) … … 77 79 } 78 80 79 int shell_open(const char * path, int flags, void *arg) 80 { 81 FRESULT res; 82 struct _IO_FILE *fp; 81 static int file_close(struct SHELL_FILE *fp); 82 static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 83 static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 84 static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org); 85 static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg); 86 static bool_t file_readable(struct SHELL_FILE *fp); 87 static void file_delete(struct SHELL_FILE *fp); 88 89 static int dir_close(struct SHELL_FILE *fp); 90 static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 91 static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 92 static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org); 93 static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg); 94 static bool_t dir_readable(struct SHELL_FILE *fp); 95 static void dir_delete(struct SHELL_FILE *fp); 96 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 }; 99 100 int shell_open(const char *path, int flags, void *arg) 101 { 102 FRESULT res; 103 struct SHELL_FILE *fp; 83 104 84 105 if (flags & O_DIRECTORY) { 85 fp = new_ dir_fd(0);106 fp = new_fp(&IO_TYPE_DIR, 0, 0); 86 107 if (fp == NULL) 87 108 return -ENOMEM; 88 109 89 DIR *dir = &fp->pdir->dir; 110 fp->exinf = malloc(sizeof(struct SHELL_DIR)); 111 memset(fp->exinf, 0, sizeof(struct SHELL_DIR)); 112 113 FATFS_DIR *dir = &((struct SHELL_DIR *)fp->exinf)->dir; 90 114 FRESULT res; 91 115 if ((res = f_opendir(dir, path)) != FR_OK) { … … 95 119 } 96 120 97 fp = new_f ile_fd(0);121 fp = new_fp(&IO_TYPE_FILE, 0, 1); 98 122 if (fp == NULL) 99 123 return -ENOMEM; 124 125 fp->exinf = malloc(sizeof(FIL)); 126 memset(fp->exinf, 0, sizeof(FIL)); 100 127 101 128 BYTE fmd = 0; … … 133 160 } 134 161 135 if ((res = f_open( fp->pfile, path, fmd)) == FR_OK) {162 if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) { 136 163 fp->handle = fp->fd; 137 164 return fp->fd; … … 141 168 } 142 169 143 int file_close(struct _IO_FILE *fp)144 { 145 FRESULT res; 146 147 if ((res = f_close( fp->pfile)) == FR_OK) {170 int file_close(struct SHELL_FILE *fp) 171 { 172 FRESULT res; 173 174 if ((res = f_close((FIL *)fp->exinf)) == FR_OK) { 148 175 return 0; 149 176 } … … 152 179 } 153 180 154 size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)181 size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 155 182 { 156 183 unsigned int ret = 0; 157 184 FRESULT res; 158 185 159 if ((res = f_read( fp->pfile, data, len, &ret)) != FR_OK)186 if ((res = f_read((FIL *)fp->exinf, data, len, &ret)) != FR_OK) 160 187 return -EIO; 161 188 … … 163 190 } 164 191 165 size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)192 size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 166 193 { 167 194 unsigned int ret = 0; 168 195 FRESULT res; 169 196 170 if ((res = f_write( fp->pfile, data, len, &ret)) != FR_OK)197 if ((res = f_write((FIL *)fp->exinf, data, len, &ret)) != FR_OK) 171 198 return -EIO; 172 199 … … 174 201 } 175 202 176 off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)203 off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 177 204 { 178 205 switch (dir) { … … 191 218 192 219 FRESULT res; 193 if ((res = f_seek( fp->pfile, ptr, dir)) != FR_OK)220 if ((res = f_seek((FIL *)fp->exinf, ptr, dir)) != FR_OK) 194 221 return -EIO; 195 222 196 return fp->pfile->fptr;197 } 198 199 int file_ioctl(struct _IO_FILE *fp, int req, void *arg)223 return ((FIL *)fp->exinf)->fptr; 224 } 225 226 int file_ioctl(struct SHELL_FILE *fp, int req, void *arg) 200 227 { 201 228 DRESULT res; 202 229 203 if ((res = disk_ioctl( fp->pfile->fs->drv, req, arg) != RES_OK))230 if ((res = disk_ioctl(((FIL *)fp->exinf)->fs->drv, req, arg) != RES_OK)) 204 231 return -EINVAL; 205 232 … … 207 234 } 208 235 236 bool_t file_readable(struct SHELL_FILE *fp) 237 { 238 return fp->readevt_w != fp->readevt_r; 239 } 240 241 void file_delete(struct SHELL_FILE *fp) 242 { 243 free((FIL *)fp->exinf); 244 fp->exinf = NULL; 245 } 246 209 247 int shell_close(int fd) 210 248 { 211 struct _IO_FILE *fp = fd_to_fp(fd);212 if (fp == NULL) 213 return -EBADF; 214 215 int ret = fp-> close(fp);249 struct SHELL_FILE *fp = fd_to_fp(fd); 250 if (fp == NULL) 251 return -EBADF; 252 253 int ret = fp->type->close(fp); 216 254 217 255 delete_fp(fp); … … 222 260 ssize_t shell_read(int fd, void *data, size_t len) 223 261 { 224 struct _IO_FILE *fp = fd_to_fp(fd);225 if (fp == NULL) 226 return -EBADF; 227 228 return fp-> read(fp, (unsigned char *)data, len);262 struct SHELL_FILE *fp = fd_to_fp(fd); 263 if (fp == NULL) 264 return -EBADF; 265 266 return fp->type->read(fp, (unsigned char *)data, len); 229 267 } 230 268 … … 232 270 { 233 271 int result = 0; 234 struct _IO_FILE *fp = fd_to_fp(fd);272 struct SHELL_FILE *fp = fd_to_fp(fd); 235 273 if (fp == NULL) 236 274 return -EBADF; … … 238 276 const struct iovec *end = &iov[iovcnt]; 239 277 for (; iov < end; iov++) { 240 result += fp-> read(fp, (unsigned char *)iov->iov_base, iov->iov_len);278 result += fp->type->read(fp, (unsigned char *)iov->iov_base, iov->iov_len); 241 279 } 242 280 … … 246 284 ssize_t shell_write(int fd, const void *data, size_t len) 247 285 { 248 struct _IO_FILE *fp = fd_to_fp(fd);249 if (fp == NULL) 250 return -EBADF; 251 252 return fp-> write(fp, (unsigned char *)data, len);286 struct SHELL_FILE *fp = fd_to_fp(fd); 287 if (fp == NULL) 288 return -EBADF; 289 290 return fp->type->write(fp, (unsigned char *)data, len); 253 291 } 254 292 … … 256 294 { 257 295 int result = 0; 258 struct _IO_FILE *fp = fd_to_fp(fd);296 struct SHELL_FILE *fp = fd_to_fp(fd); 259 297 if (fp == NULL) 260 298 return -EBADF; … … 262 300 const struct iovec *end = &iov[iovcnt]; 263 301 for (; iov < end; iov++) { 264 result += fp-> write(fp, (unsigned char *)iov->iov_base, iov->iov_len);302 result += fp->type->write(fp, (unsigned char *)iov->iov_base, iov->iov_len); 265 303 } 266 304 … … 270 308 int shell_llseek(int fd, off_t ptr, off_t *result, int dir) 271 309 { 272 struct _IO_FILE *fp = fd_to_fp(fd);273 if (fp == NULL) 274 return -EBADF; 275 276 off_t ret = fp-> seek(fp, ptr, dir);310 struct SHELL_FILE *fp = fd_to_fp(fd); 311 if (fp == NULL) 312 return -EBADF; 313 314 off_t ret = fp->type->seek(fp, ptr, dir); 277 315 if (ret < 0) 278 316 return ret; … … 284 322 int shell_fstat(int fd, struct stat * st) 285 323 { 286 struct _IO_FILE *fp = fd_to_fp(fd);324 struct SHELL_FILE *fp = fd_to_fp(fd); 287 325 if (fp == NULL) 288 326 return -EBADF; … … 296 334 int shell_fsync(int fd) 297 335 { 298 struct _IO_FILE *fp = fd_to_fp(fd);336 struct SHELL_FILE *fp = fd_to_fp(fd); 299 337 if (fp == NULL) 300 338 return -EBADF; … … 304 342 int shell_ftruncate(int fd, off_t length) 305 343 { 306 struct _IO_FILE *fp = fd_to_fp(fd);307 if (fp == NULL) 308 return -EBADF; 309 310 FRESULT res; 311 if ((res = f_truncate( fp->pfile)) != FR_OK)344 struct SHELL_FILE *fp = fd_to_fp(fd); 345 if (fp == NULL) 346 return -EBADF; 347 348 FRESULT res; 349 if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK) 312 350 return fresult2errno(res); 313 351 … … 320 358 } 321 359 360 extern IO_TYPE IO_TYPE_SIO; 361 322 362 int sio_tcgetattr(int fd, struct termios *termios) 323 363 { 324 extern ntstdio_t ntstdio; 325 326 if (fd == STDIN_FILENO) { 327 memset(termios, 0, sizeof(*termios)); 328 329 if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) { 330 termios->c_lflag |= ECHO; 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; 331 411 } 332 412 else { 333 termios->c_lflag &= ~ECHO;334 } 335 if ( ntstdio.option & NTSTDIO_OPTION_CANON) {336 termios->c_lflag |= ICANON;413 ntstdio->option &= ~NTSTDIO_OPTION_LINE_ECHO; 414 } 415 if (termios->c_lflag & ICANON) { 416 ntstdio->option |= NTSTDIO_OPTION_CANON; 337 417 } 338 418 else { 339 termios->c_lflag &= ~ICANON;340 } 341 if ( ntstdio.option & NTSTDIO_OPTION_LF_CR) {342 termios->c_iflag |= INLCR;419 ntstdio->option &= ~NTSTDIO_OPTION_CANON; 420 } 421 if (termios->c_iflag & INLCR) { 422 ntstdio->option |= NTSTDIO_OPTION_LF_CR; 343 423 } 344 424 else { 345 termios->c_iflag &= ~INLCR;346 } 347 if ( ntstdio.option & NTSTDIO_OPTION_LF_CRLF) {348 termios->c_oflag |= ONLCR;425 ntstdio->option &= ~NTSTDIO_OPTION_LF_CR; 426 } 427 if (termios->c_oflag & ONLCR) { 428 ntstdio->option |= NTSTDIO_OPTION_LF_CRLF; 349 429 } 350 430 else { 351 termios->c_oflag &= ~ONLCR;431 ntstdio->option &= ~NTSTDIO_OPTION_LF_CRLF; 352 432 } 353 433 return 0; 354 434 } 355 shell_abort(); 356 return 0; 357 } 358 359 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios) 360 { 361 extern ntstdio_t ntstdio; 362 363 if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) { 364 if (termios->c_lflag & ECHO) { 365 ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO; 366 } 367 else { 368 ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO; 369 } 370 if (termios->c_lflag & ICANON) { 371 ntstdio.option |= NTSTDIO_OPTION_CANON; 372 } 373 else { 374 ntstdio.option &= ~NTSTDIO_OPTION_CANON; 375 } 376 if (termios->c_iflag & INLCR) { 377 ntstdio.option |= NTSTDIO_OPTION_LF_CR; 378 } 379 else{ 380 ntstdio.option &= ~NTSTDIO_OPTION_LF_CR; 381 } 382 if (termios->c_oflag & ONLCR) { 383 ntstdio.option |= NTSTDIO_OPTION_LF_CRLF; 384 } 385 else { 386 ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF; 387 } 388 return 0; 389 } 435 390 436 shell_abort(); 391 437 return 0; … … 396 442 FILINFO fi; 397 443 FRESULT ret; 398 #if _USE_LFN399 static char lfn[ _MAX_LFN + 1]; /* Buffer to store the LFN */444 #if FF_USE_LFN 445 static char lfn[FF_MAX_LFN + 1]; /* Buffer to store the LFN */ 400 446 fi.lfname = lfn; 401 447 fi.lfsize = sizeof lfn; 402 448 #endif 403 449 if (strcmp(path, ".") == 0) { 404 char cwd[ _MAX_LFN];450 char cwd[FF_MAX_LFN]; 405 451 if ((ret = f_getcwd(cwd, sizeof(cwd))) != FR_OK) { 406 452 return fresult2errno(ret); … … 426 472 st->st_mtim.tv_nsec = 0; 427 473 st->st_mtim.tv_sec = fi.fdate + fi.ftime; 428 st->st_mode 474 st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH); 429 475 st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH); 430 476 st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG; … … 485 531 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 486 532 487 if (mode & S_IREAD) {488 if ((mode & S_IWRITE) == 0) {533 if (mode & S_IREAD) { 534 if ((mode & S_IWRITE) == 0) { 489 535 attr |= AM_RDO; 490 536 } … … 494 540 } 495 541 496 if ((res = f_chmod(path, attr, mask)) != FR_OK) {542 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 497 543 return fresult2errno(res); 498 544 } … … 507 553 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 508 554 509 if (mode & S_IREAD) {510 if ((mode & S_IWRITE) == 0) {555 if (mode & S_IREAD) { 556 if ((mode & S_IWRITE) == 0) { 511 557 attr |= AM_RDO; 512 558 } … … 516 562 } 517 563 518 if ((res = f_chmod(path, attr, mask)) != FR_OK) {564 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 519 565 return fresult2errno(res); 520 566 } … … 526 572 { 527 573 FRESULT ret; 528 if ((ret = f_getcwd(buf, size)) != FR_OK) {574 if ((ret = f_getcwd(buf, size)) != FR_OK) { 529 575 return NULL; 530 576 } … … 549 595 } 550 596 551 int dir_close(struct _IO_FILE *fp)552 { 553 FRESULT res; 554 if ((res = f_closedir(& fp->pdir->dir)) != FR_OK) {597 int dir_close(struct SHELL_FILE *fp) 598 { 599 FRESULT res; 600 if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 555 601 return fresult2errno(res); 556 602 } … … 564 610 return -EINVAL; 565 611 566 struct _IO_FILE *fp = fd_to_fp(fd);612 struct SHELL_FILE *fp = fd_to_fp(fd); 567 613 if (fp == NULL) 568 614 return -EBADF; 569 615 570 616 FILINFO fno; 571 #if _USE_LFN572 static char lfn[ _MAX_LFN + 1]; /* Buffer to store the LFN */617 #if FF_USE_LFN 618 static char lfn[FF_MAX_LFN + 1]; /* Buffer to store the LFN */ 573 619 fno.lfname = lfn; 574 620 fno.lfsize = sizeof lfn; 575 621 #endif 576 622 FRESULT res; 577 if ((res = f_readdir(& fp->pdir->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {623 if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') { 578 624 return fresult2errno(res); 579 625 } 580 626 581 627 memset(de, 0, sizeof(*de)); 582 #if _USE_LFN628 #if FF_USE_LFN 583 629 ntlibc_strlcpy(de->d_name, *fno.lfname ? fno.lfname : fno.fname, sizeof(de->d_name)); 584 630 #else … … 589 635 } 590 636 591 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)637 size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 592 638 { 593 639 return -EPERM; 594 640 } 595 641 596 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)642 size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 597 643 { 598 644 return -EPERM; 599 645 } 600 646 601 off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)647 off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 602 648 { 603 649 FRESULT res; … … 607 653 608 654 if (ptr == 0) { 609 if ((res = f_rewinddir(& fp->pdir->dir)) != FR_OK) {655 if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 610 656 return fresult2errno(res); 611 657 } … … 613 659 else { 614 660 FILINFO fno; 615 #if _USE_LFN616 static char lfn[ _MAX_LFN + 1]; /* Buffer to store the LFN */661 #if FF_USE_LFN 662 static char lfn[FF_MAX_LFN + 1]; /* Buffer to store the LFN */ 617 663 fno.lfname = lfn; 618 664 fno.lfsize = sizeof lfn; 619 665 #endif 620 if ((res = f_rewinddir(& fp->pdir->dir)) != FR_OK) {666 if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 621 667 return fresult2errno(res); 622 668 } 623 669 624 670 for (int i = 0; i < ptr; i++) { 625 if ((res = f_readdir(& fp->pdir->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {671 if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') { 626 672 return fresult2errno(res); 627 673 } … … 632 678 } 633 679 634 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)680 int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg) 635 681 { 636 682 return -EINVAL; 683 } 684 685 bool_t dir_readable(struct SHELL_FILE *fp) 686 { 687 return fp->readevt_w != fp->readevt_r; 688 } 689 690 void dir_delete(struct SHELL_FILE *fp) 691 { 692 free((struct SHELL_DIR *)fp->exinf); 693 fp->exinf = NULL; 637 694 } 638 695 … … 687 744 { 688 745 //if ((addr >= (void *)&_HeapBase) && (addr + len < (void *)&_HeapLimit)) { 689 690 691 746 return 0; 747 //} 748 //return -1; 692 749 } 693 750 … … 697 754 static pool_t sys_pool; 698 755 699 void sys_ init(void)756 void sys_tlsf_init(void) 700 757 { 701 758 sys_tlsf = tlsf_create(&_HeapBase);
Note:
See TracChangeset
for help on using the changeset viewer.