#ifndef _HOST_OS_DETECT_H_
#define _HOST_OS_DETECT_H_

#include "xua.h"
#include "xc_ptr.h"
#include "xassert.h"
#if XUA_USB_EN
#ifndef WINDOWS_OS_DESCRIPTOR_SUPPORT
#define WINDOWS_OS_DESCRIPTOR_SUPPORT
#endif
#define EP0_MAX_REQUEST_SIZE 2048
#include "xud.h"
#include "vendorrequests.h"

unsigned g_host_os = 0;       // 1 -> Windows, 0 -> Others


int VendorAudioRequests(XUD_ep ep0_out, XUD_ep ep0_in, unsigned char bRequest, unsigned char cs, unsigned char cn,
    unsigned short unitId, unsigned char direction, NULLABLE_RESOURCE(chanend, c_aud_ctl),
    NULLABLE_RESOURCE(chanend, c_mix_ctl),
    NULLABLE_RESOURCE(chanend, c_clk_ctL))
{

    return XUD_RES_ERR;
}
enum { OS_WIN = 1, OS_OTHERS = 2 };
#if 1
int VendorRequests(XUD_ep ep0_out, XUD_ep ep0_in, REFERENCE_PARAM(USB_SetupPacket_t, sp) VENDOR_REQUESTS_PARAMS_DEC_)
{
  XUD_Result_t result = XUD_RES_ERR;

  unsigned char request_data[EP0_MAX_REQUEST_SIZE];
  unsigned len;
  int a = 0;
  unsigned k = 5;
//SET_SHARED_GLOBAL(g_host_os, k);
  switch ((sp->bmRequestType.Direction << 7) | (sp->bmRequestType.Type << 5) | (sp->bmRequestType.Recipient)) {
    case USB_BMREQ_H2D_VENDOR_DEV:
      result = XUD_GetBuffer(ep0_out, request_data, len);
      if (result == XUD_RES_OKAY) {
        if (a/*control_process_usb_set_request(sp.wIndex, sp.wValue, sp.wLength, request_data, i_control) == CONTROL_SUCCESS*/) {
          /* zero length data to indicate success
          * on control error, go to standard requests, which will issue STALL
          */
          result = XUD_DoSetRequestStatus(ep0_in);
        } else {
          result = XUD_RES_ERR;
        }
      }
      break;

    case USB_BMREQ_D2H_VENDOR_DEV:
      /* application retrieval latency inside the control library call
      * XUD task defers further calls by NAKing USB transactions
      */
//      if (a/*control_process_usb_get_request(sp.wIndex, sp.wValue, sp.wLength, request_data, i_control) == CONTROL_SUCCESS*/) {
//        len = sp->wLength;
//        result = XUD_DoGetRequest(ep0_out, ep0_in, request_data, len, len);
//        /* on control error, go to standard requests, which will issue STALL */
//      }
#ifdef WINDOWS_OS_DESCRIPTOR_SUPPORT
//      else 
if(sp->bRequest == 0x01)	// GET_MS_DESCRIPTOR, same as the OS_STR_DESC last byte
      {
	 // dwLength, 4bytes: 16 bytes
	 request_data[0] = 0x10; request_data[1] = 0x00; request_data[2] = 0x00; request_data[3] = 0x00; 
	 // bcdVersion, 2bytes: For version 1.00, bcdVersion is set to 0x0100.
	 request_data[4] = 0x00; request_data[5] = 0x01;
	 // wIndex, 2bytes: 0x04 for extended compat ID descriptors
	 request_data[6] = 0x04; request_data[7] = 0x00;
	 // bCount, 1byte: 0
	 request_data[8] = 0x00;
	 // RESERVED, 7bytes: all 0
	 request_data[9] = 0x00; request_data[10] = 0x00; request_data[11] = 0x00;
	 request_data[12] = 0x00; request_data[13] = 0x00; request_data[14] = 0x00; request_data[15] = 0x00; 
	 
      	 result = XUD_DoGetRequest(ep0_out, ep0_in, request_data, 16, sp->wLength);
//xassert(busSpeed == XUD_SPEED_HS);    
    k = OS_WIN;
    SET_SHARED_GLOBAL(g_host_os, k);
      }

      else {
//xassert(busSpeed == XUD_SPEED_FS);
    k = OS_OTHERS;
    SET_SHARED_GLOBAL(g_host_os, k);
        result = XUD_RES_ERR;
      }
#endif
      break;
  }
  return result;
}
#endif
void VendorRequests_Init(VENDOR_REQUESTS_PARAMS_DEC)
{

}

#endif /* XUA_USB_EN */

#endif