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/misc/props.hxx>
29 #include <simgear/misc/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");
57 seperator = output->getStringValue("seperator");
59 vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
60 for (unsigned int i = 0; i < chunks.size(); i++) {
64 // chunk.name = chunks[i]->getStringValue("name");
65 chunk.format = chunks[i]->getStringValue("format", "%d");
66 chunk.offset = chunks[i]->getDoubleValue("offset");
67 chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
69 string node = chunks[i]->getStringValue("node");
70 chunk.prop = fgGetNode(node.c_str(), true);
72 string type = chunks[i]->getStringValue("type");
75 else if (type == "float")
76 chunk.type = FG_DOUBLE;
77 else if (type == "string")
78 chunk.type = FG_STRING;
82 _message.push_back(chunk);
88 FGGeneric::~FGGeneric() {
93 // generate the message
94 bool FGGeneric::gen_message() {
96 string generic_sentence;
102 for (unsigned int i = 0; i < _message.size(); i++) {
105 generic_sentence += seperator;
107 switch (_message[i].type) {
109 v = _message[i].offset +
110 _message[i].prop->getIntValue() * _message[i].factor;
111 snprintf(tmp, 255, _message[i].format.c_str(), v);
115 snprintf(tmp, 255, _message[i].format.c_str(),
116 _message[i].prop->getBoolValue());
120 d = _message[i].offset +
121 _message[i].prop->getDoubleValue() * _message[i].factor;
122 snprintf(tmp, 255, _message[i].format.c_str(), d);
125 default: // SG_STRING
126 snprintf(tmp, 255, _message[i].format.c_str(),
127 _message[i].prop->getStringValue());
130 generic_sentence += tmp;
134 length = generic_sentence.length();
135 strncpy( buf, generic_sentence.c_str(), length );
140 bool FGGeneric::parse_message() {
146 // open hailing frequencies
147 bool FGGeneric::open() {
148 if ( is_enabled() ) {
149 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
150 << "is already in use, ignoring" );
154 SGIOChannel *io = get_io_channel();
156 if ( ! io->open( get_direction() ) ) {
157 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
167 // process work for this port
168 bool FGGeneric::process() {
169 SGIOChannel *io = get_io_channel();
171 if ( get_direction() == SG_IO_OUT ) {
173 if ( ! io->write( buf, length ) ) {
174 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
177 } else if ( get_direction() == SG_IO_IN ) {
178 // Temporarliy disable this as output only!
179 //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
182 // SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
192 bool FGGeneric::close() {
193 SGIOChannel *io = get_io_channel();
195 set_enabled( false );
197 if ( ! io->close() ) {