SUPL-2.0-con-034-1/2 test case fails

SUPL-2.0-con-034-1/2 Fail because Emergency-NI
did not check inprogress session

Change-Id: I377955c455830bf3bb7cb7fe05d45c4536dabfbc
CRs-fixed: 2483780
diff --git a/core/LocAdapterBase.cpp b/core/LocAdapterBase.cpp
index 5864fa8..ffd3d5a 100644
--- a/core/LocAdapterBase.cpp
+++ b/core/LocAdapterBase.cpp
@@ -154,7 +154,9 @@
 DEFAULT_IMPL(false)
 
 bool LocAdapterBase::
-    requestNiNotifyEvent(const GnssNiNotification &/*notify*/, const void* /*data*/)
+    requestNiNotifyEvent(const GnssNiNotification &/*notify*/,
+                         const void* /*data*/,
+                         const LocInEmergency emergencyState)
 DEFAULT_IMPL(false)
 
 void LocAdapterBase::
diff --git a/core/LocAdapterBase.h b/core/LocAdapterBase.h
index 13c8493..b9b1f1e 100644
--- a/core/LocAdapterBase.h
+++ b/core/LocAdapterBase.h
@@ -171,7 +171,8 @@
     virtual bool requestATL(int connHandle, LocAGpsType agps_type,
                             LocApnTypeMask apn_type_mask);
     virtual bool releaseATL(int connHandle);
-    virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data);
+    virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data,
+                                      const LocInEmergency emergencyState);
     inline virtual bool isInSession() { return false; }
     ContextBase* getContext() const { return mContext; }
     virtual void reportGnssMeasurementsEvent(const GnssMeasurements& gnssMeasurements,
diff --git a/core/LocApiBase.cpp b/core/LocApiBase.cpp
index a521ceb..029f57b 100644
--- a/core/LocApiBase.cpp
+++ b/core/LocApiBase.cpp
@@ -514,10 +514,14 @@
     TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->releaseATL(connHandle));
 }
 
-void LocApiBase::requestNiNotify(GnssNiNotification &notify, const void* data)
+void LocApiBase::requestNiNotify(GnssNiNotification &notify, const void* data,
+                                 const LocInEmergency emergencyState)
 {
     // loop through adapters, and deliver to the first handling adapter.
-    TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestNiNotifyEvent(notify, data));
+    TO_1ST_HANDLING_LOCADAPTERS(
+            mLocAdapters[i]->requestNiNotifyEvent(notify,
+                                                  data,
+                                                  emergencyState));
 }
 
 void* LocApiBase :: getSibling()
diff --git a/core/LocApiBase.h b/core/LocApiBase.h
index 1c2afaf..8c885f7 100644
--- a/core/LocApiBase.h
+++ b/core/LocApiBase.h
@@ -177,7 +177,8 @@
     void requestLocation();
     void requestATL(int connHandle, LocAGpsType agps_type, LocApnTypeMask apn_type_mask);
     void releaseATL(int connHandle);
-    void requestNiNotify(GnssNiNotification &notify, const void* data);
+    void requestNiNotify(GnssNiNotification &notify, const void* data,
+                         const LocInEmergency emergencyState);
     void reportGnssMeasurements(GnssMeasurements& gnssMeasurements, int msInWeek);
     void reportWwanZppFix(LocGpsLocation &zppLoc);
     void reportZppBestAvailableFix(LocGpsLocation &zppLoc, GpsLocationExtended &location_extended,
diff --git a/gnss/GnssAdapter.cpp b/gnss/GnssAdapter.cpp
index 9413def..2481dd5 100644
--- a/gnss/GnssAdapter.cpp
+++ b/gnss/GnssAdapter.cpp
@@ -3508,7 +3508,8 @@
 }
 
 bool
