extern int telnet_main(int argc, char** argv)
{
	int len;
	struct sockaddr_in s_in;
#ifdef USE_POLL
	struct pollfd ufds[2];
#else
	fd_set readfds;
	int maxfd;
#endif

#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
	int opt;
#endif

#ifdef CONFIG_FEATURE_AUTOWIDTH
	get_terminal_width_height(0, &win_width, &win_height);
#endif

#ifdef CONFIG_FEATURE_TELNET_TTYPE
    ttype = getenv("TERM");
#endif

	memset(&G, 0, sizeof G);

	if (tcgetattr(0, &G.termios_def) < 0)
		exit(1);

	G.termios_raw = G.termios_def;
	cfmakeraw(&G.termios_raw);

	if (argc < 2)
		bb_show_usage();

#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
	autologin = NULL;
	while ((opt = getopt(argc, argv, "al:")) != EOF) {
		switch (opt) {
			case 'l':
				autologin = optarg;
				break;
			case 'a':
				autologin = getenv("USER");
				break;
			case '?':
				bb_show_usage();
				break;
		}
	}
	if (optind < argc) {
		bb_lookup_host(&s_in, argv[optind++]);
		s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
				"telnet", "tcp", 23);
		if (optind < argc)
			bb_show_usage();
	} else
		bb_show_usage();
#else
	bb_lookup_host(&s_in, argv[1]);
	s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
#endif

	G.netfd = xconnect(&s_in);

	setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);

	signal(SIGINT, fgotsig);

#ifdef USE_POLL
	ufds[0].fd = 0; ufds[1].fd = G.netfd;
	ufds[0].events = ufds[1].events = POLLIN;
#else
	FD_ZERO(&readfds);
	FD_SET(0, &readfds);
	FD_SET(G.netfd, &readfds);
	maxfd = G.netfd + 1;
#endif

	while (1)
	{
#ifndef USE_POLL
		fd_set rfds = readfds;

		switch (select(maxfd, &rfds, NULL, NULL, NULL))
#else
		switch (poll(ufds, 2, -1))
#endif
		{
		case 0:
			/* timeout */
		case -1:
			/* error, ignore and/or log something, bay go to loop */
			if (G.gotsig)
				conescape();
			else
				sleep(1);
			break;
		default:

#ifdef USE_POLL
			if (ufds[0].revents) /* well, should check POLLIN, but ... */
#else
			if (FD_ISSET(0, &rfds))
#endif
			{
				len = read(0, G.buf, DATABUFSIZE);

				if (len <= 0)
					doexit(0);

				TRACE(0, ("Read con: %d\n", len));

				handlenetoutput(len);
			}

#ifdef USE_POLL
			if (ufds[1].revents) /* well, should check POLLIN, but ... */
#else
			if (FD_ISSET(G.netfd, &rfds))
#endif
			{
				len = read(G.netfd, G.buf, DATABUFSIZE);

				if (len <= 0)
				{
					WriteCS(1, "Connection closed by foreign host.\r\n");
					doexit(1);
				}
				TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));

				handlenetinput(len);
			}
		}
	}
}
RazorAHRS::RazorAHRS(const std::string &port, DataCallbackFunc data_func, ErrorCallbackFunc error_func,
    Mode mode, int connect_timeout_ms, speed_t speed)
    : _mode(mode)
    , _input_pos(0)
    , _connect_timeout_ms(connect_timeout_ms)
    , data(data_func)
    , error(error_func)
    , _thread_id(0)
    , _stop_thread(false)
{
  // check data type sizes
  assert(sizeof(char) == 1);
  assert(sizeof(float) == 4);

  // open serial port
  if (port == "")
    throw std::runtime_error("No port specified!");
  if (!_open_serial_port(port.c_str()))
    throw std::runtime_error("Could not open serial port!");

  // get port attributes
  struct termios tio;
  if (int errorID = tcgetattr(_serial_port, &tio))
    throw std::runtime_error("Could not get serial port attributes! Error # " + to_str(errorID));

  /* see http://www.easysw.com/~mike/serial/serial.html */
  /* and also http://linux.die.net/man/3/tcsetattr */
  // basic raw/non-canonical setup
  cfmakeraw(&tio);

  // enable reading and ignore control lines
  tio.c_cflag |= CREAD | CLOCAL;

  // set 8N1
  tio.c_cflag &= ~PARENB; // no parity bit
  tio.c_cflag &= ~CSTOPB; // only one stop bit
  tio.c_cflag &= ~CSIZE;  // clear data bit number
  tio.c_cflag |= CS8;     // set 8 data bits

  // no hardware flow control
  tio.c_cflag &= ~CRTSCTS;
  // no software flow control
  tio.c_iflag &= ~(IXON | IXOFF | IXANY);

  // poll() is broken on OSX, so we set VTIME and use read(), which is ok since
  // we're reading only one port anyway
  tio.c_cc[VMIN]  = 0;
  tio.c_cc[VTIME] = 10; // 10 * 100ms = 1s

  // set port speed
  if (int errorID = cfsetispeed(&tio, speed))
    throw std::runtime_error(" " + to_str(errorID)
        + ": Could not set new serial port input speed to "
        + to_str(speed) + ".");
  if (int errorID = cfsetospeed(&tio, speed))
    throw std::runtime_error(" " + to_str(errorID)
        + ": Could not set new serial port output speed to "
        + to_str(speed) + ".");

  // set port attributes
  // must be done after setting speed!
  if (int errorID = tcsetattr(_serial_port, TCSANOW, &tio))
  {
    throw std::runtime_error(" " + to_str(errorID)
        + ": Could not set new serial port attributes.");
  }

  // start input/output thread
  _start_io_thread();
}
Example #3
0
int serial_init(int fd, int baudrate, int databits, int stopbits, int parity)
{
    int i;
    int ret_val;
    char buffer[32];
    struct termios options;

    tcgetattr( fd, &options );
    cfmakeraw( &options );
    switch( baudrate )
    {
        case 50:    cfsetspeed( &options, B50 ); break;
        case 75:    cfsetspeed( &options, B75 ); break;
        case 150:   cfsetspeed( &options, B150 ); break;
        case 300:   cfsetspeed( &options, B300 ); break;
        case 600:   cfsetspeed( &options, B600 ); break;
        case 1200:  cfsetspeed( &options, B1200 ); break;
        case 2400:  cfsetspeed( &options, B2400 ); break;
        case 4800:  cfsetspeed( &options, B4800 ); break;
        case 9600:  cfsetspeed( &options, B9600 ); break;
        case 19200: cfsetspeed( &options, B19200 ); break;
        case 38400: cfsetspeed( &options, B38400 ); break;
        case 115200:cfsetspeed( &options, B115200 ); break;
        default: {
                     perror("baudrate error");
                     return -1;
                 }
    }

    options.c_cflag &= ~CSIZE;
    switch( databits ) {
        case 5:     options.c_cflag |= CS5; break;
        case 6:     options.c_cflag |= CS6; break;
        case 7:     options.c_cflag |= CS7; break;
        case 8:     options.c_cflag |= CS8; break;
        default: {
                     perror("databits error");
                     return -1;
                 }
    }

    options.c_cflag |= PARENB;
    switch( parity )
    {
        case 'o':
        case 'O':   options.c_cflag |= PARODD; break;
        case 'e':
        case 'E':   options.c_cflag &= ~PARODD; break;
        case 'n':
        case 'N':   options.c_cflag &= ~PARENB; break;
        default: {
                     perror("parity error");
                     return -1;
                 }
    }

    switch (stopbits){
        case 1:
            options.c_cflag &= ~CSTOPB;
            break;
        case 2:
            options.c_cflag |= CSTOPB;
            break;
        default: {
                     perror("stopbits err");
                     return -1;
                 }
    }

    tcflush(fd, TCIOFLUSH);
    options.c_cc[VTIME] = 15;
    options.c_cc[VMIN] = 0;

    if( tcsetattr( fd, TCSANOW, &options ) != 0 ) {
        fprintf( stderr, "set attribute failed."); 
        close( fd );
        return -1;
    }

    return fd;
}
Example #4
0
/* TODO: redesign to avoid longjmp/setjmp.  Several variables here
   have a volatile qualifier to silence warnings from gcc < 3.0.
   Remove the volatile qualifiers if longjmp/setjmp are removed.
   */
int main(int argc, char **argv, char **envp)
{
    struct in_addr inetaddr;
    volatile int callmgr_sock = -1;
    char ttydev[PATH_MAX];
    int pty_fd, tty_fd, gre_fd, rc;
    volatile pid_t parent_pid, child_pid;
    u_int16_t call_id, peer_call_id;
    char buf[128];
    int pppdargc;
    char **pppdargv;
    char phonenrbuf[65]; /* maximum length of field plus one for the trailing
                          * '\0' */
    char * volatile phonenr = NULL;
    volatile int launchpppd = 1, debug = 0, nodaemon = 0;

    while(1){ 
        /* structure with all recognised options for pptp */
        static struct option long_options[] = {
            {"phone", 1, 0, 0},  
            {"nolaunchpppd", 0, 0, 0},  
            {"quirks", 1, 0, 0},
            {"debug", 0, 0, 0},
            {"sync", 0, 0, 0},
            {"timeout", 1, 0, 0},
            {"logstring", 1, 0, 0},
            {"localbind", 1, 0, 0},
            {"loglevel", 1, 0, 0},
	    {"nobuffer", 0, 0, 0},
	    {"idle-wait", 1, 0, 0},
	    {"max-echo-wait", 1, 0, 0},
	    {"version", 0, 0, 0},
            {"nodaemon", 0, 0, 0},
            {0, 0, 0, 0}
        };
        int option_index = 0;
        int c;
        c = getopt_long (argc, argv, "", long_options, &option_index);
        if (c == -1) break;  /* no more options */
        switch (c) {
            case 0: 
                if (option_index == 0) { /* --phone specified */
                    /* copy it to a buffer, as the argv's will be overwritten
                     * by inststr() */
                    strncpy(phonenrbuf,optarg,sizeof(phonenrbuf));
                    phonenrbuf[sizeof(phonenrbuf) - 1] = '\0';
                    phonenr = phonenrbuf;
                } else if (option_index == 1) {/* --nolaunchpppd specified */
                    launchpppd = 0;
                } else if (option_index == 2) {/* --quirks specified */
                    if (set_quirk_index(find_quirk(optarg)))
                        usage(argv[0]);
                } else if (option_index == 3) {/* --debug */
                    debug = 1;
                } else if (option_index == 4) {/* --sync specified */
                    syncppp = 1;
                } else if (option_index == 5) {/* --timeout */
                    float new_packet_timeout = atof(optarg);
                    if (new_packet_timeout < 0.0099 ||
                            new_packet_timeout > 10) {
                        fprintf(stderr, "Packet timeout %s (%f) out of range: "
                                "should be between 0.01 and 10 seconds\n",
                                optarg, new_packet_timeout);
                        log("Packet timeout %s (%f) out of range: should be"
                                "between 0.01 and 10 seconds", optarg,
                                new_packet_timeout);
                        exit(2);
                    } else {
                        packet_timeout_usecs = new_packet_timeout * 1000000;
                    }
                } else if (option_index == 6) {/* --logstring */
                    log_string = strdup(optarg);
                } else if (option_index == 7) {/* --localbind */ 
                    if (inet_pton(AF_INET, optarg, (void *) &localbind) < 1) {
                        fprintf(stderr, "Local bind address %s invalid\n", 
				optarg);
                        log("Local bind address %s invalid\n", optarg);
                        exit(2);
                    }
                } else if (option_index == 8) { /* --loglevel */
                    log_level = atoi(optarg);
                    if (log_level < 0 || log_level > 2)
                        usage(argv[0]);
                } else if (option_index == 9) { /* --nobuffer */
		    disable_buffer = 1;
                } else if (option_index == 10) { /* --idle-wait */
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--idle-wait must not be negative\n");
                        log("--idle-wait must not be negative\n");
                        exit(2);
                    } else {
                        idle_wait = x;
                    }
                } else if (option_index == 11) { /* --max-echo-wait */
                    int x = atoi(optarg);
                    if (x < 0) {
                        fprintf(stderr, "--max-echo-wait must not be negative\n");
                        log("--max-echo-wait must not be negative\n");
                        exit(2);
                    } else {
                        max_echo_wait = x;
                    }
		    fprintf(stderr, "--max-echo-wait ignored, not yet implemented\n");
                } else if (option_index == 12) { /* --version */
		    fprintf(stdout, "%s\n", version);
		    exit(0);
                } else if (option_index == 13) {/* --nodaemon */
                    nodaemon = 1;
                }
                break;
            case '?': /* unrecognised option */
                /* fall through */
            default:
		usage(argv[0]);
        }
        if (c == -1) break;  /* no more options for pptp */
    }

    /* at least one argument is required */
    if (argc <= optind)
        usage(argv[0]);

    /* Get IP address for the hostname in argv[1] */
    inetaddr = get_ip_address(argv[optind]);
    optind++;

    /* Find the ppp options, extract phone number */
    pppdargc = argc - optind;
    pppdargv = argv + optind;
    log("The synchronous pptp option is %sactivated\n", syncppp ? "" : "NOT ");

    /* Now we have the peer address, bind the GRE socket early,
       before starting pppd. This prevents the ICMP Unreachable bug
       documented in <10268[email protected]> */
    gre_fd = pptp_gre_bind(inetaddr);
    if (gre_fd < 0) {
        close(callmgr_sock);
        fatal("Cannot bind GRE socket, aborting.");
    }

    /* Find an open pty/tty pair. */
    if(launchpppd){
        rc = openpty (&pty_fd, &tty_fd, ttydev, NULL, NULL);
        if (rc < 0) { 
            close(callmgr_sock); 
            fatal("Could not find free pty.");
        }

#if defined(__linux__)
		/*
		 * if we do not turn off echo now,  then if pppd sleeps for any
		 * length of time (DNS timeouts or whatever) the other end of the
		 * connect may detect the link as looped.  Other OSen may want this
		 * as well,  but for now linux gets it.
		 */
		{
			struct termios tios;
			tcgetattr(tty_fd, &tios);
			cfmakeraw(&tios);
			tcsetattr(tty_fd, TCSAFLUSH, &tios);
		}
#endif

        /* fork and wait. */
        signal(SIGUSR1, do_nothing); /* don't die */
        signal(SIGCHLD, do_nothing); /* don't ignore SIGCHLD */
        parent_pid = getpid();
        switch (child_pid = fork()) {
            case -1:
                fatal("Could not fork pppd process");
            case 0: /* I'm the child! */
                close (tty_fd);
                signal(SIGUSR1, SIG_DFL);
                child_pid = getpid();
                break;
            default: /* parent */
                close (pty_fd);
                close (gre_fd);
                /*
                 * There is still a very small race condition here.  If a signal
                 * occurs after signaled is checked but before pause is called,
                 * things will hang.
                 */
                if (!signaled) {
                    pause(); /* wait for the signal */
                }
 
                if (signaled == SIGCHLD)
                    fatal("Child process died");
 
                launch_pppd(ttydev, pppdargc, pppdargv); /* launch pppd */
                perror("Error");
                fatal("Could not launch pppd");
        }
    } else { /* ! launchpppd */
        pty_fd = tty_fd = STDIN_FILENO;
        /* close unused file descriptor, that is redirected to the pty */
        close(STDOUT_FILENO);
        child_pid = getpid();
        parent_pid = 0; /* don't kill pppd */
    }

    do {
        /*
         * Open connection to call manager (Launch call manager if necessary.)
         */
        callmgr_sock = open_callmgr(inetaddr, phonenr, argc, argv, envp,
		pty_fd, gre_fd);
        /* Exchange PIDs, get call ID */
    } while (get_call_id(callmgr_sock, parent_pid, child_pid, 
                &call_id, &peer_call_id) < 0);

    /* Send signal to wake up pppd task */
    if (launchpppd) {
        kill(parent_pid, SIGUSR1);
        sleep(2);
        /* become a daemon */
        if (!(debug || nodaemon) && daemon(0, 0) != 0) {
            perror("daemon");
        }
    } else {
        /* re-open stderr as /dev/null to release it */
        file2fd("/dev/null", "wb", STDERR_FILENO);
    }

    snprintf(buf, sizeof(buf), "pptp: GRE-to-PPP gateway on %s", 
            ttyname(tty_fd));
