Bluetooth: A2DP AVRCP Squashed Gerrits for propagation

Bluetooth: Clear remote suspend flag on susupend.

Remote device may initiate AVDTP Suspend within 3s of AVRCP pause,
in that case flag is set which prevents further a2dp data.
If DUT initiates play, flag set will prevent AVDTP to go to
Start state.Change is made so that on standby flag is cleared.

CRs-Fixed: 516611

Change-Id: I6c474cd7fd9cd667744d44076ad9b681c75e3b5a

Bluetooth: Allow Carkit to play just after call

Removing check in stack where AVRCP Play and Pause commands
are being dropped if carkit initiates play or pause within
six seconds.

Change-Id: Ibb04223de746f528b356e78bd65130d3fd526a44
CRs-Fixed: 517806

Bluetooth: Synchronise A2dp HAL and Btif state.

During Remote initiate start stream, A2dp HAL and
Btif states are not in sync, Audio HAL in Suspend
and Btif in Start state hence Audio HAL cannot send
Suspend command.
Change is made to synchronise A2DP HAL and Btif
State by sending a CHECK_STREAM_Ready Command.

CRs-Fixed: 549210

Change-Id: I5da46c433ec44b94afc4151facc0894fcb88759a

Bluetooth: Handle Connection request in Opening state.

Add support to handle Connection request in opening state.
This happens in race condition when we try to connect to HS from UI
and simultaneously there is connection request from remote HS.

Change-Id: I9518a1857bf188da8851b4c6927929dce7ee4c8b
CRs-Fixed: 531637

Bluetooth: Fix Crash due to random Connection

While connecting randomly between paired headsets rc_handle was not
initialised properly and this was causing crash while trying to retrieve
peer device features.

Change-Id: I4f93d541f616ca2c2bbb1e7871740264b20e3de4
CRs-Fixed: 465980

Bluetooth: Disconnect A2dp before sending SIGKILL.

Update application for a2dp disconnection,before
sending SIGKILL due to H/W error.

Change-Id: I9bb6bcbb319f7f227e61866c9aefe6f2dcd98c6c
CRs-Fixed: 490733

Suspend a2dp if remote initiates start during active call

Suspend streaming if remote initiates avdtp start during an
ongoing call. This helps remote and DUT's a2dp state to be in
sync.
CRs-Fixed: 516822
Conflicts:
	btif/src/btif_av.c
Change-Id: I526355f9bd97b7695102626fa982c39b74bfd82c

Bluetooth: Ignore A2dp writes based on setparameter in Audio HAL

This change will drop a2dp writes when the setparameter is called with
A2dpSuspended=true and resumes when it gets reset.

Change-Id:  I3bb334373414170254744d9233cbd2e66219bfa0
CRs-Fixed:  503739

Bluetooth:Not Implemented rsp for unsupported AVRCP commands.

Changes made to ensure DUT responds as not implemented for AVRCP
pass through commands which are not supported by DUT.

CRs-Fixed: 551638

Change-Id: Ie10e74e2a399bb420f55c55005600c945b53bfc2

AVRCP: Proper handling of Fragmented AVCTP packet

- AVCTP : Allocate bigger buffer for reassembly
- AVRCP : Handle getelimentattr to eliminate duplicates
          and invalid attributes, this also fixes BOF.

CRs-Fixed: 576831
Change-Id: I778c3df1386339ff8a00a755ffc9f24881b5167d

Bluetooth: Clear remote suspend flag on play state change.

Remote device may initiate AVDTP Suspend within 3s of AVRCP pause,
in that case flag is set which prevents further a2dp data.
In case DUT initiates play before standby, remote suspend flag
won't be cleared and start fails.
Change made to ensure on Avrcp play state change event, flag
is cleared.

Change-Id: I1a0f96086030cf06bbebe3de0f2bead7846c19e3

Bluetooth: Check A2dp state inside mutex lock.

In race conditions A2dp state was updated to suspended
after checking and before acquiring mutex lock.A2dp State
should be checked inside mutex lock to avoid checking against
inconsistent state.Prevent overwriting of state
from AUDIO_A2DP_STATE_SUSPENDED to AUDIO_A2DP_STATE_STOPPED
which would again start the data path.With this change we make
sure to check state inside mutex lock and also avoid overwrite
of state in case of write failure.

Change-Id: I1d74490057fea06113dcf4e0666387f4d5224157
CRs-Fixed: 572205

BT: Absolute Volume feature should be enabled only for A2dp device.

Absolute Volume feature should be enabled only for device which
supports A2DP Sink and AVRCP. This change will make sure this
feature is not enabled for only AVRCP supported devices.

CRs-fixed: 582262
Change-Id: I75509d0b0fd1b06fa08390940a327dd2887d7562

