Fixed errors in MATLAB client and updated tests.

This commit is contained in:
Jason Watkins
2015-04-21 09:51:49 -07:00
parent 3ad2bb76cb
commit 1523405495
29 changed files with 109 additions and 128 deletions

View File

@@ -24,14 +24,15 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[getDREFs] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[getDREFs] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Send command
result = socket.getDREF(drefs);
result = socket.getDREFs(drefs);
end

View File

@@ -29,7 +29,8 @@ addOptional(p,'port',0,@isnumeric);
parse(p,varargin{:});
%% Create client
javaaddpath('XPlaneConnect.jar');
[folder, ~, ~] = fileparts(which('XPlaneConnect.openUDP'));
javaaddpath([folder, '\XPlaneConnect.jar']);
import gov.nasa.xpc.*;
socket = XPlaneConnect(p.Results.xpHost, p.Results.xpPort, p.Results.port);

View File

@@ -20,11 +20,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[pauseSim] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[pauseSim] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -37,11 +37,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendDATA] ERROR: Multiple clients open. You must specify which client to use.');
assert(istrue(length(clients) < 2), '[readDATA] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Get data

View File

@@ -19,11 +19,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendDATA] ERROR: Multiple clients open. You must specify which client to use.');
assert(istrue(length(clients) < 2), '[selectDATA] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -32,11 +32,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendCTRL] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[sendCTRL] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -28,11 +28,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendDATA] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[sendDATA] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -31,11 +31,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[pauseSim] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[sendPOSI] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input
@@ -46,6 +47,6 @@ end
ac = logical(ac);
%% Send command
socket.sendCTRL(ctrl, ac);
socket.sendPOSI(posi, ac);
end

View File

@@ -24,11 +24,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendCTRL] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[sendTEXT] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -25,19 +25,31 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendCTRL] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[sendWYPT] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Get WaypointOp class
[folder, ~, ~] = fileparts(which('XPlaneConnect.openUDP'));
javaaddpath([folder, '\XPlaneConnect.jar']);
import gov.nasa.xpc.*;
%% Validate input
len = uint32(length(points));
assert(op > 0 && op < 4);
wyptOp = WaypointOp.Add;
if isequal(op, 2)
wyptOp = WaypointOp.Del;
elseif isequal(op, 3)
wyptOp = WaypointOp.Clr;
end
assert(mod(len, 3) == 0);
%% Send command
socket.sendCTRL(ctrl, ac);
socket.sendWYPT(wyptOp, points);
end

View File

@@ -19,11 +19,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[sendCTRL] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[setCONN] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -23,11 +23,12 @@ import XPlaneConnect.*
%% Get client
global clients;
if ~exist('socket', 'var')
assert(istrue(length(clients) < 2), '[setDREF] ERROR: Multiple clients open. You must specify which client to use.');
assert(isequal(length(clients) < 2, 1), '[setDREF] ERROR: Multiple clients open. You must specify which client to use.');
if isempty(clients)
openUDP();
socket = openUDP();
else
socket = clients(1);
end
socket = clients(1);
end
%% Validate input

View File

@@ -348,14 +348,17 @@ public class XPlaneConnectTest
}
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSendDREF_MessageTooLong() throws IOException
{
// NOTE: This test originally ensured that the client restricted messages to 255 bytes in length.
// This restriction is no longer necessary, however removing it uncovered a bug that caused
// the plugin to do a bad memcpy and crash. This test is left here to ensure that X-Plane
// does not crash when given erroneously large value arrays.
String dref = "sim/cockpit/switches/gear_handle_status";
try(XPlaneConnect xpc = new XPlaneConnect())
{
xpc.setDREF(dref, new float[200]);
fail();
}
}

View File

