Commit graph

137 commits

Author SHA1 Message Date
Joakim Andersson c97e1aafb1 Bluetooth: Mesh: Fix possible NULL dereference in BT_DBG statement.
Fix possible NULL dereference in BT_DBG statement when
bt_mesh_friend_get is called before a successful cfg_srv init

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-03-06 10:04:46 +02:00
Trond Einar Snekvik 2835da8194 Bluetooth: Mesh: Fix cdb key update iteration
Fixes copy/paste error taking the size of the the wrong array.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-03-05 20:33:07 +02:00
Tobias Svehagen 60563977df Bluetooth: Mesh: Fix handling of app index and local device key
Fix problem of not checking if the remote device key is actually our
own. This bug was intruduced in
46a95f12ad and causes failure of models
that use app_idx BT_MESH_KEY_DEV_REMOTE. Since this is used by cfg_cli,
it was not possible to do self-configuration.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2020-02-28 20:18:54 +02:00
Tobias Svehagen 7a737a0084 Bluetooth: Mesh: Fix bug with storing node in CDB
The free_slot->clear must be set to false since it could still be true
from previous use.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2020-02-27 12:40:19 +02:00
Trond Einar Snekvik 7b48611143 Bluetooth: Mesh: Group resends in proxy nodes
Resend transport segments for groups on the advertiser interface, even
if a connected proxy node holds the group.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-02-27 10:02:40 +02:00
Stephen Pliaskin 84e381e0a7 Bluetooth: Mesh: typo in condition in comp_add_elem of cfg_srv
Vendor model IDs take up four (not two) bytes in the composition

Fixes #22822

Signed-off-by: Stephen Pliaskin <strelok@e-kirov.ru>
2020-02-16 23:28:51 +03:00
Trond Einar Snekvik d55c8ead03 Bluetooth: Mesh: GATT Proxy Spec 1.0.1 changes
Backport of https://github.com/apache/mynewt-nimble/pull/724

Mesh spec 1.0.1 changes proxy disabling behavior to only affect the
relaying from proxy nodes. Previously, disabling proxy would shut down
all proxy and node activity.

Tweaks from the original commit:
- Removed redundant call to bt_mesh_adv_update() in gatt_proxy_set()
- Removed invalid ref to 4.2.11.1 in node_identity_set()

---

According to Mesh Profile Spec 1.0.1, Section 4.2.11:
"If the Proxy feature is disabled, a GATT client device can connect
over GATT to that node for configuration and control. Messages from
the GATT bearer are not relayed to the advertising bearer."

Moreover some notes have been removed from the spec compared to
version 1.0:

Mesh Profile Spec 1.0, Section 4.2.11:

"Upon transition from GATT Proxy state 0x01 to GATT Proxy state 0x00
the GATT Bearer Server shall disconnect all GATT Bearer Clients."

"The Configuration Client should turn off the Proxy state as the last
step in the configuration process."

Mesh Profile Spec 1.0, Section 4.2.11.1:

"When the GATT Proxy state is set to 0x00, the Node Identity state
for all subnets shall be set to 0x00 and shall not be changed."

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-02-14 14:43:06 +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
Johan Hedberg 578dbe1c0a Bluetooth: Mesh: Support reliable sending when publishing
Until now the choice of reliable sending (segmented messages with
acks) was implicitly dependent on the size of the payload. Add a new
member to the bt_mesh_model_pub to force using segment acks even when
the payload would fit a single unsegmented message.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2020-02-07 20:42:15 +02:00
Tobias Svehagen 46a95f12ad Bluetooth: Mesh: Add database for managing nodes and keys
Refactor the handling of network nodes and their keys into a separate
Mesh Configuration Database (CDB). This, not only creates a separation
of the local node and the other nodes, but also makes it possible to
implement functions to manage the whole, or at least parts of the mesh
network.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2020-02-05 18:47:41 +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
Johan Hedberg 637582dfae Bluetooth: Mesh: Fix provisioning buffer initialization
When PB-GATT support has been enabled the provisioning code "borrows"
the buffer from the proxy code. However, the way that initialization
was happening the proxy buffers were initialized only after
provisioning initialization, resulting in a corrupted buffer with
buf->data pointing to NULL. Reorder the initialization calls so that
proxy is done first and provisioning only after it.

