#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_conf.h"

#define DINPUT_VID                    0x1209U
#define DINPUT_PID                    0xE669U
#define DINPUT_LANGID                 0x0409U
#define DINPUT_MANUFACTURER_STRING    "ERTOtech"
#define DINPUT_PRODUCT_STRING         "Refleks Lite DInput"
#define DINPUT_CONFIGURATION_STRING   "HID Config"
#define DINPUT_INTERFACE_STRING       "Gamepad"

static uint8_t *DInput_DeviceDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length);
static uint8_t *DInput_LangIdDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length);
static uint8_t *DInput_ManufacturerDescriptor(USBD_SpeedTypeDef speed,
                                              uint16_t *length);
static uint8_t *DInput_ProductDescriptor(USBD_SpeedTypeDef speed,
                                         uint16_t *length);
static uint8_t *DInput_SerialDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length);
static uint8_t *DInput_ConfigurationDescriptor(USBD_SpeedTypeDef speed,
                                               uint16_t *length);
static uint8_t *DInput_InterfaceDescriptor(USBD_SpeedTypeDef speed,
                                           uint16_t *length);
static void DInput_UpdateSerial(void);
static void DInput_IntToUnicode(uint32_t value, uint8_t *buffer,
                                uint8_t length);

USBD_DescriptorsTypeDef FS_Desc =
{
  DInput_DeviceDescriptor,
  DInput_LangIdDescriptor,
  DInput_ManufacturerDescriptor,
  DInput_ProductDescriptor,
  DInput_SerialDescriptor,
  DInput_ConfigurationDescriptor,
  DInput_InterfaceDescriptor,
};

__ALIGN_BEGIN static uint8_t dinputDeviceDescriptor[USB_LEN_DEV_DESC] __ALIGN_END =
{
  USB_LEN_DEV_DESC, USB_DESC_TYPE_DEVICE,
  0x00, 0x02,
  0x00, 0x00, 0x00,
  USB_MAX_EP0_SIZE,
  LOBYTE(DINPUT_VID), HIBYTE(DINPUT_VID),
  LOBYTE(DINPUT_PID), HIBYTE(DINPUT_PID),
  0x00, 0x01,
  USBD_IDX_MFC_STR,
  USBD_IDX_PRODUCT_STR,
  USBD_IDX_SERIAL_STR,
  0x01,
};

__ALIGN_BEGIN static uint8_t dinputLangIdDescriptor[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
{
  USB_LEN_LANGID_STR_DESC, USB_DESC_TYPE_STRING,
  LOBYTE(DINPUT_LANGID), HIBYTE(DINPUT_LANGID),
};

__ALIGN_BEGIN static uint8_t dinputStringDescriptor[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
__ALIGN_BEGIN static uint8_t dinputSerialDescriptor[USB_SIZ_STRING_SERIAL] __ALIGN_END =
{
  USB_SIZ_STRING_SERIAL, USB_DESC_TYPE_STRING,
};

static uint8_t *DInput_DeviceDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length)
{
  UNUSED(speed);
  *length = sizeof(dinputDeviceDescriptor);
  return dinputDeviceDescriptor;
}

static uint8_t *DInput_LangIdDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length)
{
  UNUSED(speed);
  *length = sizeof(dinputLangIdDescriptor);
  return dinputLangIdDescriptor;
}

static uint8_t *DInput_ManufacturerDescriptor(USBD_SpeedTypeDef speed,
                                              uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DINPUT_MANUFACTURER_STRING,
                 dinputStringDescriptor, length);
  return dinputStringDescriptor;
}

static uint8_t *DInput_ProductDescriptor(USBD_SpeedTypeDef speed,
                                         uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DINPUT_PRODUCT_STRING,
                 dinputStringDescriptor, length);
  return dinputStringDescriptor;
}

static uint8_t *DInput_SerialDescriptor(USBD_SpeedTypeDef speed,
                                        uint16_t *length)
{
  UNUSED(speed);
  DInput_UpdateSerial();
  *length = sizeof(dinputSerialDescriptor);
  return dinputSerialDescriptor;
}

static uint8_t *DInput_ConfigurationDescriptor(USBD_SpeedTypeDef speed,
                                               uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DINPUT_CONFIGURATION_STRING,
                 dinputStringDescriptor, length);
  return dinputStringDescriptor;
}

static uint8_t *DInput_InterfaceDescriptor(USBD_SpeedTypeDef speed,
                                           uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DINPUT_INTERFACE_STRING,
                 dinputStringDescriptor, length);
  return dinputStringDescriptor;
}

static void DInput_UpdateSerial(void)
{
  uint32_t serial0 = *(uint32_t *)DEVICE_ID1;
  uint32_t serial1 = *(uint32_t *)DEVICE_ID2;
  uint32_t serial2 = *(uint32_t *)DEVICE_ID3;
  serial0 += serial2;
  if (serial0 != 0U)
  {
    DInput_IntToUnicode(serial0, &dinputSerialDescriptor[2], 8U);
    DInput_IntToUnicode(serial1, &dinputSerialDescriptor[18], 4U);
  }
}

static void DInput_IntToUnicode(uint32_t value, uint8_t *buffer,
                                uint8_t length)
{
  uint8_t index;
  for (index = 0U; index < length; index++)
  {
    uint8_t digit = (uint8_t)(value >> 28);
    buffer[2U * index] = (digit < 10U) ?
                         (uint8_t)(digit + '0') :
                         (uint8_t)(digit - 10U + 'A');
    buffer[(2U * index) + 1U] = 0U;
    value <<= 4;
  }
}
