/************************************************************/
分析背景:
Android4.1.2(Based on CyanogenMod 10)
DASH(https://github.com/sonyxperiadev/DASH)
1.接口头文件sensors.h(hardware/libhardware/include/sensors.h)
刚一打开就看到SENSOR HAL的ID 为"SENSORS_HARDWARE_MODULE_ID"。
[cpp]
/**
* Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
* A Handle identifies a given sensors. The handle is used to activate
* and/or deactivate sensors.
* In this version of the API there can only be 256 handles.
*/
#define SENSORS_HANDLE_BASE 0
#define SENSORS_HANDLE_BITS 8
#define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS)
/* 8位能够支持256个handle */
/**
* Sensor types
*/
#define SENSOR_TYPE_ACCELEROMETER 1 //加速度传感器
#define SENSOR_TYPE_MAGNETIC_FIELD 2 //磁力传感器
#define SENSOR_TYPE_ORIENTATION 3 //方向传感器
#define SENSOR_TYPE_GYROSCOPE 4 //陀螺仪
#define SENSOR_TYPE_LIGHT 5 //亮度传感器
#define SENSOR_TYPE_PRESSURE 6 //压力传感器
#define SENSOR_TYPE_TEMPERATURE 7 // deprecated 温度传感器,以后的版本中将看不到任何身影
#define SENSOR_TYPE_PROXIMITY 8 //距离传感器
[cpp]
#define SENSOR_TYPE_GRAVITY 9 //重力传感器
#define SENSOR_TYPE_LINEAR_ACCELERATION 10 //速度传感器
#define SENSOR_TYPE_ROTATION_VECTOR 11 //旋转矢量传感器
#define SENSOR_TYPE_RELATIVE_HUMIDITY 12 //相对湿度传感器
#define SENSOR_TYPE_AMBIENT_TEMPERATURE 13 //环境温度传感器
支持的传感器还很多,但是 一般设备并不会用到这么多传感器,也就是加速度传感器、重力传感器、方向传感器、距离传感器、光线传感器这些,陀螺仪都很少见。
[cpp]
/**
* Definition of the axis
* ----------------------
*
* This API is relative to the screen of the device in its default orientation,
* that is, if the device can be used in portrait or landscape, this API
* is only relative to the NATURAL orientation of the screen. In other words,
* the axis are not swapped when the device's screen orientation changes.
* Higher level services /may/ perform this transformation.
*
* x<0 x>0
* ^
* |
* +-----------+--> y>0
* | |
* | |
* | 设备 |
* | | / z<0
* | 面朝天空 | /
* | | /
* O-----------+/
* |[] [ ] []/
* +----------/+ y<0
* /
* /
* |/ z>0 (toward the sky)
*
* O: Origin (x=0,y=0,z=0)
还用有趣的符号图形形象的描述了“轴”的概念。然后就是一坨坨的传感器知识,blablabla......
定义了几个结构体:
1) sensors_vec_t 对单个传感器的泛用性封装,包含坐标、角度、状态等信息;
2) sensors_event_t 对传感器细致的数据再封装,包含版本号、传感器类型、数据、加速度、磁力、角度、重力等等等等的信息;
3) sensor_t 对应每个传感器,是都会有的数据,包括传感器名称、版本、handle句柄、类型、最大范围、解析度、耗能、最小延迟等信息;
4) sensors_module_t 对hw_module_t的扩展,不仅有common为hw_module_t,还定义了一个函数get_sensors_list用来获取所支持的传感器,返回值为传感器总数;
5) sensors_poll_device_t 每个传感器所私有的数据操作,包括(反)激活、设置延时、提取数据等动作。
最后定义了两个API函数用来打开/关闭一个传感器。
[cpp]
static inline int sensors_open(const struct hw_module_t* module,
struct sensors_poll_device_t** device) {
return module->methods->open(module,
SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}
static inline int sensors_close(struct sensors_poll_device_t* device) {
return device->common.close(&device->common);
}
2. 服务开启之路(1)
sensor服务由system_server开启:
frameworks/base/cmds/system_server/system_init.cpp
[cpp]
extern "C" status_t system_init()
{
ALOGI("Entered system_init()");
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
ALOGI("ServiceManager: %p\n", sm.get());
...
}
property_get("system_init.startsensorservice", propBuf, "1");
if (strcmp(propBuf, "1") == 0) {
// Start the sensor service
SensorService::instantiate();
}
...
}
调用到frameworks/base/services/sensorservice/SensorService.cpp
[cpp]
void SensorService::onFirstRef()
{
ALOGD("nuSensorService starting...");
SensorDevice& dev(SensorDevice::getInstance());
之后又调用到了SensorDevice.cpp(同目录下)
[cpp]
SensorDevice::SensorDevice()
: mSensorDevice(0),
mSensorModule(0)
补充:软件开发 , C++ ,