1 // generic.cxx -- generic protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <string.h> // strstr()
28 #include <stdlib.h> // strtod(), atoi()
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
40 #include "generic.hxx"
44 FGGeneric::FGGeneric(string& config) {
46 string file = config+".xml";
48 SGPath path( globals->get_fg_root() );
49 path.append("Protocol");
50 path.append(file.c_str());
51 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
56 readProperties(path.str(), &root);
57 } catch (const sg_exception &e) {
58 SG_LOG(SG_GENERAL, SG_ALERT,
59 "Unable to load the protocol configuration file");
64 SGPropertyNode *output = root.getNode("generic/output");
66 read_config(output, _out_message);
69 SGPropertyNode *input = root.getNode("generic/input");
71 read_config(input, _in_message);
74 FGGeneric::~FGGeneric() {
80 // generate the message
81 bool FGGeneric::gen_message() {
83 string generic_sentence;
88 for (unsigned int i = 0; i < _out_message.size(); i++) {
91 generic_sentence += var_separator;
93 switch (_out_message[i].type) {
95 val = _out_message[i].offset +
96 _out_message[i].prop->getIntValue() * _out_message[i].factor;
97 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
101 snprintf(tmp, 255, _out_message[i].format.c_str(),
102 _out_message[i].prop->getBoolValue());
106 val = _out_message[i].offset +
107 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
108 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
111 default: // SG_STRING
112 snprintf(tmp, 255, _out_message[i].format.c_str(),
113 _out_message[i].prop->getStringValue());
116 generic_sentence += tmp;
119 /* After each lot of variables has been added, put the line separator
122 generic_sentence += line_separator;
125 length = generic_sentence.length();
126 strncpy( buf, generic_sentence.c_str(), length );
131 bool FGGeneric::parse_message() {
136 while ((++i < _in_message.size()) &&
137 p1 && strcmp(p1, line_separator.c_str())) {
139 p2 = strstr(p1, var_separator.c_str());
142 p2 += var_separator.length();
145 switch (_in_message[i].type) {
147 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
148 _in_message[i].prop->setIntValue((int)val);
152 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
156 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
157 _in_message[i].prop->setFloatValue((float)val);
160 default: // SG_STRING
161 _in_message[i].prop->setStringValue(p1);
172 // open hailing frequencies
173 bool FGGeneric::open() {
174 if ( is_enabled() ) {
175 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
176 << "is already in use, ignoring" );
180 SGIOChannel *io = get_io_channel();
182 if ( ! io->open( get_direction() ) ) {
183 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
193 // process work for this port
194 bool FGGeneric::process() {
195 SGIOChannel *io = get_io_channel();
197 if ( get_direction() == SG_IO_OUT ) {
199 if ( ! io->write( buf, length ) ) {
200 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
203 } else if ( get_direction() == SG_IO_IN ) {
204 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
207 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
217 bool FGGeneric::close() {
218 SGIOChannel *io = get_io_channel();
220 set_enabled( false );
222 if ( ! io->close() ) {
231 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
233 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
234 * file for each format
236 * var_sep_string = the string/charachter to place between variables
237 * line_sep_string = the string/charachter to place at the end of each
240 var_sep_string = root->getStringValue("var_separator");
241 line_sep_string = root->getStringValue("line_separator");
243 if ( var_sep_string == "newline" )
244 var_separator = '\n';
245 else if ( var_sep_string == "tab" )
246 var_separator = '\t';
247 else if ( var_sep_string == "space" )
249 else if ( var_sep_string == "formfeed" )
250 var_separator = '\f';
251 else if ( var_sep_string == "carriagereturn" )
252 var_sep_string = '\r';
253 else if ( var_sep_string == "verticaltab" )
254 var_separator = '\v';
256 var_separator = var_sep_string;
258 if ( line_sep_string == "newline" )
259 line_separator = '\n';
260 else if ( line_sep_string == "tab" )
261 line_separator = '\t';
262 else if ( line_sep_string == "space" )
263 line_separator = ' ';
264 else if ( line_sep_string == "formfeed" )
265 line_separator = '\f';
266 else if ( line_sep_string == "carriagereturn" )
267 line_separator = '\r';
268 else if ( line_sep_string == "verticaltab" )
269 line_separator = '\v';
271 line_separator = line_sep_string;
274 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
275 for (unsigned int i = 0; i < chunks.size(); i++) {
279 // chunk.name = chunks[i]->getStringValue("name");
280 chunk.format = chunks[i]->getStringValue("format", "%d");
281 chunk.offset = chunks[i]->getDoubleValue("offset");
282 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
284 string node = chunks[i]->getStringValue("node");
285 chunk.prop = fgGetNode(node.c_str(), true);
287 string type = chunks[i]->getStringValue("type");
289 chunk.type = FG_BOOL;
290 else if (type == "float")
291 chunk.type = FG_DOUBLE;
292 else if (type == "string")
293 chunk.type = FG_STRING;
297 msg.push_back(chunk);