Skip to content

Commit

Permalink
UsbDkHelper: Introduce DeviceMgr class with reset logic
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Gurvich <[email protected]>
Signed-off-by: Dmitry Fleytman <[email protected]>
  • Loading branch information
Pavel Gurvich authored and Dmitry Fleytman committed May 11, 2014
1 parent 7fa28c0 commit 998fd9a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
68 changes: 68 additions & 0 deletions UsbDkHelper/DeviceMgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "stdafx.h"
#include "DeviceMgr.h"

#include <cfgmgr32.h>

InstallResult DeviceMgr::ResetDeviceByClass(const GUID &ClassGuid)
{
auto hDevInfo = SetupDiGetClassDevsEx(&ClassGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, NULL, NULL, NULL);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
throw UsbDkDeviceMgrException(TEXT("DeviceMgr throw the exception: SetupDiGetClassDevsEx() failed!!"));
}

SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if (!SetupDiGetDeviceInfoListDetail(hDevInfo, &devInfoListDetail))
{
SetupDiDestroyDeviceInfoList(hDevInfo);
throw UsbDkDeviceMgrException(TEXT("DeviceMgr throw the exception: SetupDiGetDeviceInfoListDetail() failed!!"));
}

InstallResult installRes = InstallSuccess;
SP_DEVINFO_DATA devInfo;
devInfo.cbSize = sizeof(devInfo);
for (DWORD devIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, devIndex, &devInfo); devIndex++)
{
installRes = ResetDevice(hDevInfo, &devInfo, &devInfoListDetail);
if (installRes != InstallSuccess)
{
break;
}
}

SetupDiDestroyDeviceInfoList(hDevInfo);

return installRes;
}
//--------------------------------------------------------------------------------

InstallResult DeviceMgr::ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo, PSP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail)
{
InstallResult installRes = InstallSuccess;

SP_PROPCHANGE_PARAMS pcParams;
pcParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcParams.StateChange = DICS_PROPCHANGE;
pcParams.Scope = DICS_FLAG_CONFIGSPECIFIC;
pcParams.HwProfile = 0;

if (!SetupDiSetClassInstallParams(devs, devInfo, &pcParams.ClassInstallHeader, sizeof(pcParams)) ||
!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, devs, devInfo))
{
installRes = InstallFailureNeedReboot;
}
else
{
SP_DEVINSTALL_PARAMS devInstallParams;
devInstallParams.cbSize = sizeof(devInstallParams);
if (SetupDiGetDeviceInstallParams(devs, devInfo, &devInstallParams) && (devInstallParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT)))
{
installRes = InstallFailureNeedReboot;
}
}

return installRes;
}
//--------------------------------------------------------------------------------
23 changes: 23 additions & 0 deletions UsbDkHelper/DeviceMgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <Setupapi.h>
#include "UsbDkHelper.h"

//-----------------------------------------------------------------------------------

class UsbDkDeviceMgrException : public UsbDkW32ErrorException
{
public:
UsbDkDeviceMgrException() : UsbDkW32ErrorException(TEXT("DeviceMgr throw the exception")){}
UsbDkDeviceMgrException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(lpzMessage){}
};
//-----------------------------------------------------------------------------------

class DeviceMgr
{
public:
static InstallResult ResetDeviceByClass(const GUID &ClassGuid);
private:
static InstallResult ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo, PSP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail);
};
//-----------------------------------------------------------------------------------
8 changes: 8 additions & 0 deletions UsbDkHelper/Installer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "stdafx.h"
#include "Installer.h"
#include "Public.h"
#include "DeviceMgr.h"

#include <initguid.h>
#include <Usbiodef.h>

//------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -38,6 +42,8 @@ InstallResult UsbDkInstaller::Install()
m_wdfCoinstaller.PostDeviceInstall(infFilePath);

addUsbDkToRegistry();

DeviceMgr::ResetDeviceByClass(GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
}
catch(const exception &e)
{
Expand All @@ -56,6 +62,8 @@ bool UsbDkInstaller::Uninstall()
{
removeUsbDkFromRegistry();

DeviceMgr::ResetDeviceByClass(GUID_DEVINTERFACE_USB_HOST_CONTROLLER);

// copy driver to WIndows/System32/drivers folder
unsetDriver();

Expand Down
14 changes: 14 additions & 0 deletions UsbDkHelper/UsbDkHelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|x64'">
Expand All @@ -238,6 +239,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Debug|Win32'">
Expand All @@ -253,6 +255,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Debug|x64'">
Expand All @@ -268,6 +271,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|Win32'">
Expand All @@ -283,6 +287,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|x64'">
Expand All @@ -299,6 +304,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ImportLibrary>$(OutDir)$(TargetName).lib</ImportLibrary>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8 Release|Win32'">
Expand All @@ -318,6 +324,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8 Release|x64'">
Expand All @@ -337,6 +344,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win7 Release|Win32'">
Expand All @@ -356,6 +364,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win7 Release|x64'">
Expand All @@ -375,6 +384,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|Win32'">
Expand All @@ -394,6 +404,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|x64'">
Expand All @@ -413,6 +424,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down Expand Up @@ -484,6 +496,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|x64'">Use</PrecompiledHeader>
</ClCompile>
<ClCompile Include="DeviceMgr.cpp" />
<ClCompile Include="UsbDkHelper.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|x64'">Use</PrecompiledHeader>
Expand Down Expand Up @@ -546,6 +559,7 @@
<ClInclude Include="Exception.h" />
<ClInclude Include="Installer.h" />
<ClInclude Include="RegAccess.h" />
<ClInclude Include="DeviceMgr.h" />
<ClInclude Include="UsbDkHelper.h" />
<ClInclude Include="ServiceManager.h" />
<ClInclude Include="stdafx.h" />
Expand Down
6 changes: 6 additions & 0 deletions UsbDkHelper/UsbDkHelper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<ClCompile Include="Installer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DeviceMgr.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="UsbDkHelper.h">
Expand Down Expand Up @@ -71,5 +74,8 @@
<ClInclude Include="Installer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DeviceMgr.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 998fd9a

Please sign in to comment.