]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
--warnings
[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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <string.h>             // strstr()
28 #include <stdlib.h>             // strtod(), atoi()
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
36
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39
40 #include "generic.hxx"
41
42
43
44 FGGeneric::FGGeneric(string& config) {
45
46     string file = config+".xml";
47
48     SGPath path( globals->get_fg_root() );
49     path.append("Protocol");
50     path.append(file.c_str());
51     SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
52                                 << path.str());
53
54     SGPropertyNode root;
55     try {
56         readProperties(path.str(), &root);
57      } catch (const sg_exception &e) {
58         SG_LOG(SG_GENERAL, SG_ALERT,
59          "Unable to load the protocol configuration file");
60          return;
61     }
62
63     _out_message.clear();
64     SGPropertyNode *output = root.getNode("generic/output");
65     if (output)
66         read_config(output, _out_message);
67
68     _in_message.clear();
69     SGPropertyNode *input = root.getNode("generic/input");
70     if (input)
71         read_config(input, _in_message);
72 }
73
74 FGGeneric::~FGGeneric() {
75     _out_message.clear();
76     _in_message.clear();
77 }
78
79
80 // generate the message
81 bool FGGeneric::gen_message() {
82
83     string generic_sentence;
84     char tmp[255];
85
86     double val;
87
88     for (unsigned int i = 0; i < _out_message.size(); i++) {
89
90         if (i > 0)
91            generic_sentence += var_separator;
92
93         switch (_out_message[i].type) {
94         case FG_INT:
95             val = _out_message[i].offset +
96                   _out_message[i].prop->getIntValue() * _out_message[i].factor;
97             snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
98             break;
99
100         case FG_BOOL:
101             snprintf(tmp, 255, _out_message[i].format.c_str(),
102                                _out_message[i].prop->getBoolValue());
103             break;
104
105         case FG_DOUBLE:
106             val = _out_message[i].offset +
107                 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
108             snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
109             break;
110
111         default: // SG_STRING
112              snprintf(tmp, 255, _out_message[i].format.c_str(),
113                                 _out_message[i].prop->getStringValue());
114         }
115
116         generic_sentence += tmp;
117     }
118
119     /* After each lot of variables has been added, put the line separator
120      * char/string
121      */
122     generic_sentence += line_separator;
123  
124             
125     length =  generic_sentence.length();
126     strncpy( buf, generic_sentence.c_str(), length );
127
128     return true;
129 }
130
131 bool FGGeneric::parse_message() {
132     char *p2, *p1 = buf;
133     double val;
134     int i = -1;
135
136     while ((++i < (int)_in_message.size()) &&
137            p1 && strcmp(p1, line_separator.c_str())) {
138
139         p2 = strstr(p1, var_separator.c_str());
140         if (p2) {
141             *p2 = 0;
142             p2 += var_separator.length();
143         }
144
145         switch (_in_message[i].type) {
146         case FG_INT:
147             val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
148             _in_message[i].prop->setIntValue((int)val);
149             break;
150
151         case FG_BOOL:
152             _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
153             break;
154
155         case FG_DOUBLE:
156             val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
157             _in_message[i].prop->setFloatValue((float)val);
158             break;
159
160         default: // SG_STRING
161              _in_message[i].prop->setStringValue(p1);
162         }
163
164         p1 = p2;
165     }
166     
167     return true;
168 }
169
170
171
172 // open hailing frequencies
173 bool FGGeneric::open() {
174     if ( is_enabled() ) {
175         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
176                 << "is already in use, ignoring" );
177         return false;
178     }
179
180     SGIOChannel *io = get_io_channel();
181
182     if ( ! io->open( get_direction() ) ) {
183         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
184         return false;
185     }
186
187     set_enabled( true );
188
189     return true;
190 }
191
192
193 // process work for this port
194 bool FGGeneric::process() {
195     SGIOChannel *io = get_io_channel();
196
197     if ( get_direction() == SG_IO_OUT ) {
198         gen_message();
199         if ( ! io->write( buf, length ) ) {
200             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
201             return false;
202         }
203     } else if ( get_direction() == SG_IO_IN ) {
204         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
205             parse_message();
206         } else {
207             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
208             return false;
209         }
210     }
211
212     return true;
213 }
214
215
216 // close the channel
217 bool FGGeneric::close() {
218     SGIOChannel *io = get_io_channel();
219
220     set_enabled( false );
221
222     if ( ! io->close() ) {
223         return false;
224     }
225
226     return true;
227 }
228
229
230 void
231 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
232 {
233         /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
234          * file for each format
235          *
236          * var_sep_string  = the string/charachter to place between variables
237          * line_sep_string = the string/charachter to place at the end of each
238          *                   lot of variables
239          */
240     var_sep_string = root->getStringValue("var_separator");
241     line_sep_string = root->getStringValue("line_separator");
242
243         if ( var_sep_string == "newline" )
244                 var_separator = '\n';
245         else if ( var_sep_string == "tab" )
246                 var_separator = '\t';
247         else if ( var_sep_string == "space" )
248                 var_separator = ' ';
249         else if ( var_sep_string == "formfeed" )
250                 var_separator = '\f';
251         else if ( var_sep_string == "carriagereturn" )
252                 var_sep_string = '\r';
253         else if ( var_sep_string == "verticaltab" )
254                 var_separator = '\v';
255         else
256                 var_separator = var_sep_string;
257
258         if ( line_sep_string == "newline" )
259                 line_separator = '\n';
260         else if ( line_sep_string == "tab" )
261                 line_separator = '\t';
262         else if ( line_sep_string == "space" )
263                 line_separator = ' ';
264         else if ( line_sep_string == "formfeed" )
265                 line_separator = '\f';
266         else if ( line_sep_string == "carriagereturn" )
267                 line_separator = '\r';
268         else if ( line_sep_string == "verticaltab" )
269                 line_separator = '\v';
270         else
271                 line_separator = line_sep_string;
272
273
274     vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
275     for (unsigned int i = 0; i < chunks.size(); i++) {
276
277         _serial_prot chunk;
278
279      // chunk.name = chunks[i]->getStringValue("name");
280         chunk.format = chunks[i]->getStringValue("format", "%d");
281         chunk.offset = chunks[i]->getDoubleValue("offset");
282         chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
283
284         string node = chunks[i]->getStringValue("node");
285         chunk.prop = fgGetNode(node.c_str(), true);
286
287         string type = chunks[i]->getStringValue("type");
288         if (type == "bool")
289             chunk.type = FG_BOOL;
290         else if (type == "float")
291             chunk.type = FG_DOUBLE;
292         else if (type == "string")
293             chunk.type = FG_STRING;
294         else
295             chunk.type = FG_INT;
296
297         msg.push_back(chunk);
298
299     }
300
301 }
302