Android gets this Sensor object

Android gets this Sensor object

本文基于aosp/android11-release -- 清华大学开源软件镜像站AOSP

1. App to get this Sensor

Can view SensorService data passed to APK

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

The sensor is obtained through the inherited class SystemSensorManager of SensorManager. Here we only pay attention to the sensor object, not to the SensorEventListener monitoring. Get SystemSensorManager initialization objectmFullSensorsListSensor.TYPE_ACCELEROMETER in the sensor.

frameworks/base/core/java/android/hardware/SensorManager.java
frameworks/base/core/java/android/hardware/SystemSensorManager.java
frameworks/base/core/jni/android_hardware_SensorManager.cpp

Insert picture description hereInsert picture description hereInsert picture description here
Insert picture description here

2. Actually obtained through JNI

Through nativeGetSensorAtIndex in JNI, finally in SensorService.cppSensorService::getSensorListGet the SensorList, iemSensors.getUserSensors(), And finally get it in e.si- >getSensor() of SensorList.cpp .

frameworks/base/core/jni/android_hardware_SensorManager.cpp
frameworks/native/libs/sensor/SensorManager.cpp
frameworks/native/libs/sensor/include/sensor/SensorManager.h
frameworks/native/libs/sensor/ISensorServer.cpp
frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorList.cpp

Insert picture description hereInsert picture description here
Insert picture description hereInsert picture description here
Insert picture description hereInsert picture description here
Insert picture description here

3. SensorService中mSensors

mSensors registrationSensorService::registerSensorThis sensor is added. When SensorService creates onFirstRef, it will register the related HardwareSensor and VirtualSensor. For SensorService startup process, please see: SensorService startup

frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorService.h
frameworks/native/services/sensorservice/SensorList.cpp
frameworks/native/services/sensorservice/SensorList.h
SensorServiceUtil::SensorList mSensors;

Insert picture description here
Insert picture description here

4. This sensor object is converted to Sensor.java in Java

The basic context is already clear, so how does this C++ layer be converted to Java? In the above "2. Actually obtained through JNI" nativeGetSensorAtIndex method is calledtranslateNativeSensorToJavaSensorMethod JNI conversion

frameworks/core/java/android/hardware/Sensor.java
frameworks/base/core/jni/android_hardware_SensorManager.cpp

Insert picture description hereInsert picture description here

5.Sensor HAL obtains the Sensor object

  The Sensor.java object has been obtained above, but the Sensor data is all hardware support uploading data, whether it is a HardwareSensor directly supported by the hardware or a virtual VirtualSensor. The difference is that VirtualSensor combines the data reported by multiple HardwareSensors through SensorFusion, performs process calculations and then reports. For more details, please see: Overview of Android Sensor

5.1 Obtaining HardwareSensor

When SensorService is constructed, registerSensor( new HardwareSensor(list[i])) in onFirstRef adds HardwareSensor to mSensors(The essence is SensorList.cpp)in. SensorDevice is initialized through connectHidlService() so that mSensors (This is different from SensorService) Connect the hardware device and initializeSensorList to initialize mSensorList to obtain the hardware definition sensor_t. Here we do not pay attention to the connection and definition of hardware devices. This is a matter of specific devices and suppliers, and is a trade secret.

frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorDevice.cpp

Insert picture description hereInsert picture description hereInsert picture description here

5.1.1 View the sensor_t required by the hardware device

Android has defined many such interfaces in the form of HAL interfaces (defined as C header files in hardware/libhardware).

# 常用设备HAL标准接口的头文件
hardware/libhardware/include/hardware
# 常用设备HAL标准接口实现
hardware/libhardware/modules/
hardware/libhardware/include/hardware/sensors.h
struct sensor_t {
    
    

    /* Name of this sensor.
     * All sensors of the same "type" must have a different "name".
     */
    const char*     name;

    /* vendor of the hardware part */
    const char*     vendor;

    /* version of the hardware part + driver. The value of this field
     * must increase when the driver is updated in a way that changes the
     * output of this sensor. This is important for fused sensors when the
     * fusion algorithm is updated.
     */
    int             version;

    /* handle that identifies this sensors. This handle is used to reference
     * this sensor throughout the HAL API.
     */
    int             handle;

