Android usb analysis (1) UsbDeviceManager (and5.1)

Copyright statement: This article is a reprinted article, following the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Link to this article: https://blog.csdn.net/kc58236582/article/details/47810987


Let's first sort out the entire usb architecture. The user calls the interface from the UsbManager and communicates with the binder to the UsbService. And UsbService has two instances, one

UsbHostManager and one UsbDeviceManager. UsbDeviceManager and

UsbHostManager are relative concepts.

UsbHostManager is a mobile phone as a host, such as a keyboard and a mouse connected to a mobile phone via USB. The UsbDeviceManager is the connection between the mobile phone and the computer.

First look at the code of UsbDevice: just look at the constructor and systemReady

    public UsbService(Context context) {
        mContext = context;
 
        final PackageManager pm = mContext.getPackageManager();
        if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
            mHostManager = new UsbHostManager(context);//new UsbHostManager
        }
        if (new File("/sys/class/android_usb").exists()) {
            mDeviceManager = new UsbDeviceManager(context);//new UsbDeviceManager
        }
 
        setCurrentUser(UserHandle.USER_OWNER);
 
        final IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(Intent.ACTION_USER_SWITCHED);
        userFilter.addAction(Intent.ACTION_USER_STOPPED);
        mContext.registerReceiver(mUserReceiver, userFilter, null, null);
    }
    public void systemReady() {
        if (mDeviceManager != null) {
            mDeviceManager.systemReady();
        }
        if (mHostManager != null) {
            mHostManager.systemReady();
        }
    }


Today I will mainly introduce UsbDeviceManager, first look at the constructor:

    public UsbDeviceManager(Context context) {         Log.d(TAG,"UsbDeviceManager start , persist.sys.usb.config =" + SystemProperties.get("persist.sys.usb.config", ""));         mContext = context;         mContentResolver = context.getContentResolver();         mMassStorageEnable = SystemProperties.get("persist.mass_storage.enable", "true");//mass property         PackageManager pm = mContext.getPackageManager();         mHasUsbAccessory = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESS ORY );         initRndisAddress();//Initialize the usb shared address, use the serial code of the mobile phone as the address, the serial code is readOemUsbOverrideConfig() in the system properties         ;//Read the factory setting         mHandler = new UsbHandler(FgThread.get(). getLooper());//Construct UsbHandler, see details later







 

 

 
        if (nativeIsStartRequested()) {
            if (DEBUG) Slog.d(TAG, "accessory attached at boot");
            startAccessoryMode();
        }
 
        boolean secureAdbEnabled = SystemProperties.getBoolean("ro.adb.secure", false);
        boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
        if (secureAdbEnabled && !dataEncrypted) {
            mDebuggingManager = new UsbDebuggingManager(context);
        }
    }
接下来看UsbHandler这个内部类:

First look at the first half of the usbHandler constructor:

        public UsbHandler(Looper looper) {
            super(looper);
            try {
                // persist.sys.usb.config should never be unset.  But if it is, set it to "adb"
                // so we have a chance of debugging what happened.
                Log.d(TAG,"UsbHandler , persist.sys.usb.config :" + SystemProperties.get("persist.sys.usb.config"));
                if(SystemProperties.get("persist.sys.usb.config", "") == null || SystemProperties.get("persist.sys.usb.config", "") == "none"){//永久性属性里面没有就设置
                   if("false".equals(mMassStorageEnable)){
                        SystemProperties.set("persist.sys.usb.config", "mtp,adb");
                   }else{
                        SystemProperties.set("persist.sys.usb.config", "mtp,mass_storage,adb"); } }
                    if
                (
                "false".equals(mMassStorageEnable)){//If there is, directly put the obtained properties in mDefaultFunctions
                    mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "mtp,adb");
                }else{                     mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "mtp,mass_storage,adb" );                 }                 Log.d(TAG,"mMassStorageEnable ="+ mMassStorageEnable +", mDefaultFunctions :" + mDefaultFunctions);                 // Check if USB mode needs to be overridden depending on OEM specific bootmode.




                mDefaultFunctions = processOemUsbOverride(mDefaultFunctions);//Customized by the manufacturer
 
                // sanity check the sys.usb.config system property
                // this may be necessary if we crashed while switching USB configurations
                String config = SystemProperties.get("sys.usb.config ", "none");
                if (!config.equals(mDefaultFunctions)) {//If mDefaultFunctions is different from the "sys.usb.config" property, reset the property
                    Slog.w(TAG, "resetting config to persistent property : " + mDefaultFunctions);
                    SystemProperties.set("sys.usb.config", mDefaultFunctions);
                }
The above theoretically persist.sys.usb.config is the same as the sys.usb.config property

Because the persist.sys.usb.config property is set in init.usb.rc, sys.usb.config will also be set together.

# Used to set USB configuration at boot and to switch the configuration
# when changing the default configuration
on property:persist.sys.usb.config=*
    setprop sys.usb.config $persist.sys.usb.config
继续分析usbHandler的构造函数

                mCurrentFunctions = getDefaultFunctions();
                String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
                Log.d(TAG,"UsbHandler updateState  state:" + state);
                mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);//属性中如果有adb,就置位true
                updateState(state);
                Log.d(TAG," UsbHandler charging_value:" + SystemProperties.get("sys.usb.charging", "")
                        + "  ro.debuggable:" + SystemProperties.get("ro.debuggable", ""));
                if(SystemProperties.get("sys.usb.charging", "").equals("yes") && SystemProperties.get("ro.debuggable","").equals("1")) { Let's look at the updateState function, the state reads the state from the node below

private static final String STATE_PATH =
            "/sys/class/android_usb/android0/state"
updateState函数如下:

        public void updateState(String state) {
            Log.d(TAG, "updateState : state = " + state );
            int connected, configured;
 
            if ("DISCONNECTED".equals(state)) {
                connected = 0;
                configured = 0;
                SystemProperties.set("sys.lc.usb.state", "0");
            } else if ("CONNECTED".equals(state)) {
                connected = 1;
                configured = 0;
            } else if ("CONFIGURED".equals(state)) {
                connected = 1;
                configured = 1;
                SystemProperties.set("sys.lc.usb.state", "1");
            } else {
                Slog.e(TAG, "unknown state " + state);
                return;
            }
            removeMessages(MSG_UPDATE_STATE);
            Message msg = Message.obtain(this, MSG_UPDATE_STATE);
            msg.arg1 = connected;
            msg.arg2 = configured;
            // debounce disconnects to avoid problems bringing up USB tethering
            sendMessage(msg);//Send message after parameter configuration
and process after receiving message

        public void handleMessage(Message msg) {             Log.d(TAG," handleMessage msg:" + msg + " msg.arg1:" + msg.arg1 + " msg.arg2 "+ msg.arg2);             switch (msg.what) {                 case MSG_UPDATE_STATE:                     mConnected = (msg.arg1 == 1);                     mConfigured = (msg.arg2 == 1);                     updateUsbNotification()                     ; updateAdbNotification();                     if (containsFunction(mCurrentFunctions,                             UsbManager.USB_FUNCTION_ACCESSORY)) {                         update CurrentAccessory();                     } else if (!mConnected) {//After the usb is disconnected, the property is set to the default











                        // restore defaults when USB is disconnected
                        setEnabledFunctions(getDefaultFunctions(), false);
                    }
                    if (mBootCompleted) {                         updateUsbState();//Send broadcast                         updateAudioSourceFunction();                     }                     break; continue analysis




                String value = SystemProperties.get("persist.service.adb.enable", "");
                if (value.length() > 0) {
                    char enable = value.charAt(0);
                    if (enable == '1') {
                        setAdbEnabled(true);
                    } else if (enable == '0') {
                        setAdbEnabled(false);
                    }
                    SystemProperties.set("persist.service.adb.enable", "");
                }
分析setAdbEnabled函数
        private void setAdbEnabled(boolean enable) {
            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable + " mCurrentFunctions:" + mCurrentFunctions);
            if (enable != mAdbEnabled) {//当mAdbEnabled改变时
                mAdbEnabled = enable;
                // Due to the persist.sys.usb.config property trigger, changing adb state requires
                // persisting default function
                if(!mCurrentFunctions.equals(UsbManager.USB_FUNCTION_CHARGING)){
                    setEnabledFunctions(mDefaultFunctions, true);//先设置永久属性
                    // After persisting them use the lock-down aware function set
                    setEnabledFunctions(getDefaultFunctions(), false);//再设置usb.config属性
                    updateAdbNotification();
                }
            }
            if (mDebuggingManager != null) {
                mDebuggingManager.setAdbEnabled(mAdbEnabled);
            }
        }
Let's look at the setEnabledFunctions function again

        private void setEnabledFunctions(String functions, boolean makeDefault) {
            if (DEBUG) Slog.d(TAG, "setEnabledFunctions " + functions
                    + " makeDefault: " + makeDefault);
            // Do not update persystent.sys.usb.config if the device is booted up
            // with OEM specific mode.
            if (functions != null && makeDefault && !needsOemUsbOverride()) {//设置永久属性
                if (mAdbEnabled && !functions.equals(UsbManager.USB_FUNCTION_CHARGING)) {
                    functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);//根据mAdbEnabled设置系统属性
                } else {
                    functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
                }
                if (!mDefaultFunctions.equals(functions)) {//When functions change
                    if (!setUsbConfig("none")) {//Set none, disconnect usb directly, That is, different attributes are set almost every time. You have to disconnect the usb first. Take a look at the setUsbConfig function in detail later
                        Slog.e(TAG, "Failed to disable USB");
                        // revert to previous configuration if we fail
                        setUsbConfig(mCurrentFunctions);//
                        return;
                    }
                    // setting this property will also change the current USB state
                    // via a property trigger
                    SystemProperties.set("persist.sys.usb.config", functions);// Set the permanent property
                    if (waitForState(functions)) {//It is the set property, to read whether the property is the same as the set
                        if(mCurrentFunctions. equals(UsbManager.USB_FUNCTION_CHARGING) && !mCurrentFunctions.equals(functions)){                             if (DEBUG) Slog.d(TAG, " change charging functions to new functions: " + functions);                                 SystemProperties.set("sys.usb.charging" , "no");                         }                         mCurrentFunctions = functions;                         mDefaultFunctions = functions;                     } else {//Unsuccessful, set layer default






                        Slog.e(TAG, "Failed to switch persistent USB config to " + functions);
                        // revert to previous configuration if we fail
                        SystemProperties.set("persist.sys.usb.config", mDefaultFunctions);
                    }             } }
                non -persistent Attributes are almost the same as permanent attributes. Just one is setting permanent properties. One is to set the usb.config property

else {
                if (functions == null) {
                    functions = mDefaultFunctions;
                }
 
                // Override with bootmode specific usb mode if needed
                functions = processOemUsbOverride(functions);
 
                if (mAdbEnabled && !functions.equals(UsbManager.USB_FUNCTION_CHARGING)) {
                    functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
                } else {
                    functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
                }
                if (!mCurrentFunctions.equals(functions)) {
                    if (!setUsbConfig("none")) {//断开usb
                        Slog.e(TAG, "Failed to disable USB");
                        // revert to previous configuration if we fail
                        setUsbConfig(mCurrentFunctions);
                        return;
                    }
                    if (setUsbConfig(functions)) {
                        if(mCurrentFunctions.equals(UsbManager.USB_FUNCTION_CHARGING) && !mCurrentFunctions.equals(functions)){
                            if (DEBUG) Slog.d(TAG, " change charging functions to new functions: " + functions);
                                SystemProperties.set("sys.usb.charging", "no");
                        }
                        mCurrentFunctions = functions;
                    } else {                         Slog.e(TAG, "Failed to switch USB config to " + functions);                         // revert to previous configuration if we fail                         setUsbConfig(mCurrentFunctions);                     }                 }             } Let's look at the function setUsbConfig again






        private boolean setUsbConfig(String config) {
            if (DEBUG) Slog.d(TAG, "setUsbConfig(" + config + ")");
            // set the new configuration
            SystemProperties.set("sys.usb.config", config);
            return waitForState(config);
        }
waitForState函数

        private boolean waitForState(String state) {
            // wait for the transition to complete.
            // give up after 1 second.
            for (int i = 0; i < 40; i++) {
                // State transition is done when sys.usb.state is set to the new configuration
                if (state.equals(SystemProperties.get("sys.usb.state"))){//设置的属性和读取的属性相同
                    Log.i(TAG,"Setting SystemProperties ,i =  "+i);
                    if(state.equals("none")){
                        if(SystemProperties.get("sys.lc.usb.state").equals("0")){
                            return true;
                        }else{
                            Log.i(TAG,"sys.lc.usb.state="+SystemProperties.get("sys.lc.usb.state"));
                        }
                    }else{                         return true;                     }                 }                 SystemClock.sleep(50);/ / Not the same, sleep for 50ms, that is, sleep for 2s at most             }             Slog.e(TAG, "waitForState(" + state + ") FAILED");             return false;         } Continue to analyze usbhandler                 mContentResolver.registerContentObserver(                         Settings.Global.getUriFor(Settings .Global.ADB_ENABLED),                                 false, new AdbSettingsObserver());//Register an adb observer and send messages.












 
                // Watch for USB configuration changes
                mUEventObserver.startObserving(USB_STATE_MATCH);//开启两个节点的监控。
                mUEventObserver.startObserving(ACCESSORY_START_MATCH);
 
                IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
                filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
                mContext.registerReceiver(mBootCompletedReceiver, filter);
                mContext.registerReceiver(
                        mUserSwitchedReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
                mContext.registerReceiver(
                        mBatteryChangedReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
AdbSettingsObserver 发送消息
    private class AdbSettingsObserver extends ContentObserver {
        public AdbSettingsObserver() {
            super(null);
        }
        @Override
        public void onChange(boolean selfChange) {
            boolean enable = (Settings.Global.getInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, 0) > 0);
            mHandler.sendMessage(MSG_ENABLE_ADB, enable);
        }
    }
消息处理,调用setAdbEnabled
                case MSG_ENABLE_ADB:
                    setAdbEnabled(msg.arg1 == 1);
                    break;
UEventObserver 分析
    private final UEventObserver mUEventObserver = new UEventObserver() {         @Override         public void onUEvent(UEventObserver.UEvent event) {             if (DEBUG) Slog.v(TAG, "USB UEVENT: " + event.toString());             String state = event. get("USB_STATE");             String accessory = event.get("ACCESSORY");             if (state != null) {//state corresponds to a node                 mHandler.updateState(state); //updateState function has been analyzed before             } else if ("START".equals(accessory)) {//accessory corresponds to a node                 if (DEBUG) Slog.d(TAG, "got accessory start");                 startAccessoryMode();             }         }     }



 










startAccessoryMode function

    private void startAccessoryMode() {
        if (!mHasUsbAccessory) return;
 
        mAccessoryStrings = nativeGetAccessoryStrings();
        boolean enableAudio = (nativeGetAudioMode() == AUDIO_MODE_SOURCE);
        // don't start accessory mode if our mandatory strings have not been set
        boolean enableAccessory = (mAccessoryStrings != null &&
                        mAccessoryStrings[UsbAccessory.MANUFACTURER_STRING] != null &&
                        mAccessoryStrings[UsbAccessory.MODEL_STRING] != null);
        String functions = null;
 
        if (enableAccessory && enableAudio) {
            functions = UsbManager.USB_FUNCTION_ACCESSORY + ","
                    + UsbManager.USB_FUNCTION_AUDIO_SOURCE;
        } else if (enableAccessory) {
            functions = UsbManager.USB_FUNCTION_ACCESSORY;
        } else if (enableAudio) {
            functions = UsbManager.USB_FUNCTION_AUDIO_SOURCE;
        }
 
        if (functions != null) {
            mAccessoryModeRequestTime = SystemClock.elapsedRealtime();
            setCurrentFunctions(functions, false);//设置属性
        }
    }


Also, the user calls setCurrentFunction in usbmanger

    public void setCurrentFunction(String function, boolean makeDefault) {
        try {
            mService.setCurrentFunction(function, makeDefault);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setCurrentFunction", e);
        }
    }
接着调用usbservice中的setCurrentFunction函数

    public void setCurrentFunction(String function, boolean makeDefault) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
 
        // If attempt to change USB function while file transfer is restricted, ensure that
        // the current function is set to "none", and return.
        UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
            if (mDeviceManager != null) mDeviceManager.setCurrentFunctions("none", false);
            return;
        }
 
        if (mDeviceManager != null) {
            mDeviceManager.setCurrentFunctions(function, makeDefault);
        } else {             throw new IllegalStateException("USB device mode not supported");         }     } Finally call the setCurrentFunctions function in usbdevicemanager




    public void setCurrentFunctions(String functions, boolean makeDefault) {
        String currentUsbConfig = SystemProperties.get("sys.usb.config", "none");// 先从属性里面读出来,主要看有没有adb
        Log.i(TAG, "begin to setCurrentFunction : sys.usb.config = " + currentUsbConfig + " , mMassStorageEnable:"
                            + mMassStorageEnable + " , functions =" + functions +" , makeDefault =" + makeDefault);
        if(functions != null){
            if(containsFunction(functions,UsbManager.USB_FUNCTION_ENABLE_SERIAL)){
                functions = "serial";
            }else if(containsFunction(functions,UsbManager.USB_FUNCTION_DISABLE_SERIAL)){
                if("false".equals(mMassStorageEnable)){
                    functions = "mtp";
                }else{                     functions = "mtp,mass_storage";                 }             }else if(containsFunction(functions,UsbManager.USB_FUNCTION_RNDIS)){//usb sharing depends on whether the serial port is open,                 if(containsFunction(currentUsbConfig, UsbManager.USB_FUNCTION_SERIAL)){                     functions = "rndis,serial";//Open the serial port to set these two items into the properties                 }             }         }         if(containsFunction(currentUsbConfig,UsbManager.USB_FUNCTION_ADB)){             if(functions != null)addFunction(functions,UsbManager.USB_FUNCTION_ADB);         }











        Log.i(TAG, "setCurrentFunction finally function:" + fixFunction(functions) + " makeDefault:" + makeDefault);
        mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, fixFunction(functions), makeDefault);
    }
For fixFunction, add some other properties , because setting the property sets all the

    private String fixFunction(String function) {
        Log.d(TAG," fixFunction: function:" + function);
        String ret = function;
        if (null == function) {
            return null;
        }
 
        if (containsFunction(function, UsbManager.USB_FUNCTION_MTP)){
            if("false".equals(mMassStorageEnable)){
                ret = "mtp";
            } else {
                ret = "mtp,mass_storage";
            }
        } else if (containsFunction(function, UsbManager.USB_FUNCTION_PTP)) {
            if("false".equals(mMassStorageEnable)){
                ret = "ptp";
            } else {
                ret = "ptp,mass_storage";
            }
        } else {             return ret;         }         if (containsFunction(function, UsbManager.USB_FUNCTION_ADB)) {             addFunction(ret, UsbManager.USB_FUNCTION_ADB);         }         return ret;     } After the message is sent, the message is processed. The main thing is to call setEnabledFunctions to set properties. When the attribute contains rndis and accessory, it often leads to the attribute value that cannot be obtained in time. And waitforstate will wait, and a thread will start here.


 



 


                case MSG_SET_CURRENT_FUNCTIONS:
                    final String functions = (String)msg.obj;
                    final boolean makeDefault = (msg.arg1 == 1);
                    Log.d(TAG, "set current funtion to " + functions);
                    if (null != functions){
                        if(!functions.contains(UsbManager.USB_FUNCTION_CHARGING)){
                            mConnected_InChargeOnlyMode = false;
                        }else{
                            mConnected_InChargeOnlyMode = true;
                        }
                    }
                    if (null != functions && (functions.contains("rndis")||functions.contains("accessory"))) {
                        new Thread() {
                            public void run() {
                                 Log.d(TAG, "new Thread setEnabledFunctions to " + functions);
                               setEnabledFunctions(functions, makeDefault);
                             }
                            }.start();
                       } else {
                           setEnabledFunctions(functions, makeDefault);
                    }
                    break;
下面大致说下各个属性。
    public static final String USB_FUNCTION_ADB = "adb";
 
    /**
     * Name of the RNDIS ethernet USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_RNDIS = "rndis";//usb共享
 
    /**
     * Name of the MTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_MTP = "mtp";
 
    /**
     * Name of the PTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_PTP = "ptp";
 
    /**
     * Name of the audio source USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source";
 
    /**
     * Name of the Accessory USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_ACCESSORY = "accessory";
 
     /**
     * Name of the charging USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_CHARGING = "charging";
 
        /**
     * Name of the serial USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     *
     * {@hide}
     */
    public static final String USB_FUNCTION_SERIAL = "serial";//开启串口的

There is also the entire usb.config property after setting. Some triggers in init.usb.rc are fired. This is analyzed in init's blog.

Let's take a look at the entire init.usb.rc, all permutations and combinations are listed. The last one is to set the sys.usb.config property when the persist.sys.usb.config permanent system property is set.

# Used to disable USB when switching states
on property:sys.usb.config=none//设置属性none
    stop adbd
    write /sys/class/android_usb/android0/enable 0//写enable为0
    write /sys/class/android_usb/android0/bDeviceClass 0
    setprop sys.usb.state $sys.usb.config
 
#mtp,mass_storage,adb
on property:sys.usb.config=mtp,mass_storage,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181C
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#mtp,mass_storage
on property:sys.usb.config=mtp,mass_storage
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181C
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#ptp,mass_storage,adb
on property:sys.usb.config=ptp,mass_storage,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181D
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#ptp,mass_storage
on property:sys.usb.config=ptp,mass_storage
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181D
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#rndis,adb,serial
on property:sys.usb.config=rndis,adb,serial
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181F
    write /sys/class/android_usb/android0/functions rndis,serial,adb
    write /sys/class/android_usb/android0/f_rndis/wceis 1
    write /sys/class/android_usb/android0/f_serial/serial_port_num 4
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state rndis,serial,adb
 
#rndis,serial,adb
on property:sys.usb.config=rndis,serial,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181F
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_rndis/wceis 1
    write /sys/class/android_usb/android0/f_serial/serial_port_num 4
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#rndis,serial
on property:sys.usb.config=rndis,serial
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181F
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_rndis/wceis 1
    write /sys/class/android_usb/android0/f_serial/serial_port_num 4
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#rndis,adb
on property:sys.usb.config=rndis,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181E
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_rndis/wceis 1
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#rndis
on property:sys.usb.config=rndis
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181E
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_rndis/wceis 1
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#mass_storage,serial,adb
on property:sys.usb.config=mass_storage,serial,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181B
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_serial/serial_port_num 5
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
 
#accessory,adb
on property:sys.usb.config=accessory,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 2D00
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#accessory
on property:sys.usb.config=accessory
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 2D01
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#adb
on property:sys.usb.config=adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1819
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#mtp,adb
on property:sys.usb.config=mtp,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1818
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#mtp
on property:sys.usb.config=mtp
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1818
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#ptp,adb
on property:sys.usb.config=ptp,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1817
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
#ptp
on property:sys.usb.config=ptp
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1817
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
#serial,adb
on property:sys.usb.config=serial,adb
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1816
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_serial/serial_port_num 4
    write /sys/class/android_usb/android0/enable 1
    start adbd
    setprop sys.usb.state $sys.usb.config
 
 
#serial
on property:sys.usb.config=serial
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 1816
    write /sys/class/android_usb/android0/functions $sys.usb.config
    write /sys/class/android_usb/android0/f_serial/serial_port_num 4
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
 
 
#USB Charging Only configuration
#for android_usb device should not been none,so set a default function in this mode
on property:sys.usb.config=charging
    write /sys/class/android_usb/android0/enable 0
    write /sys/class/android_usb/android0/idVendor 18D1
    write /sys/class/android_usb/android0/idProduct 181C
    write /sys/class/android_usb/android0/functions mtp,mass_storage,adb
    write /sys/class/android_usb/android0/enable 1
    setprop sys.usb.state $sys.usb.config
    setprop sys.usb.charging yes
 
on property:sys.usb.charging=yes
    write /sys/devices/platform/comip-u2d/charge_mode 1
 
on property:sys.usb.charging=no
    write /sys/devices/platform/comip-u2d/charge_mode 0
 
# Used to set USB configuration at boot and to switch the configuration
# when changing the default configuration
on property:persist.sys.usb.config=*
    setprop sys.usb.config $persist.sys.usb.config

The analysis of usbdevicemanager is almost finished. For the time being, we will continue to analyze the usb part first, such as UsbHostManager and UsbSettingsManager.
————————————————
Copyright statement: This article is an original article of CSDN blogger "kc column", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this article for reprinting statement.
Original link: https://blog.csdn.net/kc58236582/article/details/47810987

Guess you like

Origin blog.csdn.net/grf123/article/details/102970805