/* Handle set address request. 
This function is used to handle set address request. */
static usb_status_t USB_DeviceCh9SetAddress(usb_device_handle handle, usb_setup_struct_t *setup, uint8_t **buffer, uint32_t *length)
/* Handle set address request. 
This function is used to handle set address request. */
static usb_status_t USB_DeviceCh9SetAddress(usb_device_handle handle, usb_setup_struct_t *setup, uint8_t **buffer, uint32_t *length)
{
    usb_status_t error = kStatus_USB_InvalidRequest;
    uint8_t state;
    USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
    if ((kUSB_DeviceStateAddressing != state) && (kUSB_DeviceStateAddress != state) &&
        (kUSB_DeviceStateDefault != state))
    {
        return error;
    }
    if (kUSB_DeviceStateAddressing != state)
    {
        state = setup->wValue & 0xFFU;
        error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, &state);
    }
    else
    {
        error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL);
        if (kStatus_USB_Success == error)
        {
            state = kUSB_DeviceStateAddress;
            error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
        }
    }
    return error;
}