    /* this sensor's type. */
    int             type;

    /* maximum range of this sensor's value in SI units */
    float           maxRange;

    /* smallest difference between two values reported by this sensor */
    float           resolution;

    /* rough estimate of this sensor's power consumption in mA */
    float           power;

    /* this value depends on the reporting mode:
     *
     *   continuous: minimum sample period allowed in microseconds
     *   on-change : 0
     *   one-shot  :-1
     *   special   : 0, unless otherwise noted
     */
    int32_t         minDelay;

    /* number of events reserved for this sensor in the batch mode FIFO.
     * If there is a dedicated FIFO for this sensor, then this is the
     * size of this FIFO. If the FIFO is shared with other sensors,
     * this is the size reserved for that sensor and it can be zero.
     */
    uint32_t        fifoReservedEventCount;

    /* maximum number of events of this sensor that could be batched.
     * This is especially relevant when the FIFO is shared between
     * several sensors; this value is then set to the size of that FIFO.
     */
    uint32_t        fifoMaxEventCount;

    /* type of this sensor as a string.
     *
     * If type is OEM specific or sensor manufacturer specific type
     * (>=SENSOR_TYPE_DEVICE_PRIVATE_BASE), this string must be defined with reserved domain of
     * vendor/OEM as a prefix, e.g. com.google.glass.onheaddetector
     *
     * For sensors of Android defined types, Android framework will override this value. It is ok to
     * leave it pointing to an empty string.
     */
    const char*    stringType;

    /* permission required to see this sensor, register to it and receive data.
     * Set to "" if no permission is required. Some sensor types like the
     * heart rate monitor have a mandatory require_permission.
     * For sensors that always require a specific permission, like the heart
     * rate monitor, the android framework might overwrite this string
     * automatically.
     */
    const char*    requiredPermission;

    /* This value is defined only for continuous mode and on-change sensors. It is the delay between
     * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
     * frequencies are requested through batch()/setDelay() the events will be generated at this
     * frequency instead. It can be used by the framework or applications to estimate when the batch
     * FIFO may be full.
     *
     * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
     *              continuous, on-change: maximum sampling period allowed in microseconds.
     *              one-shot, special : 0
     *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
     *      on 64 bit architectures only for binary compatibility reasons.
     * Availability: SENSORS_DEVICE_API_VERSION_1_3
     */
    #ifdef __LP64__
       int64_t maxDelay;
    #else
       int32_t maxDelay;
    #endif

    /* Flags for sensor. See SENSOR_FLAG_* above. Only the least significant 32 bits are used here.
     * It is declared as 64 bit on 64 bit architectures only for binary compatibility reasons.
     * Availability: SENSORS_DEVICE_API_VERSION_1_3
     */
    #ifdef __LP64__
       uint64_t flags;
    #else
       uint32_t flags;
    #endif

    /* reserved fields, must be zero */
    void*           reserved[2];
};

5.1.1 This sensor object is converted to Sensor.cpp in C++

SensorService in the onFirstRef of SensorService still gets sensor_t, initialize mSensor when registerSensor

frameworks/native/services/sensorservice/SensorInterface.h
frameworks/native/services/sensorservice/SensorInterface.cpp
frameworks/native/libs/sensor/include/sensor/Sensor.h
frameworks/native/libs/sensor/Sensor.cpp

Insert picture description hereInsert picture description here

5.2 Get VirtualSensor

Same as above "5.1 HardwareSensor acquisition",The difference is that there is an additional SensorFusion member, and sensor_t needs to be defined, such as RotationVectorSensor
Insert picture description here

frameworks/native/services/sensorservice/RotationVectorSensor.cpp
frameworks/native/services/sensorservice/RotationVectorSensor.h

Insert picture description here

summary

frameworks/core/java/android/hardware/Sensor.java new an object is given to JNI
frameworks/base/core/jni/android_hardware_SensorManager.cpp through JNI to connect
frameworks/native/libs/sensor/Sensor.cpp initialization is the
hardware corresponding to sensor_t Structure sensor_t in /libhardware/include/hardware/sensors.h

Guess you like

Origin blog.csdn.net/qq_23452385/article/details/109692705