~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_gps.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_gps.c 1136 2010-11-28 16:07:16Z mzuther $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_gps.c $
 
3
 *
 
4
 * gps plugin (nmea), code by michu / www.neophob.com, based on nmeap, http://sourceforge.net/projects/nmeap/
 
5
 *
 
6
 * Copyright (C) 2007 Michu - http://www.neophob.com
 
7
 * Copyright (C) 2004, 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
11
 * LCD4Linux is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2, or (at your option)
 
14
 * any later version.
 
15
 *
 
16
 * LCD4Linux is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * GPS Plugin for lcd4linux, by michael vogt / http://www.neophob.com
 
29
 * contact: michu@neophob.com
 
30
 *
 
31
 * based on nmeap by daveh, http://www.dmh2000.com/ or http://sourceforge.net/projects/nmeap/
 
32
 * you need a compiled libnmeap to compile this plugin.
 
33
 *
 
34
 * History:
 
35
 *      v0.1 -initial release
 
36
 *      v0.2 -fix include files, include <termios.h> and <asm/fcntl.h> (embedded devices)
 
37
 *           -fixed emulation mode (read only x bytes instead of the whole emulated nmea data)
 
38
 *           -improved error handling (if there are no parameters defined, lcd4linx will display "GPS ARG ERR")
 
39
 *           -display raw nmea string from the gps receiver
 
40
 *           -added support for gps device with 9600 baud (env. variable)
 
41
 *           -added the option that the widget graps data from the buffer and not from the gps device (usefull for multible widgets)
 
42
 *      v0.3 -improved nmea parsing
 
43
 *           -improved gps-emulator
 
44
 *           -time is now updated with rmc and gga sentence
 
45
 *
 
46
 * TODO:
 
47
 *      -update direction only when speed > 5 kmh
 
48
 *      -add more data fields
 
49
 *      -dump nmea string to external file
 
50
 *
 
51
 * Exported functions:
 
52
 * int plugin_init_gps (void) - default plugin initialisation
 
53
 * void parse(SHOW-OPTIONS, DISPLAY-OPTIONS)
 
54
 *      display option define, what the plugin should display
 
55
 *
 
56
 *      OPTION:                                         EXAMPLE:
 
57
 *      -------                                         --------
 
58
 *
 
59
 * PARAMETER 1:
 
60
 *      #define SHOW_ALTITUDE           0x000000001     alt:500
 
61
 *      #define SHOW_SPEED              0x000000010     spd:30
 
62
 *      #define SHOW_COURSE             0x000000100     dir:NO
 
63
 *      #define SHOW_SATELLITES         0x000001000     sat:4
 
64
 *      #define SHOW_QUALITY            0x000010000     qua:1
 
65
 *      #define SHOW_STATUS             0x000100000     sta:V
 
66
 *      #define SHOW_TIME_UTC           0x001000000     utc:113459
 
67
 *      #define SHOW_DATE               0x010000000     dat:190204 (19.02.2004)
 
68
 *
 
69
 * PARAMETER 2: 
 
70
 *      #define OPTION_NO_PREFIX        0x000000001     disable prefix (example, instead of "alt:500" it displays "500"
 
71
 *      #define OPTION_SPEED_IN_KNOTS   0x000000010     when use the SHOW_SPEED option, display speed in knots instead in km/h
 
72
 *      #define OPTION_RAW_NMEA         0x000000100     outputs the parsed nmea string, only valid when EMULATE is not defined!
 
73
 *      #define OPTION_GET_BUFFERDATA   0x000001000     when you define more than 1 gps widget
 
74
 *                                                      each widget will get updates and cause some ugly side effects, specially when you display the time
 
75
 *                                                      by enabling this option, the widget will not read any nmea data from the serial port.
 
76
 *                                                      KEEP IN MIND that there must be ONE widget which get buffered data (means read data from the port)
 
77
 *
 
78
 *      #define SHOW_NMEA_STATUS        0x010000000     OK:0033/Error:0002/Incomplete:0002
 
79
 *
 
80
 *
 
81
 *      Examples:  
 
82
 *              - gps::parse('0x011','0') will display the altitude and speed -> alt:500 spd:43
 
83
 *              - gps::parse('0x01100','0') will display the course and the numbers of satellites -> dir:NO sat:3
 
84
 *              - gps::parse('0x01','0x01') will display the speed without prefix -> 50
 
85
 *              - gps::parse('0x01','0x01') will display the speed in knots without prefix -> 27
 
86
 *
 
87
 * ENV VARIABLES
 
88
 *      To finetune plugin_gps you may define some env variables:
 
89
 *              GPS_PORT        gps device, default value is /dev/usb/tts/1
 
90
 *              GPS_9600        serial port speed, default is 4800 baud, if you define this var speed will be 9600 (export GPS_9600=dummy)
 
91
 */
 
92
 
 
93
 
 
94
/* define the include files you need */
 
95
#include "config.h"
 
96
 
 
97
#include <stdio.h>
 
98
#include <stdlib.h>
 
99
#include <string.h>
 
100
#include <ctype.h>
 
101
#include <unistd.h>
 
102
#include <termios.h>            //used for serial port flags
 
103
#include <asm/fcntl.h>
 
104
 
 
105
 
 
106
/* these should always be included */
 
107
#include "debug.h"
 
108
#include "plugin.h"
 
109
 
 
110
#ifdef WITH_DMALLOC
 
111
#include <dmalloc.h>
 
112
#endif
 
113
 
 
114
#include "nmeap.h"
 
115
 
 
116
//#define EMULATE                       //remove comment to enable gps data emulation...
 
