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/misc/stdint.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/props/props_io.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
41 #include "generic.hxx"
45 FGGeneric::FGGeneric(string& config) {
47 string file = config+".xml";
49 SGPath path( globals->get_fg_root() );
50 path.append("Protocol");
51 path.append(file.c_str());
52 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
57 readProperties(path.str(), &root);
58 } catch (const sg_exception &e) {
59 SG_LOG(SG_GENERAL, SG_ALERT,
60 "Unable to load the protocol configuration file");
64 SGPropertyNode *output = root.getNode("generic/output");
66 read_config(output, _out_message);
68 SGPropertyNode *input = root.getNode("generic/input");
70 read_config(input, _in_message);
73 FGGeneric::~FGGeneric() {
77 // generate the message
78 bool FGGeneric::gen_message() {
79 string generic_sentence;
85 for (unsigned int i = 0; i < _out_message.size(); i++) {
87 if (i > 0 && !binary_mode)
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;
95 *((int32_t*)&buf[length]) = (int32_t)val;
96 length += sizeof(int32_t);
98 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
104 *((int8_t*)&buf[length])
105 = _out_message[i].prop->getBoolValue() ? true : false;
106 length += sizeof(int8_t);
108 snprintf(tmp, 255, _out_message[i].format.c_str(),
109 _out_message[i].prop->getBoolValue());
114 val = _out_message[i].offset +
115 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
117 *((double*)&buf[length]) = val;
118 length += sizeof(double);
120 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
124 default: // SG_STRING
126 const char *strdata = _out_message[i].prop->getStringValue();
127 int strlength = strlen(strdata);
129 /* Format for strings is
130 * [length as int, 4 bytes][ASCII data, length bytes]
132 *((int32_t*)&buf[length]) = strlength;
133 length += sizeof(int32_t);
134 strncpy(&buf[length], strdata, strlength);
136 /* FIXME padding for alignment? Something like:
137 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
140 snprintf(tmp, 255, _out_message[i].format.c_str(),
141 _out_message[i].prop->getStringValue());
146 generic_sentence += tmp;
151 /* After each lot of variables has been added, put the line separator
154 generic_sentence += line_separator;
156 length = generic_sentence.length();
157 strncpy( buf, generic_sentence.c_str(), length );
159 // add the footer to the packet ("line")
160 switch (binary_footer_type) {
162 binary_footer_value = length;
168 if (binary_footer_type != FOOTER_NONE) {
169 *((int32_t*)&buf[length]) = binary_footer_value;
170 length += sizeof(int32_t);
177 bool FGGeneric::parse_message() {
183 SG_LOG( SG_IO, SG_ALERT,
184 "generic protocol: binary mode input is not yet implemented.");
186 while ((++i < (int)_in_message.size()) &&
187 p1 && strcmp(p1, line_separator.c_str())) {
189 p2 = strstr(p1, var_separator.c_str());
192 p2 += var_separator.length();
195 switch (_in_message[i].type) {
197 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
198 _in_message[i].prop->setIntValue((int)val);
202 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
206 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
207 _in_message[i].prop->setFloatValue((float)val);
210 default: // SG_STRING
211 _in_message[i].prop->setStringValue(p1);
222 // open hailing frequencies
223 bool FGGeneric::open() {
224 if ( is_enabled() ) {
225 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
226 << "is already in use, ignoring" );
230 SGIOChannel *io = get_io_channel();
232 if ( ! io->open( get_direction() ) ) {
233 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
243 // process work for this port
244 bool FGGeneric::process() {
245 SGIOChannel *io = get_io_channel();
247 if ( get_direction() == SG_IO_OUT ) {
249 if ( ! io->write( buf, length ) ) {
250 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
253 } else if ( get_direction() == SG_IO_IN ) {
254 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
257 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
267 bool FGGeneric::close() {
268 SGIOChannel *io = get_io_channel();
270 set_enabled( false );
272 if ( ! io->close() ) {
281 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
283 if (root->hasValue("binary_mode"))
284 binary_mode = root->getBoolValue("binary_mode");
289 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
290 * file for each format
292 * var_sep_string = the string/charachter to place between variables
293 * line_sep_string = the string/charachter to place at the end of each
296 var_sep_string = root->getStringValue("var_separator");
297 line_sep_string = root->getStringValue("line_separator");
299 if ( var_sep_string == "newline" )
300 var_separator = '\n';
301 else if ( var_sep_string == "tab" )
302 var_separator = '\t';
303 else if ( var_sep_string == "space" )
305 else if ( var_sep_string == "formfeed" )
306 var_separator = '\f';
307 else if ( var_sep_string == "carriagereturn" )
308 var_sep_string = '\r';
309 else if ( var_sep_string == "verticaltab" )
310 var_separator = '\v';
312 var_separator = var_sep_string;
314 if ( line_sep_string == "newline" )
315 line_separator = '\n';
316 else if ( line_sep_string == "tab" )
317 line_separator = '\t';
318 else if ( line_sep_string == "space" )
319 line_separator = ' ';
320 else if ( line_sep_string == "formfeed" )
321 line_separator = '\f';
322 else if ( line_sep_string == "carriagereturn" )
323 line_separator = '\r';
324 else if ( line_sep_string == "verticaltab" )
325 line_separator = '\v';
327 line_separator = line_sep_string;
329 binary_footer_type = FOOTER_NONE; // default choice
330 if ( root->hasValue("binary_footer") ) {
331 string footer_type = root->getStringValue("binary_footer");
332 if ( footer_type == "length" )
333 binary_footer_type = FOOTER_LENGTH;
334 else if ( footer_type.substr(0, 5) == "magic" ) {
335 binary_footer_type = FOOTER_MAGIC;
336 binary_footer_value = strtol(footer_type.substr(6,
337 footer_type.length() - 6).c_str(), (char**)0, 0);
338 } else if ( footer_type != "none" )
339 SG_LOG(SG_IO, SG_ALERT,
340 "generic protocol: Undefined generic binary protocol"
341 "footer, using no footer.");
345 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
346 for (unsigned int i = 0; i < chunks.size(); i++) {
350 // chunk.name = chunks[i]->getStringValue("name");
351 chunk.format = chunks[i]->getStringValue("format", "%d");
352 chunk.offset = chunks[i]->getDoubleValue("offset");
353 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
355 string node = chunks[i]->getStringValue("node");
356 chunk.prop = fgGetNode(node.c_str(), true);
358 string type = chunks[i]->getStringValue("type");
360 chunk.type = FG_BOOL;
361 else if (type == "float")
362 chunk.type = FG_DOUBLE;
363 else if (type == "string")
364 chunk.type = FG_STRING;
368 msg.push_back(chunk);