// $Id: packet.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 Network packet formats
///
/// Including data from ??? (list specs)


#ifndef _OTTO_PACKET_H
#define _OTTO_PACKET_H


//#include <sae_j2178-1.h>
//#include <iso.h>


/// Maximum packet length
#define OTTO_PACKET_LENGTH_MAX 12


/// Functional data format 2 and 3, secondary address
struct otto_funcadr_sec_adr
{

  /// Secondary ID
  unsigned char id:6;

  /// \brief Control bit
  /// See HS3000, p. 257, table 8
  unsigned char c:1;

  /// \brief Parameter/quantity bit
  /// Used to represent single bit of data
  unsigned char q:1;
};

/// Functional data format 3, extended address
struct otto_funcadr_ext_adr
{
  /// Column number
  enum
  {
    OTTO_EXTADR_COL_ALL = 0,  ///< All columns
    OTTO_EXTADR_COL_LEFTSIDE = 1,  ///< left side
    OTTO_EXTADR_COL_DRIVER = 2,  ///< driver's side
    OTTO_EXTADR_COL_LEFTCENTER = 3,  ///< left center
    OTTO_EXTADR_COL_CENTER = 4,  ///< center
    OTTO_EXTADR_COL_RIGHTCENTER = 5,  ///< right center
    OTTO_EXTADR_COL_PASSENGER = 6,  ///< passenger's side
    OTTO_EXTADR_COL_RIGHTSIDE = 7,  ///< right side
  }
  col:3;

  /// Row number
  enum
  {
    OTTO_EXTADR_ROW_ALL = 0,  ///< All rows
    OTTO_EXTADR_ROW_FRONT = 1,  ///< front surface
    OTTO_EXTADR_ROW_HOOD = 2,  ///< under hood
    OTTO_EXTADR_ROW_BULKHEAD = 3,  ///< aft bulkhead
    OTTO_EXTADR_ROW_APILlAR = 4,  ///< aft "A" pillar
    OTTO_EXTADR_ROW_MID = 5,  ///< mid vehicle
    OTTO_EXTADR_ROW_TRUNK = 6,  ///< trunk
    OTTO_EXTADR_ROW_REAR = 7,  ///< rear surface
  }
  row:3;

  /// Extended address types
  enum
  {
    OTTO_EXTADR_TYPE_GEOGRAPHICAL = 0,  ///< Geographical addressing
    OTTO_EXTADR_TYPE_RESERVED_SAE = 1,  ///< Reserved - SAE
    OTTO_EXTADR_TYPE_RESERVED_MFG_2 = 2,  ///< Reserved - MFG
    OTTO_EXTADR_TYPE_RESERVED_MFG_3 = 3,  ///< Reserved - MFG
  }
  rr:2;
};

/// \brief Functional addressing message data
union otto_funcadr_data
{
  /// \brief Functional packet format 0
  /// No additional data bytes
  struct
  {
  }
  fmt0;

  /// \brief Functional packet format 1
  /// Only parametric data
  struct
  {
    unsigned char data[OTTO_PACKET_LENGTH_MAX-1];  ///< Parametric data
  }
  fmt1;

  /// \brief Functional packet format 2
  /// Secondary address and optional data
  struct
  {
    struct otto_funcadr_sec_adr sec_id;  ///< Secondary address
    unsigned char data[OTTO_PACKET_LENGTH_MAX-2];  ///< Data
  }
  fmt2;

  /// \brief Functional packet format 3
  /// Secondary and extended addresses and optional data
  struct
  {
    struct otto_funcadr_sec_adr sec_id;  ///< Secondary address
    struct otto_funcadr_ext_adr ext_id;  ///< Extended address
    unsigned char data[OTTO_PACKET_LENGTH_MAX-3];  ///< Data
  }
  fmt3;

  /// \brief Functional packet format 4
  /// Test mode for SAE J1979.  mode is similar to secondary
  /// ID with extended address, where Q=0, C=0 for request,
  /// C=1 for response, and extended address is parameter id (PID) or
  /// test id, if used
  struct
  {
    unsigned char mode;  ///< Functional test mode
    unsigned char data[OTTO_PACKET_LENGTH_MAX-2];  ///< Data
  }
  fmt4;
};

/// \brief Physical addressed message data
union otto_phyadr_data
{
  /// \brief Physical packet format 0
  /// No additional data
  struct
  {
  }
  fmt0;