117
#define EMU_BUFFER_READ_SIZE 128        //how many bytes are read each loop aka emulation speed
 
118
#define BUFFER_SIZE 256
 
119
 
 
120
#define SHOW_ALTITUDE           0x000000001
 
121
#define SHOW_SPEED              0x000000010
 
122
#define SHOW_COURSE             0x000000100
 
123
#define SHOW_SATELLITES         0x000001000
 
124
#define SHOW_QUALITY            0x000010000
 
125
#define SHOW_STATUS             0x000100000
 
126
#define SHOW_TIME_UTC           0x001000000
 
127
#define SHOW_DATE               0x010000000
 
128
 
 
129
#define OPTION_NO_PREFIX        0x000000001
 
130
#define OPTION_SPEED_IN_KNOTS   0x000000010
 
131
#define OPTION_RAW_NMEA         0x000000100     //outputs the parsed nmea string, only valid when EMULATE is not defined!
 
132
#define OPTION_GET_BUFFERDATA   0x000001000     //when you define more than 1 gps widget
 
133
                                                //each widget will get updates and cause some ugly side effects, specially when you display the time
 
134
                                                //by enabling this option, the widget will not read any nmea data from the serial port.
 
135
                                                //KEEP IN MIND that there must be ONE widget which does not get buffered data (means read data from the port)
 
136
#define OPTION_DEBUG            0x000010000
 
137
#define SHOW_NMEA_STATUS        0x010000000
 
138
 
 
139
static float course = 0.f;      //degrees
 
140
static float altitude = 0.f;
 
141
static float speed = 0.f;       //Speed over ground in KNOTS!!
 
142
static int satellites = 0;      //Number of satellites in use (00-12)
 
143
static int quality = 0;         //GPS quality indicator (0 - fix not valid, 1 - GPS fix, 2 - DGPS fix) 
 
144
static char gpsStatus = 'V';    //A=active or V=Void 
 
145
static unsigned long gpsTime = 0;       //UTC of position fix in hhmmss format 
 
146
static unsigned long gpsDate = 0;       //Date in ddmmyy format
 
147
 
 
148
static int msgCounter = 0;      //parsed nmea-sentence
 
149
static int errCounter = 0;      //parsed error nmea-sentence
 
150
static int incomplCounter = 0;  //incomplete parsed nmea-sentence
 
151
/* ---------------------------------------------------------------------------------------*/
 
152
/* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
 
153
/*          they need to be live for the duration of the parser                           */
 
154
/* ---------------------------------------------------------------------------------------*/
 
155
static nmeap_context_t nmea;    /* parser context */
 
156
static nmeap_gga_t gga;         /* this is where the data from GGA messages will show up */
 
157
static nmeap_rmc_t rmc;         /* this is where the data from RMC messages will show up */
 
158
static int user_data;           /* user can pass in anything. typically it will be a pointer to some user data */
 
159
 
 
160
static int fd_g;                /* port handler */
 
161
static unsigned int emu_read_ofs = 0;
 
162
static int debug = 0;           //debug flag
 
163
 
 
164
static char Name[] = "plugin_gps.c";
 
165
 
 
166
static int fndStr = 0;          //how many bytes were saved from the last read
 
167
static char backBuffer[BUFFER_SIZE];    //the buffer to save incomplete nmea strings
 
168
 
 
169
 
 
170
#ifdef EMULATE
 
