Android sensor hal 詳解

                                              Android sensor hal 詳解


這裏面有關於halJNI的詳細講解

代碼的路徑是在:ics_ventana/frameworks/base/services/sensorservice/SensorDevice.cpp裏面

Androidhal層有三個重要的結構:

structhw_module_t:

structhw_device_t

structhw_module_methods_t:

structhw_module_t結構如下:


typedefstructhw_module_t{

uint32_ttag;//這個tag必須初始化爲HARDWARE_MODULE_TAG

uint16_tversion_major;//模塊的主版本號

uint16_tversion_minor;//模塊的次版本號

constchar*id;//模塊的一個標識id

constchar*name;//模塊的名字

constchar*author;//實現這個模塊的作者

structhw_module_methods_t*methods;//模塊的方法


/**module'sdso*/

void*dso;


/**padding to 128 bytes, reserved for future use */

uint32_treserved[32-7];


}hw_module_t;

每一個hardwaremodule必須有一個數據結構,並且命名爲HAL_MODULE_INFO_SYM,並且這個結構開始必須以structhw_module_t,下面纔是是這個模塊的具體定義的信息。

就比如我們的sensormodule的定義如下:

structsensors_module_t {

structhw_module_tcommon;//必須放在第一位

*/

int(*get_sensors_list)(structsensors_module_t*module,

structsensor_tconst**list);//下面是具體的自己實現的方法

};

我們具體的sensor的實現如下:

structsensors_module_t HAL_MODULE_INFO_SYM = {//每一個模塊的定義的變量的名字必須是HAL_MODULE_INFO_SYM

common: {

tag:HARDWARE_MODULE_TAG,//這個tag也必須是HARDWARE_MODULE_TAG

version_major: 1,

version_minor: 0,

id:SENSORS_HARDWARE_MODULE_ID,

name:"Ventanasensors module",

author:"廠商名字",

methods:&sensors_module_methods,

dso: NULL,

reserved: {0}

},

get_sensors_list:sensors__get_sensors_list,

};


structhw_device_t

structhw_device_t{

uint32_ttag;//這個devicetag,必須初始化爲HARDWARE_DEVICE_TAG和上面的moduletag是一個意思

uint32_tversion;//這個設備的版本號

structhw_module_t*module;//這個設備所屬於的模塊

uint32_treserved[12];//保留使用的

int(*close)(structhw_device_t*device);//關閉設備的方法


}

每一個設備必須有一個數據結構,這個結構必須以structhw_device_t開始,下面纔是這個設備的公共的方法和屬性

structsensors_poll_device_t {

structhw_device_tcommon;

...

..

}


我們一般是在structhw_module_methods_t結構裏面的open函數裏面來進行對上面的成員進行賦值。

在下面的講解structhw_module_methods_t的時候會敘述

structhw_module_methods_t:


typedefstructhw_module_methods_t{

int(*open)(conststructhw_module_t*module,constchar*id,

structhw_device_t**device);//用這個函數打開一個具體的設備

}

我們的sensor代碼的實現:

staticstruct hw_module_methods_tsensors_module_methods = {

open: open_sensors

};


structsensors_poll_context_t {

structsensors_poll_device_tdevice;//must be first

.....

.....

}


open_sensor的實現如下:


/**Open a new instance of a sensor device using name */

staticintopen_sensors(conststructhw_module_t*module,constchar*id,

structhw_device_t**device)//發現沒有,我們傳遞進來的是structhw_device_t這個結構

{

FUNC_LOG;

intstatus = -EINVAL;

sensors_poll_context_t*dev =newsensors_poll_context_t();//structsensors_poll_context_t的第一個成員的地址和structsensors_poll_device_t以及structhw_device_t的地址是一樣的,就是因爲這個原因,下面纔可以進行取值,進行賦值



memset(&dev->device,0,sizeof(sensors_poll_device_t));


dev->device.common.tag= HARDWARE_DEVICE_TAG;

dev->device.common.version = 0;

dev->device.common.module =const_cast<hw_module_t*>(module);

dev->device.common.close = poll__close;

dev->device.activate = poll__activate;

dev->device.setDelay = poll__setDelay;

dev->device.poll = poll__poll;


*device =&dev->device.common;//經賦值好的device裏面的成員,賦值給structsensors_poll_device_t裏面的每一個成員

status = 0;


returnstatus;

}

上面是Hal層的一些基礎知識,我們上層是如何通過hal層一直訪問到底層提供的結構呢?

我們是通過hw_get_module這個函數進行獲得對應的hal層的module,獲得這個module之後,再通過module裏面的interface操作底層驅動。

代碼的路徑是在:frameworks/base/services/sensorservice/SensorDevice.cpp

SensorDevice::SensorDevice()

:mSensorDevice(0),

mSensorModule(0)

{

status_terr = hw_get_module(SENSORS_HARDWARE_MODULE_ID,

(hw_module_tconst**)&mSensorModule);//獲得這個id的模塊,


LOGE_IF(err,"couldn'tload %s module (%s)",

SENSORS_HARDWARE_MODULE_ID,strerror(-err));


if(mSensorModule){

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


LOGE_IF(err,"couldn'topen device for module %s (%s)",

SENSORS_HARDWARE_MODULE_ID,strerror(-err));


if(mSensorDevice){

sensor_tconst*list;

ssize_t count =mSensorModule->get_sensors_list(mSensorModule,&list);

mActivationCount.setCapacity(count);

Infomodel;

for(size_t i=0 ; i<size_t(count) ; i++) {

mActivationCount.add(list[i].handle,model);

mSensorDevice->activate(mSensorDevice,list[i].handle, 0);

}

}

}

}

上面只要知道hw_get_module函數通過id的值獲得模塊。至於hw_get_module函數的實現我會在另外一個文檔裏面進行講解。

看下上面的sensors_open函數的實現:

staticinlineintsensors_open(conststructhw_module_t* module,

structsensors_poll_device_t**device) {

returnmodule->methods->open(module,

SENSORS_HARDWARE_POLL,(structhw_device_t**)device);

}

覺得有個參考的網站:http://blog.csdn.net/mr_raptor/article/details/8074549

http://www.cnblogs.com/innost/archive/2011/11/08/2241653.html




發佈了18 篇原創文章 · 獲贊 11 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章