]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
Update nasal function name.
[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 - http://www.flightgear.org/~curt
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 <string.h>             // strstr()
25 #include <stdlib.h>             // strtod(), atoi()
26
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>
33
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
36
37 #include "generic.hxx"
38
39
40
41 FGGeneric::FGGeneric(string& config) {
42
43     string file = config+".xml";
44
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 "
49                                 << path.str());
50
51     SGPropertyNode root;
52     try {
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");
57          return;
58     }
59
60     _out_message.clear();
61     SGPropertyNode *output = root.getNode("generic/output");
62     if (output)
63         read_config(output, _out_message);
64
65     _in_message.clear();
66     SGPropertyNode *input = root.getNode("generic/input");
67     if (input)
68         read_config(input, _in_message);
69 }
70
71 FGGeneric::~FGGeneric() {
72     _out_message.clear();
73     _in_message.clear();
74 }
75
76
77 // generate the message
78 bool FGGeneric::gen_message() {
79
80     string generic_sentence;
81     char tmp[255];
82
83     double val;
84
85     for (unsigned int i = 0; i < _out_message.size(); i++) {
86
87         if (i > 0)
88            generic_sentence += var_separator;
89
90         switch (_out_message[i].type) {
91         case FG_INT:
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);
95             break;
96
97         case FG_BOOL:
98             snprintf(tmp, 255, _out_message[i].format.c_str(),
99                                _out_message[i].prop->getBoolValue());
100             break;
101
102         case FG_DOUBLE:
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);
106             break;
107
108         default: // SG_STRING
109              snprintf(tmp, 255, _out_message[i].format.c_str(),
110                                 _out_message[i].prop->getStringValue());
111         }
112
113         generic_sentence += tmp;
114     }
115
116     /* After each lot of variables has been added, put the line separator
117      * char/string
118      */
119     generic_sentence += line_separator;
120  
121             
122     length =  generic_sentence.length();
123     strncpy( buf, generic_sentence.c_str(), length );
124
125     return true;
126 }
127
128 bool FGGeneric::parse_message() {
129     char *p2, *p1 = buf;
130     double val;
131     int i = -1;
132
133     while ((++i < _in_message.size()) &&
134            p1 && strcmp(p1, line_separator.c_str())) {
135
136         p2 = strstr(p1, var_separator.c_str());
137         if (p2) {
138             *p2 = 0;
139             p2 += var_separator.length();
140         }
141
142         switch (_in_message[i].type) {
143         case FG_INT:
144             val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
145             _in_message[i].prop->setIntValue((int)val);
146             break;
147
148         case FG_BOOL:
149             _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
150             break;
151
152         case FG_DOUBLE:
153             val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
154             _in_message[i].prop->setFloatValue((float)val);
155             break;
156
157         default: // SG_STRING
158              _in_message[i].prop->setStringValue(p1);
159         }
160
161         p1 = p2;
162     }
163     
164     return true;
165 }
166
167
168
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" );
174         return false;
175     }
176
177     SGIOChannel *io = get_io_channel();
178
179     if ( ! io->open( get_direction() ) ) {
180         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
181         return false;
182     }
183
184     set_enabled( true );
185
186     return true;
187 }
188
189
190 // process work for this port
191 bool FGGeneric::process() {
192     SGIOChannel *io = get_io_channel();
193
194     if ( get_direction() == SG_IO_OUT ) {
195         gen_message();
196         if ( ! io->write( buf, length ) ) {
197             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
198             return false;
199         }
200     } else if ( get_direction() == SG_IO_IN ) {
201         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
202             parse_message();
203         } else {
204             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
205             return false;
206         }
207     }
208
209     return true;
210 }
211
212
213 // close the channel
214 bool FGGeneric::close() {
215     SGIOChannel *io = get_io_channel();
216
217     set_enabled( false );
218
219     if ( ! io->close() ) {
220         return false;
221     }
222
223     return true;
224 }
225
226
227 void
228 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
229 {
230         /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml
231          * file for each format
232          *
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
235          *                   lot of variables
236          */
237     var_sep_string = root->getStringValue("var_separator");
238     line_sep_string = root->getStringValue("line_separator");
239
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" )
245                 var_separator = ' ';
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';
252         else
253                 var_separator = var_sep_string;
254
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';
267         else
268                 line_separator = line_sep_string;
269
270
271     vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
272     for (unsigned int i = 0; i < chunks.size(); i++) {
273
274         _serial_prot chunk;
275
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);
280
281         string node = chunks[i]->getStringValue("node");
282         chunk.prop = fgGetNode(node.c_str(), true);
283
284         string type = chunks[i]->getStringValue("type");
285         if (type == "bool")
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;
291         else
292             chunk.type = FG_INT;
293
294         msg.push_back(chunk);
295
296     }
297
298 }
299