4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
32 * \brief NMEA implementation.
34 * \author Daniele Basile <asterix@develer.com>
41 #include "cfg/cfg_nmea.h"
43 #include <cfg/debug.h>
45 #define LOG_LEVEL NMEA_LOG_LEVEL
46 #define LOG_FORMAT NMEA_LOG_FORMAT
49 #include <net/nmeap/inc/nmeap.h>
56 static uint32_t tokenToInt(const char *s, int precision)
59 bool sep_found = false;
65 for(i = 0; i < NMEAP_MAX_SENTENCE_LENGTH; i++)
75 if (c == '\0' || !isdigit(c) || (precision == 0 && sep_found))
91 static udegree_t convertToDegree(const char *str)
102 dec = tokenToInt(str, 4);
104 min = dec - deg * 1000000;
105 dec = deg * 1000000 + ((min * 5) + 1) / 3;
110 static udegree_t nmea_latitude(const char *plat, const char *phem)
120 /* north lat is +, south lat is - */
131 return ns * convertToDegree(plat);
134 static udegree_t nmea_longitude(const char *plot, const char *phem)
138 /* west long is negative, east long is positive */
152 return ew * convertToDegree(plot);
155 static uint16_t nmea_altitude(const char *palt, const char *punits)
168 /* convert to feet */
169 /* alt = alt * 3.2808399 */
170 alt = alt * 3 + /* 3.0 */
171 (alt >> 2) + /* 0.25 */
172 (alt >> 6) + /* 0.015625 */
173 (alt >> 7) + /* 0.0078125 */
174 (alt >> 8); /* 0,00390625 */
181 static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp)
188 memset(&t, 0, sizeof(t));
189 memset(&tmr, 0, sizeof(tmr));
190 memset(&date, 0, sizeof(date));
192 LOG_INFO("time_s[%lu],date[%lu]\n", (long)time_stamp, (long)date_stamp);
193 uint32_t res = time_stamp / 1000;
194 uint32_t all = time_stamp;
195 msec = all - res * 1000;
196 for (int i = 0; i < 3; i++)
200 tmr[i] = all - res * 100;
201 LOG_INFO("t[%d]%d\n", tmr[i],i);
204 t.tm_sec = tmr[0] + (ROUND_UP(msec, 1000) / 1000);
207 //If we not have refence data, we set as default 1/1/1970.
214 res = all = date_stamp;
215 for (int i = 0; i < 3; i++)
219 date[i] = all - res * 100;
220 LOG_INFO("d[%d]%d\n", date[i],i);
223 t.tm_mon = date[1] - 1; // time struct count month from 0 to 11;
224 // we should specific number of years from 1900, but the year field
225 // is only two cipher, so we sum 100 (2000 - 1900)..
226 t.tm_year = date[0] + 100;
228 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);
235 * standard GPGGA sentence parser
237 int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
240 * get pointer to sentence data
242 NmeaGga *gga = (NmeaGga *)sentence->data;
247 * if there is a data element, extract data from the tokens
249 gga->latitude = nmea_latitude(context->token[2],context->token[3]);
250 gga->longitude = nmea_longitude(context->token[4],context->token[5]);
251 gga->altitude = nmea_altitude(context->token[9],context->token[10]);
252 gga->time = timestampToSec(tokenToInt(context->token[1], 3), 0);
253 gga->satellites = atoi(context->token[7]);
254 gga->quality = atoi(context->token[6]);
255 gga->hdop = tokenToInt(context->token[8], 1);
256 gga->geoid = nmea_altitude(context->token[11],context->token[12]);
259 * if the sentence has a callout, call it
262 if (sentence->callout != 0)
263 (*sentence->callout)(context, gga, context->user_data);
269 * standard GPRMCntence parser
271 int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
275 * get pointer to sentence data
277 NmeaRmc *rmc = (NmeaRmc *)sentence->data;
280 * if there is a data element, use it
285 * extract data from the tokens
287 rmc->time = timestampToSec(tokenToInt(context->token[1], 3), atoi(context->token[9]));
288 rmc->warn = *context->token[2];
289 rmc->latitude = nmea_latitude(context->token[3],context->token[4]);
290 rmc->longitude = nmea_longitude(context->token[5],context->token[6]);
291 rmc->speed = atoi(context->token[7]);
292 rmc->course = atoi(context->token[8]);
293 rmc->mag_var = atoi(context->token[10]);
297 * if the sentence has a callout, call it
299 if (sentence->callout != 0)
300 (*sentence->callout)(context, rmc, context->user_data);
307 * standard GPVTG sentence parser
309 int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
313 * get pointer to sentence data
315 NmeaVtg *vtg = (NmeaVtg *)sentence->data;
318 * if there is a data element, use it
323 * extract data from the tokens
325 vtg->track_good = atoi(context->token[1]);
326 vtg->knot_speed = atoi(context->token[5]);
327 vtg->km_speed = atoi(context->token[7]);
331 * if the sentence has a callout, call it
333 if (sentence->callout != 0)
334 (*sentence->callout)(context, vtg, context->user_data);
340 * standard GPGDSV sentence parser
342 int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
346 * get pointer to sentence data
348 NmeaGsv *gsv = (NmeaGsv *)sentence->data;
351 * if there is a data element, use it
356 * extract data from the tokens
358 gsv->tot_message = atoi(context->token[1]);
359 gsv->message_num = atoi(context->token[2]);
360 gsv->tot_svv = atoi(context->token[3]);
361 gsv->sv_prn = atoi(context->token[4]);
362 gsv->elevation = atoi(context->token[5]);
363 gsv->azimut = atoi(context->token[6]);
364 gsv->snr = atoi(context->token[7]);
365 gsv->sv_prn2 = atoi(context->token[8]);
366 gsv->elevation2 = atoi(context->token[9]);
367 gsv->azimut2 = atoi(context->token[10]);
368 gsv->snr2 = atoi(context->token[11]);
369 gsv->sv_prn3 = atoi(context->token[12]);
370 gsv->elevation3 = atoi(context->token[13]);
371 gsv->azimut3 = atoi(context->token[14]);
372 gsv->snr3 = atoi(context->token[15]);
373 gsv->sv_prn4 = atoi(context->token[16]);
374 gsv->elevation4 = atoi(context->token[17]);
375 gsv->azimut4 = atoi(context->token[18]);
376 gsv->snr4 = atoi(context->token[19]);
381 * if the sentence has a callout, call it
383 if (sentence->callout != 0)
384 (*sentence->callout)(context, gsv, context->user_data);
389 void nmea_poll(nmeap_context_t *context, KFile *channel)
392 while ((c = kfile_getc(channel)) != EOF)
394 nmeap_parse(context, c);