Android Sensor 之 SensorService

代码路径:

./frameworks/native/services/sensorservice/SensorService.h

./frameworks/base/core/java/android/hardware/SensorManager.java
./frameworks/base/core/jni/android_hardware_SensorManager.cpp

Zygote启动之后,调用SystemServer的main方法(调用run方法)启动系统服务;

./frameworks/base/services/java/com/android/server/SystemServer.java

SystemServer类中提供的run 方法中,在启动service之前,会加载本地动态库System.loadLibrary("android_servers")初始化本地 Native service

/**
 * The main entry point from zygote.
 */
public static void main(String[] args) {
    new SystemServer().run();
}
public final class SystemServer {
    private static final String TAG = "SystemServer";
    private void run() {
            // Initialize native services.
            System.loadLibrary("android_servers");
        // Start services.
        try {
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
        }
...
}

在  startBootstrapServices(); 的过程的最后 会调用startSensorService();

    private void startBootstrapServices() {
......
        // The sensor service needs access to package manager service, app ops
        // service, and permissions service, therefore we start it after them.
        startSensorService();
    }

在JNI中 startSensorService() 既是被调用的本地方法;

通过对JNI的了解可知 System.loadLibrary("android_servers");  会去查找libandroid_servers.so 这个库文件;

yujixuan@yujixuan:~/prj/SC20_R06_master_0526/code/frameworks$ grep -rn libandroid_servers ./
./base/services/Android.mk:54:LOCAL_MODULE:= libandroid_servers

通过检索可知,加载该本地库,会调用在 base/services/ 下编译库文件的onload函数;即:

./base/services/core/jni/onload.cpp

extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        ALOGE("GetEnv failed!");
        return result;
    }
    ALOG_ASSERT(env, "Could not retrieve the env!");
    register_android_server_ActivityManagerService(env);
    register_android_server_PowerManagerService(env);
    register_android_server_SerialService(env);
    register_android_server_InputApplicationHandle(env);
    register_android_server_InputWindowHandle(env);
    register_android_server_InputManager(env);
    register_android_server_LightsService(env);
    register_android_server_AlarmManagerService(env);
    register_android_server_UsbDeviceManager(env);
    register_android_server_UsbMidiDevice(env);
    register_android_server_UsbHostManager(env);
    register_android_server_vr_VrManagerService(env);
    register_android_server_VibratorService(env);
    register_android_server_SystemServer(env);
    register_android_server_location_GnssLocationProvider(env);
    register_android_server_location_FlpHardwareProvider(env);
    register_android_server_connectivity_Vpn(env);
    register_android_server_AssetAtlasService(env);
    register_android_server_ConsumerIrService(env);
    register_android_server_BatteryStatsService(env);
    register_android_server_hdmi_HdmiCecController(env);
    register_android_server_tv_TvUinputBridge(env);
    register_android_server_tv_TvInputHal(env);
    register_android_server_PersistentDataBlockService(env);
    register_android_server_Watchdog(env);
    register_android_server_HardwarePropertiesManagerService(env);

    return JNI_VERSION_1_4;
}

由上可知,在JNI_OnLoad 中会调用添加若干个 server,没有发现sensor相关的server;

实则它是被放在了 SystemServer中去了;

代码路径:./base/services/core/jni/com_android_server_SystemServer.cpp

/*
 * JNI registration.
 */
static const JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "startSensorService", "()V", (void*) android_server_SystemServer_startSensorService },
};

int register_android_server_SystemServer(JNIEnv* env)
{
    return jniRegisterNativeMethods(env, "com/android/server/SystemServer",
            gMethods, NELEM(gMethods));
}

可以看到 register_android_server_SystemServer 实际上是注册绑定了 sensor相关的 service启动方法(为啥不直接命名为sensor server,目前还不清楚);

通过method可知,systemServer调用run方法启动 startSensorService,会调用到android_server_SystemServer_startSensorService ; 以下是它的实现:

static void android_server_SystemServer_startSensorService(JNIEnv* /* env */, jobject /* clazz */) {
    char propBuf[PROPERTY_VALUE_MAX];
    property_get("system_init.startsensorservice", propBuf, "1");
    if (strcmp(propBuf, "1") == 0) {
        // Start the sensor service in a new thread
        createThreadEtc(start_sensor_service, nullptr,
                        "StartSensorThread", PRIORITY_FOREGROUND);
    }
}

