nmea.c

Go to the documentation of this file.
00001 
00053 #include "nmea.h"
00054 
00055 #include "cfg/cfg_nmea.h"
00056 
00057 #include <cfg/debug.h>
00058 
00059 #define LOG_LEVEL  NMEA_LOG_LEVEL
00060 #define LOG_FORMAT NMEA_LOG_FORMAT
00061 #include <cfg/log.h>
00062 
00063 #include <net/nmeap/inc/nmeap.h>
00064 
00065 #include <ctype.h>
00066 #include <time.h>
00067 #include <string.h>
00068 #include <stdlib.h>
00069 
00070 /*
00071  * Make conversion from one string to int.
00072  *
00073  * You can specify the precision if the string is a float
00074  * number. The result is an int multiplied to 10^precision.
00075  */
00076 static uint32_t tokenToInt(const char *s, int precision)
00077 {
00078     uint32_t num = 0;
00079     bool sep_found = false;
00080     int i;
00081 
00082     if (!s)
00083         return 0;
00084 
00085     for(i = 0; i < NMEAP_MAX_SENTENCE_LENGTH; i++)
00086     {
00087         char c = *s++;
00088 
00089         if (c == '.')
00090         {
00091             sep_found = true;
00092             continue;
00093         }
00094 
00095         if (c == '\0' || !isdigit(c) || (precision == 0 && sep_found))
00096             break;
00097 
00098         if (sep_found)
00099             precision--;
00100 
00101         num *= 10;
00102         num += c - '0';
00103     }
00104 
00105     while (precision--)
00106         num *= 10;
00107 
00108     return num;
00109 }
00110 
00111 /*
00112  * Convert a string to micro degree.
00113  */
00114 static udegree_t convertToDegree(const char *str)
00115 {
00116     uint32_t dec;
00117     uint32_t deg;
00118     uint32_t min;
00119 
00120     if (*str == 0)
00121     {
00122         return 0;
00123     }
00124 
00125     dec = tokenToInt(str, 4);
00126     deg = dec / 1000000;
00127     min = dec - deg * 1000000;
00128     dec = deg * 1000000 + ((min * 5) + 1) / 3;
00129 
00130     return dec;
00131 }
00132 
00133 /*
00134  * Retun latitude in micro degree from a string.
00135  */
00136 static udegree_t nmea_latitude(const char *plat, const char *phem)
00137 {
00138     int ns;
00139 
00140     if (*phem == 0)
00141         return 0;
00142 
00143     /* north lat is +, south lat is - */
00144     ns = (*phem == 'N') ? 1 : -1;
00145 
00146 
00147     return ns * convertToDegree(plat);
00148 }
00149 
00150 /*
00151  * Retun longitude in micro degree from a string.
00152  */
00153 static udegree_t nmea_longitude(const char *plot, const char *phem)
00154 {
00155     int ew;
00156 
00157     if (*phem == 0)
00158         return 0;
00159 
00160     /* west long is negative, east long is positive */
00161    ew = (*phem == 'E') ? 1 : -1;
00162 
00163     return ew * convertToDegree(plot);
00164 }
00165 
00166 /*
00167  * Return altitude in meter from a string.
00168  *
00169  */
00170 static uint16_t nmea_altitude(const char *palt, const char *punits)
00171 {
00172     uint32_t alt;
00173 
00174     if (*palt == 0)
00175         return 0;
00176 
00177     alt = atoi(palt);
00178 
00179     if (*punits == 'F')
00180     {
00181         /* convert to feet */
00182         /* alt = alt * 3.2808399 */
00183         alt = alt * 3 +  /* 3.0 */
00184               (alt >> 2) + /* 0.25 */
00185               (alt >> 6) + /* 0.015625 */
00186               (alt >> 7) + /* 0.0078125 */
00187               (alt >> 8); /* 0,00390625 */
00188 
00189     }
00190 
00191     return alt;
00192 }
00193 
00194 /*
00195  * Convert time and date stamp string to unix time.
00196  */
00197 static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp)
00198 {
00199     struct tm t;
00200     uint16_t msec;
00201     uint16_t tmr[3];
00202     uint16_t date[3];
00203 
00204     memset(&t, 0, sizeof(t));
00205     memset(&tmr, 0, sizeof(tmr));
00206     memset(&date, 0, sizeof(date));
00207 
00208     LOG_INFO("time_s[%lu],date[%lu]\n", (long)time_stamp, (long)date_stamp);
00209     uint32_t res = time_stamp / 1000;
00210     uint32_t all = time_stamp;
00211     msec = all - res * 1000;
00212 
00213     for (int i = 0; i < 3; i++)
00214     {
00215         all = res;
00216         res = all / 100;
00217         tmr[i]  = all - res * 100;
00218         LOG_INFO("t[%d]%d\n", tmr[i],i);
00219     }
00220 
00221     t.tm_sec = tmr[0] + (ROUND_UP(msec, 1000) / 1000);
00222     t.tm_min = tmr[1];
00223     t.tm_hour = tmr[2];
00224     //If we not have refence data, we set as default 1/1/1970.
00225     t.tm_mday = 1;
00226     t.tm_mon = 0;
00227     t.tm_year = 70;
00228 
00229     if (date_stamp)
00230     {
00231         res = all = date_stamp;
00232         for (int i = 0; i < 3; i++)
00233         {
00234             all = res;
00235             res = all / 100;
00236             date[i]  = all - res * 100;
00237             LOG_INFO("d[%d]%d\n", date[i],i);
00238         }
00239         t.tm_mday = date[2];
00240         t.tm_mon = date[1] - 1; // time struct count month from 0 to 11;
00241         // we should specific number of years from 1900, but the year field
00242         // is only two cipher, so we sum 100 (2000 - 1900)..
00243         t.tm_year = date[0] + 100;
00244     }
00245     LOG_INFO("times=%d,%d,%d,%d,%d,%d\n",t.tm_sec, t.tm_min, t.tm_hour, t.tm_year, t.tm_mon, t.tm_mday);
00246 
00247     return  mktime(&t);
00248 }
00249 
00253 void gpgga_callout(nmeap_context_t *context, void *data, void *user_data)
00254 {
00255     (void)context;
00256     (void)user_data;
00257     (void)data;
00258     LOG_INFOB(NmeaGga *gga = (NmeaGga *)data;);
00259 
00260     LOG_INFO("Found GPGGA message %ld %ld %d %lu %d %d %d %d\n",
00261             (long)gga->latitude,
00262             (long)gga->longitude,
00263             gga->altitude,
00264             gga->time,
00265             gga->satellites,
00266             gga->quality,
00267             gga->hdop,
00268             gga->geoid);
00269 }
00270 
00274 void gprmc_callout(nmeap_context_t *context, void *data, void *user_data)
00275 {
00276     (void)context;
00277     (void)user_data;
00278     (void)data;
00279     LOG_INFOB(NmeaRmc *rmc = (NmeaRmc *)data;);
00280 
00281     LOG_INFO("Found GPRMC message %lu %c %ld %ld %d %d %d\n",
00282             rmc->time,
00283             rmc->warn,
00284             (long)rmc->latitude,
00285             (long)rmc->longitude,
00286             rmc->speed,
00287             rmc->course,
00288             rmc->mag_var);
00289 }
00290 
00294 void gpgsv_callout(nmeap_context_t *context, void *data, void *user_data)
00295 {
00296     (void)context;
00297     (void)user_data;
00298     (void)data;
00299     LOG_INFOB(NmeaGsv *gsv = (NmeaGsv *)data;);
00300 
00301     LOG_INFO("Found GPGSV message %d %d %d\n", gsv->tot_message, gsv->message_num, gsv->tot_svv);
00302 
00303     for (int i = 0; i < 4; i++)
00304         LOG_INFO("%d %d %d %d\n", gsv->info[i].sv_prn, gsv->info[i].elevation, gsv->info[i].azimut, gsv->info[i].snr);
00305 }
00306 
00310 void gpvtg_callout(nmeap_context_t *context, void *data, void *user_data)
00311 {
00312     (void)context;
00313     (void)user_data;
00314     (void)data;
00315     LOG_INFOB(NmeaVtg *vtg = (NmeaVtg *)data;);
00316 
00317     LOG_INFO("Found GPVTG message %d %d %d\n", vtg->track_good, vtg->knot_speed, vtg->km_speed);
00318 }
00319 
00320 
00321 
00325 int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
00326 {
00327     /*
00328      * get pointer to sentence data
00329      */
00330     NmeaGga *gga = (NmeaGga *)sentence->data;
00331 
00332     ASSERT(gga);
00333     ASSERT(context->tokens >= 12);
00334 
00335     gga->latitude   = nmea_latitude(context->token[2],context->token[3]);
00336     gga->longitude  = nmea_longitude(context->token[4],context->token[5]);
00337     gga->altitude   = nmea_altitude(context->token[9],context->token[10]);
00338     gga->time       = timestampToSec(tokenToInt(context->token[1], 3), 0);
00339     gga->satellites = atoi(context->token[7]);
00340     gga->quality    = atoi(context->token[6]);
00341     gga->hdop       = tokenToInt(context->token[8], 1);
00342     gga->geoid      = nmea_altitude(context->token[11],context->token[12]);
00343 
00344     /*
00345      * if the sentence has a callout, call it
00346      */
00347 
00348     if (sentence->callout != 0)
00349         (*sentence->callout)(context, gga, context->user_data);
00350 
00351     return NMEA_GPGGA;
00352 }
00353 
00357 int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
00358 {
00359 
00360     /*
00361      * get pointer to sentence data
00362      */
00363     NmeaRmc *rmc = (NmeaRmc *)sentence->data;
00364 
00365     ASSERT(rmc);
00366     ASSERT(context->tokens >= 10);
00367 
00368     /*
00369      * extract data from the tokens
00370      */
00371     rmc->time       = timestampToSec(tokenToInt(context->token[1], 3), tokenToInt(context->token[9], 0));
00372     rmc->warn       = *context->token[2];
00373     rmc->latitude   = nmea_latitude(context->token[3],context->token[4]);
00374     rmc->longitude  = nmea_longitude(context->token[5],context->token[6]);
00375     rmc->speed      = atoi(context->token[7]);
00376     rmc->course     = atoi(context->token[8]);
00377     rmc->mag_var    = atoi(context->token[10]);
00378 
00379     if (sentence->callout != 0)
00380         (*sentence->callout)(context, rmc, context->user_data);
00381 
00382     return NMEA_GPRMC;
00383 }
00384 
00385 
00389 int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
00390 {
00391 
00392     /*
00393      * get pointer to sentence data
00394      */
00395     NmeaVtg *vtg = (NmeaVtg *)sentence->data;
00396 
00397     ASSERT(vtg);
00398     ASSERT(context->tokens >= 7);
00399 
00400     /*
00401      * extract data from the tokens
00402      */
00403     vtg->track_good  = atoi(context->token[1]);
00404     vtg->knot_speed  = atoi(context->token[5]);
00405     vtg->km_speed    = atoi(context->token[7]);
00406 
00407     /*
00408      * if the sentence has a callout, call it
00409      */
00410     if (sentence->callout != 0)
00411         (*sentence->callout)(context, vtg, context->user_data);
00412 
00413     return NMEA_GPVTG;
00414 }
00415 
00419 int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
00420 {
00421 
00422     /*
00423      * get pointer to sentence data
00424      */
00425     NmeaGsv *gsv = (NmeaGsv *)sentence->data;
00426 
00427 
00428     /*
00429      * extract data from the tokens
00430      */
00431     gsv->tot_message     = atoi(context->token[1]);
00432     gsv->message_num     = atoi(context->token[2]);
00433     gsv->tot_svv         = atoi(context->token[3]);
00434 
00435     // Fill remaning member until we have token
00436     int  j = 0;
00437     for (int i = 4; i < context->tokens - 3; i += 4, j++)
00438     {
00439 
00440         gsv->info[j].sv_prn     = atoi(context->token[i]);
00441         gsv->info[j].elevation  = atoi(context->token[i + 1]);
00442         gsv->info[j].azimut     = atoi(context->token[i + 2]);
00443         gsv->info[j].snr        = atoi(context->token[i + 3]);
00444     }
00445 
00446     /*
00447      * if the sentence has a callout, call it
00448      */
00449     if (sentence->callout != 0)
00450         (*sentence->callout)(context, gsv, context->user_data);
00451 
00452     return NMEA_GPGSV;
00453 }
00454 
00455 
00456 /*
00457  * Parse NMEA sentence from a channel.
00458  */
00459 void nmea_poll(nmeap_context_t *context, KFile *channel)
00460 {
00461     int c;
00462     while ((c = kfile_getc(channel)) != EOF)
00463     {
00464         nmeap_parse(context, c);
00465     }
00466 }
00467