コード例 #1
0
/* Restore the TTY to its previous state. */
static int tty_restore(void)
{
	int ret;
	struct termios tty;

	tty = tty_before;
  	(void) tty_set_speed(&tty, "0");
	if ((ret = tty_set_state(&tty)) < 0) {
		fprintf(stderr, "slattach: tty_restore: %s\n",
			strerror(-ret));
		return ret;
	}
  	
	return 0;
}
コード例 #2
0
ファイル: slattach.c プロジェクト: ashishtanwer/net-tools
/* Hangup the line. */
static int
tty_hangup(void)
{
  struct termios tty;

  tty = tty_current;
  (void) tty_set_speed(&tty, "0");
  if (tty_set_state(&tty) < 0) {
	if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(DROP): %s\n"), strerror(errno));
	return(-errno);
  }

  (void) sleep(1);

  if (tty_set_state(&tty_current) < 0) {
	if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(RAISE): %s\n"), strerror(errno));
	return(-errno);
  }
  return(0);
}
コード例 #3
0
ファイル: main.c プロジェクト: pkuosadevelopers/smart_home
void pthread_receive_from_tty()
{
	int fd;
	int nread;
	int i = 0;
	char buff[500];
	char *pbuf;
	char *dev = "/dev/ttyUSB0";
	int baud = 9600;

	DBG("\ninit tty begins\n");
	fd = tty_open_dev(dev);
	if(fd == -1)
	{
		DBG("\nOpen dev failed\n");
		exit(-1);
	}
	DBG("\nopen success\n");

	if(tty_set_speed(fd, baud) != 0)
	{
		DBG("\nSet speed failed\n");
		exit(-1);
	}
	DBG("\nset speed success\n");

	if(tty_set_parity(fd, 8, 1, 'N') != 0)
	{
		DBG("\nSet parity failed\n");
	}
	DBG("\nset parity success\n");

	_tty_fd = fd;

	while(1)
	{
		pbuf = buff;		//reset the pbuf point to the beginning of buff
		int n;
		while(1)
		{
			n = read(fd, pbuf, 1);
			if(*pbuf++ == '$')
			{
				*pbuf = '\0';
				break;
			}
		}
		printf("%s, the size is %ld\n", buff, pbuf - buff);

		if(is_ack_msg(buff, pbuf - buff))	//if this is an ACK message, read another message 
		{
			send_sockfd = find_client_to_send(head_message, buff, strlen(buff));
			pbuf = buff;		//reset the pbuf point to the beginning of buff
			while(1)
			{
				read(fd, pbuf, 1);
				if(*pbuf++ == '$')
				{
					*pbuf = '\0';
					break;
				}
			}
			//send to client
			int vv = client_send(send_sockfd, buff, strlen(buff));

			printf("second time: %s, the size is %ld\n", buff, pbuf - buff);
			printf("second time: %d, the send_sockfd is %d\n", vv, send_sockfd);

			pthread_mutex_lock(&mutex_head_message);	//lock the gloable variable head_message
			message_delete(head_message, send_sockfd);
			pthread_mutex_unlock(&mutex_head_message);	//unlock the gloable variable head_message
		}
	}
}
コード例 #4
0
/* 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;

	/* 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;
}
コード例 #5
0
ファイル: slattach.c プロジェクト: ashishtanwer/net-tools
/* Open and initialize a terminal line. */
static int
tty_open(char *name, const char *speed)
{
  char pathbuf[PATH_MAX];
  register char *path_open, *path_lock;
  int fd;

  /* Try opening the TTY device. */
  if (name != NULL) {
	if (name[0] != '/') {
		if (strlen(name + 6) > sizeof(pathbuf)) {
			if (opt_q == 0) fprintf(stderr,
				_("slattach: 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;
	}
	if (opt_d) printf("slattach: tty_open: looking for lock\n");
	if (tty_lock(path_lock, 1)) return(-1); /* can we lock the device? */
	if (opt_d) printf("slattach: tty_open: trying to open %s\n", path_open);
	if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) {
		if (opt_q == 0) fprintf(stderr,
			"slattach: tty_open(%s, RW): %s\n",
					path_open, strerror(errno));
		return(-errno);
	}
	tty_fd = fd;
	if (opt_d) printf("slattach: tty_open: %s (fd=%d)\n", path_open, fd);
	if (!strcmp(path_open, _PATH_DEVPTMX)) {
		if (opt_d) printf("slattach: tty_open: trying to grantpt and unlockpt\n");
		if (grantpt(fd) < 0) {
		    if (opt_q == 0) fprintf(stderr,
			    "slattach: tty_open: grantpt: %s\n", strerror(errno));
		    return(-errno);
		}
		if (unlockpt(fd) < 0) {
		    if (opt_q == 0) fprintf(stderr,
			    "slattach: tty_open: unlockpt: %s\n", strerror(errno));
		    return(-errno);
		}
		path_pts = ptsname(fd);
		if (path_pts == NULL) {
		    if (opt_q == 0) fprintf(stderr,
			    "slattach: tty_open: ptsname: %s\n", strerror(errno));
		    return(-errno);
		}
		if (opt_d) printf("slattach: tty_open: %s: slave pseudo-terminal is %s\n",
				  path_open, path_pts);
	}
  } else {
	tty_fd = 0;
  }

  /* Fetch the current state of the terminal. */
  if (tty_get_state(&tty_saved) < 0) {
	if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current state!\n"));
	return(-errno);
  }
  tty_current = tty_saved;

  /* Fetch the current line discipline of this terminal. */
  if (tty_get_disc(&tty_sdisc) < 0) {
	if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current line disc!\n"));
	return(-errno);
  }
  tty_ldisc = tty_sdisc;

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

	/* Set the default speed if we need to. */
	if (speed != NULL) {
		if (tty_set_speed(&tty_current, speed) != 0) {
			if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set %s bps!\n"),
						speed);
			return(-errno);
		}
	}

	/* Set up a completely 8-bit clean line. */
	if (tty_set_databits(&tty_current, "8") ||
	    tty_set_stopbits(&tty_current, "1") ||
	    tty_set_parity(&tty_current, "N")) {
		if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set 8N1 mode!\n"));
		return(-errno);
  	}

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

  /* OK, line is open.  Do we need to "silence" it? */
  (void) tty_nomesg(tty_fd);

  return(0);
}