acc7cd487961ec6178be6546cb7b3f052cefbc28
[bertos.git] / bertos / net / nmea_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief NMEA parser test.
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * notest:avr
38  */
39
40 #include "nmea.h"
41
42 #include <struct/kfile_mem.h>
43
44 #include <cfg/debug.h>
45 #define LOG_LEVEL  NMEA_LOG_LEVEL
46 #define LOG_FORMAT NMEA_LOG_FORMAT
47 #include <cfg/log.h>
48
49 #include <cfg/test.h>
50
51 #include <string.h> //strncmp
52
53 static nmeap_context_t nmea;       /* parser context */
54 static NmeaRmc rmc;
55 static NmeaGga gga;
56 static NmeaGsv gsv;
57 static NmeaVtg vtg;
58
59 static KFileMem mem;
60
61 static char nmea_test_vector[] =
62 {
63 "$GPGGA,123519.021,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n" /* good */
64 "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n"                                      /* junk */
65 "$GPRMC,225446,A,4916.45,N,12311.120,W,000.5,054.7,191194,020.3,E*68\r\n"         /* good */
66 "$GPRMC,225446,A,4916.45,N,12311.120,W,000.5,054.7,191194,020.3,E*48\r\n"         /* checksum error */
67 "$GPGGA,091144.698,0000.0000,S,00000.0000,W,0,00,00.0,0.0,M,0.0,M,,*5C\r\n"       /* acquired */
68 "$GPRMC,091144.698,V,0000.0000,S,00000.0000,W,0.00,0.00,051009,,,A*75\r\n"        /* acquired */
69 "$GPVTG,0.00,T,,,0.00,N,0.00,K,A*70\r\n"                                          /* acquired */
70 "$GPGGA,091145.698,0000.0000,S,00000.0000,W,0,00,00.0,0.0,M,0.0,M,,*5D\r\n"       /* acquired */
71 "$GPGSV,1,1,02,1,,,41,12,,,35,,,,,,,,*4A\r\n"                                     /* acquired */
72 "$GPRMC,091145.698,V,0000.0000,S,00000.0000,W,0.00,0.00,051009,,,A*74\r\n"        /* acquired */
73 "$GPVTG,0.00,T,,,0.00,N,0.00,K,A*70\r\n"                                          /* acquired */
74 "$GPGGA,170529.948,4351.0841,N,01108.8685,E,1,05,02.6,57.4,M,45.2,M,,*50\r\n"     /* acquired */
75 "$GPRMC,170525.949,A,4351.0843,N,01108.8687,E,0.00,237.67,051009,,,A*61\r\n"      /* acquired */
76 "$GPVTG,237.67,T,,,0.00,N,0.00,K,A*77\r\n"                                        /* acquired */
77 "$GPGSV,3,1,09,3,78,302,37,6,87,031,,7,05,292,37,14,05,135,*48\r\n"               /* acquired */
78 "$GPGGA,170527.949,4351.0842,N,01108.8685,E,1,05,02.6,57.4,M,45.2,M,,*5C\r\n"     /* acquired */
79 };
80
81 NmeaGga gga_test =
82 {
83         .latitude = 43851403,
84     .longitude = 11147808,
85     .altitude = 574,
86     .time = 2736328,
87     .satellites = 5,
88     .quality = 1,
89     .hdop = 26,
90     .geoid = 452,
91 };
92
93 #define TOT_GOOD_SENTENCE_NUM    12
94
95 #define MAX_SENTENCE_POLL  20
96
97 static int tot_sentence_parsed = 0;
98
99 /**
100  * do something with the GGA data
101  */
102 static void gpgga_callout(nmeap_context_t *context, void *data, void *user_data)
103 {
104         (void)context;
105         (void)user_data;
106         NmeaGga *gga = (NmeaGga *)data;
107
108         tot_sentence_parsed++;
109
110     LOG_INFO("[%d]found GPGGA message %ld %ld %d %lu %d %d %d %d\n",tot_sentence_parsed,
111             (long)gga->latitude,
112             (long)gga->longitude,
113             gga->altitude,
114             gga->time,
115             gga->satellites,
116             gga->quality,
117             gga->hdop,
118             gga->geoid
119             );
120 }
121
122 /**
123  * called when a gpgga message is received and parsed
124  */
125 static void gprmc_callout(nmeap_context_t *context, void *data, void *user_data)
126 {
127         (void)context;
128         (void)user_data;
129     NmeaRmc *rmc = (NmeaRmc *)data;
130
131         tot_sentence_parsed++;
132
133         LOG_INFO("[%d]found GPRMC Message %lu %c %ld %ld %d %d %d\n",tot_sentence_parsed,
134             rmc->time,
135             rmc->warn,
136             (long)rmc->latitude,
137             (long)rmc->longitude,
138             rmc->speed,
139             rmc->course,
140             rmc->mag_var
141             );
142 }
143
144 /**
145  * do something with the GGA data
146  */
147 static void gpgsv_callout(nmeap_context_t *context, void *data, void *user_data)
148 {
149         (void)context;
150         (void)user_data;
151         NmeaGsv *gsv = (NmeaGsv *)data;
152
153         tot_sentence_parsed++;
154
155     LOG_INFO("[%d]found GPGSV message %d %d %d %d %d %d %d\n",tot_sentence_parsed,
156                         gsv->tot_message,
157                         gsv->message_num,
158                         gsv->tot_svv,
159                         gsv->sv_prn,
160                         gsv->elevation,
161                         gsv->azimut,
162                         gsv->snr
163             );
164 }
165
166 /**
167  * do something with the VTG data
168  */
169 static void gpvtg_callout(nmeap_context_t *context, void *data, void *user_data)
170 {
171         (void)context;
172         (void)user_data;
173         NmeaVtg *vtg = (NmeaVtg *)data;
174
175         tot_sentence_parsed++;
176
177     LOG_INFO("[%d]found GPVTG message %d %d %d\n",tot_sentence_parsed,
178                         vtg->track_good,
179                         vtg->knot_speed,
180                         vtg->km_speed
181             );
182 }
183
184 int nmea_testSetup(void)
185 {
186         kdbg_init();
187
188         kfilemem_init(&mem, nmea_test_vector, sizeof(nmea_test_vector));
189         LOG_INFO("Init test buffer..done.\n");
190
191     nmeap_init(&nmea, NULL);
192         LOG_INFO("Init NMEA context..done.\n");
193     nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, gpgga_callout, &gga);
194         LOG_INFO("Init NMEA GPGGA parser..done.\n");
195     nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, gprmc_callout, &rmc);
196         LOG_INFO("Init NMEA GPRMC parser..done.\n");
197         nmeap_addParser(&nmea, "GPGSV", nmea_gpgsv, gpgsv_callout, &gsv);
198         LOG_INFO("Init NMEA GPGSV parser..done.\n");
199         nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, gpvtg_callout, &vtg);
200         LOG_INFO("Init NMEA GPVTG parser..done.\n");
201
202         return 0;
203 }
204
205 int nmea_testTearDown(void)
206 {
207         return 0;
208 }
209
210 int nmea_testRun(void)
211 {
212         for (int i = 0; i < MAX_SENTENCE_POLL; i++)
213         {
214                 nmea_poll(&nmea, &mem.fd);
215         }
216
217         if (tot_sentence_parsed != TOT_GOOD_SENTENCE_NUM)
218         {
219                 LOG_ERR("No all test sentence have been parser.\n");
220                 return -1;
221         }
222
223         if (memcmp(&gga_test, &gga, sizeof(gga_test)))
224         {
225                 LOG_ERR("Last gga test sentence had unexpected value\n");
226                 return -1;
227         }
228         return  0;
229 }
230
231 TEST_MAIN(nmea);