Android usb knowledge and androidusb knowledge

Source: Internet
Author: User

Android usb knowledge and androidusb knowledge

Reprinted please indicate the source: http://blog.csdn.net/droyon/article/details/45098027

1. android background

Usb service, which is created at system startup. In this file, the operation class closely related to the usb status is usb devicemanager. Most usb and adb related logics are processed in this class. In UsbDeviceManager, we need to pay attention to three parts. 1. configuration file. 2. private final UEventObserver mUEventObserver = new UEventObserver () and accept UEvent events. Iii. UsbHandler.

The configuration file stores the current usb status. The UEventObserver accepts the usb event and pushes it to UsbHandler for processing. UsbHandler is a key class used to handle Usb events.

2. Where can I get the default Usb status? What is the current default status?

The current status of Usb is saved in the mCurrentFunctions member variable, which is initialized in the UsbHandler structure.

mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "adb");                // Check if USB mode needs to be overridden depending on OEM specific bootmode.                mDefaultFunctions = processOemUsbOverride(mDefaultFunctions);                // 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)) {                    Slog.w(TAG, "resetting config to persistent property: " + mDefaultFunctions);                    SystemProperties.set("sys.usb.config", mDefaultFunctions);                }                mCurrentFunctions = mDefaultFunctions;

3. What is the general adb and Notification pop-up process during usb insertion?

When the usb is inserted, our UEventObserver will receive the information and finally push it to UsbHandler.

UEvent information:

04-17 14:20:03.352 V/UsbDeviceManager(  759): USB UEVENT: {USB_STATE=DISCONNECTED, SUBSYSTEM=android_usb, SEQNUM=7528, ACTION=change, DEVPATH=/devices/virtual/android_usb/android0}
Then run the updateState method of UsbHandler.
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) {                mHandler.updateState(state);            } else if ("START".equals(accessory)) {                if (DEBUG) Slog.d(TAG, "got accessory start");                startAccessoryMode();            }        }    };

UpdateState method: update the mConnection status.

public void updateState(String state) {            int connected, configured;            if ("DISCONNECTED".equals(state)) {                connected = 0;                configured = 0;            } else if ("CONNECTED".equals(state)) {                connected = 1;                configured = 0;            } else if ("CONFIGURED".equals(state)) {                connected = 1;                configured = 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            sendMessageDelayed(msg, (connected == 0) ? UPDATE_DELAY : 0);        }
Finally, update the status in UsbHandler to display the adb and usb Notification.

case MSG_UPDATE_STATE:                    mConnected = (msg.arg1 == 1);                    mConfigured = (msg.arg2 == 1);                    updateUsbNotification();                    updateAdbNotification();                    if (containsFunction(mCurrentFunctions,                            UsbManager.USB_FUNCTION_ACCESSORY)) {                        updateCurrentAccessory();                    } else if (!mConnected) {                        // restore defaults when USB is disconnected                        setEnabledFunctions(mDefaultFunctions, false);                    }                    if (mBootCompleted) {                        updateUsbState();                        updateAudioSourceFunction();                    }                    break;
4. What types of usb are configurable?
public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage";    /**     * Name of the adb USB function.     * Used in extras for the {@link #ACTION_USB_STATE} broadcast     *     * {@hide}     */    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";    /**     * 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 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 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";
These are their definitions. Adb and charging are not compatible at the same time.

5. What is the usb configuration process?

The upper-layer class can be set to usb: usb settings. In this class, you can set the current usb settings status to mtp, ptp, and other usb States defined by the manufacturer.

During user operation, mUsbManager. setCurrentFunction (function, true) is called );

tring function = USB_FUNCTION_DEFAULT;        if (preference == mMtp && mMtp.isChecked()) {            function = UsbManager.USB_FUNCTION_MTP;        } else if (preference == mPtp && mPtp.isChecked()) {            function = UsbManager.USB_FUNCTION_PTP;        } else if (preference == mCharging && mCharging.isChecked()) {            function = UsbManager.USB_FUNCTION_CHARGING;        } else if (preference == mSDCard && mSDCard.isChecked()) {            function = UsbManager.USB_FUNCTION_MASS_STORAGE;        }        operateInprogress = true;        mUsbManager.setCurrentFunction(function, true);        updateToggles(function);
USB Manager then calls the setCurrentFunction of USB service.

USB service. java

@Override    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");        }    }
The setCurrentFunction in USB devicemanager is still called. For the remaining steps, see 2.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.