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>
40 #include "cfg/cfg_nmea.h"
42 #include <cfg/debug.h>
44 #define LOG_LEVEL NMEA_LOG_LEVEL
45 #define LOG_FORMAT NMEA_LOG_FORMAT
48 #include <net/nmeap/inc/nmeap.h>
55 static uint32_t tokenToInt(const char *s)
62 for(i = 0; i < NMEAP_MAX_TOKENS; i++)
69 if (c == '\0' || !isdigit(c))
79 static udegree_t convertToDegree(const char *str)
90 dec = tokenToInt(str);
92 min = dec - deg * 1000000;
93 dec = deg * 1000000 + ((min * 5) + 1) / 3;
96 static udegree_t nmea_latitude(const char *plat, const char *phem)
106 /* north lat is +, south lat is - */
117 return ns * convertToDegree(plat);
120 static udegree_t nmea_longitude(const char *plot, const char *phem)
124 /* west long is negative, east long is positive */
138 return ew * convertToDegree(plot);
141 static uint16_t nmea_altitude(const char *palt, const char *punits)
150 alt = tokenToInt(palt);
154 /* convert to feet */
155 /* alt = alt * 3.2808399 */
156 alt = alt * 3 + /* 3.0 */
157 (alt >> 2) + /* 0.25 */
158 (alt >> 6) + /* 0.015625 */
159 (alt >> 7) + /* 0.0078125 */
160 (alt >> 8); /* 0,00390625 */
167 static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp)
174 memset(&t, 0, sizeof(t));
175 memset(&tmr, 0, sizeof(tmr));
176 memset(&date, 0, sizeof(date));
178 LOG_INFO("time_s[%u],date[%u]\n", time_stamp, date_stamp);
179 uint32_t res = time_stamp / 1000;
180 uint32_t all = time_stamp;
181 msec = all - res * 1000;
182 for (int i = 0; i < 3; i++)
186 tmr[i] = all - res * 100;
187 LOG_INFO("t[%d]%d\n", tmr[i],i);
190 t.tm_sec = tmr[0] + (ROUND_UP(msec, 1000) / 1000);
193 //If we not have refence data, we set as default 1/1/1970.
200 res = all = date_stamp;
201 for (int i = 0; i < 3; i++)
205 date[i] = all - res * 100;
206 LOG_INFO("d[%d]%d\n", date[i],i);
209 t.tm_mon = date[1] - 1; // time struct count month from 0 to 11;
210 // we should specific number of years from 1900, but the year field
211 // is only two cipher, so we sum 100 (2000 - 1900)..
212 t.tm_year = date[0] + 100;
214 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);
221 * standard GPGGA sentence parser
223 int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
226 * get pointer to sentence data
228 NmeaGga *gga = (NmeaGga *)sentence->data;
231 * if there is a data element, extract data from the tokens
235 gga->latitude = nmea_latitude(context->token[2],context->token[3]);
236 gga->longitude = nmea_longitude(context->token[4],context->token[5]);
237 gga->altitude = nmea_altitude(context->token[9],context->token[10]);
238 gga->time = timestampToSec(tokenToInt(context->token[1]), 0);
239 gga->satellites = tokenToInt(context->token[7]);
240 gga->quality = tokenToInt(context->token[6]);
241 gga->hdop = tokenToInt(context->token[8]);
242 gga->geoid = nmea_altitude(context->token[11],context->token[12]);
248 * if the sentence has a callout, call it
250 if (sentence->callout != 0)
251 (*sentence->callout)(context, gga, context->user_data);
257 * standard GPRMCntence parser
259 int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
263 * get pointer to sentence data
265 NmeaRmc *rmc = (NmeaRmc *)sentence->data;
268 * if there is a data element, use it
273 * extract data from the tokens
275 rmc->time = timestampToSec(tokenToInt(context->token[1]), tokenToInt(context->token[9]));
276 rmc->warn = *context->token[2];
277 rmc->latitude = nmea_latitude(context->token[3],context->token[4]);
278 rmc->longitude = nmea_longitude(context->token[5],context->token[6]);
279 rmc->speed = tokenToInt(context->token[7]);
280 rmc->course = tokenToInt(context->token[8]);
281 rmc->mag_var = tokenToInt(context->token[10]);
285 * if the sentence has a callout, call it
287 if (sentence->callout != 0)
288 (*sentence->callout)(context, rmc, context->user_data);
295 * standard GPVTG sentence parser
297 int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
301 * get pointer to sentence data
303 NmeaVtg *vtg = (NmeaVtg *)sentence->data;
306 * if there is a data element, use it
311 * extract data from the tokens
313 vtg->track_good = tokenToInt(context->token[1]);
314 vtg->knot_speed = tokenToInt(context->token[5]);
315 vtg->km_speed = tokenToInt(context->token[7]);
319 * if the sentence has a callout, call it
321 if (sentence->callout != 0)
322 (*sentence->callout)(context, vtg, context->user_data);
328 * standard GPGDSV sentence parser
330 int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
334 * get pointer to sentence data
336 NmeaGsv *gsv = (NmeaGsv *)sentence->data;
339 * if there is a data element, use it
344 * extract data from the tokens
346 gsv->tot_message = tokenToInt(context->token[1]);
347 gsv->message_num = tokenToInt(context->token[2]);
348 gsv->tot_svv = tokenToInt(context->token[3]);
349 gsv->sv_prn = tokenToInt(context->token[4]);
350 gsv->elevation = tokenToInt(context->token[5]);
351 gsv->azimut = tokenToInt(context->token[6]);
352 gsv->snr = tokenToInt(context->token[7]);
353 gsv->sv_prn2 = tokenToInt(context->token[8]);
354 gsv->elevation2 = tokenToInt(context->token[9]);
355 gsv->azimut2 = tokenToInt(context->token[10]);
356 gsv->snr2 = tokenToInt(context->token[11]);
357 gsv->sv_prn3 = tokenToInt(context->token[12]);
358 gsv->elevation3 = tokenToInt(context->token[13]);
359 gsv->azimut3 = tokenToInt(context->token[14]);
360 gsv->snr3 = tokenToInt(context->token[15]);
361 gsv->sv_prn4 = tokenToInt(context->token[16]);
362 gsv->elevation4 = tokenToInt(context->token[17]);
363 gsv->azimut4 = tokenToInt(context->token[18]);
364 gsv->snr4 = tokenToInt(context->token[19]);
369 * if the sentence has a callout, call it
371 if (sentence->callout != 0)
372 (*sentence->callout)(context, gsv, context->user_data);
377 void nmea_poll(nmeap_context_t *context, KFile *channel)
380 while ((c = kfile_getc(channel)) != EOF)
382 nmeap_parse(context, c);