source: xplra/test/multigetctest.c@ 112:58892a32a039

Last change on this file since 112:58892a32a039 was 112:58892a32a039, checked in by István Váradi <ivaradi@…>, 17 months ago

The test programs can be parameterized to connect over TCP

File size: 16.5 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 "ccommon.h"
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38
39#ifdef _WIN32
40#include <windows.h>
41#else
42#include <unistd.h>
43#endif
44
45//------------------------------------------------------------------------------
46
47int main(int argc, char* argv[])
48{
49 int retval = 0;
50 const char* errorString;
51
52 int i;
53
54 int connectionID = -1;
55 int wrongGetterID = -1;
56 int getterID = -1;
57
58 size_t tailnumID = INVALID_DATAREF_ID;
59 size_t daysID = INVALID_DATAREF_ID;
60 size_t zuluSecID = INVALID_DATAREF_ID;
61 size_t pausedID = INVALID_DATAREF_ID;
62 size_t latitudeID = INVALID_DATAREF_ID;
63 size_t longitudeID = INVALID_DATAREF_ID;
64 size_t replayID = INVALID_DATAREF_ID;
65 size_t overspeedID = INVALID_DATAREF_ID;
66 size_t stalledID = INVALID_DATAREF_ID;
67 size_t onTheGroundID = INVALID_DATAREF_ID;
68 size_t emptyWeightID = INVALID_DATAREF_ID;
69 size_t payloadWeightID = INVALID_DATAREF_ID;
70 size_t grossWeightID = INVALID_DATAREF_ID;
71 size_t headingID = INVALID_DATAREF_ID;
72 size_t pitchID = INVALID_DATAREF_ID;
73 size_t bankID = INVALID_DATAREF_ID;
74 size_t iasID = INVALID_DATAREF_ID;
75 size_t machID = INVALID_DATAREF_ID;
76 size_t groundSpeedID = INVALID_DATAREF_ID;
77 size_t vsID = INVALID_DATAREF_ID;
78 size_t radioAltitudeID = INVALID_DATAREF_ID;
79 size_t altitudeID = INVALID_DATAREF_ID;
80 size_t gLoadID = INVALID_DATAREF_ID;
81 size_t flapsControlID = INVALID_DATAREF_ID;
82 size_t flapsLeftID = INVALID_DATAREF_ID;
83 size_t flapsRightID = INVALID_DATAREF_ID;
84 size_t navLightsID = INVALID_DATAREF_ID;
85 size_t beaconLightsID = INVALID_DATAREF_ID;
86 size_t strobeLightsID = INVALID_DATAREF_ID;
87 size_t landingLightsID = INVALID_DATAREF_ID;
88 size_t pitotID = INVALID_DATAREF_ID;
89 size_t parkingID = INVALID_DATAREF_ID;
90 size_t gearControlID = INVALID_DATAREF_ID;
91 size_t noseGearID = INVALID_DATAREF_ID;
92 size_t altimeterID = INVALID_DATAREF_ID;
93 size_t qnhID = INVALID_DATAREF_ID;
94 size_t navTypesID = INVALID_DATAREF_ID;
95 size_t busVoltsID = INVALID_DATAREF_ID;
96
97 const char* tailnum = 0;
98 const int32_t* days = 0;
99 const float* zuluSec = 0;
100 const int32_t* paused = 0;
101 const double* latitude = 0;
102 const double* longitude = 0;
103 const int32_t* replay = 0;
104 const int32_t* overspeed = 0;
105 const int32_t* stalled = 0;
106 const int32_t* onTheGround = 0;
107 const float* emptyWeight = 0;
108 const float* payloadWeight = 0;
109 const float* grossWeight = 0;
110 const float* heading = 0;
111 const float* pitch = 0;
112 const float* bank = 0;
113 const float* ias = 0;
114 const float* mach = 0;
115 const float* groundSpeed = 0;
116 const float* vs = 0;
117 const float* radioAltitude = 0;
118 const float* altitude = 0;
119 const float* gLoad = 0;
120 const float* flapsControl = 0;
121 const float* flapsLeft = 0;
122 const float* flapsRight = 0;
123 const int32_t* navLights = 0;
124 const int32_t* beaconLights = 0;
125 const int32_t* strobeLights = 0;
126 const int32_t* landingLights = 0;
127 const int32_t* pitot = 0;
128 const float* parking = 0;
129 const int32_t* gearControl = 0;
130 const float* noseGear = 0;
131 const float* altimeter = 0;
132 const float* qnh = 0;
133 const int32_t* navTypes = 0;
134 const float* busVolts = 0;
135
136 int dontregister = 0;
137
138 for(i = 1; i<argc; ++i) {
139 if (strcmp(argv[i], "dontregister")==0) {
140 dontregister = 1;
141 }
142 }
143
144 printf("Connecting to X-Plane...\n");
145 connectionID = connect_xplane(argc, argv);
146 if (connectionID>=0) {
147 printf("Connected to X-Plane.\n\n");
148 } else {
149 printf("Connection failed.\n\n");
150 goto cleanup;
151 }
152
153 wrongGetterID = xplra_multi_create_getter(connectionID);
154 if (wrongGetterID<0) goto error;
155 xplra_multi_add_int(wrongGetterID, "sim/time/local_date_days");
156 xplra_multi_add_int(wrongGetterID, "sim/time/paused");
157 xplra_multi_add_int(wrongGetterID, "sim/time/hello_te_lo");
158 xplra_multi_add_float(wrongGetterID, "sim/time/zulu_time_sec");
159 xplra_multi_add_int(wrongGetterID, "sim/operation/prefs/replay_mode");
160
161 printf("Executing a multi-dataref getter with an invalid dataref...\n");
162 if (xplra_multi_execute(wrongGetterID)<0) {
163 printf("Error occured: %s\n\n",
164 xplra_get_last_error_string(connectionID));
165 xplra_clear_last_error(connectionID);
166 } else {
167 printf(">>>>>>>>>>>>>>>>>>>>> Succeeded, but it should not have!!!!\n\n");
168 }
169
170 printf("Executing a registered multi-dataref getter with an invalid dataref...\n");
171 if (xplra_multi_register(wrongGetterID)<0) goto error;
172 if (xplra_multi_execute(wrongGetterID)<0) {
173 printf("Error occured: %s\n\n",
174 xplra_get_last_error_string(connectionID));
175 xplra_clear_last_error(connectionID);
176 } else {
177 printf(">>>>>>>>>>>>>>>>>>>>> Succeeded, but it should not have!!!!\n\n");
178 }
179
180
181 getterID = xplra_multi_create_getter(connectionID);
182 if (getterID<0) goto error;
183
184 tailnumID = xplra_multi_add_byte_array(getterID, "sim/aircraft/view/acf_tailnum", 40, 0);
185 daysID = xplra_multi_add_int(getterID, "sim/time/local_date_days");
186 zuluSecID = xplra_multi_add_float(getterID, "sim/time/zulu_time_sec");
187 pausedID = xplra_multi_add_int(getterID, "sim/time/paused");
188 latitudeID = xplra_multi_add_double(getterID, "sim/flightmodel/position/latitude");
189 longitudeID = xplra_multi_add_double(getterID, "sim/flightmodel/position/longitude");
190 replayID = xplra_multi_add_int(getterID, "sim/operation/prefs/replay_mode");
191 overspeedID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/over_vne");
192 stalledID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/stallwarning");
193 onTheGroundID = xplra_multi_add_int(getterID, "sim/flightmodel/failures/onground_any");
194 emptyWeightID = xplra_multi_add_float(getterID, "sim/aircraft/weight/acf_m_empty");
195 payloadWeightID = xplra_multi_add_float(getterID, "sim/flightmodel/weight/m_fixed");
196 grossWeightID = xplra_multi_add_float(getterID, "sim/flightmodel/weight/m_total");
197 headingID = xplra_multi_add_float(getterID, "sim/flightmodel/position/psi");
198 pitchID = xplra_multi_add_float(getterID, "sim/flightmodel/position/theta");
199 bankID = xplra_multi_add_float(getterID, "sim/flightmodel/position/phi");
200 iasID = xplra_multi_add_float(getterID, "sim/flightmodel/position/indicated_airspeed2");
201 machID = xplra_multi_add_float(getterID, "sim/flightmodel/misc/machno");
202 groundSpeedID = xplra_multi_add_float(getterID, "sim/flightmodel/position/groundspeed");
203 vsID = xplra_multi_add_float(getterID, "sim/flightmodel/position/vh_ind_fpm2");
204 radioAltitudeID = xplra_multi_add_float(getterID, "sim/flightmodel/position/y_agl");
205 altitudeID = xplra_multi_add_float(getterID, "sim/flightmodel/position/elevation");
206 gLoadID = xplra_multi_add_float(getterID, "sim/flightmodel/forces/g_nrml");
207 flapsControlID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flaprqst");
208 flapsLeftID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flaprat");
209 flapsRightID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/flap2rat");
210 navLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/nav_lights_on");
211 beaconLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/beacon_lights_on");
212 strobeLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/strobe_lights_on");
213 landingLightsID = xplra_multi_add_int(getterID, "sim/cockpit/electrical/landing_lights_on");
214 pitotID = xplra_multi_add_int(getterID, "sim/cockpit/switches/pitot_heat_on");
215 parkingID = xplra_multi_add_float(getterID, "sim/flightmodel/controls/parkbrake");
216 gearControlID = xplra_multi_add_int(getterID, "sim/cockpit2/controls/gear_handle_down");
217 noseGearID = xplra_multi_add_float_array(getterID, "sim/flightmodel2/gear/deploy_ratio", 1, 0);
218 altimeterID = xplra_multi_add_float(getterID, "sim/cockpit/misc/barometer_setting");
219 qnhID = xplra_multi_add_float(getterID, "sim/flightmodel/misc/Qstatic");
220 navTypesID = xplra_multi_add_int_array(getterID, "sim/cockpit/radios/nav_type", 6, 0);
221 busVoltsID = xplra_multi_add_float_array(getterID, "sim/cockpit2/electrical/bus_volts", 4, 0);
222
223 if (dontregister) {
224 xplra_multi_finalize(getterID);
225 } else {
226 printf("Registering getter...\n");
227 if (xplra_multi_register(getterID)<0) goto error;
228 printf("Registered getter.\n\n");
229 }
230
231 tailnum = xplra_multi_get_string_ptr(getterID, tailnumID, 0);
232 days = xplra_multi_get_int_ptr(getterID, daysID);
233 zuluSec = xplra_multi_get_float_ptr(getterID, zuluSecID);
234 paused = xplra_multi_get_int_ptr(getterID, pausedID);
235 latitude = xplra_multi_get_double_ptr(getterID, latitudeID);
236 longitude = xplra_multi_get_double_ptr(getterID, longitudeID);
237 // frozen
238 // slew
239 replay = xplra_multi_get_int_ptr(getterID, replayID);
240 overspeed = xplra_multi_get_int_ptr(getterID, overspeedID);
241 stalled = xplra_multi_get_int_ptr(getterID, stalledID);
242 onTheGround = xplra_multi_get_int_ptr(getterID, onTheGroundID);
243 emptyWeight = xplra_multi_get_float_ptr(getterID, emptyWeightID);
244 payloadWeight = xplra_multi_get_float_ptr(getterID, payloadWeightID);
245 grossWeight = xplra_multi_get_float_ptr(getterID, grossWeightID);
246 heading = xplra_multi_get_float_ptr(getterID, headingID);
247 pitch = xplra_multi_get_float_ptr(getterID, pitchID);
248 bank = xplra_multi_get_float_ptr(getterID, bankID);
249 ias = xplra_multi_get_float_ptr(getterID, iasID);
250 mach = xplra_multi_get_float_ptr(getterID, machID);
251 groundSpeed = xplra_multi_get_float_ptr(getterID, groundSpeedID);
252 vs = xplra_multi_get_float_ptr(getterID, vsID);
253 radioAltitude = xplra_multi_get_float_ptr(getterID, radioAltitudeID);
254 altitude = xplra_multi_get_float_ptr(getterID, altitudeID);
255 gLoad = xplra_multi_get_float_ptr(getterID, gLoadID);
256 flapsControl = xplra_multi_get_float_ptr(getterID, flapsControlID);
257 flapsLeft = xplra_multi_get_float_ptr(getterID, flapsLeftID);
258 flapsRight = xplra_multi_get_float_ptr(getterID, flapsRightID);
259 navLights = xplra_multi_get_int_ptr(getterID, navLightsID);
260 beaconLights = xplra_multi_get_int_ptr(getterID, beaconLightsID);
261 strobeLights = xplra_multi_get_int_ptr(getterID, strobeLightsID);
262 landingLights = xplra_multi_get_int_ptr(getterID, landingLightsID);
263 pitot = xplra_multi_get_int_ptr(getterID, pitotID);
264 parking = xplra_multi_get_float_ptr(getterID, parkingID);
265 gearControl = xplra_multi_get_int_ptr(getterID, gearControlID);
266 noseGear = xplra_multi_get_float_array_ptr(getterID, noseGearID, 0);
267 altimeter = xplra_multi_get_float_ptr(getterID, altimeterID);
268 qnh = xplra_multi_get_float_ptr(getterID, qnhID);
269 navTypes = xplra_multi_get_int_array_ptr(getterID, navTypesID, 0);
270 busVolts = xplra_multi_get_float_array_ptr(getterID, busVoltsID, 0);
271
272 while(1) {
273 if (xplra_multi_execute(getterID)<0) goto error;
274 printf("tail number: '%s', days: %d, zuluSec: %f\n",
275 tailnum, *days, *zuluSec);
276 printf("paused=%d, replay=%d, overspeed=%d, stalled=%d, onTheGround=%d\n",
277 *paused, *replay, *overspeed, *stalled, *onTheGround);
278 printf("emptyWeight=%f, payloadWeight=%f, grossWeight=%f\n",
279 *emptyWeight, *payloadWeight, *grossWeight);
280 printf("latitude=%f, longitude=%f, radioAltitude=%f, altitude=%f, gLoad=%f\n",
281 *latitude, *longitude, (*radioAltitude)/.3048, (*altitude)/.3048, *gLoad);
282 printf("heading=%f, pitch=%f, bank=%f, ias=%f, mach=%f, groundSpeed=%f, vs=%f\n",
283 *heading, *pitch, *bank, *ias, *mach, *groundSpeed, *vs);
284 printf("flapsControl=%f, flapsLeft=%f, flapsRight=%f\n",
285 *flapsControl, *flapsLeft, *flapsRight);
286 printf("Lights: NAV: %d, beacon: %d, strobe: %d, landing: %d\n",
287 *navLights, *beaconLights, *strobeLights, *landingLights);
288 printf("pitot: %d, parking: %f, gearControl: %d, noseGear: %f\n",
289 *pitot, *parking, *gearControl, *noseGear);
290 printf("altimeter: %f, qnh: %f\n",
291 *altimeter, *qnh);
292 printf("NAV types: %d %d %d %d %d %d\n",
293 navTypes[0], navTypes[1], navTypes[2],
294 navTypes[3], navTypes[4], navTypes[5]);
295 printf("bus voltages: %f %f %f %f\n",
296 busVolts[0], busVolts[1], busVolts[2], busVolts[3]);
297 printf("\n");
298#ifdef _WIN32
299 Sleep(1000);
300#else
301 usleep(1000*1000);
302#endif
303 }
304
305 goto cleanup;
306
307error:
308 errorString = xplra_get_last_error_string(connectionID);
309 if (errorString==0) {
310 printf("\nUnknown error occured!\n");
311 } else {
312 printf("\nError: %s\n", errorString);
313 }
314
315 retval = 1;
316
317cleanup:
318 if (connectionID>=0) xplra_disconnect(connectionID);
319 return retval;
320
321 // try {
322
323
324 // while(true) {
325 // getter.execute();
326 // printf("tail number: '%s', days: %d, zuluSec: %f\n",
327 // getter.getString(tailnumID).c_str(), days, zuluSec);
328 // printf("paused=%d, replay=%d, overspeed=%d, stalled=%d, onTheGround=%d\n",
329 // paused, replay, overspeed, stalled, onTheGround);
330 // printf("emptyWeight=%f, payloadWeight=%f, grossWeight=%f\n",
331 // emptyWeight, payloadWeight, grossWeight);
332 // printf("latitude=%f, longitude=%f, radioAltitude=%f, altitude=%f, gLoad=%f\n",
333 // latitude, longitude, radioAltitude/.3048, altitude/.3048, gLoad);
334 // printf("heading=%f, pitch=%f, bank=%f, ias=%f, mach=%f, groundSpeed=%f, vs=%f\n",
335 // heading, pitch,bank, ias, mach, groundSpeed, vs);
336 // printf("flapsControl=%f, flapsLeft=%f, flapsRight=%f\n",
337 // flapsControl, flapsLeft, flapsRight);
338 // printf("Lights: NAV: %d, beacon: %d, strobe: %d, landing: %d\n",
339 // navLights, beaconLights, strobeLights, landingLights);
340 // printf("pitot: %d, parking: %f, gearControl: %d, noseGear: %f\n",
341 // pitot, parking, gearControl, noseGear);
342 // printf("altimeter: %f, qnh: %f\n",
343 // altimeter, qnh);
344 // printf("NAV types: %d %d %d %d %d %d\n",
345 // navTypes[0], navTypes[1], navTypes[2],
346 // navTypes[3], navTypes[4], navTypes[5]);
347 // printf("bus voltages: %f %f %f %f\n",
348 // busVolts[0], busVolts[1], busVolts[2], busVolts[3]);
349 // printf("\n");
350 // Thread::sleep(1000);
351 // }
352
353 // //xplane.destroyMultiBuffer(getter);
354
355 // return 0;
356 // } catch(Exception& exception) {
357 // printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
358
359 // return 1;
360 // }
361}
362
363//------------------------------------------------------------------------------
364
365// Local Variables:
366// mode: C++
367// c-basic-offset: 4
368// indent-tabs-mode: nil
369// End:
Note: See TracBrowser for help on using the repository browser.