]> git.mxchange.org Git - flightgear.git/blob - src/Network/native.cxx
Compute teh altitude-agl if the remote end isn't sending it.
[flightgear.git] / src / Network / native.cxx
1 // native.cxx -- FGFS "Native" 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 <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
26
27 #include "native.hxx"
28
29
30 FGNative::FGNative() {
31 }
32
33 FGNative::~FGNative() {
34 }
35
36
37 // open hailing frequencies
38 bool FGNative::open() {
39     if ( is_enabled() ) {
40         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
41                 << "is already in use, ignoring" );
42         return false;
43     }
44
45     SGIOChannel *io = get_io_channel();
46
47     if ( ! io->open( get_direction() ) ) {
48         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
49         return false;
50     }
51
52     set_enabled( true );
53
54     return true;
55 }
56
57
58 // process work for this port
59 bool FGNative::process() {
60     SGIOChannel *io = get_io_channel();
61     int length = sizeof(*cur_fdm_state);
62
63     if ( get_direction() == SG_IO_OUT ) {
64         // cout << "size of cur_fdm_state = " << length << endl;
65         buf = *cur_fdm_state;
66         if ( ! io->write( (char *)(& buf), length ) ) {
67             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
68             return false;
69         }
70     } else if ( get_direction() == SG_IO_IN ) {
71         if ( io->get_type() == sgFileType ) {
72             if ( io->read( (char *)(& buf), length ) == length ) {
73                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
74                 *cur_fdm_state = buf;
75             }
76         } else {
77             while ( io->read( (char *)(& buf), length ) == length ) {
78                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
79                 *cur_fdm_state = buf;
80             }
81         }
82     }
83
84     return true;
85 }
86
87
88 // close the channel
89 bool FGNative::close() {
90     SGIOChannel *io = get_io_channel();
91
92     set_enabled( false );
93
94     if ( ! io->close() ) {
95         return false;
96     }
97
98     return true;
99 }