From 35dad8c4c4a80761fe73603bd0ce3cf09b8baae0 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Mon, 28 Oct 2013 22:57:22 +0100 Subject: [PATCH] generic binary protocol: add 'word' datatype this patch adds support for binary datatype word (aka int16) now we have int (32bit), word (16bit) and byte (8bit) integer types --- src/Network/generic.cxx | 25 +++++++++++++++++++++++++ src/Network/generic.hxx | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Network/generic.cxx b/src/Network/generic.cxx index 7ef83bdc2..8372a5e40 100644 --- a/src/Network/generic.cxx +++ b/src/Network/generic.cxx @@ -346,6 +346,16 @@ bool FGGeneric::gen_message_binary() { break; } + case FG_WORD: + { + val = _out_message[i].offset + + _out_message[i].prop->getIntValue() * _out_message[i].factor; + int16_t wordVal = val; + memcpy(&buf[length], &wordVal, sizeof(int16_t)); + length += sizeof(int16_t); + break; + } + default: // SG_STRING const char *strdata = _out_message[i].prop->getStringValue(); int32_t strlength = strlen(strdata); @@ -413,6 +423,7 @@ bool FGGeneric::gen_message_ascii() { switch (_out_message[i].type) { case FG_BYTE: + case FG_WORD: case FG_INT: val = _out_message[i].offset + _out_message[i].prop->getIntValue() * _out_message[i].factor; @@ -531,6 +542,16 @@ bool FGGeneric::parse_message_binary(int length) { p1 += sizeof(int8_t); break; + case FG_WORD: + if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { + tmp32 = sg_bswap_16(*(int16_t *)p1); + } else { + tmp32 = *(int16_t *)p1; + } + updateValue(_in_message[i], (int)tmp32); + p1 += sizeof(int16_t); + break; + default: // SG_STRING SG_LOG( SG_IO, SG_ALERT, "Generic protocol: " "Ignoring unsupported binary input chunk type."); @@ -571,6 +592,7 @@ bool FGGeneric::parse_message_ascii(int length) { switch (_in_message[i].type) { case FG_BYTE: + case FG_WORD: case FG_INT: updateValue(_in_message[i], atoi(p1)); break; @@ -931,6 +953,9 @@ FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg) } else if (type == "byte") { chunk.type = FG_BYTE; record_length += sizeof(int8_t); + } else if (type == "word") { + chunk.type = FG_WORD; + record_length += sizeof(int16_t); } else { chunk.type = FG_INT; record_length += sizeof(int32_t); diff --git a/src/Network/generic.hxx b/src/Network/generic.hxx index 50c180564..fbc146d5c 100644 --- a/src/Network/generic.hxx +++ b/src/Network/generic.hxx @@ -59,7 +59,7 @@ public: bool getInitOk(void) { return initOk; } protected: - enum e_type { FG_BOOL=0, FG_INT, FG_FLOAT, FG_DOUBLE, FG_STRING, FG_FIXED, FG_BYTE }; + enum e_type { FG_BOOL=0, FG_INT, FG_FLOAT, FG_DOUBLE, FG_STRING, FG_FIXED, FG_BYTE, FG_WORD }; typedef struct { // string name;