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_seperator");
63 line_sep_string = output->getStringValue("line_seperator");
65 if ( var_seperator == "newline" )
67 else if ( var_seperator == "formfeed" )
69 else if ( var_seperator == "carriagereturn" )
71 else if ( var_seperator == "verticaltab" )
74 var_seperator = var_sep_string;
76 if ( line_sep_string == "newline" )
77 line_seperator = '\n';
78 else if ( line_sep_string == "formfeed" )
79 line_seperator = '\f';
80 else if ( line_sep_string == "carriagereturn" )
81 line_seperator = '\r';
82 else if ( line_sep_string == "verticaltab" )
83 line_seperator = '\v';
85 line_seperator = line_sep_string;
88 vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
89 for (unsigned int i = 0; i < chunks.size(); i++) {
93 // chunk.name = chunks[i]->getStringValue("name");
94 chunk.format = chunks[i]->getStringValue("format", "%d");
95 chunk.offset = chunks[i]->getDoubleValue("offset");
96 chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
98 string node = chunks[i]->getStringValue("node");
99 chunk.prop = fgGetNode(node.c_str(), true);
101 string type = chunks[i]->getStringValue("type");
103 chunk.type = FG_BOOL;
104 else if (type == "float")
105 chunk.type = FG_DOUBLE;
106 else if (type == "string")
107 chunk.type = FG_STRING;
111 _message.push_back(chunk);
117 FGGeneric::~FGGeneric() {
122 // generate the message
123 bool FGGeneric::gen_message() {
125 string generic_sentence;
131 for (unsigned int i = 0; i < _message.size(); i++) {
134 generic_sentence += line_seperator;
136 switch (_message[i].type) {
138 v = _message[i].offset +
139 _message[i].prop->getIntValue() * _message[i].factor;
140 snprintf(tmp, 255, _message[i].format.c_str(), v);
144 snprintf(tmp, 255, _message[i].format.c_str(),
145 _message[i].prop->getBoolValue());
149 d = _message[i].offset +
150 _message[i].prop->getDoubleValue() * _message[i].factor;
151 snprintf(tmp, 255, _message[i].format.c_str(), d);
154 default: // SG_STRING
155 snprintf(tmp, 255, _message[i].format.c_str(),
156 _message[i].prop->getStringValue());
159 generic_sentence += tmp;
162 /* After each lot of variables has been added, put the line seperator char/string */
163 generic_sentence += line_seperator;
166 length = generic_sentence.length();
167 strncpy( buf, generic_sentence.c_str(), length );
172 bool FGGeneric::parse_message() {
178 // open hailing frequencies
179 bool FGGeneric::open() {
180 if ( is_enabled() ) {
181 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
182 << "is already in use, ignoring" );
186 SGIOChannel *io = get_io_channel();
188 if ( ! io->open( get_direction() ) ) {
189 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
199 // process work for this port
200 bool FGGeneric::process() {
201 SGIOChannel *io = get_io_channel();
203 if ( get_direction() == SG_IO_OUT ) {
205 if ( ! io->write( buf, length ) ) {
206 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
209 } else if ( get_direction() == SG_IO_IN ) {
210 // Temporarliy disable this as output only!
211 //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
214 // SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
224 bool FGGeneric::close() {
225 SGIOChannel *io = get_io_channel();
227 set_enabled( false );
229 if ( ! io->close() ) {