Skip to content

Commit e1656ee

Browse files
trond-snekvikjhedberg
authored andcommitted
Bluetooth: Mesh: Add some actual behavior in sample
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]>
1 parent 869978a commit e1656ee

File tree

9 files changed

+493
-104
lines changed

9 files changed

+493
-104
lines changed

samples/bluetooth/mesh/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1111
project(mesh)
1212

1313
target_sources(app PRIVATE src/main.c)
14-
target_sources_ifdef(CONFIG_BOARD_BBC_MICROBIT app PRIVATE src/microbit.c)
14+
15+
if (BOARD STREQUAL bbc_microbit)
16+
target_sources(app PRIVATE src/microbit.c)
17+
else()
18+
target_sources(app PRIVATE src/board.c)
19+
endif()

samples/bluetooth/mesh/README.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Advertising and the GATT Provisioning Bearers (i.e. PB-ADV and PB-GATT).
1212
The application also needs a functioning serial console, since that's
1313
used for the Out-of-Band provisioning procedure.
1414

15+
On boards with LEDs, a Generic OnOff Server model exposes functionality for
16+
controlling the first LED on the board over the mesh.
17+
18+
On boards with buttons, a Generic OnOff Client model will send Onoff messages
19+
to all nodes in the network when the button is pressed.
20+
1521
Requirements
1622
************
1723

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

3844
Refer to your :ref:`board's documentation <boards>` for alternative
3945
flash instructions if your board doesn't support the ``flash`` target.
46+
47+
Interacting with the sample
48+
***************************
49+
50+
The sample can either be provisioned into an existing mesh network with an
51+
external provisioner device, or self-provision through a button press.
52+
53+
When provisioning with a provisioner device, the provisioner must give the
54+
device an Application key and bind it to both Generic OnOff models.
55+
56+
When self-provisisoning, the device will take a random unicast address and
57+
bind a dummy Application key to these models.
58+
59+
Once provisioned, messages to the Generic OnOff Server will be used to turn
60+
the LED on or off, and button presses will be used to broadcast OnOff
61+
messages to all nodes in the same network.

samples/bluetooth/mesh/nrf51_qfaa.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CONFIG_INIT_STACKS=y
22
CONFIG_ISR_STACK_SIZE=512
33
CONFIG_MAIN_STACK_SIZE=512
4+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024
45

56
CONFIG_BT=y
67
CONFIG_BT_TINYCRYPT_ECC=y

samples/bluetooth/mesh/prj.conf

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CONFIG_FLASH=y
55
CONFIG_FLASH_MAP=y
66
CONFIG_NVS=y
77
CONFIG_SETTINGS=y
8+
CONFIG_HWINFO=y
89

910
CONFIG_BT_OBSERVER=y
1011
CONFIG_BT_CTLR_DUP_FILTER_LEN=0
@@ -44,15 +45,4 @@ CONFIG_BT_MESH_LABEL_COUNT=3
4445
CONFIG_BT_DEBUG_LOG=y
4546

4647
CONFIG_BT_MESH_DEBUG=y
47-
#CONFIG_BT_MESH_DEBUG_PROV=y
48-
#CONFIG_BT_MESH_DEBUG_PROXY=y
49-
#CONFIG_BT_MESH_DEBUG_BEACON=y
50-
CONFIG_BT_MESH_DEBUG_NET=y
51-
CONFIG_BT_MESH_DEBUG_TRANS=y
52-
CONFIG_BT_MESH_DEBUG_SETTINGS=y
53-
#CONFIG_BT_MESH_DEBUG_LOW_POWER=y
54-
CONFIG_BT_MESH_DEBUG_FRIEND=y
55-
#CONFIG_BT_MESH_DEBUG_MODEL=y
56-
#CONFIG_BT_MESH_DEBUG_ACCESS=y
57-
#CONFIG_BT_MESH_DEBUG_CRYPTO=y
58-
#CONFIG_BT_MESH_DEBUG_ADV=y
48+
CONFIG_BT_MESH_DEBUG_MODEL=y

samples/bluetooth/mesh/prj_bbc_microbit.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CONFIG_ISR_STACK_SIZE=768
33
CONFIG_MAIN_STACK_SIZE=512
44
CONFIG_DISPLAY=y
55
CONFIG_MICROBIT_DISPLAY=y
6-
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1500
6+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024
77
CONFIG_GPIO=y
88

99
CONFIG_BT_SETTINGS=y

