Nordic UART Service (NUS)

Hi!

And I use the Zephyr SHELL to configure the device (nrf52832) from the terminal on the android tablet.

A problem with using a shell with nus ble transport. When you first connect, keystrokes are sent to the device, but nothing comes in response. If you disconnect and reconnect, sometimes the terminal starts working as it should.

--- first log output --

[00:00:15.241,027] <inf> ble: connected: Connected 0x20001f38-0x20001f38
[00:00:15.241,485] <inf> bt_nus: bt_nus_init: NUS init success
[00:00:16.463,226] <dbg> bt_nus: nus_ccc_cfg_changed: Notification has been turned on
[00:00:17.400,726] <dbg> bt_nus: on_receive: Received data, handle 0, conn 0x20001f38
[00:00:19.150,756] <dbg> bt_nus: on_receive: Received data, handle 0, conn 0x20001f38
[00:00:19.850,799] <dbg> bt_nus: on_receive: Received data, handle 0, conn 0x20001f38

--- log output --
[00:02:20.700,256] <inf> ble: connected: Connected 0x20002000-0x20002000
[00:02:21.898,895] <dbg> bt_nus: nus_ccc_cfg_changed: Notification has been turned on
[00:02:22.043,823] <dbg> bt_nus: on_receive: Received data, handle 0, conn 0x20002000
[00:02:22.143,676] <dbg> bt_nus: on_sent: Data send, conn 0x20002000
[00:02:23.843,597] <dbg> bt_nus: on_receive: Received data, handle 0, conn 0x20002000
[00:02:23.943,450] <dbg> bt_nus: on_sent: Data send, conn 0x20002000

-----------------------------------

In function enable conn is NULL :

--- subsys/shell/shell_bt_nus.c ---

static int enable(const struct shell_transport *transport, bool blocking_tx)

        if (blocking_tx) {
                /* transport cannot work in blocking mode, shut down */
                bt_nus->ctrl_blk->conn = NULL;

-----------------------------------------------------

Workaround (subsys/shell/shell_bt_nus.c):

static void rx_callback(struct bt_conn *conn, const uint8_t *const data, uint16_t len)
{
        const struct shell_bt_nus *bt_nus =
                (const struct shell_bt_nus *)shell_transport_bt_nus.ctx;
        uint32_t done = ring_buf_put(bt_nus->rx_ringbuf, data, len);

        bt_nus->ctrl_blk->conn = conn;   // <--- added by me

-------------------------------------------------------

In this case, everything works fine.

How to solve this problem correctly.

=====================

ncs -- v2.4.1

The test code is based on the example "nrf/samples/bluetooth/shell_bt_nus".

--- prj.conf --

CONFIG_SHELL=y
CONFIG_SHELL_CMDS=y
CONFIG_BT_NUS_LOG_LEVEL_DBG=y
# # Enable the BT NUS shell
CONFIG_SHELL_BT_NUS=y
#CONFIG_SHELL_BT_NUS_LOG_LEVEL_DBG=y

  • ncs 2.4.1

    issue created by shell.c 

    shell_thread() does call sh->iface->api->enable(sh->iface, false);

    and also call z_shell_log_backend_enable calling sh->iface->api->enable(sh->iface, true); for CONFIG_LOG_MODE_IMMEDIATE defined

    This causes shell_bt_nus.c enable() function to set bt_nus->ctrl_blk->conn = NULL;
    Then shell could not process(write) nus messages properly.
    On a second shell_bt_nus.c enable() shell_init() is not called and nus shell working OK.

    The Fix without modifying NCS:

    create k_work and init

    struct k_work nus_schell_workaround_work;
    static void nus_schell_workaround_work_cb(struct k_work *work);

    static void nus_schell_workaround_work_callback (struct k_work *work)
    {
      shell_bt_nus_enable(NULL);
      shell_bt_nus_disable();
    }

    At bt shell init:

     r = shell_bt_nus_init();
    if (r) {
      LOG_ERR("shell_bt_nus_init, err: %d", r);
    return r;
    }
    k_work_init(&nus_schell_workaround_work, nus_schell_workaround_work_callback);
    k_work_schedule(&nus_schell_workaround_work, K_NO_WAIT);

    Kind regards

Related