source: xplra/test/basicctest.c

Last change on this file was 114:0e60b2a26b42, checked in by István Váradi <ivaradi@…>, 17 months ago

Modified test case originally causing X-Plane 12 to crash

File size: 14.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 <stdio.h>
36#include <string.h>
37
38#ifdef _WIN32
39#include <windows.h>
40#else
41#include <unistd.h>
42#endif
43
44//------------------------------------------------------------------------------
45
46#ifdef _WIN32
47void xplra_sleep(int ms)
48{
49 Sleep(ms);
50}
51#else
52void xplra_sleep(int ms)
53{
54 usleep(ms*1000);
55}
56#endif
57
58
59//------------------------------------------------------------------------------
60
61int main(int argc, char* argv[])
62{
63 static const char* tailNum1 = "VAI";
64
65 int retval = 0;
66 const char* errorString = 0;
67
68 int xplaneVersion = 0;
69 int xplmVersion = 0;
70 int xplraVersion = 0;
71
72 int numEngines = 0;
73 float spoolTime = 0.0;
74 double latitude = 0.0, longitude = 0.0;
75 ssize_t length, i;
76 size_t length1, j;
77 uint8_t result[260];
78 uint8_t* result1 = 0;
79 int32_t result3[8];
80 int32_t* result4 = 0;
81 float result5[8];
82 float* result6 = 0;
83 float acfElevUp;
84 double localX;
85 float numBlades[8];
86 int32_t batteryArrayOn[8];
87 uint8_t tailNum[40];
88
89 printf("Connection to X-Plane...\n");
90 int connectionID = connect_xplane(argc, argv);
91
92 if (connectionID>=0) {
93 printf("Connected.\n\n");
94 } else {
95 printf("Connection failed.\n");
96 goto cleanup;
97 }
98
99 printf("Showing a message...\n");
100 if (xplra_show_message(connectionID, "[basictest] Starting tests", 5.0)<0) goto error;
101 printf("\n");
102
103 printf("Querying the versions...\n");
104 if (xplra_get_versions(connectionID, &xplaneVersion,
105 &xplmVersion, &xplraVersion)<0) goto error;
106 printf("X-Plane version: %d, XPLM: %d, XPLRA: %03d\n\n",
107 xplaneVersion, xplmVersion, xplraVersion);
108
109 printf("Saving the situation...\n");
110 if (xplra_save_situation(connectionID, "output/situations/test.sit")<0) goto error;
111 printf("Saved the situation\n\n");
112
113 printf("Querying the number of the engines...\n");
114 if (xplra_get_int(&numEngines, connectionID,
115 "sim/aircraft/engine/acf_num_engines")<0)
116 {
117 goto error;
118 }
119 printf("The number of engines: %d\n\n", numEngines);
120
121 printf("Querying an invalid dataref...\n");
122 if (xplra_get_int(&numEngines, connectionID,
123 "sim/aircraft/engine/num_engines")==0) {
124 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
125 } else {
126 printf("Error occured: %s\n\n",
127 xplra_get_last_error_string(connectionID));
128 xplra_clear_last_error(connectionID);
129 }
130
131 printf("Querying the spool time of a jet engine...\n");
132 if (xplra_get_float(&spoolTime, connectionID,
133 "sim/aircraft/engine/acf_spooltime_jet")<0)
134 {
135 goto error;
136 }
137 printf("The spool time: %f\n\n", spoolTime);
138
139 printf("Querying the spool time of a propeller...\n");
140 if (xplra_get_float(&spoolTime, connectionID,
141 "sim/aircraft/engine/acf_spooltime_prop")<0)
142 {
143 goto error;
144 }
145 printf("The spool time: %f\n\n", spoolTime);
146
147 printf("Querying the coordinates...\n");
148 if (xplra_get_double(&latitude, connectionID,
149 "sim/flightmodel/position/latitude")<0)
150 {
151 goto error;
152 }
153 if (xplra_get_double(&longitude, connectionID,
154 "sim/flightmodel/position/longitude")<0)
155 {
156 goto error;
157 }
158 printf("The coordinates: %f, %f\n\n", latitude, longitude);
159
160 printf("Querying the aircraft's description as an array of a fixed size...\n");
161 length = xplra_get_byte_array(result, sizeof(result)/sizeof(result[0]),
162 0, connectionID,
163 "sim/aircraft/view/acf_descrip");
164 if (length<0) goto error;
165 printf("Got %lu bytes, as a string: '%s'\n\n", (unsigned long)length, result);
166
167 printf("Querying the aircraft's description as a dynamically created array with an offset of 3...\n");
168 length1 = 0;
169 result1 = xplra_get_byte_array_new(&length1, 3, connectionID,
170 "sim/aircraft/view/acf_descrip");
171 if (result1==0) goto error;
172 printf("Got %lu bytes, as a string: '%s'\n\n", (unsigned long)length1, result1);
173
174 printf("Querying the aircraft's engine types as an array of a fixed size...\n");
175 length = xplra_get_int_array(result3, sizeof(result3)/sizeof(result3[0]), 0,
176 connectionID, "sim/aircraft/prop/acf_en_type");
177
178 if (length<0) goto error;
179 printf("Got %ld values:", (long)length);
180 for(i = 0; i<length; ++i) {
181 if (i>0) printf(",");
182 printf(" %d", result3[i]);
183 }
184 printf("\n\n");
185
186 printf("Querying the aircraft's engine types as a dynamically created array...\n");
187 length1 = 0;
188 result4 = xplra_get_int_array_new(&length1, 0, connectionID,
189 "sim/aircraft/prop/acf_en_type");
190 printf("Got %lu values:", (unsigned long)length1);
191 for(j = 0; j<length1; ++j) {
192 if (j>0) printf(",");
193 printf(" %d", result4[j]);
194 }
195 printf("\n\n");
196
197 printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
198 length = xplra_get_float_array(result5, sizeof(result5)/sizeof(result5[0]),
199 0, connectionID,
200 "sim/aircraft/prop/acf_prop_dir");
201 printf("Got %ld values:", (long)length);
202 for(i = 0; i<length; ++i) {
203 if (i>0) printf(",");
204 printf(" %f", result5[i]);
205 }
206 printf("\n\n");
207
208 printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
209 length1 = 0;
210 result6 = xplra_get_float_array_new(&length1, 0, connectionID,
211 "sim/aircraft/prop/acf_prop_dir");
212 printf("Got %lu values:", (unsigned long)length1);
213 for(j = 0; j<length1; ++j) {
214 if (j>0) printf(",");
215 printf(" %f", result6[j]);
216 }
217 printf("\n\n");
218
219
220 if (xplaneVersion<12000 || xplaneVersion>12005) {
221 printf("Setting the number of the engines to %d...\n", numEngines + 1);
222 if (xplra_set_int(connectionID, "sim/aircraft/engine/acf_num_engines",
223 numEngines + 1)<0)
224 {
225 goto error;
226 }
227 if (xplra_get_int(&numEngines, connectionID,
228 "sim/aircraft/engine/acf_num_engines")<0)
229 {
230 goto error;
231 }
232 printf("The new number of engines: %d\n\n", numEngines);
233 } else {
234 if (xplra_get_int(&numEngines, connectionID,
235 "sim/aircraft/engine/acf_num_engines")<0)
236 {
237 goto error;
238 }
239 printf("Not setting the number of engines, which is: %d\n\n", numEngines);
240 }
241
242
243 if (xplra_get_float(&acfElevUp, connectionID,
244 "sim/aircraft/controls/acf_elev_up")<0)
245 {
246 goto error;
247 }
248 printf("Setting the aircraft elevator up control from %f to %f...\n",
249 acfElevUp, acfElevUp + 15.0);
250 if (xplra_set_float(connectionID,
251 "sim/aircraft/controls/acf_elev_up",
252 acfElevUp + 15.0) < 0)
253 {
254 goto error;
255 }
256 if (xplra_get_float(&acfElevUp, connectionID,
257 "sim/aircraft/controls/acf_elev_up")<0)
258 {
259 goto error;
260 }
261 printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
262
263 if (xplra_get_double(&localX, connectionID,
264 "sim/flightmodel/position/local_x")<0)
265 {
266 goto error;
267 }
268 printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
269 localX, localX + 15.0);
270 if (xplra_set_double(connectionID, "sim/flightmodel/position/local_x",
271 localX + 15.0)<0)
272 {
273 goto error;
274 }
275 if (xplra_get_double(&localX, connectionID,
276 "sim/flightmodel/position/local_x")<0)
277 {
278 goto error;
279 }
280 printf("The aircraft's local X-coordinate set to %f\n\n", localX);
281
282 length = xplra_get_float_array(numBlades,
283 sizeof(numBlades)/sizeof(numBlades[0]), 0,
284 connectionID,
285 "sim/aircraft/prop/acf_num_blades");
286 if (length<0) goto error;
287
288 printf("Setting the number of blades\n from:");
289 for(i = 0; i<length; ++i) {
290 if (i>0) printf(",");
291 printf(" %f", numBlades[i]);
292 }
293 printf("\n to:");
294 for(i = 0; i<length; ++i) {
295 numBlades[i] += 2.5;
296 if (i>0) printf(",");
297 printf(" %f", numBlades[i]);
298 }
299 printf("\n");
300 if (xplra_set_float_array(connectionID,
301 "sim/aircraft/prop/acf_num_blades",
302 numBlades, length, 0)<0)
303 {
304 goto error;
305 }
306 length = xplra_get_float_array(numBlades,
307 sizeof(numBlades)/sizeof(numBlades[0]), 0,
308 connectionID,
309 "sim/aircraft/prop/acf_num_blades");
310 if (length<0) goto error;
311 printf("The result:");
312 for(i = 0; i<length; ++i) {
313 if (i>0) printf(",");
314 printf(" %f", numBlades[i]);
315 }
316 printf("\n\n");
317
318 length = xplra_get_int_array(batteryArrayOn,
319 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
320 0, connectionID,
321 "sim/cockpit/electrical/battery_array_on");
322 if (length<0) goto error;
323 printf("Setting the batteries\n from:");
324 for(i = 0; i<length; ++i) {
325 if (i>0) printf(",");
326 printf(" %d", batteryArrayOn[i]);
327 }
328 printf("\n to:");
329 for(i = 0; i<length; ++i) {
330 batteryArrayOn[i] = !batteryArrayOn[i];
331 numBlades[i] += 2.5;
332 if (i>0) printf(",");
333 printf(" %d", batteryArrayOn[i]);
334 }
335 printf("\n");
336 if (xplra_set_int_array(connectionID,
337 "sim/cockpit/electrical/battery_array_on",
338 batteryArrayOn, length, 0)<0)
339 {
340 goto error;
341 }
342 length = xplra_get_int_array(batteryArrayOn,
343 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
344 0, connectionID,
345 "sim/cockpit/electrical/battery_array_on");
346 if (length<0) goto error;
347 printf("The result:");
348 for(i = 0; i<length; ++i) {
349 if (i>0) printf(",");
350 printf(" %d", batteryArrayOn[i]);
351 }
352 printf("\n\n");
353
354 memset(tailNum, 0, sizeof(tailNum));
355 strcpy((char*)tailNum, "HA-VAI");
356 printf("Setting the tail number to %s as a byte array...\n", tailNum);
357 if (xplra_set_byte_array(connectionID,
358 "sim/aircraft/view/acf_tailnum",
359 tailNum, sizeof(tailNum), 0)<0)
360 {
361 goto error;
362 }
363
364 memset(tailNum, 0, sizeof(tailNum));
365 length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
366 connectionID,
367 "sim/aircraft/view/acf_tailnum");
368 if (length<0) goto error;
369 printf("The tail number is: '%s'\n\n", (char*)tailNum);
370
371 printf("Setting the tail number to %s as a string...\n", tailNum1);
372 if (xplra_set_string(connectionID, "sim/aircraft/view/acf_tailnum",
373 tailNum1, 40, 0)<0)
374 {
375 goto error;
376 }
377 memset(tailNum, 0, sizeof(tailNum));
378 length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
379 connectionID,
380 "sim/aircraft/view/acf_tailnum");
381 if (length<0) goto error;
382 printf("The tail number is: '%s'\n\n", (char*)tailNum);
383
384 printf("Preparing for the message tests, sleeping for 5 seconds...\n");
385 xplra_sleep(5*1000);
386
387 printf("Showing a message for 10 seconds...\n");
388 if (xplra_show_message(connectionID, "[basictest] this message appears for 10 seconds", 10.0)<0) {
389 goto error;
390 }
391
392 printf("Sleeping for 3 seconds...\n");
393 xplra_sleep(3*1000);
394
395 printf("Showing another message interrupting the previous one for 3 seconds");
396 if (xplra_show_message(connectionID, "[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0)<0) {
397 goto error;
398 }
399
400 printf("Sleeping for 5 seconds...\n");
401 xplra_sleep(5*1000);
402 if (xplra_show_message(connectionID, "[basictest] and the tests come to an end!", 5.0)<0) {
403 goto error;
404 }
405
406 goto cleanup;
407
408error:
409 errorString = xplra_get_last_error_string(connectionID);
410 if (errorString==0) {
411 printf("\nUnknown error occured!\n");
412 } else {
413 printf("\nError: %s\n", errorString);
414 }
415
416 retval = 1;
417
418cleanup:
419 if (connectionID>=0) xplra_disconnect(connectionID);
420 return retval;
421}
422
423//------------------------------------------------------------------------------
424
425// Local Variables:
426// mode: C++
427// c-basic-offset: 4
428// indent-tabs-mode: nil
429// End:
Note: See TracBrowser for help on using the repository browser.