android alsps代码跟踪

android alsps代码跟踪

 

相关文件列表

01. frameworks/base/services/core/java/com/android/server/display/DisplayPowerController.java02. frameworks/base/services/core/java/com/android/server/display/DisplayManagerService.java
03. frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
04. frameworks/base/core/java/android/hardware/SystemSensorManager.java
05. frameworks/base/core/java/android/hardware/SensorManager.java
06. frameworks/base/core/jni/android_hardware_SensorManager.cpp
07. frameworks/native/libs/gui/SensorManager.cpp
08. frameworks/native/include/gui/SensorManager.h
09. frameworks/native/libs/gui/ISensorServer.cpp
10. frameworks/native/include/gui/ISensorServer.h
11. alps/frameworks/native/services/sensorservice/SensorService.h
12. frameworks/native/services/sensorservice/SensorService.cpp
20. frameworks/native/services/sensorservice/SensorInterface.h
21. vendor/mediatek/proprietary/hardware/sensor/sensors.c

 

1. ===============================================================================
frameworks/base/services/core/java/com/android/server/display/DisplayPowerController.java

public DisplayPowerController(Context context,
        DisplayPowerCallbacks callbacks, Handler handler,
            SensorManager sensorManager, DisplayBlanker blanker) {
    ... ...
    mSensorManager = sensorManager;
    ... ...
    if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
        mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        if (mProximitySensor != null) {
            mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(), TYPICAL_PROXIMITY_THRESHOLD);
        }
    }
    ... ...
}

 

2. ================================================================================
frameworks/base/services/core/java/com/android/server/display/DisplayManagerService.java

private final class LocalService extends DisplayManagerInternal {
    @Override
    public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
                    SensorManager sensorManager) {
        synchronized (mSyncRoot) {
            ... ...
            mDisplayPowerController = new DisplayPowerController( mContext, callbacks, handler, sensorManager, blanker);
        }
    }
    ... ...
}

 

3. ================================================================================
frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService
                    implements Watchdog.Monitor {
    ... ...
    public void systemReady(IAppOpsService appOps) {
        synchronized (mLock) {
            ... ...
            SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper());
            ... ...
            // Initialize display power management.
 mDisplayManagerInternal.initPowerManagement( mDisplayPowerCallbacks, mHandler, sensorManager);
        }
    }
    ... ...
}

 

4. ================================================================================
frameworks/base/core/java/android/hardware/SystemSensorManager.java

public class SystemSensorManager extends SensorManager {
    ... ...
    //find == mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);  //## SensorManager
    private final ArrayList<Sensor> mFullSensorsList = new ArrayList<>();
    ... ...
    /** {@hide} */
    public SystemSensorManager(Context context, Looper mainLooper) {
        mMainLooper = mainLooper;
        mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion;
        mContext = context;
        mNativeInstance = nativeCreate(context.getOpPackageName());

        synchronized(mLock) {
            if (!sSensorModuleInitialized) {
                sSensorModuleInitialized = true;
                nativeClassInit();
            }
        }

        // initialize the sensor list
        for (int index = 0;;++index) {
            Sensor sensor = new Sensor();
            if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
            mFullSensorsList.add(sensor);
            mHandleToSensor.append(sensor.getHandle(), sensor);
        }
    }
    @Override
    protected List<Sensor> getFullSensorList() {
        return mFullSensorsList;
    }
    ... ...
}

 

5. ================================================================================
frameworks/base/core/java/android/hardware/SensorManager.java

public abstract class SensorManager {
    ... ...
    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;
    }
    ... ...
  //mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
public Sensor getDefaultSensor(int type) { // TODO: need to be smarter, for now, just return the 1st sensor List<Sensor> l = getSensorList(type); boolean wakeUpSensor = false; // For the following sensor types, return a wake-up sensor. These types are by default // defined as wake-up sensors. For the rest of the SDK defined sensor types return a // non_wake-up version. if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION || type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE || type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE || type == Sensor.TYPE_WRIST_TILT_GESTURE) { wakeUpSensor = true; } for (Sensor sensor : l) { if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor; } return null; } }

 

06. ================================================================================
JNI ================================================================================
frameworks/base/core/jni/android_hardware_SensorManager.cpp

... ...
static jboolean
nativeGetSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint index)
{
    SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager);

    Sensor const* const* sensorList;
    size_t count = mgr->getSensorList(&sensorList);        //##
    if (size_t(index) >= count) {
        return false;
    }

    Sensor const* const list = sensorList[index];            //##
    const SensorOffsets& sensorOffsets(gSensorOffsets);
    jstring name = getInternedString(env, &list->getName());
    jstring vendor = getInternedString(env, &list->getVendor());
    jstring requiredPermission = getInternedString(env, &list->getRequiredPermission());
    env->SetObjectField(sensor, sensorOffsets.name,      name);
    env->SetObjectField(sensor, sensorOffsets.vendor,    vendor);
    env->SetIntField(sensor, sensorOffsets.version,      list->getVersion());
    env->SetIntField(sensor, sensorOffsets.handle,       list->getHandle());
    env->SetFloatField(sensor, sensorOffsets.range,      list->getMaxValue());
    env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution());
    env->SetFloatField(sensor, sensorOffsets.power,      list->getPowerUsage());
    env->SetIntField(sensor, sensorOffsets.minDelay,     list->getMinDelay());
    env->SetIntField(sensor, sensorOffsets.fifoReservedEventCount,
    list->getFifoReservedEventCount());
    env->SetIntField(sensor, sensorOffsets.fifoMaxEventCount,
    list->getFifoMaxEventCount());
    env->SetObjectField(sensor, sensorOffsets.requiredPermission,
    requiredPermission);
    env->SetIntField(sensor, sensorOffsets.maxDelay, list->getMaxDelay());
    env->SetIntField(sensor, sensorOffsets.flags, list->getFlags());
    if (env->CallBooleanMethod(sensor, sensorOffsets.setType, list->getType()) == JNI_FALSE) {
        jstring stringType = getInternedString(env, &list->getStringType());
        env->SetObjectField(sensor, sensorOffsets.stringType, stringType);
    }
    return true;
}
... ...

 

