Skip to content

Commit

Permalink
Simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
buldo committed Jan 7, 2024
1 parent fdb0f4d commit b37bfea
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla
return StartCommandResult.Sticky;
}

private Task PacketProcessor(ParsedRadioPacket arg)
private void PacketProcessor(ParsedRadioPacket arg)
{
_logger.LogDebug("Received");
return Task.CompletedTask;
}

private SelectedChannel CreateCurrentChannel()
Expand Down
16 changes: 11 additions & 5 deletions src/Rtl8812auNet.AndroidDemo/RtlUsb/RtlUsbDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Channels;
#nullable enable
using System.Threading.Channels;
using Android.Hardware.Usb;
using Microsoft.Extensions.Logging;

Expand All @@ -8,11 +9,12 @@ namespace Rtl8812auNet.AndroidDemo.RtlUsb
{
internal class RtlUsbDevice : IRtlUsbDevice
{
private readonly Channel<byte[]> _bulkTransfersChannel = Channel.CreateUnbounded<byte[]>();
private readonly UsbDevice _usbDevice;
private readonly UsbDeviceConnection _usbDeviceConnection;
private readonly ILogger<RtlUsbDevice> _logger;

private BulkDataHandler? _bulkDataHandler;

public RtlUsbDevice(
UsbDevice usbDevice,
UsbDeviceConnection usbDeviceConnection,
Expand All @@ -28,6 +30,12 @@ public RtlUsbDevice(

//_reader = _usbDevice.OpenEndpointReader(GetInEp());
}

public void SetBulkDataHandler(BulkDataHandler handler)
{
_bulkDataHandler = handler;
}

public void InfinityRead()
{
var ep = GetInEp();
Expand All @@ -43,7 +51,7 @@ public void InfinityRead()
}
else
{
_bulkTransfersChannel.Writer.TryWrite(readBuffer.AsSpan(0, length).ToArray());
_bulkDataHandler?.Invoke(readBuffer.AsSpan(0, length));
}
}
catch (Exception e)
Expand Down Expand Up @@ -71,8 +79,6 @@ private UsbEndpoint GetInEp()

public int Speed { get; } = 3;

public ChannelReader<byte[]> BulkTransfersReader => _bulkTransfersChannel.Reader;

public void WriteBytes(ushort register, Span<byte> data)
{
_usbDeviceConnection.ControlTransfer(
Expand Down
4 changes: 1 addition & 3 deletions src/Rtl8812auNet.ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ static void Main(string[] args)
Console.WriteLine("End");
}

private static Task PacketProcessor(ParsedRadioPacket packet)
private static void PacketProcessor(ParsedRadioPacket packet)
{
Console.WriteLine($"Packet received. Data len: {packet.Data.Length}");

return Task.CompletedTask;
}
}
8 changes: 4 additions & 4 deletions src/Rtl8812auNet/Abstractions/IRtlUsbDevice.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System.Threading.Channels;
namespace Rtl8812auNet.Abstractions;

namespace Rtl8812auNet.Abstractions;
public delegate void BulkDataHandler(ReadOnlySpan<byte> dataHandler);

public interface IRtlUsbDevice
{
int Speed { get; }

public ChannelReader<byte[]> BulkTransfersReader { get; }
void SetBulkDataHandler(BulkDataHandler handler);

void InfinityRead();

void WriteBytes(ushort register, Span<byte> data);

ReadOnlySpan<byte> ReadBytes(ushort register, ushort bytesCount);

public List<IRtlEndpoint> GetEndpoints();
List<IRtlEndpoint> GetEndpoints();
}
11 changes: 7 additions & 4 deletions src/Rtl8812auNet/LibUsbDotNet/LibUsbRtlUsbDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if WINDOWS
using System.Threading.Channels;
#nullable enable
using LibUsbDotNet;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.Main;
Expand All @@ -17,7 +17,7 @@ public class LibUsbRtlUsbDevice : IRtlUsbDevice
private readonly ILogger<LibUsbRtlUsbDevice> _logger;
private readonly UsbEndpointReader _reader;

private readonly Channel<byte[]> _bulkTransfersChannel = Channel.CreateUnbounded<byte[]>();
private BulkDataHandler? _bulkDataHandler;

public LibUsbRtlUsbDevice(
UsbDevice usbDevice,
Expand All @@ -32,7 +32,10 @@ public LibUsbRtlUsbDevice(
_reader = _usbDevice.OpenEndpointReader(GetInEp());
}

public ChannelReader<byte[]> BulkTransfersReader => _bulkTransfersChannel.Reader;
public void SetBulkDataHandler(BulkDataHandler handler)
{
_bulkDataHandler = handler;
}

public void InfinityRead()
{
Expand All @@ -53,7 +56,7 @@ public void InfinityRead()

if (len != 0)
{
_bulkTransfersChannel.Writer.TryWrite(readBuffer.AsSpan(0, len).ToArray());
_bulkDataHandler?.Invoke(readBuffer.AsSpan(0, len));
}
}
catch (Exception e)
Expand Down
2 changes: 0 additions & 2 deletions src/Rtl8812auNet/ParsedRadioPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace Rtl8812auNet;

public class ParsedRadioPacket
{
public byte[] UsbBulkTransfer { get; set; }

public byte[] Data { get; set; } = Array.Empty<byte>();
public rx_pkt_attrib Attr { get; set; }
}
10 changes: 10 additions & 0 deletions src/Rtl8812auNet/Rtl8812au/Enumerations/Bits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public static UInt32 LE_BITS_TO_4BYTE(Span<byte> __pStart, int __BitOffset, int
return LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> __BitOffset & BIT_LEN_MASK_32(__BitLen);
}

public static UInt32 LE_BITS_TO_4BYTE(ReadOnlySpan<byte> __pStart, int __BitOffset, int __BitLen)
{
return LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> __BitOffset & BIT_LEN_MASK_32(__BitLen);
}

public static UInt32 LE_BITS_TO_4BYTE(byte[] __pStart, int __BitOffset, int __BitLen)
{
return LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> __BitOffset & BIT_LEN_MASK_32(__BitLen);
Expand All @@ -100,6 +105,11 @@ private static UInt32 LE_P4BYTE_TO_HOST_4BYTE(Span<byte> __pStart)
return BinaryPrimitives.ReadUInt32LittleEndian(__pStart);
}

private static UInt32 LE_P4BYTE_TO_HOST_4BYTE(ReadOnlySpan<byte> __pStart)
{
return BinaryPrimitives.ReadUInt32LittleEndian(__pStart);
}

private static UInt32 LE_P4BYTE_TO_HOST_4BYTE(byte[] __pStart)
{
//(le32_to_cpu(*((UInt32*)(__pStart))))
Expand Down
51 changes: 25 additions & 26 deletions src/Rtl8812auNet/Rtl8812au/FrameParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ public FrameParser(ILogger<FrameParser> logger)
_logger = logger;
}

public List<ParsedRadioPacket> ParsedRadioPacket(byte[] usbTransfer)
public List<ParsedRadioPacket> ParsedRadioPacket(ReadOnlySpan<byte> usbTransfer)
{

return recvbuf2recvframe(usbTransfer)
.Select(tuple => new ParsedRadioPacket
{
UsbBulkTransfer = usbTransfer,
Data = tuple.Data,
Attr = tuple.RxAtrib
})
.ToList();
}

private List<(rx_pkt_attrib RxAtrib, byte[] Data)> recvbuf2recvframe(byte[] ptr)
private List<(rx_pkt_attrib RxAtrib, byte[] Data)> recvbuf2recvframe(ReadOnlySpan<byte> ptr)
{
var transfer_len = ptr.Length;
var pbuf = ptr.AsSpan();
var pbuf = ptr;
var pkt_cnt = GET_RX_STATUS_DESC_USB_AGG_PKTNUM_8812(pbuf);
_logger.LogInformation($"pkt_cnt == {pkt_cnt}");

Expand Down Expand Up @@ -112,7 +111,7 @@ public List<ParsedRadioPacket> ParsedRadioPacket(byte[] usbTransfer)
return ret;
}

private static uint GET_RX_STATUS_DESC_USB_AGG_PKTNUM_8812(Span<byte> __pRxStatusDesc) =>
private static uint GET_RX_STATUS_DESC_USB_AGG_PKTNUM_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc.Slice(12), 16, 8);

private static UInt32 _RND8(int sz)
Expand All @@ -121,7 +120,7 @@ private static UInt32 _RND8(int sz)
return val;
}

private static rx_pkt_attrib rtl8812_query_rx_desc_status(Span<byte> pdesc)
private static rx_pkt_attrib rtl8812_query_rx_desc_status(ReadOnlySpan<byte> pdesc)
{
var pattrib = new rx_pkt_attrib();

Expand Down Expand Up @@ -169,63 +168,63 @@ private static rx_pkt_attrib rtl8812_query_rx_desc_status(Span<byte> pdesc)
return pattrib;
}

private static UInt16 GET_RX_STATUS_DESC_PKT_LEN_8812(Span<byte> __pRxStatusDesc)
private static UInt16 GET_RX_STATUS_DESC_PKT_LEN_8812(ReadOnlySpan<byte> __pRxStatusDesc)
=> (UInt16)LE_BITS_TO_4BYTE(__pRxStatusDesc, 0, 14);

private static bool GET_RX_STATUS_DESC_CRC32_8812(Span<byte> __pRxStatusDesc)
private static bool GET_RX_STATUS_DESC_CRC32_8812(ReadOnlySpan<byte> __pRxStatusDesc)
=> LE_BITS_TO_4BYTE(__pRxStatusDesc, 14, 1) != 0;

private static bool GET_RX_STATUS_DESC_ICV_8812(Span<byte> __pRxStatusDesc) =>
private static bool GET_RX_STATUS_DESC_ICV_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc, 15, 1) != 0;

