#include "agnt_rfc.h"

/*=======================================================================
  (C) Copyright SAP AG, 2002

         
 Author  : Dostert/SAP
 Project : Get data from R/3 using XMB
 Using   : RFCSDK 
 ========================================================================*/

/*---------------------------------------------------------------------*/
/*--------Prototypes---------------------------------------------------*/
/*---------------------------------------------------------------------*/  
int R3_connect (int argc,char **argv, RFC_HANDLE *rfc_handle);
int XMI_Logon (RFC_HANDLE *rfc_handle);
int XMI_Increase_Audit_Level (RFC_HANDLE *rfc_handle);
static int check_use_saprfc_ini(char **argv, 
				char *dest, 
				char *client, 
				char *user,
                                char *pass,
                                char *lang);

/*--------------------------------------------------------------------*/
/* Init								      */
/*--------------------------------------------------------------------*/
int sapmon_connect (char *Login_Template, RFC_HANDLE *rfc_handle) {
      char *login;
      char *login_argv[AG_MAX_LOGIN_TEMPLATES];
      int  login_argc = 0;
      char *p;

      int rc;
	
       memset(login_argv, 0, sizeof(login_argv) );

       if ( (login = parse_login_file(Login_Template) )  == NULL) {
              return AG_CALL_INVALID;
        }

        p = strtok(login, " ");
        while (p != NULL && login_argc < AG_MAX_LOGIN_TEMPLATES) {
            login_argv[login_argc++] = p;
            p = strtok(NULL, " ");
        }

	
	rc = R3_connect (login_argc, login_argv, rfc_handle);
	if (rc >= AG_FIRST_ERROR)
		return AG_CALL_INVALID;

	XMI_Logon(rfc_handle);
	if (rc >= AG_FIRST_ERROR)
		return AG_CALL_INVALID;

	
	if (trace_me) {
		XMI_Increase_Audit_Level(rfc_handle);
		if (rc >= AG_FIRST_ERROR)
			return AG_CALL_INVALID;
	}

	return SAP_OK;
}


/*--------------------------------------------------------------------*/
/* connect to an R/3				                      */
/*--------------------------------------------------------------------*/
int R3_connect (int argc, char **argv, RFC_HANDLE *rfc_handle) {
	int use_saprfc_ini = 0;
	char dest[32],
	     client[4] = "000",
       	     user[13]  = "SAP*",
             pass[9]   = "PASS",
             lang[3]   = "EN";
	char connect_param[1024];
	RFC_RC	rfc_rc;

	RFC_ENV                 new_env;
	RFC_ERROR_INFO_EX 	error_info_ex;

	RFC_OPTIONS            rfc_opt;
	RFC_CONNOPT_CPIC       rfc_connopt_cpic;
	RFC_CONNOPT_R3ONLY     rfc_connopt_r3only;
	RFC_CONNOPT_VERSION_3  rfc_connopt_version_3;
	
	memset(&rfc_opt, 0, sizeof(rfc_opt));
	memset(&rfc_connopt_r3only, 0, sizeof(rfc_connopt_r3only));
	memset(&rfc_connopt_version_3, 0, sizeof(rfc_connopt_version_3));

   /*
    * sign on defaults
    */
   rfc_opt.client   = "000";
   rfc_opt.user     = "SAP*";
   rfc_opt.language = "E";
   rfc_opt.password = "PASS";
   rfc_opt.trace    = 0;
   rfc_opt.mode     = RFC_MODE_CPIC;

   /* 
    * read connection and sign on data from the command line
    */
   if(RfcFileArgv(&argc, &argv) == RFC_FAILURE)
   	   printf("Error handling reading or writing commandline arguments from/to file\n");

   /*
    * install error handler
    */
   /*
   new_env.allocate = NULL;
   new_env.errorhandler = rfc_error;
   RfcEnvironment( &new_env );
   */
   use_saprfc_ini = check_use_saprfc_ini(argv, dest, client, user, pass, lang);

   if (trace_me) printf("traceXMI: RfcOpen .. \n");
   if (use_saprfc_ini == 0) {
	   if (trace_me) printf("traceXMI: not using saprfc.ini\n");
	   RfcConnArgv( argv, &rfc_opt, &rfc_connopt_cpic,
                  &rfc_connopt_r3only );
	   
	   if (trace_me) printf("Loging on using DEST=%s CLIENT=%s USER=%s PASSWD=%s \n", 
		   rfc_opt.destination, rfc_opt.client, rfc_opt.user ,rfc_opt.password );

	   *rfc_handle = RfcOpen( &rfc_opt );
   } else {
	   if (trace_me) printf("traceXMI: using saprfc.ini\n");
	   sprintf(connect_param,
		"DEST=%s CLIENT=%s USER=%s PASSWD=%s LANG=%s",
		 dest, client, user, pass, lang);
	   
	   printf("Login on using %s\n", connect_param);
       
	   *rfc_handle = RfcOpenEx(connect_param, &error_info_ex);
 
   }
   
	if (rfc_handle == RFC_HANDLE_NULL) {
		rfc_error("RfcOpen", NULL);
		return AG_CALL_INVALID;
	}

	return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* Close an R/3	-Connection	                                      */
/*--------------------------------------------------------------------*/
int R3_Connection_Close (RFC_HANDLE *rfc_handle) {

	if (trace_me) printf("traceXMI: Closing the connection \n");
	RfcClose(*rfc_handle);
	fflush(stdout);
	return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* logon call to XMI		                                      */
/*--------------------------------------------------------------------*/

int XMI_Logon (RFC_HANDLE *rfc_handle) {

	char 	  *company,
	          *agent,
		  *smapi_short,
		  *version,
	          session_id[24];

	RFC_PARAMETER exporting[5];
	RFC_PARAMETER importing[2];
	RFC_TABLE tables[1];
	char      *exception = NULL;	
	
	char      *function;
		
	RFC_RC	rfc_rc;
	
	function = "SXMI_LOGON";

	company = "SAP_MONI_LIB";
	agent  =  "LL_MONITORING";
	smapi_short = "XAL";
	version     = "1.0";

	rfc_param(&exporting[0], "EXTCOMPANY", TYPC, company, strlen(company));
	rfc_param(&exporting[1], "EXTPRODUCT", TYPC, agent, strlen(agent) );
	rfc_param(&exporting[2], "INTERFACE",  TYPC, smapi_short, strlen(smapi_short));
	rfc_param(&exporting[3], "VERSION",    TYPC, version, strlen(version) );

	rfc_param(&importing[0], "SESSIONID", TYPC, &session_id, 24);


	/* no table ---> terminate array */
	tables[0].name = NULL;

	if (trace_me) { 
		printf("traceXMI: RfcCallReceive is next\n");
		printf("traceXMI: %s\n", function);
	};
	rfc_rc = RfcCallReceive(*rfc_handle,
							function,
							exporting,
							importing,
							tables,
							&exception
							);
	if (trace_me) printf("traceXMI: RfcCallReceive is over, rfc_ret_code = %d \n",rfc_rc);

	switch(rfc_rc) {
           case RFC_OK:
	        break;
	   case RFC_EXCEPTION:
	   case RFC_SYS_EXCEPTION:
        	rfc_error(exception, function);
        	return AG_RFC_EXCEPTION;
	  default:
	        rfc_error("RfcCallReceive", NULL);
	        break;
	 }                   
	return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* logoff call to XMI	      	              			      */
/*--------------------------------------------------------------------*/
int sapmon_disconnect (RFC_HANDLE  *rfc_handle) {
	char smapi_short[4] = "XAL";

	RFC_PARAMETER exporting[2];
	RFC_PARAMETER importing[1];
	RFC_TABLE tables[1];
	char      *exception = NULL;	

	char      *function;
		
	RFC_RC	rfc_rc;
	
	
	function = "SXMI_LOGOFF";

	memset(&importing[0], 0, sizeof(importing));

	rfc_param(&exporting[0], "INTERFACE", TYPC, &smapi_short, 3);
	/* no table ---> terminate array */
	tables[0].name = NULL;

	if (trace_me) { 
		printf("traceXMI: RfcCallReceive is next\n");
		printf("traceXMI: %s\n", function);
	};
	rfc_rc = RfcCallReceive(*rfc_handle,     
							function,
							exporting,
							importing,
							tables,
							&exception
							);
	if (trace_me) printf("traceXMI: RfcCallReceive is back, rfc_ret_code = %d \n",rfc_rc);

        switch(rfc_rc) {
           case RFC_OK:
                break;
           case RFC_EXCEPTION:
           case RFC_SYS_EXCEPTION:
                rfc_error(exception, function);
                return AG_RFC_EXCEPTION;
          default:
                rfc_error("RfcCallReceive", NULL);
                break;
        }                   	

	/* Now close the RFC-Connection */
	return R3_Connection_Close(rfc_handle);


} 


/*--------------------------------------------------------------------*/
/* XMI next call: increase auditlevel                                 */
/*--------------------------------------------------------------------*/

int XMI_Increase_Audit_Level (RFC_HANDLE *rfc_handle) {

	char auditlevel = '0';

	
	RFC_PARAMETER exporting[2];
	RFC_PARAMETER importing[1];
	RFC_TABLE tables[1];
	char      *exception = NULL;	

	char      *function;
		
	RFC_RC	rfc_rc;

	if (trace_me) auditlevel = '1';
	
	function = "SXMI_AUDITLEVEL_SET";

	memset(&importing[0], 0, sizeof(importing));

	rfc_param(&exporting[0], "AUDITLEVEL", TYPC, &auditlevel, 1);

	/* no table ---> terminate array */
	tables[0].name = NULL;

	if (trace_me) { 
		printf("traceXMI: RfcCallReceive is next\n");
		printf("traceXMI: %s\n", function);
	};

	rfc_rc = RfcCallReceive(*rfc_handle,
							function,
							exporting,
							importing,
							tables,
							&exception
							);
	if (trace_me){ 
		printf("traceXMI:    %s exception = %s\n",function,exception);
		printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
		};

        switch(rfc_rc) {
           case RFC_OK:
                break;
           case RFC_EXCEPTION:
           case RFC_SYS_EXCEPTION:
                rfc_error(exception, function);
                return AG_RFC_EXCEPTION;
          default:
                rfc_error("RfcCallReceive", NULL);
                break;
         }                   	
	return SAP_OK;
}


/*--------------------------------------------------------------------*/
/* XMB call: bapi_system_mon_gettree					      */
/*--------------------------------------------------------------------*/

/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MON_GETTREE
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        MAX_TREE_DEPTH like BAPIPARAMS_MAXLEVEL default 0 type RFC_INT length 4
        MONITOR_NAME structure BAPIMNNAME length
120 number of fields 2
        VIS_ON_USR_LEVEL like BAPIPARAMS_VISUSRLEV default 3 type RFC_INT length 4
      importing
        RETURN structure BAPIRET2 length
548 number of fields 14
      tables
        TREE_NODES structure BAPITNDEXT length 440 number of fields 40
      exceptions

------------------------------------------------------------------------
*/

int bapi_system_mon_gettree(RFC_HANDLE  *rfc_handle,
			    char *MoniSetName,
	  		    char *MoniName,
			    int  *MaxTreeDepth,                            
                            ITAB_H TreeNodes) {
	RFC_PARAMETER exporting[5];
	RFC_PARAMETER importing[2];
	RFC_TABLE tables[2];
	char      *exception = NULL;	
	
	char      *function;
	
	BAPIXMLOGR_EXTUSER extern_user = "Agent"; 
	BAPIMNNAME Moni;
	BAPIPARAMS_VISUSRLEV vis = 3;
	
	BAPIRET2 ireturn;

	RFC_RC	rfc_rc;
	
	function = "BAPI_SYSTEM_MON_GETTREE";
	memset(Moni.MsName,' ',sizeof(Moni.MsName));
	memset(Moni.MoniName,' ',sizeof(Moni.MoniName));
	strncpy(Moni.MsName, MoniSetName, strlen(MoniSetName));
	strncpy(Moni.MoniName, MoniName, strlen(MoniName))  ;
	
  if (handleOfBAPIMNNAME==0) {
    rfc_rc = RfcInstallStructure("BAPIMNNAME",
                               typeOfBAPIMNNAME,
                               ENTRIES(typeOfBAPIMNNAME),
                               &handleOfBAPIMNNAME);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIRET2==0) {
    rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPITNDEXT==0) {
    rfc_rc = RfcInstallStructure("BAPITNDEXT",
                               typeOfBAPITNDEXT,
                               ENTRIES(typeOfBAPITNDEXT),
                               &handleOfBAPITNDEXT);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "MAX_TREE_DEPTH", TYPINT, MaxTreeDepth, sizeof(RFC_INT));
  rfc_param(&exporting[2], "MONITOR_NAME", handleOfBAPIMNNAME, &Moni, sizeof(BAPIMNNAME));
  rfc_param(&exporting[3], "VIS_ON_USR_LEVEL", TYPINT, &vis, sizeof(BAPIPARAMS_VISUSRLEV));

  rfc_param(&importing[0], "RETURN", handleOfBAPIRET2, &ireturn, sizeof(BAPIRET2));

  /* define internal tables */
  tables[0].name     = "TREE_NODES";
  tables[0].nlen     = 10;
  tables[0].type     = handleOfBAPITNDEXT;
  tables[0].ithandle = TreeNodes;
  tables[0].itmode   = RFC_ITMODE_BYREFERENCE;

  tables[1].name = NULL;

  if (trace_me) { 
	printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
  			  function,
			  exporting,
			  importing,
			  tables,
			  &exception
			); 

   if (trace_me){ 
	printf("traceXMI:    %s exception = %s\n",function,exception);
	printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
   };

   switch(rfc_rc) {
      case RFC_OK:
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }                   	
   return SAP_OK;
  
}

/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MTE_GETPERFPROP
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        TID structure BAPITID length
84 number of fields 7
      importing
        PROPERTIES structure BAPIPAPROP length 124 number of fields 12
        RETURN structure BAPIRET2 length 548 number of fields 14
      tables
      exceptions

------------------------------------------------------------------------
*/
int bapi_system_mte_getperfprop( RFC_HANDLE *rfc_handle,
				 BAPITID *TID,
				 BAPIPAPROP  *Properties) {    
  int i;
  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[3];
  RFC_TABLE tables[1];
  char      *exception = NULL;	

  char      *function;
  
  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2    iReturn;

  RFC_RC	rfc_rc;

  function = "BAPI_SYSTEM_MTE_GETPERFPROP";

  /* install structures */

  if (handleOfBAPITID==0) {
    rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
        if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIPAPROP==0) {
    rfc_rc = RfcInstallStructure("BAPIPAPROP",
                               typeOfBAPIPAPROP,
                               ENTRIES(typeOfBAPIPAPROP),
                               &handleOfBAPIPAPROP);
  }

  if (handleOfBAPIRET2==0) {
          rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
          if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }


  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));


  rfc_param(&importing[0], "PROPERTIES", handleOfBAPIPAPROP, Properties, sizeof(BAPIPAPROP));
  rfc_param(&importing[1], "RETURN",  handleOfBAPIRET2,  &iReturn, sizeof(BAPIRET2));

  tables[0].name = NULL;


  if (trace_me) {
        printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
   };

   rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );

  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };

  switch(rfc_rc) {
      case RFC_OK:
	   /*	
	   *decimals =  Properties.Decimals;	
	   strncpy(currentValue,Properties.Valunit, 4);
           currentValue[4] = '\0';
	   */	
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
          return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;
}


