// $Id: device.h,v 1.1 2004/07/07 22:07:11 alpha Exp $
//
// 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 OBD2 data link implementation.


#ifndef _OTTO_DEV_H
#define _OTTO_DEV_H


/// OBD2 data link type
enum otto_link_type
{
  OTTO_LINK_TYPE_UNKNOWN = 0,  ///< Unknown data link protocol
  OTTO_LINK_TYPE_ISO,  ///< ISO 9141
  OTTO_LINK_TYPE_VPW,  ///< Variable pulse width modulation (SAE J1850)
  OTTO_LINK_TYPE_PWM,  ///< Pulse width modulation (SAE J1850)
};

/// \brief OBD2 data link device
struct otto_device
{
  char *file;  ///< Device file, if applicable
  int rate;  ///< Device data rate (don't confuse with bus bit rate)
  int flags;  ///< Device flags (to be described)
  void *priv;  ///< OBD2 device parameters (device specific)

  int nbus;  ///< Number of busses on this device
  struct otto_bus *busses;  ///< Pointer to list of busses on this device

  struct otto_driver *drv;  ///< Pointer to device driver interface
  struct otto_device *next;  ///< Next pointer for device list
};

/// \brief Otto data link bus interface
struct otto_bus
{
  enum otto_link_type type;  ///< Data link protocol
  int rate;  ///< Bus bit rate
  int flags;  ///< Bus flags (to be determined)
  void *priv;  ///< Private interface data (necessary? should busses be standardized?)
};


/// \brief OBD2 physical interface device driver
/// \todo flush()?
/// \todo up()/down()?
struct otto_driver
{
  char *name;  ///< Driver name
  char *desc;  ///< Driver description
  char *author;  ///< Driver author
  char *license;  ///< Driver license

  struct otto_device * (*open) (const char *);
  int (*close) (struct otto_device *);
  int (*write) (struct otto_device *, int, union otto_packet *);
  int (*read) (struct otto_device *, int, union otto_packet *);
  int (*control) (struct otto_device *, int, void *, int );

  struct otto_device *dev_first;  ///< Pointer to list of devices open on this driver
  struct otto_driver *next;  ///< Next pointer for device list
};


/// \brief Register a device driver
void
otto_register(
  struct otto_driver *drv  ///< Device driver operations
);

/// \brief Open and initialize OBD2 device
/// \return Otto device handle
struct otto_device *
otto_open(
  const char *name,  ///< Name of driver to use
  const char *fname  ///< Name of device file to use
);

/// \brief Close ODB2 device
int
otto_close(
  struct otto_device *dev  ///< OBD2 device
);

/// \brief Transmit a packet on an OBD2 device
int
otto_write(
  struct otto_device *dev,  ///< Otto device handle
  int bus,  ////< Device bus interface
  union otto_packet *packet,  ///< Packet to send
  size_t size  ///< Size of packet structure
);

/// \brief Receive a packet on an OBD2 device
int
otto_read(
  struct otto_device *dev,  ///< Otto device handle
  int bus,  ///< OBD2 bus interface
  union otto_packet *packet,  ///< Received packet
  size_t size  ///< Size of the packet parameter
);

/// \brief Control for device
/// \return Depends on function
int
otto_control(
  struct otto_device *dev,  ///< Otto device handle
  int bus,  ///< Device bus number
  void *data,  ///< Pointer to optional parameters, depending on function
  int cmd  ///< Function identifier
);

/// \brief Return a file descriptor for select() or poll()
/// \return file descriptor
int
otto_getselectfd(
  struct otto_device *dev
);


#endif
