wlan: RSSI based TDLS tear down.

If the RSSI value goes beyond the threshold value configured
in the ini then tear down the TDLS link.
CRs-Fixed: 458592

Change-Id: I134b2e5f0a81d7c08f3d69981fd9a970e8272a55
diff --git a/CORE/HDD/inc/wlan_hdd_cfg.h b/CORE/HDD/inc/wlan_hdd_cfg.h
index ff7ab7c..f9f25ca 100644
--- a/CORE/HDD/inc/wlan_hdd_cfg.h
+++ b/CORE/HDD/inc/wlan_hdd_cfg.h
@@ -1569,8 +1569,8 @@
 
 #define CFG_TDLS_RSSI_TRIGGER_THRESHOLD             "gTDLSRSSITriggerThreshold"
 #define CFG_TDLS_RSSI_TRIGGER_THRESHOLD_MIN         ( -120 )
-#define CFG_TDLS_RSSI_TRIGGER_THRESHOLD_MAX         ( 100 )
-#define CFG_TDLS_RSSI_TRIGGER_THRESHOLD_DEFAULT     ( 0 )
+#define CFG_TDLS_RSSI_TRIGGER_THRESHOLD_MAX         ( 0 )
+#define CFG_TDLS_RSSI_TRIGGER_THRESHOLD_DEFAULT     ( -75 )
 
 #define CFG_TDLS_RSSI_TEARDOWN_THRESHOLD            "gTDLSRSSITeardownThreshold"
 #define CFG_TDLS_RSSI_TEARDOWN_THRESHOLD_MIN        ( -120 )
diff --git a/CORE/HDD/src/wlan_hdd_p2p.c b/CORE/HDD/src/wlan_hdd_p2p.c
index 0b751c7..060d36e 100644
--- a/CORE/HDD/src/wlan_hdd_p2p.c
+++ b/CORE/HDD/src/wlan_hdd_p2p.c
@@ -1500,13 +1500,17 @@
 #ifdef FEATURE_WLAN_TDLS
             else if(pbFrames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET+1] == WLAN_HDD_PUBLIC_ACTION_TDLS_DISC_RESP)
             {
-                wlan_hdd_tdls_recv_discovery_resp(pAdapter, &pbFrames[WLAN_HDD_80211_FRM_DA_OFFSET+6]);
-                wlan_hdd_tdls_set_rssi(pAdapter, &pbFrames[WLAN_HDD_80211_FRM_DA_OFFSET+6], rxRssi);
-                hddLog(VOS_TRACE_LEVEL_ERROR,"[TDLS] TDLS Discovery Response <--- OTA");
+                u8 *mac = &pbFrames[WLAN_HDD_80211_FRM_DA_OFFSET+6];
+#ifdef WLAN_FEATURE_TDLS_DEBUG
+                hddLog(VOS_TRACE_LEVEL_ERROR,"[TDLS] TDLS Discovery Response," MAC_ADDRESS_STR " RSSI[%d] <--- OTA",
+                 MAC_ADDR_ARRAY(mac),rxRssi);
+#endif
+                wlan_hdd_tdls_set_rssi(pAdapter, mac, rxRssi);
+                wlan_hdd_tdls_recv_discovery_resp(pAdapter, mac);
             }
 #endif
         }
-#ifdef FEATURE_WLAN_TDLS
+#ifdef WLAN_FEATURE_TDLS_DEBUG
         if(pbFrames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET] == WLAN_HDD_TDLS_ACTION_FRAME)
         {
             actionFrmType = pbFrames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET+1];
diff --git a/CORE/HDD/src/wlan_hdd_tdls.c b/CORE/HDD/src/wlan_hdd_tdls.c
index b2651d9..14b2b8e 100644
--- a/CORE/HDD/src/wlan_hdd_tdls.c
+++ b/CORE/HDD/src/wlan_hdd_tdls.c
@@ -298,28 +298,6 @@
                         }
                         goto next_peer;
                     }