private static byte GET_RX_STATUS_DESC_DRVINFO_SIZE_8812(Span<byte> __pRxStatusDesc) =>
private static byte GET_RX_STATUS_DESC_DRVINFO_SIZE_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxStatusDesc, 16, 4);

private static byte GET_RX_STATUS_DESC_SECURITY_8812(Span<byte> __pRxStatusDesc) =>
private static byte GET_RX_STATUS_DESC_SECURITY_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxStatusDesc, 20, 3);

private static bool GET_RX_STATUS_DESC_QOS_8812(Span<byte> __pRxStatusDesc) =>
private static bool GET_RX_STATUS_DESC_QOS_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc, 23, 1) != 0;

private static byte GET_RX_STATUS_DESC_SHIFT_8812(Span<byte> __pRxStatusDesc) =>
private static byte GET_RX_STATUS_DESC_SHIFT_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxStatusDesc, 24, 2);

private static bool GET_RX_STATUS_DESC_PHY_STATUS_8812(Span<byte> __pRxStatusDesc) =>
private static bool GET_RX_STATUS_DESC_PHY_STATUS_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc, 26, 1) != 0;

private static bool GET_RX_STATUS_DESC_SWDEC_8812(Span<byte> __pRxStatusDesc) =>
private static bool GET_RX_STATUS_DESC_SWDEC_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc, 27, 1) != 0;

