diff --git a/YAMDCC.Service/FanControlService.cs b/YAMDCC.Service/FanControlService.cs index 7e0eb16..50f99d0 100644 --- a/YAMDCC.Service/FanControlService.cs +++ b/YAMDCC.Service/FanControlService.cs @@ -105,10 +105,10 @@ protected override void OnStart(string[] args) } Log.Info(Strings.GetString("drvLoadSuccess")); - CooldownTimer.Elapsed += CooldownTimer_Elapsed; + CooldownTimer.Elapsed += CooldownElapsed; // Set up IPC server - Log.Debug("Starting IPC server..."); + Log.Info("Starting IPC server..."); IPCServer.ClientConnected += IPCClientConnect; IPCServer.ClientDisconnected += IPCClientDisconnect; IPCServer.Error += IPCServerError; @@ -129,7 +129,7 @@ protected override void OnStart(string[] args) if (rebootFlag == 0) { - FanCurveECToConf(); + ECToConf(); File.Delete(Paths.ECtoConfPending); } } @@ -140,7 +140,7 @@ protected override void OnStart(string[] args) ApplySettings(); } - private void CooldownTimer_Elapsed(object sender, ElapsedEventArgs e) + private void CooldownElapsed(object sender, ElapsedEventArgs e) { CooldownTimer.Stop(); Cooldown = false; @@ -157,11 +157,17 @@ protected override void OnShutdown() try { StreamReader sr = new(Paths.ECtoConfPending); - if (int.TryParse(sr.ReadToEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) + try { - rebootFlag = value; + if (int.TryParse(sr.ReadToEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) + { + rebootFlag = value; + } + } + finally + { + sr.Close(); } - sr.Close(); if (rebootFlag == 1) { @@ -169,7 +175,6 @@ protected override void OnShutdown() try { sw.Write(0); - sw.Flush(); } finally { @@ -189,16 +194,16 @@ private void StopService() Log.Info(Strings.GetString("svcStopping")); // Stop the IPC server: - Log.Debug("Stopping IPC server..."); + Log.Info("Stopping IPC server..."); IPCServer.Stop(); IPCServer.ClientConnected -= IPCClientConnect; IPCServer.ClientDisconnected -= IPCClientDisconnect; IPCServer.Error -= IPCServerError; - CooldownTimer.Elapsed -= CooldownTimer_Elapsed; + CooldownTimer.Elapsed -= CooldownElapsed; // Uninstall WinRing0 to keep things clean - Log.Debug(Strings.GetString("drvUnload")); + Log.Info(Strings.GetString("drvUnload")); _EC.UnloadDriver(); Log.Info(Strings.GetString("svcStopped")); @@ -215,7 +220,7 @@ protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) if (!Cooldown) { // Re-apply the fan curve after waking up from sleep: - Log.Debug($"Re-applying fan curve..."); + Log.Info(Strings.GetString("svcWake")); ApplySettings(); Cooldown = true; CooldownTimer.Start(); @@ -250,7 +255,6 @@ private void IPCClientMessage(object sender, PipeMessageEventArgs /// Parse arguments from a given string. /// - /// The string containing the space-delimited arguments. - /// The expected number of arguments. Must be zero or positive. - /// The parsed arguments. Will be empty if parsing fails. - /// + /// + /// The string containing the space-delimited arguments. + /// + /// + /// The expected number of arguments. Must be zero or positive. + /// + /// + /// The parsed arguments. Will be empty if parsing fails. + /// + /// + /// true if the arguments were parsed successfully, + /// otherise false. + /// private bool ParseArgs(string argsIn, int numExpectedArgs, out int[] argsOut) { argsOut = new int[numExpectedArgs]; @@ -502,14 +515,13 @@ private int ReadECByte(int clientId, string args) { if (ParseArgs(args, 1, out int[] pArgs)) { - bool success = LogECReadByte((byte)pArgs[0], out byte value); - - ServiceResponse response = success - ? new(Response.ReadResult, $"{pArgs[0]} {value}") - : new(Response.Error, $"{(int)Command.ReadECByte}"); - - IPCServer.PushMessage(response, clientId); - return 0; + if (LogECReadByte((byte)pArgs[0], out byte value)) + { + IPCServer.PushMessage(new ServiceResponse( + Response.ReadResult, $"{pArgs[0]} {value}"), clientId); + return 0; + } + return 1; } return 2; } @@ -518,14 +530,13 @@ private int WriteECByte(int clientId, string args) { if (ParseArgs(args, 2, out int[] pArgs)) { - bool success = LogECWriteByte((byte)pArgs[0], (byte)pArgs[1]); - - ServiceResponse response = success - ? new(Response.Success, $"{(int)Command.WriteECByte}") - : new(Response.Error, $"{(int)Command.WriteECByte}"); - - IPCServer.PushMessage(response, clientId); - return 0; + if (LogECWriteByte((byte)pArgs[0], (byte)pArgs[1])) + { + IPCServer.PushMessage(new ServiceResponse( + Response.Success, $"{(int)Command.WriteECByte}"), clientId); + return 0; + } + return 1; } return 2; } @@ -540,14 +551,14 @@ private int GetFanSpeed(int clientId, string args) if (ParseArgs(args, 1, out int[] pArgs)) { FanConf cfg = Config.FanConfs[pArgs[0]]; - bool success = LogECReadByte(cfg.SpeedReadReg, out byte speed); - - ServiceResponse response = success - ? new(Response.FanSpeed, $"{speed}") - : new(Response.Error, $"{(int)Command.GetFanSpeed}"); - IPCServer.PushMessage(response, clientId); - return 0; + if (LogECReadByte(cfg.SpeedReadReg, out byte speed)) + { + IPCServer.PushMessage(new ServiceResponse( + Response.FanSpeed, $"{speed}"), clientId); + return 0; + } + return 1; } return 2; } @@ -563,59 +574,56 @@ private int GetFanRPM(int clientId, string args) { FanConf cfg = Config.FanConfs[pArgs[0]]; - if (cfg.RPMConf is not null) + if (cfg.RPMConf is null) { - bool success; - ushort rpmValue; - if (cfg.RPMConf.Is16Bit) - { - success = LogECReadWord(cfg.RPMConf.ReadReg, out rpmValue, cfg.RPMConf.IsBigEndian); - } - else - { - success = LogECReadByte(cfg.RPMConf.ReadReg, out byte rpmValByte); - rpmValue = rpmValByte; - } + return 0; + } - ServiceResponse response; - if (success) - { + bool success; + ushort rpmValue; + if (cfg.RPMConf.Is16Bit) + { + success = LogECReadWord(cfg.RPMConf.ReadReg, out rpmValue, cfg.RPMConf.IsBigEndian); + } + else + { + success = LogECReadByte(cfg.RPMConf.ReadReg, out byte rpmValByte); + rpmValue = rpmValByte; + } + + if (success) + { #pragma warning disable IDE0045 // Supress "if statement can be simplified" suggestion - int rpm; - if (cfg.RPMConf.Invert) + int rpm; + if (cfg.RPMConf.Invert) + { + if (rpmValue == 0) { - if (rpmValue == 0) - { - rpm = -1; - } - else if (cfg.RPMConf.DivideByMult) - { - rpm = cfg.RPMConf.RPMMult / rpmValue; - } - else - { - rpm = 1 / (rpmValue * cfg.RPMConf.RPMMult); - } + rpm = -1; } else if (cfg.RPMConf.DivideByMult) { - rpm = rpmValue / cfg.RPMConf.RPMMult; + rpm = cfg.RPMConf.RPMMult / rpmValue; } else { - rpm = rpmValue * cfg.RPMConf.RPMMult; + rpm = 1 / (rpmValue * cfg.RPMConf.RPMMult); } -#pragma warning restore IDE0045 - response = new(Response.FanRPM, $"{rpm}"); + } + else if (cfg.RPMConf.DivideByMult) + { + rpm = rpmValue / cfg.RPMConf.RPMMult; } else { - response = new(Response.Error, $"{(int)Command.GetFanRPM}"); + rpm = rpmValue * cfg.RPMConf.RPMMult; } - IPCServer.PushMessage(response, clientId); +#pragma warning restore IDE0045 + IPCServer.PushMessage(new ServiceResponse( + Response.FanRPM, $"{rpm}"), clientId); return 0; } - return 0; + return 1; } return 2; } @@ -630,52 +638,52 @@ private int GetTemp(int clientId, string args) if (ParseArgs(args, 1, out int[] pArgs)) { FanConf cfg = Config.FanConfs[pArgs[0]]; - bool success = LogECReadByte(cfg.TempReadReg, out byte temp); - - ServiceResponse response = success - ? new(Response.Temp, $"{temp}") - : new(Response.Error, $"{(int)Command.GetTemp}"); - IPCServer.PushMessage(response, clientId); - return 0; + if (LogECReadByte(cfg.TempReadReg, out byte temp)) + { + IPCServer.PushMessage(new ServiceResponse( + Response.Temp, $"{temp}"), clientId); + return 0; + } + return 1; } return 2; } private int SetFullBlast(int clientId, string args) { - if (ConfigLoaded && Config.FullBlastConf is not null) + if (!ConfigLoaded || Config.FullBlastConf is null) { - if (ParseArgs(args, 1, out int[] pArgs)) - { - if (!LogECReadByte(Config.FullBlastConf.Reg, out byte value)) - { - return 0; - } + return 0; + } - bool success; - if (pArgs[0] == 1) - { - Log.Debug("Enabling Full Blast..."); - value |= Config.FullBlastConf.Mask; - } - else - { - Log.Debug("Disabling Full Blast..."); - value &= (byte)~Config.FullBlastConf.Mask; - } - success = LogECWriteByte(Config.FullBlastConf.Reg, value); + if (ParseArgs(args, 1, out int[] pArgs)) + { + if (!LogECReadByte(Config.FullBlastConf.Reg, out byte value)) + { + return 1; + } - ServiceResponse response = success - ? new(Response.Success, $"{(int)Command.FullBlast}") - : new(Response.Error, $"{(int)Command.FullBlast}"); + if (pArgs[0] == 1) + { + Log.Debug("Enabling Full Blast..."); + value |= Config.FullBlastConf.Mask; + } + else + { + Log.Debug("Disabling Full Blast..."); + value &= (byte)~Config.FullBlastConf.Mask; + } - IPCServer.PushMessage(response, clientId); + if (LogECWriteByte(Config.FullBlastConf.Reg, value)) + { + IPCServer.PushMessage(new ServiceResponse( + Response.Success, $"{(int)Command.FullBlast}"), clientId); return 0; } - return 2; + return 1; } - return 0; + return 2; } private int GetKeyLightBright(int clientId) @@ -686,23 +694,17 @@ private int GetKeyLightBright(int clientId) } Log.Debug(Strings.GetString("svcGetKeyLightBright")); - bool success = LogECReadByte(Config.KeyLightConf.Reg, out byte value); - ServiceResponse response; - if (success) + if (LogECReadByte(Config.KeyLightConf.Reg, out byte value) && + value >= Config.KeyLightConf.MinVal && value <= Config.KeyLightConf.MaxVal) { int brightness = value - Config.KeyLightConf.MinVal; - response = value < Config.KeyLightConf.MinVal || value > Config.KeyLightConf.MaxVal - ? new(Response.Error, $"{(int)Command.GetKeyLightBright}") - : new(Response.KeyLightBright, $"{brightness}"); - } - else - { - response = new(Response.Error, $"{(int)Command.GetKeyLightBright}"); + IPCServer.PushMessage(new ServiceResponse( + Response.KeyLightBright, $"{brightness}"), clientId); + return 0; } - IPCServer.PushMessage(response, clientId); - return 0; + return 1; } private int SetKeyLightBright(int clientId, string args) @@ -715,19 +717,19 @@ private int SetKeyLightBright(int clientId, string args) if (ParseArgs(args, 1, out int[] pArgs)) { Log.Debug(Strings.GetString("svcSetKeyLightBright", pArgs[0])); - bool success = LogECWriteByte(Config.KeyLightConf.Reg, (byte)(pArgs[0] + Config.KeyLightConf.MinVal)); - ServiceResponse response = success - ? new(Response.Success, $"{Command.SetKeyLightBright}") - : new(Response.Error, $"{Command.SetKeyLightBright}"); - - IPCServer.PushMessage(response, clientId); - return 0; + if (LogECWriteByte(Config.KeyLightConf.Reg, (byte)(pArgs[0] + Config.KeyLightConf.MinVal))) + { + IPCServer.PushMessage(new ServiceResponse( + Response.Success, $"{Command.SetKeyLightBright}"), clientId); + return 0; + } + return 1; } return 2; } - private void FanCurveECToConf() + private void ECToConf() { if (!ConfigLoaded) { @@ -736,14 +738,14 @@ private void FanCurveECToConf() try { - Log.Info("Getting computer manufacturer and model..."); + Log.Info(Strings.GetString("svcReadingModel")); - string pcManufacturer = GetComputerManufacturer(), - pcModel = GetComputerModel(); + string pcManufacturer = GetPCManufacturer(), + pcModel = GetPCModel(); if (string.IsNullOrEmpty(pcManufacturer)) { - Log.Error("Failed to get computer manufacturer!"); + Log.Error(Strings.GetString("errReadManufacturer")); } else { @@ -752,7 +754,7 @@ private void FanCurveECToConf() if (string.IsNullOrEmpty(pcModel)) { - Log.Error("Failed to get computer model!"); + Log.Error(Strings.GetString("errReadModel")); } else { @@ -761,7 +763,7 @@ private void FanCurveECToConf() for (int i = 0; i < Config.FanConfs.Length; i++) { - Log.Info($"Getting fan curve from EC ({i + 1}/{Config.FanConfs.Length})..."); + Log.Info(Strings.GetString("svcReadingCurves", i + 1, Config.FanConfs.Length)); FanConf cfg = Config.FanConfs[i]; @@ -775,7 +777,7 @@ private void FanCurveECToConf() }; cfg.CurveSel = 0; } - cfg.FanCurveConfs[0].Desc = "The default fan curve of your computer.\n(auto-generated by YAMDCC)"; + cfg.FanCurveConfs[0].Desc = Strings.GetString("confDefaultDesc"); for (int j = 0; j < cfg.FanCurveRegs.Length; j++) { @@ -829,7 +831,7 @@ private void FanCurveECToConf() /// The computer model if the function succeeds, /// otherwise null. /// - private static string GetComputerModel() + private static string GetPCModel() { return GetBIOSRegValue("SystemProductName"); } @@ -841,7 +843,7 @@ private static string GetComputerModel() /// The computer manufacturer if the function succeeds, /// otherwise null. /// - private static string GetComputerManufacturer() + private static string GetPCManufacturer() { return GetBIOSRegValue("SystemManufacturer"); } @@ -858,5 +860,10 @@ private static string GetBIOSRegValue(string name) biosKey?.Close(); } } + + private static string GetWin32Error(int error) + { + return new Win32Exception(error).Message; + } } } diff --git a/YAMDCC.Service/Strings.resx b/YAMDCC.Service/Strings.resx index 13c20fe..7a80ef5 100644 --- a/YAMDCC.Service/Strings.resx +++ b/YAMDCC.Service/Strings.resx @@ -140,8 +140,8 @@ Please run YAMDCC configurator and apply a config at least once. Failed to load the WinRing0 driver, required for EC read/write access. -Check the FAQ for common fixes. If the issue still occurs, open a bug report -with this log attached. +Check the FAQ for common fixes. +If the issue still occurs, open a bug report with this log attached. WinRing0 driver loaded successfully! @@ -161,9 +161,6 @@ with this log attached. Unknown command received: {0} - - Could not acquire EC bus lock. - Failed to read from EC register 0x{0:X2}: {1} @@ -181,13 +178,13 @@ Args: {1} Client disconnected from service (ID: {0}) - This service provides functionality to apply settings from YAMDCC (Yet Another MSI Dragon Center Clone) configs to this computer. If this service's Startup Type is set to Manual, YAMDCC configs will not be applied on system startup. If this service is disabled, the YAMDCC configurator will not run, and YAMDCC configs will not be applied. + This service provides functionality to apply settings from YAMDCC configs to this computer. If this service's Startup Type is set to Manual, YAMDCC configs will not be applied on system startup. If this service is disabled, the YAMDCC configurator will not run, and YAMDCC configs will not be applied. Reading EC register 0x{0:X2}... - Successfully read EC register 0x{0:X2} (value: 0x{1:X2}). + Read from EC register 0x{0:X2} successfully (value: 0x{1:X2}). Wrote to EC register 0x{0:X2} successfully. @@ -199,13 +196,13 @@ Args: {1} Unhandled exception occurred: {0} - Service was started successfully! + Service started successfully! Starting YAMDCC service... - Service was stopped successfully. + Service stopped successfully. Stopping YAMDCC service... @@ -217,7 +214,7 @@ Args: {1} Writing custom EC register configs ({0}/{1})... - Writing fan curve configuration for {0} ({1}/{2})... + Writing fan curve configuration for `{0}` ({1}/{2})... Writing charge limit configuration... @@ -242,4 +239,23 @@ Args: {1} Please run 'YAMDCC.exe' instead. - + + The default fan curve of your computer. +(auto-generated by YAMDCC) + + + Getting computer manufacturer and model... + + + Getting fan curve from EC ({0}/{1})... + + + Failed to get computer model! + + + Failed to get computer manufacturer! + + + Resume from sleep detected, re-applying fan curve... + + \ No newline at end of file diff --git a/YAMDCC.Service/app.manifest b/YAMDCC.Service/app.manifest index e50cb77..e7a7341 100644 --- a/YAMDCC.Service/app.manifest +++ b/YAMDCC.Service/app.manifest @@ -1,8 +1 @@ - - - - - - - - + \ No newline at end of file