From 0b562d1fce24a12e6d60b762688fd560a897d71c Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Sat, 16 May 2015 09:26:27 -0700 Subject: [PATCH] Added getPOSI and getCTRL support to the C client. --- C/src/xplaneConnect.c | 67 +++++++++++++++++++++++++++++++++ C/src/xplaneConnect.h | 18 +++++++++ TestScripts/C Tests/CtrlTests.h | 39 +++++++++++++++++++ TestScripts/C Tests/PosiTests.h | 37 ++++++++++++++++++ TestScripts/C Tests/main.c | 4 ++ xpcPlugin/MessageHandlers.cpp | 5 ++- 6 files changed, 169 insertions(+), 1 deletion(-) diff --git a/C/src/xplaneConnect.c b/C/src/xplaneConnect.c index e4e633e..9969a57 100755 --- a/C/src/xplaneConnect.c +++ b/C/src/xplaneConnect.c @@ -521,6 +521,38 @@ int getDREFs(XPCSocket sock, const char* drefs[], float* values[], unsigned char /*****************************************************************************/ /**** POSI functions ****/ /*****************************************************************************/ +int getPOSI(XPCSocket sock, float values[7], char ac) +{ + // Setup send command + unsigned char buffer[6] = "GETP"; + buffer[5] = ac; + + // Send command + if (sendUDP(sock, buffer, 6) < 0) + { + printError("getPOSI", "Failed to send command."); + return -1; + } + + // Get response + unsigned char readBuffer[34]; + int readResult = readUDP(sock, readBuffer, 34); + if (readResult < 0) + { + printError("getPOSI", "Failed to read response."); + return -2; + } + if (readResult != 34) + { + printError("getPOSI", "Unexpected response length."); + return -3; + } + + // Copy response into values + memcpy(values, readBuffer + 6, 7 * sizeof(float)); + return 0; +} + int sendPOSI(XPCSocket sock, float values[], int size, char ac) { // Validate input @@ -565,6 +597,41 @@ int sendPOSI(XPCSocket sock, float values[], int size, char ac) /*****************************************************************************/ /**** CTRL functions ****/ /*****************************************************************************/ +int getCTRL(XPCSocket sock, float values[7], char ac) +{ + // Setup send command + unsigned char buffer[6] = "GETC"; + buffer[5] = ac; + + // Send command + if (sendUDP(sock, buffer, 6) < 0) + { + printError("getCTRL", "Failed to send command."); + return -1; + } + + // Get response + unsigned char readBuffer[31]; + int readResult = readUDP(sock, readBuffer, 31); + if (readResult < 0) + { + printError("getCTRL", "Failed to read response."); + return -2; + } + if (readResult != 31) + { + printError("getCTRL", "Unexpected response length."); + return -3; + } + + // Copy response into values + memcpy(values, readBuffer + 5, 4 * sizeof(float)); + values[4] = readBuffer[21]; + values[5] = *((float*)(readBuffer + 22)); + values[6] = *((float*)(readBuffer + 27)); + return 0; +} + int sendCTRL(XPCSocket sock, float values[], int size, char ac) { // Validate input diff --git a/C/src/xplaneConnect.h b/C/src/xplaneConnect.h index 3827a68..161178a 100644 --- a/C/src/xplaneConnect.h +++ b/C/src/xplaneConnect.h @@ -196,6 +196,14 @@ int getDREFs(XPCSocket sock, const char* drefs[], float* values[], unsigned char // Position +/// Gets the position and orientation of the specified aircraft. +/// +/// \param sock The socket used to send the command and receive the response. +/// \param values An array to store the position information returned by the +/// plugin. The format of values is [Lat, Lon, Alt, Pitch, Roll, Yaw, Gear] +/// \returns 0 if successful, otherwise a negative value. +int getPOSI(XPCSocket sock, float values[7], char ac); + /// Sets the position and orientation of the specified aircraft. /// /// \param sock The socket to use to send the command. @@ -209,6 +217,16 @@ int sendPOSI(XPCSocket sock, float values[], int size, char ac); // Controls +/// Gets the control surface information for the specified aircraft. +/// +/// \param sock The socket used to send the command and receive the response. +/// \param values An array to store the position information returned by the +/// plugin. The format of values is [Elevator, Aileron, Rudder, +/// Throttle, Gear, Flaps, Speed Brakes] +/// \param ac The aircraft to set the control surfaces of. 0 is the main/player aircraft. +/// \returns 0 if successful, otherwise a negative value. +int getCTRL(XPCSocket sock, float values[7], char ac); + /// Sets the control surfaces of the specified aircraft. /// /// \param sock The socket to use to send the command. diff --git a/TestScripts/C Tests/CtrlTests.h b/TestScripts/C Tests/CtrlTests.h index ccf0569..7b9e445 100644 --- a/TestScripts/C Tests/CtrlTests.h +++ b/TestScripts/C Tests/CtrlTests.h @@ -38,6 +38,33 @@ int doCTRLTest(char* drefs[7], float values[], int size, int ac, float expected[ return compareArray(expected, actual, 7); } +int doGETCTest(float values[7], int ac, float expected[7]) +{ + // Execute Test + float actual[7]; + XPCSocket sock = openUDP(IP); + int result = sendCTRL(sock, values, 7, ac); + if (result >= 0) + { + result = getCTRL(sock, actual, ac); + } + closeUDP(sock); + if (result < 0) + { + return -1; + } + + // Test values + for (int i = 0; i < 7; ++i) + { + if (fabs(expected[i] - actual[i]) > 1e-4) + { + return -10 - i; + } + } + return 0; +} + int basicCTRLTest(char** drefs, int ac) { // Set control surfaces to known state. @@ -146,4 +173,16 @@ int testCTRL_Speedbrakes() } return 0; } + +int testGETC() +{ + float CTRL[7] = { 0.0F, 0.0F, 0.0F, 0.8F, 1.0F, 0.5F, -1.5F }; + return doGETCTest(CTRL, 0, CTRL); +} + +int testGETC_NonPlayer() +{ + float CTRL[7] = { 0.0F, 0.0F, 0.0F, 0.8F, 1.0F, 0.5F, -1.5F }; + return doGETCTest(CTRL, 2, CTRL); +} #endif \ No newline at end of file diff --git a/TestScripts/C Tests/PosiTests.h b/TestScripts/C Tests/PosiTests.h index d8b5673..a46ca61 100644 --- a/TestScripts/C Tests/PosiTests.h +++ b/TestScripts/C Tests/PosiTests.h @@ -40,6 +40,33 @@ int doPOSITest(char* drefs[7], float values[], int size, int ac, float expected[ return compareArray(expected, actual, 7); } +int doGETPTest(float values[7], int ac, float expected[7]) +{ + // Execute Test + float actual[7]; + XPCSocket sock = openUDP(IP); + int result = sendPOSI(sock, values, 7, ac); + if (result >= 0) + { + result = getPOSI(sock, actual, ac); + } + closeUDP(sock); + if (result < 0) + { + return -1; + } + + // Test values + for (int i = 0; i < 7; ++i) + { + if (fabs(expected[i] - actual[i]) > 1e-4) + { + return -10 - i; + } + } + return 0; +} + int basicPOSITest(char** drefs, int ac) { // Set psoition and initial orientation @@ -116,6 +143,16 @@ int testPOSI_NonPlayer() return basicPOSITest(drefs, 1); } +int testGetPOSI_Player() +{ + float POSI[7] = { 37.524F, -122.06899F, 2500, 0, 0, 0, 1 }; + doGETPTest(POSI, 0, POSI); +} +int testGetPOSI_NonPlayer() +{ + float POSI[7] = { 37.624F, -122.06899F, 1500, 0, 0, 0, 1 }; + doGETPTest(POSI, 3, POSI); +} #endif \ No newline at end of file diff --git a/TestScripts/C Tests/main.c b/TestScripts/C Tests/main.c index 0f5cdd9..26428eb 100644 --- a/TestScripts/C Tests/main.c +++ b/TestScripts/C Tests/main.c @@ -40,9 +40,13 @@ int main(int argc, const char * argv[]) runTest(testCTRL_Player, "CTRL (player)"); runTest(testCTRL_NonPlayer, "CTRL (non-player)"); runTest(testCTRL_Speedbrakes, "CTRL (speedbrakes)"); + runTest(testGETC, "GETC (player)"); + runTest(testGETC_NonPlayer, "GETC (Non-player)"); // POSI runTest(testPOSI_Player, "POSI (player)"); runTest(testPOSI_NonPlayer, "POSI (non-player)"); + runTest(testGetPOSI_Player, "GETP (player)"); + runTest(testGetPOSI_NonPlayer, "GETP (non-player)"); // Data runTest(testDATA, "DATA"); // Text diff --git a/xpcPlugin/MessageHandlers.cpp b/xpcPlugin/MessageHandlers.cpp index 4709165..d94be09 100644 --- a/xpcPlugin/MessageHandlers.cpp +++ b/xpcPlugin/MessageHandlers.cpp @@ -530,7 +530,10 @@ namespace XPC *((float*)(response + 18)) = DataManager::GetFloat(DREF_Pitch, aircraft); *((float*)(response + 22)) = DataManager::GetFloat(DREF_Roll, aircraft); *((float*)(response + 26)) = DataManager::GetFloat(DREF_HeadingTrue, aircraft); - *((float*)(response + 30)) = (float)DataManager::GetInt(DREF_GearHandle, aircraft); + + float gear[10]; + DataManager::GetFloatArray(DREF_GearDeploy, gear, 10, aircraft); + *((float*)(response + 30)) = gear[0]; sock->SendTo(response, 34, &connection.addr); }