Skip to content

samples: bluetooth: silabs: Bluetooth blinky#142

Open
silabs-vifisch wants to merge 2 commits into
SiliconLabsSoftware:mainfrom
silabs-vifisch:bt_blinky
Open

samples: bluetooth: silabs: Bluetooth blinky#142
silabs-vifisch wants to merge 2 commits into
SiliconLabsSoftware:mainfrom
silabs-vifisch:bt_blinky

Conversation

@silabs-vifisch

@silabs-vifisch silabs-vifisch commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Similar to Silabs' SOC Blinky sample, implements two GATT characteristics.
One characteristic controls the state of the LED via write operations from
a GATT client. Second characteristic sends notifications to subscribed
clients when the button state changes on the board.
Minor changes have been made in README.rst, samples.yaml CMakeList.txt
Some coding style and global variable issues have been fixed.

@jhedberg

Copy link
Copy Markdown
Contributor

silabs-vifisch could you please fix the commit message so it makese sense. Now it seems to describe some small detail between two version of the commit, which is irrelevant once this gets merged. You can find extensive guidelines on writing good commit messages here: https://docs.zephyrproject.org/latest/contribute/guidelines.html#commit-message-guidelines

#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/services/bas.h>

static struct bt_conn *current_conn;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid global variable (static const are accepted).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connection object comes from a call-back function that needs to be from project wide it is more convenient to use a global variable current_conn. This is the reason I kept it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bt_conn is an argument of the relevant callbacks (I believe you can use BT_GATT_CCC_MANAGED() instead of BT_GATT_CCC()).

At the end, I believe you won't need this variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bt_conn is used in two custom callback where the system doesn't give it back as an argument.
I'm afraid BT_GATT_CCC_MANAGED() won't help.

Comment thread samples/bt_blinky/src/main.c Outdated
@@ -0,0 +1,447 @@
/* main.c - Application main entry point */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of a such trivial comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed! Actually it was originally in the base project.

Comment thread samples/bt_blinky/sample.yaml Outdated
- qemu_cortex_m3
tags:
- bluetooth
- LED

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowercase

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected!

Comment thread samples/bt_blinky/sample.yaml Outdated
@@ -0,0 +1,14 @@
sample:
name: Bluetooth Blinky
description: Similar to Silabs SOC Blinky

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a true description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment thread samples/bt_blinky/sample.yaml Outdated
harness: bluetooth
integration_platforms:
- frdm_k64f
- qemu_cortex_m3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense?

@silabs-vifisch silabs-vifisch Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected!

