这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@Grimler91
Copy link
Member

This PR adds patches for termux-api from termux/termux-api-package#204, and patches hidapi to make use of them, and adds a patched variant of heimdall that uses the patches.
If python-hidapi is installed, then electrum can also make use of it and termux-api to access hardware wallets (this only works in electrum if qt gui is used through github.com/termux/termux-x11 or vnc though).

More packages can and should be patched to make use of this new termux-usb library. For many packages it is as easy as replacing libusb_get_device_list with termux_usb_device_list and libusb_open with termux_usb_open+libusb_wrap_sys_device+libusb_get_device.

When termux_usb_open is run it shows a prompt in android, so it will not be possible to use termux-usb "un-attended".

To test this out one also needs the termux-api app built from the branch here: termux/termux-api#759

@Grimler91 Grimler91 requested a review from TomJo2000 as a code owner April 3, 2025 19:08
On Android we cannot use libusb_get_device_list and libusb_open
directly. Instead we need to enumerate and open devices through the
Android API. In Termux we have termux-api for handling this
communication, so we can replace the libusb_.. functions with
termux_usb_.. variants.

WIP for now, code segfaults when it tries to open a device.
Required by electrum if one wants to access some hardware wallets.
Fix two text gui crashes.
The package is EOL, and should hence not be welcome in our
repositories. It is however required by electrum (to support legacy
hardware keys), and it is no longer possible to install it through pip
without an extra patch, so let's make an exception and add it to our
repositories. Hopefully electrum can be fixed so that this dependency
is optional.
If a ledger hardware wallet is to be used with electrum, then these
dependencies are needed.
Instead of libsecp256k1.so.1 or libsecp256k1.so.2. Termux's
libsecp256k1 package provides the library without version suffix.
Add patches to use termux-api/termux-usb to enumerate and open
devices.
@Grimler91
Copy link
Member Author

Closes: #10437

@twaik
Copy link
Member

twaik commented Apr 14, 2025

Probably we should implement usb device discovery at libusb level. Patching all packages one by one may be hard and unnecessary.

@Grimler91
Copy link
Member Author

Probably we should implement usb device discovery at libusb level. Patching all packages one by one may be hard and unnecessary.

Yeah, that would be nicer. Rather than just patching libusb_get_device_list and friends I think we would have to write a full libusb termux backend. Writing a backend would be better, but difficult. As I see it the difficulties are:

  • The android API only provides some parts of what the libusb linux backend provides through scanning of sysfs/usbfs, so the discovered devices from our backend would be (very) incomplete compared to linux version
    • The android API does not provide access to the interface "altSetting" at all for example, which quite a few packages seem to use in their "scan devices" functions (1, 2), so we cannot make a fully compatible drop-in replacement.
  • After a device is opened though, libusb can access all the information as done on linux, so our backend should fill in all the information after a device is opened. Packages might have already tried to parse this information though, so information might be missing.
    • We could change so that every connected usb device is opened in the scan step, all information should then be available and it should be more similar to linux version, but this does not seem user friendly or a good idea really
  • For rooted devices original libusb works fine, so by patching packages one at the time (and verifying that they work), we keep other packages usable with original libusb on rooted devices

With separate termux_usb_* we are pretty clear about them not being full replacement, and each package has to be patched and tested separately. In my opinion it would be nice to get something like in this PR in to start with, and if I/we then later manage to write a libusb backend, then we can just drop these patches.

@twaik
Copy link
Member

twaik commented Apr 14, 2025

Oh, that makes sense, thank you.

@Grimler91
Copy link
Member Author

Oh, that makes sense, thank you.

Very good that you brought it up, I did not write a very detailed message here. Thanks!

+#include "UsbAPI.pb-c.h"
+
+#define BUF_SIZE 1024
+#define TERMUX_PREFIX "/data/data/com.termux/files/usr"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be replaced with @TERMUX_PREFIX@.

Comment on lines +98 to +156
+++ b/usb/proto/UsbAPI.proto
@@ -0,0 +1,57 @@
+syntax = "proto3";
+
+package usbapi;
+
+option java_package = "com.termux.api";
+option java_outer_classname = "UsbAPIProto";
+
+/* Modelled after libusb's struct libusb_endpoint_descriptor */
+message termuxUsbEndpointDescriptor {
+ int32 endpointAddress = 1;
+ int32 attributes = 2;
+ int32 maxPacketSize = 3;
+ int32 interval = 4;
+}
+
+/* Modelled after libusb's struct libusb_interface_descriptor */
+message termuxUsbInterfaceDescriptor {
+ int32 alternateSetting = 1;
+ int32 interfaceClass = 2;
+ int32 interfaceSubclass = 3;
+ int32 interfaceProtocol = 4;
+ string interface = 5;
+ repeated termuxUsbEndpointDescriptor endpoint = 6;
+}
+
+/* Modelled after libusb's struct libusb_config_descriptor */
+message termuxUsbConfigDescriptor {
+ int32 configurationValue = 1;
+ int32 maxPower = 2;
+ string configuration = 3;
+ repeated termuxUsbInterfaceDescriptor interface = 4;
+}
+
+/* Modelled after libusb's struct libusb_device_descriptor */
+message termuxUsbDeviceDescriptor {
+ int32 configurationCount = 1;
+ int32 deviceClass = 2;
+ int32 deviceProtocol = 3;
+ int32 deviceSubclass = 4;
+ int32 productId = 5;
+ int32 vendorId = 6;
+ string manufacturerName = 7;
+ string productName = 8;
+ string serialNumber = 9;
+}
+
+/* Loosely modelled after libusb's internal struct libusb_device */
+message termuxUsbDevice {
+ int32 busNumber = 1;
+ int32 portNumber = 2;
+ string deviceAddress = 3;
+ termuxUsbDeviceDescriptor device = 4;
+}
+
+message termuxUsb {
+ repeated termuxUsbDevice device = 1;
+}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is easy to loose this with package update. Probably this file should be somehow downloaded from correct termux/termux-api git branch or at least be versioned and the version should be checked somewhere in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe we should have some sort of termux-shared repo with the protobuf definitions. I think Apollo had some ideas already for the upcoming termux-api re-work. Having a version is a good idea, I'll think a bit on where/how to add it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants