android4.2.2---1. hal層與framework層的呼叫
阿新 • • 發佈:2018-12-30
1.HAL層
首先在hal層定義結構體sensors_module_t
#define SENSORS_HARDWARE_MODULE_ID "sensors"
2. framework層
framework/native/services/sensorservice/SensorDevice.cpp
sensors_poll_device_t
--> hw_device_t
--> 加一些函式指標
在hal層的open中:
*device =&dev->device.common;
在framework層的sensor_open中
module->methods->open(..., (struct hw_device_t**)device);
為什麼這兒是將sensors_poll_device_t轉為了hw_device_t,而不是直接傳sensor_poll_device_t呢?
答: 我感覺這兒是為了遮蔽,即讓hal層遮蔽fw層的無關程式碼.
當然也有可能是為了介面的統一.
首先在hal層定義結構體sensors_module_t
#define SENSORS_HARDWARE_MODULE_ID "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, //每一個hal層都有一個唯一的id
- name: "MEMSIC Sensors Module"
- ,
- author: "MEMSIC Inc."
- ,
- methods:
- &__module_methods,
- dso: NULL,
- reserved: {0},
- },
- get_sensors_list:
- __get_sensors_list
- };
- static struct hw_module_methods_t __module_methods = {
- open:
- __module_methods_open
- };
- static int __module_methods_open(
- const char *id, struct hw_device_t **device)
- {
- struct sensors_poll_context_t *dev;
- dev = (struct sensors_poll_context_t *)malloc(sizeof(*dev));
- //初始化dev
- dev->device.common.tag = HARDWARE_DEVICE_TAG;
- dev->device.common.
- dev->device.common.module = (struct hw_module_t *)module;
- dev->device.common.close = __common_close;
- dev->device.activate = __control_activate;
- dev->device.setDelay = __control_set_delay;
- dev->device.poll = __data_poll;
- //賦值給device
- //這兒有個問題: common是一個 struct hw_device_t(小) common;
- //一會在framework中呼叫的需要的是 sensor_poll_t(大)
- *device = &dev->device.common;
- return res;
- }
2. framework層
framework/native/services/sensorservice/SensorDevice.cpp
- SensorDevice::SensorDevice()
- : mSensorDevice(0),
- mSensorModule(0)
- {
- //fw層會根據唯一的id=SENSORS_HARDWARE_MODULE_ID來找到hal層的so,然後呼叫dlopen載入到記憶體中
- //同時定義為 struct sensors_module_t* 的mSensorModule會指向找到的so中的sensor_module_t結構體
- //這樣就可以利用hal層定義的函數了
- hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&mSensorModule);
- //struct sensors_poll_device_t* mSensorDevice;
- sensors_open(&mSensorModule->common, &mSensorDevice);
- mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
- }
- static inline int sensors_open(const struct hw_module_t* module,
- struct sensors_poll_device_t** device) {
- return module->methods->open(module,
- SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
- }
sensors_poll_device_t
--> hw_device_t
--> 加一些函式指標
在hal層的open中:
*device =&dev->device.common;
在framework層的sensor_open中
module->methods->open(..., (struct hw_device_t**)device);
為什麼這兒是將sensors_poll_device_t轉為了hw_device_t,而不是直接傳sensor_poll_device_t呢?
答: 我感覺這兒是為了遮蔽,即讓hal層遮蔽fw層的無關程式碼.
當然也有可能是為了介面的統一.