07. ================================================================================
frameworks/native/libs/gui/SensorManager.cpp

status_t SensorManager::assertStateLocked() const {
    if (mSensorServer == NULL) {
        // try for one second
        const String16 name("sensorservice");
        for (int i=0 ; i<4 ; i++) {
            status_t err = getService(name, &mSensorServer);
            ... ...
        }
        ... ...
        mSensors = mSensorServer->getSensorList(mOpPackageName);        //##
        size_t count = mSensors.size();
       mSensorList =
            static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
        for (size_t i=0 ; i<count ; i++) {
            mSensorList[i] = mSensors.array() + i;
        }
    }
    return NO_ERROR;
}
    
ssize_t SensorManager::getSensorList(Sensor const* const** list) const
{
    Mutex::Autolock _l(mLock);
    status_t err = assertStateLocked();
    if (err < 0) {
    return static_cast<ssize_t>(err);
    }
    *list = mSensorList;
    return static_cast<ssize_t>(mSensors.size());
}

 

08. ================================================================================
alps/frameworks/native/include/gui/SensorManager.h

class SensorManager :
    public ASensorManager
{
    ... ...
    mutable sp<ISensorServer> mSensorServer;
    ... ...
}

 

09. ===binder client (BpBinder)=&&=binder server (BnBinder)=======================================================
alps/frameworks/native/libs/gui/ISensorServer.cpp

class BpSensorServer : public BpInterface<ISensorServer>  // find the getSensorList
{
public:
    ... ...
    virtual Vector<Sensor> getSensorList(const String16& opPackageName)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeString16(opPackageName);
        remote()->transact(GET_SENSOR_LIST, data, &reply);        //##
        Sensor s;
        Vector<Sensor> v;
        uint32_t n = reply.readUint32();
        v.setCapacity(n);
        while (n--) {
            reply.read(s);
            v.add(s);
        }
        return v;
    }
    ... ...
}

... ...
status_t BnSensorServer::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case GET_SENSOR_LIST: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            const String16& opPackageName = data.readString16();
            Vector<Sensor> v(getSensorList(opPackageName));    //##SensorService::getSensorList
            size_t n = v.size();
            reply->writeUint32(static_cast<uint32_t>(n));
            for (size_t i = 0; i < n; i++) {
                reply->write(v[i]);
            }
            return NO_ERROR;
        }
        ... ...
    }
    return BBinder::onTransact(code, data, reply, flags);
}

 

10. ===binder server (BnBinder)============================================================
alps/frameworks/native/include/gui/ISensorServer.h

class ISensorServer : public IInterface
{
public:
    DECLARE_META_INTERFACE(SensorServer);

    virtual Vector<Sensor> getSensorList(const String16& opPackageName) = 0;    //## getSensorList
    virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
    int mode, const String16& opPackageName) = 0;
    virtual int32_t isDataInjectionEnabled() = 0;
};

class BnSensorServer : public BnInterface<ISensorServer>
{
public:
    virtual status_t onTransact( uint32_t code,
            const Parcel& data,
            Parcel* reply,
            uint32_t flags = 0);
};

 

11 ================================================================================
alps/frameworks/native/services/sensorservice/SensorService.h

class SensorService :
    public BinderService<SensorService>,
    public BnSensorServer,
    protected Thread
{
    ... ...
    Vector<Sensor> mSensorList;
    virtual Vector<Sensor> getSensorList(const String16& opPackageName);
    ... ...
}

12. ================================================================================
frameworks/native/services/sensorservice/SensorService.cpp

