16#include <simgear/debug/logstream.hxx>
17#include <simgear/io/iochannel.hxx>
18#include <simgear/structure/exception.hxx>
19#include <simgear/misc/sg_path.hxx>
20#include <simgear/misc/stdint.hxx>
21#include <simgear/misc/strutils.hxx>
22#include <simgear/props/props.hxx>
23#include <simgear/props/props_io.hxx>
24#include <simgear/math/SGMath.hxx>
33using simgear::strutils::unescape;
38 virtual int wrap(
size_t n, uint8_t * buf ) = 0;
39 virtual int unwrap(
size_t n, uint8_t * buf ) = 0;
47 virtual int wrap(
size_t n, uint8_t * buf );
48 virtual int unwrap(
size_t n, uint8_t * buf );
50 static const uint8_t FEND;
51 static const uint8_t FESC;
52 static const uint8_t TFEND;
53 static const uint8_t TFESC;
56const uint8_t FGKissWrapper::FEND = 0xC0;
57const uint8_t FGKissWrapper::FESC = 0xDB;
58const uint8_t FGKissWrapper::TFEND = 0xDC;
59const uint8_t FGKissWrapper::TFESC = 0xDD;
63 std::vector<uint8_t> dest;
68 for(
size_t i = 0;
i < n;
i++ ) {
73 dest.push_back(TFESC);
78 dest.push_back(TFEND);
88 memcpy( buf, dest.data(), dest.size() );
97 while( 0 < n && FEND != *sp ) {
103 while( 0 < n && FEND == *sp ) {
108 if( 0 == n )
return 0;
110 std::vector<uint8_t> dest;
112 bool escaped =
false;
123 dest.push_back( FESC );
127 dest.push_back( FEND );
145 SG_LOG(SG_IO, SG_WARN,
146 "KISS frame detected FEND before end of frame. Trailing data dropped." );
159 memcpy( buf, dest.data(), dest.size() );
166 virtual int wrap(
size_t n, uint8_t * buf );
167 virtual int unwrap(
size_t n, uint8_t * buf );
183 std::vector<uint8_t> dest;
195 dest.push_back(*sp++);
201 memcpy( buf, dest.data(), dest.size() );
213 if (tokens[1] ==
"socket") {
215 }
else if (tokens[1] ==
"file") {
221 if ((configToken >= tokens.size())||(tokens[ configToken ] ==
"")) {
222 SG_LOG(SG_NETWORK, SG_ALERT,
223 "Not enough tokens passed for generic '" << tokens[1] <<
"' protocol. ");
227 std::string config = tokens[ configToken ];
228 file_name = config+
".xml";
232 SG_LOG(SG_NETWORK, SG_ALERT,
"Unsupported protocol direction: " << tokens[2]);
254bool FGGeneric::gen_message_binary() {
255 std::string generic_sentence;
259 for (
unsigned int i = 0;
i < _out_message.size();
i++) {
261 switch (_out_message[
i].type) {
264 val = _out_message[
i].offset +
265 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
266 int32_t intVal = val;
267 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
268 intVal = (int32_t) sg_bswap_32((uint32_t)intVal);
270 memcpy(&buf[length], &intVal,
sizeof(int32_t));
271 length +=
sizeof(int32_t);
276 buf[length] = (char) (_out_message[
i].prop->getBoolValue() ?
true :
false);
282 val = _out_message[
i].offset +
283 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
285 int32_t fixed = (int)(val * 65536.0f);
286 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
287 fixed = (int32_t) sg_bswap_32((uint32_t)fixed);
289 memcpy(&buf[length], &fixed,
sizeof(int32_t));
290 length +=
sizeof(int32_t);
296 val = _out_message[
i].offset +
297 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
299 tmpun32.
floatVal =
static_cast<float>(val);
301 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
304 memcpy(&buf[length], &tmpun32.
intVal,
sizeof(uint32_t));
305 length +=
sizeof(uint32_t);
311 val = _out_message[
i].offset +
312 _out_message[
i].prop->getDoubleValue() * _out_message[
i].factor;
316 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
319 memcpy(&buf[length], &tmpun64.
longVal,
sizeof(uint64_t));
320 length +=
sizeof(uint64_t);
326 val = _out_message[
i].offset +
327 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
328 int8_t byteVal = val;
329 memcpy(&buf[length], &byteVal,
sizeof(int8_t));
330 length +=
sizeof(int8_t);
336 val = _out_message[
i].offset +
337 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
338 int16_t wordVal = val;
339 memcpy(&buf[length], &wordVal,
sizeof(int16_t));
340 length +=
sizeof(int16_t);
345 std::string strdata = _out_message[
i].prop->getStringValue();
346 size_t strlength = strdata.length();
348 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
349 SG_LOG( SG_IO, SG_ALERT,
"Generic protocol: "
350 "FG_STRING will be written in host byte order.");
355 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
356 strlength = sg_bswap_32(strlength);
358 memcpy(&buf[length], &strlength,
sizeof(int32_t));
359 length +=
sizeof(int32_t);
360 strncpy(&buf[length], strdata.c_str(), strlength);
371 switch (binary_footer_type) {
373 binary_footer_value = length;
381 if (binary_footer_type != FOOTER_NONE) {
382 int32_t intValue = binary_footer_value;
383 if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
384 intValue = sg_bswap_32(binary_footer_value);
386 memcpy(&buf[length], &intValue,
sizeof(int32_t));
387 length +=
sizeof(int32_t);
390 if( wrapper ) length = wrapper->wrap( length,
reinterpret_cast<uint8_t*
>(buf) );
395bool FGGeneric::gen_message_ascii() {
396 std::string generic_sentence;
401 for (
unsigned int i = 0;
i < _out_message.size();
i++) {
404 generic_sentence += var_separator;
407 std::string format = simgear::strutils::sanitizePrintfFormat(_out_message[
i].format);
409 switch (_out_message[
i].type) {
413 val = _out_message[
i].offset +
414 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
415 snprintf(tmp, 255, format.c_str(), (
int)val);
419 snprintf(tmp, 255, format.c_str(),
420 _out_message[
i].prop->getBoolValue());
424 val = _out_message[
i].offset +
425 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
426 snprintf(tmp, 255, format.c_str(), (
float)val);
430 val = _out_message[
i].offset +
431 _out_message[
i].prop->getFloatValue() * _out_message[
i].factor;
432 snprintf(tmp, 255, format.c_str(), (
float)val);
436 val = _out_message[
i].offset +
437 _out_message[
i].prop->getDoubleValue() * _out_message[
i].factor;
438 snprintf(tmp, 255, format.c_str(), (
double)val);
442 snprintf(tmp, 255, format.c_str(),
443 _out_message[
i].prop->getStringValue().c_str());
446 generic_sentence += tmp;
452 generic_sentence += line_separator;
454 length = generic_sentence.length();
455 strncpy( buf, generic_sentence.c_str(), length );
462 return gen_message_binary();
464 return gen_message_ascii();
468bool FGGeneric::parse_message_binary(
int length) {
474 while ((++
i < (
int)_in_message.size()) && (p1 <
p2)) {
476 switch (_in_message[
i].type) {
478 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
479 tmp32 = sg_bswap_32(*(int32_t *)p1);
481 tmp32 = *(int32_t *)p1;
483 updateValue(_in_message[
i], (
int)tmp32);
484 p1 +=
sizeof(int32_t);
488 updateValue(_in_message[
i], p1[0] != 0);
493 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
494 tmp32 = sg_bswap_32(*(int32_t *)p1);
496 tmp32 = *(int32_t *)p1;
498 updateValue(_in_message[
i], (
float)tmp32 / 65536.0f);
499 p1 +=
sizeof(int32_t);
504 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
505 tmpun32.
intVal = sg_bswap_32(*(uint32_t *)p1);
509 updateValue(_in_message[
i], tmpun32.
floatVal);
510 p1 +=
sizeof(int32_t);
515 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
516 tmpun64.
longVal = sg_bswap_64(*(uint64_t *)p1);
520 updateValue(_in_message[
i], tmpun64.
doubleVal);
521 p1 +=
sizeof(int64_t);
525 tmp32 = *(int8_t *)p1;
526 updateValue(_in_message[
i], (
int)tmp32);
527 p1 +=
sizeof(int8_t);
531 if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
532 tmp32 = sg_bswap_16(*(int16_t *)p1);
534 tmp32 = *(int16_t *)p1;
536 updateValue(_in_message[
i], (
int)tmp32);
537 p1 +=
sizeof(int16_t);
541 SG_LOG( SG_IO, SG_ALERT,
"Generic protocol: "
542 "Ignoring unsupported binary input chunk type.");
550bool FGGeneric::parse_message_ascii(
int length) {
553 int chunks = _in_message.size();
554 int line_separator_size = line_separator.size();
556 if (length < line_separator_size ||
557 line_separator.compare(buf + length - line_separator_size) != 0) {
559 SG_LOG(SG_IO, SG_WARN,
560 "Input line does not end with expected line separator." );
562 buf[length - line_separator_size] = 0;
565 size_t varsep_len = var_separator.length();
566 while ((++
i < chunks) && p1) {
571 p2 = strstr(p1, var_separator.c_str());
578 switch (_in_message[
i].type) {
582 updateValue(_in_message[
i],
atoi(p1));
586 updateValue(_in_message[
i],
atof(p1) != 0.0);
591 updateValue(_in_message[
i], (
float)strtod(p1, 0));
595 updateValue(_in_message[
i], (
double)strtod(p1, 0));
599 _in_message[
i].prop->setStringValue(p1);
611 return parse_message_binary(length);
613 return parse_message_ascii(length);
621 SG_LOG( SG_IO, SG_ALERT,
"This shouldn't happen, but the channel "
622 <<
"is already in use, ignoring" );
629 SG_LOG( SG_IO, SG_ALERT,
"Error opening channel communication layer." );
637 && ! preamble.empty() ) {
638 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
639 SG_LOG( SG_IO, SG_WARN,
"Error writing preamble." );
655 if ( ! io->write( buf, length ) ) {
656 SG_LOG( SG_IO, SG_WARN,
"Error writing data." );
663 if ( io->get_type() == sgFileType ) {
669 SG_LOG( SG_IO, SG_ALERT,
"Error reading data." );
673 length = io->read( buf, binary_record_length );
674 if ( length == binary_record_length ) {
677 SG_LOG( SG_IO, SG_ALERT,
678 "Generic protocol: Received binary "
679 "record of unexpected size, expected: "
680 << binary_record_length <<
" but received: "
690 while ((length = io->read( buf, binary_record_length ))
691 == binary_record_length ) {
696 SG_LOG( SG_IO, SG_ALERT,
697 "Generic protocol: Received binary "
698 "record of unexpected size, expected: "
699 << binary_record_length <<
" but received: "
721 && ! postamble.empty() ) {
722 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
723 SG_LOG( SG_IO, SG_ALERT,
"Error writing postamble." );
730 if ( ! io->close() ) {
741 SGPath path =
globals->findDataPath(
"Protocol/" + file_name);
742 if (!path.exists()) {
743 SG_LOG(SG_NETWORK, SG_WARN,
"Couldn't find protocol file for '" << file_name <<
"'");
747 SG_LOG(SG_NETWORK, SG_INFO,
"Reading communication protocol from " << path);
751 readProperties(path, &root);
752 }
catch (
const sg_exception & ex) {
753 SG_LOG(SG_NETWORK, SG_ALERT,
754 "Unable to load the protocol configuration file: " << ex.getFormattedMessage() );
759 if ((dir == SG_IO_OUT) || (dir == SG_IO_BI)) {
760 SGPropertyNode *output = root.getNode(
"generic/output");
762 _out_message.clear();
763 if (!read_config(output, _out_message))
771 if ((dir == SG_IO_IN) || (dir == SG_IO_BI)) {
772 SGPropertyNode *input = root.getNode(
"generic/input");
775 if (!read_config(input, _in_message))
780 if (!binary_mode && (line_separator.empty() ||
781 *line_separator.rbegin() !=
'\n')) {
783 SG_LOG(SG_IO, SG_WARN,
784 "Warning: Appending newline to line separator in generic input.");
785 line_separator.push_back(
'\n');
795FGGeneric::read_config(SGPropertyNode *root, std::vector<_serial_prot> &msg)
797 binary_mode = root->getBoolValue(
"binary_mode");
807 preamble = unescape(root->getStringValue(
"preamble"));
808 postamble = unescape(root->getStringValue(
"postamble"));
809 var_sep_string = unescape(root->getStringValue(
"var_separator"));
810 line_sep_string = unescape(root->getStringValue(
"line_separator"));
812 if ( var_sep_string ==
"newline" ) {
813 var_separator =
'\n';
814 }
else if ( var_sep_string ==
"tab" ) {
815 var_separator =
'\t';
816 }
else if ( var_sep_string ==
"space" ) {
818 }
else if ( var_sep_string ==
"formfeed" ) {
819 var_separator =
'\f';
820 }
else if ( var_sep_string ==
"carriagereturn" ) {
821 var_sep_string =
'\r';
822 }
else if ( var_sep_string ==
"verticaltab" ) {
823 var_separator =
'\v';
825 var_separator = var_sep_string;
828 if ( line_sep_string ==
"newline" ) {
829 line_separator =
'\n';
830 }
else if ( line_sep_string ==
"tab" ) {
831 line_separator =
'\t';
832 }
else if ( line_sep_string ==
"space" ) {
833 line_separator =
' ';
834 }
else if ( line_sep_string ==
"formfeed" ) {
835 line_separator =
'\f';
836 }
else if ( line_sep_string ==
"carriagereturn" ) {
837 line_separator =
'\r';
838 }
else if ( line_sep_string ==
"verticaltab" ) {
839 line_separator =
'\v';
841 line_separator = line_sep_string;
845 binary_footer_type = FOOTER_NONE;
846 binary_record_length = -1;
849 if (sgIsLittleEndian()) {
850 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
852 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
855 if ( root->hasValue(
"binary_footer") ) {
856 std::string footer_type = root->getStringValue(
"binary_footer");
857 if ( footer_type ==
"length" ) {
858 binary_footer_type = FOOTER_LENGTH;
859 }
else if ( footer_type.substr(0, 5) ==
"magic" ) {
860 binary_footer_type = FOOTER_MAGIC;
861 binary_footer_value = strtol(footer_type.substr(6,
862 footer_type.length() - 6).c_str(), (
char**)0, 0);
863 }
else if ( footer_type !=
"none" ) {
864 SG_LOG(SG_IO, SG_ALERT,
865 "generic protocol: Unknown generic binary protocol "
866 "footer '" << footer_type <<
"', using no footer.");
870 if ( root->hasValue(
"record_length") ) {
871 binary_record_length = root->getIntValue(
"record_length");
874 if ( root->hasValue(
"byte_order") ) {
875 std::string byte_order = root->getStringValue(
"byte_order");
876 if (byte_order ==
"network" ) {
877 if ( sgIsLittleEndian() ) {
878 binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
880 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
882 }
else if ( byte_order ==
"host" ) {
883 binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
885 SG_LOG(SG_IO, SG_ALERT,
886 "generic protocol: Undefined generic binary protocol"
887 "byte order, using HOST byte order.");
891 if( root->hasValue(
"wrapper" ) ) {
892 std::string w = root->getStringValue(
"wrapper" );
893 if( w ==
"kiss" ) wrapper =
new FGKissWrapper();
894 else if( w ==
"stxetx" ) wrapper =
new FGSTXETXWrapper();
895 else SG_LOG(SG_IO, SG_ALERT,
896 "generic protocol: Undefined binary protocol wrapper '" + w +
"' ignored" );
900 int record_length = 0;
901 std::vector<SGPropertyNode_ptr> chunks = root->getChildren(
"chunk");
903 for (
unsigned int i = 0;
i < chunks.size();
i++) {
909 chunk.offset = chunks[
i]->getDoubleValue(
"offset");
910 chunk.factor = chunks[
i]->getDoubleValue(
"factor", 1.0);
911 chunk.min = chunks[
i]->getDoubleValue(
"min");
912 chunk.max = chunks[
i]->getDoubleValue(
"max");
913 chunk.wrap = chunks[
i]->getBoolValue(
"wrap");
914 chunk.rel = chunks[
i]->getBoolValue(
"relative");
916 if( chunks[
i]->hasChild(
"const") ) {
917 chunk.prop =
new SGPropertyNode();
920 std::string node = chunks[
i]->getStringValue(
"node",
"/null");
921 chunk.prop =
fgGetNode(node.c_str(),
true);
924 std::string type = chunks[
i]->getStringValue(
"type");
928 if (type ==
"bool" || type ==
"boolean") {
931 }
else if (type ==
"float") {
933 record_length +=
sizeof(int32_t);
934 }
else if (type ==
"double") {
936 record_length +=
sizeof(int64_t);
937 }
else if (type ==
"fixed") {
939 record_length +=
sizeof(int32_t);
940 }
else if (type ==
"string") {
942 }
else if (type ==
"byte") {
944 record_length +=
sizeof(int8_t);
945 }
else if (type ==
"word") {
947 record_length +=
sizeof(int16_t);
950 record_length +=
sizeof(int32_t);
952 msg.push_back(chunk);
958 if ((chunks.size() > 1)&&(var_sep_string.length() == 0))
961 SG_LOG(SG_IO, SG_ALERT,
962 "generic protocol: Invalid configuration. "
963 "'var_separator' must not be empty for protocols which have more than one chunk per line.");
969 if (binary_record_length == -1) {
970 binary_record_length = record_length;
971 }
else if (binary_record_length < record_length) {
972 SG_LOG(SG_IO, SG_ALERT,
973 "generic protocol: Requested binary record length shorter than "
974 " requested record representation.");
975 binary_record_length = record_length;
988 setValue(prot.
prop, !getValue<bool>(prot.
prop));
992 setValue(prot.
prop, val);
FGGeneric(std::vector< std::string >)
bool parse_message_len(int length)
http://www.ka9q.net/papers/kiss.html
virtual int unwrap(size_t n, uint8_t *buf)
virtual int wrap(size_t n, uint8_t *buf)
virtual ~FGProtocolWrapper()
virtual int unwrap(size_t n, uint8_t *buf)=0
virtual int wrap(size_t n, uint8_t *buf)=0
SGProtocolDir get_direction() const
SGIOChannel * get_io_channel() const
void set_enabled(const bool b)
void set_direction(const std::string &d)
virtual int wrap(size_t n, uint8_t *buf)
virtual int unwrap(size_t n, uint8_t *buf)
const std::string & getStringValue(const char *spec)
static double atof(const string &str)
static int atoi(const string &str)
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.