]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
f74c954fe60d8d149f36059e8d15ff27b299da1c
[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/misc/stdint.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/props/props_io.hxx>
37
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Main/util.hxx>
41
42 #include "generic.hxx"
43
44
45
46 FGGeneric::FGGeneric(string& config) {
47
48     string file = config+".xml";
49
50     SGPath path( globals->get_fg_root() );
51     path.append("Protocol");
52     path.append(file.c_str());
53     SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
54                                 << path.str());
55
56     SGPropertyNode root;
57     try {
58         readProperties(path.str(), &root);
59      } catch (const sg_exception &e) {
60         SG_LOG(SG_GENERAL, SG_ALERT,
61          "Unable to load the protocol configuration file");
62          return;
63     }
64
65     SGPropertyNode *output = root.getNode("generic/output");
66     if (output)
67         read_config(output, _out_message);
68
69     SGPropertyNode *input = root.getNode("generic/input");
70     if (input)
71         read_config(input, _in_message);
72 }
73
74 FGGeneric::~FGGeneric() {
75 }
76
77
78 // generate the message
79 bool FGGeneric::gen_message() {
80     string generic_sentence;
81     char tmp[255];
82     length = 0;
83
84     double val;
85
86     for (unsigned int i = 0; i < _out_message.size(); i++) {
87
88         if (i > 0 && !binary_mode)
89             generic_sentence += var_separator;
90
91         switch (_out_message[i].type) {
92         case FG_INT:
93             val = _out_message[i].offset +
94                   _out_message[i].prop->getIntValue() * _out_message[i].factor;
95             if (binary_mode) {
96                 *((int32_t*)&buf[length]) = (int32_t)val;
97                 length += sizeof(int32_t);
98             } else {
99                 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
100             }
101             break;
102
103         case FG_BOOL:
104             if (binary_mode) {
105                 *((int8_t*)&buf[length])
106                           = _out_message[i].prop->getBoolValue() ? true : false;
107                 length += sizeof(int8_t);
108             } else {
109                 snprintf(tmp, 255, _out_message[i].format.c_str(),
110                                    _out_message[i].prop->getBoolValue());
111             }
112             break;
113
114         case FG_DOUBLE:
115             val = _out_message[i].offset +
116                 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
117             if (binary_mode) {
118                 *((double*)&buf[length]) = val;
119                 length += sizeof(double);
120             } else {
121                 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
122             }
123             break;
124
125         default: // SG_STRING
126             if (binary_mode) {
127                 const char *strdata = _out_message[i].prop->getStringValue();
128                 int strlength = strlen(strdata);
129
130                 /* Format for strings is 
131                  * [length as int, 4 bytes][ASCII data, length bytes]
132                  */
133                 *((int32_t*)&buf[length]) = strlength;
134                                 length += sizeof(int32_t);
135                                 strncpy(&buf[length], strdata, strlength);
136                                 length += strlength; 
137                 /* FIXME padding for alignment? Something like: 
138                  * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
139                  */
140             } else {
141                 snprintf(tmp, 255, _out_message[i].format.c_str(),
142                                    _out_message[i].prop->getStringValue());
143             }
144         }
145
146         if (!binary_mode) {
147             generic_sentence += tmp;
148         }
149     }
150
151     if (!binary_mode) {
152         /* After each lot of variables has been added, put the line separator
153          * char/string
154          */
155         generic_sentence += line_separator;
156
157         length =  generic_sentence.length();
158         strncpy( buf, generic_sentence.c_str(), length );
159     } else {
160         // add the footer to the packet ("line")
161         switch (binary_footer_type) {
162             case FOOTER_LENGTH:
163                 binary_footer_value = length;
164                 break;
165
166             case FOOTER_MAGIC:
167                 break;
168         }
169         if (binary_footer_type != FOOTER_NONE) {
170             *((int32_t*)&buf[length]) = binary_footer_value;
171             length += sizeof(int32_t);
172         }
173     }
174
175     return true;
176 }
177
178 bool FGGeneric::parse_message() {
179     char *p2, *p1 = buf;
180     double val;
181     int i = -1;
182
183     if (binary_mode)
184         SG_LOG( SG_IO, SG_ALERT,
185                 "generic protocol: binary mode input is not yet implemented.");
186         
187     while ((++i < (int)_in_message.size()) &&
188             p1 && strcmp(p1, line_separator.c_str())) {
189
190         p2 = strstr(p1, var_separator.c_str());
191         if (p2) {
192             *p2 = 0;
193             p2 += var_separator.length();
194         }
195
196         switch (_in_message[i].type) {
197         case FG_INT:
198             val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
199             _in_message[i].prop->setIntValue((int)val);
200             break;
201
202         case FG_BOOL:
203             _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
204             break;
205
206         case FG_DOUBLE:
207             val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
208             _in_message[i].prop->setFloatValue((float)val);
209             break;
210
211         default: // SG_STRING
212              _in_message[i].prop->setStringValue(p1);
213         }
214
215         p1 = p2;
216     }
217     
218     return true;
219 }
220
221
222
223 // open hailing frequencies
224 bool FGGeneric::open() {
225     if ( is_enabled() ) {
226         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
227                 << "is already in use, ignoring" );
228         return false;
229     }
230
231     SGIOChannel *io = get_io_channel();
232
233     if ( ! io->open( get_direction() ) ) {
234         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
235         return false;
236     }
237
238     set_enabled( true );
239
240     if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
241         if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
242             SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
243             return false;
244         }
245     }
246
247     return true;
248 }
249
250
251 // process work for this port
252 bool FGGeneric::process() {
253     SGIOChannel *io = get_io_channel();
254
255     if ( get_direction() == SG_IO_OUT ) {
256         gen_message();
257         if ( ! io->write( buf, length ) ) {
258             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
259             return false;
260         }
261     } else if ( get_direction() == SG_IO_IN ) {
262         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
263             parse_message();
264         } else {
265             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
266             return false;
267         }
268     }
269
270     return true;
271 }
272
273
274 // close the channel
275 bool FGGeneric::close() {
276     SGIOChannel *io = get_io_channel();
277
278     if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
279         if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
280             SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
281             return false;
282         }
283     }
284
285     set_enabled( false );
286
287     if ( ! io->close() ) {
288         return false;
289     }
290
291     return true;
292 }
293
294
295 void
296 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
297 {
298     binary_mode = root->getBoolValue("binary_mode");
299
300     if (!binary_mode) {
301         /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
302          * file for each format
303          *
304          * var_sep_string  = the string/charachter to place between variables
305          * line_sep_string = the string/charachter to place at the end of each
306          *                   lot of variables
307          */
308         preamble = fgUnescape(root->getStringValue("preamble"));
309         postamble = fgUnescape(root->getStringValue("postamble"));
310         var_sep_string = fgUnescape(root->getStringValue("var_separator"));
311         line_sep_string = fgUnescape(root->getStringValue("line_separator"));
312
313         if ( var_sep_string == "newline" )
314                 var_separator = '\n';
315         else if ( var_sep_string == "tab" )
316                 var_separator = '\t';
317         else if ( var_sep_string == "space" )
318                 var_separator = ' ';
319         else if ( var_sep_string == "formfeed" )
320                 var_separator = '\f';
321         else if ( var_sep_string == "carriagereturn" )
322                 var_sep_string = '\r';
323         else if ( var_sep_string == "verticaltab" )
324                 var_separator = '\v';
325         else
326                 var_separator = var_sep_string;
327
328         if ( line_sep_string == "newline" )
329                 line_separator = '\n';
330         else if ( line_sep_string == "tab" )
331                 line_separator = '\t';
332         else if ( line_sep_string == "space" )
333                 line_separator = ' ';
334         else if ( line_sep_string == "formfeed" )
335                 line_separator = '\f';
336         else if ( line_sep_string == "carriagereturn" )
337                 line_separator = '\r';
338         else if ( line_sep_string == "verticaltab" )
339                 line_separator = '\v';
340         else
341                 line_separator = line_sep_string;
342     } else {
343         binary_footer_type = FOOTER_NONE; // default choice
344         if ( root->hasValue("binary_footer") ) {
345             string footer_type = root->getStringValue("binary_footer");
346             if ( footer_type == "length" )
347                 binary_footer_type = FOOTER_LENGTH;
348             else if ( footer_type.substr(0, 5) == "magic" ) {
349                 binary_footer_type = FOOTER_MAGIC;
350                 binary_footer_value = strtol(footer_type.substr(6, 
351                             footer_type.length() - 6).c_str(), (char**)0, 0);
352             } else if ( footer_type != "none" )
353                 SG_LOG(SG_IO, SG_ALERT,
354                        "generic protocol: Undefined generic binary protocol"
355                                            "footer, using no footer.");
356         }
357     }
358
359     vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
360     for (unsigned int i = 0; i < chunks.size(); i++) {
361
362         _serial_prot chunk;
363
364         // chunk.name = chunks[i]->getStringValue("name");
365         chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
366         chunk.offset = chunks[i]->getDoubleValue("offset");
367         chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
368
369         string node = chunks[i]->getStringValue("node", "/null");
370         chunk.prop = fgGetNode(node.c_str(), true);
371
372         string type = chunks[i]->getStringValue("type");
373         if (type == "bool")
374             chunk.type = FG_BOOL;
375         else if (type == "float")
376             chunk.type = FG_DOUBLE;
377         else if (type == "string")
378             chunk.type = FG_STRING;
379         else
380             chunk.type = FG_INT;
381
382         msg.push_back(chunk);
383
384     }
385
386 }
387