Fixes #22207

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2020-01-29 18:59:40 +02:00
Trond Einar Snekvik 19718fd6da Bluetooth: Mesh: Skip publish if update fails
Allow models to skip a periodic publish interval by returning an error
from the publish update callback.

Previously, an error return from publish update would cancel periodic
publishing. This can't be recovered from, and as such, no valid model
implementation could return an error from this callback, and there was
no way to skip a periodic publish.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-01-16 14:13:37 +02:00
Ahmed Hussein d24e5e520c Bluetooth: Mesh: Enable Segmented Control Messages
The function bt_mesh_ctl_send() used to support maximum length of
11 bytes. The segmentation complies with the BLE Mesh Standard.
The ack is disabled in case of non unicast address.

Signed-off-by: Ahmed Hussein <ahmed_hussein_@hotmail.com>
2020-01-11 18:20:52 +01:00
Trond Einar Snekvik 723e14a432 Bluetooth: Mesh: Model start callback
Replaces the Mesh model settings_commit callback with a start callback,
indicating that the mesh model behavior is ready to start. Everything
that was previously done in the settings_commit callback may be moved to
this callback, which gets called just after mesh settings are committed,
instead of in the middle of the process.

This resolves an issue where models had no context in which to start
their behavior, as the previous settings_commit call fired before the
mesh was declared valid, making access APIs inaccessible.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-01-11 18:20:00 +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
Aurelien Jarno b35d414916 Bluetooth: Mesh: Fix PreviousAddress endianess in Friend Request
The upper transport layer is using big endian ordering. The
PreviousAddress field of a Friend Request message should therefore
be converted to native endianess using sys_cpu_to_be16().

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2020-01-03 10:14:10 +01:00
Joakim Andersson 63567c90ea Bluetooth: mesh: Add comment explaining use of byte order
Add comment that explains why a different byte order is used for the
3-byte opcode on the CID part of the opcode.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-02 19:05:27 +01:00
Joakim Andersson 6426cd58fc Bluetooth: mesh: Use 24-bit functions
Use 24-bit functions for byteorder and net_buf in order to make the
byteorder used more readable.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-02 19:05:27 +01:00
Lingao Meng 6e260af889 Bluetooth: Mesh: Fixes Friend Add Another Update
Fixes: #20746

According MESH/NODE/FRND/FN/BV-20-C should add another
Friend Update.

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-12-14 16:59:59 +02:00
Aurelien Jarno a2ddf99f0c Bluetooth: Mesh: fix next_period computation
If the duration to publish is roughly the same as the period, we might
end up with elapsed == period, which returns 0 and cancel the periodic
publication. Instead 1 should be returned, just like when the elapsed
time is greater than the period.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2019-12-12 09:47:42 +02:00
Trond Einar Snekvik fc56b37116 Bluetooth: Mesh: Refactor CCM
Unifies the Mesh CCM implementation parts for encryption and decryption
into a crypt and an auth step, reducing stack usage and code size.

This change also brings several performance improvements, most notably
reducing copying of the nonce and unrolling the 16 byte XOR operations.

Performance for the Mesh worst case of a 382 byte payload with 16 bytes
of additional data (full transport encrypt with virtual address) goes
from an average of 889us to 780us on nRF52840 with default optimization
flags.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-12-11 23:53:02 +02:00
Maximus Liu 2f691d2597 Bluetooth: Mesh: Fixes wrong subnet used for Friend Clear
When Friend node tries to send Friend Clear message to other
Friend nodes, it should use the subnet information based on
the net_idx from friendship.

Fixes #21165

Signed-off-by: Maximus Liu <maximus.liu@gmail.com>
2019-12-09 16:35:29 -05: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
Lingao Meng a071957746 Bluetooth: Mesh: Fixes wrong return value
Fixes missing return value on `va_del`
Fixes wrong return value on `va_add`

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-11-27 11:22:20 +02:00
Maximus Liu 1050ef7932 Bluetooth: Mesh: Fixes seg_tx_reset adv buf unref
In seg_tx_reset() in transport.c, set the busy flag to 0U
before doing adv buf unref, which will avoid sending
unnecessary adv packets in case the adv buf is already put
in the mesh adv_queue.

Fixes #20970

