欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

qcom android GPS 启动流程

程序员文章站 2022-07-14 11:25:42
...

1 LocationManagerService启动及初始化过程

(1) 在SystemServer.java的startOtherServices中添加LocationManagerService的代码如下:

try {
    location = new LocationManagerService(context);
    ServiceManager.addService(Context.LOCATION_SERVICE, location);
} catch (Throwable e) {
    reportWtf("starting Location Manager", e);
}

(2) 在systemReady中调用systemRunning, 启动LocationManagerService的代码如下:

try {
    if (locationF != null) locationF.systemRunning();
} catch (Throwable e) {
    reportWtf("Notifying Location Service running", e);
}

(3) 准备GnssLocationProvider,LocationManagerService的systemRunning函数中会调用 loadProvidersLocked, 其代码如下:

private void loadProvidersLocked() {    
    // create a passive location provider, which is always enabled
    PassiveProvider passiveProvider = new PassiveProvider(this);
    addProviderLocked(passiveProvider);
    mEnabledProviders.add(passiveProvider.getName());
    mPassiveProvider = passiveProvider;

    if (GnssLocationProvider.isSupported()) {
        // Create a gps location provider
        GnssLocationProvider gnssProvider = new GnssLocationProvider(mContext, this,
                mLocationHandler.getLooper());
        mGnssSystemInfoProvider = gnssProvider.getGnssSystemInfoProvider();
        ......
    }
    ......
}

(4) GnssLocationProvider的class_init_native方法。

该方法是一个静态方法,会在isSupported方法之前被调用。最终会调用到native函数中:android_location_GnssLocationProvider_class_init_native。
在这个函数中实现对gps.default.so的加载。

static void android_location_GnssLocationProvider_class_init_native(JNIEnv* env, jclass clazz) {
    int err;
    hw_module_t* module;
    .......
    err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
    if (err == 0) {
        hw_device_t* device;
        err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
        if (err == 0) {
            gps_device_t* gps_device = (gps_device_t *)device;
            sGpsInterface = gps_device->get_gps_interface(gps_device);
        }
    }
    if (sGpsInterface) {
        ......
    }
}

(5) sGpsInterface 接口:

static const GpsInterface sLocEngInterface =
{
   sizeof(GpsInterface),
   loc_init,
   loc_start,
   loc_stop,
   loc_cleanup,
   loc_inject_time,
   loc_inject_location,
   loc_delete_aiding_data,
   loc_set_position_mode,
   loc_get_extension
};

(6) Others interface:

具体实现请参考:/LINUX/android/hardware/qcom/gps/loc_api/libloc_api_50001/loc.cpp

if (sGpsInterface) {
    sGpsXtraInterface =
        (const GpsXtraInterface*)sGpsInterface->get_extension(GPS_XTRA_INTERFACE);
    sAGpsInterface =
        (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE);
    sGpsNiInterface =
        (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE);
    sGpsDebugInterface =
        (const GpsDebugInterface*)sGpsInterface->get_extension(GPS_DEBUG_INTERFACE);
    sAGpsRilInterface =
        (const AGpsRilInterface*)sGpsInterface->get_extension(AGPS_RIL_INTERFACE);
    sGpsGeofencingInterface =
        (const GpsGeofencingInterface*)sGpsInterface->get_extension(GPS_GEOFENCING_INTERFACE);
    sGpsMeasurementInterface =
        (const GpsMeasurementInterface*)sGpsInterface->get_extension(GPS_MEASUREMENT_INTERFACE);
    sGpsNavigationMessageInterface =
        (const GpsNavigationMessageInterface*)sGpsInterface->get_extension(
                GPS_NAVIGATION_MESSAGE_INTERFACE);
    sGnssConfigurationInterface =
        (const GnssConfigurationInterface*)sGpsInterface->get_extension(
                GNSS_CONFIGURATION_INTERFACE);
}

(7) GPS framework 回调函数库:

 GpsCallbacks sGpsCallbacks = {
    sizeof(GpsCallbacks),
    location_callback,
    status_callback,
    sv_status_callback,
    nmea_callback,
    set_capabilities_callback,
    acquire_wakelock_callback,
    release_wakelock_callback,
    create_thread_callback,
    request_utc_time_callback,
    set_system_info_callback,
    gnss_sv_status_callback,
};

