在SensorManager.java文件的开头,有一段sensor应用的示例。应用层获取sensor的数据主要是通过SensorManager的onAccuracyChanged和onSensorChanged两个监听接口。
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}
代码路径:frameworks/base/core/java/android/hardware/SensorManager.java,SensorManager类主要是对外提供sensor的调用接口:getDefaultSensor,registerListener,unregisterListener。
从下面的代码里可以看到,这三个接口会调用getFullSensorList,registerListenerImpl,unregisterListenerImpl。实际实现的代码在SystemSensorManager.java里。
public Sensor getDefaultSensor(int type) {
// TODO: need to be smarter, for now, just return the 1st sensor
List<Sensor> l = getSensorList(type);
return l.isEmpty() ? null : l.get(0);
}
public List<Sensor> getSensorList(int type) {
// cache the returned lists the first time
List<Sensor> list;
final List<Sensor> fullList = getFullSensorList();
synchronized (mSensorListByType) {
list = mSensorListByType.get(type);
if (list == null) {
if (type == Sensor.TYPE_ALL) {
list = fullList;
} else {
list = new ArrayList<Sensor>();
for (Sensor i : fullList) {
if (i.getType() == type)
list.add(i);
}
}
list = Collections.unmodifiableList(list);
mSensorListByType.append(type, list);
}
}
return list;
}
public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate) {
return registerListener(listener, sensor, rate, null);
}
public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate,
Handler handler) {
if (listener == null || sensor == null) {
return false;
}
int delay = -1;
switch (rate) {
case SENSOR_DELAY_FASTEST:
delay = 0;
break;
case SENSOR_DELAY_GAME:
delay = 20000;
break;
case SENSOR_DELAY_UI:
delay = 66667;
break;
case SENSOR_DELAY_NORMAL:
delay = 200000;
break;
default:
delay = rate;
break;
}
return registerListenerImpl(listener, sensor, delay, handler);
}
public void unregisterListener(SensorEventListener listener) {
if (listener == null) {
return;
}
unregisterListenerImpl(listener, null);
}
代码路径:frameworks/base/core/java/android/hardware/SystemSensorManager.java, SystemSensorManager继承SensorManager,完成getFullSensorList,registerListenerImpl,unregisterListenerImpl接口。在实现这几个接口时调用了JNI
private static native void nativeClassInit();
private static native int sensors_module_init();
private static native int sensors_module_get_next_sensor(Sensor sensor, int next);
// Used within this module from outside SensorManager, don't make private
static native int sensors_create_queue();
static native void sensors_destroy_queue(int queue);
static native boolean sensors_enable_sensor(int queue, String name, int sensor, int enable);
static native int sensors_data_poll(int queue, float[] values, int[] status, long[] timestamp);
在SystemSensorManager的构造方法里起了一个线程sSensorThread = new SensorThread();在这个线程里回调onAccuracyChanged和onSensorChanged方法,向上返回sensor的数据。
代码路径:frameworks/base/core/jni/android_hardware_SensorManager.cpp, 为SystemSensorManager提供JNI接口,使用Binder和SensorService通信
static JNINativeMethod gMethods[] = {
{"nativeClassInit", "()V", (void*)nativeClassInit },
{"sensors_module_init","()I", (void*)sensors_module_init },
{"sensors_module_get_next_sensor","(Landroid/hardware/Sensor;I)I",
(void*)sensors_module_get_next_sensor },
{"sensors_create_queue", "()I", (void*)sensors_create_queue },
{"sensors_destroy_queue", "(I)V", (void*)sensors_destroy_queue },
{"sensors_enable_sensor", "(ILjava/lang/String;II)Z",
(void*)sensors_enable_sensor },
{"sensors_data_poll", "(I[F[I[J)I", (void*)sensors_data_poll },
};
}; // namespace android
using namespace android;
int register_android_hardware_SensorManager(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager",
gMethods, NELEM(gMethods));
}
代码路径:frameworks/native/services/sensorservice/SensorService.cpp,
frameworks/native/services/sensorservice/SensorDevice.cpp
这两个文件会编译成libsensorservice.so,使用hw_get_module函数获取sensor模块的hw_module_t,从而实现sensor的控制和数据读取。通过SensorEventConnection与JNI通信。
以下是MTK代码的HAL实现。
代码路径:mediatek/hardware/sensor/hwmsen/sensors_hwmsen.c,编译成libsensors.default.so。
主要open /dev/hwmsensor设备,使用ioctl控制hwmsensor,完成HAL_MODULE_INFO_SYM结构.
MTK代码里使用了一个hwmsensor模块控制所有的sensor。
代码路径:mediatek/kernel/drivers/hwmon/hwmsen/hwmsen_dev.c,编译成hwmsen_dev.o,系统起来后会生成/dev/hwmsensor设备。
使用sensor_operate接口管理所有sensor驱动,向上提供hwmsen_unlocked_ioctl接口
再往下就是具体的sensor驱动代码了,根据MTK的驱动结构完成sensor_operate接口,并调用hwmsen_dev.c里的hwmsen_attach函数,把sensor_operate接口加到hwmsen_dev的列表里,这样hwmsen_dev里就能调用所有sensor的sensor_operate函数。
以上是sensor的调用过程,还有一点android_hardware_SensorManager.cpp里的本地接口与sensorservice通信传输数据。这个sensorservice是什么时候怎么启动的呢?
这个要涉及到系统服务的启动。
相应代码路径:
frameworks/base/core/java/com/android/internal/os/zygoteInit.java
frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/base/services/jni/com_android_server_SystemServer.cpp
frameworks/base/cmds/system_server/library/system_init.cpp
在init.rc启动zygote
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
由zygoteInit.java的startSystemServer()用Zygote.forkSystemServer启动com.android.server.SystemServer
SystemServer.java调用init1(),
init1()是com_android_server_SystemServer.cpp的android_server_SystemServer_init1提供的JNI接口,里面调用system_init(),
system_init()里启动SensorService::instantiate();