Merge "Use 0000 as pin key for PORCHE_PAIRING_CONFLICT issue"
diff --git a/audio_a2dp_hw/audio_a2dp_hw.c b/audio_a2dp_hw/audio_a2dp_hw.c
index 4e63817..fac94a4 100644
--- a/audio_a2dp_hw/audio_a2dp_hw.c
+++ b/audio_a2dp_hw/audio_a2dp_hw.c
@@ -25,6 +25,7 @@
  *****************************************************************************/
 
 #include <errno.h>
+#include <inttypes.h>
 #include <pthread.h>
 #include <stdint.h>
 #include <sys/time.h>
@@ -46,7 +47,7 @@
 
 #define LOG_TAG "audio_a2dp_hw"
 /* #define LOG_NDEBUG 0 */
-#include <cutils/log.h>
+#include <log/log.h>
 
 /*****************************************************************************
 **  Constants & Macros
@@ -440,7 +441,7 @@
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
     int sent;
 
-    DEBUG("write %d bytes (fd %d)", bytes, out->audio_fd);
+    DEBUG("write %zu bytes (fd %d)", bytes, out->audio_fd);
 
     if (out->state == AUDIO_A2DP_STATE_SUSPENDED)
     {
@@ -485,7 +486,7 @@
         out->state = AUDIO_A2DP_STATE_STOPPED;
     }
 
-    DEBUG("wrote %d bytes out of %d bytes", sent, bytes);
+    DEBUG("wrote %d bytes out of %zu bytes", sent, bytes);
     return sent;
 }
 
@@ -494,7 +495,7 @@
 {
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
 
-    DEBUG("rate %d", out->cfg.rate);
+    DEBUG("rate %" PRIu32, out->cfg.rate);
 
     return out->cfg.rate;
 }
@@ -503,7 +504,7 @@
 {
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
 
-    DEBUG("out_set_sample_rate : %d", rate);
+    DEBUG("out_set_sample_rate : %" PRIu32, rate);
 
     if (rate != AUDIO_STREAM_DEFAULT_RATE)
     {
@@ -520,7 +521,7 @@
 {
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
 
-    DEBUG("buffer_size : %d", out->buffer_sz);
+    DEBUG("buffer_size : %zu", out->buffer_sz);
 
     return out->buffer_sz;
 }
@@ -529,7 +530,7 @@
 {
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
 
-    DEBUG("channels 0x%x", out->cfg.channel_flags);
+    DEBUG("channels 0x%" PRIx32, out->cfg.channel_flags);
 
     return out->cfg.channel_flags;
 }
@@ -551,8 +552,6 @@
 static int out_standby(struct audio_stream *stream)
 {
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
-    int retval = 0;
-
     int retVal = 0;
 
     FNLOG();
diff --git a/bta/av/bta_av_aact.c b/bta/av/bta_av_aact.c
index a50fb80..53e8c03 100644
--- a/bta/av/bta_av_aact.c
+++ b/bta/av/bta_av_aact.c
@@ -2152,15 +2152,15 @@
         p_scb->q_tag = BTA_AV_Q_TAG_START;
     }
 
-    if (p_scb->wait & BTA_AV_WAIT_ACP_CAPS_ON)
-    {
-        p_scb->wait |= BTA_AV_WAIT_ACP_CAPS_STARTED;
-    }
-
     if (p_scb->wait)
     {
-        APPL_TRACE_DEBUG2("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
-        return;
+        APPL_TRACE_ERROR2("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
+        /* Clear first bit of p_scb->wait and not to return from this point else
+         * HAL layer gets blocked. And if there is delay in Get Capability response as
+         * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is not called
+         * again from bta_av_save_caps.
+        */
+        p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
     }
 
     /* tell role manager to check M/S role */
diff --git a/bta/dm/bta_dm_cfg.c b/bta/dm/bta_dm_cfg.c
index 5caf04a..91b88dd 100644
--- a/bta/dm/bta_dm_cfg.c
+++ b/bta/dm/bta_dm_cfg.c
@@ -111,9 +111,9 @@
 tBTA_DM_RM *p_bta_dm_rm_cfg = (tBTA_DM_RM *)&bta_dm_rm_cfg;
 
 #if BLE_INCLUDED == TRUE