/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MTE_GETPERFCURVAL
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        TID structure BAPITID length
84 number of fields 7
      importing
        CURRENT_VALUE structure BAPIPACVAL length 104 number of fields 20
        RETURN structure BAPIRET2 length 548 number of fields 14
      tables
      exceptions

------------------------------------------------------------------------
*/

int  bapi_system_mte_getperfcurval( RFC_HANDLE *rfc_handle,
				    BAPITID *TID,
                            	    char    *currentValue) {

  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[3];
  RFC_TABLE tables[1];
  char      *exception = NULL;	
  
  char      *function;

  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2 ireturn;
  BAPIPACVAL CurrentValue;
  
  BAPIPAPROP  Properties;

  RFC_RC      rfc_rc;
  
  int	      i,
	      div;	
  float       curVal;
  int 	      n;

  
  function = "BAPI_SYSTEM_MTE_GETPERFCURVAL";

  /* install structures */
  if (handleOfBAPITID==0) {
    rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIPACVAL==0) {
    rfc_rc = RfcInstallStructure("BAPIPACVAL",
                               typeOfBAPIPACVAL,
                               ENTRIES(typeOfBAPIPACVAL),
                               &handleOfBAPIPACVAL);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIRET2==0) {
    rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));

  rfc_param(&importing[0], "CURRENT_VALUE",  handleOfBAPIPACVAL, &CurrentValue, sizeof(BAPIPACVAL));
  rfc_param(&importing[1], "RETURN",  handleOfBAPIRET2, &ireturn, sizeof(BAPIRET2));

  tables[0].name = NULL;


  if (trace_me) {
        printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );

   if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
   };

   switch(rfc_rc) {
      case RFC_OK:
           bapi_system_mte_getperfprop( rfc_handle,TID, &Properties);

	   for ( i = 0, div = 1; i < Properties.Decimals; i++)
	   	div *= 10;
	   curVal = (float) CurrentValue.Alrelevval / div;	
	   n = snprintf(currentValue, AL_MONI_MAX_TEXT_LENGTH, "%.*f ", Properties.Decimals,curVal); 	
	   currentValue[AL_MONI_MAX_TEXT_LENGTH-1] = '\0';
	   if ( n + 5 < AL_MONI_MAX_TEXT_LENGTH )   
		strncat(currentValue, Properties.Valunit, 4);	
          break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;

}

