1 // generic.cxx -- generic protocol 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()
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/io/iochannel.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/misc/stdint.hxx>
36 #include <simgear/misc/strutils.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/props/props_io.hxx>
39 #include <simgear/math/SGMath.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
43 #include <Main/fg_os.hxx>
44 #include <Main/util.hxx>
45 #include "generic.hxx"
47 using simgear::strutils::unescape;
49 FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false), initOk(false)
52 if (tokens[1] == "socket") {
54 } else if (tokens[1] == "file") {
60 if ((configToken >= tokens.size())||(tokens[ configToken ] == "")) {
61 SG_LOG(SG_NETWORK, SG_ALERT,
62 "Not enough tokens passed for generic '" << tokens[1] << "' protocol. ");
66 string config = tokens[ configToken ];
67 file_name = config+".xml";
68 direction = tokens[2];
70 if (direction != "in" && direction != "out" && direction != "bi") {
71 SG_LOG(SG_NETWORK, SG_ALERT, "Unsuported protocol direction: "
79 FGGeneric::~FGGeneric() {
92 // generate the message
93 bool FGGeneric::gen_message_binary() {
94 string generic_sentence;
98 for (unsigned int i = 0; i < _out_message.size(); i++) {
100 switch (_out_message[i].type) {
103 val = _out_message[i].offset +
104 _out_message[i].prop->getIntValue() * _out_message[i].factor;
105 int32_t intVal = val;
106 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
107 intVal = (int32_t) sg_bswap_32((uint32_t)intVal);
109 memcpy(&buf[length], &intVal, sizeof(int32_t));
110 length += sizeof(int32_t);
115 buf[length] = (char) (_out_message[i].prop->getBoolValue() ? true : false);
121 val = _out_message[i].offset +
122 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
124 int32_t fixed = (int)(val * 65536.0f);
125 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
126 fixed = (int32_t) sg_bswap_32((uint32_t)fixed);
128 memcpy(&buf[length], &fixed, sizeof(int32_t));
129 length += sizeof(int32_t);
135 val = _out_message[i].offset +
136 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
138 tmpun32.floatVal = static_cast<float>(val);
140 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
141 tmpun32.intVal = sg_bswap_32(tmpun32.intVal);
143 memcpy(&buf[length], &tmpun32.intVal, sizeof(uint32_t));
144 length += sizeof(uint32_t);
150 val = _out_message[i].offset +
151 _out_message[i].prop->getDoubleValue() * _out_message[i].factor;
153 tmpun64.doubleVal = val;
155 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
156 tmpun64.longVal = sg_bswap_64(tmpun64.longVal);
158 memcpy(&buf[length], &tmpun64.longVal, sizeof(uint64_t));
159 length += sizeof(uint64_t);
163 default: // SG_STRING
164 const char *strdata = _out_message[i].prop->getStringValue();
165 int32_t strlength = strlen(strdata);
167 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
168 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
169 "FG_STRING will be written in host byte order.");
171 /* Format for strings is
172 * [length as int, 4 bytes][ASCII data, length bytes]
174 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
175 strlength = sg_bswap_32(strlength);
177 memcpy(&buf[length], &strlength, sizeof(int32_t));
178 length += sizeof(int32_t);
179 strncpy(&buf[length], strdata, strlength);
181 /* FIXME padding for alignment? Something like:
182 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
188 // add the footer to the packet ("line")
189 switch (binary_footer_type) {
191 binary_footer_value = length;
199 if (binary_footer_type != FOOTER_NONE) {
200 int32_t intValue = binary_footer_value;
201 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
202 intValue = sg_bswap_32(binary_footer_value);
204 memcpy(&buf[length], &intValue, sizeof(int32_t));
205 length += sizeof(int32_t);
211 bool FGGeneric::gen_message_ascii() {
212 string generic_sentence;
217 for (unsigned int i = 0; i < _out_message.size(); i++) {
220 generic_sentence += var_separator;
223 switch (_out_message[i].type) {
225 val = _out_message[i].offset +
226 _out_message[i].prop->getIntValue() * _out_message[i].factor;
227 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
231 snprintf(tmp, 255, _out_message[i].format.c_str(),
232 _out_message[i].prop->getBoolValue());
236 val = _out_message[i].offset +
237 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
238 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
242 val = _out_message[i].offset +
243 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
244 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
248 val = _out_message[i].offset +
249 _out_message[i].prop->getDoubleValue() * _out_message[i].factor;
250 snprintf(tmp, 255, _out_message[i].format.c_str(), (double)val);
253 default: // SG_STRING
254 snprintf(tmp, 255, _out_message[i].format.c_str(),
255 _out_message[i].prop->getStringValue());
258 generic_sentence += tmp;
261 /* After each lot of variables has been added, put the line separator
264 generic_sentence += line_separator;
266 length = generic_sentence.length();
267 strncpy( buf, generic_sentence.c_str(), length );
272 bool FGGeneric::gen_message() {
274 return gen_message_binary();
276 return gen_message_ascii();
280 bool FGGeneric::parse_message_binary(int length) {
286 while ((++i < (int)_in_message.size()) && (p1 < p2)) {
288 switch (_in_message[i].type) {
290 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
291 tmp32 = sg_bswap_32(*(int32_t *)p1);
293 tmp32 = *(int32_t *)p1;
295 updateValue(_in_message[i], (int)tmp32);
296 p1 += sizeof(int32_t);
300 updateValue(_in_message[i], p1[0] != 0);
305 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
306 tmp32 = sg_bswap_32(*(int32_t *)p1);
308 tmp32 = *(int32_t *)p1;
310 updateValue(_in_message[i], (float)tmp32 / 65536.0f);
311 p1 += sizeof(int32_t);
316 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
317 tmpun32.intVal = sg_bswap_32(*(uint32_t *)p1);
319 tmpun32.floatVal = *(float *)p1;
321 updateValue(_in_message[i], tmpun32.floatVal);
322 p1 += sizeof(int32_t);
327 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
328 tmpun64.longVal = sg_bswap_64(*(uint64_t *)p1);
330 tmpun64.doubleVal = *(double *)p1;
332 updateValue(_in_message[i], tmpun64.doubleVal);
333 p1 += sizeof(int64_t);
336 default: // SG_STRING
337 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
338 "Ignoring unsupported binary input chunk type.");
346 bool FGGeneric::parse_message_ascii(int length) {
349 int chunks = _in_message.size();
350 int line_separator_size = line_separator.size();
352 if (length < line_separator_size ||
353 line_separator.compare(buf + length - line_separator_size) != 0) {
355 SG_LOG(SG_IO, SG_WARN,
356 "Input line does not end with expected line separator." );
358 buf[length - line_separator_size] = 0;
361 size_t varsep_len = var_separator.length();
362 while ((++i < chunks) && p1) {
367 p2 = strstr(p1, var_separator.c_str());
374 switch (_in_message[i].type) {
376 updateValue(_in_message[i], atoi(p1));
380 updateValue(_in_message[i], atof(p1) != 0.0);
385 updateValue(_in_message[i], (float)strtod(p1, 0));
389 updateValue(_in_message[i], (double)strtod(p1, 0));
392 default: // SG_STRING
393 _in_message[i].prop->setStringValue(p1);
403 bool FGGeneric::parse_message_len(int length) {
405 return parse_message_binary(length);
407 return parse_message_ascii(length);
412 // open hailing frequencies
413 bool FGGeneric::open() {
414 if ( is_enabled() ) {
415 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
416 << "is already in use, ignoring" );
420 SGIOChannel *io = get_io_channel();
422 if ( ! io->open( get_direction() ) ) {
423 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
429 if ( ((get_direction() == SG_IO_OUT )||
430 (get_direction() == SG_IO_BI))
431 && ! preamble.empty() ) {
432 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
433 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
442 // process work for this port
443 bool FGGeneric::process() {
444 SGIOChannel *io = get_io_channel();
446 if ( (get_direction() == SG_IO_OUT) ||
447 (get_direction() == SG_IO_BI) ) {
449 if ( ! io->write( buf, length ) ) {
450 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
455 if (( get_direction() == SG_IO_IN ) ||
456 (get_direction() == SG_IO_BI) ) {
457 if ( io->get_type() == sgFileType ) {
459 length = io->readline( buf, FG_MAX_MSG_SIZE );
461 parse_message_len( length );
463 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
467 length = io->read( buf, binary_record_length );
468 if ( length == binary_record_length ) {
469 parse_message_len( length );
471 SG_LOG( SG_IO, SG_ALERT,
472 "Generic protocol: Received binary "
473 "record of unexpected size, expected: "
474 << binary_record_length << " but received: "
480 while ((length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
481 parse_message_len( length );
484 while ((length = io->read( buf, binary_record_length ))
485 == binary_record_length ) {
486 parse_message_len( length );
490 SG_LOG( SG_IO, SG_ALERT,
491 "Generic protocol: Received binary "
492 "record of unexpected size, expected: "
493 << binary_record_length << " but received: "
503 return true; // should not get there, but please the compiler
510 bool FGGeneric::close() {
511 SGIOChannel *io = get_io_channel();
513 if ( ((get_direction() == SG_IO_OUT)||
514 (get_direction() == SG_IO_BI))
515 && ! postamble.empty() ) {
516 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
517 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
522 set_enabled( false );
524 if ( ! io->close() ) {
535 SGPath path( globals->get_fg_root() );
536 path.append("Protocol");
537 path.append(file_name.c_str());
539 SG_LOG(SG_NETWORK, SG_INFO, "Reading communication protocol from "
544 readProperties(path.str(), &root);
545 } catch (const sg_exception & ex) {
546 SG_LOG(SG_NETWORK, SG_ALERT,
547 "Unable to load the protocol configuration file: " << ex.getFormattedMessage() );
551 if (direction == "out") {
552 SGPropertyNode *output = root.getNode("generic/output");
554 _out_message.clear();
555 if (!read_config(output, _out_message))
561 } else if (direction == "in") {
562 SGPropertyNode *input = root.getNode("generic/input");
565 if (!read_config(input, _in_message))
570 if (!binary_mode && (line_separator.size() == 0 ||
571 *line_separator.rbegin() != '\n')) {
573 SG_LOG(SG_IO, SG_WARN,
574 "Warning: Appending newline to line separator in generic input.");
575 line_separator.push_back('\n');
585 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
587 binary_mode = root->getBoolValue("binary_mode");
590 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
591 * file for each format
593 * var_sep_string = the string/charachter to place between variables
594 * line_sep_string = the string/charachter to place at the end of each
597 preamble = unescape(root->getStringValue("preamble"));
598 postamble = unescape(root->getStringValue("postamble"));
599 var_sep_string = unescape(root->getStringValue("var_separator"));
600 line_sep_string = unescape(root->getStringValue("line_separator"));
602 if ( var_sep_string == "newline" ) {
603 var_separator = '\n';
604 } else if ( var_sep_string == "tab" ) {
605 var_separator = '\t';
606 } else if ( var_sep_string == "space" ) {
608 } else if ( var_sep_string == "formfeed" ) {
609 var_separator = '\f';
610 } else if ( var_sep_string == "carriagereturn" ) {
611 var_sep_string = '\r';
612 } else if ( var_sep_string == "verticaltab" ) {
613 var_separator = '\v';
615 var_separator = var_sep_string;
618 if ( line_sep_string == "newline" ) {
619 line_separator = '\n';
620 } else if ( line_sep_string == "tab" ) {
621 line_separator = '\t';
622 } else if ( line_sep_string == "space" ) {
623 line_separator = ' ';
624 } else if ( line_sep_string == "formfeed" ) {
625 line_separator = '\f';
626 } else if ( line_sep_string == "carriagereturn" ) {
627 line_separator = '\r';
628 } else if ( line_sep_string == "verticaltab" ) {
629 line_separator = '\v';
631 line_separator = line_sep_string;
634 // default values: no footer and record_length = sizeof(representation)
635 binary_footer_type = FOOTER_NONE;
636 binary_record_length = -1;
638 // default choice is network byte order (big endian)
639 if (sgIsLittleEndian()) {
640 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
642 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
645 if ( root->hasValue("binary_footer") ) {
646 string footer_type = root->getStringValue("binary_footer");
647 if ( footer_type == "length" ) {
648 binary_footer_type = FOOTER_LENGTH;
649 } else if ( footer_type.substr(0, 5) == "magic" ) {
650 binary_footer_type = FOOTER_MAGIC;
651 binary_footer_value = strtol(footer_type.substr(6,
652 footer_type.length() - 6).c_str(), (char**)0, 0);
653 } else if ( footer_type != "none" ) {
654 SG_LOG(SG_IO, SG_ALERT,
655 "generic protocol: Unknown generic binary protocol "
656 "footer '" << footer_type << "', using no footer.");
660 if ( root->hasValue("record_length") ) {
661 binary_record_length = root->getIntValue("record_length");
664 if ( root->hasValue("byte_order") ) {
665 string byte_order = root->getStringValue("byte_order");
666 if (byte_order == "network" ) {
667 if ( sgIsLittleEndian() ) {
668 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
670 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
672 } else if ( byte_order == "host" ) {
673 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
675 SG_LOG(SG_IO, SG_ALERT,
676 "generic protocol: Undefined generic binary protocol"
677 "byte order, using HOST byte order.");
682 int record_length = 0; // Only used for binary protocols.
683 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
685 for (unsigned int i = 0; i < chunks.size(); i++) {
689 // chunk.name = chunks[i]->getStringValue("name");
690 chunk.format = unescape(chunks[i]->getStringValue("format", "%d"));
691 chunk.offset = chunks[i]->getDoubleValue("offset");
692 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
693 chunk.min = chunks[i]->getDoubleValue("min");
694 chunk.max = chunks[i]->getDoubleValue("max");
695 chunk.wrap = chunks[i]->getBoolValue("wrap");
696 chunk.rel = chunks[i]->getBoolValue("relative");
698 string node = chunks[i]->getStringValue("node", "/null");
699 chunk.prop = fgGetNode(node.c_str(), true);
701 string type = chunks[i]->getStringValue("type");
703 // Note: officially the type is called 'bool' but for backward
704 // compatibility 'boolean' will also be supported.
705 if (type == "bool" || type == "boolean") {
706 chunk.type = FG_BOOL;
708 } else if (type == "float") {
709 chunk.type = FG_FLOAT;
710 record_length += sizeof(int32_t);
711 } else if (type == "double") {
712 chunk.type = FG_DOUBLE;
713 record_length += sizeof(int64_t);
714 } else if (type == "fixed") {
715 chunk.type = FG_FIXED;
716 record_length += sizeof(int32_t);
717 } else if (type == "string")
718 chunk.type = FG_STRING;
721 record_length += sizeof(int32_t);
723 msg.push_back(chunk);
729 if ((chunks.size() > 1)&&(var_sep_string.length() == 0))
731 // ASCII protocols really need a separator when there is more than one chunk per line
732 SG_LOG(SG_IO, SG_ALERT,
733 "generic protocol: Invalid configuration. "
734 "'var_separator' must not be empty for protocols which have more than one chunk per line.");
740 if (binary_record_length == -1) {
741 binary_record_length = record_length;
742 } else if (binary_record_length < record_length) {
743 SG_LOG(SG_IO, SG_ALERT,
744 "generic protocol: Requested binary record length shorter than "
745 " requested record representation.");
746 binary_record_length = record_length;
753 void FGGeneric::updateValue(FGGeneric::_serial_prot& prot, bool val)
757 // value inverted if received true, otherwise leave unchanged
759 setValue(prot.prop, !getValue<bool>(prot.prop));
763 setValue(prot.prop, val);