source: xplra/test/basicctest.c@ 36:29e3b676c0c2

Last change on this file since 36:29e3b676c0c2 was 36:29e3b676c0c2, checked in by István Váradi <ivaradi@…>, 11 years ago

Added a new command to query the versions

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