/****************************************************************************/
/* Copyright 2000 MBARI                                                     */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Application layer interface to microcontroller communications */
/* Filename : applay.c                                                      */
/* Author   : Andrew Pearce                                                 */
/* Project  : Dorado AUV                                                    */
/* Version  : 2.0                                                           */
/* Created  : 04/06/92                                                      */
/* Modified : 03/03/00                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /data/altex/Master/auv/altex/onboard/mbariTailcone/micro/applay.c,v 1.1 2000/06/23 18:23:56 rob Exp $
 * $Log: applay.c,v $
 * Revision 1.1  2000/06/23 18:23:56  rob
 * Initial check-in of the tailcone source.  I'm sending this snapshot of
 * the tailcone directory to Don Green on 6/23.
 *
 * Revision 1.1.1.1  1997/05/02 17:15:59  pean
 * Initial release of the microcontroller software after Tiburon
 * Moolpool Dive to test IView, Lapboxes, modified Power can
 * GF/5V using bus capacitance mode.
 *
 * Revision 1.1  92/05/14  09:14:25  09:14:25  pean (Andrew Pearce 408-647-3746)
 * 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 "serial.h"                     /* data link layer functions        */
#include "applay.h"                     /* application layer functions      */

#define CARRIAGE_RET    0x0d            /* Carriage Return Character        */

/****************************************************************************/
/* Function    : wordToBuf                                                  */
/* Purpose     : Insert a word into a buffer using Big Endian format        */
/* Inputs      : buffer and word                                            */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
wordToBuf(Byte *buffer, Word data)
{
    *buffer++ = (Byte) (data >> 8);     /* store high byte                  */
    *buffer = (Byte) (data & 0xff);     /* store low byte                   */
} /* wordToBuf() */

/****************************************************************************/
/* Function    : wordFromBuf                                                */
/* Purpose     : Remove a word from a buffer using Big Endian format        */
/* Inputs      : which word [0..4] is desired                               */
/* Outputs     : Word                                                       */
/****************************************************************************/
    Word
wordFromBuf(Byte *buffer, Int16 index)
{
    return ((((Word) (*(buffer + index))) << 8) | (*(buffer + index + 1)));
} /* wordFromBuf() */



/****************************************************************************/
/* Function    : wordsToBuf                                                 */
/* Purpose     : Insert several words into a buffer using Big Endian format */
/* Inputs      : Buffer, number of arguments, list of words                 */
/* Outputs     : Word                                                       */
/****************************************************************************/
    Void
wordsToBuf(Byte *buffer, Int16 nWords, ... )
{
    Int16   i;                      /* Argument counter                     */
    va_list argPtr;                 /* Argument list pointer                */

    va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument*/

    for (i = 0; i < nWords; i++)
    {
        *buffer++ = (Byte) ((*(Word *) argPtr) >> 8);   /* store high byte  */
        *buffer++ = (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               */
} /* wordsToBuf() */

/****************************************************************************/
/* Function    : wordsFromBuf                                               */
/* Purpose     : Remove several words from a buffer using Big Endian format */
/* Inputs      : Buffer, number of arguments, list of pointers to words     */
/* Outputs     : Word                                                       */
/****************************************************************************/
    Void
wordsFromBuf(Byte *buffer, Int16 nWords, ... )
{
    Int16   arg;                    /* Argument counter                     */
    va_list argPtr;                 /* Argument list pointer                */
    Word    *ptr;                   /* Copy of buffer pointer               */

    va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument*/

    for (arg = 0; arg < nWords; arg++)
    {
        ptr = (Word *) (* (Word *)argPtr);
                                    /* Get high and low bytes and build word*/
        *ptr = (( (Word) (*buffer++) << 8) | (*buffer++));
        va_arg(argPtr, Word*);      /* Point to next word pointer argument  */
    } /* for */

    va_end(argPtr);                 /* Cleanup var args stuff               */
} /* wordsFromBuf() */

/****************************************************************************/
/* Function    : itohex                                                     */
/* Purpose     : Convert a word from binary to hex ascii string             */
/* Inputs      : buffer and word to convert                                 */
/* Outputs     : None                                                       */
/****************************************************************************/

Void itohex(Char *buffer, Word num)
{
  Int16 i = 0, j = 0;
  Char buf[4];
  Char *tmpbuf = buffer;
 
  for (i=0;i<4;i++)
  {
    j = num % 16;
    buf[i] = (j > 9) ? (j + 'A' - 10) : (j + '0');
    num /= 16;
  }

 for(i=0;i<4;i++)
    *(tmpbuf++) = (Char) buf[3-i];

}


/*   Void
itohex(Char *buffer, Word num)
{
  Int16 i, j;
  Char buf[16];
  Char *tmpbuf = buffer;
 
  for (i = 0; i < 4; i++)
  {
    j = num % 16;
    buf[i] = (j > 9) ? (j + 'A' - 10) : (j + '0');
    num /= 16;
  }

  i = 4;
  while (--i >= 0) 
    *tmpbuf++ = (char) buf[i];
}  itohex() old version  */

/****************************************************************************/
/* Function    : sendMVCReply                                               */
/* Purpose     : Send a reply message to MVC                                */
/* Inputs      : Number of word args, list of word arguments                */
/* Outputs     : Returns result of sendReply                                */
/****************************************************************************/
Int16
sendMVCReply(Word statusReq, Nat16 nWords, ... )
{
  Nat16   arg;                    /* Argument counter                     */
  va_list argPtr;                 /* Argument list pointer                */
  Byte    buffer[MAX_REPLY_WORDS * sizeof(Word)];
  Byte   *bufPtr = buffer;
  
  if(nWords >= 9 )
    return(ERROR);
  
  *(bufPtr++) = '$';              /* Insert start of message character    */
  itohex(bufPtr, statusReq);      /* Insert status request word           */
  bufPtr += 4;                   /* Bump pointer to next position         */
  
  va_start(argPtr, nWords);       /* argPtr points to 1st unnamed argument*/
  
  for (arg = 0; arg < nWords; arg++)
    {
      itohex(bufPtr, (*(Word *) argPtr));
      va_arg(argPtr, Word*);      /* Point to next word pointer argument  */
      bufPtr += 4;               /* Bump pointer to next position         */
    } /* for */
  
  va_end(argPtr);                 /* Cleanup var args stuff               */
  
  *(bufPtr++) = CARRIAGE_RET;     /* Insert carriage return               */
  
  /**** Send Data Packet to Serial Port ***/
      
  return (transmitBuffer(buffer, (Int16)(bufPtr - buffer))); 
} /* replyToCommand() */


/****************************************************************************/
/* The following empty functions are needed to satisfy symbol references
   in the IBC board library module. For instance, when using X.andy, the
   writeServiceRequest function is used to send alarm message to the serial
   port. The tailcone software will need to replace this function with
   some kind of alarm queue if this functionality is required               */
/****************************************************************************/

/****************************************************************************/
/* Function    : writeWordPacket                                            */
/* Purpose     : Applic. layer function to send a word using protcol layer  */
/* Inputs      : Word to send                                               */
/* Outputs     : Returns ERROR or OK                                        */
/****************************************************************************/
    Int16
writeWordPacket(Word value)
{
} /* writeWordPacket() */

/****************************************************************************/
/* Function    : writeServiceRequest                                        */
/* Purpose     : Applic. layer function to send a service request packet    */
/* Inputs      : Number of words and list of Service Request Words to send  */
/* Outputs     : Returns ERROR or OK                                        */
/****************************************************************************/
    Int16
writeServiceRequest(Int16 nWords, ... )
{
} /* writeServiceRequest() */

/****************************************************************************/
/* Function    : writeSRQPacket                                             */
/* Purpose     : Applic. layer function to send a Service Request packet    */
/* Inputs      : Buffer containing packet and packet length                 */
/* Outputs     : Returns ERROR or OK                                        */
/****************************************************************************/
    Int16
writeSRQPacket(Byte *packet, Int16 packetLen)
{
} /* writeSRQPacket() */


    Int16
writeDataPacket(Byte *packet, Int16 packetLen)
{
}

    Int16
replyToCommand(Int16 nWords, ... )
{
}

/****************************************************************************/
/* Function    : freeRecvdPacket                                            */
/* Purpose     : Frees packet previously read with readRecvdPacket          */
/* Inputs      : packet buffer                                              */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
freeRecvdPacket(Byte *packet)
{
     free(packet);
} /* freeRecvdPacket() */


