Skip to content

Commit

Permalink
add overloads of SkStackActiveScanOptions.Create that accepts the Fun…
Browse files Browse the repository at this point in the history
…c<IEnumerable<int>> for generating scan durations
  • Loading branch information
smdn committed Jul 4, 2024
1 parent 27a53e5 commit 1034dd4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,51 @@ public static SkStackActiveScanOptions Create(
scanDurationGenerator: scanDurationGenerator
);

/// <summary>
/// Creates the <see cref="SkStackActiveScanOptions"/> with the custom selection method and the delegate for generating duration factor.
/// </summary>
/// <param name="scanDurationGeneratorFunc">A delegate to method that iterates the scan durations.</param>
/// <param name="paaSelector">
/// A callback to select the target PAA from the PAAs found during the scan.
/// If <see langword="null"/>, selects the PAA which found at first during the scan.
/// </param>
public static SkStackActiveScanOptions Create(
Func<IEnumerable<int>> scanDurationGeneratorFunc,
Predicate<SkStackPanDescription>? paaSelector = null
)
=> new UserDefinedActiveScanOptions(
paaSelector: paaSelector,
scanDurationGeneratorFunc: scanDurationGeneratorFunc
);

private sealed class UserDefinedActiveScanOptions : SkStackActiveScanOptions {
private readonly Predicate<SkStackPanDescription>? paaSelector;
private readonly IEnumerable<int> scanDurationGenerator;
private readonly Func<IEnumerable<int>> scanDurationGeneratorFunc;

public UserDefinedActiveScanOptions(
Predicate<SkStackPanDescription>? paaSelector,
IEnumerable<int> scanDurationGenerator
)
{
if (scanDurationGenerator is null)
throw new ArgumentNullException(nameof(scanDurationGenerator));

this.paaSelector = paaSelector;
this.scanDurationGeneratorFunc = new(() => scanDurationGenerator);
}

public UserDefinedActiveScanOptions(
Predicate<SkStackPanDescription>? paaSelector,
Func<IEnumerable<int>> scanDurationGeneratorFunc
)
{
this.paaSelector = paaSelector;
this.scanDurationGenerator = scanDurationGenerator ?? throw new ArgumentNullException(nameof(scanDurationGenerator));
this.scanDurationGeneratorFunc = scanDurationGeneratorFunc ?? throw new ArgumentNullException(nameof(scanDurationGeneratorFunc));
}

public override SkStackActiveScanOptions Clone() => new UserDefinedActiveScanOptions(paaSelector, scanDurationGenerator.ToArray());
public override SkStackActiveScanOptions Clone() => new UserDefinedActiveScanOptions(paaSelector, scanDurationGeneratorFunc);
internal override bool SelectPanaAuthenticationAgent(SkStackPanDescription desc) => paaSelector?.Invoke(desc) ?? true;
internal override IEnumerable<int> YieldScanDurationFactors() => scanDurationGenerator;
internal override IEnumerable<int> YieldScanDurationFactors() => scanDurationGeneratorFunc();
}

/// <summary>
Expand All @@ -120,22 +149,50 @@ PhysicalAddress paaMacAddress
scanDurationGenerator: scanDurationGenerator
);

/// <summary>
/// Creates the <see cref="SkStackActiveScanOptions"/> with the custom selection method and the delegate for generating duration factor.
/// </summary>
/// <param name="scanDurationGeneratorFunc">A delegate to method that iterates the scan durations.</param>
/// <param name="paaMacAddress">
/// A <see cref="PhysicalAddress"/> of the target PAA. This method selects the first PAA found during the scan that matches this <see cref="PhysicalAddress"/>.
/// </param>
public static SkStackActiveScanOptions Create(
Func<IEnumerable<int>> scanDurationGeneratorFunc,
PhysicalAddress paaMacAddress
)
=> new FindByMacAddressActiveScanOptions(
paaMacAddress: paaMacAddress,
scanDurationGeneratorFunc: scanDurationGeneratorFunc
);

