From 7da82f40dc12aa1c8d1f8dbcfdd1fb6661c999a4 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Thu, 23 Apr 2015 11:10:37 -0700 Subject: [PATCH] Several variable name tweaks and error checking changes. - Minimal functional changes. The primary purpose of these tweaks is to make the clients more consistent across languages. --- C/src/xplaneConnect.c | 25 +++++---- C/src/xplaneConnect.h | 8 +-- Java/src/XPlaneConnect.java | 96 +++++++++++++++++--------------- MATLAB/+XPlaneConnect/sendCTRL.m | 8 +-- 4 files changed, 73 insertions(+), 64 deletions(-) diff --git a/C/src/xplaneConnect.c b/C/src/xplaneConnect.c index 8061c1b..e69d50b 100755 --- a/C/src/xplaneConnect.c +++ b/C/src/xplaneConnect.c @@ -276,7 +276,7 @@ int pauseSim(XPCSocket sock, char pause) /*****************************************************************************/ /**** X-Plane UDP Data functions ****/ /*****************************************************************************/ -int sendDATA(XPCSocket sock, float dataRef[][9], int rows) +int sendDATA(XPCSocket sock, float data[][9], int rows) { // Preconditions // There are only 134 DATA rows in X-Plane. Realistically, clients probably @@ -294,8 +294,8 @@ int sendDATA(XPCSocket sock, float dataRef[][9], int rows) unsigned short step = 9 * sizeof(float); for (int i=0;i 255) + { + printError("sendTEXT", "msg must be less than 255 bytes."); + return -2; + } // Setup command // 5 byte header + 8 byte position + up to 256 byte message char buffer[269] = "TEXT"; - size_t msgLen = strnlen(msg, 255); size_t len = 14 + msgLen; memcpy(buffer + 5, &x, sizeof(int)); memcpy(buffer + 9, &y, sizeof(int)); @@ -637,7 +642,7 @@ int sendTEXT(XPCSocket sock, char* msg, int x, int y) if (sendUDP(sock, buffer, 40) < 0) { printError("sendTEXT", "Failed to send command"); - return -2; + return -3; } return 0; } diff --git a/C/src/xplaneConnect.h b/C/src/xplaneConnect.h index 197550c..6c967c4 100644 --- a/C/src/xplaneConnect.h +++ b/C/src/xplaneConnect.h @@ -122,19 +122,19 @@ int pauseSim(XPCSocket sock, char pause); /// /// \details This command is compatible with the X-Plane data API. /// \param sock The socket to use to send the command. -/// \param dataRef A 2D array of data rows to read into. +/// \param data A 2D array of data rows to read into. /// \param rows The number of rows in dataRef. /// \returns 0 if successful, otherwise a negative value. -int readDATA(XPCSocket sock, float dataRef[][9], int rows); +int readDATA(XPCSocket sock, float data[][9], int rows); /// Sends X-Plane data on the specified socket. /// /// \details This command is compatible with the X-Plane data API. /// \param sock The socket to use to send the command. -/// \param dataRef A 2D array of data rows to send. +/// \param data A 2D array of data rows to send. /// \param rows The number of rows in dataRef. /// \returns 0 if successful, otherwise a negative value. -int sendDATA(XPCSocket sock, float dataRef[][9], int rows); +int sendDATA(XPCSocket sock, float data[][9], int rows); // DREF Manipulation diff --git a/Java/src/XPlaneConnect.java b/Java/src/XPlaneConnect.java index 80407e0..8297061 100644 --- a/Java/src/XPlaneConnect.java +++ b/Java/src/XPlaneConnect.java @@ -360,33 +360,33 @@ public class XPlaneConnect implements AutoCloseable } /** - * Sends command to X-Plane setting control surfaces on the player aircraft. + * Sends command to X-Plane setting control surfaces on the player ac. * - * @param ctrl

An array containing zero to six values representing control surface data as follows:

- *
    - *
  1. Latitudinal Stick [-1,1]
  2. - *
  3. Longitudinal Stick [-1,1]
  4. - *
  5. Rudder Pedals [-1, 1]
  6. - *
  7. Throttle [-1, 1]
  8. - *
  9. Gear (0=up, 1=down)
  10. - *
  11. Flaps [0, 1]
  12. - *
- *

- * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To - * change values in the middle of the array without affecting the preceding values, set the - * preceding values to -998. - *

+ * @param values

An array containing zero to six values representing control surface data as follows:

+ *
    + *
  1. Latitudinal Stick [-1,1]
  2. + *
  3. Longitudinal Stick [-1,1]
  4. + *
  5. Rudder Pedals [-1, 1]
  6. + *
  7. Throttle [-1, 1]
  8. + *
  9. Gear (0=up, 1=down)
  10. + *
  11. Flaps [0, 1]
  12. + *
+ *

+ * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To + * change values in the middle of the array without affecting the preceding values, set the + * preceding values to -998. + *

* @throws IOException If the command cannot be sent. */ - public void sendCTRL(float[] ctrl) throws IOException + public void sendCTRL(float[] values) throws IOException { - sendCTRL(ctrl, 0); + sendCTRL(values, 0); } /** - * Sends command to X-Plane setting control surfaces on the specified aircraft. + * Sends command to X-Plane setting control surfaces on the specified ac. * - * @param ctrl

An array containing zero to six values representing control surface data as follows:

+ * @param values

An array containing zero to six values representing control surface data as follows:

*
    *
  1. Latitudinal Stick [-1,1]
  2. *
  3. Longitudinal Stick [-1,1]
  4. @@ -400,23 +400,23 @@ public class XPlaneConnect implements AutoCloseable * change values in the middle of the array without affecting the preceding values, set the * preceding values to -998. *

    - * @param aircraft The aircraft to set. 0 for the player's aircraft. + * @param ac The ac to set. 0 for the player's ac. * @throws IOException If the command cannot be sent. */ - public void sendCTRL(float[] ctrl, int aircraft) throws IOException + public void sendCTRL(float[] values, int ac) throws IOException { //Preconditions - if(ctrl == null) + if(values == null) { throw new IllegalArgumentException("ctrl must no be null."); } - if(ctrl.length > 6) + if(values.length > 6) { throw new IllegalArgumentException("ctrl must have 6 or fewer elements."); } - if(aircraft < 0 || aircraft > 9) + if(ac < 0 || ac > 9) { - throw new IllegalArgumentException("aircraft must be non-negative and less than 9."); + throw new IllegalArgumentException("ac must be non-negative and less than 9."); } //Pad command values and convert to bytes @@ -424,16 +424,16 @@ public class XPlaneConnect implements AutoCloseable int cur = 0; ByteBuffer bb = ByteBuffer.allocate(22); bb.order(ByteOrder.LITTLE_ENDIAN); - for(i = 0; i < ctrl.length; ++i) + for(i = 0; i < values.length; ++i) { if(i == 4) { - bb.put(cur, (byte) ctrl[i]); + bb.put(cur, (byte) values[i]); cur += 1; } else { - bb.putFloat(cur, ctrl[i]); + bb.putFloat(cur, values[i]); cur += 4; } } @@ -450,7 +450,7 @@ public class XPlaneConnect implements AutoCloseable cur += 4; } } - bb.put(cur, (byte) aircraft); + bb.put(cur, (byte) ac); //Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); @@ -461,11 +461,11 @@ public class XPlaneConnect implements AutoCloseable } /** - * Sets the position of the player aircraft. + * Sets the position of the player ac. * - * @param posi

    An array containing position elements as follows:

    + * @param values

    An array containing position elements as follows:

    *
      - *
    1. Latitiude (deg)
    2. + *
    3. Latitude (deg)
    4. *
    5. Longitude (deg)
    6. *
    7. Altitude (m above MSL)
    8. *
    9. Roll (deg)
    10. @@ -480,17 +480,17 @@ public class XPlaneConnect implements AutoCloseable *

      * @throws IOException If the command can not be sent. */ - public void sendPOSI(float[] posi) throws IOException + public void sendPOSI(float[] values) throws IOException { - sendPOSI(posi, 0); + sendPOSI(values, 0); } /** - * Sets the position of the specified aircraft. + * Sets the position of the specified ac. * - * @param posi

      An array containing position elements as follows:

      + * @param values

      An array containing position elements as follows:

      *
        - *
      1. Latitiude (deg)
      2. + *
      3. Latitude (deg)
      4. *
      5. Longitude (deg)
      6. *
      7. Altitude (m above MSL)
      8. *
      9. Roll (deg)
      10. @@ -503,32 +503,32 @@ public class XPlaneConnect implements AutoCloseable * change values in the middle of the array without affecting the preceding values, set the * preceding values to -998. *

        - * @param aircraft The aircraft to set. 0 for the player aircraft. + * @param ac The ac to set. 0 for the player ac. * @throws IOException If the command can not be sent. */ - public void sendPOSI(float[] posi, int aircraft) throws IOException + public void sendPOSI(float[] values, int ac) throws IOException { //Preconditions - if(posi == null) + if(values == null) { throw new IllegalArgumentException("posi must no be null."); } - if(posi.length > 7) + if(values.length > 7) { throw new IllegalArgumentException("posi must have 7 or fewer elements."); } - if(aircraft < 0 || aircraft > 255) + if(ac < 0 || ac > 255) { - throw new IllegalArgumentException("aircraft must be between 0 and 255."); + throw new IllegalArgumentException("ac must be between 0 and 255."); } //Pad command values and convert to bytes int i; ByteBuffer bb = ByteBuffer.allocate(28); bb.order(ByteOrder.LITTLE_ENDIAN); - for(i = 0; i < posi.length; ++i) + for(i = 0; i < values.length; ++i) { - bb.putFloat(i * 4, posi[i]); + bb.putFloat(i * 4, values[i]); } for(; i < 7; ++i) { @@ -539,7 +539,7 @@ public class XPlaneConnect implements AutoCloseable ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("POSI".getBytes(StandardCharsets.UTF_8)); os.write(0xFF); //Placeholder for message length - os.write(aircraft); + os.write(ac); os.write(bb.array()); sendUDP(os.toByteArray()); } @@ -704,6 +704,10 @@ public class XPlaneConnect implements AutoCloseable { throw new IllegalArgumentException("points.length should be divisible by 3."); } + if(points.length / 3 > 255) + { + throw new IllegalArgumentException("Too many points. Must be less than 256."); + } //Convert points to bytes ByteBuffer bb = ByteBuffer.allocate(4 * points.length); diff --git a/MATLAB/+XPlaneConnect/sendCTRL.m b/MATLAB/+XPlaneConnect/sendCTRL.m index e6d450c..b3d85b3 100644 --- a/MATLAB/+XPlaneConnect/sendCTRL.m +++ b/MATLAB/+XPlaneConnect/sendCTRL.m @@ -1,8 +1,8 @@ -function sendCTRL( ctrl, ac, socket ) +function sendCTRL( values, ac, socket ) % sendCTRL Sends command to X-Plane setting control surfaces on the specified aircraft. % % Inputs -% ctrl: control array where the elements are as follows: +% values: control array where the elements are as follows: % 1. Latitudinal Stick [-1,1] % 2. Longitudinal Stick [-1,1] % 3. Pedal [-1, 1] @@ -41,13 +41,13 @@ if ~exist('socket', 'var') end %% Validate input -ctrl = single(ctrl); +values = single(values); if ~exist('ac', 'var') ac = 0; end ac = logical(ac); %% Send command -socket.sendCTRL(ctrl, ac); +socket.sendCTRL(values, ac); end \ No newline at end of file