Skip to content

Commit

Permalink
Add formatting to the MAC Address DeviceInfo
Browse files Browse the repository at this point in the history
#154 non-breaking
  • Loading branch information
tthiery committed Apr 30, 2021
1 parent d8e5856 commit a39594c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private static (IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo, SystemType sy
var deviceType = (PoweredUpHubManufacturerData)info.ManufacturerData[1];
devices.Add((idx, info, deviceType));
var text = (info is IPoweredUpBluetoothDeviceInfoWithMacAddress mac) ? mac.MacAddress[0].ToString() : "not revealed";
var text = (info is IPoweredUpBluetoothDeviceInfoWithMacAddress mac) ? mac.ToIdentificationString() : "not revealed";
Console.WriteLine($"{idx}: {(PoweredUpHubManufacturerData)info.ManufacturerData[1]} (with address {text})");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
using System.Linq;

namespace SharpBrick.PoweredUp.Bluetooth
{
public interface IPoweredUpBluetoothDeviceInfoWithMacAddress
{
byte[] MacAddress { get; }
}

public static class IPoweredUpBluetoothDeviceInfoWithMacAddressExtensions
{
public static string ToIdentificationString(this IPoweredUpBluetoothDeviceInfoWithMacAddress self)
=> string.Join(":", self.MacAddress.Select(b => b.ToString("X2")).ToArray());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace SharpBrick.PoweredUp.Bluetooth
{
Expand All @@ -7,7 +8,7 @@ public class PoweredUpBluetoothDeviceInfoWithMacAddress : IPoweredUpBluetoothDev
public string Name { get; set; }
public byte[] ManufacturerData { get; set; }

public byte[] MacAddress => new byte[1] { 99 };
public byte[] MacAddress => UInt64MacAddressToByteArray(MacAddressAsUInt64);

public ulong MacAddressAsUInt64 { get; set; }

Expand All @@ -25,5 +26,22 @@ public bool Equals(IPoweredUpBluetoothDeviceInfo other)

public override int GetHashCode()
=> MacAddressAsUInt64.GetHashCode();


/// <summary>
/// Convert a UInt64 into a Byte Array while keeping endianess out of the game.
/// </summary>
/// <param name="myulong">the ulong to be converted</param>
/// <returns></returns>
private static byte[] UInt64MacAddressToByteArray(ulong myulong)
{
var result = new byte[6];
for (var i = 5; i >= 0; i--)
{
result[i] = (byte)(myulong % 256);
myulong /= 256;
}
return result;
}
}
}
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
_hubs.Add((deviceInfo, hub));
var text = (deviceInfo is IPoweredUpBluetoothDeviceInfoWithMacAddress mac) ? mac.MacAddress[0].ToString() : "not revealed";
var text = (deviceInfo is IPoweredUpBluetoothDeviceInfoWithMacAddress mac) ? mac.ToIdentificationString() : "not revealed";
_logger.LogInformation($"Discovered log of type {hub.GetType().Name} with name '{deviceInfo.Name}' on Bluetooth Address '{text}'");
Expand Down

0 comments on commit a39594c

Please sign in to comment.