using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Text; namespace NIFPGA { internal sealed class FPGASession { private readonly string windowsdllname = "NiFpga"; private readonly string linuxdllname = "libNiFpga.so"; [AllowNull] private NativeLibraryLoader.NativeLoader _NativeLoader; public Boolean Error { get; private set; } = false; public NiFpga_Status LastStatus { get; private set; } = NiFpga_Status.Success; public String Message { get; private set; } = string.Empty; private uint session; public uint Session => session; internal FPGASession() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _NativeLoader = new NativeLibraryLoader.NativeLoader(windowsdllname); else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { _NativeLoader = new NativeLibraryLoader.NativeLoader(linuxdllname); _NativeLoader.SearchDirectories.Add("/usr/lib/x86_64-linux-gnu/"); } else { throw new PlatformNotSupportedException(); } _NativeLoader.Init(); } public void Close() { CheckResult(GetDelegate()(session, Interop.NiFpga_CloseAttribute.NiFpga_CloseAttribute_NoResetIfLastSession)); session = 0; } public void Open(string bitfile,string signature,string resource,NiFpga_OpenAttribute attribute) { CheckResult(GetDelegate()(bitfile, signature, resource, attribute, ref session)); } public T GetDelegate() where T : Delegate { return _NativeLoader.LoadFunction(typeof(T).Name); } public T GetDelegate(string enterpoint) where T : Delegate { return _NativeLoader.LoadFunction(enterpoint); } public bool CheckResult(NiFpga_Status result) { LastStatus = result; Error = result < NiFpga_Status.Success; Message = result.ToString(); return Error; } } }