Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d319b8a authored by Jerry Zhang's avatar Jerry Zhang
Browse files

Write descriptors for Mtp in UsbService

The current model for setting up a functionfs
function is:

UsbDeviceManager#setCurrentFunctions() ->
intent is sent to MtpReceiver to write the descriptors ->
init/hal waits for descriptors to write, then pulls up gadget ->
Gadget is configured, a USB_STATE intent starts MtpServer

The main downside of this is a lack of reliability because
the Mtp process could be killed at any point. Normally, a
gadget is unbound if its control endpoint is closed. no_disconnect
works around this, but is still a little janky. In addition, the
extra intent delays the startup of the gadget.

With the new model, UsbDeviceManager writes the descriptors
on initialization. Since it is a system service, it won't be killed.

UsbDeviceManager#setCurrentFunctions() ->
init/hal pulls up gadget ->
Gadget is configured, a USB_STATE intent starts MtpServer
MtpServer calls UsbManager#getControlFd to get a dup of the control
endpoint.

Also modify permissions so system server can access mtp files.

Bug: 72877174
Test: Change usb configurations to ptp/mtp
Change-Id: Id17d2b5930f4e1f37ec1b4f00add9d594174ad49
parent c7f6eadf
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -105,6 +105,12 @@ interface IUsbManager
    /* Gets the current screen unlocked functions. */
    long getScreenUnlockedFunctions();

    /* Get the functionfs control handle for the given function. Usb
     * descriptors will already be written, and the handle will be
     * ready to use.
     */
    ParcelFileDescriptor getControlFd(long function);

    /* Allow USB debugging from the attached host. If alwaysAllow is true, add the
     * the public key to list of host keys that the user has approved.
     */
+19 −8
Original line number Diff line number Diff line
@@ -191,14 +191,6 @@ public class UsbManager {
     */
    public static final String USB_DATA_UNLOCKED = "unlocked";

    /**
     * Boolean extra indicating whether the intent represents a change in the usb
     * configuration (as opposed to a state update).
     *
     * {@hide}
     */
    public static final String USB_CONFIG_CHANGED = "config_changed";

    /**
     * A placeholder indicating that no USB function is being specified.
     * Used for compatibility with old init scripts to indicate no functions vs. charging function.
@@ -470,6 +462,25 @@ public class UsbManager {
        }
    }

    /**
     * Gets the functionfs control file descriptor for the given function, with
     * the usb descriptors and strings already written. The file descriptor is used
     * by the function implementation to handle events and control requests.
     *
     * @param function to get control fd for. Currently {@link #FUNCTION_MTP} and
     * {@link #FUNCTION_PTP} are supported.
     * @return A ParcelFileDescriptor holding the valid fd, or null if the fd was not found.
     *
     * {@hide}
     */
    public ParcelFileDescriptor getControlFd(long function) {
        try {
            return mService.getControlFd(function);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns true if the caller has permission to access the device.
     * Permission might have been granted temporarily via
+1 −1
Original line number Diff line number Diff line
@@ -652,7 +652,7 @@ public class ZygoteInit {
        String args[] = {
            "--setuid=1000",
            "--setgid=1000",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,1032,1065,3001,3002,3003,3006,3007,3009,3010",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,1024,1032,1065,3001,3002,3003,3006,3007,3009,3010",
            "--capabilities=" + capabilities + "," + capabilities,
            "--nice-name=system_server",
            "--runtime-args",
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package android.mtp;

import com.android.internal.util.Preconditions;

import java.io.FileDescriptor;

/**
 * Java wrapper for MTP/PTP support as USB responder.
 * {@hide}
@@ -34,6 +36,7 @@ public class MtpServer implements Runnable {

    public MtpServer(
            MtpDatabase database,
            FileDescriptor controlFd,
            boolean usePtp,
            Runnable onTerminate,
            String deviceInfoManufacturer,
@@ -44,6 +47,7 @@ public class MtpServer implements Runnable {
        mOnTerminate = Preconditions.checkNotNull(onTerminate);
        native_setup(
                database,
                controlFd,
                usePtp,
                deviceInfoManufacturer,
                deviceInfoModel,
@@ -92,6 +96,7 @@ public class MtpServer implements Runnable {
    public static native final void native_configure(boolean usePtp);
    private native final void native_setup(
            MtpDatabase database,
            FileDescriptor controlFd,
            boolean usePtp,
            String deviceInfoManufacturer,
            String deviceInfoModel,
+6 −12
Original line number Diff line number Diff line
@@ -55,22 +55,17 @@ static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) {
    return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext);
}

static void android_mtp_configure(JNIEnv *, jobject, jboolean usePtp) {
    MtpServer::configure(usePtp);
}

static void
android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jboolean usePtp,
        jstring deviceInfoManufacturer,
        jstring deviceInfoModel,
        jstring deviceInfoDeviceVersion,
        jstring deviceInfoSerialNumber)
android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jobject jControlFd,
        jboolean usePtp, jstring deviceInfoManufacturer, jstring deviceInfoModel,
        jstring deviceInfoDeviceVersion, jstring deviceInfoSerialNumber)
{
    const char *deviceInfoManufacturerStr = env->GetStringUTFChars(deviceInfoManufacturer, NULL);
    const char *deviceInfoModelStr = env->GetStringUTFChars(deviceInfoModel, NULL);
    const char *deviceInfoDeviceVersionStr = env->GetStringUTFChars(deviceInfoDeviceVersion, NULL);
    const char *deviceInfoSerialNumberStr = env->GetStringUTFChars(deviceInfoSerialNumber, NULL);
    MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase),
    int controlFd = dup(jniGetFDFromFileDescriptor(env, jControlFd));
    MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase), controlFd,
            usePtp,
            MtpString((deviceInfoManufacturerStr != NULL) ? deviceInfoManufacturerStr : ""),
            MtpString((deviceInfoModelStr != NULL) ? deviceInfoModelStr : ""),
@@ -201,8 +196,7 @@ android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
// ----------------------------------------------------------------------------

static const JNINativeMethod gMethods[] = {
    {"native_configure",              "(Z)V",  (void *)android_mtp_configure},
    {"native_setup",                "(Landroid/mtp/MtpDatabase;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
    {"native_setup",                "(Landroid/mtp/MtpDatabase;Ljava/io/FileDescriptor;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
                                            (void *)android_mtp_MtpServer_setup},
    {"native_run",                  "()V",  (void *)android_mtp_MtpServer_run},
    {"native_cleanup",              "()V",  (void *)android_mtp_MtpServer_cleanup},
Loading