/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MTE_GETMLCURVAL
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        TID structure BAPITID length
84 number of fields 7
      importing
        CURRENT_VALUE structure BAPIMLCVAL length 96 number of fields 9
        RETURN structure BAPIRET2 length 548 number of fields 14
        XMI_MSG_EXT structure BAPIXMIEXT length 255 number of fields 1
        XMI_MSG_RAW structure BAPIXMIRAW length 690 number of fields 11
      tables
      exceptions

------------------------------------------------------------------------
*/

int bapi_system_mte_getmlcurval(RFC_HANDLE *rfc_handle,
				BAPITID *TID,
				char	*currentValue ) {

  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[5];
  RFC_TABLE tables[1];
  char      *exception = NULL;	

  char      *function;

  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2   iReturn;
  BAPIXMIEXT XmiMsgExt;
  BAPIXMIRAW XmiMsgRaw;
  BAPIMLCVAL CurrentValue;

  RFC_RC	rfc_rc;

  function = "BAPI_SYSTEM_MTE_GETMLCURVAL";

  /* install structures */

  if (handleOfBAPITID==0) {
    rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPIMLCVAL==0) {
	rfc_rc = RfcInstallStructure("BAPIMLCVAL",
                               typeOfBAPIMLCVAL,
                               ENTRIES(typeOfBAPIMLCVAL),
                               &handleOfBAPIMLCVAL);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPIRET2==0) {
	  rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
	  if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPIXMIEXT==0) {
    rfc_rc = RfcInstallStructure("BAPIXMIEXT",
                               typeOfBAPIXMIEXT,
                               ENTRIES(typeOfBAPIXMIEXT),
                               &handleOfBAPIXMIEXT);
    if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPIXMIRAW==0) {
	  rfc_rc = RfcInstallStructure("BAPIXMIRAW",
                               typeOfBAPIXMIRAW,
                               ENTRIES(typeOfBAPIXMIRAW),
                               &handleOfBAPIXMIRAW);
	  if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));

  rfc_param(&importing[0], "CURRENT_VALUE",  handleOfBAPIMLCVAL, &CurrentValue, sizeof(BAPIMLCVAL));
  rfc_param(&importing[1], "RETURN",  handleOfBAPIRET2, &iReturn, sizeof(BAPIRET2));
  rfc_param(&importing[2], "XMI_MSG_EXT",  handleOfBAPIXMIEXT, &XmiMsgExt, sizeof(BAPIXMIEXT));
  rfc_param(&importing[3], "XMI_MSG_RAW",  handleOfBAPIXMIRAW, &XmiMsgRaw, sizeof(BAPIXMIRAW));

  tables[0].name = NULL;

 
  if (trace_me) {
	printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
	                  function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );
	  
  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };

  switch(rfc_rc) {
      case RFC_OK:
           strncpy(currentValue,XmiMsgExt.Msg, AL_MONI_MAX_TEXT_LENGTH-1 ); 
           //strcpy(currentValue,XmiMsgRaw.Msgtext);
           currentValue[AL_MONI_MAX_TEXT_LENGTH-1] = '\0';
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;


}

