The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "GpsLocationProvider" |
| 18 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 19 | //#define LOG_NDDEBUG 0 |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "JNIHelp.h" |
| 22 | #include "jni.h" |
Mike Lockwood | b7ff457 | 2010-04-05 15:24:34 -0400 | [diff] [blame] | 23 | #include "hardware/hardware.h" |
| 24 | #include "hardware/gps.h" |
Mike Lockwood | 8f5a800 | 2010-04-07 09:05:26 -0400 | [diff] [blame^] | 25 | #include "hardware_legacy/power.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include "utils/Log.h" |
| 27 | #include "utils/misc.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <pthread.h> |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | static pthread_mutex_t sEventMutex = PTHREAD_MUTEX_INITIALIZER; |
| 33 | static pthread_cond_t sEventCond = PTHREAD_COND_INITIALIZER; |
| 34 | static jmethodID method_reportLocation; |
| 35 | static jmethodID method_reportStatus; |
| 36 | static jmethodID method_reportSvStatus; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 37 | static jmethodID method_reportAGpsStatus; |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 38 | static jmethodID method_reportNmea; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | static jmethodID method_xtraDownloadRequest; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 40 | static jmethodID method_reportNiNotification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | static const GpsInterface* sGpsInterface = NULL; |
| 43 | static const GpsXtraInterface* sGpsXtraInterface = NULL; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 44 | static const AGpsInterface* sAGpsInterface = NULL; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 45 | static const GpsNiInterface* sGpsNiInterface = NULL; |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 46 | static const GpsDebugInterface* sGpsDebugInterface = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | // data written to by GPS callbacks |
| 49 | static GpsLocation sGpsLocation; |
| 50 | static GpsStatus sGpsStatus; |
| 51 | static GpsSvStatus sGpsSvStatus; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 52 | static AGpsStatus sAGpsStatus; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 53 | static GpsNiNotification sGpsNiNotification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Mike Lockwood | 8f5a800 | 2010-04-07 09:05:26 -0400 | [diff] [blame^] | 55 | #define WAKE_LOCK_NAME "GPS" |
| 56 | |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 57 | // buffer for NMEA data |
| 58 | #define NMEA_SENTENCE_LENGTH 100 |
| 59 | #define NMEA_SENTENCE_COUNT 40 |
| 60 | struct NmeaSentence { |
| 61 | GpsUtcTime timestamp; |
| 62 | char nmea[NMEA_SENTENCE_LENGTH]; |
| 63 | }; |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 64 | static NmeaSentence sNmeaBuffer[NMEA_SENTENCE_COUNT]; |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 65 | static int mNmeaSentenceCount = 0; |
| 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | // a copy of the data shared by android_location_GpsLocationProvider_wait_for_event |
| 68 | // and android_location_GpsLocationProvider_read_status |
| 69 | static GpsLocation sGpsLocationCopy; |
| 70 | static GpsStatus sGpsStatusCopy; |
| 71 | static GpsSvStatus sGpsSvStatusCopy; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 72 | static AGpsStatus sAGpsStatusCopy; |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 73 | static NmeaSentence sNmeaBufferCopy[NMEA_SENTENCE_COUNT]; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 74 | static GpsNiNotification sGpsNiNotificationCopy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
| 76 | enum CallbackType { |
| 77 | kLocation = 1, |
| 78 | kStatus = 2, |
| 79 | kSvStatus = 4, |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 80 | kAGpsStatus = 8, |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 81 | kXtraDownloadRequest = 16, |
| 82 | kDisableRequest = 32, |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 83 | kNmeaAvailable = 64, |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 84 | kNiNotification = 128, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | }; |
| 86 | static int sPendingCallbacks; |
| 87 | |
| 88 | namespace android { |
| 89 | |
| 90 | static void location_callback(GpsLocation* location) |
| 91 | { |
| 92 | pthread_mutex_lock(&sEventMutex); |
| 93 | |
| 94 | sPendingCallbacks |= kLocation; |
| 95 | memcpy(&sGpsLocation, location, sizeof(sGpsLocation)); |
| 96 | |
| 97 | pthread_cond_signal(&sEventCond); |
| 98 | pthread_mutex_unlock(&sEventMutex); |
| 99 | } |
| 100 | |
| 101 | static void status_callback(GpsStatus* status) |
| 102 | { |
| 103 | pthread_mutex_lock(&sEventMutex); |
| 104 | |
| 105 | sPendingCallbacks |= kStatus; |
| 106 | memcpy(&sGpsStatus, status, sizeof(sGpsStatus)); |
| 107 | |
| 108 | pthread_cond_signal(&sEventCond); |
| 109 | pthread_mutex_unlock(&sEventMutex); |
| 110 | } |
| 111 | |
| 112 | static void sv_status_callback(GpsSvStatus* sv_status) |
| 113 | { |
| 114 | pthread_mutex_lock(&sEventMutex); |
| 115 | |
| 116 | sPendingCallbacks |= kSvStatus; |
| 117 | memcpy(&sGpsSvStatus, sv_status, sizeof(GpsSvStatus)); |
| 118 | |
| 119 | pthread_cond_signal(&sEventCond); |
| 120 | pthread_mutex_unlock(&sEventMutex); |
| 121 | } |
| 122 | |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 123 | static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length) |
| 124 | { |
| 125 | pthread_mutex_lock(&sEventMutex); |
| 126 | |
| 127 | if (length >= NMEA_SENTENCE_LENGTH) { |
| 128 | LOGE("NMEA data too long in nmea_callback (length = %d)\n", length); |
| 129 | length = NMEA_SENTENCE_LENGTH - 1; |
| 130 | } |
| 131 | if (mNmeaSentenceCount >= NMEA_SENTENCE_COUNT) { |
| 132 | LOGE("NMEA data overflowed buffer\n"); |
| 133 | pthread_mutex_unlock(&sEventMutex); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | sPendingCallbacks |= kNmeaAvailable; |
| 138 | sNmeaBuffer[mNmeaSentenceCount].timestamp = timestamp; |
| 139 | memcpy(sNmeaBuffer[mNmeaSentenceCount].nmea, nmea, length); |
| 140 | sNmeaBuffer[mNmeaSentenceCount].nmea[length] = 0; |
| 141 | mNmeaSentenceCount++; |
| 142 | |
| 143 | pthread_cond_signal(&sEventCond); |
| 144 | pthread_mutex_unlock(&sEventMutex); |
| 145 | } |
| 146 | |
Mike Lockwood | 8f5a800 | 2010-04-07 09:05:26 -0400 | [diff] [blame^] | 147 | static void acquire_wakelock_callback() |
| 148 | { |
| 149 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 150 | } |
| 151 | |
| 152 | static void release_wakelock_callback() |
| 153 | { |
| 154 | release_wake_lock(WAKE_LOCK_NAME); |
| 155 | } |
| 156 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 157 | static void agps_status_callback(AGpsStatus* agps_status) |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 158 | { |
| 159 | pthread_mutex_lock(&sEventMutex); |
| 160 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 161 | sPendingCallbacks |= kAGpsStatus; |
| 162 | memcpy(&sAGpsStatus, agps_status, sizeof(AGpsStatus)); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 163 | |
| 164 | pthread_cond_signal(&sEventCond); |
| 165 | pthread_mutex_unlock(&sEventMutex); |
| 166 | } |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | GpsCallbacks sGpsCallbacks = { |
Mike Lockwood | 8f5a800 | 2010-04-07 09:05:26 -0400 | [diff] [blame^] | 169 | sizeof(GpsCallbacks), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | location_callback, |
| 171 | status_callback, |
| 172 | sv_status_callback, |
Mike Lockwood | 8f5a800 | 2010-04-07 09:05:26 -0400 | [diff] [blame^] | 173 | nmea_callback, |
| 174 | acquire_wakelock_callback, |
| 175 | release_wakelock_callback, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static void |
| 179 | download_request_callback() |
| 180 | { |
| 181 | pthread_mutex_lock(&sEventMutex); |
| 182 | sPendingCallbacks |= kXtraDownloadRequest; |
| 183 | pthread_cond_signal(&sEventCond); |
| 184 | pthread_mutex_unlock(&sEventMutex); |
| 185 | } |
| 186 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 187 | static void |
| 188 | gps_ni_notify_callback(GpsNiNotification *notification) |
| 189 | { |
| 190 | LOGD("gps_ni_notify_callback: notif=%d", notification->notification_id); |
| 191 | |
| 192 | pthread_mutex_lock(&sEventMutex); |
| 193 | |
| 194 | sPendingCallbacks |= kNiNotification; |
| 195 | memcpy(&sGpsNiNotification, notification, sizeof(GpsNiNotification)); |
| 196 | |
| 197 | pthread_cond_signal(&sEventCond); |
| 198 | pthread_mutex_unlock(&sEventMutex); |
| 199 | } |
| 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | GpsXtraCallbacks sGpsXtraCallbacks = { |
| 202 | download_request_callback, |
| 203 | }; |
| 204 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 205 | AGpsCallbacks sAGpsCallbacks = { |
| 206 | agps_status_callback, |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 207 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 209 | GpsNiCallbacks sGpsNiCallbacks = { |
| 210 | gps_ni_notify_callback, |
| 211 | }; |
| 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz) { |
| 214 | method_reportLocation = env->GetMethodID(clazz, "reportLocation", "(IDDDFFFJ)V"); |
| 215 | method_reportStatus = env->GetMethodID(clazz, "reportStatus", "(I)V"); |
| 216 | method_reportSvStatus = env->GetMethodID(clazz, "reportSvStatus", "()V"); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 217 | method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(II)V"); |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 218 | method_reportNmea = env->GetMethodID(clazz, "reportNmea", "(IJ)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | method_xtraDownloadRequest = env->GetMethodID(clazz, "xtraDownloadRequest", "()V"); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 220 | method_reportNiNotification = env->GetMethodID(clazz, "reportNiNotification", "(IIIIILjava/lang/String;Ljava/lang/String;IILjava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Mike Lockwood | b7ff457 | 2010-04-05 15:24:34 -0400 | [diff] [blame] | 223 | static const GpsInterface* get_gps_interface() { |
| 224 | int err; |
| 225 | hw_module_t* module; |
| 226 | const GpsInterface* interface = NULL; |
| 227 | |
| 228 | err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module); |
| 229 | if (err == 0) { |
| 230 | hw_device_t* device; |
| 231 | err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device); |
| 232 | if (err == 0) { |
| 233 | gps_device_t* gps_device = (gps_device_t *)device; |
| 234 | interface = gps_device->get_gps_interface(gps_device); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return interface; |
| 239 | } |
| 240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) { |
| 242 | if (!sGpsInterface) |
Mike Lockwood | b7ff457 | 2010-04-05 15:24:34 -0400 | [diff] [blame] | 243 | sGpsInterface = get_gps_interface(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | return (sGpsInterface != NULL); |
| 245 | } |
| 246 | |
| 247 | static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj) |
| 248 | { |
| 249 | if (!sGpsInterface) |
Mike Lockwood | b7ff457 | 2010-04-05 15:24:34 -0400 | [diff] [blame] | 250 | sGpsInterface = get_gps_interface(); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 251 | if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0) |
| 252 | return false; |
| 253 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 254 | if (!sAGpsInterface) |
| 255 | sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE); |
| 256 | if (sAGpsInterface) |
| 257 | sAGpsInterface->init(&sAGpsCallbacks); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 258 | |
| 259 | if (!sGpsNiInterface) |
| 260 | sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE); |
| 261 | if (sGpsNiInterface) |
| 262 | sGpsNiInterface->init(&sGpsNiCallbacks); |
| 263 | |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 264 | if (!sGpsDebugInterface) |
| 265 | sGpsDebugInterface = (const GpsDebugInterface*)sGpsInterface->get_extension(GPS_DEBUG_INTERFACE); |
| 266 | |
Mike Lockwood | c21a08f | 2009-04-30 15:16:39 -0400 | [diff] [blame] | 267 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void android_location_GpsLocationProvider_disable(JNIEnv* env, jobject obj) |
| 271 | { |
| 272 | pthread_mutex_lock(&sEventMutex); |
| 273 | sPendingCallbacks |= kDisableRequest; |
| 274 | pthread_cond_signal(&sEventCond); |
| 275 | pthread_mutex_unlock(&sEventMutex); |
| 276 | } |
| 277 | |
| 278 | static void android_location_GpsLocationProvider_cleanup(JNIEnv* env, jobject obj) |
| 279 | { |
| 280 | sGpsInterface->cleanup(); |
| 281 | } |
| 282 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 283 | static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj, jint positionMode, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 284 | jboolean singleFix, jint fixFrequency) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 286 | int result = sGpsInterface->set_position_mode(positionMode, (singleFix ? 0 : fixFrequency)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | if (result) { |
David 'Digit' Turner | 9a940e8 | 2009-05-20 14:23:46 +0200 | [diff] [blame] | 288 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return (sGpsInterface->start() == 0); |
| 292 | } |
| 293 | |
| 294 | static jboolean android_location_GpsLocationProvider_stop(JNIEnv* env, jobject obj) |
| 295 | { |
| 296 | return (sGpsInterface->stop() == 0); |
| 297 | } |
| 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | static void android_location_GpsLocationProvider_delete_aiding_data(JNIEnv* env, jobject obj, jint flags) |
| 300 | { |
| 301 | sGpsInterface->delete_aiding_data(flags); |
| 302 | } |
| 303 | |
| 304 | static void android_location_GpsLocationProvider_wait_for_event(JNIEnv* env, jobject obj) |
| 305 | { |
| 306 | pthread_mutex_lock(&sEventMutex); |
Jared Suttles | 8b1243e | 2009-11-04 16:53:44 -0600 | [diff] [blame] | 307 | while (sPendingCallbacks == 0) { |
| 308 | pthread_cond_wait(&sEventCond, &sEventMutex); |
| 309 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | // copy and clear the callback flags |
| 312 | int pendingCallbacks = sPendingCallbacks; |
| 313 | sPendingCallbacks = 0; |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 314 | int nmeaSentenceCount = mNmeaSentenceCount; |
| 315 | mNmeaSentenceCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | |
| 317 | // copy everything and unlock the mutex before calling into Java code to avoid the possibility |
| 318 | // of timeouts in the GPS engine. |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 319 | if (pendingCallbacks & kLocation) |
| 320 | memcpy(&sGpsLocationCopy, &sGpsLocation, sizeof(sGpsLocationCopy)); |
| 321 | if (pendingCallbacks & kStatus) |
| 322 | memcpy(&sGpsStatusCopy, &sGpsStatus, sizeof(sGpsStatusCopy)); |
| 323 | if (pendingCallbacks & kSvStatus) |
| 324 | memcpy(&sGpsSvStatusCopy, &sGpsSvStatus, sizeof(sGpsSvStatusCopy)); |
| 325 | if (pendingCallbacks & kAGpsStatus) |
| 326 | memcpy(&sAGpsStatusCopy, &sAGpsStatus, sizeof(sAGpsStatusCopy)); |
| 327 | if (pendingCallbacks & kNmeaAvailable) |
| 328 | memcpy(&sNmeaBufferCopy, &sNmeaBuffer, nmeaSentenceCount * sizeof(sNmeaBuffer[0])); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 329 | if (pendingCallbacks & kNiNotification) |
| 330 | memcpy(&sGpsNiNotificationCopy, &sGpsNiNotification, sizeof(sGpsNiNotificationCopy)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | pthread_mutex_unlock(&sEventMutex); |
| 332 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 333 | if (pendingCallbacks & kLocation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | env->CallVoidMethod(obj, method_reportLocation, sGpsLocationCopy.flags, |
| 335 | (jdouble)sGpsLocationCopy.latitude, (jdouble)sGpsLocationCopy.longitude, |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 336 | (jdouble)sGpsLocationCopy.altitude, |
| 337 | (jfloat)sGpsLocationCopy.speed, (jfloat)sGpsLocationCopy.bearing, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | (jfloat)sGpsLocationCopy.accuracy, (jlong)sGpsLocationCopy.timestamp); |
| 339 | } |
| 340 | if (pendingCallbacks & kStatus) { |
| 341 | env->CallVoidMethod(obj, method_reportStatus, sGpsStatusCopy.status); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | if (pendingCallbacks & kSvStatus) { |
| 344 | env->CallVoidMethod(obj, method_reportSvStatus); |
| 345 | } |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 346 | if (pendingCallbacks & kAGpsStatus) { |
| 347 | env->CallVoidMethod(obj, method_reportAGpsStatus, sAGpsStatusCopy.type, sAGpsStatusCopy.status); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 348 | } |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 349 | if (pendingCallbacks & kNmeaAvailable) { |
| 350 | for (int i = 0; i < nmeaSentenceCount; i++) { |
| 351 | env->CallVoidMethod(obj, method_reportNmea, i, sNmeaBuffer[i].timestamp); |
| 352 | } |
| 353 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 354 | if (pendingCallbacks & kXtraDownloadRequest) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | env->CallVoidMethod(obj, method_xtraDownloadRequest); |
| 356 | } |
| 357 | if (pendingCallbacks & kDisableRequest) { |
| 358 | // don't need to do anything - we are just poking so wait_for_event will return. |
| 359 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 360 | if (pendingCallbacks & kNiNotification) { |
| 361 | LOGD("android_location_GpsLocationProvider_wait_for_event: sent notification callback."); |
| 362 | jstring reqId = env->NewStringUTF(sGpsNiNotificationCopy.requestor_id); |
| 363 | jstring text = env->NewStringUTF(sGpsNiNotificationCopy.text); |
| 364 | jstring extras = env->NewStringUTF(sGpsNiNotificationCopy.extras); |
| 365 | env->CallVoidMethod(obj, method_reportNiNotification, |
| 366 | sGpsNiNotificationCopy.notification_id, |
| 367 | sGpsNiNotificationCopy.ni_type, |
| 368 | sGpsNiNotificationCopy.notify_flags, |
| 369 | sGpsNiNotificationCopy.timeout, |
| 370 | sGpsNiNotificationCopy.default_response, |
| 371 | reqId, |
| 372 | text, |
| 373 | sGpsNiNotificationCopy.requestor_id_encoding, |
| 374 | sGpsNiNotificationCopy.text_encoding, |
| 375 | extras |
| 376 | ); |
| 377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 380 | static jint android_location_GpsLocationProvider_read_sv_status(JNIEnv* env, jobject obj, |
| 381 | jintArray prnArray, jfloatArray snrArray, jfloatArray elevArray, jfloatArray azumArray, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | jintArray maskArray) |
| 383 | { |
| 384 | // this should only be called from within a call to reportStatus, so we don't need to lock here |
| 385 | |
| 386 | jint* prns = env->GetIntArrayElements(prnArray, 0); |
| 387 | jfloat* snrs = env->GetFloatArrayElements(snrArray, 0); |
| 388 | jfloat* elev = env->GetFloatArrayElements(elevArray, 0); |
| 389 | jfloat* azim = env->GetFloatArrayElements(azumArray, 0); |
| 390 | jint* mask = env->GetIntArrayElements(maskArray, 0); |
| 391 | |
| 392 | int num_svs = sGpsSvStatusCopy.num_svs; |
| 393 | for (int i = 0; i < num_svs; i++) { |
| 394 | prns[i] = sGpsSvStatusCopy.sv_list[i].prn; |
| 395 | snrs[i] = sGpsSvStatusCopy.sv_list[i].snr; |
| 396 | elev[i] = sGpsSvStatusCopy.sv_list[i].elevation; |
| 397 | azim[i] = sGpsSvStatusCopy.sv_list[i].azimuth; |
| 398 | } |
| 399 | mask[0] = sGpsSvStatusCopy.ephemeris_mask; |
| 400 | mask[1] = sGpsSvStatusCopy.almanac_mask; |
| 401 | mask[2] = sGpsSvStatusCopy.used_in_fix_mask; |
| 402 | |
| 403 | env->ReleaseIntArrayElements(prnArray, prns, 0); |
| 404 | env->ReleaseFloatArrayElements(snrArray, snrs, 0); |
| 405 | env->ReleaseFloatArrayElements(elevArray, elev, 0); |
| 406 | env->ReleaseFloatArrayElements(azumArray, azim, 0); |
| 407 | env->ReleaseIntArrayElements(maskArray, mask, 0); |
| 408 | return num_svs; |
| 409 | } |
| 410 | |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 411 | static jint android_location_GpsLocationProvider_read_nmea(JNIEnv* env, jobject obj, jint index, jbyteArray nmeaArray, jint buffer_size) |
| 412 | { |
Eddie Ho | eb40aae | 2009-09-04 07:03:22 -0400 | [diff] [blame] | 413 | // this should only be called from within a call to reportNmea, so we don't need to lock here |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 414 | |
| 415 | jbyte* nmea = env->GetByteArrayElements(nmeaArray, 0); |
| 416 | |
Eddie Ho | eb40aae | 2009-09-04 07:03:22 -0400 | [diff] [blame] | 417 | int length = strlen(sNmeaBufferCopy[index].nmea); |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 418 | if (length > buffer_size) |
| 419 | length = buffer_size; |
Eddie Ho | eb40aae | 2009-09-04 07:03:22 -0400 | [diff] [blame] | 420 | memcpy(nmea, sNmeaBufferCopy[index].nmea, length); |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 421 | |
| 422 | env->ReleaseByteArrayElements(nmeaArray, nmea, 0); |
| 423 | return length; |
| 424 | } |
| 425 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | static void android_location_GpsLocationProvider_inject_time(JNIEnv* env, jobject obj, jlong time, |
| 427 | jlong timeReference, jint uncertainty) |
| 428 | { |
| 429 | sGpsInterface->inject_time(time, timeReference, uncertainty); |
| 430 | } |
| 431 | |
Mike Lockwood | d26ce0d | 2009-06-11 12:25:46 -0400 | [diff] [blame] | 432 | static void android_location_GpsLocationProvider_inject_location(JNIEnv* env, jobject obj, |
| 433 | jdouble latitude, jdouble longitude, jfloat accuracy) |
| 434 | { |
| 435 | sGpsInterface->inject_location(latitude, longitude, accuracy); |
| 436 | } |
| 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | static jboolean android_location_GpsLocationProvider_supports_xtra(JNIEnv* env, jobject obj) |
| 439 | { |
| 440 | if (!sGpsXtraInterface) { |
| 441 | sGpsXtraInterface = (const GpsXtraInterface*)sGpsInterface->get_extension(GPS_XTRA_INTERFACE); |
| 442 | if (sGpsXtraInterface) { |
| 443 | int result = sGpsXtraInterface->init(&sGpsXtraCallbacks); |
| 444 | if (result) { |
| 445 | sGpsXtraInterface = NULL; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return (sGpsXtraInterface != NULL); |
| 451 | } |
| 452 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 453 | static void android_location_GpsLocationProvider_inject_xtra_data(JNIEnv* env, jobject obj, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | jbyteArray data, jint length) |
| 455 | { |
| 456 | jbyte* bytes = env->GetByteArrayElements(data, 0); |
| 457 | sGpsXtraInterface->inject_xtra_data((char *)bytes, length); |
| 458 | env->ReleaseByteArrayElements(data, bytes, 0); |
| 459 | } |
| 460 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 461 | static void android_location_GpsLocationProvider_agps_data_conn_open(JNIEnv* env, jobject obj, jstring apn) |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 462 | { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 463 | if (!sAGpsInterface) { |
| 464 | sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 465 | } |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 466 | if (sAGpsInterface) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 467 | if (apn == NULL) { |
| 468 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 469 | return; |
| 470 | } |
| 471 | const char *apnStr = env->GetStringUTFChars(apn, NULL); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 472 | sAGpsInterface->data_conn_open(apnStr); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 473 | env->ReleaseStringUTFChars(apn, apnStr); |
| 474 | } |
| 475 | } |
| 476 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 477 | static void android_location_GpsLocationProvider_agps_data_conn_closed(JNIEnv* env, jobject obj) |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 478 | { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 479 | if (!sAGpsInterface) { |
| 480 | sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 481 | } |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 482 | if (sAGpsInterface) { |
| 483 | sAGpsInterface->data_conn_closed(); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 487 | static void android_location_GpsLocationProvider_agps_data_conn_failed(JNIEnv* env, jobject obj) |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 488 | { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 489 | if (!sAGpsInterface) { |
| 490 | sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 491 | } |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 492 | if (sAGpsInterface) { |
| 493 | sAGpsInterface->data_conn_failed(); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 497 | static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jobject obj, |
Mike Lockwood | a9e5461 | 2009-06-19 14:54:42 -0400 | [diff] [blame] | 498 | jint type, jstring hostname, jint port) |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 499 | { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 500 | if (!sAGpsInterface) { |
| 501 | sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 502 | } |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 503 | if (sAGpsInterface) { |
Mike Lockwood | a9e5461 | 2009-06-19 14:54:42 -0400 | [diff] [blame] | 504 | const char *c_hostname = env->GetStringUTFChars(hostname, NULL); |
| 505 | sAGpsInterface->set_server(type, c_hostname, port); |
| 506 | env->ReleaseStringUTFChars(hostname, c_hostname); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 510 | static void android_location_GpsLocationProvider_send_ni_response(JNIEnv* env, jobject obj, |
| 511 | jint notifId, jint response) |
| 512 | { |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 513 | if (!sGpsNiInterface) { |
| 514 | sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE); |
| 515 | } |
| 516 | if (sGpsNiInterface) { |
| 517 | sGpsNiInterface->respond(notifId, response); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static jstring android_location_GpsLocationProvider_get_internal_state(JNIEnv* env, jobject obj) |
| 522 | { |
| 523 | jstring result = NULL; |
| 524 | if (sGpsDebugInterface) { |
| 525 | const size_t maxLength = 2047; |
| 526 | char buffer[maxLength+1]; |
| 527 | size_t length = sGpsDebugInterface->get_internal_state(buffer, maxLength); |
| 528 | if (length > maxLength) length = maxLength; |
| 529 | buffer[length] = 0; |
| 530 | result = env->NewStringUTF(buffer); |
| 531 | } |
| 532 | return result; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 533 | } |
| 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | static JNINativeMethod sMethods[] = { |
| 536 | /* name, signature, funcPtr */ |
| 537 | {"class_init_native", "()V", (void *)android_location_GpsLocationProvider_class_init_native}, |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 538 | {"native_is_supported", "()Z", (void*)android_location_GpsLocationProvider_is_supported}, |
| 539 | {"native_init", "()Z", (void*)android_location_GpsLocationProvider_init}, |
| 540 | {"native_disable", "()V", (void*)android_location_GpsLocationProvider_disable}, |
| 541 | {"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup}, |
| 542 | {"native_start", "(IZI)Z", (void*)android_location_GpsLocationProvider_start}, |
| 543 | {"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop}, |
| 544 | {"native_delete_aiding_data", "(I)V", (void*)android_location_GpsLocationProvider_delete_aiding_data}, |
| 545 | {"native_wait_for_event", "()V", (void*)android_location_GpsLocationProvider_wait_for_event}, |
| 546 | {"native_read_sv_status", "([I[F[F[F[I)I", (void*)android_location_GpsLocationProvider_read_sv_status}, |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 547 | {"native_read_nmea", "(I[BI)I", (void*)android_location_GpsLocationProvider_read_nmea}, |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 548 | {"native_inject_time", "(JJI)V", (void*)android_location_GpsLocationProvider_inject_time}, |
Mike Lockwood | d26ce0d | 2009-06-11 12:25:46 -0400 | [diff] [blame] | 549 | {"native_inject_location", "(DDF)V", (void*)android_location_GpsLocationProvider_inject_location}, |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 550 | {"native_supports_xtra", "()Z", (void*)android_location_GpsLocationProvider_supports_xtra}, |
| 551 | {"native_inject_xtra_data", "([BI)V", (void*)android_location_GpsLocationProvider_inject_xtra_data}, |
| 552 | {"native_agps_data_conn_open", "(Ljava/lang/String;)V", (void*)android_location_GpsLocationProvider_agps_data_conn_open}, |
| 553 | {"native_agps_data_conn_closed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_closed}, |
| 554 | {"native_agps_data_conn_failed", "()V", (void*)android_location_GpsLocationProvider_agps_data_conn_failed}, |
Mike Lockwood | a9e5461 | 2009-06-19 14:54:42 -0400 | [diff] [blame] | 555 | {"native_set_agps_server", "(ILjava/lang/String;I)V", (void*)android_location_GpsLocationProvider_set_agps_server}, |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 556 | {"native_send_ni_response", "(II)V", (void*)android_location_GpsLocationProvider_send_ni_response}, |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 557 | {"native_get_internal_state", "()Ljava/lang/String;", (void*)android_location_GpsLocationProvider_get_internal_state}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | }; |
| 559 | |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 560 | int register_android_server_location_GpsLocationProvider(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | { |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 562 | return jniRegisterNativeMethods(env, "com/android/server/location/GpsLocationProvider", sMethods, NELEM(sMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | } /* namespace android */ |