171
char test_vector[] = {
 
172
/*    "$GPGGA,123519,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n"     // good
 
173
        "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n"    // junk 
 
174
        "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n"        // good 
 
175
        "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*48\r\n"        // checksum error 
 
176
        "$GPRMC,165110.000,A,5601.0318,N,01211.3503,E,0.09,28.11,190706,,*35\r\n"       //some more data
 
177
    "$GPGGA,165111.000,5601.0318,N,01211.3503,E,1,07,1.2,22.3,M,41.6,M,,0000*65\r\n"
 
178
        "$GPRMC,165111.000,A,5601.0318,N,01211.3503,E,0.08,43.53,190706,,*3E\r\n"
 
179
        "$GPGGA,165112.000,5601.0318,N,01211.3503,E,1,07,1.2,22.3,M,41.6,M,,0000*66\r\n"
 
180
        "$GPRMC,165112.000,A,5601.0318,N,01211.3503,E,0.08,37.38,190706,,*33\r\n"
 
181
        "$GPGGA,165113.000,5601.0318,N,01211.3503,E,1,07,1.2,22.4,M,41.6,M,,0000*60\r\n"
 
182
        "$GPRMC,165113.000,A,5601.0318,N,01211.3503,E,0.11,21.03,190706,,*35\r\n"
 
183
        "$GPGGA,165114.000,5601.0318,N,01211.3504,E,1,07,1.2,22.6,M,41.6,M,,0000*62\r\n"
 
184
        "$GPRMC,165114.000,A,5601.0318,N,01211.3504,E,0.08,45.67,190706,,*3D\r\n"
 
185
        "$GPGGA,165115.000,5601.0318,N,01211.3504,E,1,07,1.2,22.7,M,41.6,M,,0000*62\r\n"
 
186
        "$GPRMC,165115.000,A,5601.0318,N,01211.3504,E,0.10,59.41,190706,,*3C\r\n"
 
187
        "$GPGGA,165116.000,5601.0318,N,01211.3504,E,1,07,1.2,22.8,M,41.6,M,,0000*6E\r\n"
 
188
        "$GPRMC,165116.000,A,5601.0318,N,01211.3504,E,0.06,50.22,190706,,*34\r\n"
 
189
        "$GPGGA,165117.000,5601.0318,N,01211.3505,E,1,07,1.2,22.9,M,41.6,M,,0000*6F\r\n"
 
190
        "$GPRMC,165117.000,A,5601.0318,N,01211.3505,E,0.08,45.32,190706,,*3F\r\n"
 
191
        "$GPGGA,165118.000,5601.0318,N,01211.3505,E,1,07,1.2,23.0,M,41.6,M,,0000*68\r\n"
 
192
        "$GPRMC,165118.000,A,5601.0318,N,01211.3505,E,0.10,37.49,190706,,*30\r\n"*/
 
193
    "$GPGGA,165119.000,5601.0318,N,01211.3504,E,1,06,1.2,23.0,M,41.6,M,,0000*69\r\n"
 
194
        "$GPRMC,165119.000,A,5601.0318,N,01211.3504,E,0.08,27.23,190706,,*34\r\n"
 
195
        "$GPGGA,165120.000,5601.0318,N,01211.3504,E,1,07,1.2,23.0,M,41.6,M,,0000*62\r\n"
 
196
        "$GPRMC,165120.000,A,5601.0318,N,01211.3504,E,0.08,41.52,190706,,*38\r\n"
 
197
        "$GPGGA,165121.000,5601.0319,N,01211.3505,E,1,07,1.2,23.1,M,41.6,M,,0000*62\r\n"
 
198
        "$GPRMC,165121.000,A,5601.0319,N,01211.3505,E,0.09,57.19,190706,,*30\r\n"
 
199
        "$GPGGA,165122.000,5601.0319,N,01211.3505,E,1,07,1.2,23.2,M,41.6,M,,0000*62\r\n"
 
200
        "$GPRMC,165122.000,A,5601.0319,N,01211.3505,E,0.10,30.60,190706,,*34\r\n"
 
201
        "$GPGGA,165123.000,5601.0319,N,01211.3505,E,1,07,1.2,23.3,M,41.6,M,,0000*62\r\n"
 
202
        "$GPRMC,165123.000,A,5601.0319,N,01211.3505,E,0.07,45.49,190706,,*3A\r\n"
 
203
        "$GPGGA,165124.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*62\r\n"
 
204
        "$GPRMC,165124.000,A,5601.0319,N,01211.3505,E,0.09,34.85,190706,,*35\r\n"
 
205
        "$GPGGA,165125.000,5601.0319,N,01211.3506,E,1,07,1.2,23.4,M,41.6,M,,0000*60\r\n"
 
206
        "$GPRMC,165125.000,A,5601.0319,N,01211.3506,E,0.06,43.06,190706,,*33\r\n"
 
207
        "$GPGGA,165126.000,5601.0319,N,01211.3506,E,1,07,1.2,23.4,M,41.6,M,,0000*63\r\n"
 
208
        "$GPRMC,165126.000,A,5601.0319,N,01211.3506,E,0.10,37.63,190706,,*37\r\n"
 
209
        "$GPGGA,165127.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*61\r\n"
 
210
        "$GPRMC,165127.000,A,5601.0319,N,01211.3505,E,0.07,42.23,190706,,*35\r\n"
 
211
        "$GPGGA,165128.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*6E\r\n"
 
212
        "$GPRMC,165128.000,A,5601.0319,N,01211.3505,E,0.09,27.98,190706,,*37\r\n"
 
213
        "$GPGGA,165129.000,5601.0319,N,01211.3505,E,1,07,1.2,23.3,M,41.6,M,,0000*68\r\n"
 
214
        "$GPRMC,165129.000,A,5601.0319,N,01211.3505,E,0.08,51.89,190706,,*36\r\n"
 
215
        "$GPGGA,165130.000,5601.0319,N,01211.3505,E,1,07,1.2,23.2,M,41.6,M,,0000*61\r\n"
 
216
        "$GPRMC,094055.000,A,5409.998645,N,00859.370546,E,0.044,0.00,301206,,,A*55\r\n"
 
217
        "$GPGGA,094056.000,5409.998657,N,00859.370528,E,1,12,0.82,-5.168,M,45.414,M,,*47\r\n"
 
218
        "$GPRMC,094056.000,A,5409.998657,N,00859.370528,E,0.263,0.00,301206,,,A*5A\r\n"
 
219
        "$GPGGA,094057.000,5409.998726,N,00859.370512,E,1,12,0.82,-5.171,M,45.414,M,,*40\r\n"
 
220
        "$GPRMC,094057.000,A,5409.998726,N,00859.370512,E,0.311,0.00,301206,,,A*51\r\n"
 
221
        "$GPGGA,094058.000,5409.998812,N,00859.370518,E,1,12,0.82,-5.172,M,45.414,M,,*4E\r\n"
 
222
        "$GPRMC,094058.000,A,5409.998812,N,00859.370518,E,0.423,0.00,301206,,,A*5A\r\n"
 
223
        "$GPGGA,094059.000,5409.998934,N,00859.370505,E,1,12,0.82,-5.177,M,45.414,M,,*43\r\n"
 
224
        "$GPRMC,094059.000,A,5409.998934,N,00859.370505,E,0.576,0.00,301206,,,A*53\r\n"
 
225
        "$GPGGA,094100.000,5409.999097,N,00859.370542,E,1,12,0.82,-5.177,M,45.414,M,,*4C\r\n"
 
226
        "$GPRMC,094100.000,A,5409.999097,N,00859.370542,E,0.705,0.00,301206,,,A\r\n"
 
227
        "$GPGGA,004037.851,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*7A\r\n"
 
228
        "$GPGGA,175218.255,4657.3391,N,00726.2666,E,1,04,9.0,568.6,M,48.0,M,0.0,0000*7B\r\n"
 
229
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.08\r\n"
 
230
        "$GPRMC,175218.255,A,4657.3391,N,00726.2666,E,0.$GPGGA,175219.255,4657.3391,N,00726.2666,E,1,04,9.0,568.5,M,48.0,M,0.0,0000*79\r\n"
 
231
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n"
 
232
        "$7,,*06\r\n"
 
233
        "$GPGGA,175220.255,4657.3392,N,00726.2667,E,1,04,9.0,568.4,M,48.0,M,0.0,0000*70\r\n"
 
234
        "$GPGSA,A,3,26,29,17,12,,,,$GPGGA,175221.255,4657.3392,N,00726.2667,E,1,04,9.0,568.3,M,48.0,M,0.0,0000*76\r\n"
 
235
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n"
 
236
        "$GPRMC,175221.255,A,4657.3392,N,00726.2667,E,0.14,150.24,230507,,*0A\r\n"
 
237
        "$GPGGA,175222.255,4657.3393,N,00726.2668,E,1,04,9.0,568,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n"
 
238
        "$GPGSV,2,1,08,26,71,153,45,29,60,14$GPGGA,175223.255,4657.3393,N,00726.2669,E,1,04,9.0,568.2,M,48.0,M,7.3393,N,00726.2669,E,0.11,141.32,230507,,*05\r\n"
 
239
        "$GPGGA,175224.255,4657.3394,N,00726.2670,E,1,04,9.0,568.0,M,48.0,M,0.0,0000*70\r\n"
 
240
        "$GPGSA,$GPGGA,175225.255,4657.3395,N,00726.2670,E,1,04,9.0,567.8,M,48.0,M,0.0,0000*77\r\n"
 
241
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n"
 
242
        "$GPRMC,175225.255,A,4657.3395,N,00726.2670,E,0.10,146.99,230507,,*0A\r\n"
 
243
        "$GPGGA,175226.255,465567.5,M,48.0,M,0.0,0000*7B\r\n"
 
244
        "$GPGSA,A,3,26,29,17,12$GPGGA,175227.255,4657.3397,N,00726.2671,E,1,04,9.0,567.1,M,48.0,M,0.0,0000*7F\r\n"
 
245
        "$.7*38\r\n"
 
246
        "$GPGSV,2,1,08,26,71,153,46,29,60,149,46,09,55,286,00,28,32,051$GPGGA,175228.254,4657.3399,N,00726.2672,E,1,04,9.0,566.8,M,48.0,M,0.0,0000*74\r\n"
 
247
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.72,E,0.12,146.43,230507,,*0D\r\n"
 
248
        "$GPGGA,175229.254,4657.3400,N,00726.2673,E,1,04,9.0,566.4,M,48.0,M,0.0,0000*7F\r\n"
 
249
        "$GPGSA,A,3,26,29,17,1$GPGGA,175230.254,4657.3401,N,00726.2675,E,1,04,9.0,566.0,M,48.0,M,0.0,0000*74\r\n"
 
250
        "$GPGSA,A,3,26,29,17,12$GPGGA,175231.254,4657.3402,N,00726.2676,E,1,04,9.0,565.7,M,48.0,M,0.0,0000*71\r\n"
 
251
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n"
 
252
        "$GPRMC,175231.2$GPGGA,175232.254,4657.3404,N,00726.2677,E,1,04,9.,153,45,29,60,149,46,09,55,286,00,28,32,051,00*7F\r\n"
 
253
        "$GPGSV,2,2,08,17,32,102,38,18,28,298,00,12,12,21$GPGGA,175233.254,4657.3405,N,00726.2678,E,1,04,9.0,565.1,M,48.0,M,0.0,0000*7C\r\n"
 
254
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
255
        "$,*09\r\n"
 
256
        "$GPGGA,175234.254,4657.3406,N,00726.2679,E,1,04,9.0,564.9,M,48.0,M,0.0,0000*70\r\n"
 
257
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
258
        "$GPRMC,175234.254,A,4657.3$GPGGA,175235.254,4657.3408,N,00726.2680,E,1,04,9.00,M,0.0,0000*76\r\n"
 
259
        "$GPGSA,A,3,26,$GPGGA,175236.254,439\r\n"
 
260
        "$GPRMC,175236.254,A,4657.3409,N,00726.2681,E,0.14,142.56,230507,,*06\r\n"
 
261
        "$GPGGA,175237.254,4657.3410,N,00726.2682,E,1,04,9.0,564.5,M,48.0,M,0.0,0000*7C\r\n"
 
262
        "$GPGSA,A,3,26,29,17,12,,,,00,28,32,051,00*7F\r\n"
 
263
        "$GPGSV,2,2,08,17,32,102,39,18,28,298,00,12,12,218,35,22,09,3$GPGGA,175238.254,4657.3411,N,00726.2682,E,1,04$GPGGA,175239.254,4657.3412,N,00726.2683,E,1,04,9.0,564.7,M,48.0,M,0.0,0000*73\r\n"
 
264
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
265
        "$GPRMC,175239.254,A,4657.3412,N$GPGGA,175240.254,4657.3412,N,00726.2684,E,1,04,9.0,564.8,M,48.0,M,0.0,0000*75\r\n"
 
266
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
267
        "$GPRMC,$GPGGA,175241.254,4657.3413,N,00726.2684,E,1,04,9.0,565.1,M,48.0,M,0.0,0000*7D\r\n"
 
268
        ",,,,,,,,9.5,9.0,2.6*39\r\n"
 
269
        "$GPRMC,175241.254,A,4657.3413,N,00726.2684,E,0.$GPGGA,175242.254,4657.3413,N,00726.2684,E,1,04,9.0,565.3,M,48.0,M,0.0,0000*7C\r\n"
 
270
        "$GPGSA,A,3,26,29,179,60,149,46,09,55,286,00,28,32,051,00*7C\r\n"
 
271
        "$GPGS$GPGGA,175243.254,4657.3414,N,00726.2685,E,1,04,9.0,565.7,M,48.0,M,0.0,0000*7F\r\n"
 
272
        "$GPGSA,A,3$GPGGA,175244.253,4657.3414,N,00726.2685,E,1,04,9.0,566.0,M,48.0,M,0.0,0000*7B\r\n"
 
273
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
274
        "$GPGGA,175245.253,4657.3415,N,00726.2685,E,1,04,9.0,566.3,M,48.0,M,0.0,0000*78\r\n"
 
275
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
276
        "$GPRMC,175245.253,A,4657.3415,N,00726.2685,E,0.2$GPGGA,175246.253,4657.3415,N,00726.2686,E,1,04,9.0,566.5,M,48.0,M,0.0,0000*7E\r\n"
 
277
        ",175246.253,A,4657.3415,N,00726.2686,E,0.13,145$GPGGA,175247.253,4657.3416,N,00726.2686,E,1,04,9.0,566.8,M,48.0,M,0.0,0000*71\r\n"
 
278
        "$GPGSA,A,3,26,17,32,101,39*7D\r\n"
 
279
        "$GPGSV,2,2,08,28,32,051,00,18,$GPGGA,175248.253,4657.3417,N,00726.2686,E,1,04,9.0,567.0,M,48.0,M,0.0,0000*76\r\n"
 
280
        "$GPGSA,A,3,26,29$GPGGA,175249.253,4657.3417,N,00726.2686,E,1,04.3,M,48.0,M,0.0,0000*7$GPGGA,175250.253,4657.3418,N,00726.2686,E,1,04,9.0,567.4,M,48.0,M,0.0,0000*74\r\n"
 
281
        ".5,9.0,2.6*39\r\n"
 
282
        "$GPRMC,175250.253,A,4657.3$GPGGA,175251.253,4657.3418,N,00726.2687,E,1,04,9.0,567.5,M,48.0,M,0.0,0000*75\r\n"
 
283
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
284
        ".2687,E,0.14,146.26,230507,,*05\r\n"
 
285
        "$GPGGA,175252.253,4657.,00,18,28,298,00,12,12,218,36,22,09,326,00*7E\r\n"
 
286
        "$GPRMC,175252.253,A,4657.3419,N,04,9.0,567.9,M,48.0,M,0.0,0000*7A\r\n"
 
287
        "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
288
        "$GPRMC,175253.253,A,4657.$GPGGA,175254.253,4657.3420,N,00726.2687,E,1,0417,12,,,,,,,,,9.5,9.0,2.6*39\r\n"
 
289
        "$GPRMC,175254.253,A,4657.3420,N,00726.26$GPGGA,175255.253,4657.3421,N,00726.2688,E,1,04,9.0,568.4,M,48.0,M,0.0,0000*7A\r\n"
 
290
        "$GPGSA,A,,E,0.16,151.85,230507,,*09\r\n"
 
291
};
 
292
#endif
 
293
 
 
294
 
 
295
/*************************************************************************/
 
296
 
 
297
/*
 
298
 * open the specified serial port for read/write
 
299
 * @return port file descriptor or -1
 
300
 */
 
301
 
 
302
#ifndef EMULATE
 
303
static int openPort(const char *tty, int baud)
 
304
{
 
305
    int status;
 
306
    int fd;
 
307
    struct termios newtio;
 
308
 
 
309
    /* open the tty */
 
310
    fd = open(tty, O_RDWR | O_NOCTTY);
 
311
    if (fd < 0) {
 
312
        error("openPort: error open");
 
313
        return fd;
 
314
    }
 
315
 
 
316
    /* flush serial port */
 
317
    status = tcflush(fd, TCIFLUSH);
 
318
    if (status < 0) {
 
319
        error("openPort: error tcflush");
 
320
        close(fd);
 
321
        return -1;
 
322
    }
 
323
 
 
324
    /* get current terminal state */
 
325
    tcgetattr(fd, &newtio);
 
326
 
 
327
    /* set to raw terminal type */
 
328
    newtio.c_cflag = baud | CS8 | CLOCAL | CREAD;
 
329
    newtio.c_iflag = IGNBRK | IGNPAR;
 
330
    newtio.c_oflag = 0;
 
331
 
 
332
    /* control parameters */
 
333
    newtio.c_cc[VMIN] = 1;      /* block for at least one charater */
 
334
 
 
335
    /* set its new attrigutes */
 
336
    status = tcsetattr(fd, TCSANOW, &newtio);
 
337
    if (status < 0) {
 
338
        //error("tcsetattr() failed: %s", strerror(errno));
 
339
        error("tcsetattr() failed: __ERRNO removed due lazy coder");
 
340
        close(fd);
 
341
        fd = -1;
 
342
        return fd;
 
343
    }
 
344
    return fd;
 
345
}
 
