Commit graph

1495 commits

Author SHA1 Message Date
Joakim Andersson ffa10eba83 Bluetooth: host: Include scan response with name when non-connectable
When using BT_LE_ADV_NCONN_NAME then the advertising name will not be
included in the advertising data. This is because the host always puts
the device name in the scan response. But since the scan data was
otherwise empty the advertising type was set to ADV_NONCONN_IND.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-20 14:06:42 +02:00
Dan Erichsen 6a12a2dbe1 bluetooth: host: Do not send unwanted SC indicate
Fixes #23485

When we create a GATT table dynamically, we also create a hash
identifying this table. This hash can be stored in persistent memory and
we can thus determine after recreating the GATT table whether the
services have changed or not from before the reboot.

When these hashes are identical, it implies that the table has not
changed, wherefore a service changed indication should not be sent to
any bonded clients. The method for achieving this was to remove the
gatt_sc.work entry from the work queue. This work queue entry was to
send an indication to the clients when the table had been allocated.
If the final entry then caused the hashes to match, the indication
would be cancelled.

On unit testing this behaviour in simulation and in practice, we found
that the indication was sent nonetheless, and the issue was located to
be tied to the SERVICE_RANGE_CHANGED flag which is set when the services
are changed and is cleared when the indications are being sent out.

It was the job of the work queue entry to clear this flag, and as the
entry was never serviced, the flag was never cleared, and when
sc_commit() is called at the end of the process, it believes that there
is a new service change pending and therefore starts the job over, thus
creating a redundant indication to the clients.

This commit fixes the issue by clearing the flag when the work entry
is removed due to a hash match. This has been unittested in a live
environment, in a simulation environment, and sanitycheck has been run
on it.

Signed-off-by: Dan Erichsen <daee@demant.com>
2020-03-16 21:09:24 +02:00
Johan Hedberg 80cb358fe2 Bluetooth: Kconfig: Fix BT_ECC defaults
The commit e85dd8af5d changed the way the BT_ECC Kconfig option
is enabled, however it got the dependency wrong. The dependency should
only look at BT_SMP_OOB_LEGACY_PAIR_ONLY if BT_SMP was also enable.

This broke e.g. the build of the mesh_demo app for the BBC
micro:bit since the memory consumption jumped up by roughly 2k.

This patch fixes the issue, and in the same go makes the Mesh handling
consistent by also using a conditional default rather than select.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2020-03-16 14:26:08 +02:00
Joakim Andersson 628b3bc416 Bluetooth: host: Allow get local OOB data while advertiser is enabled
Allow to get local OOB data while advertiser, scanner or whitelist
initiator is active. If direct initiator is active or the advertiser
is using the random address as a random static identity address then
the function will return error.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-13 17:16:51 +02:00
Joakim Andersson 7ff4a632b7 Bluetooth: host: Allow to initiate pairing without OOB present
When OOB callbacks are present it is possible to achieve authenticated
pairing without having the remote OOB data present. Using OOB with
LE Secure Connection only one side of the pairing procedure is required
to have the OOB data present. If we have given the remote our OOB data
then pairing can proceed.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-13 14:18:48 +02:00
Iván Morales e85dd8af5d Bluetooth: smp: Adding Legacy OOB only mode and no JW/Passkey mode
Added CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY option that completely disables
all legacy and SC pairing modes(except for Out of Band) and frees the
memory previously used by these.

Added CONFIG_BT_SMP_DISABLE_LEGACY_JW_PASSKEY option that force rejects
pair requests that lead to legacy Just Works or Passkey pairing.

Signed-off-by: Iván Morales <ivan98ams@gmail.com>
2020-03-12 12:30:15 +02:00
Iván Morales cc0d2447f1 Bluetooth: smp: Adding Legacy OOB pairing support
Added support for Legacy pairing using OOB Temporary Key

Signed-off-by: Iván Morales <ivan98ams@gmail.com>
2020-03-12 12:30:15 +02:00
Joakim Andersson 564ba1a07b Bluetooth: host: Use switch statement in bt_conn_set_state
Refactor old state handling in bt_conn_set_state to use switch statement
instead of if statements.
This will give us warning about enum values not covered.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-10 15:09:43 +02:00
Joakim Andersson f19b7a6e69 Bluetooth: host: Fix local RPA not valid in connection complete
Fix connection complete event handling when the local RPA is not valid.
This can happen when the controller was not instructed to use an RPA
address, or the local IRK was set to an all zero IRK.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-10 15:09:31 +02:00
Joakim Andersson 681dfdc12b Bluetooth: HCI_VS: Check for supported command
According to the Zephyr VS HCI specification:
Only Read_Version_Information and Read_Supported_Commands commands are
mandatory.

Check for supported Read Supported Features command before issuing this
command to the controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-10 15:02:48 +02:00
Joakim Andersson 94e1620c03 Bluetooth: host: Fix host resolving peer identity in connection complete
Fix host resolving the peer identity address in enhanced connection
complete event when the resolving list in the controller is full and
resolution is done in the host.
Move the handling from legacy connection complete into enhanced
connection complete event so that it is done for both connection
complete events.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-10 12:18:09 +02:00
Joakim Andersson bb42abdd7c Bluetooth: host: Fix LE SC OOB authentication and id for central role
If the application has used bt_le_oob_get_local to retrieve the OOB
RPA address and OOB authentication information the central role should
use this RPA address for the next RPA timeout period.

The central role always refreshes the RPA address for the initiator,
this will make the OOB information not usable as the peer cannot
recognize the central role since the RPA address is changed.
Check if the initiator can use the address for the duration of the of
remaining RPA period.

Fix central role using the advertiser identity when setting the private
address. The central role should only use the default identity.

Regressions from:
fbe3285bfa
and
4876a8f39a

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-06 18:25:48 +02:00
Rubin Gerritsen 3a30eed083 bluetooth: Fix wrong warning text for unsupported Zephyr HCI commands
They were interchanged.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2020-02-27 12:31:01 +02:00
Luiz Augusto von Dentz 9e8b78b8e1 Bluetooth: GATT: Fix not clearing CF_OUT_OF_SYNC
If the client is change-unware and disconnects the spec requires that
the stack still sends the error out of sync for the next request:

'The ATT_ERROR_RSP PDU is sent only once after the client becomes
change-unaware, unless the client _disconnects_ or the database changes
again before the client becomes change-aware in which case the
ATT_ERROR_RSP PDU shall be sent again'

Fixes #23110

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-27 11:26:30 +02:00
Joakim Andersson 58908aa5d0 Bluetooth: host: Fix missing connection id checks
Fix issue where a new connection with the same peer would use the CCC
from from first connection, despite different local identity.
Since there is no CCC for the new connection yet this caused the
application to think that CCC was enabled but the remote device had not
yet subscribed.