Signed-off-by: Maximus Liu <maximus.liu@gmail.com>
2019-11-27 11:21:46 +02:00
Lingao Meng c0cdd46adf Bluetooth: Mesh: Fixes Delete On Node
Fixes a issure where nodes could not be deleted.

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-11-27 10:16:53 +02:00
Lingao Meng ff9f208492 Bluetooth: Mesh: Fixes Config client send publish message
When Config client model send publish message, app_idx will
be need, however, currently code clear this value use `&`,
this will be generate error when app_idx not zero.

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-11-14 14:26:21 +02:00
Trond Einar Snekvik 0151d6dc33 Bluetooth: Mesh: Model extension concept
Adds the model extension concept to the access layer, as described in
the Mesh Profile Specification, Section 2.3.6. Extensions are
implemented as a tree, using two pointers in each model:

The extends pointer points to the first extended model, and the next
pointer points to the next sibling or (if the NEXT_IS_PARENT flag is
set) the parent model in the tree, forming a cyclical "Left-child
right-sibling" (LCRS) tree. The tree root can be obtained by calling
bt_mesh_model_root_get(), and the extended models can be walked by
calling bt_mesh_model_tree_walk().

According to the Mesh Profile Specification Section 4.2.3, all models in
the same extension tree share one subscription list per element. This is
implemented by walking the model's extension tree, and pooling the
subscription lists of all models in the same element into one. If the
config server adds a subscription to a model, it may be stored in any of
the model tree's models' subscription lists. No two models in the same
extension tree and element will have duplicate groups listed. This
allows us to increase extended models' capacity for subscriptions
significantly.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-11-07 18:57:32 +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
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
Tobias Svehagen 3282ff22c4 Bluetooth: Mesh: Add support for provisioner over PB-ADV to shell
Adds the command provision-adv to the mesh shell.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2019-10-30 13:08:09 +01:00
Tobias Svehagen 7e3f0c125a Bluetooth: Mesh: Add bt_mesh_provision_adv API
Add API for supporting provisioning of devices over PB-ADV.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2019-10-30 13:08:09 +01:00
Tobias Svehagen 80669decce Bluetooth: Mesh: Add support for provisioner role over PB-ADV
Make it possible to provision devices over advertising bearer (PB-ADV).
Many messages in the provisioning protocol are the same for provisioner
and device so much of the code could be reused by only changing when
they are expected to arrive.

This introduces to concept of local and remote device keys. The models
for cfg_cli and cfg_srv have been updated to reflect this concept. Both
the send and receive path in the transport layer have been updated to
support encrypting/decrypting with local and remote device keys.

When a node has been provisioned it is stored in bt_mesh_net.nodes. If
CONFIG_BT_SETTINGS is enabled, they are also saved to settings. If the
callback node_added in bt_mesh_prov has been set, it will be called for
every node that gets provisioned. This includes when they are retrieved
from settings.

The configuration CONFIG_BT_MESH_NODE_COUNT controls how many nodes that
can be provisioned.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2019-10-30 13:08:09 +01:00
Tobias Svehagen de92d1a83b Bluetooth: Mesh: Add callback for unprovisioned device beacon
Adds the unprovisioned_beacon callback to the bt_mesh_prov structure.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2019-10-30 13:08:09 +01:00
Lingao Meng 101ad56d43 Bluetooth: Mesh: Persistent storage of Virtual Addresses
The 16-bit format group addresses will be stored,
but we don't store (or restore) the virtual label UUIDs,
i.e. after a power cycle the 16-bit group addresses
would be meaningless.

Fixes #19342

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-10-17 12:24:13 +03:00
Lingao Meng 345b64f106 Bluetooth: Mesh: Fixed Provision Random buffer size
Fixed some minor issues, missing a byte for opcode.

Fixes: #19767

