Skip to content

Commit

Permalink
matlab bindings: [WIP] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SrikanthPagadarai committed Jan 6, 2025
1 parent 4d47b5c commit 7c813f0
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 16 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
warnings = [];
libName = adi.libiio.helpers.getIIOLibName();
headername = adi.libiio.helpers.getIIOHeaderName();
% persistent IsLibiioLoaded
% if isempty(IsLibiioLoaded)
% [notfound, warnings] = loadlibrary(libName,headername);
% if ~isempty(notfound)
% % error
% end
% IsLibiioLoaded = libisloaded(libName);
% end

if ~libisloaded(libName)
[notfound, warnings] = loadlibrary(libName,headername);
Expand All @@ -33,14 +25,6 @@

function unloadLibIIO()
libName = adi.libiio.helpers.getIIOLibName();
% persistent IsLibiioLoaded
% if isempty(IsLibiioLoaded)
% IsLibiioLoaded = libisloaded(libName);
% end
%
% if IsLibiioLoaded
% unloadlibrary(libName);
% end

if libisloaded(libName)
unloadlibrary(libName);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions bindings/matlab/codegen/genlibiioAPI.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
tic;
functionNames = {...
'iioReadDevAttrLonglong.m',...
'iioWriteDevAttrLonglong.m',...
};
cfg = coder.config('dll');
cfg.TargetLang = 'C';
cfg.FilePartitionMethod = 'SingleFile';
cfg.RuntimeChecks = true;
cfg.GenCodeOnly = true;
cfg.EnableAutoExtrinsicCalls = false;
cfg.MATLABSourceComments = true;
% cfg.GenerateReport = true;
% cfg.LaunchReport = true;
cfg.HighlightPotentialDataTypeIssues = true;

outputLIBName = 'mlibiio';
cfg.HardwareImplementation.TargetHWDeviceType = 'Intel->x86-64 (Linux 64)'; %'Generic->32-bit Embedded Processor';
result = codegen('-config','cfg',functionNames{:},'-O ','disable:openmp','-o',outputLIBName);
% result = codegen('-config','cfg',functionNames{:},...
% '-args',coder.opaque('const struct iio_context_params *'),...
% '-O ','disable:openmp','-o',outputLIBName);
assert(result.summary.passed)
toc;
28 changes: 28 additions & 0 deletions bindings/matlab/codegen/iioReadDevAttrLonglong.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function [status, attrName, value] = iioReadDevAttrLonglong(uri, phyDevName, devAttrName)
assert(isa(uri,'char') && all(size(uri) <= [1,20]));
assert(isa(phyDevName,'char') && all(size(phyDevName) <= [1,20]));
assert(isa(devAttrName,'char') && all(size(devAttrName) <= [1,20]));

% Get Context
iioCtxPtr = adi.libiio.context.iio_create_context(uri);
status = -int32(iioCtxPtr==coder.opaque('struct iio_context*', 'NULL'));
if status ~= 0
value = int64(0);
return;
end

% Get PhyDev Pointer
iioPhyDevPtr = adi.libiio.context.iio_context_find_device(iioCtxPtr, phyDevName);

% Get Attribute Pointer
iioPhyDevAttrPtr = adi.libiio.device.iio_device_find_attr(iioPhyDevPtr, devAttrName);

% get Attribute Name
attrName = adi.libiio.attribute.iio_attr_get_name(iioPhyDevAttrPtr);

% Read Attribute
[status, value] = adi.libiio.attribute.iio_attr_read_longlong(iioPhyDevAttrPtr);

% Destroy Context
adi.libiio.context.iio_context_destroy(iioCtxPtr);
end
25 changes: 25 additions & 0 deletions bindings/matlab/codegen/iioWriteDevAttrLonglong.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function status = iioWriteDevAttrLonglong(uri, phyDevName, devAttrName, value)
assert(isa(uri,'char') && all(size(uri) <= [1,20]));
assert(isa(phyDevName,'char') && all(size(phyDevName) <= [1,20]));
assert(isa(devAttrName,'char') && all(size(devAttrName) <= [1,20]));
assert(isa(value,'int64') && isreal(value) && all(size(value) == [1,1]));

% Get Context
iioCtxPtr = adi.libiio.context.iio_create_context(uri);
status = -int32(iioCtxPtr==coder.opaque('struct iio_context*', 'NULL'));
if status ~= 0
return;
end

% Get PhyDev Pointer
iioPhyDevPtr = adi.libiio.context.iio_context_find_device(iioCtxPtr, phyDevName);

% Get Attribute Pointer
iioPhyDevAttrPtr = adi.libiio.device.iio_device_find_attr(iioPhyDevPtr, devAttrName);

% Write Attribute
status = adi.libiio.attribute.iio_attr_write_longlong(iioPhyDevAttrPtr, value);

% Destroy Context
adi.libiio.context.iio_context_destroy(iioCtxPtr);
end
14 changes: 14 additions & 0 deletions bindings/matlab/test/runTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
clc;
clear;

mfilepath = fileparts(mfilename('fullpath'));
cd(mfilepath);
addpath(strcat("..", filesep, "codegen"));
suite = testsuite("test");
import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageReport
runner = testrunner("textoutput");
reportFormat = CoverageReport("coverageReport");
p = CodeCoveragePlugin.forFolder(strcat("..", filesep, "+adi", filesep, "+libiio"),"Producing",reportFormat);
runner.addPlugin(p);
results = runner.run(suite);
35 changes: 35 additions & 0 deletions bindings/matlab/test/testDeviceAttributes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
classdef testDeviceAttributes < matlab.unittest.TestCase
properties(TestParameter)
uri = {'ip:pluto.local'}
phyDev = {'ad9361-phy'}
devAttr = {'xo_correction'}
value = {int64(40000005)}
end

methods(Test)
function testPlutoDeviceAttributes(testCase)
% Read from Device Attribute
[status, attrName, result] = iioReadDevAttrLonglong(testCase.uri{1}, testCase.phyDev{1}, testCase.devAttr{1});
assert(strcmp(attrName, testCase.devAttr{1}));
assert(status==0);
OrigValue = result;

% Write to Device Attribute and Verify Value is Written
status = iioWriteDevAttrLonglong(testCase.uri{1}, testCase.phyDev{1}, testCase.devAttr{1}, testCase.value{1});
assert(status==0);
[status, attrName, result] = iioReadDevAttrLonglong(testCase.uri{1}, testCase.phyDev{1}, testCase.devAttr{1});
assert(status==0);
assert(result==testCase.value{1});

% Write Original Value to Device Attribute and Verify Value is Written
status = iioWriteDevAttrLonglong(testCase.uri{1}, testCase.phyDev{1}, testCase.devAttr{1}, OrigValue);
assert(status==0);
[status, attrName, result] = iioReadDevAttrLonglong(testCase.uri{1}, testCase.phyDev{1}, testCase.devAttr{1});
assert(status==0);
assert(result==OrigValue);

% Unload Library
adi.libiio.helpers.unloadLibIIO();
end
end
end

0 comments on commit 7c813f0

Please sign in to comment.