@@ -1,6 +1,7 @@
function CTRLTest( )
%CTRLTest Summary of this function goes here
% Detailed explanation goes here
%% Test player aircraft
addpath('../../MATLAB')
import XPlaneConnect.*
@@ -14,14 +15,15 @@ THROT = rand();
CTRL = [0.0, 0.0, 1.0, THROT, 0.0, 1.0];
sendCTRL(CTRL);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),6),'CTRLTest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(length(result{4}),8),'CTRLTest: requestDREF unsucessful- element 1 incorrect size (should be size 8)');
assert(isequal(length(result),6),'CTRLTest: getDREFs unsucessful-wrong number of elements returned');
assert(isequal(length(result{4}),8),'CTRLTest: getDREFs unsucessful- element 1 incorrect size (should be size 8)');
for i=1:length(CTRL)-1
assert(abs(result{i}(1)-CTRL(i))<1e-4,['CTRLTest: DATA set unsucessful-',num2str(i)]);
end
%% Test NPC aircraft
DREFS = {'sim/multiplayer/position/plane1_yolk_pitch',...
'sim/multiplayer/position/plane1_yolk_roll',...
'sim/multiplayer/position/plane1_yolk_yaw',...
@@ -32,7 +34,7 @@ THROT = rand();
CTRL = [0.0, 0.0, 1.0, THROT, 0.0, 1.0];
sendCTRL(CTRL, 1);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),6),'CTRLTest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(length(result{4}),8),'CTRLTest: requestDREF unsucessful- element 1 incorrect size (should be size 8)');

View File

@@ -10,11 +10,10 @@ value = rand();
data = struct('h',25,'d',[value,-998,-998,-998,-998,-998,-998,-998]);
sendDATA(data);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),1),'DATATest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(length(result{1}),8),'DATATest: requestDREF unsucessful- element 1 incorrect size (should be size 8)');
assert(abs(result{1}(1)-value)<1e-4,'DATATest: DATA set unsucessful');
assert(isequal(length(result),8),'DATATest: getDREFs unsucessful-wrong number of elements returned');
assert(abs(result(1)-value)<1e-4,'DATATest: DATA set unsucessful');
end

View File

@@ -14,14 +14,14 @@ DREFS = {'sim/flightmodel/position/latitude', ...
POSI = [37.524, -122.06899, 2500, 0, 0, 0, 1]; % Gear
sendPOSI(POSI);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),7),'POSITest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(length(result),7),'POSITest: getDREFs unsucessful-wrong number of elements returned');
for i=1:length(POSI)
if i==3
continue
end
assert(abs(result{i}(1)-POSI(i))<1e-4,['POSITest: DATA set unsucessful-',num2str(i)]);
assert(abs(result(i)-POSI(i))<1e-4,['POSITest: DATA set unsucessful-',num2str(i)]);
end
end

View File

@@ -1,4 +1,4 @@
function requestDREFTest( )
function getDREFsTest( )
%SENDREADTEST Summary of this function goes here
% Detailed explanation goes here
addpath('../../MATLAB')
@@ -7,7 +7,7 @@ import XPlaneConnect.*
DREFS = {'sim/cockpit/switches/gear_handle_status',...
'sim/cockpit2/switches/panel_brightness_ratio'};
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),2));
assert(isequal(length(result{1}),1));

View File

@@ -4,13 +4,10 @@ function openCloseTest()
addpath('../../MATLAB')
import XPlaneConnect.*
socket = openUDP( 49007 );
closeUDP( socket );
assert(isequal(socket.isClosed(),1),'openCloseTest: socket is still open');
socket = openUDP( 49007 );
closeUDP( socket );
socket = openUDP();
closeUDP(socket);
socket = openUDP();
closeUDP(socket);
end

View File

@@ -9,17 +9,15 @@ DREFS = {'sim/operation/override/override_planepath'};
value = 1; % Pause
pauseSim(value);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),1),'pauseTest: requestDREF unsucessful-wrong number of elements returned');
assert(abs(result{1}(1)-value)<1e-4,'pauseTest: Pause Unsuccessful');
assert(abs(result(1)-value)<1e-4,'pauseTest: Pause Unsuccessful');
value = 0; % Resume
pauseSim(value);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),1),'pauseTest: requestDREF unsucessful-wrong number of elements returned');
assert(abs(result{1}(1)-value)<1e-4,'pauseTest: Pause Unsuccessful');
assert(abs(result(1)-value)<1e-4,'pauseTest: Pause Unsuccessful');
end

View File

