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)val);
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 );
403 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
404 _in_message[i].prop->setFloatValue((float)val);
407 default: // SG_STRING
408 _in_message[i].prop->setStringValue(p1);
417 bool FGGeneric::parse_message() {
419 return parse_message_binary();
421 return parse_message_ascii();
426 // open hailing frequencies
427 bool FGGeneric::open() {
428 if ( is_enabled() ) {
429 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
430 << "is already in use, ignoring" );
434 SGIOChannel *io = get_io_channel();
436 if ( ! io->open( get_direction() ) ) {
437 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
443 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
444 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
445 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
454 // process work for this port
455 bool FGGeneric::process() {
456 SGIOChannel *io = get_io_channel();
458 if ( get_direction() == SG_IO_OUT ) {
460 if ( ! io->write( buf, length ) ) {
461 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
464 } else if ( get_direction() == SG_IO_IN ) {
465 if ( io->get_type() == sgFileType ) {
467 length = io->readline( buf, FG_MAX_MSG_SIZE );
471 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
475 length = io->read( buf, binary_record_length );
476 if ( length == binary_record_length ) {
479 SG_LOG( SG_IO, SG_ALERT,
480 "Generic protocol: Received binary "
481 "record of unexpected size, expected: "
482 << binary_record_length << " but received: "
489 length = io->readline( buf, FG_MAX_MSG_SIZE );
494 length = io->read( buf, binary_record_length );
495 if ( length == binary_record_length ) {
497 } else if ( length > 0 ) {
498 SG_LOG( SG_IO, SG_ALERT,
499 "Generic protocol: Received binary "
500 "record of unexpected size, expected: "
501 << binary_record_length << " but received: "
505 } while ( length == binary_record_length );
512 return true; // should not get there, but please the compiler
519 bool FGGeneric::close() {
520 SGIOChannel *io = get_io_channel();
522 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
523 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
524 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
529 set_enabled( false );
531 if ( ! io->close() ) {
540 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
542 binary_mode = root->getBoolValue("binary_mode");
545 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
546 * file for each format
548 * var_sep_string = the string/charachter to place between variables
549 * line_sep_string = the string/charachter to place at the end of each
552 preamble = fgUnescape(root->getStringValue("preamble"));
553 postamble = fgUnescape(root->getStringValue("postamble"));
554 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
555 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
557 if ( var_sep_string == "newline" ) {
558 var_separator = '\n';
559 } else if ( var_sep_string == "tab" ) {
560 var_separator = '\t';
561 } else if ( var_sep_string == "space" ) {
563 } else if ( var_sep_string == "formfeed" ) {
564 var_separator = '\f';
565 } else if ( var_sep_string == "carriagereturn" ) {
566 var_sep_string = '\r';
567 } else if ( var_sep_string == "verticaltab" ) {
568 var_separator = '\v';
570 var_separator = var_sep_string;
573 if ( line_sep_string == "newline" ) {
574 line_separator = '\n';
575 } else if ( line_sep_string == "tab" ) {
576 line_separator = '\t';
577 } else if ( line_sep_string == "space" ) {
578 line_separator = ' ';
579 } else if ( line_sep_string == "formfeed" ) {
580 line_separator = '\f';
581 } else if ( line_sep_string == "carriagereturn" ) {
582 line_separator = '\r';
583 } else if ( line_sep_string == "verticaltab" ) {
584 line_separator = '\v';
586 line_separator = line_sep_string;
589 // default values: no footer and record_length = sizeof(representation)
590 binary_footer_type = FOOTER_NONE;
591 binary_record_length = -1;
593 // default choice is network byte order (big endian)
594 if (sgIsLittleEndian()) {
595 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
597 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
600 if ( root->hasValue("binary_footer") ) {
601 string footer_type = root->getStringValue("binary_footer");
602 if ( footer_type == "length" ) {
603 binary_footer_type = FOOTER_LENGTH;
604 } else if ( footer_type.substr(0, 5) == "magic" ) {
605 binary_footer_type = FOOTER_MAGIC;
606 binary_footer_value = strtol(footer_type.substr(6,
607 footer_type.length() - 6).c_str(), (char**)0, 0);
608 } else if ( footer_type != "none" ) {
609 SG_LOG(SG_IO, SG_ALERT,
610 "generic protocol: Unknown generic binary protocol "
611 "footer '" << footer_type << "', using no footer.");
615 if ( root->hasValue("record_length") ) {
616 binary_record_length = root->getIntValue("record_length");
619 if ( root->hasValue("byte_order") ) {
620 string byte_order = root->getStringValue("byte_order");
621 if (byte_order == "network" ) {
622 if ( sgIsLittleEndian() ) {
623 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
625 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
627 } else if ( byte_order == "host" ) {
628 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
630 SG_LOG(SG_IO, SG_ALERT,
631 "generic protocol: Undefined generic binary protocol"
632 "byte order, using HOST byte order.");
637 int record_length = 0; // Only used for binary protocols.
638 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
639 for (unsigned int i = 0; i < chunks.size(); i++) {
643 // chunk.name = chunks[i]->getStringValue("name");
644 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
645 chunk.offset = chunks[i]->getDoubleValue("offset");
646 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
648 string node = chunks[i]->getStringValue("node", "/null");
649 chunk.prop = fgGetNode(node.c_str(), true);
651 string type = chunks[i]->getStringValue("type");
653 // Note: officially the type is called 'bool' but for backward
654 // compatibility 'boolean' will also be supported.
655 if (type == "bool" || type == "boolean") {
656 chunk.type = FG_BOOL;
658 } else if (type == "float") {
659 chunk.type = FG_FLOAT;
660 record_length += sizeof(int32_t);
661 } else if (type == "double") {
662 chunk.type = FG_DOUBLE;
663 record_length += sizeof(int64_t);
664 } else if (type == "fixed") {
665 chunk.type = FG_FIXED;
666 record_length += sizeof(int32_t);
667 } else if (type == "string")
668 chunk.type = FG_STRING;
671 record_length += sizeof(int32_t);
673 msg.push_back(chunk);
677 if (binary_record_length == -1) {
678 binary_record_length = record_length;
679 } else if (binary_record_length < record_length) {
680 SG_LOG(SG_IO, SG_ALERT,
681 "generic protocol: Requested binary record length shorter than "
682 " requested record representation.");
683 binary_record_length = record_length;