-                    if ((((tANI_S32)curr_peer->rssi >
-                            (tANI_S32)(pHddTdlsCtx->threshold_config.rssi_hysteresis +
-                                pHddTdlsCtx->ap_rssi)) ||
-                         ((tANI_S32)(curr_peer->rssi >
-                            pHddTdlsCtx->threshold_config.rssi_trigger_threshold))) &&
-                         (HDD_MAX_NUM_TDLS_STA > wlan_hdd_tdlsConnectedPeers(pHddTdlsCtx->pAdapter))){
-
-                        VOS_TRACE( VOS_MODULE_ID_HDD, TDLS_LOG_LEVEL,
-                                "%s: RSSI (peer %d > ap %d + hysteresis %d) triggering to %02x:%02x:%02x:%02x:%02x:%02x ",
-                                __func__, (tANI_S32)curr_peer->rssi,
-                                pHddTdlsCtx->ap_rssi,
-                                (tANI_S32)(pHddTdlsCtx->threshold_config.rssi_hysteresis),
-                                curr_peer->peerMac[0], curr_peer->peerMac[1], curr_peer->peerMac[2],
-                                curr_peer->peerMac[3], curr_peer->peerMac[4], curr_peer->peerMac[5]);
-
-#ifdef CONFIG_TDLS_IMPLICIT
-                        cfg80211_tdls_oper_request(pHddTdlsCtx->pAdapter->dev,
-                                                   curr_peer->peerMac,
-                                                   NL80211_TDLS_SETUP, FALSE,
-                                                   GFP_KERNEL);
-#endif
-                    }
                 } else {
                     if ((tANI_S32)curr_peer->rssi <
                         (tANI_S32)pHddTdlsCtx->threshold_config.rssi_teardown_threshold) {
@@ -811,14 +789,25 @@
 
     if (eTDLS_LINK_DISCOVERING == curr_peer->link_status)
     {
-        curr_peer->link_status = eTDLS_LINK_DISCOVERED;
-
-        VOS_TRACE( VOS_MODULE_ID_HDD, TDLS_LOG_LEVEL,
-                   "Indicating Set-Up to supplicant");
-        cfg80211_tdls_oper_request(pAdapter->dev,
-                                   curr_peer->peerMac,
-                                   NL80211_TDLS_SETUP, FALSE,
-                                   GFP_KERNEL);
+        /* Since we are here, it means Throughput threshold is alredy met. Make sure RSSI
+           threshold is also met before setting up TDLS link*/
+        if ((tANI_S32) curr_peer->rssi > (tANI_S32) pHddTdlsCtx->threshold_config.rssi_trigger_threshold)
+        {
+            curr_peer->link_status = eTDLS_LINK_DISCOVERED;
+            VOS_TRACE( VOS_MODULE_ID_HDD, TDLS_LOG_LEVEL,
+            "Rssi Threshold met: "MAC_ADDRESS_STR" rssi = %d threshold= %d" ,
+             MAC_ADDR_ARRAY(curr_peer->peerMac), curr_peer->rssi,
+             pHddTdlsCtx->threshold_config.rssi_trigger_threshold);
+            cfg80211_tdls_oper_request(pAdapter->dev, curr_peer->peerMac, NL80211_TDLS_SETUP, FALSE, GFP_KERNEL);
+        }
+        else
+        {
+            VOS_TRACE( VOS_MODULE_ID_HDD, TDLS_LOG_LEVEL, 
+            "Rssi Threshold not met: "MAC_ADDRESS_STR" rssi = %d threshold = %d ",
+            MAC_ADDR_ARRAY(curr_peer->peerMac), curr_peer->rssi,
+            pHddTdlsCtx->threshold_config.rssi_trigger_threshold);
+            curr_peer->link_status = eTDLS_LINK_IDLE;
+        }
     }
     else
     {
@@ -833,7 +822,7 @@
 {
     hddTdlsPeer_t *curr_peer;
 
-    curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac);
+    curr_peer = wlan_hdd_tdls_find_peer(pAdapter, mac);
     if (curr_peer == NULL)
         return -1;
 
diff --git a/CORE/HDD/src/wlan_hdd_tx_rx.c b/CORE/HDD/src/wlan_hdd_tx_rx.c
index fb312d3..10c07d1 100644
--- a/CORE/HDD/src/wlan_hdd_tx_rx.c
+++ b/CORE/HDD/src/wlan_hdd_tx_rx.c
@@ -1400,11 +1400,17 @@
                       "rx broadcast packet, not adding to peer list");
         } else if (memcmp(pHddStaCtx->conn_info.bssId,
                             mac, 6) != 0) {
+            hddTdlsPeer_t *curr_peer;
             VOS_TRACE( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO_MED,
-                      "rx extract mac:%x %x %x %x %x %x",
-                      mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
-
-            wlan_hdd_tdls_increment_pkt_count(pAdapter, mac, 0);
+                      "rx extract mac:" MAC_ADDRESS_STR,
+                      MAC_ADDR_ARRAY(mac) );
+            curr_peer = wlan_hdd_tdls_find_peer(pAdapter, mac);
+            if ((NULL != curr_peer) && (eTDLS_LINK_CONNECTED == curr_peer->link_status))
+            {
+                wlan_hdd_tdls_increment_pkt_count(pAdapter, mac, 0);
+                VOS_TRACE( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,"rssi is %d", pRxMetaInfo->rssiAvg);
+                wlan_hdd_tdls_set_rssi (pAdapter, mac, pRxMetaInfo->rssiAvg);
+            }
         } else {
             VOS_TRACE( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO_MED,
                        "rx packet sa is bssid, not adding to peer list");
diff --git a/CORE/TL/inc/wlan_qct_tl.h b/CORE/TL/inc/wlan_qct_tl.h
index fd9ddfe..7bf9578 100644
--- a/CORE/TL/inc/wlan_qct_tl.h
+++ b/CORE/TL/inc/wlan_qct_tl.h
@@ -157,6 +157,10 @@
 
 
 #define WLANTL_MAX_TID                        15
+/* Default RSSI average Alpha */
+#define WLANTL_HO_DEFAULT_ALPHA               5
+#define WLANTL_HO_TDLS_ALPHA                  7
+
 /*--------------------------------------------------------------------------
   Access category enum used by TL
   - order must be kept as these values are used to setup the AC mask
@@ -436,6 +440,8 @@
   v_U8_t    ucUP;
   /* Address 3 Index of the received packet */
   v_U16_t   ucDesSTAId;
+ /*Rssi based on the received packet */
+  v_S7_t    rssiAvg;
 }WLANTL_RxMetaInfoType;
 
 
diff --git a/CORE/TL/src/wlan_qct_tl.c b/CORE/TL/src/wlan_qct_tl.c
index accdeea..aa6aba8 100644
--- a/CORE/TL/src/wlan_qct_tl.c
+++ b/CORE/TL/src/wlan_qct_tl.c
@@ -1337,6 +1337,19 @@
      data to calculate RSSI. So to avoid reporting zero, we are initializing
      RSSI with RSSI saved in BssDescription during scanning. */
   pClientSTA->rssiAvg = rssi;
+#ifdef FEATURE_WLAN_TDLS
+  if(WLAN_STA_TDLS == pClientSTA->wSTADesc.wSTAType)
+  {
+    /* If client is TDLS, use TDLS specific alpha */
+    pClientSTA->rssiAlpha = WLANTL_HO_TDLS_ALPHA;
+  }
+  else
+  {
+    pClientSTA->rssiAlpha = WLANTL_HO_DEFAULT_ALPHA;
+  }
+#else
+    pClientSTA->rssiAlpha = WLANTL_HO_DEFAULT_ALPHA;
+#endif /* FEATURE_WLAN_TDLS */
 
   /*Tx not suspended and station fully registered*/
   vos_atomic_set_U8(
@@ -7742,6 +7755,7 @@
     else
     {
       wRxMetaInfo.ucUP = ucTid;
+      wRxMetaInfo.rssiAvg = pClientSTA->rssiAvg;
       pClientSTA->pfnSTARx( pvosGCtx, vosDataBuff, ucSTAId,
                                             &wRxMetaInfo );
     }
diff --git a/CORE/TL/src/wlan_qct_tl_hosupport.c b/CORE/TL/src/wlan_qct_tl_hosupport.c
index 64b584e..d6be98a 100644
--- a/CORE/TL/src/wlan_qct_tl_hosupport.c
+++ b/CORE/TL/src/wlan_qct_tl_hosupport.c
@@ -90,7 +90,6 @@
 //#define WLANTL_HO_UTEST
 
 #define WLANTL_HO_DEFAULT_RSSI      0xFF
-#define WLANTL_HO_DEFAULT_ALPHA     5
 #define WLANTL_HO_INVALID_RSSI      -100
 /* RSSI sampling period, usec based
  * To reduce performance overhead
@@ -876,28 +875,21 @@
    TLHS_UtestHandleNewRSSI(&currentRSSI, pAdapter);
 #endif /* WLANTL_HO_UTEST */
 
-/* Commenting this part of the code as this may not be necessarity true in all cases */
-#if 0
-   if(WLANTL_HO_INVALID_RSSI == currentRSSI)
-   {
-      return status;
-   }
-#endif
-
-   if(0 == currentHO->historyRSSI)
+   if(0 == tlCtxt->atlSTAClients[STAid]->rssiAvg)
    {
       *currentAvgRSSI = currentRSSI;
    }
    else
    {
-      *currentAvgRSSI = ((currentHO->historyRSSI * currentHO->alpha) +
-                         (currentRSSI * (10 - currentHO->alpha))) / 10;
+      *currentAvgRSSI = ((tlCtxt->atlSTAClients[STAid]->rssiAvg  *
+                          tlCtxt->atlSTAClients[STAid]->rssiAlpha) +
+                         (currentRSSI * (10 - tlCtxt->atlSTAClients[STAid]->rssiAlpha))) / 10;
    }
+
 #ifdef RSSI_HACK
    *currentAvgRSSI = (v_S7_t)dumpCmdRSSI;
 #endif
 
-
    tlCtxt->atlSTAClients[STAid]->rssiAvg = *currentAvgRSSI;
 
    TLLOG1(VOS_TRACE(VOS_MODULE_ID_TL, VOS_TRACE_LEVEL_INFO,"Current new RSSI is %d, averaged RSSI is %d", currentRSSI, *currentAvgRSSI));
diff --git a/CORE/TL/src/wlan_qct_tli.h b/CORE/TL/src/wlan_qct_tli.h
index 167fc2e..a8fa010 100644
--- a/CORE/TL/src/wlan_qct_tli.h
+++ b/CORE/TL/src/wlan_qct_tli.h
@@ -508,6 +508,9 @@
   /* Value of the averaged RSSI for this station in BMPS */
   v_S7_t                        rssiAvgBmps;
 
+  /* Value of the Alpha to calculate RSSI average */
+  v_S7_t                        rssiAlpha;
+
   /* Value of the averaged RSSI for this station */
   v_U32_t                       uLinkQualityAvg;