Init...
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
namespace Unosquare.RaspberryIO.Abstractions.Native
|
||||
{
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a low-level exception, typically thrown when return codes from a
|
||||
/// low-level operation is non-zero or in some cases when it is less than zero.
|
||||
/// </summary>
|
||||
/// <seealso cref="Exception" />
|
||||
public class HardwareException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HardwareException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="errorCode">The error code.</param>
|
||||
/// <param name="component">The component.</param>
|
||||
public HardwareException(int errorCode, string component)
|
||||
: base($"A hardware exception occurred. Error Code: {errorCode}")
|
||||
{
|
||||
ExtendedMessage = null;
|
||||
|
||||
try
|
||||
{
|
||||
ExtendedMessage = Standard.Strerror(errorCode);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
|
||||
ErrorCode = errorCode;
|
||||
Component = component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the error code.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The error code.
|
||||
/// </value>
|
||||
public int ErrorCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the component.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The component.
|
||||
/// </value>
|
||||
public string Component { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the extended message (could be null).
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The extended message.
|
||||
/// </value>
|
||||
public string? ExtendedMessage { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new instance of a hardware error by retrieving the last error number (errno).
|
||||
/// </summary>
|
||||
/// <param name="className">Name of the class.</param>
|
||||
/// <param name="methodName">Name of the method.</param>
|
||||
/// <exception cref="HardwareException">When an error thrown by an API call occurs.</exception>
|
||||
public static void Throw(string className, string methodName) => throw new HardwareException(Marshal.GetLastWin32Error(), $"{className}.{methodName}");
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"{nameof(HardwareException)}{(string.IsNullOrWhiteSpace(Component) ? string.Empty : $" on {Component}")}: ({ErrorCode}) - {Message}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace Unosquare.RaspberryIO.Abstractions.Native
|
||||
{
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// Provides standard 'libc' calls using platform-invoke.
|
||||
/// </summary>
|
||||
public static class Standard
|
||||
{
|
||||
internal const string LibCLibrary = "libc";
|
||||
|
||||
#region LibC Calls
|
||||
|
||||
/// <summary>
|
||||
/// Strerrors the specified error.
|
||||
/// </summary>
|
||||
/// <param name="error">The error.</param>
|
||||
/// <returns>The error string.</returns>
|
||||
public static string Strerror(int error)
|
||||
{
|
||||
if (Type.GetType("Mono.Runtime") == null) return Marshal.PtrToStringAnsi(StrError(error));
|
||||
|
||||
try
|
||||
{
|
||||
var buffer = new StringBuilder(256);
|
||||
var result = Strerror(error, buffer, (ulong)buffer.Capacity);
|
||||
return (result != -1) ? buffer.ToString() : null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport(LibCLibrary, EntryPoint = "strerror", SetLastError = true)]
|
||||
private static extern IntPtr StrError(int errnum);
|
||||
|
||||
[DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)]
|
||||
private static extern int Strerror(int error, [Out] StringBuilder buffer, ulong length);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user