int tty_setrtscts(int fd,int enable) { struct termios options; if(tcgetattr(fd,&options)==-1) { LOGPRINTF(1,"%s: tcgetattr() failed", __FUNCTION__); LOGPERROR(1, __FUNCTION__); return(0); } if(enable) { options.c_cflag|=CRTSCTS; } else { options.c_cflag&=~CRTSCTS; } if(tcsetattr(fd,TCSAFLUSH,&options)==-1) { LOGPRINTF(1,"%s: tcsetattr() failed", __FUNCTION__); LOGPERROR(1, __FUNCTION__); return(0); } return(1); }
int tty_read(int fd,char *byte) { fd_set fds; int ret; struct timeval tv; FD_ZERO(&fds); FD_SET(fd,&fds); tv.tv_sec=1; /* timeout after 1 sec */ tv.tv_usec=0; ret=select(fd+1,&fds,NULL,NULL,&tv); if(ret==0) { logprintf(LOG_ERR,"tty_read(): timeout"); return(-1); /* received nothing, bad */ } else if(ret!=1) { LOGPRINTF(1,"tty_read(): select() failed"); LOGPERROR(1,"tty_read()"); return(-1); } if(read(fd,byte,1)!=1) { LOGPRINTF(1,"tty_read(): read() failed"); LOGPERROR(1,"tty_read()"); return(-1); } return(1); }
int bte_connect(void) { struct termios tattr; LOGPRINTF(3, "bte_connect called"); if (hw.fd >= 0) close(hw.fd); do //try block { errno = 0; if ((hw.fd = open(hw.device, O_RDWR | O_NOCTTY)) == -1) { LOGPRINTF(1, "could not open %s", hw.device); LOGPERROR(1, "bte_connect"); break; } if (tcgetattr(hw.fd, &tattr) == -1) { LOGPRINTF(1, "bte_connect: tcgetattr() failed"); LOGPERROR(1, "bte_connect"); break; } LOGPRINTF(1, "opened %s", hw.device); LOGPERROR(1, "bte_connect"); cfmakeraw(&tattr); tattr.c_cc[VMIN] = 1; tattr.c_cc[VTIME] = 0; if (tcsetattr(hw.fd, TCSAFLUSH, &tattr) == -1) { LOGPRINTF(1, "bte_connect: tcsetattr() failed"); LOGPERROR(1, "bte_connect"); break; } if (!tty_setbaud(hw.fd, 115200)) { LOGPRINTF(1, "bte_connect: could not set baud rate %s", hw.device); LOGPERROR(1, "bte_connect"); break; } logprintf(LOG_ERR, "bte_connect: connection established"); io_failed = 0; if (bte_sendcmd("E?", BTE_INIT)) // Ask for echo state just to syncronise { return (1); } else { LOGPRINTF(1, "bte_connect: device did not respond"); } } while (0); //try block failed io_failed = 1; if (hw.fd >= 0) close(hw.fd); if ((hw.fd = open("/dev/zero", O_RDONLY)) == -1) { logprintf(LOG_ERR, "could not open /dev/zero/"); logperror(LOG_ERR, "bte_init()"); } sleep(1); return 0; }
int tty_reset(int fd) { struct termios options; if(tcgetattr(fd,&options)==-1) { LOGPRINTF(1,"tty_reset(): tcgetattr() failed"); LOGPERROR(1,"tty_reset()"); return(0); } cfmakeraw(&options); if(tcsetattr(fd,TCSAFLUSH,&options)==-1) { LOGPRINTF(1,"tty_reset(): tcsetattr() failed"); LOGPERROR(1,"tty_reset()"); return(0); } return(1); }
int tty_setcsize(int fd,int csize) { struct termios options; int size; switch(csize) { case 5: size=CS5; break; case 6: size=CS6; break; case 7: size=CS7; break; case 8: size=CS8; break; default: LOGPRINTF(1,"tty_setcsize(): bad csize rate %d",csize); return(0); } if(tcgetattr(fd, &options)==-1) { LOGPRINTF(1,"tty_setcsize(): tcgetattr() failed"); LOGPERROR(1,"tty_setcsize()"); return(0); } options.c_cflag &= ~CSIZE; options.c_cflag |= size; if(tcsetattr(fd,TCSAFLUSH,&options)==-1) { LOGPRINTF(1,"tty_setcsize(): tcsetattr() failed"); LOGPERROR(1,"tty_setcsize()"); return(0); } return(1); }
int tty_clear(int fd,int rts,int dtr) { int mask; mask=rts ? TIOCM_RTS:0; mask|=dtr ? TIOCM_DTR:0; if(ioctl(fd,TIOCMBIC,&mask)==-1) { LOGPRINTF(1,"tty_clear(): ioctl() failed"); LOGPERROR(1,"tty_clear()"); return(0); } return(1); }
int tty_setdtr(int fd, int enable) { int cmd, sts; #ifdef DEBUG if (ioctl(fd, TIOCMGET, &sts) < 0) { LOGPRINTF(1,"%s: ioctl(TIOCMGET) failed", __FUNCTION__); LOGPERROR(1,__FUNCTION__); return(0); } if (((sts & TIOCM_DTR) == 0) && enable) { LOGPRINTF(1, "%s: 0->1", __FUNCTION__); } else if ((!enable) && (sts & TIOCM_DTR)) { LOGPRINTF(1, "%s: 1->0", __FUNCTION__); } #endif if (enable) { cmd = TIOCMBIS; } else { cmd = TIOCMBIC; } sts = TIOCM_DTR; if (ioctl(fd, cmd, &sts) < 0) { LOGPRINTF(1, "%s: ioctl(TIOCMBI(S|C)) failed", __FUNCTION__); LOGPERROR(1, __FUNCTION__); return(0); } return(1); }
lirc_t audio_readdata(lirc_t timeout) { lirc_t data; int ret; if (!waitfordata((long)timeout)) return 0; ret = read(hw.fd, &data, sizeof(data)); if (ret != sizeof(data)) { LOGPRINTF(1, "error reading from lirc"); LOGPERROR(1, NULL); raise(SIGTERM); return 0; } return (data); }
int tty_write(int fd,char byte) { if(write(fd,&byte,1)!=1) { LOGPRINTF(1,"tty_write(): write() failed"); LOGPERROR(1,"tty_write()"); return(-1); } /* wait until the stop bit of Control Byte is sent (for 9600 baud rate, it takes about 100 msec */ usleep(100*1000); /* we don't wait because tcdrain() does this for us */ /* tcdrain(fd); */ /* but unfortunately this does not seem to be implemented in 2.0.x kernels ... */ return(1); }
lirc_t readdata(void) { lirc_t data; int ret; #if defined(SIM_REC) && !defined(DAEMONIZE) while(1) { unsigned long scan; ret=fscanf(stdin,"space %ld\n",&scan); if(ret==1) { data=(lirc_t) scan; break; } ret=fscanf(stdin,"pulse %ld\n",&scan); if(ret==1) { data=(lirc_t) scan|PULSE_BIT; break; } ret=fscanf(stdin,"%*s\n"); if(ret==EOF) { dosigterm(SIGTERM); } } #else ret=read(hw.fd,&data,sizeof(data)); if(ret!=sizeof(data)) { LOGPRINTF(1,"error reading from lirc"); LOGPERROR(1,NULL); dosigterm(SIGTERM); } #endif return(data); }
int tty_setbaud(int fd,int baud) { struct termios options; int speed; #if defined (__linux__) int use_custom_divisor = 0; struct serial_struct serinfo; #endif switch(baud) { case 300: speed=B300; break; case 1200: speed=B1200; break; case 2400: speed=B2400; break; case 4800: speed=B4800; break; case 9600: speed=B9600; break; case 19200: speed=B19200; break; case 38400: speed=B38400; break; case 57600: speed=B57600; break; case 115200: speed=B115200; break; #ifdef B230400 case 230400: speed=B230400; break; #endif #ifdef B460800 case 460800: speed=B460800; break; #endif #ifdef B500000 case 500000: speed=B500000; break; #endif #ifdef B576000 case 576000: speed=B576000; break; #endif #ifdef B921600 case 921600: speed=B921600; break; #endif #ifdef B1000000 case 1000000: speed=B1000000; break; #endif #ifdef B1152000 case 1152000: speed=B1152000; break; #endif #ifdef B1500000 case 1500000: speed=B1500000; break; #endif #ifdef B2000000 case 2000000: speed=B2000000; break; #endif #ifdef B2500000 case 2500000: speed=B2500000; break; #endif #ifdef B3000000 case 3000000: speed=B3000000; break; #endif #ifdef B3500000 case 3500000: speed=B3500000; break; #endif #ifdef B4000000 case 4000000: speed=B4000000; break; #endif default: #if defined (__linux__) speed=B38400; use_custom_divisor=1; break; #else LOGPRINTF(1,"tty_setbaud(): bad baud rate %d",baud); return(0); #endif } if(tcgetattr(fd, &options)==-1) { LOGPRINTF(1,"tty_setbaud(): tcgetattr() failed"); LOGPERROR(1,"tty_setbaud()"); return(0); } (void) cfsetispeed(&options,speed); (void) cfsetospeed(&options,speed); if(tcsetattr(fd,TCSAFLUSH,&options)==-1) { LOGPRINTF(1,"tty_setbaud(): tcsetattr() failed"); LOGPERROR(1,"tty_setbaud()"); return(0); } #if defined (__linux__) if (use_custom_divisor) { if(ioctl(fd, TIOCGSERIAL, &serinfo) < 0) { LOGPRINTF(1,"tty_setbaud(): TIOCGSERIAL failed"); LOGPERROR(1,"tty_setbaud()"); return(0); } serinfo.flags &= ~ASYNC_SPD_MASK; serinfo.flags |= ASYNC_SPD_CUST; serinfo.custom_divisor = serinfo.baud_base / baud; if(ioctl(fd, TIOCSSERIAL, &serinfo) < 0) { LOGPRINTF(1,"tty_setbaud(): TIOCSSERIAL failed"); LOGPERROR(1,"tty_setbaud()"); return(0); } } #endif return(1); }