source: xplra/test/basicctest.c@ 113:14139defbd04

Last change on this file since 113:14139defbd04 was 112:58892a32a039, checked in by István Váradi <ivaradi@…>, 2 years ago

The test programs can be parameterized to connect over TCP

File size: 14.2 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 printf("Setting the number of the engines to %d...\n", numEngines + 1);
220 if (xplra_set_int(connectionID, "sim/aircraft/engine/acf_num_engines",
221 numEngines + 1)<0)
222 {
223 goto error;
224 }
225 if (xplra_get_int(&numEngines, connectionID,
226 "sim/aircraft/engine/acf_num_engines")<0)
227 {
228 goto error;
229 }
230 printf("The new number of engines: %d\n\n", numEngines);
231
232
233 if (xplra_get_float(&acfElevUp, connectionID,
234 "sim/aircraft/controls/acf_elev_up")<0)
235 {
236 goto error;
237 }
238 printf("Setting the aircraft elevator up control from %f to %f...\n",
239 acfElevUp, acfElevUp + 15.0);
240 if (xplra_set_float(connectionID,
241 "sim/aircraft/controls/acf_elev_up",
242 acfElevUp + 15.0) < 0)
243 {
244 goto error;
245 }
246 if (xplra_get_float(&acfElevUp, connectionID,
247 "sim/aircraft/controls/acf_elev_up")<0)
248 {
249 goto error;
250 }
251 printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
252
253 if (xplra_get_double(&localX, connectionID,
254 "sim/flightmodel/position/local_x")<0)
255 {
256 goto error;
257 }
258 printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
259 localX, localX + 15.0);
260 if (xplra_set_double(connectionID, "sim/flightmodel/position/local_x",
261 localX + 15.0)<0)
262 {
263 goto error;
264 }
265 if (xplra_get_double(&localX, connectionID,
266 "sim/flightmodel/position/local_x")<0)
267 {
268 goto error;
269 }
270 printf("The aircraft's local X-coordinate set to %f\n\n", localX);
271
272 length = xplra_get_float_array(numBlades,
273 sizeof(numBlades)/sizeof(numBlades[0]), 0,
274 connectionID,
275 "sim/aircraft/prop/acf_num_blades");
276 if (length<0) goto error;
277
278 printf("Setting the number of blades\n from:");
279 for(i = 0; i<length; ++i) {
280 if (i>0) printf(",");
281 printf(" %f", numBlades[i]);
282 }
283 printf("\n to:");
284 for(i = 0; i<length; ++i) {
285 numBlades[i] += 2.5;
286 if (i>0) printf(",");
287 printf(" %f", numBlades[i]);
288 }
289 printf("\n");
290 if (xplra_set_float_array(connectionID,
291 "sim/aircraft/prop/acf_num_blades",
292 numBlades, length, 0)<0)
293 {
294 goto error;
295 }
296 length = xplra_get_float_array(numBlades,
297 sizeof(numBlades)/sizeof(numBlades[0]), 0,
298 connectionID,
299 "sim/aircraft/prop/acf_num_blades");
300 if (length<0) goto error;
301 printf("The result:");
302 for(i = 0; i<length; ++i) {
303 if (i>0) printf(",");
304 printf(" %f", numBlades[i]);
305 }
306 printf("\n\n");
307
308 length = xplra_get_int_array(batteryArrayOn,
309 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
310 0, connectionID,
311 "sim/cockpit/electrical/battery_array_on");
312 if (length<0) goto error;
313 printf("Setting the batteries\n from:");
314 for(i = 0; i<length; ++i) {
315 if (i>0) printf(",");
316 printf(" %d", batteryArrayOn[i]);
317 }
318 printf("\n to:");
319 for(i = 0; i<length; ++i) {
320 batteryArrayOn[i] = !batteryArrayOn[i];
321 numBlades[i] += 2.5;
322 if (i>0) printf(",");
323 printf(" %d", batteryArrayOn[i]);
324 }
325 printf("\n");
326 if (xplra_set_int_array(connectionID,
327 "sim/cockpit/electrical/battery_array_on",
328 batteryArrayOn, length, 0)<0)
329 {
330 goto error;
331 }
332 length = xplra_get_int_array(batteryArrayOn,
333 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
334 0, connectionID,
335 "sim/cockpit/electrical/battery_array_on");
336 if (length<0) goto error;
337 printf("The result:");
338 for(i = 0; i<length; ++i) {
339 if (i>0) printf(",");
340 printf(" %d", batteryArrayOn[i]);
341 }
342 printf("\n\n");
343
344 memset(tailNum, 0, sizeof(tailNum));
345 strcpy((char*)tailNum, "HA-VAI");
346 printf("Setting the tail number to %s as a byte array...\n", tailNum);
347 if (xplra_set_byte_array(connectionID,
348 "sim/aircraft/view/acf_tailnum",
349 tailNum, sizeof(tailNum), 0)<0)
350 {
351 goto error;
352 }
353
354 memset(tailNum, 0, sizeof(tailNum));
355 length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
356 connectionID,
357 "sim/aircraft/view/acf_tailnum");
358 if (length<0) goto error;
359 printf("The tail number is: '%s'\n\n", (char*)tailNum);
360
361 printf("Setting the tail number to %s as a string...\n", tailNum1);
362 if (xplra_set_string(connectionID, "sim/aircraft/view/acf_tailnum",
363 tailNum1, 40, 0)<0)
364 {
365 goto error;
366 }
367 memset(tailNum, 0, sizeof(tailNum));
368 length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
369 connectionID,
370 "sim/aircraft/view/acf_tailnum");
371 if (length<0) goto error;
372 printf("The tail number is: '%s'\n\n", (char*)tailNum);
373
374 printf("Preparing for the message tests, sleeping for 5 seconds...\n");
375 xplra_sleep(5*1000);
376
377 printf("Showing a message for 10 seconds...\n");
378 if (xplra_show_message(connectionID, "[basictest] this message appears for 10 seconds", 10.0)<0) {
379 goto error;
380 }
381
382 printf("Sleeping for 3 seconds...\n");
383 xplra_sleep(3*1000);
384
385 printf("Showing another message interrupting the previous one for 3 seconds");
386 if (xplra_show_message(connectionID, "[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0)<0) {
387 goto error;
388 }
389
390 printf("Sleeping for 5 seconds...\n");
391 xplra_sleep(5*1000);
392 if (xplra_show_message(connectionID, "[basictest] and the tests come to an end!", 5.0)<0) {
393 goto error;
394 }
395
396 goto cleanup;
397
398error:
399 errorString = xplra_get_last_error_string(connectionID);
400 if (errorString==0) {
401 printf("\nUnknown error occured!\n");
402 } else {
403 printf("\nError: %s\n", errorString);
404 }
405
406 retval = 1;
407
408cleanup:
409 if (connectionID>=0) xplra_disconnect(connectionID);
410 return retval;
411}
412
413//------------------------------------------------------------------------------
414
415// Local Variables:
416// mode: C++
417// c-basic-offset: 4
418// indent-tabs-mode: nil
419// End:
Note: See TracBrowser for help on using the repository browser.