Skip to content

Commit

Permalink
Naive implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
inga-lovinde committed Nov 30, 2019
1 parent 4f9b5b1 commit 3282196
Show file tree
Hide file tree
Showing 29 changed files with 1,689 additions and 0 deletions.
25 changes: 25 additions & 0 deletions RadeonResetBugFix.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RadeonResetBugFixService", "RadeonResetBugFixService\RadeonResetBugFixService.csproj", "{1B9A6DE9-69F9-48B2-B70D-632E5766404A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B9A6DE9-69F9-48B2-B70D-632E5766404A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B9A6DE9-69F9-48B2-B70D-632E5766404A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B9A6DE9-69F9-48B2-B70D-632E5766404A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B9A6DE9-69F9-48B2-B70D-632E5766404A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {67C6E2F8-1519-4E55-9C93-1EAD4050026F}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions RadeonResetBugFixService/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
25 changes: 25 additions & 0 deletions RadeonResetBugFixService/Contracts/DeviceInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace RadeonResetBugFixService.Contracts
{
using System;

class DeviceInfo
{
public Guid ClassGuid { get; set; }

public string ClassName { get; set; }

public string DeviceId { get; set; }

public long? ErrorCode { get; set; }

public bool IsDisabled => this.ErrorCode == 22;

public bool IsPresent { get; set; }

public string Manufacturer { get; set; }

public string Name { get; set; }

public string Service { get; set; }
}
}
10 changes: 10 additions & 0 deletions RadeonResetBugFixService/Contracts/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RadeonResetBugFixService.Contracts
{
using System;

interface ILogger : IDisposable
{
void Log(string message);
void LogError(string message);
}
}
83 changes: 83 additions & 0 deletions RadeonResetBugFixService/Devices/DeviceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace RadeonResetBugFixService.Devices
{
using System;
using System.Collections.Generic;
using System.Management;
using Contracts;

class DeviceHelper
{
private static T GetProperty<T>(PropertyDataCollection properties, string key)
{
try
{
return (T)properties[key].Value;
}
catch (Exception)
{
return default;
}
}

private static Guid GuidTryParse(string input)
{
Guid.TryParse(input, out var result);
return result;
}

private static DeviceInfo ConvertDeviceInfo(PropertyDataCollection deviceProperties)
{
return new DeviceInfo
{
ClassGuid = GuidTryParse(GetProperty<string>(deviceProperties, "ClassGuid")),
ClassName = GetProperty<string>(deviceProperties, "PNPClass") ?? string.Empty,
DeviceId = GetProperty<string>(deviceProperties, "PNPDeviceId") ?? string.Empty,
ErrorCode = GetProperty<UInt32>(deviceProperties, "ConfigManagerErrorCode"),
IsPresent = GetProperty<bool>(deviceProperties, "Present"),
Manufacturer = GetProperty<string>(deviceProperties, "Manufacturer") ?? string.Empty,
Name = GetProperty<string>(deviceProperties, "Name") ?? string.Empty,
Service = GetProperty<string>(deviceProperties, "Service") ?? string.Empty,
};
}

public static IEnumerable<DeviceInfo> GetDevices()
{
ManagementPath path = new ManagementPath
{
Server = ".",
NamespacePath = @"root\CIMV2",
RelativePath = @"Win32_PnPentity",
};

using (var devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, TimeSpan.FromMinutes(1), false)))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach (ManagementObject mo in moc)
{
/*Console.WriteLine("===================================");
Console.WriteLine("New device: " + mo.Path.Path);
PropertyDataCollection devsProperties = mo.Properties;
foreach (PropertyData devProperty in devsProperties)
{
if (devProperty.Type != CimType.DateTime)
{
Console.WriteLine("Property = {0}\tValue = {1}\tType={2}", devProperty.Name, devProperty.Value, devProperty.Value?.GetType()?.Name);
}
}*/

yield return ConvertDeviceInfo(mo.Properties);
}
}
}

