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

#define DFU_VID                  0x1209U
#define DFU_PID                  0xE667U
#define DFU_LANGID               0x0409U
#define DFU_MANUFACTURER_STRING  "ERTOtech"
#define DFU_PRODUCT_STRING       "Refleks Lite DFU"
#define DFU_CONFIG_STRING        "DFU Config"
#define DFU_INTERFACE_STRING     "DFU Interface"

static uint8_t *DFU_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_LangIdDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_ManufacturerDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_ProductDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_SerialDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_ConfigDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static uint8_t *DFU_InterfaceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
static void DFU_UpdateSerial(void);
static void DFU_IntToUnicode(uint32_t value, uint8_t *buffer, uint8_t length);

USBD_DescriptorsTypeDef FS_Desc =
{
  DFU_DeviceDescriptor,
  DFU_LangIdDescriptor,
  DFU_ManufacturerDescriptor,
  DFU_ProductDescriptor,
  DFU_SerialDescriptor,
  DFU_ConfigDescriptor,
  DFU_InterfaceDescriptor,
};

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

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

__ALIGN_BEGIN static uint8_t dfuStringDescriptor[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
__ALIGN_BEGIN static uint8_t dfuSerialDescriptor[USB_SIZ_STRING_SERIAL] __ALIGN_END =
{
  USB_SIZ_STRING_SERIAL, USB_DESC_TYPE_STRING,
};

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

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

static uint8_t *DFU_ManufacturerDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DFU_MANUFACTURER_STRING, dfuStringDescriptor, length);
  return dfuStringDescriptor;
}

static uint8_t *DFU_ProductDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DFU_PRODUCT_STRING, dfuStringDescriptor, length);
  return dfuStringDescriptor;
}

static uint8_t *DFU_SerialDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  UNUSED(speed);
  DFU_UpdateSerial();
  *length = sizeof(dfuSerialDescriptor);
  return dfuSerialDescriptor;
}

static uint8_t *DFU_ConfigDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DFU_CONFIG_STRING, dfuStringDescriptor, length);
  return dfuStringDescriptor;
}

static uint8_t *DFU_InterfaceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  UNUSED(speed);
  USBD_GetString((uint8_t *)DFU_INTERFACE_STRING, dfuStringDescriptor, length);
  return dfuStringDescriptor;
}

static void DFU_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)
  {
    DFU_IntToUnicode(serial0, &dfuSerialDescriptor[2], 8U);
    DFU_IntToUnicode(serial1, &dfuSerialDescriptor[18], 4U);
  }
}

static void DFU_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;
  }
}