private static byte GET_RX_STATUS_DESC_TID_8812(Span<byte> __pRxDesc) =>
private static byte GET_RX_STATUS_DESC_TID_8812(ReadOnlySpan<byte> __pRxDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxDesc.Slice(4), 8, 4);

private static bool GET_RX_STATUS_DESC_MORE_DATA_8812(Span<byte> __pRxDesc) =>
private static bool GET_RX_STATUS_DESC_MORE_DATA_8812(ReadOnlySpan<byte> __pRxDesc) =>
LE_BITS_TO_4BYTE(__pRxDesc.Slice(4), 26, 1) != 0;

private static bool GET_RX_STATUS_DESC_MORE_FRAG_8812(Span<byte> __pRxDesc) =>
private static bool GET_RX_STATUS_DESC_MORE_FRAG_8812(ReadOnlySpan<byte> __pRxDesc) =>
LE_BITS_TO_4BYTE(__pRxDesc.Slice(4), 27, 1) != 0;

private static UInt16 GET_RX_STATUS_DESC_SEQ_8812(Span<byte> __pRxStatusDesc) =>
private static UInt16 GET_RX_STATUS_DESC_SEQ_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(UInt16)LE_BITS_TO_4BYTE(__pRxStatusDesc.Slice(8), 0, 12);

