source: xplra/test/basicctest.c@ 89:69ba1a2b5897

Last change on this file since 89:69ba1a2b5897 was 89:69ba1a2b5897, checked in by István Váradi <ivaradi@…>, 12 years ago

Added calls to save the current situation

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