]> git.mxchange.org Git - flightgear.git/blob - src/Network/nmea.cxx
#903: generic protocol: fix a rare corner case
[flightgear.git] / src / Network / nmea.cxx
1 // nmea.cxx -- NMEA protocal class
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <cstdlib>
28 #include <cstring>
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/io/iochannel.hxx>
33 #include <simgear/timing/sg_time.hxx>
34
35 #include <FDM/flightProperties.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38
39 #include "nmea.hxx"
40
41 FGNMEA::FGNMEA() {
42   fdm = new FlightProperties();
43 }
44
45 FGNMEA::~FGNMEA() {
46   delete fdm;
47 }
48
49
50 // calculate the nmea check sum
51 static char calc_nmea_cksum(char *sentence) {
52     unsigned char sum = 0;
53     int i, len;
54
55     // cout << sentence << endl;
56
57     len = strlen(sentence);
58     sum = sentence[0];
59     for ( i = 1; i < len; i++ ) {
60         // cout << sentence[i];
61         sum ^= sentence[i];
62     }
63     // cout << endl;
64
65     // printf("sum = %02x\n", sum);
66     return sum;
67 }
68
69
70 // generate NMEA message
71 bool FGNMEA::gen_message() {
72     // cout << "generating nmea message" << endl;
73
74     char rmc[256], gga[256], gsa[256];
75     char rmc_sum[10], gga_sum[10];
76     char dir;
77     int deg;
78     double min;
79
80     SGTime *t = globals->get_time_params();
81
82     char utc[10];
83     sprintf( utc, "%02d%02d%02d", 
84              t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
85
86     char gga_lat[20], rmc_lat[20];
87     double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
88     if ( latd < 0.0 ) {
89         latd = -latd;
90         dir = 'S';
91     } else {
92         dir = 'N';
93     }
94     deg = (int)(latd);
95     min = (latd - (double)deg) * 60.0;
96     sprintf( gga_lat, "%02d%07.4f,%c", abs(deg), min, dir);
97     sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
98
99     char gga_lon[20], rmc_lon[20];
100     double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
101     if ( lond < 0.0 ) {
102         lond = -lond;
103         dir = 'W';
104     } else {
105         dir = 'E';
106     }
107     deg = (int)(lond);
108     min = (lond - (double)deg) * 60.0;
109     sprintf( gga_lon, "%03d%07.4f,%c", abs(deg), min, dir);
110     sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
111
112     double vn = fgGetDouble( "/velocities/speed-north-fps" );
113     double ve = fgGetDouble( "/velocities/speed-east-fps" );
114     double fps = sqrt( vn*vn + ve*ve );
115     double mps = fps * SG_FEET_TO_METER;
116     double kts = mps * SG_METER_TO_NM * 3600;
117     char speed[10];
118     sprintf( speed, "%.1f", kts );
119
120     double hdg_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
121     if ( hdg_true < 0 ) {
122       hdg_true += 360.0;
123     }
124     char heading[10];
125     sprintf( heading, "%.1f", hdg_true );
126
127     char altitude_m[10];
128     sprintf( altitude_m, "%.1f", 
129              fdm->get_Altitude() * SG_FEET_TO_METER );
130
131     char date[10];
132     int year = t->getGmt()->tm_year;
133     while ( year >= 100 ) { year -= 100; }
134     sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday, 
135              t->getGmt()->tm_mon+1, year );
136
137     char magvar[10];
138     float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
139     if ( magdeg < 0.0 ) {
140         magdeg = -magdeg;
141         dir = 'W';
142     } else {
143         dir = 'E';
144     }
145     sprintf( magvar, "%.1f,%c", magdeg, dir );
146  
147     // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E,A*XX
148     sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s,A",
149              utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
150     sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
151
152     // $GPGGA,HHMMSS,DDMM.MMMM,N,DDDMM.MMMM,W,1,NN,H.H,AAAA.A,M,GG.G,M,,*XX
153     sprintf( gga, "GPGGA,%s,%s,%s,1,08,0.9,%s,M,0.0,M,,",
154              utc, gga_lat, gga_lon, altitude_m );
155     sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
156     sprintf( gsa, "%s",
157              "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
158
159     SG_LOG( SG_IO, SG_DEBUG, rmc );
160     SG_LOG( SG_IO, SG_DEBUG, gga );
161     SG_LOG( SG_IO, SG_DEBUG, gsa );
162
163     string nmea_sentence;
164
165     // RMC sentence
166     nmea_sentence = "$";
167     nmea_sentence += rmc;
168     nmea_sentence += "*";
169     nmea_sentence += rmc_sum;
170     nmea_sentence += "\n";
171
172     // GGA sentence
173     nmea_sentence += "$";
174     nmea_sentence += gga;
175     nmea_sentence += "*";
176     nmea_sentence += gga_sum;
177     nmea_sentence += "\n";
178
179     // GSA sentence (totally faked)
180     nmea_sentence += gsa;
181     nmea_sentence += "\n";
182
183     // cout << nmea_sentence;
184
185     length = nmea_sentence.length();
186     strncpy( buf, nmea_sentence.c_str(), length );
187
188     return true;
189 }
190
191
192 // parse NMEA message.  messages will look something like the
193 // following:
194 //
195 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
196 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
197
198 bool FGNMEA::parse_message() {
199     SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
200
201     string msg = buf;
202     msg = msg.substr( 0, length );
203     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
204
205     string::size_type begin_line, end_line, begin, end;
206     begin_line = begin = 0;
207
208     // extract out each line
209     end_line = msg.find("\n", begin_line);
210     while ( end_line != string::npos ) {
211         string line = msg.substr(begin_line, end_line - begin_line);
212         begin_line = end_line + 1;
213         SG_LOG( SG_IO, SG_INFO, "  input line = " << line );
214
215         // leading character
216         string start = msg.substr(begin, 1);
217         ++begin;
218         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
219
220         // sentence
221         end = msg.find(",", begin);
222         if ( end == string::npos ) {
223             return false;
224         }
225     
226         string sentence = msg.substr(begin, end - begin);
227         begin = end + 1;
228         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
229
230         double lon_deg, lon_min, lat_deg, lat_min;
231         double lon, lat, speed, heading, altitude;
232
233         if ( sentence == "GPRMC" ) {
234             // time
235             end = msg.find(",", begin);
236             if ( end == string::npos ) {
237                 return false;
238             }
239     
240             string utc = msg.substr(begin, end - begin);
241             begin = end + 1;
242             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
243
244             // junk
245             end = msg.find(",", begin);
246             if ( end == string::npos ) {
247                 return false;
248             }
249     
250             string junk = msg.substr(begin, end - begin);
251             begin = end + 1;
252             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
253
254             // lat val
255             end = msg.find(",", begin);
256             if ( end == string::npos ) {
257                 return false;
258             }
259     
260             string lat_str = msg.substr(begin, end - begin);
261             begin = end + 1;
262
263             lat_deg = atof( lat_str.substr(0, 2).c_str() );
264             lat_min = atof( lat_str.substr(2).c_str() );
265
266             // lat dir
267             end = msg.find(",", begin);
268             if ( end == string::npos ) {
269                 return false;
270             }
271     
272             string lat_dir = msg.substr(begin, end - begin);
273             begin = end + 1;
274
275             lat = lat_deg + ( lat_min / 60.0 );
276             if ( lat_dir == "S" ) {
277                 lat *= -1;
278             }
279
280             fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
281             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
282
283             // lon val
284             end = msg.find(",", begin);
285             if ( end == string::npos ) {
286                 return false;
287             }
288     
289             string lon_str = msg.substr(begin, end - begin);
290             begin = end + 1;
291
292             lon_deg = atof( lon_str.substr(0, 3).c_str() );
293             lon_min = atof( lon_str.substr(3).c_str() );
294
295             // lon dir
296             end = msg.find(",", begin);
297             if ( end == string::npos ) {
298                 return false;
299             }
300     
301             string lon_dir = msg.substr(begin, end - begin);
302             begin = end + 1;
303
304             lon = lon_deg + ( lon_min / 60.0 );
305             if ( lon_dir == "W" ) {
306                 lon *= -1;
307             }
308
309             fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
310             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
311
312 #if 0
313             double sl_radius, lat_geoc;
314             sgGeodToGeoc( fdm->get_Latitude(), 
315                           fdm->get_Altitude(), 
316                           &sl_radius, &lat_geoc );
317             fdm->set_Geocentric_Position( lat_geoc, 
318                            fdm->get_Longitude(), 
319                            sl_radius + fdm->get_Altitude() );
320 #endif
321
322             // speed
323             end = msg.find(",", begin);
324             if ( end == string::npos ) {
325                 return false;
326             }
327     
328             string speed_str = msg.substr(begin, end - begin);
329             begin = end + 1;
330             speed = atof( speed_str.c_str() );
331             fdm->set_V_calibrated_kts( speed );
332             // fdm->set_V_ground_speed( speed );
333             SG_LOG( SG_IO, SG_INFO, "  speed = " << speed );
334
335             // heading
336             end = msg.find(",", begin);
337             if ( end == string::npos ) {
338                 return false;
339             }
340     
341             string hdg_str = msg.substr(begin, end - begin);
342             begin = end + 1;
343             heading = atof( hdg_str.c_str() );
344             fdm->set_Euler_Angles( fdm->get_Phi(), 
345                                              fdm->get_Theta(), 
346                                              heading * SGD_DEGREES_TO_RADIANS );
347             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
348         } else if ( sentence == "GPGGA" ) {
349             // time
350             end = msg.find(",", begin);
351             if ( end == string::npos ) {
352                 return false;
353             }
354     
355             string utc = msg.substr(begin, end - begin);
356             begin = end + 1;
357             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
358
359             // lat val
360             end = msg.find(",", begin);
361             if ( end == string::npos ) {
362                 return false;
363             }
364     
365             string lat_str = msg.substr(begin, end - begin);
366             begin = end + 1;
367
368             lat_deg = atof( lat_str.substr(0, 2).c_str() );
369             lat_min = atof( lat_str.substr(2).c_str() );
370
371             // lat dir
372             end = msg.find(",", begin);
373             if ( end == string::npos ) {
374                 return false;
375             }
376     
377             string lat_dir = msg.substr(begin, end - begin);
378             begin = end + 1;
379
380             lat = lat_deg + ( lat_min / 60.0 );
381             if ( lat_dir == "S" ) {
382                 lat *= -1;
383             }
384
385             // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
386             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
387
388             // lon val
389             end = msg.find(",", begin);
390             if ( end == string::npos ) {
391                 return false;
392             }
393     
394             string lon_str = msg.substr(begin, end - begin);
395             begin = end + 1;
396
397             lon_deg = atof( lon_str.substr(0, 3).c_str() );
398             lon_min = atof( lon_str.substr(3).c_str() );
399
400             // lon dir
401             end = msg.find(",", begin);
402             if ( end == string::npos ) {
403                 return false;
404             }
405     
406             string lon_dir = msg.substr(begin, end - begin);
407             begin = end + 1;
408
409             lon = lon_deg + ( lon_min / 60.0 );
410             if ( lon_dir == "W" ) {
411                 lon *= -1;
412             }
413
414             // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
415             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
416
417             // junk
418             end = msg.find(",", begin);
419             if ( end == string::npos ) {
420                 return false;
421             }
422     
423             string junk = msg.substr(begin, end - begin);
424             begin = end + 1;
425             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
426
427             // junk
428             end = msg.find(",", begin);
429             if ( end == string::npos ) {
430                 return false;
431             }
432     
433             junk = msg.substr(begin, end - begin);
434             begin = end + 1;
435             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
436
437             // junk
438             end = msg.find(",", begin);
439             if ( end == string::npos ) {
440                 return false;
441             }
442     
443             junk = msg.substr(begin, end - begin);
444             begin = end + 1;
445             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
446
447             // altitude
448             end = msg.find(",", begin);
449             if ( end == string::npos ) {
450                 return false;
451             }
452     
453             string alt_str = msg.substr(begin, end - begin);
454             altitude = atof( alt_str.c_str() );
455             begin = end + 1;
456
457             // altitude units
458             end = msg.find(",", begin);
459             if ( end == string::npos ) {
460                 return false;
461             }
462     
463             string alt_units = msg.substr(begin, end - begin);
464             begin = end + 1;
465
466             if ( alt_units != "F" ) {
467                 altitude *= SG_METER_TO_FEET;
468             }
469
470             fdm->set_Altitude( altitude );
471     
472             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
473
474         }
475
476         // printf("%.8f %.8f\n", lon, lat);
477
478         begin = begin_line;
479         end_line = msg.find("\n", begin_line);
480     }
481
482     return true;
483 }
484
485
486 // open hailing frequencies
487 bool FGNMEA::open() {
488     if ( is_enabled() ) {
489         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
490                 << "is already in use, ignoring" );
491         return false;
492     }
493
494     SGIOChannel *io = get_io_channel();
495
496     if ( ! io->open( get_direction() ) ) {
497         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
498         return false;
499     }
500
501     set_enabled( true );
502
503     return true;
504 }
505
506
507 // process work for this port
508 bool FGNMEA::process() {
509     SGIOChannel *io = get_io_channel();
510
511     if ( get_direction() == SG_IO_OUT ) {
512         gen_message();
513         if ( ! io->write( buf, length ) ) {
514             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
515             return false;
516         }
517     } else if ( get_direction() == SG_IO_IN ) {
518         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
519             parse_message();
520         } else {
521             SG_LOG( SG_IO, SG_WARN, "Error reading data." );
522             return false;
523         }
524         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
525             parse_message();
526         } else {
527             SG_LOG( SG_IO, SG_WARN, "Error reading data." );
528             return false;
529         }
530     }
531
532     return true;
533 }
534
535
536 // close the channel
537 bool FGNMEA::close() {
538     SGIOChannel *io = get_io_channel();
539
540     set_enabled( false );
541
542     if ( ! io->close() ) {
543         return false;
544     }
545
546     return true;
547 }