Fix return function.
[bertos.git] / bertos / net / nmea.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  * \brief NMEA implementation.
33  *
34  * \author Daniele Basile <asterix@develer.com>
35  *
36  */
37
38 #include "nmea.h"
39
40 #include "cfg/cfg_nmea.h"
41
42 #include <cfg/debug.h>
43
44 #define LOG_LEVEL  NMEA_LOG_LEVEL
45 #define LOG_FORMAT NMEA_LOG_FORMAT
46 #include <cfg/log.h>
47
48 #include <net/nmeap/inc/nmeap.h>
49
50 #include <ctype.h>
51 #include <time.h>
52 #include <string.h>
53
54
55 static uint32_t tokenToInt(const char *s)
56 {
57         uint32_t num = 0;
58         int i;
59
60         ASSERT(s);
61
62         for(i = 0; i < NMEAP_MAX_TOKENS; i++)
63         {
64                 char c = *s++;
65
66                 if (c == '.')
67                         continue;
68
69                 if (c == '\0' || !isdigit(c))
70                         break;
71
72                 num *= 10;
73                 num += c - '0';
74         }
75
76         return num;
77 }
78
79 static udegree_t convertToDegree(const char *str)
80 {
81         uint32_t dec;
82         uint32_t deg;
83         uint32_t min;
84
85         if (*str == 0)
86         {
87         return 0;
88     }
89
90         dec = tokenToInt(str);
91         deg = dec / 1000000;
92         min = dec - deg * 1000000;
93         dec = deg * 1000000 + ((min * 5) + 1) / 3;
94
95         return dec;
96 }
97
98 static udegree_t nmea_latitude(const char *plat, const char *phem)
99 {
100         int ns;
101
102     if (*phem == 0)
103         {
104         return 0;
105     }
106
107
108     /* north lat is +, south lat is - */
109     if (*phem == 'N')
110         {
111         ns = 1;
112     }
113     else
114         {
115         ns = -1;
116     }
117
118
119         return ns * convertToDegree(plat);
120 }
121
122 static udegree_t nmea_longitude(const char *plot, const char *phem)
123 {
124         int ew;
125
126     /* west long is negative, east long is positive */
127     if (*phem == 'E')
128         {
129         ew = 1;
130     }
131     else {
132         ew = -1;
133     }
134
135     if (*phem == 0)
136         {
137         return 0;
138     }
139
140         return ew * convertToDegree(plot);
141 }
142
143 static uint16_t nmea_altitude(const char *palt, const char *punits)
144 {
145         uint32_t alt;
146
147         if (*palt == 0)
148         {
149         return 0;
150     }
151
152         alt = tokenToInt(palt);
153
154     if (*punits == 'F')
155         {
156         /* convert to feet */
157         /* alt = alt * 3.2808399 */
158                 alt = alt * 3 +  /* 3.0 */
159                           (alt >> 2) + /* 0.25 */
160                           (alt >> 6) + /* 0.015625 */
161                           (alt >> 7) + /* 0.0078125 */
162                           (alt >> 8); /* 0,00390625 */
163
164     }
165
166         return alt;
167 }
168
169 static time_t timestampToSec(uint32_t time_stamp, uint32_t date_stamp)
170 {
171         struct tm t;
172         uint16_t msec;
173         uint16_t tmr[3];
174         uint16_t date[3];
175
176         memset(&t, 0, sizeof(t));
177         memset(&tmr, 0, sizeof(tmr));
178         memset(&date, 0, sizeof(date));
179
180         LOG_INFO("time_s[%u],date[%u]\n", time_stamp, date_stamp);
181         uint32_t res = time_stamp / 1000;
182         uint32_t all = time_stamp;
183         msec = all - res * 1000;
184         for (int i = 0; i < 3; i++)
185         {
186                 all = res;
187                 res = all / 100;
188                 tmr[i]  = all - res * 100;
189                 LOG_INFO("t[%d]%d\n", tmr[i],i);
190         }
191
192         t.tm_sec = tmr[0] + (ROUND_UP(msec, 1000) / 1000);
193         t.tm_min = tmr[1];
194         t.tm_hour = tmr[2];
195         //If we not have refence data, we set as default 1/1/1970.
196         t.tm_mday = 1;
197         t.tm_mon = 1;
198         t.tm_year = 70;
199
200         if (date_stamp)
201         {
202                 res = all = date_stamp;
203                 for (int i = 0; i < 3; i++)
204                 {
205                         all = res;
206                         res = all / 100;
207                         date[i]  = all - res * 100;
208                         LOG_INFO("d[%d]%d\n", date[i],i);
209                 }
210                 t.tm_mday = date[2];
211                 t.tm_mon = date[1] - 1; // time struct count month from 0 to 11;
212                 // we should specific number of years from 1900, but the year field
213                 // is only two cipher, so we sum 100 (2000 - 1900)..
214                 t.tm_year = date[0] + 100;
215         }
216         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);
217
218         return  mktime(&t);
219 }
220
221
222 /**
223  * standard GPGGA sentence parser
224  */
225 int nmea_gpgga(nmeap_context_t *context, nmeap_sentence_t *sentence)
226 {
227         /*
228          * get pointer to sentence data
229          */
230         NmeaGga *gga = (NmeaGga *)sentence->data;
231
232         /*
233          * if there is a data element, extract data from the tokens
234          */
235         if (gga != 0)
236         {
237                 gga->latitude   = nmea_latitude(context->token[2],context->token[3]);
238                 gga->longitude  = nmea_longitude(context->token[4],context->token[5]);
239                 gga->altitude   = nmea_altitude(context->token[9],context->token[10]);
240                 gga->time       = timestampToSec(tokenToInt(context->token[1]), 0);
241                 gga->satellites = tokenToInt(context->token[7]);
242                 gga->quality    = tokenToInt(context->token[6]);
243                 gga->hdop       = tokenToInt(context->token[8]);
244                 gga->geoid      = nmea_altitude(context->token[11],context->token[12]);
245
246         }
247
248
249         /*
250          * if the sentence has a callout, call it
251          */
252         if (sentence->callout != 0)
253                 (*sentence->callout)(context, gga, context->user_data);
254
255         return NMEA_GPGGA;
256 }
257
258 /**
259  * standard GPRMCntence parser
260  */
261 int nmea_gprmc(nmeap_context_t *context, nmeap_sentence_t *sentence)
262 {
263
264     /*
265          * get pointer to sentence data
266          */
267     NmeaRmc *rmc = (NmeaRmc *)sentence->data;
268
269         /*
270          * if there is a data element, use it
271          */
272         if (rmc != 0)
273         {
274                 /*
275                  * extract data from the tokens
276                  */
277                 rmc->time       = timestampToSec(tokenToInt(context->token[1]), tokenToInt(context->token[9]));
278                 rmc->warn       = *context->token[2];
279                 rmc->latitude   = nmea_latitude(context->token[3],context->token[4]);
280                 rmc->longitude  = nmea_longitude(context->token[5],context->token[6]);
281                 rmc->speed      = tokenToInt(context->token[7]);
282                 rmc->course     = tokenToInt(context->token[8]);
283                 rmc->mag_var    = tokenToInt(context->token[10]);
284         }
285
286     /*
287          * if the sentence has a callout, call it
288          */
289     if (sentence->callout != 0)
290         (*sentence->callout)(context, rmc, context->user_data);
291
292     return NMEA_GPRMC;
293 }
294
295
296 /**
297  * standard GPVTG sentence parser
298  */
299 int nmea_gpvtg(nmeap_context_t *context, nmeap_sentence_t *sentence)
300 {
301
302     /*
303          * get pointer to sentence data
304          */
305     NmeaVtg *vtg = (NmeaVtg *)sentence->data;
306
307         /*
308          * if there is a data element, use it
309          */
310         if (vtg != 0)
311         {
312                 /*
313                  * extract data from the tokens
314                  */
315                 vtg->track_good  = tokenToInt(context->token[1]);
316                 vtg->knot_speed  = tokenToInt(context->token[5]);
317                 vtg->km_speed    = tokenToInt(context->token[7]);
318         }
319
320     /*
321          * if the sentence has a callout, call it
322          */
323     if (sentence->callout != 0)
324         (*sentence->callout)(context, vtg, context->user_data);
325
326     return NMEA_GPVTG;
327 }
328
329 /**
330  * standard GPGDSV sentence parser
331  */
332 int nmea_gpgsv(nmeap_context_t *context, nmeap_sentence_t *sentence)
333 {
334
335     /*
336          * get pointer to sentence data
337          */
338     NmeaGsv *gsv = (NmeaGsv *)sentence->data;
339
340         /*
341          * if there is a data element, use it
342          */
343         if (gsv != 0)
344         {
345                 /*
346                  * extract data from the tokens
347                  */
348                 gsv->tot_message     = tokenToInt(context->token[1]);
349                 gsv->message_num     = tokenToInt(context->token[2]);
350                 gsv->tot_svv         = tokenToInt(context->token[3]);
351                 gsv->sv_prn          = tokenToInt(context->token[4]);
352                 gsv->elevation       = tokenToInt(context->token[5]);
353                 gsv->azimut          = tokenToInt(context->token[6]);
354                 gsv->snr             = tokenToInt(context->token[7]);
355                 gsv->sv_prn2         = tokenToInt(context->token[8]);
356                 gsv->elevation2      = tokenToInt(context->token[9]);
357                 gsv->azimut2         = tokenToInt(context->token[10]);
358                 gsv->snr2            = tokenToInt(context->token[11]);
359                 gsv->sv_prn3         = tokenToInt(context->token[12]);
360                 gsv->elevation3      = tokenToInt(context->token[13]);
361                 gsv->azimut3         = tokenToInt(context->token[14]);
362                 gsv->snr3            = tokenToInt(context->token[15]);
363                 gsv->sv_prn4         = tokenToInt(context->token[16]);
364                 gsv->elevation4      = tokenToInt(context->token[17]);
365                 gsv->azimut4         = tokenToInt(context->token[18]);
366                 gsv->snr4            = tokenToInt(context->token[19]);
367         }
368
369
370     /*
371          * if the sentence has a callout, call it
372          */
373     if (sentence->callout != 0)
374         (*sentence->callout)(context, gsv, context->user_data);
375
376     return NMEA_GPGSV;
377 }
378
379 void nmea_poll(nmeap_context_t *context, KFile *channel)
380 {
381         int c;
382         while ((c = kfile_getc(channel)) != EOF)
383         {
384                 nmeap_parse(context, c);
385         }
386 }
387