Skip to content

Commit

Permalink
refactor: add/update message decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed May 10, 2024
1 parent 89fd2fb commit 14e14fe
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ private void OnGetAllDataResponse(object? sender, DebugGetAllDataDecoder e)
HwVersion = e.HardwareVersion ?? Unknown;
SwVersion = e.SoftwareVersion ?? Unknown;
TouchSwVersion = e.TouchSoftwareVersion ?? Unknown;
BluetoothAddress = e is { LeftBluetoothAddress: not null, RightBluetoothAddress: not null }
? $"{Strings.Left}: {e.LeftBluetoothAddress}, {Strings.Right}: {e.RightBluetoothAddress}"
BluetoothAddress = e.LocalBluetoothAddress != null || e.PeerBluetoothAddress != null
? string.Format(Strings.SystemBtaddrTemplate, e.LocalBluetoothAddress ?? Unknown, e.PeerBluetoothAddress ?? Unknown)
: Unknown;
}

Expand Down
33 changes: 33 additions & 0 deletions GalaxyBudsClient/Message/Decoder/CouplingHelperReadDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.IO;
using GalaxyBudsClient.Generated.Model.Attributes;
using GalaxyBudsClient.Model.Constants;
using GalaxyBudsClient.Utils.Extensions;

namespace GalaxyBudsClient.Message.Decoder;

[MessageDecoder(MsgIds.COUPLING_HELPER_READ)]
public class CouplingHelperReadDecoder : BaseMessageDecoder
{
public string FirmwareVersion { get; }
public string LocalAddress { get; }
public string PeerAddress { get; }
public DevicesInverted HostDevice { get; }
public bool BridgeStatus { get; }
public byte RcvGrade { get; }

public CouplingHelperReadDecoder(SppMessage msg) : base(msg)
{
using var stream = new MemoryStream(msg.Payload);
using var reader = new BinaryReader(stream);

// Note: This string is normally 12 bytes long, the last 8 bytes are always null
FirmwareVersion = new string(reader.ReadChars(20));

LocalAddress = reader.ReadBytes(6).BytesToMacString();
PeerAddress = reader.ReadBytes(6).BytesToMacString();

HostDevice = (DevicesInverted)reader.ReadByte();
BridgeStatus = reader.ReadBoolean();
RcvGrade = reader.ReadByte();
}
}
28 changes: 7 additions & 21 deletions GalaxyBudsClient/Message/Decoder/DebugGetAllDataDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GalaxyBudsClient.Generated.Model.Attributes;
using GalaxyBudsClient.Model.Attributes;
using GalaxyBudsClient.Model.Constants;
using GalaxyBudsClient.Utils.Extensions;

namespace GalaxyBudsClient.Message.Decoder;

Expand All @@ -18,8 +19,8 @@ public class DebugGetAllDataDecoder : BaseMessageDecoder
private readonly string[] _swYear = ["O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
public string? HardwareVersion { get; }
public string? SoftwareVersion { get; }
public string? LeftBluetoothAddress { get; }
public string? RightBluetoothAddress { get; }
public string? LocalBluetoothAddress { get; }
public string? PeerBluetoothAddress { get; }
public string? TouchSoftwareVersion { get; }
public short LeftAcceleratorX { get; }
public short LeftAcceleratorY { get; }
Expand Down Expand Up @@ -100,8 +101,8 @@ public DebugGetAllDataDecoder(SppMessage msg) : base(msg)
HardwareVersion = "rev" + hw1.ToString("X") + "." + hw2.ToString("X");
SoftwareVersion = VersionDataToString(msg.Payload, 1);
TouchSoftwareVersion = $"0x{msg.Payload[4]:X}";
LeftBluetoothAddress = BytesToMacString(msg.Payload, 5);
RightBluetoothAddress = BytesToMacString(msg.Payload, 11);
LocalBluetoothAddress = msg.Payload.BytesToMacString(5);
PeerBluetoothAddress = msg.Payload.BytesToMacString(11);

LeftAcceleratorX = BitConverter.ToInt16(msg.Payload, 17);
LeftAcceleratorY = BitConverter.ToInt16(msg.Payload, 19);
Expand Down Expand Up @@ -136,8 +137,8 @@ public DebugGetAllDataDecoder(SppMessage msg) : base(msg)
HardwareVersion = "rev" + hw1.ToString("X") + "." + hw2.ToString("X");
SoftwareVersion = VersionDataToString(msg.Payload, 2);
TouchSoftwareVersion = $"0x{msg.Payload[5]:X}";
LeftBluetoothAddress = BytesToMacString(msg.Payload, 6);
RightBluetoothAddress = BytesToMacString(msg.Payload, 12);
LocalBluetoothAddress = msg.Payload.BytesToMacString(6);
PeerBluetoothAddress = msg.Payload.BytesToMacString(12);

LeftAcceleratorX = BitConverter.ToInt16(msg.Payload, 18);
LeftAcceleratorY = BitConverter.ToInt16(msg.Payload, 20);
Expand Down Expand Up @@ -186,21 +187,6 @@ public DebugGetAllDataDecoder(SppMessage msg) : base(msg)
RightCradleBatt = msg.Payload[82];
}
}

