/* #define USE_OLD_TTY */ #include #include #include #include #include #ifndef USE_OLD_TTY #include #endif #include #include #include "aplinks.h" #include "disp_ctr.h" static int fd; #ifdef USE_OLD_TTY struct sgttyb org_tbuf; struct sgttyb tbuf; #else struct termios org_tbuf; struct termios tbuf; #endif int set_port_name(int argc, char *argv[], char *port_name) { int i; #ifdef UNIX strcpy(port_name, DEFAULT_PORT); for(i = 1; i < argc; i++){ if(strncmp(argv[i], "-P", 2) == 0){ strcpy(port_name, (argv[i] + 2)); } } #else strcpy(port_name, "COM1(内蔵 DRIVER)"); #endif return 0; } int open_port(char *port_name, unsigned int bps) { if((fd = open(port_name, O_RDWR | O_NONBLOCK , 0)) == -1){ return -1; } else{ #ifndef USE_OLD_TTY if(ioctl(fd, TIOCGETA, &org_tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to get original value)\n"); return -1; } if(ioctl(fd, TIOCGETA, &tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to get value for setting)\n"); return -1; } tbuf.c_ispeed = bps; tbuf.c_ospeed = bps; tbuf.c_lflag = tbuf.c_lflag & ~ICANON; tbuf.c_lflag &= ~TOSTOP; tbuf.c_cflag |= CLOCAL; /* 勉強不足でてきとー(^^; 違っていたら教えてください。 */ #if 0 /* tbuf.c_iflag |= IGNBRK; */ /* 入力の break を無視する。 */ tbuf.c_cflag |= CLOCAL; /* ignore modem status lines */ tbuf.c_oflag &= ~OPOST; /* enable following outpur processing */ tbuf.c_iflag &= ~IXON; /* enable output flow control */ tbuf.c_iflag &= ~IXOFF; /* enable input flow control */ tbuf.c_iflag &= ~IXANY; /* any char will restart after stop */ tbuf.c_cflag = CS8 | CREAD | CLOCAL; tbuf.c_cflag &= ~CSTOPB; tbuf.c_cflag &= ~PARENB; /* やっぱり、このへんの制御を自分でやらないといけないのかしら (^^; */ /* for ON */ /* tbuf.c_cflag |= CCTS_OFLOW; */ /* CTS flow control of output */ /* tbuf.c_cflag |= CRTS_IFLOW; */ /* RTS flow control of input */ /* tbuf.c_cflag |= CDTR_IFLOW; */ /* DTR flow control of input */ /* tbuf.c_cflag |= CDSR_OFLOW; */ /* DSR flow control of output */ /* tbuf.c_cflag |= CCAR_OFLOW; */ /* DCD flow control of output */ /* for OFF */ /* tbuf.c_cflag &= ~CCTS_OFLOW; */ /* CTS flow control of output */ /* tbuf.c_cflag &= ~CRTS_IFLOW; */ /* RTS flow control of input */ /* tbuf.c_cflag &= ~CDTR_IFLOW; */ /* DTR flow control of input */ /* tbuf.c_cflag &= ~CDSR_OFLOW; */ /* DSR flow control of output */ /* tbuf.c_cflag &= ~CCAR_OFLOW; */ /* DCD flow control of output */ #endif if(ioctl(fd, TIOCSETA, &tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to set aplinks value)\n"); exit(0); } /* USE_OLD_TTY(BSD 系?) */ #else if(ioctl(fd, TIOCGETP, &org_tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to get original value)\n"); return -1; } if(ioctl(fd, TIOCGETP, &tbuf) == -1){ c_printf(2, stderr, "ioctl error! (in stage to get value for setting)\n"); return -1; } tbuf.sg_ispeed = 14; /* 19200 bps(EXTA) */ tbuf.sg_ospeed = 14; /* 19200 bps(EXTA) */ if(ioctl(fd, TIOSGETP, &tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to set aplinks value)\n"); #endif return 0; } } int close_port(void) { #ifndef USE_OLD_TTY if(ioctl(fd, TIOCSETA, &org_tbuf) == -1){ c_fprintf(2, stderr, "ioctl error! (in stage to restore original value)\n"); return -1; } #else if(ioctl(fd, TIOCSETP, &org_tbuf) == -1){ c_fprintf(2, "ioctl error! (in stage to restore original value)\n"); return -1; } #endif close(fd); return 0; } unsigned char get_port(void) { unsigned char buf; fd_set readfds; struct timeval timeout; FD_ZERO(&readfds); FD_SET(fd, &readfds); timeout.tv_sec = 1; timeout.tv_usec = 0; select(fd + 1, &readfds, NULL, NULL, &timeout); if(FD_ISSET(fd, &readfds)){ read(fd, &buf, 1); return (unsigned char)buf; } else{ return 0; } /* l = 0; while(1){ if(read(fd, &buf, 1) != -1){ break; } else{ l++; } } return (unsigned char)buf; */ } unsigned char nowait_get_port(void) { char buf; if(read(fd, &buf, 1) == -1){ return 0; } else{ return (unsigned char)buf; } } int put_port(unsigned char data) { int retval; int whatword; whatword = FWRITE; if(ioctl(fd, TIOCFLUSH, &whatword) == -1){ displist_c_printf(2, "TIOCFLUSH error!\n"); } while(1){ if((retval = write(fd, &data, 1)) != -1){ break; } } whatword = FREAD; if(ioctl(fd, TIOCFLUSH, &whatword) == -1){ displist_c_printf(2, "TIOCFLUSH error!\n"); } return retval; } int put_sector(unsigned char *buffer[], unsigned int sector) { int i; /* long l; */ int retval; unsigned char port_buffer[SECTOR_SIZE + 2]; fd_set writefds; struct timeval timeout; /* struct ltchars ltc; struct termios tios; int wait; */ int whatword; /* char data; */ if(buffer[sector] != NULL){ memcpy(port_buffer, buffer[sector], SECTOR_SIZE); if(buffer[sector + 1] != NULL){ port_buffer[SECTOR_SIZE] = *(buffer[sector + 1]); retval = 0; } else{ port_buffer[SECTOR_SIZE] = 0x00; retval = 0; } } else{ bell(); for(i = 0; i <= SECTOR_SIZE; i++){ port_buffer[i] = 0x00; } retval = -1; } whatword = FWRITE; if(ioctl(fd, TIOCFLUSH, &whatword) == -1){ displist_c_printf(2, "TIOCFLUSH error!\n"); } FD_ZERO(&writefds); FD_SET(fd, &writefds); timeout.tv_sec = 0; timeout.tv_usec = 10; select(fd + 1, NULL, &writefds, NULL, &timeout); if(FD_ISSET(fd, &writefds)){ if(write(fd, port_buffer, (SECTOR_SIZE + 1)) != (SECTOR_SIZE + 1)){ c_fprintf(2, stderr, "put_sector write error!\n"); return -1; } } whatword = FREAD; if(ioctl(fd, TIOCFLUSH, &whatword) == -1){ displist_c_printf(2, "TIOCFLUSH error!\n"); } /* if(ioctl(fd, TIOCFLUSH, 0) == -1){ c_fprintf(2, stderr, "putsector ioctl flush error!\n"); exit(0); } */ /* error! */ /* ltc.t_flushc |= FLUSHO; */ /* だめ */ /* if(ioctl(fd, TIOCGETA, &tios) == -1){ c_fprintf(2, stderr, "putsector ioctl get error!\n"); exit(0); } */ /* tios.c_lflag |= FLUSHO; */ /* if(ioctl(fd, TIOCSETA, &tios) == -1){ */ /* if(ioctl(fd, TCSADRAIN) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } */ /* if(ioctl(fd, TIOCMODS, TIOCM_LE | TIOCM_DTR | TIOCM_RTS | TIOCM_CTS | TIOCM_DSR) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } */ /* error! */ /* if(ioctl(fd, TIOCCDTR) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } */ /* if(ioctl(fd, TIOCCBRK) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } */ /* if(ioctl(fd, TIOCPKT, TIOCPKT_FLUSHWRITE) == -1){ c_fprintf(2, stderr, "putsector ioctl pkt error!\n"); exit(0); } */ /* error! */ /* if(ioctl(fd, TIOCGDRAINWAIT, &wait) == -1){ c_fprintf(2, stderr, "putsector ioctl pkt error!\n"); exit(0); } else{ displist_c_printf(4, "wait %d\n", wait); } wait = 10; if(ioctl(fd, TIOCSDRAINWAIT, 0) == -1){ c_fprintf(2, stderr, "putsector ioctl waitset error!\n"); exit(0); } */ /* if(ioctl(fd, TIOCGLTC, <c) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } ltc.t_flushc |= FLUSHO; if(ioctl(fd, TIOCSLTC, <c) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } ltc.t_flushc |= FLUSHO; if(ioctl(fd, TIOCSLTC, <c) == -1){ c_fprintf(2, stderr, "putsector ioctl error!\n"); exit(0); } */ /* for(i = 0; i <= SECTOR_SIZE; i++){ FD_ZERO(&writefds); FD_SET(fd, &writefds); timeout.tv_sec = 1; timeout.tv_usec = 0; select(fd + 1, NULL, &writefds, NULL, &timeout); if(FD_ISSET(fd, &writefds)){ put_port(port_buffer[i]); } } */ /* displist_c_printf(4, "exit putsector\n"); */ return 0; } int get_sector(unsigned char *buffer[], unsigned int sector) { unsigned char port_buffer[SECTOR_SIZE + 3]; int i; for(i = 0; i < SECTOR_SIZE + 2; i++){ port_buffer[i] = get_port(); } if(port_buffer[SECTOR_SIZE + 1] == 0xff){ memcpy(buffer[sector], port_buffer, SECTOR_SIZE); *(buffer[sector + 1]) = port_buffer[SECTOR_SIZE]; put_port(0); } return 0; }