Using K_timer in Zephyr NCS 1.8.0 to initiate a BLE disconnection on Central

Hello,

I am using nRF Connect SDK 1.8.0 on the nRF52840.  I want to limit the amount of time that a peripheral can connect to my central 52840.  On connection start I initiate a timer following the K_timer convention set by zephyr and it times out properly.  I want to use the timeout handler function to call

int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason)
 

I have used asynchronous functions to handle the connection and disconnection events but I had to register their callbacks using the struct 

static struct bt_conn_cb connection_callbacks
.  

Since there is no BLE event I'm timing out with, I wasn't sure how to get the bt_conn object to the timer's timeout handling function.  I assume that the on_connected or on_disconnected events are receiving the bt_conn object through the event handler, but I was not sure about this.  I tried to make the bt_conn object a static global var within the file, but in the timer handler the bt_conn_disconnect function fails with ENOTCONN (-128) so it seems it isn't accepting the bt_conn object correctly.

What would be the best way to correctly give the bt_conn object to the timer's timeout handling function here?

Thanks!

Parents
  • Your link is broken and you didn't answer any of the questions in my post.  I will look for the peripheral_uart sample elsewhere though.

  • Hello,

    I am sorry as the first link was broken.  Now you can check the link and also if you can find this in the NCS folder (nrf\samples\bluetooth\peripheral_uart).

    If you declare the bt_conn object the way it is in the peripheral_uart sample, then it should act a global variable which can be used in your connection and disconnection function. Both connection and disconnection are defined from callback function.

    static struct bt_conn *current_conn
    --
    
    
    
    static void connected(struct bt_conn *conn, uint8_t err)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	if (err) {
    		LOG_ERR("Connection failed (err %u)", err);
    		return;
    	}
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    	LOG_INF("Connected %s", addr);
    
    	current_conn = bt_conn_ref(conn);
    
    	dk_set_led_on(CON_STATUS_LED);
    }
    
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	LOG_INF("Disconnected: %s (reason %u)", addr, reason);
    
    	if (auth_conn) {
    		bt_conn_unref(auth_conn);
    		auth_conn = NULL;
    	}
    
    	if (current_conn) {
    		bt_conn_unref(current_conn);
    		current_conn = NULL;
    		dk_set_led_off(CON_STATUS_LED);
    	}
    }
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.connected    = connected,
    	.disconnected = disconnected,
    #ifdef CONFIG_BT_NUS_SECURITY_ENABLED
    	.security_changed = security_changed,
    #endif
    };

    BR

    Kazi

Reply
  • Hello,

    I am sorry as the first link was broken.  Now you can check the link and also if you can find this in the NCS folder (nrf\samples\bluetooth\peripheral_uart).

    If you declare the bt_conn object the way it is in the peripheral_uart sample, then it should act a global variable which can be used in your connection and disconnection function. Both connection and disconnection are defined from callback function.

    static struct bt_conn *current_conn
    --
    
    
    
    static void connected(struct bt_conn *conn, uint8_t err)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	if (err) {
    		LOG_ERR("Connection failed (err %u)", err);
    		return;
    	}
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    	LOG_INF("Connected %s", addr);
    
    	current_conn = bt_conn_ref(conn);
    
    	dk_set_led_on(CON_STATUS_LED);
    }
    
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	LOG_INF("Disconnected: %s (reason %u)", addr, reason);
    
    	if (auth_conn) {
    		bt_conn_unref(auth_conn);
    		auth_conn = NULL;
    	}
    
    	if (current_conn) {
    		bt_conn_unref(current_conn);
    		current_conn = NULL;
    		dk_set_led_off(CON_STATUS_LED);
    	}
    }
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.connected    = connected,
    	.disconnected = disconnected,
    #ifdef CONFIG_BT_NUS_SECURITY_ENABLED
    	.security_changed = security_changed,
    #endif
    };

    BR

    Kazi

Children
Related