public static void DisableDevice(DeviceInfo deviceInfo)
{
ThirdParty.DisableDevice.DeviceHelper.SetDeviceEnabled(deviceInfo.ClassGuid, deviceInfo.DeviceId, false);
}

public static void EnableDevice(DeviceInfo deviceInfo)
{
ThirdParty.DisableDevice.DeviceHelper.SetDeviceEnabled(deviceInfo.ClassGuid, deviceInfo.DeviceId, true);
}
}
}
18 changes: 18 additions & 0 deletions RadeonResetBugFixService/Devices/KnownDevices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace RadeonResetBugFixService.Devices
{
using Contracts;

static class KnownDevices
{
public static bool IsAmdVideo(DeviceInfo device)
{
return (device.Manufacturer.ToLowerInvariant() == "amd" || device.Manufacturer.ToLowerInvariant().Contains("advanced micro devices")) &&
(device.Service.ToLowerInvariant() == "hdaudbus" || device.ClassName.ToLowerInvariant() == "display");
}

public static bool IsVirtualVideo(DeviceInfo device)
{
return device.Service.ToLowerInvariant() == "hypervideo";
}
}
}
26 changes: 26 additions & 0 deletions RadeonResetBugFixService/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace RadeonResetBugFixService.Logging
{
using System;
using System.IO;
using Contracts;

class FileLogger : ILogger
{
private string Filename { get; }

public FileLogger(string filename)
{
this.Filename = filename;
}

private void LogString(string message) => File.AppendAllLines(this.Filename, new[] { $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}" });

void ILogger.Log(string message) => LogString(message);

void ILogger.LogError(string message) => LogString($"Error: {message}");

void IDisposable.Dispose()
{
}
}
}
26 changes: 26 additions & 0 deletions RadeonResetBugFixService/Logging/TaskLoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace RadeonResetBugFixService.Logging
{
using System;
using Contracts;

class TaskLoggerWrapper : ILogger
{
private ILogger InnerLogger { get; }

private string Prefix { get; }

public TaskLoggerWrapper(ILogger innerLogger, string taskName)
{
this.InnerLogger = innerLogger;
this.Prefix = $"[{taskName}]";

innerLogger.Log($"{this.Prefix} begin");
}

void ILogger.Log(string message) => this.InnerLogger.Log($"{this.Prefix} {message}");

void ILogger.LogError(string message) => this.InnerLogger.LogError($"{this.Prefix} {message}");

void IDisposable.Dispose() => this.InnerLogger.Log($"{this.Prefix} end");
}
}
66 changes: 66 additions & 0 deletions RadeonResetBugFixService/MainHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
namespace RadeonResetBugFixService
{
using System;
using System.IO;
using System.Reflection;
using Contracts;
using Logging;
using Tasks;

class MainHandler
{
private string LogFilename { get; }

private object Mutex = new object();

public MainHandler()
{
var date = DateTime.Now;
this.LogFilename = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"logs",
$"radeonfix_{date:yyyyMMdd}_{date:HHmmss}.log");
}

public void HandleStartup()
{
using (var fileLogger = new FileLogger(this.LogFilename))
{
using (ILogger logger = new TaskLoggerWrapper(fileLogger, "Startup"))
{
lock (this.Mutex)
{
TasksProcessor.ProcessTasks(
logger,
new ITask[]
{
new DisableVirtualVideoTask(),
new EnableAmdVideoTask(),
});
}
}
}
}

public void HandleShutdown()
{
using (var fileLogger = new FileLogger(this.LogFilename))
{
using (ILogger logger = new TaskLoggerWrapper(fileLogger, "Shutdown"))
{
lock (this.Mutex)
{
TasksProcessor.ProcessTasks(
logger,
new ITask[]
{
new StopAudioServiceTask(),
new DisableAmdVideoTask(),
new EnableVirtualVideoTask(),
});
}
}
}
}
}
}
Loading

0 comments on commit 3282196

Please sign in to comment.