Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve central_uart sample application #12537

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions samples/bluetooth/central_uart/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright (c) 2023 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

source "Kconfig.zephyr"

menu "Nordic UART Service Client sample"

config BT_NUS_UART_BUFFER_SIZE
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe BT_NUS_C_.... since this is about client side

int "UART payload buffer element size"
default 20
help
Size of the payload buffer in each RX and TX FIFO element

config BT_NUS_SECURITY_ENABLED
bool "Enable security"
default y
select BT_SMP
help
"Enable BLE security for the UART service client"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add dot an every help line end

Suggested change
"Enable BLE security for the UART service client"
Enable BLE security for the UART service client


config BT_NUS_UART_RX_WAIT_TIME
int "Timeout for UART RX complete event"
default 50000
help
Wait for RX complete event time in microseconds

config BT_NUS_WRITE_TIMEOUT
int "Timeout for NUS write complete event"
default 150
help
Wait for NUS write complete event time in milliseconds

endmenu
1 change: 0 additions & 1 deletion samples/bluetooth/central_uart/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ CONFIG_UART_CONSOLE=y
# Enable the BLE stack with GATT Client configuration
CONFIG_BT=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_GATT_CLIENT=y

# Enable the BLE modules from NCS
Expand Down
19 changes: 15 additions & 4 deletions samples/bluetooth/central_uart/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
LOG_MODULE_REGISTER(LOG_MODULE_NAME);

/* UART payload buffer element size. */
#define UART_BUF_SIZE 20
#define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE

#define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
#define KEY_PASSKEY_REJECT DK_BTN2_MSK

#define NUS_WRITE_TIMEOUT K_MSEC(150)
#define NUS_WRITE_TIMEOUT K_MSEC(CONFIG_BT_NUS_WRITE_TIMEOUT)
#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
#define UART_RX_TIMEOUT 50
#define UART_RX_TIMEOUT CONFIG_BT_NUS_UART_RX_WAIT_TIME

static const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart0));
static struct k_work_delayable uart_work;
Expand Down Expand Up @@ -384,12 +384,16 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
LOG_WRN("MTU exchange failed (err %d)", err);
}

#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use here IS_ENABLED macro instead of conditional compilation

Suggested change
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
if(IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED)) {

err = bt_conn_set_security(conn, BT_SECURITY_L2);
if (err) {
LOG_WRN("Failed to set security: %d", err);

gatt_discover(conn);
}
#else
gatt_discover(conn);
#endif

err = bt_scan_stop();
if ((!err) && (err != -EALREADY)) {
Expand Down Expand Up @@ -420,6 +424,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
}
}

#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
static void security_changed(struct bt_conn *conn, bt_security_t level,
enum bt_security_err err)
{
Expand All @@ -436,11 +441,14 @@ static void security_changed(struct bt_conn *conn, bt_security_t level,

gatt_discover(conn);
}
#endif

BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
.security_changed = security_changed
#endif
};

static void scan_filter_match(struct bt_scan_device_info *device_info,
Expand Down Expand Up @@ -515,7 +523,7 @@ static int scan_init(void)
return err;
}


#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
Expand Down Expand Up @@ -553,11 +561,13 @@ static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
.pairing_complete = pairing_complete,
.pairing_failed = pairing_failed
};
#endif

int main(void)
{
int err;

#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

Please try to use IS_ENABLED macro like it is done in the peripheral_uart sample

err = bt_conn_auth_cb_register(&conn_auth_callbacks);
if (err) {
LOG_ERR("Failed to register authorization callbacks.");
Expand All @@ -569,6 +579,7 @@ int main(void)
printk("Failed to register authorization info callbacks.\n");
return 0;
}
#endif

err = bt_enable(NULL);
if (err) {
Expand Down
Loading