General Process Analysis of android usb

Source: Internet
Author: User



The analysis process is also analyzed from this figure:
First, the kernel layer consists of the kernel, power_supply_core.c, and kernel files under kernel_imx/drivers/power/. power_supply_core.c mainly provides unified Device File Creation and sending uevents related to power_supply; bq27x00_battery.c mainly provides specific battery information.
The driver calls the interrupt function ext_power_isr whenever a charge/discharge interruption occurs. This function calls the power_supply_core.c power_supply_changed (& (id-> bat) function of power_supply to send a Uevent of the power_supply attribute, after receiving the event, the java-layer BatteryService calls the native -- update function of the jni layer to obtain the battery information and broadcasts it to the corresponding activity through intnet.

Then, a Uevent is sent by the kernel layer. When the usb is inserted, the socket and bind are created through NetlinkManager: start () in NetLinkManage:
MSock = socket (PF_NETLINK,
SOCK_DGRAM, NETLINK_KOBJECT_UEVENT, bind (mSock, (struct sockaddr *) & nladdr, sizeof (nladdr)
Then a Handler is sent to socketListener. cpp:
MHandler = new NetlinkHandler (mSock); mHandler-> start ();
Then the socketListener: startListener () of SocketListener. cpp starts listening.
MSock = android_get_control_socket (mSocketName ),
MClients-> push_back (new SocketClient (mSock ));
The creation thread pthread_create (& mThread, NULL, SocketListener: threadStart, this) is continuously monitored.
VolumeManager uses the findParam method of NetLinkeEent
HandleSwitchEvent
HandleBlockEvent
HandleUsbCompositeEvent to obtain the path, while getBroadcaster () in policyumsavailable is also called through * getBroadcaster () of SocketListen called by VolumeManager. h ().
Then NativeDaemonConnector. java listens to sockets from VolumeManager,
Public void run () {listenToSocket ();}.
The onEvent of MountService transmits data to IMountServiceListener, and IMountService sends MountService to MountService through MountServiceListene, for example, MountServiceBinderListener (IMountServiceListener listener ).

IMountServiceListener is passed to StorageManager through AIDL
OnStorageStateChanged (path, oldState, newState); and
OnUsbMassStorageConnectionChanged (connected );
Implementation in StorageManager:
Public void onUsbMassStorageConnectionChanged (boolean available ){
Final int size = mListeners. size ();
For (int I = 0; I <size; I ++ ){
MListeners. get (I). sendpolicavailabilitychanged (available );
}
Public void onStorageStateChanged (String path, String oldState, String newState ){
Final int size = mListeners. size ();
For (int I = 0; I <size; I ++ ){
MListeners. get (I). sendStorageStateChanged (path, oldState, newState );
}
}

In terms of battery information: the upper layer is notified to read the battery information because the battery cannot be interrupted when the battery changes. Therefore, a timer is set, send a Uevent every 30 s to allow BatteryService to read the battery information and check whether the information has changed: act as the Uevent observer here.
Private UEventObserver mUEventObserver = new UEventObserver (){
@ Override
Public void onUEvent (UEventObserver. UEvent event ){
Update ();
}
};
Here we only focus on the power_supply event:
MUEventObserver. startObserverving ("SUBSYSTEM = power_supply ");
When an event related to power_supply is reported, the update function is called.
Update first calls native_update to read the relevant status from sysfs.
Update updates the member variables of BatteryService Based on the read status, and broadcasts an Intent to notify other components that focus on the Power status.
Private final void sendIntent (){
// Pack up the values and broadcast them to everyone
Intent intent = new Intent (Intent. ACTION_BATTERY_CHANGED );
Intent. addFlags (Intent. FLAG_RECEIVER_REGISTERED_ONLY
| Intent. FLAG_RECEIVER_REPLACE_PENDING );

Int icon = getIcon (mBatteryLevel );

Intent. putExtra (BatteryManager. EXTRA_STATUS, mBatteryStatus );
Intent. putExtra (BatteryManager. EXTRA_HEALTH, mBatteryHealth );
Intent. putExtra (BatteryManager. EXTRA_PRESENT, mBatteryPresent );
Intent. putExtra (BatteryManager. EXTRA_LEVEL, mBatteryLevel );
Intent. putExtra (BatteryManager. EXTRA_SCALE, BATTERY_SCALE );
Intent. putExtra (BatteryManager. EXTRA_ICON_SMALL, icon );
Intent. putExtra (BatteryManager. EXTRA_PLUGGED, mPlugType );
Log. I ("mPlugType", "mPlugType =" + mPlugType + "mBatteryLevel" + mBatteryLevel );
Intent. putExtra (BatteryManager. EXTRA_VOLTAGE, mBatteryVoltage );
Intent. putExtra (BatteryManager. EXTRA_TEMPERATURE, mBatteryTemperature );
Intent. putExtra (BatteryManager. EXTRA_TECHNOLOGY, mBatteryTechnology );
..........
When there is a change, call native_update () in the JNI layer to update it. Use the android_server_BatteryService_update method from JNI to com_android_sever_batteryService.cpp:
Static void android_server_BatteryService_update (JNIEnv * env, jobject obj)
{
SetBooleanField (env, obj, gPaths. acOnlinePath, gFieldIds. mAcOnline );
SetBooleanField (env, obj, gPaths. usbOnlinePath, gFieldIds. mUsbOnline );
SetBooleanField (env, obj, gPaths. batteryPresentPath, gFieldIds. mBatteryPresent );
SetIntField (env, obj, gPaths. batteryCapacityPath, gFieldIds. mBatteryLevel );
SetVoltageField (env, obj, gPaths. batteryVoltagePath, gFieldIds. mBatteryVoltage );
SetIntField (env, obj, gPaths. batteryTemperaturePath, gFieldIds. mBatteryTemperature );
Const int SIZE = 128;
Char buf [SIZE];
If (readFromFile (gPaths. batteryStatusPath, buf, SIZE)> 0)
Env-> SetIntField (obj, gFieldIds. mBatteryStatus, getBatteryStatus (buf ));
Else
Env-> SetIntField (obj, gFieldIds. mBatteryStatus,
GConstants. statusUnknown );

If (readFromFile (gPaths. batteryHealthPath, buf, SIZE)> 0)
Env-> SetIntField (obj, gFieldIds. mBatteryHealth, getBatteryHealth (buf ));

If (readFromFile (gPaths. batterypolicypath, buf, SIZE)> 0)
Env-> SetObjectField (obj, gFieldIds. mBatteryTechnology, env-> NewStringUTF (buf ));
}
Static JNINativeMethod sMethods [] = {
/* Name, signature, funcPtr */
{"Native_update", "() V", (void *) android_server_BatteryService_update },
};

 

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.