#ifdef PR_SET_NAME
    rc = prctl(PR_SET_NAME, "pptpgw", 0, 0, 0);
    if (rc != 0) perror("prctl");
#endif
    inststr(argc, argv, envp, buf);
    if (sigsetjmp(env, 1)!= 0) goto shutdown;

    signal(SIGINT,  sighandler);
    signal(SIGTERM, sighandler);
    signal(SIGKILL, sighandler);
    signal(SIGCHLD, sighandler);
    signal(SIGUSR1, sigstats);

    /* Do GRE copy until close. */
    pptp_gre_copy(call_id, peer_call_id, pty_fd, gre_fd);

shutdown:
    /* on close, kill all. */
    if(launchpppd)
        kill(parent_pid, SIGTERM);
    close(pty_fd);
    close(callmgr_sock);
    exit(0);
}
int configurePort(int fd, unsigned long baudrate)
{
#ifdef WIN32
	DCB dcb = {0};
	HANDLE hCom = (HANDLE)fd;

	dcb.DCBlength = sizeof(dcb);

	dcb.BaudRate = baudrate;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;

	if( !SetCommState(hCom, &dcb) ){
		return -1;
	}

	return (int)hCom;
#else
    speed_t baud = B921600;
	struct termios g_new_tio;

	switch (baudrate) {
		case 921600:
			baud = B921600;
			break;
		case 115200:
			baud = B115200;
			break;
		case 1000000:
			baud = B1000000;
			break;
		case 1500000:
			baud = B1500000;
		default:
			printf("unknown speed setting \n");
			return -1;
			break;
	}

	memset(&g_new_tio, 0x00 , sizeof(g_new_tio));
	cfmakeraw(&g_new_tio);

	g_new_tio.c_cflag |=  (CS8 | CLOCAL | CREAD);
	g_new_tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
	g_new_tio.c_oflag = 0;
	g_new_tio.c_lflag = 0;

	g_new_tio.c_cc[VTIME] = 0;
	g_new_tio.c_cc[VMIN] = 1;

#ifdef MACOSX

	if( tcsetattr(fd, TCSANOW, &g_new_tio) < 0 ) {
		return -1;
	}

	return ioctl( fd, IOSSIOSPEED, &baud );
#else
	cfsetispeed (&g_new_tio, baudrate);
	cfsetospeed (&g_new_tio, baudrate);

	tcflush(fd, TCIOFLUSH);

	return tcsetattr(fd, TCSANOW, &g_new_tio);
#endif //#ifdef MACOSX

#endif
}
Example #6
0
int pty_forward_new(
                sd_event *event,
                int master,
                PTYForwardFlags flags,
                PTYForward **ret) {

        _cleanup_(pty_forward_freep) PTYForward *f = NULL;
        struct winsize ws;
        int r;

        f = new0(PTYForward, 1);
        if (!f)
                return -ENOMEM;

        f->flags = flags;

        if (event)
                f->event = sd_event_ref(event);
        else {
                r = sd_event_default(&f->event);
                if (r < 0)
                        return r;
        }

        if (!(flags & PTY_FORWARD_READ_ONLY)) {
                r = fd_nonblock(STDIN_FILENO, true);
                if (r < 0)
                        return r;

                r = fd_nonblock(STDOUT_FILENO, true);
                if (r < 0)
                        return r;
        }

        r = fd_nonblock(master, true);
        if (r < 0)
                return r;

        f->master = master;

        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
                (void) ioctl(master, TIOCSWINSZ, &ws);

        if (!(flags & PTY_FORWARD_READ_ONLY)) {
                if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
                        struct termios raw_stdin_attr;

                        f->saved_stdin = true;

                        raw_stdin_attr = f->saved_stdin_attr;
                        cfmakeraw(&raw_stdin_attr);
                        raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
                        tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
                }

                if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
                        struct termios raw_stdout_attr;

                        f->saved_stdout = true;

                        raw_stdout_attr = f->saved_stdout_attr;
                        cfmakeraw(&raw_stdout_attr);
                        raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
                        raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
                        tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
                }

                r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
                if (r < 0 && r != -EPERM)
                        return r;
        }

        r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
        if (r == -EPERM)
                /* stdout without epoll support. Likely redirected to regular file. */
                f->stdout_writable = true;
        else if (r < 0)
                return r;

        r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
        if (r < 0)
                return r;

        r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
        if (r < 0)
                return r;

        *ret = f;
        f = NULL;

        return 0;
}
Example #7
0
int ChassisEr1::moduleInit(void)
{
    int ret;
    struct termios oldtio,newtio;
    struct serial_struct serial;
    int fd;

    // call RackDataModule init function (first command in init)
    ret = RackDataModule::moduleInit();
    if (ret)
    {
        return ret;
    }
    initBits.setBit(INIT_BIT_DATA_MODULE);

    // create hardware mutex
    ret = hwMtx.create();
    if (ret)
    {
        goto init_error;
    }
    initBits.setBit(INIT_BIT_MTX_CREATED);

    // open serial port
/*    ret = serialPort.open(serialDev, &pioneer_serial_config, this);
    if (ret)
    {
        printf("Can't open serialDev %i\n", serialDev);
        goto init_error;
    }
*/

    fd = rcm.handle= open(serialDev, O_RDWR|O_NOCTTY);

    if (fd < 0 ) {
        printf("Can't open serialDev\n");
        goto init_error;
    }

    tcgetattr(fd,&oldtio);
    bzero(&newtio,sizeof(newtio));

    newtio.c_cflag = B38400|CS8|CLOCAL|CREAD;
    newtio.c_iflag = IGNPAR | ICRNL ;
    newtio.c_oflag = 0;
//      newtio.c_lflag = ICANON;
    cfmakeraw(&newtio);

    newtio.c_cc[VMIN] = 1;
    newtio.c_cc[VTIME] = 0;

    tcflush(fd,TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

    ioctl(fd,TIOCGSERIAL,&serial);
    serial.custom_divisor = serial.baud_base / 250000;
    serial.flags = 0x0030;
    ioctl(fd,TIOCSSERIAL,&serial);

    initBits.setBit(INIT_BIT_SERIAL_OPENED);

    printf("Completed SERIAL Initialize\n");

    return 0;

init_error:
    ChassisEr1::moduleCleanup();
    return ret;
}
Example #8
0
bool cedrus::xid_con_t::setup_com_port()
{
	bool status = true;

	// http://developer.apple.com/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/chapter_2_section_7.html#//apple_ref/doc/uid/TP40000972-TP30000384-CIHIAAFF

    // Get the current options and save them so we can restore the
    // default settings later.
    if (tcgetattr(m_darwinPimpl->m_FileDescriptor, &(m_darwinPimpl->m_OptionsOriginal)) == OS_FILE_ERROR)
        status = false;
    else
    {
        // The serial port attributes such as timeouts and baud rate are set by
        // modifying the termios structure and then calling tcsetattr to
        // cause the changes to take effect. Note that the
        // changes will not take effect without the tcsetattr() call.
        // See tcsetattr(4) ("man 4 tcsetattr") for details.
        
        m_darwinPimpl->m_OptionsCurrent = m_darwinPimpl->m_OptionsOriginal;
    }

	if ( status )
	{
		// Set raw input (non-canonical) mode, with reads blocking until either
		// a single character has been received or a one second timeout expires.
		// See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios")
		// for details.

		cfmakeraw(&(m_darwinPimpl->m_OptionsCurrent));
		// the following two settings cause reads to be non-blocking, despite
		// the fact that the port has been set to block
		m_darwinPimpl->m_OptionsCurrent.c_cc[VMIN] = 0;
		m_darwinPimpl->m_OptionsCurrent.c_cc[VTIME] = 0;

		m_darwinPimpl->m_OptionsCurrent.c_iflag = ( IGNBRK | IGNPAR );	/* input flags */

		m_darwinPimpl->m_OptionsCurrent.c_oflag = 0;	/* output flags */
		m_darwinPimpl->m_OptionsCurrent.c_cflag = 0;	/* control flags */

		tcflag_t flags = m_darwinPimpl->m_OptionsCurrent.c_cflag;

		flags |= ( CREAD | HUPCL | CLOCAL );

		switch ( handshaking_ )
		{
			case HANDSHAKE_XON_XOFF:
				m_darwinPimpl->m_OptionsCurrent.c_iflag |= ( IXON | IXOFF | IXANY );
				break;

			case HANDSHAKE_HARDWARE:
				flags |= CRTSCTS;	// CTS and RTS flow control of output
				break;

			case HANDSHAKE_NONE:
				m_darwinPimpl->m_OptionsCurrent.c_iflag &= ~( IXON | IXOFF | IXANY );
				flags &= ~ CRTSCTS;
				break;

			default:
				break;
		}

		flags &= ~( PARENB | PARODD );
		m_darwinPimpl->m_OptionsCurrent.c_iflag &= ~( INPCK | ISTRIP );
		switch ( bit_parity_ )
		{
			case BITPARITY_ODD:
				m_darwinPimpl->m_OptionsCurrent.c_iflag |= INPCK | ISTRIP;
				flags |= PARODD; // this sets the parity to odd -- even is the default
			case BITPARITY_EVEN:
				m_darwinPimpl->m_OptionsCurrent.c_iflag |= INPCK;
				flags |= PARENB; // this enables parity
				break;

			case BITPARITY_NONE:
				break;

			default:
				break;
		}

		// The baud rate, word length, and handshake options can be set as follows:

		flags &= ~CSIZE;
		switch ( byte_size_ )
		{
			case BYTESIZE_6:
				flags |= CS6;
				break;

			case BYTESIZE_7:
				flags |= CS7;
				break;

			case BYTESIZE_8:
				flags |= CS8;
				break;

			default:
				break;
		}

		if ( stop_bits_ == STOP_BIT_2 )
			flags |= CSTOPB;
		else
			flags &= ~CSTOPB;

		cfsetspeed(&(m_darwinPimpl->m_OptionsCurrent), baud_rate_);
		m_darwinPimpl->m_OptionsCurrent.c_cflag = ( flags );

		// Cause the new options to take effect immediately.
		usleep(10*1000);

		if (tcsetattr(m_darwinPimpl->m_FileDescriptor, TCSAFLUSH, &(m_darwinPimpl->m_OptionsCurrent)) == OS_FILE_ERROR)
			status = false;
	}

	if ( handshaking_ == HANDSHAKE_HARDWARE )
	{

		if ( status )
		{
			// To set the modem handshake lines, use the following ioctls.
			// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
			if (ioctl(m_darwinPimpl->m_FileDescriptor, TIOCSDTR) == OS_FILE_ERROR)
				status = false;
		}

		if ( status )
		{
			// Clear Data Terminal Ready (DTR)
			if (ioctl(m_darwinPimpl->m_FileDescriptor, TIOCCDTR) == OS_FILE_ERROR)
				status = false;
		}

		unsigned long handshake = 0;

		if ( status )
		{
			handshake = TIOCM_DTR | TIOCM_DSR | TIOCM_RTS | TIOCM_CTS;
			// Set the modem lines depending on the bits set in handshake.
			if (ioctl(m_darwinPimpl->m_FileDescriptor, TIOCMSET, &handshake) == OS_FILE_ERROR)
				status = false;
		}

		if ( status )
		{
			// To read the state of the modem lines, use the following ioctl.
			// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.

			// Store the state of the modem lines in handshake.
			if (ioctl(m_darwinPimpl->m_FileDescriptor, TIOCMGET, &handshake) == OS_FILE_ERROR)
				status = false;
		}
	} // handshaking

	// :whschultz:20070507
	// Found Apple documentation stating how to set the read latency on a standard serial port.
	// http://developer.apple.com/samplecode/SerialPortSample/listing2.html
    // http://developer.apple.com/library/mac/samplecode/SerialPortSample/index.html
#if defined(MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
	if ( status )
	{
		unsigned long mics = 1UL;

		// Set the receive latency in microseconds. Serial drivers use this value to determine how often to
		// dequeue characters received by the hardware. Most applications don't need to set this value: if an
		// app reads lines of characters, the app can't do anything until the line termination character has been
		// received anyway. The most common applications which are sensitive to read latency are MIDI and IrDA
		// applications.

		// set latency to 1 microsecond
		if (ioctl(m_darwinPimpl->m_FileDescriptor, IOSSDATALAT, &mics) == OS_FILE_ERROR) //does that actually happen??
			status = false;
	}
#endif

	if ( status )
	{
		flush_write_to_device_buffer();
		flush_read_from_device_buffer();

        // This seems to ensure that the buffers actually get flushed.
        usleep(10*1000);
	}

	return status;
}
Example #9
0
 /**
 *@brief Open and initialise the serial port with a specific baud rate.
 *@param baud Baud rate for port ttyAMA0
 *@return fd  device file number
 */
int SerialOpen (int baud)
{
  struct termios options ;
  speed_t myBaud ;
  int     status, fd ;

  switch (baud)
  {
    case     50:	myBaud =     B50 ; break ;
    case     75:	myBaud =     B75 ; break ;
    case    110:	myBaud =    B110 ; break ;
    case    134:	myBaud =    B134 ; break ;
    case    150:	myBaud =    B150 ; break ;
    case    200:	myBaud =    B200 ; break ;
    case    300:	myBaud =    B300 ; break ;
    case    600:	myBaud =    B600 ; break ;
    case   1200:	myBaud =   B1200 ; break ;
    case   1800:	myBaud =   B1800 ; break ;
    case   2400:	myBaud =   B2400 ; break ;
    case   9600:	myBaud =   B9600 ; break ;
    case  19200:	myBaud =  B19200 ; break ;
    case  38400:	myBaud =  B38400 ; break ;
    case  57600:	myBaud =  B57600 ; break ;
    case 115200:	myBaud = B115200 ; break ;
    case 230400:	myBaud = B230400 ; break ;

    default:
      return -2 ;
  }

  if ((fd = open ("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
  {
	fprintf (stderr, "Cannot open port serial port.\n") ;    
  }

  fcntl (fd, F_SETFL, O_RDWR) ;

// Get and modify current options:

  tcgetattr (fd, &options) ;

  cfmakeraw   (&options) ;
  cfsetispeed (&options, myBaud) ;
  cfsetospeed (&options, myBaud) ;

  options.c_cflag |= (CLOCAL | CREAD) ;
  options.c_cflag &= ~PARENB ;
  options.c_cflag &= ~CSTOPB ;
  options.c_cflag &= ~CSIZE ;
  options.c_cflag |= CS8 ;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
  options.c_oflag &= ~OPOST ;

  options.c_cc [VMIN]  =   0 ;
  options.c_cc [VTIME] = 100 ;	// Ten seconds (100 deciseconds)
  tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;

  ioctl (fd, TIOCMGET, &status);

  status |= TIOCM_DTR ;
  status |= TIOCM_RTS ;

  ioctl (fd, TIOCMSET, &status);

  usleep (10000) ;	// 10mS

  return fd ;
}
static int h4_open(void *transport_config)
{
    hci_uart_config = (hci_uart_config_t *) transport_config;

    int fd = socket(PF_NETGRAPH, SOCK_STREAM, NG_CONTROL);
    if (fd < 0) {
        perror("socket(HCI_IF)");
        goto err_out0;
    }
    
    // get node address
    struct ioctl_arg_t {
        uint32_t result;
        char socket_name[96];
    } ioctl_arg;
    memset((void *) &ioctl_arg, 0x00, sizeof(struct ioctl_arg_t));
    strcpy((char *) &ioctl_arg.socket_name, SOCKET_DEVICE);
    if (ioctl(fd, SOCK_IOCTL_VAL, &ioctl_arg) != 0) {
        perror("ioctl(fd_sock, SOCK_IOCTL_VAL)");
        goto err_out1;
    }
    
    // setup sock addr struct
    struct sockaddr_ng sock_addr;
    sock_addr.sg_len     = sizeof(struct sockaddr_ng);
    sock_addr.sg_family  = PF_NETGRAPH;
    sock_addr.sg_subtype = 0x02;
    sock_addr.sg_node    = ioctl_arg.result;
    sock_addr.sg_null    = 0;
    
    // connect
    if (connect(fd, (const struct sockaddr *) &sock_addr, sizeof(struct sockaddr_ng)) != 0) {
        perror("connect(fd_sock)");
        goto err_out2;
    }
    
    // configure UART
    struct termios toptions;
    socklen_t toptions_len = sizeof(struct termios);
    if (getsockopt(fd, SO_ACCEPTCONN, GETSOCKOPT_VAL, &toptions, &toptions_len) != 0) {
        perror("getsockopt(fd_sock)");
        goto err_out3;
    }
    cfmakeraw(&toptions);
    speed_t brate = (speed_t) hci_uart_config->baudrate_init;
    cfsetspeed(&toptions, brate);    
    toptions.c_iflag |=  IGNPAR;
    toptions.c_cflag = 0x00038b00;
    if (setsockopt(fd, SO_ACCEPTCONN, SETSOCKOPT_VAL, &toptions, toptions_len) != 0) {
        perror("setsockopt(fd_sock)");
        goto err_out4;
    }
        
    // set up data_source
    hci_transport_h4->ds = malloc(sizeof(data_source_t));
    if (!hci_transport_h4->ds) return -1;
    hci_transport_h4->uart_fd = fd;
    
    hci_transport_h4->ds->fd = fd;
    hci_transport_h4->ds->process = h4_process;
    run_loop_add_data_source(hci_transport_h4->ds);
    
    // init state machine
    bytes_to_read = 1;
    h4_state = H4_W4_PACKET_TYPE;
    read_pos = 0;
    
    return 0;
    
err_out4:
err_out3:
err_out2:
err_out1:
    close(fd);
err_out0:
    fprintf(stderr, "h4_open error\n");

    return -1;
}
Example #11
0
JNIEXPORT jobject JNICALL Java_com_leon_agriculturerobot_serialport_SerialPort_open
  (JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
{
    	int fd;
    	speed_t speed;
    	jobject mFileDescriptor;

    	/* Check arguments */
    	{
    		speed = getBaudrate(baudrate);
    		if (speed == -1) {
    			LOGE("Invalid baudrate");
    			return NULL;
    		}
    	}

    	/* Opening device */
    	{
    		jboolean iscopy;
    		const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
    		LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
    		fd = open(path_utf, O_RDWR | flags);
    		LOGD("open() fd = %d", fd);
    		(*env)->ReleaseStringUTFChars(env, path, path_utf);
    		if (fd == -1)
    		{
    			/* Throw an exception */
    			LOGE("Cannot open port");
    			return NULL;
    		}
    	}

    	/* Configure device */
    	{
    		struct termios cfg;
    		LOGD("Configuring serial port");
    		if (tcgetattr(fd, &cfg))
    		{
    			LOGE("tcgetattr() failed");
    			close(fd);
    			return NULL;
    		}

    		cfmakeraw(&cfg);
    		cfsetispeed(&cfg, speed);
    		cfsetospeed(&cfg, speed);

    		if (tcsetattr(fd, TCSANOW, &cfg))
    		{
    			LOGE("tcsetattr() failed");
    			close(fd);
    			return NULL;
    		}
    	}

    	/* Create a corresponding file descriptor */
    	{
    		jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
    		jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
    		jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
    		mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
    		(*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
    	}

    	return mFileDescriptor;
}
Example #12
0
// BSD - OSX version
int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd)
{
       int	t_fd = -1;
       int bps;
       char msg[80];
    int	handshake;
    struct termios	tty_setting;

    // Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
    // The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
    // See open(2) ("man 2 open") for details.

    t_fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (t_fd == -1)
    {
        printf("Error opening serial port %s - %s(%d).\n",
               device, strerror(errno), errno);
        goto error;
    }

    // Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
    // unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
    // processes.
    // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.

    if (ioctl(t_fd, TIOCEXCL) == -1)
    {
        printf("Error setting TIOCEXCL on %s - %s(%d).\n",
            device, strerror(errno), errno);
        goto error;
    }

    // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
    // See fcntl(2) ("man 2 fcntl") for details.

    if (fcntl(t_fd, F_SETFL, 0) == -1)
    {
        printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
            device, strerror(errno), errno);
        goto error;
    }

    // Get the current options and save them so we can restore the default settings later.
    if (tcgetattr(t_fd, &tty_setting) == -1)
    {
        printf("Error getting tty attributes %s - %s(%d).\n",
            device, strerror(errno), errno);
        goto error;
    }

    // Set raw input (non-canonical) mode, with reads blocking until either a single character
    // has been received or a one second timeout expires.
    // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.

    cfmakeraw(&tty_setting);
    tty_setting.c_cc[VMIN] = 1;
    tty_setting.c_cc[VTIME] = 10;

    // The baud rate, word length, and handshake options can be set as follows:
        switch (bit_rate) {
                case 0:
                        bps = B0;
                        break;
                case 50:
                        bps = B50;
                        break;
                case 75:
                        bps = B75;
                        break;
                case 110:
                        bps = B110;
                        break;
                case 134:
                        bps = B134;
                        break;
                case 150:
                        bps = B150;
                        break;
                case 200:
                        bps = B200;
                        break;
                case 300:
                        bps = B300;
                        break;
                case 600:
                        bps = B600;
                        break;
                case 1200:
                        bps = B1200;
                        break;
                case 1800:
                        bps = B1800;
                        break;
                case 2400:
                        bps = B2400;
                        break;
                case 4800:
                        bps = B4800;
                        break;
                case 9600:
                        bps = B9600;
                        break;
                case 19200:
                        bps = B19200;
                        break;
                case 38400:
                        bps = B38400;
                        break;
                case 57600:
                        bps = B57600;
                        break;
                case 115200:
                        bps = B115200;
                        break;
                case 230400:
                        bps = B230400;
                        break;
                default:
                        if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid bit rate.", bit_rate) < 0)
                                perror(NULL);
                        else
                                perror(msg);
                        return TTY_PARAM_ERROR;
        }

     cfsetspeed(&tty_setting, bps);		// Set baud rate
        /* word size */
        switch (word_size) {
                case 5:
                        tty_setting.c_cflag |= CS5;
                        break;
                case 6:
                        tty_setting.c_cflag |= CS6;
                        break;
                case 7:
                        tty_setting.c_cflag |= CS7;
                        break;
                case 8:
                        tty_setting.c_cflag |= CS8;
                        break;
                default:

                        fprintf( stderr, "Default\n") ;
                        if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid data bit count.", word_size) < 0)
                                perror(NULL);
                        else
                                perror(msg);

                        return TTY_PARAM_ERROR;
        }

        /* parity */
        switch (parity) {
                case PARITY_NONE:
                        break;
                case PARITY_EVEN:
                        tty_setting.c_cflag |= PARENB;
                        break;
                case PARITY_ODD:
                        tty_setting.c_cflag |= PARENB | PARODD;
                        break;
                default:

                        fprintf( stderr, "Default1\n") ;
                        if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid parity selection value.", parity) < 0)
                                perror(NULL);
                        else
                                perror(msg);

                        return TTY_PARAM_ERROR;
        }

        /* stop_bits */
        switch (stop_bits) {
                case 1:
                        break;
                case 2:
                        tty_setting.c_cflag |= CSTOPB;
                        break;
                default:
                        fprintf( stderr, "Default2\n") ;
                        if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid stop bit count.", stop_bits) < 0)
                                perror(NULL);
                        else
                                perror(msg);

                        return TTY_PARAM_ERROR;
        }

#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
       // Starting with Tiger, the IOSSIOSPEED ioctl can be used to set arbitrary baud rates
       // other than those specified by POSIX. The driver for the underlying serial hardware
       // ultimately determines which baud rates can be used. This ioctl sets both the input
       // and output speed.

       speed_t speed = 14400; // Set 14400 baud
    if (ioctl(fileDescriptor, IOSSIOSPEED, &speed) == -1)
    {
        printf("Error calling ioctl(..., IOSSIOSPEED, ...) %s - %s(%d).\n",
            bsdPath, strerror(errno), errno);
    }
#endif

    // Cause the new options to take effect immediately.
    if (tcsetattr(t_fd, TCSANOW, &tty_setting) == -1)
    {
        printf("Error setting tty attributes %s - %s(%d).\n",
            device, strerror(errno), errno);
        goto error;
    }

    // To set the modem handshake lines, use the following ioctls.
    // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.

    if (ioctl(t_fd, TIOCSDTR) == -1) // Assert Data Terminal Ready (DTR)
    {
        printf("Error asserting DTR %s - %s(%d).\n",
            device, strerror(errno), errno);
    }

    if (ioctl(t_fd, TIOCCDTR) == -1) // Clear Data Terminal Ready (DTR)
    {
        printf("Error clearing DTR %s - %s(%d).\n",
            device, strerror(errno), errno);
    }

    handshake = TIOCM_DTR | TIOCM_RTS | TIOCM_CTS | TIOCM_DSR;
    if (ioctl(t_fd, TIOCMSET, &handshake) == -1)
    // Set the modem lines depending on the bits set in handshake
    {
        printf("Error setting handshake lines %s - %s(%d).\n",
            device, strerror(errno), errno);
    }

    // To read the state of the modem lines, use the following ioctl.
    // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.

    if (ioctl(t_fd, TIOCMGET, &handshake) == -1)
    // Store the state of the modem lines in handshake
    {
        printf("Error getting handshake lines %s - %s(%d).\n",
            device, strerror(errno), errno);
    }

    printf("Handshake lines currently set to %d\n", handshake);

#if defined(MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
       unsigned long mics = 1UL;

       // Set the receive latency in microseconds. Serial drivers use this value to determine how often to
       // dequeue characters received by the hardware. Most applications don't need to set this value: if an
       // app reads lines of characters, the app can't do anything until the line termination character has been
       // received anyway. The most common applications which are sensitive to read latency are MIDI and IrDA
       // applications.

       if (ioctl(t_fd, IOSSDATALAT, &mics) == -1)
       {
               // set latency to 1 microsecond
        printf("Error setting read latency %s - %s(%d).\n",
            device, strerror(errno), errno);
        goto error;
       }
#endif

   *fd = t_fd;
  /* return success */
  return TTY_OK;

    // Failure path
error:
    if (t_fd != -1)
    {
        close(t_fd);
        *fd = -1;
    }

    return TTY_PORT_FAILURE;
}
Example #13
0
int createPty(struct Connection *connection) {
    int ptm, pts;
    ptm = posix_openpt(O_RDWR);
    if (ptm == -1) {
        perror("creating new plm");
        return -1;
    }
    if (grantpt(ptm) == -1) {
        perror("granting pt access");
        return -1;
    }
    if (unlockpt(ptm) == -1) {
        perror("unlocking pt");
        return -1;
    }
    pts = open(ptsname(ptm), O_RDWR);
    if (pts == -1) {
        perror("opening pts");
        return -1;
    }
    if (setNonBlock(ptm) == -1) {
        fprintf(stderr, "Error: making ptm non-block\n");
        return -1;
    }
    if (setNonBlock(pts) == -1) {
        fprintf(stderr, "Error: making pts non-block\n");
        return -1;
    }

    connection->ptm = ptm;
    if (addToEpoll(epollfd, ptm, EPOLLET | EPOLLIN) == -1) {
        fprintf(stderr, "Error: adding ptm to epoll\n");
        return -1;
    }

    if (fork()) {
        if (close(pts) == -1) {
            perror("closing pts in parent process");
            return -1;
        }
    } else {
        if (close(ptm) == -1) {
            perror("closing ptm in child process");
            return -1;
        }
        struct termios oldSettings, newSettings;
        if (tcgetattr(pts, &oldSettings) == -1) {
            perror("getting old terminal settings\n");
            return -1;
        }
        newSettings = oldSettings;
        cfmakeraw(&newSettings);
        if (tcsetattr(pts, TCSANOW, &newSettings) == -1) {
            perror("setting new terminal settings\n");
            return -1;
        }
        close(0);
        close(1);
        close(2);

        dup(pts);
        dup(pts);
        dup(pts);

        close(pts);

        setsid();

        ioctl(0, TIOCSCTTY, 1);
        execvp("/bin/bash", NULL);
    }
    return 0;
}
Example #14
0
static bool _srmio_ios_update( srmio_io_t h, srmio_error_t *err )
{
	struct termios ios;

	memset(&ios, 0, sizeof(struct termios));
#ifdef HAVE_CFMAKERAW
	/* not sure, if this is needed right after memset ...: */
	cfmakeraw( &ios );
#endif

	/* TODO: make other termios parameters configurable */

	ios.c_iflag = IGNPAR;
	ios.c_cflag = CS8 | CLOCAL | CREAD;

	switch( h->flow ){
	  case srmio_io_flow_none:
		/* do nothing */
		break;

	  case srmio_io_flow_xonoff:
		ios.c_iflag |= IXON | IXOFF;
		break;

	  case srmio_io_flow_rtscts:
		ios.c_cflag |= CRTSCTS;
		break;

	  default:
		srmio_error_set( err, "ios invalid flow control: %u", h->flow);
		return false;
	}

	switch( h->parity ){
	  case srmio_io_parity_none:
		/* do nothing */
		break;

	  case srmio_io_parity_even:
		ios.c_cflag |= PARENB;
		break;

	  case srmio_io_parity_odd:
		ios.c_cflag |= PARENB | PARODD;
		break;

	  default:
		srmio_error_set( err, "ios invalid parity: %u", h->parity );
		return false;
	}

	if( 0 > cfsetispeed( &ios, _srmio_ios_baud[h->baudrate] ) ){
		srmio_error_errno( err, "ios setispieed" );
		return false;
	}

	if( 0 > cfsetospeed( &ios, _srmio_ios_baud[h->baudrate] ) ){
		srmio_error_errno( err, "ios setospieed" );
		return false;
	}

	/* wait max 1 sec for whole read() */
	ios.c_cc[VMIN] = 0;
	ios.c_cc[VTIME] = 10;

	if( tcsetattr( SELF(h)->fd, TCSANOW, &ios ) ){
		srmio_error_errno( err, "ios settattr" );
		return false;
	}

	return true;
}
int set_serial (struct serial *s, struct serial_mode *serial_mode)

{

#ifdef WIN32

	COMMTIMEOUTS timeouts;
	DCB dcbSerial;
	memset (&dcbSerial, 0, sizeof (dcbSerial));
	dcbSerial.DCBlength = sizeof (dcbSerial);
	if (!GetCommState (s->h, &dcbSerial))
	{
		return (-1);
	}
	dcbSerial.BaudRate = serial_mode->baud_rate;
	dcbSerial.ByteSize = serial_mode->data_bits;
	switch (serial_mode->stop_bits)
	{
	case 1:
		dcbSerial.StopBits = ONESTOPBIT;
		break;
	case 2:
		dcbSerial.StopBits = TWOSTOPBITS;
		break;
	default:
		error (1, 0, "invalid stop bit setting");
	}
	switch (serial_mode->parity)
	{
	case UART_ODDPARITY:
		dcbSerial.Parity = ODDPARITY;
		dcbSerial.fParity = TRUE;
		break;
	case UART_EVENPARITY:
		dcbSerial.Parity = EVENPARITY;
		dcbSerial.fParity = TRUE;
		break;
	case UART_NOPARITY:
		dcbSerial.Parity = NOPARITY;
		dcbSerial.fParity = FALSE;
		break;
	default:
		error (1, 0, "invalid parity serial_mode");
	}
	if (!SetCommState (s->h, &dcbSerial))
	{
		error (0, 0, "could not set serial port settings");
		return (-1);
	}
	timeouts.ReadIntervalTimeout = 0;
	timeouts.ReadTotalTimeoutConstant = 10;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.WriteTotalTimeoutConstant = 10;
	timeouts.WriteTotalTimeoutMultiplier = 10;
	if (!SetCommTimeouts (s->h, &timeouts))
	{
		return (-1);
	}

#else

	struct termios termios;
	speed_t speed;
	tcgetattr (s->fd, &termios);
	cfmakeraw (&termios);
	termios.c_cflag &= ~CSIZE;
	switch (serial_mode->data_bits)
	{
	case 8:
		termios.c_cflag |= CS8;
		break;
	case 7:
		termios.c_cflag |= CS7;
		break;
	case 6:
		termios.c_cflag |= CS6;
		break;
	case 5:
		termios.c_cflag |= CS5;
		break;
	default:
		error (1, 0, "invalid serial byte size");
	}
	switch (serial_mode->stop_bits)
	{
	case 2:
		termios.c_cflag |= CSTOPB;
		break;
	case 1:
		termios.c_cflag &= ~CSTOPB;
		break;
	default:
		error (1, 0, "invalid number of stop bits");
	}
	switch (serial_mode->parity)
	{
	case UART_ODDPARITY:
		termios.c_cflag |= PARENB;
		termios.c_cflag |= PARODD;
		break;
	case UART_EVENPARITY:
		termios.c_cflag |= PARENB;
		termios.c_cflag &= ~PARODD;
		break;
	case UART_NOPARITY:
		termios.c_cflag &= ~PARENB;
		break;
	default:
		error (1, 0, "invalid parity serial_mode");
	}
	if (baudrate (serial_mode->baud_rate, &speed) == -1)
	{
		error (0, 0, "warning: unsupported baud rate: %d", serial_mode->baud_rate);
		return (-1);
	}
	if (cfsetspeed (&termios, speed) == -1) error (1, 0, "could not set serial baud rate");
	termios.c_cc [VTIME] = 1;
	termios.c_cc [VMIN] = 0;
	if (tcsetattr (s->fd, TCSANOW, &termios) == -1) error (1, 0, "could not set serial attributes");

#endif

	return (0);
}
Example #16
0
static int    h4_open(void *transport_config) {
    hci_uart_config = (hci_uart_config_t*) transport_config;
    struct termios toptions;
    int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
    int fd = open(hci_uart_config->device_name, flags);
    if (fd == -1)  {
        perror("init_serialport: Unable to open port ");
        perror(hci_uart_config->device_name);
        return -1;
    }

    if (tcgetattr(fd, &toptions) < 0) {
        perror("init_serialport: Couldn't get term attributes");
        return -1;
    }

    cfmakeraw(&toptions);   // make raw

    // 8N1
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag |= CS8;

    if (hci_uart_config->flowcontrol) {
        // with flow control
        toptions.c_cflag |= CRTSCTS;
    } else {
        // no flow control
        toptions.c_cflag &= ~CRTSCTS;
    }

    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
    toptions.c_cc[VMIN]  = 1;
    toptions.c_cc[VTIME] = 0;

    if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
        perror("init_serialport: Couldn't set term attributes");
        return -1;
    }

    // set up data_source
    hci_transport_h4->ds = (data_source_t*) malloc(sizeof(data_source_t));
    if (!hci_transport_h4->ds) return -1;
    hci_transport_h4->uart_fd = fd;
    hci_transport_h4->ds->fd = fd;
    hci_transport_h4->ds->process = h4_process;
    run_loop_add_data_source(hci_transport_h4->ds);

    // also set baudrate
    if (h4_set_baudrate(hci_uart_config->baudrate_init) < 0) {
        return -1;
    }

    // init state machine
    bytes_to_read = 1;
    h4_state = H4_W4_PACKET_TYPE;
    read_pos = 0;
    return 0;
}
Example #17
0
int aries_modem_bootstrap(struct ipc_client *client)
{
    int s3c2410_serial3_fd = -1;
    int onedram_fd = -1;

    /* Control variables. */
    int rc = 0;

    /* Boot variables */
    uint8_t *radio_img_p = NULL;
    uint32_t onedram_data = 0;
    uint8_t bootcore_version = 0;
    uint8_t info_size = 0;
    uint8_t crc_byte = 0;
    int block_size = 0;

    /* s3c2410 serial setup variables. */
    struct termios termios;
    int serial;

    /* fds maniplation variables */
    struct timeval timeout;
    fd_set fds;

    /* nv_data variables */
    void *nv_data_p;
    void *onedram_p;

    /* General purpose variables. */
    uint8_t data;
    uint16_t data_16;
    uint8_t *data_p;
    int i;

    ipc_client_log(client, "aries_ipc_bootstrap: enter");

    /* Read the radio.img image. */
    ipc_client_log(client, "aries_ipc_bootstrap: reading radio image");
    radio_img_p = ipc_client_mtd_read(client, "/dev/block/bml12", RADIO_IMG_READ_SIZE, RADIO_IMG_READ_SIZE);
    ipc_client_log(client, "aries_ipc_bootstrap: radio image read");

    ipc_client_log(client, "aries_ipc_bootstrap: open onedram");
    onedram_fd=open("/dev/onedram", O_RDWR);
    if(onedram_fd < 0)
        goto error;

    /* Reset the modem before init to send the first part of modem.img. */
    ipc_client_log(client, "aries_ipc_bootstrap: turning %s iface down", PHONET_IFACE);
    rc = phonet_iface_ifdown();
    if(rc < 0)
        goto error;

    ipc_client_power_off(client);
    ipc_client_log(client, "aries_ipc_bootstrap: sent PHONE \"off\" command");
    usleep(1000);

    ipc_client_power_on(client);
    ipc_client_log(client, "aries_ipc_bootstrap: sent PHONE \"on\" command");
    usleep(200000);

    ipc_client_log(client, "aries_ipc_bootstrap: open s3c2410_serial3");
    s3c2410_serial3_fd=open("/dev/s3c2410_serial3", O_RDWR);
    if(s3c2410_serial3_fd < 0)
        goto error;

    /* Setup the s3c2410 serial. */
    ipc_client_log(client, "aries_ipc_bootstrap: setup s3c2410_serial3");
    tcgetattr(s3c2410_serial3_fd, &termios);

    cfmakeraw(&termios);
    cfsetispeed(&termios, B115200);
    cfsetospeed(&termios, B115200);

    tcsetattr(s3c2410_serial3_fd, TCSANOW, &termios);

    /* Send 'AT' in ASCII. */
    ipc_client_log(client, "aries_ipc_bootstrap: sending AT in ASCII");
    for(i=0 ; i < 20 ; i++)
    {
        rc = write(s3c2410_serial3_fd, "AT", 2);
        usleep(50000);
    }
    ipc_client_log(client, "aries_ipc_bootstrap: sending AT in ASCII done");

    usleep(50000); //FIXME

    /* Write the first part of modem.img. */
    FD_ZERO(&fds);
    FD_SET(s3c2410_serial3_fd, &fds);

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    /* Get and check bootcore version. */
    read(s3c2410_serial3_fd, &bootcore_version, sizeof(bootcore_version));
    ipc_client_log(client, "aries_ipc_bootstrap: got bootcore version: 0x%x", bootcore_version);

    if(bootcore_version != BOOTCORE_VERSION)
        goto error;

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    /* Get info_size. */
    read(s3c2410_serial3_fd, &info_size, sizeof(info_size));
    ipc_client_log(client, "aries_ipc_bootstrap: got info_size: 0x%x", info_size);

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    /* Send PSI magic. */
    data=PSI_MAGIC;
    write(s3c2410_serial3_fd, &data, sizeof(data));
    ipc_client_log(client, "aries_ipc_bootstrap: sent PSI_MAGIC (0x%x)", PSI_MAGIC);

    /* Send PSI data len. */
    data_16=PSI_DATA_LEN;
    data_p=(uint8_t *)&data_16;

    for(i=0 ; i < 2 ; i++)
    {
        write(s3c2410_serial3_fd, data_p, 1);
        data_p++;
    }
    ipc_client_log(client, "aries_ipc_bootstrap: sent PSI_DATA_LEN (0x%x)", PSI_DATA_LEN);

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    data_p=radio_img_p;

    ipc_client_log(client, "aries_ipc_bootstrap: sending the first part of radio.img");

    for(i=0 ; i < PSI_DATA_LEN ; i++)
    {
        if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
        {
            ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
            goto error;
        }

        write(s3c2410_serial3_fd, data_p, 1);
        crc_byte=crc_byte ^ *data_p;

        data_p++;
    }

    ipc_client_log(client, "aries_ipc_bootstrap: first part of radio.img sent; crc_byte is 0x%x", crc_byte);

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    write(s3c2410_serial3_fd, &crc_byte, sizeof(crc_byte));

    ipc_client_log(client, "aries_ipc_bootstrap: crc_byte sent");

    data = 0;
    for(i = 0 ; data != 0x01 ; i++)
    {
        timeout.tv_sec=5;
        timeout.tv_usec=0;

        if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
        {
            ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
            goto error;
        }

        read(s3c2410_serial3_fd, &data, sizeof(data));

        if(i > 50)
        {
            ipc_client_log(client, "aries_ipc_bootstrap: fairly too much attempts to get ACK");
            goto error;
        }
    }

    ipc_client_log(client, "aries_ipc_bootstrap: close s3c2410_serial3");
    close(s3c2410_serial3_fd);

    FD_ZERO(&fds);
    FD_SET(onedram_fd, &fds);

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    ipc_client_log(client, "aries_ipc_bootstrap: wait for 0x12341234 from onedram");
    if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    read(onedram_fd, &onedram_data, sizeof(onedram_data));

    if(onedram_data != ONEDRAM_INIT_READ)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: wrong onedram init magic (got 0x%04x)", onedram_data);
        goto error;
    }

    ipc_client_log(client, "aries_ipc_bootstrap: got 0x%04x", onedram_data);

    ipc_client_log(client, "aries_ipc_bootstrap: writing the rest of modem.img to onedram.");

    /* Pointer to the remaining part of radio.img. */
    data_p=radio_img_p + PSI_DATA_LEN;

    onedram_p = mmap(NULL, ONENAND_MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, onedram_fd, 0);

    if(onedram_p == NULL || onedram_p < 0 || onedram_p == 0xffffffff)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: could not map onedram to memory");
        goto error;
    }
 
    // it sometimes hangs here

    memcpy(onedram_p, data_p, RADIO_IMG_READ_SIZE - PSI_DATA_LEN);

    free(radio_img_p);

    /* nv_data part. */

    /* Check if all the nv_data files are ok. */
    if (nv_data_check(client) < 0)
        goto error;

    /* Check if the MD5 is ok. */
    if (nv_data_md5_check(client) < 0)
        goto error;

    /* Write nv_data.bin to modem_ctl. */
    ipc_client_log(client, "aries_ipc_bootstrap: write nv_data to onedram");

    nv_data_p = ipc_client_file_read(client, nv_data_path(client), nv_data_size(client), nv_data_chunk_size(client));
    if (nv_data_p == NULL)
        goto error;
    data_p = nv_data_p;

    memcpy(onedram_p + RADIO_IMG_MAX_SIZE, data_p, nv_data_size(client));

    free(nv_data_p);

    munmap(onedram_p, ONENAND_MAP_SIZE);

    if(ioctl(onedram_fd, ONEDRAM_REL_SEM) < 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: ONEDRAM_REL_SEM ioctl on onedram failed");
        goto error;
    }

    onedram_data = ONEDRAM_DEINIT_CMD;

    timeout.tv_sec=5;
    timeout.tv_usec=0;

    ipc_client_log(client, "aries_ipc_bootstrap: send 0x%04x", onedram_data);
    write(onedram_fd, &onedram_data, sizeof(onedram_data));

    if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: select timeout passed");
        goto error;
    }

    read(onedram_fd, &onedram_data, sizeof(onedram_data));

    if(onedram_data != ONEDRAM_DEINIT_READ)
    {
        ipc_client_log(client, "aries_ipc_bootstrap: wrong onedram deinit magic (got 0x%04x)", onedram_data);
        goto error;
    }

    ipc_client_log(client, "aries_ipc_bootstrap: got 0x%04x", onedram_data);

    close(onedram_fd);

    rc = 0;
    goto exit;

error:
    ipc_client_log(client, "aries_ipc_bootstrap: something went wrong");
    rc = -1;

exit:
    ipc_client_log(client, "aries_ipc_bootstrap: exit");
    return rc;
}
Example #18
0
/*======================================
*Function: set_com_config
*description:Setting up the serial port parameters 
*Author: zorro
*Created Date :6/18/2015
*======================================*/
int
set_com_config(int fd, int baud_rate, int data_bits, char parity, int stop_bits)
{
	struct termios options;
	int speed;

	/*Save and test the existing serial interface parameter Settings, here if the serial number and other errors,
	   There will be a relevant error message*/
	if (tcgetattr(fd, &options) != 0){
		perror("tcgetattr");
		printf("\n\n\nzorro, tcgetattr err: %s\n", strerror(errno));
		return -1;
	}

	/*set the character size*/
	cfmakeraw(&options); /*configured to the original model*/
	options.c_cflag &= ~CSIZE;

	/*set the baud rate*/
	switch (baud_rate){
		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;
		default:
		case 115200:
		{
			speed = B115200;
		}
		break;
	}
	cfsetispeed(&options, speed);
	cfsetospeed(&options, speed);

	/* set the stop bit */
	switch (data_bits){
		case 7:
		{
			options.c_cflag |= CS7;
		}
		break;

		default:
		case 8:
		{
			options.c_cflag |= CS8;
		}
		break;
	}

	/* Set the parity bit */
	switch (parity){
		default:
		case  'n':
		case  'N':
		{
			options.c_cflag &= ~PARENB;
			options.c_iflag &= ~INPCK;
		}
		break;

		case  'o':
		case  'O':
		{
			options.c_cflag |= (PARODD | PARENB);
			options.c_iflag |= INPCK;
		}
		break;

		case 'e':
		case 'E':
		{
			options.c_cflag |= PARENB;
			options.c_cflag &= ~PARODD;
			options.c_iflag |= INPCK;
		}
		break;

		case 's': /*as no parity*/
		case 'S':
		{
			options.c_cflag &= ~PARENB;
			options.c_cflag &= ~CSTOPB;
		}
		break;			
	}

	switch (stop_bits){
		default:
		case  1:
		{
			options.c_cflag &= ~CSTOPB;
		}
		break;

		case 2:
		{
			options.c_cflag |= CSTOPB;
		}
		break;
	}

	/*Set the waiting time and minimum received characters*/
	options.c_cc[VTIME] = 0;
	options.c_cc[VMIN] = 1;

	/*Handle not receive characters, clean the receive or transmit buff*/
	tcflush(fd, TCIFLUSH);
	
	/*Activate the new configuration*/
	if ((tcsetattr(fd, TCSANOW, &options)) !=0){
		perror("tcsetattr");
		printf("\n\n\nzorro, tcsetattr err: %s\n", strerror(errno));
		return -1;
	}
	return 0;
}
Example #19
0
int COM_Setup(int fd, COM_PARAM param)
{
	int ret = -1;
	struct termios termios_old;
	struct termios termios_new;
	int baudrate = 0;
	int fctl = 0;
	int databit = 0;
	int stopbit = 0;
	int parity = 0;
	
	if (fd < 0)
	{
		return -1;
	}
	
	bzero(&termios_old, sizeof(termios_old));
	bzero(&termios_new, sizeof(termios_new));
	
	cfmakeraw(&termios_new);
	tcgetattr(fd, &termios_old);
	
	// baudrates
	baudrate = convert_baudrate(param.nBaudRate);
	cfsetispeed(&termios_new, baudrate);		
	cfsetospeed(&termios_new, baudrate);		
	termios_new.c_cflag |= CLOCAL;			
	termios_new.c_cflag |= CREAD;
				
	fctl = param.nFlowCtrl;
	switch (fctl)
	{
	case '0':
		termios_new.c_cflag &= ~CRTSCTS;				// no flow control
		break;
		
	case '1':
		termios_new.c_cflag |= CRTSCTS;	    		// hardware flow control
		break;
		
	case '2':
		termios_new.c_iflag |= IXON | IXOFF |IXANY;  	//software flow control
		break;
	}

	// data bits
	termios_new.c_cflag &= ~CSIZE;		
	databit = param.nDataBits;
	switch (databit)
	{
	case '5':
		termios_new.c_cflag |= CS5;
		break;
		
	case '6':
		termios_new.c_cflag |= CS6;
		break;
		
	case '7':
		termios_new.c_cflag |= CS7;
		break;
		
	default:
		termios_new.c_cflag |= CS8;
		break;
	}

	// parity check
	parity = param.nParity;
	switch (parity)
	{
	case '0':
		termios_new.c_cflag &= ~PARENB;		// no parity check
		break;
		
	case '1':
		termios_new.c_cflag |= PARENB;			// odd check
		termios_new.c_cflag &= ~PARODD;
		break;
		
	case '2':
		termios_new.c_cflag |= PARENB;			// even check
		termios_new.c_cflag |= PARODD;
		break;
	}

	// stop bits
	stopbit = param.nStopBits;
	if (stopbit == '2')
	{
		termios_new.c_cflag |= CSTOPB;			// 2 stop bits
	}
	else
	{
		termios_new.c_cflag &= ~CSTOPB;		// 1 stop bits
	}

	//other attributions default
	termios_new.c_oflag &= ~OPOST;			
	termios_new.c_cc[VMIN]  = 1;		
	termios_new.c_cc[VTIME] = 1;				// unit: (1/10)second

	tcflush(fd, TCIFLUSH);			
	ret = tcsetattr(fd, TCSANOW, &termios_new);	// TCSANOW
	tcgetattr(fd, &termios_old);
	
	return ret;		
	
}
Example #20
0
int rcmIfInit(rcmIfType ifType, char *destAddr)
{
    unsigned radioIpAddr;

    switch (ifType)
    {
        case rcmIfIp:
#ifdef WIN32
            {
                // Initialize Windows sockets
                WSADATA wsad;
                memset(&wsad, 0, sizeof(wsad));
                WSAStartup(MAKEWORD(2, 2), &wsad);
            }
#endif

            // convert from string to binary
            radioIpAddr = inet_addr(destAddr);

            // make sure IP address is valid
            if (radioIpAddr == INADDR_NONE)
            {
                printf("Invalid IP address.\n");
                return ERR;
            }

            // create UDP socket
            radioFd = (int)socket(AF_INET, SOCK_DGRAM, 0);
            if (radioFd == -1)
            {
                printf("Unable to open socket");
                return ERR;
            }

            // initialize radio address structure
            memset(&radioAddr, 0, sizeof(radioAddr));
            radioAddr.sin_family = AF_INET;
            radioAddr.sin_port = htons(RCM_SOCKET_PORT_NUM);
            radioAddr.sin_addr.s_addr = radioIpAddr;
            break;

        case rcmIfSerial:
        case rcmIfUsb:
#ifdef WIN32
            {
                DCB dcbSerialParams = {0};
                wchar_t wcs[100];
                char comPortStr[100];

                // Windows requirement for COM ports above 9
                // but works for all
                sprintf(comPortStr, "\\\\.\\%s", destAddr);
                mbstowcs(wcs, comPortStr, sizeof(wcs));
                hComm = CreateFile(wcs,
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        0,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        0);
                if (hComm == INVALID_HANDLE_VALUE)
                {
                    printf("Can't open serial port\n");
                    return ERR;
                }

                dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
                GetCommState(hComm, &dcbSerialParams);
                dcbSerialParams.BaudRate=CBR_115200;
                dcbSerialParams.ByteSize=8;
                dcbSerialParams.StopBits=ONESTOPBIT;
                dcbSerialParams.Parity=NOPARITY;
                SetCommState(hComm, &dcbSerialParams);
            }
#else
            {
                struct termios term;

                radioFd = open(destAddr, O_RDWR|O_NOCTTY|O_NONBLOCK);
                if (radioFd < 0)
                {
                    printf("Can't open serial port\n");
                    return ERR;
                }
                tcgetattr(radioFd, &term);

                memset(&term, 0, sizeof(term));
                cfmakeraw(&term);
                term.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
                term.c_iflag = IGNPAR;
                                                
                tcflush(radioFd, TCIFLUSH);

                tcsetattr(radioFd, TCSANOW, &term);
            }
#endif
            break;

        default:
            printf("Unknown interface type.\n");
            exit(-1);
            break;
    }
    rcmIf = ifType;

    return OK;
}
Example #21
0
// -1 ---> Not Change
int RS232Interface::SetSerialParams(long speed, int bits, int parity, int stops, int flow_control)
{
	int result = E2ERR_OPENFAILED;

#ifdef	_WINDOWS
	if (hCom != INVALID_HANDLE_VALUE)
	{
		if (speed >= 300 && speed <= 115200)
			actual_speed = speed;
		if (bits >= 1 && bits <= 16)
			actual_bits = bits;
		if (parity == 'N' || parity == 'E' || parity == 'O')
			actual_parity = parity;
		if (stops >= 1 && stops <= 2)
			actual_stops = stops;
		if (flow_control >= 0 && flow_control <= 2)
			actual_flowcontrol = flow_control;

		char dcb_str[256];
		DCB com_dcb;

		if ( GetCommState(hCom, &com_dcb) )
		{
			snprintf(dcb_str, 256, "baud=%ld parity=%c data=%d stop=%d", actual_speed, actual_parity, actual_bits, actual_stops);
			dcb_str[255] = '\0';
			if ( BuildCommDCB(dcb_str, &com_dcb) )
			{
				if (actual_flowcontrol == 0)
				{
					com_dcb.fDtrControl = DTR_CONTROL_DISABLE;
					com_dcb.fRtsControl = RTS_CONTROL_DISABLE;
				}

				if ( SetCommState(hCom, &com_dcb) )
					result = OK;
				else
					result = GetLastError();

				PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);
			}
		}
	}
