#include "usb_desc.h"
//#include "usb_hid_desc.h"
#include "usb_printer_desc.h"

#if FUNC_USBDEV_EN

//设备描述符
//配置描述符
//字符串描述符
//Device ID

//usb device descriptor
AT(.usbdev.com.table)
const u8 desc_usb_device[18] = {
    18,                     // Num bytes of the descriptor
    USB_DESC_TYPE_DEV,      // Device Descriptor type
    0x10,                   // Revision of USB Spec. LSB (in BCD)
    0x01,                   // Revision of USB Spec. MSB (in BCD)
    0,                      //
    0,                      //
    0,                      //
    USB_MAX_EP0_SIZE,       // Max packet size of Endpoint 0

    L_BYTE(USB_DEV_VID),    // Vendor ID LSB
    H_BYTE(USB_DEV_VID),    // Vendor ID MSB
    L_BYTE(USB_DEV_PID),    // Product ID LSB
    H_BYTE(USB_DEV_PID),    // Product ID MSB
    0x00,                   // Device Revision LSB (in BCD)
    0x01,                   // Device Revision MSB (in BCD)

    1,                      // Index of Manufacture string descriptor
    2,                      // Index of Product string descriptor
    3,                      // Index of Serial No. string descriptor
    1                       // Num Configurations, Must be 1
};
//usb config descriptor
AT(.usbdev.com.table)
const u8 config_desc_usb_device[32] = {
    0x09,
    USB_DESC_TYPE_CFG,
    L_BYTE(DEV_CFG_DESC_SIZE),
    H_BYTE(DEV_CFG_DESC_SIZE),
    0x01,
    0x01,
    0x00,
    0xc0,
    0x00,//9byte 配置描述符

    0x09,
    USB_DESC_TYPE_IF,
    0x00,
    0x00,
    0x02,
    0x07,
    0x01,
    0x02,
    0x04,//9byte 接口描述符

    0x07,
    USB_DESC_TYPE_EP,
    USB_DEV_IN_EP | 0x80,
    0x02,
    L_BYTE(EP_IN_MAX_SIZE),
    H_BYTE(EP_IN_MAX_SIZE),
    0x00,//7byte 端点描述符

    0x07,
    USB_DESC_TYPE_EP,
    USB_DEV_OUT_EP,
    0x02,
    L_BYTE(EP_OUT_MAX_SIZE),
    H_BYTE(EP_OUT_MAX_SIZE),
    0x00,//7byte 端点描述符
};
//Language ID
AT(.usbdev.com.table)
const u8 str_language_id[] = {
    4,
    3,
    L_BYTE(USB_DEV_LANG_ID),
    H_BYTE(USB_DEV_LANG_ID),
};

AT(.usbdev.com.table)
const u8 device_id[37-8] = {
    0x00,0x50,0x4d,0x46,0x47,0x3a,//"MFG:"
    'G','e','e','h','y',
    0x3b,0x4d,0x44,0x4c,0x3a,//";MDL:"
    ' ','P','r','i','n','t','e','r',
    0x3b,0x43,0x4d,0x44,0x3a,0x50,0x50,0x4c,0x49,0x3b//";CMD:PPLI;"

};

AT(.usb_buf.com.buf)
u8 string_desc_buf[USB_MAX_STR_DESC_SIZE];

void usb_desc_config(u8 *dest, char *src, u8 *length)
{
    u8 str_length = 0;
    u8 idx = 0;

    if (src != NULL) {
        while (src[str_length] != '\0' && str_length < USB_MAX_STR_DESC_SIZE) {
            str_length++;
        }

        *length = str_length * 2 + 2;
        dest[idx++] = *length;
        dest[idx++] = USB_DESC_TYPE_STR;

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

u8 *usb_get_device_descriptor(u8 *length)
{
    *length = sizeof(desc_usb_device);
    printf("usb_get_device_descriptor\n");
	return (u8 *)desc_usb_device;
}

u8 *usb_get_lang_id_str_descriptor(u8 *length)
{
    *length = sizeof(str_language_id);
    printf("usb_get_id_str_descriptor\n");
    return (u8 *)str_language_id;
}

u8 *usb_get_manufacturer_str_descriptor(u8 *length)
{
    usb_desc_config(string_desc_buf, USB_DESC_MANUF, length);
    printf("usb_get_manuf_descriptor\n");
    return string_desc_buf;
}

u8 *usb_get_product_str_descriptor(u8 *length)
{
    usb_desc_config(string_desc_buf, USB_DESC_PRODUCT, length);
    return string_desc_buf;
}

u8 *usb_get_serial_str_descriptor(u8 *length)
{
    usb_desc_config(string_desc_buf, USB_DESC_SERIAL, length);
    return string_desc_buf;
}

#endif
