FPGASession.cs 2.1 KB

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