Skip to content

Commit 6ad59e5

Browse files
committed
usb: add api for reading from device stream
1 parent e179bc1 commit 6ad59e5

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kb = luausb.create({ id = 0, type = "keyboard" })
2+
3+
while true do
4+
print(kb.debug())
5+
wait(1000)
6+
end

app/src/main/assets/scripts/exfiltrate.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ while true do
1515
end
1616

1717
print("running")
18-
-- wait 1 second for things to settle down
19-
wait(1000)
18+
-- wait 2 second for things to settle down
19+
wait(2000)
2020

2121
kb.chord(MOD_LSUPER, KEY_R)
2222
wait(1000)

app/src/main/java/org/netdex/androidusbscript/function/DeviceStream.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
import java.io.Closeable;
66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.io.OutputStream;
9+
import java.io.PushbackInputStream;
810

911
public abstract class DeviceStream implements Closeable {
1012
private final FileSystem fs_;
1113
private final String devicePath_;
1214

1315
private OutputStream os_;
16+
private InputStream is_;
1417

1518
public DeviceStream(FileSystem fs, String devicePath) {
1619
this.fs_ = fs;
@@ -22,6 +25,14 @@ protected void write(byte[] b) throws IOException {
2225
this.getOutputStream().write(b);
2326
}
2427

28+
protected int read(byte[] b) throws IOException {
29+
// Log.d(TAG, String.format("read %s > %s", Util.bytesToHex(buffer), devicePath_));
30+
return this.getInputStream().read(b);
31+
}
32+
public int available() throws IOException {
33+
return this.getInputStream().available();
34+
}
35+
2536
protected OutputStream getOutputStream() throws IOException {
2637
if (os_ == null) {
2738
if (!fs_.exists(devicePath_))
@@ -31,9 +42,20 @@ protected OutputStream getOutputStream() throws IOException {
3142
return os_;
3243
}
3344

45+
protected InputStream getInputStream() throws IOException {
46+
if (is_ == null) {
47+
if (!fs_.exists(devicePath_))
48+
throw new RuntimeException(String.format("Device \"%s\" does not exist", devicePath_));
49+
is_ = fs_.fopen_r(devicePath_);
50+
}
51+
return is_;
52+
}
53+
3454
@Override
3555
public void close() throws IOException {
3656
if (os_ != null)
3757
os_.close();
58+
if (is_ != null)
59+
is_.close();
3860
}
3961
}

app/src/main/java/org/netdex/androidusbscript/task/LuaUsbLibrary.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public keyboard(int id) {
243243
public LuaValue call() {
244244
LuaValue library = tableOf();
245245
for (LuaFunction f : new LuaFunction[]{
246-
new raw(), new chord(), new press(), new string()}) {
246+
new raw(), new chord(), new press(), new string(), new debug()}) {
247247
library.set(f.name(), f);
248248
}
249249
return library;
@@ -314,7 +314,6 @@ public Varargs invoke(Varargs args) {
314314
}
315315

316316
class string extends TwoArgFunction {
317-
318317
@Override
319318
public LuaValue call(LuaValue arg1, LuaValue arg2) {
320319
String string = arg1.checkjstring();
@@ -327,6 +326,17 @@ public LuaValue call(LuaValue arg1, LuaValue arg2) {
327326
return NIL;
328327
}
329328
}
329+
330+
class debug extends ZeroArgFunction {
331+
@Override
332+
public LuaValue call() {
333+
try {
334+
return valueOf(hid_.available());
335+
} catch (IOException e) {
336+
throw new LuaError(e);
337+
}
338+
}
339+
}
330340
}
331341

332342
class mouse extends ZeroArgFunction {

0 commit comments

Comments
 (0)