Add nmea module test.
[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
38 #include "nmea.h"
39
40 #include <struct/kfile_mem.h>
41
42 #include <cfg/debug.h>
43 #include <cfg/test.h>
44
45 #include <string.h> //strncmp
46
47 static nmeap_context_t nmea;       /* parser context */
48 static NmeaRmc rmc;
49 static NmeaGga gga;
50 static NmeaGsv gsv;
51 static NmeaVtg vtg;
52
53 static KFileMem mem;
54
55 static char nmea_test_vector[] =
56 {
57 "$GPGGA,123519,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n" /* good */
58 "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n"                                  /* junk */
59 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n"      /* good */
60 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*48\r\n"      /* checksum error */
61 "$GPGGA,091144.698,0000.0000,S,00000.0000,W,0,00,00.0,0.0,M,0.0,M,,*5C\r\n"   /* acquired */
62 "$GPRMC,091144.698,V,0000.0000,S,00000.0000,W,0.00,0.00,051009,,,A*75\r\n"    /* acquired */
63 "$GPVTG,0.00,T,,,0.00,N,0.00,K,A*70\r\n"                                      /* acquired */
64 "$GPGGA,091145.698,0000.0000,S,00000.0000,W,0,00,00.0,0.0,M,0.0,M,,*5D\r\n"   /* acquired */
65 "$GPGSV,1,1,02,1,,,41,12,,,35,,,,,,,,*4A\r\n"                                 /* acquired */
66 "$GPRMC,091145.698,V,0000.0000,S,00000.0000,W,0.00,0.00,051009,,,A*74\r\n"    /* acquired */
67 "$GPVTG,0.00,T,,,0.00,N,0.00,K,A*70\r\n"                                      /* acquired */
68 };
69
70
71 /**
72  * do something with the GGA data
73  */
74 static void gpgga_callout(nmeap_context_t *context, void *data, void *user_data)
75 {
76         (void)context;
77         (void)user_data;
78         NmeaGga *gga = (NmeaGga *)data;
79
80     kprintf("found GPGGA message %d %d %d %d %d %d %d %d\n",
81             gga->latitude,
82             gga->longitude,
83             gga->altitude,
84             gga->time,
85             gga->satellites,
86             gga->quality,
87             gga->hdop,
88             gga->geoid
89             );
90 }
91
92 /**
93  * called when a gpgga message is received and parsed
94  */
95 static void gprmc_callout(nmeap_context_t *context, void *data, void *user_data)
96 {
97         (void)context;
98         (void)user_data;
99     NmeaRmc *rmc = (NmeaRmc *)data;
100
101         kprintf("found GPRMC Message %d %c %d %d %d %d %d %d\n",
102             rmc->time,
103             rmc->warn,
104             rmc->latitude,
105             rmc->longitude,
106             rmc->speed,
107             rmc->course,
108             rmc->date,
109             rmc->mag_var
110             );
111 }
112
113 /**
114  * do something with the GGA data
115  */
116 static void gpgsv_callout(nmeap_context_t *context, void *data, void *user_data)
117 {
118         (void)context;
119         (void)user_data;
120         NmeaGsv *gsv = (NmeaGsv *)data;
121
122     kprintf("found GPGSV message %d %d %d %d %d %d %d\n",
123                         gsv->tot_message,
124                         gsv->message_num,
125                         gsv->tot_svv,
126                         gsv->sv_prn,
127                         gsv->elevation,
128                         gsv->azimut,
129                         gsv->snr
130             );
131 }
132
133 /**
134  * do something with the VTG data
135  */
136 static void gpvtg_callout(nmeap_context_t *context, void *data, void *user_data)
137 {
138         (void)context;
139         (void)user_data;
140         NmeaVtg *vtg = (NmeaVtg *)data;
141
142     kprintf("found GPVTG message %d %d %d\n",
143                         vtg->track_good,
144                         vtg->knot_speed,
145                         vtg->km_speed
146             );
147 }
148
149 int nmea_testSetup(void)
150 {
151         kdbg_init();
152
153         kfilemem_init(&mem, nmea_test_vector, sizeof(nmea_test_vector));
154         kprintf("Init test buffer..done.\n");
155
156     nmeap_init(&nmea, NULL);
157         kprintf("Init NMEA context..done.\n");
158     nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, gpgga_callout, &gga);
159         kprintf("Init NMEA GPGGA parser..done.\n");
160     nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, gprmc_callout, &rmc);
161         kprintf("Init NMEA GPRMC parser..done.\n");
162         nmeap_addParser(&nmea, "GPGSV", nmea_gpgsv, gpgsv_callout, &gsv);
163         kprintf("Init NMEA GPGSV parser..done.\n");
164         nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, gpvtg_callout, &vtg);
165         kprintf("Init NMEA GPVTG parser..done.\n");
166
167         return 0;
168 }
169
170 int nmea_testTearDown(void)
171 {
172         return 0;
173 }
174
175 int nmea_testRun(void)
176 {
177         nmea_poll(&nmea, &mem.fd);
178         return  0;
179 }
180
181 TEST_MAIN(nmea);