/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include<ch.h>
#include<stdio.h>

static ChInterp_t interp;
/* C functions to replace the Ch function pointers */
static return_type1 setFunction6_chdl_funarg1(data_type1 arg1); 
static return_type2 setFunction6_chdl_funarg2(data_type2 arg2); 

/* save the Ch function pointers */
static void *setFunction6_chdl_funptr1, *setFunction6_chdl_funptr2; 

EXPORTCH return_type3 setFunction6_chdl(void *varg) {
  ChVaList_t ap;
  data_type3 arg3;
  struct s_t *arg4;
  return_type3 retval;

  Ch_VaStart(interp, ap, varg);
  arg3 = Ch_VaArg(interp, ap, data_type3);   /* get 1st argument  */

  arg4 = Ch_VaArg(interp, ap, struct s_t *); /* get pointer to a struct from Ch space */
  if(arg4 != NULL) { /* save the Ch function pointer */
    setFunction6_chdl_funptr1 = (void*)arg4->funptr1; // used for callback 
    if( arg4->funptr1 != NULL) {
      arg4->funptr1 = setFunction6_chdl_funarg1;
    }

    setFunction6_chdl_funptr2 = (void*)arg4->funptr2; // used for callback 
    if( arg4->funptr2 != NULL) {
       arg4->funptr2 = setFunction6_chdl_funarg2;
    }
  }
  retval = setFunction6(arg3, arg4);

  if(arg4 != NULL) { /* restore the original pointers */
    arg4->funptr1 = setFunction6_chdl_funptr1;
    arg4->funptr2 = setFunction6_chdl_funptr2;
  }
  Ch_VaEnd(interp, ap);
  return retval;
}

/* C function to replace Ch function pointers as struct members */
static return_type1 setFunction6_chdl_funarg1(data_type1 arg1) {
  return_type1 retval;
  Ch_CallFuncByAddr(interp, setFunction6_chdl_funptr1, &retval, arg1);
  return retval;
}
static return_type2 setFunction6_chdl_funarg2(data_type2 arg2) {
  return_type2 retval; 
  Ch_CallFuncByAddr(interp, setFunction6_chdl_funptr2, &retval, arg2);
  return retval;
}
