<commit_before>#include <velib/base/base.h>
#include <velib/base/ve_string.h>
#include <velib/types/ve_item.h>
#include <velib/utils/ve_logger.h>

#include "values.h"
#include "gps.h"

#include <string.h>
#include <errno.h>
#include <math.h>
#include <time.h>

#ifdef WIN32
#include <stdio.h>
#else
#include <stdlib.h>
#endif

#define MODULE	"GPS"

/* ASCII digit to binairy value */
#define a2b(a)						((a) - '0')

/* GPRMS field number */
#define GPS_RMC_UTC_TIME			1
#define GPS_RMC_STATUS				2
#define GPS_RMC_LATITUDE			3
#define GPS_RMC_LAT_NORTH_SOUTH		4
#define GPS_RMC_LONGITUDE			5
#define GPS_RMC_LONG_EAST_WEST		6
#define GPS_RMC_SPEED				7
#define GPS_RMC_TRUE_COURSE			8
#define GPS_RMC_UTC_DATE			9
#define GPS_RMC_VARIATION			10
#define GPS_RMC_VAR_EAST_WEST		11
#define GPS_RMC_MODE				12

/* GPGGA field number */
#define GPS_GGA_UTC_TIME			1
#define GPS_GGA_LATITUDE			2
#define GPS_GGA_LAT_NORTH_SOUTH		3
#define GPS_GGA_LONGITUDE			4
#define GPS_GGA_LONG_EAST_WEST		5
#define GPS_GGA_QUALITY_INDICATOR	6
#define GPS_GGA_NR_OF_SATELITES		7
#define GPS_GGA_HORZ_DILUTION		8
#define GPS_GGA_ALTITUDE			9
#define GPS_GGA_ALTITUDE_UNIT		10
#define GPS_GGA_GEOIDAL_HEIGHT		11
#define GPS_GGA_GEOIDAL_HEIGHT_UNIT	12
#define GPS_GGA_AGE_OF_DATA			13
#define GPS_GGA_STATION_ID			14

typedef void (*GpsSentenceParser)(un8 index, char *value);

#define F_CONNECTED					1
static un8 flags;

#define CONNECTION_TIMEOUT			5*20	/* 50ms */
static un16 timeout;

typedef struct
{
	VeVariant fix;
	VeVariant timestamp;
	VeVariant latitude;
	VeVariant longitude;
	VeVariant variation;
	VeVariant speed;
	VeVariant course;
	VeVariant altitude;
	VeVariant nrOfSats;

	struct tm time;
} GpsData;
static GpsData local;

/* Container for VeItems */
static Gps gps;

static VeVariantUnitFmt none		= {	0,	""		};
static VeVariantUnitFmt angle5		= {	5,	"deg"	};
static VeVariantUnitFmt angle1		= {	1,	"deg"	};
static VeVariantUnitFmt velocity	= {	2,	"m/s"	};
static VeVariantUnitFmt length		= {	1,	"m"		};

static ItemInfo const itemInfo[] =
{
	{	&gps.product.connected,			NULL,				"Connected",			&none,		0	},
	{	&gps.product.name,				NULL,				"ProductName",			&none,		0	},
	{	&gps.product.id,				NULL,				"ProductId",			&none,		0	},
	{	&gps.product.firmwareVersion,	NULL,				"FirmwareVersion",		&none,		0	},
	{	&gps.product.hardwareRevision,	NULL,				"HardwareVersion",		&none,		0	},
	{	&gps.product.instance,			NULL,				"DeviceInstance",		&none,		0	},
	{	&gps.fix,						NULL,				"Fix",					&none,		5	},
	{	&gps.latitude,					&local.latitude,	"Position/Latitude",	&angle5,	5	},
	{	&gps.longitude,					&local.longitude,	"Position/Longitude",	&angle5,	5	},
	{	&gps.variation,					&local.variation,	"MagneticVariation",	&angle5,	5	},
	{	&gps.speed,						&local.speed,		"Speed",				&velocity,	5	},
	{	&gps.course,					&local.course,		"Course",				&angle1,	5	},
	{	&gps.altitude,					&local.altitude,	"Altitude",				&length,	5	},
	{	&gps.nrOfSatellites,			&local.nrOfSats,	"NrOfSatellites",		&none,		5	},
	{	&gps.utcTimestamp,				&local.timestamp,	"UtcTimestamp",			&none,		5	}
};

void gpsInit(VeItem *root)
{
	un8 i;
	for (i = 0; i < ARRAY_LENGTH(itemInfo); i++) {
		veItemAddChildByUid(root, itemInfo[i].id, itemInfo[i].item);
		veItemSetFmt(itemInfo[i].item, veVariantFmt, itemInfo[i].fmt);
		veItemSetTimeout(itemInfo[i].item, itemInfo[i].timeout);
	}
}

static void updateValues(void)
{
	un8 i;

	/* Fix? */
	veItemOwnerSet(&gps.fix, &local.fix);
	if (gps.fix.variant.value.UN32 == 0)
		return;

	/* Copy local values to items */
	for (i = 0; i < ARRAY_LENGTH(itemInfo); i++) {
		if (itemInfo[i].local && veVariantIsValid(itemInfo[i].local))
			veItemOwnerSet(itemInfo[i].item, itemInfo[i].local);
	}
}

