Skip to content

Commit

Permalink
[icue-link] fix subdevice packet read
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanMulawski committed Dec 20, 2024
1 parent b78b324 commit d0c9d75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/devices/icue_link/ICueLinkHubDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static class DataTypes
public static ReadOnlySpan<byte> Temperatures => new byte[] { 0x10, 0x00 };
public static ReadOnlySpan<byte> SoftwareSpeedFixedPercent => new byte[] { 0x07, 0x00 };
public static ReadOnlySpan<byte> SubDevices => new byte[] { 0x21, 0x00 };
public static ReadOnlySpan<byte> SubDevicesContinuation => new byte[] { 0x37, 0x31 };
public static ReadOnlySpan<byte> Continuation => new byte[] { };
}

private const int DEFAULT_SPEED_CHANNEL_POWER = 50;
Expand Down Expand Up @@ -193,13 +193,13 @@ private void RefreshImpl(bool initialize = false)
SendCommand(Commands.CloseEndpoint, Endpoints.GetSubDevices);
SendCommand(Commands.OpenEndpoint, Endpoints.GetSubDevices);
res1 = SendCommand(Commands.Read, waitForDataType: DataTypes.SubDevices);
res2 = SendCommand(Commands.Read, waitForDataType: DataTypes.SubDevicesContinuation);
res2 = SendCommand(Commands.Read);
SendCommand(Commands.CloseEndpoint, Endpoints.GetSubDevices);
}

return [
new EndpointResponse(res1, DataTypes.SubDevices),
new EndpointResponse(res2, DataTypes.SubDevicesContinuation),
new EndpointResponse(res2, DataTypes.Continuation),
];
}

Expand Down Expand Up @@ -521,6 +521,6 @@ public EndpointResponse(ReadOnlySpan<byte> payload, ReadOnlySpan<byte> dataType)
public byte[] Payload { get; }
public byte[] DataType { get; }

public ReadOnlySpan<byte> GetData() => Payload.AsSpan().Slice(6);
public ReadOnlySpan<byte> GetData() => Payload.AsSpan().Slice(4 + DataType.Length);
}
}
28 changes: 16 additions & 12 deletions src/devices/icue_link/LinkHubDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ public static class LinkHubDataReader
{
public static IReadOnlyCollection<LinkHubConnectedDevice> GetDevices(ReadOnlySpan<byte> subDevicesPacket, ReadOnlySpan<byte> subDevicesContinuationPacket = default)
{
var continuationChannel = subDevicesPacket[6];

IReadOnlyCollection<LinkHubConnectedDevice> ParsePacket(ReadOnlySpan<byte> packet, int startingChannel)
static IReadOnlyCollection<LinkHubConnectedDevice> ParsePacket(ReadOnlySpan<byte> packetData)
{
// payload format:
// [6] = last channel index
// [7,] = devices
// [0] = last channel index
// [1,] = devices

// device format:
// [2] = device type
Expand All @@ -27,16 +25,16 @@ IReadOnlyCollection<LinkHubConnectedDevice> ParsePacket(ReadOnlySpan<byte> packe

var devices = new List<LinkHubConnectedDevice>();

if (packet.Length == 0)
if (packetData.Length == 0)
{
return devices;
}

var lastChannel = packet[6];
var d = packet.Slice(7);
var lastChannel = packetData[0];
var d = packetData.Slice(1);
var i = 0;

for (int ch = startingChannel; ch <= lastChannel; ch++)
for (int ch = 1; ch <= lastChannel; ch++)
{
var deviceIdLength = d[i + 7];
if (deviceIdLength == 0)
Expand Down Expand Up @@ -68,10 +66,16 @@ IReadOnlyCollection<LinkHubConnectedDevice> ParsePacket(ReadOnlySpan<byte> packe
return devices;
}

var devices1 = ParsePacket(subDevicesPacket, startingChannel: 1);
var devices2 = ParsePacket(subDevicesContinuationPacket, startingChannel: continuationChannel);
var subDevicesPacketData1 = subDevicesPacket.Slice(6);
var subDevicesPacketData2 = subDevicesContinuationPacket.Length > 4 ? subDevicesContinuationPacket.Slice(4) : [];

var subDevicesFullPacketData = new byte[subDevicesPacketData1.Length + subDevicesPacketData2.Length];
subDevicesPacketData1.CopyTo(subDevicesFullPacketData);
subDevicesPacketData2.CopyTo(subDevicesFullPacketData.AsSpan(subDevicesPacketData1.Length));

var devices = ParsePacket(subDevicesFullPacketData);

return devices1.Union(devices2).ToList();
return devices;
}

public static string GetFirmwareVersion(ReadOnlySpan<byte> packet)
Expand Down

0 comments on commit d0c9d75

Please sign in to comment.