Ejemplo n.º 1
0
/**  Main function which starts the service, this will be an blocking function.If any error is there it returns immediately.
 * Returns immediately on error, else will be an blocking function.
 * @param port serial device port name.
 * @return error status of <b>open()</b> or <b>tcsetattr()</b>.
 * @note This is an blocking function, so it should be called from different thread.
 *
 * */
int gecko_start_serial_service(char * port)
{

	init_serial_lock();
	struct sigaction saio;
	saio.sa_handler = signal_callback_handler;
	sigemptyset(&saio.sa_mask);
	saio.sa_flags = 0;
	saio.sa_restorer = NULL;
	sigaction(SIGINT, &saio, NULL);

	//char * port = "/dev/ttyUSB0";

	LOGGER(LOG_DEBUG,"Using port %s", port);
	int fd = open_serial_port(port);
	if (fd == -1)
	{
		LOGGER(LOG_ERR,"error in open device %s",port);
		return EXIT_FAILURE;
	}
	if (configure_serial_port(fd) != -1)
	{
		if(LOG_LEVEL==LOG_DEBUG)
		read_display_current_configuration(fd);

		serial_write(fd, START_SCAN);
		LOGGER(LOG_DEBUG,"send start scan command to serial device\n");
		start_serial_reader_thread(fd);
	}
	else
	{
		LOGGER(LOG_CRIT,"Error in opening serial port\n");
		perror("error in configuring port");
	}

	return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int ret = 0;
    struct stat st;
    int status = 0;
    int opt;

    progname = argv[0];

    while ((opt = getopt (argc, argv, "D:b:fc:sh")) != -1)
    {
        switch (opt)
        {
            case 'D': device_path = optarg; if (strlen(device_path) == 0) syntax(); break;
            case 'b': baudrate = atoi(optarg); if (baudrate == 0) syntax(); break;
            case 'f': flowcontrol = 1; break;
            case 'c': csize = atoi(optarg); if ((csize < 5) || (csize > 8)) syntax(); break;
            case 's': twostopbits = 1; break;
            case 'h': // fall through
            default:
                syntax();
                break;
        }
    }

    if (device_path == NULL)
        syntax();

    if (stat(device_path, &st) != 0)
    {
        fprintf(stderr, "Error: %s does not exist\n", device_path);
        ret = EXIT_FAILURE;
        goto main_init_error;
    }

    serial_fd = open(device_path, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
    if (serial_fd < 0)
    {
        fprintf(stderr, "Error: unable to open %s\n", device_path);
        ret = EXIT_FAILURE;
        goto main_init_error;
    }

    if (configure_serial_port(serial_fd) != 0)
    {
        fprintf(stderr, "Error: unable to configure %s\n", device_path);
        ret = EXIT_FAILURE;
        goto main_init_error;
    }

    // set standard file descriptors to non-blocking
    set_non_block(STDIN_FILENO, 1);
    set_non_block(STDOUT_FILENO, 1);
    set_non_block(STDERR_FILENO, 1);

    signal(SIGINT, handle_terminate_signal);
    signal(SIGTERM, handle_terminate_signal);

    forward(serial_fd);

main_init_error:
    if (serial_fd >= 0)
    {
        close(serial_fd);
        serial_fd = -1;
    }
    return ret;
}