判断属性 "system_init.startsensorservice" 是否存在,如果存在 创建线程"StartSensorThread"(默认被配置了,还找到哪里配置的)

线程内容如下:

static int start_sensor_service(void* /*unused*/) {
    SensorService::instantiate();
    return 0;
}

该线程调用SensorService中的instantiate方法,看下SensorService的定义:
路径:./frameworks/native/services/sensorservice/SensorService.h

class SensorService :
        public BinderService<SensorService>,
        public BnSensorServer,
        protected Thread
{
    // nested class/struct for internal use
    class SensorEventConnection;

public:
    void cleanupConnection(SensorEventConnection* connection);

    status_t enable(const sp<SensorEventConnection>& connection, int handle,
                    nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags,
                    const String16& opPackageName);

    status_t disable(const sp<SensorEventConnection>& connection, int handle);

    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,
                          const String16& opPackageName);
    status_t flushSensor(const sp<SensorEventConnection>& connection,
                         const String16& opPackageName);
private:
    friend class BinderService<SensorService>;
    // nested class/struct for internal use
    class SensorRecord;
    class SensorEventAckReceiver;
    struct SensorRegistrationInfo
    static char const* getServiceName() ANDROID_API { return "sensorservice"; }
    SensorService() ANDROID_API;
    virtual ~SensorService();

    virtual void onFirstRef();

    // Thread interface
    virtual bool threadLoop();

    // ISensorServer interface
    virtual Vector<Sensor> getSensorList(const String16& opPackageName);
    virtual status_t dump(int fd, const Vector<String16>& args);

    String8 getSensorName(int handle) const;
    bool isVirtualSensor(int handle) const;
    sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const;
    bool isWakeUpSensor(int type) const;
    void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
    const Sensor& registerSensor(SensorInterface* sensor,
                                 bool isDebug = false, bool isVirtual = false);
    const Sensor& registerVirtualSensor(SensorInterface* sensor, bool isDebug = false);
};

删了一些不知道干嘛的方法及属性后,还有如上内容,发现了几个熟悉的方法,在Hal里nativesensormange使用的一些方法,但是并没有instantiate方法;

通过类的定义可以发现SensorService 继承了BinderService;BnSensorServer,Thread这三个class;  instantiate 是被定义在它的父类BinderService中了;

同样它是在本地的service,代码路径:
./frameworks/native/include/binder/BinderService.h

class BinderService
{
public:
    static status_t publish(bool allowIsolated = false) {
        sp<IServiceManager> sm(defaultServiceManager());
        return sm->addService(
                String16(SERVICE::getServiceName()),
                new SERVICE(), allowIsolated);
    }

    static void publishAndJoinThreadPool(bool allowIsolated = false) {
        publish(allowIsolated);
        joinThreadPool();
    }

    static void instantiate() { publish(); }
......
};

publish()中

sp sm(defaultServiceManager());   

可知SensorService::instantiate()在这一过程创建了SensorService并通过addService将自己新创建的SensorService服务添加到Android服务列表里了。

有关:Android系统智能指针的设计思路(轻量级指针、强指针、弱指针)

可知在Android引用计数系统里,当RefBase的子类对象被第一次强引用时自动调用其onFirstRef方法,所以当第一次使用SensorService,onFirstRef方法将被被自动回调。

uml时序图

根据以上过程,绘制了一个uml时序图,如下:


