source: xplra/test/multigetctest.c@ 30:d92d686b4d70

Last change on this file since 30:d92d686b4d70 was 30:d92d686b4d70, checked in by István Váradi <ivaradi@…>, 11 years ago

Fixed problems with detecting a broken connection

File size: 15.3 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29//------------------------------------------------------------------------------
30
31#include <hu/varadiistvan/xplra/xplra.h>
32
33#include <stdlib.h>
34#include <stdio.h>
35#include <string.h>
36
37#ifdef _WIN32
38#include <windows.h>
39#else
40#include <unistd.h>
41#endif
42
43//------------------------------------------------------------------------------
44
45int main(int argc, char* argv[])
46{
47 int retval = 0;
48 const char* errorString;
49
50 int i;
51
52 int connectionID = -1;
53 int getterID = -1;
54
55 size_t tailnumID = INVALID_DATAREF_ID;
56 size_t daysID = INVALID_DATAREF_ID;
57 size_t zuluSecID = INVALID_DATAREF_ID;
58 size_t pausedID = INVALID_DATAREF_ID;
59 size_t latitudeID = INVALID_DATAREF_ID;
60 size_t longitudeID = INVALID_DATAREF_ID;
61 size_t replayID = INVALID_DATAREF_ID;
62 size_t overspeedID = INVALID_DATAREF_ID;
63 size_t stalledID = INVALID_DATAREF_ID;
64 size_t onTheGroundID = INVALID_DATAREF_ID;
65 size_t emptyWeightID = INVALID_DATAREF_ID;
66 size_t payloadWeightID = INVALID_DATAREF_ID;
67 size_t grossWeightID = INVALID_DATAREF_ID;
68 size_t headingID = INVALID_DATAREF_ID;
69 size_t pitchID = INVALID_DATAREF_ID;
70 size_t bankID = INVALID_DATAREF_ID;
71 size_t iasID = INVALID_DATAREF_ID;
72 size_t machID = INVALID_DATAREF_ID;
73 size_t groundSpeedID = INVALID_DATAREF_ID;
74 size_t vsID = INVALID_DATAREF_ID;
75 size_t radioAltitudeID = INVALID_DATAREF_ID;
76 size_t altitudeID = INVALID_DATAREF_ID;
77 size_t gLoadID = INVALID_DATAREF_ID;
78 size_t flapsControlID = INVALID_DATAREF_ID;
79 size_t flapsLeftID = INVALID_DATAREF_ID;
80 size_t flapsRightID = INVALID_DATAREF_ID;
81 size_t navLightsID = INVALID_DATAREF_ID;
82 size_t beaconLightsID = INVALID_DATAREF_ID;
83 size_t strobeLightsID = INVALID_DATAREF_ID;
84 size_t landingLightsID = INVALID_DATAREF_ID;
85 size_t pitotID = INVALID_DATAREF_ID;
86 size_t parkingID = INVALID_DATAREF_ID;
87 size_t gearControlID = INVALID_DATAREF_ID;
88 size_t noseGearID = INVALID_DATAREF_ID;
89 size_t altimeterID = INVALID_DATAREF_ID;
90 size_t qnhID = INVALID_DATAREF_ID;
91 size_t navTypesID = INVALID_DATAREF_ID;
92 size_t busVoltsID = INVALID_DATAREF_ID;
93
94 const char* tailnum = 0;
95 const int32_t* days = 0;
96 const float* zuluSec = 0;
97 const int32_t* paused = 0;
98 const double* latitude = 0;
99 const double* longitude = 0;
100 const int32_t* replay = 0;
101 const int32_t* overspeed = 0;
102 const int32_t* stalled = 0;
103 const int32_t* onTheGround = 0;
104 const float* emptyWeight = 0;
105 const float* payloadWeight = 0;
106 const float* grossWeight = 0;
107 const float* heading = 0;
108 const float* pitch = 0;
109 const float* bank = 0;
110 const float* ias = 0;
111 const float* mach = 0;
112 const float* groundSpeed = 0;
113 const float* vs = 0;
114 const float* radioAltitude = 0;
115 const float* altitude = 0;
116 const float* gLoad = 0;
117 const float* flapsControl = 0;
118 const float* flapsLeft = 0;
119 const float* flapsRight = 0;
120 const int32_t* navLights = 0;
121 const int32_t* beaconLights = 0;
122 const int32_t* strobeLights = 0;
123 const int32_t* landingLights = 0;
124 const int32_t* pitot = 0;
125 const float* parking = 0;
126 const int32_t* gearControl = 0;
127 const float* noseGear = 0;
128 const float* altimeter = 0;
129 const float* qnh = 0;
130 const int32_t* navTypes = 0;
131 const float* busVolts = 0;
132
133 int dontregister = 0;
134
135 for(i = 1; i<argc; ++i) {
136 if (strcmp(argv[i], "dontregister")==0) {
137 dontregister = 1;
138 }
139 }
140
141 printf("Connecting to X-Plane...\n");
142 connectionID = xplra_connect();
143 if (connectionID>=0) {
144 printf("Connected to X-Plane.\n\n");
145 } else {
146 printf("Connection failed.\n\n");
147 goto cleanup;
148 }
149
150 getterID = xplra_multi_create_getter(connectionID);
151 if (getterID<0) goto error;
152
153 tailnumID = xplra_multi_add_byte_array(getterID, "sim/aircraft/view/acf_tailnum", 40, 0);
154 daysID = xplra_multi_add_int(getterID, "sim/time/local_date_days");
155 zuluSecID = xplra_multi_add_float(getterID, "sim/time/zulu_time_sec");
156 pausedID = xplra_multi_add_int(getterID, "sim/time/paused");
157 latitudeID = xplra_multi_add_double(getterID, "sim/flightmodel/position/latitude");
158 longitudeID = xplra_multi_add_double(getterID, "sim/flightmodel/position/longitude");
159 replayID = xplra_multi_add_int(getterID, "sim/operation/prefs/replay_mode");
160 overspeedID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/over_vne");
161 stalledID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/stallwarning");
162 onTheGroundID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/onground_any");
163 emptyWeightID = xplra_multi_add_float(getterID, "sim/aircraft/weight/acf_m_empty");
164 payloadWeightID = xplra_multi_add_float(getterID, "sim/flightmodel/weight/m_fixed");
165 grossWeightID = xplra_multi_add_float(getterID, "sim/flightmodel/weight/m_total");
166 headingID = xplra_multi_add_float(getterID, "sim/flightmodel/position/psi");
167 pitchID = xplra_multi_add_float(getterID, "sim/flightmodel/position/theta");
168 bankID = xplra_multi_add_float(getterID, "sim/flightmodel/position/phi");
169 iasID = xplra_multi_add_float(getterID, "sim/flightmodel/position/indicated_airspeed2");
170 machID = xplra_multi_add_float(getterID, "sim/flightmodel/misc/machno");
171 groundSpeedID = xplra_multi_add_float(getterID, "sim/flightmodel/position/groundspeed");
172 vsID = xplra_multi_add_float(getterID, "sim/flightmodel/position/vh_ind_fpm2");
173 radioAltitudeID = xplra_multi_add_float(getterID, "sim/flightmodel/position/y_agl");
174 altitudeID = xplra_multi_add_float(getterID, "sim/flightmodel/position/elevation");
175 gLoadID = xplra_multi_add_float(getterID, "sim/flightmodel/forces/g_nrml");
176 flapsControlID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flaprqst");
177 flapsLeftID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flaprat");
178 flapsRightID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flap2rat");
179 navLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/nav_lights_on");
180 beaconLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/beacon_lights_on");
181 strobeLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/strobe_lights_on");
182 landingLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/landing_lights_on");
183 pitotID = xplra_multi_add_int(getterID, "sim/cockpit/switches/pitot_heat_on");
184 parkingID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/parkbrake");
185 gearControlID = xplra_multi_add_int(getterID, "sim/cockpit2/controls/gear_handle_down");
186 noseGearID = xplra_multi_add_float_array(getterID, "sim/flightmodel2/gear/deploy_ratio", 1, 0);
187 altimeterID = xplra_multi_add_float(getterID, "sim/cockpit/misc/barometer_setting");
188 qnhID = xplra_multi_add_float(getterID, "sim/flightmodel/misc/Qstatic");
189 navTypesID = xplra_multi_add_int_array(getterID, "sim/cockpit/radios/nav_type", 6, 0);
190 busVoltsID = xplra_multi_add_float_array(getterID, "sim/cockpit2/electrical/bus_volts", 4, 0);
191
192 if (dontregister) {
193 xplra_multi_finalize(getterID);
194 } else {
195 printf("Registering getter...\n");
196 if (xplra_multi_register(getterID)<0) goto error;
197 printf("Registered getter.\n\n");
198 }
199
200 tailnum = xplra_multi_get_string_ptr(getterID, tailnumID, 0);
201 days = xplra_multi_get_int_ptr(getterID, daysID);
202 zuluSec = xplra_multi_get_float_ptr(getterID, zuluSecID);
203 paused = xplra_multi_get_int_ptr(getterID, pausedID);
204 latitude = xplra_multi_get_double_ptr(getterID, latitudeID);
205 longitude = xplra_multi_get_double_ptr(getterID, longitudeID);
206 // frozen
207 // slew
208 replay = xplra_multi_get_int_ptr(getterID, replayID);
209 overspeed = xplra_multi_get_int_ptr(getterID, overspeedID);
210 stalled = xplra_multi_get_int_ptr(getterID, stalledID);
211 onTheGround = xplra_multi_get_int_ptr(getterID, onTheGroundID);
212 emptyWeight = xplra_multi_get_float_ptr(getterID, emptyWeightID);
213 payloadWeight = xplra_multi_get_float_ptr(getterID, payloadWeightID);
214 grossWeight = xplra_multi_get_float_ptr(getterID, grossWeightID);
215 heading = xplra_multi_get_float_ptr(getterID, headingID);
216 pitch = xplra_multi_get_float_ptr(getterID, pitchID);
217 bank = xplra_multi_get_float_ptr(getterID, bankID);
218 ias = xplra_multi_get_float_ptr(getterID, iasID);
219 mach = xplra_multi_get_float_ptr(getterID, machID);
220 groundSpeed = xplra_multi_get_float_ptr(getterID, groundSpeedID);
221 vs = xplra_multi_get_float_ptr(getterID, vsID);
222 radioAltitude = xplra_multi_get_float_ptr(getterID, radioAltitudeID);
223 altitude = xplra_multi_get_float_ptr(getterID, altitudeID);
224 gLoad = xplra_multi_get_float_ptr(getterID, gLoadID);
225 flapsControl = xplra_multi_get_float_ptr(getterID, flapsControlID);
226 flapsLeft = xplra_multi_get_float_ptr(getterID, flapsLeftID);
227 flapsRight = xplra_multi_get_float_ptr(getterID, flapsRightID);
228 navLights = xplra_multi_get_int_ptr(getterID, navLightsID);
229 beaconLights = xplra_multi_get_int_ptr(getterID, beaconLightsID);
230 strobeLights = xplra_multi_get_int_ptr(getterID, strobeLightsID);
231 landingLights = xplra_multi_get_int_ptr(getterID, landingLightsID);
232 pitot = xplra_multi_get_int_ptr(getterID, pitotID);
233 parking = xplra_multi_get_float_ptr(getterID, parkingID);
234 gearControl = xplra_multi_get_int_ptr(getterID, gearControlID);
235 noseGear = xplra_multi_get_float_array_ptr(getterID, noseGearID, 0);
236 altimeter = xplra_multi_get_float_ptr(getterID, altimeterID);
237 qnh = xplra_multi_get_float_ptr(getterID, qnhID);
238 navTypes = xplra_multi_get_int_array_ptr(getterID, navTypesID, 0);
239 busVolts = xplra_multi_get_float_array_ptr(getterID, busVoltsID, 0);
240
241 while(1) {
242 if (xplra_multi_execute(getterID)<0) goto error;
243 printf("tail number: '%s', days: %d, zuluSec: %f\n",
244 tailnum, *days, *zuluSec);
245 printf("paused=%d, replay=%d, overspeed=%d, stalled=%d, onTheGround=%d\n",
246 *paused, *replay, *overspeed, *stalled, *onTheGround);
247 printf("emptyWeight=%f, payloadWeight=%f, grossWeight=%f\n",
248 *emptyWeight, *payloadWeight, *grossWeight);
249 printf("latitude=%f, longitude=%f, radioAltitude=%f, altitude=%f, gLoad=%f\n",
250 *latitude, *longitude, (*radioAltitude)/.3048, (*altitude)/.3048, *gLoad);
251 printf("heading=%f, pitch=%f, bank=%f, ias=%f, mach=%f, groundSpeed=%f, vs=%f\n",
252 *heading, *pitch, *bank, *ias, *mach, *groundSpeed, *vs);
253 printf("flapsControl=%f, flapsLeft=%f, flapsRight=%f\n",
254 *flapsControl, *flapsLeft, *flapsRight);
255 printf("Lights: NAV: %d, beacon: %d, strobe: %d, landing: %d\n",
256 *navLights, *beaconLights, *strobeLights, *landingLights);
257 printf("pitot: %d, parking: %f, gearControl: %d, noseGear: %f\n",
258 *pitot, *parking, *gearControl, *noseGear);
259 printf("altimeter: %f, qnh: %f\n",
260 *altimeter, *qnh);
261 printf("NAV types: %d %d %d %d %d %d\n",
262 navTypes[0], navTypes[1], navTypes[2],
263 navTypes[3], navTypes[4], navTypes[5]);
264 printf("bus voltages: %f %f %f %f\n",
265 busVolts[0], busVolts[1], busVolts[2], busVolts[3]);
266 printf("\n");
267#ifdef _WIN32
268 Sleep(1000);
269#else
270 usleep(1000*1000);
271#endif
272 }
273
274 goto cleanup;
275
276error:
277 errorString = xplra_get_last_error_string(connectionID);
278 if (errorString==0) {
279 printf("\nUnknown error occured!\n");
280 } else {
281 printf("\nError: %s\n", errorString);
282 }
283
284 retval = 1;
285
286cleanup:
287 if (connectionID>=0) xplra_disconnect(connectionID);
288 return retval;
289
290 // try {
291
292
293 // while(true) {
294 // getter.execute();
295 // printf("tail number: '%s', days: %d, zuluSec: %f\n",
296 // getter.getString(tailnumID).c_str(), days, zuluSec);
297 // printf("paused=%d, replay=%d, overspeed=%d, stalled=%d, onTheGround=%d\n",
298 // paused, replay, overspeed, stalled, onTheGround);
299 // printf("emptyWeight=%f, payloadWeight=%f, grossWeight=%f\n",
300 // emptyWeight, payloadWeight, grossWeight);
301 // printf("latitude=%f, longitude=%f, radioAltitude=%f, altitude=%f, gLoad=%f\n",
302 // latitude, longitude, radioAltitude/.3048, altitude/.3048, gLoad);
303 // printf("heading=%f, pitch=%f, bank=%f, ias=%f, mach=%f, groundSpeed=%f, vs=%f\n",
304 // heading, pitch,bank, ias, mach, groundSpeed, vs);
305 // printf("flapsControl=%f, flapsLeft=%f, flapsRight=%f\n",
306 // flapsControl, flapsLeft, flapsRight);
307 // printf("Lights: NAV: %d, beacon: %d, strobe: %d, landing: %d\n",
308 // navLights, beaconLights, strobeLights, landingLights);
309 // printf("pitot: %d, parking: %f, gearControl: %d, noseGear: %f\n",
310 // pitot, parking, gearControl, noseGear);
311 // printf("altimeter: %f, qnh: %f\n",
312 // altimeter, qnh);
313 // printf("NAV types: %d %d %d %d %d %d\n",
314 // navTypes[0], navTypes[1], navTypes[2],
315 // navTypes[3], navTypes[4], navTypes[5]);
316 // printf("bus voltages: %f %f %f %f\n",
317 // busVolts[0], busVolts[1], busVolts[2], busVolts[3]);
318 // printf("\n");
319 // Thread::sleep(1000);
320 // }
321
322 // //xplane.destroyMultiBuffer(getter);
323
324 // return 0;
325 // } catch(Exception& exception) {
326 // printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
327
328 // return 1;
329 // }
330}
331
332//------------------------------------------------------------------------------
333
334// Local Variables:
335// mode: C++
336// c-basic-offset: 4
337// indent-tabs-mode: nil
338// End:
Note: See TracBrowser for help on using the repository browser.