JNI層調(diào)用在 frameworks\base\services\core\jni\com_android_server_location_GnssLocationProvider.cpp
我們跟著 class_init_native 方法看初始化
#目錄位置 frameworks\base\services\core\jni\com_android_server_location_GnssLocationProvider.cpp
static void android_location_GnssLocationProvider_class_init_native(JNIEnv* env, jclass clazz) {
gnssHal_V1_1 = IGnss_V1_1::getService();
if (gnssHal_V1_1 == nullptr) {
ALOGD("gnssHal 1.1 was null, trying 1.0");
gnssHal = IGnss_V1_0::getService();
} else {
gnssHal = gnssHal_V1_1;
}
}
在初始化中我們看到這里進(jìn)行了GPS 的HADL層的初始化阵赠,使用的GNSS版本是 android/hardware/gnss/1.0/IGnss.h 1.0版本乖篷,接下來(lái)看 native_start 開(kāi)始定位的方法
static jboolean android_location_GnssLocationProvider_start(JNIEnv* /* env */, jobject /* obj */) {
if (gnssHal != nullptr) {
#這里調(diào)用了HADL層的start開(kāi)始定位方法
auto result = gnssHal->start();
if (!result.isOk()) {
return JNI_FALSE;
} else {
return result;
}
} else {
return JNI_FALSE;
}
}
接下來(lái),我們進(jìn)入到GNSS的HADL層代碼位置在 hardware\interfaces\gnss\1.0险胰,這里的 IGnss.hal是定義HIDL服務(wù)的方法名字志衣,具體的實(shí)現(xiàn)是在default目錄下 Gnss.cpp 文件屯援,我們看這里的start方法做了什么
IGnss* HIDL_FETCH_IGnss(const char* /* hal */) {
hw_module_t* module;
IGnss* iface = nullptr;
#調(diào)用了HAL層獲取廠商的so操作拿到操作GPS的對(duì)象
int 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對(duì)象進(jìn)行操作
iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
} else {
ALOGE("gnssDevice open %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
}
} else {
ALOGE("gnss hw_get_module %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
}
return iface;
}
調(diào)用了 mGnssIface的start方法就是 gps_device_t 定位開(kāi)始的方法
Return<bool> Gnss::start() {
if (mGnssIface == nullptr) {
ALOGE("%s: Gnss interface is unavailable", __func__);
return false;
}
return (mGnssIface->start() == 0);
}
GnnssHal層回調(diào)設(shè)置在JNI層中
https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/jni/com_android_server_location_GnssLocationProvider.cpp;l=1968?q=com_android_server_location_GnssLocationProvider&ss=android%2Fplatform%2Fsuperproject
這里調(diào)用了Gnns Hal 層的 setCallback(const sp<IGnssCallback>& callback) 回調(diào),來(lái)接受驅(qū)動(dòng)的回調(diào)
到此全部從應(yīng)用層到底層全部走通