Merge "GNSS O Features according to go/o-gps-hal"
diff --git a/gnss/1.0/IGnssCallback.hal b/gnss/1.0/IGnssCallback.hal
index b072807..eb66d78 100644
--- a/gnss/1.0/IGnssCallback.hal
+++ b/gnss/1.0/IGnssCallback.hal
@@ -29,21 +29,21 @@
          * If this is not set, then the framework will use 1000ms for
          * minInterval and will call start() and stop() to schedule the GNSS.
          */
-        SCHEDULING     = 1 << 0,
+        SCHEDULING                      = 1 << 0,
         /** GNSS supports MS-Based AGNSS mode */
-        MSB            = 1 << 1,
+        MSB                             = 1 << 1,
         /** GNSS supports MS-Assisted AGNSS mode */
-        MSA            = 1 << 2,
+        MSA                             = 1 << 2,
         /** GNSS supports single-shot fixes */
-        SINGLE_SHOT    = 1 << 3,
+        SINGLE_SHOT                     = 1 << 3,
         /** GNSS supports on demand time injection */
-        ON_DEMAND_TIME = 1 << 4,
+        ON_DEMAND_TIME                  = 1 << 4,
         /** GNSS supports Geofencing  */
-        GEOFENCING     = 1 << 5,
-        /** GNSS supports Measurements. */
-        MEASUREMENTS   = 1 << 6,
+        GEOFENCING                      = 1 << 5,
+        /** GNSS supports Measurements for at least GPS. */
+        MEASUREMENTS                    = 1 << 6,
         /** GNSS supports Navigation Messages */
-        NAV_MESSAGES   = 1 << 7,
+        NAV_MESSAGES                    = 1 << 7
     };
 
     /* GNSS status event values. */
