X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fnet%2Fnmea.c;h=8403973c28ad88671c88e2d82f2ca57d7400ad4c;hb=27b7177adf935135af60b935fe0701f7210ed3f7;hp=93985e79b3db0456d29c6d98fa7d31c871606556;hpb=efc33d8c4107aa7e95b20c4728836df4171e17a9;p=bertos.git diff --git a/bertos/net/nmea.c b/bertos/net/nmea.c index 93985e79..8403973c 100644 --- a/bertos/net/nmea.c +++ b/bertos/net/nmea.c @@ -29,7 +29,21 @@ * Copyright 2009 Develer S.r.l. (http://www.develer.com/) * * --> - * \brief NMEA implementation. + * + * \brief NMEA parser implementation. + * + * NMEA 0183 is acronym of National Marine Electronics Association that + * combined electrical and data specification for communication between marine + * electronic devices such as echo sounder, sonars, anemometer (wind speed and direction), + * gyrocompass, autopilot, GPS receivers and many other types of instruments. + * It has been defined by, and is controlled by, the U.S.-based National Marine + * Electronics Association. + * The NMEA 0183 standard uses a simple ASCII, serial communications protocol + * that defines how data is transmitted in a "sentence" from one "talker" + * to multiple "listeners" at a time. + * At the application layer, the standard also defines the contents of each sentence + * (message) type so that all listeners can parse messages accurately. + * * * \author Daniele Basile * @@ -53,7 +67,12 @@ #include #include - +/* + * Make conversion from one string to int. + * + * You can specify the precision if the string is a float + * number. The result is an int multiplied to 10^precision. + */ static uint32_t tokenToInt(const char *s, int precision) { uint32_t num = 0; @@ -89,6 +108,9 @@ static uint32_t tokenToInt(const char *s, int precision) return num; } +/* + * Convert a string to micro degree. + */ static udegree_t convertToDegree(const char *str) { uint32_t dec; @@ -108,6 +130,9 @@ static udegree_t convertToDegree(const char *str) return dec; } +/* + * Retun latitude in micro degree from a string. + */ static udegree_t nmea_latitude(const char *plat, const char *phem) { int ns; @@ -116,15 +141,15 @@ static udegree_t nmea_latitude(const char *plat, const char *phem) return 0; /* north lat is +, south lat is - */ - if (*phem == 'N') - ns = 1; - else - ns = -1; + ns = (*phem == 'N') ? 1 : -1; return ns * convertToDegree(plat); } +/* + * Retun longitude in micro degree from a string. + */ static udegree_t nmea_longitude(const char *plot, const char *phem) { int ew; @@ -133,14 +158,15 @@ static udegree_t nmea_longitude(const char *plot, const char *phem) return 0; /* west long is negative, east long is positive */ - if (*phem == 'E') - ew = 1; - else - ew = -1; + ew = (*phem == 'E') ? 1 : -1; return ew * convertToDegree(plot); } +/* + * Return altitude in meter from a string. + * + */ static uint16_t nmea_altitude(const char *palt, const char *punits) { uint32_t alt; @@ -165,6 +191,9 @@ static uint16_t nmea_altitude(const char *palt, const char *punits) return alt; } +/* + * Convert time and date stamp string to unix time. + */ static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp) { struct tm t; @@ -180,6 +209,7 @@ static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp) uint32_t res = time_stamp / 1000; uint32_t all = time_stamp; msec = all - res * 1000; + for (int i = 0; i < 3; i++) { all = res; @@ -193,7 +223,7 @@ static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp) t.tm_hour = tmr[2]; //If we not have refence data, we set as default 1/1/1970. t.tm_mday = 1; - t.tm_mon = 1; + t.tm_mon = 0; t.tm_year = 70; if (date_stamp) @@ -217,6 +247,83 @@ static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp) return mktime(&t); } +/** + * Callout example for GGA data + */ +void gpgga_callout(nmeap_context_t *context, void *data, void *user_data) +{ + (void)context; + (void)user_data; + (void)data; + LOG_INFOB( + NmeaGga *gga = (NmeaGga *)data; + LOG_INFO("Found GPGGA message %ld %ld %d %lu %d %d %d %d\n", + (long)gga->latitude, + (long)gga->longitude, + gga->altitude, + gga->time, + gga->satellites, + gga->quality, + gga->hdop, + gga->geoid); + ); +} + +/** + * Callout example for RMC + */ +void gprmc_callout(nmeap_context_t *context, void *data, void *user_data) +{ + (void)context; + (void)user_data; + (void)data; + LOG_INFOB( + NmeaRmc *rmc = (NmeaRmc *)data; + + LOG_INFO("Found GPRMC message %lu %c %ld %ld %d %d %d\n", + rmc->time, + rmc->warn, + (long)rmc->latitude, + (long)rmc->longitude, + rmc->speed, + rmc->course, + rmc->mag_var); + ); +} + +/** + * Callout example for GSV data + */ +void gpgsv_callout(nmeap_context_t *context, void *data, void *user_data) +{ + (void)context; + (void)user_data; + (void)data; + LOG_INFOB( + NmeaGsv *gsv = (NmeaGsv *)data; + + LOG_INFO("Found GPGSV message %d %d %d\n", gsv->tot_message, gsv->message_num, gsv->tot_svv); + + for (int i = 0; i < 4; i++) + LOG_INFO("%d %d %d %d\n", gsv->info[i].sv_prn, gsv->info[i].elevation, gsv->info[i].azimut, gsv->info[i].snr); + ); +} + +/** + * Callout example for VTG data + */ +void gpvtg_callout(nmeap_context_t *context, void *data, void *user_data) +{ + (void)context; + (void)user_data; + (void)data; + LOG_INFOB( + NmeaVtg *vtg = (NmeaVtg *)data; + LOG_INFO("Found GPVTG message %d %d %d\n", vtg->track_good, vtg->knot_speed, vtg->km_speed); + ); +} + + /** * standard GPGGA sentence parser @@ -229,10 +336,8 @@ int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence) NmeaGga *gga = (NmeaGga *)sentence->data; ASSERT(gga); + ASSERT(context->tokens >= 12); - /* - * if there is a data element, extract data from the tokens - */ gga->latitude = nmea_latitude(context->token[2],context->token[3]); gga->longitude = nmea_longitude(context->token[4],context->token[5]); gga->altitude = nmea_altitude(context->token[9],context->token[10]); @@ -263,26 +368,20 @@ int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence) */ NmeaRmc *rmc = (NmeaRmc *)sentence->data; + ASSERT(rmc); + ASSERT(context->tokens >= 10); + /* - * if there is a data element, use it + * extract data from the tokens */ - if (rmc != 0) - { - /* - * extract data from the tokens - */ - rmc->time = timestampToSec(tokenToInt(context->token[1], 3), atoi(context->token[9])); - rmc->warn = *context->token[2]; - rmc->latitude = nmea_latitude(context->token[3],context->token[4]); - rmc->longitude = nmea_longitude(context->token[5],context->token[6]); - rmc->speed = atoi(context->token[7]); - rmc->course = atoi(context->token[8]); - rmc->mag_var = atoi(context->token[10]); - } + rmc->time = timestampToSec(tokenToInt(context->token[1], 3), tokenToInt(context->token[9], 0)); + rmc->warn = *context->token[2]; + rmc->latitude = nmea_latitude(context->token[3],context->token[4]); + rmc->longitude = nmea_longitude(context->token[5],context->token[6]); + rmc->speed = atoi(context->token[7]); + rmc->course = atoi(context->token[8]); + rmc->mag_var = atoi(context->token[10]); - /* - * if the sentence has a callout, call it - */ if (sentence->callout != 0) (*sentence->callout)(context, rmc, context->user_data); @@ -301,18 +400,15 @@ int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence) */ NmeaVtg *vtg = (NmeaVtg *)sentence->data; + ASSERT(vtg); + ASSERT(context->tokens >= 7); + /* - * if there is a data element, use it + * extract data from the tokens */ - if (vtg != 0) - { - /* - * extract data from the tokens - */ - vtg->track_good = atoi(context->token[1]); - vtg->knot_speed = atoi(context->token[5]); - vtg->km_speed = atoi(context->token[7]); - } + vtg->track_good = atoi(context->token[1]); + vtg->knot_speed = atoi(context->token[5]); + vtg->km_speed = atoi(context->token[7]); /* * if the sentence has a callout, call it @@ -334,35 +430,24 @@ int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence) */ NmeaGsv *gsv = (NmeaGsv *)sentence->data; + /* - * if there is a data element, use it + * extract data from the tokens */ - if (gsv != 0) + gsv->tot_message = atoi(context->token[1]); + gsv->message_num = atoi(context->token[2]); + gsv->tot_svv = atoi(context->token[3]); + + // Fill remaning member until we have token + int j = 0; + for (int i = 4; i < context->tokens - 3; i += 4, j++) { - /* - * extract data from the tokens - */ - gsv->tot_message = atoi(context->token[1]); - gsv->message_num = atoi(context->token[2]); - gsv->tot_svv = atoi(context->token[3]); - gsv->sv_prn = atoi(context->token[4]); - gsv->elevation = atoi(context->token[5]); - gsv->azimut = atoi(context->token[6]); - gsv->snr = atoi(context->token[7]); - gsv->sv_prn2 = atoi(context->token[8]); - gsv->elevation2 = atoi(context->token[9]); - gsv->azimut2 = atoi(context->token[10]); - gsv->snr2 = atoi(context->token[11]); - gsv->sv_prn3 = atoi(context->token[12]); - gsv->elevation3 = atoi(context->token[13]); - gsv->azimut3 = atoi(context->token[14]); - gsv->snr3 = atoi(context->token[15]); - gsv->sv_prn4 = atoi(context->token[16]); - gsv->elevation4 = atoi(context->token[17]); - gsv->azimut4 = atoi(context->token[18]); - gsv->snr4 = atoi(context->token[19]); - } + gsv->info[j].sv_prn = atoi(context->token[i]); + gsv->info[j].elevation = atoi(context->token[i + 1]); + gsv->info[j].azimut = atoi(context->token[i + 2]); + gsv->info[j].snr = atoi(context->token[i + 3]); + } /* * if the sentence has a callout, call it @@ -373,6 +458,10 @@ int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence) return NMEA_GPGSV; } + +/* + * Parse NMEA sentence from a channel. + */ void nmea_poll(nmeap_context_t *context, KFile *channel) { int c;