//-----------------------------------------------------------------------------
///
/// \brief  USB Descriptor
///
///         Nothing implemented yet
///
/// \date   2018025
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes------------------------------------------------------------------


//---Own------------------------------
#include <biwak/usb_descriptor.h>
#include <biwak/usb_defines.h>

//---Global---------------------------

#include <inttypes.h>
#include <string.h>


//---Defines-------------------------------------------------------------------


//#define USBD_VID                      0x0483
//#define USBD_PID                      0x5710
//#define USBD_LANGID_STRING            0x409
#define USBD_LANGID_STRING            1033

//#define USBD_VID                      0x0000
//#define USBD_PID                      0x0000

//#define USBD_MANUFACTURER_STRING      "Seesslen.net"
//#define USBD_PRODUCT_FS_STRING        "HID Terminal"

//#define USBD_MANUFACTURER_STRING      "STMicroelectronics"
//#define USBD_PRODUCT_FS_STRING        "HID Joystick in FS Mode"

#define USBD_CONFIGURATION_FS_STRING  "HID Config"
#define USBD_INTERFACE_FS_STRING      "HID Interface"

#define USBD_VID                      0x046a
#define USBD_PID                      0x0012    // Original Keyboard: 0x0011
#define USBD_MANUFACTURER_STRING      "Minutnik"
#define USBD_PRODUCT_FS_STRING        "G83 (RS 6000) Keyboard"

#define         DEVICE_ID1          (0x1FFFF7AC)
#define         DEVICE_ID2          (0x1FFFF7B0)
#define         DEVICE_ID3          (0x1FFFF7B4)


//---Implementation------------------------------------------------------------

#if 1
/* USB Standard Device Descriptor */
static const uint8_t deviceDescriptor[USB_LEN_DEV_DESC]= {
  0x12,                       /* bLength */
  USB_DESC_TYPE_DEVICE,       /* bDescriptorType */
  0x00,                       /* bcdUSB bcd, 2.0*/
  0x02,
  0x02,                       /* bDeviceClass CDC*/
  0x00,                       /* bDeviceSubClass */
  0x00,                       /* bDeviceProtocol */
  USB_MAX_EP0_SIZE,           /* bMaxPacketSize */
  LOBYTE(USBD_VID),           /* idVendor */
  HIBYTE(USBD_VID),           /* idVendor */
  LOBYTE(USBD_PID),           /* idVendor */
  HIBYTE(USBD_PID),           /* idVendor */
  0x00,                       /* bcdDevice rel. 2.00 */
  0x02,
  USBD_IDX_MFC_STR,           /* Index of manufacturer string */
  USBD_IDX_PRODUCT_STR,       /* Index of product string */
  USBD_IDX_SERIAL_STR,        /* Index of serial number string */
  USBD_MAX_NUM_CONFIGURATION  /* bNumConfigurations */
}; /* USB_DeviceDescriptor */


/* USB Standard Device Descriptor */
static const uint8_t langIDStringDescriptor[USB_LEN_LANGID_STR_DESC]=
{
  USB_LEN_LANGID_STR_DESC,
  USB_DESC_TYPE_STRING,
  LOBYTE(USBD_LANGID_STRING),
  HIBYTE(USBD_LANGID_STRING),
};

uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] =
{
  USB_SIZ_STRING_SERIAL,
  USB_DESC_TYPE_STRING,
};

uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ];

CUsbDescriptor::CUsbDescriptor( )
{
}


uint8_t *CUsbDescriptor::getDeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
   *length=sizeof(deviceDescriptor);
   return( (uint8_t *)deviceDescriptor );
}

uint8_t *CUsbDescriptor::getLangIDStringDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
   *length=sizeof(langIDStringDescriptor);
   return( (uint8_t *)langIDStringDescriptor );
}

uint8_t *CUsbDescriptor::getProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
   getString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
   return USBD_StrDesc;
}


uint8_t *CUsbDescriptor::getManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  getString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  return USBD_StrDesc;
}

uint8_t *CUsbDescriptor::getSerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
  *length = USB_SIZ_STRING_SERIAL;

  /* Update the serial number string descriptor with the data from the unique ID*/
  getSerialNum();

  return USBD_StringSerial;
}


void CUsbDescriptor::getString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
{
  uint8_t idx = 0;

  if (desc != NULL)
  {
    *len =  strlen((char *)desc) * 2 + 2;
    unicode[idx++] = *len;
    unicode[idx++] =  USB_DESC_TYPE_STRING;

    while (*desc != '\0')
    {
      unicode[idx++] = *desc++;
      unicode[idx++] =  0x00;
    }
  }
}


void CUsbDescriptor::getSerialNum(void)
{
  uint32_t deviceserial0, deviceserial1, deviceserial2;

  deviceserial0 = *(uint32_t*)DEVICE_ID1;
  deviceserial1 = *(uint32_t*)DEVICE_ID2;
  deviceserial2 = *(uint32_t*)DEVICE_ID3;

  deviceserial0 += deviceserial2;

  if (deviceserial0 != 0)
  {
    intToUnicode (deviceserial0, &USBD_StringSerial[2] ,8);
    intToUnicode (deviceserial1, &USBD_StringSerial[18] ,4);
  }
}

void CUsbDescriptor::intToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
{
  uint8_t idx = 0;

  for( idx = 0 ; idx < len ; idx ++)
  {
    if( ((value >> 28)) < 0xA )
    {
      pbuf[ 2* idx] = (value >> 28) + '0';
    }
    else
    {
      pbuf[2* idx] = (value >> 28) + 'A' - 10;
    }

    value = value << 4;

    pbuf[ 2* idx + 1] = 0;
  }
}
#endif

//---fin-----------------------------------------------------------------------
