46                                          {
   48  HDEVINFO hDeviceInfo;
   49  SP_DEVINFO_DATA DeviceInfoData;
   50  SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
   51  PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = NULL;
   52  ULONG requiredLength = 0;
   53  DWORD index = 0;
   54  HANDLE hnd = INVALID_HANDLE_VALUE;
   55  WINUSB_INTERFACE_HANDLE wusbHnd = INVALID_HANDLE_VALUE;
   56 
   57  
   58  
   59  hDeviceInfo = SetupDiGetClassDevs(&devGUID, NULL, NULL,
   60                                    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
   61 
   62  if (hDeviceInfo == INVALID_HANDLE_VALUE) {
   63    DBG_ERR(
"SetupDiGetClassDevs failed, error=%lx\n", GetLastError());
 
   64    return false;
   65  }
   66 
   67  
   68  DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
   69 
   70  for (index = 0; SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData);
   71       index++) {
   72 
   73    deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
   74 
   75    
   76 
   77    bResult = SetupDiEnumDeviceInterfaces(hDeviceInfo, &DeviceInfoData,
   78                                          &devGUID, 0, &deviceInterfaceData);
   79 
   80    if (!bResult) {
   81      
   82      if (GetLastError() == ERROR_NO_MORE_ITEMS) {
   84        goto out;
   85      }
   86 
   87      
   88      DBG_ERR(
"SetupDiEnumDeviceInterfaces(1) failed, error=%lx\n",
 
   89              GetLastError());
   90      goto out;
   91    }
   92 
   93    
   94    
   95    
   96    
   97 
   98    bResult = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &deviceInterfaceData,
   99                                              NULL, 0, &requiredLength, NULL);
  100 
  101    if (!bResult && (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
  102      DBG_ERR(
"SetupDiEnumDeviceInterfaces(2) failed, error=%lx\n",
 
  103              GetLastError());
  104      goto out;
  105    }
  106 
  107    
  108    pInterfaceDetailData = malloc(requiredLength);
  109    pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  110 
  111    
  112    bResult =
  113      SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &deviceInterfaceData,
  114                                      pInterfaceDetailData, requiredLength,
  115                                      NULL, &DeviceInfoData);
  116 
  117    if (!bResult) {
  118      DBG_ERR(
"SetupDiGetDeviceInterfaceDetail failed, error=%lx\n",
 
  119              GetLastError());
  120      goto out;
  121    }
  122 
  123    hnd =
  124      CreateFile(pInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
  125                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  126                 FILE_FLAG_OVERLAPPED, NULL);
  127 
  128    free(pInterfaceDetailData);
  129    pInterfaceDetailData = NULL;
  130 
  131    if (hnd == INVALID_HANDLE_VALUE) {
  132      DBG_ERR(
"CreateFile failed, error=%lx", GetLastError());
 
  133      continue;
  134    }
  135 
  136    if (!WinUsb_Initialize(hnd, &wusbHnd)) {
  137      DBG_ERR(
"WinUsb_Initialize failed, error=%lx", GetLastError());
 
  139    }
  140 
  141    if (backend->
serial != 0) {
 
  142      USB_DEVICE_DESCRIPTOR desc;
  143      ULONG written;
  144      unsigned char ser_num[128] = {0};
  145 
  146      if (!WinUsb_GetDescriptor(wusbHnd, USB_DEVICE_DESCRIPTOR_TYPE, 0, 0,
  147                                (unsigned char *) &desc, sizeof(desc),
  148                                &written)) {
  149        DBG_ERR(
"WinUsb_GetDescriptor failed, error=%lx", GetLastError());
 
  151      }
  152 
  153      if (!WinUsb_GetDescriptor(wusbHnd, USB_STRING_DESCRIPTOR_TYPE,
  154                                desc.iSerialNumber,
  155                                0x0409, 
  156                                ser_num, sizeof(ser_num), &written)) {
  157        DBG_ERR(
"WinUsb_GetDescriptor failed, error=%lx", GetLastError());
 
  159      }
  160 
  161      DBG_INFO(
"Extracted serial %ls (%lu bytes) from device desc %d",
 
  162               (wchar_t *) (ser_num + 2), written, desc.iSerialNumber);
  163 
  164      unsigned long devSerial = wcstoul((wchar_t *) (ser_num + 2), NULL, 10);
  165      if (devSerial != backend->
serial) {
 
  166        DBG_INFO(
"Device has serial %lu, not matching searched %lu", devSerial,
 
  169      }
  170    }
  171    break;
  173    if (wusbHnd != INVALID_HANDLE_VALUE) {
  174      WinUsb_Free(wusbHnd);
  175      wusbHnd = INVALID_HANDLE_VALUE;
  176    }
  177    if (hnd != INVALID_HANDLE_VALUE) {
  178      CloseHandle(hnd);
  179      hnd = INVALID_HANDLE_VALUE;
  180    }
  181    continue;
  182  }
  183 
  184  if (wusbHnd != INVALID_HANDLE_VALUE) {
  185 
  186    
  187    bResult = WinUsb_SetPipePolicy(wusbHnd, 
PIPE_OUT, SHORT_PACKET_TERMINATE, 1,
 
  188                                   (PVOID) "\x1");
  189    if (!bResult) {
  190      DBG_ERR(
"SetPipePolicy failed");
 
  191      WinUsb_Free(wusbHnd);
  192      wusbHnd = INVALID_HANDLE_VALUE;
  193      CloseHandle(hnd);
  194      hnd = INVALID_HANDLE_VALUE;
  195      goto out;
  196    }
  197 
  198    {
  199      
  200      
  201      
  202      
  204      unsigned long transferred = 0;
  205      unsigned long timeout = 10;
  206 
  207      bResult = WinUsb_SetPipePolicy(wusbHnd, 
PIPE_IN, PIPE_TRANSFER_TIMEOUT,
 
  208                                     sizeof(timeout), &timeout);
  209      if (!bResult) {
  210        DBG_ERR(
"SetPipePolicy failed");
 
  211        WinUsb_Free(wusbHnd);
  212        wusbHnd = INVALID_HANDLE_VALUE;
  213        CloseHandle(hnd);
  214        hnd = INVALID_HANDLE_VALUE;
  215        goto out;
  216      }
  217 
  218      if (WinUsb_ReadPipe(wusbHnd, 
PIPE_IN, 
buf, 
sizeof(
buf), &transferred,
 
  219                          0)) {
  220        DBG_INFO(
"%lu bytes of stale data read", transferred);
 
  221      }
  222 
  223      timeout = 0;
  224      bResult = WinUsb_SetPipePolicy(wusbHnd, 
PIPE_IN, PIPE_TRANSFER_TIMEOUT,
 
  225                                     sizeof(timeout), &timeout);
  226      if (!bResult) {
  227        DBG_ERR(
"SetPipePolicy failed");
 
  228        WinUsb_Free(wusbHnd);
  229        wusbHnd = INVALID_HANDLE_VALUE;
  230        CloseHandle(hnd);
  231        hnd = INVALID_HANDLE_VALUE;
  232        goto out;
  233      }
  234    }
  235  }
  236 
  237out:
  240 
  241  if (hDeviceInfo) {
  242    SetupDiDestroyDeviceInfoList(hDeviceInfo);
  243  }
  244 
  245  free(pInterfaceDetailData);
  246 
  247  if (wusbHnd != INVALID_HANDLE_VALUE) {
  248    return true;
  249  } else {
  250    return false;
  251  }
  252}
uint32_t next(octet_iterator &it, octet_iterator end)
 
#define YH_MSG_BUF_SIZE
Maximum length of message buffer.