예제 #1
0
/** @fn static void vehicle_file_io(struct vehicle_priv *priv)
*****************************************************************************
* @b Description: function to get data from GPS
*****************************************************************************
* @param      priv : pointer on the private data of the plugin
*****************************************************************************
* @remarks Not used on WIN32 operating system
*****************************************************************************
**/
static void
vehicle_file_io(struct vehicle_priv *priv)
{
	dbg(1, "vehicle_file_io : enter\n");
#ifndef _WIN32
	int size, rc = 0;
	char *str, *tok;

	size = read(priv->fd, priv->buffer + priv->buffer_pos, buffer_size - priv->buffer_pos - 1);
	if (size <= 0) {
		switch (priv->on_eof) {
		case 0:
			vehicle_file_close(priv);
			vehicle_file_open(priv);
			break;
		case 1:
			vehicle_file_disable_watch(priv);
			break;
		case 2:
			exit(0);
			break;
		}
		return;
	}
	priv->buffer_pos += size;
	priv->buffer[priv->buffer_pos] = '\0';
	dbg(1, "size=%d pos=%d buffer='%s'\n", size,
	    priv->buffer_pos, priv->buffer);
	str = priv->buffer;
	while ((tok = strchr(str, '\n'))) {
		*tok++ = '\0';
		dbg(1, "line='%s'\n", str);
		rc +=vehicle_file_parse(priv, str);
		str = tok;
	}

	if (str != priv->buffer) {
		size = priv->buffer + priv->buffer_pos - str;
		memmove(priv->buffer, str, size + 1);
		priv->buffer_pos = size;
		dbg(1, "now pos=%d buffer='%s'\n",
		    priv->buffer_pos, priv->buffer);
	} else if (priv->buffer_pos == buffer_size - 1) {
		dbg(0,
		    "Overflow. Most likely wrong baud rate or no nmea protocol\n");
		priv->buffer_pos = 0;
	}
	if (rc)
		callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
#endif
}
예제 #2
0
/** @fn static struct vehicle_priv * vehicle_file_new_file(
*                                       struct vehicle_methods *meth,
*                                       struct callback_list   *cbl,
*                                       struct attr            **attrs)
*****************************************************************************
* @b Description: Function called to initialize the plugin
*****************************************************************************
* @param      meth  : ?
* @param      cbl   : ?
* @param      attrs : ?
*****************************************************************************
* @return     pointer on the private data of the plugin
*****************************************************************************
* @remarks private data is allocated by this function (g_new0)
*****************************************************************************
**/
static struct vehicle_priv *
vehicle_file_new_file(struct vehicle_methods
		      *meth, struct callback_list
		      *cbl, struct attr **attrs)
{
	struct vehicle_priv *ret;
	struct attr *source;
	struct attr *time;
	struct attr *on_eof;
	struct attr *baudrate;
	struct attr *checksum_ignore;

	dbg(1, "enter\n");

	source = attr_search(attrs, NULL, attr_source);
	if(source == NULL){
		 dbg(0,"Missing source attribute");
		 return NULL;
    }
	ret = g_new0(struct vehicle_priv, 1);   // allocate and initialize to 0
	ret->fd = -1;
	ret->cbl = cbl;
	ret->source = g_strdup(source->u.str);
	ret->buffer = g_malloc(buffer_size);
	ret->time=1000;
	ret->baudrate=B4800;
	time = attr_search(attrs, NULL, attr_time);
	if (time)
		ret->time=time->u.num;
	baudrate = attr_search(attrs, NULL, attr_baudrate);
	if (baudrate) {
		switch (baudrate->u.num) {
		case 4800:
			ret->baudrate=B4800;
			break;
		case 9600:
			ret->baudrate=B9600;
			break;
		case 19200:
			ret->baudrate=B19200;
			break;
#ifdef B38400
		case 38400:
			ret->baudrate=B38400;
			break;
#endif
#ifdef B57600
		case 57600:
			ret->baudrate=B57600;
			break;
#endif
#ifdef B115200
		case 115200:
			ret->baudrate=B115200;
			break;
#endif
		}
	}
	checksum_ignore = attr_search(attrs, NULL, attr_checksum_ignore);
	if (checksum_ignore)
		ret->checksum_ignore=checksum_ignore->u.num;
	ret->attrs = attrs;
	on_eof = attr_search(attrs, NULL, attr_on_eof);
	if (on_eof && !strcasecmp(on_eof->u.str, "stop"))
		ret->on_eof=1;
	if (on_eof && !strcasecmp(on_eof->u.str, "exit"))
		ret->on_eof=2;
	dbg(0,"on_eof=%d\n", ret->on_eof);
	*meth = vehicle_file_methods;
	ret->cb=callback_new_1(callback_cast(vehicle_file_io), ret);
	ret->cbt=callback_new_1(callback_cast(vehicle_file_enable_watch_timer), ret);
	ret->sat_item.type=type_position_sat;
	ret->sat_item.id_hi=ret->sat_item.id_lo=0;
	ret->sat_item.priv_data=ret;
	ret->sat_item.meth=&vehicle_file_sat_methods;
#ifdef _WIN32
	ret->no_data_count = 0;
#endif

	dbg(1, "vehicle_file_new_file:open\n");
	if (!vehicle_file_open(ret)) {
        dbg(0, "Failed to open '%s'\n", ret->source);
	}

	vehicle_file_enable_watch(ret);
	// vehicle_file_destroy(ret);
	// return NULL;
	dbg(1, "leave\n");
	return ret;
}
예제 #3
0
static int vehicle_win32_serial_track(struct vehicle_priv *priv)
{
    static char buffer[2048] = {0,};
    static int current_index = 0;
    const int chunk_size = 1024;
    int rc = 0;

    dbg(1, "enter, *priv='%x', priv->source='%s'\n", priv, priv->source);

    if ( priv->no_data_count > 5 )
    {
        vehicle_file_close( priv );
        priv->no_data_count = 0;
        vehicle_file_open( priv );
        vehicle_file_enable_watch(priv);
    }

    //if ( priv->fd <= 0 )
    //{
    //    vehicle_file_open( priv );
    //}

    if ( current_index >= ( sizeof( buffer ) - chunk_size ) )
    {
        // discard
        current_index = 0;
        memset( buffer, 0 , sizeof( buffer ) );
    }

    int dwBytes = serial_io_read( priv->fd, &buffer[ current_index ], chunk_size );
    if ( dwBytes > 0 )
    {
        current_index += dwBytes;

        char* return_pos = NULL;
        while ( ( return_pos = strchr( buffer, '\n' ) ) != NULL )
        {
            char return_buffer[1024];
            int bytes_to_copy = return_pos - buffer + 1;
            memcpy( return_buffer, buffer, bytes_to_copy );
            return_buffer[ bytes_to_copy + 1 ] = '\0';
            return_buffer[ bytes_to_copy ] = '\0';

            // printf( "received %d : '%s' bytes to copy\n", bytes_to_copy, return_buffer );
            rc += vehicle_file_parse( priv, return_buffer );

            current_index -= bytes_to_copy;
            memmove( buffer, &buffer[ bytes_to_copy ] , sizeof( buffer ) - bytes_to_copy );
        }
        if (rc) {
            priv->no_data_count = 0;
            callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
            if (rc > 1)
                dbg(0, "Can not keep with gps data delay is %d seconds\n", rc - 1);

        }
    }
    else
    {
        priv->no_data_count++;
    }
    dbg(2, "leave, return '1', priv->no_data_count='%d'\n", priv->no_data_count);
    return 1;
}
/** @fn static void vehicle_file_io(struct vehicle_priv *priv)
*****************************************************************************
* @b Description: function to get data from GPS
*****************************************************************************
* @param      priv : pointer on the private data of the plugin
*****************************************************************************
* @remarks 
*****************************************************************************
**/
static void
vehicle_file_io(struct vehicle_priv *priv)
{
	int size, rc = 0;
	char *str, *tok;
    dbg(1, "vehicle_file_io : enter\n");

	if (priv->process_statefile) {
		unsigned char *data;
		priv->process_statefile=0;
		if (file_get_contents(priv->statefile, &data, &size)) {
			if (size > buffer_size)
				size=buffer_size;
			memcpy(priv->buffer, data, size);
			priv->buffer_pos=0;
			g_free(data);
		} else
			return;
	} else {
		size = read(priv->fd, priv->buffer + priv->buffer_pos, buffer_size - priv->buffer_pos - 1);
	}
	if (size <= 0) {
		switch (priv->on_eof) {
		case 0:
			vehicle_file_close(priv);
			vehicle_file_open(priv);
			break;
		case 1:
			vehicle_file_disable_watch(priv);
			break;
		case 2:
			exit(0);
			break;
		}
		return;
	}
	priv->buffer_pos += size;
	priv->buffer[priv->buffer_pos] = '\0';
	dbg(1, "size=%d pos=%d buffer='%s'\n", size,
	    priv->buffer_pos, priv->buffer);
	str = priv->buffer;
	while ((tok = strchr(str, '\n'))) {
		*tok++ = '\0';
		dbg(1, "line='%s'\n", str);
		rc +=vehicle_file_parse(priv, str);
		str = tok;
		if (priv->file_type == file_type_file && rc)
			break;
	}

	if (str != priv->buffer) {
		size = priv->buffer + priv->buffer_pos - str;
		memmove(priv->buffer, str, size + 1);
		priv->buffer_pos = size;
		dbg(1, "now pos=%d buffer='%s'\n",
		    priv->buffer_pos, priv->buffer);
	} else if (priv->buffer_pos == buffer_size - 1) {
		dbg(0,
		    "Overflow. Most likely wrong baud rate or no nmea protocol\n");
		priv->buffer_pos = 0;
	}
	if (rc)
		callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}