// $Id: iso.h,v 1.3 2004/06/29 20:27:02 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 ISO9141 and ISO14230 packet formats and identifier defintions
/// \todo key byte composition
/// \todo all remaining packet formats


#ifndef _OTTO_ISO_H
#define _OTTO_ISO_H


/// \todo What is the value for this?
#define OTTO_ISO_PACKET_LENGTH_MAX 16


/// \brief Communcation request and response service ids
enum otto_iso_svcid
{
  OTTO_ISO_SVCID_SCR = 0x81,  ///< Start communication request
  OTTO_ISO_SVCID_SCRPR = 0xc1,  ///< Start communication request positive response
  OTTO_ISO_SVCID_SPR = 0x82,  ///< Stop communication request
  OTTO_ISO_SVCID_SPRPR = 0xc2,  ///< Stop communication request positive response
  OTTO_ISO_SVCID_SPRNR = 0x7f,  ///< Stop communication request negative response
};

/// ISO bus communication messages
union otto_iso_packet
{
  struct
  {
    unsigned char data[OTTO_ISO_PACKET_LENGTH_MAX];
    int length;
  }
  raw;  ///< RAW packet data

  /// start comm message
  struct
  {
    unsigned char fmt;  ///< format: 0x81 physical addr, 0xc1 functional
    unsigned char tgt;  ///< target
    unsigned char src;  ///< source
    unsigned char scr;  ///< start comm service id: 0x81
    unsigned char cs;  ///< checksum
  }
  scr;

  /// start comm response
  struct
  {
    unsigned char fmt;  ///< format: 0x80 physical adr, 0xc0 functional
    unsigned char tgt;  ///< target
    unsigned char src;  ///< source
    unsigned char len;  ///< additional length: 0x03
    unsigned char scrpr;  ///< start comm positive response service id: 0xc1 (SCRPR)
    unsigned char kb1;  ///< key byte 1: 0xea (for iso14230)
    unsigned char kb2;  ///< key byte 2: 0x8f (for iso14230)
    unsigned char cs;  ///< checksum
  }
  scrpr;
};


#endif
