1 // generic.cxx -- generic protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #include <string.h> // strstr()
28 #include <stdlib.h> // strtod(), atoi()
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>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
41 #include "generic.hxx"
45 FGGeneric::FGGeneric(string& config) {
47 string file = config+".xml";
49 SGPath path( globals->get_fg_root() );
50 path.append("Protocol");
51 path.append(file.c_str());
52 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
57 readProperties(path.str(), &root);
58 } catch (const sg_exception &e) {
59 SG_LOG(SG_GENERAL, SG_ALERT,
60 "Unable to load the protocol configuration file");
65 SGPropertyNode *output = root.getNode("generic/output");
67 read_config(output, _out_message);
70 SGPropertyNode *input = root.getNode("generic/input");
72 read_config(input, _in_message);
75 FGGeneric::~FGGeneric() {
81 // generate the message
82 bool FGGeneric::gen_message() {
83 string generic_sentence;
89 for (unsigned int i = 0; i < _out_message.size(); i++) {
91 if (i > 0 && !binary_mode)
92 generic_sentence += var_separator;
94 switch (_out_message[i].type) {
96 val = _out_message[i].offset +
97 _out_message[i].prop->getIntValue() * _out_message[i].factor;
99 *((int32_t*)&buf[length]) = (int32_t)val;
100 length += sizeof(int32_t);
102 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
108 *((int8_t*)&buf[length])
109 = _out_message[i].prop->getBoolValue() ? true : false;
110 length += sizeof(int8_t);
112 snprintf(tmp, 255, _out_message[i].format.c_str(),
113 _out_message[i].prop->getBoolValue());
118 val = _out_message[i].offset +
119 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
121 *((double*)&buf[length]) = val;
122 length += sizeof(double);
124 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
128 default: // SG_STRING
130 const char *strdata = _out_message[i].prop->getStringValue();
131 int strlength = strlen(strdata);
133 /* Format for strings is
134 * [length as int, 4 bytes][ASCII data, length bytes]
136 *((int32_t*)&buf[length]) = strlength;
137 length += sizeof(int32_t);
138 strncpy(&buf[length], strdata, strlength);
140 /* FIXME padding for alignment? Something like:
141 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
144 snprintf(tmp, 255, _out_message[i].format.c_str(),
145 _out_message[i].prop->getStringValue());
150 generic_sentence += tmp;
155 /* After each lot of variables has been added, put the line separator
158 generic_sentence += line_separator;
160 length = generic_sentence.length();
161 strncpy( buf, generic_sentence.c_str(), length );
163 // add the footer to the packet ("line")
164 switch (binary_footer_type) {
166 binary_footer_value = length;
172 if (binary_footer_type != FOOTER_NONE) {
173 *((int32_t*)&buf[length]) = binary_footer_value;
174 length += sizeof(int32_t);
181 bool FGGeneric::parse_message() {
187 SG_LOG( SG_IO, SG_ALERT,
188 "generic protocol: binary mode input is not yet implemented.");
190 while ((++i < (int)_in_message.size()) &&
191 p1 && strcmp(p1, line_separator.c_str())) {
193 p2 = strstr(p1, var_separator.c_str());
196 p2 += var_separator.length();
199 switch (_in_message[i].type) {
201 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
202 _in_message[i].prop->setIntValue((int)val);
206 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
210 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
211 _in_message[i].prop->setFloatValue((float)val);
214 default: // SG_STRING
215 _in_message[i].prop->setStringValue(p1);
226 // open hailing frequencies
227 bool FGGeneric::open() {
228 if ( is_enabled() ) {
229 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
230 << "is already in use, ignoring" );
234 SGIOChannel *io = get_io_channel();
236 if ( ! io->open( get_direction() ) ) {
237 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
247 // process work for this port
248 bool FGGeneric::process() {
249 SGIOChannel *io = get_io_channel();
251 if ( get_direction() == SG_IO_OUT ) {
253 if ( ! io->write( buf, length ) ) {
254 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
257 } else if ( get_direction() == SG_IO_IN ) {
258 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
261 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
271 bool FGGeneric::close() {
272 SGIOChannel *io = get_io_channel();
274 set_enabled( false );
276 if ( ! io->close() ) {
285 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
287 if (root->hasValue("binary_mode"))
288 binary_mode = root->getBoolValue("binary_mode");
293 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
294 * file for each format
296 * var_sep_string = the string/charachter to place between variables
297 * line_sep_string = the string/charachter to place at the end of each
300 var_sep_string = root->getStringValue("var_separator");
301 line_sep_string = root->getStringValue("line_separator");
303 if ( var_sep_string == "newline" )
304 var_separator = '\n';
305 else if ( var_sep_string == "tab" )
306 var_separator = '\t';
307 else if ( var_sep_string == "space" )
309 else if ( var_sep_string == "formfeed" )
310 var_separator = '\f';
311 else if ( var_sep_string == "carriagereturn" )
312 var_sep_string = '\r';
313 else if ( var_sep_string == "verticaltab" )
314 var_separator = '\v';
316 var_separator = var_sep_string;
318 if ( line_sep_string == "newline" )
319 line_separator = '\n';
320 else if ( line_sep_string == "tab" )
321 line_separator = '\t';
322 else if ( line_sep_string == "space" )
323 line_separator = ' ';
324 else if ( line_sep_string == "formfeed" )
325 line_separator = '\f';
326 else if ( line_sep_string == "carriagereturn" )
327 line_separator = '\r';
328 else if ( line_sep_string == "verticaltab" )
329 line_separator = '\v';
331 line_separator = line_sep_string;
333 binary_footer_type = FOOTER_NONE; // default choice
334 if ( root->hasValue("binary_footer") ) {
335 string footer_type = root->getStringValue("binary_footer");
336 if ( footer_type == "length" )
337 binary_footer_type = FOOTER_LENGTH;
338 else if ( footer_type.substr(0, 5) == "magic" ) {
339 binary_footer_type = FOOTER_MAGIC;
340 binary_footer_value = strtol(footer_type.substr(6,
341 footer_type.length() - 6).c_str(), (char**)0, 0);
342 } else if ( footer_type != "none" )
343 SG_LOG(SG_IO, SG_ALERT,
344 "generic protocol: Undefined generic binary protocol"
345 "footer, using no footer.");
349 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
350 for (unsigned int i = 0; i < chunks.size(); i++) {
354 // chunk.name = chunks[i]->getStringValue("name");
355 chunk.format = chunks[i]->getStringValue("format", "%d");
356 chunk.offset = chunks[i]->getDoubleValue("offset");
357 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
359 string node = chunks[i]->getStringValue("node");
360 chunk.prop = fgGetNode(node.c_str(), true);
362 string type = chunks[i]->getStringValue("type");
364 chunk.type = FG_BOOL;
365 else if (type == "float")
366 chunk.type = FG_DOUBLE;
367 else if (type == "string")
368 chunk.type = FG_STRING;
372 msg.push_back(chunk);