2 Copyright (c) 2005, David M Howard (daveh at dmh2000.com)
5 This product is licensed for use and distribution under the BSD Open Source License.
6 see the file COPYING for more details.
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.
23 ========================================================================================================
24 EXAMPLE : SETUP FOR GGA AND RMC SENTENCES WITH CHARACTER BY CHARACTER IO
25 =======================================================================================================
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 */
43 char *pvec = test_vector;
45 /** simulate character by character IO */
58 /** do something with the GGA data */
59 static void print_gga(nmeap_gga_t *gga)
61 printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n",
73 /** called when a gpgga message is received and parsed */
74 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data)
76 nmeap_gga_t *gga = (nmeap_gga_t *)data;
78 printf("-------------callout\n");
83 /** do something with the RMC data */
84 static void print_rmc(nmeap_rmc_t *rmc)
86 printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n",
98 /** called when a gprmc message is received and parsed */
99 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data)
101 nmeap_rmc_t *rmc = (nmeap_rmc_t *)data;
103 printf("-------------callout\n");
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 */
116 int main(int argc,char *argv[])
121 /* ---------------------------------------*/
122 /*STEP 2 : initialize the nmea context */
123 /* ---------------------------------------*/
124 status = nmeap_init(&nmea,(void *)&user_data);
126 printf("nmeap_init %d\n",status);
130 /* ---------------------------------------*/
131 /*STEP 3 : add standard GPGGA parser */
132 /* -------------------------------------- */
133 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
135 printf("nmeap_add %d\n",status);
139 /* ---------------------------------------*/
140 /*STEP 4 : add standard GPRMC parser */
141 /* -------------------------------------- */
142 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
144 printf("nmeap_add %d\n",status);
148 /* ---------------------------------------*/
149 /*STEP 5 : process input until done */
150 /* -------------------------------------- */
152 /* ---------------------------------------*/
153 /*STEP 6 : get a byte at a time */
154 /* -------------------------------------- */
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);
170 /* ---------------------------------------*/
171 /*STEP 8 : process the return code */
172 /* -------------------------------------- */
175 /* GOT A GPGGA MESSAGE */
176 printf("-------------switch\n");
178 printf("-------------\n");
181 /* GOT A GPRMC MESSAGE */
182 printf("-------------switch\n");
184 printf("-------------\n");