FPGASession.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. [AllowNull]
  12. private NativeLibraryLoader.NativeLoader _NativeLoader;
  13. public Boolean Error { get; private set; } = false;
  14. public NiFpga_Status LastStatus { get; private set; } = NiFpga_Status.Success;
  15. public String Message { get; private set; } = string.Empty;
  16. private uint session;
  17. public uint Session => session;
  18. internal FPGASession()
  19. {
  20. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _NativeLoader = new NativeLibraryLoader.NativeLoader(windowsdllname);
  21. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  22. {
  23. _NativeLoader = new NativeLibraryLoader.NativeLoader(linuxdllname);
  24. _NativeLoader.SearchDirectories.Add("/usr/lib/x86_64-linux-gnu/");
  25. }
  26. else
  27. {
  28. throw new PlatformNotSupportedException();
  29. }
  30. _NativeLoader.Init();
  31. }
  32. public void Close()
  33. {
  34. CheckResult(GetDelegate<Interop.NiFpgaDll_Close>()(session, Interop.NiFpga_CloseAttribute.NiFpga_CloseAttribute_NoResetIfLastSession));
  35. session = 0;
  36. }
  37. public void Open(string bitfile,string signature,string resource,NiFpga_OpenAttribute attribute)
  38. {
  39. CheckResult(GetDelegate<Interop.NiFpgaDll_Open>()(bitfile, signature, resource, attribute, ref session));
  40. }
  41. public T GetDelegate<T>() where T : Delegate
  42. {
  43. return _NativeLoader.LoadFunction<T>(typeof(T).Name);
  44. }
  45. public T GetDelegate<T>(string enterpoint) where T : Delegate
  46. {
  47. return _NativeLoader.LoadFunction<T>(enterpoint);
  48. }
  49. public bool CheckResult(NiFpga_Status result)
  50. {
  51. LastStatus = result;
  52. Error = result < NiFpga_Status.Success;
  53. Message = result.ToString();
  54. return Error;
  55. }
  56. }
  57. }