summaryrefslogtreecommitdiffstats
path: root/host/windows/usb/api/adb_helper_routines.cpp
blob: 3a7748bbc2c066e68ff2ce6c75defd41d6bee435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** \file
  This file consists of implementation of helper routines used
  in the API.
*/

#include "stdafx.h"
#include "adb_api.h"
#include "adb_helper_routines.h"
#include "adb_interface_enum.h"

bool EnumerateDeviceInterfaces(HDEVINFO hardware_dev_info,
                               GUID class_id,
                               bool exclude_removed,
                               bool active_only,
                               AdbEnumInterfaceArray* interfaces) {
  AdbEnumInterfaceArray tmp;
  bool ret = false;

  // Enumerate interfaces on this device
  for (ULONG index = 0; ; index++) {
    SP_DEVICE_INTERFACE_DATA interface_data;
    interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    // SetupDiEnumDeviceInterfaces() returns information about device
    // interfaces exposed by one or more devices defined by our interface
    // class. Each call returns information about one interface. The routine
    // can be called repeatedly to get information about several interfaces
    // exposed by one or more devices.
    if (SetupDiEnumDeviceInterfaces(hardware_dev_info,
                                    0, 
                                    &class_id,
                                    index,
                                    &interface_data)) {
      // Satisfy "exclude removed" and "active only" filters.
      if ((!exclude_removed || (0 == (interface_data.Flags & SPINT_REMOVED))) &&
          (!active_only || (interface_data.Flags & SPINT_ACTIVE))) {
        std::wstring dev_name;

        if (GetUsbDeviceName(hardware_dev_info, &interface_data, &dev_name)) {
          try {
            // Add new entry to the array
            tmp.push_back(AdbInstanceEnumEntry(dev_name.c_str(),
                                               interface_data.InterfaceClassGuid,
                                               interface_data.Flags));
          } catch (... ) {
            SetLastError(ERROR_OUTOFMEMORY);
            break;
          }
        } else {
          // Something went wrong in getting device name
          break;
        }
      }
    } else {
      if (ERROR_NO_MORE_ITEMS == GetLastError()) {
        // There are no more items in the list. Enum is completed.
        ret = true;
        break;
      } else {
        // Something went wrong in SDK enum
        break;
      }
    }
  }

  // On success, swap temp array with the returning one
  if (ret)
    interfaces->swap(tmp);

  return ret;
}

bool EnumerateDeviceInterfaces(GUID class_id,
                               ULONG flags,
                               bool exclude_removed,
                               bool active_only,
                               AdbEnumInterfaceArray* interfaces) {
  // Open a handle to the plug and play dev node.
  // SetupDiGetClassDevs() returns a device information set that
  // contains info on all installed devices of a specified class.
  HDEVINFO hardware_dev_info =
    SetupDiGetClassDevs(&class_id, NULL, NULL, flags);

  bool ret = false;

  if (INVALID_HANDLE_VALUE != hardware_dev_info) {
    // Do the enum
    ret = EnumerateDeviceInterfaces(hardware_dev_info,
                                    class_id,
                                    exclude_removed,
                                    active_only,
                                    interfaces);

    // Preserve last error accross hardware_dev_info destruction
    ULONG error_to_report = ret ? NO_ERROR : GetLastError();

    SetupDiDestroyDeviceInfoList(hardware_dev_info);

    if (NO_ERROR != error_to_report)
      SetLastError(error_to_report);
  }

  return ret;
}

bool GetUsbDeviceDetails(
    HDEVINFO hardware_dev_info,
    PSP_DEVICE_INTERFACE_DATA dev_info_data,
    PSP_DEVICE_INTERFACE_DETAIL_DATA* dev_info_detail_data) {
  ULONG required_len = 0;

  // First query for the structure size. At this point we expect this call
  // to fail with ERROR_INSUFFICIENT_BUFFER error code.
  if (SetupDiGetDeviceInterfaceDetail(hardware_dev_info,
                                      dev_info_data,
                                      NULL,
                                      0,
                                      &required_len,
                                      NULL)) {
    return false;
  }

  if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
    return false;

  // Allocate buffer for the structure
  PSP_DEVICE_INTERFACE_DETAIL_DATA buffer =
    reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(malloc(required_len));

  if (NULL == buffer) {
    SetLastError(ERROR_OUTOFMEMORY);
    return false;
  }

  buffer->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

  // Retrieve the information from Plug and Play.
  if (SetupDiGetDeviceInterfaceDetail(hardware_dev_info,
                                      dev_info_data,
                                      buffer,
                                      required_len,
                                      &required_len,
                                      NULL)) {
    *dev_info_detail_data = buffer;
    return true;
  } else {
    // Free the buffer if this call failed
    free(buffer);

    return false;
  }
}

bool GetUsbDeviceName(HDEVINFO hardware_dev_info,
                      PSP_DEVICE_INTERFACE_DATA dev_info_data,
                      std::wstring* name) {
  PSP_DEVICE_INTERFACE_DETAIL_DATA func_class_dev_data = NULL;
  if (!GetUsbDeviceDetails(hardware_dev_info,
                           dev_info_data,
                           &func_class_dev_data)) {
    return false;
  }

  try {
    *name = func_class_dev_data->DevicePath;
  } catch (...) {
    SetLastError(ERROR_OUTOFMEMORY);
  }

  free(func_class_dev_data);

  return !name->empty();
}