1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /** \file
18   This file consists of implementation of AdbInterfaceEnumObject class that
19   encapsulates enumerator of USB interfaces available through this API.
20 */
21 
22 #include "stdafx.h"
23 #include "adb_api.h"
24 #include "adb_interface_enum.h"
25 #include "adb_helper_routines.h"
26 
AdbInterfaceEnumObject()27 AdbInterfaceEnumObject::AdbInterfaceEnumObject()
28     : AdbObjectHandle(AdbObjectTypeInterfaceEnumerator) {
29   current_interface_ = interfaces_.begin();
30 }
31 
~AdbInterfaceEnumObject()32 AdbInterfaceEnumObject::~AdbInterfaceEnumObject() {
33 }
34 
InitializeEnum(GUID class_id,bool exclude_not_present,bool exclude_removed,bool active_only)35 bool AdbInterfaceEnumObject::InitializeEnum(GUID class_id,
36                                             bool exclude_not_present,
37                                             bool exclude_removed,
38                                             bool active_only) {
39   // Calc flags for SetupDiGetClassDevs
40   DWORD flags = DIGCF_DEVICEINTERFACE;
41   if (exclude_not_present)
42     flags |= DIGCF_PRESENT;
43 
44   // Do the enum
45   bool ret = EnumerateDeviceInterfaces(class_id,
46                                        flags,
47                                        exclude_removed,
48                                        active_only,
49                                        &interfaces_);
50 
51   // If enum was successfull set current enum pointer
52   // to the beginning of the array
53   if (ret)
54     current_interface_ = interfaces_.begin();
55 
56   return ret;
57 }
58 
Next(AdbInterfaceInfo * info,ULONG * size)59 bool AdbInterfaceEnumObject::Next(AdbInterfaceInfo* info, ULONG* size) {
60   // Make sure that it's opened
61   if (!IsOpened()) {
62     SetLastError(ERROR_INVALID_HANDLE);
63     return false;
64   }
65 
66   ATLASSERT(NULL != size);
67   if (NULL == size) {
68     SetLastError(ERROR_INVALID_PARAMETER);
69     return false;
70   }
71 
72   // Lets see if enum is over
73   if (interfaces_.end() == current_interface_) {
74     SetLastError(ERROR_NO_MORE_ITEMS);
75     return false;
76   }
77 
78   AdbInstanceEnumEntry& entry = *current_interface_;
79 
80   // Big enough?
81   if ((NULL == info) || (*size < entry.GetFlatSize())) {
82     *size = entry.GetFlatSize();
83     SetLastError(ERROR_INSUFFICIENT_BUFFER);
84     return false;
85   }
86 
87   // All checks passed
88   entry.Save(info);
89   current_interface_++;
90   return true;
91 }
92 
Reset()93 bool AdbInterfaceEnumObject::Reset() {
94   // Make sure that it's opened
95   if (!IsOpened()) {
96     SetLastError(ERROR_INVALID_HANDLE);
97     return false;
98   }
99 
100   current_interface_ = interfaces_.begin();
101 
102   return true;
103 }
104