MODULE UsbTools;
IMPORT
KernelLog, Commands, Plugins, Disks, FSTools;
CONST
CommandSeparator = "+";
PROCEDURE Mount*(context : Commands.Context);
VAR msg : ARRAY 8 OF CHAR; res : LONGINT;
BEGIN
Commands.Activate("UsbTools.BlockingMount", context, {}, res, msg);
END Mount;
PROCEDURE BlockingMount*(context : Commands.Context);
VAR
plugin : Plugins.Plugin;
devicename, string : ARRAY 128 OF CHAR;
BEGIN
context.arg.SkipWhitespace; context.arg.String(string);
context.arg.SkipWhitespace; context.arg.String(string);
context.arg.SkipWhitespace; context.arg.String(string);
IF ExtractDevicename(string, devicename) THEN
IF (context.arg.CanSetPos()) THEN
context.arg.SetPos(0);
KernelLog.Enter; KernelLog.String("UsbTools: Waiting for USB device "); KernelLog.String(devicename); KernelLog.Exit;
plugin := Disks.registry.Await(devicename);
KernelLog.Enter; KernelLog.String("UsbTools: Device "); KernelLog.String(devicename); KernelLog.String(" connected."); KernelLog.Exit;
FSTools.Mount(context);
ELSE context.error.String("UsbTools.BlockingMount: Argument stream expected to support SetPos."); context.error.Ln;
END;
ELSE context.error.String("UsbTools.BlockingMount: Could not extract device name"); context.error.Ln;
END;
END BlockingMount;
PROCEDURE AwaitAndExecute*(context : Commands.Context);
VAR msg : ARRAY 8 OF CHAR; res : LONGINT;
BEGIN
Commands.Activate("UsbTools.AwaitAndExecute0", context, {}, res, msg);
END AwaitAndExecute;
PROCEDURE AwaitAndExecute0*(context : Commands.Context);
VAR
devicename, msg : ARRAY 128 OF CHAR;
commandList : POINTER TO ARRAY OF CHAR;
plugin : Plugins.Plugin;
i, size, len, res : LONGINT;
BEGIN
context.arg.SkipWhitespace; context.arg.String(devicename);
KernelLog.Enter; KernelLog.String("UsbTools: Waiting for USB device "); KernelLog.String(devicename); KernelLog.Exit;
plugin := Disks.registry.Await(devicename);
KernelLog.Enter; KernelLog.String("UsbTools: Device "); KernelLog.String(devicename); KernelLog.String(" connected."); KernelLog.Exit;
context.arg.SkipWhitespace;
size := context.arg.Available();
IF size > 0 THEN
NEW(commandList, size + 1);
context.arg.Bytes(commandList^, 0, size, len);
FOR i := 0 TO size DO IF commandList[i] = CommandSeparator THEN commandList[i] := ";"; END; END;
commandList^[size] := 0X;
Commands.Call(commandList^, {}, res, msg);
IF res # Commands.Ok THEN
KernelLog.Enter;
KernelLog.String("UsbTools: AwaitAndExecute: Command execution error, res: "); KernelLog.Int(res, 0);
KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")");
KernelLog.Exit;
END;
END;
END AwaitAndExecute0;
PROCEDURE ExtractDevicename(CONST devpart : ARRAY OF CHAR; VAR devicename : ARRAY OF CHAR) : BOOLEAN;
VAR i : LONGINT;
BEGIN
WHILE (i < LEN(devpart)) & (devpart[i] # "#") & (i < LEN(devicename)) DO
devicename[i] := devpart[i];
INC(i);
END;
RETURN (i < LEN(devpart)) & (devpart[i] = "#");
END ExtractDevicename;
END UsbTools.
UsbTools.Mount USB AosFS USB0#1 ~ SystemTools.Free UsbTools ~