CIN MgErr CINRun(LStrHandle pathname, int32 *flags, int32 *fd, int32 *UnixErr, int32 *LVErr) {

  int32 pathnamelen;

  pathnamelen = LStrLen(*pathname) + 1;
  *UnixErr = 0;
  *LVErr = noErr;
  if ( (*LVErr = NumericArrayResize(uB, 1L, (UHandle*)&pathname, pathnamelen))) {
    goto out;
  } else {
    LStrLen(*pathname) = pathnamelen;
    LStrBuf(*pathname)[pathnamelen - 1] = 0;
    if ( ( *fd = open(LStrBuf(*pathname), *flags) ) < 0 ) {
      *UnixErr = (int32) errno;
      *LVErr = (int32) UnixToLVErr();
      goto out;
    } else if ( fcntl(*fd, F_SETFL, *flags) < 0 ) {
      *UnixErr = (int32) errno;
      *LVErr = (int32) UnixToLVErr();
      goto out;
    }
    *flags = fcntl(*fd, F_GETFL);
  };
 out:
  return noErr;
}
CIN MgErr CINRun(LStrHandle Rtai_lxrt_t, int16 *Dynx, int16 *Size, int32 *Srq, LStrHandle Arg, LStrHandle Buffer, int32 *BufPosInArg, int32 *UnixErr, int32 *LVErr) {

  int  DEBUG=0;  
  union rtai_lxrt_t retval;
  int *iArg=NULL;
  char lbuf[LStrLen(*Buffer)+1]; // dynamically allocate on Stack; +1 if it is zero

  *UnixErr = 0;
  *LVErr = noErr;
  DEBUG=LStrLen(*Rtai_lxrt_t)+1;
  if (DEBUG>2) DEBUG=0;
  if (DEBUG) {  
    DbgPrintf(NULL); /* Close window */
    DbgPrintf("Debuglevel:%d, calling lxrt:%d\n", DEBUG, DEBUG-1);
  }
  // DbgPrintf("DEBUG=%d\n", DEBUG);

  /* set size of return string */
  
  if ((*LVErr = NumericArrayResize( uB, 1L, (UHandle *)&Rtai_lxrt_t, sizeof(union rtai_lxrt_t)))) {
    goto out;
  } else {
    LStrLen(*Rtai_lxrt_t) = sizeof(union rtai_lxrt_t);
  }
  
  
  /* set size of argument string according to int[12], see rtai_rtai.h  */
  
  if ((*LVErr = NumericArrayResize( uB, 1L, (UHandle *)&Arg, sizeof(int[12])))) {
    goto out;
  } else {
    LStrLen(*Arg) = sizeof(int[12]);
  }
  
  iArg=(int*)LStrBuf(*Arg);
  
  /* do we have to insert a Buffer into the arguments? */
  if ((*BufPosInArg > 0) && (*BufPosInArg < 12)) {
    if (DEBUG) DbgPrintf("Handling BufferAdr to inserted at PosInArg=%d with BufferLen=%d\n", *BufPosInArg, LStrLen(*Buffer));
    /* copy Buffer to local mem on stack */
    if (DEBUG) DbgPrintf("Doing memcpy(0x%x, 0x%x, %d)", lbuf, LStrBuf(*Buffer), LStrLen(*Buffer));
    memcpy(lbuf, LStrBuf(*Buffer), LStrLen(*Buffer));
    /* and insert &lbuf into *Arg */
    ((int*)LStrBuf(*Arg))[*BufPosInArg] = (int)lbuf;
  }
  
  //*Size = (int16)LStrLen(*Arg);
  if (DEBUG) {  
    /* print arguments */
    DbgPrintf("(int) Arg[0...3]: 0x%x 0x%x 0x%x 0x%x\n", iArg[0],iArg[1],iArg[2],iArg[3]);
    DbgPrintf("(int) Arg[4...7]: 0x%x 0x%x 0x%x 0x%x\n", iArg[4],iArg[5],iArg[6],iArg[7]);
    DbgPrintf("(int) Arg[8..11]: 0x%x 0x%x 0x%x 0x%x\n", iArg[8],iArg[9],iArg[10],iArg[11]);
        
    if ((*BufPosInArg > 0) && (*BufPosInArg < 12)) {
      int i;
      DbgPrintf("Buffer string from Arg[%d]:", *BufPosInArg);
      for (i=0; i<LStrLen(*Buffer); i++) {
	DbgPrintf("0x%02x", (int) (((char*)(iArg[*BufPosInArg]))[i]));
      }
    }
  }
  if ((DEBUG==0) || (DEBUG==2)) {
    /* rtai_lxrt call */
    if (DEBUG==2) {
      DbgPrintf("Calling rtai_lxrt(Dynx=%d, Size=%d, Srq=%d, int Arg[0..11])\n",(int) *Dynx, (int) *Size, (int) *Srq);  
    }
    retval = rtai_lxrt( *Dynx, *Size, *Srq, LStrBuf(*Arg));
  }
  /* copy retval back to first eight bytes of LStrBuf(*Rtai_lxrt_t) */
  if (DEBUG==2) {
    DbgPrintf("Retval[0..1]: %x %x\n", retval.i[LOW], retval.i[HIGH]);
  }
  *((int32 *)LStrBuf(*Rtai_lxrt_t)) = retval.i[LOW];
  *((int32 *)LStrBuf(*Rtai_lxrt_t)+1) = retval.i[HIGH];
  
  /* if Buffer used copy back from Stack */
  if ((*BufPosInArg > 0) && (*BufPosInArg < 12)) {
    int i;
    if (DEBUG) DbgPrintf("Doing memcpy(0x%x, 0x%x, %d)", LStrBuf(*Buffer), lbuf, LStrLen(*Buffer));
    memcpy(LStrBuf(*Buffer), lbuf, LStrLen(*Buffer));
    if (DEBUG) {
      DbgPrintf("Buffer contents now:");
      for (i=0; i<LStrLen(*Buffer); i++) {
	DbgPrintf("0x%02x", (int) (((char*)LStrBuf(*Buffer))[i]));
      }
    }
  }
  
  *UnixErr = (int32) 0; //errno;
  *LVErr = (int32) 0; //UnixToLVErr();
 out:
  return noErr;
}
#include "cphidgetweightsensor.h"

