/*********************************  parser.h  *******************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/include/parser.h,v $
 *  Copyright (C) 2003 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: parser.h,v 1.8 2006/03/25 00:41:04 brent Exp $
 *
 * Serial message parsing and processing
 *    in a "sort of" object oriented fashion
 *
 *****************************************************************************/

#ifndef parser_h
#define parser_h

#include "fifos.h"

//define how to kickstart all the transmitters and poll all the receivers
#include "kickstart.h"    
//comment out following if xmitter or receiver must be probed individually
#define parserKickstartAll()  kickstart()
#define parserPollAll() pollInput() 

typedef const struct parserInstance parserInstance;

struct parserInstance {
  fifo *input, *output;     //serial data from/to host
  voidFn *pollInput;        //poll input with interrupts disabled
  voidFn *kickstartOutput;  //kickstart the transmitter
  void (*resync) (byte USARTerrMask);  //resychronize with host & restart
};

// Collection of Serial FIFO utilities
// So an external command processor can get bytes from and put bytes to a host

#ifdef parserKickstartAll

#define parserKickstart(ignoredInstance)  parserKickstartAll()

#else

void inline parserKickstart (parserInstance *instance)
{
  instance->kickstartOutput();
}

#endif

// If command processor needs to poll for input with interrupts disabled
#ifdef parserPollAll

#define parserPoll(ignoredInstance)  parserPollAll()

#else

void inline parserPoll (parserInstance *instance)
{
  instance->pollInput();
}

#endif


void parserAwaitInput (fifo *from, unsigned length);
/*
  block until fifo contains at least length bytes
*/


unsigned parserGetByte (parserInstance *instance);
/*
  return the next byte from the host
  block while no bytes available
*/

unsigned parserGetByteAhead (parserInstance *instance, byte **head);
/*
  like getByte above, but may advance a head pointer outside the fifo body
  so that the input may be rescanned or discarded later.
*/

uint16 parserGetWord (parserInstance *instance);
/*
  return the next word from the host
  block while no words available
*/


void parserPutByte (parserInstance *instance, unsigned b);
/*
  write b to the host
  block while buffer full
*/

void parserPutWord (parserInstance *instance, uint16 word);
/*
  write b to the host
  block while buffer full
*/

void parserPutBlock (parserInstance *instance, byte *src, unsigned len);
/*
  output len byte memory block to serial port
*/

void parserPutString (parserInstance *instance, char *s);
/*
  write NUL terminated string s to host
*/

void parserGetBlock (parserInstance *instance, byte **dst, unsigned len);
/*
  input len bytes to *dst
*/

void parserSkipInput (parserInstance *instance, unsigned len);
/*
  skip past next len bytes of input
*/

//input an unsigned value in hexidecimal from the specified port
//terminated by carridge return
// returns < 0 if input was invalid
int parserGetHex (parserInstance *port, unsigned limit);

#endif
