classdef LinuxHardwareModule < codertarget.tic2000.hwsetup.HardwareInterface
    % LinuxHardwareModule - Class that covers all hardware specific
    % callbacks.

    % Copyright 2022-23 The MathWorks, Inc.

    properties
        userPath = fullfile('/home',getenv('USER'));
        ccsVersion = strrep(message('TIC2000:hwsetup:CCSVersion').getString, '.', '');
        dummyCCSPath = strcat(fullfile('/home',getenv('USER')),'/ti/ccs',strrep(message('TIC2000:hwsetup:CCSVersion').getString, '.', ''),'/ccs');
        dummyCSPath = strcat(fullfile('/home',getenv('USER')),'/ti/controlSUITE');
        dummyF28044Path = strcat(fullfile('/home',getenv('USER')),'/tidcs/c28/DSP2804x/v130/');
        dummyF280xPath = strcat(fullfile('/home',getenv('USER')),'/tidcs/c28/DSP280x/v170/');
        dummyF281xPath = strcat(fullfile('/home',getenv('USER')),'/tidcs/c28/DSP281x/v120/');
        dummyCGTPath = strcat(fullfile('/home',getenv('USER')),'/ti-cgt-c2000_', message('TIC2000:hwsetup:TICGTVersion').string, '.LTS');
        dummyARMCGTPath = strcat(fullfile('/home',getenv('USER')),'/ti-cgt-arm_', message('TIC2000:hwsetup:ARMCGTVersion').string, '.LTS');
        dummyC2000WarePath = strcat(fullfile('/home',getenv('USER')),'/ti/c2000/C2000Ware_', message('TIC2000:hwsetup:C2000WareVersion').string);
        C2000WareVersion = char(message('TIC2000:hwsetup:C2000WareVersion').string);
        C2000WareVersionTag = strcat('C2000Ware_', message('TIC2000:hwsetup:C2000WareVersion').string);
        % regQueryCmdStringArray = [];
        % regQueryStatus = 1;
    end

    methods
        function [CCSLocation, CCSVersion, ControlSUITELocation, ...
                ControlSUITEVersion, F28044Location, F28044Version, F280xLocation, F280xVersion, ...
                F281xLocation, F281xVersion, C2000WareLocation, C2000WareVersion] ...
                = getCCSAndControlSUITEInfo(obj, ~)
            % Auto detect CCS from the registry, extract install
            % location and installed version
            [CCSLocation, CCSVersion] = getCCSInfo(obj);
            [ControlSUITELocation, ControlSUITEVersion] = getControlSUITEInfo(obj);
            [F28044Location, F28044Version] = getF28044Details(obj);
            [F280xLocation, F280xVersion] = getF280xDetails(obj);
            [F281xLocation, F281xVersion] = getF281xDetails(obj);
            [C2000WareLocation, C2000WareVersion] = getC2000WareInfo(obj);


        end% end of getCCSAndControlSUITEInfo method

        function [CCSLocation, CCSVersion]  = getCCSInfo(obj)
            % Auto detect CCS from the registry, extract install
            % location and installed version
            CCSLocation = '';
            CCSVersion = '';
            ccsInstallationCount = 0;
            ccsInstallationLocation = {''};
            ccsInstallVersion = {''};
            folderName = " 'ccs*'";
            command = strcat('find $HOME/ti -maxdepth 1 -type d -name ',folderName,'-print');

            [~, ccsInstallationLocation] = system (command);

            %their version
            for i=1:ccsInstallationCount
                % Extract only first digit from version
                versionNumber = str2double(regexp(ccsInstallVersion{i}, '(\d+)(\.|$)', 'match'));
                switch numel(versionNumber)
                    case 0
                        versionMajor{i} = 0; %#ok<AGROW> if regexp fails, if version number is weird
                    case 1
                        % No need to handle corner cases here as version is
                        % read from registry
                        versionMajor{i} = versionNumber(1)*100; %#ok<AGROW>
                    case 2
                        versionMajor{i} = versionNumber(1)*100 + versionNumber(2)*10; %#ok<AGROW>
                    case 3
                        versionMajor{i} = versionNumber(1)*100 + versionNumber(2)*10 + versionNumber(3); %#ok<AGROW>
                    otherwise
                        versionMajor{i} = versionNumber(1)*100 + versionNumber(2)*10 + versionNumber(3); %#ok<AGROW>
                end
            end
            if ccsInstallationCount > 1
                % Check for more than one CCS installation
                versionNumber = cell2mat(versionMajor);
                % Find the latest tested version
                maxTestedVer = max(versionNumber(versionNumber(:) <= str2double(strrep(message('TIC2000:hwsetup:CCSVersion').getString, '.', ''))));
                if ~isempty(maxTestedVer)
                    maxIndex = find([versionMajor{:}] == maxTestedVer); % find index of max tested version
                else
                    maxVer = max(versionNumber(:)); % Find the latest version
                    maxIndex = find([versionMajor{:}] == maxVer); % find index of max version
                end
                CCSLocation =  ccsInstallationLocation{maxIndex}; % get CCS location
                CCSVersion =  ccsInstallVersion{maxIndex};
            elseif ccsInstallationCount == 1
                % Check for only one CCS installation
                CCSLocation =  ccsInstallationLocation{ccsInstallationCount};
                CCSVersion =  ccsInstallVersion{ccsInstallationCount};
            else
                % No CCS installation found
                CCSLocation = '';
                CCSVersion = '';
            end

        end% end of getCCSInfo method

        function [ControlSUITELocation, ControlSUITEVersion]  = getControlSUITEInfo(obj)
            % Auto detect ControlSUITE from the registry, extract install
            % location and installed version
            ControlSUITELocation = '';
            ControlSUITEVersion = '';
           
            %ControlSUITE installables only present for windows. User must install in windows and copy folder to Linux platform

        end% end of getControlSUITEInfo methods

        function version = getControlSUITEVersion(~, ControlSUITELocation)
            tempLocation = ControlSUITELocation;
            if exist(tempLocation, 'dir') == 7
                try
                    path = fullfile(tempLocation, 'Release Notes.txt');
                    text = fileread(path);
                    fullmatch = regexpi(text, '\d+(\.\d+)+', 'match');%Extract only version numbers
                    version = fullmatch{1}; % getting only first line
                catch
                    version = '';
                end
                version = strtrim(version);
            else
                version = '';
            end
        end

        function [F28044Location, F28044Version]  = getF28044Details(obj)
            % Auto detect Software with name 'C2804x C/C++ Header Files and Peripheral Examples' from the registry, extract install
            % location and installed version
            F28044Location = '';
            F28044Version = '';
            
            %DSP2804x installables only present for windows. User must install in windows and copy folder to Linux platform
            if contains(F28044Location, 'v130')
                F28044Version = 'v130';
            end
        end% end of getControlSUITEInfo method

        function [C2000WareLocation, C2000WareVersion]  = getC2000WareInfo(obj)
            % Auto detect C2000Ware from home location and update its path
            C2000WareLocation = obj.dummyC2000WarePath;
            C2000WareVersion = '';

            %Write logic here to find C2000Ware install location and
            %version. No command line function to detect installation but if we know the user path we can search for folder with name C2000Ware
            
        end% end of getC2000WareInfo method

        function [F280xLocation, F280xVersion]  = getF280xDetails(obj)
            % Auto detect C280xinstall not possible in Linux
            F280xLocation = '';
            F280xVersion = '';
            
            %DSP280x installables only present for windows. User must install in windows and copy folder to Linux platform
            if contains(F280xLocation, 'v170')
                F280xVersion = 'v170';
            end
        end% end of getF280xDetails method

        function [F281xLocation, F281xVersion]  = getF281xDetails(obj)
            % Auto detect C281xinstall not possible in Linux
            F281xLocation = '';
            F281xVersion = '';
            
            %DSP281x installables only present for windows. User must install in windows and copy folder to Linux platform
            if contains(F281xLocation, 'v120')
                F281xVersion = 'v120';
            end
        end% end of getF281xDetails method
    end
end