mesh_driver/lunix-attach.c
static int tty_open(char *name)
{
	int fd;
	int ret;
	int saved_errno;
	char pathbuf[PATH_MAX];
	register char *path_open, *path_lock;

	/* Try opening the TTY device. */
	if (name != NULL) {
		if (name[0] != '/') {
			if (strlen(name + 6) > sizeof(pathbuf)) {
				fprintf(stderr, "tty name too long\n");
				return -1;
			}
			sprintf(pathbuf, "/dev/%s", name);
			path_open = pathbuf;
			path_lock = name;
		} else if (!strncmp(name, "/dev/", 5)) {
			path_open = name;
			path_lock = name + 5;
		} else {
			path_open = name;
			path_lock = name;
		}
	
		fprintf(stderr, "tty_open: looking for lock\n");
		if (tty_lock(path_lock, 1))
			return -1 ; /* can we lock the device? */
		fprintf(stderr, "tty_open: trying to open %s\n",
			path_open);
		if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) {
			saved_errno = errno;
			fprintf(stderr, "tty_open(%s, RW): %s\n",
				path_open, strerror(errno));
			return -saved_errno;
		}
		tty_fd = fd;
		fprintf(stderr, "tty_open: %s (fd=%d) ", path_open, fd);
  	} else {
		tty_fd = 0;
	}

	/* Fetch the current state of the terminal. */
	if (tty_get_state(&tty_before) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot get current state\n");
		return -saved_errno;
	}
	tty_current = tty_before;
	
	/* Fetch the current line discipline of this terminal. */
	if (tty_get_ldisc(&ldisc_before) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot get current line disc\n");
		return -saved_errno;
	}

	/* Put this terminal line in a 8-bit transparent mode. */
	if (tty_set_raw(&tty_current) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot set RAW mode\n");
		return -saved_errno;
	}

	/**************************************************
	 * The sensor needs to be setup at
	 * 57600bps, 8 data bits, No parity, 1 stop bit:
	 **************************************************
	 */
	if (tty_set_speed(&tty_current, "57600") != 0) {
			saved_errno = errno;
			fprintf(stderr, "tty_open: cannot set data rate to 57600bps\n");
			return -saved_errno;
	}
	if (tty_set_databits(&tty_current, "8") ||
	    tty_set_stopbits(&tty_current, "1") ||
	    tty_set_parity(&tty_current, "N")) {
	    	saved_errno = errno;
		fprintf(stderr, "tty_open: cannot set 8N1 mode\n");
		return -saved_errno;
  	};

	/* Set the new line mode. */
	if ((ret = tty_set_state(&tty_current)) < 0)
		return ret;

	/* And activate the new line discipline */
	if ((ret = tty_set_ldisc(N_LUNIX_LDISC)) < 0)
		return ret;
		
	return 0;
}
/* Open and initialize a terminal line. */
Open and initialize a terminal line.
Open and initialize a terminal line .
static int tty_open(char *name)
{
	int fd;
	int ret;
	int saved_errno;
	char pathbuf[PATH_MAX];
	register char *path_open, *path_lock;
	if (name != NULL) {
		if (name[0] != '/') {
			if (strlen(name + 6) > sizeof(pathbuf)) {
				fprintf(stderr, "tty name too long\n");
				return -1;
			}
			sprintf(pathbuf, "/dev/%s", name);
			path_open = pathbuf;
			path_lock = name;
		} else if (!strncmp(name, "/dev/", 5)) {
			path_open = name;
			path_lock = name + 5;
		} else {
			path_open = name;
			path_lock = name;
		}
		fprintf(stderr, "tty_open: looking for lock\n");
		if (tty_lock(path_lock, 1))
			return -1 ; 
		fprintf(stderr, "tty_open: trying to open %s\n",
			path_open);
		if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) {
			saved_errno = errno;
			fprintf(stderr, "tty_open(%s, RW): %s\n",
				path_open, strerror(errno));
			return -saved_errno;
		}
		tty_fd = fd;
		fprintf(stderr, "tty_open: %s (fd=%d) ", path_open, fd);
  	} else {
		tty_fd = 0;
	}
	if (tty_get_state(&tty_before) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot get current state\n");
		return -saved_errno;
	}
	tty_current = tty_before;
	if (tty_get_ldisc(&ldisc_before) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot get current line disc\n");
		return -saved_errno;
	}
	if (tty_set_raw(&tty_current) < 0) {
		saved_errno = errno;
		fprintf(stderr, "tty_open: cannot set RAW mode\n");
		return -saved_errno;
	}
	if (tty_set_speed(&tty_current, "57600") != 0) {
			saved_errno = errno;
			fprintf(stderr, "tty_open: cannot set data rate to 57600bps\n");
			return -saved_errno;
	}
	if (tty_set_databits(&tty_current, "8") ||
	    tty_set_stopbits(&tty_current, "1") ||
	    tty_set_parity(&tty_current, "N")) {
	    	saved_errno = errno;
		fprintf(stderr, "tty_open: cannot set 8N1 mode\n");
		return -saved_errno;
  	};
	if ((ret = tty_set_state(&tty_current)) < 0)
		return ret;
	if ((ret = tty_set_ldisc(N_LUNIX_LDISC)) < 0)
		return ret;
	return 0;
}
static int tty_open ( char * name ) { int fd ; int ret ; int saved_errno ; char pathbuf [ PATH_MAX ] ; register char * path_open , * path_lock ; if ( name != NULL ) { if ( name [ 0 ] != ' ' ) { if ( strlen ( name + 6 ) > sizeof ( pathbuf ) ) { fprintf ( stderr , " \n " ) ; return -1 ; } sprintf ( pathbuf , " " , name ) ; path_open = pathbuf ; path_lock = name ; } else if ( ! strncmp ( name , " " , 5 ) ) { path_open = name ; path_lock = name + 5 ; } else { path_open = name ; path_lock = name ; } fprintf ( stderr , " \n " ) ; if ( tty_lock ( path_lock , 1 ) ) return -1 ; fprintf ( stderr , " \n " , path_open ) ; if ( ( fd = open ( path_open , O_RDWR | O_NDELAY ) ) < 0 ) { saved_errno = errno ; fprintf ( stderr , " \n " , path_open , strerror ( errno ) ) ; return - saved_errno ; } tty_fd = fd ; fprintf ( stderr , " " , path_open , fd ) ; } else { tty_fd = 0 ; } if ( tty_get_state ( & tty_before ) < 0 ) { saved_errno = errno ; fprintf ( stderr , " \n " ) ; return - saved_errno ; } tty_current = tty_before ; if ( tty_get_ldisc ( & ldisc_before ) < 0 ) { saved_errno = errno ; fprintf ( stderr , " \n " ) ; return - saved_errno ; } if ( tty_set_raw ( & tty_current ) < 0 ) { saved_errno = errno ; fprintf ( stderr , " \n " ) ; return - saved_errno ; } if ( tty_set_speed ( & tty_current , " " ) != 0 ) { saved_errno = errno ; fprintf ( stderr , " \n " ) ; return - saved_errno ; } if ( tty_set_databits ( & tty_current , " " ) || tty_set_stopbits ( & tty_current , " " ) || tty_set_parity ( & tty_current , " " ) ) { saved_errno = errno ; fprintf ( stderr , " \n " ) ; return - saved_errno ; } ; if ( ( ret = tty_set_state ( & tty_current ) ) < 0 ) return ret ; if ( ( ret = tty_set_ldisc ( N_LUNIX_LDISC ) ) < 0 ) return ret ; return 0 ; }
Open and initialize a terminal line.
Open and initialize a terminal line .
/* Try opening the TTY device. */ /* can we lock the device? */ /* Fetch the current state of the terminal. */ /* Fetch the current line discipline of this terminal. */ /* Put this terminal line in a 8-bit transparent mode. */ /**************************************************
	 * The sensor needs to be setup at
	 * 57600bps, 8 data bits, No parity, 1 stop bit:
	 **************************************************
	 */ /* Set the new line mode. */ /* And activate the new line discipline */