UsbTools.Mod 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. MODULE UsbTools; (** AUTHOR "staubesv"; PURPOSE "USB Tools"; *)
  2. (**
  3. * Usage:
  4. *
  5. * UsbTools.Mount prefix alias [volpar] ["|" fspar] ~ Starts a thread that waits for the disk device to become accessible and then mounts it.
  6. * (non-blocking)
  7. * Example: UsbTools.Mount USBDrive AosFS USB0#1 ~
  8. *
  9. *
  10. * UsbTools.BlockingMount prefix alias [volpar] ["|" fspar] ~ Waits for the disk device to become accessible and then mounts it. Should not be used by a user.
  11. * (blocking)
  12. * Example: UsbTools.BlockingMount USBDrive AosFS USB0#1 ~
  13. *
  14. * UsbTools.AwaitAndExecute devicename commandlist ~ Starts a thread that waits for the disk device to become accessible and then executes the commands.
  15. * (non-blocking)
  16. * Example: UsbTools.AwaitAndExecute USB0 FSTools.Mount USBDrive AosFS USB0#1+PET.Open USBDrive:/PET.Mod ~
  17. *
  18. * UsbTools.AwaitAndExecute0 devicename commandlist ~ Waits for the disk device to become accessible and then executes the commands. Not for users.
  19. * (blocking)
  20. * Example: UsbTools.AwaitAndExecute0 USB0 FSTools.Mount USBDrive AosFS USB0#1~
  21. *
  22. * System.Free UsbTools ~
  23. *
  24. * History:
  25. *
  26. * 28.06.2006 First release (staubesv)
  27. *)
  28. IMPORT
  29. KernelLog, Commands, Plugins, Disks, FSTools;
  30. CONST
  31. CommandSeparator = "+";
  32. (** Starts a thread that first waits until the device to be mounted becomes accessible and then mounts it *)
  33. PROCEDURE Mount*(context : Commands.Context); (** prefix alias [volpar] ["|" fspar] ~ *)
  34. VAR msg : ARRAY 8 OF CHAR; res : WORD;
  35. BEGIN
  36. Commands.Activate("UsbTools.BlockingMount", context, {}, res, msg); (* ignore result *)
  37. END Mount;
  38. (** Waits for the device to be mounted and then try to mount it *)
  39. PROCEDURE BlockingMount*(context : Commands.Context); (** prefix alias [volpar] ["|" fspar] ~ *)
  40. VAR
  41. plugin : Plugins.Plugin;
  42. devicename, string : ARRAY 128 OF CHAR;
  43. BEGIN
  44. (* Skip the first two parameters *)
  45. context.arg.SkipWhitespace; context.arg.String(string);
  46. context.arg.SkipWhitespace; context.arg.String(string);
  47. (* read dev#part string *)
  48. context.arg.SkipWhitespace; context.arg.String(string);
  49. IF ExtractDevicename(string, devicename) THEN
  50. IF (context.arg.CanSetPos()) THEN
  51. context.arg.SetPos(0);
  52. KernelLog.Enter; KernelLog.String("UsbTools: Waiting for USB device "); KernelLog.String(devicename); KernelLog.Exit;
  53. plugin := Disks.registry.Await(devicename);
  54. KernelLog.Enter; KernelLog.String("UsbTools: Device "); KernelLog.String(devicename); KernelLog.String(" connected."); KernelLog.Exit;
  55. FSTools.Mount(context);
  56. ELSE context.error.String("UsbTools.BlockingMount: Argument stream expected to support SetPos."); context.error.Ln;
  57. END;
  58. ELSE context.error.String("UsbTools.BlockingMount: Could not extract device name"); context.error.Ln;
  59. END;
  60. END BlockingMount;
  61. (** Waits for the specified disk device and then execute the commands *)
  62. PROCEDURE AwaitAndExecute*(context : Commands.Context); (** devicename commandlist ~ *)
  63. VAR msg : ARRAY 8 OF CHAR; res : WORD;
  64. BEGIN
  65. Commands.Activate("UsbTools.AwaitAndExecute0", context, {}, res, msg); (* ignore result *)
  66. END AwaitAndExecute;
  67. (** Waits for the specified disk device and then execute the commands. Note: Does NOT mount the device. *)
  68. PROCEDURE AwaitAndExecute0*(context : Commands.Context); (** devicename commandlist ~ *)
  69. VAR
  70. devicename, msg : ARRAY 128 OF CHAR;
  71. commandList : POINTER TO ARRAY OF CHAR;
  72. plugin : Plugins.Plugin;
  73. i, size, len : LONGINT; res : WORD;
  74. BEGIN
  75. context.arg.SkipWhitespace; context.arg.String(devicename);
  76. KernelLog.Enter; KernelLog.String("UsbTools: Waiting for USB device "); KernelLog.String(devicename); KernelLog.Exit;
  77. plugin := Disks.registry.Await(devicename);
  78. KernelLog.Enter; KernelLog.String("UsbTools: Device "); KernelLog.String(devicename); KernelLog.String(" connected."); KernelLog.Exit;
  79. (* read and copy command list *)
  80. context.arg.SkipWhitespace;
  81. size := context.arg.Available();
  82. IF size > 0 THEN
  83. NEW(commandList, size + 1);
  84. context.arg.Bytes(commandList^, 0, size, len);
  85. FOR i := 0 TO size DO IF commandList[i] = CommandSeparator THEN commandList[i] := ";"; END; END;
  86. commandList^[size] := 0X;
  87. Commands.Call(commandList^, {}, res, msg);
  88. IF res # Commands.Ok THEN
  89. KernelLog.Enter;
  90. KernelLog.String("UsbTools: AwaitAndExecute: Command execution error, res: "); KernelLog.Int(res, 0);
  91. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")");
  92. KernelLog.Exit;
  93. END;
  94. END;
  95. END AwaitAndExecute0;
  96. PROCEDURE ExtractDevicename(CONST devpart : ARRAY OF CHAR; VAR devicename : ARRAY OF CHAR) : BOOLEAN;
  97. VAR i : LONGINT;
  98. BEGIN
  99. WHILE (i < LEN(devpart)) & (devpart[i] # "#") & (i < LEN(devicename)) DO
  100. devicename[i] := devpart[i];
  101. INC(i);
  102. END;
  103. RETURN (i < LEN(devpart)) & (devpart[i] = "#");
  104. END ExtractDevicename;
  105. END UsbTools.
  106. UsbTools.Mount USB AosFS USB0#1 ~ System.Free UsbTools ~