Signed-off-by: Lingao Meng <mengabc1086@gmail.com>
2019-10-15 23:22:04 +03:00
Trond Einar Snekvik f7d9af8cb2 Bluetooth: Mesh: Proxy forwards ALL_NODES addr
Unless explicitly blacklisted, the Proxy node will forward all messages
for the ALL_NODES address to the GATT proxy client.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-10 13:09:42 +02:00
Trond Einar Snekvik 5aee3ee995 Bluetooth: Mesh: Model message macros
Creates macros for determining model message lengths based on opcode,
payload length and MIC size. Also adds macro wrapping
NET_BUF_SIMPLE_DEFINE to serve the most common use case.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-08 18:38:43 +03:00
Trond Einar Snekvik 6911287899 Bluetooth: Mesh: Use net_buf_simple_clone
Uses net_buf_simple_clone to access the sdu of an unsegmented app packet
for re-encryption.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-04 20:56:25 +03:00
Trond Einar Snekvik 9fdefc7d73 Bluetooth: Mesh: Skip local messages in rx
Local messages are already enqueued for the LPN in the tx path, and
don't have to be added again in the rx path.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-04 20:56:25 +03:00
Trond Einar Snekvik a7cf776d52 Bluetooth: Mesh: Ensure seqnum match in app/net
Re-encrypts single-segment application messages when the network seqnum
has changed, to avoid encrypting messages with different seqnums in
network and transport. This operation is only required for unsegmented
messages, as segmented messages don't need to use the same seqnum in
network.

Reinstates the special adv data for friend messages to store the app key
index.

Fixes #19265.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-04 20:56:25 +03:00
Trond Einar Snekvik 70b5556b09 Bluetooth: Mesh: Expose app key get
Provides a utility function for getting an application key given a
subnet and an app ID. Primary use-case in friendship re-encryption.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-04 20:56:25 +03:00
Trond Einar Snekvik ec5e43ba5f Bluetooth: Mesh: Expose header parsing outside net
Provides utility function for parsing network headers outside of the
network layer. The primary intended use-case is friendship.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-10-04 20:56:25 +03:00
Johan Hedberg bca12197fd Bluetooth: Mesh: crypto: Remove unnecessary #ifdefs
Static variables and functions don't need #ifdefs if the code calling
them is using IS_ENABLED(). If IS_ENABLED() evaluates to false the
compiler will strip out all the static entities.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-10-04 16:03:57 +03:00
Johan Hedberg 84847f5c6d Bluetooth: Mesh: beacon: Remove unnecessary #ifdefs
Static variables and functions don't need #ifdefs if the code calling
them is using IS_ENABLED(). If IS_ENABLED() evaluates to false the
compiler will strip out all the static entities.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-10-04 16:03:57 +03:00
Johan Hedberg 71283c6c9b Bluetooth: Mesh: net: Remove unnecessary #ifdefs
Static variables and functions don't need #ifdefs if the code calling
them is using IS_ENABLED(). If IS_ENABLED() evaluates to false the
compiler will strip out all the static entities.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-10-04 16:03:57 +03:00
Johan Hedberg 4f3b0cb28c Bluetooth: Mesh: Remove unused macro
The FRIEND_BUF_SIZE macro has no users.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-10-02 10:36:12 -07:00
Joakim Andersson 3920844842 Bluetooth: Host: Fix usage of re-using Bluetooth log buffers
Fix calling bt_hex and bt_addr_le_str multiple times in the same logging
call could result in string overwritten since log_strdup is not
guaranteed to duplicate the string buffer in all logging configurations.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-30 17:47:43 +02:00
Johan Hedberg 6d798c8cce Bluetooth: Mesh: Fix Clear Procedure start timestamp initialization
The start timestamp was supposed to signify the starting point of the
clear procedure. The code was incorrectly initializing it to the *end*
point of the procedure.

Fixes #19263

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-09-26 15:30:51 +03:00
Luiz Augusto von Dentz 03b9ce487c Bluetooth: GATT: Add support to setting permission on CCCD
This adds support to set different permissions to CCCD so security can
be checked when enabling notification which conforms to:

BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2360:

  '3.3.3.3 Client Characteristic Configuration

   Authentication and authorization may be required by the server to
   write the configuration descriptor.'

In addition to that also ensure that notification are not re-enabled
until the proper security level is reached to conform to the following
statement:

  '10.3.1.1 Handling of GATT indications and notifications

   A client “requests” a server to send indications and notifications
   by appropriately configuring the server via a Client Characteristic
   Configuration Descriptor. Since the configuration is persistent
   across a disconnection and reconnection, security requirements must
   be checked against the configuration upon a reconnection before
   sending indications or notifications. When a server reconnects to a
   client to send an indication or notification for which security is
   required, the server shall initiate or request encryption with the
   client prior to sending an indication or notification. If the client
   does not have an LTK indicating that the client has lost the bond,
   enabling encryption will fail.'

