1 // generic.cxx -- generic protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
26 #include <simgear/misc/exception.hxx>
27 #include <simgear/misc/sg_path.hxx>
28 #include <simgear/props/props.hxx>
29 #include <simgear/props/props_io.hxx>
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
34 #include "generic.hxx"
37 FGGeneric::FGGeneric(string& config) {
39 string file = config+".xml";
41 SGPath path( globals->get_fg_root() );
42 path.append("Protocol");
43 path.append(file.c_str());
44 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
49 readProperties(path.str(), &root);
50 } catch (const sg_exception &e) {
51 SG_LOG(SG_GENERAL, SG_ALERT,
52 "Unable to load the protocol configuration file");
56 SGPropertyNode *output = root.getNode("generic/output");
58 /* These variables specified in the fgfsbase/Properties/xxxx.xml file for each format
59 * var_sep_string = the string/charachter to place between variables
60 * line_sep_string = the string/charachter to place at the end of each lot of variables
62 var_sep_string = output->getStringValue("var_separator");
63 line_sep_string = output->getStringValue("line_separator");
65 if ( var_sep_string == "newline" )
67 else if ( var_sep_string == "tab" )
69 else if ( var_sep_string == "space" )
71 else if ( var_sep_string == "formfeed" )
73 else if ( var_sep_string == "carriagereturn" )
74 var_sep_string = '\r';
75 else if ( var_sep_string == "verticaltab" )
78 var_separator = var_sep_string;
80 if ( line_sep_string == "newline" )
81 line_separator = '\n';
82 if ( line_sep_string == "tab" )
83 line_separator = '\t';
84 if ( line_sep_string == "space" )
86 else if ( line_sep_string == "formfeed" )
87 line_separator = '\f';
88 else if ( line_sep_string == "carriagereturn" )
89 line_separator = '\r';
90 else if ( line_sep_string == "verticaltab" )
91 line_separator = '\v';
93 line_separator = line_sep_string;
96 vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
97 for (unsigned int i = 0; i < chunks.size(); i++) {
101 // chunk.name = chunks[i]->getStringValue("name");
102 chunk.format = chunks[i]->getStringValue("format", "%d");
103 chunk.offset = chunks[i]->getDoubleValue("offset");
104 chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
106 string node = chunks[i]->getStringValue("node");
107 chunk.prop = fgGetNode(node.c_str(), true);
109 string type = chunks[i]->getStringValue("type");
111 chunk.type = FG_BOOL;
112 else if (type == "float")
113 chunk.type = FG_DOUBLE;
114 else if (type == "string")
115 chunk.type = FG_STRING;
119 _message.push_back(chunk);
125 FGGeneric::~FGGeneric() {
130 // generate the message
131 bool FGGeneric::gen_message() {
133 string generic_sentence;
139 for (unsigned int i = 0; i < _message.size(); i++) {
142 generic_sentence += var_separator;
144 switch (_message[i].type) {
146 v = _message[i].offset +
147 _message[i].prop->getIntValue() * _message[i].factor;
148 snprintf(tmp, 255, _message[i].format.c_str(), v);
152 snprintf(tmp, 255, _message[i].format.c_str(),
153 _message[i].prop->getBoolValue());
157 d = _message[i].offset +
158 _message[i].prop->getDoubleValue() * _message[i].factor;
159 snprintf(tmp, 255, _message[i].format.c_str(), d);
162 default: // SG_STRING
163 snprintf(tmp, 255, _message[i].format.c_str(),
164 _message[i].prop->getStringValue());
167 generic_sentence += tmp;
170 /* After each lot of variables has been added, put the line separator char/string */
171 generic_sentence += line_separator;
174 length = generic_sentence.length();
175 strncpy( buf, generic_sentence.c_str(), length );
180 bool FGGeneric::parse_message() {
186 // open hailing frequencies
187 bool FGGeneric::open() {
188 if ( is_enabled() ) {
189 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
190 << "is already in use, ignoring" );
194 SGIOChannel *io = get_io_channel();
196 if ( ! io->open( get_direction() ) ) {
197 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
207 // process work for this port
208 bool FGGeneric::process() {
209 SGIOChannel *io = get_io_channel();
211 if ( get_direction() == SG_IO_OUT ) {
213 if ( ! io->write( buf, length ) ) {
214 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
217 } else if ( get_direction() == SG_IO_IN ) {
218 // Temporarliy disable this as output only!
219 //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
222 // SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
232 bool FGGeneric::close() {
233 SGIOChannel *io = get_io_channel();
235 set_enabled( false );
237 if ( ! io->close() ) {