// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include #include #include #ifdef _WIN32 #include #else #include #endif //------------------------------------------------------------------------------ #ifdef _WIN32 void xplra_sleep(int ms) { Sleep(ms); } #else void xplra_sleep(int ms) { usleep(ms*1000); } #endif //------------------------------------------------------------------------------ int main() { static const char* tailNum1 = "VAI"; int retval = 0; const char* errorString = 0; int xplaneVersion = 0; int xplmVersion = 0; int xplraVersion = 0; int numEngines = 0; float spoolTime = 0.0; double latitude = 0.0, longitude = 0.0; ssize_t length, i; size_t length1, j; uint8_t result[260]; uint8_t* result1 = 0; int32_t result3[8]; int32_t* result4 = 0; float result5[8]; float* result6 = 0; float acfElevUp; double localX; float numBlades[8]; int32_t batteryArrayOn[8]; uint8_t tailNum[40]; printf("Connection to X-Plane...\n"); int connectionID = xplra_connect(); if (connectionID>=0) { printf("Connected.\n\n"); } else { printf("Connection failed.\n"); goto cleanup; } printf("Showing a message...\n"); if (xplra_show_message(connectionID, "[basictest] Starting tests", 5.0)<0) goto error; printf("\n"); printf("Querying the versions...\n"); if (xplra_get_versions(connectionID, &xplaneVersion, &xplmVersion, &xplraVersion)<0) goto error; printf("X-Plane version: %d, XPLM: %d, XPLRA: %03d\n\n", xplaneVersion, xplmVersion, xplraVersion); printf("Querying the number of the engines...\n"); if (xplra_get_int(&numEngines, connectionID, "sim/aircraft/engine/acf_num_engines")<0) { goto error; } printf("The number of engines: %d\n\n", numEngines); printf("Querying an invalid dataref...\n"); if (xplra_get_int(&numEngines, connectionID, "sim/aircraft/engine/num_engines")==0) { printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n"); } else { printf("Error occured: %s\n\n", xplra_get_last_error_string(connectionID)); xplra_clear_last_error(connectionID); } printf("Querying the spool time of a jet engine...\n"); if (xplra_get_float(&spoolTime, connectionID, "sim/aircraft/engine/acf_spooltime_jet")<0) { goto error; } printf("The spool time: %f\n\n", spoolTime); printf("Querying the spool time of a propeller...\n"); if (xplra_get_float(&spoolTime, connectionID, "sim/aircraft/engine/acf_spooltime_prop")<0) { goto error; } printf("The spool time: %f\n\n", spoolTime); printf("Querying the coordinates...\n"); if (xplra_get_double(&latitude, connectionID, "sim/flightmodel/position/latitude")<0) { goto error; } if (xplra_get_double(&longitude, connectionID, "sim/flightmodel/position/longitude")<0) { goto error; } printf("The coordinates: %f, %f\n\n", latitude, longitude); printf("Querying the aircraft's description as an array of a fixed size...\n"); length = xplra_get_byte_array(result, sizeof(result)/sizeof(result[0]), 0, connectionID, "sim/aircraft/view/acf_descrip"); if (length<0) goto error; printf("Got %zu bytes, as a string: '%s'\n\n", length, result); printf("Querying the aircraft's description as a dynamically created array with an offset of 3...\n"); length1 = 0; result1 = xplra_get_byte_array_new(&length1, 3, connectionID, "sim/aircraft/view/acf_descrip"); if (result1==0) goto error; printf("Got %zu bytes, as a string: '%s'\n\n", length1, result1); printf("Querying the aircraft's engine types as an array of a fixed size...\n"); length = xplra_get_int_array(result3, sizeof(result3)/sizeof(result3[0]), 0, connectionID, "sim/aircraft/prop/acf_en_type"); if (length<0) goto error; printf("Got %zd values:", length); for(i = 0; i0) printf(","); printf(" %d", result3[i]); } printf("\n\n"); printf("Querying the aircraft's engine types as a dynamically created array...\n"); length1 = 0; result4 = xplra_get_int_array_new(&length1, 0, connectionID, "sim/aircraft/prop/acf_en_type"); printf("Got %zu values:", length1); for(j = 0; j0) printf(","); printf(" %d", result4[j]); } printf("\n\n"); printf("Querying the aircraft's propeller direction as an array of a fixed size...\n"); length = xplra_get_float_array(result5, sizeof(result5)/sizeof(result5[0]), 0, connectionID, "sim/aircraft/prop/acf_prop_dir"); printf("Got %zd values:", length); for(i = 0; i0) printf(","); printf(" %f", result5[i]); } printf("\n\n"); printf("Querying the aircraft's propeller direction as a dynamically created array...\n"); length1 = 0; result6 = xplra_get_float_array_new(&length1, 0, connectionID, "sim/aircraft/prop/acf_prop_dir"); printf("Got %zu values:", length1); for(j = 0; j0) printf(","); printf(" %f", result6[j]); } printf("\n\n"); printf("Setting the number of the engines to %d...\n", numEngines + 1); if (xplra_set_int(connectionID, "sim/aircraft/engine/acf_num_engines", numEngines + 1)<0) { goto error; } if (xplra_get_int(&numEngines, connectionID, "sim/aircraft/engine/acf_num_engines")<0) { goto error; } printf("The new number of engines: %d\n\n", numEngines); if (xplra_get_float(&acfElevUp, connectionID, "sim/aircraft/controls/acf_elev_up")<0) { goto error; } printf("Setting the aircraft elevator up control from %f to %f...\n", acfElevUp, acfElevUp + 15.0); if (xplra_set_float(connectionID, "sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0) < 0) { goto error; } if (xplra_get_float(&acfElevUp, connectionID, "sim/aircraft/controls/acf_elev_up")<0) { goto error; } printf("The aircraft elevator up control set to %f\n\n", acfElevUp); if (xplra_get_double(&localX, connectionID, "sim/flightmodel/position/local_x")<0) { goto error; } printf("Setting the aircraft's local X-coordinate from %f to %f...\n", localX, localX + 15.0); if (xplra_set_double(connectionID, "sim/flightmodel/position/local_x", localX + 15.0)<0) { goto error; } if (xplra_get_double(&localX, connectionID, "sim/flightmodel/position/local_x")<0) { goto error; } printf("The aircraft's local X-coordinate set to %f\n\n", localX); length = xplra_get_float_array(numBlades, sizeof(numBlades)/sizeof(numBlades[0]), 0, connectionID, "sim/aircraft/prop/acf_num_blades"); if (length<0) goto error; printf("Setting the number of blades\n from:"); for(i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n to:"); for(i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n"); if (xplra_set_float_array(connectionID, "sim/aircraft/prop/acf_num_blades", numBlades, length, 0)<0) { goto error; } length = xplra_get_float_array(numBlades, sizeof(numBlades)/sizeof(numBlades[0]), 0, connectionID, "sim/aircraft/prop/acf_num_blades"); if (length<0) goto error; printf("The result:"); for(i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n\n"); length = xplra_get_int_array(batteryArrayOn, sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]), 0, connectionID, "sim/cockpit/electrical/battery_array_on"); if (length<0) goto error; printf("Setting the batteries\n from:"); for(i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n to:"); for(i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n"); if (xplra_set_int_array(connectionID, "sim/cockpit/electrical/battery_array_on", batteryArrayOn, length, 0)<0) { goto error; } length = xplra_get_int_array(batteryArrayOn, sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]), 0, connectionID, "sim/cockpit/electrical/battery_array_on"); if (length<0) goto error; printf("The result:"); for(i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n\n"); memset(tailNum, 0, sizeof(tailNum)); strcpy((char*)tailNum, "HA-VAI"); printf("Setting the tail number to %s as a byte array...\n", tailNum); if (xplra_set_byte_array(connectionID, "sim/aircraft/view/acf_tailnum", tailNum, sizeof(tailNum), 0)<0) { goto error; } memset(tailNum, 0, sizeof(tailNum)); length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0, connectionID, "sim/aircraft/view/acf_tailnum"); if (length<0) goto error; printf("The tail number is: '%s'\n\n", (char*)tailNum); printf("Setting the tail number to %s as a string...\n", tailNum1); if (xplra_set_string(connectionID, "sim/aircraft/view/acf_tailnum", tailNum1, 40, 0)<0) { goto error; } memset(tailNum, 0, sizeof(tailNum)); length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0, connectionID, "sim/aircraft/view/acf_tailnum"); if (length<0) goto error; printf("The tail number is: '%s'\n\n", (char*)tailNum); printf("Preparing for the message tests, sleeping for 5 seconds...\n"); xplra_sleep(5*1000); printf("Showing a message for 10 seconds...\n"); if (xplra_show_message(connectionID, "[basictest] this message appears for 10 seconds", 10.0)<0) { goto error; } printf("Sleeping for 3 seconds...\n"); xplra_sleep(3*1000); printf("Showing another message interrupting the previous one for 3 seconds"); if (xplra_show_message(connectionID, "[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0)<0) { goto error; } printf("Sleeping for 5 seconds...\n"); xplra_sleep(5*1000); if (xplra_show_message(connectionID, "[basictest] and the tests come to an end!", 5.0)<0) { goto error; } goto cleanup; error: errorString = xplra_get_last_error_string(connectionID); if (errorString==0) { printf("\nUnknown error occured!\n"); } else { printf("\nError: %s\n", errorString); } retval = 1; cleanup: if (connectionID>=0) xplra_disconnect(connectionID); return retval; } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: