
/*
 * serial.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: serial.c,v $
 * Revision 1.13  2001/01/30 06:16:31  root
 * #
 *
 * Revision 1.12  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.11  2000/11/23 23:45:28  root
 * #
 *
 * Revision 1.10  2000/10/07 18:30:10  root
 * #
 *
 * Revision 1.9  2000/10/06 19:52:45  root
 * #
 *
 * Revision 1.8  2000/02/15 04:15:06  root
 * #
 *
 * Revision 1.7  2000/02/15 04:14:52  root
 * #
 *
 * Revision 1.6  2000/02/14 22:17:05  root
 * #
 *
 * Revision 1.5  2000/02/14 03:45:03  root
 * #
 *
 * Revision 1.4  2000/02/13 21:22:24  root
 * #
 *
 * Revision 1.3  2000/02/13 21:13:14  root
 * #
 *
 * Revision 1.2  2000/02/13 19:15:34  root
 * #
 *
 * Revision 1.1  2000/02/13 18:57:48  root
 * #
 *
 *
 */

static char rcsid[] = "$Id: serial.c,v 1.13 2001/01/30 06:16:31 root Exp $";

#include "project.h"
#include <SerialMgr.h>


#define SerialLibraryName "Serial Library"
static UInt SerialLibrary;


#define SERIAL_BUF_SIZE 1024
Byte SerialBuffer[SERIAL_BUF_SIZE];

#define GET_BUF_SIZE 400
Byte GetBuf[GET_BUF_SIZE];


#define DEFAULT_PORT 0

int
serial_init ()
{
  Err e;
  SerSettingsType ss;

#ifdef NOSER
  return;
#endif

  if (SysLibFind (SerialLibraryName, &SerialLibrary))
    {
      CRASH_AND_BURN ("Could find serial library");
    }


#ifdef EMBED
  SysSetAutoOffTime (0);
#endif

  e = SerOpen (SerialLibrary, DEFAULT_PORT, 9600);
  switch (e)
    {
    case serErrAlreadyOpen:
      FrmAlert (SerialInUseAlert);
      SerClose (SerialLibrary);
      return 0;
    case serErrBadParam:
    case memErrNotEnoughSpace:
    default:
      CRASH_AND_BURN ("Some or other error");
    case 0:
      break;
    }

#ifdef EMBED
  e = SerControl (SerialLibrary, serCtlIrDADisable, 0, 0);
#else
  e = SerControl (SerialLibrary, serCtlIrDAEnable, 0, 0);
#endif
  switch (e)
    {
    case 0:
      break;
    default:
      CRASH_AND_BURN ("another error");
    }


  if (SerSetReceiveBuffer (SerialLibrary, SerialBuffer,
                           (int) sizeof (SerialBuffer)))
    {
      CRASH_AND_BURN ("some error");
    }

  if (SerGetSettings (SerialLibrary, &ss))
    {
      CRASH_AND_BURN ("surreal error");
    }

  ss.baudRate = 9600;
  ss.flags = serSettingsFlagStopBits1 | serSettingsFlagBitsPerChar8;

  if (SerSetSettings (SerialLibrary, &ss))
    {
      CRASH_AND_BURN ("really surreal error");
    }

#ifdef EMBED
  e = SerControl (SerialLibrary, serCtlIrDADisable, 0, 0);
#else
  e = SerControl (SerialLibrary, serCtlIrDAEnable, 0, 0);
#endif
  switch (e)
    {
    case 0:
      break;
    default:
      CRASH_AND_BURN ("another error");
    }


  return 1;
}


int
serial_send (void *buf, int len)
{
  Err e;
#ifdef NOSER
  return 0;
#endif
  return SerSend (SerialLibrary, buf, len, &e);
}

void
serial_dispatch (void)
{
  int i;
  Err e;
  ULong n;
  ULong red;

#ifdef NOSER
  return;
#endif

  do
    {

      e = SerReceiveCheck (SerialLibrary, &n);
      switch (e)
        {
        case serErrLineErr:
          SerClearErr (SerialLibrary);
          moan ("Serial error\n");
          break;
        case 0:
          break;
        default:
          return;
        }

    }
  while (e != 0);


  while (n)
    {
      red = n > GET_BUF_SIZE ? GET_BUF_SIZE : n;
      red = SerReceive (SerialLibrary, GetBuf, red, 0, &e);
      if (red > 0)
        {
          irda_parse (GetBuf, (int) red);
          n -= red;
        }
      else
        {
          return;
        }
    }

}

void
serial_done ()
{
#ifdef NOSER
  return;
#endif
  SerSetReceiveBuffer (SerialLibrary, NULL, 0);
  SerClose (SerialLibrary);
}

void
serial_flush ()
{
#ifdef NOSER
  return;
#endif
  SerSendWait (SerialLibrary, -1);
}
