]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
Improved tumbling behaviour -- the AI doesn't just freeze now, but
[flightgear.git] / src / Network / generic.cxx
1 // generic.cxx -- generic 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 #include <simgear/misc/exception.hxx>
27 #include <simgear/misc/sg_path.hxx>
28 #include <simgear/misc/props.hxx>
29 #include <simgear/misc/props_io.hxx>
30
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
33
34 #include "generic.hxx"
35
36
37 FGGeneric::FGGeneric(string& config) {
38
39     string file = config+".xml";
40
41     SGPath path( globals->get_fg_root() );
42     path.append("Protocol");
43     path.append(file.c_str());
44     SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
45                                 << path.str());
46
47     SGPropertyNode root;
48     try {
49         readProperties(path.str(), &root);
50      } catch (const sg_exception &e) {
51         SG_LOG(SG_GENERAL, SG_ALERT,
52          "Unable to load the protocol configuration file");
53          return;
54     }
55
56     SGPropertyNode *output = root.getNode("generic/output");
57     seperator = output->getStringValue("seperator");
58
59     vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
60     for (unsigned int i = 0; i < chunks.size(); i++) {
61
62         _serial_prot chunk;
63
64      // chunk.name = chunks[i]->getStringValue("name");
65         chunk.format = chunks[i]->getStringValue("format", "%d");
66         chunk.offset = chunks[i]->getDoubleValue("offset");
67         chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
68
69         string node = chunks[i]->getStringValue("node");
70         chunk.prop = fgGetNode(node.c_str(), true);
71
72         string type = chunks[i]->getStringValue("type");
73         if (type == "bool")
74             chunk.type = FG_BOOL;
75         else if (type == "float")
76             chunk.type = FG_DOUBLE;
77         else if (type == "string")
78             chunk.type = FG_STRING;
79         else
80             chunk.type = FG_INT;
81
82         _message.push_back(chunk);
83         
84     }
85
86 }
87
88 FGGeneric::~FGGeneric() {
89     _message.clear();
90 }
91
92
93 // generate the message
94 bool FGGeneric::gen_message() {
95
96     string generic_sentence;
97     char tmp[255];
98
99     int v;
100     double d;
101
102     for (unsigned int i = 0; i < _message.size(); i++) {
103
104         if (i > 0)
105            generic_sentence += seperator;
106
107         switch (_message[i].type) {
108         case FG_INT:
109             v = _message[i].offset +
110                     _message[i].prop->getIntValue() * _message[i].factor;
111             snprintf(tmp, 255, _message[i].format.c_str(), v);
112             break;
113
114         case FG_BOOL:
115             snprintf(tmp, 255, _message[i].format.c_str(),
116                                _message[i].prop->getBoolValue());
117             break;
118
119         case FG_DOUBLE:
120             d = _message[i].offset +
121                        _message[i].prop->getDoubleValue() * _message[i].factor;
122             snprintf(tmp, 255, _message[i].format.c_str(), d);
123             break;
124
125         default: // SG_STRING
126              snprintf(tmp, 255, _message[i].format.c_str(),
127                                _message[i].prop->getStringValue());
128         }
129
130         generic_sentence += tmp;
131     }
132  
133             
134     length =  generic_sentence.length();
135     strncpy( buf, generic_sentence.c_str(), length );
136
137     return true;
138 }
139
140 bool FGGeneric::parse_message() {
141         return true;
142 }
143
144
145
146 // open hailing frequencies
147 bool FGGeneric::open() {
148     if ( is_enabled() ) {
149         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
150                 << "is already in use, ignoring" );
151         return false;
152     }
153
154     SGIOChannel *io = get_io_channel();
155
156     if ( ! io->open( get_direction() ) ) {
157         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
158         return false;
159     }
160
161     set_enabled( true );
162
163     return true;
164 }
165
166
167 // process work for this port
168 bool FGGeneric::process() {
169     SGIOChannel *io = get_io_channel();
170
171     if ( get_direction() == SG_IO_OUT ) {
172         gen_message();
173         if ( ! io->write( buf, length ) ) {
174             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
175             return false;
176         }
177     } else if ( get_direction() == SG_IO_IN ) {
178         // Temporarliy disable this as output only!
179         //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
180         //    parse_message();
181         //} else {
182         //    SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
183         //    return false;
184         //}
185     }
186
187     return true;
188 }
189
190
191 // close the channel
192 bool FGGeneric::close() {
193     SGIOChannel *io = get_io_channel();
194
195     set_enabled( false );
196
197     if ( ! io->close() ) {
198         return false;
199     }
200
201     return true;
202 }