/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MTE_GETSMVALUE
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        TID structure BAPITID length
84 number of fields 7
      importing
        RETURN structure BAPIRET2 length 548 number of fields 14
        VALUE structure BAPISMCVAL length 276 number of fields 4
      tables
      exceptions

------------------------------------------------------------------------
*/

int bapi_system_mte_getsmvalue( RFC_HANDLE *rfc_handle,
				BAPITID *TID,
				char	*currentValue ) {

  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[3];
  RFC_TABLE tables[1];
  char      *exception = NULL;	

  char      *function;
  
  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2    iReturn;
  BAPISMCVAL  CurrentValue;

  RFC_RC	rfc_rc;

  function = "BAPI_SYSTEM_MTE_GETSMVALUE";

  /* install structures */

  if (handleOfBAPITID==0) {
    rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPISMCVAL==0) {
    rfc_rc = RfcInstallStructure("BAPISMCVAL",
                               typeOfBAPISMCVAL,
                               ENTRIES(typeOfBAPISMCVAL),
                               &handleOfBAPISMCVAL);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPIRET2==0) {
	  rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
	  if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

 
  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));

  
  rfc_param(&importing[0], "RETURN",  handleOfBAPIRET2,  &iReturn, sizeof(BAPIRET2));
  rfc_param(&importing[1], "VALUE",  handleOfBAPISMCVAL, &CurrentValue, sizeof(BAPISMCVAL));

  tables[0].name = NULL;

 
  if (trace_me) {
	printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );
	  
  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };

  switch(rfc_rc) {
      case RFC_OK:
           strncpy(currentValue,CurrentValue.Msg, AL_MONI_MAX_TEXT_LENGTH-1);
           currentValue[AL_MONI_MAX_TEXT_LENGTH-1] = '\0';
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;


}


/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_MTE_GETTXTPROP
      exporting
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        TID structure BAPITID length
84 number of fields 7
      importing
        PROPERTIES structure BAPITXTPRP length 256 number of fields 1
        RETURN structure BAPIRET2 length 548 number of fields 14
      tables
      exceptions

------------------------------------------------------------------------
*/


int bapi_system_mte_gettxtprop( RFC_HANDLE *rfc_handle,
				BAPITID *TID,
				char	*currentValue ) {

  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[3];
  RFC_TABLE tables[1];
  char      *exception = NULL;	

  char      *function;

  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2    iReturn;
  BAPITXTPRP  Properties;

  RFC_RC	rfc_rc;

  function = "BAPI_SYSTEM_MTE_GETTXTPROP";

  /* install structures */

  if (handleOfBAPITID==0) {
    rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

  if (handleOfBAPITXTPRP==0) {
	rfc_rc = RfcInstallStructure("BAPITXTPRP",
                               typeOfBAPITXTPRP,
                               ENTRIES(typeOfBAPITXTPRP),
                               &handleOfBAPITXTPRP);
	if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }


  if (handleOfBAPIRET2==0) {
	  rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
	  if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }

 
  /* define export params */
  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));

  
  rfc_param(&importing[0], "PROPERTIES", handleOfBAPITXTPRP, &Properties, sizeof(BAPITXTPRP));
  rfc_param(&importing[1], "RETURN",  handleOfBAPIRET2,  &iReturn, sizeof(BAPIRET2));
  
  tables[0].name = NULL;

 
  if (trace_me) {
	printf("traceXMI: RfcCallReceive is next\n");
	printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );
	  
  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };

  switch(rfc_rc) {
      case RFC_OK:
           strncpy(currentValue,Properties.Text, AL_MONI_MAX_TEXT_LENGTH-1);
           currentValue[AL_MONI_MAX_TEXT_LENGTH-1] = '\0';
	   break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;


}

/*
------------------------------------------------------------------------
  Call function BAPI_SYSTEM_MTE_SETPERFPROP
      exporting
        DECIMALS like BAPIPAPROP_DECIMALS  type RFC_INT length 4
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
        RELEVANT_VALUE like BAPIPAPROP_RELVALTYPE  type RFC_INT length 4
        THRESHOLD_DIRECTION like BAPIPAPROP_THRESHDIR  type RFC_INT length 4
        THRESHOLD_GREEN_TO_YELLOW like BAPIPAPROP_TRESHG2Y  type RFC_INT length 4
        THRESHOLD_RED_TO_YELLOW like BAPIPAPROP_TRESHR2Y  type RFC_INT length 4
        THRESHOLD_YELLOW_TO_GREEN like BAPIPAPROP_TRESHY2G  type RFC_INT length 4
        THRESHOLD_YELLOW_TO_RED like BAPIPAPROP_TRESHY2R  type RFC_INT length 4
        TID structure ALGLOBTID length
84 number of fields 7
        TID_SPECIFIC like BAPIPARAMS_BOOL_PARA default ' ' type RFC_CHAR length 1
        VALUE_UNIT like BAPIPAPROP_VALUNIT  type RFC_CHAR length 4
        VARIANT_NAME like BAPIVARINT_NAME  type RFC_CHAR length 20
        XMI_MSG_CLASS like BAPIPAPROP_MSGCLASS  type RFC_CHAR length 16
        XMI_MSG_ID like BAPIPAPROP_MSGID  type RFC_CHAR length 30
      importing
        RETURN structure BAPIRET2 length
548 number of fields 14
      tables
      exceptions

------------------------------------------------------------------------
*/

