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>
40 #include <Main/util.hxx>
42 #include "generic.hxx"
46 FGGeneric::FGGeneric(string& config) {
48 string file = config+".xml";
50 SGPath path( globals->get_fg_root() );
51 path.append("Protocol");
52 path.append(file.c_str());
53 SG_LOG(SG_GENERAL, SG_INFO, "Reading communication protocol from "
58 readProperties(path.str(), &root);
59 } catch (const sg_exception &e) {
60 SG_LOG(SG_GENERAL, SG_ALERT,
61 "Unable to load the protocol configuration file");
65 SGPropertyNode *output = root.getNode("generic/output");
67 read_config(output, _out_message);
69 SGPropertyNode *input = root.getNode("generic/input");
71 read_config(input, _in_message);
74 FGGeneric::~FGGeneric() {
78 // generate the message
79 bool FGGeneric::gen_message() {
80 string generic_sentence;
86 for (unsigned int i = 0; i < _out_message.size(); i++) {
88 if (i > 0 && !binary_mode)
89 generic_sentence += var_separator;
91 switch (_out_message[i].type) {
93 val = _out_message[i].offset +
94 _out_message[i].prop->getIntValue() * _out_message[i].factor;
96 *((int32_t*)&buf[length]) = (int32_t)val;
97 length += sizeof(int32_t);
99 snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
105 *((int8_t*)&buf[length])
106 = _out_message[i].prop->getBoolValue() ? true : false;
107 length += sizeof(int8_t);
109 snprintf(tmp, 255, _out_message[i].format.c_str(),
110 _out_message[i].prop->getBoolValue());
115 val = _out_message[i].offset +
116 _out_message[i].prop->getFloatValue() * _out_message[i].factor;
118 *((double*)&buf[length]) = val;
119 length += sizeof(double);
121 snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
125 default: // SG_STRING
127 const char *strdata = _out_message[i].prop->getStringValue();
128 int strlength = strlen(strdata);
130 /* Format for strings is
131 * [length as int, 4 bytes][ASCII data, length bytes]
133 *((int32_t*)&buf[length]) = strlength;
134 length += sizeof(int32_t);
135 strncpy(&buf[length], strdata, strlength);
137 /* FIXME padding for alignment? Something like:
138 * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0;
141 snprintf(tmp, 255, _out_message[i].format.c_str(),
142 _out_message[i].prop->getStringValue());
147 generic_sentence += tmp;
152 /* After each lot of variables has been added, put the line separator
155 generic_sentence += line_separator;
157 length = generic_sentence.length();
158 strncpy( buf, generic_sentence.c_str(), length );
160 // add the footer to the packet ("line")
161 switch (binary_footer_type) {
163 binary_footer_value = length;
169 if (binary_footer_type != FOOTER_NONE) {
170 *((int32_t*)&buf[length]) = binary_footer_value;
171 length += sizeof(int32_t);
178 bool FGGeneric::parse_message() {
184 SG_LOG( SG_IO, SG_ALERT,
185 "generic protocol: binary mode input is not yet implemented.");
187 while ((++i < (int)_in_message.size()) &&
188 p1 && strcmp(p1, line_separator.c_str())) {
190 p2 = strstr(p1, var_separator.c_str());
193 p2 += var_separator.length();
196 switch (_in_message[i].type) {
198 val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
199 _in_message[i].prop->setIntValue((int)val);
203 _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
207 val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
208 _in_message[i].prop->setFloatValue((float)val);
211 default: // SG_STRING
212 _in_message[i].prop->setStringValue(p1);
223 // open hailing frequencies
224 bool FGGeneric::open() {
225 if ( is_enabled() ) {
226 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
227 << "is already in use, ignoring" );
231 SGIOChannel *io = get_io_channel();
233 if ( ! io->open( get_direction() ) ) {
234 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
240 if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
241 if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
242 SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
251 // process work for this port
252 bool FGGeneric::process() {
253 SGIOChannel *io = get_io_channel();
255 if ( get_direction() == SG_IO_OUT ) {
257 if ( ! io->write( buf, length ) ) {
258 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
261 } else if ( get_direction() == SG_IO_IN ) {
262 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
265 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
275 bool FGGeneric::close() {
276 SGIOChannel *io = get_io_channel();
278 if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
279 if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
280 SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
285 set_enabled( false );
287 if ( ! io->close() ) {
296 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
298 binary_mode = root->getBoolValue("binary_mode");
301 /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
302 * file for each format
304 * var_sep_string = the string/charachter to place between variables
305 * line_sep_string = the string/charachter to place at the end of each
308 preamble = fgUnescape(root->getStringValue("preamble"));
309 postamble = fgUnescape(root->getStringValue("postamble"));
310 var_sep_string = fgUnescape(root->getStringValue("var_separator"));
311 line_sep_string = fgUnescape(root->getStringValue("line_separator"));
313 if ( var_sep_string == "newline" )
314 var_separator = '\n';
315 else if ( var_sep_string == "tab" )
316 var_separator = '\t';
317 else if ( var_sep_string == "space" )
319 else if ( var_sep_string == "formfeed" )
320 var_separator = '\f';
321 else if ( var_sep_string == "carriagereturn" )
322 var_sep_string = '\r';
323 else if ( var_sep_string == "verticaltab" )
324 var_separator = '\v';
326 var_separator = var_sep_string;
328 if ( line_sep_string == "newline" )
329 line_separator = '\n';
330 else if ( line_sep_string == "tab" )
331 line_separator = '\t';
332 else if ( line_sep_string == "space" )
333 line_separator = ' ';
334 else if ( line_sep_string == "formfeed" )
335 line_separator = '\f';
336 else if ( line_sep_string == "carriagereturn" )
337 line_separator = '\r';
338 else if ( line_sep_string == "verticaltab" )
339 line_separator = '\v';
341 line_separator = line_sep_string;
343 binary_footer_type = FOOTER_NONE; // default choice
344 if ( root->hasValue("binary_footer") ) {
345 string footer_type = root->getStringValue("binary_footer");
346 if ( footer_type == "length" )
347 binary_footer_type = FOOTER_LENGTH;
348 else if ( footer_type.substr(0, 5) == "magic" ) {
349 binary_footer_type = FOOTER_MAGIC;
350 binary_footer_value = strtol(footer_type.substr(6,
351 footer_type.length() - 6).c_str(), (char**)0, 0);
352 } else if ( footer_type != "none" )
353 SG_LOG(SG_IO, SG_ALERT,
354 "generic protocol: Undefined generic binary protocol"
355 "footer, using no footer.");
359 vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
360 for (unsigned int i = 0; i < chunks.size(); i++) {
364 // chunk.name = chunks[i]->getStringValue("name");
365 chunk.format = fgUnescape(chunks[i]->getStringValue("format", "%d"));
366 chunk.offset = chunks[i]->getDoubleValue("offset");
367 chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
369 string node = chunks[i]->getStringValue("node", "/null");
370 chunk.prop = fgGetNode(node.c_str(), true);
372 string type = chunks[i]->getStringValue("type");
374 chunk.type = FG_BOOL;
375 else if (type == "float")
376 chunk.type = FG_DOUBLE;
377 else if (type == "string")
378 chunk.type = FG_STRING;
382 msg.push_back(chunk);