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 readbuffer(char *buffer,int len) 00047 { 00048 int i; 00049 00050 if (*pvec == 0) { 00051 // end of file 00052 return -1; 00053 } 00054 00055 for(i=0;i<len;i++) { 00056 /* quit when no more data */ 00057 if (*pvec == 0) { 00058 break; 00059 } 00060 buffer[i] = *pvec++; 00061 } 00062 return i; 00063 } 00064 00066 static void print_gga(nmeap_gga_t *gga) 00067 { 00068 printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n", 00069 gga->latitude , 00070 gga->longitude, 00071 gga->altitude , 00072 gga->time , 00073 gga->satellites, 00074 gga->quality , 00075 gga->hdop , 00076 gga->geoid 00077 ); 00078 } 00079 00081 static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data) 00082 { 00083 nmeap_gga_t *gga = (nmeap_gga_t *)data; 00084 00085 printf("-------------callout\n"); 00086 print_gga(gga); 00087 } 00088 00089 00091 static void print_rmc(nmeap_rmc_t *rmc) 00092 { 00093 printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n", 00094 rmc->time, 00095 rmc->warn, 00096 rmc->latitude, 00097 rmc->longitude, 00098 rmc->speed, 00099 rmc->course, 00100 rmc->date, 00101 rmc->magvar 00102 ); 00103 } 00104 00106 static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data) 00107 { 00108 nmeap_rmc_t *rmc = (nmeap_rmc_t *)data; 00109 00110 printf("-------------callout\n"); 00111 print_rmc(rmc); 00112 } 00113 00114 /* ---------------------------------------------------------------------------------------*/ 00115 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */ 00116 /* they need to be live for the duration of the parser */ 00117 /* ---------------------------------------------------------------------------------------*/ 00118 static nmeap_context_t nmea; /* parser context */ 00119 static nmeap_gga_t gga; /* this is where the data from GGA messages will show up */ 00120 static nmeap_rmc_t rmc; /* this is where the data from RMC messages will show up */ 00121 static int user_data; /* user can pass in anything. typically it will be a pointer to some user data */ 00122 00123 int main(int argc,char *argv[]) 00124 { 00125 int status; 00126 int rem; 00127 int offset; 00128 int len; 00129 char buffer[32]; 00130 00131 /* ---------------------------------------*/ 00132 /*STEP 2 : initialize the nmea context */ 00133 /* ---------------------------------------*/ 00134 status = nmeap_init(&nmea,(void *)&user_data); 00135 if (status != 0) { 00136 printf("nmeap_init %d\n",status); 00137 exit(1); 00138 } 00139 00140 /* ---------------------------------------*/ 00141 /*STEP 3 : add standard GPGGA parser */ 00142 /* -------------------------------------- */ 00143 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga); 00144 if (status != 0) { 00145 printf("nmeap_add %d\n",status); 00146 exit(1); 00147 } 00148 00149 /* ---------------------------------------*/ 00150 /*STEP 4 : add standard GPRMC parser */ 00151 /* -------------------------------------- */ 00152 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); 00153 if (status != 0) { 00154 printf("nmeap_add %d\n",status); 00155 exit(1); 00156 } 00157 00158 /* ---------------------------------------*/ 00159 /*STEP 5 : process input until done */ 00160 /* -------------------------------------- */ 00161 for(;;) { 00162 /* ---------------------------------------*/ 00163 /*STEP 6 : get a buffer of input */ 00164 /* -------------------------------------- */ 00165 len = rem = readbuffer(buffer,sizeof(buffer)); 00166 if (len <= 0) { 00167 break; 00168 } 00169 00170 /* ----------------------------------------------*/ 00171 /*STEP 7 : process input until buffer is used up */ 00172 /* --------------------------------------------- */ 00173 offset = 0; 00174 while(rem > 0) { 00175 /* --------------------------------------- */ 00176 /*STEP 8 : pass it to the parser */ 00177 /* status indicates whether a complete msg */ 00178 /* arrived for this byte */ 00179 /* NOTE : in addition to the return status */ 00180 /* the message callout will be fired when */ 00181 /* a complete message is processed */ 00182 /* --------------------------------------- */ 00183 status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem); 00184 offset += (len - rem); 00185 00186 /* ---------------------------------------*/ 00187 /*STEP 9 : process the return code */ 00188 /* -------------------------------------- */ 00189 switch(status) { 00190 case NMEAP_GPGGA: 00191 printf("-------------switch\n"); 00192 print_gga(&gga); 00193 printf("-------------\n"); 00194 break; 00195 case NMEAP_GPRMC: 00196 printf("-------------switch\n"); 00197 print_rmc(&rmc); 00198 printf("-------------\n"); 00199 break; 00200 default: 00201 break; 00202 } 00203 } 00204 } 00205 00206 return 0; 00207 } 00208