SDBAPIHANDLE SdbLegacyInterfaceObject::OpenEndpoint(
    UCHAR endpoint_index,
    SdbOpenAccessType access_type,
    SdbOpenSharingMode sharing_mode) {
  // Convert index into name and ID.
  std::wstring endpoint_name;
  UCHAR endpoint_id;

  try {
    if ((SDB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) ||
        (def_read_endpoint_ == endpoint_index)) {
      endpoint_name = DEVICE_BULK_READ_PIPE_NAME;
      endpoint_id = read_endpoint_id_;
      endpoint_index = def_read_endpoint_;
    } else if ((SDB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) ||
               (def_write_endpoint_ == endpoint_index)) {
      endpoint_name = DEVICE_BULK_WRITE_PIPE_NAME;
      endpoint_id = write_endpoint_id_;
      endpoint_index = def_write_endpoint_;
    } else {
      SetLastError(ERROR_INVALID_PARAMETER);
      return false;
    }
  } catch (...) {
    // We don't expect exceptions other than OOM thrown here.
    SetLastError(ERROR_OUTOFMEMORY);
    return NULL;
  }

  return OpenEndpoint(endpoint_name.c_str(), endpoint_id, endpoint_index,
                      access_type, sharing_mode);
}
ADBAPIHANDLE AdbWinUsbInterfaceObject::OpenEndpoint(
    UCHAR endpoint_index,
    AdbOpenAccessType access_type,
    AdbOpenSharingMode sharing_mode) {
  // Convert index into id
  UCHAR endpoint_id;

  if ((ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) ||
      (def_read_endpoint_ == endpoint_index)) {
    endpoint_id = read_endpoint_id_;
    endpoint_index = def_read_endpoint_;
  } else if ((ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) ||
             (def_write_endpoint_ == endpoint_index)) {
    endpoint_id = write_endpoint_id_;
    endpoint_index = def_write_endpoint_;
  } else {
    SetLastError(ERROR_INVALID_PARAMETER);
    return false;
  }

  return OpenEndpoint(endpoint_id, endpoint_index);
}
ADBAPIHANDLE AdbInterfaceObject::OpenEndpoint(
    UCHAR endpoint_index,
    AdbOpenAccessType access_type,
    AdbOpenSharingMode sharing_mode) {
  // Convert index into name
  std::wstring endpoint_name;

  try {
    if (ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) {
      endpoint_name = DEVICE_BULK_READ_PIPE_NAME;
    } else if (ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) {
      endpoint_name = DEVICE_BULK_WRITE_PIPE_NAME;
    } else {
      wchar_t fmt[265];
      swprintf(fmt, L"%ws%u", DEVICE_PIPE_NAME_PREFIX, endpoint_index);
      endpoint_name = fmt;
    }
  } catch (...) {
    SetLastError(ERROR_OUTOFMEMORY);
    return NULL;
  }

  return OpenEndpoint(endpoint_name.c_str(), access_type, sharing_mode);
}