int bapi_system_mte_setperfprop( RFC_HANDLE     *rfc_handle,
				 BAPITID 	*TID,
				 BAPIPAPROP_TRESHG2Y   *ThresholdGreenToYellow,
				 BAPIPAPROP_TRESHR2Y   *ThresholdRedToYellow,
				 BAPIPAPROP_TRESHY2G   *ThresholdYellowToGreen,
				 BAPIPAPROP_TRESHY2R   *ThresholdYellowToRed,
				 BAPIPARAMS_BOOL_PARA  *TidSpecific,
 				 BAPIVARINT_NAME       *VariantName ) {
  /* RFC variables */
  RFC_PARAMETER exporting[15];
  RFC_PARAMETER importing[2];
  RFC_TABLE tables[1];
  char      *exception = NULL;

  char      *function;

  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  BAPIRET2    iReturn;
  BAPIPAPROP_DECIMALS Decimals;
  BAPIPAPROP_RELVALTYPE RelevantValue;
  BAPIPAPROP_THRESHDIR  ThresholdDirection;
  BAPIPAPROP_VALUNIT    ValueUnit;
  BAPIPAPROP_MSGCLASS   XmiMsgClass;
  BAPIPAPROP_MSGID      XmiMsgId;

  int i = 2;	

  RFC_RC        rfc_rc;

  function = "BAPI_SYSTEM_MTE_SETPERFPROP";

  /* install structures */
  if (handleOfBAPITID==0) {
  	 rfc_rc = RfcInstallStructure("BAPITID",
                               typeOfBAPITID,
                               ENTRIES(typeOfBAPITID),
                               &handleOfBAPITID);
        if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIRET2==0) {
  	  rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
	  if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL); 
  }


  rfc_param(&exporting[0], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));
  rfc_param(&exporting[1], "TID",  handleOfBAPITID, TID, sizeof(BAPITID));  
  if (TidSpecific)	
  	  rfc_param(&exporting[i++], "TID_SPECIFIC", TYPC, TidSpecific, sizeof(BAPIPARAMS_BOOL_PARA));
  if(ThresholdGreenToYellow)	
	  rfc_param(&exporting[i++], "THRESHOLD_GREEN_TO_YELLOW", TYPINT, ThresholdGreenToYellow, sizeof(BAPIPAPROP_TRESHG2Y));
  if(ThresholdRedToYellow)	
	  rfc_param(&exporting[i++], "THRESHOLD_RED_TO_YELLOW", TYPINT, ThresholdRedToYellow, sizeof(BAPIPAPROP_TRESHR2Y));
  if(ThresholdYellowToGreen)	
	  rfc_param(&exporting[i++], "THRESHOLD_YELLOW_TO_GREEN", TYPINT, ThresholdYellowToGreen, sizeof(BAPIPAPROP_TRESHY2G));
  if(ThresholdYellowToRed)	
	  rfc_param(&exporting[i++], "THRESHOLD_YELLOW_TO_RED", TYPINT, ThresholdYellowToRed, sizeof(BAPIPAPROP_TRESHY2R));
  if(VariantName)
  	  rfc_param(&exporting[i++], "VARIANT_NAME", TYPC, VariantName, strlen(*VariantName));

  /* rfc_param(&exporting[0], "DECIMALS", TYPINT, &Decimals, sizeof(BAPIPAPROP_DECIMALS)); 
  rfc_param(&exporting[2], "RELEVANT_VALUE", TYPINT, &RelevantValue, sizeof(BAPIPAPROP_RELVALTYPE)); 
  rfc_param(&exporting[3], "THRESHOLD_DIRECTION", TYPINT, &ThresholdDirection, sizeof(BAPIPAPROP_THRESHDIR)); 
  rfc_param(&exporting[10], "VALUE_UNIT", TYPC, &ValueUnit, sizeof(BAPIPAPROP_VALUNIT));
  rfc_param(&exporting[12], "XMI_MSG_CLASS", TYPC, &XmiMsgClass, sizeof(BAPIPAPROP_MSGCLASS));
  rfc_param(&exporting[13], "XMI_MSG_ID", TYPC, &XmiMsgId, sizeof(BAPIPAPROP_MSGID));*/


  rfc_param(&importing[0], "RETURN",  handleOfBAPIRET2,  &iReturn, sizeof(BAPIRET2));

 tables[0].name = NULL;


  if (trace_me) {
        printf("traceXMI: RfcCallReceive is next\n");
        printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );

  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };


  switch(rfc_rc) {
      case RFC_OK:
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;


}


/*
------------------------------------------------------------------------

  Call function BAPI_SYSTEM_ALERT_ACKNOWLEDGE
      exporting
        AID structure BAPIAID length 84 number of fields 7
        EXTERNAL_USER_NAME like BAPIXMLOGR_EXTUSER  type RFC_CHAR length 16
      importing
        RETURN structure BAPIRET2 length
548 number of fields 14
      tables
        BAPIAID_TBL structure BAPIAID_RC length 120 number of fields 9
      exceptions

------------------------------------------------------------------------
*/