Properly handle outgoing connection request in opened state.

Properly handle outgoing connection request in opened state.
This happens in race condition when we try to connect to a remote A2dp
device from Bluetooth Settings UI and simultaneously there is incoming
connection request from previously connected remote. This fix addresses
the scenario where because of incoming a2dp connection from second device
btif moves to opened state and then the outgoing connect request is
received in btif for first device. This scenario is handled by gracefully
rejecting the outgoing connect request from btif layer for first device
if it is already connected by the incoming connect request for second one.

CRs-Fixed: 595148
Change-Id: I308bad99bd26adb528f7b97e6cc5bbdfaf939cbb

Bluetooth: Check for device address

Do not update app for disconnected state
if device address is same when moved from
idle to opening state.

CRs-Fixed: 619008
Change-Id: I53be400d1d7c9a84ca9d9a75b6b6f010975cf0cf

Bluetooth: Suspend AVDTP link in remote initiated Start

Few remote devices sends start on power up and does not
suspend causing link not to go to sniff.
Change done was to send suspend and initiate start
when local playback starts.

Change-Id: I8ebee009c95148cf733e39d897bc8ffe5af7791e
CRs-Fixed: 621127

Bluetooth: Check RC Handle for processing pass-through Commands

In some race conditions 2nd AVRCP connection
is connected and remote sends pass through commands
immediately, in such conditions check for RC handle before
processing pass-through command.

Change-Id: Idc8756983adfd12b3871dfe46d5aafbe6c2b6a91
CRs-Fixed: 642934

Process AVRCP passthrough commands only if A2dp is connected

Process AVRCP passthrough commands only if A2dp is connected,
discard it otherwise.

CRs-Fixed: 610067

Change-Id: I20513cdec4414193db0d913e318e5cbd5a59a318

A2dp: Reduce out buffer size to accomodate 8 PCM frames

Reduce a2dp audio HAL out buffer size to 8*512 bytes to allow
audio to write lesser amount of data to A2dp HAL.

CRs-Fixed: 649937

Change-Id: I3e762bf8d269eb47a85bb4bc727f0365e8a48844

A2DP HAL: Delay open_output_stream

Delay adev_open_output_stream to ensure that the Headset is
in proper state when START is initiated from DUT immediately
after the connection due to ongoing music playback.

Change-Id: I79a895a85ac7d37208192acf9064fd5c979dd28b
CRs-Fixed: 647163

Bluetooth: set the proper authentication

During Avopen security was made none hence A2DP
connection was happening without bonding
Changes made from No security to SEC Authenticate

Change-Id: I7fc39fe8acef874f0031c9b78d4269a62b94e4b0
CRs-Fixed: 636724

BTIF-AV: Use valid memory for AVRCP message

After the context switch to BTIF for META message processing
the buffer allocated for tAVRC_MSG will be freed, hence allocate
the required memory and free that after handling the META
message.

Crs-Fixed: 651506
Change-Id: Icbf61776a6fb63fac1555bf25d3700beccf2b67a

HFP-HS: Abort parsing in case of format error

Abort parsing in case of format error in AT commands.

Change-Id: Id3973da824b4229fcf69a256a0615ce5b1e0677b

Squashed commit of the following:

commit bc9322f3f1310bf7c6f52604afa85bcbfb661811
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Tue Apr 22 10:54:03 2014 +0530

    Bluetooth: A2dp connection Failure.

    Send disconnect event to closing state from state machine
    and handle closing event BTIF layer to update app for
    connection failure.

    CRs-Fixed: 655231
    Change-Id: Iab6de022227f20db82bbbe20b9eac6457f426061

commit 755b40cccb33a9843ef982e1c976c8a141ffc210
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Thu Jan 30 12:18:09 2014 +0530

    Bluetooth: Process signal down event.

    Send AVDT_DISCONNECT_EVT when AVDTP signalling
    channel is disconnected.This is required to move
    BTA AV state machine to move back to idle state.

    Change-Id: I3fe030aa0c5af95b5b87c02fde19cae191c78ef6
    CRs-Fixed: 608733

commit e5ce659bb2a2d8c630dd6c10dff7371a723c81bf
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Mon Feb 10 15:41:30 2014 +0530

    Bluetooth: Proper AV state movement.

    On incoming connection request update
    app for connection before checking for
    priority of device.

    Change-Id: I0d9cb2f297d3286a709ecbbc94cd67ccf7c9e814
    CRs-Fixed: 610053

commit a49c2b12c554397d6d550ad36c2dd6be4eccd5c4
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Tue Jan 7 16:47:19 2014 +0530

    Bluetooth: Move to proper state.

    Move to init state after cleanup on
    connection failure of streaming channel
    and failure of response from remote for
    get capabilities.

    CRs-Fixed: 597781

    Change-Id: I011d9ffe9d721235217320c1e5281f70328c7f13

