%%
%% $Revision $ 
%%
%% Abstract:
%%      TLC file for generating noninlined S-Function helper functions
%%      into their own file.
%%
%% Copyright 2004 The MathWorks, Inc.

%implements "gensfcnhelper" "C"

%% Function: SLibSfcnHelperFcns ===================================================
%% Abstract:
%%     Generate functions needed by S-Functions which call function-call subsystems.
%% 
%function SLibSfcnHelperFcns(fctInfo,fileH,fileC) void
  %% Create header file contents
  %openfile buf
  /*
  * %<fctInfo.FileName>.h
  *
  %<SLibCommonHeaderInfo()>\
  */
  %closefile buf
  %<LibSetSourceFileSection(fileH, "Banner", buf)>
  
  %openfile buf
  #include "rtwtypes.h"
  #include "simstruc.h"

  %closefile buf
  %<LibSetSourceFileSection(fileH, "Includes", buf)>
  
  %openfile buf
  extern int_T rt_CallSys(SimStruct *S, int_T element, int_T tid);
  extern int_T rt_EnableSys(SimStruct *S, int_T element, int_T tid);
  extern int_T rt_DisableSys(SimStruct *S, int_T element, int_T tid);

  %closefile buf
  %<LibSetSourceFileSection(fileH, "Declarations", buf)>
  
  %% Create source file contents
  %openfile buf
  /*
  * %<fctInfo.FileName>.c
  *
  %<SLibCommonHeaderInfo()>\
  *
  */
  %closefile buf
  %<LibSetSourceFileSection(fileC, "Banner", buf)>

  %openfile buf
  /*
  * Abstract:
  *      Real-Time Workshop helper functions for noninlined S-Functions.
  *      Used by noninlined S-functions configured for calling function-call
  *      subsystems.
  */
  
  #include "rt_sfcn_helper.h"
  
  %closefile buf
  %<LibSetSourceFileSection(fileC, "Includes", buf)>
    
  %openfile buf
  /* Helper function to make function calls from non-inlined S-functions */
  int_T rt_CallSys(SimStruct *S, int_T element, int_T tid)
  {
    (*(S)->callSys.fcns[element])((S)->callSys.args1[element], 
    (S)->callSys.args2[element], tid);
    
    if (ssGetErrorStatus(S) != NULL) {
      return 0;
    } else {
      return 1;
    }
  }

  /* Helper function to make function enables from non-inlined S-functions */
  int_T rt_EnableSys(SimStruct *S, int_T element, int_T tid)
  {
    if ((S)->callSys.fcns[ssGetOutputPortWidth(S,0)+element] != NULL) {
      (*(S)->callSys.fcns[ssGetOutputPortWidth(S,0)+element])
      ((S)->callSys.args1[element], 
      (S)->callSys.args2[element], tid);
      
      if (ssGetErrorStatus(S) != NULL) {
	return 0;
      }
    }
    if ((S)->callSys.fcns[2*ssGetOutputPortWidth(S,0)+element] != NULL) {
      (*(S)->callSys.fcns[2*ssGetOutputPortWidth(S,0)+element])
      ((S)->callSys.args1[element], 
      (S)->callSys.args2[element], tid);
      
      if (ssGetErrorStatus(S) != NULL) {
	return 0;
      }
    }
    return 1;
  }
      
  /* Helper function to make function disables from non-inlined S-functions */
  int_T rt_DisableSys(SimStruct *S, int_T element, int_T tid)
  {
    (*(S)->callSys.fcns[3*ssGetOutputPortWidth(S,0)+element])
    ((S)->callSys.args1[element], 
    (S)->callSys.args2[element], tid);
    
    if (ssGetErrorStatus(S) != NULL) {
      return 0;
    } else {
      return 1;
    }
  }

  /* end rt_sfcn_helper.c */
    
  %closefile buf
  %<LibSetSourceFileSection(fileC, "Functions", buf)>
%endfunction