  /// \brief Physical packet format 1
  /// Physical test mode and optional data
  struct
  {
    unsigned char mode;  ///< Physical test mode
    unsigned char data[OTTO_PACKET_LENGTH_MAX-2];  ///< Data
  }
  fmt1;

  /// \brief Physical packet format 2
  /// Physical test mode and optional data
  struct
  {
    unsigned char addr;  ///< Physical target address
    unsigned char mode;  ///< Physical test mode
    unsigned char data[OTTO_PACKET_LENGTH_MAX-3];  ///< Data
  }
  fmt2;
};

/// \brief OBD2 Packet format
union otto_packet
{
  struct
  {
    unsigned char data[OTTO_PACKET_LENGTH_MAX];  ///< Packet data
    int length;  ///< Packet length
    unsigned char nb;  ///< Normalization bit for in frame response (only valid with VPW)
  }
  raw;

  struct
  {
    unsigned char id;  ///< Message ID
    unsigned char data[OTTO_PACKET_LENGTH_MAX-1];  ///< Data field
  }
  single;  ///< Single byte header packet

  union
  {
    struct 
    {
      /// Message ID low 4 bits
      unsigned char id_lo:4;

      /// \brief Header type
      enum
      {
	OTTO_PKTHDRCS_THREEBYTE = 0,  ///< three byte form
	OTTO_PKTHDRCS_ONEBYTE = 1,  ///< one byte form
      }
      h:1;

      /// Message ID high 3 bits
      unsigned char id_hi:3;
    }
    hdr;  ///< Single byte form of consolidated header

    struct
    {
      union otto_funcadr_data data;
    }
    func;

    struct
    {
      union otto_phyadr_data data;
    }
    phy;
  }
  consol1;  ///< Single byte form of consolidated header message

  union
  {
    struct
    {
      /// \brief Message type
      /// see HS3000, p. 254, table 6
      unsigned char z:2;

      /// \brief Addressing mode
      enum
      {
	OTTO_PKTHDR_FUNCTIONAL = 0,  ///< Functional addressing
	OTTO_PKTHDR_PHYSICAL = 1,  ///< 1 Physical addressing
      }
      y:1;

      /// \brief In-frame response (IFR)
      enum
      {
	OTTO_PKTHDR_IFRREQUIRED = 0,  ///< IFR required
	OTTO_PKTHDR_IFRDISALLOWED = 1,  ///< IFR not allowed
      }
      k:1;

      /// \brief Header type
      enum
      {
	OTTO_PKTHDR_THREEBYTE = 0,  ///< three byte form
	OTTO_PKTHDR_ONEBYTE = 1,  ///< one byte form
      }
      h:1;

      /// \brief Priority
      /// Zero is highest priority, 7 is lowest
      unsigned char p:3;
    }
    first;  ///< First byte

    struct 
    {
      struct 
      {
	/// \brief Functional target address type
	enum
        {
	  OTTO_FUNCADR_TGTADR_COMMAND = 0,  ///< Command
	  OTTO_FUNCADR_TGTADR_STATUS = 1,  ///< 1 Status
	}
	w:1;

	/// Primary ID
	unsigned char id:7;
      }
      target;  ///< Target address
      unsigned char source;  ///< Source address

      union otto_funcadr_data data;  ///< Data formats
    }
    func;  ///< Functional addressing

    struct
    {
      unsigned char target;  ///< Target address
      unsigned char source;  ///< Source address
      
      union otto_phyadr_data data;  ///< Data
    }
    phy;  ///< Physical addressing
  }
  consol3;  ///< Three byte form of consolidated header message
};


/// \brief Calculate CRC for a packet
///
/// \return Calculated checksum
int
otto_crc_calc(
  union otto_packet *pkt  ///< Packet to compute CRC for
);


/// \brief Validate CRC for a packet
///
/// \return One if CRC is valid, zero if invalid, negative on error
int
otto_crc_valid(
  union otto_packet *pkt  ///< Packet to validate CRC
);


/// \brief Find a valid packet within a packet buffer
///
/// \return One if packet found, zero if not found, negative on error.
int
otto_findpacket(
  union otto_packet *pkt,  ///< Partial packet buffer to search
  int *start,  ///< [out] Start of first packet found
  int *len  ///< [out] Length of first packet found
);


#endif
