Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate ProductInfo helper methods into the ProductInfo class #2402

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions iothub/device/src/NativeMethods.cs

This file was deleted.

61 changes: 59 additions & 2 deletions iothub/device/src/ProductInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Azure.Devices.Client.Extensions;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Win32;

namespace Microsoft.Azure.Devices.Client
{
internal class ProductInfo
{
public string Extra { get; set; } = "";

private readonly Lazy<int> _productType = new Lazy<int>(() => NativeMethods.GetWindowsProductType());
private readonly Lazy<string> _sqmId = new Lazy<string>(() => TelemetryMethods.GetSqmMachineId());
private readonly Lazy<int> _productType = new Lazy<int>(() => GetWindowsProductType());
private readonly Lazy<string> _sqmId = new Lazy<string>(() => GetSqmMachineId());

public override string ToString()
{
Expand Down Expand Up @@ -95,6 +98,60 @@ private string ToString(string format)

return userAgent;
}

internal static string GetSqmMachineId()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\SQMClient");
if (key != null)
{
return key.GetValue("MachineId") as string;
}
}
catch (Exception ex)
{
Debug.Assert(false, ex.Message);

if (Logging.IsEnabled)
Logging.Error(null, ex, nameof(ProductInfo));
}
}

return null;
}

internal static int GetWindowsProductType()
{
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& GetProductInfo(Environment.OSVersion.Version.Major, Environment.OSVersion.Version.Minor, 0, 0, out int productType))
{
return productType;
}
}
catch (DllNotFoundException ex)
{
// Catch any DLL not found exceptions
Debug.Assert(false, ex.Message);

if (Logging.IsEnabled)
Logging.Error(null, ex, nameof(ProductInfo));
}

return 0;
}

[DllImport("kernel32.dll", SetLastError = false)]
private static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out int pdwReturnedProductType);
}

internal enum UserAgentFormats
Expand Down
38 changes: 0 additions & 38 deletions iothub/device/src/TelemetryMethods.cs

This file was deleted.

32 changes: 29 additions & 3 deletions iothub/device/tests/ProductInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Win32;

namespace Microsoft.Azure.Devices.Client.Test
{
Expand Down Expand Up @@ -103,9 +104,9 @@ private string ExpectedUserAgentString()
string operatingSystem = RuntimeInformation.OSDescription.Trim();
string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().Trim();

int productType = NativeMethods.GetWindowsProductType();
int productType = ProductInfo.GetWindowsProductType();
string productTypeString = (productType != 0) ? $" WindowsProduct:0x{productType:X8}" : string.Empty;
string deviceId = TelemetryMethods.GetSqmMachineId() ?? string.Empty;
string deviceId = ProductInfo.GetSqmMachineId() ?? string.Empty;

string[] agentInfoParts =
{
Expand All @@ -125,12 +126,37 @@ private string ExpectedHttpUserAgentString()
string operatingSystem = RuntimeInformation.OSDescription.Trim();
string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().Trim();

int productType = NativeMethods.GetWindowsProductType();
int productType = ProductInfo.GetWindowsProductType();
string productTypeString = (productType != 0) ? $" WindowsProduct:0x{productType:X8}" : string.Empty;

return $".NET/{version} ({runtime}; {operatingSystem + productTypeString}; {processorArchitecture})";
}

#endif

[TestMethod]
public void GetSqmMachineId_ReturnsExpectedValue()
{
string actualValue = ProductInfo.GetSqmMachineId();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string expectedValue = null;

// Get SQM ID from Registry if exists
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\SQMClient");
if (key != null)
{
expectedValue = key.GetValue("MachineId") as string;
}

Assert.AreEqual(expectedValue, actualValue);
}
else
{
// GetSqmMachineId() should always return null for all other platforms
Assert.IsNull(actualValue);
}
}
}
}
39 changes: 0 additions & 39 deletions iothub/device/tests/TelemetryMethodsTests.cs

This file was deleted.