/* 50 ms ticker, used for time out */
void gpsTick(void)
{
	VeVariant variant;
<commit_msg>Set DeviceInstance fixed to 0<commit_after>#include <velib/base/base.h>
#include <velib/base/ve_string.h>
#include <velib/types/ve_item.h>
#include <velib/utils/ve_logger.h>

#include "values.h"
#include "gps.h"

#include <string.h>
#include <errno.h>
#include <math.h>
#include <time.h>

#ifdef WIN32
#include <stdio.h>
#else
#include <stdlib.h>
#endif

#define MODULE	"GPS"

/* ASCII digit to binairy value */
#define a2b(a)						((a) - '0')

/* GPRMS field number */
#define GPS_RMC_UTC_TIME			1
#define GPS_RMC_STATUS				2
#define GPS_RMC_LATITUDE			3
#define GPS_RMC_LAT_NORTH_SOUTH		4
#define GPS_RMC_LONGITUDE			5
#define GPS_RMC_LONG_EAST_WEST		6
#define GPS_RMC_SPEED				7
#define GPS_RMC_TRUE_COURSE			8
#define GPS_RMC_UTC_DATE			9
#define GPS_RMC_VARIATION			10
#define GPS_RMC_VAR_EAST_WEST		11
#define GPS_RMC_MODE				12

/* GPGGA field number */
#define GPS_GGA_UTC_TIME			1
#define GPS_GGA_LATITUDE			2
#define GPS_GGA_LAT_NORTH_SOUTH		3
#define GPS_GGA_LONGITUDE			4
#define GPS_GGA_LONG_EAST_WEST		5
#define GPS_GGA_QUALITY_INDICATOR	6
#define GPS_GGA_NR_OF_SATELITES		7
#define GPS_GGA_HORZ_DILUTION		8
#define GPS_GGA_ALTITUDE			9
#define GPS_GGA_ALTITUDE_UNIT		10
#define GPS_GGA_GEOIDAL_HEIGHT		11
#define GPS_GGA_GEOIDAL_HEIGHT_UNIT	12
#define GPS_GGA_AGE_OF_DATA			13
#define GPS_GGA_STATION_ID			14

typedef void (*GpsSentenceParser)(un8 index, char *value);

#define F_CONNECTED					1
static un8 flags;

#define CONNECTION_TIMEOUT			5*20	/* 50ms */
static un16 timeout;

typedef struct
{
	VeVariant fix;
	VeVariant timestamp;
	VeVariant latitude;
	VeVariant longitude;
	VeVariant variation;
	VeVariant speed;
	VeVariant course;
	VeVariant altitude;
	VeVariant nrOfSats;

	struct tm time;
} GpsData;
static GpsData local;

/* Container for VeItems */
static Gps gps;

static VeVariantUnitFmt none		= {	0,	""		};
static VeVariantUnitFmt angle5		= {	5,	"deg"	};
static VeVariantUnitFmt angle1		= {	1,	"deg"	};
static VeVariantUnitFmt velocity	= {	2,	"m/s"	};
static VeVariantUnitFmt length		= {	1,	"m"		};

static ItemInfo const itemInfo[] =
{
	{	&gps.product.connected,			NULL,				"Connected",			&none,		0	},
	{	&gps.product.name,				NULL,				"ProductName",			&none,		0	},
	{	&gps.product.id,				NULL,				"ProductId",			&none,		0	},
	{	&gps.product.firmwareVersion,	NULL,				"FirmwareVersion",		&none,		0	},
	{	&gps.product.hardwareRevision,	NULL,				"HardwareVersion",		&none,		0	},
	{	&gps.product.instance,			NULL,				"DeviceInstance",		&none,		0	},
	{	&gps.fix,						NULL,				"Fix",					&none,		5	},
	{	&gps.latitude,					&local.latitude,	"Position/Latitude",	&angle5,	5	},
	{	&gps.longitude,					&local.longitude,	"Position/Longitude",	&angle5,	5	},
	{	&gps.variation,					&local.variation,	"MagneticVariation",	&angle5,	5	},
	{	&gps.speed,						&local.speed,		"Speed",				&velocity,	5	},
	{	&gps.course,					&local.course,		"Course",				&angle1,	5	},
	{	&gps.altitude,					&local.altitude,	"Altitude",				&length,	5	},
	{	&gps.nrOfSatellites,			&local.nrOfSats,	"NrOfSatellites",		&none,		5	},
	{	&gps.utcTimestamp,				&local.timestamp,	"UtcTimestamp",			&none,		5	}
};

void gpsInit(VeItem *root)
{
	VeVariant variant;

	un8 i;
	for (i = 0; i < ARRAY_LENGTH(itemInfo); i++) {
		veItemAddChildByUid(root, itemInfo[i].id, itemInfo[i].item);
		veItemSetFmt(itemInfo[i].item, veVariantFmt, itemInfo[i].fmt);
		veItemSetTimeout(itemInfo[i].item, itemInfo[i].timeout);
	}

	veItemOwnerSet(&gps.product.instance, veVariantUn32(&variant, 0)); // Fixed instance of 0
}

static void updateValues(void)
{
	un8 i;

	/* Fix? */
	veItemOwnerSet(&gps.fix, &local.fix);
	if (gps.fix.variant.value.UN32 == 0)
		return;

	/* Copy local values to items */
	for (i = 0; i < ARRAY_LENGTH(itemInfo); i++) {
		if (itemInfo[i].local && veVariantIsValid(itemInfo[i].local))
			veItemOwnerSet(itemInfo[i].item, itemInfo[i].local);
	}
}

/* 50 ms ticker, used for time out */
void gpsTick(void)
{
	VeVariant variant;

{'fraction_non_alphanumeric': 0.13613380007779075, 'fraction_numerical': 0.022170361726954493, 'mean_word_length': 4.166778298727395, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3992875', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3454, 'n_words': 687}