forked from antonpup/Aurora
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aytackydln
committed
Jul 9, 2024
1 parent
7710197
commit a025f0e
Showing
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
Project-Aurora/Project-Aurora/Modules/OnlineConfigs/Model/RazerDevices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace AuroraRgb.Modules.OnlineConfigs.Model; | ||
|
||
public class RazerDevices | ||
{ | ||
public Dictionary<string, RazerMouseHidInfo> MouseHidInfos { get; set; } = new(); | ||
} | ||
|
||
public class RazerMouseHidInfo | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
public string TransactionId { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
Project-Aurora/Project-Aurora/Nodes/Razer/RazerBatteryFetcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using AuroraRgb.Modules; | ||
using AuroraRgb.Modules.OnlineConfigs.Model; | ||
using LibUsbDotNet.LibUsb; | ||
using LibUsbDotNet.Main; | ||
|
||
namespace AuroraRgb.Nodes.Razer; | ||
|
||
class RazerBatteryFetcher : IDisposable | ||
{ | ||
private const int HidReqSetReport = 0x09; | ||
private const int HidReqGetReport = 0x01; // Add GET_REPORT request | ||
private const int UsbTypeClass = 0x20; | ||
private const int UsbRecipInterface = 0x01; | ||
private const int UsbDirOut = 0x00; | ||
private const int UsbDirIn = 0x80; // Direction IN for reading | ||
private const int UsbTypeRequestOut = UsbTypeClass | UsbRecipInterface | UsbDirOut; | ||
private const int UsbTypeRequestIn = UsbTypeClass | UsbRecipInterface | UsbDirIn; | ||
private const int RazerUsbReportLen = 90; // Example length, set this according to actual length | ||
|
||
public double MouseBatteryPercentage { get; private set; } | ||
private readonly Timer _timer; | ||
|
||
public RazerBatteryFetcher() | ||
{ | ||
_timer = new Timer(_ => UpdateBattery(), null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20)); | ||
} | ||
|
||
private byte[] GenerateMessage(RazerMouseHidInfo mouseHidInfo) | ||
{ | ||
var tid = byte.Parse(mouseHidInfo.TransactionId.Split('x')[1], System.Globalization.NumberStyles.HexNumber); | ||
var header = new byte[] { 0x00, tid, 0x00, 0x00, 0x00, 0x02, 0x07, 0x80 }; | ||
|
||
var crc = 0; | ||
for (var i = 2; i < header.Length; i++) | ||
{ | ||
crc ^= header[i]; | ||
} | ||
|
||
var data = new byte[80]; | ||
var crcData = new byte[] { (byte)crc, 0 }; | ||
|
||
return header.Concat(data).Concat(crcData).ToArray(); | ||
} | ||
|
||
private void UpdateBattery() | ||
{ | ||
const int vendorId = 0x1532; | ||
|
||
Mutex mutex = new(false, "Global\\RazerLinkReadWriteGuardMutex"); | ||
|
||
try | ||
{ | ||
if (!mutex.WaitOne(TimeSpan.FromMilliseconds(2000), false)) | ||
{ | ||
mutex.Dispose(); | ||
return; | ||
} | ||
} | ||
catch (AbandonedMutexException) | ||
{ | ||
//continue | ||
} | ||
|
||
var res = GetValue(vendorId); | ||
mutex.Dispose(); | ||
|
||
if (res == null) | ||
{ | ||
return; | ||
} | ||
|
||
MouseBatteryPercentage = res[9]; | ||
} | ||
|
||
private byte[]? GetValue(int vendorId) | ||
{ | ||
var mouseDictionary = OnlineSettings.RazerDeviceInfo.MouseHidInfos; | ||
|
||
using var context = new UsbContext(); | ||
var usbDevice = context.Find(d => | ||
d.VendorId == vendorId && | ||
mouseDictionary.ContainsKey(GetDeviceProductKeyString(d))); | ||
if (usbDevice == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var mouseHidInfo = mouseDictionary[GetDeviceProductKeyString(usbDevice)]; | ||
var msg = GenerateMessage(mouseHidInfo); | ||
|
||
usbDevice.Open(); | ||
RazerSendControlMsg(usbDevice, msg, 0x09, 200, 2000); | ||
var res = RazerReadResponseMsg(usbDevice, 0x01); | ||
usbDevice.Close(); | ||
usbDevice.Dispose(); | ||
return res; | ||
} | ||
|
||
private static string GetDeviceProductKeyString(IUsbDevice device) | ||
{ | ||
return "0x"+device.ProductId.ToString("X4"); | ||
} | ||
|
||
private static void RazerSendControlMsg(IUsbDevice usbDev, byte[] data, uint reportIndex, int waitMin, int waitMax) | ||
{ | ||
const ushort value = 0x300; | ||
|
||
var setupPacket = new UsbSetupPacket(UsbTypeRequestOut, HidReqSetReport, value, (ushort)reportIndex, (ushort)data.Length); | ||
|
||
// Send USB control message | ||
var transferredLength = data.Length; | ||
var ec = usbDev.ControlTransfer(setupPacket, data, 0, transferredLength); | ||
if (ec == 0) | ||
{ | ||
return; | ||
} | ||
|
||
// Wait | ||
var waitTime = new Random().Next(waitMin, waitMax); | ||
Thread.Sleep(waitTime); | ||
} | ||
|
||
private static byte[]? RazerReadResponseMsg(IUsbDevice usbDev, uint reportIndex) | ||
{ | ||
const ushort value = 0x300; | ||
var responseBuffer = new byte[RazerUsbReportLen]; | ||
|
||
var setupPacket = new UsbSetupPacket(UsbTypeRequestIn, HidReqGetReport, value, (ushort)reportIndex, (ushort)responseBuffer.Length); | ||
|
||
// Receive USB control message | ||
var transferredLength = responseBuffer.Length; | ||
var ec = usbDev.ControlTransfer(setupPacket, responseBuffer, 0, transferredLength); | ||
if (ec == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
return transferredLength != responseBuffer.Length ? null : responseBuffer; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_timer.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using AuroraRgb.Nodes.Razer; | ||
using AuroraRgb.Utils; | ||
|
||
namespace AuroraRgb.Nodes; | ||
|
||
public class RazerDevices : Node | ||
{ | ||
private Temporary<RazerBatteryFetcher> _razerBatteryFetcher = new(() => new RazerBatteryFetcher()); | ||
|
||
public double MouseBatteryPercentage => _razerBatteryFetcher.Value.MouseBatteryPercentage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters