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() {
101 // generate the message
102 bool FGGeneric::gen_message_binary() {
103 string generic_sentence;
107 for (unsigned int i = 0; i < _out_message.size(); i++) {
109 switch (_out_message[i].type) {
111 val = _out_message[i].offset +
112 _out_message[i].prop->getIntValue() * _out_message[i].factor;
114 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
115 *((int32_t*)&buf[length]) = (int32_t)val;
117 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
119 length += sizeof(int32_t);
123 *((int8_t*)&buf[length])
124 = _out_message[i].prop->getBoolValue() ? true : false;
130 val = _out_message[i].offset +
131 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
133 int fixed = (int)(val * 65536.0f);
134 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
135 *((int32_t*)&buf[length]) = (int32_t)fixed;
137 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
139 length += sizeof(int32_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 *((float*)&buf[length]) = val;
149 *((float*)&buf[length]) = sg_bswap_32(*(uint32_t*)&val);
151 length += sizeof(int32_t);
155 val = _out_message[i].offset +
156 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
158 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
159 *((double*)&buf[length]) = val;
161 *((double*)&buf[length]) = sg_bswap_64(*(uint64_t*)&val);
163 length += sizeof(int64_t);
166 default: // SG_STRING
167 const char *strdata = _out_message[i].prop->getStringValue();
168 int strlength = strlen(strdata);
170 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
171 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
172 "FG_STRING will be written in host byte order.");
174 /* Format for strings is
175 * [length as int, 4 bytes][ASCII data, length bytes]
177 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
178 *((int32_t*)&buf[length]) = strlength;
180 *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
182 length += sizeof(int32_t);
183 strncpy(&buf[length], strdata, strlength);
185 /* FIXME padding for alignment? Something like:
186 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
191 // add the footer to the packet ("line")
192 switch (binary_footer_type) {
194 binary_footer_value = length;
201 if (binary_footer_type != FOOTER_NONE) {
202 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
203 *((int32_t*)&buf[length]) = binary_footer_value;
205 *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
207 length += sizeof(int32_t);
213 bool FGGeneric::gen_message_ascii() {
214 string generic_sentence;
219 for (unsigned int i = 0; i < _out_message.size(); i++) {
222 generic_sentence += var_separator;
225 switch (_out_message[i].type) {
227 val = _out_message[i].offset +
228 _out_message[i].prop->getIntValue() * _out_message[i].factor;
229 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
233 snprintf(tmp, 255, _out_message[i].format.c_str(),
234 _out_message[i].prop->getBoolValue());
238 val = _out_message[i].offset +
239 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
240 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
244 val = _out_message[i].offset +
245 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
246 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
250 val = _out_message[i].offset +
251 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
252 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
255 default: // SG_STRING
256 snprintf(tmp, 255, _out_message[i].format.c_str(),
257 _out_message[i].prop->getStringValue());
260 generic_sentence += tmp;
263 /* After each lot of variables has been added, put the line separator
266 generic_sentence += line_separator;
268 length = generic_sentence.length();
269 strncpy( buf, generic_sentence.c_str(), length );
274 bool FGGeneric::gen_message() {
276 return gen_message_binary();
278 return gen_message_ascii();
282 bool FGGeneric::parse_message_binary() {
289 p2 = p1 + FG_MAX_MSG_SIZE;
290 while ((++i < (int)_in_message.size()) && (p1 < p2)) {
292 switch (_in_message[i].type) {
294 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
295 tmp32 = sg_bswap_32(*(int32_t *)p1);
297 tmp32 = *(int32_t *)p1;
300 val = _in_message[i].offset + (double)tmp32 * _in_message[i].factor;
302 _in_message[i].prop->setIntValue((int)val);
303 p1 += sizeof(int32_t);
307 _in_message[i].prop->setBoolValue( p1[0] != 0 );
312 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
313 tmp32 = sg_bswap_32(*(int32_t *)p1);
315 tmp32 = *(int32_t *)p1;
318 val = _in_message[i].offset +
319 ((double)tmp32 / 65536.0f) * _in_message[i].factor;
321 _in_message[i].prop->setFloatValue(val);
322 p1 += sizeof(int32_t);
326 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
327 tmp32 = sg_bswap_32(*(int32_t *)p1);
329 tmp32 = *(int32_t *)p1;
332 val = _in_message[i].offset +
333 *(float *)&tmp32 * _in_message[i].factor;
335 _in_message[i].prop->setFloatValue(val);
336 p1 += sizeof(int32_t);
340 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
341 tmp64 = sg_bswap_64(*(int64_t *)p1);
343 tmp64 = *(int64_t *)p1;
346 val = _in_message[i].offset +
347 *(double *)&tmp64 * _in_message[i].factor;
349 _in_message[i].prop->setDoubleValue(val);
350 p1 += sizeof(int64_t);
353 default: // SG_STRING
354 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
355 "Ignoring unsupported binary input chunk type.");
362 bool FGGeneric::parse_message_ascii() {
367 while ((++i < (int)_in_message.size()) &&
368 p1 && strcmp(p1, line_separator.c_str())) {
370 p2 = strstr(p1, var_separator.c_str());
373 p2 += var_separator.length();
376 switch (_in_message[i].type) {
378 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
379 _in_message[i].prop->setIntValue((int)val);
383 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
388 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
389 _in_message[i].prop->setFloatValue((float)val);
392 default: // SG_STRING
393 _in_message[i].prop->setStringValue(p1);
402 bool FGGeneric::parse_message() {
404 return parse_message_binary();
406 return parse_message_ascii();
411 // open hailing frequencies
412 bool FGGeneric::open() {
413 if ( is_enabled() ) {
414 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
415 << "is already in use, ignoring" );
419 SGIOChannel *io = get_io_channel();
421 if ( ! io->open( get_direction() ) ) {
422 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
428 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
429 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
430 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
439 // process work for this port
440 bool FGGeneric::process() {
441 SGIOChannel *io = get_io_channel();
443 if ( get_direction() == SG_IO_OUT ) {
445 if ( ! io->write( buf, length ) ) {
446 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
449 } else if ( get_direction() == SG_IO_IN ) {
450 if ( io->get_type() == sgFileType ) {
452 length = io->readline( buf, FG_MAX_MSG_SIZE );
456 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
460 length = io->read( buf, binary_record_length );
461 if ( length == binary_record_length ) {
464 SG_LOG( SG_IO, SG_ALERT,
465 "Generic protocol: Received binary "
466 "record of unexpected size, expected: "
467 << binary_record_length << " but received: "
474 length = io->readline( buf, FG_MAX_MSG_SIZE );
479 length = io->read( buf, binary_record_length );
480 if ( length == binary_record_length ) {
482 } else if ( length > 0 ) {
483 SG_LOG( SG_IO, SG_ALERT,
484 "Generic protocol: Received binary "
485 "record of unexpected size, expected: "
486 << binary_record_length << " but received: "
490 } while ( length == binary_record_length );
497 return true; // should not get there, but please the compiler
504 bool FGGeneric::close() {
505 SGIOChannel *io = get_io_channel();
507 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
508 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
509 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
514 set_enabled( false );
516 if ( ! io->close() ) {
525 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
527 binary_mode = root->getBoolValue("binary_mode");
530 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
531 * file for each format
533 * var_sep_string = the string/charachter to place between variables
534 * line_sep_string = the string/charachter to place at the end of each
537 preamble = fgUnescape(root->getStringValue("preamble"));
538 postamble = fgUnescape(root->getStringValue("postamble"));
539 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
540 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
542 if ( var_sep_string == "newline" ) {
543 var_separator = '\n';
544 } else if ( var_sep_string == "tab" ) {
545 var_separator = '\t';
546 } else if ( var_sep_string == "space" ) {
548 } else if ( var_sep_string == "formfeed" ) {
549 var_separator = '\f';
550 } else if ( var_sep_string == "carriagereturn" ) {
551 var_sep_string = '\r';
552 } else if ( var_sep_string == "verticaltab" ) {
553 var_separator = '\v';
555 var_separator = var_sep_string;
558 if ( line_sep_string == "newline" ) {
559 line_separator = '\n';
560 } else if ( line_sep_string == "tab" ) {
561 line_separator = '\t';
562 } else if ( line_sep_string == "space" ) {
563 line_separator = ' ';
564 } else if ( line_sep_string == "formfeed" ) {
565 line_separator = '\f';
566 } else if ( line_sep_string == "carriagereturn" ) {
567 line_separator = '\r';
568 } else if ( line_sep_string == "verticaltab" ) {
569 line_separator = '\v';
571 line_separator = line_sep_string;
574 // default values: no footer and record_length = sizeof(representation)
575 binary_footer_type = FOOTER_NONE;
576 binary_record_length = -1;
578 // default choice is network byte order (big endian)
579 if (sgIsLittleEndian()) {
580 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
582 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
585 if ( root->hasValue("binary_footer") ) {
586 string footer_type = root->getStringValue("binary_footer");
587 if ( footer_type == "length" ) {
588 binary_footer_type = FOOTER_LENGTH;
589 } else if ( footer_type.substr(0, 5) == "magic" ) {
590 binary_footer_type = FOOTER_MAGIC;
591 binary_footer_value = strtol(footer_type.substr(6,
592 footer_type.length() - 6).c_str(), (char**)0, 0);
593 } else if ( footer_type != "none" ) {
594 SG_LOG(SG_IO, SG_ALERT,
595 "generic protocol: Unknown generic binary protocol "
596 "footer '" << footer_type << "', using no footer.");
600 if ( root->hasValue("record_length") ) {
601 binary_record_length = root->getIntValue("record_length");
604 if ( root->hasValue("byte_order") ) {
605 string byte_order = root->getStringValue("byte_order");
606 if (byte_order == "network" ) {
607 if ( sgIsLittleEndian() ) {
608 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
610 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
612 } else if ( byte_order == "host" ) {
613 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
615 SG_LOG(SG_IO, SG_ALERT,
616 "generic protocol: Undefined generic binary protocol"
617 "byte order, using HOST byte order.");
622 int record_length = 0; // Only used for binary protocols.
623 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
624 for (unsigned int i = 0; i < chunks.size(); i++) {
628 // chunk.name = chunks[i]->getStringValue("name");
629 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
630 chunk.offset = chunks[i]->getDoubleValue("offset");
631 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
633 string node = chunks[i]->getStringValue("node", "/null");
634 chunk.prop = fgGetNode(node.c_str(), true);
636 string type = chunks[i]->getStringValue("type");
638 // Note: officially the type is called 'bool' but for backward
639 // compatibility 'boolean' will also be supported.
640 if (type == "bool" || type == "boolean") {
641 chunk.type = FG_BOOL;
643 } else if (type == "float") {
644 chunk.type = FG_FLOAT;
645 record_length += sizeof(int32_t);
646 } else if (type == "double") {
647 chunk.type = FG_DOUBLE;
648 record_length += sizeof(int64_t);
649 } else if (type == "fixed") {
650 chunk.type = FG_FIXED;
651 record_length += sizeof(int32_t);
652 } else if (type == "string")
653 chunk.type = FG_STRING;
656 record_length += sizeof(int32_t);
658 msg.push_back(chunk);
662 if (binary_record_length == -1) {
663 binary_record_length = record_length;
664 } else if (binary_record_length < record_length) {
665 SG_LOG(SG_IO, SG_ALERT,
666 "generic protocol: Requested binary record length shorter than "
667 " requested record representation.");
668 binary_record_length = record_length;