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.
This commit is contained in:
Jason Watkins
2015-04-23 11:10:37 -07:00
parent 02f4c92fce
commit 7da82f40dc
4 changed files with 73 additions and 64 deletions

View File

@@ -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<rows;i++)
{
buffer[5 + i * step] = (char)dataRef[i][0];
memcpy(&buffer[9 + i*step], &dataRef[i][1], 8 * sizeof(float));
buffer[5 + i * step] = (char)data[i][0];
memcpy(&buffer[9 + i*step], &data[i][1], 8 * sizeof(float));
}
// Send command
if (sendUDP(sock, buffer, len ) < 0)
@@ -306,7 +306,7 @@ int sendDATA(XPCSocket sock, float dataRef[][9], int rows)
return 0;
}
int readDATA(XPCSocket sock, float dataRef[][9], int rows)
int readDATA(XPCSocket sock, float data[][9], int rows)
{
// Preconditions
// There are only 134 DATA rows in X-Plane. Realistically, clients probably
@@ -338,8 +338,8 @@ int readDATA(XPCSocket sock, float dataRef[][9], int rows)
// 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));
data[i][0] = buffer[5 + i * 36];
memcpy(&data[i][1], &buffer[9 + i * 36], 8 * sizeof(float));
}
return rows;
}
@@ -539,7 +539,7 @@ int sendPOSI(XPCSocket sock, float values[], int size, char ac)
if (sendUDP(sock, buffer, 40) < 0)
{
printError("sendPOSI", "Failed to send command");
return -2;
return -3;
}
return 0;
}
@@ -597,7 +597,7 @@ int sendCTRL(XPCSocket sock, float values[], int size, char ac)
if (sendUDP(sock, buffer, 27) < 0)
{
printError("sendCTRL", "Failed to send command");
return -2;
return -3;
}
return 0;
}
@@ -610,6 +610,7 @@ int sendCTRL(XPCSocket sock, float values[], int size, char ac)
/*****************************************************************************/
int sendTEXT(XPCSocket sock, char* msg, int x, int y)
{
size_t msgLen = strnlen(msg, 255);
// Input Validation
if (x < -1)
{
@@ -622,11 +623,15 @@ int sendTEXT(XPCSocket sock, char* msg, int x, int y)
// Negative y will never result in text being displayed.
return -1;
}
if (msgLen > 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;
}

View File

@@ -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

View File

@@ -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 <p>An array containing zero to six values representing control surface data as follows:</p>
* <ol>
* <li>Latitudinal Stick [-1,1]</li>
* <li>Longitudinal Stick [-1,1]</li>
* <li>Rudder Pedals [-1, 1]</li>
* <li>Throttle [-1, 1]</li>
* <li>Gear (0=up, 1=down)</li>
* <li>Flaps [0, 1]</li>
* </ol>
* <p>
* 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.
* </p>
* @param values <p>An array containing zero to six values representing control surface data as follows:</p>
* <ol>
* <li>Latitudinal Stick [-1,1]</li>
* <li>Longitudinal Stick [-1,1]</li>
* <li>Rudder Pedals [-1, 1]</li>
* <li>Throttle [-1, 1]</li>
* <li>Gear (0=up, 1=down)</li>
* <li>Flaps [0, 1]</li>
* </ol>
* <p>
* 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.
* </p>
* @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 <p>An array containing zero to six values representing control surface data as follows:</p>
* @param values <p>An array containing zero to six values representing control surface data as follows:</p>
* <ol>
* <li>Latitudinal Stick [-1,1]</li>
* <li>Longitudinal Stick [-1,1]</li>
@@ -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.
* </p>
* @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 <p>An array containing position elements as follows:</p>
* @param values <p>An array containing position elements as follows:</p>
* <ol>
* <li>Latitiude (deg)</li>
* <li>Latitude (deg)</li>
* <li>Longitude (deg)</li>
* <li>Altitude (m above MSL)</li>
* <li>Roll (deg)</li>
@@ -480,17 +480,17 @@ public class XPlaneConnect implements AutoCloseable
* </p>
* @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 <p>An array containing position elements as follows:</p>
* @param values <p>An array containing position elements as follows:</p>
* <ol>
* <li>Latitiude (deg)</li>
* <li>Latitude (deg)</li>
* <li>Longitude (deg)</li>
* <li>Altitude (m above MSL)</li>
* <li>Roll (deg)</li>
@@ -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.
* </p>
* @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);

View File

@@ -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