Fix this issue by making the id as an input to the peer address check
function. This will force us to make the check every time. This commit
might also fix similar issues not yes discovered as the ID check was
missing in a few other places as well.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-21 21:28:30 +02:00
Joakim Andersson 4e829eb544 Bluetooth: host: Rename acl context id to index
Rename the acl buf context id to index since to avoid confusing it with
the conn object ID parameter. Especially the bt_conn_lookup_id function
was creating confusion.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-21 21:28:30 +02:00
Rubin Gerritsen fa241f0249 bluetooth: Add decoding for BLE 5.2 version string
The BLE 5.2 specification has been assigned the version number 0x0b.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2020-02-21 16:53:16 +02:00
Joakim Andersson 9ab17a3eb4 Bluetooth: test: Delay SMP pairing distribution phase when testing
Avoid the HCI-USB race condition where HCI data and HCI events can be
re-ordered, and pairing information appears to be sent unencrypted.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-20 21:32:59 +02:00
Joakim Andersson 2e6983c0f5 Bluetooth: GATT: Fix invalid DB hast written to settings storage
Fix issue where an invalid (all zeroes) hash was written to settings
storage on reset. This caused the old value to written to zeroes, before
being written back to it's original value again immediately after.
This causes excessive flash wear.

This happens because the check if (k_delayed_work_remaining) returns the
amount of time until the work will execute. When that time has run out
the time is zero, but the work has not yet been executed.
We then write the invalid hash to flash, and then once the work-item
executes it will write the correct value.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-18 13:55:33 +02:00
Luiz Augusto von Dentz bd5048c251 Bluetooth: Setting: Make bt_settings_encode_key take a const address
This removes the need to cast in case the address is already const.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-14 14:15:11 +02:00
Luiz Augusto von Dentz 4edfd45c6d Bluetooth: GATT: Fix not clearing SC when disconnecting
SC config data is no longer stored within the CCC config itself
therefore it must be cleared separately.

Fixes #22539

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-14 14:15:11 +02:00
Luiz Augusto von Dentz b6825ffc31 Bluetooth: GATT: Fix not clearing stored data when unpairing
GATT data shall not be considered conditional to BT_SETTINGS since
the data is stored in RAM it must also be cleared when unpairing.

Fixes #22514

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-14 14:15:11 +02:00
Luiz Augusto von Dentz 730989078c Bluetooth: GATT: Move functions related to bt_gatt_clear
These functions shall not be conditional to BT_SETTINGS.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-14 14:15:11 +02:00
Luiz Augusto von Dentz 1156412904 Bluetooth: SMP: Fix failing to pass SM/MAS/PROT/BV-01-C
smp_pairing_complete does actually clears flags so setting
SMP_FLAG_TIMEOUT must come after that.

Fixes #22786

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-14 11:55:14 +02:00
Joakim Andersson 1f0f3ca9ba Bluetooth: GATT: Fix gatt indicate using wrong pointer for attribute
Fix bt_gatt_indicate using the wrong attribute pointer when a uuid was
provided as input.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-12 20:28:32 +02:00
Joakim Andersson 08a39851dc Bluetooth: host: Rename scan recv callback info struct
Rename the scan recv callback info struct so that it reflects that it
is part of the scan recv callback. This will make it consistent with
future plans for advertising callbacks.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-11 23:57:27 +02:00
Joakim Andersson 794cbd641b Bluetooth: host: Fix handling of scan start failed
Handle scan start failed and release the connection object in this case.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-11 23:56:47 +02:00
Joakim Andersson abd03aa3ad Bluetooth: host: Fix scanner using wrong identity to resolve peer
Fix the scanner using the advertiser identity instead of the scanners
identity, scanner always use BT_ID_DEFAULT.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-11 23:56:47 +02:00
Kamil Piszczek 8f7fe8357e bluetooth: host: fix RPMsg driver headroom configuration
Corrected configuration of BT_HCI_RESERVE for the RPMsg HCI driver.
This change fixes the following assert:
[net_buf_simple_headroom(buf) >= len] @ ZEPHYR_BASE/subsys/net/buf.c:881

Signed-off-by: Kamil Piszczek <Kamil.Piszczek@nordicsemi.no>
2020-02-11 13:23:50 +02:00
Luiz Augusto von Dentz 6ea0ea38a7 Bluetooth: L2CAP: Fix not queueing in case there are no credits
This can happens if for example the remote peer have the initial credits
set to 0 which would cause bt_l2cap_chan_send to fail instead of just
queue the packets until more credits are given.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-02-10 22:08:14 +02:00
Ulf Magnusson eddd98f811 kconfig: Replace some single-symbol 'if's with 'depends on'
I think people might be reading differences into 'if' and 'depends on'
that aren't there, like maybe 'if' being needed to "hide" a symbol,
while 'depends on' just adds a dependency.

There are no differences between 'if' and 'depends on'. 'if' is just a
shorthand for 'depends on'. They work the same when it comes to creating
implicit menus too.

The way symbols get "hidden" is through their dependencies not being
satisfied ('if'/'depends on' get copied up as a dependency on the
prompt).

Since 'if' and 'depends on' are the same, an 'if' with just a single
symbol in it can be replaced with a 'depends on'. IMO, it's best to
avoid 'if' there as a style choice too, because it confuses people into
thinking there's deep Kconfig magic going on that requires 'if'.

Going for 'depends on' can also remove some nested 'if's, which
generates nicer symbol information and docs, because nested 'if's really
are so simple/dumb that they just add the dependencies from both 'if's
to all symbols within.

Replace a bunch of single-symbol 'if's with 'depends on' to despam the
Kconfig files a bit and make it clearer how things work. Also do some
other minor related dependency refactoring.

The replacement isn't complete. Will fix up the rest later. Splitting it
a bit to make it more manageable.

(Everything above is true for choices, menus, and comments as well.)

Detected by tweaking the Kconfiglib parsing code. It's impossible to
detect after parsing, because 'if' turns into 'depends on'.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2020-02-08 08:32:42 -05:00
Andrew Boie efc5fe07a2 kernel: overhaul unused stack measurement
The existing stack_analyze APIs had some problems:

1. Not properly namespaced
2. Accepted the stack object as a parameter, yet the stack object
   does not contain the necessary information to get the associated
   buffer region, the thread object is needed for this
3. Caused a crash on certain platforms that do not allow inspection
   of unused stack space for the currently running thread
4. No user mode access
5. Separately passed in thread name

We deprecate these functions and add a new API
k_thread_stack_space_get() which addresses all of these issues.

A helper API log_stack_usage() also added which resembles
STACK_ANALYZE() in functionality.

Fixes: #17852

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2020-02-08 10:02:35 +02:00
François Delawarde 9d2e34e9c8 bluetooth: host: Add workaround for USB HCI controllers
This commit adds a new option CONFIG_BT_SMP_USB_HCI_CTLR_WORKAROUND
to support USB HCI controllers that sometimes send out-of-order HCI
events and ACL Data due to using different USB endpoints.

Enabling this option will make the master role not require the
encryption-change event to be received before accepting
key-distribution data.

It opens up for a potential vulnerability as the master cannot detect
if the keys are distributed over an encrypted link.

Fixes: #22086

Signed-off-by: François Delawarde <fnde@oticon.com>
2020-02-03 18:39:55 +02:00
Vinayak Kariappa Chettimada 814654af80 Bluetooth: host: Invalid role used in finding connections
Directed advertising timeout was dropped in the host due to
not being able to find a pending connection. Host used the
role parameter from the error event parameters which has
been earlier zero-ed out.

Regression introduced in commit a0349689ff ("Bluetooth:
host: Fix conn object assigned to wrong connection")

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
2020-02-03 12:51:59 +02:00
Joakim Andersson a570be6e93 Bluetooth: host: Refresh advertiser NRPA when active scan is using NRPA
When starting a non-connectable advertiser and an active scanner is also
using NRPA address then this use-case should be supported. A new
advertiser that is non-connectable should have a fresh NRPA address
every time it is started, so we must refresh the NRPA used by the active
scanner.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Joakim Andersson d977282514 Bluetooth: host: Scanner must be disabled to update to NRPA address
When advertiser is disabled we should update the random address for both
passive and active scanner back to an NRPA. But this command will fail
because the if the scanner is an active scanner, we must disable and
re-enabled the active scanner after setting the random address.
This behavior should not be there when scanner is configured to scan
with identity address.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Joakim Andersson 499b4e0ce0 Bluetooth: host: Document privacy-disabled directed advertising reports
Document why a privacy-disabled scanner will not notify about directed
advertising reports. This is the default behaviour of the
privacy-disabled scanner. In order to receive the reports the option
BT_SCAN_WITH_IDENTITY must be enabled.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Joakim Andersson 56bbc4e6d3 Bluetooth: host: Prevent adv overwriting passive scan identity address
Prevent the advertiser from overwriting the passive scanners identity
address when the scanner has been configured to scan using the identity.
In this case the LE Set Random Address command would not prevent the
address from being overwritten. So instead we explicitly stop it in the
host.

Also fix original code function not working at all since the first
if statement was should had a bad check. Resulting in it always
returning success.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Joakim Andersson a6b978b1f2 Bluetooth: host: Drop directed adv reports for NRPA passive scanner
Drop directed advertiser reports when the passive scanner should have
been using the NRPA address. The advertiser has overwritten the NRPA
with it's identity address instead and a peer is sending directed
advertiser packets to it.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Trond Einar Snekvik a112f1568d Bluetooth: host: Skip set passive scan when not scanning with identity
When privacy is disabled by default the scanner still protects it's
identity with the use of NRPA addresses. We should not set the identity
address for the passive scanner unless the Kconfig option to scan with
the identity has been enabled.

This makes passive scanner behave the same way as an active scanner
since none of them will report directed advertising reports towards
the scanners identity.

This also enables the advertiser to switch out the random address which
is needed for the Bluetooth Mesh LPN case.

Fixes #22088.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-02-03 12:44:39 +02:00
Martin Rieva 2685a94c02 Bluetooth: Host: Add generic pairing query callback
Similar to pairing_confirm this callback is called each
time a peer requests pairing, but for all types of
pairings, except SSP. The pairing req/rsp information is
passed as a parameter so the application can decide
wheter to accept or reject the pairing.

Fixes: #21036

Signed-off-by: Martin Rieva <mrrv@demant.com>
2020-01-31 20:39:25 +02:00
Joakim Andersson 291ebdd4e4 Bluetooth: Fix infinite recursion in host-based bt_rand
Fix infinite recursion in host-based bt_rand function. This would call
HCI LE Random Number command, which would in turn call bt_rand, causing
an infinite recursion.

bt_rand -> prng_reseed -> BT_HCI_OP_LE_RAND -> le_rand -> bt_rand

To solve this issue the controller should avoid doing calls into the
host, so all calls to bt_rand in the controller should be replaced with
a call to a controller function.

Fixes #22202

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-31 18:33:06 +01:00
Joakim Andersson bdc535dba3 Bluetooth: host: Fix out of range parameters provided to the controller
Fix LE Create Connection command giving out of range parameters to the
controller, this came back as 0x30 (Out of Range Parameters) status code
on the command from the controller.
This appears to be the min and max CE parameters in the command.
Revert back memset from 137f704064

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-31 17:45:11 +02:00
Joakim Andersson 8629f0a450 Bluetooth: host: Fix app notified connected but no connection exists
Fix problem where application was notified about a new connection being
established, but no connection has actually been made.
This occurred because the LE Create Connection command failed directly
from the API, in which case the state transition thinks the err is valid
and always notifies the application.

Introduced by:
6c1f52dff7 for bt_conn_create_le
e9eebf0c40 for bt_conn_create_auto_le.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-31 17:45:11 +02:00
François Delawarde a3e89e84a8 bluetooth: host: Fix simultaneous pairings getting the same keys slot
Fix an issue where a slot in the key pool was considered free when
either the address was cleared or no keys were written in the entry
(enc_size == 0). This caused a problem with simultaneous pairing
attempts that would be assigned the same entry.

This patch makes it so a a slot is considered free even when keys are
not yet present in the entry, and makes sure the address is cleared in
case of pairing failure or timeout so to mark the slot as free.

Signed-off-by: François Delawarde <fnde@oticon.com>
2020-01-31 12:50:03 +02:00
Trond Einar Snekvik 4ea59711d2 Bluetooth: Move Mesh CCM into a separate module
Moves the Mesh AES-CCM module out into a separate module, to make it
accessible from other subsystems. Adds the new CCM API in
include/bluetooth/crypto.h along with the bt_encrypt functions.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-01-30 12:41:45 +02:00
Joakim Andersson 576c68317a Bluetooth: L2CAP: Reset channel status before destroy callback
Move reset of channel status from after the destroy callback since the
after the destroy callback the memory should be assumed to be released.
Instead clear the channel status when the channel is created in
l2cap_chan_add. This way we don't rely on the memory given being set to
the correct value.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-28 17:55:38 +02:00
Joakim Andersson d84528edb1 Bluetooth: ATT: Fix ATT corrupting mem_slab structure after free
Fix ATT releasing the att structure back to the memory slab allocator
before the structure is actually ready to be released. The memory slab
allocator will write context data inside the freed slab which is
currently being overwritten by l2cap during channel teardown.

This manifests as an "Unable to allocate ATT context for conn" when
reconnecting with multiple connections.

Since the l2cap channel is embedded inside of the ATT context and l2cap
still has a valid referenc to the l2cap channel we need to release the
ATT context at a later time.
This should be fixed by implementing the channel destroy function and
releasing the channel there.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-28 17:55:38 +02:00
Joakim Andersson 83f108afd1 Bluetooth: L2CAP: Add destroy callback to fixed L2CAP channels
Add the option to provide the destroy callback to the fixed channels.
This can be used to free the memory for the L2CAP channel context which
is provided by the fixed channel in the accept callback.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-28 17:55:38 +02:00
Marek Pieta bce335458b Bluetooth: ATT: Fix handling ATT response
The callback function may modify the att->req and it has to be
called after the att_process. The att_process does not re-check
if att->req is still NULL.

Signed-off-by: Marek Pieta <Marek.Pieta@nordicsemi.no>
2020-01-25 22:39:45 +01:00
Ulf Magnusson 0618cf00e8 Bluetooth: host: kconfig: Fix broken ref. to BT_CTLR_TX_BUFFER_SIZE
The CONFIG_ prefix was missing.

Found with a work-in-progress scripts/kconfig/lint.py check.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2020-01-25 08:15:34 -05:00
Joakim Andersson bf33338652 Bluetooth: host: Fix net buf assert on att encrypt change event
Fix assert in net_buf triggered on att encrypt change event.

	ASSERTION FAIL [net_buf_simple_headroom(buf) >= len] @
	  ZEPHYR_BASE/subsys/net/buf.c:881

This happens because when the att request was allocated, it was not
properly initialized and req->retrying was left as true.
This caused the att encrypt change handling to assume an att request
needed to be resent, starting resending with an invalid request and
request buffer.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-23 18:08:24 +02:00
Joakim Andersson 813e5bdd7a Bluetooth: host: Refactor use of #ifdef to IS_ENABLED()
Refactor use of #ifdef to IS_ENABLED() pattern for handling cancellation
of outgoing connection attempt. Reduce the amount of indentation by
combining outer if-statements.
Move handling of canceled create connection into a helper function.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 64b890463d Bluetooth: host: Fix advertiser with identity switches to RPA
Fix advertiser requested to use the identity address while privacy
feature is enabled will change to using RPA address when advertise is
resumed or when RPA timeout occurred.
RPA timeout does not need to run when advertiser is using identity.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson fbe3285bfa Bluetooth: host: Handle initiator at RPA timeout
Handle initiator role when RPA timeout expires. For direct connect
establishment procedure we make sure the RPA is refreshed when starting
initiator and limit the timeout to the RPA timeout.
For auto establishment procedure we cancel the initiator and restart it
again in the connection complete event that is generated when canceling
an initiator.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 99662c63bc Bluetooth: host: Handle starting roles with different random address
Handle starting of advertiser and scanner or initiator when advertiser
is using a different identity than the default identity to generate the
random resolvable address in the controller.
We need to handle this only for the privacy case because the random
address is set in the RPA timeout handler and not from the API.
When privacy is disabled we can return error code from the LE Set Random
Address HCI command instead.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 4876a8f39a Bluetooth: host: Handle scanner active at RPA timeout
Fix RPA timeout handling when the scanner is active. An active scanner
must be restarted at RPA timeout otherwise the Set Random Address
command will fail.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 137f704064 Bluetooth: host: Refactor initiator to common code and initiator state
Refactor the handling of sending the LE Create Connection command and
for whitelist and direct initiator to use the same host state flag
and common handling of the privacy address.
Also simplify the way we check if the procedure has already been started
when application tries to start it again.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 6c1f52dff7 Bluetooth: host: Set conn state before sending HCI command
Handle a possible race condition in the host connection state.
Set the conn state of the connection object before command is sent. This
is in case the calling function is not scheduled again before the
connection complete event arrives. In this case find_pending_conn will
not find the connection object.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson cc6b8c3a6f Bluetooth: host: Ifdef starting of background scanner in init
Starting a background scanner for auto-connection is an API that is only
available when whitelist API is not enabled.
There is currently no way to set this bit when the whitelist API is
enabled so there is not any issues with the current code, but it is
still not correct.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 0861c8c834 Bluetooth: host: Disconnect connection if no conn object is available
When receiving a connection complete event but no connection object are
available in the host something strange has happened. In this case
the controller might have a connection that cannot be controlled by the
application. It would then be sensible to disconnect this connection in
the controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Carles Cufi 6cf7ac77bf Bluetooth: Always use full path to hci_driver.h
The path to include/drivers should not be in the compiler include path
list, only include/. In order to make this possible, always explictly
refer to hci_driver.h via the drivers/bluetooth/ path and not only
bluetooth/.

Fixes #21974.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-01-16 19:20:39 -05:00
Wolfgang Puffitsch dbdfd2995b Bluetooth: host: Guard calls to bt_conn functions
Guard calls to bt_conn functions in bt_le_adv_start_internal with
IS_ENABLED(CONFIG_BT_PERIPHERAL) to avoid undefined symbols in builds
that do not support that role.

Signed-off-by: Wolfgang Puffitsch <wopu@demant.com>
2020-01-16 08:53:21 +01:00
Joakim Andersson ca2d3f0a89 Bluetooth: Kconfig: Fix error in buffer pool sizes
Fix error in calculation of the minimum discardable buffer size. For
the LE Advertising Report the maximum payload is 31 bytes plus
additional data in the event gives an event size of 41 bytes (given that
num_reports is 1). Since this is a meta event we need to include the
sub-event code, plus the event header of 2 bytes. Total of 44 bytes.

This is a regression from afa9c42d75 where we forgot th 1 byte for the
RSSI that is appended after the data. Easy to miss since it is not part
of the struct.

Fix error in calculation of the num complete buffer size. Here we forgot
to include the 2 byte event header.

This is a regression from 89981b07c8.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 21:19:17 +01:00
Joakim Andersson 4401b6a2f3 Bluetooth: host: Stop using existing conn object when creating new conn
Remove re-using connection objects in disconnected state when creating
directed advertiser or establishing a connection as a central using
direct connection procedure.
This makes the API mores consistent it terms of which connection roles
can be started from the disconnected callback.
This also avoids a central connection object being re-used for a
connection as a peripheral instead and vice versa.

When attempting to create a new connection the API would returning
a valid connection object if there is already an existing connection
object.
This existing connection object could be either in the process of
establishing the connection or already connected.
Returning the connection object in this would give the false impression
that the stack has initiated connection procedure, when in fact it just
returned an existing connection object.

The application has the ability to check for existing connection objects
using the bt_conn_lookup_addr_le API.

Add warning plus comment possible scenarios why the a valid connection
object might exists. Most important is to explain why a valid connection
object exists during the disconnected callback.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson 46bf20036a Bluetooth: host: Reserve conn object for connectable advertiser
Reserve conn object for undirected connectable advertiser. This means we
won't have a situation where we start a connectable advertise but will
fail to allocate a connection object for it in the connection complete
event.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson e9eebf0c40 Bluetooth: host: Reserve conn object for connecting with whitelist
Reserve a connection object when starting the auto-initiator using the
controller whitelist.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson e91f9804e6 Bluetooth: host: Fix error code when calling API before init
Fix bt_conn_create_aute_le returning the wrong error code when bt_init
has not been called yet. This is inconsistent with the rest of the API
functions.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson c635425487 Bluetooth: host: Refactor stopping directed advertiser
Refactor stopping directed advertiser to disconnect the state object
when calling advertise stop. This follows the same pattern as
bt_conn_disconnect.

Remove returning conn objects in BT_CONN_CONNECT state, this state could
only  be an initiator starting a connection in the central role.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson a45226a238 Bluetooth: host: Clear auto-conn state when failed to get conn object
Make sure that the auto-conn state is cleared correctly when we might
fail to allocate a new connection object.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson a0349689ff Bluetooth: host: Fix conn object assigned to wrong connection
Fix conn object assigned to the wrong controller connection in the
connection complete handler. This could happen when running a
directed advertiser and establishing a connection at the same time to
the same peer.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson 89981b07c8 Bluetooth: host: Lower the size of the num_complete buffer
Set the size of the num complete buffer to the maximum possible size it
can be.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-13 17:59:00 +01:00
Joakim Andersson afa9c42d75 Bluetooth: host: Add config option for discardable buffer size.
Add option to set the size of the discardable buffer pool. This saves
memory for the MESH use case where we expect a large number of advertise
reports.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-13 17:59:00 +01:00
Trond Einar Snekvik 5b14ab4481 Bluetooth: Add dynamic le scan listener interface
Adds a way to register listeners for incoming scanner packets, in
addition to the callback passed in bt_le_scan_enable.

This allows application modules to add multiple scan packet listeners
without owning the scanner life cycle API, enabling use cases like
beacon scanning alongside Bluetooth Mesh.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-01-13 14:27:01 +01:00
Kim Sekkelund 9ea6e72bae Bluetooth: host: cfg_write callback to return error code
Current implementation of application's cfg_write callback only has the
possibility of returning boolean status, which in case of failure only
allows for one error code; BT_ATT_ERR_WRITE_NOT_PERMITTED.
This change makes the application able to add own security check on
characteristic subscription in the cfg_write callback and report a more
relevant error code (e.g. BT_ATT_ERR_AUTHORIZATION).

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2020-01-10 17:41:15 +01:00
Joakim Andersson c336539249 Bluetooth: host: Remove toggling advertiser on advertise data update
Remove toggling the advertise enable state when the advertiser name has
been updated. Advertise and scan response data should be used by the
controller on subsequent advertising events.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-10 17:10:46 +01:00
Luiz Augusto von Dentz 708c8bae54 Bluetooth: ATT: Use k_mem_slab for connection context
This uses k_mem_slab APIs to allocate/free ATT context instead of custom
array.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Luiz Augusto von Dentz 3be9980bd6 Bluetooth: GATT: Remove _peer member from bt_gatt_subscribe_params
This further reduce the overhead on each subscription at expense of
having a dedicated array to store subscriptions, the code now maintain
a separate list for each peer which should also scale better with large
number of subscriptions to different peers.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Luiz Augusto von Dentz d278cdc3d7 Bluetooth: GATT: Allocate request from a memory slab
This should reduce the footprint on applications that do a lot of
requests i.e have a lot of subscriptions.

Fixes #21103

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Marco Sterbik 9217c6634e bluetooth: fix removal of needed subscriptions
Providing 'tmp', which was never updated, resulted in removeal of
subscriptions from the beginning.
Using the updated 'prev' resolves this.

Signed-off-by: Marco Sterbik <madbadmax00@gmail.com>
2020-01-07 17:01:25 +01:00
Joakim Andersson 57addad856 Bluetooth: host: Use direct connection if not host resolving list
Start initiator immediately instead of scanning for device first.
If the host resolving list is used we need to go via scanner to resolve
the address.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-07 16:04:03 +01:00
Jacob Siverskog 6eef6cd946 Bluetooth: host: Rethink how unpair all works
Instead of having all (=addr NULL or BT_ADDR_LE_ANY to bt_unpair) as a
special case, iterate over all connected peers and unpair them the
regular way. This means bt_gatt_clear is called too. Doing this way
allows us to remove a lot of (now) unused code as well.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2020-01-04 14:10:46 +01:00
ZhongYao Luo a94e81ba3e Bluetooth: RFCOMM: Fix length errors when payload exceeds 127
hdr->length is the length of the payload, it should be
buf->len - sizeof(*hdr) - 1 or buf->len - (sizeof(*hdr) + 1)

Signed-off-by: ZhongYao Luo <LuoZhongYao@gmail.com>
2020-01-03 10:14:59 +01:00
Luiz Augusto von Dentz 2cfef8cd77 Bluetooth: L2CAP: Fix using s16_t to represent credits
Credits are 2 octects long so an s16_t positive portion can only half to
the theorical maximum number of credits, so instead this uses u16_t and
do a bound check instead of checking for negative values.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-02 16:53:27 +01:00
Luiz Augusto von Dentz 4ff711d8a5 Bluetooth: L2CAP: Remove use of k_sem for credits
With the changes that introduced a queue k_sem is only used with
K_NO_WAIT which means it is no longer possible to wait/block for credits
so the usage of k_sem is no longer needed and can be safely replaced
with atomic_t just to count the available credits at a given instant.

Fixes #19922

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-02 16:53:27 +01:00
Sverre Storvold 649a813bf6 Bluetooth: host: Add calling of read_remote_version
Make remote features and remote version accesible to the application
through the bt_conn_get_remote_info object. The host will auto initiate
the procedures. If the procedures have not finished with the application
calls bt_conn_get_remote_info then EBUSY will be returned.
The procedures should finish during the first 10 connection intervals.

Signed-off-by: Sverre Storvold <Sverre.Storvold@nordicsemi.no>
Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-23 14:47:31 +02:00
Joakim Andersson 32bde4fe78 Bluetooth: host: Refactor host auto initiated LL procedures
Refactor the handling of the host auto initiated LL procedures.
This makes it easier to add new auto initiated procedures as well as
reduced the maintenance by reducing code duplication.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-23 14:47:31 +02:00
Joakim Andersson b69edc3844 Bluetooth: HCI: Check length of VS command complete
Always check that the length of the returned command complete
event for a vendor specific command matches the expected length
when the support for Zephyr VS HCI commands are uncertain.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-20 15:45:34 +01:00
Joakim Andersson a34d4afe7d Bluetooth: Host: Fix issues with host IRK handling
Fix multiple issues related to the way the host handles
Identity Information related to privacy

1. If the controller provided a public address the IRK
   for this identity would be randomly generated but not
   stored persistenly.

2. Fix the handling of the above issue which was fixed
   for the random address but would initiate settings save
   ID on every boot.

3. Fix the host not using the Vendor Specific HCI commands
   related to retrieving the Identity Root (IR) from the
   controller and using the key diversified function d1
   to generate an IRK as specified in the BT Core spec.

Make sure that a Host generated ID is only saved when it is first
generated.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-20 15:45:34 +01:00
Johan Hedberg 847f4e6ae6 Bluetooth: L2CAP: Fix checking for STATUS_OUT
The test_and_set_bit() should be checking if the flag was *not*
already set, since that's the scenario where we want to call the
status callback.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Johan Hedberg 525889c6b5 Bluetooth: L2CAP: Remove bogus NULL checks
The ch pointer is the result of a CONTAINER_OF() operation, so
checking it for NULL is pointless. Additionally, there's no place that
calls this function with chan set to NULL.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Johan Hedberg 0e91493ab4 Bluetooth: L2CAP: Make channel ops const
Make the channel ops struct const since there really isn't anything
there that needs to change at runtime. The only exception is the L2CAP
shell which was playing with the recv callback, however that can be
fixed by introducing a simple bool variable.

With tests/bluetooth/shell this reduces RAM consumption by 112 bytes
while adding only 16 bytes to flash consumption.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Luiz Augusto von Dentz 9a111df676 Bluetooth: L2CAP: Add define for signalling timeout
This adds a define for the so called RTX timeout.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 1a7f85cb53 Bluetooth: L2CAP: Document cases where -EAGAIN is returned
This documents the special cases where -EAGAIN is returned which leads
the buffer to be queued.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz b8911a8d35 Bluetooth: L2CAP: Fix data leaks when segment cannot be sent
Now that bt_l2cap_send_cb can fail the buffer state needs to be save
and restored otherwise the data stored on it would be lost.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz bdc1eff35b Bluetooth: L2CAP: Introduce BT_L2CAP_STATUS_SHUTDOWN flag
This introduces BT_L2CAP_STATUS_SHUTDOWN which is used to indicate when
a channel has been shutdown.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz cf84216b1a Bluetooth: L2CAP: Offload processing of tx_queue to a work
This offloads the processing of tx_queue to a work so the callbacks
calling resume don't start sending packets directly which can cause
stack overflow.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 99066db21d Bluetooth: L2CAP: Ignore packets received while disconnecting
Drop packets received while disconnecting since they would most likely
be flushed once peer respond there is no gain in keeping them on a
queue.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 5d26693d02 Bluetooth: L2CAP: Add dedicated pool for disconnect request
This prevents disconnect request packets to not being sent due to lack
of buffers normally caused by flooding or congestion.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz aa9a1f627c Bluetooth: Use NET_BUF_RX_COUNT if NET_L2_BT is enabled
If NET_L2_BT is enabled we need enough acl_in_pool needs to be big
enough to contain a full IP packet since that is no longer processed by
RX thread buffer would be queued to syswq to reassemble the SDU.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz dd4b340205 Bluetooth: L2CAP: Use NET_BUF_TX_COUNT as fragment count
When NET_L2_BT the memory pressure for fragments can be quite high
since that would be transfering IP packets which are considerable big
so this makes our frag_pool to be of the same size as NET_BUF_TX_COUNT.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 4b8cd200ed Bluetooth: conn: Add debug variant to buffer allocation
This enable chaning the function and line number making it easier to
debug where a buffer allocation is blocking.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz bdb8b0af70 Bluetooth: L2CAP: Queue packets when a segment could not be allocated
When a segment could not be allocated it should be possible to resume
sending it later once previous segments complete, the only exception is
when there is no previous activity and we are unable to alocate even the
very first segment which should indicate to the caller that it would
block since that only happens on syswq the caller might need to defer to
another thread or resubmit the work.

Fixes #20640

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Wolfgang Puffitsch b0207eb778 Bluetooth: host: Refactor checks in update_ccc for efficiency
Check peer address only if active.

Signed-off-by: Wolfgang Puffitsch <wopu@demant.com>
2019-12-12 15:26:38 +02:00
Joakim Andersson f7f63c6306 Bluetooth: host: Check for device ready for whitelist API
The whitelist API uses the controller directly through HCI commands.
Bluetooth device must have been initialized before sending HCI commands.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-12 12:19:40 +02:00
Joakim Andersson a1d73acfcd Bluetooth: HCI: Handle invalid ACL flags.
Handle invalid ACL flags in HCI transport.
Only Point to Point is supported over HCI in both directions.
Fix flushable start HCI ACL packets not allowed on LE-U connections
from Host to controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-11 12:34:34 +02:00
ZhongYao Luo d1aa80c049 bluetooth: l2cap_br: l2cap channel used wrong cid when removing
In bt_l2cap_br_chan, rx.cid is the local cid and tx.cid is the
remote cid. According to Core-5.0 Vol3.Part A 4.6-4.7,
l2cap_br_remove_tx_cid should be searched using tx.cid

Signed-off-by: ZhongYao Luo <LuoZhongYao@gmail.com>
2019-12-11 11:34:43 +02:00
Joakim Andersson e4136178ed Bluetooth: Host: Fix duplicate whitelist entries issue
If the whitelist already exists in the controller then the controller
should not add the device tot the whitelist and should return success.
In that case the counting of entries in the whitelist in the host will
be wrong.

Remove all whitelist counting in the host, and instead rely on the error
reported by the controller for this.
The controller should return error if the whitelist is full.
The controller should return error if use of whitelist was requested but
the whitelist was empty.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-10 17:54:10 +02:00
Joakim Andersson b30b480c7c Bluetooth: UUID: Expose bt_uuid_to_str to application
Expose the bt_uuid_to_str function as an API to the application.
This aligns this function with the bt_addr_to_str function call. This
allows the application to use this function without having to enable
the BT_DEBUG option.

Move the in-place bt_uuid_str to internal logging, this is mainly done
due to the limitation in the log_strdup that shouldn't be exposed to the
application.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 17:15:05 +02:00
Ulf Magnusson 87e917a925 kconfig: Remove redundant 'default n' and 'prompt' properties
Bool symbols implicitly default to 'n'.

A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.

Also replace some

    config
    	prompt "foo"
    	bool/int

with the more common shorthand

    config
    	bool/int "foo"

See the 'Style recommendations and shorthands' section in
https://docs.zephyrproject.org/latest/guides/kconfig/index.html.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-12-09 16:14:50 +01:00
Joakim Andersson cbf666ecc3 Bluetooth: Host: Check that bluetooth device is ready
Check that the bluetooth device has in fact been initialized before
continuing with public API calls. This could lead to crashes when using
state that has not yet been initialized.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 16:11:19 +01:00
François Delawarde db106a2b5b bluetooth: host: Add flag to prevent client resubscription on reconnect
When set, the BT_GATT_SUBSCRIBE_FLAG_NO_RESUB flag indicates that the
subscription should not be renewed when reconnecting with the server.

This is useful if the application layer knows that the GATT server
persists subscription information.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-12-09 17:07:39 +02:00
Martin Rieva bf361aa66c Bluetooth: Host: Add CONFIG_BT_BONDING_REQUIRED flag
Added configuration for accepting pairing requests only if both devices
has bonding flag set in order to reject other devices at an early stage,
thus leaving more chance for devices expected to bond.

With the CONFIG_BT_BONDING_REQUIRED flag the device only accept pairing
requests if it has CONFIG_BT_BONMDABLE set and the device requesting
pairing has Bonding_Flags field set to Bonding (0x01) in its AuthReq.
Note: When using bt_set_bondable(false) pairing requests will be
rejected when CONFIG_BT_BONDING_REQUIRED is set.

Signed-off-by: Martin Rieva <mrrv@demant.com>
2019-12-09 17:05:32 +02:00
Kiran Paramaswaran f30bed350c Bluetooth: Host: Enable/Disable Automatic Sending of Conn Parameter update
Added a new Kconfig flag to enable/disable this feature.

Signed-off-by: Kiran Paramaswaran <kipm@oticon.com>
2019-12-09 17:04:50 +02:00
Joakim Andersson b5f70ef1c0 Bluetooth: GATT: Fix discover callback invalid pointer
Fix issues that surfaced when trying out GCC 9.2, official release name
gcc-arm-none-eabi-9-2019-q4-major, both related to invalid pointers in
GATT when declaring UUIDs in if-scope.

1. Fix the discovery callback giving an invalid pointer in the discovery
callback in two instances.
2. Fix gatt_find_type sending invalid data during discovery procedure.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 16:58:01 +02:00
Kiran Paramaswaran d86d37a075 Bluetooth: Host: Adding valid param check in send_conn_le_param_update()
Send connection parameter update request only if it contains the valid
range of values for connection intervals, latency and timeout.

Signed-off-by: Kiran Paramaswaran <kipm@oticon.com>
2019-12-09 16:57:38 +02:00
Jacob Siverskog 7096fa512f Bluetooth: GATT: Remove all subscriptions for connection when unpairing
Make sure all subscriptions are removed when a connection is unpaired.

Fixes #21131

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2019-12-04 19:14:28 +01:00
Joakim Andersson afe088247d Bluetooth: ATT: Fix ATT MTU support for larger MTUs
Fix ATT MTU size of length variables resulting in wrong length values
reported to the user. Communicating with an Android device using an MTU
of 517 resulted in write commands reported as length 2 instead.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-04 19:08:10 +01:00
Luiz Augusto von Dentz 5beb5b5992 Bluetooth: ATT: Fix not handling errors properly
Since bt_conn_send_cb can fail to send buffer causing it to unref this
may cause buffer leaks as the caller is not aware of the error assuming
the buffer could be sent.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-03 19:44:51 +01:00
Luiz Augusto von Dentz 42c5b0a7fa Bluetooth: L2CAP: Fix unrefing buffers that are queued
Processing of data received on dynamic channels is still done via syswq
so the buffer shall not be unrefed when they are queued.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-11-29 15:19:08 +02:00
Luiz Augusto von Dentz 3f43413887 Bluetooth: L2CAP: Fix not unrefing tx_buf
If the channel has a tx_buf it must be unrefed when destroying the
channel.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-11-29 15:19:08 +02:00
Johan Hedberg 1e831befea Bluetooth: host: Fix ordering of TX sent callbacks
Now that the TX callbacks happen from the system workqueue but fixed
channels get processed from the RX thread there's a risk that the
ordering of these gets messed up. This is particularly bad for ATT
when it's trying to enforce flow control.

To fix the issue store the completed TX packet information in a
per-connection list and process this list before processing any new
packets for the same connection. We still also schedule a workqueue
callback, which will simply do nothing for this list if bt_recv()
already took care of it.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 19:55:59 +02:00
Johan Hedberg 39291fbbbe Bluetooth: Remove usage of BT_BUF_USER_DATA_MIN
This define is not of use anymore since there's a global net_buf user
data Kconfig variable and its definition already guarantees a
sufficient minimum for Bluetooth.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 13:35:11 +02:00
Johan Hedberg be71e68fcb Bluetooth: L2CAP: Use fixed size for net_buf user data
The "sent" tracking doesn't have to be a signed integer. Use a fixed
size so that the consumed size doesn't change between different
architectures. Use a u16_t since bigger sizes are needed and because
this is mapped to an int function return higher up in the stack.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 13:35:11 +02:00
Kamil Piszczek 362f2299cd drivers: bluetooth: hci: rpmsg: fix handling of hci events
Fixed handling of HCI events in the HCI driver over RPMsg. Now,
the driver makes use of discardable buffer pool when allocating
memory for certain HCI event types (e.g. Advertising Report Event).
Applications that are flooded with Advertising Reports will run
much better after this change (e.g. Mesh applications).

Signed-off-by: Kamil Piszczek <Kamil.Piszczek@nordicsemi.no>
2019-11-28 10:55:32 +02:00
Johan Hedberg 0a925bdf9c Bluetooth: L2CAP: Process fixed channels in the RX thread
Now that the TX path and TX context (bt_conn_tx) has been redesigned
to free the contexts always in the system workqueue, it means the
system workqueue is the only context where their allocation may also
fail. This is particularly problematic with us having all L2CAP
channels (fixed & CoC alike) deferred to the system workqueue. It is
especially bad for fixed channels where being able to send responses
for SM, L2CAP signaling and ATT is critical to avoid timeouts for the
connection.

This patch moves the processing of all fixed L2CAP channels back to
the RX thread, thereby making it possible (and safe) to block while
waiting for a TX context to become available.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg a065e5782a Bluetooth: host: Move system workqueue special case to the right place
Now that we've removed the TX allocation dependency from the TX thread
we no longer have the need to do special-casing for the system
workqueue when allocating buffers. Instead, we do have to special-case
the system workqueue when allocating TX contexts since the system
workqueue is the only place where they get freed up.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg d8689cdc72 Bluetooth: host: Fix deadlocks with pending TX packet handling
This is a moderate redesign of the pending TX packet handling that
aims to eliminate potential deadlocks between the TX thread and the
system workqueue thread. The main changes are:

 - TX context (bt_conn_tx) is allocated during buffer allocation, i.e.
   not in the TX thread.

 - We don't allocate a TX context unless there's an associated
   callback. When there's no callback simple integer counters are used
   for tracking.

 - The TX thread is no longer responsible for TX callbacks or
   scheduling of TX callbacks. Instead, the callbacks get directly
   scheduled (k_work_submit) from the RX priority thread.

 - CONFIG_BT_CONN_TX_MAX defaults to CONFIG_BT_L2CAP_TX_BUF_COUNT,
   and in most cases wont need changing. The value now only indicates
   how many pending packets with a callback are possible.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg 28f89478f6 Bluetooth: host: Shrink size of struct bt_conn_tx
The `node` and `work` members are never used simultaneously.
Additionally k_work already has built-in support for being in a linked
list, however a union makes this change a bit cleaner.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg 5a8b143028 Bluetooth: host: Fix command buffer corruption
A recent patch increased struct cmd_data from 8 to 12 bytes, which is
more than the default user data for Bluetooth. We generally don't want
the core stack to require more than 8, so instead of increasing the
requirement, move the data out from the buffer into its own array with
the help of the net_buf_id() API.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-26 16:08:00 +02:00
Joakim Andersson 0bf9931c2c Bluetooth: Host: Fix create connection fails to stop scanner
Fix race condition in bt_conn_create_le for the state of the scanner in
the Host. This leads to the host issuing a create connection command
without stopping the scanner first. This leads to command disallowed and
failing to establish connection. As well as inconsistent state in the
host which does not allow to stop the running scanner.

The race condition exists because the processing of le_adv_report
handler is done before the thread that called bt_conn_create_le was
woken up to continue after the command_complete event.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-25 17:21:17 +01:00
Joakim Andersson 113b80ff0d Bluetooth: Host: Fix silent LE conn param update command failure
Fix command status for LE Command Param Update HCI command silently
dropped by the host without notifying the application that this command
has failed. This happens because the host does not wait for the command
status event to check the status code returned.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-21 17:21:01 +02:00
Joakim Andersson a961679171 Bluetooth: Host: Fix deadlock calling API functions from SMP callbacks
Fix deadlock in Bluetooth Host. Deadlock could happen from the SMP
callbacks when calling Bluetooth API functions. This is because the
callbacks was given directly from the HCI TX thread. If the calling
API function resulted in trying to send a new HCI command it would post
this HCI command to the HCI TX thread and then wait for command complete
event. This would result in the HCI TX thread blocked waiting for the
itself to process the command.

Example:
Calling bt_conn_le_conn_param_update from pairing_complete callback.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-21 14:51:50 +02:00
Johan Hedberg d67416321e Bluetooth: hci_raw: Add support for specifying buffer headroom
The HCI transport implemented by an application using the HCI raw
interface may have its own buffer headroom requirements. Currently the
available headroom gets completely determined by the selected HCI
driver. E.g. most of the time this is the native controller driver
which doesn't reserve any headroom at all.

To cover for the needs of HCI raw users, add a new Kconfig variable
for the apps to set to whatever they need. Correspondingly, use the
maximum of the HCI driver and HCI raw headroom requirements for the
buffer pool definitions and the headroom initializations.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-20 19:29:06 +01:00
Joakim Andersson ea7a1859af Bluetooth: GATT: Fix taking address of packed member of struct sc_data.
Fix GCC9 warning "warning: taking address of packed member of
'struct sc_data' may result in an unaligned pointer value"
Issue is that the on-air structure of sc_data was re-used for the gatt
service changed data.

Added build assert because data is stored in settings, so the structure
should be the same size to be compatible.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-18 13:54:15 +01:00
Sebastian Bøe 59abaf461b cmake: make the BT_HCI_TX_STACK_SIZE's prompt conditional
The BT_HCI_TX_STACK_SIZE is carefully calculated from other
options. When those options change, e.g. from a menuconfig update, it
is important that the stack size is re-calculated, but it is not when
there is a prompt.

Therefore, make the prompt conditional such that the previously set
value is only used when it has been explicitly configured to be so.

Now users can still change the value through menuconfig and prj.conf,
by also enabling <option>_WITH_PROMPT, but when the value is
calculated by the defaults, it will continue to be calculated by
defaults instead of inheriting the intial value.

This is AFAIK a novel approach, but testing has shown that it gives
the users the behaviour they want, at the cost of some boilerplate of
course. This pattern can be applied to other options if it proves to
work as intended.

Alternatively one could remove the prompt, but then it would no longer
be possible to override the value through menuconfig.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2019-11-15 13:28:56 +01:00
Johan Hedberg 1df9a2e1d3 Bluetooth: hci_raw: Fix buffer init after allocation
The code was not properly taking into account CONFIG_BT_HCI_RESERVE,
which would cause buffer underruns for any HCI driver where this value
defaults to non-zero. Also, all the allocation functions use the same
pool, so we can map them simply to bt_buf_get_rx() instead of
repeating the same code.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-12 16:59:43 +02:00
Johann Fischer fd82d2b47c Bluetooth: l2cap: fix null pointer dereference
bt_conn_create_pdu_timeout() may return NULL if no buffer
is available, l2cap_chan_create_seg() does not check the
subsequent return value.
Fix possible null pointer dereference in l2cap_chan_create_seg()
and l2cap_chan_le_send().

Signed-off-by: Johann Fischer <j.fischer@phytec.de>
2019-11-10 19:18:53 +02:00
François Delawarde 4ca408ed6a bluetooth: host: refactor ccc settings handling
Move functions in order to avoid function prototype and use IS_ENABLED
instead of #if defined where possible.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde a51986616e bluetooth: host: add ability to load CCC settings on demand
This commits adds a BT_SETTINGS_CCC_LAZY_LOADING option to allow for
CCC settings to be loaded on demand when a peer device connects in
order to reduce memory usage.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde 42eb629188 bluetooth: host: refactor sc settings handling
Move functions in order to avoid function prototypes.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde fd10c4c962 bluetooth: host: do not remove SC entry when peer subscribed and paired
Maintain Service Changed entry as long as the peer device is subscribed
to SC indications and bonded. This allows to save indication data for
disconnected peers peers when CCC settings are not available (loaded
on-demand).

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
Ioannis Glaropoulos 04e66dab53 bluetooth: host: add nRF53 in HW variants
This commit adds a string representation of the
nRF53 variant in bluetooth host.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
2019-11-08 19:26:35 +01:00
Joakim Andersson 64d6d1079c Bluetooth: SMP: Add option to disallow unauthenticated re-pairing
Disallows all unauthenticated pairing attempts made by the
peer where an unauthenticated bond already exists.
This would enable cases where an attacker could copy the peer device
address to connect and start an unauthenticated pairing procedure
to replace the existing bond. Now in order to create a new bond the old
bond has to be explicitly deleted with bt_unpair.

Added option to disable this rule in order to maintain backwards
compatibility in case this behavior is accepted.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-08 13:42:56 +02:00
François Delawarde 129e05c03f bluetooth: host: fix missing log_strdup
Fix missing log_strdup when loading bt/name setting. It should be done
on every string which is not in read only memory.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-07 12:49:34 +02:00
Trond Einar Snekvik 2cc0263a53 Bluetooth: Reduce severity of unavoidable warnings
Reduces the severity of warnings that happen from normal behavior, or
can't be prevented by the user:
- "No ID address" in hci_core.c: Reduced to an informational warning, as
  this will always output with the expected usage. This isn't useful
  information for 99.9% of users, and pollutes the output of all samples
  using the module.
- "Composition page %u not available" in cfg_srv.c: According to the
  Mesh Profile Specification section 4.4.2.2.2, the client is expected
  to send page=0xff. Reduced to a debug message.
- "Connectable advertising deferred" in proxy.c: Gets logged every 10
  seconds when in a Mesh Proxy connection. This is not useful
  information unless the user is debugging the proxy module. Reduced to
  a debug message.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-11-06 19:24:32 +02:00
Ulf Magnusson bd6e04411e kconfig: Clean up header comments and make them consistent
Use this short header style in all Kconfig files:

    # <description>

    # <copyright>
    # <license>

    ...

Also change all <description>s from

    # Kconfig[.extension] - Foo-related options

to just

    # Foo-related options

It's clear enough that it's about Kconfig.

The <description> cleanup was done with this command, along with some
manual cleanup (big letter at the start, etc.)

    git ls-files '*Kconfig*' | \
        xargs sed -i -E '1 s/#\s*Kconfig[\w.-]*\s*-\s*/# /'

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-04 17:31:27 -05:00
François Delawarde 5317008ad3 bluetooth: host: fix missing bt_conn_unref
Fix missing bt_conn_unref when using the ccc match callback.

Fixes: #20299

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-04 09:43:13 +01:00
Peter A. Bigot bf5817d273 Bluetooth: rework to support C++20 designated initializers
C++ does not allow chaining of data members when identifying the
designator.  Since the generic structure has only one member remove
the designator from its internal initializer.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-11-03 13:24:57 +01:00
Ulf Magnusson 975de21858 kconfig: Global whitespace/consistency cleanup
Clean up space errors and use a consistent style throughout the Kconfig
files. This makes reading the Kconfig files more distraction-free, helps
with grepping, and encourages the same style getting copied around
everywhere (meaning another pass hopefully won't be needed).

Go for the most common style:

 - Indent properties with a single tab, including for choices.

   Properties on choices work exactly the same syntactically as
   properties on symbols, so not sure how the no-indentation thing
   happened.

 - Indent help texts with a tab followed by two spaces

 - Put a space between 'config' and the symbol name, not a tab. This
   also helps when grepping for definitions.

 - Do '# A comment' instead of '#A comment'

I tweaked Kconfiglib a bit to find most of the stuff.

Some help texts were reflowed to 79 columns with 'gq' in Vim as well,
though not all, because I was afraid I'd accidentally mess up
formatting.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-01 15:53:23 +01:00