//
// Copyright (c) 1999-2004 Darius Rad
//
// This file is part of the OpenOtto project.
//
// OpenOtto is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//

/// \file
/// \brief User space interface for software data link devices on a serial port.
/// \todo


#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>

#include <otto/otto.h>
#include <otto/packet.h>
#include <otto/device.h>
#include <otto/control.h>

#include "llc.h"


static const char _ID_[] __attribute__ ((unused)) =
"$Id: llc_serial.c,v 1.5 2004/07/07 22:07:11 alpha Exp $";


/// \brief Private driver data
struct llc_serial_priv
{
  int fd;  ///< File descriptor to kernel driver
  char *file;  ///< Device file
  int rate;  ///< Device data rate
  int flags;  ///< Device flags
  struct otto_llc_format *llc;  ///< Pointer to LLC format
  struct otto_llc_pulse pulses[OTTO_PACKET_PULSES_MAX];  ///< Partial bytes
  int pulses_len;  ///< Partial byte length
  union otto_packet partial;  ///< Partial packet
};


static struct otto_device *
llc_serial_open(
  const char *fname
)
{
  assert( fname != NULL );

  struct otto_device *dev;
  struct llc_serial_priv *priv;


  dev = malloc(sizeof(struct llc_serial_priv));
  if( dev == NULL )
  {
    perror("malloc");
    return(NULL);
  }

  priv = malloc(sizeof(struct llc_serial_priv));
  if( priv == NULL )
  {
    perror("malloc");
    return(NULL);
  }

  dev->priv = priv;

  memset( priv, 0, sizeof(struct llc_serial_priv) );

  /// \todo open file descriptor, setup kernel device

  return(0);
}


static int
llc_serial_close(
  struct otto_device *dev
)
{
  struct llc_serial_priv *priv = dev->priv;
  int ret = 0;

  // close device
  if( close( priv->fd ) != 0 )
  {
    ret = -1;
  }

  free(dev->priv);
  free(dev);

  return(ret);
}


static int
llc_serial_write(
  struct otto_device *dev,
  int bus,
  union otto_packet *pkt
)
{
  assert( dev != NULL );
  assert( pkt != NULL );

  assert( pkt->raw.length > 0 );
  assert( pkt->raw.length <= OTTO_PACKET_LENGTH_MAX );

  //struct llc_serial_priv *priv = dev->priv;

  // convert packet to pulses
  // write pulses to device

  return(-1);
}


static int
llc_serial_read(
  struct otto_device *dev,
  int bus,
  union otto_packet *pkt
)
{
  assert( dev != NULL );
  assert( pkt != NULL );

  // select(dev->fd) to see if data available
  // read pending data
  // attempt to convert as much as possible of buffer
  // if have full packet, return it
  // otherwise, return "not yet"

  return(-1);
}


static int
llc_serial_control(
  struct otto_device *dev,
  int bus,
  void *data,
  int cmd
)
{
  assert( dev != NULL );
  assert( bus >= 0 );
  assert( bus <= dev->nbus );

  union otto_control_data *idat = data;
  int ret = 0;

  switch(cmd)
  {
    case OTTO_IOCBUSINIT:
      // no init process necessary
      ret = 0;
      break;

    case OTTO_IOCSDATARATE:
      // \todo validate rate, set
      break;

    case OTTO_IOCGDATARATE:
      if( bus == 0 )
      {
	idat->rate = dev->rate;
      }
      else
      {
	idat->rate = dev->busses[bus-1].rate;
      }
      ret = 0;
      break;

    case OTTO_IOCGSELECTFD:
      idat->fd = ((struct llc_serial_priv *)(dev->priv))->fd;
      ret = 0;
      break;

    case OTTO_IOCSBUSADR:
    case OTTO_IOCGBUSADR:
    default:
      ret = -1;
  }

  return(ret);
}


static struct otto_driver llc_serial_drv =
{
  .name = "llc_serial",
  .desc = "Device driver for SAE VPW and PWM buses over a serial port",
  .author = "Darius Rad <alpha@area49.net>",
  .license = "GPL",
  .open = llc_serial_open,
  .close = llc_serial_close,
  .write = llc_serial_write,
  .read = llc_serial_read,
  .control = llc_serial_control,
};


static void __attribute((constructor))
uart_serial_constructor(void)
{
  otto_register(&llc_serial_drv);
}
