Skip to content

Commit

Permalink
Updates to device, context, attribute, lowlevel classes
Browse files Browse the repository at this point in the history
Signed-off-by: Pagadarai <[email protected]>
  • Loading branch information
SrikanthPagadarai committed Aug 26, 2024
1 parent 248424a commit bcb65a0
Show file tree
Hide file tree
Showing 5 changed files with 1,003 additions and 137 deletions.
220 changes: 203 additions & 17 deletions +adi/+libiio/attribute.m
Original file line number Diff line number Diff line change
@@ -1,57 +1,243 @@
classdef attribute < handle
% methods
% function obj = attribute()
% % CHANNEL constructor method for matlabshared.libiio.context
% %
% % Returns the matlabshared.libiio.context object
% coder.allowpcode('plain');
% end
% end

%% Internal Helper Functions
methods (Static)
%% attribute methods
function status = iio_attr_read_raw(attrPtr, dstPtr, len)
% Read the content of the given attribute
%
% Args:
% attrPtr: A pointer to an iio_attr structure
% dstPtr: A pointer to the memory area where the read data
% will be stored
% len: The available length of the memory area, in bytes
%
% Returns:
% On success, the number of bytes written to the buffer
% On error, a negative errno code is returned
%
% libiio function: iio_attr_read_raw
validateattributes(len, { 'double','single' }, ...
{'real', 'scalar', 'finite', 'nonnan', 'nonempty', ...
'nonnegative', 'integer'});

if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_read_raw', attrPtr, dstPtr, len);
else
status = coder.ceval('iio_attr_read_raw', attrPtr, dstPtr, len);
end
end

function status = iio_attr_write_raw(attrPtr, srcPtr, len)
% Read the content of the given attribute
%
% Args:
% attrPtr: A pointer to an iio_attr structure
% srcPtr: A pointer to the data to be written
% len: The number of bytes that should be written
%
% Returns:
% On success, the number of bytes written
% On error, a negative errno code is returned
%
% libiio function: iio_attr_write_raw
validateattributes(len, { 'double','single' }, ...
{'real', 'scalar', 'finite', 'nonnan', 'nonempty', ...
'nonnegative', 'integer'});

if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_write_raw', attrPtr, srcPtr, len);
else
status = coder.ceval('iio_attr_write_raw', attrPtr, srcPtr, len);
end
end

function name = iio_attr_get_name(attrPtr)
% Retrieve the name of an attribute
%
% Args:
% attrPtr: A pointer to an iio_attr structure
%
% Returns:
% A pointer to a static NULL-terminated string
%
% libiio function: iio_attr_get_name

if coder.target('MATLAB')
name = adi.libiio.attribute.calllibADI('iio_attr_get_name', attrPtr);
else
name = coder.ceval('iio_attr_get_name', attrPtr);
end
end

function name = iio_attr_get_filename(attrPtr)
% Retrieve the filename of an attribute
%
% Args:
% attrPtr: A pointer to an iio_attr structure
%
% Returns:
% A pointer to a static NULL-terminated string
%
% libiio function: iio_attr_get_filename

if coder.target('MATLAB')
name = adi.libiio.attribute.calllibADI('iio_attr_get_filename', attrPtr);
else
name = coder.ceval('iio_attr_get_filename', attrPtr);
end
end

function name = iio_attr_get_static_value(attrPtr)
% Retrieve the static value of an attribute
%
% Args:
% attrPtr: A pointer to an iio_attr structure
%
% Returns:
% On success, a pointer to a static NULL-terminated string
% If the attribute does not have a static value, NULL is returned.
%
% libiio function: iio_attr_get_filename

if coder.target('MATLAB')
name = adi.libiio.attribute.calllibADI('iio_attr_get_static_value', attrPtr);
else
name = coder.ceval('iio_attr_get_static_value', attrPtr);
end
end

function [status, value] = iio_attr_read_bool(attrPtr)
valPtr = libpointer('bool', 0);
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_read_bool', attrPtr, valPtr);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_read_bool', attrPtr, valPtr);
else
status = coder.ceval('iio_attr_read_bool', attrPtr, valPtr);
end

if ~status
value = valPtr.value;
end
end

function [status, value] = iio_attr_read_longlong(attrPtr)
valPtr = libpointer('int64Ptr', 0);
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_read_longlong', attrPtr, valPtr);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_read_longlong', attrPtr, valPtr);
else
status = coder.ceval('iio_attr_read_longlong', attrPtr, valPtr);
end

if ~status
value = valPtr.value;
end
end

function [status, value] = iio_attr_read_double(attrPtr)
valPtr = libpointer('double', 0);
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_read_double', attrPtr, valPtr);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_read_double', attrPtr, valPtr);
else
status = coder.ceval('iio_attr_read_double', attrPtr, valPtr);
end

if ~status
value = valPtr.value;
end
end

function status = iio_attr_write_string(attrPtr, value)
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_write_string', attrPtr, value);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_write_string', attrPtr, value);
else
status = coder.ceval('iio_attr_write_string', attrPtr, value);
end
end

function status = iio_attr_write_bool(attrPtr, value)
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_write_bool', attrPtr, value);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_write_bool', attrPtr, value);
else
status = coder.ceval('iio_attr_write_bool', attrPtr, value);
end
end

function status = iio_attr_write_longlong(attrPtr, value)
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_write_longlong', attrPtr, value);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_write_longlong', attrPtr, value);
else
status = coder.ceval('iio_attr_write_longlong', attrPtr, value);
end
end

function status = iio_attr_write_double(attrPtr, value)
status = calllib(adi.libiio.attribute.getIIOLibName(), 'iio_attr_write_double', attrPtr, value);
if coder.target('MATLAB')
status = adi.libiio.attribute.calllibADI('iio_attr_write_double', attrPtr, value);
else
status = coder.ceval('iio_attr_write_double', attrPtr, value);
end
end
end

%%Helpers
methods (Hidden, Access = private, Static)
function libName = getIIOLibName()
libName = 'libiio1';
end

function headername = getIIOHeaderName()
headername = 'iio.h';
end

function [notfound, warnings] = loadLibIIO()
notfound = [];
warnings = [];
libName = adi.libiio.attribute.getIIOLibName();
headername = adi.libiio.attribute.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);
if ~isempty(notfound)
% error
end
end
end

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

if libisloaded(libName)
unloadlibrary(libName);
end
end

function varargout = calllibADI(fn, varargin)
[notfound, warnings] = adi.libiio.attribute.loadLibIIO();
varargout = cell(1, nargout);
varargoutLocal = calllib(adi.libiio.attribute.getIIOLibName(), fn, varargin{:});
% adi.libiio.attribute.unloadLibIIO();
[varargout{:}] = varargoutLocal;
end

function strout = ntstr(strin)
% Appends a null character to terminate the string.
% This is needed for code generation since MATLAB character
% arrays are not null terminated in code generation.
strout = [uint8(strin) uint8(0)];
end
end
end
122 changes: 118 additions & 4 deletions +adi/+libiio/context.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,131 @@
classdef context < handle
%% Internal Helper Functions
methods (Static)
%% Context Methods
%% context methods
function ctxPtr = iio_create_context(ctxParamsPtr, uri)
ctxPtr = calllib(adi.libiio.context.getIIOLibName(), 'iio_create_context', ctxParamsPtr, uri);
% Create a context from a URI description
%
% Args:
% ctxParamsPtr: A pointer to a iio_context_params structure
% that contains context creation information; can be NULL
% uri: a URI describing the context location. If NULL, the
% backend will be created using the URI string present in
% the IIOD_REMOTE environment variable, or if not set, a
% local backend is created.
%
% Returns:
% On success, a pointer to a iio_context structure
% On failure, a pointer-encoded error is returned
%
% libiio function: iio_create_context

if coder.target('MATLAB')
ctxPtr = adi.libiio.context.calllibADI('iio_create_context', ctxParamsPtr, uri);
else
ctxPtr = coder.opaque('struct iio_context*', 'NULL');
ctxPtr = coder.ceval('iio_create_context', ctxParamsPtr, adi.libiio.context.ntstr(uri));
end
end

function iio_context_destroy(ctxPtr)
% Create a context from a URI description
%
% Args:
% ctxPtr: A pointer to a iio_context structure
%
% libiio function: iio_context_destroyiio_context_destroy

if coder.target('MATLAB')
adi.libiio.context.calllibADI('iio_context_destroy', ctxPtr);
else
coder.ceval('iio_context_destroy', ctxPtr);
end
end

function devPtr = iio_context_find_device(ctxPtr, name)
devPtr = calllib(adi.libiio.context.getIIOLibName(), 'iio_context_find_device', ctxPtr, name);
% Try to find a device structure by its ID, label or name
%
% Args:
% ctxPtr: A pointer to an iio_context structure
% name: A NULL-terminated string corresponding to the ID,
% label or nameof the device to search for
%
% Returns:
% On success, a pointer to a iio_device structure
% If the parameter does not correspond to the ID, label or
% name of any known device, NULL is returned
%
% libiio function: iio_context_find_device

if coder.target('MATLAB')
devPtr = adi.libiio.context.calllibADI('iio_context_find_device', ctxPtr, name);
else
devPtr = coder.opaque('struct iio_device*', 'NULL');
devPtr = coder.ceval('iio_context_find_device', ctxPtr, adi.libiio.context.ntstr(name));
end
end
end

%%Helpers
methods (Hidden, Access = private, Static)
function libName = getIIOLibName()
libName = 'libiio1';
end

function headername = getIIOHeaderName()
headername = 'iio.h';
end

function [notfound, warnings] = loadLibIIO()
notfound = [];
warnings = [];
libName = adi.libiio.context.getIIOLibName();
headername = adi.libiio.context.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);
if ~isempty(notfound)
% error
end
end
end

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

if libisloaded(libName)
unloadlibrary(libName);
end
end

function varargout = calllibADI(fn, varargin)
[notfound, warnings] = adi.libiio.context.loadLibIIO();
varargout = cell(1, nargout);
varargoutLocal = calllib(adi.libiio.context.getIIOLibName(), fn, varargin{:});
% adi.libiio.context.unloadLibIIO();
[varargout{:}] = varargoutLocal;
end

function strout = ntstr(strin)
% Appends a null character to terminate the string.
% This is needed for code generation since MATLAB character
% arrays are not null terminated in code generation.
strout = [uint8(strin) uint8(0)];
end
end
end
Loading

0 comments on commit bcb65a0

Please sign in to comment.