]> git.mxchange.org Git - flightgear.git/blob - src/Network/opengc.cxx
Substantial rewrite of FGNewMat, the material class. Most of the
[flightgear.git] / src / Network / opengc.cxx
1 // opengc.cxx - Network interface program to send sim data onto a LAN
2 //
3 // Created by:  J. Wojnaroski  -- castle@mminternet.com
4 // Date:                21 Nov 2001 
5 //
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
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/io/iochannel.hxx>
24
25 #include <vector>
26
27 #include "opengc.hxx"
28 #include <FDM/flight.hxx>
29 #include <Main/globals.hxx>
30 #include <Cockpit/radiostack.hxx>
31
32 SG_USING_STD(vector);
33
34 FGOpenGC::FGOpenGC() {
35 }
36
37 FGOpenGC::~FGOpenGC() {
38 }
39
40 // open hailing frequencies
41 bool FGOpenGC::open() {
42     if ( is_enabled() ) {
43         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
44                 << "is already in use, ignoring" );
45         return false;
46     }
47
48     SGIOChannel *io = get_io_channel();
49
50     if ( ! io->open( get_direction() ) ) {
51         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
52         return false;
53     }
54
55     set_enabled( true );
56
57     return true;
58 }
59
60
61 static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
62     //static void collect_data( ogcFGData *data ) {
63
64     FGEngInterface      *p_engine[4];  // four is enough unless you're a BUF
65
66     p_engine[0] = cur_fdm_state->get_engine(0);
67     p_engine[1] = cur_fdm_state->get_engine(1);
68
69     data->version_id = 0x0011;
70
71     data->latitude = fdm->get_Longitude_deg();   
72     data->longitude = cur_fdm_state->get_Latitude_deg();
73
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();
80
81     data->magvar = globals->get_mag()->get_magvar();
82
83     data->rpm[0] = p_engine[0]->get_RPM();
84     data->rpm[1] = p_engine[1]->get_RPM();
85
86     data->epr[0] = p_engine[0]->get_Manifold_Pressure();
87     data->epr[1] = p_engine[1]->get_Manifold_Pressure();
88
89     data->egt[0] = p_engine[0]->get_EGT();
90     data->egt[1] = p_engine[1]->get_EGT();
91
92     data->oil_pressure[0] = p_engine[0]->get_Oil_Pressure();
93     data->oil_pressure[1] = p_engine[1]->get_Oil_Pressure();
94
95
96 // navigation data
97 // Once OPenGC develops a comparable navaids database some of this will not be required
98
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(); 
103
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(); 
108
109 }
110
111 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
112     // just a place holder until the CDU is developed
113         
114 }
115
116 // process work for this port
117 bool FGOpenGC::process() {
118     SGIOChannel *io = get_io_channel();
119     int length = sizeof(buf);
120
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." );
126             return false;
127         }
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 );
133             }
134         } else {
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 );
138             }
139         }
140     }
141
142     return true;
143 }
144
145
146 // close the channel
147 bool FGOpenGC::close() {
148     SGIOChannel *io = get_io_channel();
149
150     set_enabled( false );
151
152     if ( ! io->close() ) {
153         return false;
154     }
155
156     return true;
157 }
158