FPGASession.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace NIFPGA
  6. {
  7. internal sealed class FPGASession
  8. {
  9. private readonly string windowsdllname = "NiFpga";
  10. private readonly string linuxdllname = "libNiFpga.so";
  11. private NativeLibraryLoader.NativeLoader _NativeLoader;
  12. public Boolean Error { get; private set; } = false;
  13. public NiFpga_Status LastStatus { get; private set; } = NiFpga_Status.Success;
  14. public String Message { get; private set; } = string.Empty;
  15. private uint session;
  16. public uint Session => session;
  17. internal FPGASession()
  18. {
  19. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _NativeLoader = new NativeLibraryLoader.NativeLoader(windowsdllname);
  20. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  21. {
  22. _NativeLoader = new NativeLibraryLoader.NativeLoader(linuxdllname);
  23. _NativeLoader.SearchDirectories.Add("/usr/lib/x86_64-linux-gnu/");
  24. }
  25. else
  26. {
  27. throw new PlatformNotSupportedException();
  28. }
  29. _NativeLoader.Init();
  30. }
  31. public void Close()
  32. {
  33. CheckResult(GetDelegate<Interop.NiFpgaDll_Close>()(session, Interop.NiFpga_CloseAttribute.NiFpga_CloseAttribute_NoResetIfLastSession));
  34. session = 0;
  35. }
  36. public void Open(string bitfile,string signature,string resource,NiFpga_OpenAttribute attribute)
  37. {
  38. CheckResult(GetDelegate<Interop.NiFpgaDll_Open>()(bitfile, signature, resource, attribute, ref session));
  39. }
  40. public T GetDelegate<T>() where T : Delegate
  41. {
  42. return _NativeLoader.LoadFunction<T>(typeof(T).Name);
  43. }
  44. public T GetDelegate<T>(string enterpoint) where T : Delegate
  45. {
  46. return _NativeLoader.LoadFunction<T>(enterpoint);
  47. }
  48. public bool CheckResult(NiFpga_Status result)
  49. {
  50. LastStatus = result;
  51. Error = result < NiFpga_Status.Success;
  52. Message = result.ToString();
  53. return Error;
  54. }
  55. }
  56. }