Vector<Sensor> SensorService::getSensorList(const String16& opPackageName)
{
    char value[PROPERTY_VALUE_MAX];
    property_get("debug.sensors", value, "0");
    const Vector<Sensor>& initialSensorList = (atoi(value)) ? mUserSensorListDebug : mUserSensorList;        //## ?= mUserSensorList
    Vector<Sensor> accessibleSensorList;
    for (size_t i = 0; i < initialSensorList.size(); i++) {
        Sensor sensor = initialSensorList[i];
        if (canAccessSensor(sensor, "getSensorList", opPackageName)) {
            accessibleSensorList.add(sensor);                //## add SensorList
        } else {
            ALOGI("Skipped sensor %s because it requires permission %s and app op %d",
            sensor.getName().string(),
            sensor.getRequiredPermission().string(),
            sensor.getRequiredAppOp());
        }
    }
    return accessibleSensorList;
}


void SensorService::onFirstRef()
{
    sensor_t const* list;            //##
    ssize_t count = dev.getSensorList(&list);

    ... ...
    for (ssize_t i=0 ; i<count ; i++) {
        registerSensor( new HardwareSensor(list[i]) );    //#### new Sensor ##
        ... ...
    }
    mUserSensorList = mSensorList;    //## mSensorList
    if (hasGyro && hasAccel && hasMag) {
        Sensor aSensor;
        aSensor = registerVirtualSensor( new RotationVectorSensor() );
        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
            mUserSensorList.add(aSensor);        //## mSensorList
        }
        aSensor = registerVirtualSensor( new GravitySensor(list, count) );
        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
            mUserSensorList.add(aSensor);
        }
        aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
            mUserSensorList.add(aSensor);
        }
        aSensor = registerVirtualSensor( new OrientationSensor() );
        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
            mUserSensorList.replaceAt(aSensor, orientationIndex);
        }
        registerVirtualSensor( new CorrectedGyroSensor(list, count) );
        registerVirtualSensor( new GyroDriftSensor() );
    }
    mUserSensorListDebug = mSensorList;        //## mSensorList

    ... ...
}

Sensor SensorService::registerSensor(SensorInterface* s)
{
    sensors_event_t event;
    memset(&event, 0, sizeof(event));

    const Sensor sensor(s->getSensor());            //## sensor
    // add to the sensor list (returned to clients)
    mSensorList.add(sensor);                //## mSensorList
    // add to our handle->SensorInterface mapping
    mSensorMap.add(sensor.getHandle(), s);
    // create an entry in the mLastEventSeen array
    mLastEventSeen.add(sensor.getHandle(), NULL);
    return sensor;
}
Sensor SensorService::registerVirtualSensor(SensorInterface* s)
{
    Sensor sensor = registerSensor(s);
    mVirtualSensorList.add( s );
    return sensor;
}

 

13 ================================================================================
alps/frameworks/native/services/sensorservice/SensorInterface.h

Sensor HardwareSensor::getSensor() const {
    return mSensor;
}

 

===============================================================================

================无关联分割线==========================================

===============================================================================

20 ================================================================================
alps/vendor/mediatek/proprietary/hardware/sensor/sensors.c

... ...
#ifndef PROXIMITY_RANGE
    #define PROXIMITY_RANGE 1.00f
#endif
... ...
struct sensor_t sSensorList[] = 
{
    ... ...
    #if defined(CUSTOM_KERNEL_ALSPS) || defined(CUSTOM_KERNEL_PS) 
    {
    .name       = PROXIMITY,
    .vendor     = PROXIMITY_VENDER,
    .version    = 1,
    .handle     = ID_PROXIMITY+ID_OFFSET,
    .type       = SENSOR_TYPE_PROXIMITY,
    .maxRange = PROXIMITY_RANGE,//1.00f,
    .resolution = PROXIMITY_RESOLUTION,//1.0f,
    .power      = PROXIMITY_POWER,//0.13f,
    .reserved   = {}
    },
    #endif  //CUSTOM_KERNEL_ALSPS || CUSTOM_KERNEL_PS
    ... ...
}

21 ————————————————————————————————————————-
alps/vendor/mediatek/proprietary/hardware/sensorhub/cwmcu/SensorChipInfo.cpp

... ...
#ifndef PROXIMITY_RANGE
#define PROXIMITY_RANGE 1.00f
#endif

... ...
struct sensor_t sSensorList[] = {
    ... ...
    #ifdef CUSTOM_KERNEL_PS
    {    //Proximity
        .name       = "Proximity Sensor",
        .vendor     = "CyWee Group Ltd.",
        .version    = 1,
        .handle     = PROXIMITY,
        .type       = SENSOR_TYPE_PROXIMITY,
    .maxRange = PROXIMITY_RANGE,
        .resolution = PROXIMITY_RESOLUTION,
        .power      = PROXIMITY_POWER,
        .minDelay    = 0,
        .fifoReservedEventCount = 0,
        .fifoMaxEventCount = 0,
        .stringType = NULL,
        .requiredPermission = NULL,
        .maxDelay = 10000,
        .flags = SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE,
    },
    #endif
    ... ...
}

 

遗憾:未跟踪到maxRange, 只是搜索找到maxRange

———— the end

    原文作者:PowerManagerService
    原文地址: http://www.cnblogs.com/TS-qrt/articles/android_alsps.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