1,通常所说的HAL 层的理解误区
以前一直认为工作中一直提到的HAL 层是Android 系统的hal 层,直到今天遇到一个问题去追代码后才发现,原来的理解错了。正确的理解应该是:我们一般提到的hal 层是说的安卓五层结构中的HAL( 物理抽象层).架构图如下

代码路径:
hardware/chip_name/module/
针对于MTK SensorHub架构HAL 层是指的SCP(Sensor Controller Processor)协处理器单独的物理抽象层。
架构图如下:

代码路径:
vendor/mediatek/proprietary/hardware/sensor
2, APP 如何和SCP 测物理sensor 一一对应的
APP 获取到物理sensor 的信息是通过读取HAL 层的vendor/mediatek/proprietary/hardwaare/sensor/sensors-1.0/SensorList.cpp 中定义的器件信息获得的。
#ifdef CUSTOM_KERNEL_ACCELEROMETER
memset(&sensor, 0, sizeof(struct sensor_t));
sensor.name = ACCELEROMETER;
sensor.vendor = ACCELEROMETER_VENDER;
sensor.version = ACCELEROMETER_VERSION;
sensor.handle = ID_ACCELEROMETER + ID_OFFSET;
sensor.type = SENSOR_TYPE_ACCELEROMETER;
sensor.maxRange = ACCELEROMETER_RANGE;
sensor.resolution = ACCELEROMETER_RESOLUTION;
sensor.power = ACCELEROMETER_POWER;
sensor.minDelay = ACCELEROMETER_MINDELAY;
sensor.fifoReservedEventCount = ACCELEROMETER_FIFO_RESERVE_COUNT;
sensor.fifoMaxEventCount = ACCELEROMETER_FIFO_MAX_COUNT;
sensor.stringType = SENSOR_STRING_TYPE_ACCELEROMETER;
sensor.maxDelay = ACCELEROMETER_MAXDELAY;
sensor.flags = ACCELEROMETER_FLAGS;
mSensorList.push_back(sensor);
#endif
#ifdef CUSTOM_KERNEL_MAGNETOMETER
memset(&sensor, 0, sizeof(struct sensor_t));
sensor.name = MAGNETOMETER;
sensor.vendor = MAGNETOMETER_VENDER;
sensor.version = MAGNETOMETER_VERSION;
sensor.handle = ID_MAGNETIC + ID_OFFSET;
sensor.type = SENSOR_TYPE_MAGNETIC_FIELD;
sensor.maxRange = MAGNETOMETER_RANGE;
sensor.resolution = MAGNETOMETER_RESOLUTION;
sensor.power = MAGNETOMETER_POWER;
sensor.minDelay = MAGNETOMETER_MINDELAY;
sensor.fifoReservedEventCount = MAGNETOMETER_FIFO_RESERVE_COUNT;
sensor.fifoMaxEventCount = MAGNETOMETER_FIFO_MAX_COUNT;
sensor.stringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD;
sensor.maxDelay = MAGNETOMETER_MAXDELAY;
sensor.flags = MAGNETOMETER_FLAGS;
mSensorList.push_back(sensor);
#endif
系统启动后,启动sensorservice,然后通过系统调用宏 HAL_MODULE_INFO_SYM作为系统访问HAL层接口的入口,系统启动sensorservice 会调用sensors_get_sensors_list函数获取sensorList .cpp定义物理sensor 和 虚拟sensor配置,sensors_module_methods中调用了init_sensors 然后创建了sensors_poll_context_t 实例dev 和 SensorManager的实例 mSensorManager 方法,这些方法的定义是在SensorManager.cpp。也就是说APP 打开/关闭/一个sensor后会调用SensorManager.cpp的函数,然后会打开物理sensor.
vendor/mediatek/proprietary/hardware/sensor/sensors.cpp
static int poll__close(struct hw_device_t *dev) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
if (ctx)
delete ctx;
mSensorManager->removeSensorConnection(mNativeConnection); //调用mSensorManager 方法
delete mSensorManager;
delete mVendorInterface;
delete mMtkInterface;
delete mSensorCalibration;
delete mSensorList;
delete mDirectChannelManager;
return 0;
}
static int poll__activate(struct sensors_poll_device_t * /*dev*/,
int handle, int enabled) {
return mSensorManager->activate(mNativeConnection, handle - ID_OFFSET, enabled);
}
static int poll__setDelay(struct sensors_poll_device_t * /*dev*/,
int handle, int64_t ns) {
return mSensorManager->batch(mNativeConnection, handle - ID_OFFSET, ns, 0);
}
static int poll__poll(struct sensors_poll_device_t * /*dev*/,
sensors_event_t* data, int count) {
return mSensorManager->pollEvent(data, count);
}
static int poll__batch(struct sensors_poll_device_1 * /*dev*/,
int handle, int /*flags*/, int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs) {
return mSensorManager->batch(mNativeConnection,
handle - ID_OFFSET, samplingPeriodNs, maxBatchReportLatencyNs);
}
static int poll__flush(struct sensors_poll_device_1 * /*dev*/,
int handle) {
return mSensorManager->flush(mNativeConnection, handle - ID_OFFSET);
}
static int init_sensors(hw_module_t const* module, hw_device_t** device)
{
sensors_poll_context_t *dev;
size_t count = 0;
const struct sensor_t *list = NULL;
/* must initialize sensorlist first for hwgyrosupport */
mSensorList = SensorList::getInstance();
count = mSensorList->getSensorList(&list);
dev = sensors_poll_context_t::getInstance(); //创建一个dev 实例
memset(&dev->device, 0, sizeof(sensors_poll_device_1)); //初始化这个实例
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = SENSORS_DEVICE_API_VERSION_1_4;
dev->device.common.module = const_cast<hw_module_t*>(module);
dev->device.common.close = poll__close; ////调用mSensorManager 中的方法
dev->device.activate = poll__activate;
dev->device.setDelay = poll__setDelay;
dev->device.poll = poll__poll;
dev->device.batch = poll__batch;
dev->device.flush = poll__flush;
dev->device.inject_sensor_data = poll__injectSensorData;
#ifdef SUPPORT_DIRECT_CHANNEL
dev->device.register_direct_channel = poll__registerDirectChannel;
dev->device.config_direct_report = poll__configDirectReport;
#endif
*device = &dev->device.common;
mSensorManager = SensorManager::getInstance(); //创建一个mSensorManager 的实例
mSensorManager->addSensorsList(list, count);
mNativeConnection = mSensorManager->createSensorConnection(numFds);
mSensorManager->setNativeConnection(mNativeConnection);
mSensorManager->setSensorContext(dev);
mVendorInterface = VendorInterface::getInstance();
mMtkInterface = MtkInterface::getInstance();
mSensorCalibration = SensorCalibration::getInstance();
mDirectChannelManager = DirectChannelManager::getInstance();
return 0;
}
static int open_sensors(const struct hw_module_t* module, const char* name,
struct hw_device_t** device)
{
ALOGD("%s: name=%s!\n", __func__, name);
return init_sensors(module, device);
}
static int sensors__get_sensors_list(struct sensors_module_t* /*module*/,
struct sensor_t const** list) {
size_t count = mSensorList->getSensorList(list);
ALOGD("%s count=%zu!\n", __func__, count);
return count;
}
static int set_operation_mode(unsigned int mode) {
ALOGV("%s: mode=%u!", __func__, mode);
return -EINVAL;
}
static struct hw_module_methods_t sensors_module_methods = {
.open = open_sensors
};
struct sensors_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = SENSORS_HARDWARE_MODULE_ID,
.name = "MTK SENSORS Module",
.author = "Mediatek",
.methods = &sensors_module_methods,
},
.get_sensors_list = sensors__get_sensors_list,
.set_operation_mode = set_operation_mode,
};
2743

被折叠的 条评论
为什么被折叠?