@@ -1,19 +0,0 @@
function sendDREFTest( )
%SENDREADTEST Summary of this function goes here
% Detailed explanation goes here
addpath('../../MATLAB')
import XPlaneConnect.*
DREFS = {'sim/cockpit/switches/gear_handle_status'};
value = randi([0 10]);
sendDREF(DREFS{1},value);
result = requestDREF(DREFS);
assert(isequal(length(result),1),'sendDREFTest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(length(result{1}),1),'sendDREFTest: requestDREF unsucessful- element 1 incorrect size (should be size 1)');
assert(isequal(result{1},value),'sendDREFTest: DREF set unsucessful');
end

View File

@@ -1,18 +0,0 @@
function sendReadTest( )
%SENDREADTEST Summary of this function goes here
% Detailed explanation goes here
addpath('../../MATLAB')
import XPlaneConnect.*
recvPortNum = 49074;
recvPort = openUDP(49074);
sendUDP('test2'-0,'127.0.0.1',49074);
result = readUDP(recvPort);
closeUDP(recvPort);
assert(~isequal(result,-998), 'no data received');
assert(all(result(1:4)'=='test'-0));
end

View File

@@ -8,7 +8,7 @@ import XPlaneConnect.*
DREFS = {'sim/cockpit/switches/gear_handle_status'};
setConn(49055);
result = requestDREF(DREFS);
result = getDREFs(DREFS);
assert(isequal(length(result),1),'setConnTest: requestDREF unsucessful-wrong number of elements returned');

View File

@@ -0,0 +1,18 @@
function setDREFTest( )
%SENDREADTEST Summary of this function goes here
% Detailed explanation goes here
addpath('../../MATLAB')
import XPlaneConnect.*
DREFS = {'sim/cockpit/switches/gear_handle_status'};
value = randi([0 10]);
setDREF(DREFS{1},value);
result = getDREFs(DREFS);
assert(isequal(length(result),1),'setDREFTest: requestDREF unsucessful-wrong number of elements returned');
assert(isequal(result(1),value),'setDREFTest: DREF set unsucessful');
end

View File

@@ -1,19 +0,0 @@
function struTest( )
%STRUTEST Summary of this function goes here
% Detailed explanation goes here
addpath('../../MATLAB')
import XPlaneConnect.*
recvNum = 49076;
recvPort = openUDP(recvNum);
testSTRU = struct('h',15,'d',[12, 34, 55, 99, 1, 0],'test','theTest');
sendSTRU(testSTRU,'127.0.0.1',recvNum);
resultSTRU = readDATA(recvPort);
closeUDP(recvPort);
resultSTRU = rmfield(resultSTRU,'raw');
assert(isequal(testSTRU,resultSTRU),'struTest: Error-structs are not equal');
end

View File

@@ -11,16 +11,14 @@ end
disp(['XPC Tests-MATLAB (', os, ')']);
theTests = {{@openCloseTest, 'Open/Close Test', 0},...
{@sendReadTest,'Send/Read Test', 0},...
{@sendTEXTTest,'TEXT Test', 0},...
{@requestDREFTest,'Request DREF Test', 0},...
{@sendDREFTest,'Send DREF Test', 0},...
{@getDREFsTest,'Request DREF Test', 0},...
{@setDREFTest,'Send DREF Test', 0},...
{@DATATest,'DATA Test', 0},...
{@CTRLTest,'CTRL Test', 0},...
{@POSITest,'POSI Test', 0},...
{@sendWYPTTest,'WYPT Test', 0},...
{@pauseTest,'Pause Test', 0},...
{@struTest,'Struct Test', 0},...
{@setConnTest, 'setConn Test', 0}};
for i=1:length(theTests)

View File

@@ -382,8 +382,7 @@ namespace XPC
std::string dref = std::string((char*)buffer + 6, len);
std::uint8_t valueCount = buffer[6 + len];
float values[40];
memcpy(values, buffer + len + 7, valueCount * sizeof(float));
float* values = (float*)(buffer + 7 + len);
#if LOG_VERBOSITY > 1
Log::FormatLine("[DREF] Request to set DREF value received (Conn %i): %s", connection.id, dref.c_str());

Binary file not shown.

Binary file not shown.