From c88464fb98d6d51b2e0efd7a7968165549fa1ba4 Mon Sep 17 00:00:00 2001
From: asterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Date: Fri, 9 Oct 2009 08:22:43 +0000
Subject: [PATCH] Refactor nmea module. Redefine gsv struct.

git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3050 38d2e660-2303-0410-9eaa-f027e97ec537
---
 bertos/net/nmea.c      | 88 ++++++++++++++++--------------------------
 bertos/net/nmea.h      | 25 +++++-------
 bertos/net/nmea_test.c | 21 ++++------
 3 files changed, 50 insertions(+), 84 deletions(-)

diff --git a/bertos/net/nmea.c b/bertos/net/nmea.c
index 93985e79..23e6dbcd 100644
--- a/bertos/net/nmea.c
+++ b/bertos/net/nmea.c
@@ -229,10 +229,8 @@ int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
 	NmeaGga *gga = (NmeaGga *)sentence->data;
 
 	ASSERT(gga);
+	ASSERT(context->tokens >= 12);
 
-	/*
-	 * if there is a data element, extract data from the tokens
-	 */
 	gga->latitude   = nmea_latitude(context->token[2],context->token[3]);
 	gga->longitude  = nmea_longitude(context->token[4],context->token[5]);
 	gga->altitude   = nmea_altitude(context->token[9],context->token[10]);
@@ -263,26 +261,20 @@ int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
 	 */
     NmeaRmc *rmc = (NmeaRmc *)sentence->data;
 
+	ASSERT(rmc);
+	ASSERT(context->tokens >= 10);
+
 	/*
-	 * if there is a data element, use it
+	 * extract data from the tokens
 	 */
-	if (rmc != 0)
-	{
-		/*
-		 * extract data from the tokens
-		 */
-		rmc->time       = timestampToSec(tokenToInt(context->token[1], 3), atoi(context->token[9]));
-		rmc->warn       = *context->token[2];
-		rmc->latitude   = nmea_latitude(context->token[3],context->token[4]);
-		rmc->longitude  = nmea_longitude(context->token[5],context->token[6]);
-		rmc->speed      = atoi(context->token[7]);
-		rmc->course     = atoi(context->token[8]);
-		rmc->mag_var    = atoi(context->token[10]);
-	}
+	rmc->time       = timestampToSec(tokenToInt(context->token[1], 3), atoi(context->token[9]));
+	rmc->warn       = *context->token[2];
+	rmc->latitude   = nmea_latitude(context->token[3],context->token[4]);
+	rmc->longitude  = nmea_longitude(context->token[5],context->token[6]);
+	rmc->speed      = atoi(context->token[7]);
+	rmc->course     = atoi(context->token[8]);
+	rmc->mag_var    = atoi(context->token[10]);
 
-    /*
-	 * if the sentence has a callout, call it
-	 */
     if (sentence->callout != 0)
         (*sentence->callout)(context, rmc, context->user_data);
 
@@ -301,18 +293,15 @@ int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
 	 */
     NmeaVtg *vtg = (NmeaVtg *)sentence->data;
 
+	ASSERT(vtg);
+	ASSERT(context->tokens >= 7);
+
 	/*
-	 * if there is a data element, use it
+	 * extract data from the tokens
 	 */
-	if (vtg != 0)
-	{
-		/*
-		 * extract data from the tokens
-		 */
-		vtg->track_good  = atoi(context->token[1]);
-		vtg->knot_speed  = atoi(context->token[5]);
-		vtg->km_speed    = atoi(context->token[7]);
-	}
+	vtg->track_good  = atoi(context->token[1]);
+	vtg->knot_speed  = atoi(context->token[5]);
+	vtg->km_speed    = atoi(context->token[7]);
 
     /*
 	 * if the sentence has a callout, call it
@@ -334,35 +323,24 @@ int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
 	 */
     NmeaGsv *gsv = (NmeaGsv *)sentence->data;
 
+
 	/*
-	 * if there is a data element, use it
+	 * extract data from the tokens
 	 */
-	if (gsv != 0)
+	gsv->tot_message     = atoi(context->token[1]);
+	gsv->message_num     = atoi(context->token[2]);
+	gsv->tot_svv         = atoi(context->token[3]);
+
+	// Fill remaning member until we have token
+	int  j = 0;
+	for (int i = 4; i < context->tokens - 3; i += 4, j++)
 	{
-		/*
-		 * extract data from the tokens
-		 */
-		gsv->tot_message     = atoi(context->token[1]);
-		gsv->message_num     = atoi(context->token[2]);
-		gsv->tot_svv         = atoi(context->token[3]);
-		gsv->sv_prn          = atoi(context->token[4]);
-		gsv->elevation       = atoi(context->token[5]);
-		gsv->azimut          = atoi(context->token[6]);
-		gsv->snr             = atoi(context->token[7]);
-		gsv->sv_prn2         = atoi(context->token[8]);
-		gsv->elevation2      = atoi(context->token[9]);
-		gsv->azimut2         = atoi(context->token[10]);
-		gsv->snr2            = atoi(context->token[11]);
-		gsv->sv_prn3         = atoi(context->token[12]);
-		gsv->elevation3      = atoi(context->token[13]);
-		gsv->azimut3         = atoi(context->token[14]);
-		gsv->snr3            = atoi(context->token[15]);
-		gsv->sv_prn4         = atoi(context->token[16]);
-		gsv->elevation4      = atoi(context->token[17]);
-		gsv->azimut4         = atoi(context->token[18]);
-		gsv->snr4            = atoi(context->token[19]);
-	}
 
