WIP: Bluetooth: Look hci_conn up by bdaddr

In order to get the right hci_conn object once the LE connection is
completed, we should lookup hci_conn objects by bdaddr instead of
state.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: LE Create Connection support in request framework

No Command Complete event is sent by the controller to indicate that
HCI LE Create Connection command has been completed. Instead, the LE
Connection Complete events indicates this command has been completed.
For that reason, HCI request framework requires a special handling
for this command.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Add LE create connection helpers

This patch adds two static helpers with HCI request support. They
will be used by the LE connection state machine.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: LE connection state machine

This patch implements a state machine for carrying out the general
connection establishment procedure described in Core spec.

The state machine should be used as follows: when the kernel
receives a new LE connection attempt, it should go to HCI_CONN_LE_
SCAN state, starting the passive LE scanning. Once the target remote
device is in-range, it should go to HCI_CONN_LE_INITIATE state,
stopping the ongoing LE scanning and create the LE connection.

This state machine will be used by the LE connection routine in
order to establish LE connections.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
WIP: Bluetooth: Handle hci_conn timeout in BT_CONNECT

In order to properly cancel an ongoing LE connection attempts (hci_conn
in BT_CONNECT state), we should set HCI_CONN_LE_IDLE state. We also
need to do the proper hci_conn deletion when the scanning is disabled
as we already to in case the connection attempt fails in le_conn_complete.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>

WIP: Bluetooth: Add LE Create Connection Cancel handler

This patch adds a handler for HCI LE Create Connection Cancel command
complete event. This way, we can do all the properly handling of this
command in this function instead of fixing this logic within LE conn
complete handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Change LE connection routine

In order to better support LE connection requirements, such as multiple
connections and re-connections, we should use the general connection
establishment procedure described in Core spec. Today, we use the direct
connection establishment procedure which has some limitations and,
therefore, requires extra connection management at user-space in order
to support LE connection requirements.

According to the spec, the general procedure is described as follows:
The host starts scanning for LE devices, once the device we want to
connect to is in-range, the host stops scanning and initiates a
connection. The procedure is terminated when the connection is
established or when the host terminates the procedure.

This patch changes the LE connection routine so we perform the general
procedure instead of the direct procedure.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Count pending LE connection attempts

To avoid traversing the hci_conn list every time we get an advertising
report, we keep a counter of pending LE connection attempts (connections
in HCI_CONN_LE_SCAN state). This way, we only traverse the list if the
counter is greater from zero.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove unused functions

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove unneeded command status handler

This patch removes the hci_cs_le_create_conn handler from hci_event.c.
Since LE connection failures are now handled in le_state_complete
callback, there is no need to have the hci_cs_le_create_conn handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove unneeded handling

This patch removes codes to handle LE Connection Complete events with
error status. All cases of failure are already handled in le_state_
complete callback or in LE Create Connection Cancel Command Complete
event handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
WIP: Bluetooth: Support multiple connection attempts

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Introduce hci_conn_background_le_scan

This patch introduces the hci_conn_background_le_scan function which
starts the background passive scanning we have pending LE connections.
In case of HCI command failure (the controller couldn't enable the LE
scanning), all pending LE connections terminated.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Background LE scanning

In order to be able to connection to devices as soon as the are
in-range, we should keep scanning. Therefore, we should start the
background scanning when the Device discovery stops or when an LE
connection attempt terminates.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove BT_CONNECT check in hci_connect_le

Since the we supported only on LE connection attempt at a time, we
had to check if we already had one connection attempt running.
However, now we support multiple LE connection attempts so we can
safely remove this check in hci_connect_le function.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
WIP: Bluetooth: Check connection in INITIATE state

Since some controllers don't support LE scanning and LE connection
initiating at the same time, we shouldn't start the background
scanning if we have a connection in INITIATE state.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Don't set chan_timer for ATT fixed channel

According to Core spec (see Vol 3, Part A, 6.2.1), the channel timer
used to terminate the channel when the remote endpoint is unreponsive
to signaling requests. Since there is no signaling requests in LE ATT
fixed channel, there is no need we setup this timer.

As a side effect, LE connection attempts won't be aborted by the
channel timer anymore. Therefore, SOCK_NONBLOCK LE L2CAP sockets will
be ready (will receive POLLOUT event) when the LE connection is
established.

This patch modifies l2cap_chan_connect function so it doesn't set
the timer if the cid is L2CAP_CID_LE_DATA.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>