Android Senor Framework (三)SensorService启动

2024-04-18 10:58

本文主要是介绍Android Senor Framework (三)SensorService启动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SystemServer 启动SensorService

Zygote启动之后,调用SystemServer的main方法(调用run方法)启动系统服务;

代码路径:

./frameworks/base/services/java/com/android/server/SystemServer.java

SystemServer类中提供的run 方法中,在启动service之前,会加载本地动态库System.loadLibrary(“android_servers”)初始化本地 Native service,过程如下:

/*** The main entry point from zygote.*/
public static void main(String[] args) {new SystemServer().run();
}
public final class SystemServer {private static final String TAG = "SystemServer";private void run() {// Initialize native services.System.loadLibrary("android_servers");// Start services.try {Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");startBootstrapServices();startCoreServices();startOtherServices();}
...
}

在 startBootstrapServices(); 的过程的最后 会调用startSensorService(); 如下:

    private void startBootstrapServices() {
......// The sensor service needs access to package manager service, app ops// service, and permissions service, therefore we start it after them.startSensorService();}

在SystemServer类定义中,定义了本地方法startSensorService 如下:

public final class SystemServer {                                                                                                                                                                                   private static final String TAG = "SystemServer";//......private static native void startSensorService();//......
}

当SystemServer调用run 方法,会通过JNI调用到本地方法;

JNI 访问native 方法

通过对JNI的了解 System.loadLibrary(“android_servers”); 会去查找libandroid_servers.so 这个库文件;

yujixuan@yujixuan:~/prj/SC20_R06_master_0526/code/frameworks$ grep -rn libandroid_servers ./
./base/services/Android.mk:54:LOCAL_MODULE:= libandroid_servers

通过检索可知,加载该本地库,会调用在 base/services/ 下编译库文件的onload函数;即:

代码路径: ./base/services/core/jni/onload.cpp
extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{JNIEnv* env = NULL;jint result = -1;if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {ALOGE("GetEnv failed!");return result;}ALOG_ASSERT(env, "Could not retrieve the env!");register_android_server_ActivityManagerService(env);register_android_server_PowerManagerService(env);register_android_server_SerialService(env);register_android_server_InputApplicationHandle(env);register_android_server_InputWindowHandle(env);register_android_server_InputManager(env);register_android_server_LightsService(env);register_android_server_AlarmManagerService(env);register_android_server_UsbDeviceManager(env);register_android_server_UsbMidiDevice(env);register_android_server_UsbHostManager(env);register_android_server_vr_VrManagerService(env);register_android_server_VibratorService(env);register_android_server_SystemServer(env);
//......return JNI_VERSION_1_4;
}

由上可知,在JNI_OnLoad 中会调用添加若干个 server,没有发现sensor相关的server;

实则它是 SystemServer中,下面是register_android_server_SystemServer内容:

代码路径:./base/services/core/jni/com_android_server_SystemServer.cpp
/** JNI registration.*/
static const JNINativeMethod gMethods[] = {/* name, signature, funcPtr */{ "startSensorService", "()V", (void*) android_server_SystemServer_startSensorService },
};int register_android_server_SystemServer(JNIEnv* env)
{return jniRegisterNativeMethods(env, "com/android/server/SystemServer",gMethods, NELEM(gMethods));
}

可以看到 register_android_server_SystemServer 实际上是注册绑定了 sensor相关的 service启动方法(为啥不直接命名为sensor server,目前还不清楚);

通过method可知,systemServer调用run方法启动 startSensorService,会调用到android_server_SystemServer_startSensorService ; 以下是它的实现:

static void android_server_SystemServer_startSensorService(JNIEnv* /* env */, jobject /* clazz */) {char propBuf[PROPERTY_VALUE_MAX];property_get("system_init.startsensorservice", propBuf, "1");if (strcmp(propBuf, "1") == 0) {// Start the sensor service in a new threadcreateThreadEtc(start_sensor_service, nullptr,"StartSensorThread", PRIORITY_FOREGROUND);}
}

判断属性 “system_init.startsensorservice” 是否存在,如果存在 创建线程"StartSensorThread"(默认被配置了,还找到哪里配置的)

线程内容如下:

static int start_sensor_service(void* /*unused*/) {SensorService::instantiate();return 0;
}

该线程调用SensorService中的instantiate方法,创建了SensorService的实例;

SensorService实现

继续分析SensorService instantiate的创建,首先是看SensorService类的定义:

路径:./frameworks/native/services/sensorservice/SensorService.h
class SensorService :public BinderService<SensorService>,public BnSensorServer,protected Thread
{// nested class/struct for internal useclass SensorEventConnection;public:void cleanupConnection(SensorEventConnection* connection);status_t enable(const sp<SensorEventConnection>& connection, int handle,nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags,const String16& opPackageName);status_t disable(const sp<SensorEventConnection>& connection, int handle);status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,const String16& opPackageName);status_t flushSensor(const sp<SensorEventConnection>& connection,const String16& opPackageName);
private:friend class BinderService<SensorService>;// nested class/struct for internal useclass SensorRecord;class SensorEventAckReceiver;struct SensorRegistrationInfostatic char const* getServiceName() ANDROID_API { return "sensorservice"; }SensorService() ANDROID_API;virtual ~SensorService();virtual void onFirstRef();// Thread interfacevirtual bool threadLoop();// ISensorServer interfacevirtual Vector<Sensor> getSensorList(const String16& opPackageName);virtual status_t dump(int fd, const Vector<String16>& args);String8 getSensorName(int handle) const;bool isVirtualSensor(int handle) const;sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const;bool isWakeUpSensor(int type) const;void recordLastValueLocked(sensors_event_t const* buffer, size_t count);static void sortEventBuffer(sensors_event_t* buffer, size_t count);const Sensor& registerSensor(SensorInterface* sensor,bool isDebug = false, bool isVirtual = false);const Sensor& registerVirtualSensor(SensorInterface* sensor, bool isDebug = false);
};

删了一些相对无关的方法及属性后,还有如上内容,没有instantiate方法;

通过类的定义可以发现SensorService 继承了BinderService;BnSensorServer,Thread这三个class; instantiate 是被定义在它的父类BinderService中了;

以下是BinderService的实现:

代码路径:./frameworks/native/include/binder/BinderService.hclass BinderService
{
public:static status_t publish(bool allowIsolated = false) {sp<IServiceManager> sm(defaultServiceManager());return sm->addService(String16(SERVICE::getServiceName()),new SERVICE(), allowIsolated);}static void publishAndJoinThreadPool(bool allowIsolated = false) {publish(allowIsolated);joinThreadPool();}static void instantiate() { publish(); }
......
};

publish()中

sp sm(defaultServiceManager());

可知SensorService::instantiate()在这一过程创建了SensorService并通过addService将自己新创建的SensorService服务添加到Android服务列表里了。

SensorService有一条继承关系如下:

SensorService : public BnSensorServer
BnSensorServer : public BnInterface<ISensorServer>
BnInterface :  public BBinder
BBinder : public IBinder
IBinder : public virtual RefBase 

在Android引用计数系统里,当RefBase的子类对象被第一次强引用时自动调用其onFirstRef方法,所以当第一次使用SensorService,onFirstRef方法将被被自动回调,onFirstRef方法里会创建SensorDevice 用来与HAL层进行交互,后面内容会做分析。

note:Android系统智能指针的设计思路(轻量级指针、强指针、弱指针) 虚继承

void SensorService::onFirstRef() {ALOGD("nuSensorService starting...");SensorDevice& dev(SensorDevice::getInstance());
//......

时序图:

根据以上过程,可绘制绘制出简要的uml时序图,如下:

总结过程:

  1. 系统初始化进程加载,启动SystemServer,Zygote中会执行SystemServier main方法,导致其run方法被调用;
  2. 加载本地库文件, System.loadLibrary("android_servers"); 获取本地方法;
  3. 被加载到的JNI库文件导致JNI_Onload函数被调用;调用本地jni文件
  4. 注册本地方法jniRegisterNativeMethods 数组;
  5. 完成Java 到 C++ 函数绑定,使Java能否访问到C库中的函数;
  6. 启动startBootstrapServices();
  7. 最后调用native方法 native void startSensorService();
  8. JNI文件com_android_server_SystemServer.cpp,绑定的函数数组,由java的startSensorService方法绑定到android_server_SystemServer_startSensorService函数;
  9. C++函数中,start_sensor_service被调用;
  10. 调用SensorService的inistantiate函数(继承父类BinderService得到的);
  11. 调用publish
  12. 创建一个Serivces,通过sm->addService 来添加到android中去; sm->addService( String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated); 其中sm是ServiceManager的引用:sp sm(defaultServiceManager());

这篇关于Android Senor Framework (三)SensorService启动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/914592

相关文章

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I