/**************************************************************************//**
* @file
* @brief         Source file for the DDP protocol driver for USB communication
*                with the Atmel USB HID DLL.
*                Compliant with the DDP Protocol v1.1.
* @details       Defines the packet structure and predefined bit masks
*                of DD Protocol v1.1 RawHID USB Data Packets.
******************************************************************************
* @author        M. Eng. CCR (Technikreferent des Durstigen Donnerstags)
* @version       1.0
* @date          Q1 2016
******************************************************************************
* @bug
* @warning
******************************************************************************
* @copyright     <b>CC-Software</b> - http://www.cc-software.de
*                Open Source Software for http://www.durstiger-donnerstag.de
* @section       License
*                This code is open source. Feel free to use it for non-commercial purposes.
*                However consider:
*                - always mention the origin (CC-Software and Durstiger Donnerstag)
*                - if you altered code, additionally mark it as altered by you
*                - the author doesn't guarantee functionality and is not liable for
*                  any accidents or other occurrences due to the usage of this code
*******************************************************************************/

#ifndef DDPROTOCOL_H_
#define DDPROTOCOL_H_

/***************************************************************************//**
* @addtogroup AlcoLyzer Console
* @{
******************************************************************************/


#define DDP_PACKET_SIZE            64                                         /**< Number of Bytes the DD Protocol USB Packet consists of. */
#define DDP_PACKET_HEADER_SIZE    3                                           /**< Number of bytes the header of a DD Protocol USB Packet has. */
#define DDP_PACKET_PAYLOAD_SIZE (DDP_PACKET_SIZE - DDP_PACKET_HEADER_SIZE)    /**< Number of bytes the payload of a DD Protocol USB Packet has. */

#define DDP_PACKET_PREAMBLE                     0xDD        /**< Preamble bit mask for all DD Protocol USB Packets. */
#define DDP_PACKET_NAME_LENGTH                  10          /**< Number of bytes of the user name. */
#define DDP_PACKET_RFIDID_LENGTH                5           /**< Number of bytes of the RFID ID. */
#define DDP_PACKET_BEERLIST_LENGTH              32          /**< Number of bytes the beerlist of an user has. */
#define DDP_PACKET_EVENT_DESCRIPTION_LENGTH     32          /**< Number of bytes of the description of an event. */
#define DDP_PACKET_EVENT_DATE_LENGTH            6           /**< Number of bytes of date and time data set. */


/**************************************************************************//**
* @brief
*    Packet types of DD Protocol USB packets (header).
*****************************************************************************/
typedef enum
{
    DDP_PACKET_TYPE_REQUEST         = 0x51, /**< Requesting event no, firmware, user data by hex_id, clock data, eeprom content (master_mode), event data. Direction: alcolyzer to bieralyzer. */
    DDP_PACKET_TYPE_DATA            = 0x52, /**< Sending data, for instance new user to register, or event no to register including description, or settings. Direction: alcolyzer to bieralyzer. */
    DDP_PACKET_TYPE_RESPONSE        = 0x54, /**< Response to all request packets. Direction: bieralyzer to alcolyzer. */
    DDP_PACKET_TYPE_ACKNOWLEDGE     = 0x55, /**< Acknowledging to data packets. Direction: bieralyzer to alcolyzer. */
    DDP_PACKET_TYPE_NOT_ACKNOWLEDGE = 0x56  /**< Not Acknowledging to data packets. Direction: bieralyzer to alcolyzer. */
}
ddp_packet_type_t;

/**************************************************************************//**
* @brief
*    Packet content types/information of DD Protocol USB packets (header).
*****************************************************************************/
typedef enum
{
    DDP_PACKET_CONTENT_USER     = 0x63,    /**< Packet contains or requests user data. */
    DDP_PACKET_CONTENT_EVENT    = 0x67,    /**< Packet contains or requests event data. */
    DDP_PACKET_CONTENT_SETTINGS = 0x62,    /**< Packet contains or requests clock data, current event number that is active, the current firmware (ignored in data packets) and other BierAlyzer settings (RFU). */
    DDP_PACKET_CONTENT_PING     = 0x70     /**< Packet is a ping packet. */
}
ddp_packet_content_t;

/**************************************************************************//**
* @brief
*    Indices in the payload of packets containing user data.
*****************************************************************************/
enum ddp_packet_payload_index_user_e
{
    DDP_PACKET_PAYLOAD_INDEX_USER_COMMAND,        /**< Command, what is to be done with the user data, for instance unregister or register user. */
    DDP_PACKET_PAYLOAD_INDEX_USER_HEXID,          /**< The hex id of the user. */
    DDP_PACKET_PAYLOAD_INDEX_USER_RFIDID,         /**< The first byte of the rfid id of the user. */
    DDP_PACKET_PAYLOAD_INDEX_USER_NAME = DDP_PACKET_PAYLOAD_INDEX_USER_RFIDID + DDP_PACKET_RFIDID_LENGTH,        /**< The first byte of the name of the user. */
    DDP_PACKET_PAYLOAD_INDEX_USER_BEERLIST = DDP_PACKET_PAYLOAD_INDEX_USER_NAME + DDP_PACKET_NAME_LENGTH,        /**< The first byte of the beer list of the user. */
    DDP_PACKET_PAYLOAD_INDEX_USER_STATUS = DDP_PACKET_PAYLOAD_INDEX_USER_BEERLIST + DDP_PACKET_BEERLIST_LENGTH   /**< The status of the user. */
};

/**************************************************************************//**
* @brief
*    Indices in the payload of packets containing event data.
*****************************************************************************/
enum ddp_packet_payload_index_event_e
{
    DDP_PACKET_PAYLOAD_INDEX_EVENT_COMMAND,       /**< Command, what is to be done with the event data, for instance unregister or register event. */
    DDP_PACKET_PAYLOAD_INDEX_EVENT_ID,            /**< The event id of the event. */
    DDP_PACKET_PAYLOAD_INDEX_EVENT_DESCRIPTION,   /**< The first byte of the description of the event. */
    DDP_PACKET_PAYLOAD_INDEX_EVENT_DATE = DDP_PACKET_PAYLOAD_INDEX_EVENT_DESCRIPTION + DDP_PACKET_EVENT_DESCRIPTION_LENGTH,    /**< The first byte of the date of the event. */
    DDP_PACKET_PAYLOAD_INDEX_EVENT_ACTIVE_DAYS = DDP_PACKET_PAYLOAD_INDEX_EVENT_DATE + DDP_PACKET_EVENT_DATE_LENGTH,           /**< Number of active days of the event. */
    DDP_PACKET_PAYLOAD_INDEX_EVENT_STATUS         /**< Status of the event. */
};


char initDDP(void);
void deinitDDP(void);
void sendPacket(unsigned char *payload, unsigned char type, unsigned char content);
char receivePacket(unsigned char *payload, unsigned char *type, unsigned char *content, int recv_timeout);

/** @} (end group AlcoLyzer Console) */

#endif /* DDPROTOCOL_H_ */