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 <string.h> // strstr()
25 #include <stdlib.h> // strtod(), atoi()
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/props_io.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
37 #include "generic.hxx"
41 FGGeneric::FGGeneric(string& config) {
43 string file = config+".xml";
45 SGPath path( globals->get_fg_root() );
46 path.append("Protocol");
47 path.append(file.c_str());
48 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
53 readProperties(path.str(), &root);
54 } catch (const sg_exception &e) {
55 SG_LOG(SG_GENERAL, SG_ALERT,
56 "Unable to load the protocol configuration file");
61 SGPropertyNode *output = root.getNode("generic/output");
63 read_config(output, _out_message);
66 SGPropertyNode *input = root.getNode("generic/input");
68 read_config(input, _in_message);
71 FGGeneric::~FGGeneric() {
77 // generate the message
78 bool FGGeneric::gen_message() {
80 string generic_sentence;
85 for (unsigned int i = 0; i < _out_message.size(); i++) {
88 generic_sentence += var_separator;
90 switch (_out_message[i].type) {
92 val = _out_message[i].offset +
93 _out_message[i].prop->getIntValue() * _out_message[i].factor;
94 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
98 snprintf(tmp, 255, _out_message[i].format.c_str(),
99 _out_message[i].prop->getBoolValue());
103 val = _out_message[i].offset +
104 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
105 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
108 default: // SG_STRING
109 snprintf(tmp, 255, _out_message[i].format.c_str(),
110 _out_message[i].prop->getStringValue());
113 generic_sentence += tmp;
116 /* After each lot of variables has been added, put the line separator
119 generic_sentence += line_separator;
122 length = generic_sentence.length();
123 strncpy( buf, generic_sentence.c_str(), length );
128 bool FGGeneric::parse_message() {
133 while ((++i < _in_message.size()) &&
134 p1 && strcmp(p1, line_separator.c_str())) {
136 p2 = strstr(p1, var_separator.c_str());
139 p2 += var_separator.length();
142 switch (_in_message[i].type) {
144 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
145 _in_message[i].prop->setIntValue((int)val);
149 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
153 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
154 _in_message[i].prop->setFloatValue((float)val);
157 default: // SG_STRING
158 _in_message[i].prop->setStringValue(p1);
169 // open hailing frequencies
170 bool FGGeneric::open() {
171 if ( is_enabled() ) {
172 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
173 << "is already in use, ignoring" );
177 SGIOChannel *io = get_io_channel();
179 if ( ! io->open( get_direction() ) ) {
180 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
190 // process work for this port
191 bool FGGeneric::process() {
192 SGIOChannel *io = get_io_channel();
194 if ( get_direction() == SG_IO_OUT ) {
196 if ( ! io->write( buf, length ) ) {
197 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
200 } else if ( get_direction() == SG_IO_IN ) {
201 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
204 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
214 bool FGGeneric::close() {
215 SGIOChannel *io = get_io_channel();
217 set_enabled( false );
219 if ( ! io->close() ) {
228 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
230 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
231 * file for each format
233 * var_sep_string = the string/charachter to place between variables
234 * line_sep_string = the string/charachter to place at the end of each
237 var_sep_string = root->getStringValue("var_separator");
238 line_sep_string = root->getStringValue("line_separator");
240 if ( var_sep_string == "newline" )
241 var_separator = '\n';
242 else if ( var_sep_string == "tab" )
243 var_separator = '\t';
244 else if ( var_sep_string == "space" )
246 else if ( var_sep_string == "formfeed" )
247 var_separator = '\f';
248 else if ( var_sep_string == "carriagereturn" )
249 var_sep_string = '\r';
250 else if ( var_sep_string == "verticaltab" )
251 var_separator = '\v';
253 var_separator = var_sep_string;
255 if ( line_sep_string == "newline" )
256 line_separator = '\n';
257 else if ( line_sep_string == "tab" )
258 line_separator = '\t';
259 else if ( line_sep_string == "space" )
260 line_separator = ' ';
261 else if ( line_sep_string == "formfeed" )
262 line_separator = '\f';
263 else if ( line_sep_string == "carriagereturn" )
264 line_separator = '\r';
265 else if ( line_sep_string == "verticaltab" )
266 line_separator = '\v';
268 line_separator = line_sep_string;
271 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
272 for (unsigned int i = 0; i < chunks.size(); i++) {
276 // chunk.name = chunks[i]->getStringValue("name");
277 chunk.format = chunks[i]->getStringValue("format", "%d");
278 chunk.offset = chunks[i]->getDoubleValue("offset");
279 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
281 string node = chunks[i]->getStringValue("node");
282 chunk.prop = fgGetNode(node.c_str(), true);
284 string type = chunks[i]->getStringValue("type");
286 chunk.type = FG_BOOL;
287 else if (type == "float")
288 chunk.type = FG_DOUBLE;
289 else if (type == "string")
290 chunk.type = FG_STRING;
294 msg.push_back(chunk);