Fixes #17983

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-09-19 21:12:39 +03:00
Joakim Andersson ff2bd93eea Bluetooth: GATT: Add initializer for GATT CCC
Add GATT initializer macro for GATT CCC and allow the
BT_GATT_CCC_MANAGED to accept an already initialized CCC user data.
This allows the application to specify the storage location of the CCC
user data.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-19 14:08:12 +02:00
Trond Einar Snekvik b7d05fbf13 Bluetooth: Mesh: Add model reset callback
Adds additional model callback that gets called on node_reset. Will also
erase any user data when this happens.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-16 22:56:22 +03:00
Trond Einar Snekvik 91ca7ca763 Bluetooth: Mesh: Add persistent model user data
Adds user data file in the model settings, allowing each model instance
to store some application specific data without having to reinvent the
settings path encoding for model element/ID combinations. Exposes the
settings_handler interface in the model callback structure and adds a
data store function.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-16 22:56:22 +03:00
Trond Einar Snekvik c2c05c916a Bluetooth: Mesh: Add model callback structure
Adds a structure of callbacks for each model instance. This allows for
more flexible model implementations, that can interact with the Mesh
stack without going through the application.

For now, only an init callback is added, replacing the init mechanism in
the foundation models. The init callback does not provide the primary
flag that used to be in the foundation model callbacks, but replaces
this with an inline function in access.h.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-16 22:56:22 +03:00
Trond Einar Snekvik 532241d5ad Bluetooth: Mesh: Encrypt friend packets on send
Stores friend queue packets unencrypted, removing any out-of-order
issues caused by seqnum allocation. Also moves as much of the metadata
storage as possible into the packet, allowing us to free up some bytes
of net_buf user data for friend packets.

Fixes #18488

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-16 14:00:57 +03:00
Trond Einar Snekvik 9e85c33239 Bluetooth: Mesh: Expose SeqZero mask
Makes a define for the seqzero 13-bit mask in transport, and exposes it
in the header for use in the friend module.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-16 14:00:57 +03:00
Johan Hedberg c333e8e8a4 Bluetooth: Mesh: Fix matching for "All Proxies" group address
The bt_mesh_fixed_group_match() function is intended to match the
various well-known group addresses, however it was never updated when
Proxy support was added.

Fixes #19015

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-09-09 13:03:47 +03:00
Trond Einar Snekvik 110332cad9 Bluetooth: Mesh: Make unicast elem lookup O(1)
As element addresses are sequential, there's no need for iterating
through the elements to find the one matching a unicast address.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-09 11:14:30 +03:00
Trond Einar Snekvik b2463a7126 Bluetooth: Mesh: Move model_find to public header
Allows models to find each other without direct access to the
composition data.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-07 15:51:25 +03:00
Trond Einar Snekvik 9a3ff5cdff Bluetooth: Mesh: Add prov input_complete to shell
Notifies the user that the callback fired.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-07 15:51:09 +03:00
Trond Einar Snekvik c91d0646a6 Bluetooth: Mesh: Add prov input_complete cb
Allows the user to pass a provisioning input complete callback to the
provisioning module, letting the application stop displaying its output
OOB value when the other party finishes their OOB input.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-09-07 15:51:09 +03:00
Johan Hedberg 71d00c1bf8 Bluetooth: Mesh: Fix publication period timestamp initialization
The period_start timestamp wasn't getting properly initialized in the
case that retransmission was not being used. In the case of
retransmission the timestamp was getting updated in the mod_publish()
delayed work callback. Add a send_start callback and do the
initialization there, since this covers both the retransmission as
well as the no-retransmission cases.

Fixes #17820

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-22 17:28:19 +03:00
Luiz Augusto von Dentz 5f3595e47c Bluetooth: GATT: Fix using variable size storage for CCC
This removes the necessity of registering the storage for CCC and make
it part of the declaration itself.