private sealed class FindByMacAddressActiveScanOptions : SkStackActiveScanOptions {
private readonly PhysicalAddress paaMacAddress;
private readonly IEnumerable<int> scanDurationGenerator;
private readonly Func<IEnumerable<int>> scanDurationGeneratorFunc;

public FindByMacAddressActiveScanOptions(
PhysicalAddress paaMacAddress,
IEnumerable<int> scanDurationGenerator
)
{
if (scanDurationGenerator is null)
throw new ArgumentNullException(nameof(scanDurationGenerator));

this.paaMacAddress = paaMacAddress;
scanDurationGeneratorFunc = new(() => scanDurationGenerator);
}

public FindByMacAddressActiveScanOptions(
PhysicalAddress paaMacAddress,
Func<IEnumerable<int>> scanDurationGeneratorFunc
)
{
this.paaMacAddress = paaMacAddress;
this.scanDurationGenerator = scanDurationGenerator ?? throw new ArgumentNullException(nameof(scanDurationGenerator));
this.scanDurationGeneratorFunc = scanDurationGeneratorFunc ?? throw new ArgumentNullException(nameof(scanDurationGeneratorFunc));
}

public override SkStackActiveScanOptions Clone() => new FindByMacAddressActiveScanOptions(paaMacAddress, scanDurationGenerator.ToArray());
public override SkStackActiveScanOptions Clone() => new FindByMacAddressActiveScanOptions(paaMacAddress, scanDurationGeneratorFunc);
internal override bool SelectPanaAuthenticationAgent(SkStackPanDescription desc) => desc.MacAddress.Equals(paaMacAddress);
internal override IEnumerable<int> YieldScanDurationFactors() => scanDurationGenerator;
internal override IEnumerable<int> YieldScanDurationFactors() => scanDurationGeneratorFunc();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,29 @@ public void YieldScanDurationFactors_Create_ScanDurationGenerator_WithPaaMacAddr
() => TestYieldScanDurationFactors(SkStackActiveScanOptions.Create(scanDurationFactors!, paaMacAddress: PhysicalAddress.None), scanDurationFactors!),
typeOfExpectedException is null ? Throws.Nothing : Throws.TypeOf(typeOfExpectedException)
);

[Test]
public void YieldScanDurationFactors_Create_ScanDurationGeneratorFunc_ArgumentNull()
{
Assert.That(() => SkStackActiveScanOptions.Create(scanDurationGeneratorFunc: null!, paaSelector: null), Throws.ArgumentNullException);
Assert.That(() => SkStackActiveScanOptions.Create(scanDurationGeneratorFunc: null!, paaMacAddress: PhysicalAddress.None), Throws.ArgumentNullException);
}

[TestCase(new int[0])]
[TestCase(new int[] { 1 })]
[TestCase(new int[] { 1, 2, 3, 4, 5 })]
public void YieldScanDurationFactors_Create_ScanDurationGeneratorFunc_WithPaaSelector(int[] scanDurationFactors)
=> Assert.That(
() => TestYieldScanDurationFactors(SkStackActiveScanOptions.Create(scanDurationGeneratorFunc: () => scanDurationFactors, paaSelector: null), scanDurationFactors!),
Throws.Nothing
);

[TestCase(new int[0])]
[TestCase(new int[] { 1 })]
[TestCase(new int[] { 1, 2, 3, 4, 5 })]
public void YieldScanDurationFactors_Create_ScanDurationGeneratorFunc_WithPaaMacAddress(int[] scanDurationFactors)
=> Assert.That(
() => TestYieldScanDurationFactors(SkStackActiveScanOptions.Create(scanDurationGeneratorFunc: () => scanDurationFactors, paaMacAddress: PhysicalAddress.None), scanDurationFactors!),
Throws.Nothing
);
}

0 comments on commit 1034dd4

Please sign in to comment.