GpsXtraCallbacks sGpsXtraCallbacks = {
    xtra_download_request_callback,
    create_thread_callback,
};

AGpsCallbacks sAGpsCallbacks = {
    agps_status_callback,
    create_thread_callback,
};

GpsNiCallbacks sGpsNiCallbacks = {
    gps_ni_notify_callback,
    create_thread_callback,
};

AGpsRilCallbacks sAGpsRilCallbacks = {
    agps_request_set_id,
    agps_request_ref_location,
    create_thread_callback,
};

......

2 GPS启动流程

(1) GnssLocationProvider.java里处理GPS启动,关闭请求

@Override    public void handleMessage(Message msg) {
    int message = msg.what;
    switch (message) {
        case ENABLE:
            if (msg.arg1 == 1) {
                handleEnable();
            } else {
                handleDisable();
            }
            break;
    ......
    }
}

(2) handleEnable里启动native层初始化动作

private void handleEnable() {
    if (DEBUG) Log.d(TAG, "handleEnable");
    synchronized (mLock) {
        if (mEnabled) return;
        mEnabled = true;
    }
    boolean enabled = native_init();
    if (enabled) {
        mSupportsXtra = true;
    } else {
        synchronized (mLock) {
            mEnabled = false;
        }
        Log.w(TAG, "Failed to enable location provider");
    }
}

(3) GPS native初始化,即GnssLocationProvider初始化过程:

static jboolean android_location_GnssLocationProvider_init(JNIEnv* env, jobject obj){
    // this must be set before calling into the HAL library
    if (!mCallbacksObj)
        mCallbacksObj = env->NewGlobalRef(obj);

    // fail if the main interface fails to initialize
    if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
        return JNI_FALSE;

    // if XTRA initialization fails we will disable it by sGpsXtraInterface to NULL,
    // but continue to allow the rest of the GPS interface to work.
    if (sGpsXtraInterface && sGpsXtraInterface->init(&sGpsXtraCallbacks) != 0)
        sGpsXtraInterface = NULL;
    ......
    return JNI_TRUE;
}

(4) Loc_init, 设置framework层回调函数,初始化loc eng,并通过modem接口上电:

static int loc_init(GpsCallbacks* callbacks){
    int retVal = -1;
    ENTRY_LOG();
    LOC_API_ADAPTER_EVENT_MASK_T event;
    ......
    LocCallbacks clientCallbacks = {local_loc_cb, /* location_cb */
                                    callbacks->status_cb, /* status_cb */
                                    local_sv_cb, /* sv_status_cb */
                                    callbacks->nmea_cb, /* nmea_cb */
                                    callbacks->set_capabilities_cb, /* set_capabilities_cb */
                                    callbacks->acquire_wakelock_cb, /* acquire_wakelock_cb */
                                    callbacks->release_wakelock_cb, /* release_wakelock_cb */
                                    callbacks->create_thread_cb, /* create_thread_cb */
                                    NULL, /* location_ext_parser */
                                    NULL, /* sv_ext_parser */
                                    callbacks->request_utc_time_cb, /* request_utc_time_cb */
                                    callbacks->set_system_info_cb, /* set_system_info_cb */
                                    callbacks->gnss_sv_status_cb, /* gnss_sv_status_cb */
                                    };

    gps_loc_cb = callbacks->location_cb;
    gps_sv_cb = callbacks->sv_status_cb;

    retVal = loc_eng_init(loc_afw_data, &clientCallbacks, event, NULL);
    ......

    if(retVal) {
        LOC_LOGE("loc_eng_init() fail!");
        goto err;
    }

    loc_afw_data.adapter->setPowerVoteRight(loc_get_target() == TARGET_QCA1530);
    loc_afw_data.adapter->setPowerVote(true);

    LOC_LOGD("loc_eng_init() success!");

err:
    EXIT_LOG(%d, retVal);
    return retVal;
}

(5) LocEngInit及msgTask初始化流程:

qcom android GPS 启动流程

相关标签: android gps