+
+
+void
+FGGeneric::reinit()
+{
+ SGPath path( globals->get_fg_root() );
+ path.append("Protocol");
+ path.append(file_name.c_str());
+
+ SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
+ << path.str());
+
+ SGPropertyNode root;
+ try {
+ readProperties(path.str(), &root);
+ } catch (const sg_exception &) {
+ SG_LOG(SG_GENERAL, SG_ALERT,
+ "Unable to load the protocol configuration file");
+ return;
+ }
+
+ if (direction == "out") {
+ SGPropertyNode *output = root.getNode("generic/output");
+ if (output) {
+ _out_message.clear();
+ read_config(output, _out_message);
+ }
+ } else if (direction == "in") {
+ SGPropertyNode *input = root.getNode("generic/input");
+ if (input) {
+ _in_message.clear();
+ read_config(input, _in_message);
+ }
+ }
+}
+
+
+void
+FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
+{
+ binary_mode = root->getBoolValue("binary_mode");
+
+ if (!binary_mode) {
+ /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
+ * file for each format
+ *
+ * var_sep_string = the string/charachter to place between variables
+ * line_sep_string = the string/charachter to place at the end of each
+ * lot of variables
+ */
+ preamble = fgUnescape(root->getStringValue("preamble"));
+ postamble = fgUnescape(root->getStringValue("postamble"));
+ var_sep_string = fgUnescape(root->getStringValue("var_separator"));
+ line_sep_string = fgUnescape(root->getStringValue("line_separator"));
+
+ if ( var_sep_string == "newline" ) {
+ var_separator = '\n';
+ } else if ( var_sep_string == "tab" ) {
+ var_separator = '\t';
+ } else if ( var_sep_string == "space" ) {
+ var_separator = ' ';
+ } else if ( var_sep_string == "formfeed" ) {
+ var_separator = '\f';
+ } else if ( var_sep_string == "carriagereturn" ) {
+ var_sep_string = '\r';
+ } else if ( var_sep_string == "verticaltab" ) {
+ var_separator = '\v';
+ } else {
+ var_separator = var_sep_string;
+ }
+
+ if ( line_sep_string == "newline" ) {
+ line_separator = '\n';
+ } else if ( line_sep_string == "tab" ) {
+ line_separator = '\t';
+ } else if ( line_sep_string == "space" ) {
+ line_separator = ' ';
+ } else if ( line_sep_string == "formfeed" ) {
+ line_separator = '\f';
+ } else if ( line_sep_string == "carriagereturn" ) {
+ line_separator = '\r';
+ } else if ( line_sep_string == "verticaltab" ) {
+ line_separator = '\v';
+ } else {
+ line_separator = line_sep_string;
+ }
+ } else {
+ // default values: no footer and record_length = sizeof(representation)
+ binary_footer_type = FOOTER_NONE;
+ binary_record_length = -1;
+
+ // default choice is network byte order (big endian)
+ if (sgIsLittleEndian()) {
+ binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
+ } else {
+ binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
+ }
+
+ if ( root->hasValue("binary_footer") ) {
+ string footer_type = root->getStringValue("binary_footer");
+ if ( footer_type == "length" ) {
+ binary_footer_type = FOOTER_LENGTH;
+ } else if ( footer_type.substr(0, 5) == "magic" ) {
+ binary_footer_type = FOOTER_MAGIC;
+ binary_footer_value = strtol(footer_type.substr(6,
+ footer_type.length() - 6).c_str(), (char**)0, 0);
+ } else if ( footer_type != "none" ) {
+ SG_LOG(SG_IO, SG_ALERT,
+ "generic protocol: Unknown generic binary protocol "
+ "footer '" << footer_type << "', using no footer.");
+ }
+ }
+
+ if ( root->hasValue("record_length") ) {
+ binary_record_length = root->getIntValue("record_length");
+ }
+
+ if ( root->hasValue("byte_order") ) {
+ string byte_order = root->getStringValue("byte_order");
+ if (byte_order == "network" ) {
+ if ( sgIsLittleEndian() ) {
+ binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION;
+ } else {
+ binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
+ }
+ } else if ( byte_order == "host" ) {
+ binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER;
+ } else {
+ SG_LOG(SG_IO, SG_ALERT,
+ "generic protocol: Undefined generic binary protocol"
+ "byte order, using HOST byte order.");
+ }
+ }
+ }
+
+ int record_length = 0; // Only used for binary protocols.
+ vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
+ for (unsigned int i = 0; i < chunks.size(); i++) {
+
+ _serial_prot chunk;
+
+ // chunk.name = chunks[i]->getStringValue("name");
+ chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
+ chunk.offset = chunks[i]->getDoubleValue("offset");
+ chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
+
+ string node = chunks[i]->getStringValue("node", "/null");
+ chunk.prop = fgGetNode(node.c_str(), true);
+
+ string type = chunks[i]->getStringValue("type");
+
+ // Note: officially the type is called 'bool' but for backward
+ // compatibility 'boolean' will also be supported.
+ if (type == "bool" || type == "boolean") {
+ chunk.type = FG_BOOL;
+ record_length += 1;
+ } else if (type == "float") {
+ chunk.type = FG_FLOAT;
+ record_length += sizeof(int32_t);
+ } else if (type == "double") {
+ chunk.type = FG_DOUBLE;
+ record_length += sizeof(int64_t);
+ } else if (type == "fixed") {
+ chunk.type = FG_FIXED;
+ record_length += sizeof(int32_t);
+ } else if (type == "string")
+ chunk.type = FG_STRING;
+ else {
+ chunk.type = FG_INT;
+ record_length += sizeof(int32_t);
+ }
+ msg.push_back(chunk);
+
+ }
+
+ if( binary_mode ) {
+ if (binary_record_length == -1) {
+ binary_record_length = record_length;
+ } else if (binary_record_length < record_length) {
+ SG_LOG(SG_IO, SG_ALERT,
+ "generic protocol: Requested binary record length shorter than "
+ " requested record representation.");
+ binary_record_length = record_length;
+ }
+ }
+}