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 file_name = config+".xml";
65 direction = tokens[2];
67 if (direction != "in" && direction != "out") {
68 SG_LOG(SG_GENERAL, SG_ALERT, "Unsuported protocol direction: "
75 FGGeneric::~FGGeneric() {
88 // generate the message
89 bool FGGeneric::gen_message_binary() {
90 string generic_sentence;
94 for (unsigned int i = 0; i < _out_message.size(); i++) {
96 switch (_out_message[i].type) {
98 val = _out_message[i].offset +
99 _out_message[i].prop->getIntValue() * _out_message[i].factor;
101 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
102 *((int32_t*)&buf[length]) = (int32_t)val;
104 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
106 length += sizeof(int32_t);
110 *((int8_t*)&buf[length])
111 = _out_message[i].prop->getBoolValue() ? true : false;
117 val = _out_message[i].offset +
118 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
120 int fixed = (int)(val * 65536.0f);
121 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
122 *((int32_t*)&buf[length]) = (int32_t)fixed;
124 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)fixed);
126 length += sizeof(int32_t);
130 val = _out_message[i].offset +
131 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
133 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
134 *((float*)&buf[length]) = val;
137 tmpun32.floatVal = static_cast<float>(val);
138 *((uint32_t*)&buf[length]) = sg_bswap_32(tmpun32.intVal);
140 length += sizeof(uint32_t);
144 val = _out_message[i].offset +
145 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
147 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
148 *((double*)&buf[length]) = val;
151 tmpun64.doubleVal = val;
152 *((uint64_t*)&buf[length]) = sg_bswap_64(tmpun64.longVal);
154 length += sizeof(int64_t);
157 default: // SG_STRING
158 const char *strdata = _out_message[i].prop->getStringValue();
159 int strlength = strlen(strdata);
161 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
162 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
163 "FG_STRING will be written in host byte order.");
165 /* Format for strings is
166 * [length as int, 4 bytes][ASCII data, length bytes]
168 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
169 *((int32_t*)&buf[length]) = strlength;
171 *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
173 length += sizeof(int32_t);
174 strncpy(&buf[length], strdata, strlength);
176 /* FIXME padding for alignment? Something like:
177 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
182 // add the footer to the packet ("line")
183 switch (binary_footer_type) {
185 binary_footer_value = length;
193 if (binary_footer_type != FOOTER_NONE) {
194 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
195 *((int32_t*)&buf[length]) = binary_footer_value;
197 *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
199 length += sizeof(int32_t);
205 bool FGGeneric::gen_message_ascii() {
206 string generic_sentence;
211 for (unsigned int i = 0; i < _out_message.size(); i++) {
214 generic_sentence += var_separator;
217 switch (_out_message[i].type) {
219 val = _out_message[i].offset +
220 _out_message[i].prop->getIntValue() * _out_message[i].factor;
221 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
225 snprintf(tmp, 255, _out_message[i].format.c_str(),
226 _out_message[i].prop->getBoolValue());
230 val = _out_message[i].offset +
231 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
232 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
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->getDoubleValue() * _out_message[i].factor;
244 snprintf(tmp, 255, _out_message[i].format.c_str(), (double)val);
247 default: // SG_STRING
248 snprintf(tmp, 255, _out_message[i].format.c_str(),
249 _out_message[i].prop->getStringValue());
252 generic_sentence += tmp;
255 /* After each lot of variables has been added, put the line separator
258 generic_sentence += line_separator;
260 length = generic_sentence.length();
261 strncpy( buf, generic_sentence.c_str(), length );
266 bool FGGeneric::gen_message() {
268 return gen_message_binary();
270 return gen_message_ascii();
274 bool FGGeneric::parse_message_binary() {
280 p2 = p1 + FG_MAX_MSG_SIZE;
281 while ((++i < (int)_in_message.size()) && (p1 < p2)) {
283 switch (_in_message[i].type) {
285 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
286 tmp32 = sg_bswap_32(*(int32_t *)p1);
288 tmp32 = *(int32_t *)p1;
291 val = _in_message[i].offset + (double)tmp32 * _in_message[i].factor;
293 _in_message[i].prop->setIntValue((int)val);
294 p1 += sizeof(int32_t);
298 _in_message[i].prop->setBoolValue( p1[0] != 0 );
303 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
304 tmp32 = sg_bswap_32(*(int32_t *)p1);
306 tmp32 = *(int32_t *)p1;
309 val = _in_message[i].offset +
310 ((double)tmp32 / 65536.0f) * _in_message[i].factor;
312 _in_message[i].prop->setFloatValue(val);
313 p1 += sizeof(int32_t);
318 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
319 tmpun32.intVal = sg_bswap_32(*(uint32_t *)p1);
321 tmpun32.floatVal = *(float *)p1;
324 val = _in_message[i].offset +
325 tmpun32.floatVal * _in_message[i].factor;
327 _in_message[i].prop->setFloatValue(val);
328 p1 += sizeof(int32_t);
333 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
334 tmpun64.longVal = sg_bswap_64(*(uint64_t *)p1);
336 tmpun64.doubleVal = *(double *)p1;
339 val = _in_message[i].offset +
340 tmpun64.doubleVal * _in_message[i].factor;
342 _in_message[i].prop->setDoubleValue(val);
343 p1 += sizeof(int64_t);
346 default: // SG_STRING
347 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
348 "Ignoring unsupported binary input chunk type.");
355 bool FGGeneric::parse_message_ascii() {
360 while ((++i < (int)_in_message.size()) &&
361 p1 && strcmp(p1, line_separator.c_str())) {
363 p2 = strstr(p1, var_separator.c_str());
366 p2 += var_separator.length();
369 switch (_in_message[i].type) {
371 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
372 _in_message[i].prop->setIntValue((int)val);
376 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
381 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
382 _in_message[i].prop->setFloatValue((float)val);
386 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
387 _in_message[i].prop->setDoubleValue(val);
390 default: // SG_STRING
391 _in_message[i].prop->setStringValue(p1);
400 bool FGGeneric::parse_message() {
402 return parse_message_binary();
404 return parse_message_ascii();
409 // open hailing frequencies
410 bool FGGeneric::open() {
411 if ( is_enabled() ) {
412 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
413 << "is already in use, ignoring" );
417 SGIOChannel *io = get_io_channel();
419 if ( ! io->open( get_direction() ) ) {
420 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
426 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
427 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
428 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
437 // process work for this port
438 bool FGGeneric::process() {
439 SGIOChannel *io = get_io_channel();
441 if ( get_direction() == SG_IO_OUT ) {
443 if ( ! io->write( buf, length ) ) {
444 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
447 } else if ( get_direction() == SG_IO_IN ) {
448 if ( io->get_type() == sgFileType ) {
450 length = io->readline( buf, FG_MAX_MSG_SIZE );
454 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
458 length = io->read( buf, binary_record_length );
459 if ( length == binary_record_length ) {
462 SG_LOG( SG_IO, SG_ALERT,
463 "Generic protocol: Received binary "
464 "record of unexpected size, expected: "
465 << binary_record_length << " but received: "
471 while ((length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
475 while ((length = io->read( buf, binary_record_length ))
476 == binary_record_length ) {
481 SG_LOG( SG_IO, SG_ALERT,
482 "Generic protocol: Received binary "
483 "record of unexpected size, expected: "
484 << binary_record_length << " but received: "
494 return true; // should not get there, but please the compiler
501 bool FGGeneric::close() {
502 SGIOChannel *io = get_io_channel();
504 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
505 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
506 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
511 set_enabled( false );
513 if ( ! io->close() ) {
524 SGPath path( globals->get_fg_root() );
525 path.append("Protocol");
526 path.append(file_name.c_str());
528 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
533 readProperties(path.str(), &root);
534 } catch (const sg_exception &) {
535 SG_LOG(SG_GENERAL, SG_ALERT,
536 "Unable to load the protocol configuration file");
540 if (direction == "out") {
541 SGPropertyNode *output = root.getNode("generic/output");
543 _out_message.clear();
544 read_config(output, _out_message);
546 } else if (direction == "in") {
547 SGPropertyNode *input = root.getNode("generic/input");
550 read_config(input, _in_message);
557 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
559 binary_mode = root->getBoolValue("binary_mode");
562 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
563 * file for each format
565 * var_sep_string = the string/charachter to place between variables
566 * line_sep_string = the string/charachter to place at the end of each
569 preamble = fgUnescape(root->getStringValue("preamble"));
570 postamble = fgUnescape(root->getStringValue("postamble"));
571 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
572 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
574 if ( var_sep_string == "newline" ) {
575 var_separator = '\n';
576 } else if ( var_sep_string == "tab" ) {
577 var_separator = '\t';
578 } else if ( var_sep_string == "space" ) {
580 } else if ( var_sep_string == "formfeed" ) {
581 var_separator = '\f';
582 } else if ( var_sep_string == "carriagereturn" ) {
583 var_sep_string = '\r';
584 } else if ( var_sep_string == "verticaltab" ) {
585 var_separator = '\v';
587 var_separator = var_sep_string;
590 if ( line_sep_string == "newline" ) {
591 line_separator = '\n';
592 } else if ( line_sep_string == "tab" ) {
593 line_separator = '\t';
594 } else if ( line_sep_string == "space" ) {
595 line_separator = ' ';
596 } else if ( line_sep_string == "formfeed" ) {
597 line_separator = '\f';
598 } else if ( line_sep_string == "carriagereturn" ) {
599 line_separator = '\r';
600 } else if ( line_sep_string == "verticaltab" ) {
601 line_separator = '\v';
603 line_separator = line_sep_string;
606 // default values: no footer and record_length = sizeof(representation)
607 binary_footer_type = FOOTER_NONE;
608 binary_record_length = -1;
610 // default choice is network byte order (big endian)
611 if (sgIsLittleEndian()) {
612 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
614 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
617 if ( root->hasValue("binary_footer") ) {
618 string footer_type = root->getStringValue("binary_footer");
619 if ( footer_type == "length" ) {
620 binary_footer_type = FOOTER_LENGTH;
621 } else if ( footer_type.substr(0, 5) == "magic" ) {
622 binary_footer_type = FOOTER_MAGIC;
623 binary_footer_value = strtol(footer_type.substr(6,
624 footer_type.length() - 6).c_str(), (char**)0, 0);
625 } else if ( footer_type != "none" ) {
626 SG_LOG(SG_IO, SG_ALERT,
627 "generic protocol: Unknown generic binary protocol "
628 "footer '" << footer_type << "', using no footer.");
632 if ( root->hasValue("record_length") ) {
633 binary_record_length = root->getIntValue("record_length");
636 if ( root->hasValue("byte_order") ) {
637 string byte_order = root->getStringValue("byte_order");
638 if (byte_order == "network" ) {
639 if ( sgIsLittleEndian() ) {
640 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
642 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
644 } else if ( byte_order == "host" ) {
645 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
647 SG_LOG(SG_IO, SG_ALERT,
648 "generic protocol: Undefined generic binary protocol"
649 "byte order, using HOST byte order.");
654 int record_length = 0; // Only used for binary protocols.
655 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
656 for (unsigned int i = 0; i < chunks.size(); i++) {
660 // chunk.name = chunks[i]->getStringValue("name");
661 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
662 chunk.offset = chunks[i]->getDoubleValue("offset");
663 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
665 string node = chunks[i]->getStringValue("node", "/null");
666 chunk.prop = fgGetNode(node.c_str(), true);
668 string type = chunks[i]->getStringValue("type");
670 // Note: officially the type is called 'bool' but for backward
671 // compatibility 'boolean' will also be supported.
672 if (type == "bool" || type == "boolean") {
673 chunk.type = FG_BOOL;
675 } else if (type == "float") {
676 chunk.type = FG_FLOAT;
677 record_length += sizeof(int32_t);
678 } else if (type == "double") {
679 chunk.type = FG_DOUBLE;
680 record_length += sizeof(int64_t);
681 } else if (type == "fixed") {
682 chunk.type = FG_FIXED;
683 record_length += sizeof(int32_t);
684 } else if (type == "string")
685 chunk.type = FG_STRING;
688 record_length += sizeof(int32_t);
690 msg.push_back(chunk);
695 if (binary_record_length == -1) {
696 binary_record_length = record_length;
697 } else if (binary_record_length < record_length) {
698 SG_LOG(SG_IO, SG_ALERT,
699 "generic protocol: Requested binary record length shorter than "
700 " requested record representation.");
701 binary_record_length = record_length;