Bluetooth: Make hci_le_create_connection return error

This patch changes hci_le_create_connection helper so it return error
if hci_send_cmd fails. It also does the proper error handling in
hci_connect_le.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Initialize hci_conn fields in hci_connect_le

This patch moves some hci_conn fields initialization from hci_le_
create_connection. It makes more sense to initialize these fields
within the function that creates the hci_conn object.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Refactor LE Connection Complete HCI event handler

This patch does some code refactorig in LE Connection Complete HCI
event handler. It basically adds a switch case to separate new master
connection code from new slave connection code.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Locking refactor in hci_le_conn_complete_evt

This patch moves hci_dev_lock and hci_dev_unlock calls to where they
are really required, reducing the critical region in hci_le_conn_
complete_evt function. hdev->lock is required only in hci_conn_del
and hci_conn_add call to protect concurrent add and remove operations
in hci_conn_hash list.

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

Some HCI commands don't send a Command Complete Event once the
HCI command has completed and they require special handling by
the HCI request framework. HCI LE Create Connection command is
one of those.

The HCI LE Create Connection command starts the connection creation
procedure. A successful Command Status Event for this command means
the controller is running the connection creation procedure. For that
reason, the HCI request framework should consider the LE Create
Connection command has completed once the host receives the Command
Status Event.

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

This patch adds the LE connection states. They will indicate what
phase of the General Connection Establishment procedure a hci_conn
object is.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Introduce the background LE scan helper

This patch adds the local function enable_background_le_scan which
starts the LE passive scanning. It will be used to enable the passive
scanning for connection establisment.

This patch also adds the hci_conn_fail_pending which does the proper
cleanup in case a connection attempt failed. It will also be used
by other functions in hci_conn.c.

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

This patch adds the hci_conn_check_le_pending which checks if we have
a pending connection for a given tuple of address and address type.
If there is a pending connection for the given device, it disable
the ongoing LE scanning and initiate the connection.

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: 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 check the current le_state. If connection
is in HCI_CONN_LE_SCAN, we should send a HCI to disable the ongoing
scanning. If the connection is in HCI_CONN_LE_INITIATE state, we should
send a command to cancel the connection creation.

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 status handling in hci_cs_le_create_conn

This patch removes the status error handling in hci_cs_le_create_conn
since this is alreay handled in le_conn_initiate_complete callback.

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

This patch removes the unused functions hci_le_create_connection and
hci_le_create_connection_cancel.

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