Immediately bounce back SYMM packet when initiator.

On first establishment of the LLCP link, if we are
the initiator, we waited the full LTO before sending
the SYMM to the target. This will prevent the target
from communicating with us for 750ms! Old Android
implementations only connected LLCP on touch, but
newer ones will immediately connect LLCP.

To solve this, bounce the SYMM back immediately, to allow
the target to start sending as soon as it wants to.

Change-Id: I2fb09599798527f2e6f908b8ba362e0061120be2
Fix some LLCP threading issues.

On Android we now start connecting SNEP/other LLCP
services as soon as the link comes up. That means
we will have connect requests incoming, accept
responses outgoing, connect requests outgoing and
accept responses incoming.

This lays bare a lot of threading issues in this lib.
Since the receives come in serialized, those typically
do not cause any issue. However, we may be sending
something on a thread coming from the NfcService,
while at the same time we'll receive for example
a connect frame from the remote, to which the receive
thread immediately wants to send a response.

This is a first attempt at making sends thread-safe:
already there was a lot of logic to deal with the fact
that another send was in progress, in the form of the
bSendPending variable. That variable was however
not checked atomically, and some operations did not
even set it correctly.

This change tests and sets that variable atomically,
more or less implementing a semaphore with try-acquires
and fallbacks for the failure case.

The generic structure is:

if (testAndSetSendPending()) {
   // Another thread is sending, stash data
   // and most importantly do *NOT* change shared data
} else {
   // We're now the only one sending, we're free
   // to change shared data until the callback is
   // called.
}

This is just a band-aid fix, but given the fact that
this stack will be deprecated it is hopefully enough
for our planned usecases.

A decent fix would involve switching to
a TX-queue per socket, and a generic TX-queue
for the whole LLCP link. This allows us to fully
decouple the connection layer from the transport
layer, as it should be.

Change-Id: Ibd8742f7350a58459771f5036a049af3a487f783
Fix wrong callback being cleared.

When a call to disconnect() failed, the
connect() callback was cleared, not the
disconnect() one. This would cause deadlocks,
where JNI threads were forever blocked on
doConnect().

Bug: 7324226
Change-Id: I784c8d700c87d643315f2417610ca75ef8f9aa7c
Make sure LLCP header fields are reset.

In some conditions these would persist
over a link tear-down, causing them to
be sent out the next time the link comes up.

Also, don't overwrite them if a send is
still in progress.

Change-Id: I19dec5d79ae4b25f41fb5e8a0514e565ceca7a91
Add liblog

Bug: 8580410
Change-Id: Ia6f8b425e15b2cb1f7c5baf7a3db6d8c55afb147
Restore SE activated mode in case of failure.

There is a tight race condition where opening
of the SE can fail, due to a tag/p2p device
having just come in. In that case, restore
the original state of the SE activated mode.

This will prevent future calls into this
method and Connect() from immediately bailing out.

Bug: 8599167
Change-Id: I1890e4efb73a9f5bdd6abe0644f88051b32e8c02
Return NFCSTATUS_BUSY in case of invalid state transition.

This allows the caller to recovery by trying again.

Bug: 8552749
Change-Id: Iff979c097d33f9adda06654de9e588595daf89a3
Merge tag 'android-4.3_r2.1' into jellybean-mr2.1

Android 4.3 release 2.1