1 // opengc.cxx - Network interface program to send sim data onto a LAN
3 // Created by: J. Wojnaroski -- castle@mminternet.com
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.
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.
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.
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/io/iochannel.hxx>
28 #include <FDM/flight.hxx>
29 #include <Main/globals.hxx>
30 #include <Cockpit/radiostack.hxx>
34 FGOpenGC::FGOpenGC() {
37 FGOpenGC::~FGOpenGC() {
40 // open hailing frequencies
41 bool FGOpenGC::open() {
43 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
44 << "is already in use, ignoring" );
48 SGIOChannel *io = get_io_channel();
50 if ( ! io->open( get_direction() ) ) {
51 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
61 static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
62 //static void collect_data( ogcFGData *data ) {
64 FGEngInterface *p_engine[4]; // four is enough unless you're a BUF
66 p_engine[0] = cur_fdm_state->get_engine(0);
67 p_engine[1] = cur_fdm_state->get_engine(1);
69 data->version_id = 0x0011;
71 data->latitude = fdm->get_Longitude_deg();
72 data->longitude = cur_fdm_state->get_Latitude_deg();
74 data->pitch = cur_fdm_state->get_Theta_deg();
75 data->bank = cur_fdm_state->get_Phi_deg();
76 data->heading = cur_fdm_state->get_Psi_deg();
77 data->altitude = cur_fdm_state->get_Altitude();
78 data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
79 data->vvi = cur_fdm_state->get_Climb_Rate();
81 data->magvar = globals->get_mag()->get_magvar();
83 data->rpm[0] = p_engine[0]->get_RPM();
84 data->rpm[1] = p_engine[1]->get_RPM();
86 data->epr[0] = p_engine[0]->get_Manifold_Pressure();
87 data->epr[1] = p_engine[1]->get_Manifold_Pressure();
89 data->egt[0] = p_engine[0]->get_EGT();
90 data->egt[1] = p_engine[1]->get_EGT();
92 data->oil_pressure[0] = p_engine[0]->get_Oil_Pressure();
93 data->oil_pressure[1] = p_engine[1]->get_Oil_Pressure();
97 // Once OPenGC develops a comparable navaids database some of this will not be required
99 //data->nav1_ident = current_radiostack->get_nav1_ident();
100 data->nav1_freq = current_radiostack->get_nav1_freq();
101 data->nav1_radial = current_radiostack->get_nav1_sel_radial();
102 data->nav1_course_dev = current_radiostack->get_nav1_heading_needle_deflection();
104 //data->nav1_ident = current_radiostack->get_nav1_ident();
105 data->nav2_freq = current_radiostack->get_nav2_freq();
106 data->nav2_radial = current_radiostack->get_nav2_sel_radial();
107 data->nav2_course_dev = current_radiostack->get_nav2_heading_needle_deflection();
111 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
112 // just a place holder until the CDU is developed
116 // process work for this port
117 bool FGOpenGC::process() {
118 SGIOChannel *io = get_io_channel();
119 int length = sizeof(buf);
121 if ( get_direction() == SG_IO_OUT ) {
122 collect_data( cur_fdm_state, &buf );
123 //collect_data( &buf );
124 if ( ! io->write( (char *)(& buf), length ) ) {
125 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
128 } else if ( get_direction() == SG_IO_IN ) {
129 if ( io->get_type() == sgFileType ) {
130 if ( io->read( (char *)(& buf), length ) == length ) {
131 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
132 distribute_data( &buf, cur_fdm_state );
135 while ( io->read( (char *)(& buf), length ) == length ) {
136 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
137 distribute_data( &buf, cur_fdm_state );
147 bool FGOpenGC::close() {
148 SGIOChannel *io = get_io_channel();
150 set_enabled( false );
152 if ( ! io->close() ) {