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>
40 #include <Main/util.hxx>
42 #include "generic.hxx"
46 FGGeneric::FGGeneric(string& config) : exitOnError(false)
49 string file = config+".xml";
51 SGPath path( globals->get_fg_root() );
52 path.append("Protocol");
53 path.append(file.c_str());
54 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
59 readProperties(path.str(), &root);
60 } catch (const sg_exception &e) {
61 SG_LOG(SG_GENERAL, SG_ALERT,
62 "Unable to load the protocol configuration file");
66 SGPropertyNode *output = root.getNode("generic/output");
68 read_config(output, _out_message);
70 SGPropertyNode *input = root.getNode("generic/input");
72 read_config(input, _in_message);
75 FGGeneric::~FGGeneric() {
79 // generate the message
80 bool FGGeneric::gen_message() {
81 string generic_sentence;
87 for (unsigned int i = 0; i < _out_message.size(); i++) {
89 if (i > 0 && !binary_mode)
90 generic_sentence += var_separator;
92 switch (_out_message[i].type) {
94 val = _out_message[i].offset +
95 _out_message[i].prop->getIntValue() * _out_message[i].factor;
97 if (binary_byte_order == HOST_BYTE_ORDER) {
98 *((int32_t*)&buf[length]) = (int32_t)val;
100 buf[length + 0] = (int8_t)((int32_t)val >> 24);
101 buf[length + 1] = (int8_t)((int32_t)val >> 16);
102 buf[length + 2] = (int8_t)((int32_t)val >> 8);
103 buf[length + 3] = (int8_t)val;
105 length += sizeof(int32_t);
107 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
113 *((int8_t*)&buf[length])
114 = _out_message[i].prop->getBoolValue() ? true : false;
115 length += sizeof(int8_t);
117 snprintf(tmp, 255, _out_message[i].format.c_str(),
118 _out_message[i].prop->getBoolValue());
123 val = _out_message[i].offset +
124 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
126 int fixed = (int)(val * 65536.0f);
127 if (binary_byte_order == HOST_BYTE_ORDER) {
128 *((int32_t*)&buf[length]) = (int32_t)fixed;
130 buf[length + 0] = (int8_t)(fixed >> 24);
131 buf[length + 1] = (int8_t)(fixed >> 16);
132 buf[length + 2] = (int8_t)(fixed >> 8);
133 buf[length + 3] = (int8_t)fixed;
135 length += sizeof(int32_t);
137 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
142 val = _out_message[i].offset +
143 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
145 if (binary_byte_order == HOST_BYTE_ORDER) {
146 *((double*)&buf[length]) = val;
148 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
149 "FG_DOUBLE will be written in host byte order.");
150 *((double*)&buf[length]) = val;
152 length += sizeof(double);
154 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
158 default: // SG_STRING
160 const char *strdata = _out_message[i].prop->getStringValue();
161 int strlength = strlen(strdata);
163 if (binary_byte_order == NETWORK_BYTE_ORDER) {
164 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
165 "FG_STRING will be written in host byte order.");
167 /* Format for strings is
168 * [length as int, 4 bytes][ASCII data, length bytes]
170 *((int32_t*)&buf[length]) = strlength;
171 length += sizeof(int32_t);
172 strncpy(&buf[length], strdata, strlength);
174 /* FIXME padding for alignment? Something like:
175 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
178 snprintf(tmp, 255, _out_message[i].format.c_str(),
179 _out_message[i].prop->getStringValue());
184 generic_sentence += tmp;
189 /* After each lot of variables has been added, put the line separator
192 generic_sentence += line_separator;
194 length = generic_sentence.length();
195 strncpy( buf, generic_sentence.c_str(), length );
197 // add the footer to the packet ("line")
198 switch (binary_footer_type) {
200 binary_footer_value = length;
206 if (binary_footer_type != FOOTER_NONE) {
207 *((int32_t*)&buf[length]) = binary_footer_value;
208 length += sizeof(int32_t);
215 bool FGGeneric::parse_message() {
222 while ((++i < (int)_in_message.size()) &&
223 p1 && strcmp(p1, line_separator.c_str())) {
225 p2 = strstr(p1, var_separator.c_str());
228 p2 += var_separator.length();
231 switch (_in_message[i].type) {
233 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
234 _in_message[i].prop->setIntValue((int)val);
238 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
243 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
244 _in_message[i].prop->setFloatValue((float)val);
247 default: // SG_STRING
248 _in_message[i].prop->setStringValue(p1);
255 while ((++i < (int)_in_message.size()) &&
256 (p1 - buf < FG_MAX_MSG_SIZE)) {
258 switch (_in_message[i].type) {
260 if (binary_byte_order == NETWORK_BYTE_ORDER) {
262 (((p1[0]) & 0xff) << 24) |
263 (((p1[1]) & 0xff) << 16) |
264 (((p1[2]) & 0xff) << 8) |
267 tmp = *(int32_t *)p1;
269 val = _in_message[i].offset +
271 _in_message[i].factor;
272 _in_message[i].prop->setIntValue((int)val);
273 p1 += sizeof(int32_t);
277 _in_message[i].prop->setBoolValue( p1[0] != 0.0 );
282 if (binary_byte_order == NETWORK_BYTE_ORDER) {
284 (((p1[0]) & 0xff) << 24) |
285 (((p1[1]) & 0xff) << 16) |
286 (((p1[2]) & 0xff) << 8) |
289 tmp = *(int32_t *)p1;
291 val = _in_message[i].offset +
292 ((double)tmp / 65536.0f) * _in_message[i].factor;
293 _in_message[i].prop->setFloatValue(val);
294 p1 += sizeof(int32_t);
298 default: // SG_STRING
299 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
300 "Ignoring unsupported binary input chunk type.");
310 // open hailing frequencies
311 bool FGGeneric::open() {
312 if ( is_enabled() ) {
313 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
314 << "is already in use, ignoring" );
318 SGIOChannel *io = get_io_channel();
320 if ( ! io->open( get_direction() ) ) {
321 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
327 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
328 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
329 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
338 // process work for this port
339 bool FGGeneric::process() {
340 SGIOChannel *io = get_io_channel();
342 if ( get_direction() == SG_IO_OUT ) {
344 if ( ! io->write( buf, length ) ) {
345 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
348 } else if ( get_direction() == SG_IO_IN ) {
350 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
353 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
357 if ( (length = io->read( buf, binary_record_length )) > 0 ) {
358 if (length != binary_record_length) {
359 SG_LOG( SG_IO, SG_ALERT,
360 "Generic protocol: Received binary "
361 "record of unexpected size." );
363 SG_LOG( SG_IO, SG_DEBUG,
364 "Generic protocol: received record of " << length <<
369 SG_LOG( SG_IO, SG_INFO,
370 "Generic protocol: Error reading data." );
385 bool FGGeneric::close() {
386 SGIOChannel *io = get_io_channel();
388 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
389 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
390 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
395 set_enabled( false );
397 if ( ! io->close() ) {
406 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
408 binary_mode = root->getBoolValue("binary_mode");
411 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
412 * file for each format
414 * var_sep_string = the string/charachter to place between variables
415 * line_sep_string = the string/charachter to place at the end of each
418 preamble = fgUnescape(root->getStringValue("preamble"));
419 postamble = fgUnescape(root->getStringValue("postamble"));
420 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
421 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
423 if ( var_sep_string == "newline" )
424 var_separator = '\n';
425 else if ( var_sep_string == "tab" )
426 var_separator = '\t';
427 else if ( var_sep_string == "space" )
429 else if ( var_sep_string == "formfeed" )
430 var_separator = '\f';
431 else if ( var_sep_string == "carriagereturn" )
432 var_sep_string = '\r';
433 else if ( var_sep_string == "verticaltab" )
434 var_separator = '\v';
436 var_separator = var_sep_string;
438 if ( line_sep_string == "newline" )
439 line_separator = '\n';
440 else if ( line_sep_string == "tab" )
441 line_separator = '\t';
442 else if ( line_sep_string == "space" )
443 line_separator = ' ';
444 else if ( line_sep_string == "formfeed" )
445 line_separator = '\f';
446 else if ( line_sep_string == "carriagereturn" )
447 line_separator = '\r';
448 else if ( line_sep_string == "verticaltab" )
449 line_separator = '\v';
451 line_separator = line_sep_string;
453 binary_footer_type = FOOTER_NONE; // default choice
454 binary_record_length = -1; // default choice = sizeof(representation)
455 binary_byte_order = HOST_BYTE_ORDER; // default choice
456 if ( root->hasValue("binary_footer") ) {
457 string footer_type = root->getStringValue("binary_footer");
458 if ( footer_type == "length" )
459 binary_footer_type = FOOTER_LENGTH;
460 else if ( footer_type.substr(0, 5) == "magic" ) {
461 binary_footer_type = FOOTER_MAGIC;
462 binary_footer_value = strtol(footer_type.substr(6,
463 footer_type.length() - 6).c_str(), (char**)0, 0);
464 } else if ( footer_type != "none" )
465 SG_LOG(SG_IO, SG_ALERT,
466 "generic protocol: Undefined generic binary protocol"
467 "footer, using no footer.");
469 if ( root->hasValue("record_length") ) {
470 binary_record_length = root->getIntValue("record_length");
472 if ( root->hasValue("byte_order") ) {
473 string byte_order = root->getStringValue("byte_order");
474 if ( byte_order == "network" ) {
475 binary_byte_order = NETWORK_BYTE_ORDER;
476 } else if ( byte_order == "host" ) {
477 binary_byte_order = HOST_BYTE_ORDER;
479 SG_LOG(SG_IO, SG_ALERT,
480 "generic protocol: Undefined generic binary protocol"
481 "byte order, using HOST byte order.");
486 int record_length = 0; // Only used for binary protocols.
487 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
488 for (unsigned int i = 0; i < chunks.size(); i++) {
492 // chunk.name = chunks[i]->getStringValue("name");
493 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
494 chunk.offset = chunks[i]->getDoubleValue("offset");
495 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
497 string node = chunks[i]->getStringValue("node", "/null");
498 chunk.prop = fgGetNode(node.c_str(), true);
500 string type = chunks[i]->getStringValue("type");
501 if (type == "bool") {
502 chunk.type = FG_BOOL;
504 } else if (type == "float") {
505 chunk.type = FG_DOUBLE;
506 record_length += sizeof(double);
507 } else if (type == "fixed") {
508 chunk.type = FG_FIXED;
509 record_length += sizeof(int32_t);
510 } else if (type == "string")
511 chunk.type = FG_STRING;
514 record_length += sizeof(int32_t);
516 msg.push_back(chunk);
520 if (binary_record_length == -1) {
521 binary_record_length = record_length;
522 } else if (binary_record_length < record_length) {
523 SG_LOG(SG_IO, SG_ALERT,
524 "generic protocol: Requested binary record length shorter than"
525 " requested record representation.");
526 binary_record_length = record_length;