当前位置:   article > 正文

Android的 google hal层 sensor分析_android.hardware.sensors@1.0-service.rc

android.hardware.sensors@1.0-service.rc

分析的Android版本为:Android 8.0,Android 8.1

1.,google-hal层的sensor作为一个server在运行,通过ps |grep sensor可以看到如下进程名字,


通过hidl机制与framework的SensorService(的SensorDevice)通信,

源码位于android/hardware/interfaces/sensors/1.0/default/

LOCAL_MODULE :=android.hardware.sensors@1.0-service

LOCAL_INIT_RC := android.hardware.sensors@1.0-service.rc

include $(BUILD_EXECUTABLE)

编译成可执行程序放在/vendor/bin/hw里边,

是在android.hardware.sensors@1.0-service.rc里边通过service方式启动的,

service sensors-hal-1-0 /vendor/bin/hw/android.hardware.sensors@1.0-service

   class hal

   user system

   group system wakelock input

capabilities BLOCK_SUSPENDSYS_NICE

2,主要的类为Sensors类,它继承并实现了android::hardware::sensors::V1_0::ISensors里边声明的那些SensorService可以通过hidl调用的方法,如activate(),poll(),flush(),getSensorsLis()等,

 

3,先看一下构造函数,

  1. Sensors::Sensors()
  2. : mInitCheck(NO_INIT),
  3. mSensorModule(nullptr),
  4. mSensorDevice(nullptr) {
  5. status_t err = OK;
  6. if (UseMultiHal()) {
  7. //走这边,获取vendor-hal的module的方法
  8. mSensorModule = ::get_multi_hal_module_info();
  9. } else {
  10. err = hw_get_module(
  11. SENSORS_HARDWARE_MODULE_ID,
  12. (hw_module_t const **)&mSensorModule);
  13. }
  14. if (mSensorModule == NULL) {
  15. err = UNKNOWN_ERROR;
  16. }
  17. if (err != OK) {
  18. LOG(ERROR) << "Couldn't load "
  19. << SENSORS_HARDWARE_MODULE_ID
  20. << " module ("
  21. << strerror(-err)
  22. << ")";
  23. mInitCheck = err;
  24. return;
  25. }
  26. //这边是调用了multihal.cpp的open_sensors()方法,对mSensorDevice进行初始化
  27. err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
  28. if (err != OK) {
  29. LOG(ERROR) << "Couldn't open device for module "
  30. << SENSORS_HARDWARE_MODULE_ID
  31. << " ("
  32. << strerror(-err)
  33. << ")";
  34. mInitCheck = err;
  35. return;
  36. }
  37. //……
  38. mInitCheck = OK;
  39. }

mSensorDevice

sensors_poll_device_1_t *mSensorDevice;

err = sensors_open_1(&mSensorModule->common,&mSensorDevice);

在构造函数里边通过调用multihal.cpp的open_sensors()方法被初始化的,




  1. static int open_sensors(const struct hw_module_t* hw_module, const char* name,
  2. struct hw_device_t** hw_device_out) {
  3. ALOGV("open_sensors begin...");
  4. lazy_init_modules();
  5. // Create proxy device, to return later.
  6. sensors_poll_context_t *dev = new sensors_poll_context_t();
  7. memset(dev, 0, sizeof(sensors_poll_device_1_t));
  8. dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
  9. dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
  10. dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
  11. dev->proxy_device.common.close = device__close;
  12. dev->proxy_device.activate = device__activate;
  13. dev->proxy_device.setDelay = device__setDelay;
  14. dev->proxy_device.poll = device__poll;
  15. dev->proxy_device.batch = device__batch;
  16. dev->proxy_device.flush = device__flush;
  17. dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
  18. dev->proxy_device.register_direct_channel = device__register_direct_channel;
  19. dev->proxy_device.config_direct_report = device__config_direct_report;
  20. dev->nextReadIndex = 0;
  21. // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
  22. for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
  23. it != sub_hw_modules->end(); it++) {
  24. sensors_module_t *sensors_module = (sensors_module_t*) *it;
  25. struct hw_device_t* sub_hw_device;
  26. int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
  27. if (!sub_open_result) {
  28. if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
  29. ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
  30. ALOGE("This HAL reports non-compliant API level : %s",
  31. apiNumToStr(sub_hw_device->version));
  32. ALOGE("Sensors belonging to this HAL will get ignored !");
  33. }
  34. dev->addSubHwDevice(sub_hw_device);
  35. }
  36. }
  37. // Prepare the output param and return
  38. *hw_device_out = &dev->proxy_device.common;
  39. ALOGV("...open_sensors end");
  40. return 0;
  41. }

这边让mSensorDevice指向dev->proxy_device.common,之后通过类型转换,调用mSensorDevice->func(),就等价于调用dev->proxy_device.func(),

 

4,以SensorService调用activate()为例来分析一下


  1. Return<Result> Sensors::activate(
  2. int32_t sensor_handle, bool enabled) {
  3. return ResultFromStatus(
  4. mSensorDevice->activate(
  5. reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
  6. sensor_handle,
  7. enabled));
  8. }

调用mSensorDevice->activate(),

调用mSensorDevice->active()等价于调用dev->proxy_device.activate= device__activate;

  1. static int device__activate(struct sensors_poll_device_t *dev, int handle,
  2. int enabled) {
  3. sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
  4. return ctx->activate(handle, enabled);
  5. }

看sensors_poll_context_t::active(),

  1. int sensors_poll_context_t::activate(int handle, int enabled) {
  2. int retval = -EINVAL;
  3. ALOGV("activate");
  4. int local_handle = get_local_handle(handle);
  5. sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
  6. if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
  7. retval = v0->activate(v0, local_handle, enabled);
  8. } else {
  9. ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
  10. enabled, handle);
  11. }
  12. ALOGV("retval %d", retval);
  13. return retval;
  14. }






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

闽ICP备14008679号