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 block IO */
46 int readbuffer(char *buffer,int len)
56 /* quit when no more data */
65 /** do something with the GGA data */
66 static void print_gga(nmeap_gga_t *gga)
68 printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n",
80 /** called when a gpgga message is received and parsed */
81 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data)
83 nmeap_gga_t *gga = (nmeap_gga_t *)data;
85 printf("-------------callout\n");
90 /** do something with the RMC data */
91 static void print_rmc(nmeap_rmc_t *rmc)
93 printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n",
105 /** called when a gprmc message is received and parsed */
106 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data)
108 nmeap_rmc_t *rmc = (nmeap_rmc_t *)data;
110 printf("-------------callout\n");
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 */
123 int main(int argc,char *argv[])
131 /* ---------------------------------------*/
132 /*STEP 2 : initialize the nmea context */
133 /* ---------------------------------------*/
134 status = nmeap_init(&nmea,(void *)&user_data);
136 printf("nmeap_init %d\n",status);
140 /* ---------------------------------------*/
141 /*STEP 3 : add standard GPGGA parser */
142 /* -------------------------------------- */
143 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
145 printf("nmeap_add %d\n",status);
149 /* ---------------------------------------*/
150 /*STEP 4 : add standard GPRMC parser */
151 /* -------------------------------------- */
152 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
154 printf("nmeap_add %d\n",status);
158 /* ---------------------------------------*/
159 /*STEP 5 : process input until done */
160 /* -------------------------------------- */
162 /* ---------------------------------------*/
163 /*STEP 6 : get a buffer of input */
164 /* -------------------------------------- */
165 len = rem = readbuffer(buffer,sizeof(buffer));
170 /* ----------------------------------------------*/
171 /*STEP 7 : process input until buffer is used up */
172 /* --------------------------------------------- */
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);
186 /* ---------------------------------------*/
187 /*STEP 9 : process the return code */
188 /* -------------------------------------- */
191 printf("-------------switch\n");
193 printf("-------------\n");
196 printf("-------------switch\n");
198 printf("-------------\n");