commit 75b9d3532fb0211bd2e9c0f856c113f454db8838
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Tue Dec 10 19:18:27 2013 +0530

    Bluetooth: Move to proper state on SDP failure

    After SDP is failed for AVDTP connection, we should move
    to proper stable state.

    CRs-Fixed: 588108

    Change-Id: I58e60cd29e9f2ede98cb514a7f45774a3bc1d8ac

commit a7ebc85b5b480099c6556f47d38a6a653864ab50
Author: Gaurav Asati <gasati@codeaurora.org>
Date:   Wed Oct 9 14:02:12 2013 +0530

    Bluetooth: Check A2dp device priority.

    Check for device priority in stack before sending connect
    request on time-out for remote open event.

    Change-Id: I191f5f3089d7945cbd6facc223a341452462421c
    CRs-Fixed: 510957

Change-Id: I3dadb9d73cae4badf1299ee741b94ecfbea2039d

Bluetooth: Delete Connect Req from Queue.

Delete connect request node from connect queue
when app sends connect request for same UUID.

Change-Id: I3dec215f09050ef2290992288c2fe9f7672af102
CRs-Fixed: 614558

Bluetooth: Added new API in btif layer to get Remote Supported features

Added new API in btif layer so that application can get the
Remote supported by using this.

CRs-fixed: 594652
Change-Id: I9ec9101d550dca6982b693b46d5d5c087c78a6e6

BTIF-AV: Do not attempt connection for unbonded devices

Do not attempt connection for unbonded devices

CRs-fixed: 631398

Change-Id: I0b87338dca5bce07ee03d774d3339131514c1268

Bluetooth: Reduce AVDT START RX TIMEOUT

-Change START RX TIMEOUT from 15s to 5s.
 Higher value causes ANR as during out write
 Mutex is acquired and if remote device does not
 responds and audio tries to close or set parameter
 which tries to acquire same mutex hence causeing ANR.
-In Audio HAL,do mutex lock only for a2dp operations.

CRs-Fixed: 588036

Change-Id: I6a4004236db29f3b9179a5c29f2b38c65149de78

Bluetooth: Handle Open after set_config

 - handle priority check from idle state
 - handle open after set_config
 - changed queue size to 24

Change-Id: I834c4c577f14d7d0e6a00a52aac7fe6bc989a95f
CRs-Fixed: 631557

Bluetooth: Reject second AVRCP connection.

This change rejects the RC connection from
second device if the DUT is already connected
to RC of first device.

Change-Id: If2e39cd90575baffd0e4442b8e7cd2a91eebdf56
CRs-Fixed: 626011

Bluetooth: Validate the Metadata length.

This change ensures that DUT rejects
invalid AVRCP Meta command having invalid
data length.

CRs-Fixed: 667378
Change-Id: I8a591d601636a0fce268b5b965f2bd691c24c319
diff --git a/audio_a2dp_hw/audio_a2dp_hw.c b/audio_a2dp_hw/audio_a2dp_hw.c
index 5310ba3..4d061d0 100644
--- a/audio_a2dp_hw/audio_a2dp_hw.c
+++ b/audio_a2dp_hw/audio_a2dp_hw.c
@@ -146,6 +146,7 @@
         CASE_RETURN_STR(A2DP_CTRL_CMD_START)
         CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
         CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
+        CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_STREAM_STARTED)
         default:
             return "UNKNOWN MSG ID";
     }
@@ -349,6 +350,10 @@
 
     DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack);
 
+    if (ack == A2DP_CTRL_ACK_INCALL_FAILURE)
+    {
+        return ack;
+    }
     if (ack != A2DP_CTRL_ACK_SUCCESS)
         return -1;
 
@@ -443,6 +448,7 @@
 static int start_audio_datapath(struct a2dp_stream_common *common)
 {
     int oldstate = common->state;
+    int a2dp_status;
 
     INFO("state %d", common->state);
 
@@ -453,13 +459,20 @@
 
     common->state = AUDIO_A2DP_STATE_STARTING;
 
-    if (a2dp_command(common, A2DP_CTRL_CMD_START) < 0)
+    a2dp_status =  a2dp_command(common, A2DP_CTRL_CMD_START);
+    if (a2dp_status < 0)
     {
         ERROR("audiopath start failed");
 
         common->state = oldstate;
         return -1;
     }
+    else if (a2dp_status == A2DP_CTRL_ACK_INCALL_FAILURE)
+    {
+        ERROR("audiopath start failed- In call a2dp, move to suspended");
+        common->state = AUDIO_A2DP_STATE_SUSPENDED;
+        return -1;
+    }
 
     /* connect socket if not yet connected */
     if (common->audio_fd == AUDIO_SKT_DISCONNECTED)
@@ -534,6 +547,17 @@
 }
 
 
+static int check_a2dp_stream_started(struct a2dp_stream_out *out)
+{
+   if (a2dp_command(&out->common, A2DP_CTRL_CMD_CHECK_STREAM_STARTED) < 0)
+   {
+       DEBUG("Btif not in stream state");
+       return -1;
+   }
+   return 0;
+}
+
+
 /*****************************************************************************
 **
 **  audio output callbacks
@@ -548,9 +572,11 @@
 
     DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
 
+    pthread_mutex_lock(&out->common.lock);
     if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
     {
         DEBUG("stream suspended");
+        pthread_mutex_unlock(&out->common.lock);
         return -1;
     }
 
@@ -558,7 +584,6 @@
     if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) ||
         (out->common.state == AUDIO_A2DP_STATE_STANDBY))
     {
-        pthread_mutex_lock(&out->common.lock);
 
         if (start_audio_datapath(&out->common) < 0)
         {
@@ -574,21 +599,26 @@
             return -1;
         }
 
-        pthread_mutex_unlock(&out->common.lock);
     }
     else if (out->common.state != AUDIO_A2DP_STATE_STARTED)
     {
         ERROR("stream not in stopped or standby");
+        pthread_mutex_unlock(&out->common.lock);
         return -1;
     }
 
+    pthread_mutex_unlock(&out->common.lock);
+
     sent = skt_write(out->common.audio_fd, buffer,  bytes);
 
     if (sent == -1)
     {
         skt_disconnect(out->common.audio_fd);
         out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
-        out->common.state = AUDIO_A2DP_STATE_STOPPED;
+        if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
+            out->common.state = AUDIO_A2DP_STATE_STOPPED;
+        else
+            ERROR("write failed : stream suspended, avoid resetting state");
     }
 
     DEBUG("wrote %d bytes out of %zu bytes", sent, bytes);
@@ -664,10 +694,13 @@
 
     pthread_mutex_lock(&out->common.lock);
 
-    if (out->common.state == AUDIO_A2DP_STATE_STARTED)
+    /*Need not check State here as btif layer does
+    check of btif state , during remote initited suspend
+    DUT need to clear flag else start will not happen but
+    Do nothing in SUSPENDED state. */
+    if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
         retVal =  suspend_audio_datapath(&out->common, true);
-    else
-        retVal = 0;
+
     pthread_mutex_unlock(&out->common.lock);
 
     return retVal;
@@ -686,13 +719,10 @@
     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
     struct str_parms *parms;
     char keyval[16];
-    int retval;
-    int status = 0;
+    int retval = 0;
 
     INFO("state %d", out->common.state);
 
-    pthread_mutex_lock(&out->common.lock);
-
     parms = str_parms_create_str(kvpairs);
 
     /* dump params */
@@ -705,7 +735,9 @@
         if (strcmp(keyval, "true") == 0)
         {
             DEBUG("stream closing, disallow any writes");
+            pthread_mutex_lock(&out->common.lock);
             out->common.state = AUDIO_A2DP_STATE_STOPPING;
+            pthread_mutex_unlock(&out->common.lock);
         }
     }
 
@@ -713,10 +745,23 @@
 
     if (retval >= 0)
     {
+        pthread_mutex_lock(&out->common.lock);
         if (strcmp(keyval, "true") == 0)
         {
             if (out->common.state == AUDIO_A2DP_STATE_STARTED)
-                status = suspend_audio_datapath(&out->common, false);
+            {
+                retval = suspend_audio_datapath(&out->common, false);
+            }
+            else
+            {
+                if (check_a2dp_stream_started(out) == 0)
+                   /*Btif and A2dp HAL state can be out of sync
+                    *check state of btif and suspend audio.
+                    *Happens when remote initiates start.*/
+                    retval = suspend_audio_datapath(&out->common, false);
+                else
+                    out->common.state = AUDIO_A2DP_STATE_SUSPENDED;
+            }
         }
         else
         {
@@ -727,12 +772,12 @@
                 out->common.state = AUDIO_A2DP_STATE_STANDBY;
             /* Irrespective of the state, return 0 */
         }
+        pthread_mutex_unlock(&out->common.lock);
     }
 
-    pthread_mutex_unlock(&out->common.lock);
     str_parms_destroy(parms);
 
-    return status;
+    return retval;
 }
 
 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
@@ -1065,6 +1110,9 @@
     }
 
     DEBUG("success");
+    /* Delay to ensure Headset is in proper state when START is initiated
+       from DUT immediately after the connection due to ongoing music playback. */
+    usleep(250000);
     return 0;
 
 err_open: