samples: bluetooth: silabs: Bluetooth blinky#142
Conversation
132b991 to
8d87754
Compare
|
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 |
8d87754 to
a92b59a
Compare
| #include <zephyr/bluetooth/gatt.h> | ||
| #include <zephyr/bluetooth/services/bas.h> | ||
|
|
||
| static struct bt_conn *current_conn; |
There was a problem hiding this comment.
Avoid global variable (static const are accepted).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| @@ -0,0 +1,447 @@ | |||
| /* main.c - Application main entry point */ | |||
There was a problem hiding this comment.
What is the purpose of a such trivial comment?
There was a problem hiding this comment.
Removed! Actually it was originally in the base project.
| - qemu_cortex_m3 | ||
| tags: | ||
| - bluetooth | ||
| - LED |
There was a problem hiding this comment.
lowercase
| @@ -0,0 +1,14 @@ | |||
| sample: | |||
| name: Bluetooth Blinky | |||
| description: Similar to Silabs SOC Blinky | |||
There was a problem hiding this comment.
Please provide a true description.
| harness: bluetooth | ||
| integration_platforms: | ||
| - frdm_k64f | ||
| - qemu_cortex_m3 |
There was a problem hiding this comment.
Does it make sense?
| { | ||
| if (state > 0) { | ||
| led_value = 1; | ||
| led_report_value = 1; |
There was a problem hiding this comment.
Can't you replace these variable by gpio_pin_get() (single source of trust)?
There was a problem hiding this comment.
As mentioned the are GATT entries they must hold valid states.
There was a problem hiding this comment.
I don't get you. What prevent you to read the actual state in the callbacks?
There was a problem hiding this comment.
As led_value is a GATT entry being single source of truth not the led.pin.
| printk("failed.\n"); | ||
| return -EIO; | ||
| } | ||
| printk("done.\n"); |
There was a problem hiding this comment.
Do not log success (you can log errors).
| uint32_t pins) | ||
| { | ||
| /* Simple: toggle LED using current state */ | ||
| set_led_state(!led_value); |
There was a problem hiding this comment.
gpio_pin_toggle_dt()?
There was a problem hiding this comment.
Actually here the led state (in the GATT table) is set.
|
|
||
| /* BT_LE_ADV_CONN_FAST_1 */ | ||
|
|
||
| #if !defined(CONFIG_BT_EXT_ADV) |
There was a problem hiding this comment.
Consider use of if (IS_ENABLED(CONFIG_BT_EXT_ADV)). I believe it should compile.
There was a problem hiding this comment.
Pls. see comment above!
There was a problem hiding this comment.
which comment?
There was a problem hiding this comment.
Right upper comment, this is inherited from the previous project, it is not used here, so maybe it can be completely removed.
7601499 to
1e5e1b4
Compare
| 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, |
There was a problem hiding this comment.
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.
| }; | ||
|
|
||
| static uint8_t led_value = 1; | ||
| static uint8_t led_report_value = 1; |
There was a problem hiding this comment.
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).
| printk("Connected\n"); | ||
|
|
||
| current_conn = conn; | ||
| (void)atomic_set_bit(state, STATE_CONNECTED); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
|
|
||
| #if defined(CONFIG_GPIO) | ||
|
|
||
| #if DT_NODE_HAS_STATUS_OKAY(LED0_NODE) |
There was a problem hiding this comment.
Does it make sense to be able to compile "blinky" without a led?
|
|
||
| /* Implement notification. */ | ||
| while (1) { | ||
| k_sleep(K_SECONDS(1)); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
which comment?
| { | ||
| if (state > 0) { | ||
| led_value = 1; | ||
| led_report_value = 1; |
There was a problem hiding this comment.
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>
e814648 to
1d3debe
Compare
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>
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.