source: xplra/test/basictest.cc@ 112:58892a32a039

Last change on this file since 112:58892a32a039 was 112:58892a32a039, checked in by István Váradi <ivaradi@…>, 17 months ago

The test programs can be parameterized to connect over TCP

File size: 12.3 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 "common.h"
32
33#include <hu/varadiistvan/xplra/XPlane.h>
34
35#include <hu/varadiistvan/scpl/Thread.h>
36
37#include <cstdio>
38#include <cstring>
39
40//------------------------------------------------------------------------------
41
42using hu::varadiistvan::xplra::XPlane;
43using hu::varadiistvan::xplra::Exception;
44using hu::varadiistvan::xplra::ProtocolException;
45
46using hu::varadiistvan::scpl::Thread;
47
48using std::string;
49
50//------------------------------------------------------------------------------
51
52int main(int argc, char* argv[])
53{
54 try {
55 XPlane xplane;
56
57 printf("Connecting to X-Plane...\n");
58 connectXPlane(xplane, argc, argv);
59 printf("Connected to X-Plane.\n\n");
60
61 int xplaneVersion = 0;
62 int xplmVersion = 0;
63 int xplraVersion = 0;
64
65 printf("Showing a message...\n");
66 xplane.showMessage("[basictest] Starting tests", 5.0);
67 printf("\n");
68
69 printf("Querying the versions...\n");
70 xplane.getVersions(xplaneVersion, xplmVersion, xplraVersion);
71 printf("X-Plane version: %d, XPLM: %d, XPLRA: %03d\n\n",
72 xplaneVersion, xplmVersion, xplraVersion);
73
74 printf("Saving the situation...\n");
75 xplane.saveSituation("output/situations/test.sit");
76 printf("Saved the situation\n\n");
77
78 printf("Querying the number of the engines...\n");
79 int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
80 printf("The number of engines: %d\n\n", numEngines);
81
82 try {
83 printf("Querying an invalid dataref...\n");
84 xplane.getInt("sim/aircraft/engine/num_engines");
85 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
86 } catch(const ProtocolException& exception) {
87 printf("Exception caugth: %s\n\n", exception.what());
88 }
89
90 printf("Querying the spool time of a jet engine...\n");
91 float spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet");
92 printf("The spool time: %f\n\n", spoolTime);
93
94 printf("Querying the spool time of a propeller...\n");
95 spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop");
96 printf("The spool time: %f\n\n", spoolTime);
97
98 printf("Querying the coordinates...\n");
99 double latitude = xplane.getDouble("sim/flightmodel/position/latitude");
100 double longitude = xplane.getDouble("sim/flightmodel/position/longitude");
101 printf("The coordinates: %f, %f\n\n", latitude, longitude);
102
103 printf("Querying the aircraft's description as an array of a fixed size...\n");
104 uint8_t result[260];
105 size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip",
106 result, sizeof(result)/sizeof(result[0]));
107 printf("Got %lu bytes, as a string: '%s'\n\n",
108 static_cast<unsigned long>(length), result);
109
110 printf("Querying the aircraft's description as a dynamically created array...\n");
111 uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
112 printf("Got %lu bytes, as a string: '%s'\n\n",
113 static_cast<unsigned long>(length), result1);
114
115 printf("Querying the aircraft's description as a string, with an offset of 3...\n");
116 string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
117 printf("Got: '%s'\n\n", result2.c_str());
118
119 printf("Querying the aircraft's engine types as an array of a fixed size...\n");
120 int32_t result3[8];
121 length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
122 result3, sizeof(result3)/sizeof(result3[0]));
123 printf("Got %lu values:", static_cast<unsigned long>(length));
124 for(size_t i = 0; i<length; ++i) {
125 if (i>0) printf(",");
126 printf(" %d", result3[i]);
127 }
128 printf("\n\n");
129
130 printf("Querying the aircraft's engine types as a dynamically created array...\n");
131 length = 0;
132 int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
133 printf("Got %lu values:", static_cast<unsigned long>(length));
134 for(size_t i = 0; i<length; ++i) {
135 if (i>0) printf(",");
136 printf(" %d", result4[i]);
137 }
138 printf("\n\n");
139
140 printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
141 float result5[8];
142 length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
143 result5, sizeof(result5)/sizeof(result5[0]));
144 printf("Got %lu values:", static_cast<unsigned long>(length));
145 for(size_t i = 0; i<length; ++i) {
146 if (i>0) printf(",");
147 printf(" %f", result5[i]);
148 }
149 printf("\n\n");
150
151 printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
152 length = 0;
153 float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
154 printf("Got %lu values:", static_cast<unsigned long>(length));
155 for(size_t i = 0; i<length; ++i) {
156 if (i>0) printf(",");
157 printf(" %f", result6[i]);
158 }
159 printf("\n\n");
160
161 printf("Setting the number of the engines to %d...\n", numEngines + 1);
162 xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1);
163 numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
164 printf("The new number of engines: %d\n\n", numEngines);
165
166 float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
167 printf("Setting the aircraft elevator up control from %f to %f...\n",
168 acfElevUp, acfElevUp + 15.0);
169 xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0);
170 acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
171 printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
172
173 double localX = xplane.getDouble("sim/flightmodel/position/local_x");
174 printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
175 localX, localX + 15.0);
176 xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0);
177 localX = xplane.getDouble("sim/flightmodel/position/local_x");
178 printf("The aircraft's local X-coordinate set to %f\n\n", localX);
179
180 float numBlades[8];
181 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
182 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
183
184 printf("Setting the number of blades\n from:");
185 for(size_t i = 0; i<length; ++i) {
186 if (i>0) printf(",");
187 printf(" %f", numBlades[i]);
188 }
189 printf("\n to:");
190 for(size_t i = 0; i<length; ++i) {
191 numBlades[i] += 2.5;
192 if (i>0) printf(",");
193 printf(" %f", numBlades[i]);
194 }
195 printf("\n");
196 xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",
197 numBlades, length);
198 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
199 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
200 printf("The result:");
201 for(size_t i = 0; i<length; ++i) {
202 if (i>0) printf(",");
203 printf(" %f", numBlades[i]);
204 }
205 printf("\n\n");
206
207 int32_t batteryArrayOn[8];
208 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
209 batteryArrayOn,
210 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
211
212 printf("Setting the batteries\n from:");
213 for(size_t i = 0; i<length; ++i) {
214 if (i>0) printf(",");
215 printf(" %d", batteryArrayOn[i]);
216 }
217 printf("\n to:");
218 for(size_t i = 0; i<length; ++i) {
219 batteryArrayOn[i] = !batteryArrayOn[i];
220 numBlades[i] += 2.5;
221 if (i>0) printf(",");
222 printf(" %d", batteryArrayOn[i]);
223 }
224 printf("\n");
225 xplane.setIntArray("sim/cockpit/electrical/battery_array_on",
226 batteryArrayOn, length);
227 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
228 batteryArrayOn,
229 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
230 printf("The result:");
231 for(size_t i = 0; i<length; ++i) {
232 if (i>0) printf(",");
233 printf(" %d", batteryArrayOn[i]);
234 }
235 printf("\n\n");
236
237 uint8_t tailNum[40];
238 memset(tailNum, 0, sizeof(tailNum));
239 strcpy(reinterpret_cast<char*>(tailNum), "HA-VAI");
240 printf("Setting the tail number to %s as a byte array...\n", tailNum);
241 xplane.setByteArray("sim/aircraft/view/acf_tailnum",
242 tailNum, sizeof(tailNum));
243 printf("The tail number is: '%s'\n\n",
244 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
245
246 static const char* tailNum1 = "VAI";
247 printf("Setting the tail number to %s as a string...\n", tailNum1);
248 xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40);
249 printf("The tail number is: '%s'\n\n",
250 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
251
252 try {
253 printf("Querying an invalid dataref...\n");
254 xplane.getInt("sim/aircraft/engine/num_engines");
255 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
256 } catch(const ProtocolException& exception) {
257 printf("Exception caugth: %s\n\n", exception.what());
258 }
259
260 printf("Preparing for the message tests, sleeping for 5 seconds...\n");
261 Thread::sleep(5*1000);
262
263 printf("Showing a message for 10 seconds...\n");
264 xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0);
265
266 printf("Sleeping for 3 seconds...\n");
267 Thread::sleep(3*1000);
268
269 printf("Showing another message interrupting the previous one for 3 seconds\n");
270 xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0);
271
272 printf("Sleeping for 5 seconds...\n");
273 Thread::sleep(5*1000);
274 xplane.showMessage("[basictest] and the tests come to an end!", 5.0);
275
276 return 0;
277 } catch(const Exception& exception) {
278 printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
279
280 return 1;
281 }
282}
283
284//------------------------------------------------------------------------------
285
286// Local Variables:
287// mode: C++
288// c-basic-offset: 4
289// indent-tabs-mode: nil
290// End:
Note: See TracBrowser for help on using the repository browser.