From 3ad94b2cf577264a48309a67fe50b664cfdb5412 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 11:32:41 +0000 Subject: [PATCH 1/4] Fix USB Arsenal Apply: free the UDC and keep our gadget bound Apply silently failed on any device with USB ADB active. Three issues in the gadget-switch path combined to make it a no-op: - adbd holds an fd on the vendor gadget's ffs.adb function, so writing an empty string to g1/UDC returned EBUSY and the controller was never freed. bind() now stops adbd first to release that fd. - Even after a successful switch, Android init's USB property triggers re-bound the vendor gadget to the controller as soon as adbd restarted (sys.usb.ffs.ready=1). bind() now freezes vendor.usb.configfs and sys.usb.configfs (standard AOSP props) before touching adbd, and unbind() thaws them back to their captured originals. - bind()'s script ran without set -e and reported success purely from the shell marker, so a failed UDC write still looked like success. bind() now reads /config/usb_gadget/stryker/UDC back and only reports success when our gadget is actually attached. Because there is a single UDC, USB-ADB cannot coexist with the Stryker gadget. Added an opt-in "Keep ADB over Wi-Fi (TCP 5555)" toggle (default off) that restarts adbd in TCP mode after the switch, instead of silently opening a network ADB port for everyone. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_017pdnBy5tWi4SFFo3vwPkr3 --- .../hid/configfs/UsbGadgetController.java | 96 ++++++++++++++++++- .../usbarsenal/ui/UsbArsenalFragment.java | 6 ++ .../main/res/layout/fragment_usb_arsenal.xml | 17 ++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 116 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java index a9f91dd2..5a9875ce 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java @@ -20,6 +20,23 @@ public final class UsbGadgetController { public static final String GADGET_BASE = "/config/usb_gadget/" + GADGET_NAME; private static final String VENDOR_GADGET = "/config/usb_gadget/g1"; + // Android init USB property triggers. Standard across AOSP regardless of the + // SoC-specific init.*.usb.*.rc file that actually carries them. Setting both + // to 0 stops init from re-binding the vendor gadget to the controller right + // after adbd restarts (see Bug 2 in the USB Arsenal report). + private static final String PROP_SYS_CONFIGFS = "sys.usb.configfs"; + private static final String PROP_VENDOR_CONFIGFS = "vendor.usb.configfs"; + private static final String PROP_ADB_TCP_PORT = "service.adb.tcp.port"; + private static final String ADB_TCP_PORT = "5555"; + + // Opt-in: keep ADB reachable over Wi-Fi while our gadget owns the single UDC. + // There is only one UDC, so USB-ADB (the vendor gadget) cannot coexist with + // the Stryker gadget; this is off by default to avoid silently opening an + // unauthenticated network ADB port. + public static final String PREF_KEEP_ADB_TCP = "arsenal_keep_adb_tcp"; + private static final String PREF_SAVED_SYS_CONFIGFS = "arsenal_saved_sys_configfs"; + private static final String PREF_SAVED_VENDOR_CONFIGFS = "arsenal_saved_vendor_configfs"; + private static final String KEYBOARD_DESC_HEX = "05010906a101050719e029e71500250175019508810295017508810195057501050819012905910295017503910195067508150025650507190029658100c0"; @@ -109,30 +126,99 @@ public boolean prepare(@NonNull GadgetProfile profile) { public String bind() { String udc = capabilities().udcName; if (udc == null) return null; + + // Remember the current trigger-property values so "Restore vendor" can + // put them back exactly as they were. + rememberOriginalTriggerState(); + List script = new ArrayList<>(); + // 1. Freeze Android's USB property triggers BEFORE touching adbd, so init + // cannot re-bind the vendor gadget to the controller once adbd comes + // back and sets sys.usb.ffs.ready=1 (Bug 2). vendor.usb.configfs must + // go first, otherwise the init counter-trigger resets sys.usb.configfs. + script.add("setprop " + PROP_VENDOR_CONFIGFS + " 0"); + script.add("setprop " + PROP_SYS_CONFIGFS + " 0"); + // 2. Stop adbd so it releases the ffs.adb file descriptor that pins the + // vendor gadget; without this, unbinding g1 fails with EBUSY (Bug 1). + script.add("stop adbd 2>/dev/null || true"); + script.add("sleep 1"); + // 3. Unbind the vendor gadget (now succeeds) then bind ours to the UDC. script.add("if [ -s " + sq(VENDOR_GADGET + "/UDC") + " ]; then " + "echo > " + sq(VENDOR_GADGET + "/UDC") + " || true; fi"); - script.add("echo " + sq(udc) + " > " + sq(GADGET_BASE + "/UDC")); - script.add("sleep 1"); - if (run(script)) { + script.add("sleep 0.3"); + script.add("echo " + sq(udc) + " > " + sq(GADGET_BASE + "/UDC") + " || true"); + script.add("sleep 0.5"); + // 4. Optionally keep ADB reachable over Wi-Fi (opt-in, default off). The + // single UDC means USB-ADB cannot coexist with our gadget. + if (core.getBoolean(PREF_KEEP_ADB_TCP)) { + script.add("setprop " + PROP_ADB_TCP_PORT + " " + ADB_TCP_PORT); + script.add("start adbd 2>/dev/null || true"); + } + run(script); + + // Authoritative success check: the vendor-gadget unbind and our bind are + // best-effort (|| true) so the shell never aborts mid-switch, which means + // the shell's exit status says nothing. Read back the UDC and only report + // success when our gadget is actually attached to the controller. + String actual = read(GADGET_BASE + "/UDC").trim(); + if (!actual.isEmpty()) { chmodHidNodes(); - return udc; + return actual; } return null; } public boolean unbind() { List script = new ArrayList<>(); + // Detach our gadget (no ffs fd pins it, so this always succeeds). script.add("if [ -d " + sq(GADGET_BASE) + " ]; then " + "echo > " + sq(GADGET_BASE + "/UDC") + " || true; fi"); + script.add("sleep 0.3"); + // Thaw the init triggers we froze during bind so Android can manage USB + // again, restoring their original values (default 1 when never captured). + script.add("setprop " + PROP_SYS_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_SYS_CONFIGFS))); + script.add("setprop " + PROP_VENDOR_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_VENDOR_CONFIGFS))); + // Re-bind the vendor gadget as a fallback for devices whose init triggers + // do not fire on their own, then bring adbd back for USB ADB. String udc = capabilities().udcName; if (udc != null) { - script.add("if [ -d " + sq(VENDOR_GADGET) + " ]; then " + script.add("if [ -s " + sq(VENDOR_GADGET + "/UDC") + " ]; then : ; " + + "elif [ -d " + sq(VENDOR_GADGET) + " ]; then " + "echo " + sq(udc) + " > " + sq(VENDOR_GADGET + "/UDC") + " || true; fi"); } + script.add("start adbd 2>/dev/null || true"); return run(script); } + private void rememberOriginalTriggerState() { + rememberTrigger(PREF_SAVED_SYS_CONFIGFS, getprop(PROP_SYS_CONFIGFS)); + rememberTrigger(PREF_SAVED_VENDOR_CONFIGFS, getprop(PROP_VENDOR_CONFIGFS)); + } + + private void rememberTrigger(String prefKey, String current) { + // Never persist our own frozen "0" as the original; otherwise a second + // Apply before a Restore would make us "restore" the frozen value. A real + // value (normally "1") is captured; "0"/blank keeps the prior original. + if (current != null && !current.isEmpty() && !current.equals("0")) { + core.putString(prefKey, current); + } + } + + private String savedTriggerValue(String prefKey) { + String v = core.getString(prefKey); + return (v == null || v.isEmpty()) ? "1" : v; + } + + private String getprop(String name) { + List out = core.customCommand("getprop " + sq(name), true); + if (out != null) { + for (String line : out) { + if (line != null && !line.trim().isEmpty()) return line.trim(); + } + } + return ""; + } + public boolean teardown() { List script = new ArrayList<>(); script.add("if [ -d " + sq(GADGET_BASE) + " ]; then"); diff --git a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java index 07d9fe03..3afffe60 100644 --- a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java +++ b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.switchmaterial.SwitchMaterial; import com.google.android.material.textview.MaterialTextView; import com.zalexdev.stryker.R; import com.zalexdev.stryker.hid.capability.HidCapabilities; @@ -69,6 +70,11 @@ public void onViewCreated(@NonNull View v, @Nullable Bundle savedInstanceState) statePill = v.findViewById(R.id.arsenal_state_pill); stateDetail = v.findViewById(R.id.arsenal_state_detail); + SwitchMaterial keepAdbSwitch = v.findViewById(R.id.arsenal_keep_adb_switch); + keepAdbSwitch.setChecked(core.getBoolean(UsbGadgetController.PREF_KEEP_ADB_TCP)); + keepAdbSwitch.setOnCheckedChangeListener((btn, checked) -> + core.putBoolean(UsbGadgetController.PREF_KEEP_ADB_TCP, checked)); + RecyclerView list = v.findViewById(R.id.arsenal_profile_list); list.setLayoutManager(new LinearLayoutManager(requireContext())); adapter = new GadgetProfileAdapter(this); diff --git a/app/src/main/res/layout/fragment_usb_arsenal.xml b/app/src/main/res/layout/fragment_usb_arsenal.xml index 1a934b24..9bf03e8a 100644 --- a/app/src/main/res/layout/fragment_usb_arsenal.xml +++ b/app/src/main/res/layout/fragment_usb_arsenal.xml @@ -122,6 +122,23 @@ android:textColor="@color/grey" android:textSize="11sp" /> + + + + Profile applied — gadget bound to %s Failed to apply profile (check logcat) Vendor gadget restored + Keep ADB over Wi-Fi (TCP 5555) + Applying a profile takes over USB, so USB-ADB drops. Turn this on to switch ADB to Wi-Fi (adb connect PHONE_IP:5555) first. Off by default — this opens an unauthenticated ADB port on your network. Save Cancel VID must look like 0x046d From 0818d2e05d3b4c838d0491fe1188a4e51221f955 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 11:33:21 +0000 Subject: [PATCH 2/4] Surface the real /dev/hidg0 signal in USB Arsenal state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit USB Arsenal reported gadget health only from configfs (stryker_gadget and the linked-function set), while the HID Attacks screen keyed off whether /dev/hidg0 actually exists. The two could disagree: a gadget could be bound yet have no working HID node (e.g. after init stole the controller back), and nothing on the Arsenal screen showed it. readState() now also checks /dev/hidg0 when the profile exposes a HID function, and the state detail line prints hidg0=✓/✗ — the same signal HID Attacks uses — so the two screens no longer contradict each other. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_017pdnBy5tWi4SFFo3vwPkr3 --- .../zalexdev/stryker/hid/configfs/GadgetState.java | 12 +++++++++++- .../stryker/hid/configfs/UsbGadgetController.java | 5 ++++- .../stryker/usbarsenal/ui/UsbArsenalFragment.java | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java index f2ed3737..993b6498 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/GadgetState.java @@ -11,12 +11,15 @@ public final class GadgetState { public final String boundUdc; public final Set linkedFunctions; public final String massStorageFile; + /** Whether /dev/hidg0 exists — the same signal the HID Attacks screen uses. */ + public final boolean hidNodePresent; public GadgetState(boolean strykerGadgetExists, boolean strykerGadgetBound, String boundUdc, Set linkedFunctions, - String massStorageFile) { + String massStorageFile, + boolean hidNodePresent) { this.strykerGadgetExists = strykerGadgetExists; this.strykerGadgetBound = strykerGadgetBound; this.boundUdc = boundUdc; @@ -24,5 +27,12 @@ public GadgetState(boolean strykerGadgetExists, ? Collections.unmodifiableSet(EnumSet.noneOf(GadgetFunction.class)) : Collections.unmodifiableSet(EnumSet.copyOf(linkedFunctions)); this.massStorageFile = massStorageFile; + this.hidNodePresent = hidNodePresent; + } + + /** True when this state exposes a HID function (keyboard or mouse). */ + public boolean exposesHid() { + return linkedFunctions.contains(GadgetFunction.HID_KEYBOARD) + || linkedFunctions.contains(GadgetFunction.HID_MOUSE); } } diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java index 5a9875ce..d9d01e6f 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java @@ -71,7 +71,10 @@ public GadgetState readState() { String msFile = exists && linked.contains(GadgetFunction.MASS_STORAGE) ? read(GADGET_BASE + "/functions/mass_storage.0/lun.0/file").trim() : null; - return new GadgetState(exists, bound, bound ? udc : null, linked, msFile); + boolean hidNode = (linked.contains(GadgetFunction.HID_KEYBOARD) + || linked.contains(GadgetFunction.HID_MOUSE)) + && pathExists("/dev/hidg0"); + return new GadgetState(exists, bound, bound ? udc : null, linked, msFile, hidNode); } public boolean prepare(@NonNull GadgetProfile profile) { diff --git a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java index 3afffe60..d51b15b6 100644 --- a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java +++ b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java @@ -133,6 +133,9 @@ private void bindState(@NonNull HidCapabilities caps, @NonNull GadgetState state detail.append("udc=").append(caps.udcName == null ? "—" : caps.udcName) .append(" · configfs=").append(caps.configFsMounted ? "✓" : "✗") .append(" · stryker_gadget=").append(state.strykerGadgetExists ? "✓" : "✗"); + if (state.exposesHid()) { + detail.append(" · hidg0=").append(state.hidNodePresent ? "✓" : "✗"); + } if (!state.linkedFunctions.isEmpty()) { detail.append('\n').append("functions="); Iterator it = state.linkedFunctions.iterator(); From 36da8a4d4d590ed87061279e7fc5f9ea1584fc0d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 11:36:05 +0000 Subject: [PATCH 3/4] Skip kernel-unsupported USB functions instead of aborting Apply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prepare() ran its whole configfs script under set -e, so a single function whose driver is missing from the kernel (e.g. usb_f_acm on the Nokia 6.1 Plus 4.4 kernel, where mkdir acm.0 fails with ENOENT) aborted the entire gadget — a HID+ACM profile created nothing at all, not even the keyboard. prepare() now keeps set -e only for the structural setup, then drops it before creating function directories and links each function only when its directory actually came up. It returns the set of functions that were really created. applyProfile() reports an ApplyResult carrying the requested, active and dropped function sets; apply(GadgetProfile) is kept as a boolean convenience for the HID IDE auto-apply path. USB Arsenal now falls back to whatever the kernel supports and toasts which functions were skipped, or, when none of the requested functions are supported, says so plainly instead of a generic failure. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_017pdnBy5tWi4SFFo3vwPkr3 --- .../stryker/hid/configfs/ApplyResult.java | 67 +++++++++++++++++++ .../hid/configfs/UsbGadgetController.java | 53 ++++++++++++--- .../usbarsenal/ui/UsbArsenalFragment.java | 40 +++++++++-- app/src/main/res/values/strings.xml | 2 + 4 files changed, 148 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java new file mode 100644 index 00000000..1f3b633e --- /dev/null +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/ApplyResult.java @@ -0,0 +1,67 @@ +package com.zalexdev.stryker.hid.configfs; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +/** + * Outcome of {@link UsbGadgetController#applyProfile}. Carries not just success + * but which of the profile's requested functions the kernel could actually + * create, so the UI can warn when (for example) the ACM serial driver is not + * compiled into the kernel and the gadget falls back to HID-only. + */ +public final class ApplyResult { + + public final boolean success; + @Nullable + public final String boundUdc; + /** Functions the profile asked for. */ + public final Set requested; + /** Functions actually created and linked into the gadget. */ + public final Set active; + /** Requested functions the kernel could not provide (requested minus active). */ + public final Set dropped; + + private ApplyResult(boolean success, + @Nullable String boundUdc, + @NonNull Set requested, + @NonNull Set active) { + this.success = success; + this.boundUdc = boundUdc; + this.requested = immutable(requested); + this.active = immutable(active); + EnumSet drop = isEmpty(requested) + ? EnumSet.noneOf(GadgetFunction.class) + : EnumSet.copyOf(requested); + drop.removeAll(active); + this.dropped = Collections.unmodifiableSet(drop); + } + + public boolean hasDropped() { + return !dropped.isEmpty(); + } + + static ApplyResult failure(@NonNull Set requested, + @NonNull Set active) { + return new ApplyResult(false, null, requested, active); + } + + static ApplyResult bound(@NonNull String udc, + @NonNull Set requested, + @NonNull Set active) { + return new ApplyResult(true, udc, requested, active); + } + + private static boolean isEmpty(Set s) { + return s == null || s.isEmpty(); + } + + private static Set immutable(Set s) { + return isEmpty(s) + ? Collections.unmodifiableSet(EnumSet.noneOf(GadgetFunction.class)) + : Collections.unmodifiableSet(EnumSet.copyOf(s)); + } +} diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java index d9d01e6f..e9733505 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java @@ -77,9 +77,20 @@ public GadgetState readState() { return new GadgetState(exists, bound, bound ? udc : null, linked, msFile, hidNode); } - public boolean prepare(@NonNull GadgetProfile profile) { - if (!core.checkRoot()) return false; + /** + * Builds the gadget and its functions. Returns the set of functions that were + * actually created and linked into the configuration. A function whose kernel + * driver is missing (e.g. {@code usb_f_acm} on kernels built without ACM) is + * silently skipped rather than aborting the whole gadget, so a HID+ACM profile + * still comes up as HID-only. An empty result means preparation failed (no + * root, or none of the requested functions could be created). + */ + @NonNull + public EnumSet prepare(@NonNull GadgetProfile profile) { + EnumSet created = EnumSet.noneOf(GadgetFunction.class); + if (!core.checkRoot()) return created; List script = new ArrayList<>(); + // Structural setup must fully succeed, so keep set -e for this part. script.add("set -e"); script.add("mkdir -p " + sq(GADGET_BASE)); script.add("mkdir -p " + sq(GADGET_BASE + "/configs/c.1")); @@ -95,6 +106,10 @@ public boolean prepare(@NonNull GadgetProfile profile) { + sq(GADGET_BASE + "/configs/c.1/strings/0x409/configuration")); script.add("echo 120 > " + sq(GADGET_BASE + "/configs/c.1/MaxPower")); + // Function directories can only be created for drivers the kernel has. + // Creating a missing one fails with ENOENT, so drop set -e from here on and + // let each unsupported function be skipped individually. + script.add("set +e"); if (profile.functions.contains(GadgetFunction.HID_KEYBOARD)) { script.addAll(prepareHidKeyboard()); } @@ -116,13 +131,24 @@ public boolean prepare(@NonNull GadgetProfile profile) { script.addAll(prepareEcm()); } + // Link only the functions whose directory actually came up. for (GadgetFunction f : profile.functions) { + String fnDir = GADGET_BASE + "/functions/" + f.functionDir; String link = GADGET_BASE + "/configs/c.1/" + f.functionDir; - script.add("[ -e " + sq(link) + " ] || ln -s " - + sq(GADGET_BASE + "/functions/" + f.functionDir) + " " + sq(link)); + script.add("if [ -d " + sq(fnDir) + " ] && [ ! -e " + sq(link) + " ]; then " + + "ln -s " + sq(fnDir) + " " + sq(link) + " || true; fi"); } - return run(script); + run(script); + + // The kernel is the source of truth: a function counts as created only + // when its link exists in the configuration. + for (GadgetFunction f : profile.functions) { + if (pathExists(GADGET_BASE + "/configs/c.1/" + f.functionDir)) { + created.add(f); + } + } + return created; } @Nullable @@ -236,17 +262,24 @@ public boolean teardown() { return run(script); } - public boolean apply(@NonNull GadgetProfile profile) { - if (!prepare(profile)) { + @NonNull + public ApplyResult applyProfile(@NonNull GadgetProfile profile) { + EnumSet created = prepare(profile); + if (created.isEmpty()) { + // Nothing the profile asked for is supported by this kernel. teardown(); - return false; + return ApplyResult.failure(profile.functions, created); } String udc = bind(); if (udc == null) { unbind(); - return false; + return ApplyResult.failure(profile.functions, created); } - return true; + return ApplyResult.bound(udc, profile.functions, created); + } + + public boolean apply(@NonNull GadgetProfile profile) { + return applyProfile(profile).success; } public boolean swapMassStorageImage(String absoluteImagePath, diff --git a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java index d51b15b6..791e2343 100644 --- a/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java +++ b/app/src/main/java/com/zalexdev/stryker/usbarsenal/ui/UsbArsenalFragment.java @@ -20,6 +20,7 @@ import com.zalexdev.stryker.R; import com.zalexdev.stryker.hid.capability.HidCapabilities; import com.zalexdev.stryker.hid.capability.HidCapabilityProbe; +import com.zalexdev.stryker.hid.configfs.ApplyResult; import com.zalexdev.stryker.hid.configfs.GadgetFunction; import com.zalexdev.stryker.hid.configfs.GadgetProfile; import com.zalexdev.stryker.hid.configfs.GadgetState; @@ -29,6 +30,7 @@ import java.util.Iterator; import java.util.Locale; +import java.util.Set; public class UsbArsenalFragment extends Fragment implements GadgetProfileAdapter.Callbacks { @@ -156,14 +158,22 @@ public void onApply(@NonNull GadgetProfile profile) { statePill.setBackgroundResource(R.drawable.chip_pill_warn); statePill.setTextColor(0xFFEF6C00); new Thread(() -> { - boolean teardownOk = controller.teardown(); - boolean ok = controller.apply(profile); + controller.teardown(); + ApplyResult result = controller.applyProfile(profile); GadgetState state = controller.readState(); HidCapabilities caps = new HidCapabilityProbe(requireContext()).probe(); requireActivity().runOnUiThread(() -> { bindState(caps, state); - if (ok && state.boundUdc != null) { - core.toaster(getString(R.string.arsenal_apply_ok, state.boundUdc)); + if (result.success && state.boundUdc != null) { + if (result.hasDropped()) { + core.toaster(getString(R.string.arsenal_apply_partial, + functionLabels(result.dropped))); + } else { + core.toaster(getString(R.string.arsenal_apply_ok, state.boundUdc)); + } + } else if (!result.success && result.active.isEmpty() && result.hasDropped()) { + core.toaster(getString(R.string.arsenal_apply_unsupported, + functionLabels(result.dropped))); } else { core.toaster(getString(R.string.arsenal_apply_failed)); } @@ -171,6 +181,28 @@ public void onApply(@NonNull GadgetProfile profile) { }, "arsenal-apply").start(); } + private String functionLabels(@NonNull Set fns) { + StringBuilder sb = new StringBuilder(); + for (GadgetFunction f : fns) { + if (sb.length() > 0) sb.append(", "); + sb.append(getString(functionLabelRes(f))); + } + return sb.toString(); + } + + private static int functionLabelRes(@NonNull GadgetFunction f) { + switch (f) { + case HID_KEYBOARD: return R.string.arsenal_function_hid_kbd; + case HID_MOUSE: return R.string.arsenal_function_hid_mouse; + case MASS_STORAGE: return R.string.arsenal_function_mass; + case RNDIS: return R.string.arsenal_function_rndis; + case ECM: return R.string.arsenal_function_ecm; + case ACM: return R.string.arsenal_function_acm; + case ADB: return R.string.arsenal_function_adb; + default: return R.string.arsenal_functions_label; + } + } + @Override public void onEdit(@NonNull GadgetProfile profile) { openEditor(profile); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4a5c06aa..d1fecacf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1230,6 +1230,8 @@ manager Emulate CD-ROM Inquiry string (28 ASCII) Profile applied — gadget bound to %s + Applied without %1$s — this kernel has no driver for it + Can\'t apply: %1$s not supported by this kernel Failed to apply profile (check logcat) Vendor gadget restored Keep ADB over Wi-Fi (TCP 5555) From 38858ffc9f627b8afd3c8988f058f0f48a23af9f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 01:59:11 +0000 Subject: [PATCH 4/4] Restore adbd's TCP port on Restore vendor so the debugging notification clears With "Keep ADB over Wi-Fi" on (or any switch that set service.adb.tcp.port), Restore vendor never put the port back, and its plain "start adbd" is a no-op when adbd is already running in TCP mode. adbd kept listening on 5555 after the cable was unplugged, which keeps Android's "USB debugging connected" notification on during normal phone use. bind() now captures the original service.adb.tcp.port (an empty value is valid) once per switch, guarded by a switched flag so a second Apply cannot overwrite the originals with our own 5555 or frozen trigger values. unbind() restores the port and restarts adbd so it re-reads it. Also reordered the restore path: adbd is started first and the fallback rebind of the vendor gadget runs afterwards, since the vendor gadget cannot bind until adbd has opened its ffs endpoint. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_017pdnBy5tWi4SFFo3vwPkr3 --- .../hid/configfs/UsbGadgetController.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java index e9733505..c3e62015 100644 --- a/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java +++ b/app/src/main/java/com/zalexdev/stryker/hid/configfs/UsbGadgetController.java @@ -36,6 +36,11 @@ public final class UsbGadgetController { public static final String PREF_KEEP_ADB_TCP = "arsenal_keep_adb_tcp"; private static final String PREF_SAVED_SYS_CONFIGFS = "arsenal_saved_sys_configfs"; private static final String PREF_SAVED_VENDOR_CONFIGFS = "arsenal_saved_vendor_configfs"; + // The adbd TCP port as it was before we switched (often empty or -1), and a + // flag marking that we are currently switched, so a second Apply cannot + // overwrite the captured originals with our own 5555 / frozen values. + private static final String PREF_SAVED_ADB_TCP_PORT = "arsenal_saved_adb_tcp_port"; + private static final String PREF_SWITCHED = "arsenal_gadget_switched"; private static final String KEYBOARD_DESC_HEX = "05010906a101050719e029e71500250175019508810295017508810195057501050819012905910295017503910195067508150025650507190029658100c0"; @@ -207,21 +212,42 @@ public boolean unbind() { // again, restoring their original values (default 1 when never captured). script.add("setprop " + PROP_SYS_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_SYS_CONFIGFS))); script.add("setprop " + PROP_VENDOR_CONFIGFS + " " + sq(savedTriggerValue(PREF_SAVED_VENDOR_CONFIGFS))); - // Re-bind the vendor gadget as a fallback for devices whose init triggers - // do not fire on their own, then bring adbd back for USB ADB. + boolean switched = core.getBoolean(PREF_SWITCHED); + if (switched) { + // Put adbd's TCP port back exactly as it was. Leaving 5555 behind keeps + // adbd listening on the network, which is what keeps Android's "USB + // debugging connected" notification on after the cable is unplugged. + script.add("setprop " + PROP_ADB_TCP_PORT + " " + sq(core.getString(PREF_SAVED_ADB_TCP_PORT))); + // adbd only re-reads the port on start, and a plain start is a no-op + // when it is already running in TCP mode, so restart it. + script.add("stop adbd 2>/dev/null || true"); + script.add("sleep 0.5"); + } + script.add("start adbd 2>/dev/null || true"); + // adbd must be up (ffs ready) before the vendor gadget can bind. init's + // property trigger normally rebinds it; wait briefly and, if it did not, + // bind it ourselves for devices whose triggers do not fire. + script.add("sleep 1"); String udc = capabilities().udcName; if (udc != null) { script.add("if [ -s " + sq(VENDOR_GADGET + "/UDC") + " ]; then : ; " + "elif [ -d " + sq(VENDOR_GADGET) + " ]; then " + "echo " + sq(udc) + " > " + sq(VENDOR_GADGET + "/UDC") + " || true; fi"); } - script.add("start adbd 2>/dev/null || true"); - return run(script); + boolean ok = run(script); + if (switched) core.putBoolean(PREF_SWITCHED, false); + return ok; } private void rememberOriginalTriggerState() { + // Already switched by a previous Apply: the live values are ours (frozen + // triggers, possibly 5555), so keep the originals captured the first time. + if (core.getBoolean(PREF_SWITCHED)) return; rememberTrigger(PREF_SAVED_SYS_CONFIGFS, getprop(PROP_SYS_CONFIGFS)); rememberTrigger(PREF_SAVED_VENDOR_CONFIGFS, getprop(PROP_VENDOR_CONFIGFS)); + // An empty port is a legitimate original value, so store it as-is. + core.putString(PREF_SAVED_ADB_TCP_PORT, getprop(PROP_ADB_TCP_PORT)); + core.putBoolean(PREF_SWITCHED, true); } private void rememberTrigger(String prefKey, String current) {