GPS: Support for GPS HAL managing its own scheduling

Also update to support new position mode API.

Change-Id: I00acc094d3e85bc5c0cd431af517064bfa8f2b1a
Signed-off-by: Mike Lockwood <lockwood@android.com>
This commit is contained in:
Mike Lockwood
2010-04-14 17:17:24 -04:00
parent f440b4aa7f
commit 04598b67a5
2 changed files with 100 additions and 33 deletions

View File

@@ -81,6 +81,10 @@ public class GpsLocationProvider implements LocationProviderInterface {
private static final int GPS_POSITION_MODE_MS_BASED = 1; private static final int GPS_POSITION_MODE_MS_BASED = 1;
private static final int GPS_POSITION_MODE_MS_ASSISTED = 2; private static final int GPS_POSITION_MODE_MS_ASSISTED = 2;
// these need to match GpsPositionRecurrence enum in gps.h
private static final int GPS_POSITION_RECURRENCE_PERIODIC = 0;
private static final int GPS_POSITION_RECURRENCE_SINGLE = 1;
// these need to match GpsStatusValue defines in gps.h // these need to match GpsStatusValue defines in gps.h
private static final int GPS_STATUS_NONE = 0; private static final int GPS_STATUS_NONE = 0;
private static final int GPS_STATUS_SESSION_BEGIN = 1; private static final int GPS_STATUS_SESSION_BEGIN = 1;
@@ -119,6 +123,13 @@ public class GpsLocationProvider implements LocationProviderInterface {
private static final int GPS_DELETE_CELLDB_INFO = 0x8000; private static final int GPS_DELETE_CELLDB_INFO = 0x8000;
private static final int GPS_DELETE_ALL = 0xFFFF; private static final int GPS_DELETE_ALL = 0xFFFF;
// The GPS_CAPABILITY_* flags must match the values in gps.h
private static final int GPS_CAPABILITY_SCHEDULING = 0x0000001;
private static final int GPS_CAPABILITY_MSB = 0x0000002;
private static final int GPS_CAPABILITY_MSA = 0x0000004;
private static final int GPS_CAPABILITY_SINGLE_SHOT = 0x0000008;
// these need to match AGpsType enum in gps.h // these need to match AGpsType enum in gps.h
private static final int AGPS_TYPE_SUPL = 1; private static final int AGPS_TYPE_SUPL = 1;
private static final int AGPS_TYPE_C2K = 2; private static final int AGPS_TYPE_C2K = 2;
@@ -150,13 +161,13 @@ public class GpsLocationProvider implements LocationProviderInterface {
private long mStatusUpdateTime = SystemClock.elapsedRealtime(); private long mStatusUpdateTime = SystemClock.elapsedRealtime();
// turn off GPS fix icon if we haven't received a fix in 10 seconds // turn off GPS fix icon if we haven't received a fix in 10 seconds
private static final long RECENT_FIX_TIMEOUT = 10; private static final long RECENT_FIX_TIMEOUT = 10 * 1000;
// number of fixes to receive before disabling GPS // number of fixes to receive before disabling GPS
private static final int MIN_FIX_COUNT = 10; private static final int MIN_FIX_COUNT = 10;
// stop trying if we do not receive a fix within 60 seconds // stop trying if we do not receive a fix within 60 seconds
private static final int NO_FIX_TIMEOUT = 60; private static final int NO_FIX_TIMEOUT = 60 * 1000;
// true if we are enabled // true if we are enabled
private volatile boolean mEnabled; private volatile boolean mEnabled;
@@ -175,8 +186,8 @@ public class GpsLocationProvider implements LocationProviderInterface {
// true if GPS engine is on // true if GPS engine is on
private boolean mEngineOn; private boolean mEngineOn;
// requested frequency of fixes, in seconds // requested frequency of fixes, in milliseconds
private int mFixInterval = 1; private int mFixInterval = 1000;
// number of fixes we have received since we started navigating // number of fixes we have received since we started navigating
private int mFixCount; private int mFixCount;
@@ -184,6 +195,9 @@ public class GpsLocationProvider implements LocationProviderInterface {
// true if we started navigation // true if we started navigation
private boolean mStarted; private boolean mStarted;
// capabilities of the GPS engine
private int mEngineCapabilities;
// for calculating time to first fix // for calculating time to first fix
private long mFixRequestTime = 0; private long mFixRequestTime = 0;
// time to first fix for most recent session // time to first fix for most recent session
@@ -191,6 +205,8 @@ public class GpsLocationProvider implements LocationProviderInterface {
// time we received our last fix // time we received our last fix
private long mLastFixTime; private long mLastFixTime;
private int mPositionMode;
// properties loaded from PROPERTIES_FILE // properties loaded from PROPERTIES_FILE
private Properties mProperties; private Properties mProperties;
private String mNtpServer; private String mNtpServer;
@@ -717,8 +733,10 @@ public class GpsLocationProvider implements LocationProviderInterface {
mLastFixTime = 0; mLastFixTime = 0;
startNavigating(); startNavigating();
} else { } else {
mAlarmManager.cancel(mWakeupIntent); if (!hasCapability(GPS_CAPABILITY_SCHEDULING)) {
mAlarmManager.cancel(mTimeoutIntent); mAlarmManager.cancel(mWakeupIntent);
mAlarmManager.cancel(mTimeoutIntent);
}
stopNavigating(); stopNavigating();
} }
} }
@@ -727,11 +745,14 @@ public class GpsLocationProvider implements LocationProviderInterface {
if (DEBUG) Log.d(TAG, "setMinTime " + minTime); if (DEBUG) Log.d(TAG, "setMinTime " + minTime);
if (minTime >= 0) { if (minTime >= 0) {
int interval = (int)(minTime/1000); mFixInterval = (int)minTime;
if (interval < 1) {
interval = 1; if (mStarted && hasCapability(GPS_CAPABILITY_SCHEDULING)) {
if (!native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC,
mFixInterval, 0, 0)) {
Log.e(TAG, "set_position_mode failed in setMinTime()");
}
} }
mFixInterval = interval;
} }
} }
@@ -871,15 +892,22 @@ public class GpsLocationProvider implements LocationProviderInterface {
if (!mStarted) { if (!mStarted) {
if (DEBUG) Log.d(TAG, "startNavigating"); if (DEBUG) Log.d(TAG, "startNavigating");
mStarted = true; mStarted = true;
int positionMode; if (hasCapability(GPS_CAPABILITY_MSB) &&
if (Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ASSISTED_GPS_ENABLED, 1) != 0) { Settings.Secure.ASSISTED_GPS_ENABLED, 1) != 0) {
positionMode = GPS_POSITION_MODE_MS_BASED; mPositionMode = GPS_POSITION_MODE_MS_BASED;
} else { } else {
positionMode = GPS_POSITION_MODE_STANDALONE; mPositionMode = GPS_POSITION_MODE_STANDALONE;
} }
if (!native_start(positionMode, false, 1)) { int interval = (hasCapability(GPS_CAPABILITY_SCHEDULING) ? mFixInterval : 1000);
if (!native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC,
interval, 0, 0)) {
mStarted = false;
Log.e(TAG, "set_position_mode failed in startNavigating()");
return;
}
if (!native_start()) {
mStarted = false; mStarted = false;
Log.e(TAG, "native_start failed in startNavigating()"); Log.e(TAG, "native_start failed in startNavigating()");
return; return;
@@ -889,11 +917,13 @@ public class GpsLocationProvider implements LocationProviderInterface {
updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, 0); updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, 0);
mFixCount = 0; mFixCount = 0;
mFixRequestTime = System.currentTimeMillis(); mFixRequestTime = System.currentTimeMillis();
// set timer to give up if we do not receive a fix within NO_FIX_TIMEOUT if (!hasCapability(GPS_CAPABILITY_SCHEDULING)) {
// and our fix interval is not short // set timer to give up if we do not receive a fix within NO_FIX_TIMEOUT
if (mFixInterval >= NO_FIX_TIMEOUT) { // and our fix interval is not short
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, if (mFixInterval >= NO_FIX_TIMEOUT) {
SystemClock.elapsedRealtime() + NO_FIX_TIMEOUT * 1000, mTimeoutIntent); mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + NO_FIX_TIMEOUT, mTimeoutIntent);
}
} }
} }
} }
@@ -920,7 +950,11 @@ public class GpsLocationProvider implements LocationProviderInterface {
mAlarmManager.cancel(mWakeupIntent); mAlarmManager.cancel(mWakeupIntent);
long now = SystemClock.elapsedRealtime(); long now = SystemClock.elapsedRealtime();
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + mFixInterval * 1000, mWakeupIntent); SystemClock.elapsedRealtime() + mFixInterval, mWakeupIntent);
}
private boolean hasCapability(int capability) {
return ((mEngineCapabilities & capability) != 0);
} }
/** /**
@@ -992,7 +1026,7 @@ public class GpsLocationProvider implements LocationProviderInterface {
if (mStarted && mStatus != LocationProvider.AVAILABLE) { if (mStarted && mStatus != LocationProvider.AVAILABLE) {
// we still want to time out if we do not receive MIN_FIX_COUNT // we still want to time out if we do not receive MIN_FIX_COUNT
// within the time out and we are requesting infrequent fixes // within the time out and we are requesting infrequent fixes
if (mFixInterval < NO_FIX_TIMEOUT) { if (!hasCapability(GPS_CAPABILITY_SCHEDULING) && mFixInterval < NO_FIX_TIMEOUT) {
mAlarmManager.cancel(mTimeoutIntent); mAlarmManager.cancel(mTimeoutIntent);
} }
@@ -1003,7 +1037,8 @@ public class GpsLocationProvider implements LocationProviderInterface {
updateStatus(LocationProvider.AVAILABLE, mSvCount); updateStatus(LocationProvider.AVAILABLE, mSvCount);
} }
if (mFixCount++ >= MIN_FIX_COUNT && mFixInterval > 1) { if (!hasCapability(GPS_CAPABILITY_SCHEDULING) &&
mFixCount++ >= MIN_FIX_COUNT && mFixInterval > 1000) {
if (DEBUG) Log.d(TAG, "exceeded MIN_FIX_COUNT"); if (DEBUG) Log.d(TAG, "exceeded MIN_FIX_COUNT");
hibernate(); hibernate();
} }
@@ -1117,7 +1152,7 @@ public class GpsLocationProvider implements LocationProviderInterface {
updateStatus(mStatus, svCount); updateStatus(mStatus, svCount);
if (mNavigating && mStatus == LocationProvider.AVAILABLE && mLastFixTime > 0 && if (mNavigating && mStatus == LocationProvider.AVAILABLE && mLastFixTime > 0 &&
System.currentTimeMillis() - mLastFixTime > RECENT_FIX_TIMEOUT * 1000) { System.currentTimeMillis() - mLastFixTime > RECENT_FIX_TIMEOUT) {
// send an intent to notify that the GPS is no longer receiving fixes. // send an intent to notify that the GPS is no longer receiving fixes.
Intent intent = new Intent(LocationManager.GPS_FIX_CHANGE_ACTION); Intent intent = new Intent(LocationManager.GPS_FIX_CHANGE_ACTION);
intent.putExtra(LocationManager.EXTRA_GPS_ENABLED, false); intent.putExtra(LocationManager.EXTRA_GPS_ENABLED, false);
@@ -1194,6 +1229,13 @@ public class GpsLocationProvider implements LocationProviderInterface {
} }
} }
/**
* called from native code to inform us what the GPS engine capabilities are
*/
private void setEngineCapabilities(int capabilities) {
mEngineCapabilities = capabilities;
}
/** /**
* called from native code to request XTRA data * called from native code to request XTRA data
*/ */
@@ -1417,9 +1459,10 @@ public class GpsLocationProvider implements LocationProviderInterface {
private native boolean native_init(); private native boolean native_init();
private native void native_disable(); private native void native_disable();
private native void native_cleanup(); private native void native_cleanup();
private native boolean native_start(int positionMode, boolean singleFix, int fixInterval); private native boolean native_set_position_mode(int mode, int recurrence, int min_interval,
int preferred_accuracy, int preferred_time);
private native boolean native_start();
private native boolean native_stop(); private native boolean native_stop();
private native void native_set_fix_frequency(int fixFrequency);
private native void native_delete_aiding_data(int flags); private native void native_delete_aiding_data(int flags);
private native void native_wait_for_event(); private native void native_wait_for_event();
// returns number of SVs // returns number of SVs

View File

@@ -36,6 +36,7 @@ static jmethodID method_reportStatus;
static jmethodID method_reportSvStatus; static jmethodID method_reportSvStatus;
static jmethodID method_reportAGpsStatus; static jmethodID method_reportAGpsStatus;
static jmethodID method_reportNmea; static jmethodID method_reportNmea;
static jmethodID method_setEngineCapabilities;
static jmethodID method_xtraDownloadRequest; static jmethodID method_xtraDownloadRequest;
static jmethodID method_reportNiNotification; static jmethodID method_reportNiNotification;
@@ -72,6 +73,8 @@ static GpsSvStatus sGpsSvStatusCopy;
static AGpsStatus sAGpsStatusCopy; static AGpsStatus sAGpsStatusCopy;
static NmeaSentence sNmeaBufferCopy[NMEA_SENTENCE_COUNT]; static NmeaSentence sNmeaBufferCopy[NMEA_SENTENCE_COUNT];
static GpsNiNotification sGpsNiNotificationCopy; static GpsNiNotification sGpsNiNotificationCopy;
static uint32_t sEngineCapabilities;
enum CallbackType { enum CallbackType {
kLocation = 1, kLocation = 1,
@@ -82,6 +85,7 @@ enum CallbackType {
kDisableRequest = 32, kDisableRequest = 32,
kNmeaAvailable = 64, kNmeaAvailable = 64,
kNiNotification = 128, kNiNotification = 128,
kSetCapabilities = 256,
}; };
static int sPendingCallbacks; static int sPendingCallbacks;
@@ -144,6 +148,19 @@ static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length)
pthread_mutex_unlock(&sEventMutex); pthread_mutex_unlock(&sEventMutex);
} }
static void set_capabilities_callback(uint32_t capabilities)
{
LOGD("set_capabilities_callback: %08X", capabilities);
pthread_mutex_lock(&sEventMutex);
sPendingCallbacks |= kSetCapabilities;
sEngineCapabilities = capabilities;
pthread_cond_signal(&sEventCond);
pthread_mutex_unlock(&sEventMutex);
}
static void acquire_wakelock_callback() static void acquire_wakelock_callback()
{ {
acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
@@ -171,6 +188,7 @@ GpsCallbacks sGpsCallbacks = {
status_callback, status_callback,
sv_status_callback, sv_status_callback,
nmea_callback, nmea_callback,
set_capabilities_callback,
acquire_wakelock_callback, acquire_wakelock_callback,
release_wakelock_callback, release_wakelock_callback,
}; };
@@ -216,6 +234,7 @@ static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env,
method_reportSvStatus = env->GetMethodID(clazz, "reportSvStatus", "()V"); method_reportSvStatus = env->GetMethodID(clazz, "reportSvStatus", "()V");
method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(II)V"); method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(II)V");
method_reportNmea = env->GetMethodID(clazz, "reportNmea", "(IJ)V"); method_reportNmea = env->GetMethodID(clazz, "reportNmea", "(IJ)V");
method_setEngineCapabilities = env->GetMethodID(clazz, "setEngineCapabilities", "(I)V");
method_xtraDownloadRequest = env->GetMethodID(clazz, "xtraDownloadRequest", "()V"); method_xtraDownloadRequest = env->GetMethodID(clazz, "xtraDownloadRequest", "()V");
method_reportNiNotification = env->GetMethodID(clazz, "reportNiNotification", "(IIIIILjava/lang/String;Ljava/lang/String;IILjava/lang/String;)V"); method_reportNiNotification = env->GetMethodID(clazz, "reportNiNotification", "(IIIIILjava/lang/String;Ljava/lang/String;IILjava/lang/String;)V");
} }
@@ -280,14 +299,15 @@ static void android_location_GpsLocationProvider_cleanup(JNIEnv* env, jobject ob
sGpsInterface->cleanup(); sGpsInterface->cleanup();
} }
static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj, jint positionMode, static jboolean android_location_GpsLocationProvider_set_position_mode(JNIEnv* env, jobject obj,
jboolean singleFix, jint fixFrequency) jint mode, jint recurrence, jint min_interval, jint preferred_accuracy, jint preferred_time)
{ {
int result = sGpsInterface->set_position_mode(positionMode, (singleFix ? 0 : fixFrequency)); return (sGpsInterface->set_position_mode(mode, recurrence, min_interval, preferred_accuracy,
if (result) { preferred_time) == 0);
return false; }
}
static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)
{
return (sGpsInterface->start() == 0); return (sGpsInterface->start() == 0);
} }
@@ -375,6 +395,9 @@ static void android_location_GpsLocationProvider_wait_for_event(JNIEnv* env, job
extras extras
); );
} }
if (pendingCallbacks & kSetCapabilities) {
env->CallVoidMethod(obj, method_setEngineCapabilities, sEngineCapabilities);
}
} }
static jint android_location_GpsLocationProvider_read_sv_status(JNIEnv* env, jobject obj, static jint android_location_GpsLocationProvider_read_sv_status(JNIEnv* env, jobject obj,
@@ -539,7 +562,8 @@ static JNINativeMethod sMethods[] = {
{"native_init", "()Z", (void*)android_location_GpsLocationProvider_init}, {"native_init", "()Z", (void*)android_location_GpsLocationProvider_init},
{"native_disable", "()V", (void*)android_location_GpsLocationProvider_disable}, {"native_disable", "()V", (void*)android_location_GpsLocationProvider_disable},
{"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup}, {"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup},
{"native_start", "(IZI)Z", (void*)android_location_GpsLocationProvider_start}, {"native_set_position_mode", "(IIIII)Z", (void*)android_location_GpsLocationProvider_set_position_mode},
{"native_start", "()Z", (void*)android_location_GpsLocationProvider_start},
{"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop}, {"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop},
{"native_delete_aiding_data", "(I)V", (void*)android_location_GpsLocationProvider_delete_aiding_data}, {"native_delete_aiding_data", "(I)V", (void*)android_location_GpsLocationProvider_delete_aiding_data},
{"native_wait_for_event", "()V", (void*)android_location_GpsLocationProvider_wait_for_event}, {"native_wait_for_event", "()V", (void*)android_location_GpsLocationProvider_wait_for_event},