+		gsv->info[j].sv_prn     = atoi(context->token[i]);
+		gsv->info[j].elevation  = atoi(context->token[i + 1]);
+		gsv->info[j].azimut     = atoi(context->token[i + 2]);
+		gsv->info[j].snr        = atoi(context->token[i + 3]);
+	}
 
     /*
 	 * if the sentence has a callout, call it
diff --git a/bertos/net/nmea.h b/bertos/net/nmea.h
index 44528195..84dfcc18 100644
--- a/bertos/net/nmea.h
+++ b/bertos/net/nmea.h
@@ -115,27 +115,20 @@ typedef struct NmeaVtg
 /**
  * Extracted data from an gsv message
  */
-typedef struct NmeaGsv
+struct SvInfo
 {
-	uint16_t    tot_message;  /* Total number of messages of this type in this cycle */
-	uint16_t    message_num;  /* Message number */
-	uint16_t    tot_svv;      /* Total number of SVs in view */
 	uint16_t    sv_prn;       /* SV PRN number */
 	degree_t    elevation;    /* Elevation in degrees, 90 maximum */
 	degree_t    azimut;       /* Azimuth, degrees from true north, 000 to 359 */
 	uint16_t    snr;          /* SNR, 00-99 dB (null when not tracking) */
-	uint16_t    sv_prn2;      /* Information about second SV PRN number */
-	degree_t	elevation2;   /* Information about second SV elevation in degrees, 90 maximum */
-	degree_t    azimut2;      /* Information about second SV azimuth, degrees from true north, 000 to 359 */
-	uint16_t    snr2;         /* Information about second SV SNR, 00-99 dB (null when not tracking) */
-	uint16_t    sv_prn3;      /* Information about third SV PRN number */
-	degree_t	elevation3;   /* Information about third SV elevation in degrees, 90 maximum */
-	degree_t    azimut3;      /* Information about third SV azimuth, degrees from true north, 000 to 359 */
-	uint16_t    snr3;         /* Information about third SV SNR, 00-99 dB (null when not tracking) */
-	uint16_t    sv_prn4;      /* Information about fourth SV PRN number */
-	degree_t	elevation4;   /* Information about fourth SV elevation in degrees, 90 maximum */
-	degree_t    azimut4;      /* Information about fourth SV azimuth, degrees from true north, 000 to 359 */
-	uint16_t    snr4;         /* Information about fourth SV SNR, 00-99 dB (null when not tracking) */
+};
+
+typedef struct NmeaGsv
+{
+	uint16_t    tot_message;  /* Total number of messages of this type in this cycle */
+	uint16_t    message_num;  /* Message number */
+	uint16_t    tot_svv;      /* Total number of SVs in view */
+	struct SvInfo info[4];    /* Stanrd gsv nmea report up to 4 sv info */
 } NmeaGsv;
 
 void nmea_poll(nmeap_context_t *context, KFile *channel);
diff --git a/bertos/net/nmea_test.c b/bertos/net/nmea_test.c
index 8f463f0e..4213f58a 100644
--- a/bertos/net/nmea_test.c
+++ b/bertos/net/nmea_test.c
@@ -90,6 +90,8 @@ NmeaGga gga_test =
     .geoid = 45,
 };
 
+#include <net/nmea_log.c>
+
 #define TOT_GOOD_SENTENCE_NUM    12
 
 #define MAX_SENTENCE_POLL  20
@@ -152,15 +154,13 @@ static void gpgsv_callout(nmeap_context_t *context, void *data, void *user_data)
 
 	tot_sentence_parsed++;
 
-    LOG_INFO("[%d]found GPGSV message %d %d %d %d %d %d %d\n",tot_sentence_parsed,
+    LOG_INFO("[%d]found GPGSV message %d %d %d\n",tot_sentence_parsed,
 			gsv->tot_message,
 			gsv->message_num,
-			gsv->tot_svv,
-			gsv->sv_prn,
-			gsv->elevation,
-			gsv->azimut,
-			gsv->snr
-            );
+			gsv->tot_svv);
+
+	for (int i = 0; i < 4; i++)
+	    LOG_INFO("[%d]%d %d %d %d\n", i, gsv->info[i].sv_prn, gsv->info[i].elevation, gsv->info[i].azimut, gsv->info[i].snr);
 }
 
 /**
@@ -185,19 +185,14 @@ int nmea_testSetup(void)
 {
 	kdbg_init();
 
-	kfilemem_init(&mem, nmea_test_vector, sizeof(nmea_test_vector));
+	kfilemem_init(&mem, test, sizeof(test));
 	LOG_INFO("Init test buffer..done.\n");
 
     nmeap_init(&nmea, NULL);
-	LOG_INFO("Init NMEA context..done.\n");
     nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, gpgga_callout, &gga);
-	LOG_INFO("Init NMEA GPGGA parser..done.\n");
     nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, gprmc_callout, &rmc);
-	LOG_INFO("Init NMEA GPRMC parser..done.\n");
 	nmeap_addParser(&nmea, "GPGSV", nmea_gpgsv, gpgsv_callout, &gsv);
-	LOG_INFO("Init NMEA GPGSV parser..done.\n");
 	nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, gpvtg_callout, &vtg);
-	LOG_INFO("Init NMEA GPVTG parser..done.\n");
 
 	return 0;
 }
-- 
2.34.1