-#define BTA_DM_NUM_PM_ENTRY         (17+BTA_DM_NUM_JV_ID)  /* number of entries in bta_dm_pm_cfg except the first */
+#define BTA_DM_NUM_PM_ENTRY         (19+BTA_DM_NUM_JV_ID)  /* number of entries in bta_dm_pm_cfg except the first */
 #else
-#define BTA_DM_NUM_PM_ENTRY         (15+BTA_DM_NUM_JV_ID)  /* number of entries in bta_dm_pm_cfg except the first */
+#define BTA_DM_NUM_PM_ENTRY         (17+BTA_DM_NUM_JV_ID)  /* number of entries in bta_dm_pm_cfg except the first */
 #endif
 
 tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_CFG bta_dm_pm_cfg[] =
@@ -135,24 +135,26 @@
   {BTA_ID_MSE, BTA_ALL_APP_ID,      7},  /* reuse fts spec table */
   {BTA_ID_JV,  BTA_JV_PM_ID_1,      6},  /* app BTA_JV_PM_ID_1, reuse ftc spec table */
   {BTA_ID_JV,  BTA_ALL_APP_ID,      7},  /* reuse fts spec table */
-  {BTA_ID_HL,  BTA_ALL_APP_ID,      8}   /* reuse fts spec table */
+  {BTA_ID_HL,  BTA_ALL_APP_ID,      8},  /* reuse fts spec table */
+  {BTA_ID_PAN, BTUI_PAN_ID_PANU,    9},  /*  PANU spec table */
+  {BTA_ID_PAN, BTUI_PAN_ID_NAP,    10}   /* NAP spec table */
 #if BLE_INCLUDED == TRUE
-  ,{BTA_ID_GATTC,  BTA_ALL_APP_ID,   9}   /* gattc spec table */
-  ,{BTA_ID_GATTS,  BTA_ALL_APP_ID,   10}  /* gatts spec table */
+  ,{BTA_ID_GATTC,  BTA_ALL_APP_ID,   11}   /* gattc spec table */
+  ,{BTA_ID_GATTS,  BTA_ALL_APP_ID,   12}  /* gatts spec table */
 #endif
 };
 
 #if BLE_INCLUDED == TRUE /* add GATT PM entry for GATT over BR/EDR  */
 #ifdef BTE_SIM_APP      /* For Insight builds only, see the detail below */
-#define BTA_DM_NUM_PM_SPEC      (11 + 2)  /* additional two */
+#define BTA_DM_NUM_PM_SPEC      (13 + 2)  /* additional two */
 #else
-#define BTA_DM_NUM_PM_SPEC      11 /* additional JV*/
+#define BTA_DM_NUM_PM_SPEC      13 /* additional JV*/
 #endif
 #else
 #ifdef BTE_SIM_APP      /* For Insight builds only, see the detail below */
-#define BTA_DM_NUM_PM_SPEC      (9 + 2)  /* additional two */
+#define BTA_DM_NUM_PM_SPEC      (11 + 2)  /* additional two */
 #else
-#define BTA_DM_NUM_PM_SPEC      9  /* additional JV*/
+#define BTA_DM_NUM_PM_SPEC      11  /* additional JV*/
 #endif
 #endif
 
@@ -328,6 +330,44 @@
       {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},   /* busy */
       {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}}    /* mode change retry */
   }
+ },
+
+  /* PANU */
+ {
+  (BTA_DM_PM_SNIFF),                                             /* allow sniff */
+#if (BTM_SSR_INCLUDED == TRUE)
+  (BTA_DM_PM_SSR2),                                              /* the SSR entry */
+#endif
+  {
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* conn open  active */
+      {{BTA_DM_PM_NO_PREF,   0},   {BTA_DM_PM_NO_ACTION, 0}},    /* conn close  */
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* app open */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* app close */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* sco open  */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* sco close   */
+      {{BTA_DM_PM_SNIFF,  7000},   {BTA_DM_PM_NO_ACTION, 0}},    /* idle */
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* busy */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}}     /* mode change retry */
+  }
+ },
+
+  /* NAP */
+ {
+  (BTA_DM_PM_SNIFF),                                             /* allow sniff */
+#if (BTM_SSR_INCLUDED == TRUE)
+  (BTA_DM_PM_SSR2),                                              /* the SSR entry */
+#endif
+  {
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* conn open  active */
+      {{BTA_DM_PM_NO_PREF,   0},   {BTA_DM_PM_NO_ACTION, 0}},    /* conn close  */
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* app open */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* app close */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* sco open  */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}},    /* sco close   */
+      {{BTA_DM_PM_SNIFF,  5000},   {BTA_DM_PM_NO_ACTION, 0}},    /* idle */
+      {{BTA_DM_PM_ACTIVE,    0},   {BTA_DM_PM_NO_ACTION, 0}},    /* busy */
+      {{BTA_DM_PM_NO_ACTION, 0},   {BTA_DM_PM_NO_ACTION, 0}}     /* mode change retry */
+  }
  }
 
 #if BLE_INCLUDED == TRUE