#else
# ifdef	_LINUX_
	if ( fd != INVALID_HANDLE_VALUE )
	{
		if (speed >= 300 && speed <= 115200)
			actual_speed = speed;
		if (bits >= 1 && bits <= 16)
			actual_bits = bits;
		if (parity == 'N' || parity == 'E' || parity == 'O')
			actual_parity = parity;
		if (stops >= 1 && stops <= 2)
			actual_stops = stops;
		if (flow_control >= 0 && flow_control <= 2)
			actual_flowcontrol = flow_control;

		struct termios termios;

		if ( tcgetattr(fd, &termios) != 0 )
			return result;

		cfmakeraw(&termios);
		termios.c_cflag |= CLOCAL;		//Disable modem status line check

		//Flow control
		if (actual_flowcontrol == 0)
		{
			termios.c_cflag &= ~CRTSCTS;	//Disable hardware flow control
			termios.c_iflag &= ~(IXON|IXOFF);	//Disable software flow control
		}
		else
		if (actual_flowcontrol == 1)
		{
			termios.c_cflag |= CRTSCTS;
			termios.c_iflag &= ~(IXON|IXOFF);
		}
		else
		{
			termios.c_cflag &= ~CRTSCTS;
			termios.c_iflag |= (IXON|IXOFF);
		}

		//Set size of bits
		termios.c_cflag &= ~CSIZE;
		if (actual_bits <= 5)
			termios.c_cflag |= CS5;
		else
		if (actual_bits == 6)
			termios.c_cflag |= CS6;
		else
		if (actual_bits == 7)
			termios.c_cflag |= CS7;
		else
			termios.c_cflag |= CS8;

		//Set stop bits
		if (actual_stops == 2)
			termios.c_cflag |= CSTOPB;
		else
			termios.c_cflag &= ~CSTOPB;

		//Set parity bit
		if (actual_parity == 'N')
		{
			termios.c_cflag &= ~PARENB;
		}
		else
		if (actual_parity == 'E')
		{
			termios.c_cflag |= PARENB;
			termios.c_cflag &= ~PARODD;
		}
		else
		{	//'O'
			termios.c_cflag |= (PARENB|PARODD);
		}

		//Set speed
		speed_t baudrate;
		switch (speed)
		{
		case 300:
			baudrate = B300;
			break;
		case 600:
			baudrate = B600;
			break;
		case 1200:
			baudrate = B1200;
			break;
		case 2400:
			baudrate = B2400;
			break;
		case 4800:
			baudrate = B4800;
			break;
		case 9600:
			baudrate = B9600;
			break;
		case 19200:
			baudrate = B19200;
			break;
		case 38400:
			baudrate = B38400;
			break;
		case 57600:
			baudrate = B57600;
			break;
		case 115200:
			baudrate = B115200;
			break;
		case 230400:
			baudrate = B230400;
			break;
		default:
			baudrate = B9600;
			break;
		}
		cfsetispeed(&termios,baudrate);
		cfsetospeed(&termios,baudrate);

		termios.c_cc[VMIN] = 1;
		termios.c_cc[VTIME] = 0;

		if ( tcsetattr(fd, TCSANOW, &termios) == 0 )
			result = OK;
	}