#include "csocket.h"

#ifdef COMPILE_PHIDGETS_LABVIEW

#include "phidget_labview.h"

LV_CFHANDLE_0(, Attach, lvNothing)
LV_CFHANDLE_0(, Detach, lvNothing)
LV_CFHANDLE_0(, ServerConnect, lvNothing)
LV_CFHANDLE_0(, ServerDisconnect, lvNothing)
LV_CFHANDLE_BODY(, Error, lvError, void *userPtr, int val1, const char *val2)
	data->val1 = val1;
    data->val2=(LStrHandle)DSNewHandle(sizeof(int32)+255*sizeof(char));
	memset(LStrBuf(*data->val2),'\0',255);
	snprintf((char*)LStrBuf(*data->val2),255,"%s",val2);
	LStrLen(*data->val2)=strlen(val2);

    ret = PostLVUserEvent(ev, data);

	DSDisposeHandle(data->val2);
    DSDisposePtr(data);
	return EPHIDGET_OK;
}

LV_CFHANDLE_2(Accelerometer, AccelerationChange, lvIndexedDouble, int, double)

LV_CFHANDLE_2(AdvancedServo, PositionChange, lvIndexedDouble, int, double)
LV_CFHANDLE_2(AdvancedServo, VelocityChange, lvIndexedDouble, int, double)
LV_CFHANDLE_2(AdvancedServo, CurrentChange, lvIndexedDouble, int, double)