Refactor ADB API dll to support WinUsb

This commit is contained in:
vchtchetkine
2009-07-22 13:34:53 -07:00
parent 8c78ba6438
commit dceaaa52ce
30 changed files with 4032 additions and 4433 deletions

View File

@@ -18,6 +18,12 @@
#include "stdafx.h"
extern "C" {
int _forceCRTManifest;
int _forceMFCManifest;
int _forceAtlDllManifest;
};
class CAdbWinApiModule : public CAtlDllModuleT< CAdbWinApiModule > {
public:
};

View File

@@ -1,21 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 8.00
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AdbWinApi", "AdbWinApi.vcproj", "{C0A471E9-6892-4270-96DE-DB5F8D526FB1}"
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfiguration) = preSolution
Debug = Debug
Release = Release
EndGlobalSection
GlobalSection(ProjectConfiguration) = postSolution
{C0A471E9-6892-4270-96DE-DB5F8D526FB1}.Debug.ActiveCfg = Debug|Win32
{C0A471E9-6892-4270-96DE-DB5F8D526FB1}.Debug.Build.0 = Debug|Win32
{C0A471E9-6892-4270-96DE-DB5F8D526FB1}.Release.ActiveCfg = Release|Win32
{C0A471E9-6892-4270-96DE-DB5F8D526FB1}.Release.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection
GlobalSection(ExtensibilityAddIns) = postSolution
EndGlobalSection
EndGlobal

View File

@@ -1,290 +0,0 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="AdbWinApi"
ProjectGUID="{C0A471E9-6892-4270-96DE-DB5F8D526FB1}"
Keyword="AtlProj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="2"
UseOfATL="1"
ATLMinimizesCRunTimeLibraryUsage="FALSE"
CharacterSet="1">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="c:\winddk\6000\inc\api;..\common"
PreprocessorDefinitions="WIN32;_WINDOWS;_DEBUG;_USRDLL;ADBWIN_EXPORTS"
MinimalRebuild="FALSE"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
BufferSecurityCheck="TRUE"
TreatWChar_tAsBuiltInType="TRUE"
UsePrecompiledHeader="3"
ProgramDataBaseFileName="..\build\$(OutDir)\i386\$(TargetName).pdb"
WarningLevel="4"
WarnAsError="TRUE"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="4"
DisableSpecificWarnings="4100;4200;4702"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
IgnoreImportLibrary="TRUE"
AdditionalDependencies="c:\winddk\6000\lib\wxp\i386\usbd.lib setupapi.lib"
OutputFile="..\build\$(OutDir)\i386/AdbWinApi.dll"
LinkIncremental="2"
AdditionalLibraryDirectories=""
ModuleDefinitionFile=".\AdbWinApi.def"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="..\build\$(OutDir)\i386/$(ProjectName).pdb"
SubSystem="2"
ImportLibrary="..\build\$(OutDir)\i386/AdbWinApi.lib"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"
PreprocessorDefinitions="_DEBUG"
MkTypLibCompatible="FALSE"
TargetEnvironment="1"
GenerateStublessProxies="TRUE"
TypeLibraryName="$(IntDir)/AdbWinApi.tlb"
HeaderFileName="AdbWinApi.h"
DLLDataFileName=""
InterfaceIdentifierFileName="AdbWinApi_i.c"
ProxyFileName="AdbWinApi_p.c"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"
PreprocessorDefinitions="_DEBUG"
Culture="1033"
AdditionalIncludeDirectories="$(IntDir)"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="2"
UseOfATL="1"
ATLMinimizesCRunTimeLibraryUsage="FALSE"
CharacterSet="1">
<Tool
Name="VCCLCompilerTool"
Optimization="4"
InlineFunctionExpansion="2"
AdditionalIncludeDirectories="c:\winddk\6000\inc\api;..\common"
PreprocessorDefinitions="WIN32;_WINDOWS;NDEBUG;_USRDLL;ADBWIN_EXPORTS"
MinimalRebuild="FALSE"
RuntimeLibrary="0"
BufferSecurityCheck="TRUE"
TreatWChar_tAsBuiltInType="TRUE"
UsePrecompiledHeader="3"
ProgramDataBaseFileName="..\build\$(OutDir)\i386\$(TargetName).pdb"
WarningLevel="4"
WarnAsError="TRUE"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"
DisableSpecificWarnings="4100;4200;4702"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
IgnoreImportLibrary="TRUE"
AdditionalDependencies="c:\winddk\6000\lib\wxp\i386\usbd.lib setupapi.lib"
OutputFile="..\build\$(OutDir)\i386/AdbWinApi.dll"
LinkIncremental="1"
AdditionalLibraryDirectories=""
ModuleDefinitionFile=".\AdbWinApi.def"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="..\build\$(OutDir)\i386/$(ProjectName).pdb"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
ImportLibrary="..\build\$(OutDir)\i386/AdbWinApi.lib"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"
PreprocessorDefinitions="NDEBUG"
MkTypLibCompatible="FALSE"
TargetEnvironment="1"
GenerateStublessProxies="TRUE"
TypeLibraryName="$(IntDir)/AdbWinApi.tlb"
HeaderFileName="AdbWinApi.h"
DLLDataFileName=""
InterfaceIdentifierFileName="AdbWinApi_i.c"
ProxyFileName="AdbWinApi_p.c"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"
PreprocessorDefinitions="NDEBUG"
Culture="1033"
AdditionalIncludeDirectories="$(IntDir)"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}">
<File
RelativePath=".\adb_api.cpp">
</File>
<File
RelativePath=".\adb_endpoint_object.cpp">
</File>
<File
RelativePath=".\adb_helper_routines.cpp">
</File>
<File
RelativePath=".\adb_interface.cpp">
</File>
<File
RelativePath=".\adb_interface_enum.cpp">
</File>
<File
RelativePath=".\adb_io_completion.cpp">
</File>
<File
RelativePath=".\adb_io_object.cpp">
</File>
<File
RelativePath=".\adb_object_handle.cpp">
</File>
<File
RelativePath=".\AdbWinApi.cpp">
</File>
<File
RelativePath=".\AdbWinApi.def">
</File>
<File
RelativePath=".\stdafx.cpp">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32">
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"/>
</FileConfiguration>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
<File
RelativePath=".\adb_api.h">
</File>
<File
RelativePath=".\adb_api_private_defines.h">
</File>
<File
RelativePath=".\adb_endpoint_object.h">
</File>
<File
RelativePath=".\adb_helper_routines.h">
</File>
<File
RelativePath=".\adb_interface.h">
</File>
<File
RelativePath=".\adb_interface_enum.h">
</File>
<File
RelativePath=".\adb_io_completion.h">
</File>
<File
RelativePath=".\adb_io_object.h">
</File>
<File
RelativePath=".\adb_object_handle.h">
</File>
<File
RelativePath=".\Resource.h">
</File>
<File
RelativePath=".\stdafx.h">
</File>
<Filter
Name="common"
Filter="">
<File
RelativePath=".\adb_api_extra.h">
</File>
<File
RelativePath="..\common\android_usb_common_defines.h">
</File>
</Filter>
<Filter
Name="USB"
Filter="">
<File
RelativePath="..\..\..\..\..\..\..\..\Winddk\6000\inc\api\usb.h">
</File>
<File
RelativePath="..\..\..\..\..\..\..\..\Winddk\6000\inc\api\usb100.h">
</File>
<File
RelativePath="..\..\..\..\..\..\..\..\Winddk\6000\inc\api\usb200.h">
</File>
<File
RelativePath="..\..\..\..\..\..\..\..\Winddk\6000\inc\api\usbdi.h">
</File>
</Filter>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
<File
RelativePath=".\AdbWinApi.rc">
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

22
host/windows/usb/api/MAKEFILE Executable file
View File

@@ -0,0 +1,22 @@
#
# 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.
#
#
# DO NOT EDIT THIS FILE!!! Edit .\sources. if you want to add a new source
# file to this component. This file merely indirects to the real make file
# that is shared by all the components of NT OS/2
#
!INCLUDE $(NTMAKEENV)\makefile.def

96
host/windows/usb/api/SOURCES Executable file
View File

@@ -0,0 +1,96 @@
#
# 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.
#
TARGETNAME = AdbWinApi
TARGETPATH = obj
TARGETTYPE = DYNLINK
UMTYPE = windows
DLLDEF = AdbWinApi.def
# Use statically linked atl libraries:
# - atls.lib for free build
# - atlsd.lib for checked build
USE_STATIC_ATL = 1
# Use ATL v. 7.1
ATL_VER = 71
# Use STL v. 6.0
USE_STL = 1
STL_VER = 60
# Use multithreaded libraries
USE_LIBCMT = 1
# Include directories
INCLUDES = $(DDK_INC_PATH); \
$(SDK_INC_PATH); \
$(CRT_INC_PATH); \
$(SDK_INC_PATH)\crt; \
$(CRT_INC_PATH)\atl71; \
$(SDK_INC_PATH)\crt\stl60
# Common target libraries
TARGETLIBS = $(SDK_LIB_PATH)\ole32.lib \
$(SDK_LIB_PATH)\Advapi32.lib \
$(SDK_LIB_PATH)\Kernel32.lib \
$(SDK_LIB_PATH)\User32.lib \
$(SDK_LIB_PATH)\oleaut32.lib \
$(SDK_LIB_PATH)\wbemuuid.lib \
$(SDK_LIB_PATH)\uuid.lib \
$(SDK_LIB_PATH)\setupapi.lib \
$(SDK_LIB_PATH)\usbd.lib \
$(SDK_LIB_PATH)\winusb.lib
!IF "$(DDKBUILDENV)" == "fre"
# Libraries for release (free) builds
TARGETLIBS = $(TARGETLIBS) $(ATL_LIB_PATH)\atls.lib
!ELSE
# Libraries for debug (checked) builds
TARGETLIBS = $(TARGETLIBS) $(ATL_LIB_PATH)\atlsd.lib
!ENDIF
# Common C defines
C_DEFINES= $(C_DEFINES) -DADBWIN_EXPORTS -D_UNICODE \
-DUNICODE -DWIN32 -D_WINDOWS -D_USRDLL -D_WINDLL
!IF "$(DDKBUILDENV)" == "fre"
# C defines for release (free) builds
C_DEFINES = $(C_DEFINES) -DNDEBUG
!ELSE
# C defines for debug (checked) builds
C_DEFINES = $(C_DEFINES) -D_DEBUG
!ENDIF
# Turn on all warnings, and treat warnings as errors
MSC_WARNING_LEVEL = /W4 /Wp64 /WX
# Common C defines
USER_C_FLAGS = $(USER_C_FLAGS) /FD /EHsc /wd4100 /wd4200 /wd4702 /nologo
# Set precompiled header information
PRECOMPILED_CXX = 1
PRECOMPILED_INCLUDE = stdafx.h
PRECOMPILED_SOURCEFILE = stdafx.cpp
# Define source files for AdbWinApi.dll
SOURCES = adb_api.cpp \
adb_endpoint_object.cpp \
adb_helper_routines.cpp \
adb_interface.cpp \
adb_interface_enum.cpp \
adb_io_completion.cpp \
adb_object_handle.cpp \
AdbWinApi.cpp \
AdbWinApi.rc

View File

@@ -28,7 +28,7 @@
#include "adb_io_completion.h"
#include "adb_helper_routines.h"
ADBAPIHANDLE AdbEnumInterfaces(GUID class_id,
ADBAPIHANDLE __cdecl AdbEnumInterfaces(GUID class_id,
bool exclude_not_present,
bool exclude_removed,
bool active_only) {
@@ -56,7 +56,7 @@ ADBAPIHANDLE AdbEnumInterfaces(GUID class_id,
return ret;
}
bool AdbNextInterface(ADBAPIHANDLE adb_handle,
bool __cdecl AdbNextInterface(ADBAPIHANDLE adb_handle,
AdbInterfaceInfo* info,
unsigned long* size) {
if (NULL == size) {
@@ -78,7 +78,7 @@ bool AdbNextInterface(ADBAPIHANDLE adb_handle,
return ret;
}
bool AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle) {
bool __cdecl AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle) {
// Lookup AdbInterfaceEnumObject object for the handle
AdbInterfaceEnumObject* adb_ienum_object =
LookupObject<AdbInterfaceEnumObject>(adb_handle);
@@ -93,7 +93,7 @@ bool AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle) {
return ret;
}
ADBWIN_API ADBAPIHANDLE AdbCreateInterfaceByName(
ADBAPIHANDLE __cdecl AdbCreateInterfaceByName(
const wchar_t* interface_name) {
AdbInterfaceObject* obj = NULL;
ADBAPIHANDLE ret = NULL;
@@ -114,7 +114,7 @@ ADBWIN_API ADBAPIHANDLE AdbCreateInterfaceByName(
return ret;
}
ADBAPIHANDLE AdbCreateInterface(GUID class_id,
ADBAPIHANDLE __cdecl AdbCreateInterface(GUID class_id,
unsigned short vendor_id,
unsigned short product_id,
unsigned char interface_id) {
@@ -159,7 +159,7 @@ ADBAPIHANDLE AdbCreateInterface(GUID class_id,
for (AdbEnumInterfaceArray::iterator it = interfaces.begin();
it != interfaces.end(); it++) {
const AdbInstanceEnumEntry& next_interface = *it;
if (0 == wcsnicmp(match_name,
if (0 == _wcsnicmp(match_name,
next_interface.device_name().c_str(),
match_len)) {
// Found requested interface among active interfaces.
@@ -171,7 +171,7 @@ ADBAPIHANDLE AdbCreateInterface(GUID class_id,
return NULL;
}
bool AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
void* buffer,
unsigned long* buffer_char_size,
bool ansi) {
@@ -190,7 +190,7 @@ bool AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
void* buffer,
unsigned long* buffer_char_size,
bool ansi) {
@@ -209,7 +209,7 @@ bool AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
USB_DEVICE_DESCRIPTOR* desc) {
// Lookup interface object for the handle
AdbInterfaceObject* adb_object =
@@ -226,7 +226,7 @@ bool AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
USB_CONFIGURATION_DESCRIPTOR* desc) {
// Lookup interface object for the handle
AdbInterfaceObject* adb_object =
@@ -243,7 +243,7 @@ bool AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
USB_INTERFACE_DESCRIPTOR* desc) {
// Lookup interface object for the handle
AdbInterfaceObject* adb_object =
@@ -260,7 +260,7 @@ bool AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
UCHAR endpoint_index,
AdbEndpointInformation* info) {
// Lookup interface object for the handle
@@ -278,21 +278,21 @@ bool AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
}
}
bool AdbGetDefaultBulkReadEndpointInformation(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetDefaultBulkReadEndpointInformation(ADBAPIHANDLE adb_interface,
AdbEndpointInformation* info) {
return AdbGetEndpointInformation(adb_interface,
ADB_QUERY_BULK_READ_ENDPOINT_INDEX,
info);
}
bool AdbGetDefaultBulkWriteEndpointInformation(ADBAPIHANDLE adb_interface,
bool __cdecl AdbGetDefaultBulkWriteEndpointInformation(ADBAPIHANDLE adb_interface,
AdbEndpointInformation* info) {
return AdbGetEndpointInformation(adb_interface,
ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX,
info);
}
ADBAPIHANDLE AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
ADBAPIHANDLE __cdecl AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
unsigned char endpoint_index,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode) {
@@ -312,7 +312,7 @@ ADBAPIHANDLE AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
}
}
ADBAPIHANDLE AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
ADBAPIHANDLE __cdecl AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode) {
return AdbOpenEndpoint(adb_interface,
@@ -321,7 +321,7 @@ ADBAPIHANDLE AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
sharing_mode);
}
ADBAPIHANDLE AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
ADBAPIHANDLE __cdecl AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode) {
return AdbOpenEndpoint(adb_interface,
@@ -330,7 +330,7 @@ ADBAPIHANDLE AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
sharing_mode);
}
ADBAPIHANDLE AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint) {
ADBAPIHANDLE __cdecl AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint) {
// Lookup endpoint object for the handle
AdbEndpointObject* adb_object =
LookupObject<AdbEndpointObject>(adb_endpoint);
@@ -346,7 +346,7 @@ ADBAPIHANDLE AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint) {
}
}
bool AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
bool __cdecl AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
AdbEndpointInformation* info) {
// Lookup endpoint object for the handle
AdbEndpointObject* adb_object =
@@ -363,7 +363,7 @@ bool AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
}
}
ADBAPIHANDLE AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
ADBAPIHANDLE __cdecl AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_read,
unsigned long* bytes_read,
@@ -388,7 +388,7 @@ ADBAPIHANDLE AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
}
}
ADBAPIHANDLE AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
ADBAPIHANDLE __cdecl AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_write,
unsigned long* bytes_written,
@@ -413,7 +413,7 @@ ADBAPIHANDLE AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
}
}
bool AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
bool __cdecl AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_read,
unsigned long* bytes_read,
@@ -434,7 +434,7 @@ bool AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
}
}
bool AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
bool __cdecl AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_write,
unsigned long* bytes_written,
@@ -455,7 +455,7 @@ bool AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
}
}
bool AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
bool __cdecl AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
LPOVERLAPPED overlapped,
unsigned long* bytes_transferred,
bool wait) {
@@ -475,7 +475,7 @@ bool AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
}
}
bool AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion) {
bool __cdecl AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion) {
// Lookup endpoint object for the handle
AdbIOCompletion* adb_object =
LookupObject<AdbIOCompletion>(adb_io_completion);
@@ -492,7 +492,7 @@ bool AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion) {
}
}
bool AdbCloseHandle(ADBAPIHANDLE adb_handle) {
bool __cdecl AdbCloseHandle(ADBAPIHANDLE adb_handle) {
// Lookup object for the handle
AdbObjectHandle* adb_object = AdbObjectHandle::Lookup(adb_handle);

View File

@@ -19,12 +19,8 @@
/** \file
This file consists of declarations of routines exported by the API as well
as types, structures, and constants definitions used in the API.
Declarations in this file, combined with definitions found in adb_api_extra.h
comprise ADB API for windows.
*/
#include "adb_api_extra.h"
// Enables compillation for "straight" C
#ifdef __cplusplus
#define EXTERN_C extern "C"
@@ -35,6 +31,84 @@
#define false 0
#endif
/** \brief Enumerates ADB endpoint types.
This enum is taken from WDF_USB_PIPE_TYPE enum found in WDK.
*/
typedef enum _AdbEndpointType {
/// Unknown (invalid, or not initialized) endpoint type.
AdbEndpointTypeInvalid = 0,
/// Endpoint is device control pipe.
AdbEndpointTypeControl,
/// Endpoint is isochronous r/w pipe.
AdbEndpointTypeIsochronous,
/// Endpoint is a bulk r/w pipe.
AdbEndpointTypeBulk,
/// Endpoint is an interrupt r/w pipe.
AdbEndpointTypeInterrupt,
} AdbEndpointType;
/** \brief Endpoint desriptor.
This structure is based on WDF_USB_PIPE_INFORMATION structure found in WDK.
*/
typedef struct _AdbEndpointInformation {
/// Maximum packet size this endpoint is capable of.
unsigned long max_packet_size;
/// Maximum size of one transfer which should be sent to the host controller.
unsigned long max_transfer_size;
/// ADB endpoint type.
AdbEndpointType endpoint_type;
/// Raw endpoint address on the device as described by its descriptor.
unsigned char endpoint_address;
/// Polling interval.
unsigned char polling_interval;
/// Which alternate setting this structure is relevant for.
unsigned char setting_index;
} AdbEndpointInformation;
/// Shortcut to default write bulk endpoint in zero-based endpoint index API.
#define ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX 0xFC
/// Shortcut to default read bulk endpoint in zero-based endpoint index API.
#define ADB_QUERY_BULK_READ_ENDPOINT_INDEX 0xFE
// {F72FE0D4-CBCB-407d-8814-9ED673D0DD6B}
/// Our USB class id that driver uses to register our device.
#define ANDROID_USB_CLASS_ID \
{0xf72fe0d4, 0xcbcb, 0x407d, {0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b}};
/// Defines vendor ID for HCT devices.
#define DEVICE_VENDOR_ID 0x0BB4
/// Defines product ID for the device with single interface.
#define DEVICE_SINGLE_PRODUCT_ID 0x0C01
/// Defines product ID for the Dream composite device.
#define DEVICE_COMPOSITE_PRODUCT_ID 0x0C02
/// Defines product ID for the Magic composite device.
#define DEVICE_MAGIC_COMPOSITE_PRODUCT_ID 0x0C03
/// Defines interface ID for the device.
#define DEVICE_INTERFACE_ID 0x01
/// Defines vendor ID for the device
#define DEVICE_EMULATOR_VENDOR_ID 0x18D1
/// Defines product ID for a SoftUSB device simulator that is used to test
/// the driver in isolation from hardware.
#define DEVICE_EMULATOR_PROD_ID 0xDDDD
// The following ifdef block is the standard way of creating macros which make
// exporting from a DLL simpler. All files within this DLL are compiled with
// the ADBWIN_EXPORTS symbol defined on the command line. this symbol should
@@ -48,7 +122,7 @@
#define ADBWIN_API EXTERN_C __declspec(dllimport)
#endif
/** Handle to an API object
/** \brief Handle to an API object.
To access USB interface and its components clients must first obtain a
handle to the required object. API Objects that are represented by a
@@ -83,41 +157,41 @@
*/
typedef void* ADBAPIHANDLE;
/** Enumeration AdbOpenAccessType defines access type with which
an I/O object (endpoint) should be opened.
/** \brief Defines access type with which an I/O object (endpoint)
should be opened.
*/
typedef enum _AdbOpenAccessType {
/// Opens for read and write access
/// Opens for read and write access.
AdbOpenAccessTypeReadWrite,
/// Opens for read only access
/// Opens for read only access.
AdbOpenAccessTypeRead,
/// Opens for write only access
/// Opens for write only access.
AdbOpenAccessTypeWrite,
/// Opens for querying information
/// Opens for querying information.
AdbOpenAccessTypeQueryInfo,
} AdbOpenAccessType;
/** Enumeration AdbOpenSharingMode defines sharing mode with which
an I/O object (endpoint) should be opened.
/** \brief Defines sharing mode with which an I/O object (endpoint)
should be opened.
*/
typedef enum _AdbOpenSharingMode {
/// Shares read and write
/// Shares read and write.
AdbOpenSharingModeReadWrite,
/// Shares only read
/// Shares only read.
AdbOpenSharingModeRead,
/// Shares only write
/// Shares only write.
AdbOpenSharingModeWrite,
/// Opens exclusive
/// Opens exclusive.
AdbOpenSharingModeExclusive,
} AdbOpenSharingMode;
/** Structure AdbInterfaceInfo provides information about an interface
/** \brief Provides information about an interface.
*/
typedef struct _AdbInterfaceInfo {
/// Inteface's class id (see SP_DEVICE_INTERFACE_DATA for details)
@@ -137,30 +211,30 @@ typedef struct _AdbInterfaceInfo {
This routine uses SetupDiGetClassDevs SDK routine to enumerate devices that
match class ID and then SetupDiEnumDeviceInterfaces SDK routine is called
to enumerate interfaces on the devices.
@param class_id[in] Device class ID, assigned by the driver.
@param exclude_not_present[in] If 'true' enumation will include only those
@param[in] class_id Device class ID, assigned by the driver.
@param[in] exclude_not_present If true enumation will include only those
devices that are currently present.
@param exclude_removed[in] If 'true' interfaces with SPINT_REMOVED flag set
@param[in] exclude_removed If true interfaces with SPINT_REMOVED flag set
will be not included in the enumeration.
@param active_only[in] If 'true' only active interfaces (with flag
@param[in] active_only If true only active interfaces (with flag
SPINT_ACTIVE set) will be included in the enumeration.
@return Handle to the enumerator object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbEnumInterfaces(GUID class_id,
ADBWIN_API ADBAPIHANDLE __cdecl AdbEnumInterfaces(GUID class_id,
bool exclude_not_present,
bool exclude_removed,
bool active_only);
/** \brief Gets next interface information
@param adb_handle[in] Handle to interface enumerator object obtained via
@param[in] adb_handle Handle to interface enumerator object obtained via
AdbEnumInterfaces call.
@param info[out] Upon successful completion will receive interface
@param[out] info Upon successful completion will receive interface
information. Can be NULL. If it is NULL, upon return from this
routine size parameter will contain memory size required for the
next entry.
@param size[in,out]. On the way in provides size of the memory buffer
@param[in,out] size On the way in provides size of the memory buffer
addressed by info parameter. On the way out (only if buffer was not
big enough) will provide memory size required for the next entry.
@return true on success, false on error. If false is returned
@@ -170,86 +244,86 @@ ADBWIN_API ADBAPIHANDLE AdbEnumInterfaces(GUID class_id,
required for the next entry. ERROR_NO_MORE_ITEMS indicates that
enumeration is over and there are no more entries to return.
*/
ADBWIN_API bool AdbNextInterface(ADBAPIHANDLE adb_handle,
ADBWIN_API bool __cdecl AdbNextInterface(ADBAPIHANDLE adb_handle,
AdbInterfaceInfo* info,
unsigned long* size);
/** \brief Resets enumerator so next call to AdbNextInterface will start
from the beginning.
@param adb_handle[in] Handle to interface enumerator object obtained via
@param[in] adb_handle Handle to interface enumerator object obtained via
AdbEnumInterfaces call.
@return true on success, false on error. If false is returned GetLastError()
provides extended error information.
*/
ADBWIN_API bool AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle);
ADBWIN_API bool __cdecl AdbResetInterfaceEnum(ADBAPIHANDLE adb_handle);
/** \brief Creates USB interface object
This routine creates an object that represents a USB interface.
@param interface_name[in] Name of the interface.
@param[in] interface_name Name of the interface.
@return Handle to the interface object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbCreateInterfaceByName(const wchar_t* interface_name);
ADBWIN_API ADBAPIHANDLE __cdecl AdbCreateInterfaceByName(const wchar_t* interface_name);
/** \brief
Creates USB interface object based on vendor, product and interface IDs.
/** \brief Creates USB interface object based on vendor, product and
interface IDs.
This routine creates and object that represents a USB interface on our
device. It uses AdbCreateInterfaceByName to actually do the create.
@param class_id[in] Device class ID, assigned by the driver.
@param vendor_id[in] Device vendor ID
@param product_id[in] Device product ID
@param interface_id[in] Device interface ID. This parameter is optional.
@param[in] class_id Device class ID, assigned by the driver.
@param[in] vendor_id Device vendor ID
@param[in] product_id Device product ID
@param[in] interface_id Device interface ID. This parameter is optional.
Value 0xFF indicates that interface should be addressed by vendor
and product IDs only.
@return Handle to the interface object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbCreateInterface(GUID class_id,
ADBWIN_API ADBAPIHANDLE __cdecl AdbCreateInterface(GUID class_id,
unsigned short vendor_id,
unsigned short product_id,
unsigned char interface_id);
/** \brief Gets interface name.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param buffer[out] Buffer for the name. Can be NULL in which case
@param[out] buffer Buffer for the name. Can be NULL in which case
buffer_char_size will contain number of characters required for
the name.
@param buffer_char_size[in/out] On the way in supplies size (in characters)
@param[in,out] buffer_char_size On the way in supplies size (in characters)
of the buffer. On the way out, if method failed and GetLastError
reports ERROR_INSUFFICIENT_BUFFER, will contain number of characters
required for the name.
@param ansi[in] If 'true' the name will be returned as single character
@param[in] ansi If true the name will be returned as single character
string. Otherwise name will be returned as wide character string.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetInterfaceName(ADBAPIHANDLE adb_interface,
void* buffer,
unsigned long* buffer_char_size,
bool ansi);
/** \brief Gets serial number for interface's device.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param buffer[out] Buffer for the serail number string. Can be NULL in which
@param[out] buffer Buffer for the serail number string. Can be NULL in which
case buffer_char_size will contain number of characters required for
the string.
@param buffer_char_size[in/out] On the way in supplies size (in characters)
@param[in,out] buffer_char_size On the way in supplies size (in characters)
of the buffer. On the way out, if method failed and GetLastError
reports ERROR_INSUFFICIENT_BUFFER, will contain number of characters
required for the name.
@param ansi[in] If 'true' the name will be returned as single character
@param[in] ansi If true the name will be returned as single character
string. Otherwise name will be returned as wide character string.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
void* buffer,
unsigned long* buffer_char_size,
bool ansi);
@@ -257,99 +331,102 @@ ADBWIN_API bool AdbGetSerialNumber(ADBAPIHANDLE adb_interface,
/** \brief Gets device descriptor for the USB device associated with
the given interface.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param desc[out] Upon successful completion will have usb device
@param[out] desc Upon successful completion will have usb device
descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetUsbDeviceDescriptor(ADBAPIHANDLE adb_interface,
USB_DEVICE_DESCRIPTOR* desc);
/** \brief Gets descriptor for the selected USB device configuration.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param desc[out] Upon successful completion will have usb device
@param[out] desc Upon successful completion will have usb device
configuration descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetUsbConfigurationDescriptor(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetUsbConfigurationDescriptor(
ADBAPIHANDLE adb_interface,
USB_CONFIGURATION_DESCRIPTOR* desc);
/** \brief Gets descriptor for the given interface.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param desc[out] Upon successful completion will have usb device
@param[out] desc Upon successful completion will have usb device
configuration descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetUsbInterfaceDescriptor(ADBAPIHANDLE adb_interface,
USB_INTERFACE_DESCRIPTOR* desc);
/** \brief Gets information about an endpoint on the given interface.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param endpoint_index[in] Zero-based endpoint index. There are two
@param[in] endpoint_index Zero-based endpoint index. There are two
shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide information
about bulk write and bulk read endpoints respectively.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetEndpointInformation(ADBAPIHANDLE adb_interface,
unsigned char endpoint_index,
AdbEndpointInformation* info);
/** \brief
Gets information about default bulk read endpoint on the given interface.
/** \brief Gets information about default bulk read endpoint on the given
interface.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetDefaultBulkReadEndpointInformation(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetDefaultBulkReadEndpointInformation(
ADBAPIHANDLE adb_interface,
AdbEndpointInformation* info);
/** \brief
Gets information about default bulk write endpoint on the given interface.
/** \brief Gets information about default bulk write endpoint on the given
interface.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbGetDefaultBulkWriteEndpointInformation(ADBAPIHANDLE adb_interface,
ADBWIN_API bool __cdecl AdbGetDefaultBulkWriteEndpointInformation(
ADBAPIHANDLE adb_interface,
AdbEndpointInformation* info);
/** \brief Opens an endpoint on the given interface.
Endpoints are always opened for overlapped I/O.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param endpoint_index[in] Zero-based endpoint index. There are two
@param[in] endpoint_index Zero-based endpoint index. There are two
shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide information
about bulk write and bulk read endpoints respectively.
@param access_type[in] Desired access type. In the current implementation
@param[in] access_type Desired access type. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
@param[in] sharing_mode Desired share mode. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always shared for read / write.
@return Handle to the opened endpoint object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
ADBWIN_API ADBAPIHANDLE __cdecl AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
unsigned char endpoint_index,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode);
@@ -357,76 +434,78 @@ ADBWIN_API ADBAPIHANDLE AdbOpenEndpoint(ADBAPIHANDLE adb_interface,
/** \brief Opens default bulk read endpoint on the given interface.
Endpoints are always opened for overlapped I/O.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param access_type[in] Desired access type. In the current implementation
@param[in] access_type Desired access type. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
@param[in] sharing_mode Desired share mode. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always shared for read / write.
@return Handle to the opened endpoint object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbOpenDefaultBulkReadEndpoint(ADBAPIHANDLE adb_interface,
ADBWIN_API ADBAPIHANDLE __cdecl AdbOpenDefaultBulkReadEndpoint(
ADBAPIHANDLE adb_interface,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode);
/** \brief Opens default bulk write endpoint on the given interface.
Endpoints are always opened for overlapped I/O.
@param adb_interface[in] A handle to interface object created with
@param[in] adb_interface A handle to interface object created with
AdbCreateInterface call.
@param access_type[in] Desired access type. In the current implementation
@param[in] access_type Desired access type. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
@param[in] sharing_mode Desired share mode. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always shared for read / write.
@return Handle to the opened endpoint object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbOpenDefaultBulkWriteEndpoint(ADBAPIHANDLE adb_interface,
ADBWIN_API ADBAPIHANDLE __cdecl AdbOpenDefaultBulkWriteEndpoint(
ADBAPIHANDLE adb_interface,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode);
/** \brief Gets handle to interface object for the given endpoint
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@return Handle to the interface for this endpoint or NULL on failure. If NULL
is returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint);
ADBWIN_API ADBAPIHANDLE __cdecl AdbGetEndpointInterface(ADBAPIHANDLE adb_endpoint);
/** \brief Gets information about the given endpoint.
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
ADBWIN_API bool __cdecl AdbQueryInformationEndpoint(ADBAPIHANDLE adb_endpoint,
AdbEndpointInformation* info);
/** \brief Asynchronously reads from the given endpoint.
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@param buffer[out] Pointer to the buffer that receives the data.
@param bytes_to_read[in] Number of bytes to be read.
@param bytes_read[out] Number of bytes read. Can be NULL.
@param event_handle[in] Event handle that should be signaled when async I/O
@param[out] buffer Pointer to the buffer that receives the data.
@param[in] bytes_to_read Number of bytes to be read.
@param[out] bytes_read Number of bytes read. Can be NULL.
@param[in] event_handle Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value for this parameter means that there is no
timeout for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
ADBWIN_API ADBAPIHANDLE __cdecl AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_read,
unsigned long* bytes_read,
@@ -435,21 +514,21 @@ ADBWIN_API ADBAPIHANDLE AdbReadEndpointAsync(ADBAPIHANDLE adb_endpoint,
/** \brief Asynchronously writes to the given endpoint.
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@param buffer[in] Pointer to the buffer containing the data to be written.
@param bytes_to_write[in] Number of bytes to be written.
@param bytes_written[out] Number of bytes written. Can be NULL.
@param event_handle[in] Event handle that should be signaled when async I/O
@param[in] buffer Pointer to the buffer containing the data to be written.
@param[in] bytes_to_write Number of bytes to be written.
@param[out] bytes_written Number of bytes written. Can be NULL.
@param[in] event_handle Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value for this parameter means that there is no
timeout for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
ADBWIN_API ADBAPIHANDLE AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
ADBWIN_API ADBAPIHANDLE __cdecl AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_write,
unsigned long* bytes_written,
@@ -458,18 +537,18 @@ ADBWIN_API ADBAPIHANDLE AdbWriteEndpointAsync(ADBAPIHANDLE adb_endpoint,
/** \brief Synchronously reads from the given endpoint.
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@param buffer[out] Pointer to the buffer that receives the data.
@param bytes_to_read[in] Number of bytes to be read.
@param bytes_read[out] Number of bytes read. Can be NULL.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
@param[out] buffer Pointer to the buffer that receives the data.
@param[in] bytes_to_read Number of bytes to be read.
@param[out] bytes_read Number of bytes read. Can be NULL.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value for this parameter means that there is no
timeout for this I/O.
@return 'true' on success and 'false' on failure. If 'false' is
@return true on success and false on failure. If false is
returned GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
ADBWIN_API bool __cdecl AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_read,
unsigned long* bytes_read,
@@ -477,65 +556,64 @@ ADBWIN_API bool AdbReadEndpointSync(ADBAPIHANDLE adb_endpoint,
/** \brief Synchronously writes to the given endpoint.
@param adb_endpoint[in] A handle to opened endpoint object, obtained via one
@param[in] adb_endpoint A handle to opened endpoint object, obtained via one
of the AdbOpenXxxEndpoint calls.
@param buffer[in] Pointer to the buffer containing the data to be written.
@param bytes_to_write[in] Number of bytes to be written.
@param bytes_written[out] Number of bytes written. Can be NULL.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
@param[in] buffer Pointer to the buffer containing the data to be written.
@param[in] bytes_to_write Number of bytes to be written.
@param[out] bytes_written Number of bytes written. Can be NULL.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value for this parameter means that there is no
timeout for this I/O.
@return 'true' on success and 'false' on failure. If 'false' is
@return true on success and false on failure. If false is
returned GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
ADBWIN_API bool __cdecl AdbWriteEndpointSync(ADBAPIHANDLE adb_endpoint,
void* buffer,
unsigned long bytes_to_write,
unsigned long* bytes_written,
unsigned long time_out);
/** \brief Gets overlapped I/O result for async I/O performed on the
given endpoint
given endpoint.
@param adb_io_completion[in] A handle to an I/O completion object returned
@param[in] adb_io_completion A handle to an I/O completion object returned
from AdbRead/WriteAsync routines.
@param ovl_data[out] Buffer for the copy of this object's OVERLAPPED
@param[out] ovl_data Buffer for the copy of this object's OVERLAPPED
structure. Can be NULL.
@param bytes_transferred[out] Pointer to a variable that receives the
@param[out] bytes_transferred Pointer to a variable that receives the
number of bytes that were actually transferred by a read or write
operation. See SDK doc on GetOvelappedResult for more information.
Unlike regular GetOvelappedResult call this parameter can be NULL.
@param wait[in] If this parameter is 'true', the method does not return
until the operation has been completed. If this parameter is 'false'
and the operation is still pending, the method returns 'false' and
@param[in] wait If this parameter is true, the method does not return
until the operation has been completed. If this parameter is false
and the operation is still pending, the method returns false and
the GetLastError function returns ERROR_IO_INCOMPLETE.
@return 'true' if I/O has been completed or 'false' on failure or if request
is not yet completed. If 'false' is returned GetLastError() provides
@return true if I/O has been completed or false on failure or if request
is not yet completed. If false is returned GetLastError() provides
extended error information. If GetLastError returns
ERROR_IO_INCOMPLETE it means that I/O is not yet completed.
*/
ADBWIN_API bool AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
ADBWIN_API bool __cdecl AdbGetOvelappedIoResult(ADBAPIHANDLE adb_io_completion,
LPOVERLAPPED overlapped,
unsigned long* bytes_transferred,
bool wait);
/** \brief Checks if overlapped I/O has been completed.
@param adb_io_completion[in] A handle to an I/O completion object returned
@param[in] adb_io_completion A handle to an I/O completion object returned
from AdbRead/WriteAsync routines.
@return 'true' if I/O has been completed or 'false' if it's still
@return true if I/O has been completed or false if it's still
incomplete. Regardless of the returned value, caller should
check GetLastError to validate that handle was OK.
*/
ADBWIN_API bool AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion);
ADBWIN_API bool __cdecl AdbHasOvelappedIoComplated(ADBAPIHANDLE adb_io_completion);
/** \brief Closes handle previously opened with one of the API calls
@param adb_handle[in] ADB handle previously opened with one of the API calls
@return 'true' on success or 'false' on failure. If 'false' is returned
@param[in] adb_handle ADB handle previously opened with one of the API calls
@return true on success or false on failure. If false is returned
GetLastError() provides extended error information.
*/
ADBWIN_API bool AdbCloseHandle(ADBAPIHANDLE adb_handle);
ADBWIN_API bool __cdecl AdbCloseHandle(ADBAPIHANDLE adb_handle);
#endif // ANDROID_USB_API_ADBWINAPI_H__

View File

@@ -1,88 +0,0 @@
/*
* 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.
*/
#ifndef ANDROID_USB_API_ADB_API_EXTRA_H__
#define ANDROID_USB_API_ADB_API_EXTRA_H__
/** \file
This file consists of public API declarations that are also used by the
driver and as such cannot be declared in adb_api.h
*/
/** AdbEndpointType enumerates endpoint types. It enum is taken from
WDF_USB_PIPE_TYPE enum found in WDK.
*/
typedef enum _AdbEndpointType {
AdbEndpointTypeInvalid = 0,
AdbEndpointTypeControl,
AdbEndpointTypeIsochronous,
AdbEndpointTypeBulk,
AdbEndpointTypeInterrupt,
} AdbEndpointType;
/** Structure AdbEndpointInformation describes an endpoint. It is
based on WDF_USB_PIPE_INFORMATION structure found in WDK.
*/
typedef struct _AdbEndpointInformation {
/// Maximum packet size this endpoint is capable of
unsigned long max_packet_size;
// Maximum size of one transfer which should be sent to the host controller
unsigned long max_transfer_size;
// The type of the endpoint
AdbEndpointType endpoint_type;
/// Raw endpoint address of the device as described by its descriptor
unsigned char endpoint_address;
/// Polling interval
unsigned char polling_interval;
/// Which alternate setting this structure is relevant for
unsigned char setting_index;
} AdbEndpointInformation;
/// Shortcut to default write bulk endpoint in zero-based endpoint index API
#define ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX 0xFC
/// Shortcut to default read bulk endpoint in zero-based endpoint index API
#define ADB_QUERY_BULK_READ_ENDPOINT_INDEX 0xFE
// {F72FE0D4-CBCB-407d-8814-9ED673D0DD6B}
/// Our USB class id that driver uses to register our device
#define ANDROID_USB_CLASS_ID \
{0xf72fe0d4, 0xcbcb, 0x407d, {0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b}};
/// Defines vendor ID for the device
#define DEVICE_VENDOR_ID 0x0BB4
/// Defines product ID for the device with single interface.
#define DEVICE_SINGLE_PRODUCT_ID 0x0C01
/// Defines product ID for the composite device.
#define DEVICE_COMPOSITE_PRODUCT_ID 0x0C02
/// Defines interface ID for the device.
#define DEVICE_INTERFACE_ID 0x01
/// Defines vendor ID for the device
#define DEVICE_EMULATOR_VENDOR_ID 0x18D1
/// Defines product ID for a SoftUSB device simulator that is used to test
/// the driver in isolation from hardware.
#define DEVICE_EMULATOR_PROD_ID 0xDDDD
#endif // ANDROID_USB_API_ADB_API_EXTRA_H__

View File

@@ -22,8 +22,7 @@
#include "adb_api.h"
/** Class AdbInstanceEnumEntry encapsulates an entry in the array of
enumerated interfaces.
/** \brief Encapsulates an entry in the array of enumerated interfaces.
*/
class AdbInstanceEnumEntry {
public:
@@ -73,7 +72,7 @@ class AdbInstanceEnumEntry {
/** \brief Saves this entry into AdbInterfaceInfo structure.
@param info[in] Buffer to save this entry to. Must be big enough to fit it.
@param[in] info Buffer to save this entry to. Must be big enough to fit it.
Use GetFlatSize() method to get buffer size needed for that.
*/

View File

@@ -21,34 +21,201 @@
#include "stdafx.h"
#include "adb_endpoint_object.h"
#include "adb_io_completion.h"
#include "adb_helper_routines.h"
AdbEndpointObject::AdbEndpointObject(AdbInterfaceObject* parent_interf)
: AdbIOObject(parent_interf, AdbObjectTypeEndpoint) {
AdbEndpointObject::AdbEndpointObject(AdbInterfaceObject* parent_interf,
UCHAR endpoint_id,
UCHAR endpoint_index)
: AdbObjectHandle(AdbObjectTypeEndpoint),
parent_interface_(parent_interf),
endpoint_id_(endpoint_id),
endpoint_index_(endpoint_index) {
if (NULL != parent_interface_)
parent_interface_->AddRef();
}
AdbEndpointObject::~AdbEndpointObject() {
}
bool AdbEndpointObject::IsObjectOfType(AdbObjectType obj_type) const {
return ((obj_type == AdbObjectTypeEndpoint) ||
(obj_type == AdbObjectTypeIo));
if (NULL != parent_interface_)
parent_interface_->Release();
}
bool AdbEndpointObject::GetEndpointInformation(AdbEndpointInformation* info) {
if (!IsOpened() || !IsUsbOpened()) {
if (!IsOpened()) {
SetLastError(ERROR_INVALID_HANDLE);
return false;
}
// Send IOCTL
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_handle(),
ADB_IOCTL_GET_ENDPOINT_INFORMATION,
NULL, 0,
info, sizeof(AdbEndpointInformation),
&ret_bytes,
NULL);
ATLASSERT(!ret || (sizeof(AdbEndpointInformation) == ret_bytes));
return parent_interface()->GetEndpointInformation(endpoint_index(), info);
}
ADBAPIHANDLE AdbEndpointObject::AsyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
HANDLE event_handle,
ULONG time_out) {
return CommonAsyncReadWrite(true,
buffer,
bytes_to_read,
bytes_read,
event_handle,
time_out);
}
ADBAPIHANDLE AdbEndpointObject::AsyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
HANDLE event_handle,
ULONG time_out) {
return CommonAsyncReadWrite(false,
buffer,
bytes_to_write,
bytes_written,
event_handle,
time_out);
}
bool AdbEndpointObject::SyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
ULONG time_out) {
return CommonSyncReadWrite(true,
buffer,
bytes_to_read,
bytes_read,
time_out);
}
bool AdbEndpointObject::SyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
ULONG time_out) {
return CommonSyncReadWrite(false,
buffer,
bytes_to_write,
bytes_written,
time_out);
}
ADBAPIHANDLE AdbEndpointObject::CommonAsyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
HANDLE event_handle,
ULONG time_out) {
if (!SetTimeout(time_out))
return false;
// Create completion i/o object
AdbIOCompletion* adb_io_completion = NULL;
try {
adb_io_completion = new AdbIOCompletion(this,
bytes_to_transfer,
event_handle);
} catch (... ) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
// Create a handle for it
ADBAPIHANDLE ret = adb_io_completion->CreateHandle();
ULONG transferred = 0;
if (NULL != ret) {
BOOL res = TRUE;
// Go the read / write file way
res = is_read ?
WinUsb_ReadPipe(parent_interface()->winusb_handle(),
endpoint_id(),
reinterpret_cast<PUCHAR>(buffer),
bytes_to_transfer,
&transferred,
adb_io_completion->overlapped()) :
WinUsb_WritePipe(parent_interface()->winusb_handle(),
endpoint_id(),
reinterpret_cast<PUCHAR>(buffer),
bytes_to_transfer,
&transferred,
adb_io_completion->overlapped());
if (NULL != bytes_transferred)
*bytes_transferred = transferred;
ULONG error = GetLastError();
if (!res && (ERROR_IO_PENDING != error)) {
// I/O failed immediatelly. We need to close i/o completion object
// before we return NULL to the caller.
adb_io_completion->CloseHandle();
ret = NULL;
SetLastError(error);
}
}
// Offseting 'new'
adb_io_completion->Release();
return ret;
}
bool AdbEndpointObject::CommonSyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
ULONG time_out) {
if (!SetTimeout(time_out))
return false;
// This is synchronous I/O. Since we always open I/O items for
// overlapped I/O we're obligated to always provide OVERLAPPED
// structure to read / write routines. Prepare it now.
OVERLAPPED overlapped;
ZeroMemory(&overlapped, sizeof(overlapped));
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
BOOL ret = TRUE;
ULONG transferred = 0;
// Go the read / write file way
ret = is_read ?
WinUsb_ReadPipe(parent_interface()->winusb_handle(),
endpoint_id(),
reinterpret_cast<PUCHAR>(buffer),
bytes_to_transfer,
&transferred,
&overlapped) :
WinUsb_WritePipe(parent_interface()->winusb_handle(),
endpoint_id(),
reinterpret_cast<PUCHAR>(buffer),
bytes_to_transfer,
&transferred,
&overlapped);
// Lets see the result
if (!ret && (ERROR_IO_PENDING != GetLastError())) {
// I/O failed.
if (NULL != overlapped.hEvent)
::CloseHandle(overlapped.hEvent);
return false;
}
// Lets wait till I/O completes
ret = WinUsb_GetOverlappedResult(parent_interface()->winusb_handle(), &overlapped,
&transferred, TRUE);
if (ret && (NULL != bytes_transferred)) {
*bytes_transferred = transferred;
}
if (NULL != overlapped.hEvent)
::CloseHandle(overlapped.hEvent);
return ret ? true : false;
}
bool AdbEndpointObject::SetTimeout(ULONG timeout) {
if (!WinUsb_SetPipePolicy(parent_interface()->winusb_handle(),
endpoint_id(), PIPE_TRANSFER_TIMEOUT,
sizeof(ULONG), &timeout)) {
return false;
}
return true;
}

View File

@@ -21,26 +21,29 @@
handle opened to an endpoint on our device.
*/
#include "adb_io_object.h"
#include "adb_interface.h"
/** Class AdbEndpointObject encapsulates a handle opened to an endpoint on
our device.
*/
class AdbEndpointObject : public AdbIOObject {
class AdbEndpointObject : public AdbObjectHandle {
public:
/** \brief Constructs the object
@param interface[in] Parent interface for this object. Interface will be
@param[in] interface Parent interface for this object. Interface will be
referenced in this object's constructur and released in the
destructor.
@param obj_type[in] Object type from AdbObjectType enum
@param[in] endpoint_id Endpoint ID (endpoint address) on the device.
@param[in] endpoint_index Zero-based endpoint index in the interface's
array of endpoints.
*/
AdbEndpointObject(AdbInterfaceObject* parent_interf);
AdbEndpointObject(AdbInterfaceObject* parent_interf,
UCHAR endpoint_id,
UCHAR endpoint_index);
protected:
/** \brief Destructs the object.
parent_interface_ will be dereferenced here.
We hide destructor in order to prevent ourseves from accidentaly allocating
instances on the stack. If such attemp occur, compiler will error.
*/
@@ -49,23 +52,182 @@ class AdbEndpointObject : public AdbIOObject {
public:
/** \brief Gets information about this endpoint.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetEndpointInformation(AdbEndpointInformation* info);
/** \brief Checks if this object is of the given type
/** \brief Reads from opened I/O object asynchronously
@param obj_type[in] One of the AdbObjectType types to check
@return 'true' is this object type matches obj_type and 'false' otherwise.
@param[out] buffer Pointer to the buffer that receives the data.
@param[in] bytes_to_read Number of bytes to be read.
@param[out] bytes_read Number of bytes read. Can be NULL.
@param[in] event_handle Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual bool IsObjectOfType(AdbObjectType obj_type) const;
virtual ADBAPIHANDLE AsyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
HANDLE event_handle,
ULONG time_out);
// This is a helper for extracting object from the AdbObjectHandleMap
/** \brief Writes to opened I/O object asynchronously
@param[in] buffer Pointer to the buffer containing the data to be written.
@param[in] bytes_to_write Number of bytes to be written.
@param[out] bytes_written Number of bytes written. Can be NULL.
@param[in] event_handle Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual ADBAPIHANDLE AsyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
HANDLE event_handle,
ULONG time_out);
/** \brief Reads from opened I/O object synchronously
@param[out] buffer Pointer to the buffer that receives the data.
@param[in] bytes_to_read Number of bytes to be read.
@param[out] bytes_read Number of bytes read. Can be NULL.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return true on success and false on failure. If false is
returned GetLastError() provides extended error information.
*/
virtual bool SyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
ULONG time_out);
/** \brief Writes to opened I/O object synchronously
@param[in] buffer Pointer to the buffer containing the data to be written.
@param[in] bytes_to_write Number of bytes to be written.
@param[out] bytes_written Number of bytes written. Can be NULL.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return true on success and false on failure. If false is
returned GetLastError() provides extended error information.
*/
virtual bool SyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
ULONG time_out);
protected:
/** \brief Common code for async read / write
@param[in] is_read Read or write selector.
@param[in,out] buffer Pointer to the buffer for read / write.
@param[in] bytes_to_transfer Number of bytes to be read / written.
@param[out] bytes_transferred Number of bytes read / written. Can be NULL.
@param[in] event_handle Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual ADBAPIHANDLE CommonAsyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
HANDLE event_handle,
ULONG time_out);
/** \brief Common code for sync read / write
@param[in] is_read Read or write selector.
@param[in,out] buffer Pointer to the buffer for read / write.
@param[in] bytes_to_transfer Number of bytes to be read / written.
@param[out] bytes_transferred Number of bytes read / written. Can be NULL.
@param[in] time_out A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
virtual bool CommonSyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
ULONG time_out);
/** \brief Sets read / write operation timeout.
@param[in] timeout Timeout value in milliseconds to use for current read
or write operation. Zero value passed in this parameters indicate
not timeout at all. Note that timeout that is set with this method is
global per endpoint (pipe). I.e. once set, it will be used against
all read / write operations performed on this endpoint, untill
another call to this method modifies it. This is a WinUsb design
flaw. Microsoft is aware of this and (hopefuly) future versions of
WinUsb framework will accept a timeout parameter in WinUsb_Read/Write
routines. For the purposes of ADB this flaw doesn't apperar to be an
issue, since we use single-threaded synchronous read / writes, so
there is no conflict in setting per-endpoint timeouts.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
virtual bool SetTimeout(ULONG timeout);
public:
/// This is a helper for extracting object from the AdbObjectHandleMap
static AdbObjectType Type() {
return AdbObjectTypeEndpoint;
}
/// Gets parent interface
AdbInterfaceObject* parent_interface() const {
return parent_interface_;
}
/// Gets this endpoint ID
UCHAR endpoint_id() const {
return endpoint_id_;
}
/// Gets this endpoint index on the interface
UCHAR endpoint_index() const {
return endpoint_index_;
}
/// Gets parent interface handle
ADBAPIHANDLE GetParentInterfaceHandle() const {
return (NULL != parent_interface()) ? parent_interface()->adb_handle() :
NULL;
}
/// Gets parent interface WinUsb handle
WINUSB_INTERFACE_HANDLE winusb_handle() const {
return parent_interface()->winusb_handle();
}
protected:
/// Parent interface
AdbInterfaceObject* parent_interface_;
/// This endpoint id
UCHAR endpoint_id_;
/// This endpoint index on the interface
UCHAR endpoint_index_;
};
#endif // ANDROID_USB_API_ADB_ENDPOINT_OBJECT_H__

View File

@@ -24,65 +24,6 @@
#include "adb_helper_routines.h"
#include "adb_interface_enum.h"
bool GetSDKComplientParam(AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode,
ULONG* desired_access,
ULONG* desired_sharing) {
if (NULL != desired_access) {
switch (access_type) {
case AdbOpenAccessTypeReadWrite:
*desired_access = GENERIC_READ | GENERIC_WRITE;
break;
case AdbOpenAccessTypeRead:
*desired_access = GENERIC_READ;
break;
case AdbOpenAccessTypeWrite:
*desired_access = GENERIC_WRITE;
break;
case AdbOpenAccessTypeQueryInfo:
*desired_access = FILE_READ_ATTRIBUTES | FILE_READ_EA;
break;
default:
AtlTrace("\n!!!!! ADB API -> GetSDKComplientParam %u is unknown access type",
access_type);
SetLastError(ERROR_INVALID_ACCESS);
return false;
}
}
if (NULL != desired_sharing) {
switch (sharing_mode) {
case AdbOpenSharingModeReadWrite:
*desired_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
break;
case AdbOpenSharingModeRead:
*desired_sharing = FILE_SHARE_READ;
break;
case AdbOpenSharingModeWrite:
*desired_sharing = FILE_SHARE_WRITE;
break;
case AdbOpenSharingModeExclusive:
*desired_sharing = 0;
break;
default:
AtlTrace("\n!!!!! ADB API -> GetSDKComplientParam %u is unknown share mode",
sharing_mode);
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
}
return true;
}
bool EnumerateDeviceInterfaces(HDEVINFO hardware_dev_info,
GUID class_id,
bool exclude_removed,

View File

@@ -23,34 +23,17 @@
#include "adb_api_private_defines.h"
/** \brief Converts access type and share mode from our enum into
SDK - complient values.
/** \brief Given the hardware device information enumerates interfaces for
this device.
@param access_type[in] Enumerated access type
@param sharing_mode[in] Enumerated share mode
@param desired_access[out] Will receive SDK - complient desired access
flags. This parameter can be NULL.
@param desired_sharing[out] Will receive SDK - complient share mode.
This parameter can be NULL.
@return True on success, false on failure, in which case GetLastError()
provides extended information about the error that occurred.
*/
bool GetSDKComplientParam(AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode,
ULONG* desired_access,
ULONG* desired_sharing);
/** \brief
Given the hardware device information enumerates interfaces for this device
@param hardware_dev_info[in] A handle to hardware device information obtained
@param[in] hardware_dev_info A handle to hardware device information obtained
from PnP manager via SetupDiGetClassDevs()
@param class_id[in] Device class ID how it is specified by our USB driver
@param exclude_removed[in] If true interfaces with SPINT_REMOVED flag set
@param[in] class_id Device class ID how it is specified by our USB driver.
@param[in] exclude_removed If true interfaces with SPINT_REMOVED flag set
will be not included in the enumeration.
@param active_only[in] If 'true' only active interfaces (with flag
@param[in] active_only If true only active interfaces (with flag
SPINT_ACTIVE set) will be included in the enumeration.
@param interfaces[out] Upon successfull completion will consist of array of
@param[out] interfaces Upon successfull completion will consist of array of
all interfaces found for this device (matching all filters).
@return True on success, false on failure, in which case GetLastError()
provides extended information about the error that occurred.
@@ -61,18 +44,18 @@ bool EnumerateDeviceInterfaces(HDEVINFO hardware_dev_info,
bool active_only,
AdbEnumInterfaceArray* interfaces);
/** \brief Enumerates all interfaces for our device class
/** \brief Enumerates all interfaces for our device class.
This routine uses SetupDiGetClassDevs to get our device info and calls
EnumerateDeviceInterfaces to perform the enumeration.
@param class_id[in] Device class ID how it is specified by our USB driver
@param flags[in] Flags to pass to SetupDiGetClassDevs to filter devices. See
@param[in] class_id Device class ID how it is specified by our USB driver
@param[in] flags Flags to pass to SetupDiGetClassDevs to filter devices. See
SetupDiGetClassDevs() in SDK for more info on these flags.
@param exclude_removed[in] If true interfaces with SPINT_REMOVED flag set
@param[in] exclude_removed If true interfaces with SPINT_REMOVED flag set
will be not included in the enumeration.
@param active_only[in] If 'true' only active interfaces (with flag
@param[in] active_only If true only active interfaces (with flag
SPINT_ACTIVE set) will be included in the enumeration.
@param interfaces[out] Upon successfull completion will consist of array of
@param[out] interfaces Upon successfull completion will consist of array of
all interfaces found for this device (matching all filters).
@return True on success, false on failure, in which case GetLastError()
provides extended information about the error that occurred.
@@ -83,16 +66,16 @@ bool EnumerateDeviceInterfaces(GUID class_id,
bool active_only,
AdbEnumInterfaceArray* interfaces);
/** \brief Given the hardware device information and data gets data details
/** \brief Given the hardware device information and data gets data details.
Given the hardware_dev_info, representing a handle to the plug and
play information, and dev_info_data, representing a specific usb device,
gets detailed data about the device (interface).
@param hardware_dev_info[in] A handle to hardware device information obtained
@param[in] hardware_dev_info A handle to hardware device information obtained
from PnP manager via SetupDiGetClassDevs()
@param dev_info_data[in] Device information data obtained via call to
@param[in] dev_info_data Device information data obtained via call to
SetupDiEnumDeviceInterfaces()
@param dev_info_detail_data[out] Upon successfull completion will consist of
@param[out] dev_info_detail_data Upon successfull completion will consist of
the detailed data about device interface. This routine always
allocates memory for the output structure so content of this pointer
doesn't matter and will be overwritten by this routine. The caller
@@ -111,11 +94,11 @@ bool GetUsbDeviceDetails(HDEVINFO hardware_dev_info,
play information, and dev_info_data, representing a specific usb device,
gets device name. This routine uses GetUsbDeviceDetails to extract device
name.
@param hardware_dev_info[in] A handle to hardware device information obtained
@param[in] hardware_dev_info A handle to hardware device information obtained
from PnP manager via SetupDiGetClassDevs()
@param dev_info_data[in] Device information data obtained via call to
@param[in] dev_info_data Device information data obtained via call to
SetupDiEnumDeviceInterfaces()
@param name[out] Upon successfull completion will have name for the device.
@param[out] name Upon successfull completion will have name for the device.
@return True on success, false on failure, in which case GetLastError()
provides extended information about the error that occurred.
*/

View File

@@ -25,51 +25,118 @@
AdbInterfaceObject::AdbInterfaceObject(const wchar_t* interf_name)
: AdbObjectHandle(AdbObjectTypeInterface),
interface_name_(interf_name) {
interface_name_(interf_name),
usb_device_handle_(INVALID_HANDLE_VALUE),
winusb_handle_(NULL),
interface_number_(0xFF),
def_read_endpoint_(0xFF),
read_endpoint_id_(0xFF),
def_write_endpoint_(0xFF),
write_endpoint_id_(0xFF) {
ATLASSERT(NULL != interf_name);
}
AdbInterfaceObject::~AdbInterfaceObject() {
ATLASSERT(NULL == winusb_handle_);
ATLASSERT(INVALID_HANDLE_VALUE == usb_device_handle_);
}
ADBAPIHANDLE AdbInterfaceObject::CreateHandle() {
// Open USB device for this intefface
HANDLE usb_device_handle = CreateFile(interface_name().c_str(),
// Open USB device for this inteface Note that WinUsb API
// requires the handle to be opened for overlapped I/O.
usb_device_handle_ = CreateFile(interface_name().c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == usb_device_handle)
NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
if (INVALID_HANDLE_VALUE == usb_device_handle_)
return NULL;
// Now, we ensured that our usb device / interface is up and running.
// Lets collect device, interface and pipe information
bool ok = true;
if (!CacheUsbDeviceDescriptor(usb_device_handle) ||
!CacheUsbConfigurationDescriptor(usb_device_handle) ||
!CacheUsbInterfaceDescriptor(usb_device_handle)) {
ok = false;
}
// Initialize WinUSB API for this interface
if (!WinUsb_Initialize(usb_device_handle_, &winusb_handle_))
return NULL;
// Preserve error accross handle close
ULONG error = ok ? NO_ERROR : GetLastError();
::CloseHandle(usb_device_handle);
if (NO_ERROR != error)
SetLastError(error);
if (!ok)
// Cache current interface number that will be used in
// WinUsb_Xxx calls performed on this interface.
if (!WinUsb_GetCurrentAlternateSetting(winusb_handle(), &interface_number_))
return false;
// Cache interface properties
unsigned long bytes_written;
// Cache USB device descriptor
if (!WinUsb_GetDescriptor(winusb_handle(), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0,
reinterpret_cast<PUCHAR>(&usb_device_descriptor_),
sizeof(usb_device_descriptor_), &bytes_written)) {
return false;
}
// Cache USB configuration descriptor
if (!WinUsb_GetDescriptor(winusb_handle(), USB_CONFIGURATION_DESCRIPTOR_TYPE,
0, 0,
reinterpret_cast<PUCHAR>(&usb_config_descriptor_),
sizeof(usb_config_descriptor_), &bytes_written)) {
return false;
}
// Cache USB interface descriptor
if (!WinUsb_QueryInterfaceSettings(winusb_handle(), interface_number(),
&usb_interface_descriptor_)) {
return false;
}
// Save indexes and IDs for bulk read / write endpoints. We will use them to
// convert ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX and
// ADB_QUERY_BULK_READ_ENDPOINT_INDEX into actual endpoint indexes and IDs.
for (UCHAR endpoint = 0; endpoint < usb_interface_descriptor_.bNumEndpoints;
endpoint++) {
// Get endpoint information
WINUSB_PIPE_INFORMATION pipe_info;
if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint,
&pipe_info)) {
return false;
}
if (UsbdPipeTypeBulk == pipe_info.PipeType) {
// This is a bulk endpoint. Cache its index and ID.
if (0 != (pipe_info.PipeId & USB_ENDPOINT_DIRECTION_MASK)) {
// Use this endpoint as default bulk read endpoint
ATLASSERT(0xFF == def_read_endpoint_);
def_read_endpoint_ = endpoint;
read_endpoint_id_ = pipe_info.PipeId;
} else {
// Use this endpoint as default bulk write endpoint
ATLASSERT(0xFF == def_write_endpoint_);
def_write_endpoint_ = endpoint;
write_endpoint_id_ = pipe_info.PipeId;
}
}
}
return AdbObjectHandle::CreateHandle();
}
bool AdbInterfaceObject::CloseHandle() {
if (NULL != winusb_handle_) {
WinUsb_Free(winusb_handle_);
winusb_handle_ = NULL;
}
if (INVALID_HANDLE_VALUE != usb_device_handle_) {
::CloseHandle(usb_device_handle_);
usb_device_handle_ = INVALID_HANDLE_VALUE;
}
return AdbObjectHandle::CloseHandle();
}
bool AdbInterfaceObject::GetInterfaceName(void* buffer,
unsigned long* buffer_char_size,
bool ansi) {
if (NULL == buffer_char_size) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
// Lets see if buffer is big enough
ULONG name_len = static_cast<ULONG>(interface_name_.length() + 1);
if ((NULL == buffer) || (*buffer_char_size < name_len)) {
@@ -104,63 +171,91 @@ bool AdbInterfaceObject::GetSerialNumber(void* buffer,
return false;
}
// Open USB device for this intefface
HANDLE usb_device_handle = CreateFile(interface_name().c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == usb_device_handle)
return NULL;
WCHAR serial_number[512];
// Send IOCTL
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_device_handle,
ADB_IOCTL_GET_SERIAL_NUMBER,
NULL, 0,
serial_number, sizeof(serial_number),
&ret_bytes,
NULL);
// Preserve error accross CloseHandle
ULONG error = ret ? NO_ERROR : GetLastError();
::CloseHandle(usb_device_handle);
if (NO_ERROR != error) {
SetLastError(error);
if (NULL == buffer_char_size) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
unsigned long str_len =
static_cast<unsigned long>(wcslen(serial_number) + 1);
// Calculate serial number string size. Note that WinUsb_GetDescriptor
// API will not return number of bytes needed to store serial number
// string. So we will have to start with a reasonably large preallocated
// buffer and then loop through WinUsb_GetDescriptor calls, doubling up
// string buffer size every time ERROR_INSUFFICIENT_BUFFER is returned.
union {
// Preallocate reasonably sized buffer on the stack.
char small_buffer[64];
USB_STRING_DESCRIPTOR initial_ser_num;
};
USB_STRING_DESCRIPTOR* ser_num = &initial_ser_num;
// Buffer byte size
unsigned long ser_num_size = sizeof(small_buffer);
// After successful call to WinUsb_GetDescriptor will contain serial
// number descriptor size.
unsigned long bytes_written;
while (!WinUsb_GetDescriptor(winusb_handle(), USB_STRING_DESCRIPTOR_TYPE,
usb_device_descriptor_.iSerialNumber,
0x0409, // English (US)
reinterpret_cast<PUCHAR>(ser_num),
ser_num_size, &bytes_written)) {
// Any error other than ERROR_INSUFFICIENT_BUFFER is terminal here.
if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) {
if (ser_num != &initial_ser_num)
delete[] reinterpret_cast<char*>(ser_num);
return false;
}
if ((NULL == buffer) || (*buffer_char_size < str_len)) {
*buffer_char_size = str_len;
// Double up buffer size and reallocate string buffer
ser_num_size *= 2;
if (ser_num != &initial_ser_num)
delete[] reinterpret_cast<char*>(ser_num);
try {
ser_num =
reinterpret_cast<USB_STRING_DESCRIPTOR*>(new char[ser_num_size]);
} catch (...) {
SetLastError(ERROR_OUTOFMEMORY);
return false;
}
}
// Serial number string length
unsigned long str_len = (ser_num->bLength -
FIELD_OFFSET(USB_STRING_DESCRIPTOR, bString)) /
sizeof(wchar_t);
// Lets see if requested buffer is big enough to fit the string
if ((NULL == buffer) || (*buffer_char_size < (str_len + 1))) {
// Requested buffer is too small.
if (ser_num != &initial_ser_num)
delete[] reinterpret_cast<char*>(ser_num);
*buffer_char_size = str_len + 1;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return false;
}
if (!ansi) {
// If user asked for wide char name just return it
wcscpy(reinterpret_cast<wchar_t*>(buffer), serial_number);
return true;
}
bool ret = true;
if (ansi) {
// We need to convert name from wide char to ansi string
int res = WideCharToMultiByte(CP_ACP,
0,
serial_number,
if (0 != WideCharToMultiByte(CP_ACP, 0, ser_num->bString,
static_cast<int>(str_len),
reinterpret_cast<PSTR>(buffer),
static_cast<int>(*buffer_char_size),
NULL,
NULL);
return (res != 0);
NULL, NULL)) {
// Zero-terminate output string.
reinterpret_cast<char*>(buffer)[str_len] = '\0';
} else {
ret = false;
}
} else {
// For wide char output just copy string buffer,
// and zero-terminate output string.
CopyMemory(buffer, ser_num->bString, bytes_written);
reinterpret_cast<wchar_t*>(buffer)[str_len] = L'\0';
}
if (ser_num != &initial_ser_num)
delete[] reinterpret_cast<char*>(ser_num);
return ret;
}
bool AdbInterfaceObject::GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR* desc) {
@@ -169,6 +264,11 @@ bool AdbInterfaceObject::GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR* desc) {
return false;
}
if (NULL == desc) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
CopyMemory(desc, usb_device_descriptor(), sizeof(USB_DEVICE_DESCRIPTOR));
return true;
@@ -181,6 +281,11 @@ bool AdbInterfaceObject::GetUsbConfigurationDescriptor(
return false;
}
if (NULL == desc) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
CopyMemory(desc, usb_config_descriptor(),
sizeof(USB_CONFIGURATION_DESCRIPTOR));
@@ -194,6 +299,11 @@ bool AdbInterfaceObject::GetUsbInterfaceDescriptor(
return false;
}
if (NULL == desc) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
CopyMemory(desc, usb_interface_descriptor(), sizeof(USB_INTERFACE_DESCRIPTOR));
return true;
@@ -206,71 +316,81 @@ bool AdbInterfaceObject::GetEndpointInformation(UCHAR endpoint_index,
return false;
}
// Open USB device for this intefface
HANDLE usb_device_handle = CreateFile(interface_name().c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == usb_device_handle)
return NULL;
if (NULL == info) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
// Init ICTL param
AdbQueryEndpointInformation param;
param.endpoint_index = endpoint_index;
// Get actual endpoint index for predefined read / write endpoints.
if (ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) {
endpoint_index = def_read_endpoint_;
} else if (ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) {
endpoint_index = def_write_endpoint_;
}
// Send IOCTL
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_device_handle,
ADB_IOCTL_GET_ENDPOINT_INFORMATION,
&param, sizeof(param),
info, sizeof(AdbEndpointInformation),
&ret_bytes,
NULL);
ATLASSERT(!ret || (sizeof(AdbEndpointInformation) == ret_bytes));
// Query endpoint information
WINUSB_PIPE_INFORMATION pipe_info;
if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint_index,
&pipe_info)) {
return false;
}
// Preserve error accross CloseHandle
ULONG error = ret ? NO_ERROR : GetLastError();
// Save endpoint information into output.
info->max_packet_size = pipe_info.MaximumPacketSize;
info->max_transfer_size = 0xFFFFFFFF;
info->endpoint_address = pipe_info.PipeId;
info->polling_interval = pipe_info.Interval;
info->setting_index = interface_number();
switch (pipe_info.PipeType) {
case UsbdPipeTypeControl:
info->endpoint_type = AdbEndpointTypeControl;
break;
::CloseHandle(usb_device_handle);
case UsbdPipeTypeIsochronous:
info->endpoint_type = AdbEndpointTypeIsochronous;
break;
if (NO_ERROR != error)
SetLastError(error);
case UsbdPipeTypeBulk:
info->endpoint_type = AdbEndpointTypeBulk;
break;
return ret ? true : false;
case UsbdPipeTypeInterrupt:
info->endpoint_type = AdbEndpointTypeInterrupt;
break;
default:
info->endpoint_type = AdbEndpointTypeInvalid;
break;
}
return true;
}
ADBAPIHANDLE AdbInterfaceObject::OpenEndpoint(
UCHAR endpoint_index,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode) {
// Convert index into name
std::wstring endpoint_name;
// Convert index into id
UCHAR endpoint_id;
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;
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 {
wchar_t fmt[265];
swprintf(fmt, L"%ws%u", DEVICE_PIPE_NAME_PREFIX, endpoint_index);
endpoint_name = fmt;
}
} catch (...) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
return OpenEndpoint(endpoint_name.c_str(), access_type, sharing_mode);
return OpenEndpoint(endpoint_id, endpoint_index);
}
ADBAPIHANDLE AdbInterfaceObject::OpenEndpoint(
const wchar_t* endpoint_name,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode) {
ADBAPIHANDLE AdbInterfaceObject::OpenEndpoint(UCHAR endpoint_id,
UCHAR endpoint_index) {
if (!IsOpened()) {
SetLastError(ERROR_INVALID_HANDLE);
return false;
@@ -279,66 +399,15 @@ ADBAPIHANDLE AdbInterfaceObject::OpenEndpoint(
AdbEndpointObject* adb_endpoint = NULL;
try {
adb_endpoint = new AdbEndpointObject(this);
adb_endpoint = new AdbEndpointObject(this, endpoint_id, endpoint_index);
} catch (...) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
// Build full path to the object
std::wstring endpoint_path = interface_name();
endpoint_path += L"\\";
endpoint_path += endpoint_name;
ADBAPIHANDLE ret = adb_endpoint->CreateHandle(endpoint_path.c_str(),
access_type,
sharing_mode);
ADBAPIHANDLE ret = adb_endpoint->CreateHandle();
adb_endpoint->Release();
return ret;
}
bool AdbInterfaceObject::CacheUsbDeviceDescriptor(HANDLE usb_device_handle) {
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_device_handle,
ADB_IOCTL_GET_USB_DEVICE_DESCRIPTOR,
NULL, 0,
&usb_device_descriptor_,
sizeof(usb_device_descriptor_),
&ret_bytes,
NULL);
ATLASSERT(!ret || (sizeof(USB_DEVICE_DESCRIPTOR) == ret_bytes));
return ret ? true : false;
}
bool AdbInterfaceObject::CacheUsbConfigurationDescriptor(
HANDLE usb_device_handle) {
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_device_handle,
ADB_IOCTL_GET_USB_CONFIGURATION_DESCRIPTOR,
NULL, 0,
&usb_config_descriptor_,
sizeof(usb_config_descriptor_),
&ret_bytes,
NULL);
ATLASSERT(!ret || (sizeof(USB_CONFIGURATION_DESCRIPTOR) == ret_bytes));
return ret ? true : false;
}
bool AdbInterfaceObject::CacheUsbInterfaceDescriptor(
HANDLE usb_device_handle) {
DWORD ret_bytes = 0;
BOOL ret = DeviceIoControl(usb_device_handle,
ADB_IOCTL_GET_USB_INTERFACE_DESCRIPTOR,
NULL, 0,
&usb_interface_descriptor_,
sizeof(usb_interface_descriptor_),
&ret_bytes,
NULL);
ATLASSERT(!ret || (sizeof(USB_INTERFACE_DESCRIPTOR) == ret_bytes));
return ret ? true : false;
}

View File

@@ -23,13 +23,13 @@
#include "adb_object_handle.h"
/** Class AdbInterfaceObject encapsulates an interface on our USB device.
/** \brief Encapsulates an interface on our USB device.
*/
class AdbInterfaceObject : public AdbObjectHandle {
public:
/** \brief Constructs the object
/** \brief Constructs the object.
@param interf_name[in] Name of the interface
@param[in] interf_name Name of the interface
*/
explicit AdbInterfaceObject(const wchar_t* interf_name);
@@ -42,13 +42,16 @@ class AdbInterfaceObject : public AdbObjectHandle {
virtual ~AdbInterfaceObject();
public:
/** \brief Creates handle to this object
/** \brief Creates handle to this object.
In this call a handle for this object is generated and object is added
to the AdbObjectHandleMap. We override this method in order to verify that
interface indeed exists and gather device, interface and pipe properties.
If this step succeeds then and only then AdbObjectHandle::CreateHandle
will be called.
will be called. Note that in this method we will open a handle to the
USB device (saved in usb_device_handle_). The handle will be opened for
read and write access, and for read and write sharing mode. The handle
will be closed in CloseHandle method of this class.
@return A handle to this object on success or NULL on an error.
If NULL is returned GetLastError() provides extended error
information. ERROR_GEN_FAILURE is set if an attempt was
@@ -56,18 +59,29 @@ class AdbInterfaceObject : public AdbObjectHandle {
*/
virtual ADBAPIHANDLE CreateHandle();
/** \brief This method is called when handle to this object gets closed.
In this call object is deleted from the AdbObjectHandleMap. We override
this method in order close device and WinUsb handles created in
CreateHandle method of this class.
@return true on success or false if object is already closed. If
false is returned GetLastError() provides extended error
information.
*/
virtual bool CloseHandle();
/** \brief Gets interface device name.
@param buffer[out] Buffer for the name. Can be NULL in which case
@param[out] buffer Buffer for the name. Can be NULL in which case
buffer_char_size will contain number of characters required to fit
the name.
@param buffer_char_size[in/out] On the way in supplies size (in characters)
@param[in,out] buffer_char_size On the way in supplies size (in characters)
of the buffer. On the way out if method failed and GetLastError
reports ERROR_INSUFFICIENT_BUFFER will contain number of characters
required to fit the name.
@param ansi[in] If true the name will be returned as single character
@param[in] ansi If true the name will be returned as single character
string. Otherwise name will be returned as wide character string.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetInterfaceName(void* buffer,
@@ -76,16 +90,16 @@ class AdbInterfaceObject : public AdbObjectHandle {
/** \brief Gets serial number for interface's device.
@param buffer[out] Buffer for the serail number string. Can be NULL in
@param[out] buffer Buffer for the serail number string. Can be NULL in
which case buffer_char_size will contain number of characters
required for the string.
@param buffer_char_size[in/out] On the way in supplies size (in characters)
@param[in,out] buffer_char_size On the way in supplies size (in characters)
of the buffer. On the way out, if method failed and GetLastError
reports ERROR_INSUFFICIENT_BUFFER, will contain number of characters
required for the name.
@param ansi[in] If 'true' the name will be returned as single character
@param[in] ansi If true the name will be returned as single character
string. Otherwise name will be returned as wide character string.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetSerialNumber(void* buffer,
@@ -95,53 +109,54 @@ class AdbInterfaceObject : public AdbObjectHandle {
/** \brief Gets device descriptor for the USB device associated with
this interface.
@param desc[out] Upon successful completion will have usb device
@param[out] desc Upon successful completion will have usb device
descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetUsbDeviceDescriptor(USB_DEVICE_DESCRIPTOR* desc);
/** \brief Gets descriptor for the selected USB device configuration.
@param desc[out] Upon successful completion will have usb device
@param[out] desc Upon successful completion will have usb device
configuration descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetUsbConfigurationDescriptor(USB_CONFIGURATION_DESCRIPTOR* desc);
/** \brief Gets descriptor for this interface.
@param desc[out] Upon successful completion will have interface
@param[out] desc Upon successful completion will have interface
descriptor.
@return 'true' on success, 'false' on failure. If 'false' is returned
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetUsbInterfaceDescriptor(USB_INTERFACE_DESCRIPTOR* desc);
/** \brief Gets information about an endpoint on this interface.
@param endpoint_index[in] Zero-based endpoint index. There are two
@param[in] endpoint_index Zero-based endpoint index. There are two
shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about
(default?) bulk write and read endpoints respectively.
@param info[out] Upon successful completion will have endpoint information.
@return 'true' on success, 'false' on failure. If 'false' is returned
@param[out] info Upon successful completion will have endpoint information.
@return true on success, false on failure. If false is returned
GetLastError() provides extended error information.
*/
bool GetEndpointInformation(UCHAR endpoint_index, AdbEndpointInformation* info);
bool GetEndpointInformation(UCHAR endpoint_index,
AdbEndpointInformation* info);
/** \brief Opens an endpoint on this interface.
@param endpoint_index[in] Zero-based endpoint index. There are two
@param[in] endpoint_index Zero-based endpoint index. There are two
shortcuts for this parameter: ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX
and ADB_QUERY_BULK_READ_ENDPOINT_INDEX that provide infor about
(default?) bulk write and read endpoints respectively.
@param access_type[in] Desired access type. In the current implementation
@param[in] access_type Desired access type. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
@param[in] sharing_mode Desired share mode. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always shared for read / write.
@return Handle to the opened endpoint object or NULL on failure.
@@ -154,52 +169,13 @@ class AdbInterfaceObject : public AdbObjectHandle {
/** \brief Opens an endpoint on this interface.
@param endpoint_name[in] Endpoint file name.
@param access_type[in] Desired access type. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
this parameter has no effect on the way endpoint is opened. It's
always shared for read / write.
@param[in] endpoint_id Endpoint (pipe) address on the device.
@param[in] endpoint_index Zero-based endpoint index.
@return Handle to the opened endpoint object or NULL on failure.
If NULL is returned GetLastError() provides extended information
about the error that occurred.
*/
ADBAPIHANDLE OpenEndpoint(const wchar_t* endpoint_name,
AdbOpenAccessType access_type,
AdbOpenSharingMode sharing_mode);
private:
/** \brief Caches device descriptor for the USB device associated with
this interface.
This method is called from CreateHandle method to cache some interface
information.
@param usb_device_handle[in] Handle to USB device.
@return 'true' on success, 'false' on failure. If 'false' is returned
GetLastError() provides extended error information.
*/
bool CacheUsbDeviceDescriptor(HANDLE usb_device_handle);
/** \brief Caches descriptor for the selected USB device configuration.
This method is called from CreateHandle method to cache some interface
information.
@param usb_device_handle[in] Handle to USB device.
@return 'true' on success, 'false' on failure. If 'false' is returned
GetLastError() provides extended error information.
*/
bool CacheUsbConfigurationDescriptor(HANDLE usb_device_handle);
/** \brief Caches descriptor for this interface.
This method is called from CreateHandle method to cache some interface
information.
@param usb_device_handle[in] Handle to USB device.
@return 'true' on success, 'false' on failure. If 'false' is returned
GetLastError() provides extended error information.
*/
bool CacheUsbInterfaceDescriptor(HANDLE usb_device_handle);
ADBAPIHANDLE OpenEndpoint(UCHAR endpoint_id, UCHAR endpoint_index);
public:
/// Gets name of the USB interface (device name) for this object
@@ -207,7 +183,7 @@ class AdbInterfaceObject : public AdbObjectHandle {
return interface_name_;
}
// This is a helper for extracting object from the AdbObjectHandleMap
/// This is a helper for extracting object from the AdbObjectHandleMap
static AdbObjectType Type() {
return AdbObjectTypeInterface;
}
@@ -227,6 +203,21 @@ class AdbInterfaceObject : public AdbObjectHandle {
return &usb_interface_descriptor_;
}
/// Gets handle to the USB device
HANDLE usb_device_handle() const {
return usb_device_handle_;
}
/// Gets interface handle used by WinUSB API
WINUSB_INTERFACE_HANDLE winusb_handle() const {
return winusb_handle_;
}
/// Gets current interface number.
UCHAR interface_number() const {
return interface_number_;
}
private:
/// Name of the USB interface (device name) for this object
std::wstring interface_name_;
@@ -239,6 +230,29 @@ private:
/// Cached usb interface descriptor
USB_INTERFACE_DESCRIPTOR usb_interface_descriptor_;
/// Handle to the USB device
HANDLE usb_device_handle_;
/// Interface handle used by WinUSB API
WINUSB_INTERFACE_HANDLE winusb_handle_;
/// Current interface number. This value is obtained via call to
/// WinUsb_GetCurrentAlternateSetting and is used in WinUsb_Xxx
/// calls that require interface number.
UCHAR interface_number_;
/// Index for the default bulk read endpoint
UCHAR def_read_endpoint_;
/// ID for the default bulk read endpoint
UCHAR read_endpoint_id_;
/// Index for the default bulk write endpoint
UCHAR def_write_endpoint_;
/// ID for the default bulk write endpoint
UCHAR write_endpoint_id_;
};
#endif // ANDROID_USB_API_ADB_INTERFACE_H__

View File

@@ -23,8 +23,7 @@
#include "adb_object_handle.h"
/** Class AdbInterfaceEnumObject encapsulates enumerator of USB
interfaces available through this API.
/** \brief Enumerator of USB interfaces available through this API.
*/
class AdbInterfaceEnumObject : public AdbObjectHandle {
public:
@@ -41,16 +40,16 @@ class AdbInterfaceEnumObject : public AdbObjectHandle {
virtual ~AdbInterfaceEnumObject();
public:
/** \brief Enumerates all interfaces for our device class
/** \brief Enumerates all interfaces for the given device class.
This routine uses SetupDiGetClassDevs to get our device info and calls
EnumerateDeviceInterfaces to perform the enumeration.
@param class_id[in] Device class ID that is specified by our USB driver
@param exclude_not_present[in] If set include only those devices that are
@param[in] class_id Device class ID that is specified by our USB driver
@param[in] exclude_not_present If set include only those devices that are
currently present.
@param exclude_removed[in] If true interfaces with SPINT_REMOVED flag set
@param[in] exclude_removed If true interfaces with SPINT_REMOVED flag set
will be not included in the enumeration.
@param active_only[in] If 'true' only active interfaces (with flag
@param[in] active_only If true only active interfaces (with flag
SPINT_ACTIVE set) will be included in the enumeration.
@return True on success, false on failure, in which case GetLastError()
provides extended information about the error that occurred.
@@ -61,11 +60,10 @@ class AdbInterfaceEnumObject : public AdbObjectHandle {
bool active_only);
/** \brief Gets next enumerated interface information
@param info[out] Upon successful completion will receive interface
@param[out] info Upon successful completion will receive interface
information. Can be NULL. If it is NULL, upon return from this
method *size will have memory size required to fit this entry.
@param size[in,out]. On the way in provides size of the memory buffer
@param[in,out] size On the way in provides size of the memory buffer
addressed by info param. On the way out (only if buffer is not
big enough) will provide memory size required to fit this entry.
@return true on success, false on error. If false is returned
@@ -78,7 +76,6 @@ class AdbInterfaceEnumObject : public AdbObjectHandle {
bool Next(AdbInterfaceInfo* info, ULONG* size);
/** \brief Makes enumerator to start from the beginning.
@return true on success, false on error. If false is returned
GetLastError() provides extended information about the error that
occurred.

View File

@@ -23,14 +23,11 @@
#include "stdafx.h"
#include "adb_io_completion.h"
AdbIOCompletion::AdbIOCompletion(AdbIOObject* parent_io_obj,
bool is_write_ctl,
AdbIOCompletion::AdbIOCompletion(AdbEndpointObject* parent_io_obj,
ULONG expected_trans_size,
HANDLE event_hndl)
: AdbObjectHandle(AdbObjectTypeIoCompletion),
transferred_bytes_(0),
expected_transfer_size_(expected_trans_size),
is_write_ioctl_(is_write_ctl),
parent_io_object_(parent_io_obj) {
ATLASSERT(NULL != parent_io_obj);
parent_io_obj->AddRef();
@@ -54,11 +51,10 @@ bool AdbIOCompletion::GetOvelappedIoResult(LPOVERLAPPED ovl_data,
}
ULONG transfer;
bool ret = GetOverlappedResult(parent_io_object()->usb_handle(),
bool ret = WinUsb_GetOverlappedResult(parent_io_object()->winusb_handle(),
overlapped(),
&transfer,
wait) ? true :
false;
wait ? TRUE : FALSE) ? true : false;
// TODO: This is bizzare but I've seen it happening
// that GetOverlappedResult with wait set to true returns "prematurely",
@@ -70,11 +66,10 @@ bool AdbIOCompletion::GetOvelappedIoResult(LPOVERLAPPED ovl_data,
((ERROR_IO_INCOMPLETE == error) || (ERROR_IO_PENDING == error))) {
for (int trying = 0; trying < 10; trying++) {
Sleep(2);
ret = GetOverlappedResult(parent_io_object()->usb_handle(),
ret = WinUsb_GetOverlappedResult(parent_io_object()->winusb_handle(),
overlapped(),
&transfer,
wait) ? true :
false;
wait ? TRUE : FALSE) ? true : false;
error = GetLastError();
if (!ret || (0 != transfer) ||
((ERROR_IO_INCOMPLETE != error) && (ERROR_IO_PENDING != error))) {
@@ -87,7 +82,7 @@ bool AdbIOCompletion::GetOvelappedIoResult(LPOVERLAPPED ovl_data,
CopyMemory(ovl_data, overlapped(), sizeof(OVERLAPPED));
if (NULL != bytes_transferred)
*bytes_transferred = is_write_ioctl() ? transferred_bytes_ : transfer;
*bytes_transferred = transfer;
return ret;
}

View File

@@ -22,10 +22,11 @@
requests.
*/
#include "adb_io_object.h"
#include "adb_endpoint_object.h"
/** \brief Encapsulates encapsulates a wrapper around OVERLAPPED Win32
structure returned from asynchronous I/O requests.
/** Class AdbIOCompletion encapsulates encapsulates a wrapper around
OVERLAPPED Win32 structure returned from asynchronous I/O requests.
A handle to this object is returned to the caller of each successful
asynchronous I/O request. Just like all other handles this handle
must be closed after it's no longer needed.
@@ -34,24 +35,22 @@ class AdbIOCompletion : public AdbObjectHandle {
public:
/** \brief Constructs the object
@param parent_io_obj[in] Parent I/O object that created this instance.
Parent object will be referenced in this object's constructur and
@param[in] parent_io_obj Parent I/O object that created this instance.
Parent object will be referenced in this object's constructor and
released in the destructor.
@param is_write_ctl[in] Flag indicating whether or not this completion
object is created for ADB_IOCTL_BULK_WRITE I/O.
@param event_hndl[in] Event handle that should be signaled when I/O
@param[in] expected_trans_size Number of bytes expected to be transferred
with the I/O.
@param[in] event_hndl Event handle that should be signaled when I/O
completes. Can be NULL. If it's not NULL this handle will be
used to initialize OVERLAPPED structure for this object.
*/
AdbIOCompletion(AdbIOObject* parent_io_obj,
bool is_write_ctl,
AdbIOCompletion(AdbEndpointObject* parent_io_obj,
ULONG expected_trans_size,
HANDLE event_hndl);
protected:
/** \brief Destructs the object.
parent_io_object_ will be dereferenced here.
We hide destructor in order to prevent ourseves from accidentaly allocating
instances on the stack. If such attemp occur, compiler will error.
*/
@@ -60,18 +59,18 @@ class AdbIOCompletion : public AdbObjectHandle {
public:
/** \brief Gets overlapped I/O result
@param ovl_data[out] Buffer for the copy of this object's OVERLAPPED
@param[out] ovl_data Buffer for the copy of this object's OVERLAPPED
structure. Can be NULL.
@param bytes_transferred[out] Pointer to a variable that receives the
@param[out] bytes_transferred Pointer to a variable that receives the
number of bytes that were actually transferred by a read or write
operation. See SDK doc on GetOvelappedResult for more information.
Unlike regular GetOvelappedResult call this parameter can be NULL.
@param wait[in] If this parameter is 'true', the method does not return
until the operation has been completed. If this parameter is 'false'
and the operation is still pending, the method returns 'false' and
@param[in] wait If this parameter is true, the method does not return
until the operation has been completed. If this parameter is false
and the operation is still pending, the method returns false and
the GetLastError function returns ERROR_IO_INCOMPLETE.
@return 'true' if I/O has been completed or 'false' on failure or if request
is not yet completed. If 'false' is returned GetLastError() provides
@return true if I/O has been completed or false on failure or if request
is not yet completed. If false is returned GetLastError() provides
extended error information. If GetLastError returns
ERROR_IO_INCOMPLETE it means that I/O is not yet completed.
*/
@@ -81,7 +80,7 @@ class AdbIOCompletion : public AdbObjectHandle {
/** \brief Checks if I/O that this object represents has completed.
@return 'true' if I/O has been completed or 'false' if it's still
@return true if I/O has been completed or false if it's still
incomplete. Regardless of the returned value, caller should
check GetLastError to validate that handle was OK.
*/
@@ -94,7 +93,7 @@ class AdbIOCompletion : public AdbObjectHandle {
}
/// Gets parent object
AdbIOObject* parent_io_object() const {
AdbEndpointObject* parent_io_object() const {
return parent_io_object_;
}
@@ -104,17 +103,6 @@ class AdbIOCompletion : public AdbObjectHandle {
NULL;
}
/// Gets address for ADB_IOCTL_BULK_WRITE output buffer
ULONG* transferred_bytes_ptr() {
ATLASSERT(is_write_ioctl());
return &transferred_bytes_;
}
/// Gets write IOCTL flag
bool is_write_ioctl() const {
return is_write_ioctl_;
}
// This is a helper for extracting object from the AdbObjectHandleMap
static AdbObjectType Type() {
return AdbObjectTypeIoCompletion;
@@ -125,16 +113,10 @@ class AdbIOCompletion : public AdbObjectHandle {
OVERLAPPED overlapped_;
/// Parent I/O object
AdbIOObject* parent_io_object_;
/// Recepient for number of transferred bytes in write IOCTL
ULONG transferred_bytes_;
AdbEndpointObject* parent_io_object_;
/// Expected number of bytes transferred in thi I/O
ULONG expected_transfer_size_;
/// Write IOCTL flag
bool is_write_ioctl_;
};
#endif // ANDROID_USB_API_ADB_IO_COMPLETION_H__

View File

@@ -1,284 +0,0 @@
/*
* 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 class AdbIOObject that encapsulates
an item on our device that is opened for read / write / IOCTL I/O.
*/
#include "stdafx.h"
#include "adb_io_object.h"
#include "adb_io_completion.h"
#include "adb_helper_routines.h"
AdbIOObject::AdbIOObject(AdbInterfaceObject* parent_interf,
AdbObjectType obj_type)
: AdbObjectHandle(obj_type),
usb_handle_(INVALID_HANDLE_VALUE),
parent_interface_(parent_interf) {
ATLASSERT(NULL != parent_interf);
parent_interf->AddRef();
}
AdbIOObject::~AdbIOObject() {
if (INVALID_HANDLE_VALUE != usb_handle_)
::CloseHandle(usb_handle_);
parent_interface_->Release();
}
ADBAPIHANDLE AdbIOObject::CreateHandle(const wchar_t* item_path,
AdbOpenAccessType access_type,
AdbOpenSharingMode share_mode) {
// Make sure that we don't have USB handle here
if (IsUsbOpened()) {
SetLastError(ERROR_GEN_FAILURE);
return NULL;
}
// Convert access / share parameters into CreateFile - compatible
ULONG desired_access;
ULONG desired_sharing;
if (!GetSDKComplientParam(access_type, share_mode,
&desired_access, &desired_sharing)) {
return NULL;
}
// Open USB handle
usb_handle_ = CreateFile(item_path,
desired_access,
share_mode,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // Always overlapped!
NULL);
if (INVALID_HANDLE_VALUE == usb_handle_)
return NULL;
// Create ADB handle
ADBAPIHANDLE ret = AdbObjectHandle::CreateHandle();
if (NULL == ret) {
// If creation of ADB handle failed we have to close USB handle too.
ULONG error = GetLastError();
::CloseHandle(usb_handle());
usb_handle_ = INVALID_HANDLE_VALUE;
SetLastError(error);
}
return ret;
}
bool AdbIOObject::CloseHandle() {
// Lets close USB item first
if (IsUsbOpened()) {
::CloseHandle(usb_handle());
usb_handle_ = INVALID_HANDLE_VALUE;
}
return AdbObjectHandle::CloseHandle();
}
ADBAPIHANDLE AdbIOObject::AsyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
HANDLE event_handle,
ULONG time_out) {
return CommonAsyncReadWrite(true,
buffer,
bytes_to_read,
bytes_read,
event_handle,
time_out);
}
ADBAPIHANDLE AdbIOObject::AsyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
HANDLE event_handle,
ULONG time_out) {
return CommonAsyncReadWrite(false,
buffer,
bytes_to_write,
bytes_written,
event_handle,
time_out);
}
bool AdbIOObject::SyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
ULONG time_out) {
return CommonSyncReadWrite(true,
buffer,
bytes_to_read,
bytes_read,
time_out);
}
bool AdbIOObject::SyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
ULONG time_out) {
return CommonSyncReadWrite(false,
buffer,
bytes_to_write,
bytes_written,
time_out);
}
ADBAPIHANDLE AdbIOObject::CommonAsyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
HANDLE event_handle,
ULONG time_out) {
if (NULL != bytes_transferred)
*bytes_transferred = 0;
if (!IsOpened() || !IsUsbOpened()) {
SetLastError(ERROR_INVALID_HANDLE);
return false;
}
bool is_ioctl_write = is_read ? false : (0 != time_out);
// Create completion i/o object
AdbIOCompletion* adb_io_completion = NULL;
try {
adb_io_completion = new AdbIOCompletion(this,
is_ioctl_write,
bytes_to_transfer,
event_handle);
} catch (... ) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
// Create a handle for it
ADBAPIHANDLE ret = adb_io_completion->CreateHandle();
ULONG transferred = 0;
if (NULL != ret) {
BOOL res = TRUE;
if (0 == time_out) {
// Go the read / write file way
res = is_read ? ReadFile(usb_handle(),
buffer,
bytes_to_transfer,
&transferred,
adb_io_completion->overlapped()) :
WriteFile(usb_handle(),
buffer,
bytes_to_transfer,
&transferred,
adb_io_completion->overlapped());
} else {
// Go IOCTL way
AdbBulkTransfer transfer_param;
transfer_param.time_out = time_out;
transfer_param.transfer_size = is_read ? 0 : bytes_to_transfer;
transfer_param.SetWriteBuffer(is_read ? NULL : buffer);
res = DeviceIoControl(usb_handle(),
is_read ? ADB_IOCTL_BULK_READ : ADB_IOCTL_BULK_WRITE,
&transfer_param, sizeof(transfer_param),
is_read ? buffer : adb_io_completion->transferred_bytes_ptr(),
is_read ? bytes_to_transfer : sizeof(ULONG),
&transferred,
adb_io_completion->overlapped());
}
if (NULL != bytes_transferred)
*bytes_transferred = transferred;
ULONG error = GetLastError();
if (!res && (ERROR_IO_PENDING != error)) {
// I/O failed immediatelly. We need to close i/o completion object
// before we return NULL to the caller.
adb_io_completion->CloseHandle();
ret = NULL;
SetLastError(error);
}
}
// Offseting 'new'
adb_io_completion->Release();
return ret;
}
bool AdbIOObject::CommonSyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
ULONG time_out) {
if (NULL != bytes_transferred)
*bytes_transferred = 0;
if (!IsOpened() || !IsUsbOpened()) {
SetLastError(ERROR_INVALID_HANDLE);
return false;
}
bool is_ioctl_write = is_read ? false : (0 != time_out);
// This is synchronous I/O. Since we always open I/O items for
// overlapped I/O we're obligated to always provide OVERLAPPED
// structure to read / write routines. Prepare it now.
OVERLAPPED overlapped;
ZeroMemory(&overlapped, sizeof(overlapped));
BOOL ret = TRUE;
ULONG ioctl_write_transferred = 0;
if (0 == time_out) {
// Go the read / write file way
ret = is_read ?
ReadFile(usb_handle(), buffer, bytes_to_transfer, bytes_transferred, &overlapped) :
WriteFile(usb_handle(), buffer, bytes_to_transfer, bytes_transferred, &overlapped);
} else {
// Go IOCTL way
AdbBulkTransfer transfer_param;
transfer_param.time_out = time_out;
transfer_param.transfer_size = is_read ? 0 : bytes_to_transfer;
transfer_param.SetWriteBuffer(is_read ? NULL : buffer);
ULONG tmp;
ret = DeviceIoControl(usb_handle(),
is_read ? ADB_IOCTL_BULK_READ : ADB_IOCTL_BULK_WRITE,
&transfer_param, sizeof(transfer_param),
is_read ? buffer : &ioctl_write_transferred,
is_read ? bytes_to_transfer : sizeof(ULONG),
&tmp,
&overlapped);
}
// Lets see the result
if (!ret && (ERROR_IO_PENDING != GetLastError())) {
// I/O failed.
return false;
}
// Lets wait till I/O completes
ULONG transferred = 0;
ret = GetOverlappedResult(usb_handle(), &overlapped, &transferred, TRUE);
if (ret && (NULL != bytes_transferred)) {
*bytes_transferred = is_ioctl_write ? ioctl_write_transferred :
transferred;
}
return ret ? true : false;
}

View File

@@ -1,238 +0,0 @@
/*
* 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.
*/
#ifndef ANDROID_USB_API_ADB_IO_OBJECT_H__
#define ANDROID_USB_API_ADB_IO_OBJECT_H__
/** \file
This file consists of declaration of class AdbIOObject that encapsulates an
item on our device that is opened for read / write / IOCTL I/O.
*/
#include "adb_interface.h"
/** Class AdbIOObject encapsulates an item on our device that is opened for
read / write / IOCTL I/O.. All I/O items (currently only endpoints) are
always opened for overlapped I/O (i.e. FILE_OVERLAPPED flag is set in
create attributes). This way each object of the derived class automatically
supports both, synchronous as well as asynchronous I/O. Since async I/O
requires "giving out" some I/O context, we have to provide async I/O caller
with some safe handle to this context. This is done wia allocating
AdbIOCompletion object that holds async I/O context and returning handle to
this object to the caller of async I/O.
*/
class AdbIOObject : public AdbObjectHandle {
public:
/** \brief Constructs the object
@param interface[in] Parent interface for this object. Interface will be
referenced in this object's constructur and released in the
destructor.
@param obj_type[in] Object type from AdbObjectType enum
*/
AdbIOObject(AdbInterfaceObject* parent_interf, AdbObjectType obj_type);
protected:
/** \brief Destructs the object.
parent_interface_ will be dereferenced here.
We hide destructor in order to prevent ourseves from accidentaly allocating
instances on the stack. If such attemp occur, compiler will error.
*/
virtual ~AdbIOObject();
public:
/** \brief Opens USB item and creates a handle to this object
We combine in this method ADB handle association and opening required
object on our USB device. The sequence is to open USB item first and if
(and only if) this open succeedes we proceed to creating ADB handle by
calling AdbObjectHandle::CreateHandle(). We always open USB handle for
overlapped I/O.
@param item_path[in] Path to the item on our USB device.
@param access_type[in] Desired access type. In the current implementation
this parameter has no effect on the way item is opened. It's
always read / write access.
@param sharing_mode[in] Desired share mode. In the current implementation
this parameter has no effect on the way item is opened. It's
always shared for read / write.
@return A handle to this object on success or NULL on an error.
If NULL is returned GetLastError() provides extended error
information. ERROR_GEN_FAILURE is set if an attempt was
made to create already opened object.
*/
virtual ADBAPIHANDLE CreateHandle(const wchar_t* item_path,
AdbOpenAccessType access_type,
AdbOpenSharingMode share_mode);
/** \brief This method is called when handle to this object gets closed
We overwrite this method in order to close USB handle along with this
object handle.
@return 'true' on success or 'false' if object is already closed. If
'false' is returned GetLastError() provides extended error
information.
*/
virtual bool CloseHandle();
/** \brief Reads from opened I/O object asynchronously
@param buffer[out] Pointer to the buffer that receives the data.
@param bytes_to_read[in] Number of bytes to be read.
@param bytes_read[out] Number of bytes read. Can be NULL.
@param event_handle[in] Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual ADBAPIHANDLE AsyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
HANDLE event_handle,
ULONG time_out);
/** \brief Writes to opened I/O object asynchronously
@param buffer[in] Pointer to the buffer containing the data to be written.
@param bytes_to_write[in] Number of bytes to be written.
@param bytes_written[out] Number of bytes written. Can be NULL.
@param event_handle[in] Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual ADBAPIHANDLE AsyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
HANDLE event_handle,
ULONG time_out);
/** \brief Reads from opened I/O object synchronously
@param buffer[out] Pointer to the buffer that receives the data.
@param bytes_to_read[in] Number of bytes to be read.
@param bytes_read[out] Number of bytes read. Can be NULL.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return 'true' on success and 'false' on failure. If 'false' is
returned GetLastError() provides extended error information.
*/
virtual bool SyncRead(void* buffer,
ULONG bytes_to_read,
ULONG* bytes_read,
ULONG time_out);
/** \brief Writes to opened I/O object synchronously
@param buffer[in] Pointer to the buffer containing the data to be written.
@param bytes_to_write[in] Number of bytes to be written.
@param bytes_written[out] Number of bytes written. Can be NULL.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return 'true' on success and 'false' on failure. If 'false' is
returned GetLastError() provides extended error information.
*/
virtual bool SyncWrite(void* buffer,
ULONG bytes_to_write,
ULONG* bytes_written,
ULONG time_out);
protected:
/** \brief Common code for async read / write
@param is_read[in] Read or write selector.
@param buffer[in,out] Pointer to the buffer for read / write.
@param bytes_to_transfer[in] Number of bytes to be read / written.
@param bytes_transferred[out] Number of bytes read / written. Can be NULL.
@param event_handle[in] Event handle that should be signaled when async I/O
completes. Can be NULL. If it's not NULL this handle will be used to
initialize OVERLAPPED structure for this I/O.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return A handle to IO completion object or NULL on failure. If NULL is
returned GetLastError() provides extended error information.
*/
virtual ADBAPIHANDLE CommonAsyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
HANDLE event_handle,
ULONG time_out);
/** \brief Common code for sync read / write
@param is_read[in] Read or write selector.
@param buffer[in,out] Pointer to the buffer for read / write.
@param bytes_to_transfer[in] Number of bytes to be read / written.
@param bytes_transferred[out] Number of bytes read / written. Can be NULL.
@param time_out[in] A timeout (in milliseconds) required for this I/O to
complete. Zero value in this parameter means that there is no
timeout set for this I/O.
@return 'true' on success, 'false' on failure. If 'false' is returned
GetLastError() provides extended error information.
*/
virtual bool CommonSyncReadWrite(bool is_read,
void* buffer,
ULONG bytes_to_transfer,
ULONG* bytes_transferred,
ULONG time_out);
public:
/// Gets parent interface
AdbInterfaceObject* parent_interface() const {
return parent_interface_;
}
/// Gets parent interface handle
ADBAPIHANDLE GetParentInterfaceHandle() const {
return (NULL != parent_interface()) ? parent_interface()->adb_handle() :
NULL;
}
/// Gets handle to an item opened on our USB device
HANDLE usb_handle() const {
return usb_handle_;
}
/// Checks if USB item is opened
bool IsUsbOpened() const {
return (INVALID_HANDLE_VALUE != usb_handle());
}
// This is a helper for extracting object from the AdbObjectHandleMap
static AdbObjectType Type() {
return AdbObjectTypeIo;
}
protected:
/// Parent interface
AdbInterfaceObject* parent_interface_;
/// Handle to an item opened on our USB device
HANDLE usb_handle_;
};
#endif // ANDROID_USB_API_ADB_IO_OBJECT_H__

View File

@@ -24,31 +24,29 @@
#include "adb_api_private_defines.h"
/** AdbObjectType enum defines types of internal API objects
/** \brief Defines types of internal API objects
*/
enum AdbObjectType {
/// Object is AdbInterfaceEnumObject
/// Object is AdbInterfaceEnumObject.
AdbObjectTypeInterfaceEnumerator,
/// Object is AdbInterfaceObject
/// Object is AdbInterfaceObject.
AdbObjectTypeInterface,
/// Object is derived from AdbIOObject
AdbObjectTypeIo,
/// Object is AdbEndpointObject
/// Object is AdbEndpointObject.
AdbObjectTypeEndpoint,
/// Object is AdbIOCompletion
/// Object is AdbIOCompletion.
AdbObjectTypeIoCompletion,
AdbObjectTypeMax
};
/** Class AdbObjectHandle encapsulates an internal API basic object that is
visible to the outside of the API through a handle. In order to prevent
crashes when API client tries to access an object through an invalid or
already closed handle, we keep track of all opened handles in
/** \brief Encapsulates an internal API basic object that is visible to the
outside of the API through a handle.
In order to prevent crashes when API client tries to access an object through
an invalid or already closed handle, we keep track of all opened handles in
AdbObjectHandleMap that maps association between valid ADBAPIHANDLE and
an object that this handle represents. All objects that are exposed to the
outside of API via ADBAPIHANDLE are self-destructing referenced objects.
@@ -78,7 +76,7 @@ class AdbObjectHandle {
/** \brief Constructs the object
Refernce counter is set to 1 in the constructor.
@param obj_type[in] Object type from AdbObjectType enum
@param[in] obj_type Object type from AdbObjectType enum
*/
explicit AdbObjectHandle(AdbObjectType obj_type);
@@ -91,14 +89,14 @@ class AdbObjectHandle {
virtual ~AdbObjectHandle();
public:
/** \brief References the object
/** \brief References the object.
@return Value of the reference counter after object is referenced in this
method.
*/
virtual LONG AddRef();
/** \brief Releases the object
/** \brief Releases the object.
If refcount drops to zero as the result of this release, the object is
destroyed in this method. As a general rule, objects must not be touched
@@ -108,7 +106,7 @@ class AdbObjectHandle {
*/
virtual LONG Release();
/** \brief Creates handle to this object
/** \brief Creates handle to this object.
In this call a handle for this object is generated and object is added
to the AdbObjectHandleMap.
@@ -119,19 +117,19 @@ class AdbObjectHandle {
*/
virtual ADBAPIHANDLE CreateHandle();
/** \brief This method is called when handle to this object gets closed
/** \brief This method is called when handle to this object gets closed.
In this call object is deleted from the AdbObjectHandleMap.
@return 'true' on success or 'false' if object is already closed. If
'false' is returned GetLastError() provides extended error
@return true on success or false if object is already closed. If
false is returned GetLastError() provides extended error
information.
*/
virtual bool CloseHandle();
/** \brief Checks if this object is of the given type
/** \brief Checks if this object is of the given type.
@param obj_type[in] One of the AdbObjectType types to check
@return 'true' is this object type matches obj_type, or 'false' otherwise.
@param[in] obj_type One of the AdbObjectType types to check
@return true is this object type matches obj_type, or false otherwise.
*/
virtual bool IsObjectOfType(AdbObjectType obj_type) const;
@@ -139,7 +137,7 @@ class AdbObjectHandle {
in the AdbObjectHandleMap.
This method increments reference counter for the returned found object.
@param adb_handle[in] ADB handle to the object
@param[in] adb_handle ADB handle to the object
@return API object associated with the handle or NULL if object is not
found. If NULL is returned GetLastError() provides extended error
information.
@@ -188,7 +186,7 @@ typedef std::map< ADBAPIHANDLE, AdbObjectHandle* > AdbObjectHandleMap;
/** \brief Template routine that unifies extracting of objects of different
types from the AdbObjectHandleMap
@param adb_handle[in] API handle for the object
@param[in] adb_handle API handle for the object
@return Object associated with the handle or NULL on error. If NULL is
returned GetLastError() provides extended error information.
*/

View File

@@ -44,6 +44,12 @@
#define _WIN32_IE 0x0501 // Change this to the appropriate value to target IE 5.0 or later.
#endif
// These defines prevent the MS header files from ejecting #pragma comment
// statements with the manifest information of the used ATL, STL, and CRT
#define _ATL_NOFORCE_MANIFEST
#define _STL_NOFORCE_MANIFEST
#define _CRT_NOFORCE_MANIFEST
#define _ATL_APARTMENT_THREADED
#define _ATL_NO_AUTOMATIC_NAMESPACE
@@ -52,20 +58,25 @@
// turns off ATL's hiding of some common and often safely ignored warning messages
#define _ATL_ALL_WARNINGS
// #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#pragma warning(disable: 4702)
#include "resource.h"
#pragma warning(disable: 4201)
#include <atlbase.h>
#include <atlcom.h>
#include <winioctl.h>
#include <setupapi.h>
#include <vector>
#include <map>
#include <string>
#pragma warning(default: 4201)
#pragma warning(disable: 4200)
extern "C" {
#include <usbdi.h>
#include <winusb.h>
#include <usb100.h>
}
#include "android_usb_common_defines.h"
#include "resource.h"
using namespace ATL;