-GnssAdapter::requestNiNotifyEvent(const GnssNiNotification &notify, const void* data)
+GnssAdapter::requestNiNotifyEvent(const GnssNiNotification &notify, const void* data,
+                                  const LocInEmergency emergencyState)
 {
     LOC_LOGI("%s]: notif_type: %d, timeout: %d, default_resp: %d"
              "requestor_id: %s (encoding: %d) text: %s text (encoding: %d) extras: %s",
@@ -3521,37 +3522,49 @@
         LocApiBase& mApi;
         const GnssNiNotification mNotify;
         const void* mData;
+        const LocInEmergency mEmergencyState;
         inline MsgReportNiNotify(GnssAdapter& adapter,
                                  LocApiBase& api,
                                  const GnssNiNotification& notify,
-                                 const void* data) :
+                                 const void* data,
+                                 const LocInEmergency emergencyState) :
             LocMsg(),
             mAdapter(adapter),
             mApi(api),
             mNotify(notify),
-            mData(data) {}
+            mData(data),
+            mEmergencyState(emergencyState) {}
         inline virtual void proc() const {
+            bool bIsInEmergency = false;
+            bool bInformNiAccept = false;
+
+            bIsInEmergency = ((LOC_IN_EMERGENCY_UNKNOWN == mEmergencyState) &&
+                    mAdapter.getE911State()) ||                // older modems
+                    (LOC_IN_EMERGENCY_SET == mEmergencyState); // newer modems
+
             if (GNSS_NI_TYPE_EMERGENCY_SUPL == mNotify.type) {
-                if (mAdapter.getE911State() ||
-                    (GNSS_CONFIG_SUPL_EMERGENCY_SERVICES_NO == ContextBase::mGps_conf.SUPL_ES)) {
-                    mApi.informNiResponse(GNSS_NI_RESPONSE_ACCEPT, mData);
+                bInformNiAccept = bIsInEmergency ||
+                        (GNSS_CONFIG_SUPL_EMERGENCY_SERVICES_NO == ContextBase::mGps_conf.SUPL_ES);
+
+                if (bInformNiAccept) {
+                    mAdapter.requestNiNotify(mNotify, mData, bInformNiAccept);
                 } else {
                     mApi.informNiResponse(GNSS_NI_RESPONSE_DENY, mData);
                 }
             } else if (GNSS_NI_TYPE_CONTROL_PLANE == mNotify.type) {
-                if (mAdapter.getE911State() &&
-                    (1 == ContextBase::mGps_conf.CP_MTLR_ES)) {
+                if (bIsInEmergency && (1 == ContextBase::mGps_conf.CP_MTLR_ES)) {
                     mApi.informNiResponse(GNSS_NI_RESPONSE_ACCEPT, mData);
-                } else {
-                    mAdapter.requestNiNotify(mNotify, mData);
+                }
+                else {
+                    mAdapter.requestNiNotify(mNotify, mData, false);
                 }
             } else {
-                mAdapter.requestNiNotify(mNotify, mData);
+                mAdapter.requestNiNotify(mNotify, mData, false);
             }
         }
     };
 
-    sendMsg(new MsgReportNiNotify(*this, *mLocApi, notify, data));
+    sendMsg(new MsgReportNiNotify(*this, *mLocApi, notify, data, emergencyState));
 
     return true;
 }
@@ -3674,7 +3687,8 @@
 }
 
 bool
-GnssAdapter::requestNiNotify(const GnssNiNotification& notify, const void* data)
+GnssAdapter::requestNiNotify(const GnssNiNotification& notify, const void* data,
+                             const bool bInformNiAccept)
 {
     NiSession* pSession = NULL;
     gnssNiCallback gnssNiCb = nullptr;
@@ -3686,6 +3700,20 @@
         }
     }
     if (nullptr == gnssNiCb) {
+        if (GNSS_NI_TYPE_EMERGENCY_SUPL == notify.type) {
+            if (bInformNiAccept) {
+                mLocApi->informNiResponse(GNSS_NI_RESPONSE_ACCEPT, data);
+                NiData& niData = getNiData();
+                // ignore any SUPL NI non-Es session if a SUPL NI ES is accepted
+                if (NULL != niData.session.rawRequest) {
+                    pthread_mutex_lock(&niData.session.tLock);
+                    niData.session.resp = GNSS_NI_RESPONSE_IGNORE;
+                    niData.session.respRecvd = true;
+                    pthread_cond_signal(&niData.session.tCond);
+                    pthread_mutex_unlock(&niData.session.tLock);
+                }
+            }
+        }
         EXIT_LOG(%s, "no clients with gnssNiCb.");
         return false;
     }
diff --git a/gnss/GnssAdapter.h b/gnss/GnssAdapter.h
index 9a727a2..a44e68c 100644
--- a/gnss/GnssAdapter.h
+++ b/gnss/GnssAdapter.h
@@ -359,7 +359,8 @@
                                bool fromEngineHub=false);
     virtual void reportNmeaEvent(const char* nmea, size_t length);
     virtual void reportDataEvent(const GnssDataNotification& dataNotify, int msInWeek);
-    virtual bool requestNiNotifyEvent(const GnssNiNotification& notify, const void* data);
+    virtual bool requestNiNotifyEvent(const GnssNiNotification& notify, const void* data,
+                                      const LocInEmergency emergencyState);
     virtual void reportGnssMeasurementsEvent(const GnssMeasurements& gnssMeasurements,
                                                 int msInWeek);
     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
@@ -389,7 +390,8 @@
     void reportSv(GnssSvNotification& svNotify);
     void reportNmea(const char* nmea, size_t length);
     void reportData(GnssDataNotification& dataNotify);
-    bool requestNiNotify(const GnssNiNotification& notify, const void* data);
+    bool requestNiNotify(const GnssNiNotification& notify, const void* data,
+                         const bool bInformNiAccept);
     void reportGnssMeasurementData(const GnssMeasurementsNotification& measurements);
     void reportGnssSvIdConfig(const GnssSvIdConfig& config);
     void reportGnssSvTypeConfig(const GnssSvTypeConfig& config);
diff --git a/utils/gps_extended_c.h b/utils/gps_extended_c.h
index 07d67ed..90ec339 100644
--- a/utils/gps_extended_c.h
+++ b/utils/gps_extended_c.h
@@ -457,6 +457,12 @@
     LOC_RELIABILITY_HIGH = 4
 }LocReliability;
 
+typedef enum {
+    LOC_IN_EMERGENCY_UNKNOWN = 0,
+    LOC_IN_EMERGENCY_SET = 1,
+    LOC_IN_EMERGENCY_NOT_SET = 2
+}LocInEmergency;
+
 typedef struct {
     struct timespec32_t apTimeStamp;
     /*boottime received from pps-ktimer*/