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(vector<string> tokens) : exitOnError(false)
49 if (tokens[1] == "socket") {
51 } else if (tokens[1] == "file") {
57 if (configToken >= tokens.size()) {
58 SG_LOG(SG_GENERAL, SG_ALERT,
59 "Not enough tokens passed for generic protocol");
63 string config = tokens[ configToken ];
64 string file = config+".xml";
66 SGPath path( globals->get_fg_root() );
67 path.append("Protocol");
68 path.append(file.c_str());
69 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
74 readProperties(path.str(), &root);
75 } catch (const sg_exception &) {
76 SG_LOG(SG_GENERAL, SG_ALERT,
77 "Unable to load the protocol configuration file");
81 if (tokens[2] == "out") {
82 SGPropertyNode *output = root.getNode("generic/output");
84 read_config(output, _out_message);
86 } else if (tokens[2] == "in") {
87 SGPropertyNode *input = root.getNode("generic/input");
89 read_config(input, _in_message);
92 SG_LOG(SG_GENERAL, SG_ALERT, "Unsuported protocol direction: "
97 FGGeneric::~FGGeneric() {
110 // generate the message
111 bool FGGeneric::gen_message_binary() {
112 string generic_sentence;
116 for (unsigned int i = 0; i < _out_message.size(); i++) {
118 switch (_out_message[i].type) {
120 val = _out_message[i].offset +
121 _out_message[i].prop->getIntValue() * _out_message[i].factor;
123 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
124 *((int32_t*)&buf[length]) = (int32_t)val;
126 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
128 length += sizeof(int32_t);
132 *((int8_t*)&buf[length])
133 = _out_message[i].prop->getBoolValue() ? true : false;
139 val = _out_message[i].offset +
140 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
142 int fixed = (int)(val * 65536.0f);
143 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
144 *((int32_t*)&buf[length]) = (int32_t)fixed;
146 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)fixed);
148 length += sizeof(int32_t);
152 val = _out_message[i].offset +
153 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
155 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
156 *((float*)&buf[length]) = val;
159 tmpun32.floatVal = static_cast<float>(val);
160 *((uint32_t*)&buf[length]) = sg_bswap_32(tmpun32.intVal);
162 length += sizeof(uint32_t);
166 val = _out_message[i].offset +
167 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
169 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
170 *((double*)&buf[length]) = val;
173 tmpun64.doubleVal = val;
174 *((uint64_t*)&buf[length]) = sg_bswap_64(tmpun64.longVal);
176 length += sizeof(int64_t);
179 default: // SG_STRING
180 const char *strdata = _out_message[i].prop->getStringValue();
181 int strlength = strlen(strdata);
183 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
184 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
185 "FG_STRING will be written in host byte order.");
187 /* Format for strings is
188 * [length as int, 4 bytes][ASCII data, length bytes]
190 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
191 *((int32_t*)&buf[length]) = strlength;
193 *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
195 length += sizeof(int32_t);
196 strncpy(&buf[length], strdata, strlength);
198 /* FIXME padding for alignment? Something like:
199 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
204 // add the footer to the packet ("line")
205 switch (binary_footer_type) {
207 binary_footer_value = length;
215 if (binary_footer_type != FOOTER_NONE) {
216 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
217 *((int32_t*)&buf[length]) = binary_footer_value;
219 *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
221 length += sizeof(int32_t);
227 bool FGGeneric::gen_message_ascii() {
228 string generic_sentence;
233 for (unsigned int i = 0; i < _out_message.size(); i++) {
236 generic_sentence += var_separator;
239 switch (_out_message[i].type) {
241 val = _out_message[i].offset +
242 _out_message[i].prop->getIntValue() * _out_message[i].factor;
243 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
247 snprintf(tmp, 255, _out_message[i].format.c_str(),
248 _out_message[i].prop->getBoolValue());
252 val = _out_message[i].offset +
253 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
254 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
258 val = _out_message[i].offset +
259 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
260 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
264 val = _out_message[i].offset +
265 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
266 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
269 default: // SG_STRING
270 snprintf(tmp, 255, _out_message[i].format.c_str(),
271 _out_message[i].prop->getStringValue());
274 generic_sentence += tmp;
277 /* After each lot of variables has been added, put the line separator
280 generic_sentence += line_separator;
282 length = generic_sentence.length();
283 strncpy( buf, generic_sentence.c_str(), length );
288 bool FGGeneric::gen_message() {
290 return gen_message_binary();
292 return gen_message_ascii();
296 bool FGGeneric::parse_message_binary() {
302 p2 = p1 + FG_MAX_MSG_SIZE;
303 while ((++i < (int)_in_message.size()) && (p1 < p2)) {
305 switch (_in_message[i].type) {
307 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
308 tmp32 = sg_bswap_32(*(int32_t *)p1);
310 tmp32 = *(int32_t *)p1;
313 val = _in_message[i].offset + (double)tmp32 * _in_message[i].factor;
315 _in_message[i].prop->setIntValue((int)val);
316 p1 += sizeof(int32_t);
320 _in_message[i].prop->setBoolValue( p1[0] != 0 );
325 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
326 tmp32 = sg_bswap_32(*(int32_t *)p1);
328 tmp32 = *(int32_t *)p1;
331 val = _in_message[i].offset +
332 ((double)tmp32 / 65536.0f) * _in_message[i].factor;
334 _in_message[i].prop->setFloatValue(val);
335 p1 += sizeof(int32_t);
340 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
341 tmpun32.intVal = sg_bswap_32(*(uint32_t *)p1);
343 tmpun32.floatVal = *(float *)p1;
346 val = _in_message[i].offset +
347 tmpun32.floatVal * _in_message[i].factor;
349 _in_message[i].prop->setFloatValue(val);
350 p1 += sizeof(int32_t);
355 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
356 tmpun64.longVal = sg_bswap_64(*(uint64_t *)p1);
358 tmpun64.doubleVal = *(double *)p1;
361 val = _in_message[i].offset +
362 tmpun64.doubleVal * _in_message[i].factor;
364 _in_message[i].prop->setDoubleValue(val);
365 p1 += sizeof(int64_t);
368 default: // SG_STRING
369 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
370 "Ignoring unsupported binary input chunk type.");
377 bool FGGeneric::parse_message_ascii() {
382 while ((++i < (int)_in_message.size()) &&
383 p1 && strcmp(p1, line_separator.c_str())) {
385 p2 = strstr(p1, var_separator.c_str());
388 p2 += var_separator.length();
391 switch (_in_message[i].type) {
393 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
394 _in_message[i].prop->setIntValue((int)val);
398 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
404 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
405 _in_message[i].prop->setFloatValue((float)val);
408 default: // SG_STRING
409 _in_message[i].prop->setStringValue(p1);
418 bool FGGeneric::parse_message() {
420 return parse_message_binary();
422 return parse_message_ascii();
427 // open hailing frequencies
428 bool FGGeneric::open() {
429 if ( is_enabled() ) {
430 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
431 << "is already in use, ignoring" );
435 SGIOChannel *io = get_io_channel();
437 if ( ! io->open( get_direction() ) ) {
438 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
444 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
445 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
446 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
455 // process work for this port
456 bool FGGeneric::process() {
457 SGIOChannel *io = get_io_channel();
459 if ( get_direction() == SG_IO_OUT ) {
461 if ( ! io->write( buf, length ) ) {
462 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
465 } else if ( get_direction() == SG_IO_IN ) {
466 if ( io->get_type() == sgFileType ) {
468 length = io->readline( buf, FG_MAX_MSG_SIZE );
472 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
476 length = io->read( buf, binary_record_length );
477 if ( length == binary_record_length ) {
480 SG_LOG( SG_IO, SG_ALERT,
481 "Generic protocol: Received binary "
482 "record of unexpected size, expected: "
483 << binary_record_length << " but received: "
490 length = io->readline( buf, FG_MAX_MSG_SIZE );
495 length = io->read( buf, binary_record_length );
496 if ( length == binary_record_length ) {
498 } else if ( length > 0 ) {
499 SG_LOG( SG_IO, SG_ALERT,
500 "Generic protocol: Received binary "
501 "record of unexpected size, expected: "
502 << binary_record_length << " but received: "
506 } while ( length == binary_record_length );
513 return true; // should not get there, but please the compiler
520 bool FGGeneric::close() {
521 SGIOChannel *io = get_io_channel();
523 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
524 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
525 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
530 set_enabled( false );
532 if ( ! io->close() ) {
541 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
543 binary_mode = root->getBoolValue("binary_mode");
546 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
547 * file for each format
549 * var_sep_string = the string/charachter to place between variables
550 * line_sep_string = the string/charachter to place at the end of each
553 preamble = fgUnescape(root->getStringValue("preamble"));
554 postamble = fgUnescape(root->getStringValue("postamble"));
555 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
556 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
558 if ( var_sep_string == "newline" ) {
559 var_separator = '\n';
560 } else if ( var_sep_string == "tab" ) {
561 var_separator = '\t';
562 } else if ( var_sep_string == "space" ) {
564 } else if ( var_sep_string == "formfeed" ) {
565 var_separator = '\f';
566 } else if ( var_sep_string == "carriagereturn" ) {
567 var_sep_string = '\r';
568 } else if ( var_sep_string == "verticaltab" ) {
569 var_separator = '\v';
571 var_separator = var_sep_string;
574 if ( line_sep_string == "newline" ) {
575 line_separator = '\n';
576 } else if ( line_sep_string == "tab" ) {
577 line_separator = '\t';
578 } else if ( line_sep_string == "space" ) {
579 line_separator = ' ';
580 } else if ( line_sep_string == "formfeed" ) {
581 line_separator = '\f';
582 } else if ( line_sep_string == "carriagereturn" ) {
583 line_separator = '\r';
584 } else if ( line_sep_string == "verticaltab" ) {
585 line_separator = '\v';
587 line_separator = line_sep_string;
590 // default values: no footer and record_length = sizeof(representation)
591 binary_footer_type = FOOTER_NONE;
592 binary_record_length = -1;
594 // default choice is network byte order (big endian)
595 if (sgIsLittleEndian()) {
596 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
598 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
601 if ( root->hasValue("binary_footer") ) {
602 string footer_type = root->getStringValue("binary_footer");
603 if ( footer_type == "length" ) {
604 binary_footer_type = FOOTER_LENGTH;
605 } else if ( footer_type.substr(0, 5) == "magic" ) {
606 binary_footer_type = FOOTER_MAGIC;
607 binary_footer_value = strtol(footer_type.substr(6,
608 footer_type.length() - 6).c_str(), (char**)0, 0);
609 } else if ( footer_type != "none" ) {
610 SG_LOG(SG_IO, SG_ALERT,
611 "generic protocol: Unknown generic binary protocol "
612 "footer '" << footer_type << "', using no footer.");
616 if ( root->hasValue("record_length") ) {
617 binary_record_length = root->getIntValue("record_length");
620 if ( root->hasValue("byte_order") ) {
621 string byte_order = root->getStringValue("byte_order");
622 if (byte_order == "network" ) {
623 if ( sgIsLittleEndian() ) {
624 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
626 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
628 } else if ( byte_order == "host" ) {
629 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
631 SG_LOG(SG_IO, SG_ALERT,
632 "generic protocol: Undefined generic binary protocol"
633 "byte order, using HOST byte order.");
638 int record_length = 0; // Only used for binary protocols.
639 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
640 for (unsigned int i = 0; i < chunks.size(); i++) {
644 // chunk.name = chunks[i]->getStringValue("name");
645 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
646 chunk.offset = chunks[i]->getDoubleValue("offset");
647 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
649 string node = chunks[i]->getStringValue("node", "/null");
650 chunk.prop = fgGetNode(node.c_str(), true);
652 string type = chunks[i]->getStringValue("type");
654 // Note: officially the type is called 'bool' but for backward
655 // compatibility 'boolean' will also be supported.
656 if (type == "bool" || type == "boolean") {
657 chunk.type = FG_BOOL;
659 } else if (type == "float") {
660 chunk.type = FG_FLOAT;
661 record_length += sizeof(int32_t);
662 } else if (type == "double") {
663 chunk.type = FG_DOUBLE;
664 record_length += sizeof(int64_t);
665 } else if (type == "fixed") {
666 chunk.type = FG_FIXED;
667 record_length += sizeof(int32_t);
668 } else if (type == "string")
669 chunk.type = FG_STRING;
672 record_length += sizeof(int32_t);
674 msg.push_back(chunk);
678 if (binary_record_length == -1) {
679 binary_record_length = record_length;
680 } else if (binary_record_length < record_length) {
681 SG_LOG(SG_IO, SG_ALERT,
682 "generic protocol: Requested binary record length shorter than "
683 " requested record representation.");
684 binary_record_length = record_length;