FPGASession.cs 2.0 KB

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