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