/****************************************************************************/
/* Copyright 1995 MBARI                                                     */
/****************************************************************************/
/* Summary  : Application layer interface to microcontroller communications */
/*            Via a quad Serial Board                                       */
/* Filename : qsapplay.c                                                    */
/* Author   : Douglas Au                                                    */
/* Project  : ROV 1.5 Microcontroller Board Rev 2.0                         */
/* Version  : 1.0                                                           */
/* Created  : 10/23/95                                                      */
/* Modified : 10/23/95                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header:
 * $Log:
 * Revision
 * Initial revision
 *
*/
/****************************************************************************/
                                        /* Generate code for the 80C196KB   */
#pragma model(196)
#pragma nosignedchar                    /* Use unsigned char (Char)         */

#include "C:/C96/include/80C196.h"      /* 80196 Register mapping           */
#include "C:/C96/include/stddef.h"      /* C Standard Definitions           */
#include "C:/C96/include/stdarg.h"      /* C var args Definitions           */

#include "const.h"                      /* Misc. constants - TRUE/FALSE etc */
#include "types.h"                      /* MBARI style guide declarations   */
#include "microasm.h"                   /* Assembly language functions      */
#include "malloc.h"                     /* Malloc support definitions       */
#include "microdef.h"                   /* Microcontroller Definitions      */
#include "microlib.h"                   /* Microcontroller Library decls.   */
#include "qsserial.h"                  /* data link layer functions        */
#include "qs_proto.h"                   /* protocol layer functions         */
#include "qsapplay.h"                   /* application layer functions      */

/****************************************************************************/
/* Function    : initQsSerialProtocol                                       */
/* Purpose     : Application layer function to initialize serial protocol   */
/* Inputs      : None                                                       */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
initQsSerialProtocol( Void (*disconnectFunc) () )
{
    initQsDataLinkLayer();              /* Initialize data link layer       */
    initQsProtocolLayer();              /* Initialize protocol layer        */
                                        /* Add disconnect function hook     */
    dataLinkDisconnectHook(disconnectFunc);
} /* initQsSerialProtocol() */

/****************************************************************************/
/* Function    : freeRecvdPacket                                            */
/* Purpose     : Frees packet previously read with readRecvdPacket          */
/* Inputs      : packet buffer                                              */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
freeRecvdPacket(Byte *packet)
{
     free(packet);
} /* freeRecvdPacket() */

/****************************************************************************/
/* Function    : qsWriteWordPacket                                          */
/* Purpose     : Applic. layer function to send a word using protcol layer  */
/* Inputs      : Word to send                                               */
/* Outputs     : Returns ERROR or OK                                        */
/****************************************************************************/
    Int16
qsWriteWordPacket(Word value)
{
    Byte    buffer[sizeof(value)];     /* buffer to hold the data          */

    wordToBuf(buffer, value);          /* copy the status word into buffer */
                                       /* and write to serial port         */
    return(writeDataPacket(buffer, sizeof(value)));
} /* qsWriteWordPacket() */

/****************************************************************************/
/* Function    : replyToCommand                                             */
/* Purpose     : Send serial command reply to serial port                   */
/* Inputs      : Number of arguments, list of word arguments                */
/* Outputs     : Returns result of writeDataPacket                          */
/****************************************************************************/
    Int16
qsReplyToCommand(Int16 nWords, ... )
{
    Int16   arg;                    /* Argument counter                     */
    va_list argPtr;                 /* Argument list pointer                */
    Byte    buffer[MAX_REPLY_WORDS * sizeof(Word)];
    Byte   *bufPtr = buffer;

    va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument*/

    for (arg = 0; arg < nWords; arg++)
    {
        *bufPtr++ = (Byte) ((*(Word *) argPtr) >> 8);   /* store high byte  */
        *bufPtr++ = (Byte) ((*(Word *) argPtr) & 0xff); /* store low byte   */
        va_arg(argPtr, Word*);      /* Point to next word pointer argument  */
    } /* for */

    va_end(argPtr);                 /* Cleanup var args stuff               */

                                    /**** Send Data Packet to Serial Port ***/
    return (writeDataPacket(buffer, nWords + nWords));
} /* qsReplyToCommand() */

/****************************************************************************/
/* Function    : qsWriteDataPacket                                            */
/* Purpose     : Applic. layer function to send a data packet               */
/* Inputs      : Buffer containing packet and packet length                 */
/* Outputs     : Returns ERROR or OK                                        */
/****************************************************************************/
    Int16
qsWriteDataPacket(Byte *packet, Int16 packetLen)
{
    return (transmitDataPacket(packet, packetLen));
} /* qsWriteDataPacket() */















