// $Id: llc.h,v 1.6 2004/07/07 16:14:01 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 Header for software LLC (logical link control) decode


#ifndef _LLC_H
#define _LLC_H


/// Maximum number of pulses per packet
#define OTTO_PACKET_PULSES_MAX  102


/// Data link device kernel interface data word.
struct otto_llc_pulse
{
  unsigned int bus:4;  ///< Bus number
  unsigned int unused:3;  ///< Unused, reserved for future expansion
  unsigned int polarity:1;  ///< Pulse polarity
  unsigned int length:24;  ///< Pulse length in nanoseconds
};

/// \brief Logical link control symbols.
enum otto_llc_symbol
{
  OTTO_LLC_SYM_UNKNOWN = 0,  ///< Unknown symbol
  OTTO_LLC_SYM_ZERO,  ///< Zero data bit
  OTTO_LLC_SYM_ONE,  ///< One data bit
  OTTO_LLC_SYM_SOF,  ///< Start of frame
  OTTO_LLC_SYM_EOD,  ///< End of data
  OTTO_LLC_SYM_EOF,  ///< End of frame
  OTTO_LLC_SYM_BRK,  ///< Break
  OTTO_LLC_SYM_NB0,  ///< Passive normalization bit
  OTTO_LLC_SYM_NB1,  ///< Active normalization bit
  OTTO_LLC_SYM_IFS,  ///< Inter-frame separation
};


/// \brief Timing table entry for LLC decode
struct otto_llc_timing
{
  int min;  ///< Received minimum timing
  int nom;  ///< Transmit nominal timing
  int max;  ///< Received maximum timing
};


/// \brief LLC format definition
///
/// \todo have encode/decode able to work with arbitrary number of pulses (vpw needs 1, pwm needs 2)
/// \todo pass previous pulse to encode symbol, if useful?
struct otto_llc_format
{
  char *name;
  char *desc;
  enum otto_llc_symbol (*decode_symbol) ( struct otto_llc_pulse );
  int (*encode_symbol) (struct otto_llc_pulse *, enum otto_llc_symbol, struct otto_llc_pulse, enum otto_llc_symbol);
};


/// \brief Enable specified busses on software data link device.
/// \return Zero of success, error on failure
int
otto_llc_set_busenable(
  unsigned int busmask  /// Bit fields specifying busses to enable
);

/// \brief Retrieve enabled busses on software data link device.
/// \return Zero on success, error on failure
int
otto_llc_get_busenable(
  unsigned int *busmask  ///< Bit fields specifying enabled busses
);

/// \brief Configure special pulses on software data link device.
/// \return Zero on success, error on failure
int
otto_llc_set_pulse(
  enum otto_llc_symbol id,  ///< Pulse identification (only IFS and BRK supported)
  struct otto_llc_pulse pulse  ///< Pulse
);

/// \brief Get special pulse configuration on software data link device.
/// \return Zero on success, error on failure
int
otto_llc_get_pulse(
  enum otto_llc_symbol id,  ///< Pulse identification (only IFS and BRK supported)
  struct otto_llc_pulse *pulse  ///< Pulse
);

/// \brief Data link decode.
/// \return Zero on success, error on failure.
int
otto_llc_decode(
  union otto_packet *pkt,  ///< Decoded OBD2 packet
  struct otto_llc_pulse pulses[],  ///< Pulses
  int npulses  ///< Number of pulses
);

/// \brief Data link encode.
/// \return Zero on success, error on failure.
int
otto_llc_encode(
  union otto_packet *pkt,  ///< Packet to encode
  struct otto_llc_pulse pulses[],  ///< Encoded pulses
  int *npulses  ///< On input, size of pulses.  On return, number of pulses generated.
);


#endif
