在前面的文章中,我们分析到Sensor HAL层的启动以及数据获取过程,我们提到,SensorContext以及Sensor在初始化的时候会通过sensor1_open创建一个客户端的socket,这个socket会不停的从服务端读取数据,并通过注册的回调来处理得到的数据,这个服务端的socket就是我们今天分析的主角,SensorDaemon守护进程,那么SensorDaemon在Sensor架构中到底起着怎样的作用呢? 我们拭目以待。
戳下面复习:
Android Sensor HAL层初始化流程
Android Sensor HAL层数据获取,基于Qualcomm平台
SensorDaemon启动
SensorDaemon是在init阶段通过device/qcom/common/rootdir/etc/init.qcom.rc中配置启动的。
service sensors /system/bin/sensors.qcom
class late_start
user root
group root
disabled
因此我们直接跟进其main方法,相关的代码路径在vendor/qcom/proprietary/sensors/dsps/sensordaemon中,而main方法定义在文件sns_main.c中。
/*!
@brief "Main" function for the sensor daemon process. Handles starting
the process, initializing sockets and sensor1 API, and blocking for
incoming data on sockets.
@param[i] argc: Count of arguments on the command line.
@param[i] argv: Array of strings contaning command line arguments.
@return
0 - no error
*/
/*=========================================================================*/
int
main( int argc, char *argv[] )
{
sensor1_error_e sns_err;
sns_main_data_s *srv_data;
bool error = FALSE;
pthread_mutexattr_t attr;
if( 'd' == getopt( argc, argv, "d" ) ) {
/* Put ourselves in a background daemon process */
sns_main_daemonize();
} else {
printf("Use option \"-d\" to run as background process.\n");
printf("Running in foreground...\n");
}
/* Start up the sensor library */
sns_err = sensor1_init();
if( SENSOR1_SUCCESS != sns_err ) {
SNS_PRINTF_STRING_ERROR_1( SNS_DBG_MOD_APPS_MAIN,
"Exiting! sensor1_init failed with %d",
sns_err );
exit(6);
}
/* Initialize mutex */
pthread_mutexattr_init( &attr );
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
pthread_mutex_init( &sns_main_data_mutex, &attr );
/* Initialize server */
srv_data = sns_main_setup();
if( NULL == srv_data ) {
SNS_PRINTF_STRING_ERROR_0( SNS_DBG_MOD_APPS_MAIN,
"Exiting! Can't setup server socket" );
exit(4);
}
/* Check to ensure sensors are enabled */
sns_main_check_enabled( srv_data );
/* Drop all root privs */
error = sns_main_drop_root();
if( error != false ) {
SNS_PRINTF_STRING_ERROR_0( SNS_DBG_MOD_APPS_MAIN,
"Error dropping root privs. Exiting" );
exit(5);
}
initialized = true;
sns_monitor_adsp_restart();
utime(SENSOR_CTL_SOCKET, NULL);
while( !error ) {
error = sns_main_sock_handle( srv_data );
}
SNS_PRINTF_STRING_ERROR_1( SNS_DBG_MOD_APPS_MAIN, "Exiting! Err %d",
error );
return 0;
}
这里首先也调用了sensor1_init来初始化sensor,这里的sensor1_init方法并不是之前libsensor1中的方法了,而是SensorDaemon自己定义的,他在sns_acm.c中,这个文件是作为我们的application client manager来运作的,负责和后面会讲到的Sensor Message Router(SMR)通信以传递客户端请求到SensorManager(SMGR),并回传response给到对应的客户端,init的内容最后会分析,这里先跳过。
server的初始化时在sns_main_setup方法中完成的,我们接下来分析这个方法。
static sns_main_data_s*
sns_main_setup( void )
{
sns_main_data_s *srv_data;
int error;
srv_data = malloc( sizeof(*srv_data) );
if( NULL == srv_data ) {
SNS_PRINTF_STRING_ERROR_0( SNS_DBG_MOD_APPS_MAIN, "malloc error" );
return NULL;
}
memset( srv_data, 0, sizeof(*srv_data) );
if( 0 != sns_main_setup_srv_sock( srv_data ) ) {
free(srv_data);
return NULL;
}
srv_data->enabled = true;
srv_data->inotify_fd = inotify_init();
if( srv_data->inotify_fd == -1 ) {
SNS_PRINTF_STRING_ERROR_1( SNS_DBG_MOD_APPS_MAIN,