]> git.mxchange.org Git - flightgear.git/blob - src/Network/generic.cxx
Moved random ground cover object management code (userdata.[ch]xx) over
[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/misc/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_seperator");
63     line_sep_string = output->getStringValue("line_seperator");
64
65         if ( var_seperator == "newline" )
66                 var_seperator = '\n';
67         else if ( var_seperator == "formfeed" )
68                 var_seperator = '\f';
69         else if ( var_seperator == "carriagereturn" )
70                 var_seperator = '\r';
71         else if ( var_seperator == "verticaltab" )
72                 var_seperator = '\v';
73         else
74                 var_seperator = var_sep_string;
75
76         if ( line_sep_string == "newline" )
77                 line_seperator = '\n';
78         else if ( line_sep_string == "formfeed" )
79                 line_seperator = '\f';
80         else if ( line_sep_string == "carriagereturn" )
81                 line_seperator = '\r';
82         else if ( line_sep_string == "verticaltab" )
83                 line_seperator = '\v';
84         else
85                 line_seperator = line_sep_string;
86
87
88     vector<SGPropertyNode_ptr> chunks = output->getChildren("chunk");
89     for (unsigned int i = 0; i < chunks.size(); i++) {
90
91         _serial_prot chunk;
92
93      // chunk.name = chunks[i]->getStringValue("name");
94         chunk.format = chunks[i]->getStringValue("format", "%d");
95         chunk.offset = chunks[i]->getDoubleValue("offset");
96         chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
97
98         string node = chunks[i]->getStringValue("node");
99         chunk.prop = fgGetNode(node.c_str(), true);
100
101         string type = chunks[i]->getStringValue("type");
102         if (type == "bool")
103             chunk.type = FG_BOOL;
104         else if (type == "float")
105             chunk.type = FG_DOUBLE;
106         else if (type == "string")
107             chunk.type = FG_STRING;
108         else
109             chunk.type = FG_INT;
110
111         _message.push_back(chunk);
112         
113     }
114
115 }
116
117 FGGeneric::~FGGeneric() {
118     _message.clear();
119 }
120
121
122 // generate the message
123 bool FGGeneric::gen_message() {
124
125     string generic_sentence;
126     char tmp[255];
127
128     int v;
129     double d;
130
131     for (unsigned int i = 0; i < _message.size(); i++) {
132
133         if (i > 0)
134            generic_sentence += line_seperator;
135
136         switch (_message[i].type) {
137         case FG_INT:
138             v = _message[i].offset +
139                     _message[i].prop->getIntValue() * _message[i].factor;
140             snprintf(tmp, 255, _message[i].format.c_str(), v);
141             break;
142
143         case FG_BOOL:
144             snprintf(tmp, 255, _message[i].format.c_str(),
145                                _message[i].prop->getBoolValue());
146             break;
147
148         case FG_DOUBLE:
149             d = _message[i].offset +
150                        _message[i].prop->getDoubleValue() * _message[i].factor;
151             snprintf(tmp, 255, _message[i].format.c_str(), d);
152             break;
153
154         default: // SG_STRING
155              snprintf(tmp, 255, _message[i].format.c_str(),
156                                _message[i].prop->getStringValue());
157         }
158
159         generic_sentence += tmp;
160     }
161
162     /* After each lot of variables has been added, put the line seperator char/string */
163     generic_sentence += line_seperator;
164  
165             
166     length =  generic_sentence.length();
167     strncpy( buf, generic_sentence.c_str(), length );
168
169     return true;
170 }
171
172 bool FGGeneric::parse_message() {
173         return true;
174 }
175
176
177
178 // open hailing frequencies
179 bool FGGeneric::open() {
180     if ( is_enabled() ) {
181         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
182                 << "is already in use, ignoring" );
183         return false;
184     }
185
186     SGIOChannel *io = get_io_channel();
187
188     if ( ! io->open( get_direction() ) ) {
189         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
190         return false;
191     }
192
193     set_enabled( true );
194
195     return true;
196 }
197
198
199 // process work for this port
200 bool FGGeneric::process() {
201     SGIOChannel *io = get_io_channel();
202
203     if ( get_direction() == SG_IO_OUT ) {
204         gen_message();
205         if ( ! io->write( buf, length ) ) {
206             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
207             return false;
208         }
209     } else if ( get_direction() == SG_IO_IN ) {
210         // Temporarliy disable this as output only!
211         //if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
212         //    parse_message();
213         //} else {
214         //    SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
215         //    return false;
216         //}
217     }
218
219     return true;
220 }
221
222
223 // close the channel
224 bool FGGeneric::close() {
225     SGIOChannel *io = get_io_channel();
226
227     set_enabled( false );
228
229     if ( ! io->close() ) {
230         return false;
231     }
232
233     return true;
234 }