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>
57 static uint32_t tokenToInt(const char *s, int precision)
60 bool sep_found = false;
66 for(i = 0; i < NMEAP_MAX_SENTENCE_LENGTH; i++)
76 if (c == '\0' || !isdigit(c) || (precision == 0 && sep_found))
92 static udegree_t convertToDegree(const char *str)
103 dec = tokenToInt(str, 4);
105 min = dec - deg * 1000000;
106 dec = deg * 1000000 + ((min * 5) + 1) / 3;
111 static udegree_t nmea_latitude(const char *plat, const char *phem)
118 /* north lat is +, south lat is - */
125 return ns * convertToDegree(plat);
128 static udegree_t nmea_longitude(const char *plot, const char *phem)
135 /* west long is negative, east long is positive */
141 return ew * convertToDegree(plot);
144 static uint16_t nmea_altitude(const char *palt, const char *punits)
155 /* convert to feet */
156 /* alt = alt * 3.2808399 */
157 alt = alt * 3 + /* 3.0 */
158 (alt >> 2) + /* 0.25 */
159 (alt >> 6) + /* 0.015625 */
160 (alt >> 7) + /* 0.0078125 */
161 (alt >> 8); /* 0,00390625 */
168 static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp)
175 memset(&t, 0, sizeof(t));
176 memset(&tmr, 0, sizeof(tmr));
177 memset(&date, 0, sizeof(date));
179 LOG_INFO("time_s[%lu],date[%lu]\n", (long)time_stamp, (long)date_stamp);
180 uint32_t res = time_stamp / 1000;
181 uint32_t all = time_stamp;
182 msec = all - res * 1000;
183 for (int i = 0; i < 3; i++)
187 tmr[i] = all - res * 100;
188 LOG_INFO("t[%d]%d\n", tmr[i],i);
191 t.tm_sec = tmr[0] + (ROUND_UP(msec, 1000) / 1000);
194 //If we not have refence data, we set as default 1/1/1970.
201 res = all = date_stamp;
202 for (int i = 0; i < 3; i++)
206 date[i] = all - res * 100;
207 LOG_INFO("d[%d]%d\n", date[i],i);
210 t.tm_mon = date[1] - 1; // time struct count month from 0 to 11;
211 // we should specific number of years from 1900, but the year field
212 // is only two cipher, so we sum 100 (2000 - 1900)..
213 t.tm_year = date[0] + 100;
215 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);
222 * standard GPGGA sentence parser
224 int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
227 * get pointer to sentence data
229 NmeaGga *gga = (NmeaGga *)sentence->data;
232 ASSERT(context->tokens >= 12);
234 gga->latitude = nmea_latitude(context->token[2],context->token[3]);
235 gga->longitude = nmea_longitude(context->token[4],context->token[5]);
236 gga->altitude = nmea_altitude(context->token[9],context->token[10]);
237 gga->time = timestampToSec(tokenToInt(context->token[1], 3), 0);
238 gga->satellites = atoi(context->token[7]);
239 gga->quality = atoi(context->token[6]);
240 gga->hdop = tokenToInt(context->token[8], 1);
241 gga->geoid = nmea_altitude(context->token[11],context->token[12]);
244 * if the sentence has a callout, call it
247 if (sentence->callout != 0)
248 (*sentence->callout)(context, gga, context->user_data);
254 * standard GPRMCntence parser
256 int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
260 * get pointer to sentence data
262 NmeaRmc *rmc = (NmeaRmc *)sentence->data;
265 ASSERT(context->tokens >= 10);
268 * extract data from the tokens
270 rmc->time = timestampToSec(tokenToInt(context->token[1], 3), atoi(context->token[9]));
271 rmc->warn = *context->token[2];
272 rmc->latitude = nmea_latitude(context->token[3],context->token[4]);
273 rmc->longitude = nmea_longitude(context->token[5],context->token[6]);
274 rmc->speed = atoi(context->token[7]);
275 rmc->course = atoi(context->token[8]);
276 rmc->mag_var = atoi(context->token[10]);
278 if (sentence->callout != 0)
279 (*sentence->callout)(context, rmc, context->user_data);
286 * standard GPVTG sentence parser
288 int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
292 * get pointer to sentence data
294 NmeaVtg *vtg = (NmeaVtg *)sentence->data;
297 ASSERT(context->tokens >= 7);
300 * extract data from the tokens
302 vtg->track_good = atoi(context->token[1]);
303 vtg->knot_speed = atoi(context->token[5]);
304 vtg->km_speed = atoi(context->token[7]);
307 * if the sentence has a callout, call it
309 if (sentence->callout != 0)
310 (*sentence->callout)(context, vtg, context->user_data);
316 * standard GPGDSV sentence parser
318 int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
322 * get pointer to sentence data
324 NmeaGsv *gsv = (NmeaGsv *)sentence->data;
328 * extract data from the tokens
330 gsv->tot_message = atoi(context->token[1]);
331 gsv->message_num = atoi(context->token[2]);
332 gsv->tot_svv = atoi(context->token[3]);
334 // Fill remaning member until we have token
336 for (int i = 4; i < context->tokens - 3; i += 4, j++)
339 gsv->info[j].sv_prn = atoi(context->token[i]);
340 gsv->info[j].elevation = atoi(context->token[i + 1]);
341 gsv->info[j].azimut = atoi(context->token[i + 2]);
342 gsv->info[j].snr = atoi(context->token[i + 3]);
346 * if the sentence has a callout, call it
348 if (sentence->callout != 0)
349 (*sentence->callout)(context, gsv, context->user_data);
354 void nmea_poll(nmeap_context_t *context, KFile *channel)
357 while ((c = kfile_getc(channel)) != EOF)
359 nmeap_parse(context, c);