Skip to content

Commit

Permalink
Bluetooth: Mesh: Add some actual behavior in sample
Browse files Browse the repository at this point in the history
Populates the onoff server stubs in the Bluetooth Mesh sample, and
implements generic LED based hardware support to include more boards.

Fixes #31031.

Signed-off-by: Trond Einar Snekvik <[email protected]>
  • Loading branch information
trond-snekvik committed Mar 26, 2021
1 parent b813616 commit 1a14bc8
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 104 deletions.
7 changes: 6 additions & 1 deletion samples/bluetooth/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mesh)

target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_BOARD_BBC_MICROBIT app PRIVATE src/microbit.c)

if (BOARD STREQUAL bbc_microbit)
target_sources(app PRIVATE src/microbit.c)
else()
target_sources(app PRIVATE src/board.c)
endif()
22 changes: 22 additions & 0 deletions samples/bluetooth/mesh/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Advertising and the GATT Provisioning Bearers (i.e. PB-ADV and PB-GATT).
The application also needs a functioning serial console, since that's
used for the Out-of-Band provisioning procedure.

On boards with LEDs, a Generic OnOff Server model exposes functionality for
controlling the first LED on the board over the mesh.

On boards with buttons, a Generic OnOff Client model will send Onoff messages
to all nodes in the network when the button is pressed.

Requirements
************

Expand All @@ -37,3 +43,19 @@ For other boards, build and flash the application as follows:

Refer to your :ref:`board's documentation <boards>` for alternative
flash instructions if your board doesn't support the ``flash`` target.

Interacting with the sample
***************************

The sample can either be provisioned into an existing mesh network with an
external provisioner device, or self-provision through a button press.

When provisioning with a provisioner device, the provisioner must give the
device an Application key and bind it to both Generic OnOff models.

When self-provisisoning, the device will take a random unicast address and
bind a dummy Application key to these models.

Once provisioned, messages to the Generic OnOff Server will be used to turn
the LED on or off, and button presses will be used to broadcast OnOff
messages to all nodes in the same network.
1 change: 1 addition & 0 deletions samples/bluetooth/mesh/nrf51_qfaa.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CONFIG_INIT_STACKS=y
CONFIG_ISR_STACK_SIZE=512
CONFIG_MAIN_STACK_SIZE=512
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024

CONFIG_BT=y
CONFIG_BT_TINYCRYPT_ECC=y
Expand Down
14 changes: 2 additions & 12 deletions samples/bluetooth/mesh/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_HWINFO=y

CONFIG_BT_OBSERVER=y
CONFIG_BT_CTLR_DUP_FILTER_LEN=0
Expand Down Expand Up @@ -44,15 +45,4 @@ CONFIG_BT_MESH_LABEL_COUNT=3
CONFIG_BT_DEBUG_LOG=y

CONFIG_BT_MESH_DEBUG=y
#CONFIG_BT_MESH_DEBUG_PROV=y
#CONFIG_BT_MESH_DEBUG_PROXY=y
#CONFIG_BT_MESH_DEBUG_BEACON=y
CONFIG_BT_MESH_DEBUG_NET=y
CONFIG_BT_MESH_DEBUG_TRANS=y
CONFIG_BT_MESH_DEBUG_SETTINGS=y
#CONFIG_BT_MESH_DEBUG_LOW_POWER=y
CONFIG_BT_MESH_DEBUG_FRIEND=y
#CONFIG_BT_MESH_DEBUG_MODEL=y
#CONFIG_BT_MESH_DEBUG_ACCESS=y
#CONFIG_BT_MESH_DEBUG_CRYPTO=y
#CONFIG_BT_MESH_DEBUG_ADV=y
CONFIG_BT_MESH_DEBUG_MODEL=y
2 changes: 1 addition & 1 deletion samples/bluetooth/mesh/prj_bbc_microbit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CONFIG_ISR_STACK_SIZE=768
CONFIG_MAIN_STACK_SIZE=512
CONFIG_DISPLAY=y
CONFIG_MICROBIT_DISPLAY=y
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1500
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024
CONFIG_GPIO=y

CONFIG_BT_SETTINGS=y
Expand Down
134 changes: 134 additions & 0 deletions samples/bluetooth/mesh/src/board.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* board.c - Generic HW interaction hooks */