private string BytesToMacString(IReadOnlyList<byte> payload, int startIndex)
{
var sb = new StringBuilder();
for (var i13 = 0; i13 < 6; i13++)
{
if (i13 != 0)
{
sb.Append(':');
}
sb.Append(((payload[i13 + startIndex] & 240) >> 4).ToString("X"));
sb.Append((payload[i13 + startIndex] & 15).ToString("X"));
}
return sb.ToString();
}

private string VersionDataToString(IReadOnlyList<byte> payload, int startIndex)
{
Expand Down
23 changes: 18 additions & 5 deletions GalaxyBudsClient/Message/Decoder/GenericEventDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using GalaxyBudsClient.Generated.Model.Attributes;
using GalaxyBudsClient.Model.Constants;

Expand All @@ -8,17 +9,29 @@ namespace GalaxyBudsClient.Message.Decoder;
public class GenericEventDecoder : BaseMessageDecoder
{
public Devices Device { get; }
public uint Timestamp { get; }
public TimeSpan Timestamp { get; }
public byte EventId { get; }
// Other fields are unknown
public MsgTypes MessageType { get; }
public byte[] EventData { get; }
// EventIds and EventData contents are unknown

public GenericEventDecoder(SppMessage msg) : base(msg)
{
using var stream = new MemoryStream(msg.Payload);
using var reader = new BinaryReader(stream);

Device = reader.ReadChar() == 'L' ? Devices.L : Devices.R;
Timestamp = reader.ReadUInt32();
EventId = reader.ReadByte();
Timestamp = TimeSpan.FromMilliseconds(reader.ReadUInt32());
EventId = reader.ReadByte();
MessageType = (MsgTypes)reader.ReadByte();

try
{
EventData = msg.Payload[7..];
}
catch (Exception)
{
EventData = Array.Empty<byte>();
}
}
}
2 changes: 1 addition & 1 deletion GalaxyBudsClient/Message/SppMessageEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public enum MsgIds
FIND_MY_EARBUDS_ON_WEARING_START = 166, // previously: NOTIFICATION_INFO
UPDATE_TIME = 167,
START_VOICE_RECORD = 168,
UNK_CONN_INFO = 169,
COUPLING_HELPER_READ = 169,
UNKNOWN_170 = 170,
SELF_TEST = 171,
SET_FMM_CONFIG = 172,
Expand Down
2 changes: 1 addition & 1 deletion GalaxyBudsClient/Scripting/Experiment/ExperimentResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ExperimentResult
public Models Device => BluetoothImpl.Instance.CurrentModel;
public int Revision => DeviceMessageCache.Instance.ExtendedStatusUpdate?.Revision ?? 0;
public string FirmwareVersion => DeviceMessageCache.Instance.DebugGetAllData?.SoftwareVersion ?? "Unknown";
public string MacAddress => BitConverter.ToString(SHA1.HashData(Encoding.ASCII.GetBytes(DeviceMessageCache.Instance.DebugGetAllData?.LeftBluetoothAddress ?? ""))).Replace("-", "");
public string MacAddress => BitConverter.ToString(SHA1.HashData(Encoding.ASCII.GetBytes(DeviceMessageCache.Instance.DebugGetAllData?.PeerBluetoothAddress ?? ""))).Replace("-", "");
public string AppVersion => Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "Unknown";
public string CountryCode => RegionInfo.CurrentRegion.TwoLetterISORegionName;
public PlatformUtils.Platforms Platform => PlatformUtils.Platform;
Expand Down
17 changes: 17 additions & 0 deletions GalaxyBudsClient/Utils/Extensions/DataExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace GalaxyBudsClient.Utils.Extensions;

Expand All @@ -14,6 +16,21 @@ public static IDisposable ScopedSeek(this Stream stream, long offset, SeekOrigin
stream.Seek(offset, origin);
return Finally.Create(() => stream.Seek(prevPosition, SeekOrigin.Begin));
}

public static string BytesToMacString(this IReadOnlyList<byte> payload, int startIndex = 0)
{
var sb = new StringBuilder();
for (var i13 = 0; i13 < 6; i13++)
{
if (i13 != 0)
{
sb.Append(':');
}
sb.Append(((payload[i13 + startIndex] & 240) >> 4).ToString("X"));
sb.Append((payload[i13 + startIndex] & 15).ToString("X"));
}
return sb.ToString();
}
}

public readonly struct Finally : IDisposable
Expand Down
1 change: 1 addition & 0 deletions GalaxyBudsClient/i18n/en.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ This program is designed to provide much more functionality over the official Sa
<sys:String x:Key="system_touchver">Touch firmware version</sys:String>
<sys:String x:Key="system_protover">Protocol revision</sys:String>
<sys:String x:Key="system_btaddr">Bluetooth addresses</sys:String>
<sys:String x:Key="system_btaddr_template">Local address: {0}, Peer address: {1}</sys:String>
<sys:String x:Key="system_serial">Serial numbers</sys:String>
<sys:String x:Key="system_build_info">Build string</sys:String>
<sys:String x:Key="system_battery_type">Battery type</sys:String>
Expand Down

0 comments on commit 14e14fe

Please sign in to comment.