classdef RLSUtils
%RLSUTILS SIL/PIL reusable library subsystem utilities
%
% This is an undocumented class. Its methods and properties are likely to
% change without warning from one release to the next.

% Copyright 2019 The MathWorks, Inc.

methods(Static)
function [libCodeFolder, contextCodeFolder] = getRLSFoldersFromHarnessInfo(harnessInfo)
% extract the library code folder information from the harness
% info
[libraryName, remain] = strtok(harnessInfo.ownerFullPath, '/');
subsystemName = strtok(remain, '/');
functionInterfaceName = harnessInfo.functionInterfaceName;
% get STF and hw from the harness's ConfigSet to avoid
% loading the context and hit an error because an harness
% it already loaded
harnessName = harnessInfo.name;
% get library path
[libCodeFolder, contextCodeFolder] = ...
rtw.pil.RLSUtils.getRLSFoldersInternal(harnessName, ...
libraryName, functionInterfaceName, subsystemName);
end

function [libCodeFolder, contextCodeFolder] = getRLSFoldersFromContext(codeContext, libraryName, functionInterface, subsystemName)
% extract the library code folder information from the exported
% code context
[libCodeFolder, contextCodeFolder] = ...
rtw.pil.RLSUtils.getRLSFoldersInternal(codeContext, ...
libraryName, functionInterface, subsystemName);
end

function name = getUniqueContextName(contextName)
% append a suffix to the context name
name = [contextName '_XILRLS'];
end
end

methods (Static, Access=private)
function [libCodeFolder, contextCodeFolder] = getRLSFoldersInternal(srcModel, libraryName, functionInterfaceName, subsystemName)
% Find the root of the code folder, this is the same the
% location of the library SLX file
root = fileparts(which(libraryName));
stf = get_param(srcModel,'SystemTargetFile');
hardware = get_param(srcModel, 'TargetHWDeviceType');
libCodeFolder = coder.internal.constructLibraryCodeHelper(root, ...
libraryName, ...
stf,...
hardware, ...
'CodeFolder');
% Strip out nonASCII characters, carriage returns and trailing
% spaces from the subsystemName
maxIdLength = get_param(srcModel, 'MaxIdLength');
rls = coder.internal.libcodegen.LibraryCodeFolderUtils.block2FolderName(subsystemName, maxIdLength);
contextCodeFolder = fullfile(libCodeFolder, rls, functionInterfaceName);
end
end
end