# endif
#endif

	return result;
}
Example #22
0
int OpenSerial(int *fd, char *SerialName, speed_t baudrate)
{
    struct termios SerialConfig;
    memset(&SerialConfig, 0, sizeof(SerialConfig));
    
    if(_DEBUG)
        printf("Opening serial port: %s...", SerialName);
    
    *fd = open(SerialName, O_RDWR);
    if(*fd < 0)
    {
        if(_DEBUG)
            printf("Fail\n");
        return -1;
    }
    else
    {
        if(_DEBUG)
            printf("Done\n");
            
        if(_DEBUG)
            printf("Getting current configuration...");
        
        if(tcgetattr(*fd, &SerialConfig) != 0)
        {
            if(_DEBUG)
                printf("Fail\n");
            return -1;
        }
        else
        {
            if(_DEBUG)
                printf("Done\n");
        }
    }
    
    if(_DEBUG)
        printf("Setting in/out speed...");
    cfsetispeed(&SerialConfig, baudrate);
    cfsetospeed(&SerialConfig, baudrate);
    cfmakeraw(&SerialConfig);
    if(_DEBUG)
        printf("Done\n");
        
    if(_DEBUG)
       printf("Flushing serial port...");
       tcflush(*fd, TCIFLUSH);
    if(_DEBUG)
       printf("Done\n");
        
    if(_DEBUG)
        printf("Applying new configuration...");
        if(tcsetattr(*fd, TCSANOW, &SerialConfig) != 0)
        {
            if(_DEBUG)
                printf("Fail\n");
            return -1;
        }
        else
        {
            if(_DEBUG)
                printf("Done\n");
            return 0;
        }
    
    
    
}
Example #23
0
/**
 * open serial port
 * @return file descriptor of the serial port, or
 * 	SER_ERR,if failed
 */
