]> git.mxchange.org Git - flightgear.git/blob - src/Network/opengc.cxx
Recover this file. We are not able to use boost as a replacement because
[flightgear.git] / src / Network / opengc.cxx
1 // opengc.cxx - Network interface program to send data to display processor over LAN
2 //
3 // Created by:  J. Wojnaroski  -- castle@mminternet.com
4 // Date:                21 Nov 2001 
5 //
6 // Extended from original network code developed by C. Olson
7 //
8 //  Modified 12/02/01 - Update engine structure for multi-engine models
9 //                        - Added data preamble to id msg types
10 //
11 //  Modified 01/23/02 - Converted portions of the Engine and Gear accesssors to properties
12 //                          - Removed data from navigation functions. OpenGC provides own nav functions
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License as
16 // published by the Free Software Foundation; either version 2 of the
17 // License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful, but
20 // WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 // General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 //
28
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/io/iochannel.hxx>
31
32 #include <vector>
33
34 #include "opengc.hxx"
35 #include <FDM/flight.hxx>
36 #include <Main/globals.hxx>
37 #include <Cockpit/radiostack.hxx>
38 #include <Controls/controls.hxx>
39 #include <Main/fg_props.hxx>
40
41 SG_USING_STD(vector);
42
43 FGOpenGC::FGOpenGC() {
44 }
45
46 FGOpenGC::~FGOpenGC() {
47 }
48
49 // open hailing frequencies
50 bool FGOpenGC::open() {
51     if ( is_enabled() ) {
52         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
53                 << "is already in use, ignoring" );
54         return false;
55     }
56
57     SGIOChannel *io = get_io_channel();
58
59     if ( ! io->open( get_direction() ) ) {
60         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
61         return false;
62     }
63
64     set_enabled( true );
65
66     return true;
67 }
68
69
70 static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
71                                                                                                 
72     data->version_id = 0x0012;
73
74     data->longitude = cur_fdm_state->get_Longitude_deg();   
75     data->latitude = cur_fdm_state->get_Latitude_deg();
76     data->magvar = globals->get_mag()->get_magvar();
77    
78     data->pitch = cur_fdm_state->get_Theta_deg();
79     data->bank = cur_fdm_state->get_Phi_deg();
80     data->heading = cur_fdm_state->get_Psi_deg();
81     data->altitude = cur_fdm_state->get_Altitude();
82     data->altitude_agl = cur_fdm_state->get_Altitude_AGL();
83     data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
84     data->vvi = cur_fdm_state->get_Climb_Rate();
85     data->mach = cur_fdm_state->get_Mach_number();
86     data->groundspeed = cur_fdm_state->get_V_ground_speed();
87     data->v_tas = cur_fdm_state->get_V_equiv_kts();
88
89     data->phi_dot = cur_fdm_state->get_Phi_dot();
90     data->theta_dot = cur_fdm_state->get_Theta_dot();
91     data->psi_dot = cur_fdm_state->get_Psi_dot();
92
93     data->alpha = cur_fdm_state->get_Alpha();
94     data->beta = cur_fdm_state->get_Beta();
95     data->alpha_dot = cur_fdm_state->get_Alpha_dot();
96     data->beta_dot = cur_fdm_state->get_Beta_dot();
97
98  
99     data->aileron = globals->get_controls()->get_aileron();
100     //data->aileron_trim = p_Controls->get_aileron_trim();
101     data->elevator = globals->get_controls()->get_elevator();
102     data->elevator_trim = globals->get_controls()->get_elevator_trim();
103     data->rudder = globals->get_controls()->get_rudder();
104     //data->rudder_trim = p_Controls->get_rudder_trim();
105     //data->flaps = fgGetDouble("/controls/flaps");
106     data->flaps = globals->get_controls()->get_flaps();
107
108     //data->gear_nose = p_gear[0]->GetPosition();
109     //data->gear_left = p_gear[1]->GetPosition();
110     //data->gear_right = p_gear[2]->GetPosition();
111
112     data->rpm[0] = fgGetDouble("/engines/engine[0]/rpm");
113     data->rpm[1] = fgGetDouble("/engines/engine[1]/rpm");   
114
115     data->epr[0] = fgGetDouble("/engines/engine[0]/epr");
116     data->epr[1] = fgGetDouble("/engines/engine[1]/epr");
117     data->epr[2] = fgGetDouble("/engines/engine[2]/epr");
118     data->epr[3] = fgGetDouble("/engines/engine[3]/epr");
119
120     data->egt[0] = fgGetDouble("/engines/engine[0]/egt-degf");
121     data->egt[1] = fgGetDouble("/engines/engine[1]/egt-degf");
122     data->egt[2] = fgGetDouble("/engines/engine[2]/egt-degf");
123     data->egt[3] = fgGetDouble("/engines/engine[3]/egt-degf");
124
125     data->n2_turbine[0] = fgGetDouble("/engines/engine[0]/n2");
126     data->n2_turbine[1] = fgGetDouble("/engines/engine[1]/n2");
127     data->n2_turbine[2] = fgGetDouble("/engines/engine[2]/n2");
128     data->n2_turbine[3] = fgGetDouble("/engines/engine[3]/n2");
129
130     data->n1_turbine[0] = fgGetDouble("/engines/engine[0]/n1");
131     data->n1_turbine[1] = fgGetDouble("/engines/engine[1]/n1");
132     data->n1_turbine[2] = fgGetDouble("/engines/engine[2]/n1");
133     data->n1_turbine[3] = fgGetDouble("/engines/engine[3]/n1");
134
135     data->fuel_flow[0] = fgGetDouble("/engines/engine[0]/fuel-flow-gph");
136     data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph");
137     data->fuel_flow[1] = fgGetDouble("/engines/engine[1]/fuel-flow-gph");
138     data->fuel_flow[2] = fgGetDouble("/engines/engine[2]/fuel-flow-gph");
139
140     data->oil_pressure[0] = fgGetDouble("/engines/engine[0]/oil-pressure-psi");
141     data->oil_pressure[1] = fgGetDouble("/engines/engine[1]/oil-pressure-psi");
142     data->oil_pressure[2] = fgGetDouble("/engines/engine[2]/oil-pressure-psi");
143     data->oil_pressure[3] = fgGetDouble("/engines/engine[3]/oil-pressure-psi");
144
145     data->man_pressure[0] = fgGetDouble("/engines/engine[0]/mp-osi");
146     data->man_pressure[1] = fgGetDouble("/engines/engine[1]/mp-osi");    
147
148     data->static_temperature = cur_fdm_state->get_Static_temperature();
149     data->total_temperature = cur_fdm_state->get_Total_temperature();
150     data->static_pressure = cur_fdm_state->get_Static_pressure();
151     data->total_pressure = cur_fdm_state->get_Total_pressure();
152     data->dynamic_pressure = cur_fdm_state->get_Dynamic_pressure();
153 }
154
155 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
156     // just a place holder until the CDU is developed
157         
158 }
159
160 // process work for this port
161 bool FGOpenGC::process() {
162     SGIOChannel *io = get_io_channel();
163     int length = sizeof(buf);
164
165     if ( get_direction() == SG_IO_OUT ) {
166         collect_data( cur_fdm_state, &buf );
167         //collect_data( &buf );
168         if ( ! io->write( (char *)(& buf), length ) ) {
169             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
170             return false;
171         }
172     } else if ( get_direction() == SG_IO_IN ) {
173         if ( io->get_type() == sgFileType ) {
174             if ( io->read( (char *)(& buf), length ) == length ) {
175                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
176                 distribute_data( &buf, cur_fdm_state );
177             }
178         } else {
179             while ( io->read( (char *)(& buf), length ) == length ) {
180                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
181                 distribute_data( &buf, cur_fdm_state );
182             }
183         }
184     }
185
186     return true;
187 }
188
189
190 // close the channel
191 bool FGOpenGC::close() {
192     SGIOChannel *io = get_io_channel();
193
194     set_enabled( false );
195
196     if ( ! io->close() ) {
197         return false;
198     }
199
200     return true;
201 }
202