@@ -64,10 +64,11 @@
      * Flags that indicate information about the satellite
      */
     enum GnssSvFlags : uint8_t {
-        NONE                = 0,
-        HAS_EPHEMERIS_DATA  = 1 << 0,
-        HAS_ALMANAC_DATA    = 1 << 1,
-        USED_IN_FIX         = 1 << 2
+        NONE                  = 0,
+        HAS_EPHEMERIS_DATA    = 1 << 0,
+        HAS_ALMANAC_DATA      = 1 << 1,
+        USED_IN_FIX           = 1 << 2,
+        HAS_CARRIER_FREQUENCY = 1 << 3
     };
 
     struct GnssSvInfo {
@@ -109,6 +110,17 @@
         float azimuthDegrees;
 
         /*
+         * Carrier frequency of the signal tracked, for example it can be the
+         * GPS L1 = 1.57542e9 Hz, or L2, L5, varying GLO channels, etc. If
+         * the field is not set, it is the primary common use frequency,
+         * e.g. L1 for GPS.
+         *
+         * If the data is available, gnssClockFlags must contain
+         * HAS_CARRIER_FREQUENCY.
+         */
+        float carrierFrequencyHz;
+
+        /*
          * Contains additional data about the given SV.
          */
         bitfield<GnssSvFlags> svFlag;
diff --git a/gnss/1.0/IGnssDebug.hal b/gnss/1.0/IGnssDebug.hal
index ff9ea9f..8784d1a 100644
--- a/gnss/1.0/IGnssDebug.hal
+++ b/gnss/1.0/IGnssDebug.hal
@@ -45,11 +45,28 @@
         double longitudeDegrees;
         /* Altitude above ellipsoid expressed in meters */
         float altitudeMeters;
+        /* Represents speed in meters per second. */
+        float speedMetersPerSec;
+        /* Represents heading in degrees. */
+        float bearingDegrees;
         /*
          * estimated horizontal accuracy of position expressed in meters, radial,
          * 68% confidence.
          */
-        double accuracyMeters;
+        double horizontalAccuracyMeters;
+        /*
+         * estimated vertical accuracy of position expressed in meters, with
+         * 68% confidence.
+         */
+        double verticalAccuracyMeters;
+        /*
+         * estimated speed accuracy in meters per second with 68% confidence.
+         */
+        double speedAccuracyMetersPerSecond;
+        /*
+         * estimated bearing accuracy degrees with 68% confidence.
+         */
+        double bearingAccuracyDegrees;
         /*
          * Time duration before this report that this position information was
          * valid.
@@ -103,7 +120,7 @@
         TimeDebug time;
         /*
          * Provides a list of the decoded satellite ephemeris.
-         * Should provide a complete list for all constellations device can track,
+         * Must provide a complete list for all constellations device can track,
          * including GnssConstellationType UNKNOWN.
          */
         vec<SatelliteData> satelliteDataArray;
diff --git a/gnss/1.0/IGnssMeasurementCallback.hal b/gnss/1.0/IGnssMeasurementCallback.hal
index 9685942..de640ae 100644
--- a/gnss/1.0/IGnssMeasurementCallback.hal
+++ b/gnss/1.0/IGnssMeasurementCallback.hal
@@ -51,7 +51,9 @@
         /** A valid 'carrier phase' is stored in the data structure. */
         HAS_CARRIER_PHASE              = 1 << 11,
         /** A valid 'carrier phase uncertainty' is stored in the data structure. */
-        HAS_CARRIER_PHASE_UNCERTAINTY  = 1 << 12
+        HAS_CARRIER_PHASE_UNCERTAINTY  = 1 << 12,
+        /** A valid automatic gain control is stored in the data structure. */
+        HAS_AUTOMATIC_GAIN_CONTROL     = 1 << 13
     };
 
     /*
@@ -95,7 +97,9 @@
         STATE_GAL_E1BC_CODE_LOCK     = 1 << 10,
         STATE_GAL_E1C_2ND_CODE_LOCK  = 1 << 11,
         STATE_GAL_E1B_PAGE_SYNC      = 1 << 12,
-        STATE_SBAS_SYNC              = 1 << 13
+        STATE_SBAS_SYNC              = 1 << 13,
+        STATE_TOW_KNOWN              = 1 << 14,
+        STATE_GLO_TOD_KNOWN          = 1 << 15,
     };
 
     /*
@@ -321,6 +325,11 @@
          * Bit sync        : [ 0 20ms  ] : STATE_BIT_SYNC set
          * Subframe sync   : [ 0  6s   ] : STATE_SUBFRAME_SYNC set
          * TOW decoded     : [ 0 1week ] : STATE_TOW_DECODED set
+         * TOW Known       : [ 0 1week ] : STATE_TOW_KNOWN set
+         *
+         * Note: TOW Known refers to the case where TOW is possibly not decoded
+         * over the air but has been determined from other sources. If TOW
+         * decoded is set then TOW Known must also be set.
          *
          * Note: If there is any ambiguity in integer millisecond,
          * GNSS_MEASUREMENT_STATE_MSEC_AMBIGUOUS must be set accordingly, in the
@@ -333,33 +342,48 @@
          *
          * Given the highest sync state that can be achieved, per each satellite,
          * valid range for this field can be:
-         * Searching       : [ 0       ] : STATE_UNKNOWN set
-         * C/A code lock   : [ 0   1ms ] : STATE_CODE_LOCK set
-         * Symbol sync     : [ 0  10ms ] : STATE_SYMBOL_SYNC set
-         * Bit sync        : [ 0  20ms ] : STATE_BIT_SYNC set
-         * String sync     : [ 0    2s ] : STATE_GLO_STRING_SYNC set
-         * Time of day     : [ 0  1day ] : STATE_GLO_TOW_DECODED set
+         * Searching           : [ 0       ] : STATE_UNKNOWN set
+         * C/A code lock       : [ 0   1ms ] : STATE_CODE_LOCK set
+         * Symbol sync         : [ 0  10ms ] : STATE_SYMBOL_SYNC set
+         * Bit sync            : [ 0  20ms ] : STATE_BIT_SYNC set
+         * String sync         : [ 0    2s ] : STATE_GLO_STRING_SYNC set
+         * Time of day decoded : [ 0  1day ] : STATE_GLO_TOD_DECODED set
+         * Time of day known   : [ 0  1day ] : STATE_GLO_TOD_KNOWN set
+         *
+         * Note: Time of day known refers to the case where it is possibly not
+         * decoded over the air but has been determined from other sources. If
+         * Time of day decoded is set then Time of day known must also be set.
          *
          * For Beidou, this is the received Beidou time of week,
          * at the measurement time in nanoseconds.
          *
          * Given the highest sync state that can be achieved, per each satellite,
          * valid range for this field can be:
-         * Searching    : [ 0       ] : STATE_UNKNOWN set.
-         * C/A code lock: [ 0   1ms ] : STATE_CODE_LOCK set.
-         * Bit sync (D2): [ 0   2ms ] : STATE_BDS_D2_BIT_SYNC set.
-         * Bit sync (D1): [ 0  20ms ] : STATE_BIT_SYNC set.
-         * Subframe (D2): [ 0  0.6s ] : STATE_BDS_D2_SUBFRAME_SYNC set.
-         * Subframe (D1): [ 0    6s ] : STATE_SUBFRAME_SYNC set.
-         * Time of week : [ 0 1week ] : STATE_TOW_DECODED set.
+         * Searching            : [ 0       ] : STATE_UNKNOWN set.
+         * C/A code lock        : [ 0   1ms ] : STATE_CODE_LOCK set.
+         * Bit sync (D2)        : [ 0   2ms ] : STATE_BDS_D2_BIT_SYNC set.
+         * Bit sync (D1)        : [ 0  20ms ] : STATE_BIT_SYNC set.
+         * Subframe (D2)        : [ 0  0.6s ] : STATE_BDS_D2_SUBFRAME_SYNC set.
+         * Subframe (D1)        : [ 0    6s ] : STATE_SUBFRAME_SYNC set.
+         * Time of week decoded : [ 0 1week ] : STATE_TOW_DECODED set.
+         * Time of week known   : [ 0 1week ] : STATE_TOW_KNOWN set
+         *
+         * Note: TOW Known refers to the case where TOW is possibly not decoded
+         * over the air but has been determined from other sources. If TOW
+         * decoded is set then TOW Known must also be set.
          *
          * For Galileo, this is the received Galileo time of week,
          * at the measurement time in nanoseconds.
          *
-         * E1BC code lock   : [ 0  4ms ] : STATE_GAL_E1BC_CODE_LOCK set.
-         * E1C 2nd code lock: [ 0 100ms] : STATE_GAL_E1C_2ND_CODE_LOCK set.
-         * E1B page         : [ 0   2s ] : STATE_GAL_E1B_PAGE_SYNC set.
-         * Time of week     : [ 0 1week] : STATE_TOW_DECODED is set.
+         * E1BC code lock       : [ 0  4ms ] : STATE_GAL_E1BC_CODE_LOCK set.
+         * E1C 2nd code lock    : [ 0 100ms] : STATE_GAL_E1C_2ND_CODE_LOCK set.
+         * E1B page             : [ 0   2s ] : STATE_GAL_E1B_PAGE_SYNC set.
+         * Time of week decoded : [ 0 1week] : STATE_TOW_DECODED is set.
+         * Time of week known   : [ 0 1week] : STATE_TOW_KNOWN set
+         *
+         * Note: TOW Known refers to the case where TOW is possibly not decoded
+         * over the air but has been determined from other sources. If TOW
+         * decoded is set then TOW Known must also be set.
          *
          * For SBAS, this is received SBAS time, at the measurement time in
          * nanoseconds.
@@ -455,9 +479,10 @@
         double accumulatedDeltaRangeUncertaintyM;
 
         /*
-         * Carrier frequency at which codes and messages are modulated, it can
-         * be L1 or L2. If the field is not set, the carrier frequency is
-         * assumed to be L1.
+         * Carrier frequency of the signal tracked, for example it can be the
+         * GPS L1 = 1.57542e9 Hz, or L2, L5, varying GLO channels, etc. If the
+         * field is not set, it is the primary common use frequency,
+         * e.g. L1 for GPS.
          *
          * If the data is available, gnssClockFlags must contain
          * HAS_CARRIER_FREQUENCY.
@@ -524,6 +549,24 @@
          * observed noise floor" to "the noise RMS".
          */
         double snrDb;
