Example #1
0
ssize_t 
NetStream::write_to_net(
   const void *buf, 
   size_t      nbytes
   ) const
{
   set_blocking(true);
#ifdef WIN32
   ssize_t   bytes_written = write_win32(_fd, buf, nbytes);
#else
   ssize_t   bytes_written = ::write(_fd, buf, nbytes);
#endif
   set_blocking(false);

   if (bytes_written < 0 || NET_exception) {
      perror("NetStream::write_to_net: Warning: ");
      NET_exception = 0;
   } else if (bytes_written < (ssize_t)nbytes)
      cerr << "Couldn't flush the buffer.  Some data wasn't written. (nbytes="
           << nbytes << " written=" << bytes_written << ")\n";
   return bytes_written;
}
Example #2
0
ssize_t timed_read(int sockfd, void* buf, size_t count, int timeout_ms)
{
  ssize_t rv;

  set_non_blocking(sockfd);
  do {
    if (0 <= (rv = wait_fd(sockfd, timeout_ms)))
      rv = read(sockfd, buf, count);
  } while (errno == EINTR);
  set_blocking(sockfd);

  return rv;
}
Example #3
0
static BOOL open_sockets(BOOL isdaemon, int port)
{
	/*
	 * The sockets opened here will be used to receive broadcast
	 * packets *only*. Interface specific sockets are opened in
	 * make_subnet() in namedbsubnet.c. Thus we bind to the
	 * address "0.0.0.0". The parameter 'socket address' is
	 * now deprecated.
	 */

	if ( isdaemon )
		ClientNMB = open_socket_in(SOCK_DGRAM, port,
					   0, interpret_addr(lp_socket_address()),
					   True);
	else
		ClientNMB = 0;
  
	ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
					   3, interpret_addr(lp_socket_address()),
					   True);

	if ( ClientNMB == -1 )
		return( False );

#ifndef _XBOX
	/* we are never interested in SIGPIPE */
	BlockSignals(True,SIGPIPE);
#endif

	set_socket_options( ClientNMB,   "SO_BROADCAST" );
	set_socket_options( ClientDGRAM, "SO_BROADCAST" );

	/* Ensure we're non-blocking. */
	set_blocking( ClientNMB, False);
	set_blocking( ClientDGRAM, False);

	DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
	return( True );
}
Example #4
0
File: serial.c Project: PontusO/lc1
/**
 * Opens and initializes a serial port.
 */
int serial_create (char *device)
{
    int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY);
    if (fd < 0) {
        printf("error %d opening '%s', %s\n", errno, device, strerror(errno));
        goto exit;
    }

    set_interface_attribs(fd, B19200, 0);
    set_blocking(fd, 0);
exit:
    return fd;
}
Example #5
0
static NTSTATUS unixdom_listen(struct socket_context *sock,
			       const struct socket_address *my_address, 
			       int queue_size, uint32_t flags)
{
	struct sockaddr_un my_addr;
	int ret;

	/* delete if it already exists */
	if (my_address->addr) {
		unlink(my_address->addr);
	}

	if (my_address->sockaddr) {
		ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
	} else if (my_address->addr == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	} else {
		if (strlen(my_address->addr)+1 > sizeof(my_addr.sun_path)) {
			return NT_STATUS_OBJECT_PATH_INVALID;
		}
		
		
		ZERO_STRUCT(my_addr);
		my_addr.sun_family = AF_UNIX;
		snprintf(my_addr.sun_path, sizeof(my_addr.sun_path), "%s", my_address->addr);

		ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
	}
	if (ret == -1) {
		return unixdom_error(errno);
	}

	if (sock->type == SOCKET_TYPE_STREAM) {
		ret = listen(sock->fd, queue_size);
		if (ret == -1) {
			return unixdom_error(errno);
		}
	}

	if (!(flags & SOCKET_FLAG_BLOCK)) {
		ret = set_blocking(sock->fd, false);
		if (ret == -1) {
			return unixdom_error(errno);
		}
	}

	sock->state = SOCKET_STATE_SERVER_LISTEN;
	sock->private_data = (void *)talloc_strdup(sock, my_address->addr);

	return NT_STATUS_OK;
}
Example #6
0
int main(int argc, char **argv){
    
    char *portname0 = "/dev/ttyUSB1";
    char *portname1 = "/dev/ttyUSB0";

    int fd0 = open(portname0, O_RDWR | O_NOCTTY | O_SYNC);
    
    if (fd0 < 0) {
	fprintf(stderr, "error %d opening %s: %s", errno, portname0, strerror(errno));
	return;
    }

    set_interface_attribs(fd0, B9600, 0); //B115200
    set_blocking(fd0, 0);
    

    	write(fd0, "hello!\n", 7);
	printf("hello!\n");	
        usleep((7+25)*1000);
    close(fd0);

    char buf[100];


    int fd1 = open(portname1, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd1 < 0) {
	fprintf(stderr, "error %d opening %s: %s", errno, portname1, strerror(errno));
	return;
    }
    set_interface_attribs(fd1, B9600, 0);
    set_blocking(fd1, 0);

        int n = read(fd1, buf, sizeof buf);
        printf("%s",buf);

	close(fd1);
    return 0;
}
Example #7
0
void Socket::create(Socket::Handle handle) {
    if (handle_ == priv::SocketImpl::invalid()) {
        int op_value = 1;

        handle_ = handle;

        set_blocking(blocking_);

        if (setsockopt(handle_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&op_value), sizeof(op_value)) == -1) {
            // throw Exception("Failed to set socket option TCP_NODELAY");
            return;
        }
    }
}
Example #8
0
int initializeSerialPort(char *portname) {
  int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
  printf("FD OPEN, fd = %d", fd);
  if (fd < 0)
  {
          printf("error %d opening %s: %s", errno, portname, strerror (errno));
          return -1;
  }

  set_interface_attribs (fd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
  set_blocking (fd, 0);                // set no blocking
  return fd;

}
Example #9
0
int initMW(void)
{
  int fd = open (MINDWAVEPORT, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    fprintf (stderr, "error %d opening %s: %s\n", errno, MINDWAVEPORT,
      strerror (errno));
    exit (1);
  }

  set_interface_attribs (fd, B57600, 0);   // 57,600 bps, 8n1 (no parity)
  set_blocking (fd, 0);                    // set non blocking

  return (fd);
}
Example #10
0
static int Connect (int fd, SA *addr)
{
	int	retval;

	set_non_blocking(fd);
	if ((retval = connect(fd, addr, socklen(addr))))
	{
	    if (errno != EINPROGRESS)
		syserr(-1, "Connect: connect(%d) failed: %s", fd, strerror(errno));
	    else
		retval = 0;
	}
	set_blocking(fd);
	return retval;
}
Example #11
0
int initSerial( int *fd, int baudrate, char *devname )
{
    *fd = open( devname, O_RDWR | O_NOCTTY | O_SYNC );
    if (*fd < 0)
    {
        fprintf(stderr, "! ERROR: Could not open serial port!\n");
        perror("! ERROR Message from system");
        return EXIT_FAILURE;
    }
	
    set_interface_attribs (*fd, baudrate, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (*fd, 0);                // set no blockin  

    return EXIT_SUCCESS;
}
Example #12
0
/*
  note that for simplicity of the API, socket_listen() is also
  use for DGRAM sockets, but in reality only a bind() is done
*/
static NTSTATUS ipv4_listen(struct socket_context *sock,
			    const struct socket_address *my_address, 
			    int queue_size, uint32_t flags)
{
	struct sockaddr_in my_addr;
	struct in_addr ip_addr;
	int ret;

	socket_set_option(sock, "SO_REUSEADDR=1", NULL);

	if (my_address->sockaddr) {
		ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
	} else {
		ip_addr = interpret_addr2(my_address->addr);
		
		ZERO_STRUCT(my_addr);
#ifdef HAVE_SOCK_SIN_LEN
		my_addr.sin_len		= sizeof(my_addr);
#endif
		my_addr.sin_addr.s_addr	= ip_addr.s_addr;
		my_addr.sin_port	= htons(my_address->port);
		my_addr.sin_family	= PF_INET;
		
		ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
	}

	if (ret == -1) {
		return map_nt_error_from_unix_common(errno);
	}

	if (sock->type == SOCKET_TYPE_STREAM) {
		ret = listen(sock->fd, queue_size);
		if (ret == -1) {
			return map_nt_error_from_unix_common(errno);
		}
	}

	if (!(flags & SOCKET_FLAG_BLOCK)) {
		ret = set_blocking(sock->fd, false);
		if (ret == -1) {
			return map_nt_error_from_unix_common(errno);
		}
	}

	sock->state= SOCKET_STATE_SERVER_LISTEN;

	return NT_STATUS_OK;
}
Example #13
0
static NTSTATUS ipv6_tcp_accept(struct socket_context *sock, struct socket_context **new_sock)
{
	struct sockaddr_in6 cli_addr;
	socklen_t cli_addr_len = sizeof(cli_addr);
	int new_fd;
	
	if (sock->type != SOCKET_TYPE_STREAM) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
	if (new_fd == -1) {
		return map_nt_error_from_unix_common(errno);
	}

	if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
		int ret = set_blocking(new_fd, false);
		if (ret == -1) {
			close(new_fd);
			return map_nt_error_from_unix_common(errno);
		}
	}

	/* TODO: we could add a 'accept_check' hook here
	 *	 which get the black/white lists via socket_set_accept_filter()
	 *	 or something like that
	 *	 --metze
	 */

	(*new_sock) = talloc(NULL, struct socket_context);
	if (!(*new_sock)) {
		close(new_fd);
		return NT_STATUS_NO_MEMORY;
	}

	/* copy the socket_context */
	(*new_sock)->type		= sock->type;
	(*new_sock)->state		= SOCKET_STATE_SERVER_CONNECTED;
	(*new_sock)->flags		= sock->flags;

	(*new_sock)->fd			= new_fd;

	(*new_sock)->private_data	= NULL;
	(*new_sock)->ops		= sock->ops;
	(*new_sock)->backend_name	= sock->backend_name;

	return NT_STATUS_OK;
}
Example #14
0
//Open the serial port file descriptor
int openSerial(){
	char *portname = "/dev/ttyMCC";
	
	// Open serial port (file descriptor)
	int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
	if (fd < 0)
	{
			printf("error %d opening %s: %s", errno, portname, strerror (errno));
			return 0;
	}
	
	// Configure the serial communication interface before use
	set_interface_attribs (fd, B115200, 0);		// set speed to 115,200 bps, 8n1 (no parity)
	set_blocking (fd, 0);						// set no blocking
	return fd;
}
Example #15
0
/** sms **/
static void * sms_thread (void * arg) {
	struct smsnode * tmp, * sq = ((struct thrdarg *) arg)->sq;
	char ttyb[64];
	int tty, ttyr, ttyw;

	while (1) {
		sq = sq->next;
		if (sq->status == CRYPTO_SMS_STATUS_HOME) continue;
		if (sq->status == CRYPTO_SMS_STATUS_SENT) {
			printf ("sms sent. leaving queue %s\n", sq->number);
			tmp = sq;
			sq->prev->next = sq->next;
			sq->next->prev = sq->prev;
			sq = sq->prev;
			free (tmp->key);
			free (tmp->number);
			free (tmp);
			}
		if (sq->status == CRYPTO_SMS_STATUS_PENDING) {
			printf ("sending sms key %s\nto number %s\n", sq->key, sq->number);
			tty = open ("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
			if (tty < 0) continue;
			set_serial (tty, B2400, 0);
			set_blocking (tty, B9600, 0);


			ttyw = write (tty, "AT+CMGF=1\r\n", 11);
			printf ("ttyw=%d\n", ttyw);
			sleep (1);
			sprintf (ttyb, "AT+CMGS=\"%s\"\r%s%c", sq->number, sq->key, (char) 26);
			ttyw = write (tty, ttyb, strlen(ttyb));
			printf ("ttyb=%s,\n", ttyb);
			printf ("ttyw=%d\n", ttyw);
			sleep (1);
			close (tty);

			sq->status = CRYPTO_SMS_STATUS_SENT;
			}
		if (sq->status == CRYPTO_SMS_STATUS_ERROR) {
			tmp = sq;
			sq->prev->next = sq->next;
			sq->next->prev = sq->prev;
			sq = sq->prev;
			free (tmp);
			}
		}
	}
Example #16
0
_PUBLIC_ NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socket_ops *ops,
					 struct socket_context **new_sock, 
					 enum socket_type type, uint32_t flags)
{
	NTSTATUS status;

	(*new_sock) = talloc(mem_ctx, struct socket_context);
	if (!(*new_sock)) {
		return NT_STATUS_NO_MEMORY;
	}

	(*new_sock)->type = type;
	(*new_sock)->state = SOCKET_STATE_UNDEFINED;
	(*new_sock)->flags = flags;

	(*new_sock)->fd = -1;

	(*new_sock)->private_data = NULL;
	(*new_sock)->ops = ops;
	(*new_sock)->backend_name = NULL;

	status = (*new_sock)->ops->fn_init((*new_sock));
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(*new_sock);
		return status;
	}

	/* by enabling "testnonblock" mode, all socket receive and
	   send calls on non-blocking sockets will randomly recv/send
	   less data than requested */

	if (!(flags & SOCKET_FLAG_BLOCK) &&
	    type == SOCKET_TYPE_STREAM &&
		getenv("SOCKET_TESTNONBLOCK") != NULL) {
		(*new_sock)->flags |= SOCKET_FLAG_TESTNONBLOCK;
	}

	/* we don't do a connect() on dgram sockets, so need to set
	   non-blocking at socket create time */
	if (!(flags & SOCKET_FLAG_BLOCK) && type == SOCKET_TYPE_DGRAM) {
		set_blocking(socket_get_fd(*new_sock), false);
	}

	talloc_set_destructor(*new_sock, socket_destructor);

	return NT_STATUS_OK;
}
int openPort(char* portname, int speed, int vmin, int vtime)
{
	printf("trying to open port %s\n", portname);

	int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);

	if (fd < 0)
	{
	        printf("error %d opening %s: %s\n", errno, portname, strerror (errno));
	        return;
	}

	set_interface_attribs (fd, speed, 0);  
	set_blocking (fd, vmin, vtime);

	return fd;
}
Example #18
0
static int read_from_sd ( int sd )
{
   char buf[100];
   int n;

   set_blocking(sd);
   n = read(sd, buf, 99);
   if (n <= 0) return 0; /* closed */
   copyout(buf, n);

   set_nonblocking(sd);
   while (1) {
      n = read(sd, buf, 100);
      if (n <= 0) return 1; /* not closed */
      copyout(buf, n);
   }
}
int main(void) {
	char *portname = "/dev/ttymxc3";
	
	int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
	if (fd < 0)
	{
			printf("error %d opening %s: %s", errno, portname, strerror (errno));
			return;
	}

	set_interface_attribs (fd, B115200, 0);		// set speed to 115,200 bps, 8n1 (no parity)
	set_blocking (fd, 0);						// set no blocking
	
	char buf [7];
	int n;

	char buffON[2] = {13,1};
	write(fd, buffON, 2);           	// send 2 character greeting
	
	sleep(1);							// delay 1 second 
	
	n = read (fd, buf, sizeof buf); 	// read up to 7 characters if ready to read
	if (n > 0) {
		//printf("quanti: %d\n", n);
		int i;
		for (i = 0; i < n; i++) {
			printf("%c",buf[i]);
		}
	}
	
	char buffOFF[2] = {13,0};
	write(fd, buffOFF, 2); 
	
	sleep(1);							// delay 1 second 
	
	n = read (fd, buf, sizeof buf); 	// read up to 7 characters if ready to read
	if (n > 0) {
		//printf("quanti: %d\n", n);
		int i;
		for (i = 0; i < n; i++) {
			printf("%c",buf[i]);
		}
	}
	  
}
Example #20
0
static NTSTATUS unixdom_accept(struct socket_context *sock, 
			       struct socket_context **new_sock)
{
	struct sockaddr_un cli_addr;
	socklen_t cli_addr_len = sizeof(cli_addr);
	int new_fd;