diff --git a/bta/pan/bta_pan_act.c b/bta/pan/bta_pan_act.c
index b29920f..24a5ccf 100644
--- a/bta/pan/bta_pan_act.c
+++ b/bta/pan/bta_pan_act.c
@@ -42,6 +42,40 @@
 #define BTA_PAN_TX_MASK              0xF0
 
 /*******************************************************************************
+ **
+ ** Function    bta_pan_pm_conn_busy
+ **
+ ** Description set pan pm connection busy state
+ **
+ ** Params      p_scb: state machine control block of pan connection
+ **
+ ** Returns     void
+ **
+ *******************************************************************************/
+static void bta_pan_pm_conn_busy(tBTA_PAN_SCB *p_scb)
+{
+    if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
+        bta_sys_busy(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
+}
+
+/*******************************************************************************
+ **
+ ** Function    bta_pan_pm_conn_idle
+ **
+ ** Description set pan pm connection idle state
+ **
+ ** Params      p_scb: state machine control block of pan connection
+ **
+ ** Returns     void
+ **
+ *******************************************************************************/
+static void bta_pan_pm_conn_idle(tBTA_PAN_SCB *p_scb)
+{
+    if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
+        bta_sys_idle(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
+}
+
+/*******************************************************************************
 **
 ** Function         bta_pan_conn_state_cback
 **
@@ -456,6 +490,8 @@
         bta_pan_scb_dealloc(p_scb);
         bdcpy(data.bd_addr, p_data->api_open.bd_addr);
         data.status = BTA_PAN_FAIL;
+        data.local_role = p_data->api_open.local_role;
+        data.peer_role = p_data->api_open.peer_role;
         bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, (tBTA_PAN *)&data);
     }
 
@@ -620,12 +656,14 @@
     /* if data path configured for tx pull */
     if ((bta_pan_cb.flow_mask & BTA_PAN_TX_MASK) == BTA_PAN_TX_PULL)
     {
+        bta_pan_pm_conn_busy(p_scb);
         /* call application callout function for tx path */
         bta_pan_co_tx_path(p_scb->handle, p_scb->app_id);
 
         /* free data that exceeds queue level */
         while(p_scb->data_queue.count > bta_pan_cb.q_level)
             GKI_freebuf(GKI_dequeue(&p_scb->data_queue));
+        bta_pan_pm_conn_idle(p_scb);
     }
     /* if configured for zero copy push */
     else if ((bta_pan_cb.flow_mask & BTA_PAN_TX_MASK) == BTA_PAN_TX_PUSH_BUF)
@@ -697,6 +735,7 @@
 {
     if ((bta_pan_cb.flow_mask & BTA_PAN_RX_MASK) == BTA_PAN_RX_PUSH_BUF)
     {
+        bta_pan_pm_conn_busy(p_scb);
 
         PAN_WriteBuf (p_scb->handle,
                       ((tBTA_PAN_DATA_PARAMS *)p_data)->dst,
@@ -704,6 +743,7 @@
                       ((tBTA_PAN_DATA_PARAMS *)p_data)->protocol,
                       (BT_HDR *)p_data,
                       ((tBTA_PAN_DATA_PARAMS *)p_data)->ext);
+        bta_pan_pm_conn_idle(p_scb);
 
     }
 }
diff --git a/bta/pan/bta_pan_int.h b/bta/pan/bta_pan_int.h
index 1667e57..bd75311 100644
--- a/bta/pan/bta_pan_int.h
+++ b/bta/pan/bta_pan_int.h
@@ -56,6 +56,14 @@
     BTA_PAN_API_OPEN_EVT
 };
 
+/* state machine states */
+enum
+{
+    BTA_PAN_IDLE_ST,
+    BTA_PAN_OPEN_ST,
+    BTA_PAN_CLOSING_ST
+};
+
 
 
 
diff --git a/bta/pan/bta_pan_main.c b/bta/pan/bta_pan_main.c
index add1c7c..bc92b36 100644
--- a/bta/pan/bta_pan_main.c
+++ b/bta/pan/bta_pan_main.c
@@ -83,14 +83,6 @@
 #define BTA_PAN_NUM_COLS             2       /* number of columns in state tables */
 
 
-/* state machine states */
-enum
-{
-    BTA_PAN_IDLE_ST,
-    BTA_PAN_OPEN_ST,
-    BTA_PAN_CLOSING_ST
-};
-
 
 /* state table for listen state */
 const UINT8 bta_pan_st_idle[][BTA_PAN_NUM_COLS] =
diff --git a/btif/src/btif_pan.c b/btif/src/btif_pan.c
index 633f6ef..e6cc1fe 100644
--- a/btif/src/btif_pan.c
+++ b/btif/src/btif_pan.c
@@ -197,7 +197,7 @@
 }
 static volatile int btpan_dev_local_role;
 static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY};