int serialOpen(const char *device, unsigned int baudrate, unsigned int bits, char parity, unsigned int stop)
{
	int fd=-1;
	struct termios optnew;
	speed_t speed = B19200;

	/* Open serial device */
	fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
	if ( fd >0 ) {
		/* Read with blocking behavior */
		fcntl(fd, F_SETFL, 0);

		/* Get current option */
		tcgetattr(fd, &optnew);

		/* initialize new option to raw input/output */
//		memset(&optnew, 0, sizeof(optnew));
		cfmakeraw(&optnew);
		optnew.c_cc[VMIN ] = 0;
		optnew.c_cc[VTIME] = TIMEDOUT*10;

		/* set baudrate */
		switch (baudrate) {
			case   1200: speed =   B1200;  break;
			case   1800: speed =   B1800;  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;
			default:    speed = B19200; break;
		}
		cfsetispeed(&optnew, speed);
		cfsetospeed(&optnew, speed);

		/* Set data bits */
		optnew.c_cflag &= ~CSIZE;
		optnew.c_cflag &= ~CRTSCTS;
		optnew.c_iflag &= ~(ICRNL|IXON);
		optnew.c_cflag |= CLOCAL | CREAD;
		optnew.c_oflag &= ~OPOST;

		switch (bits) {
			case 5: optnew.c_cflag |= CS5; break;
			case 6: optnew.c_cflag |= CS6; break;
			case 7: optnew.c_cflag |= CS7; break;
			default :
				optnew.c_cflag |= CS8; break;
		}

		/* Set parity checking */
		optnew.c_cflag |= PARENB;
		switch (parity) {
			case 'e':
			case 'E': optnew.c_cflag &= ~PARODD; break;
			case 'o':
			case 'O': optnew.c_cflag &=  PARODD; break;
			default :
				optnew.c_cflag &= ~PARENB; break;
		}

		/* Set stop bit(s) */
		if (stop == 2)
			optnew.c_cflag &=  CSTOPB;
		else
			optnew.c_cflag &= ~CSTOPB;
	
		optnew.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );

		/* Apply new option */
		tcsetattr(fd, TCSANOW, &optnew);
	}
	return fd;
}
Example #24
0
/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval <0      Error.
 */