private static byte GET_RX_STATUS_DESC_FRAG_8812(Span<byte> __pRxStatusDesc) =>
private static byte GET_RX_STATUS_DESC_FRAG_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxStatusDesc.Slice(8), 12, 4);

private static bool GET_RX_STATUS_DESC_RPT_SEL_8812(Span<byte> __pRxStatusDesc) =>
private static bool GET_RX_STATUS_DESC_RPT_SEL_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
LE_BITS_TO_4BYTE(__pRxStatusDesc.Slice(8), 28, 1) != 0;

private static byte GET_RX_STATUS_DESC_RX_RATE_8812(Span<byte> __pRxStatusDesc) =>
private static byte GET_RX_STATUS_DESC_RX_RATE_8812(ReadOnlySpan<byte> __pRxStatusDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxStatusDesc.Slice(12), 0, 7);

private static byte GET_RX_STATUS_DESC_SPLCP_8812(Span<byte> __pRxDesc) =>
private static byte GET_RX_STATUS_DESC_SPLCP_8812(ReadOnlySpan<byte> __pRxDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxDesc.Slice(16), 0, 1);

private static byte GET_RX_STATUS_DESC_LDPC_8812(Span<byte> __pRxDesc) =>
private static byte GET_RX_STATUS_DESC_LDPC_8812(ReadOnlySpan<byte> __pRxDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxDesc.Slice(16), 1, 1);

private static byte GET_RX_STATUS_DESC_STBC_8812(Span<byte> __pRxDesc) =>
private static byte GET_RX_STATUS_DESC_STBC_8812(ReadOnlySpan<byte> __pRxDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxDesc.Slice(16), 2, 1);

private static byte GET_RX_STATUS_DESC_BW_8812(Span<byte> __pRxDesc) =>
private static byte GET_RX_STATUS_DESC_BW_8812(ReadOnlySpan<byte> __pRxDesc) =>
(byte)LE_BITS_TO_4BYTE(__pRxDesc.Slice(16), 4, 2);
}
31 changes: 14 additions & 17 deletions src/Rtl8812auNet/Rtl8812au/Rtl8812aDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;

using Microsoft.Extensions.Logging;
using Rtl8812auNet.Rtl8812au.Enumerations;
using Rtl8812auNet.Rtl8812au.Models;
using Rtl8812auNet.Rtl8812au.Modules;
Expand All @@ -13,9 +14,8 @@ public class Rtl8812aDevice
private readonly RadioManagementModule _radioManagement;

private Task _readTask;
private Task _parseTask;
private readonly HalModule _halModule;
private Func<ParsedRadioPacket, Task> _packetProcessor;
private Action<ParsedRadioPacket> _packetProcessor;

internal Rtl8812aDevice(
RtlUsbAdapter device,
Expand All @@ -35,16 +35,16 @@ internal Rtl8812aDevice(
}

public void Init(
Func<ParsedRadioPacket, Task> packetProcessor,
Action<ParsedRadioPacket> packetProcessor,
SelectedChannel channel)
{
_packetProcessor = packetProcessor;

StartWithMonitorMode(channel);
SetMonitorChannel(channel);

_device.UsbDevice.SetBulkDataHandler(BulkDataHandler);
_readTask = Task.Run(() => _device.UsbDevice.InfinityRead());
_parseTask = Task.Run(ParseUsbData);
}

public void SetMonitorChannel(SelectedChannel channel)
Expand Down Expand Up @@ -73,21 +73,18 @@ private bool NetDevOpen(SelectedChannel selectedChannel)
return true;
}

private async Task ParseUsbData()
private void BulkDataHandler(ReadOnlySpan<byte> data)
{
await foreach (var transfer in _device.UsbDevice.BulkTransfersReader.ReadAllAsync())
var packet = _frameParser.ParsedRadioPacket(data);
foreach (var radioPacket in packet)
{
var packet = _frameParser.ParsedRadioPacket(transfer);
foreach (var radioPacket in packet)
try
{
try
{
await _packetProcessor(radioPacket);
}
catch (Exception e)
{
_logger.LogError(e, "_packetProcessor Exception");
}
_packetProcessor(radioPacket);
}
catch (Exception e)
{
_logger.LogError(e, "_packetProcessor Exception");
}
}
}
Expand Down

0 comments on commit b37bfea

Please sign in to comment.