Fixes #18547

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-22 15:14:39 +03:00
Tobias Svehagen 9f2d364414 Bluetooth: Mesh: Remove dependency on BT_MESH_IV_UPDATE_TEST in shell
Only enable the IV update commands when CONFIG_BT_MESH_IV_UPDATE_TEST
is defined.

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2019-08-22 13:13:29 +02:00
Johan Hedberg 6de5b79f9c Bluetooth: Mesh: Fix discarding messages with many segments
The code for checking space in the Friend queue was faulty in the case
that we receive a message with more segments than the configured Friend
Queue size. This is not an issue for the default configuration but
still a possible one. Move the check for exceeding Friend Queue Size
to the per-LPN function, so that bt_mesh_friend_queue_has_space()
iterates all LPNs before delivering its verdict. This allows us to
return success in case no LPN matched (which is how the code was
intended to work).

Fixes #18522

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-20 21:24:09 +03:00
Trond Einar Snekvik 7d472c3c38 Bluetooth: Mesh: Use define for no prov pdu
Adds a prov PDU type define for cases when no PDU is expected.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-08-19 18:47:41 +03:00
Trond Einar Snekvik d408f4afe5 Bluetooth: Mesh: Prov confirm tx after confirm rx
According to Mesh Profile Specification 1.0.1 Figure 5.17, the
unprovisioned device should send its confirmation value after the
provisioner sends theirs. Previously, the confirmation value would be
sent immediately after OOB input complete. Now it first waits for the
input data, then from confirmation from the provisioner before sending
the confirmation.

Fixes: #18178.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-08-19 18:47:41 +03:00
Johan Hedberg 8aba96b772 Bluetooth: Mesh: Fix adhering to the configured Friend Queue size
Qualification test case MESH/NODE/FRND/FN/BV-08-C requires that we do
not store more messages than the reported Friend Queue size. The
implementation was so far opportunistic and stored more if it could
(it would later discard if necessary to make sure all queues can store
the required amount). The spec also requires the queues to have new
messages overwrite old ones (in the style of a circular buffer), so we
have to keep track of which buffers are part of the same segmented
message (so we discard all buffers belonging to the same message).

To pass the test case, add APIs to check for space in the Friend
queue, and track the number of buffers for each incoming segmented
message.

Fixes #18090

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-19 16:30:31 +03:00
Johan Hedberg e613b6e61b Bluetooth: Mesh: Remove unused code
The bt_mesh_trans_resend() function had no users, and had in fact not
even a prototype in a header file. Just remove it.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-19 16:30:31 +03:00
Johan Hedberg 81effc053b Bluetooth: Mesh: Fix missing call to send_cb_finalize
When sent solely to the Friend Queue the send callbacks were not
getting called for unsegmented messages.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-19 16:30:31 +03:00
Rich Barlow 5d5706e785 Bluetooth: Mesh: LPN: Remove msg from cache on rejection
When in Low Power mode an LPN may receive messages sent by nodes other
than its friend during its brief receive window. These messages get
rejected by the transport layer. At some point in the future the LPN
will receive these messages from its friend, however they will have
already been added to the network message cache earlier.

When the transport layer rejects a message due to it being received from
a non-friend node while in Low Power mode it must be removed from the
network message cache so that it can be correctly received from its
friend in the future.

Fixes #17809

Signed-off-by: Rich Barlow <rich@bennellick.com>
2019-08-15 13:24:17 +02:00
Johan Hedberg 286a59c8b5 Bluetooth: Mesh: Fix reference count imbalance in bt_mesh_net_resend()
The commit 1c7b668804 tried to fix
resending segments to the GATT bearer, however it got the buffer
refernce counting wrong. The bt_mesh_net_resend() function doesn't
own a reference to the buffer, i.e. it's not responsible for unrefing
it. E.g. bt_mesh_adv_send() takes its own reference.

What was missing however was the handling of the callbacks. Use the
recently introduced send_cb_finalize() helper to make sure they're
called.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-15 11:53:10 +02:00
Johan Hedberg 104d30c989 Bluetooth: Mesh: Introduce a helper for send callback finalization
There are several places that require the send callbacks to be called
immediately. Reduce the code a bit by introducing a helper for this.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-15 11:53:10 +02:00
Johan Hedberg 59ba9a1156 Bluetooth: Mesh: Fix starting IV Update when not on primary subnet
The Mesh Profile Specification states in section 3.10.5 IV Update
procedure: "The IV Update procedure is initiated by any node that is a
member of a primary subnet", meaning if we're not on the primary
subnet we should not try to initiate the procedure.