总结过程:

  1. 系统初始化进程加载,启动SystemServer,Zygote中会执行SystemServier main方法,导致其run方法被调用;
  2. 加载本地库文件, System.loadLibrary("android_servers"); 获取本地方法;
  3. 被加载到的JNI库文件导致JNI_Onload函数被调用;调用本地jni文件
  4. 注册本地方法jniRegisterNativeMethods 数组;
  5. 完成Java 到 C++ 函数绑定,使Java能否访问到C库中的函数;
  6. 启动startBootstrapServices();
  7. 最后调用native方法 native void startSensorService();
  8. JNI文件com_android_server_SystemServer.cpp,绑定的函数数组,由java的startSensorService方法绑定到android_server_SystemServer_startSensorService函数;
  9. C++函数中,start_sensor_service被调用;
  10. 调用SensorService的inistantiate函数(继承父类BinderService得到的);
  11. 调用publish
  12. 创建一个Serivces,通过sm->addService 来添加到android中去;
    sm->addService( String16(SERVICE::getServiceName()),  new SERVICE(), allowIsolated);
    其中sm是ServiceManager的引用:sp sm(defaultServiceManager());

  1. onFirstRef()属于其父类RefBase,该函数在强引用sp新增引用计数时,第一次强引用会自动调用此函数; //参考:深入理解android 之sp和wp
  2. 获取一个SensorDevice实例对象,SensorDevice& dev(SensorDevice::getInstance());
  3. 加载HAL库, 查找 hal中对应的 module id 微"sensors"的moudle;
    hw_get_module(SENSORS_HARDWARE_MODULE_ID,  (hw_module_t const**)&mSensorModule);得到mSensorModule;
  4. sensors_open_1:module->methods->open 调用open函数,
    hw_module_t除了设备信息外提供另外一个结构体hw_module_methods_t, 它只有一个open函数,调用设备注册的open回调;让调用者得到基于hw_device_t的设备结构体,hw_device_t包含了hw_module_t; (标红为HAL的三个关键结构体)
    HAL的核心内容是获取hw_device_t,向上转型,获得其中的设备数据结构以及函数接口;
  5. 通过HAL中添加的get_sensors_list获取list;
  6. qcom平台上,HAL中的实际管控是有NativeSensorManager来管控,这里也是返回单例NativeSensorManager对象中的sensorlist,及sensor个数
  7. 根据sensor个数,for循环,执行active
  8. 完成SensorDevice的构造,SensorService得到了 实例对象SensorDevice dev;
  9. 获取sensor list,及sensor num;调用过程与上面相同;
  10. 根据sensor个数,for循环, 注册Sensor到sensorsercvice
  11. 注册的过程是创建对应的HardwareSensor;
  12. SensorService 创建SensorEeventReceiver,SensorEventAckReceiver : public 集成了Thread ; 
  13. 启动mAckReceiver 重写实现的这个线程,SensorEventAckReceiver,调用run方法,启动这个线程:"SensorEventAckReceiver"
  14. SensorService自身也集成了 Thread, 在SensorService中重写threadLoop; 这个地方调用了其run方法,启动线程:"SensorService";
  15. 通过device poll 管理,sensorEvent
     

在app启动加载之前,启动初始化时已经创建了SensorMangager; 通过JNI绑定了 SensorManager的相应native方法,以下是其简要步骤;

  • a: AndroidRuntime::start() ->REG_JNI(register_android_hardware_SensorManager)
    ./base/core/jni/AndroidRuntime.cpp中调用注册了本地C++ 方法,库文件是libandroid_runtime.so;
    详细过程参考:Android系统启动流程
  • b: android_hardware_SensorManager.cpp中完成native C++函数与java方法的绑定,RegisterMethods;
  • c: 上面过程中已经完成了加载,Java代码中SystemSensorManager可以直接调用Sensor相关的native方法;

  • A: 系统服务注册时会创建SensorManager;
    代码路径:./core/java/android/app/SystemServiceRegistry.java
     registerService(Context.SENSOR_SERVICE, SensorManager.class,                                                        new CachedServiceFetcher<SensorManager>() {
              @Override
              public SensorManager createService(ContextImpl ctx) {
                  return new SystemSensorManager(ctx.getOuterContext(),
                    ctx.mMainThread.getHandler().getLooper());
              }});
    参考:android笔记之SystemServiceRegistry  
  • B: 创建一个SystemSensorManger实例;其构造函数会调用本地方法: nativeClassInit();
  • C: 构造函数会调用本地方法: nativeCreate(context.getOpPackageName());
  • D: 调用:nativeGetSensorAtIndex 循环添加sensor;

  1. App中,MainActivity中获取SensorManager;
  2. 通过SensorManager 获取类型为Acc的sensor对象;
  3. 通过SensorManager.registerListener注册SensorEventListener监听器,监听传感器
  4. registerListener调用到registerListenerImpl会创建SensorEventQueue,继承自BaseEventQueue ; 
  5. 通过JNI调用本地方法nativeInitBaseEventQueue;
  6. C++中绑定的nativeInitSensorEventQueue函数被调用;创建event队列createEventQueue
  7. 添加Receiver;
  8. addSensor将sensor添加到queue,addSensorEvent(sensor);;
  9. addSensor使能sensor,通过JNI调用到本地方法:nativeEnableSensor;
  10. 调用enable
posted @ 2021-06-21 20:31  yujixuan  阅读(823)  评论(0编辑  收藏  举报