+
+        /*
+         * Automatic gain control (AGC) level. AGC acts as a variable gain
+         * amplifier adjusting the power of the incoming signal to minimize the
+         * quantization losses. The AGC level may be used to indicate potential
+         * interference. When AGC is at a nominal level, this value
+         * must be set as 0. Higher gain (and/or lower input power) must be
+         * output as a positive number. Hence in cases of strong jamming, in the
+         * band of this signal, this value must go more negative.
+         *
+         * Note: Different hardware designs (e.g. antenna, pre-amplification, or
+         * other RF HW components) may also affect the typical output of of this
+         * value on any given hardware design in an open sky test - the
+         * important aspect of this output is that changes in this value are
+         * indicative of changes on input signal power in the frequency band for
+         * this measurement.
+         */
+        double agcLevelDb;
     };
 
     /*
diff --git a/gnss/1.0/default/Gnss.cpp b/gnss/1.0/default/Gnss.cpp
index 28a1950..19e22c2 100644
--- a/gnss/1.0/default/Gnss.cpp
+++ b/gnss/1.0/default/Gnss.cpp
@@ -121,7 +121,10 @@
             .cN0Dbhz = svInfo.c_n0_dbhz,
             .elevationDegrees = svInfo.elevation,
             .azimuthDegrees = svInfo.azimuth,
-            .svFlag = svInfo.flags
+            .svFlag = svInfo.flags,
+            // Older chipsets do not provide carrier frequency, hence HAS_CARRIER_FREQUENCY flag
+            // is not set and the carrierFrequencyHz field is set to zero
+            .carrierFrequencyHz = 0
         };
         svStatus.gnssSvList[i] = gnssSvInfo;
     }
diff --git a/gnss/1.0/default/GnssMeasurement.cpp b/gnss/1.0/default/GnssMeasurement.cpp
index 0d11636..67f6d8d 100644
--- a/gnss/1.0/default/GnssMeasurement.cpp
+++ b/gnss/1.0/default/GnssMeasurement.cpp
@@ -51,12 +51,19 @@
 
     for (size_t i = 0; i < gnssData.measurementCount; i++) {
         auto entry = legacyGnssData->measurements[i];
+        auto state = static_cast<GnssMeasurementState>(entry.state);
+        if (state & IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED) {
+          state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_KNOWN;
+        }
+        if (state & IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED) {
+          state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_KNOWN;
+        }
         gnssData.measurements[i] = {
             .flags = entry.flags,
             .svid = entry.svid,
             .constellation = static_cast<GnssConstellationType>(entry.constellation),
             .timeOffsetNs = entry.time_offset_ns,
-            .state = entry.state,
+            .state = state,
             .receivedSvTimeInNs = entry.received_sv_time_in_ns,
             .receivedSvTimeUncertaintyInNs = entry.received_sv_time_uncertainty_in_ns,
             .cN0DbHz = entry.c_n0_dbhz,
diff --git a/gnss/1.0/default/GnssUtils.cpp b/gnss/1.0/default/GnssUtils.cpp
index 9f7e356..4b3ca44 100644
--- a/gnss/1.0/default/GnssUtils.cpp
+++ b/gnss/1.0/default/GnssUtils.cpp
@@ -34,7 +34,13 @@
             .altitudeMeters = location->altitude,
             .speedMetersPerSec = location->speed,
             .bearingDegrees = location->bearing,
-            .accuracyMeters = location->accuracy,
+            .horizontalAccuracyMeters = location->accuracy,
+            // Older chipsets do not provide the following 3 fields, hence the flags
+            // HAS_VERTICAL_ACCURACY, HAS_SPEED_ACCURACY and HAS_BEARING_ACCURACY are
+            // not set and the field are set to zeros.
+            .verticalAccuracyMeters = 0,
+            .speedAccuracyMetersPerSecond = 0,
+            .bearingAccuracyDegrees = 0,
             .timestamp = location->timestamp
         };
     }
diff --git a/gnss/1.0/types.hal b/gnss/1.0/types.hal
index ea104c5..2721d44 100644
--- a/gnss/1.0/types.hal
+++ b/gnss/1.0/types.hal
@@ -40,15 +40,21 @@
 /** Bit mask to indicate which values are valid in a GnssLocation object. */
 enum GnssLocationFlags : uint16_t {
     /** GnssLocation has valid latitude and longitude. */
-    HAS_LAT_LONG = 0x0001,
+    HAS_LAT_LONG              = 0x0001,
     /** GnssLocation has valid altitude. */
-    HAS_ALTITUDE = 0x0002,
+    HAS_ALTITUDE              = 0x0002,
     /** GnssLocation has valid speed. */
-    HAS_SPEED    = 0x0004,
+    HAS_SPEED                 = 0x0004,
     /** GnssLocation has valid bearing. */
-    HAS_BEARING  = 0x0008,
-    /** GnssLocation has valid accuracy. */
-    HAS_ACCURACY = 0x0010
+    HAS_BEARING               = 0x0008,
+    /** GpsLocation has valid horizontal accuracy. */
+    HAS_HORIZONTAL_ACCURACY   = 0x0010,
+    /** GpsLocation has valid vertical accuracy. */
+    HAS_VERTICAL_ACCURACY     = 0x0020,
+    /** GpsLocation has valid speed accuracy. */
+    HAS_SPEED_ACCURACY        = 0x0040,
+    /** GpsLocation has valid bearing accuracy. */
+    HAS_BEARING_ACCURACY      = 0x0080
 };
 
 /* Represents a location. */
@@ -73,8 +79,29 @@
     /* Represents heading in degrees. */
     float bearingDegrees;
 
-    /* Represents expected accuracy in meters. */
-    float accuracyMeters;
+    /*
+    * Represents expected horizontal position accuracy, radial, in meters
+    * (68% confidence).
+    */
+    float horizontalAccuracyMeters;
+
+    /*
+    * Represents expected vertical position accuracy in meters
+    * (68% confidence).
+    */
+    float verticalAccuracyMeters;
+
+    /*
+    * Represents expected speed accuracy in meter per seconds
+    * (68% confidence).
+    */
+    float speedAccuracyMetersPerSecond;
+
+    /*
+    * Represents expected bearing accuracy in degrees
+    * (68% confidence).
+    */
+    float bearingAccuracyDegrees;
 
     /* Timestamp for the location fix. */
     GnssUtcTime timestamp;