本文主要是介绍Android9.0源码学习-Sensor Framework,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- FileList
- Questions
- SensorService初始化
- registerListener流程分析
- APP
- Framework
- Java
- Native
- Client
- Server
- How the app get sensor data
- Server
- BitTube
- Client
- Reference
在之前的学习中, Android Sensor概述介绍了常用传感器的功能,测量值的含义,测量原理等等。 Android Sensor应用介绍了app如何得到Sensor的数值。那么接下来就应该思考应用层是如何得到硬件测量的数值,传感器的数值又是如何一步一步上传至应用层的。
FileList
Framework(/frameworks/base/core/java/android/hardware/)
SensorManager.java
:SDK接口,封装了Sensor相关的API,提供给APP层使用。SystemSensorManager.java
:继承于SensorManager,SensorManager的实现类,负责与系统Sensor的联系。Sensor.java
:代表一个Sensor类SensorEvent.java
:代表一个SensorEvent类SensorEventListener.java
:用于接收传感器事件的Interface
JNI(/framework/base/core/jni)
android_hardware_SensorManager.cpp
:负责Java层和Native层的通信
Native Framework
- Client(/framework/native/lib/sensor)
SensorManager.cpp
:SensorManager的Native层,负责与SensorService的通信SensorEventQueue.cpp
:消息队列Sensor.cpp
:Sensor的native层ISensorServer.cpp
:SensorServer在客户端接口ISensorEventConnection.cpp
:SensorEventConnection在客户端接口BitTube.cpp
:单向字节管道,提供进程间单向数据通信功能。
- Server(/framework/native/services/sensorservice)
SensorDevice.cpp
:负责管理和维护系统中所有的Sensor,封装了Sensor的使能,配置,数据读取等功能。SensorInterface.cpp
:服务端Sensor接口SensorService.cpp
:是整个Android Sensor Framework最核心的模块,它实现了主要的Sensor控制流和数据流,完成Sensor的参数配置,数据分发SensorEventConnection.cpp
:Sensor的数据传输通道,当Client开始监听某一个Sensor时,一个对应的SensorEventConnection将会被创建,Server端在接收到Sensor数据后,通过写入到SensorEventConnection传递给Client端。
Questions
在Framework的学习中,我们需要搞清楚以下几个问题?
- APP层的控制指令是怎样一层一层下发到Framework层和HAL层?
- sensor采集到的数据又是怎样一层一层传到APP层?
- Client端和Server端又是怎样交互的,数据是如何通讯的?
- 当系统第一次创建SensorService时,SensorService做了哪些初始化的工作?
SensorService初始化
SystemServer的main函数,会调用startSensorService()
,从而创建第一个SensorService
实例。当SensorService第一个实例创建时,其onFirstRef()
接口会被调用。
onFirstRef()
做了这么以下几个工作:
- 创建SensorDevice的实例(
SensorDevice& dev(SensorDevice::getInstance());
),并且是单例类connectHidlService()
mSensors = ISensors::getService()
Sensors::Sensors()
hw_get_module()
加载Sensor HAL的动态库sensors_open_1()
执行Sensor HAL的初始化
- 初始化
mSensorList
(Vector<sensor_t> mSensorList
) mSensors->activate()
- 通过dev获取SensorList(
list
) - registerSensor
- 创建Looper(
mLooper = new Looper(false)
),用于enable sensor后,进行数据的接收 - 创建SensorEventAckReceiver(
mAckReceiver = new SensorEventAckReceiver(this)
):用于在dispatch wake up sensor event给上层后,接收上层返回的确认ACK。
registerListener流程分析
APP
下面的代码是在Android官网提供的APP获取light sensor数据的代码段,这里在获取SensorManager和Sensor后,会调用sensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
注册。
public class SensorActivity extends Activity implements SensorEventListener {private SensorManager sensorManager;private Sensor mLight;@Overridepublic final void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mLight = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);}@Overridepublic final void onAccuracyChanged(Sensor sensor, int accuracy) {// Do something here if sensor accuracy changes.}@Overridepublic final void onSensorChanged(SensorEvent event) {// The light sensor returns a single value.// Many sensors return 3 values, one for each axis.float lux = event.values[0];// Do something with this sensor value.}@Overrideprotected void onResume() {super.onResume();sensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);}@Overrideprotected void onPause() {super.onPause();sensorManager.unregisterListener(this);}
}
Framework
Java
在java层的调用顺序为
SensorManager.java
registerListener()
registerListenerImpl()
SystemSensorManager.java
registerListenerImpl()
registerListenerImpl()
会维护一个mSensorListeners
的HashMap(HashMap<SensorEventListener, SensorEventQueue> mSensorListeners
)。
queue.addSensor()
(SensorEventQueue queue
)mActiveSensors.put(handle, true)
(handle = snesor.getHandle()
)enableSensor()
nativeEnableSensor()
到这里会通过JNI去call到Native层
/frameworks/base/core/java/android/hardware/SystemSensorManager.java
@Overrideprotected boolean registerListenerImpl(SensorEventListener listener, Sensor sensor,int delayUs, Handler handler, int maxBatchReportLatencyUs, int reservedFlags) {if (listener == null || sensor == null) {Log.e(TAG, "sensor or listener is null");return false;}// Trigger Sensors should use the requestTriggerSensor call.if (sensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) {Log.e(TAG, "Trigger Sensors should use the requestTriggerSensor.");return false;}if (maxBatchReportLatencyUs < 0 || delayUs < 0) {Log.e(TAG, "maxBatchReportLatencyUs and delayUs should be non-negative");return false;}if (mSensorListeners.size() >= MAX_LISTENER_COUNT) {throw new IllegalStateException("register failed, "+ "the sensor listeners size has exceeded the maximum limit "+ MAX_LISTENER_COUNT);}// Invariants to preserve:// - one Looper per SensorEventListener// - one Looper per SensorEventQueue// We map SensorEventListener to a SensorEventQueue, which holds the loopersynchronized (mSensorListeners) {SensorEventQueue queue = mSensorListeners.get(listener);if (queue == null) {Looper looper = (handler != null) ? handler.getLooper() : mMainLooper;final String fullClassName =listener.getClass().getEnclosingClass() != null? listener.getClass().getEnclosingClass().getName(): listener.getClass().getName();queue = new SensorEventQueue(listener, looper, this, fullClassName);if (!queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs)) {queue.dispose();return false;}mSensorListeners.put(listener, queue);return true;} else {return queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs);}}}
Native
Client
通过JNI,java层的enableSensor()
会call到SensorEventQueue.cpp的enableSensor()
,到这里就要去通过mSensorEventConnection
(const sp<ISensorEventConnection>
)去call到Server端了。
/frameworks/native/libs/sensor/SensorEventQueue.cpp
status_t SensorEventQueue::enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const {return mSensorEventConnection->enableDisable(sensor->getHandle(), true,us2ns(samplingPeriodUs), 0, 0);
}
/frameworks/native/libs/sensor/ISensorEventConnection.cpp
virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,nsecs_t maxBatchReportLatencyNs, int reservedFlags){Parcel data, reply;data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());data.writeInt32(handle);data.writeInt32(enabled);data.writeInt64(samplingPeriodNs);data.writeInt64(maxBatchReportLatencyNs);data.writeInt32(reservedFlags);remote()->transact(ENABLE_DISABLE, data, &reply);return reply.readInt32();}
Server
/frameworks/native/services/sensorservice/SensorEventConnection.cpp
status_t SensorService::SensorEventConnection::enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,int reservedFlags)
{status_t err;if (enabled) {err = mService->enable
这篇关于Android9.0源码学习-Sensor Framework的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!