/*
* Copyright (c) 2021 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <bluetooth/mesh.h>
#include <drivers/gpio.h>
#include "board.h"

/* Locate led0 as alias or label by that name */
#if DT_NODE_EXISTS(DT_ALIAS(led0))
#define LED0 DT_ALIAS(led0)
#elif DT_NODE_EXISTS(DT_NODELABEL(led0))
#define LED0 DT_NODELABEL(led0)
#else
#define LED0 DT_INVALID_NODE
#endif

/* Locate button0 as alias or label by sw0 or button0 */
#if DT_NODE_EXISTS(DT_ALIAS(sw0))
#define BUTTON0 DT_ALIAS(sw0)
#elif DT_NODE_EXISTS(DT_ALIAS(button0))
#define BUTTON0 DT_ALIAS(button0)
#elif DT_NODE_EXISTS(DT_NODELABEL(sw0))
#define BUTTON0 DT_NODELABEL(sw0)
#elif DT_NODE_EXISTS(DT_NODELABEL(button0))
#define BUTTON0 DT_NODELABEL(button0)
#else
#define BUTTON0 DT_INVALID_NODE
#endif

#if DT_NODE_EXISTS(LED0)
#define LED0_DEV DT_PHANDLE(LED0, gpios)
#define LED0_PIN DT_PHA(LED0, gpios, pin)
#define LED0_FLAGS DT_PHA(LED0, gpios, flags)

static const struct device *led_dev = DEVICE_DT_GET(LED0_DEV);
#endif /* LED0 */

#if DT_NODE_EXISTS(BUTTON0)
#define BUTTON0_DEV DT_PHANDLE(BUTTON0, gpios)
#define BUTTON0_PIN DT_PHA(BUTTON0, gpios, pin)
#define BUTTON0_FLAGS DT_PHA(BUTTON0, gpios, flags)

static const struct device *button_dev = DEVICE_DT_GET(BUTTON0_DEV);
static struct k_work *button_work;

static void button_cb(const struct device *port, struct gpio_callback *cb,
gpio_port_pins_t pins)
{
k_work_submit(button_work);
}
#endif /* BUTTON0 */

static int led_init(void)
{
#if DT_NODE_EXISTS(LED0)
int err;

if (!device_is_ready(led_dev)) {
return -ENODEV;
}

err = gpio_pin_configure(led_dev, LED0_PIN,
LED0_FLAGS | GPIO_OUTPUT_INACTIVE);
if (err) {
return err;
}
#else
printk("WARNING: LEDs not supported on this board.\n");
#endif

return 0;
}

static int button_init(struct k_work *button_pressed)
{
#if DT_NODE_EXISTS(BUTTON0)
int err;

err = gpio_pin_configure(button_dev, BUTTON0_PIN,
BUTTON0_FLAGS | GPIO_INPUT);
if (err) {
return err;
}

static struct gpio_callback gpio_cb;

err = gpio_pin_interrupt_configure(button_dev, BUTTON0_PIN,
GPIO_INT_EDGE_TO_ACTIVE);
if (err) {
return err;
}

button_work = button_pressed;

gpio_init_callback(&gpio_cb, button_cb, BIT(BUTTON0_PIN));
gpio_add_callback(button_dev, &gpio_cb);
#else
printk("WARNING: Buttons not supported on this board.\n");
#endif

return 0;
}

int board_init(struct k_work *button_pressed)
{
int err;

err = led_init();
if (err) {
return err;
}

return button_init(button_pressed);
}

void board_led_set(bool val)
{
#if DT_NODE_EXISTS(LED0)
gpio_pin_set(led_dev, LED0_PIN, val);
#endif
}

void board_output_number(bt_mesh_output_action_t action, uint32_t number)
{
}

void board_prov_complete(void)
{
}
17 changes: 2 additions & 15 deletions samples/bluetooth/mesh/src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

#if defined(CONFIG_BOARD_BBC_MICROBIT)
void board_output_number(bt_mesh_output_action_t action, uint32_t number);

void board_prov_complete(void);

void board_init(void);
#else
static inline void board_output_number(bt_mesh_output_action_t action,
uint32_t number)
{
}
int board_init(struct k_work *button_work);

static inline void board_prov_complete(void)
{
}

static inline void board_init(void)
{
}
#endif
void board_led_set(bool val);
Loading

0 comments on commit 1a14bc8

Please sign in to comment.