	if (sock->type != SOCKET_TYPE_STREAM) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
	if (new_fd == -1) {
		return unixdom_error(errno);
	}

	if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
		int ret = set_blocking(new_fd, false);
		if (ret == -1) {
			close(new_fd);
			return map_nt_error_from_unix_common(errno);
		}
	}

	smb_set_close_on_exec(new_fd);

	(*new_sock) = talloc(NULL, struct socket_context);
	if (!(*new_sock)) {
		close(new_fd);
		return NT_STATUS_NO_MEMORY;
	}

	/* copy the socket_context */
	(*new_sock)->type		= sock->type;
	(*new_sock)->state		= SOCKET_STATE_SERVER_CONNECTED;
	(*new_sock)->flags		= sock->flags;

	(*new_sock)->fd			= new_fd;

	(*new_sock)->private_data	= NULL;
	(*new_sock)->ops		= sock->ops;
	(*new_sock)->backend_name	= sock->backend_name;

	return NT_STATUS_OK;
}
Example #21
0
int main(int argc, char **argv) {
	fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
	if (fd < 0)
	{
			printf("error %d opening %s: %s", errno, portname, strerror (errno));
			return;
	}

	set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
	set_blocking (fd, 0);                // set no blocking

	if(argc == 2){
		if(strcmp(argv[1], "red") == 0)
			sendcmd(254, 0, 0);
		else if(strcmp(argv[1], "green")== 0)
			sendcmd(0, 254, 0);
		else if(strcmp(argv[1], "blue")== 0)
			sendcmd(0, 0, 254);
		else if(strcmp(argv[1], "yellow")== 0)
			sendcmd(254, 254, 0);
		else if(strcmp(argv[1], "pink")== 0)
			sendcmd(254, 0, 254);
		else if(strcmp(argv[1], "cyan")== 0)
			sendcmd(0, 254, 254);
		else if(strcmp(argv[1], "black")== 0)
			sendcmd(0, 0, 0);
		else if(strcmp(argv[1], "white")== 0)
			sendcmd(254, 254, 254);
		else
			printf("\nUnknown argument\n");
	}
	else if(argc == 4){
		sendcmd(atoi(argv[0]), atoi(argv[1]),atoi(argv[2]));
	}
	else{	
		printf("GionjiLed\n");
		printf("Usage:\n");
		printf("gionjiled [red | green | blue | yellow | pink | cyan | white | black]\n");
		printf("gionjiled R G B [from 0 to 254]\n");
		printf("Enjoy");
	}

	
	
}
Example #22
0
static NTSTATUS get_ldapi_ctx(TALLOC_CTX *mem_ctx, struct tldap_context **pld)
{
	struct tldap_context *ld;
	struct sockaddr_un addr;
	char *sockaddr;
	int fd;
	NTSTATUS status;
	int res;

	sockaddr = talloc_asprintf(talloc_tos(), "/%s/ldap_priv/ldapi",
				   lp_private_dir());
	if (sockaddr == NULL) {
		DEBUG(10, ("talloc failed\n"));
		return NT_STATUS_NO_MEMORY;
	}

	ZERO_STRUCT(addr);
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, sockaddr, sizeof(addr.sun_path));
	TALLOC_FREE(sockaddr);

	status = open_socket_out((struct sockaddr_storage *)(void *)&addr,
				 0, 0, &fd);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(10, ("Could not connect to %s: %s\n", addr.sun_path,
			   nt_errstr(status)));
		return status;
	}
	set_blocking(fd, false);

	ld = tldap_context_create(mem_ctx, fd);
	if (ld == NULL) {
		close(fd);
		return NT_STATUS_NO_MEMORY;
	}
	res = tldap_fetch_rootdse(ld);
	if (res != TLDAP_SUCCESS) {
		DEBUG(10, ("tldap_fetch_rootdse failed: %s\n",
			   tldap_errstr(talloc_tos(), ld, res)));
		TALLOC_FREE(ld);
		return NT_STATUS_LDAP(res);
	}
	*pld = ld;
	return NT_STATUS_OK;;
}
Example #23
0
pid_t piped_child_http(char **command)
{
    pid_t pid;
    pid = do_fork();
    if (pid == -1) {
        rsyserr(FERROR, errno, "fork");
        exit_cleanup(RERR_IPC);
    }

    if (pid == 0) {
        set_blocking(STDIN_FILENO);
        execvp(command[0], command);
        rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
        exit_cleanup(RERR_IPC);
    }

    return pid;
}
// Initialise serial cable and run one time setup
bool application_init(void)
{

    char *portname = "/dev/ttyUSB0";

    fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
        NABTO_LOG_INFO(("error %d opening %s: %s\n", errno, portname, strerror (errno)));
        return 0;
    }
    else {
        NABTO_LOG_INFO(("Opening %s: %s\n", portname, strerror (errno)));
    }

    set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (fd, 0);                // set no blocking

    // Wake Roomba
    setRTS(fd, 0);
    usleep(100000); //0.1 sec
    setRTS(fd, 1);
    sleep(2);

    // Songs can be defined while in passive mode (which we are in right now)
    char DSB[] = {140, 0, 3, 74, 40, 75, 20, 70, 32};
    write(fd, &DSB, sizeof(DSB));
    usleep ((sizeof(DSB)+25) * 100);

    char reverse[] = {140, 1, 6, 84, 32, 30, 32, 84, 32, 30, 32, 84, 32, 30, 32};
    write(fd, &reverse, sizeof(reverse));
    usleep ((sizeof(reverse)+25) * 100);

    char car_low[] = {140, 2, 12, 74,10, 71,10, 67,10, 67,10, 67,10, 69,10, 71,10, 72,10, 74,10, 74,10, 74,10, 71,10};
    write(fd, &car_low, sizeof(car_low));
    usleep ((sizeof(car_low)+25) * 100);

    char car_high[] = {140, 3, 12, 86,10, 83,10, 79,10, 79,10, 79,10, 81,10, 83,10, 84,10, 86,10, 86,10, 86,10, 83,10};
    write(fd, &car_high, sizeof(car_high));
    usleep ((sizeof(car_high)+25) * 100);

    return 1;
}
Example #25
0
void
client_connection_cb(int sd, short type, void *arg)
{
    int nsd;

    if ((nsd = accept(sd, NULL, NULL)) < 0) {
        if (errno == EWOULDBLOCK || errno == ECONNABORTED)
            return;
        else
            btpd_err("client accept: %s\n", strerror(errno));
    }

    if ((errno = set_blocking(nsd)) != 0)
        btpd_err("set_blocking: %s.\n", strerror(errno));

    struct cli *cli = btpd_calloc(1, sizeof(*cli));
    cli->sd = nsd;
    btpd_ev_new(&cli->read, cli->sd, EV_READ, cli_read_cb, cli);
}
Example #26
0
int
read_port()
{
char *portname = "/dev/ttyACM0";

int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
        printf("error %d opening %s: %s", errno, portname, strerror (errno));
        return(0);
}

