Example #1
0
/** @fn static int vehicle_file_open(struct vehicle_priv *priv)
*****************************************************************************
* @b Description: open dialogue with the GPS
*****************************************************************************
* @param      priv : pointer on the private data of the plugin
*****************************************************************************
* @return     1 if ok
*             0 if error
*****************************************************************************
**/
static int
vehicle_file_open(struct vehicle_priv *priv)
{
#ifdef _WIN32
    dbg(1, "enter, priv->source='%s'\n", priv->source);

    if ( priv->source )
    {
        char* raw_setting_str = g_strdup( priv->source );

        char* strport = strchr(raw_setting_str, ':' );
        char* strsettings = strchr(raw_setting_str, ' ' );

        if ( strport && strsettings )
        {
            strport++;
            *strsettings = '\0';
            strsettings++;

            priv->fd=serial_io_init( strport, strsettings );
        }
        g_free( raw_setting_str );

        // Add the callback
        dbg(2, "Add the callback ...\n", priv->source);
   		priv->timeout_callback=callback_new_1(callback_cast(vehicle_win32_serial_track), priv);
    }
#else
	char *name;
	struct stat st;
	struct termios tio;

	name = priv->source + 5;
	if (!strncmp(priv->source, "file:", 5)) {
		priv->fd = open(name, O_RDONLY | O_NDELAY);
		if (priv->fd < 0)
			return 0;
		stat(name, &st);
		if (S_ISREG(st.st_mode)) {
			priv->file_type = file_type_file;
		} else {
			tcgetattr(priv->fd, &tio);
			cfmakeraw(&tio);
			cfsetispeed(&tio, priv->baudrate);
			cfsetospeed(&tio, priv->baudrate);
			tio.c_cc[VMIN] = 0;
			tio.c_cc[VTIME] = 200;
			tcsetattr(priv->fd, TCSANOW, &tio);
			priv->file_type = file_type_device;
		}
	} else {
		priv->file = popen(name, "r");
		if (!priv->file)
			return 0;
		priv->fd = fileno(priv->file);
		priv->file_type = file_type_pipe;
	}
#endif
    return(priv->fd != -1);
}
/** @fn static int vehicle_file_open(struct vehicle_priv *priv)
*****************************************************************************
* @b Description: open dialogue with the GPS
*****************************************************************************
* @param      priv : pointer on the private data of the plugin
*****************************************************************************
* @return     1 if ok
*             0 if error
*****************************************************************************
**/
static int
vehicle_file_open(struct vehicle_priv *priv)
{
	char *name;
#ifndef _WIN32
	struct termios tio;
#else
	#define O_NDELAY 0
#endif

	name = priv->source + 5;
	if (!strncmp(priv->source, "file:", 5)) {
		priv->fd = open(name, O_RDONLY | O_NDELAY);
		if (priv->fd < 0)
			return 0;
		if (file_is_reg(name)) {
			priv->file_type = file_type_file;
		}
#ifndef _WIN32
		else {
			tcgetattr(priv->fd, &tio);
			cfmakeraw(&tio);
			cfsetispeed(&tio, priv->baudrate);
			cfsetospeed(&tio, priv->baudrate);
			tio.c_cc[VMIN] = 0;
			tio.c_cc[VTIME] = 200;
			tcsetattr(priv->fd, TCSANOW, &tio);
			priv->file_type = file_type_device;
		}
	} else if (!strncmp(priv->source,"pipe:", 5)) {
		priv->file = popen(name, "r");
		if (!priv->file)
			return 0;
		priv->fd = fileno(priv->file);
		priv->file_type = file_type_pipe;
#endif //!_WIN32
#if defined(HAVE_SOCKET) || defined(HAVE_WINSOCK) 
	} else if (!strncmp(priv->source,"socket:", 7)) {
		#ifdef _WIN32
		WSADATA wsi;
		WSAStartup(0x00020002,&wsi);
		#endif
		char *p,*s=g_strdup(priv->source+7);
		struct sockaddr_in sin;
		p=strchr(s,':');
		if (!p) {
			dbg(0,"port number missing in %s\n",s);
			g_free(s);
			return 0;
		}
		*p++='\0';
		sin.sin_family=AF_INET;
		sin.sin_port=ntohs(atoi(p));
		if (!inet_aton(s, &sin.sin_addr)) {
			dbg(0,"failed to parse %s\n",s);
			g_free(s);
			return 0;
		}
		priv->fd = socket(PF_INET, SOCK_STREAM, 0);
		if (priv->fd != -1) {
			if (connect(priv->fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
				dbg(0,"failed to connect to %s:%s\n",s,p);
				g_free(s);
				return 0;
			}
		}
		priv->file_type = file_type_socket;
#endif //HAVE_SOCKET
	} else if (!strncmp(priv->source,"serial:",7)) {
#ifdef _WIN32
		char* raw_setting_str = g_strdup( priv->source );

		char* strport = strchr(raw_setting_str, ':' );
		char* strsettings = strchr(raw_setting_str, ' ' );

		if ( strport && strsettings )
		{
		    strport++;
		    *strsettings = '\0';
		    strsettings++;

		    priv->fd=serial_io_init( strport, strsettings );
		}
		g_free( raw_setting_str );
		priv->file_type = file_type_serial;
		// Add the callback
		dbg(2, "Add the callback ...\n", priv->source);
			priv->timeout_callback=callback_new_1(callback_cast(vehicle_win32_serial_track), priv);
#else
		//TODO - add linux serial
#endif //!_WIN32
    }
    return(priv->fd != -1);
}