int bapi_system_alert_acknowledge(RFC_HANDLE *rfc_handle,
                            BAPIAID *AID) {

  /* RFC variables */
  RFC_PARAMETER exporting[3];
  RFC_PARAMETER importing[2];
  RFC_TABLE tables[2];
  char      *exception = NULL;

  char      *function;

  BAPIXMLOGR_EXTUSER extern_user = "Agent";
  /*
   *ITAB_H thBapiaidTbl = ITAB_NULL;
   */
  BAPIRET2    iReturn;

  RFC_RC        rfc_rc;


  function = "BAPI_SYSTEM_ALERT_ACKNOWLEDGE";
  /*
   * thBapiaidTbl = ItCreate("BAPIAID_TBL",sizeof( BAPIAID_RC),0,0);
   */	
  
  /* install structures */
  if (handleOfBAPIAID==0) {
    rfc_rc = RfcInstallStructure("BAPIAID",
                               typeOfBAPIAID,
                               ENTRIES(typeOfBAPIAID),
                               &handleOfBAPIAID);
        if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIRET2==0) {
    rfc_rc = RfcInstallStructure("BAPIRET2",
                               typeOfBAPIRET2,
                               ENTRIES(typeOfBAPIRET2),
                               &handleOfBAPIRET2);
        if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  if (handleOfBAPIAID_RC==0) {
    rfc_rc = RfcInstallStructure("BAPIAID_RC",
                               typeOfBAPIAID_RC,
                               ENTRIES(typeOfBAPIAID_RC),
                               &handleOfBAPIAID_RC);
        if (rfc_rc != RFC_OK) rfc_error("RfcInstallStructure", NULL);
  }

  rfc_param(&exporting[0], "AID",  handleOfBAPIAID, AID, sizeof(BAPIAID));  
  rfc_param(&exporting[1], "EXTERNAL_USER_NAME", TYPC, &extern_user, strlen(extern_user));

  rfc_param(&importing[0], "RETURN",  handleOfBAPIRET2,  &iReturn, sizeof(BAPIRET2));



  /* define internal tables */
  /*
   * tables[0].name     = "BAPIAID_TBL";
   * tables[0].nlen     = 11;
   * tables[0].type     = handleOfBAPIAID_RC;
   * tables[0].ithandle = thBapiaidTbl;
   *
   * tables[1].name = NULL;
   */
   
  tables[0].name = NULL;
    
  if (trace_me) {
        printf("traceXMI: RfcCallReceive is next\n");
        printf("traceXMI: %s\n", function);
  };

  rfc_rc = RfcCallReceive(*rfc_handle,
                          function,
                          exporting,
                          importing,
                          tables,
                          &exception
                        );

  if (trace_me){
        printf("traceXMI:    %s exception = %s\n",function,exception);
        printf("traceXMI: RfcCallReceive rfc_ret_code = %d\n",rfc_rc);
  };


  switch(rfc_rc) {
      case RFC_OK:
           break;
      case RFC_EXCEPTION:
      case RFC_SYS_EXCEPTION:
           rfc_error(exception, function);
           return AG_RFC_EXCEPTION;
      default:
           rfc_error("RfcCallReceive", NULL);
           break;
   }
   return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* Get current values				                      */
/*--------------------------------------------------------------------*/
int get_current_values(RFC_HANDLE *rfc_handle,ITAB_H TreeNodes, ITAB_H TreeNodeValues) {
     unsigned crow;
     BAPITNDEXT *tTreeNodes;
     BAPITID    tid;
     char	curVal[AL_MONI_MAX_TEXT_LENGTH];	
     char	*ptr;	

     if (trace_me) printf("get_current_values: Lines %d\n", ItFill( TreeNodes)) ;

     ItFree(TreeNodeValues);	


     for (crow = 0;crow < ItFill( TreeNodes); crow++) {
	 tTreeNodes = ItGetLine( TreeNodes,crow+1);

 	 memset(&tid, ' ', sizeof(tid));
	 memset(&curVal, ' ', sizeof(curVal));

         strncpy(tid.Mtsysid, tTreeNodes->Mtsysid, 3); 
         strncpy(tid.Mtmcname, tTreeNodes->Mtmcname, 40); 
         strncpy(tid.Mtnumrange, tTreeNodes->Mtnumrange, 3); 
	 strncpy(tid.Mtuid, tTreeNodes->Mtuid, 10); 
         strncpy(tid.Mtclass, tTreeNodes->Mtclass,3); 
 	 strncpy(tid.Mtindex, tTreeNodes->Mtindex, 10); 
 	 strncpy(tid.Extindex, tTreeNodes->Extindex, 10); 
	
	 
	 if( strncmp(tid.Mtclass,  MT_CLASS_PERFORMANCE, 3) == 0) {
		 bapi_system_mte_getperfcurval(rfc_handle, &tid, curVal);
	 } else if( strncmp(tid.Mtclass,  MT_CLASS_ML, 3) == 0) {
                 bapi_system_mte_getmlcurval(rfc_handle,&tid, curVal);
	 } else if( strncmp(tid.Mtclass,  MT_CLASS_SMES, 3) == 0) {
		 bapi_system_mte_getsmvalue(rfc_handle,&tid, curVal);
	 } else if( strncmp(tid.Mtclass,  MT_CLASS_SHORTTEXT, 3) == 0) {
		 bapi_system_mte_gettxtprop(rfc_handle, &tid, curVal);
	 }

         if (trace_me) printf("curVal %s\n", curVal);
         ptr = ItAppLine( TreeNodeValues );
 
        if( ptr != NULL )  {
            strncpy(ptr, curVal, AL_MONI_MAX_TEXT_LENGTH);
        }

   }		
   return SAP_OK;	
}



/*--------------------------------------------------------------------*/
/* set up RFC parameters: INCREASING order ONLY!!                     */
/*--------------------------------------------------------------------*/
void rfc_param(RFC_PARAMETER *rfc_param,
	       char          *abap_field_name,
	       unsigned      data_type,
	       void          *data_addr,
	       unsigned      data_len)
{
  RFC_PARAMETER *p = rfc_param;

  p->name = (void *)abap_field_name;
  p->nlen = abap_field_name == NULL ? 0 : strlen(abap_field_name);
  p->type = data_type;
  p->addr = data_addr;
  p->leng = data_len;

  /* init next parameter for no more parameter */

  p++;
  p->name = NULL;
  p->nlen = 0;
  p->type = 0;
  p->addr = NULL;
  p->leng = 0;

  return;
} 

/*--------------------------------------------------------------------*/
/* Generate Monitor				                      */
/*--------------------------------------------------------------------*/
int sapmon_request (RFC_HANDLE *rfc_handle, AlMoniHandle_T *MoniHandle, int NrOfMoniTempl) {
    AlMoniTemplate_T *MoniTemplates;
    int i;	 

    for ( i = 0; i < NrOfMoniTempl; i++ ) {
		MoniTemplates = get_template_by_handle(MoniHandle[i]);
                if( bapi_system_mon_gettree (rfc_handle,
                			     MoniTemplates->MoniSetName,
                                             MoniTemplates->MoniName,
                                             &MoniTemplates->MaxTreeDepth,
                                             MoniTemplates->TreeNodes)
                                           >=  AG_FIRST_ERROR) {
                        printf("Could not get treenodes\n");
                        continue;
                        //return AG_CALL_INVALID;
                }

                /* Filter List of MTE through PATTERN */
                Filter_MTEs (MoniHandle[i]);


                // Get more information of left MTEs
                get_current_values(rfc_handle, MoniTemplates->TreeNodes, MoniTemplates->TreeNodeValues);
        }
	return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* Refresh Monitor					              */
/*--------------------------------------------------------------------*/
int sapmon_refresh (RFC_HANDLE *rfc_handle,  AlMoniHandle_T *MoniHandle, int NrOfMoniTempl) {
    AlMoniTemplate_T *MoniTemplates;
    int i;

    for ( i = 0; i < NrOfMoniTempl; i++ ) {
		MoniTemplates = get_template_by_handle(MoniHandle[i]);
                // Get more information of left MTEs
                get_current_values(rfc_handle,MoniTemplates->TreeNodes, MoniTemplates->TreeNodeValues);
        }

   return SAP_OK;
}

/*--------------------------------------------------------------------*/
/* Error Cleanup because of a RFC-Error                               */
/* Because of Windows DLL function must not be defined as static      */
/*--------------------------------------------------------------------*/
void DLL_CALL_BACK_FUNCTION rfc_error(char *operation, char *function) {
	RFC_ERROR_INFO error_info;
	
	memset(&error_info, 0, sizeof(error_info));
	
	printf("\n<==  RfcLastError\n");
	
	RfcLastError(&error_info);
	if (&error_info != NULL) {	
	 	  if (function != NULL)
			printf("\nFUNCTION: %s", function);
		
	          printf("\nRFC operation/code %s\n", operation);
		
		  printf("ERROR/EXCEPTION\n");
		  printf("key     : %s\n", error_info.key);
		  printf("status  : %s\n", error_info.status);
		  printf("message : %s\n", error_info.message);
		  printf("internal: %s\n", error_info.intstat);
		
		  printf("\n<==  RfcClose\n");
		  RfcClose(RFC_HANDLE_NULL);
		
		  exit(1);
	}		  

} /* rfc_error */

/*--------------------------------------------------------------------*/
/* Check the command line for using with or without saprfc.ini        */
/*--------------------------------------------------------------------*/
static int check_use_saprfc_ini(char **argv,
                                char *dest,
                                char *client,
                                char *user,
                                char *pass,
                                char *lang) {
  char 	  *str;
  int     token_2;
  int     rc = 0;
  while( argv[0] != NULL ) {
    if( argv[0][0] == '-' ) {
        str = argv[0] + 2;
        if( argv[0][1] == 0 || str[0] == 0 ) {
           str = argv[1];
           token_2 = 1;
        } else 
	   token_2 = 0;
	
	
        /* option found */
        switch( argv[0][1] )  {
           case 'D' :  /* Destination in saprfc.ini */
             if( str == NULL ) 
				 return 1;
             strcpy(dest, str);
             rc = 1;
             break;
           case 'c' :  /* Client */
             if( str == NULL ) 
				 return 1;
             strcpy(client, str);
             break;
           case 'u' :  /* User */
             if( str == NULL )
				 return 1;
             strcpy(user, str);
             break;

           case 'p' :  /* Password */
             if( str == NULL ) 
				 return 1;
             strcpy(pass, str);
             break;

           case 'l' :  /* Language */
             if( str == NULL ) 
				 return 1;
             strcpy(lang, str);
             break;

           default  :
           token_2 = 0;
           break;
        }
        if( token_2 ) { 
		argv += 2;

	}  else {
		argv++;
	}
    } else {
      *argv++;
    }
  }
  return rc;
}
