void device_changespeed(int speed, struct gn_statemachine *state) { switch (state->device.type) { case GN_CT_DKU2: case GN_CT_Serial: case GN_CT_Infrared: dprintf("Serial device: setting speed to %d\n", speed); serial_changespeed(state->device.fd, speed, state); break; case GN_CT_Tekram: dprintf("Serial device: setting speed to %d\n", speed); tekram_changespeed(state->device.fd, speed, state); break; case GN_CT_Irda: case GN_CT_Bluetooth: case GN_CT_TCP: case GN_CT_DKU2LIBUSB: case GN_CT_SOCKETPHONET: default: break; } }
/* * Open a device with standard options. * Use value (-1) for "with_hw_handshake" if its specification is required from the user. */ int serial_opendevice(const char *file, int with_odd_parity, int with_async, int with_hw_handshake, struct gn_statemachine *state) { int fd; int retcode; struct termios tp; /* Open device */ /* * O_NONBLOCK MUST be used here as the CLOCAL may be currently off * and if DCD is down the "open" syscall would be stuck waiting for DCD. */ fd = serial_open(file, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) return fd; /* Initialise the port settings */ memcpy(&tp, &serial_termios, sizeof(struct termios)); /* Set port settings for canonical input processing */ tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL; if (with_odd_parity) { tp.c_cflag |= (PARENB | PARODD); tp.c_iflag = 0; } else tp.c_iflag = IGNPAR; #ifdef CRTSCTS if (state->config.hardware_handshake) tp.c_cflag |= CRTSCTS; else tp.c_cflag &= ~CRTSCTS; #endif tp.c_oflag = 0; tp.c_lflag = 0; tp.c_cc[VMIN] = 1; tp.c_cc[VTIME] = 0; retcode = tcflush(fd, TCIFLUSH); if (retcode == -1) { perror("Gnokii serial_opendevice: tcflush"); serial_close(fd, state); return -1; } retcode = tcsetattr(fd, TCSANOW, &tp); if (retcode == -1) { perror("Gnokii serial_opendevice: tcsetattr"); serial_close(fd, state); return -1; } if (serial_changespeed(fd, state->config.serial_baudrate, state) != GN_ERR_NONE) serial_changespeed(fd, 19200 /* default value */, state); #if !(__unices__) /* Allow process/thread to receive SIGIO */ retcode = fcntl(fd, F_SETOWN, getpid()); if (retcode == -1) { perror("Gnokii serial_opendevice: fcntl(F_SETOWN)"); serial_close(fd, state); return -1; } #endif /* Make filedescriptor asynchronous. */ if (with_async) { /* * We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset * by F_SETFL as a side-effect! */ #ifdef FNONBLOCK retcode = fcntl(fd, F_SETFL, (with_async ? FASYNC : 0) | FNONBLOCK); #else # ifdef FASYNC retcode = fcntl(fd, F_SETFL, (with_async ? FASYNC : 0) | O_NONBLOCK); # else retcode = fcntl(fd, F_SETFL, O_NONBLOCK); if (retcode != -1) retcode = ioctl(fd, FIOASYNC, &with_async); # endif #endif if (retcode == -1) { perror("Gnokii serial_opendevice: fcntl(F_SETFL)"); serial_close(fd, state); return -1; } } return fd; }