BeRTOS
|
00001 /* 00002 Copyright (c) 2005, David M Howard (daveh at dmh2000.com) 00003 All rights reserved. 00004 00005 This product is licensed for use and distribution under the BSD Open Source License. 00006 see the file COPYING for more details. 00007 00008 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00009 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00010 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00011 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00012 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00013 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00014 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 00015 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00016 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00017 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00018 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00019 00020 */ 00021 00022 /* 00023 ======================================================================================================== 00024 EXAMPLE : SETUP FOR GGA AND RMC SENTENCES WITH CHARACTER BY CHARACTER IO 00025 ======================================================================================================= 00026 */ 00027 00028 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <string.h> 00032 #include "nmeap.h" 00033 00034 nmeap_gga_t g_gga; 00035 00036 char test_vector[] = { 00037 "$GPGGA,123519,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n" /* good */ 00038 "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n" /* junk */ 00039 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n" /* good */ 00040 "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*48\r\n" /* checksum error */ 00041 }; 00042 00043 char *pvec = test_vector; 00044 00046 int readchar() 00047 { 00048 int ch; 00049 if (*pvec == 0) { 00050 ch = -1; 00051 } 00052 else { 00053 ch = *pvec++; 00054 } 00055 return ch; 00056 } 00057 00059 static void print_gga(nmeap_gga_t *gga) 00060 { 00061 printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n", 00062 gga->latitude , 00063 gga->longitude, 00064 gga->altitude , 00065 gga->time , 00066 gga->satellites, 00067 gga->quality , 00068 gga->hdop , 00069 gga->geoid 00070 ); 00071 } 00072 00074 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data) 00075 { 00076 nmeap_gga_t *gga = (nmeap_gga_t *)data; 00077 00078 printf("-------------callout\n"); 00079 print_gga(gga); 00080 } 00081 00082 00084 static void print_rmc(nmeap_rmc_t *rmc) 00085 { 00086 printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n", 00087 rmc->time, 00088 rmc->warn, 00089 rmc->latitude, 00090 rmc->longitude, 00091 rmc->speed, 00092 rmc->course, 00093 rmc->date, 00094 rmc->magvar 00095 ); 00096 } 00097 00099 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data) 00100 { 00101 nmeap_rmc_t *rmc = (nmeap_rmc_t *)data; 00102 00103 printf("-------------callout\n"); 00104 print_rmc(rmc); 00105 } 00106 00107 /* ---------------------------------------------------------------------------------------*/ 00108 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */ 00109 /* they need to be live for the duration of the parser */ 00110 /* ---------------------------------------------------------------------------------------*/ 00111 static nmeap_context_t nmea; /* parser context */ 00112 static nmeap_gga_t gga; /* this is where the data from GGA messages will show up */ 00113 static nmeap_rmc_t rmc; /* this is where the data from RMC messages will show up */ 00114 static int user_data; /* user can pass in anything. typically it will be a pointer to some user data */ 00115 00116 int main(int argc,char *argv[]) 00117 { 00118 int status; 00119 char ch; 00120 00121 /* ---------------------------------------*/ 00122 /*STEP 2 : initialize the nmea context */ 00123 /* ---------------------------------------*/ 00124 status = nmeap_init(&nmea,(void *)&user_data); 00125 if (status != 0) { 00126 printf("nmeap_init %d\n",status); 00127 exit(1); 00128 } 00129 00130 /* ---------------------------------------*/ 00131 /*STEP 3 : add standard GPGGA parser */ 00132 /* -------------------------------------- */ 00133 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga); 00134 if (status != 0) { 00135 printf("nmeap_add %d\n",status); 00136 exit(1); 00137 } 00138 00139 /* ---------------------------------------*/ 00140 /*STEP 4 : add standard GPRMC parser */ 00141 /* -------------------------------------- */ 00142 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); 00143 if (status != 0) { 00144 printf("nmeap_add %d\n",status); 00145 exit(1); 00146 } 00147 00148 /* ---------------------------------------*/ 00149 /*STEP 5 : process input until done */ 00150 /* -------------------------------------- */ 00151 for(;;) { 00152 /* ---------------------------------------*/ 00153 /*STEP 6 : get a byte at a time */ 00154 /* -------------------------------------- */ 00155 ch = readchar(); 00156 if (ch <= 0) { 00157 break; 00158 } 00159 00160 /* --------------------------------------- */ 00161 /*STEP 7 : pass it to the parser */ 00162 /* status indicates whether a complete msg */ 00163 /* arrived for this byte */ 00164 /* NOTE : in addition to the return status */ 00165 /* the message callout will be fired when */ 00166 /* a complete message is processed */ 00167 /* --------------------------------------- */ 00168 status = nmeap_parse(&nmea,ch); 00169 00170 /* ---------------------------------------*/ 00171 /*STEP 8 : process the return code */ 00172 /* -------------------------------------- */ 00173 switch(status) { 00174 case NMEAP_GPGGA: 00175 /* GOT A GPGGA MESSAGE */ 00176 printf("-------------switch\n"); 00177 print_gga(&gga); 00178 printf("-------------\n"); 00179 break; 00180 case NMEAP_GPRMC: 00181 /* GOT A GPRMC MESSAGE */ 00182 printf("-------------switch\n"); 00183 print_rmc(&rmc); 00184 printf("-------------\n"); 00185 break; 00186 default: 00187 break; 00188 } 00189 } 00190 00191 return 0; 00192 } 00193