% Autogenerated file: Mon Mar 26 22:18:03 2012
function writeCodeInfoFcn

% Load a ComponentInterface object from IR
codeIRInfoStruct = load('C:\Users\amir\Documents\My Dropbox\Projects\simulink_lecture\Final_2\simulink_coder_SIL\Subsystem_ert_rtw\tlc\codeIRInfo.mat');
codeInfo = codeIRInfoStruct.codeInfo;




  if ~isempty([])
  codeInfo.Parameters([]) = [];
  end

tmpParams = [];
if ~isempty(tmpParams)
codeInfo.Parameters = [codeInfo.Parameters; tmpParams];
end

  tmpArgs         = [];
tmpActualArgs   = [];
tmpRet          = [];
tmpActualReturn = [];
  

  Initialize_Prototype = RTW.CImplementation;
  Initialize_Prototype.Arguments  = tmpArgs;
  Initialize_Prototype.Return     = tmpRet;
  Initialize_Prototype.HeaderFile = ['Subsystem', '.h'];
  Initialize_Prototype.SourceFile = ['Subsystem', '.c'];

  Initialize_Interface = RTW.FunctionInterface;
  Initialize_Interface.Prototype    = Initialize_Prototype;
  Initialize_Interface.ActualArgs   = tmpActualArgs;
  Initialize_Interface.ActualReturn = tmpActualReturn;

      Initialize_Prototype.Name = 'Subsystem_initialize';
  time_constant = codeInfo.TimingProperties(getIndexFromTimingInternalId(codeInfo.TimingInternalIds, -1));
      Initialize_Interface.Timing = time_constant;
      codeInfo.InitializeFunctions =  [codeInfo.InitializeFunctions; Initialize_Interface'];

        tmpArgs         = [];
tmpActualArgs   = [];
tmpRet          = [];
tmpActualReturn = [];
  

  OutputUpdate_Prototype = RTW.CImplementation;
  OutputUpdate_Prototype.Arguments  = tmpArgs;
  OutputUpdate_Prototype.Return     = tmpRet;
  OutputUpdate_Prototype.HeaderFile = ['Subsystem', '.h'];
  OutputUpdate_Prototype.SourceFile = ['Subsystem', '.c'];

  OutputUpdate_Interface = RTW.FunctionInterface;
  OutputUpdate_Interface.Prototype    = OutputUpdate_Prototype;
  OutputUpdate_Interface.ActualArgs   = tmpActualArgs;
  OutputUpdate_Interface.ActualReturn = tmpActualReturn;

      OutputUpdate_Prototype.Name = 'Subsystem_step';
  time_0 = codeInfo.TimingProperties(getIndexFromTimingInternalId(codeInfo.TimingInternalIds, 0));
      OutputUpdate_Interface.Timing = time_0;
      codeInfo.OutputFunctions =  [codeInfo.OutputFunctions; OutputUpdate_Interface'];

  
    tmpArgs         = [];
tmpActualArgs   = [];
tmpRet          = [];
tmpActualReturn = [];
  

  Terminate_Prototype = RTW.CImplementation;
  Terminate_Prototype.Arguments  = tmpArgs;
  Terminate_Prototype.Return     = tmpRet;
  Terminate_Prototype.HeaderFile = ['Subsystem', '.h'];
  Terminate_Prototype.SourceFile = ['Subsystem', '.c'];

  Terminate_Interface = RTW.FunctionInterface;
  Terminate_Interface.Prototype    = Terminate_Prototype;
  Terminate_Interface.ActualArgs   = tmpActualArgs;
  Terminate_Interface.ActualReturn = tmpActualReturn;

  time_constant = codeInfo.TimingProperties(getIndexFromTimingInternalId(codeInfo.TimingInternalIds, -1));
      Terminate_Interface.Timing = time_constant;
      Terminate_Prototype.Name = [codeInfo.Name, '_terminate'];
      codeInfo.TerminateFunctions =  [codeInfo.TerminateFunctions; Terminate_Interface'];




% Handling Right-Click Builds
ss = rtwprivate('getSourceSubsystemHandle',codeInfo.GraphicalPath);
% Check if Rt-Click build, then re-map SIDs
if ~isempty(ss) && rtwprivate('rtwattic','hasSIDMap')
   codeInfo = modifyCodeInfoForSubsystemBuild(ss, codeInfo);
end

save codeInfo.mat codeInfo ;
  
returnVal = 1;

% End Function: writeCodeInfoFcn

function idx = getIndexFromTimingInternalId(internalIdVec, internalId)
 idx = find(internalIdVec == internalId);
 if (isempty(idx) || (length(idx) > 1))
    ciMsg = 'Time object cannot be empty'; 
    ciExc = MException('RTW:buildProcess:CodeInfoInternalError',ciMsg);
    throw(ciExc);
  end
% End Function: getIndexFromTimingInternalId

function checkDataGraphicalNames(ciName, rtwName)
  if (strcmp(ciName, rtwName) ~= 1)
    ciMsg = ['Name mismatch: ', ciName, ' and ', rtwName]; 
    ciExc = MException('RTW:buildProcess:CodeInfoInternalError',ciMsg);
    throw(ciExc);
  end
% End Function: checkDataGraphicalNames

function iData = getInternalDataByName(iDataVec, iDataName)
 iData = find(iDataVec, 'GraphicalName', iDataName);
 if (isempty(iData) || (length(iData) > 1))
    ciMsg = 'Internal Data object cannot be empty'; 
    ciExc = MException('RTW:buildProcess:CodeInfoInternalError',ciMsg);
    throw(ciExc);
  end
% End Function: getInternalDataByName

function iData = getInternalDataByVariableName(iDataVec, iDataVariableName)
 iData = [];
 for idxData = 1:numel(iDataVec)
    if strcmp(iDataVariableName, iDataVec(idxData).Implementation.VariableName)
       iData = iDataVec(idxData);
       break
    end
 end
 if isempty(iData)
    ciMsg = 'Internal Data object cannot be empty'; 
    ciExc = MException('RTW:buildProcess:CodeInfoInternalError',ciMsg);
    throw(ciExc);
  end
% End Function: getInternalDataByVariableName

function codeInfo = modifyCodeInfoForSubsystemBuild(ss, codeInfo)
    
% Extract Subsystem Build Map from AtticData
    Subsystem_Build_Mapping = rtwprivate('rtwattic','getSIDMap');

% Parameters
    for i = 1:length(codeInfo.Parameters)
        sid = codeInfo.Parameters(i).SID;        

        sid = Simulink.ID.getSubsystemBuildSID(sid,ss, Subsystem_Build_Mapping);
        codeInfo.Parameters(i).SID = sid;        
    end
    
% Data Stores
     for i = 1:length(codeInfo.DataStores)
         sid = codeInfo.DataStores(i).SID;         

         sid = Simulink.ID.getSubsystemBuildSID(sid,ss, Subsystem_Build_Mapping);
         codeInfo.DataStores(i).SID = sid;
     end
    
% Inports
    for i = 1:length(codeInfo.Inports)
        sid = codeInfo.Inports(i).SID;
        
        sid = Simulink.ID.getSubsystemBuildSID(sid,ss, Subsystem_Build_Mapping);
        codeInfo.Inports(i).SID = sid;
    end
    
% Outports
    for i = 1:length(codeInfo.Outports)
        sid = codeInfo.Outports(i).SID;
 
        sid = Simulink.ID.getSubsystemBuildSID(sid,ss, Subsystem_Build_Mapping);
        codeInfo.Outports(i).SID = sid;
    end    
   
