Import NMEAP 0.3 library.
[bertos.git] / bertos / net / nmeap / tst / test1.c
1 /*
2 Copyright (c) 2005, David M Howard (daveh at dmh2000.com)
3 All rights reserved.
4
5 This product is licensed for use and distribution under the BSD Open Source License.
6 see the file COPYING for more details.
7
8 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
9 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
10 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
11 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
12 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
13 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
14 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
15 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
16 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
17 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
18 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
19
20 */
21
22 /*
23 ========================================================================================================
24 EXAMPLE : SETUP FOR GGA AND RMC SENTENCES WITH CHARACTER BY CHARACTER IO
25 =======================================================================================================
26 */   
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "nmeap.h"
33
34 nmeap_gga_t g_gga;
35
36 char test_vector[] = {
37 "$GPGGA,123519,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n" /* good */
38 "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n"                                  /* junk */
39 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n"      /* good */
40 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*48\r\n"      /* checksum error */
41 };
42
43 char *pvec = test_vector;
44
45 /** simulate character by character IO */
46 int readchar() 
47 {
48     int ch;
49     if (*pvec == 0) {
50         ch = -1;
51     }
52     else {
53         ch = *pvec++;
54     }
55     return ch;
56 }
57
58 /** do something with the GGA data */
59 static void print_gga(nmeap_gga_t *gga)
60 {
61     printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n",
62             gga->latitude  ,
63             gga->longitude, 
64             gga->altitude , 
65             gga->time     , 
66             gga->satellites,
67             gga->quality   ,
68             gga->hdop      ,
69             gga->geoid     
70             );
71 }
72
73 /** called when a gpgga message is received and parsed */
74 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data)
75 {
76     nmeap_gga_t *gga = (nmeap_gga_t *)data;
77     
78     printf("-------------callout\n");
79     print_gga(gga);
80 }
81
82
83 /** do something with the RMC data */
84 static void print_rmc(nmeap_rmc_t *rmc)
85 {
86     printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n",
87             rmc->time,
88             rmc->warn,
89             rmc->latitude,
90             rmc->longitude,
91             rmc->speed,
92             rmc->course,
93             rmc->date,
94             rmc->magvar
95             );
96 }
97
98 /** called when a gprmc message is received and parsed */
99 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data)
100 {
101     nmeap_rmc_t *rmc = (nmeap_rmc_t *)data;
102     
103     printf("-------------callout\n");
104     print_rmc(rmc);
105 }
106
107 /* ---------------------------------------------------------------------------------------*/
108 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
109 /*          they need to be live for the duration of the parser                           */
110 /* ---------------------------------------------------------------------------------------*/
111 static nmeap_context_t nmea;       /* parser context */
112 static nmeap_gga_t     gga;                /* this is where the data from GGA messages will show up */
113 static nmeap_rmc_t     rmc;                /* this is where the data from RMC messages will show up */
114 static int             user_data; /* user can pass in anything. typically it will be a pointer to some user data */
115
116 int main(int argc,char *argv[])
117 {
118     int             status;
119     char            ch;
120     
121         /* ---------------------------------------*/
122         /*STEP 2 : initialize the nmea context    */                                                
123         /* ---------------------------------------*/
124     status = nmeap_init(&nmea,(void *)&user_data);
125     if (status != 0) {
126         printf("nmeap_init %d\n",status);
127         exit(1);
128     }
129     
130         /* ---------------------------------------*/
131         /*STEP 3 : add standard GPGGA parser      */                                                
132         /* -------------------------------------- */
133     status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
134     if (status != 0) {
135         printf("nmeap_add %d\n",status);
136         exit(1);
137     }
138
139         /* ---------------------------------------*/
140         /*STEP 4 : add standard GPRMC parser      */                                                
141         /* -------------------------------------- */
142     status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
143     if (status != 0) {
144         printf("nmeap_add %d\n",status);
145         exit(1);
146     }
147     
148         /* ---------------------------------------*/
149         /*STEP 5 : process input until done       */                                                
150         /* -------------------------------------- */
151     for(;;) {
152                 /* ---------------------------------------*/
153                 /*STEP 6 : get a byte at a time           */                                                
154                 /* -------------------------------------- */
155         ch = readchar();
156         if (ch <= 0) {
157             break;
158         }
159         
160                 /* --------------------------------------- */
161                 /*STEP 7 : pass it to the parser           */
162                 /* status indicates whether a complete msg */
163                 /* arrived for this byte                   */
164                 /* NOTE : in addition to the return status */
165                 /* the message callout will be fired when  */
166                 /* a complete message is processed         */
167                 /* --------------------------------------- */
168         status = nmeap_parse(&nmea,ch);
169         
170                 /* ---------------------------------------*/
171                 /*STEP 8 : process the return code        */                                                
172                 /* -------------------------------------- */
173         switch(status) {
174         case NMEAP_GPGGA:
175                         /* GOT A GPGGA MESSAGE */
176             printf("-------------switch\n");
177             print_gga(&gga);
178             printf("-------------\n");
179             break;
180         case NMEAP_GPRMC:
181                         /* GOT A GPRMC MESSAGE */
182             printf("-------------switch\n");
183             print_rmc(&rmc);
184             printf("-------------\n");
185             break;
186         default:
187             break;
188         }
189     }
190     
191     return 0;
192 }
193