samples/bluetooth/mesh/src/board.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* board.c - Generic HW interaction hooks */
2+
3+
/*
4+
* Copyright (c) 2021 Nordic Semiconductor
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <bluetooth/mesh.h>
10+
#include <drivers/gpio.h>
11+
#include "board.h"
12+
13+
/* Locate led0 as alias or label by that name */
14+
#if DT_NODE_EXISTS(DT_ALIAS(led0))
15+
#define LED0 DT_ALIAS(led0)
16+
#elif DT_NODE_EXISTS(DT_NODELABEL(led0))
17+
#define LED0 DT_NODELABEL(led0)
18+
#else
19+
#define LED0 DT_INVALID_NODE
20+
#endif
21+
22+
/* Locate button0 as alias or label by sw0 or button0 */
23+
#if DT_NODE_EXISTS(DT_ALIAS(sw0))
24+
#define BUTTON0 DT_ALIAS(sw0)
25+
#elif DT_NODE_EXISTS(DT_ALIAS(button0))
26+
#define BUTTON0 DT_ALIAS(button0)
27+
#elif DT_NODE_EXISTS(DT_NODELABEL(sw0))
28+
#define BUTTON0 DT_NODELABEL(sw0)
29+
#elif DT_NODE_EXISTS(DT_NODELABEL(button0))
30+
#define BUTTON0 DT_NODELABEL(button0)
31+
#else
32+
#define BUTTON0 DT_INVALID_NODE
33+
#endif
34+
35+
#if DT_NODE_EXISTS(LED0)
36+
#define LED0_DEV DT_PHANDLE(LED0, gpios)
37+
#define LED0_PIN DT_PHA(LED0, gpios, pin)
38+
#define LED0_FLAGS DT_PHA(LED0, gpios, flags)
39+
40+
static const struct device *led_dev = DEVICE_DT_GET(LED0_DEV);
41+
#endif /* LED0 */
42+
43+
#if DT_NODE_EXISTS(BUTTON0)
44+
#define BUTTON0_DEV DT_PHANDLE(BUTTON0, gpios)
45+
#define BUTTON0_PIN DT_PHA(BUTTON0, gpios, pin)
46+
#define BUTTON0_FLAGS DT_PHA(BUTTON0, gpios, flags)
47+
48+
static const struct device *button_dev = DEVICE_DT_GET(BUTTON0_DEV);
49+
static struct k_work *button_work;
50+
51+
static void button_cb(const struct device *port, struct gpio_callback *cb,
52+
gpio_port_pins_t pins)
53+
{
54+
k_work_submit(button_work);
55+
}
56+
#endif /* BUTTON0 */
57+
58+
static int led_init(void)
59+
{
60+
#if DT_NODE_EXISTS(LED0)
61+
int err;
62+
63+
if (!device_is_ready(led_dev)) {
64+
return -ENODEV;
65+
}
66+
67+
err = gpio_pin_configure(led_dev, LED0_PIN,
68+
LED0_FLAGS | GPIO_OUTPUT_INACTIVE);
69+
if (err) {
70+
return err;
71+
}
72+
#else
73+
printk("WARNING: LEDs not supported on this board.\n");
74+
#endif
75+
76+
return 0;
77+
}
78+
79+
static int button_init(struct k_work *button_pressed)
80+
{
81+
#if DT_NODE_EXISTS(BUTTON0)
82+
int err;
83+
84+
err = gpio_pin_configure(button_dev, BUTTON0_PIN,
85+
BUTTON0_FLAGS | GPIO_INPUT);
86+
if (err) {
87+
return err;
88+
}
89+
90+
static struct gpio_callback gpio_cb;
91+
92+
err = gpio_pin_interrupt_configure(button_dev, BUTTON0_PIN,
93+
GPIO_INT_EDGE_TO_ACTIVE);
94+
if (err) {
95+
return err;
96+
}
97+
98+
button_work = button_pressed;
99+
100+
gpio_init_callback(&gpio_cb, button_cb, BIT(BUTTON0_PIN));
101+
gpio_add_callback(button_dev, &gpio_cb);
102+
#else
103+
printk("WARNING: Buttons not supported on this board.\n");
104+
#endif
105+
106+
return 0;
107+
}
108+
109+
int board_init(struct k_work *button_pressed)
110+
{
111+
int err;
112+
113+
err = led_init();
114+
if (err) {
115+
return err;
116+
}
117+
118+
return button_init(button_pressed);
119+
}
120+
121+
void board_led_set(bool val)
122+
{
123+
#if DT_NODE_EXISTS(LED0)
124+
gpio_pin_set(led_dev, LED0_PIN, val);
125+
#endif
126+
}
127+
128+
void board_output_number(bt_mesh_output_action_t action, uint32_t number)
129+
{
130+
}
131+
132+
void board_prov_complete(void)
133+
{
134+
}

samples/bluetooth/mesh/src/board.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,10 @@
66
* SPDX-License-Identifier: Apache-2.0
77
*/
88

9-
#if defined(CONFIG_BOARD_BBC_MICROBIT)
109
void board_output_number(bt_mesh_output_action_t action, uint32_t number);
1110

1211
void board_prov_complete(void);
1312

14-
void board_init(void);
15-
#else
16-
static inline void board_output_number(bt_mesh_output_action_t action,
17-
uint32_t number)
18-
{
19-
}
13+
int board_init(struct k_work *button_work);
2014

21-
static inline void board_prov_complete(void)
22-
{
23-
}
24-
25-
static inline void board_init(void)
26-
{
27-
}
28-
#endif
15+
void board_led_set(bool val);

0 commit comments

Comments
 (0)