set_interface_attribs (fd, B9600, 0);  // set speed to 9600 bps, 8n1 (no parity)
set_blocking (fd, 0);                // set no blocking

write (fd, "hello!\n", 7);           // send 7 character greeting
char buf [100];
int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
printf("data is:",fd);
}
Example #27
0
File: ethos.c Project: drmrboy/RIOT
int _open_serial_connection(char *name, char *baudrate_arg)
{
    unsigned baudrate = 0;
    if (_parse_baudrate(baudrate_arg, &baudrate) == -1) {
        fprintf(stderr, "Invalid baudrate specified: %s\n", baudrate_arg);
        return 1;
    }

    int serial_fd = open(name, O_RDWR | O_NOCTTY | O_SYNC);

    if (serial_fd < 0) {
        fprintf(stderr, "Error opening serial device %s\n", name);
        return -1;
    }

    set_serial_attribs(serial_fd, baudrate, 0);
    set_blocking(serial_fd, 1);

    return serial_fd;
}
int KeypadController::setup()
{
    fd = open(keypadname.toLocal8Bit().data(), O_RDWR); //Sets FD to open said port

    if (fd < 0) //If Statement produces error message if selected port cannot be opened
    {
        qDebug() << "error" << errno << " opening " << keypadname << strerror(errno);
        return 1;
    }

    set_interface_attribs(B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking(0);

    // Set up Ports on PIO
    sendToKeypad("@00D000\r"); // Sets Port A to output
//    qDebug() << response;
    sendToKeypad("@00D1FF\r"); // Sets Port B to input
//    qDebug() << response;
    sendToKeypad("@00D200\r"); // Sets Port C to output
//    qDebug() << response;
    return 0;
}
int serial_init(int* fd,const char* seriale_dev)
{


    /* apro la porta seriale*/
    const char * portname = seriale_dev;

     *fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (*fd < 0)
    {
        printf("error %d opening %s: %s", errno, portname, strerror (errno));
        return -1;
    }
    /*imposto baud rate*/
    set_interface_attribs (*fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (*fd, 0);




    return 1;
}
Example #30
0
int main() {
	rs232_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (rs232_fd < 0) {
		fprintf(stderr, "RS232 error\n");
		exit(1);
	}
	set_interface_attribs(rs232_fd, B115200, 0);
	set_blocking(rs232_fd, 0);

	write(rs232_fd, "PCready\n", 8);
	printf("READY\n");
	while(1) {
		int array[2];
		array[0] = 0, array[1] = 1;
		toggle_sound(array, 2, 1);
		usleep(10000000);
	}
	/*
	   while(1) {
	   fprintf(rs232, "PC ready\n");
	   fgets(buffer, 10000, rs232);
	   if (strcmp(buffer, "DE2 ready") == 0) {
	   break;
	   }
	   }
	   */
	/*
	   int mode;
	   scanf("%d", &n);
	   switch (mode) {
	   case 1:
	   break;
	   case 2:
	   break;
	   }
	   */
	return 0;
}