-static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 0, PAN_SECURITY};
+static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, PAN_SECURITY};
 
 static bt_status_t btpan_enable(int local_role)
 {
diff --git a/btif/src/btif_sock_rfc.c b/btif/src/btif_sock_rfc.c
index 2de1cd4..9c76028 100644
--- a/btif/src/btif_sock_rfc.c
+++ b/btif/src/btif_sock_rfc.c
@@ -283,26 +283,35 @@
                                         int open_handle, int new_listen_handle)
 {
     rfc_slot_t *accept_rs = alloc_rfc_slot(addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, FALSE);
-    clear_slot_flag(&accept_rs->f);
-    accept_rs->f.server = FALSE;
-    accept_rs->f.connected = TRUE;
-    accept_rs->security = srv_rs->security;
-    accept_rs->mtu = srv_rs->mtu;
-    accept_rs->role = srv_rs->role;
-    accept_rs->rfc_handle = open_handle;
-    accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
-     //now update listen rfc_handle of server slot
-    srv_rs->rfc_handle = new_listen_handle;
-    srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
-    BTIF_TRACE_DEBUG4("create_srv_accept__rfc_slot(open_handle: 0x%x, new_listen_handle:"
-            "0x%x) accept_rs->rfc_handle:0x%x, srv_rs_listen->rfc_handle:0x%x"
-      ,open_handle, new_listen_handle, accept_rs->rfc_port_handle, srv_rs->rfc_port_handle);
-    asrt(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
-  //now swap the slot id
-    uint32_t new_listen_id = accept_rs->id;
-    accept_rs->id = srv_rs->id;
-    srv_rs->id = new_listen_id;
-    return accept_rs;
+    if( accept_rs)
+    {
+        clear_slot_flag(&accept_rs->f);
+        accept_rs->f.server = FALSE;
+        accept_rs->f.connected = TRUE;
+        accept_rs->security = srv_rs->security;
+        accept_rs->mtu = srv_rs->mtu;
+        accept_rs->role = srv_rs->role;
+        accept_rs->rfc_handle = open_handle;
+        accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
+        //now update listen rfc_handle of server slot
+        srv_rs->rfc_handle = new_listen_handle;
+        srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
+        BTIF_TRACE_DEBUG4("create_srv_accept__rfc_slot(open_handle: 0x%x, new_listen_handle:"
+                "0x%x) accept_rs->rfc_handle:0x%x, srv_rs_listen->rfc_handle:0x%x"
+                ,open_handle, new_listen_handle, accept_rs->rfc_port_handle, srv_rs->rfc_port_handle);
+        asrt(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
+        //now swap the slot id
+        uint32_t new_listen_id = accept_rs->id;
+        accept_rs->id = srv_rs->id;
+        srv_rs->id = new_listen_id;
+
+        return accept_rs;
+    }
+    else
+    {
+        APPL_TRACE_ERROR1(" accept_rs is NULL %s", __FUNCTION__);
+        return NULL;
+    }
 }
 bt_status_t btsock_rfc_listen(const char* service_name, const uint8_t* service_uuid, int channel,
                             int* sock_fd, int flags)