当前位置:   article > 正文

Android Sensor (3) -- 服务端SensorService启动_isensors::getservice

isensors::getservice

 

目录

1.1 SysterServer启动sensorservice

1.2 android_servers.so 启动sersorservice

1.3 SensorService instantiate

1.4 SensorService:: onFirstRef


 

1.1 SysterServer启动sensorservice

 

sensorservice通过SystemServer启动,运行在SystemServer进程,是一个binder服务,通过addService(sensorservice)到ServiceManager

 

  1. @SystemServer.java
  2. startBootstrapServices();
  3. startCoreServices();
  4. startOtherServices();
  5. private void startBootstrapServices() {
  6. ...
  7. mSensorServiceStart = SystemServerInitThreadPool.get().submit(() -> {
  8. startSensorService(); //startSensorService
  9. }, START_SENSOR_SERVICE);
  10. }
  11. private static native void startSensorService(); //libandroid_servers.so

1.2 android_servers.so 启动sersorservice

startSensorService是使用的libandroid_servers.so,这个库是在SystemServer.run()里面加载

  1. @frameworks/base/services/core/jni/com_android_server_SystemServer.cpp
  2. static const JNINativeMethod gMethods[] = {
  3. /* name, signature, funcPtr */
  4. { "startSensorService", "()V", (void*) android_server_SystemServer_startSensorService },
  5. { "startHidlServices", "()V", (void*) android_server_SystemServer_startHidlServices },
  6. };
  7. static void android_server_SystemServer_startSensorService(JNIEnv* /* env */, jobject /* clazz */) {
  8. char propBuf[PROPERTY_VALUE_MAX];
  9. property_get("system_init.startsensorservice", propBuf, "1");
  10. if (strcmp(propBuf, "1") == 0) {
  11. SensorService::instantiate();
  12. }
  13. }

1.3 SensorService instantiate