MODULE_EXPORT int
NoritakeVFD_init (Driver *drvthis)
{
	struct termios portset;
	int tmp, w, h;
	int reboot = 0;
	char size[200] = DEFAULT_SIZE;

	PrivateData *p;

	/* Allocate and store private data */
	p = (PrivateData *) calloc(1, sizeof(PrivateData));
	if (p == NULL)
		return -1;
	if (drvthis->store_private_ptr(drvthis, p))
		return -1;

	/* Initialize the PrivateData structure */
	p->fd = -1;
	p->cellwidth = DEFAULT_CELL_WIDTH;
	p->cellheight = DEFAULT_CELL_HEIGHT;
	p->ccmode = standard;

	debug(RPT_INFO, "%s(%p)", __FUNCTION__, drvthis);

	/* Read config file */
	/* Which device should be used */
	strncpy(p->device, drvthis->config_get_string(drvthis->name, "Device", 0, DEFAULT_DEVICE), sizeof(p->device));
	p->device[sizeof(p->device)-1] = '\0';
	report(RPT_INFO, "%s: using Device %s", drvthis->name, p->device);

	/* Which size */
	strncpy(size, drvthis->config_get_string(drvthis->name, "Size", 0, DEFAULT_SIZE), sizeof(size));
	size[sizeof(size)-1] = '\0';
	if ((sscanf(size, "%dx%d", &w, &h) != 2)
	    || (w <= 0) || (w > LCD_MAX_WIDTH)
	    || (h <= 0) || (h > LCD_MAX_HEIGHT)) {
		report(RPT_WARNING, "%s: cannot parse Size: %s; using default %s",
			drvthis->name, size, DEFAULT_SIZE);
		sscanf(DEFAULT_SIZE, "%dx%d", &w, &h);
	}
	p->width = w;
	p->height = h;

	/* Which backlight brightness */
	tmp = drvthis->config_get_int(drvthis->name, "Brightness", 0, DEFAULT_BRIGHTNESS);
	if ((tmp < 0) || (tmp > 1000)) {
		report(RPT_WARNING, "%s: Brightness must be between 0 and 1000; using default %d",
			drvthis->name, DEFAULT_BRIGHTNESS);
		tmp = DEFAULT_BRIGHTNESS;
	}
	p->brightness = tmp;

	/* Which backlight-off "brightness" */
	tmp = drvthis->config_get_int(drvthis->name, "OffBrightness", 0, DEFAULT_OFFBRIGHTNESS);
	debug(RPT_INFO, "%s: OffBrightness (in config) is '%d'", __FUNCTION__, tmp);
	if ((tmp < 0) || (tmp > 1000)) {
		report(RPT_WARNING, "%s: OffBrightness must be between 0 and 1000; using default %d",
			drvthis->name, DEFAULT_OFFBRIGHTNESS);
		tmp = DEFAULT_OFFBRIGHTNESS;
	}
	p->offbrightness = tmp;

	/* Which speed */
	tmp = drvthis->config_get_int(drvthis->name, "Speed", 0, DEFAULT_SPEED);
	if ((tmp != 1200) && (tmp != 2400) && (tmp != 9600) && (tmp != 19200) && (tmp != 115200)) {
		report(RPT_WARNING, "%s: Speed must be 1200, 2400, 9600, 19200 or 115200; using default %d",
			drvthis->name, DEFAULT_SPEED);
		tmp = DEFAULT_SPEED;
	}
	if (tmp == 1200) p->speed = B1200;
	else if (tmp == 2400) p->speed = B2400;
	else if (tmp == 9600) p->speed = B9600;
	else if (tmp == 19200) p->speed = B19200;
	else if (tmp == 115200) p->speed = B115200;

	/* Which parity */
	tmp = drvthis->config_get_int(drvthis->name, "Parity", 0, DEFAULT_PARITY);
	if ((tmp != 0) && (tmp != 1) && (tmp != 2) ) {
		report(RPT_WARNING, "%s: Parity must be 0(=none), 1(=odd), 2(=even); using default %d",
			drvthis->name, DEFAULT_PARITY);
		tmp = DEFAULT_PARITY;
	}
	if (tmp != 0)
		p->parity = (tmp & 1) ? (PARENB | PARODD) : PARENB;


	/* Reboot display? */
	reboot = drvthis->config_get_bool(drvthis->name, "Reboot", 0, 0);

	/* Set up io port correctly, and open it...*/
	debug(RPT_DEBUG, "%s: Opening device: %s", __FUNCTION__, p->device);
	p->fd = open(p->device, O_RDWR | O_NOCTTY | O_NDELAY);

	if (p->fd == -1) {
		report(RPT_ERR, "%s: open() of %s failed (%s)", drvthis->name, p->device, strerror(errno));
		return -1;
	}

	tcgetattr(p->fd, &portset);

	// We use RAW mode (with varying parity)
#ifdef HAVE_CFMAKERAW
	// The easy way
	cfmakeraw(&portset);
	// set parity the way we want it
	portset.c_cflag &= ~(PARENB | PARODD);
	portset.c_cflag |= p->parity;
#else
	// The hard way
	portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP
   	                   | INLCR | IGNCR | ICRNL | IXON );
	portset.c_oflag &= ~OPOST;
	portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN );
	portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS );
	portset.c_cflag |= CS8 | CREAD | CLOCAL | p->parity;