Additionally this fixes initiating IV Update in all cases where the
sequence is updated, by putting the code into bt_mesh_next_seq().

Fixes #17977

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 16:27:22 +02:00
Johan Hedberg a43ef332ec Bluetooth: Mesh: Fix canceling publication retransmission timer
This branch is inherited from old code that used to start the timer as
soon as a message was queued for transmission rather than when it was
actually transmitted (the case today). It'll also cause a race in case
the publication goes over the GATT layer since the "sent" callback
happens synchronously in that case.

Fixes #17821

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:56:26 +02:00
Johan Hedberg 1c7b668804 Bluetooth: Mesh: Fix resending segments on correct bearer
The GATT bearer is a reliable one so there should theoretically never
be a need to resend segments. If however for some strange reason the
proxy client doesn't immediately ack all segments we should do the
resending on the GATT bearer, rather than sending them over
advertising.

Fixes #17907

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:50:36 +02:00
Johan Hedberg 680d778f57 Bluetooth: Mesh: Fix not sending all segments through the Friend Queue
The code was incorrectly bailing out with "return 0" rather than
continue. Also, it was incorrectly making a reference to
tx->seg[seg_o] since when a PDU goes through the friend queue we don't
use the usual retransmission mechanism.

Fixes: #17932

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:49:43 +02:00
Johan Hedberg 5f6043bc35 Bluetooth: Mesh: Fix canceled buffer memory leak
In case a queued buffer is canceled before sending we have to unref
it, since that's what adv_send() would do as well.

Fixes #17936
Fixes #18013

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:49:43 +02:00
Johan Hedberg 94fcb211de Bluetooth: Mesh: Fix heartbeat sending on Friendship established/lost
Mesh Profile 3.6.7.2 Publishing Heartbeat messages:

"
Triggered publishing of Heartbeat messages is enabled by
the Heartbeat Publication Features state (see Section 4.2.17.5):
...
- If the Low Power bit is set to 1, a Heartbeat message shall be
 published when the node establishes or loses Friendship (see Section
 3.6.6.1).
"

Fixes #18194

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:48:37 +02:00
Johan Hedberg 39edab1013 Bluetooth: Mesh: Fix checking for active heartbeat publication
When sending heartbeat messages triggered by feature changes the code
was trying to look up the configured publication subnet, in an
apparent attempt to figure out if publication is enabled or not. A
more appropriate way is to check for the heartbeat publication
destination address, and since we have a helper function this can be
done in a single place.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:48:37 +02:00
Johan Hedberg ed71fc9a9e Bluetooth: Mesh: Move heartbeat sending to transport layer
The heartbeat is a transport layer feature, so move it to transport.c.
This also opens the way to properly fix Friendship-established/lost
triggering for LPN role.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-14 14:48:37 +02:00
Mariusz Skamra 83b50f6bee Bluetooth: Mesh: Fix rejecting invalid remote public key
The code was already rejecting the key, however that rejection
happened only after we had already sent our public key as response,
which got interpreted as acceptance by the tester (PTS).

This fixes issue that d4fd267086a56c270a793114e7575afae9a9befa
supposed to fix. The problem is bt_dh_key_gen is async.
Local public key cannot be sent from the same context
the bt_dh_key_gen is called because we don't know yet
if remote key is valid.

Fixes MESH/NODE/PROV/BI-13-C.

Signed-off-by: Mariusz Skamra <mariusz.skamra@codecoup.pl>
2019-08-07 17:13:32 +02:00
Joakim Andersson 7a93e948a9 kernel: lib: Add convert functions for hex strings and binary arrays
Move duplicate hex2bin and add bin2hex function so that application can
use the functions and avoid code duplication.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-16 12:44:18 +02:00
Johan Hedberg 408703e8dd Bluetooth: Mesh: Move under subsys/bluetooth/mesh
We've already got GATT services in subsys/bluetooth/services so
subsys/bluetooth/mesh is a more natural place. Aditionally this aims
to fix the Kconfig dependencies to be able to use mesh together with
BT_CUSTOM (i.e. a custom, potentially non-HCI, host stack).

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-07-10 09:41:57 +03:00