346
#endif
 
347
 
 
348
/** called when a gpgga message is received and parsed */
 
349
static void gpgga_callout( __attribute__ ((unused)) nmeap_context_t * context, void *data, __attribute__ ((unused))
 
350
                          void *user_data)
 
351
{
 
352
    nmeap_gga_t *gga = (nmeap_gga_t *) data;
 
353
 
 
354
    altitude = gga->altitude;
 
355
    satellites = gga->satellites;
 
356
    quality = gga->quality;
 
357
    gpsTime = gga->time;
 
358
 
 
359
    if (debug == 1)
 
360
        debug("gps:debug: get gga callout\n");
 
361
 
 
362
}
 
363
 
 
364
 
 
365
//search a buffer for a string (forwards)
 
366
int strLastOcc(char *theBuffer, char searchChar, int size)
 
367
{
 
368
    int i, ret;
 
369
 
 
370
    ret = -1;
 
371
    for (i = size; i >= 0; i--) {
 
372
        if (theBuffer[i] == searchChar) {
 
373
            ret = i;
 
374
            break;
 
375
        }
 
376
    }
 
377
    return ret;
 
378
}
 
379
 
 
380
//search a buffer for a string (backwards)  
 
381
int strFirstOcc(char *theBuffer, char searchChar, int size)
 
382
{
 
383
    int i, ret;
 
384
    ret = -1;
 
385
    for (i = 0; i < size; i++) {
 
386
        if (theBuffer[i] == searchChar) {
 
387
            ret = i;
 
388
            break;
 
389
        }
 
390
    }
 
391
    return ret;
 
392
}
 
393
 
 
394
 
 
395
 
 
396
/** called when a gprmc message is received and parsed */
 
397
static void gprmc_callout( __attribute__ ((unused)) nmeap_context_t * context, void *data, __attribute__ ((unused))
 
398
                          void *user_data)
 
399
{
 
400
    nmeap_rmc_t *rmc = (nmeap_rmc_t *) data;
 
401
 
 
402
    speed = rmc->speed;
 
403
    gpsStatus = rmc->warn;
 
404
    gpsTime = rmc->time;
 
405
    gpsDate = rmc->date;
 
406
 
 
407
    if (debug == 1)
 
408
        debug("gps:debug: get rmc callout\n");
 
409
 
 
410
}
 
411
 
 
412
 
 
413
static int prepare_gps_parser()
 
414
{
 
415
    int status;
 
416
    char *port = "/dev/usb/tts/1";
 
417
    char *test;
 
418
    int speed = 0;              // 0 = default 4800 baud, 1 is 9600 baud
 
419
 
 
420
    if ((test = getenv("GPS_PORT"))) {  /* define your port via env variable */
 
421
        port = test;
 
422
    }
 
423
 
 
424
    if ((test = getenv("GPS_9600"))) {  /* define your port via env variable */
 
425
        speed = 1;
 
426
    }
 
427
 
 
428
    /* --------------------------------------- */
 
429
    /* open the serial port device             */
 
430
    /* using default 4800 baud for most GPS    */
 
431
    /* --------------------------------------- */
 
432
#ifndef EMULATE
 
433
    if (speed == 0)
 
434
        fd_g = openPort(port, B4800);
 
435
    else
 
436
        fd_g = openPort(port, B9600);
 
437
 
 
438
    if (fd_g < 0) {
 
439
        /* open failed */
 
440
        error("GPS PLUGIN, Error: openPort %d", fd_g);
 
441
        return fd_g;
 
442
    }
 
443
#endif
 
444
 
 
445
    /* --------------------------------------- */
 
446
    /*STEP 2 : initialize the nmea context    */
 
447
    /* --------------------------------------- */
 
448
    status = nmeap_init(&nmea, (void *) &user_data);
 
449
    if (status != 0) {
 
450
        error("GPS PLUGIN, Error: nmeap_init %d", status);
 
451
        exit(1);
 
452
    }
 
453
 
 
454
    /* --------------------------------------- */
 
455
    /*STEP 3 : add standard GPGGA parser      */
 
456
    /* -------------------------------------- */
 
457
    status = nmeap_addParser(&nmea, "GPGGA", nmeap_gpgga, gpgga_callout, &gga);
 
458
    if (status != 0) {
 
459
        error("GPS PLUGIN, Error: nmeap_add GPGGA parser, error:%d", status);
 
460
        return -1;
 
461
    }
 
462
 
 
463
    /* --------------------------------------- */
 
464
    /*STEP 4 : add standard GPRMC parser      */
 
465
    /* -------------------------------------- */
 
466
    /* status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); */
 
467
    status = nmeap_addParser(&nmea, "GPRMC", nmeap_gprmc, gprmc_callout, &rmc);
 
468
    if (status != 0) {
 
469
        error("GPS PLUGIN, Error: nmeap_add GPRMC parser, error:%d", status);
 
470
        return -1;
 
471
    }
 
472
 
 
473
    return fd_g;
 
474
}
 
475
 
 
476
 
 
477
 
 
478
static void parse(RESULT * result, RESULT * theOptions, RESULT * displayOptions)
 
479
{
 
480
    int rem;
 
481
    int offset;
 
482
    int status;
 
483
    int len;
 
484
 
 
485
    long options;
 
486
    long dispOptions;
 
487
 
 
488
    char buffer[BUFFER_SIZE];
 
489
    char bufferTmp[BUFFER_SIZE];
 
490
 
 
491
    int validStart, validEnd;
 
492
 
 
493
    options = R2N(theOptions);
 
494
    dispOptions = R2N(displayOptions);
 
495
    //error("options: %x\n",options);
 
496
 
 
497
    if (dispOptions & OPTION_DEBUG)
 
498
        debug = 1;
 
499
 
 
500
    if ((dispOptions & OPTION_GET_BUFFERDATA) == 0) {
 
501
 
 
502
        /* ---------------------------------------- */
 
503
        /* STEP 6 : get a buffer of input          */
 
504
        /* --------------------------------------- */
 
505
 
 
506
        memset(buffer, 0, BUFFER_SIZE);
 
507
        if (fndStr > BUFFER_SIZE)
 
508
            fndStr = 0;
 
509
        //copy unfinished nmea strings back
 
510
        if (fndStr > 0) {
 
511
            memcpy(buffer, backBuffer, fndStr);
 
512
        }
 
513
#ifdef EMULATE
 
514
        memcpy(&buffer[fndStr], &test_vector[emu_read_ofs], BUFFER_SIZE - fndStr);
 
515
 
 
516
        len = rem = EMU_BUFFER_READ_SIZE;
 
517
        emu_read_ofs += EMU_BUFFER_READ_SIZE;
 
518
        if (emu_read_ofs > (sizeof(test_vector) - BUFFER_SIZE)) {
 
519
            emu_read_ofs = 0;
 
520
            memset(buffer, 0, BUFFER_SIZE);
 
521
        }
 
522
#else
 
523
 
 
524
        len = rem = read(fd_g, buffer, BUFFER_SIZE - fndStr);
 
525
        if (len <= 0) {
 
526
            error("GPS Plugin, Error read from port, try using the GPS_PORT env variable (export GPS_PORT=/dev/mydev)");
 
527
            //break;
 
528
        }
 
529
        if (debug == 1)
 
530
            debug("gps:debug: read %d bytes\n", len);
 
531
 
 
532
#endif
 
533
        if (dispOptions & OPTION_RAW_NMEA)
 
534
            printf("\n__[%s]", buffer + '\0');
 
535
 
 
536
        /* ---------------------------------------------- */
 
537
        /* STEP 7 : process input until buffer is used up */
 
538
        /* ---------------------------------------------- */
 
539
        validStart = strFirstOcc(buffer, '$', len);
 
540
        validEnd = strLastOcc(buffer, '\n', len);
 
541
 
 
542
        if (validStart >= 0 && validEnd > 0 && validStart < validEnd) {
 
543
            //valid string found
 
544
            memcpy(bufferTmp, buffer, sizeof(buffer));  //save buffer
 
545
            memset(backBuffer, 0, sizeof(backBuffer));  //clear backup buffer
 
546
            memcpy(backBuffer, buffer + validEnd, len - validEnd);      // save incomplete nmea string
 
547
            memset(buffer, 0, sizeof(buffer));  //clean buffer
 
548
            memcpy(buffer, bufferTmp + validStart, validEnd - validStart + 1);  //copy valid name string
 
549
            fndStr = len - validEnd + validStart;       //save the size of the buffer
 
550
        } else {
 
551
            //no valid nmea string found
 
552
            fndStr = 0;
 
553
            memset(buffer, 0, sizeof(buffer));
 
554
            memset(backBuffer, 0, sizeof(backBuffer));
 
555
        }
 
556
 
 
557
        offset = 0;
 
558
        if (debug == 1)
 
559
            debug("backBuffer: %s\n", backBuffer);
 
560
 
 
561
        //the nmeap_parseBuffer function needs whole nmea strings, combined string will NOT work!
 
562
        validStart = strFirstOcc(buffer, '$', len);
 
563
        validEnd = strFirstOcc(buffer, '\n', len);
 
564
        while (validStart >= 0 && validEnd > 0 && validStart < validEnd) {
 
565
            memset(bufferTmp, 0, sizeof(bufferTmp));    //empty temp buffer
 
566
            memcpy(bufferTmp, buffer + offset + validStart, validEnd - validStart + 1); //fill temp buffer
 
567
 
 
568
            if (debug == 1)
 
569
                debug("submit: %s\n", bufferTmp);
 
570
 
 
571
            rem = len - offset;
 
572
            status = nmeap_parseBuffer(&nmea, (const char *) &bufferTmp, &rem); //parse it
 
573
            if (status == -1) {
 
574
                errCounter++;
 
575
                error("parser error occurred! (cnt: %i)\n", errCounter);
 
576
            } else if (status == 0) {
 
577
                incomplCounter++;
 
578
            } else if (status > 0)
 
579
                msgCounter++;
 
580
 
 
581
            offset += validEnd - validStart + 1;        //update offset
 
582
            validStart = strFirstOcc(buffer + offset, '$', len - offset);       //find next sentence
 
583
            validEnd = strFirstOcc(buffer + offset, '\n', len - offset);
 
584
        }
 
585
 
 
586
 
 
587
/*      while (rem > 0) {
 
588
            status = nmeap_parseBuffer(&nmea, &buffer[offset], &rem);
 
589
            debug("\nGPS::debug: remaining: %d bytes\n",rem);
 
590
            offset += (len - rem);
 
591
        }*/
 
592
    }                           // end of OPTION get bufferdata
 
593
 
 
594
 
 
595
    /* --------------------------------------- */
 
596
    /* DISPLAY stuff comes here...             */
 
597
    /* --------------------------------------- */
 
598
    char *value = " ";
 
599
    char outputStr[80];
 
600
    memset(outputStr, 0, 80);
 
601
 
 
602
    if (options & SHOW_ALTITUDE) {
 
603
        if (dispOptions & OPTION_NO_PREFIX)
 
604
            sprintf(outputStr, "%s%.0f ", outputStr, altitude);
 
605
        else
 
606
            sprintf(outputStr, "%salt:%.0f ", outputStr, altitude);
 
607
    }
 
608
    if (options & SHOW_SPEED) {
 
609
        float knotsConvert = 1.852f;    //default speed display=km/h
 
610
        if (dispOptions & OPTION_SPEED_IN_KNOTS)
 
611
            knotsConvert = 1.0f;        //use knots
 
612
 
 
613
        if (dispOptions & OPTION_NO_PREFIX)
 
614
            sprintf(outputStr, "%s%.0f ", outputStr, speed * knotsConvert);
 
615
        else
 
616
            sprintf(outputStr, "%sspd:%.0f ", outputStr, speed * knotsConvert);
 
617
    }
 
618
    if (options & SHOW_COURSE) {
 
619
        char courses[8][3] = { "N ", "NO", "O ", "SO", "S ", "SW", "W ", "NW" };
 
620
        float degrees[8] = { 22.5f, 67.5f, 112.5f, 157.5f, 202.5f, 247.5f, 292.5f, 337.5f };
 
621
        int selectedDegree = 0;
 
622
        int n;
 
623
 
 
624
        for (n = 0; n < 8; n++) {
 
625
            if (course < degrees[n]) {
 
626
                selectedDegree = n;
 
627
                break;
 
628
            }
 
629
        }
 
630
        if (dispOptions & OPTION_NO_PREFIX)
 
631
            sprintf(outputStr, "%s%s ", outputStr, courses[selectedDegree]);
 
632
        else
 
633
            sprintf(outputStr, "%sdir:%s ", outputStr, courses[selectedDegree]);
 
634
    }
 
635
    if (options & SHOW_SATELLITES) {
 
636
        if (dispOptions & OPTION_NO_PREFIX)
 
637
            sprintf(outputStr, "%s%d ", outputStr, satellites);
 
638
        else
 
639
            sprintf(outputStr, "%ssat:%d ", outputStr, satellites);
 
640
    }
 
641
    if (options & SHOW_QUALITY) {
 
642
        if (dispOptions & OPTION_NO_PREFIX)
 
643
            sprintf(outputStr, "%s%d ", outputStr, quality);
 
644
        else
 
645
            sprintf(outputStr, "%squa:%d ", outputStr, quality);
 
646
    }
 
647
    if (options & SHOW_STATUS) {
 
648
        if (dispOptions & OPTION_NO_PREFIX)
 
649
            sprintf(outputStr, "%s%c ", outputStr, gpsStatus);
 
650
        else
 
651
            sprintf(outputStr, "%ssta:%c ", outputStr, gpsStatus);
 
652
    }
 
653
    if (options & SHOW_TIME_UTC) {
 
654
        char digitizer[9];      //01:34:67
 
655
        sprintf(digitizer, "%.6ld", gpsTime);   //<012345>
 
656
        digitizer[7] = digitizer[5];
 
657
        digitizer[6] = digitizer[4];
 
658
        digitizer[4] = digitizer[3];
 
659
        digitizer[3] = digitizer[2];
 
660
        digitizer[2] = ':';
 
661
        digitizer[5] = ':';
 
662
        digitizer[8] = '\0';
 
663
        if (dispOptions & OPTION_NO_PREFIX)
 
664
            sprintf(outputStr, "%s%s ", outputStr, digitizer);
 
665
        else
 
666
            sprintf(outputStr, "%sutc:%s ", outputStr, digitizer);
 
667
    }
 
668
    if (options & SHOW_DATE) {
 
669
        char digitizer[9];      //01:34:67
 
670
        sprintf(digitizer, "%.6ld", gpsDate);   //<012345>
 
671
        digitizer[7] = digitizer[5];
 
672
        digitizer[6] = digitizer[4];
 
673
        digitizer[4] = digitizer[3];
 
674
        digitizer[3] = digitizer[2];
 
675
        digitizer[2] = '/';
 
676
        digitizer[5] = '/';
 
677
        digitizer[8] = '\0';
 
678
        if (dispOptions & OPTION_NO_PREFIX)
 
679
            sprintf(outputStr, "%s%s ", outputStr, digitizer);
 
680
        else
 
681
            sprintf(outputStr, "%sdat:%s ", outputStr, digitizer);
 
682
    }
 
683
 
 
684
 
 
685
    if (dispOptions & SHOW_NMEA_STATUS) {
 
686
        if (dispOptions & OPTION_NO_PREFIX)
 
687
            sprintf(outputStr, "%s%04d/%04d/%04d ", outputStr, msgCounter, errCounter, incomplCounter);
 
688
        else
 
689
            sprintf(outputStr, "%sOK:%03d/Er:%03d/In:%03d ", outputStr, msgCounter, errCounter, incomplCounter);
 
690
    }
 
691
 
 
692
    if (options == 0 && dispOptions == 0) {     //error, no parameter defined!
 
693
        error("gps::parse() ERROR, no parameter specified!");
 
694
        value = strdup("GPS ARG ERR");
 
695
    } else {
 
696
        value = strdup(outputStr);
 
697
    }
 
698
 
 
699
    SetResult(&result, R_STRING, value);
 
700
    free(value);
 
701
}
 
702
 
 
703
 
 
704
/* plugin initialization */
 
705
/* MUST NOT be declared 'static'! */
 
706
int plugin_init_gps(void)
 
707
{
 
708
    info("%s: v%s", Name, "0.2");
 
709
    prepare_gps_parser();
 
710
 
 
711
    /* register all our cool functions */
 
712
    /* the second parameter is the number of arguments */
 
713
    /* -1 stands for variable argument list */
 
714
    AddFunction("gps::parse", 2, parse);
 
715
 
 
716
    return 0;
 
717
}
 
718
 
 
719
void plugin_exit_gps(void)
 
720
{
 
721
    info("%s: shutting down plugin.", Name);
 
722
#ifndef EMULATE
 
723
    close(fd_g);
 
724
#endif
 
725
}