// $Id: data.h,v 1.5 2004/07/05 23:25:39 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 Data parameter definitions
///
/// Parameter reference number (PRN), scaling, limit, offset, transfer (SLOT) structures, and diagnostic trouble code (DTC) format


#ifndef _OTTO_DATA_H
#define _OTTO_DATA_H


/// PRN structure
union otto_prn
{
  int raw;  ///< Raw PRN

  struct
  {
    /// Sequence number
    unsigned char seq;

    /// \brief Owner
    enum
    {
      OTTO_PRN_OWNER_SAE = 0,  ///< SAE
      OTTO_PRN_OWNER_MFG1 = 1,  ///< MFG
      OTTO_PRN_OWNER_MFG2 = 2,  ///< MFG
      OTTO_PRN_OWNER_MFG3 = 3,  ///< MFG
      OTTO_PRN_OWNER_MFG4 = 4,  ///< MFG
      OTTO_PRN_OWNER_MFG5 = 5,  ///< MFG
      OTTO_PRN_OWNER_MFG6 = 6,  ///< MFG
      OTTO_PRN_OWNER_MFG7 = 7,  ///< MFG
    }
    owner:3;

    /// \brief Subgroup
    /// See HS3000, p 271, table 1
    unsigned char subgroup:2;

    /// \brief System
    enum
    {
      OTTO_PRN_SYSTEM_POWERTRAIN = 0,  ///< Powertrain
      OTTO_PRN_SYSTEM_CHASSIS = 1,  ///< Chassis
      OTTO_PRN_SYSTEM_BODY1 = 2,  ///< Body 1
      OTTO_PRN_SYSTEM_BODY2 = 3,  ///< Body 2
      OTTO_PRN_SYSTEM_BODY3 = 4, ///< Body 3
      OTTO_PRN_SYSTEM_BODY4 = 5,  ///< Body 4
      OTTO_PRN_SYSTEM_OTHER1 = 6,  ///< Other 1
      OTTO_PRN_SYSTEM_OTHER2 = 7,  ///< Other 2
    }
    system:3;
  }
  prn;

};


/// SLOT definition
struct otto_slot
{
  /// SLOT formats
  enum
  {
    OTTO_SLOT_PKT = 0,  ///< Multiple parameter packeted
    OTTO_SLOT_BMM,  ///< Bit mapped with mask
    OTTO_SLOT_BMP,  ///< Bit mapped without mask
    OTTO_SLOT_UNM,  ///< Unsigned numeric
    OTTO_SLOT_SNM,  ///< 2's complement signed numeric
    OTTO_SLOT_SED,  ///< State encoded
    OTTO_SLOT_ASC,  ///< ASCII encoded
    OTTO_SLOT_BCD,  ///< Binary coded decimal (BCD)
    OTTO_SLOT_SFP,  ///< Signed floating point (scientific notation)
  }
  format;  ///< SLOT format

  int bits;  ///< Number of bits in parameter
  int seq;  ///< Sequence number
};


/// PRN table entry
struct otto_prn_table_entry
{
  union otto_prn prn;
  char *name;
  char *units;
  struct otto_slot slot;
};


/// PKT SLOT type
struct otto_slot_pkt_table_entry
{
  struct otto_slot slot;  ///< SLOT definition
  char *name;  ///< SLOT name
  //  struct otto_prn prn[]; ///< List of PRNs
  int prn[7];  ///< List of PRNs, terminated by -1
};


/// BMP SLOT type
struct otto_slot_bmp_table_entry
{
  struct otto_slot slot;  ///< SLOT definition
  char *name;

  struct
  {
    char *name;  ///< Bit name
    char *unset;  ///< Unset bit meaning
    char *set;  ///< Set bit meaning
  }
  bit[];  ///< Bit definitions
};


/// \brief BMM SLOT type
///
/// No BMM SLOTs are defined
struct otto_slot_bmm_table_entry
{
};


/// UNM SLOT type
struct otto_slot_unm_table_entry
{
  struct otto_slot slot;  ///< SLOT definition
  float scale;  ///< Scaling, value of one bit
  float min;  ///< Minimum value
  float max;  ///< Maximum value
  float offset;  ///< Offset
};


/// SNM SLOT type
struct otto_slot_snm_table_entry
{
  struct otto_slot slot;  ///< SLOT definition
  float scale;  ///< Scaling, value of one bit
  float min;  ///< Minimum value
  float max;  ///< Maximum value
  float offset;  ///< Offset
};


/// SED SLOT type
struct otto_slot_sed_table_entry
{
  struct otto_slot slot;  ///< SLOT definition
  char *name;  ///< State group name

  struct
  {
    int value;  ///< State value
    char *name;  ///< State name
  }
  state[];  ///< States, terminated by { -1, "" } state
};


// ASC is nothing special
// BCD is also nothing special


// SFP


/// \brief Decoded parameter
/// \todo decoded slot value
struct otto_parameter_value
{
  struct otto_slot slot;
  union
  {
    int i;
    float f;
  }
  value;

  char units[1024];
};


/// Encoded DTC
union otto_dtc
{
  int raw;

  struct
  {
    enum
    {
      OTTO_DTC_POWERTRAIN = 0,
      OTTO_DTC_CHASSIS = 1,
      OTTO_DTC_BODY = 2,
      OTTO_DTC_NETWORK = 3,
    }
    des:2;  ///< DTC designator

    unsigned char code1000:2;  ///< DTC code, 1000s place
    unsigned char code100:4;  ///< DTC code, 100s place
    unsigned char code10:4;  ///< DTC code, 10s place
    unsigned char code1:4;  ///< DTC code, 1s place
  }
  dtc;
};


/// Apply SLOT transformation to OBD2 data and return general format data.
int
otto_decode_by_slot(
  struct otto_slot *slot,  ///< SLOT describing format of data
  int in,  ///< Input data (OBD2 format)
  int *out  ///< Output data (general format)
);

/// Apply SLOT transformation to general format data and return OBD2 data.
int
otto_encode_by_slot(
  struct otto_slot *slot,  ///< SLOT describing format of data
  int in,  ///< Input data (general format)
  int *out  ///< Output data (OBD2 format) 
);

/// \todo encode/decode numeric slot
/// \todo decode state encoded slot state

/// \brief Returns the string representation of a DTC
/// \return Zero on success, error on failure
int
otto_dtc_tostring(
  union otto_dtc *dtc,  ///< DTC to format
  char *str,  ///< Buffer to store DTC formatted as a string, must be at least 6 characters
  size_t size  ///< Size of str, must be >= 6
);

/// \brief Returns a diagnostic message from looking up the specified DTC in the database.
/// \return Zero on succes, error on failure
int
otto_dtclookup(
  union otto_dtc *dtc,  ///< DTC to lookup
  char *desc,  ///< Buffer to store DTC description
  size_t size  ///< Size of desc
);


#endif
