LogicalUnitManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. namespace SCSI.Win32
  10. {
  11. internal class LogicalUnit
  12. {
  13. public byte AssociatedLun;
  14. public byte PathId;
  15. public byte TargetId;
  16. public byte TargetLun;
  17. public PeripheralDeviceType DeviceType;
  18. public uint? BlockSize;
  19. }
  20. internal class LogicalUnitManager
  21. {
  22. private IDictionary<byte, LogicalUnit> m_luns = new Dictionary<byte, LogicalUnit>();
  23. public LogicalUnitManager()
  24. {
  25. }
  26. public void AddLogicalUnit(LogicalUnit logicalUnit)
  27. {
  28. m_luns.Add(logicalUnit.AssociatedLun, logicalUnit);
  29. }
  30. public LogicalUnit FindLogicalUnit(byte lun)
  31. {
  32. LogicalUnit result;
  33. m_luns.TryGetValue(lun, out result);
  34. return result;
  35. }
  36. public byte? FindAssociatedLUN(byte pathId, byte targetId, byte targetLun)
  37. {
  38. foreach (byte associatedLUN in m_luns.Keys)
  39. {
  40. if (m_luns[associatedLUN].PathId == pathId &&
  41. m_luns[associatedLUN].TargetId == targetId &&
  42. m_luns[associatedLUN].TargetLun == targetLun)
  43. {
  44. return associatedLUN;
  45. }
  46. }
  47. return null;
  48. }
  49. public byte? FindUnusedLUN()
  50. {
  51. // Windows supports 0x00 - 0xFE
  52. for (byte lun = 0; lun < 255; lun++)
  53. {
  54. if (!m_luns.ContainsKey(lun))
  55. {
  56. return lun;
  57. }
  58. }
  59. return null;
  60. }
  61. }
  62. }