Some fix. Reformat. Check if read sentence return correct value.
[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_test1[] =
62 {
63 "$GPRMC,170525.949,A,4351.0843,N,01108.8687,E,0.00,237.67,051009,,,A*61\r\n"      /* acquired */
64 "$GPVTG,237.67,T,,,0.00,N,0.00,K,A*77\r\n"                                        /* acquired */
65 "$GPGSV,3,1,09,3,78,302,37,6,87,031,,7,05,292,37,14,05,135,*48\r\n"               /* acquired */
66 "$GPGGA,170527.949,4351.0842,N,01108.8685,E,1,05,02.6,57.4,M,45.2,M,,*5C\r\n"     /* acquired */
67 };
68
69 static NmeaGga gga_test =
70 {
71         .latitude = 43851403,
72     .longitude = 11147808,
73     .altitude = 57,
74     .time = 57928,
75     .satellites = 5,
76     .quality = 1,
77     .hdop = 26,
78     .geoid = 45,
79 };
80
81 static NmeaRmc rmc_test =
82 {
83         .time = 1254758726,
84         .warn = 'A',
85         .latitude = 43851405,
86         .longitude = 11147812,
87         .speed = 0,
88         .course = 237,
89         .mag_var = 0
90 };
91
92
93 static NmeaVtg vtg_test =
94 {
95         .track_good = 237,
96         .knot_speed = 0,
97         .km_speed = 0
98 };
99
100 static NmeaGsv gsv_test =
101 {
102         .tot_message = 3,
103         .message_num = 1,
104         .tot_svv = 9,
105         .info =
106     {
107                 {  3, 78, 302, 37 },
108                 {  6, 87,  31,  0 },
109                 {  7,  5, 292, 37 },
110                 { 14,  5, 135,  0 }
111         }
112 };
113
114
115 #define TOT_GOOD_SENTENCE_NUM    12
116
117 #define MAX_SENTENCE_POLL  20
118
119 static int tot_sentence_parsed = 0;
120
121 /**
122  * do something with the GGA data
123  */
124 static void gpgga_callout_test(nmeap_context_t *context, void *data, void *user_data)
125 {
126         (void)context;
127         (void)user_data;
128         NmeaGga *gga = (NmeaGga *)data;
129
130         tot_sentence_parsed++;
131
132     LOG_INFO("[%d]found GPGGA message %ld %ld %d %lu %d %d %d %d\n",tot_sentence_parsed,
133             (long)gga->latitude,
134             (long)gga->longitude,
135             gga->altitude,
136             gga->time,
137             gga->satellites,
138             gga->quality,
139             gga->hdop,
140             gga->geoid);
141 }
142
143 /**
144  * do something with the RMC data
145  */
146 static void gprmc_callout_test(nmeap_context_t *context, void *data, void *user_data)
147 {
148         (void)context;
149         (void)user_data;
150     NmeaRmc *rmc = (NmeaRmc *)data;
151
152         tot_sentence_parsed++;
153
154         LOG_INFO("[%d]found GPRMC Message %lu %c %ld %ld %d %d %d\n",tot_sentence_parsed,
155             rmc->time,
156             rmc->warn,
157             (long)rmc->latitude,
158             (long)rmc->longitude,
159             rmc->speed,
160             rmc->course,
161             rmc->mag_var);
162 }
163
164 /**
165  * do something with the GSV data
166  */
167 static void gpgsv_callout_test(nmeap_context_t *context, void *data, void *user_data)
168 {
169         (void)context;
170         (void)user_data;
171         NmeaGsv *gsv = (NmeaGsv *)data;
172
173         tot_sentence_parsed++;
174
175     LOG_INFO("[%d]found GPGSV message %d %d %d\n",tot_sentence_parsed,
176                         gsv->tot_message,
177                         gsv->message_num,
178                         gsv->tot_svv);
179
180         for (int i = 0; i < 4; i++)
181             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);
182 }
183
184 /**
185  * do something with the VTG data
186  */
187 static void gpvtg_callout_test(nmeap_context_t *context, void *data, void *user_data)
188 {
189         (void)context;
190         (void)user_data;
191         NmeaVtg *vtg = (NmeaVtg *)data;
192
193         tot_sentence_parsed++;
194
195     LOG_INFO("[%d]found GPVTG message %d %d %d\n",tot_sentence_parsed,
196                         vtg->track_good,
197                         vtg->knot_speed,
198                         vtg->km_speed);
199 }
200
201 int nmea_testSetup(void)
202 {
203         kdbg_init();
204
205         kfilemem_init(&mem, nmea_test1, sizeof(nmea_test1));
206         LOG_INFO("Init test buffer..done.\n");
207
208         nmeap_init(&nmea, NULL);
209         nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, gpgga_callout_test, &gga);
210         nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, gprmc_callout_test, &rmc);
211         nmeap_addParser(&nmea, "GPGSV", nmea_gpgsv, gpgsv_callout_test, &gsv);
212         nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, gpvtg_callout_test, &vtg);
213
214         return 0;
215 }
216
217 int nmea_testTearDown(void)
218 {
219         return 0;
220 }
221
222 int nmea_testRun(void)
223 {
224         for (int i = 0; i < MAX_SENTENCE_POLL; i++)
225         {
226                 nmea_poll(&nmea, &mem.fd);
227         }
228
229         if (memcmp(&gga_test, &gga, sizeof(gga_test)) &&
230                 memcmp(&rmc_test, &rmc, sizeof(rmc_test)) &&
231                 memcmp(&vtg_test, &vtg, sizeof(vtg_test)) &&
232                 memcmp(&gsv_test, &gsv, sizeof(gsv_test)))
233         {
234                 LOG_ERR("Last gga test sentence had unexpected value\n");
235                 return -1;
236         }
237
238         return  0;
239 }
240
241 TEST_MAIN(nmea);