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 string config = tokens[ configToken ];
58 string file = config+".xml";
60 SGPath path( globals->get_fg_root() );
61 path.append("Protocol");
62 path.append(file.c_str());
63 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
68 readProperties(path.str(), &root);
69 } catch (const sg_exception &e) {
70 SG_LOG(SG_GENERAL, SG_ALERT,
71 "Unable to load the protocol configuration file");
75 if (tokens[2] == "out") {
76 SGPropertyNode *output = root.getNode("generic/output");
78 read_config(output, _out_message);
80 } else if (tokens[2] == "in") {
81 SGPropertyNode *input = root.getNode("generic/input");
83 read_config(input, _in_message);
86 SG_LOG(SG_GENERAL, SG_ALERT, "Unsuported protocol direction: "
91 FGGeneric::~FGGeneric() {
95 // generate the message
96 bool FGGeneric::gen_message() {
97 string generic_sentence;
102 for (unsigned int i = 0; i < _out_message.size(); i++) {
104 if (i > 0 && !binary_mode)
105 generic_sentence += var_separator;
107 switch (_out_message[i].type) {
109 val = _out_message[i].offset +
110 _out_message[i].prop->getIntValue() * _out_message[i].factor;
112 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
113 *((int32_t*)&buf[length]) = (int32_t)val;
115 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
117 length += sizeof(int32_t);
119 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
125 *((int8_t*)&buf[length])
126 = _out_message[i].prop->getBoolValue() ? true : false;
129 snprintf(tmp, 255, _out_message[i].format.c_str(),
130 _out_message[i].prop->getBoolValue());
135 val = _out_message[i].offset +
136 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
138 int fixed = (int)(val * 65536.0f);
139 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
140 *((int32_t*)&buf[length]) = (int32_t)fixed;
142 *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
144 length += sizeof(int32_t);
146 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
151 val = _out_message[i].offset +
152 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
154 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
155 *((float*)&buf[length]) = val;
157 *((float*)&buf[length]) = sg_bswap_32(*(uint32_t*)&val);
159 length += sizeof(int32_t);
161 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
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;
172 *((double*)&buf[length]) = sg_bswap_64(*(uint64_t*)&val);
174 length += sizeof(int64_t);
176 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
180 default: // SG_STRING
182 const char *strdata = _out_message[i].prop->getStringValue();
183 int strlength = strlen(strdata);
185 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
186 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
187 "FG_STRING will be written in host byte order.");
189 /* Format for strings is
190 * [length as int, 4 bytes][ASCII data, length bytes]
192 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
193 *((int32_t*)&buf[length]) = strlength;
195 *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
197 length += sizeof(int32_t);
198 strncpy(&buf[length], strdata, strlength);
200 /* FIXME padding for alignment? Something like:
201 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
204 snprintf(tmp, 255, _out_message[i].format.c_str(),
205 _out_message[i].prop->getStringValue());
210 generic_sentence += tmp;
215 /* After each lot of variables has been added, put the line separator
218 generic_sentence += line_separator;
220 length = generic_sentence.length();
221 strncpy( buf, generic_sentence.c_str(), length );
223 // add the footer to the packet ("line")
224 switch (binary_footer_type) {
226 binary_footer_value = length;
232 if (binary_footer_type != FOOTER_NONE) {
233 if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
234 *((int32_t*)&buf[length]) = binary_footer_value;
236 *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
238 length += sizeof(int32_t);
245 bool FGGeneric::parse_message() {
251 while ((++i < (int)_in_message.size()) &&
252 p1 && strcmp(p1, line_separator.c_str())) {
254 p2 = strstr(p1, var_separator.c_str());
257 p2 += var_separator.length();
260 switch (_in_message[i].type) {
262 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
263 _in_message[i].prop->setIntValue((int)val);
267 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
272 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
273 _in_message[i].prop->setFloatValue((float)val);
276 default: // SG_STRING
277 _in_message[i].prop->setStringValue(p1);
286 while ((++i < (int)_in_message.size()) &&
287 (p1 - buf < FG_MAX_MSG_SIZE)) {
289 switch (_in_message[i].type) {
291 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
292 tmp = sg_bswap_32(*(int32_t *)p1);
294 tmp = *(int32_t *)p1;
296 val = _in_message[i].offset +
298 _in_message[i].factor;
299 _in_message[i].prop->setIntValue((int)val);
300 p1 += sizeof(int32_t);
304 _in_message[i].prop->setBoolValue( p1[0] != 0 );
309 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
310 tmp = sg_bswap_32(*(int32_t *)p1);
312 tmp = *(int32_t *)p1;
314 val = _in_message[i].offset +
315 ((double)tmp / 65536.0f) * _in_message[i].factor;
316 _in_message[i].prop->setFloatValue(val);
317 p1 += sizeof(int32_t);
321 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
322 tmp = sg_bswap_32(*(int32_t *)p1);
324 tmp = *(int32_t *)p1;
326 val = _in_message[i].offset +
327 *(float *)&tmp * _in_message[i].factor;
328 _in_message[i].prop->setFloatValue(val);
329 p1 += sizeof(int32_t);
333 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
334 tmp = sg_bswap_64(*(int64_t *)p1);
336 tmp = *(int64_t *)p1;
338 val = _in_message[i].offset +
339 *(double *)&tmp * _in_message[i].factor;
340 _in_message[i].prop->setDoubleValue(val);
341 p1 += sizeof(int64_t);
344 default: // SG_STRING
345 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
346 "Ignoring unsupported binary input chunk type.");
356 // open hailing frequencies
357 bool FGGeneric::open() {
358 if ( is_enabled() ) {
359 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
360 << "is already in use, ignoring" );
364 SGIOChannel *io = get_io_channel();
366 if ( ! io->open( get_direction() ) ) {
367 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
373 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
374 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
375 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
384 // process work for this port
385 bool FGGeneric::process() {
386 SGIOChannel *io = get_io_channel();
388 if ( get_direction() == SG_IO_OUT ) {
390 if ( ! io->write( buf, length ) ) {
391 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
394 } else if ( get_direction() == SG_IO_IN ) {
396 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
399 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
403 if ( (length = io->read( buf, binary_record_length )) > 0 ) {
404 if (length != binary_record_length) {
405 SG_LOG( SG_IO, SG_ALERT,
406 "Generic protocol: Received binary "
407 "record of unexpected size, expected: "
408 << binary_record_length << "received: "
411 SG_LOG( SG_IO, SG_DEBUG,
412 "Generic protocol: received record of " << length <<
417 SG_LOG( SG_IO, SG_INFO,
418 "Generic protocol: Error reading data." );
433 bool FGGeneric::close() {
434 SGIOChannel *io = get_io_channel();
436 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
437 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
438 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
443 set_enabled( false );
445 if ( ! io->close() ) {
454 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
456 binary_mode = root->getBoolValue("binary_mode");
459 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
460 * file for each format
462 * var_sep_string = the string/charachter to place between variables
463 * line_sep_string = the string/charachter to place at the end of each
466 preamble = fgUnescape(root->getStringValue("preamble"));
467 postamble = fgUnescape(root->getStringValue("postamble"));
468 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
469 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
471 if ( var_sep_string == "newline" )
472 var_separator = '\n';
473 else if ( var_sep_string == "tab" )
474 var_separator = '\t';
475 else if ( var_sep_string == "space" )
477 else if ( var_sep_string == "formfeed" )
478 var_separator = '\f';
479 else if ( var_sep_string == "carriagereturn" )
480 var_sep_string = '\r';
481 else if ( var_sep_string == "verticaltab" )
482 var_separator = '\v';
484 var_separator = var_sep_string;
486 if ( line_sep_string == "newline" )
487 line_separator = '\n';
488 else if ( line_sep_string == "tab" )
489 line_separator = '\t';
490 else if ( line_sep_string == "space" )
491 line_separator = ' ';
492 else if ( line_sep_string == "formfeed" )
493 line_separator = '\f';
494 else if ( line_sep_string == "carriagereturn" )
495 line_separator = '\r';
496 else if ( line_sep_string == "verticaltab" )
497 line_separator = '\v';
499 line_separator = line_sep_string;
501 binary_footer_type = FOOTER_NONE; // default choice
502 binary_record_length = -1; // default choice = sizeof(representation)
504 // default choice is network byte order (big endian)
505 if (sgIsLittleEndian()) {
506 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
508 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
511 if ( root->hasValue("binary_footer") ) {
512 string footer_type = root->getStringValue("binary_footer");
513 if ( footer_type == "length" )
514 binary_footer_type = FOOTER_LENGTH;
515 else if ( footer_type.substr(0, 5) == "magic" ) {
516 binary_footer_type = FOOTER_MAGIC;
517 binary_footer_value = strtol(footer_type.substr(6,
518 footer_type.length() - 6).c_str(), (char**)0, 0);
519 } else if ( footer_type != "none" )
520 SG_LOG(SG_IO, SG_ALERT,
521 "generic protocol: Undefined generic binary protocol"
522 "footer, using no footer.");
524 if ( root->hasValue("record_length") ) {
525 binary_record_length = root->getIntValue("record_length");
527 if ( root->hasValue("byte_order") ) {
528 string byte_order = root->getStringValue("byte_order");
529 if (byte_order == "network" ) {
530 if ( sgIsLittleEndian() ) {
531 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
533 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
535 } else if ( byte_order == "host" ) {
536 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
538 SG_LOG(SG_IO, SG_ALERT,
539 "generic protocol: Undefined generic binary protocol"
540 "byte order, using HOST byte order.");
545 int record_length = 0; // Only used for binary protocols.
546 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
547 for (unsigned int i = 0; i < chunks.size(); i++) {
551 // chunk.name = chunks[i]->getStringValue("name");
552 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
553 chunk.offset = chunks[i]->getDoubleValue("offset");
554 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
556 string node = chunks[i]->getStringValue("node", "/null");
557 chunk.prop = fgGetNode(node.c_str(), true);
559 string type = chunks[i]->getStringValue("type");
560 if (type == "bool") {
561 chunk.type = FG_BOOL;
563 } else if (type == "float") {
564 chunk.type = FG_FLOAT;
565 record_length += sizeof(int32_t);
566 } else if (type == "double") {
567 chunk.type = FG_DOUBLE;
568 record_length += sizeof(int64_t);
569 } else if (type == "fixed") {
570 chunk.type = FG_FIXED;
571 record_length += sizeof(int32_t);
572 } else if (type == "string")
573 chunk.type = FG_STRING;
576 record_length += sizeof(int32_t);
578 msg.push_back(chunk);
582 if (binary_record_length == -1) {
583 binary_record_length = record_length;
584 } else if (binary_record_length < record_length) {
585 SG_LOG(SG_IO, SG_ALERT,
586 "generic protocol: Requested binary record length shorter than"
587 " requested record representation.");
588 binary_record_length = record_length;