]> git.mxchange.org Git - flightgear.git/blob - src/Network/garmin.cxx
Expose more runway methods to Nasal
[flightgear.git] / src / Network / garmin.cxx
1 // garmin.cxx -- Garmin 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 <iostream>
28 #include <cstdlib>
29 #include <cstring>
30 #include <cstdio>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/timing/sg_time.hxx>
36
37 #include <FDM/flightProperties.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/globals.hxx>
40
41 #include "garmin.hxx"
42
43 using std::string;
44
45 FGGarmin::FGGarmin() {
46   fdm = new FlightProperties;
47 }
48
49 FGGarmin::~FGGarmin() {
50   delete fdm;
51 }
52
53
54 // calculate the garmin check sum
55 static char calc_nmea_cksum(char *sentence) {
56     unsigned char sum = 0;
57     int i, len;
58
59     // cout << sentence << endl;
60
61     len = strlen(sentence);
62     sum = sentence[0];
63     for ( i = 1; i < len; i++ ) {
64         // cout << sentence[i];
65         sum ^= sentence[i];
66     }
67     // cout << endl;
68
69     // printf("sum = %02x\n", sum);
70     return sum;
71 }
72
73
74 // generate Garmin message
75 bool FGGarmin::gen_message() {
76     // cout << "generating garmin message" << endl;
77
78     char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
79     char dir;
80     int deg;
81     double min;
82
83     SGTime *t = globals->get_time_params();
84
85     char utc[10];
86     sprintf( utc, "%02d%02d%02d", 
87              t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
88
89     char rmc_lat[20];
90     double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
91     if ( latd < 0.0 ) {
92         latd = -latd;
93         dir = 'S';
94     } else {
95         dir = 'N';
96     }
97     deg = (int)(latd);
98     min = (latd - (double)deg) * 60.0;
99     sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
100
101     char rmc_lon[20];
102     double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
103     if ( lond < 0.0 ) {
104         lond = -lond;
105         dir = 'W';
106     } else {
107         dir = 'E';
108     }
109     deg = (int)(lond);
110     min = (lond - (double)deg) * 60.0;
111     sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
112
113     char speed[10];
114     sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
115
116     char heading[10];
117     sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
118
119     char altitude_m[10];
120     sprintf( altitude_m, "%02d", 
121              (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
122
123     char date[10];
124     int year = t->getGmt()->tm_year;
125     while ( year >= 100 ) { year -= 100; }
126     sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday, 
127              t->getGmt()->tm_mon+1, year );
128
129     char magvar[10];
130     float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
131     if ( magdeg < 0.0 ) {
132         magdeg = -magdeg;
133         dir = 'W';
134     } else {
135         dir = 'E';
136     }
137     sprintf( magvar, "%05.1f,%c", magdeg, dir );
138
139     // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
140     sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
141              utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
142     sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
143
144     // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
145     //          utc, lat, lon, altitude_m );
146
147     sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
148     sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
149
150     sprintf( gsa, "%s",
151              "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
152
153     SG_LOG( SG_IO, SG_DEBUG, rmc );
154     SG_LOG( SG_IO, SG_DEBUG, rmz );
155     SG_LOG( SG_IO, SG_DEBUG, gsa );
156
157     string garmin_sentence;
158
159     // RMC sentence
160     garmin_sentence = "$";
161     garmin_sentence += rmc;
162     garmin_sentence += "*";
163     garmin_sentence += rmc_sum;
164     garmin_sentence += "\r\n";
165
166     // RMZ sentence (garmin proprietary)
167     garmin_sentence += "$";
168     garmin_sentence += rmz;
169     garmin_sentence += "*";
170     garmin_sentence += rmz_sum;
171     garmin_sentence += "\r\n";
172
173     // GSA sentence (totally faked)
174     garmin_sentence += gsa;
175     garmin_sentence += "\r\n";
176
177     std::cout << garmin_sentence;
178
179     length = garmin_sentence.length();
180     strncpy( buf, garmin_sentence.c_str(), length );
181
182     return true;
183 }
184
185
186 // parse Garmin message
187 bool FGGarmin::parse_message() {
188     SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
189
190     string msg = buf;
191     msg = msg.substr( 0, length );
192     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
193
194     string::size_type begin_line, end_line, begin, end;
195     begin_line = begin = 0;
196
197     // extract out each line
198     end_line = msg.find("\n", begin_line);
199     while ( end_line != string::npos ) {
200         string line = msg.substr(begin_line, end_line - begin_line);
201         begin_line = end_line + 1;
202         SG_LOG( SG_IO, SG_INFO, "  input line = " << line );
203
204         // leading character
205         string start = msg.substr(begin, 1);
206         ++begin;
207         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
208
209         // sentence
210         end = msg.find(",", begin);
211         if ( end == string::npos ) {
212             return false;
213         }
214     
215         string sentence = msg.substr(begin, end - begin);
216         begin = end + 1;
217         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
218
219         double lon_deg, lon_min, lat_deg, lat_min;
220         double lon, lat, speed, heading, altitude;
221
222         if ( sentence == "GPRMC" ) {
223             // time
224             end = msg.find(",", begin);
225             if ( end == string::npos ) {
226                 return false;
227             }
228     
229             string utc = msg.substr(begin, end - begin);
230             begin = end + 1;
231             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
232
233             // junk
234             end = msg.find(",", begin);
235             if ( end == string::npos ) {
236                 return false;
237             }
238     
239             string junk = msg.substr(begin, end - begin);
240             begin = end + 1;
241             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
242
243             // lat val
244             end = msg.find(",", begin);
245             if ( end == string::npos ) {
246                 return false;
247             }
248     
249             string lat_str = msg.substr(begin, end - begin);
250             begin = end + 1;
251
252             lat_deg = atof( lat_str.substr(0, 2).c_str() );
253             lat_min = atof( lat_str.substr(2).c_str() );
254
255             // lat dir
256             end = msg.find(",", begin);
257             if ( end == string::npos ) {
258                 return false;
259             }
260     
261             string lat_dir = msg.substr(begin, end - begin);
262             begin = end + 1;
263
264             lat = lat_deg + ( lat_min / 60.0 );
265             if ( lat_dir == "S" ) {
266                 lat *= -1;
267             }
268
269             fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
270             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
271
272             // lon val
273             end = msg.find(",", begin);
274             if ( end == string::npos ) {
275                 return false;
276             }
277     
278             string lon_str = msg.substr(begin, end - begin);
279             begin = end + 1;
280
281             lon_deg = atof( lon_str.substr(0, 3).c_str() );
282             lon_min = atof( lon_str.substr(3).c_str() );
283
284             // lon dir
285             end = msg.find(",", begin);
286             if ( end == string::npos ) {
287                 return false;
288             }
289     
290             string lon_dir = msg.substr(begin, end - begin);
291             begin = end + 1;
292
293             lon = lon_deg + ( lon_min / 60.0 );
294             if ( lon_dir == "W" ) {
295                 lon *= -1;
296             }
297
298             fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
299             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
300
301 #if 0
302             double sl_radius, lat_geoc;
303             sgGeodToGeoc( fdm->get_Latitude(), 
304                           fdm->get_Altitude(), 
305                           &sl_radius, &lat_geoc );
306             fdm->set_Geocentric_Position( lat_geoc, 
307                            fdm->get_Longitude(), 
308                            sl_radius + fdm->get_Altitude() );
309 #endif
310
311             // speed
312             end = msg.find(",", begin);
313             if ( end == string::npos ) {
314                 return false;
315             }
316     
317             string speed_str = msg.substr(begin, end - begin);
318             begin = end + 1;
319             speed = atof( speed_str.c_str() );
320             fdm->set_V_calibrated_kts( speed );
321             // fdm->set_V_ground_speed( speed );
322             SG_LOG( SG_IO, SG_INFO, "  speed = " << speed );
323
324             // heading
325             end = msg.find(",", begin);
326             if ( end == string::npos ) {
327                 return false;
328             }
329     
330             string hdg_str = msg.substr(begin, end - begin);
331             begin = end + 1;
332             heading = atof( hdg_str.c_str() );
333             fdm->set_Euler_Angles( fdm->get_Phi(), 
334                                              fdm->get_Theta(), 
335                                              heading * SGD_DEGREES_TO_RADIANS );
336             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
337         } else if ( sentence == "PGRMZ" ) {
338             // altitude
339             end = msg.find(",", begin);
340             if ( end == string::npos ) {
341                 return false;
342             }
343     
344             string alt_str = msg.substr(begin, end - begin);
345             altitude = atof( alt_str.c_str() );
346             begin = end + 1;
347
348             // altitude units
349             end = msg.find(",", begin);
350             if ( end == string::npos ) {
351                 return false;
352             }
353     
354             string alt_units = msg.substr(begin, end - begin);
355             begin = end + 1;
356
357             if ( alt_units != "F" && alt_units != "f" ) {
358                 altitude *= SG_METER_TO_FEET;
359             }
360
361             fdm->set_Altitude( altitude );
362     
363             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
364
365         }
366
367         // printf("%.8f %.8f\n", lon, lat);
368
369         begin = begin_line;
370         end_line = msg.find("\n", begin_line);
371     }
372
373     return true;
374 }
375
376
377 // open hailing frequencies
378 bool FGGarmin::open() {
379     if ( is_enabled() ) {
380         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
381                 << "is already in use, ignoring" );
382         return false;
383     }
384
385     SGIOChannel *io = get_io_channel();
386
387     if ( ! io->open( get_direction() ) ) {
388         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
389         return false;
390     }
391
392     set_enabled( true );
393
394     return true;
395 }
396
397
398 // process work for this port
399 bool FGGarmin::process() {
400     SGIOChannel *io = get_io_channel();
401
402     if ( get_direction() == SG_IO_OUT ) {
403         gen_message();
404         if ( ! io->write( buf, length ) ) {
405             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
406             return false;
407         }
408     } else if ( get_direction() == SG_IO_IN ) {
409         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
410             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
411             if ( parse_message() ) {
412                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
413             } else {
414                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
415             }
416         } else {
417             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
418             return false;
419         }
420         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
421             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
422             if ( parse_message() ) {
423                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
424             } else {
425                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
426             }
427         } else {
428             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
429             return false;
430         }
431     }
432
433     return true;
434 }
435
436
437 // close the channel
438 bool FGGarmin::close() {
439     SGIOChannel *io = get_io_channel();
440
441     set_enabled( false );
442
443     if ( ! io->close() ) {
444         return false;
445     }
446
447     return true;
448 }