Import NMEAP 0.3 library.
[bertos.git] / bertos / net / nmeap / tst / test2.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 block IO */
46 int readbuffer(char *buffer,int len)
47 {
48     int i;
49     
50     if (*pvec == 0) {
51         // end of file
52         return -1;
53     }
54     
55     for(i=0;i<len;i++) {
56         /* quit when no more data */
57         if (*pvec == 0) {
58             break;
59         }
60         buffer[i] = *pvec++;
61     }
62     return i;
63 }
64
65 /** do something with the GGA data */
66 static void print_gga(nmeap_gga_t *gga)
67 {
68     printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n",
69             gga->latitude  ,
70             gga->longitude, 
71             gga->altitude , 
72             gga->time     , 
73             gga->satellites,
74             gga->quality   ,
75             gga->hdop      ,
76             gga->geoid     
77             );
78 }
79
80 /** called when a gpgga message is received and parsed */
81 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data)
82 {
83     nmeap_gga_t *gga = (nmeap_gga_t *)data;
84     
85     printf("-------------callout\n");
86     print_gga(gga);
87 }
88
89
90 /** do something with the RMC data */
91 static void print_rmc(nmeap_rmc_t *rmc)
92 {
93     printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n",
94             rmc->time,
95             rmc->warn,
96             rmc->latitude,
97             rmc->longitude,
98             rmc->speed,
99             rmc->course,
100             rmc->date,
101             rmc->magvar
102             );
103 }
104
105 /** called when a gprmc message is received and parsed */
106 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data)
107 {
108     nmeap_rmc_t *rmc = (nmeap_rmc_t *)data;
109     
110     printf("-------------callout\n");
111     print_rmc(rmc);
112 }
113
114 /* ---------------------------------------------------------------------------------------*/
115 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
116 /*          they need to be live for the duration of the parser                           */
117 /* ---------------------------------------------------------------------------------------*/
118 static nmeap_context_t nmea;       /* parser context */
119 static nmeap_gga_t     gga;                /* this is where the data from GGA messages will show up */
120 static nmeap_rmc_t     rmc;                /* this is where the data from RMC messages will show up */
121 static int             user_data; /* user can pass in anything. typically it will be a pointer to some user data */
122
123 int main(int argc,char *argv[])
124 {
125     int  status;
126     int  rem;
127         int  offset;
128         int  len;
129         char buffer[32];
130     
131         /* ---------------------------------------*/
132         /*STEP 2 : initialize the nmea context    */                                                
133         /* ---------------------------------------*/
134     status = nmeap_init(&nmea,(void *)&user_data);
135     if (status != 0) {
136         printf("nmeap_init %d\n",status);
137         exit(1);
138     }
139     
140         /* ---------------------------------------*/
141         /*STEP 3 : add standard GPGGA parser      */                                                
142         /* -------------------------------------- */
143     status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
144     if (status != 0) {
145         printf("nmeap_add %d\n",status);
146         exit(1);
147     }
148
149         /* ---------------------------------------*/
150         /*STEP 4 : add standard GPRMC parser      */                                                
151         /* -------------------------------------- */
152     status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
153     if (status != 0) {
154         printf("nmeap_add %d\n",status);
155         exit(1);
156     }
157     
158         /* ---------------------------------------*/
159         /*STEP 5 : process input until done       */                                                
160         /* -------------------------------------- */
161     for(;;) {
162                 /* ---------------------------------------*/
163                 /*STEP 6 : get a buffer of input          */                                                
164                 /* -------------------------------------- */
165         len = rem = readbuffer(buffer,sizeof(buffer));
166         if (len <= 0) {
167             break;
168         }
169         
170                 /* ----------------------------------------------*/
171                 /*STEP 7 : process input until buffer is used up */                                                
172                 /* --------------------------------------------- */
173                 offset = 0;
174         while(rem > 0) {
175                         /* --------------------------------------- */
176                         /*STEP 8 : pass it to the parser           */
177                         /* status indicates whether a complete msg */
178                         /* arrived for this byte                   */
179                         /* NOTE : in addition to the return status */
180                         /* the message callout will be fired when  */
181                         /* a complete message is processed         */
182                         /* --------------------------------------- */
183             status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem);
184                         offset += (len - rem); 
185             
186                         /* ---------------------------------------*/
187                         /*STEP 9 : process the return code        */                                                
188                         /* -------------------------------------- */
189             switch(status) {
190             case NMEAP_GPGGA:
191                 printf("-------------switch\n");
192                 print_gga(&gga);
193                 printf("-------------\n");
194                 break;
195             case NMEAP_GPRMC:
196                 printf("-------------switch\n");
197                 print_rmc(&rmc);
198                 printf("-------------\n");
199                 break;
200             default:
201                 break;
202             }
203         }
204     }
205     
206     return 0;
207 }
208