]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
YASim now supports the new fuel.nas fuel management system. It
[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     int v;
137     double d;
138
139     for (unsigned int i = 0; i < _message.size(); i++) {
140
141         if (i > 0)
142            generic_sentence += var_separator;
143
144         switch (_message[i].type) {
145         case FG_INT:
146             v = _message[i].offset +
147                     _message[i].prop->getIntValue() * _message[i].factor;
148             snprintf(tmp, 255, _message[i].format.c_str(), v);
149             break;
150
151         case FG_BOOL:
152             snprintf(tmp, 255, _message[i].format.c_str(),
153                                _message[i].prop->getBoolValue());
154             break;
155
156         case FG_DOUBLE:
157             d = _message[i].offset +
158                        _message[i].prop->getDoubleValue() * _message[i].factor;
159             snprintf(tmp, 255, _message[i].format.c_str(), d);
160             break;
161
162         default: // SG_STRING
163              snprintf(tmp, 255, _message[i].format.c_str(),
164                                _message[i].prop->getStringValue());
165         }
166
167         generic_sentence += tmp;
168     }
169
170     /* After each lot of variables has been added, put the line separator char/string */
171     generic_sentence += line_separator;
172  
173             
174     length =  generic_sentence.length();
175     strncpy( buf, generic_sentence.c_str(), length );
176
177     return true;
178 }
179
180 bool FGGeneric::parse_message() {
181         return true;
182 }
183
184
185
186 // open hailing frequencies
187 bool FGGeneric::open() {
188     if ( is_enabled() ) {
189         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
190                 << "is already in use, ignoring" );
191         return false;
192     }
193
194     SGIOChannel *io = get_io_channel();
195
196     if ( ! io->open( get_direction() ) ) {
197         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
198         return false;
199     }
200
201     set_enabled( true );
202
203     return true;
204 }
205
206
207 // process work for this port
208 bool FGGeneric::process() {
209     SGIOChannel *io = get_io_channel();
210
211     if ( get_direction() == SG_IO_OUT ) {
212         gen_message();
213         if ( ! io->write( buf, length ) ) {
214             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
215             return false;
216         }
217     } else if ( get_direction() == SG_IO_IN ) {
218         // Temporarliy disable this as output only!
219         //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
220         //    parse_message();
221         //} else {
222         //    SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
223         //    return false;
224         //}
225     }
226
227     return true;
228 }
229
230
231 // close the channel
232 bool FGGeneric::close() {
233     SGIOChannel *io = get_io_channel();
234
235     set_enabled( false );
236
237     if ( ! io->close() ) {
238         return false;
239     }
240
241     return true;
242 }