#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef UNIX
# include <malloc.h>
#else
# include <stdlib.h>
#endif
#include "vxUtils.h"
#include "MicroInterface.h"

MicroInterface::MicroInterface(const char *name, Nat16 serialChanNo)
{
  char chanName[256];
  sprintf(chanName, "/sio32/%s", name);
  _chanName = strdup(chanName);
  
  _serialChanNo = serialChanNo;
  
  if (sio32ChanInit(serialChanNo, _chanName, &_sio32Chan) == ERROR) 
  {
  }
}


MicroInterface::~MicroInterface()
{
  free(_chanName);
  sio32ChanDestroy(_serialChanNo, &_sio32Chan);
}

#ifndef UNIX
int MicroInterface::sendCmd(Void *reply,
			    Int32 replyLen,
			    Int32 nWords, ... )

{
    FAST  Int32   arg;               /* Argument counter                      */
    FAST Int32   argVal;   	    /* Argument covnerted to an integer      */
    va_list argPtr;                 /* Argument list pointer                 */
    Byte buffer[MAX_PACKET_LEN];    /* Buffer containing date to send        */
    FAST Byte  *bufPtr = buffer;    /* Pointer to buffer input position      */
    STATUS  status;		    /* Return status of read call            */

    va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument */

    for (arg = 0; arg < nWords; arg++)
    {
        argVal = va_arg(argPtr, Int32);     /* Get next word argument        */

        *bufPtr++ = (Byte) (argVal >> 8);   /* store high byte               */
        *bufPtr++ = (Byte) (argVal & 0xff); /* store low byte                */
    } /* for */
 
    va_end(argPtr);                 /* Cleanup var args stuff                */

    if (semTake(_sio32Chan.mutexSem, _sio32Chan.timeout) == ERROR)
	return(ERROR);

    sio32ChanRxFlush(&_sio32Chan); /* Discard any old packets               */

                                    /**** Send Data Packet to Serial Port ****/
    writeServerPacket(&_sio32Chan, (Byte*) buffer, nWords * sizeof(Word));

    if (reply != NULL)		    /* If reply buffer is NULL then exit     */
    				    /* Read received packet from serial port */
        status = readServerPacket(&_sio32Chan, (Byte *) reply, replyLen,
	     _sio32Chan.timeout);
        
    else
	status = OK;		    /* No reply expected so return OK        */

    semGive(_sio32Chan.mutexSem);/* Release access to serial channel      */

    return(status);
}


int MicroInterface::sendWord(Reg Char* buffer, Word value)
{
  return 0;
}

Word MicroInterface::getWord(Void *value, Int32 nWord)
{
  return (wordFromBuf(value, nWord));
}

int MicroInterface::getWords(Word *buffer, Int32 nWords, ... )
{
    FAST Int32   arg;               /* Argument counter                      */
    FAST Word    *ptr;              /* Copy of buffer pointer                */
    FAST Word    val;
    va_list argPtr;                 /* Argument list pointer                 */

    va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument */

    for (arg = 0; arg < nWords; arg++)
    {
        ptr = va_arg(argPtr, Word *);     /* Get next word argument          */
                                    /* Get high and low bytes and build word*/
        val  =  (* ((Byte *) buffer)++ << 8);
        val +=  (* ((Byte *) buffer)++);
	*ptr = val;
    } /* for */

    va_end(argPtr);                 /* Cleanup var args stuff                */
}

int MicroInterface::readSRQ(Byte *packet, Int16 maxBytes, Int32 timeout)
{
  return(readSRQPacket(&_sio32Chan, packet, maxBytes, timeout));
}


int MicroInterface::reset()
{
  microResetCmd(&_sio32Chan);
  return 0;
}
#endif










