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;
46 static const GpsInterface* sGpsInterface = NULL;
47 static const GpsXtraInterface* sGpsXtraInterface = NULL;
48 static const AGpsInterface* sAGpsInterface = NULL;
49 static const GpsNiInterface* sGpsNiInterface = NULL;
50 static const GpsDebugInterface* sGpsDebugInterface = NULL;
51 static const AGpsRilInterface* sAGpsRilInterface = NULL;
53 // temporary storage for GPS callbacks
54 static GpsSvStatus sGpsSvStatus;
55 static const char* sNmeaString;
56 static int sNmeaStringLength;
58 #define WAKE_LOCK_NAME "GPS"
62 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
63 if (env->ExceptionCheck()) {
64 LOGE("An exception was thrown by callback '%s'.", methodName);
66 env->ExceptionClear();
70 static void location_callback(GpsLocation* location)
72 JNIEnv* env = AndroidRuntime::getJNIEnv();
73 env->CallVoidMethod(mCallbacksObj, method_reportLocation, location->flags,
74 (jdouble)location->latitude, (jdouble)location->longitude,
75 (jdouble)location->altitude,
76 (jfloat)location->speed, (jfloat)location->bearing,
77 (jfloat)location->accuracy, (jlong)location->timestamp);
78 checkAndClearExceptionFromCallback(env, __FUNCTION__);
81 static void status_callback(GpsStatus* status)
83 JNIEnv* env = AndroidRuntime::getJNIEnv();
84 env->CallVoidMethod(mCallbacksObj, method_reportStatus, status->status);
85 checkAndClearExceptionFromCallback(env, __FUNCTION__);
88 static void sv_status_callback(GpsSvStatus* sv_status)
90 JNIEnv* env = AndroidRuntime::getJNIEnv();
91 memcpy(&sGpsSvStatus, sv_status, sizeof(sGpsSvStatus));
92 env->CallVoidMethod(mCallbacksObj, method_reportSvStatus);
93 checkAndClearExceptionFromCallback(env, __FUNCTION__);
96 static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length)
98 JNIEnv* env = AndroidRuntime::getJNIEnv();
99 // The Java code will call back to read these values
100 // We do this to avoid creating unnecessary String objects
102 sNmeaStringLength = length;
103 env->CallVoidMethod(mCallbacksObj, method_reportNmea, timestamp);
104 checkAndClearExceptionFromCallback(env, __FUNCTION__);
107 static void set_capabilities_callback(uint32_t capabilities)
109 LOGD("set_capabilities_callback: %ld\n", capabilities);
110 JNIEnv* env = AndroidRuntime::getJNIEnv();
111 env->CallVoidMethod(mCallbacksObj, method_setEngineCapabilities, capabilities);
112 checkAndClearExceptionFromCallback(env, __FUNCTION__);
115 static void acquire_wakelock_callback()
117 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
120 static void release_wakelock_callback()
122 release_wake_lock(WAKE_LOCK_NAME);
125 static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg)
127 return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg);
130 GpsCallbacks sGpsCallbacks = {
131 sizeof(GpsCallbacks),
136 set_capabilities_callback,
137 acquire_wakelock_callback,
138 release_wakelock_callback,
139 create_thread_callback,
142 static void xtra_download_request_callback()
144 JNIEnv* env = AndroidRuntime::getJNIEnv();
145 env->CallVoidMethod(mCallbacksObj, method_xtraDownloadRequest);
146 checkAndClearExceptionFromCallback(env, __FUNCTION__);
149 GpsXtraCallbacks sGpsXtraCallbacks = {
150 xtra_download_request_callback,
151 create_thread_callback,
154 static void agps_status_callback(AGpsStatus* agps_status)
156 JNIEnv* env = AndroidRuntime::getJNIEnv();
157 env->CallVoidMethod(mCallbacksObj, method_reportAGpsStatus,
158 agps_status->type, agps_status->status);
159 checkAndClearExceptionFromCallback(env, __FUNCTION__);
162 AGpsCallbacks sAGpsCallbacks = {
163 agps_status_callback,
164 create_thread_callback,
167 static void gps_ni_notify_callback(GpsNiNotification *notification)
169 LOGD("gps_ni_notify_callback\n");
170 JNIEnv* env = AndroidRuntime::getJNIEnv();
171 jstring requestor_id = env->NewStringUTF(notification->requestor_id);
172 jstring text = env->NewStringUTF(notification->text);
173 jstring extras = env->NewStringUTF(notification->extras);
175 if (requestor_id && text && extras) {
176 env->CallVoidMethod(mCallbacksObj, method_reportNiNotification,
177 notification->notification_id, notification->ni_type,
178 notification->notify_flags, notification->timeout,
179 notification->default_response, requestor_id, text,
180 notification->requestor_id_encoding,
181 notification->text_encoding, extras);
183 LOGE("out of memory in gps_ni_notify_callback\n");
187 env->DeleteLocalRef(requestor_id);
189 env->DeleteLocalRef(text);
191 env->DeleteLocalRef(extras);
192 checkAndClearExceptionFromCallback(env, __FUNCTION__);
195 GpsNiCallbacks sGpsNiCallbacks = {
196 gps_ni_notify_callback,
197 create_thread_callback,
200 static void agps_request_set_id(uint32_t flags)
202 JNIEnv* env = AndroidRuntime::getJNIEnv();
203 env->CallVoidMethod(mCallbacksObj, method_requestSetID, flags);
204 checkAndClearExceptionFromCallback(env, __FUNCTION__);
207 static void agps_request_ref_location(uint32_t flags)
209 JNIEnv* env = AndroidRuntime::getJNIEnv();
210 env->CallVoidMethod(mCallbacksObj, method_requestRefLocation, flags);
211 checkAndClearExceptionFromCallback(env, __FUNCTION__);
214 AGpsRilCallbacks sAGpsRilCallbacks = {
216 agps_request_ref_location,
217 create_thread_callback,
220 static const GpsInterface* get_gps_interface() {
223 const GpsInterface* interface = NULL;
225 err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
228 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
230 gps_device_t* gps_device = (gps_device_t *)device;
231 interface = gps_device->get_gps_interface(gps_device);
238 static const GpsInterface* GetGpsInterface(JNIEnv* env, jobject obj) {
239 // this must be set before calling into the HAL library
241 mCallbacksObj = env->NewGlobalRef(obj);
243 if (!sGpsInterface) {
244 sGpsInterface = get_gps_interface();
245 if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0) {
246 sGpsInterface = NULL;
250 return sGpsInterface;
253 static const AGpsInterface* GetAGpsInterface(JNIEnv* env, jobject obj)
255 const GpsInterface* interface = GetGpsInterface(env, obj);
259 if (!sAGpsInterface) {
260 sAGpsInterface = (const AGpsInterface*)interface->get_extension(AGPS_INTERFACE);
262 sAGpsInterface->init(&sAGpsCallbacks);
264 return sAGpsInterface;
267 static const GpsNiInterface* GetNiInterface(JNIEnv* env, jobject obj)
269 const GpsInterface* interface = GetGpsInterface(env, obj);
273 if (!sGpsNiInterface) {
274 sGpsNiInterface = (const GpsNiInterface*)interface->get_extension(GPS_NI_INTERFACE);
276 sGpsNiInterface->init(&sGpsNiCallbacks);
278 return sGpsNiInterface;
281 static const AGpsRilInterface* GetAGpsRilInterface(JNIEnv* env, jobject obj)
283 const GpsInterface* interface = GetGpsInterface(env, obj);
287 if (!sAGpsRilInterface) {
288 sAGpsRilInterface = (const AGpsRilInterface*)interface->get_extension(AGPS_RIL_INTERFACE);
289 if (sAGpsRilInterface)
290 sAGpsRilInterface->init(&sAGpsRilCallbacks);
292 return sAGpsRilInterface;
295 static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz) {
296 method_reportLocation = env->GetMethodID(clazz, "reportLocation", "(IDDDFFFJ)V");
297 method_reportStatus = env->GetMethodID(clazz, "reportStatus", "(I)V");
298 method_reportSvStatus = env->GetMethodID(clazz, "reportSvStatus", "()V");
299 method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(II)V");
300 method_reportNmea = env->GetMethodID(clazz, "reportNmea", "(J)V");
301 method_setEngineCapabilities = env->GetMethodID(clazz, "setEngineCapabilities", "(I)V");
302 method_xtraDownloadRequest = env->GetMethodID(clazz, "xtraDownloadRequest", "()V");
303 method_reportNiNotification = env->GetMethodID(clazz, "reportNiNotification", "(IIIIILjava/lang/String;Ljava/lang/String;IILjava/lang/String;)V");
304 method_requestRefLocation = env->GetMethodID(clazz,"requestRefLocation","(I)V");
305 method_requestSetID = env->GetMethodID(clazz,"requestSetID","(I)V");
308 static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) {
309 return (sGpsInterface != NULL || get_gps_interface() != NULL);
312 static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
314 const GpsInterface* interface = GetGpsInterface(env, obj);
318 if (!sGpsDebugInterface)
319 sGpsDebugInterface = (const GpsDebugInterface*)interface->get_extension(GPS_DEBUG_INTERFACE);
324 static void android_location_GpsLocationProvider_cleanup(JNIEnv* env, jobject obj)
326 const GpsInterface* interface = GetGpsInterface(env, obj);
328 interface->cleanup();
331 static jboolean android_location_GpsLocationProvider_set_position_mode(JNIEnv* env, jobject obj,
332 jint mode, jint recurrence, jint min_interval, jint preferred_accuracy, jint preferred_time)
334 const GpsInterface* interface = GetGpsInterface(env, obj);
336 return (interface->set_position_mode(mode, recurrence, min_interval, preferred_accuracy,
337 preferred_time) == 0);
342 static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)
344 const GpsInterface* interface = GetGpsInterface(env, obj);
346 return (interface->start() == 0);
351 static jboolean android_location_GpsLocationProvider_stop(JNIEnv* env, jobject obj)
353 const GpsInterface* interface = GetGpsInterface(env, obj);
355 return (interface->stop() == 0);
360 static void android_location_GpsLocationProvider_delete_aiding_data(JNIEnv* env, jobject obj, jint flags)
362 const GpsInterface* interface = GetGpsInterface(env, obj);
364 interface->delete_aiding_data(flags);
367 static jint android_location_GpsLocationProvider_read_sv_status(JNIEnv* env, jobject obj,
368 jintArray prnArray, jfloatArray snrArray, jfloatArray elevArray, jfloatArray azumArray,
371 // this should only be called from within a call to reportSvStatus
373 jint* prns = env->GetIntArrayElements(prnArray, 0);
374 jfloat* snrs = env->GetFloatArrayElements(snrArray, 0);
375 jfloat* elev = env->GetFloatArrayElements(elevArray, 0);
376 jfloat* azim = env->GetFloatArrayElements(azumArray, 0);
377 jint* mask = env->GetIntArrayElements(maskArray, 0);
379 int num_svs = sGpsSvStatus.num_svs;
380 for (int i = 0; i < num_svs; i++) {
381 prns[i] = sGpsSvStatus.sv_list[i].prn;
382 snrs[i] = sGpsSvStatus.sv_list[i].snr;
383 elev[i] = sGpsSvStatus.sv_list[i].elevation;
384 azim[i] = sGpsSvStatus.sv_list[i].azimuth;
386 mask[0] = sGpsSvStatus.ephemeris_mask;
387 mask[1] = sGpsSvStatus.almanac_mask;
388 mask[2] = sGpsSvStatus.used_in_fix_mask;
390 env->ReleaseIntArrayElements(prnArray, prns, 0);
391 env->ReleaseFloatArrayElements(snrArray, snrs, 0);
392 env->ReleaseFloatArrayElements(elevArray, elev, 0);
393 env->ReleaseFloatArrayElements(azumArray, azim, 0);
394 env->ReleaseIntArrayElements(maskArray, mask, 0);
398 static void android_location_GpsLocationProvider_agps_set_reference_location_cellid(JNIEnv* env,
399 jobject obj, jint type, jint mcc, jint mnc, jint lac, jint cid)
401 AGpsRefLocation location;
402 const AGpsRilInterface* interface = GetAGpsRilInterface(env, obj);
404 LOGE("no AGPS RIL interface in agps_set_reference_location_cellid");
409 case AGPS_REF_LOCATION_TYPE_GSM_CELLID:
410 case AGPS_REF_LOCATION_TYPE_UMTS_CELLID:
411 location.type = type;
412 location.u.cellID.mcc = mcc;
413 location.u.cellID.mnc = mnc;
414 location.u.cellID.lac = lac;
415 location.u.cellID.cid = cid;
418 LOGE("Neither a GSM nor a UMTS cellid (%s:%d).",__FUNCTION__,__LINE__);
422 interface->set_ref_location(&location, sizeof(location));
425 static void android_location_GpsLocationProvider_agps_send_ni_message(JNIEnv* env,
426 jobject obj, jbyteArray ni_msg, jint size)
429 const AGpsRilInterface* interface = GetAGpsRilInterface(env, obj);
431 LOGE("no AGPS RIL interface in send_ni_message");
437 jbyte* b = env->GetByteArrayElements(ni_msg, 0);
438 interface->ni_message((uint8_t *)b,sz);
439 env->ReleaseByteArrayElements(ni_msg,b,0);
442 static void android_location_GpsLocationProvider_agps_set_id(JNIEnv *env,
443 jobject obj, jint type, jstring setid_string)
445 const AGpsRilInterface* interface = GetAGpsRilInterface(env, obj);
447 LOGE("no AGPS RIL interface in agps_set_id");
451 const char *setid = env->GetStringUTFChars(setid_string, NULL);
452 interface->set_set_id(type, setid);
453 env->ReleaseStringUTFChars(setid_string, setid);
456 static jint android_location_GpsLocationProvider_read_nmea(JNIEnv* env, jobject obj,
457 jbyteArray nmeaArray, jint buffer_size)
459 // this should only be called from within a call to reportNmea
460 jbyte* nmea = (jbyte *)env->GetPrimitiveArrayCritical(nmeaArray, 0);
461 int length = sNmeaStringLength;
462 if (length > buffer_size)
463 length = buffer_size;
464 memcpy(nmea, sNmeaString, length);
465 env->ReleasePrimitiveArrayCritical(nmeaArray, nmea, JNI_ABORT);
469 static void android_location_GpsLocationProvider_inject_time(JNIEnv* env, jobject obj,
470 jlong time, jlong timeReference, jint uncertainty)
472 const GpsInterface* interface = GetGpsInterface(env, obj);
474 interface->inject_time(time, timeReference, uncertainty);
477 static void android_location_GpsLocationProvider_inject_location(JNIEnv* env, jobject obj,
478 jdouble latitude, jdouble longitude, jfloat accuracy)
480 const GpsInterface* interface = GetGpsInterface(env, obj);
482 interface->inject_location(latitude, longitude, accuracy);
485 static jboolean android_location_GpsLocationProvider_supports_xtra(JNIEnv* env, jobject obj)
487 if (!sGpsXtraInterface) {
488 const GpsInterface* interface = GetGpsInterface(env, obj);
491 sGpsXtraInterface = (const GpsXtraInterface*)interface->get_extension(GPS_XTRA_INTERFACE);
492 if (sGpsXtraInterface) {
493 int result = sGpsXtraInterface->init(&sGpsXtraCallbacks);
495 sGpsXtraInterface = NULL;
500 return (sGpsXtraInterface != NULL);
503 static void android_location_GpsLocationProvider_inject_xtra_data(JNIEnv* env, jobject obj,
504 jbyteArray data, jint length)
506 jbyte* bytes = (jbyte *)env->GetPrimitiveArrayCritical(data, 0);
507 sGpsXtraInterface->inject_xtra_data((char *)bytes, length);
508 env->ReleasePrimitiveArrayCritical(data, bytes, JNI_ABORT);
511 static void android_location_GpsLocationProvider_agps_data_conn_open(JNIEnv* env, jobject obj, jstring apn)
513 const AGpsInterface* interface = GetAGpsInterface(env, obj);
515 LOGE("no AGPS interface in agps_data_conn_open");
519 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
522 const char *apnStr = env->GetStringUTFChars(apn, NULL);
523 interface->data_conn_open(apnStr);
524 env->ReleaseStringUTFChars(apn, apnStr);
527 static void android_location_GpsLocationProvider_agps_data_conn_closed(JNIEnv* env, jobject obj)
529 const AGpsInterface* interface = GetAGpsInterface(env, obj);
531 LOGE("no AGPS interface in agps_data_conn_open");
534 interface->data_conn_closed();
537 static void android_location_GpsLocationProvider_agps_data_conn_failed(JNIEnv* env, jobject obj)
539 const AGpsInterface* interface = GetAGpsInterface(env, obj);
541 LOGE("no AGPS interface in agps_data_conn_open");
544 interface->data_conn_failed();
547 static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jobject obj,
548 jint type, jstring hostname, jint port)
550 const AGpsInterface* interface = GetAGpsInterface(env, obj);
552 LOGE("no AGPS interface in agps_data_conn_open");
555 const char *c_hostname = env->GetStringUTFChars(hostname, NULL);
556 interface->set_server(type, c_hostname, port);
557 env->ReleaseStringUTFChars(hostname, c_hostname);
560 static void android_location_GpsLocationProvider_send_ni_response(JNIEnv* env, jobject obj,
561 jint notifId, jint response)
563 const GpsNiInterface* interface = GetNiInterface(env, obj);
565 LOGE("no NI interface in send_ni_response");
569 interface->respond(notifId, response);
572 static jstring android_location_GpsLocationProvider_get_internal_state(JNIEnv* env, jobject obj)
574 jstring result = NULL;
575 if (sGpsDebugInterface) {
576 const size_t maxLength = 2047;
577 char buffer[maxLength+1];
578 size_t length = sGpsDebugInterface->get_internal_state(buffer, maxLength);
579 if (length > maxLength) length = maxLength;
581 result = env->NewStringUTF(buffer);
586 static void android_location_GpsLocationProvider_update_network_state(JNIEnv* env, jobject obj,
587 jboolean connected, int type, jboolean roaming, jstring extraInfo)
589 const AGpsRilInterface* interface = GetAGpsRilInterface(env, obj);
590 if (interface && interface->update_network_state) {
592 const char *extraInfoStr = env->GetStringUTFChars(extraInfo, NULL);
593 interface->update_network_state(connected, type, roaming, extraInfoStr);
594 env->ReleaseStringUTFChars(extraInfo, extraInfoStr);
596 interface->update_network_state(connected, type, roaming, NULL);
601 static JNINativeMethod sMethods[] = {
602 /* name, signature, funcPtr */
603 {"class_init_native", "()V", (void *)android_location_GpsLocationProvider_class_init_native},
604 {"native_is_supported", "()Z", (void*)android_location_GpsLocationProvider_is_supported},
605 {"native_init", "()Z", (void*)android_location_GpsLocationProvider_init},
606 {"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup},
607 {"native_set_position_mode", "(IIIII)Z", (void*)android_location_GpsLocationProvider_set_position_mode},
608 {"native_start", "()Z", (void*)android_location_GpsLocationProvider_start},
609 {"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop},
610 {"native_delete_aiding_data", "(I)V", (void*)android_location_GpsLocationProvider_delete_aiding_data},
611 {"native_read_sv_status", "([I[F[F[F[I)I", (void*)android_location_GpsLocationProvider_read_sv_status},
612 {"native_read_nmea", "([BI)I", (void*)android_location_GpsLocationProvider_read_nmea},
613 {"native_inject_time", "(JJI)V", (void*)android_location_GpsLocationProvider_inject_time},
614 {"native_inject_location", "(DDF)V", (void*)android_location_GpsLocationProvider_inject_location},
615 {"native_supports_xtra", "()Z", (void*)android_location_GpsLocationProvider_supports_xtra},
616 {"native_inject_xtra_data", "([BI)V", (void*)android_location_GpsLocationProvider_inject_xtra_data},
617 {"native_agps_data_conn_open", "(Ljava/lang/String;)V", (void*)android_location_GpsLocationProvider_agps_data_conn_open},
618 {"native_agps_data_conn_closed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_closed},
619 {"native_agps_data_conn_failed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_failed},
620 {"native_agps_set_id","(ILjava/lang/String;)V",(void*)android_location_GpsLocationProvider_agps_set_id},
621 {"native_agps_set_ref_location_cellid","(IIIII)V",(void*)android_location_GpsLocationProvider_agps_set_reference_location_cellid},
622 {"native_set_agps_server", "(ILjava/lang/String;I)V", (void*)android_location_GpsLocationProvider_set_agps_server},
623 {"native_send_ni_response", "(II)V", (void*)android_location_GpsLocationProvider_send_ni_response},
624 {"native_agps_ni_message", "([BI)V", (void *)android_location_GpsLocationProvider_agps_send_ni_message},
625 {"native_get_internal_state", "()Ljava/lang/String;", (void*)android_location_GpsLocationProvider_get_internal_state},
626 {"native_update_network_state", "(ZIZLjava/lang/String;)V", (void*)android_location_GpsLocationProvider_update_network_state },
629 int register_android_server_location_GpsLocationProvider(JNIEnv* env)
631 return jniRegisterNativeMethods(env, "com/android/server/location/GpsLocationProvider", sMethods, NELEM(sMethods));
634 } /* namespace android */