#endif

	// Set port speed
	cfsetospeed(&portset, p->speed);
	cfsetispeed(&portset, B0);

	// Do it...
	tcsetattr(p->fd, TCSANOW, &portset);

	/* make sure the frame buffer is there... */
	p->framebuf = (unsigned char *) malloc(p->width * p->height);
	if (p->framebuf == NULL) {
		report(RPT_ERR, "%s: unable to create framebuffer", drvthis->name);
		return -1;
	}
	memset(p->framebuf, ' ', p->width * p->height);

	/* make sure the framebuffer backing store is there... */
	p->backingstore = (unsigned char *) malloc(p->width * p->height);
	if (p->backingstore == NULL) {
		report(RPT_ERR, "%s: unable to create framebuffer backing store", drvthis->name);
		return -1;
	}
	memset(p->backingstore, ' ', p->width * p->height);


	/* Set display-specific stuff..*/
	if (reboot) {
		NoritakeVFD_reboot(drvthis);
		sleep(4);
	}
	NoritakeVFD_hidecursor(drvthis);
	NoritakeVFD_autoscroll(drvthis, 0);
	NoritakeVFD_set_brightness(drvthis, 1, p->brightness);

	report(RPT_DEBUG, "%s: init() done", drvthis->name);

	return 0;

}
Example #25
0
int OpenSerialPort(int serialSpeed)
{
    struct termios originalTTYAttrs;
    struct termios options;
    
    int fileDescriptor = -1;
 
    // Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
    // The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
    // See open(2) ("man 2 open") for details.
 
    fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fileDescriptor == -1)
    {
        fileDescriptor = -2;
        printf("Failed to open /dev/tty.iap; errno: %s (%d)\n", strerror(errno), errno);
        goto error;
    }
 
    // Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
    // unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
    // processes.
    // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
 
    if (ioctl(fileDescriptor, TIOCEXCL) == -1)
    {
        fileDescriptor = -3;
        goto error;
    }
 
    // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
    // See fcntl(2) ("man 2 fcntl") for details.
 
    if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
    {
        fileDescriptor = -4;
        goto error;
    }
 
    // Get the current options and save them so we can restore the default settings later.
    if (tcgetattr(fileDescriptor, &originalTTYAttrs) == -1)
    {
        fileDescriptor = -5;
        goto error;
    }
 
    // The serial port attributes such as timeouts and baud rate are set by modifying the termios
    // structure and then calling tcsetattr() to cause the changes to take effect. Note that the
    // changes will not become effective without the tcsetattr() call.
    // See tcsetattr(4) ("man 4 tcsetattr") for details.
 
    options = originalTTYAttrs;
 
    // Set raw input (non-canonical) mode, with reads blocking until either a single character
    // has been received or a one second timeout expires.
    // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.
 
    cfmakeraw(&options);
    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 10;
 
    // The baud rate, word length, and handshake options can be set as follows:
 
    cfsetspeed(&options, serialSpeed);    // Set 19200 baud
    options.c_cflag |= (CS8);  // RTS flow control of input
 
 
    // Cause the new options to take effect immediately.
    if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
    {
        fileDescriptor = -6;
        goto error;
    }

    // Success
    return fileDescriptor;
 
    // Failure "/dev/tty.iap"
