From 4e7e816da1791dd664c5735b17866ac320fb0906 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Wed, 15 Apr 2015 15:58:23 -0700 Subject: [PATCH] Cleaned up DATA related functions. - Tweaked signatures to improve consistency and safety. - Improved error logging. - Removed parseDATA. When merged into readDATA, the meat of parseDATA is only a few lines of code. --- C/src/xplaneConnect.c | 118 ++++++++++++++++++++++++------------------ C/src/xplaneConnect.h | 7 ++- 2 files changed, 72 insertions(+), 53 deletions(-) diff --git a/C/src/xplaneConnect.c b/C/src/xplaneConnect.c index 5f199a6..d04c8b7 100755 --- a/C/src/xplaneConnect.c +++ b/C/src/xplaneConnect.c @@ -265,29 +265,80 @@ int pauseSim(XPCSocket sock, char pause) /**** End Configuration functions ****/ /*****************************************************************************/ -short sendDATA(XPCSocket recfd, float dataRef[][9], unsigned short rows) +/*****************************************************************************/ +/**** X-Plane UDP Data functions ****/ +/*****************************************************************************/ +int sendDATA(XPCSocket sock, float dataRef[][9], int rows) { - int i; - unsigned short length = rows*9*sizeof(float) + 5; - char message[5000]; - unsigned short step = 9*sizeof(float); - - strncpy(message,"DATA",4); - - for (i=0;i 134) { - message[5+i*step] = (char) dataRef[i][0]; - message[6+i*step] = (char) 0; - message[7+i*step] = (char) 0; - message[8+i*step] = (char) 0; - memcpy(&message[9+i*step], &dataRef[i][1], 8*sizeof(float)); + printError("sendDATA", "Too many rows."); + return -1; + } + + // Setup command + // 5 byte header + 134 rows * 9 values * 4 bytes per value => 4829 byte max length. + char buffer[4829] = "DATA"; + int len = 5 + rows * 9 * sizeof(float); + unsigned short step = 9 * sizeof(float); + for (int i=0;i 134) + { + printError("sendDATA", "Too many rows."); + // Read as much as we can anyway + rows = 134; + } + + // Read data + char buffer[4829] = { 0 }; + int result = readUDP(sock, buffer, 5120, NULL); + if (result <= 0) + { + printError("readDATA", "Failed to read from socket."); + return -1; + } + // Validate data + int readRows = (result - 5) / 36; + if (readRows > rows) + { + printError("readDATA", "Read more rows than will fit in dataRef."); + // Copy as much data as we can anyway + rows = readRows; + } + + // Parse data + for (int i = 0; i < rows; ++i) + { + dataRef[i][0] = buffer[5 + i * 36]; + memcpy(&dataRef[i][1], &buffer[9 + i * 36], 8 * sizeof(float)); + } + return rows; +} +/*****************************************************************************/ +/**** End X-Plane UDP Data functions ****/ +/*****************************************************************************/ + short sendDREF(XPCSocket recfd, const char *dataRef, unsigned short length_of_DREF, float *values, unsigned short number_of_values) { char message[5000]; int length = 7+length_of_DREF+number_of_values*sizeof(float); @@ -478,17 +529,7 @@ short sendWYPT(XPCSocket sendfd, WYPT_OP op, float points[], int numPoints) //READ //---------------------------------------- -short readDATA(XPCSocket recfd, float dataRef[][9]) -{ - char buf[5000]; - int buflen = readUDP(recfd,buf, NULL); - - if (buf[0]!= '\0') - { - return parseDATA(buf, buflen, dataRef); - } - return -1; -} + short readRequest(XPCSocket recfd, float *dataRef[], short arraySizes[], struct sockaddr *recvaddr) { @@ -544,27 +585,6 @@ xpcCtrl readCTRL(XPCSocket recfd) //PARSE //--------------------- -short parseDATA(const char my_message[], short messageLength, float dataRef[][9]) -{ - int i, j; - unsigned short totalColumns = ((messageLength-5)/36); - float tmp; - float data[20][9]; - - // Input Validation - - for (i=0;i