Bluetooth: Move discovery macros to hci_core.h

Some of discovery macros will be used in hci_core so we need to
define them in common place such as hci_core.h. Thus, this patch
moves discovery macros to hci_core.h and also adds the DISCOV_
prefix to them.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Update le_scan_disable_work to use HCI request

This patch updates the le_scan_disable_work function so it uses the
HCI request framework. All discovery state handling are done in the
in le_scan_disable_work_complete callback.

This patch also changes the le_scan_disable_work behavior by adding
the HCI Inquiry command to the request in case we are running the
interleaved discovery. This way we don't need the extra logic in
hci_cc_le_set_scan_enable to trigger the inquiry procedure and the
mgmt_interleaved_discovery function becomes useless.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Update stop_discovery to use HCI request

This patch modifies the stop_discovery function so it uses the HCI
request framework.

The HCI request is built according to the current discovery state
(inquiry, LE scanning or name resolving) and a complete callback is
register to handle the command complete event for the stop discovery
command. This way, we move all stop_discovery mgmt handling code
spread in hci_event.c to a single place in mgmt.c.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove stop discovery handling from hci_event.c

Since all mgmt stop discovery command complete events are now handled
in stop_discovery_complete callback in mgmt.c, we can remove this
handling from hci_event.c.

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

This patch does a trivial refactoring in hci_cc_le_set_scan_enable.
Since start and stop discovery command failures are now handled in
mgmt layer, the status check became empty. So, we can move it to
outside the switch statement.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove LE scan helpers

This patch removes the LE scan helpers hci_le_scan and hci_cancel_
le_scan and all code related to it. We now use the HCI request
framework in device discovery functionality and these helpers are
no longer needed.

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

This patch removes hci_do_inquiry and hci_cancel_inquiry helpers. We
now use the HCI request framework in device discovery functionality
and these helpers are no longer needed.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove empty event handler

This patch removes the hci_cc_le_set_scan_param event handler. This
handler became empty because failures of this event are now handled
by start_discovery_complete function in mgmt.c.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Rename le_scan_disable delayed work

The le_scan_disable work basically implements a timer, but now it
does more than simply disabling the LE scanning (it may also start
the inquiry in case we are running an interleaved discovery). So,
renaming it to discovery_timeout seems suitable.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Mgmt Device Found Event

We only want to send Mgmt Device Found Events if we are running the
Device Discovery procedure (started by the MGMT Start Discovery
Command). Inquiry or LE scanning triggered by HCI raw interface (e.g.
hcitool) or kernel internals should not send Mgmt Device Found Events.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
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>