]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
Get rid of the double comma's for the file section of the generic protocol also.
[flightgear.git] / src / Network / generic.cxx
1 // generic.cxx -- generic protocal class
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/misc/sg_path.hxx>
28 #include <simgear/props/props.hxx>
29 #include <simgear/props/props_io.hxx>
30
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
33
34 #include "generic.hxx"
35
36
37 FGGeneric::FGGeneric(string& config) {
38
39     string file = config+".xml";
40
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 "
45                                 << path.str());
46
47     SGPropertyNode root;
48     try {
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");
53          return;
54     }
55
56     SGPropertyNode *output = root.getNode("generic/output");
57
58         /* These variables specified in the fgfsbase/Properties/xxxx.xml file for each format
59          * var_sep_string = the string/charachter to place between variables
60          * line_sep_string = the string/charachter to place at the end of each lot of variables
61          */
62     var_sep_string = output->getStringValue("var_separator");
63     line_sep_string = output->getStringValue("line_separator");
64
65         if ( var_sep_string == "newline" )
66                 var_separator = '\n';
67         else if ( var_sep_string == "tab" )
68                 var_separator = '\t';
69         else if ( var_sep_string == "space" )
70                 var_separator = ' ';
71         else if ( var_sep_string == "formfeed" )
72                 var_separator = '\f';
73         else if ( var_sep_string == "carriagereturn" )
74                 var_sep_string = '\r';
75         else if ( var_sep_string == "verticaltab" )
76                 var_separator = '\v';
77         else
78                 var_separator = var_sep_string;
79
80         if ( line_sep_string == "newline" )
81                 line_separator = '\n';
82         if ( line_sep_string == "tab" )
83                 line_separator = '\t';
84         if ( line_sep_string == "space" )
85                 line_separator = ' ';
86         else if ( line_sep_string == "formfeed" )
87                 line_separator = '\f';
88         else if ( line_sep_string == "carriagereturn" )
89                 line_separator = '\r';
90         else if ( line_sep_string == "verticaltab" )
91                 line_separator = '\v';
92         else
93                 line_separator = line_sep_string;
94
95
96     vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
97     for (unsigned int i = 0; i < chunks.size(); i++) {
98
99         _serial_prot chunk;
100
101      // chunk.name = chunks[i]->getStringValue("name");
102         chunk.format = chunks[i]->getStringValue("format", "%d");
103         chunk.offset = chunks[i]->getDoubleValue("offset");
104         chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
105
106         string node = chunks[i]->getStringValue("node");
107         chunk.prop = fgGetNode(node.c_str(), true);
108
109         string type = chunks[i]->getStringValue("type");
110         if (type == "bool")
111             chunk.type = FG_BOOL;
112         else if (type == "float")
113             chunk.type = FG_DOUBLE;
114         else if (type == "string")
115             chunk.type = FG_STRING;
116         else
117             chunk.type = FG_INT;
118
119         _message.push_back(chunk);
120         
121     }
122
123 }
124
125 FGGeneric::~FGGeneric() {
126     _message.clear();
127 }
128
129
130 // generate the message
131 bool FGGeneric::gen_message() {
132
133     string generic_sentence;
134     char tmp[255];
135
136     double val;
137
138     for (unsigned int i = 0; i < _message.size(); i++) {
139
140         if (i > 0)
141            generic_sentence += var_separator;
142
143         switch (_message[i].type) {
144         case FG_INT:
145             val = _message[i].offset +
146                     _message[i].prop->getIntValue() * _message[i].factor;
147             snprintf(tmp, 255, _message[i].format.c_str(), (int)val);
148             break;
149
150         case FG_BOOL:
151             snprintf(tmp, 255, _message[i].format.c_str(),
152                                _message[i].prop->getBoolValue());
153             break;
154
155         case FG_DOUBLE:
156             val = _message[i].offset +
157                        _message[i].prop->getDoubleValue() * _message[i].factor;
158             snprintf(tmp, 255, _message[i].format.c_str(), val);
159             break;
160
161         default: // SG_STRING
162              snprintf(tmp, 255, _message[i].format.c_str(),
163                                _message[i].prop->getStringValue());
164         }
165
166         generic_sentence += tmp;
167     }
168
169     /* After each lot of variables has been added, put the line separator char/string */
170     generic_sentence += line_separator;
171  
172             
173     length =  generic_sentence.length();
174     strncpy( buf, generic_sentence.c_str(), length );
175
176     return true;
177 }
178
179 bool FGGeneric::parse_message() {
180         return true;
181 }
182
183
184
185 // open hailing frequencies
186 bool FGGeneric::open() {
187     if ( is_enabled() ) {
188         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
189                 << "is already in use, ignoring" );
190         return false;
191     }
192
193     SGIOChannel *io = get_io_channel();
194
195     if ( ! io->open( get_direction() ) ) {
196         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
197         return false;
198     }
199
200     set_enabled( true );
201
202     return true;
203 }
204
205
206 // process work for this port
207 bool FGGeneric::process() {
208     SGIOChannel *io = get_io_channel();
209
210     if ( get_direction() == SG_IO_OUT ) {
211         gen_message();
212         if ( ! io->write( buf, length ) ) {
213             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
214             return false;
215         }
216     } else if ( get_direction() == SG_IO_IN ) {
217         // Temporarliy disable this as output only!
218         //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
219         //    parse_message();
220         //} else {
221         //    SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
222         //    return false;
223         //}
224     }
225
226     return true;
227 }
228
229
230 // close the channel
231 bool FGGeneric::close() {
232     SGIOChannel *io = get_io_channel();
233
234     set_enabled( false );
235
236     if ( ! io->close() ) {
237         return false;
238     }
239
240     return true;
241 }