source: xplra/test/basicctest.c@ 41:a3f71b4eaca1

Last change on this file since 41:a3f71b4eaca1 was 40:ec5dde8a6ff6, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented the client support for the new commands and updated the basic test programs with tests showing messages

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