SensorService继承BinderService,执行BinderService::instantiate,也就是addService到ServiceManager

  1. @SensorService.h
  2. class SensorService :
  3. public BinderService<SensorService>,
  4. public BnSensorServer,
  5. protected Thread
  6. {
  7. @frameworks/native/include/binder/BinderService.h
  8. class BinderService
  9. {
  10. public:
  11. static status_t publish(bool allowIsolated = false) {
  12. sp<IServiceManager> sm(defaultServiceManager());
  13. return sm->addService(
  14. String16(SERVICE::getServiceName()),
  15. new SERVICE(), allowIsolated);
  16. }
  17. static void publishAndJoinThreadPool(bool allowIsolated = false) {
  18. publish(allowIsolated);
  19. joinThreadPool();
  20. }
  21. static void instantiate() { publish(); }
  22. static status_t shutdown() { return NO_ERROR; }


//代码里SERVICE为sensorservice,也可以通过service list查看

  1. @/frameworks/native/services/sensorservice/SensorService.h
  2. static char const* getServiceName() ANDROID_API { return "sensorservice"; }

SensorService最终会继承到RefBase,就会执行自己的onFirstRef方法

 

1.4 SensorService:: onFirstRef

  1. //SensorService集成Binder也是一个Thread,因此会执行 onFirstRef初始化,和执行threadLoop处理SensorEvent
  2. @frameworks/native/services/sensorservice/SensorService.cpp
  3. void SensorService::onFirstRef() {
  4. SensorDevice& dev(SensorDevice::getInstance()); //1 获取SensorDevice对象
  5. sHmacGlobalKeyIsValid = initializeHmacKey();
  6. if (dev.initCheck() == NO_ERROR) {
  7. sensor_t const* list;
  8. ssize_t count = dev.getSensorList(&list); //2 获取芯片商在Hal层初始化好的SensorList,并返回sensor的数目
  9. if (count > 0) {
  10. ssize_t orientationIndex = -1;
  11. for (ssize_t i=0 ; i<count ; i++) {
  12. bool useThisSensor=true;
  13. ...
  14. if (useThisSensor) {
  15. registerSensor( new HardwareSensor(list[i]) ); //3 sensor注册
  16. }
  17. }
  18. SensorFusion::getInstance();
  19. ...
  20. if (hasAccel && hasGyro) {
  21. bool needGravitySensor = (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) != 0;
  22. registerSensor(new GravitySensor(list, count), !needGravitySensor, true);
  23. bool needGameRotationVector =
  24. (virtualSensorsNeeds & (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR)) != 0;
  25. registerSensor(new GameRotationVectorSensor(), !needGameRotationVector, true);
  26. }
  27. ...
  28. mAckReceiver = new SensorEventAckReceiver(this); //4 启动服务
  29. mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
  30. run("SensorService", PRIORITY_URGENT_DISPLAY);
  31. }
  32. }
  33. }

new 了几个对象后,调用 SensorEventAckReceiver:run()以及 SensorService:run(),进入它们各自实现其父类 Thread 的

threadLoop 方法里边,先看 SensorEventAckReceiver:threadLoop(),

  1. bool SensorService::threadLoop() {
  2. SensorDevice& device(SensorDevice::getInstance());
  3. const int halVersion = device.getHalDeviceVersion();
  4. do {
  5. ssize_t count = device.poll(mSensorEventBuffer, numEventMax); //device.poll
  6. SortedVector< sp<SensorEventConnection> > activeConnections;
  7. populateActiveConnections(&activeConnections);
  8. // handle virtual sensors
  9. if (count && vcount) {
  10. sensors_event_t const * const event = mSensorEventBuffer;
  11. ...
  12. }
  13. //发送事件到客户端 android_hardware_SensorManager.cpp Receiver
  14. // Send our events to clients. Check the state of wake lock for each client and release the
  15. // lock if none of the clients need it.
  16. bool needsWakeLock = false;
  17. size_t numConnections = activeConnections.size();
  18. for (size_t i=0 ; i < numConnections; ++i) {
  19. if (activeConnections[i] != 0) {
  20. activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
  21. mMapFlushEventsToConnections);
  22. needsWakeLock |= activeConnections[i]->needsWakeLock();
  23. // If the connection has one-shot sensors, it may be cleaned up after first trigger.
  24. // Early check for one-shot sensors.
  25. if (activeConnections[i]->hasOneShotSensors()) {
  26. cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer,
  27. count);
  28. }
  29. }
  30. }
  31. } while (!Thread::exitPending());
  32. ...
  33. }

  1. //Singleton单例模式
  2. class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
  3. //mSensors = Iservice.getservice mSensors就是ISensors
  4. SensorDevice::SensorDevice() : mHidlTransportErrors(20) {
  5. ...
  6. }
  7. #include "android/hardware/sensors/1.0/ISensors.h"
  8. @hardware/interfaces/sensors/1.0/ISensors.hal
  9. activate(int32_t sensorHandle, bool enabled) generates (Result result);
  10. @android/hardware/interfaces/sensors/1.0/default/Sensors.cpp
  11. Return<Result> Sensors::activate(
  12. int32_t sensor_handle, bool enabled) {
  13. return ResultFromStatus(
  14. mSensorDevice->activate(
  15. reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
  16. sensor_handle,
  17. enabled));
  18. }
  19. Sensors::Sensors()
  20. status_t err = OK;
  21. if (UseMultiHal()) {
  22. mSensorModule = ::get_multi_hal_module_info();
  23. } else {
  24. err = hw_get_module( //1
  25. SENSORS_HARDWARE_MODULE_ID,
  26. (hw_module_t const **)&mSensorModule);
  27. }
  28. ...
  29. err = sensors_open_1(&mSensorModule->common, &mSensorDevice); //调用sensor.h的sensors_open_1方法打开设备
  30. }

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/779006
推荐阅读
相关标签
  

闽ICP备14008679号