error:
    if (fileDescriptor != -1)
    {
        close(fileDescriptor);
    }
 
    return fileDescriptor;
}
Example #26
0
static void open_audio(struct modem_data *modem)
{
	GIOChannel *channel;
	struct termios ti;
	int fd;

	if (modem->is_huawei == FALSE)
		return;

	if (modem->audio_users > 0)
		return;

	g_print("enabling audio\n");

	modem->dsp_out = open("/dev/dsp", O_WRONLY, 0);
	if (modem->dsp_out < 0) {
		g_printerr("Failed to open DSP device\n");
		return;
	}

	if (ioctl(modem->dsp_out, SNDCTL_DSP_SETFMT, &modem->format) < 0)
		g_printerr("Failed to set DSP format\n");

	if (ioctl(modem->dsp_out, SNDCTL_DSP_CHANNELS, &modem->channels) < 0)
		g_printerr("Failed to set DSP channels\n");

	if (ioctl(modem->dsp_out, SNDCTL_DSP_SPEED, &modem->speed) < 0)
		g_printerr("Failed to set DSP speed\n");

	modem->dsp_in = open("/dev/dsp", O_RDONLY, 0);
	if (modem->dsp_in < 0) {
		g_printerr("Failed to open DSP device\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		return;
	}

	if (ioctl(modem->dsp_in, SNDCTL_DSP_SETFMT, &modem->format) < 0)
		g_printerr("Failed to set DSP input format\n");

	if (ioctl(modem->dsp_in, SNDCTL_DSP_CHANNELS, &modem->channels) < 0)
		g_printerr("Failed to set DSP input channels\n");

	if (ioctl(modem->dsp_in, SNDCTL_DSP_SPEED, &modem->speed) < 0)
		g_printerr("Failed to set DSP input speed\n");

	fd = open(option_device, O_RDWR | O_NOCTTY);
	if (fd < 0) {
		g_printerr("Failed to open audio port\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		close(modem->dsp_in);
		modem->dsp_in = -1;
		return;
	}

	/* Switch TTY to raw mode */
	memset(&ti, 0, sizeof(ti));
	cfmakeraw(&ti);

	tcflush(fd, TCIOFLUSH);
	tcsetattr(fd, TCSANOW, &ti);

	channel = g_io_channel_unix_new(fd);
	if (channel == NULL) {
		g_printerr("Failed to create IO channel\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		close(modem->dsp_in);
		modem->dsp_in = -1;
		close(fd);
		return;
	}

	g_io_channel_set_close_on_unref(channel, TRUE);

	modem->audio_watch = g_io_add_watch(channel,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				audio_receive, modem);

	g_io_channel_unref(channel);

	modem->audio_users++;
}
Example #27
0
/* 
 * Setup serial port, return 0 if success.
 */
static int vtty_serial_setup(vtty_t *vtty, const vtty_serial_option_t *option)
{
   struct termios tio;
   int tio_baudrate;

   if (tcgetattr(vtty->fd_array[0], &tio) != 0) { 
      fprintf(stderr, "error: tcgetattr failed\n");
      return(-1);
   }

   cfmakeraw(&tio);

   tio.c_cflag = 0
      |CLOCAL     // ignore modem control lines
      ;

   tio.c_cflag &= ~CREAD;
   tio.c_cflag |= CREAD;

   switch(option->baudrate) {
      case 50     : tio_baudrate = B50; break;
      case 75     : tio_baudrate = B75; break;
      case 110    : tio_baudrate = B110; break;
      case 134    : tio_baudrate = B134; break;
      case 150    : tio_baudrate = B150; break;
      case 200    : tio_baudrate = B200; break;
      case 300    : tio_baudrate = B300; break;
      case 600    : tio_baudrate = B600; break;
      case 1200   : tio_baudrate = B1200; break;
      case 1800   : tio_baudrate = B1800; break;
      case 2400   : tio_baudrate = B2400; break;
      case 4800   : tio_baudrate = B4800; break;
      case 9600   : tio_baudrate = B9600; break;
      case 19200  : tio_baudrate = B19200; break;
      case 38400  : tio_baudrate = B38400; break;
      case 57600  : tio_baudrate = B57600; break;
#if defined(B76800)
      case 76800  : tio_baudrate = B76800; break;
#endif
      case 115200 : tio_baudrate = B115200; break;
#if defined(B230400)
      case 230400 : tio_baudrate = B230400; break;
#endif
      default:
         fprintf(stderr, "error: unsupported baudrate\n");
         return(-1);
   }

   cfsetospeed(&tio, tio_baudrate);
   cfsetispeed(&tio, tio_baudrate);

   tio.c_cflag &= ~CSIZE; /* clear size flag */
   switch(option->databits) {
      case 5 : tio.c_cflag |= CS5; break;
      case 6 : tio.c_cflag |= CS6; break;
      case 7 : tio.c_cflag |= CS7; break;
      case 8 : tio.c_cflag |= CS8; break;
      default :
         fprintf(stderr, "error: unsupported databits\n");
         return(-1);
   }

   tio.c_iflag &= ~INPCK;  /* clear parity flag */
   tio.c_cflag &= ~(PARENB|PARODD);
   switch(option->parity) {
      case 0 : break;
      case 2 : tio.c_iflag|=INPCK; tio.c_cflag|=PARENB; break;  /* even */
      case 1 : tio.c_iflag|=INPCK; tio.c_cflag|=PARENB|PARODD; break; /* odd */
      default:
         fprintf(stderr, "error: unsupported parity\n");
         return(-1);
   }

   tio.c_cflag &= ~CSTOPB; /* clear stop flag */
   switch(option->stopbits) {
      case 1 : break;
      case 2 : tio.c_cflag |= CSTOPB; break;
      default :
         fprintf(stderr, "error: unsupported stopbits\n");
         return(-1);
   }

#if defined(CRTSCTS)
   tio.c_cflag &= ~CRTSCTS;
#endif
#if defined(CNEW_RTSCTS)
   tio.c_cflag &= ~CNEW_RTSCTS;
#endif
   if (option->hwflow) {
#if defined(CRTSCTS)
      tio.c_cflag |= CRTSCTS;
#else
      tio.c_cflag |= CNEW_RTSCTS;
#endif
   }

   tio.c_cc[VTIME] = 0;
   tio.c_cc[VMIN] = 1; /* block read() until one character is available */

#if 0
   /* not neccessary unless O_NONBLOCK used */
   if (fcntl(vtty->fd_array[0], F_SETFL, 0) != 0) {  /* enable blocking mode */
      fprintf(stderr, "error: fnctl F_SETFL failed\n");
      return(-1);
   }
#endif
  
   if (tcflush(vtty->fd_array[0], TCIOFLUSH) != 0) {
      fprintf(stderr, "error: tcflush failed\n");
      return(-1);
   }
  
   if (tcsetattr(vtty->fd_array[0], TCSANOW, &tio) != 0 ) {
      fprintf(stderr, "error: tcsetattr failed\n");
      return(-1);
   }

   return(0);
}
Example #28
0
BTServer::~BTServer()
{

	printf("Disconnected\n");
	close(this->bt_so);

#if 0 
	if (linger) {
		struct linger l = { l_onoff = 1, l_linger = linger };

		if (setsockopt(nsk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
			perror("Can't set linger option");
			close(nsk);
			return;
		}
	}
#endif

// serial tty ding anlegen ??????????????????
/*
	memset(&req, 0, sizeof(req));
	req.dev_id = dev;
	req.flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP);

	bacpy(&req.src, &laddr.rc_bdaddr);
	bacpy(&req.dst, &raddr.rc_bdaddr);
	req.channel = raddr.rc_channel;

	dev = ioctl(nsk, RFCOMMCREATEDEV, &req);
	if (dev < 0) {
		perror("Can't create RFCOMM TTY");
		close(sk);
		return;
	}

	snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
	while ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0) {
		if (errno == EACCES) {
			perror("Can't open RFCOMM device");
			goto release;
		}

		snprintf(devname, MAXPATHLEN - 1, "/dev/bluetooth/rfcomm/%d", dev);
		if ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0) {
			if (ntry--) {
				snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
				usleep(100);
				continue;
			}
			perror("Can't open RFCOMM device");
			goto release;
		}
	}

*/

#if 0
	if (rfcomm_raw_tty) {
		tcflush(fd, TCIOFLUSH);

		cfmakeraw(&ti);
		tcsetattr(fd, TCSANOW, &ti);
	}
#endif



/*
	close(sk);

	memset(&sa, 0, sizeof(sa));
	sa.sa_flags   = SA_NOCLDSTOP;
	sa.sa_handler = SIG_IGN;
	sigaction(SIGCHLD, &sa, NULL);
	sigaction(SIGPIPE, &sa, NULL);

	sa.sa_handler = sig_term;
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT,  &sa, NULL);

	sa.sa_handler = sig_hup;
	sigaction(SIGHUP, &sa, NULL);

	sigfillset(&sigs);
	sigdelset(&sigs, SIGCHLD);
	sigdelset(&sigs, SIGPIPE);
	sigdelset(&sigs, SIGTERM);
	sigdelset(&sigs, SIGINT);
	sigdelset(&sigs, SIGHUP); */

/*
	p.fd = fd;
	p.events = POLLERR | POLLHUP;

	if (argc <= 2) {
		while (!__io_canceled) {
			p.revents = 0;
			if (ppoll(&p, 1, NULL, &sigs) > 0)
				break;
		}
	} else
		run_cmdline(&p, &sigs, devname, argc - 2, argv + 2);
*/

/*
	sa.sa_handler = NULL;
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT,  &sa, NULL);
	close(fd);
	*/


	return;

/*
release:
	memset(&req, 0, sizeof(req));
	req.dev_id = dev;
	req.flags = (1 << RFCOMM_HANGUP_NOW);
	ioctl(ctl, RFCOMMRELEASEDEV, &req);

	close(sk);
*/
}
Example #29
0
void open_cm(char *dev_name, struct CMInfoStruct *cminfo)
{

  int fd;
  struct termios settings;

  bprintf(info, "Attempting to open %s motor at %s\n", 
          cminfo->motorstr, dev_name);

  /*for some reason, setting O_NDELAY below was necessary to prevent this 
    process from waiting for open() */

  if( (fd = open(dev_name, O_RDWR | O_NDELAY | O_NOCTTY)) < 0) {
    berror(err, "Error opening %s motor device %s", cminfo->motorstr, dev_name);
    cminfo->open = 0;
    return;
  } else { 
    bprintf(info, "%s motor port %s is now open\n", cminfo->motorstr, dev_name);
    cminfo->fd = fd;
  }

  if(tcgetattr(fd, &settings) < 0) {
    berror(err, "tcgetattr failed");
    cminfo->open = 0;
    return;
  }
  
  #if 0
  /* clear Character size; set no parity bits; set 1 stop bit */
  settings.c_cflag &= ~(CSTOPB | CSIZE | PARENB);

  /* set 8 data bits; set local port; enable receiver */
  settings.c_cflag |= (CS8 | CLOCAL | CREAD);

  /* disable all software flow control */
  settings.c_iflag &= ~(IXON | IXOFF | IXANY);

  /* disable output processing (raw output) */
  settings.c_oflag &= ~OPOST;

  /* disable input processing (raw input) */
  settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  #endif

  cfmakeraw(&settings);

  if(cfsetospeed(&settings, B38400) < 0) {	
    berror(err, "Error setting output baud rate");
    cminfo->open = 0;
    return;  
  }

  if(cfsetispeed(&settings, B38400) < 0) {	
    berror(err, "Error setting input baud rate");
    cminfo->open = 0;
    return;      
  }

  if(tcsetattr(fd, TCSANOW, &settings) < 0) {
    berror(err,"tcsetattr failed");
    cminfo->open = 0;
    return;
  }

  cminfo->open = 1;
}
Example #30
0
int open_comport()
{
#ifdef ALLEGRO_WINDOWS
   DCB dcb;
   COMMTIMEOUTS timeouts;
   DWORD bytes_written;
   char temp_str[16];
#endif
   
   if (comport.status == READY)    // if the comport is open,
      close_comport();    // close it
   
#ifdef ALLEGRO_WINDOWS
   // Naming of serial ports 10 and higher: See
   // http://www.connecttech.com/KnowledgeDatabase/kdb227.htm
   // http://support.microsoft.com/?id=115831
   sprintf(temp_str, "\\\\.\\COM%i", comport.number + 1);
   com_port = CreateFile(temp_str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
   if (com_port == INVALID_HANDLE_VALUE)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
   
   // Setup comport
   GetCommState(com_port, &dcb);
   dcb.BaudRate = comport.baud_rate;
   dcb.ByteSize = 8;
   dcb.StopBits = ONESTOPBIT;
   dcb.fParity = FALSE;
   dcb.Parity = NOPARITY;
   dcb.fOutxCtsFlow = FALSE;
   dcb.fOutxDsrFlow = FALSE;
   dcb.fOutX = FALSE;
   dcb.fInX = FALSE;
   dcb.fDtrControl = DTR_CONTROL_ENABLE;
   dcb.fRtsControl = RTS_CONTROL_ENABLE;
   dcb.fDsrSensitivity = FALSE;
   dcb.fErrorChar = FALSE;
   dcb.fAbortOnError = FALSE;
   SetCommState(com_port, &dcb);
   
   // Setup comm timeouts
   timeouts.ReadIntervalTimeout = MAXWORD;
   timeouts.ReadTotalTimeoutMultiplier = 0;
   timeouts.ReadTotalTimeoutConstant = 0;
   timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER;
   timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT;
   SetCommTimeouts(com_port, &timeouts);
   // Hack to get around Windows 2000 multiplying timeout values by 15
   GetCommTimeouts(com_port, &timeouts);
   if (TX_TIMEOUT_MULTIPLIER > 0)
      timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER * TX_TIMEOUT_MULTIPLIER / timeouts.WriteTotalTimeoutMultiplier;
   if (TX_TIMEOUT_CONSTANT > 0)
      timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT * TX_TIMEOUT_CONSTANT / timeouts.WriteTotalTimeoutConstant;
   SetCommTimeouts(com_port, &timeouts);
   
   // If the port is Bluetooth, make sure device is active
   PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
   WriteFile(com_port, "?\r", 2, &bytes_written, 0);
   if (bytes_written != 2)  // If Tx timeout occured
   {
      PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
      CloseHandle(com_port);
      comport.status = NOT_OPEN; //port was not open
      return -1;
   }
#elif TERMIOS
    char tmp[54];
    if( comport.number < 100 )
        snprintf(tmp, sizeof(tmp), "/dev/ttyS%d", comport.number);
    else
        snprintf(tmp, sizeof(tmp), "/dev/ttyUSB%d", comport.number-100);
    fdtty = open( tmp, O_RDWR | O_NOCTTY );
    if (fdtty <0) { return(-1); }

    tcgetattr(fdtty,&oldtio); /* save current port settings */

    bzero(&newtio, sizeof(newtio));

    cfsetspeed(&newtio, comport.baud_rate);

    cfmakeraw(&newtio);
    newtio.c_cflag |= (CLOCAL | CREAD);

            // No parity (8N1):
    newtio.c_cflag &= ~PARENB;
    newtio.c_cflag &= ~CSTOPB;
    newtio.c_cflag &= ~CSIZE;
    newtio.c_cflag |= CS8;

        // disable hardware flow control
    newtio.c_cflag &= ~CRTSCTS ;

    newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
    newtio.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */

    tcflush(fdtty, TCIFLUSH);
    tcsetattr(fdtty,TCSANOW,&newtio);
#else
   com_port = comm_port_init(comport.number);
   if (!com_port) {
      write_log(szDZCommErr);
      comport.status = NOT_OPEN;
      return -1;
   }
   comm_port_set_baud_rate(com_port, comport.baud_rate);
   comm_port_set_parity(com_port, NO_PARITY);
   comm_port_set_data_bits(com_port, BITS_8);
   comm_port_set_stop_bits(com_port, STOP_1);
   comm_port_set_flow_control(com_port, NO_CONTROL);
   if (comm_port_install_handler(com_port) != 1)
   {
      write_log(szDZCommErr);
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
#endif
   
   serial_time_out = FALSE;
   comport.status = READY;
   
   return 0; // everything is okay
}