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";
66 if (tokens[2] != "in" && tokens[2] != "out") {
67 SG_LOG(SG_GENERAL, SG_ALERT, "Unsuported protocol direction: "
74 FGGeneric::~FGGeneric() {
87 // generate the message
88 bool FGGeneric::gen_message_binary() {
89 string generic_sentence;
93 for (unsigned int i = 0; i < _out_message.size(); i++) {
95 switch (_out_message[i].type) {
97 val = _out_message[i].offset +
98 _out_message[i].prop->getIntValue() * _out_message[i].factor;
100 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
101 *((int32_t*)&buf[length]) = (int32_t)val;
103 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
105 length += sizeof(int32_t);
109 *((int8_t*)&buf[length])
110 = _out_message[i].prop->getBoolValue() ? true : false;
116 val = _out_message[i].offset +
117 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
119 int fixed = (int)(val * 65536.0f);
120 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
121 *((int32_t*)&buf[length]) = (int32_t)fixed;
123 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)fixed);
125 length += sizeof(int32_t);
129 val = _out_message[i].offset +
130 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
132 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
133 *((float*)&buf[length]) = val;
136 tmpun32.floatVal = static_cast<float>(val);
137 *((uint32_t*)&buf[length]) = sg_bswap_32(tmpun32.intVal);
139 length += sizeof(uint32_t);
143 val = _out_message[i].offset +
144 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
146 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
147 *((double*)&buf[length]) = val;
150 tmpun64.doubleVal = val;
151 *((uint64_t*)&buf[length]) = sg_bswap_64(tmpun64.longVal);
153 length += sizeof(int64_t);
156 default: // SG_STRING
157 const char *strdata = _out_message[i].prop->getStringValue();
158 int strlength = strlen(strdata);
160 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
161 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
162 "FG_STRING will be written in host byte order.");
164 /* Format for strings is
165 * [length as int, 4 bytes][ASCII data, length bytes]
167 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
168 *((int32_t*)&buf[length]) = strlength;
170 *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
172 length += sizeof(int32_t);
173 strncpy(&buf[length], strdata, strlength);
175 /* FIXME padding for alignment? Something like:
176 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
181 // add the footer to the packet ("line")
182 switch (binary_footer_type) {
184 binary_footer_value = length;
192 if (binary_footer_type != FOOTER_NONE) {
193 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
194 *((int32_t*)&buf[length]) = binary_footer_value;
196 *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
198 length += sizeof(int32_t);
204 bool FGGeneric::gen_message_ascii() {
205 string generic_sentence;
210 for (unsigned int i = 0; i < _out_message.size(); i++) {
213 generic_sentence += var_separator;
216 switch (_out_message[i].type) {
218 val = _out_message[i].offset +
219 _out_message[i].prop->getIntValue() * _out_message[i].factor;
220 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
224 snprintf(tmp, 255, _out_message[i].format.c_str(),
225 _out_message[i].prop->getBoolValue());
229 val = _out_message[i].offset +
230 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
231 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
235 val = _out_message[i].offset +
236 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
237 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
241 val = _out_message[i].offset +
242 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
243 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
246 default: // SG_STRING
247 snprintf(tmp, 255, _out_message[i].format.c_str(),
248 _out_message[i].prop->getStringValue());
251 generic_sentence += tmp;
254 /* After each lot of variables has been added, put the line separator
257 generic_sentence += line_separator;
259 length = generic_sentence.length();
260 strncpy( buf, generic_sentence.c_str(), length );
265 bool FGGeneric::gen_message() {
267 return gen_message_binary();
269 return gen_message_ascii();
273 bool FGGeneric::parse_message_binary() {
279 p2 = p1 + FG_MAX_MSG_SIZE;
280 while ((++i < (int)_in_message.size()) && (p1 < p2)) {
282 switch (_in_message[i].type) {
284 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
285 tmp32 = sg_bswap_32(*(int32_t *)p1);
287 tmp32 = *(int32_t *)p1;
290 val = _in_message[i].offset + (double)tmp32 * _in_message[i].factor;
292 _in_message[i].prop->setIntValue((int)val);
293 p1 += sizeof(int32_t);
297 _in_message[i].prop->setBoolValue( p1[0] != 0 );
302 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
303 tmp32 = sg_bswap_32(*(int32_t *)p1);
305 tmp32 = *(int32_t *)p1;
308 val = _in_message[i].offset +
309 ((double)tmp32 / 65536.0f) * _in_message[i].factor;
311 _in_message[i].prop->setFloatValue(val);
312 p1 += sizeof(int32_t);
317 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
318 tmpun32.intVal = sg_bswap_32(*(uint32_t *)p1);
320 tmpun32.floatVal = *(float *)p1;
323 val = _in_message[i].offset +
324 tmpun32.floatVal * _in_message[i].factor;
326 _in_message[i].prop->setFloatValue(val);
327 p1 += sizeof(int32_t);
332 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
333 tmpun64.longVal = sg_bswap_64(*(uint64_t *)p1);
335 tmpun64.doubleVal = *(double *)p1;
338 val = _in_message[i].offset +
339 tmpun64.doubleVal * _in_message[i].factor;
341 _in_message[i].prop->setDoubleValue(val);
342 p1 += sizeof(int64_t);
345 default: // SG_STRING
346 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
347 "Ignoring unsupported binary input chunk type.");
354 bool FGGeneric::parse_message_ascii() {
359 while ((++i < (int)_in_message.size()) &&
360 p1 && strcmp(p1, line_separator.c_str())) {
362 p2 = strstr(p1, var_separator.c_str());
365 p2 += var_separator.length();
368 switch (_in_message[i].type) {
370 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
371 _in_message[i].prop->setIntValue((int)val);
375 _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);
385 default: // SG_STRING
386 _in_message[i].prop->setStringValue(p1);
395 bool FGGeneric::parse_message() {
397 return parse_message_binary();
399 return parse_message_ascii();
404 // open hailing frequencies
405 bool FGGeneric::open() {
406 if ( is_enabled() ) {
407 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
408 << "is already in use, ignoring" );
412 SGIOChannel *io = get_io_channel();
414 if ( ! io->open( get_direction() ) ) {
415 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
421 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
422 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
423 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
432 // process work for this port
433 bool FGGeneric::process() {
434 SGIOChannel *io = get_io_channel();
436 if ( get_direction() == SG_IO_OUT ) {
438 if ( ! io->write( buf, length ) ) {
439 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
442 } else if ( get_direction() == SG_IO_IN ) {
443 if ( io->get_type() == sgFileType ) {
445 length = io->readline( buf, FG_MAX_MSG_SIZE );
449 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
453 length = io->read( buf, binary_record_length );
454 if ( length == binary_record_length ) {
457 SG_LOG( SG_IO, SG_ALERT,
458 "Generic protocol: Received binary "
459 "record of unexpected size, expected: "
460 << binary_record_length << " but received: "
467 length = io->readline( buf, FG_MAX_MSG_SIZE );
472 length = io->read( buf, binary_record_length );
473 if ( length == binary_record_length ) {
475 } else if ( length > 0 ) {
476 SG_LOG( SG_IO, SG_ALERT,
477 "Generic protocol: Received binary "
478 "record of unexpected size, expected: "
479 << binary_record_length << " but received: "
483 } while ( length == binary_record_length );
490 return true; // should not get there, but please the compiler
497 bool FGGeneric::close() {
498 SGIOChannel *io = get_io_channel();
500 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
501 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
502 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
507 set_enabled( false );
509 if ( ! io->close() ) {
520 SGPath path( globals->get_fg_root() );
521 path.append("Protocol");
522 path.append(file_name.c_str());
524 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
529 readProperties(path.str(), &root);
530 } catch (const sg_exception &) {
531 SG_LOG(SG_GENERAL, SG_ALERT,
532 "Unable to load the protocol configuration file");
536 SGPropertyNode *output = root.getNode("generic/output");
538 read_config(output, _out_message);
541 SGPropertyNode *input = root.getNode("generic/input");
543 read_config(input, _in_message);
549 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
551 binary_mode = root->getBoolValue("binary_mode");
554 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
555 * file for each format
557 * var_sep_string = the string/charachter to place between variables
558 * line_sep_string = the string/charachter to place at the end of each
561 preamble = fgUnescape(root->getStringValue("preamble"));
562 postamble = fgUnescape(root->getStringValue("postamble"));
563 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
564 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
566 if ( var_sep_string == "newline" ) {
567 var_separator = '\n';
568 } else if ( var_sep_string == "tab" ) {
569 var_separator = '\t';
570 } else if ( var_sep_string == "space" ) {
572 } else if ( var_sep_string == "formfeed" ) {
573 var_separator = '\f';
574 } else if ( var_sep_string == "carriagereturn" ) {
575 var_sep_string = '\r';
576 } else if ( var_sep_string == "verticaltab" ) {
577 var_separator = '\v';
579 var_separator = var_sep_string;
582 if ( line_sep_string == "newline" ) {
583 line_separator = '\n';
584 } else if ( line_sep_string == "tab" ) {
585 line_separator = '\t';
586 } else if ( line_sep_string == "space" ) {
587 line_separator = ' ';
588 } else if ( line_sep_string == "formfeed" ) {
589 line_separator = '\f';
590 } else if ( line_sep_string == "carriagereturn" ) {
591 line_separator = '\r';
592 } else if ( line_sep_string == "verticaltab" ) {
593 line_separator = '\v';
595 line_separator = line_sep_string;
598 // default values: no footer and record_length = sizeof(representation)
599 binary_footer_type = FOOTER_NONE;
600 binary_record_length = -1;
602 // default choice is network byte order (big endian)
603 if (sgIsLittleEndian()) {
604 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
606 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
609 if ( root->hasValue("binary_footer") ) {
610 string footer_type = root->getStringValue("binary_footer");
611 if ( footer_type == "length" ) {
612 binary_footer_type = FOOTER_LENGTH;
613 } else if ( footer_type.substr(0, 5) == "magic" ) {
614 binary_footer_type = FOOTER_MAGIC;
615 binary_footer_value = strtol(footer_type.substr(6,
616 footer_type.length() - 6).c_str(), (char**)0, 0);
617 } else if ( footer_type != "none" ) {
618 SG_LOG(SG_IO, SG_ALERT,
619 "generic protocol: Unknown generic binary protocol "
620 "footer '" << footer_type << "', using no footer.");
624 if ( root->hasValue("record_length") ) {
625 binary_record_length = root->getIntValue("record_length");
628 if ( root->hasValue("byte_order") ) {
629 string byte_order = root->getStringValue("byte_order");
630 if (byte_order == "network" ) {
631 if ( sgIsLittleEndian() ) {
632 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
634 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
636 } else if ( byte_order == "host" ) {
637 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
639 SG_LOG(SG_IO, SG_ALERT,
640 "generic protocol: Undefined generic binary protocol"
641 "byte order, using HOST byte order.");
646 int record_length = 0; // Only used for binary protocols.
647 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
648 for (unsigned int i = 0; i < chunks.size(); i++) {
652 // chunk.name = chunks[i]->getStringValue("name");
653 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
654 chunk.offset = chunks[i]->getDoubleValue("offset");
655 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
657 string node = chunks[i]->getStringValue("node", "/null");
658 chunk.prop = fgGetNode(node.c_str(), true);
660 string type = chunks[i]->getStringValue("type");
662 // Note: officially the type is called 'bool' but for backward
663 // compatibility 'boolean' will also be supported.
664 if (type == "bool" || type == "boolean") {
665 chunk.type = FG_BOOL;
667 } else if (type == "float") {
668 chunk.type = FG_FLOAT;
669 record_length += sizeof(int32_t);
670 } else if (type == "double") {
671 chunk.type = FG_DOUBLE;
672 record_length += sizeof(int64_t);
673 } else if (type == "fixed") {
674 chunk.type = FG_FIXED;
675 record_length += sizeof(int32_t);
676 } else if (type == "string")
677 chunk.type = FG_STRING;
680 record_length += sizeof(int32_t);
682 msg.push_back(chunk);
686 if (binary_record_length == -1) {
687 binary_record_length = record_length;
688 } else if (binary_record_length < record_length) {
689 SG_LOG(SG_IO, SG_ALERT,
690 "generic protocol: Requested binary record length shorter than "
691 " requested record representation.");
692 binary_record_length = record_length;