android usb大体流程解析

我分析这个流程也是从这张图来分析:android usb大体流程解析_第1张图片

首先kernel层是由kernel_imx/drivers/power/下的bq27x00_battery.c和power_supply_core.c以及power_supply_sysfs.c三个文件来做,power_supply_core.c主要提供统一设备文件的创建以及发送power_supply相关的Uevent;bq27x00_battery.c主要提供具体的电池信息.

每当产生充放电中断时,驱动会调用中断函数ext_power_isr,该函数调用power_supply_core.c的power_supply_changed(&(id->bat))函数,发送一个power_supply属性的Uevent,java层的BatteryService接收到该事件后就会调用jni层的native——update函数来获得电池信息,然后通过intnet广播给相应的activity。


那么由kernel层发送一个Uevent,当插入usb时,然后通过NetLinkManage中的NetlinkManager::start()开始创建socket和bind:

mSock = socket(PF_NETLINK,
                        SOCK_DGRAM,NETLINK_KOBJECT_UEVENT,bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)

然后通过一个Handler传送给socketListener.cpp:

mHandler = new NetlinkHandler(mSock);mHandler->start();

然后socketListener.cpp的SocketListener::startListener() 开始监听

mSock = android_get_control_socket(mSocketName),

 mClients->push_back(new SocketClient(mSock));

创建线程pthread_create(&mThread, NULL, SocketListener::threadStart, this)不断监听。

而VolumeManager则通过NetLinkeEent的findParam方法在

handleSwitchEvent

handleBlockEvent

handleUsbCompositeEvent中来获取路径,而在notifyUmsAvailable中的getBroadcaster()则又通过VolumeManager.h调用的SocketListen的*getBroadcaster()。

再由NativeDaemonConnector.java来监听VolumeManager中而来的socket,

public void run() {   listenToSocket();}。

MountService的onEvent给IMountServiceListener传递数据,并且IMountService 通过MountServiceListene给MountService:如MountServiceBinderListener(IMountServiceListener listener)。


      IMountServiceListener通过AIDL向StorageManager传递

onStorageStateChanged(path, oldState, newState);和

onUsbMassStorageConnectionChanged(connected);

在StorageManager中去实现:

public void onUsbMassStorageConnectionChanged(boolean available) {
            final int size = mListeners.size();
            for (int i = 0; i < size; i++) {
                mListeners.get(i).sendShareAvailabilityChanged(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);
            }
        }


在电池信息方面:由于电池电量变化时不能产生中断来通知上层读取电池信息,所以设置了一个定时器,每隔30s发送一个Uevent事件让BatteryService来读取电池信息是否有变化 :在这里充当Uevent观察者的角色

private UEventObserver mUEventObserver = new UEventObserver() {
        @Override
        public void onUEvent(UEventObserver.UEvent event) {
            update();
        }
    };

这里只关注power_supply的事件:

mUEventObserver.startObserverving("SUBSYSTEM=power_supply");
当有power_supply相关的事件上报时,就会调用update函数。
update先调用native_update从sysfs中读取相关状态。

Update然后根据读到的状态更新BatteryService的成员变量,并广播一个Intent来通知其它关注电源状态的组件。

 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);

..........

当有变化时就调用JNI层的native_update()来 更新,通过JNI到   com_android_sever_batteryService.cpp的 android_server_BatteryService_update方法   :

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.batteryTechnologyPath, 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},
};
   

首先kernel层是由kernel_imx/drivers/power/下的bq27x00_battery.c和power_supply_core.c以及power_supply_sysfs.c三个文件来做,power_supply_core.c主要提供统一设备文件的创建以及发送power_supply相关的Uevent;bq27x00_battery.c主要提供具体的电池信息.

每当产生充放电中断时,驱动会调用中断函数ext_power_isr,该函数调用power_supply_core.c的power_supply_changed(&(id->bat))函数,发送一个power_supply属性的Uevent,java层的BatteryService接收到该事件后就会调用jni层的native——update函数来获得电池信息,然后通过intnet广播给相应的activity。


那么由kernel层发送一个Uevent,当插入usb时,然后通过NetLinkManage中的NetlinkManager::start()开始创建socket和bind:

mSock = socket(PF_NETLINK,
                        SOCK_DGRAM,NETLINK_KOBJECT_UEVENT,bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)

然后通过一个Handler传送给socketListener.cpp:

mHandler = new NetlinkHandler(mSock);mHandler->start();

然后socketListener.cpp的SocketListener::startListener() 开始监听

mSock = android_get_control_socket(mSocketName),

 mClients->push_back(new SocketClient(mSock));

创建线程pthread_create(&mThread, NULL, SocketListener::threadStart, this)不断监听。

而VolumeManager则通过NetLinkeEent的findParam方法在

handleSwitchEvent

handleBlockEvent

handleUsbCompositeEvent中来获取路径,而在notifyUmsAvailable中的getBroadcaster()则又通过VolumeManager.h调用的SocketListen的*getBroadcaster()。

再由NativeDaemonConnector.java来监听VolumeManager中而来的socket,

public void run() {   listenToSocket();}。

MountService的onEvent给IMountServiceListener传递数据,并且IMountService 通过MountServiceListene给MountService:如MountServiceBinderListener(IMountServiceListener listener)。


      IMountServiceListener通过AIDLStorageManager传递

onStorageStateChanged(path, oldState, newState);和

onUsbMassStorageConnectionChanged(connected);

StorageManager中去实现

public void onUsbMassStorageConnectionChanged(boolean available) {
            final int size = mListeners.size();
            for (int i = 0; i < size; i++) {
                mListeners.get(i).sendShareAvailabilityChanged(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);
            }
        }


在电池信息方面:由于电池电量变化时不能产生中断来通知上层读取电池信息,所以设置了一个定时器,每隔30s发送一个Uevent事件让BatteryService来读取电池信息是否有变化 :在这里充当Uevent观察者的角色

private UEventObserver mUEventObserver = new UEventObserver() {
        @Override
        public void onUEvent(UEventObserver.UEvent event) {
            update();
        }
    };

这里只关注power_supply的事件:

mUEventObserver.startObserverving("SUBSYSTEM=power_supply");
当有power_supply相关的事件上报时,就会调用update函数。
update先调用native_update从sysfs中读取相关状态。

Update然后根据读到的状态更新BatteryService的成员变量,并广播一个Intent来通知其它关注电源状态的组件。

 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);

..........

当有变化时就调用JNI层的native_update()来,通过JNI   com_android_sever_batteryService.cpp的 android_server_BatteryService_update方法  

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.batteryTechnologyPath, 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},
};

   


你可能感兴趣的:(android,usb)