Comment thread samples/bt_blinky/src/main.c Outdated
{
if (state > 0) {
led_value = 1;
led_report_value = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you replace these variable by gpio_pin_get() (single source of trust)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned the are GATT entries they must hold valid states.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get you. What prevent you to read the actual state in the callbacks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As led_value is a GATT entry being single source of truth not the led.pin.

Comment thread samples/bt_blinky/src/main.c Outdated
printk("failed.\n");
return -EIO;
}
printk("done.\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not log success (you can log errors).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected!

Comment thread samples/bt_blinky/src/main.c Outdated
uint32_t pins)
{
/* Simple: toggle LED using current state */
set_led_state(!led_value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpio_pin_toggle_dt()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually here the led state (in the GATT table) is set.

Comment thread samples/bt_blinky/src/main.c

/* BT_LE_ADV_CONN_FAST_1 */

#if !defined(CONFIG_BT_EXT_ADV)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider use of if (IS_ENABLED(CONFIG_BT_EXT_ADV)). I believe it should compile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls. see comment above!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right upper comment, this is inherited from the previous project, it is not used here, so maybe it can be completely removed.

Comment thread samples/bt_blinky/src/main.c Outdated
BT_GATT_SERVICE_DEFINE(led_svc,
BT_GATT_PRIMARY_SERVICE(&led_service_uuid),
BT_GATT_CHARACTERISTIC(&led_char_uuid.uuid,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still no correct. BT_GATT_CHRC_READ should be aligned with the opening parenthesis of BT_GATT_CHARACTERISTIC(:

BT_GATT_SERVICE_DEFINE(led_svc,
       BT_GATT_PRIMARY_SERVICE(&led_service_uuid),
       BT_GATT_CHARACTERISTIC(&led_char_uuid.uuid,
			      BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
			      BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
			      btapp_read_led,
			      btapp_write_led,
			      &led_value),
       BT_GATT_CHARACTERISTIC(&led_report_char_uuid.uuid,
			      BT_GATT_CHRC_NOTIFY,
			      BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
			      NULL,
			      NULL,
			      &led_report_value),
       BT_GATT_CCC(btapp_led_report_ccc_cfg_changed,
		   BT_GATT_PERM_READ | BT_GATT_PERM_WRITE)
);

(Note it makes sense to have an exception on this rule for the arguments of BT_GATT_SERVICE_DEFINE())

Don't forgot to apply this rule to the other functions calls/declarations.

Comment thread samples/bt_blinky/src/main.c Outdated
};

static uint8_t led_value = 1;
static uint8_t led_report_value = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it is difficult to track these variables (I have to admit after 10min I have still not understand what they do).

I believe You can simply get rid of led_value and led_report_value and only rely on gpio_pin_get(led.port, led.pin) and gpio_pin_set(led.port, led.pin, x) (so you will also comply with the rule of single source of trust).

Comment thread samples/bt_blinky/src/main.c Outdated
printk("Connected\n");

current_conn = conn;
(void)atomic_set_bit(state, STATE_CONNECTED);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be more states, and it is used project-wide that is why I kept it in its global form.

For now, you don't need it (YAGNI rule).

#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/services/bas.h>

static struct bt_conn *current_conn;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bt_conn is an argument of the relevant callbacks (I believe you can use BT_GATT_CCC_MANAGED() instead of BT_GATT_CCC()).

At the end, I believe you won't need this variable.

Comment thread samples/bt_blinky/src/main.c Outdated

#if defined(CONFIG_GPIO)

#if DT_NODE_HAS_STATUS_OKAY(LED0_NODE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to be able to compile "blinky" without a led?

Comment thread samples/bt_blinky/src/main.c Outdated

/* Implement notification. */
while (1) {
k_sleep(K_SECONDS(1));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RTOS have been designed to avoid such polling loops (not realtime efficient, not scalable, not power efficient). Even if it not a big deal for this example, I know the customer tends to reuse the samples for their real products.

Maybe you could manage the event directly in the callbacks. If you can't, wake up the main loop using events or semaphore (see zpoll()).

The final customer will rely on an rely on the hardware (the ADC I assume) to get the change on the battery level. You can get something close with k_timer_start().

printk("Successfully Connected\n");

} else if (atomic_test_and_clear_bit(state, STATE_DISCONNECTED)) {
#if !defined(CONFIG_BT_EXT_ADV)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you replace this #ifdef with if (IS_ENABLED(CONFIG_BT_EXT_ADV)) {?


/* BT_LE_ADV_CONN_FAST_1 */

#if !defined(CONFIG_BT_EXT_ADV)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which comment?

Comment thread samples/bt_blinky/src/main.c Outdated
{
if (state > 0) {
led_value = 1;
led_report_value = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get you. What prevent you to read the actual state in the callbacks?

Similar to Silabs' SOC Blinky sample, implements two GATT characteristics.
One characteristic controls the state of the LED via write operations from
a GATT client. Second characteristic sends notifications to subscribed
clients when the button state changes on the board.
Minor changes have been made in README.rst, samples.yaml CMakeList.txt
Some coding style and global variable issues have been fixed.

Signed-off-by: Viktor Fisch <viktor.fisch@silabs.com>
@silabs-vifisch silabs-vifisch force-pushed the bt_blinky branch 2 times, most recently from e814648 to 1d3debe Compare July 7, 2026 10:15
 Major changes in main.c includes introduction of signal usage
 instead of state bits and battery level notification is moved to
 a scheduled task.

Signed-off-by: Viktor Fisch <viktor.fisch@silabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants