function [transports, mexfiles, interfaces] = ccsTarget_extmode_registration(cs)
% Register external mode interfaces for TI C2000
  
% Copyright 2008-2022 The MathWorks, Inc.
%

sysTargetFile = get_param(cs, 'SystemTargetFile');
showTfExtMode = isequal(sysTargetFile, linkfoundation.util.getSTFName('ert')) || ...
    isequal(sysTargetFile, linkfoundation.util.getSTFName('grt')) || ...
    (isequal(sysTargetFile, 'ert.tlc') && codertarget.target.isCoderTarget(cs));
c2000ChipSelected = local_HWDeviceTypeEq(cs.getProp('ProdHWDeviceType'), 'TI C2000');
c6000ChipSelected = local_HWDeviceTypeEq(cs.getProp('ProdHWDeviceType'), 'TI C6000');
if (showTfExtMode && c2000ChipSelected)
    transports = { 'CAN', 'serial', 'serialNoAck', 'tcpip', 'XCP on Serial', 'XCP on CAN'};
    mexfiles = { 'TIC2000ExternalModeOpen', 'ext_serial_win32_comm',  'ext_serial_win32_comm_no_acks', 'ext_comm', 'ext_xcp', 'ext_xcp'};
    interfaces = { 'Level2 - Open', 'Level1', 'Level1', 'Level1', 'Level2 - Open', 'Level2 - Open'};
    %Change the SetExtModeCAN.m in matlab\toolbox\c2b\tic2000\+codertarget\+registry
    % if order of the file is changed
%     transports = { 'serial',  'CAN' };
%      mexfiles = {  'ext_serial_win32_comm', 'TIC2000ExternalModeOpen'};
%      interfaces = { 'Level1', 'Level2 - Open' };

elseif (showTfExtMode && c6000ChipSelected)
    transports = {'tcpip'}; % Index 0 in the template makefiles
    mexfiles = {'ext_comm'}; % MEX-File for 'tcpip'
    interfaces = {'Level1'};
else
    transports = {'tcpip'}; 
    mexfiles = {'ext_comm'}; 
    interfaces = {'Level1'};
end

%--------------------------------------------------------------------------
%Using local function in absence of code generation API.
%Replace when code generation API is available
function result = local_HWDeviceType(type)

hh = targetrepository.getHardwareImplementationHelper();
dev = hh.getDevice(type);

if isempty(dev.Manufacturer)
    result = [dev.Name '->' dev.Name];
else
    result = [dev.Manufacturer '->' dev.Name];
end

%--------------------------------------------------------------------------
function result = local_HWDeviceTypeEq(type1,type2)

dev1 = local_HWDeviceType(type1);
dev2 = local_HWDeviceType(type2);
result = strcmp(dev1, dev2);


