2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "GpsLocationProvider"
23 #include "hardware/hardware.h"
24 #include "hardware/gps.h"
25 #include "hardware_legacy/power.h"
26 #include "utils/Log.h"
27 #include "utils/misc.h"
28 #include "android_runtime/AndroidRuntime.h"
33 static jobject mCallbacksObj = NULL;
35 static jmethodID method_reportLocation;
36 static jmethodID method_reportStatus;
37 static jmethodID method_reportSvStatus;
38 static jmethodID method_reportAGpsStatus;
39 static jmethodID method_reportNmea;
40 static jmethodID method_setEngineCapabilities;
41 static jmethodID method_xtraDownloadRequest;
42 static jmethodID method_reportNiNotification;
43 static jmethodID method_requestRefLocation;
44 static jmethodID method_requestSetID;
45 static jmethodID method_requestUtcTime;
47 static const GpsInterface* sGpsInterface = NULL;
48 static const GpsXtraInterface* sGpsXtraInterface = NULL;
49 static const AGpsInterface* sAGpsInterface = NULL;
50 static const GpsNiInterface* sGpsNiInterface = NULL;
51 static const GpsDebugInterface* sGpsDebugInterface = NULL;
52 static const AGpsRilInterface* sAGpsRilInterface = NULL;
54 // temporary storage for GPS callbacks
55 static GpsSvStatus sGpsSvStatus;
56 static const char* sNmeaString;
57 static int sNmeaStringLength;
59 #define WAKE_LOCK_NAME "GPS"
63 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
64 if (env->ExceptionCheck()) {
65 ALOGE("An exception was thrown by callback '%s'.", methodName);
67 env->ExceptionClear();
71 static void location_callback(GpsLocation* location)
73 JNIEnv* env = AndroidRuntime::getJNIEnv();
74 env->CallVoidMethod(mCallbacksObj, method_reportLocation, location->flags,
75 (jdouble)location->latitude, (jdouble)location->longitude,
76 (jdouble)location->altitude,
77 (jfloat)location->speed, (jfloat)location->bearing,
78 (jfloat)location->accuracy, (jlong)location->timestamp);
79 checkAndClearExceptionFromCallback(env, __FUNCTION__);
82 static void status_callback(GpsStatus* status)
84 JNIEnv* env = AndroidRuntime::getJNIEnv();
85 env->CallVoidMethod(mCallbacksObj, method_reportStatus, status->status);
86 checkAndClearExceptionFromCallback(env, __FUNCTION__);
89 static void sv_status_callback(GpsSvStatus* sv_status)
91 JNIEnv* env = AndroidRuntime::getJNIEnv();
92 memcpy(&sGpsSvStatus, sv_status, sizeof(sGpsSvStatus));
93 env->CallVoidMethod(mCallbacksObj, method_reportSvStatus);
94 checkAndClearExceptionFromCallback(env, __FUNCTION__);
97 static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length)
99 JNIEnv* env = AndroidRuntime::getJNIEnv();
100 // The Java code will call back to read these values
101 // We do this to avoid creating unnecessary String objects
103 sNmeaStringLength = length;
104 env->CallVoidMethod(mCallbacksObj, method_reportNmea, timestamp);
105 checkAndClearExceptionFromCallback(env, __FUNCTION__);
108 static void set_capabilities_callback(uint32_t capabilities)
110 ALOGD("set_capabilities_callback: %ld\n", capabilities);
111 JNIEnv* env = AndroidRuntime::getJNIEnv();
112 env->CallVoidMethod(mCallbacksObj, method_setEngineCapabilities, capabilities);
113 checkAndClearExceptionFromCallback(env, __FUNCTION__);
116 static void acquire_wakelock_callback()
118 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
121 static void release_wakelock_callback()
123 release_wake_lock(WAKE_LOCK_NAME);
126 static void request_utc_time_callback()
128 JNIEnv* env = AndroidRuntime::getJNIEnv();
129 env->CallVoidMethod(mCallbacksObj, method_requestUtcTime);
130 checkAndClearExceptionFromCallback(env, __FUNCTION__);
133 static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg)
135 return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg);
138 GpsCallbacks sGpsCallbacks = {
139 sizeof(GpsCallbacks),
144 set_capabilities_callback,
145 acquire_wakelock_callback,
146 release_wakelock_callback,
147 create_thread_callback,
148 request_utc_time_callback,
151 static void xtra_download_request_callback()
153 JNIEnv* env = AndroidRuntime::getJNIEnv();
154 env->CallVoidMethod(mCallbacksObj, method_xtraDownloadRequest);
155 checkAndClearExceptionFromCallback(env, __FUNCTION__);
158 GpsXtraCallbacks sGpsXtraCallbacks = {
159 xtra_download_request_callback,
160 create_thread_callback,
163 static void agps_status_callback(AGpsStatus* agps_status)
165 JNIEnv* env = AndroidRuntime::getJNIEnv();
168 // ipaddr field was not included in original AGpsStatus
169 if (agps_status->size >= sizeof(AGpsStatus))
170 ipaddr = agps_status->ipaddr;
173 env->CallVoidMethod(mCallbacksObj, method_reportAGpsStatus,
174 agps_status->type, agps_status->status, ipaddr);
175 checkAndClearExceptionFromCallback(env, __FUNCTION__);
178 AGpsCallbacks sAGpsCallbacks = {
179 agps_status_callback,
180 create_thread_callback,
183 static void gps_ni_notify_callback(GpsNiNotification *notification)
185 ALOGD("gps_ni_notify_callback\n");
186 JNIEnv* env = AndroidRuntime::getJNIEnv();
187 jstring requestor_id = env->NewStringUTF(notification->requestor_id);
188 jstring text = env->NewStringUTF(notification->text);
189 jstring extras = env->NewStringUTF(notification->extras);
191 if (requestor_id && text && extras) {
192 env->CallVoidMethod(mCallbacksObj, method_reportNiNotification,
193 notification->notification_id, notification->ni_type,
194 notification->notify_flags, notification->timeout,
195 notification->default_response, requestor_id, text,
196 notification->requestor_id_encoding,
197 notification->text_encoding, extras);
199 ALOGE("out of memory in gps_ni_notify_callback\n");
203 env->DeleteLocalRef(requestor_id);
205 env->DeleteLocalRef(text);
207 env->DeleteLocalRef(extras);
208 checkAndClearExceptionFromCallback(env, __FUNCTION__);
211 GpsNiCallbacks sGpsNiCallbacks = {
212 gps_ni_notify_callback,
213 create_thread_callback,
216 static void agps_request_set_id(uint32_t flags)
218 JNIEnv* env = AndroidRuntime::getJNIEnv();
219 env->CallVoidMethod(mCallbacksObj, method_requestSetID, flags);
220 checkAndClearExceptionFromCallback(env, __FUNCTION__);
223 static void agps_request_ref_location(uint32_t flags)
225 JNIEnv* env = AndroidRuntime::getJNIEnv();
226 env->CallVoidMethod(mCallbacksObj, method_requestRefLocation, flags);
227 checkAndClearExceptionFromCallback(env, __FUNCTION__);
230 AGpsRilCallbacks sAGpsRilCallbacks = {
232 agps_request_ref_location,
233 create_thread_callback,
236 static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz) {
240 method_reportLocation = env->GetMethodID(clazz, "reportLocation", "(IDDDFFFJ)V");
241 method_reportStatus = env->GetMethodID(clazz, "reportStatus", "(I)V");
242 method_reportSvStatus = env->GetMethodID(clazz, "reportSvStatus", "()V");
243 method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(III)V");
244 method_reportNmea = env->GetMethodID(clazz, "reportNmea", "(J)V");
245 method_setEngineCapabilities = env->GetMethodID(clazz, "setEngineCapabilities", "(I)V");
246 method_xtraDownloadRequest = env->GetMethodID(clazz, "xtraDownloadRequest", "()V");
247 method_reportNiNotification = env->GetMethodID(clazz, "reportNiNotification",
248 "(IIIIILjava/lang/String;Ljava/lang/String;IILjava/lang/String;)V");
249 method_requestRefLocation = env->GetMethodID(clazz,"requestRefLocation","(I)V");
250 method_requestSetID = env->GetMethodID(clazz,"requestSetID","(I)V");
251 method_requestUtcTime = env->GetMethodID(clazz,"requestUtcTime","()V");
253 err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
256 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
258 gps_device_t* gps_device = (gps_device_t *)device;
259 sGpsInterface = gps_device->get_gps_interface(gps_device);
264 (const GpsXtraInterface*)sGpsInterface->get_extension(GPS_XTRA_INTERFACE);
266 (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE);
268 (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE);
270 (const GpsDebugInterface*)sGpsInterface->get_extension(GPS_DEBUG_INTERFACE);
272 (const AGpsRilInterface*)sGpsInterface->get_extension(AGPS_RIL_INTERFACE);
276 static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) {
277 return (sGpsInterface != NULL);
280 static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
282 // this must be set before calling into the HAL library
284 mCallbacksObj = env->NewGlobalRef(obj);
286 // fail if the main interface fails to initialize
287 if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
290 // if XTRA initialization fails we will disable it by sGpsXtraInterface to null,
291 // but continue to allow the rest of the GPS interface to work.
292 if (sGpsXtraInterface && sGpsXtraInterface->init(&sGpsXtraCallbacks) != 0)
293 sGpsXtraInterface = NULL;
295 sAGpsInterface->init(&sAGpsCallbacks);
297 sGpsNiInterface->init(&sGpsNiCallbacks);
298 if (sAGpsRilInterface)
299 sAGpsRilInterface->init(&sAGpsRilCallbacks);
304 static void android_location_GpsLocationProvider_cleanup(JNIEnv* env, jobject obj)
307 sGpsInterface->cleanup();
310 static jboolean android_location_GpsLocationProvider_set_position_mode(JNIEnv* env, jobject obj,
311 jint mode, jint recurrence, jint min_interval, jint preferred_accuracy, jint preferred_time)
314 return (sGpsInterface->set_position_mode(mode, recurrence, min_interval, preferred_accuracy,
315 preferred_time) == 0);
320 static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)
323 return (sGpsInterface->start() == 0);
328 static jboolean android_location_GpsLocationProvider_stop(JNIEnv* env, jobject obj)
331 return (sGpsInterface->stop() == 0);
336 static void android_location_GpsLocationProvider_delete_aiding_data(JNIEnv* env, jobject obj, jint flags)
339 sGpsInterface->delete_aiding_data(flags);
342 static jint android_location_GpsLocationProvider_read_sv_status(JNIEnv* env, jobject obj,
343 jintArray prnArray, jfloatArray snrArray, jfloatArray elevArray, jfloatArray azumArray,
346 // this should only be called from within a call to reportSvStatus
348 jint* prns = env->GetIntArrayElements(prnArray, 0);
349 jfloat* snrs = env->GetFloatArrayElements(snrArray, 0);
350 jfloat* elev = env->GetFloatArrayElements(elevArray, 0);
351 jfloat* azim = env->GetFloatArrayElements(azumArray, 0);
352 jint* mask = env->GetIntArrayElements(maskArray, 0);
354 int num_svs = sGpsSvStatus.num_svs;
355 for (int i = 0; i < num_svs; i++) {
356 prns[i] = sGpsSvStatus.sv_list[i].prn;
357 snrs[i] = sGpsSvStatus.sv_list[i].snr;
358 elev[i] = sGpsSvStatus.sv_list[i].elevation;
359 azim[i] = sGpsSvStatus.sv_list[i].azimuth;
361 mask[0] = sGpsSvStatus.ephemeris_mask;
362 mask[1] = sGpsSvStatus.almanac_mask;
363 mask[2] = sGpsSvStatus.used_in_fix_mask;
365 env->ReleaseIntArrayElements(prnArray, prns, 0);
366 env->ReleaseFloatArrayElements(snrArray, snrs, 0);
367 env->ReleaseFloatArrayElements(elevArray, elev, 0);
368 env->ReleaseFloatArrayElements(azumArray, azim, 0);
369 env->ReleaseIntArrayElements(maskArray, mask, 0);
373 static void android_location_GpsLocationProvider_agps_set_reference_location_cellid(JNIEnv* env,
374 jobject obj, jint type, jint mcc, jint mnc, jint lac, jint cid)
376 AGpsRefLocation location;
378 if (!sAGpsRilInterface) {
379 ALOGE("no AGPS RIL interface in agps_set_reference_location_cellid");
384 case AGPS_REF_LOCATION_TYPE_GSM_CELLID:
385 case AGPS_REF_LOCATION_TYPE_UMTS_CELLID:
386 location.type = type;
387 location.u.cellID.mcc = mcc;
388 location.u.cellID.mnc = mnc;
389 location.u.cellID.lac = lac;
390 location.u.cellID.cid = cid;
393 ALOGE("Neither a GSM nor a UMTS cellid (%s:%d).",__FUNCTION__,__LINE__);
397 sAGpsRilInterface->set_ref_location(&location, sizeof(location));
400 static void android_location_GpsLocationProvider_agps_send_ni_message(JNIEnv* env,
401 jobject obj, jbyteArray ni_msg, jint size)
405 if (!sAGpsRilInterface) {
406 ALOGE("no AGPS RIL interface in send_ni_message");
412 jbyte* b = env->GetByteArrayElements(ni_msg, 0);
413 sAGpsRilInterface->ni_message((uint8_t *)b,sz);
414 env->ReleaseByteArrayElements(ni_msg,b,0);
417 static void android_location_GpsLocationProvider_agps_set_id(JNIEnv *env,
418 jobject obj, jint type, jstring setid_string)
420 if (!sAGpsRilInterface) {
421 ALOGE("no AGPS RIL interface in agps_set_id");
425 const char *setid = env->GetStringUTFChars(setid_string, NULL);
426 sAGpsRilInterface->set_set_id(type, setid);
427 env->ReleaseStringUTFChars(setid_string, setid);
430 static jint android_location_GpsLocationProvider_read_nmea(JNIEnv* env, jobject obj,
431 jbyteArray nmeaArray, jint buffer_size)
433 // this should only be called from within a call to reportNmea
434 jbyte* nmea = (jbyte *)env->GetPrimitiveArrayCritical(nmeaArray, 0);
435 int length = sNmeaStringLength;
436 if (length > buffer_size)
437 length = buffer_size;
438 memcpy(nmea, sNmeaString, length);
439 env->ReleasePrimitiveArrayCritical(nmeaArray, nmea, JNI_ABORT);
443 static void android_location_GpsLocationProvider_inject_time(JNIEnv* env, jobject obj,
444 jlong time, jlong timeReference, jint uncertainty)
447 sGpsInterface->inject_time(time, timeReference, uncertainty);
450 static void android_location_GpsLocationProvider_inject_location(JNIEnv* env, jobject obj,
451 jdouble latitude, jdouble longitude, jfloat accuracy)
454 sGpsInterface->inject_location(latitude, longitude, accuracy);
457 static jboolean android_location_GpsLocationProvider_supports_xtra(JNIEnv* env, jobject obj)
459 return (sGpsXtraInterface != NULL);
462 static void android_location_GpsLocationProvider_inject_xtra_data(JNIEnv* env, jobject obj,
463 jbyteArray data, jint length)
465 if (!sGpsXtraInterface) {
466 ALOGE("no XTRA interface in inject_xtra_data");
470 jbyte* bytes = (jbyte *)env->GetPrimitiveArrayCritical(data, 0);
471 sGpsXtraInterface->inject_xtra_data((char *)bytes, length);
472 env->ReleasePrimitiveArrayCritical(data, bytes, JNI_ABORT);
475 static void android_location_GpsLocationProvider_agps_data_conn_open(JNIEnv* env, jobject obj, jstring apn)
477 if (!sAGpsInterface) {
478 ALOGE("no AGPS interface in agps_data_conn_open");
482 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
485 const char *apnStr = env->GetStringUTFChars(apn, NULL);
486 sAGpsInterface->data_conn_open(apnStr);
487 env->ReleaseStringUTFChars(apn, apnStr);
490 static void android_location_GpsLocationProvider_agps_data_conn_closed(JNIEnv* env, jobject obj)
492 if (!sAGpsInterface) {
493 ALOGE("no AGPS interface in agps_data_conn_open");
496 sAGpsInterface->data_conn_closed();
499 static void android_location_GpsLocationProvider_agps_data_conn_failed(JNIEnv* env, jobject obj)
501 if (!sAGpsInterface) {
502 ALOGE("no AGPS interface in agps_data_conn_open");
505 sAGpsInterface->data_conn_failed();
508 static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jobject obj,
509 jint type, jstring hostname, jint port)
511 if (!sAGpsInterface) {
512 ALOGE("no AGPS interface in agps_data_conn_open");
515 const char *c_hostname = env->GetStringUTFChars(hostname, NULL);
516 sAGpsInterface->set_server(type, c_hostname, port);
517 env->ReleaseStringUTFChars(hostname, c_hostname);
520 static void android_location_GpsLocationProvider_send_ni_response(JNIEnv* env, jobject obj,
521 jint notifId, jint response)
523 if (!sGpsNiInterface) {
524 ALOGE("no NI interface in send_ni_response");
528 sGpsNiInterface->respond(notifId, response);
531 static jstring android_location_GpsLocationProvider_get_internal_state(JNIEnv* env, jobject obj)
533 jstring result = NULL;
534 if (sGpsDebugInterface) {
535 const size_t maxLength = 2047;
536 char buffer[maxLength+1];
537 size_t length = sGpsDebugInterface->get_internal_state(buffer, maxLength);
538 if (length > maxLength) length = maxLength;
540 result = env->NewStringUTF(buffer);
545 static void android_location_GpsLocationProvider_update_network_state(JNIEnv* env, jobject obj,
546 jboolean connected, int type, jboolean roaming, jboolean available, jstring extraInfo, jstring apn)
549 if (sAGpsRilInterface && sAGpsRilInterface->update_network_state) {
551 const char *extraInfoStr = env->GetStringUTFChars(extraInfo, NULL);
552 sAGpsRilInterface->update_network_state(connected, type, roaming, extraInfoStr);
553 env->ReleaseStringUTFChars(extraInfo, extraInfoStr);
555 sAGpsRilInterface->update_network_state(connected, type, roaming, NULL);
558 // update_network_availability callback was not included in original AGpsRilInterface
559 if (sAGpsRilInterface->size >= sizeof(AGpsRilInterface)
560 && sAGpsRilInterface->update_network_availability) {
561 const char *c_apn = env->GetStringUTFChars(apn, NULL);
562 sAGpsRilInterface->update_network_availability(available, c_apn);
563 env->ReleaseStringUTFChars(apn, c_apn);
568 static JNINativeMethod sMethods[] = {
569 /* name, signature, funcPtr */
570 {"class_init_native", "()V", (void *)android_location_GpsLocationProvider_class_init_native},
571 {"native_is_supported", "()Z", (void*)android_location_GpsLocationProvider_is_supported},
572 {"native_init", "()Z", (void*)android_location_GpsLocationProvider_init},
573 {"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup},
574 {"native_set_position_mode", "(IIIII)Z", (void*)android_location_GpsLocationProvider_set_position_mode},
575 {"native_start", "()Z", (void*)android_location_GpsLocationProvider_start},
576 {"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop},
577 {"native_delete_aiding_data", "(I)V", (void*)android_location_GpsLocationProvider_delete_aiding_data},
578 {"native_read_sv_status", "([I[F[F[F[I)I", (void*)android_location_GpsLocationProvider_read_sv_status},
579 {"native_read_nmea", "([BI)I", (void*)android_location_GpsLocationProvider_read_nmea},
580 {"native_inject_time", "(JJI)V", (void*)android_location_GpsLocationProvider_inject_time},
581 {"native_inject_location", "(DDF)V", (void*)android_location_GpsLocationProvider_inject_location},
582 {"native_supports_xtra", "()Z", (void*)android_location_GpsLocationProvider_supports_xtra},
583 {"native_inject_xtra_data", "([BI)V", (void*)android_location_GpsLocationProvider_inject_xtra_data},
584 {"native_agps_data_conn_open", "(Ljava/lang/String;)V", (void*)android_location_GpsLocationProvider_agps_data_conn_open},
585 {"native_agps_data_conn_closed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_closed},
586 {"native_agps_data_conn_failed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_failed},
587 {"native_agps_set_id","(ILjava/lang/String;)V",(void*)android_location_GpsLocationProvider_agps_set_id},
588 {"native_agps_set_ref_location_cellid","(IIIII)V",(void*)android_location_GpsLocationProvider_agps_set_reference_location_cellid},
589 {"native_set_agps_server", "(ILjava/lang/String;I)V", (void*)android_location_GpsLocationProvider_set_agps_server},
590 {"native_send_ni_response", "(II)V", (void*)android_location_GpsLocationProvider_send_ni_response},
591 {"native_agps_ni_message", "([BI)V", (void *)android_location_GpsLocationProvider_agps_send_ni_message},
592 {"native_get_internal_state", "()Ljava/lang/String;", (void*)android_location_GpsLocationProvider_get_internal_state},
593 {"native_update_network_state", "(ZIZZLjava/lang/String;Ljava/lang/String;)V", (void*)android_location_GpsLocationProvider_update_network_state },
596 int register_android_server_location_GpsLocationProvider(JNIEnv* env)
598 return jniRegisterNativeMethods(env, "com/android/server/location/GpsLocationProvider", sMethods, NELEM(sMethods));
601 } /* namespace android */