赞
踩
分析的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,先看一下构造函数,
- Sensors::Sensors()
- : mInitCheck(NO_INIT),
- mSensorModule(nullptr),
- mSensorDevice(nullptr) {
- status_t err = OK;
- if (UseMultiHal()) {
- //走这边,获取vendor-hal的module的方法
- mSensorModule = ::get_multi_hal_module_info();
- } else {
- err = hw_get_module(
- SENSORS_HARDWARE_MODULE_ID,
- (hw_module_t const **)&mSensorModule);
- }
- if (mSensorModule == NULL) {
- err = UNKNOWN_ERROR;
- }
-
- if (err != OK) {
- LOG(ERROR) << "Couldn't load "
- << SENSORS_HARDWARE_MODULE_ID
- << " module ("
- << strerror(-err)
- << ")";
-
- mInitCheck = err;
- return;
- }
- //这边是调用了multihal.cpp的open_sensors()方法,对mSensorDevice进行初始化
- err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
-
- if (err != OK) {
- LOG(ERROR) << "Couldn't open device for module "
- << SENSORS_HARDWARE_MODULE_ID
- << " ("
- << strerror(-err)
- << ")";
-
- mInitCheck = err;
- return;
- }
-
- //……
- mInitCheck = OK;
- }
看mSensorDevice,
sensors_poll_device_1_t *mSensorDevice;
err = sensors_open_1(&mSensorModule->common,&mSensorDevice);
在构造函数里边通过调用multihal.cpp的open_sensors()方法被初始化的,
- static int open_sensors(const struct hw_module_t* hw_module, const char* name,
- struct hw_device_t** hw_device_out) {
- ALOGV("open_sensors begin...");
-
- lazy_init_modules();
-
- // Create proxy device, to return later.
- sensors_poll_context_t *dev = new sensors_poll_context_t();
- memset(dev, 0, sizeof(sensors_poll_device_1_t));
- dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
- dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
- dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
- dev->proxy_device.common.close = device__close;
- dev->proxy_device.activate = device__activate;
- dev->proxy_device.setDelay = device__setDelay;
- dev->proxy_device.poll = device__poll;
- dev->proxy_device.batch = device__batch;
- dev->proxy_device.flush = device__flush;
- dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
- dev->proxy_device.register_direct_channel = device__register_direct_channel;
- dev->proxy_device.config_direct_report = device__config_direct_report;
-
- dev->nextReadIndex = 0;
-
- // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
- for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
- it != sub_hw_modules->end(); it++) {
- sensors_module_t *sensors_module = (sensors_module_t*) *it;
- struct hw_device_t* sub_hw_device;
- int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
- if (!sub_open_result) {
- if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) {
- ALOGE("SENSORS_DEVICE_API_VERSION_1_3 or newer is required for all sensor HALs");
- ALOGE("This HAL reports non-compliant API level : %s",
- apiNumToStr(sub_hw_device->version));
- ALOGE("Sensors belonging to this HAL will get ignored !");
- }
- dev->addSubHwDevice(sub_hw_device);
- }
- }
-
- // Prepare the output param and return
- *hw_device_out = &dev->proxy_device.common;
- ALOGV("...open_sensors end");
- return 0;
- }
这边让mSensorDevice指向dev->proxy_device.common,之后通过类型转换,调用mSensorDevice->func(),就等价于调用dev->proxy_device.func(),
4,以SensorService调用activate()为例来分析一下
- Return<Result> Sensors::activate(
- int32_t sensor_handle, bool enabled) {
- return ResultFromStatus(
- mSensorDevice->activate(
- reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
- sensor_handle,
- enabled));
- }
调用mSensorDevice->activate(),
调用mSensorDevice->active()等价于调用dev->proxy_device.activate= device__activate;
- static int device__activate(struct sensors_poll_device_t *dev, int handle,
- int enabled) {
- sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
- return ctx->activate(handle, enabled);
- }
看sensors_poll_context_t::active(),
- int sensors_poll_context_t::activate(int handle, int enabled) {
- int retval = -EINVAL;
- ALOGV("activate");
- int local_handle = get_local_handle(handle);
- sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
- if (halIsCompliant(this, handle) && local_handle >= 0 && v0) {
- retval = v0->activate(v0, local_handle, enabled);
- } else {
- ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !",
- enabled, handle);
- }
- ALOGV("retval %d", retval);
- return retval;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。