static void conn_params_timer_handler(void *privData) { OS_ERR_TYPE os_err; struct bt_conn_info info = { 0 }; const struct bt_le_conn_param conn_params = { MIN_CONN_INTERVAL, MAX_CONN_INTERVAL, SLAVE_LATENCY, CONN_SUP_TIMEOUT }; bt_conn_get_info(_ble_app_cb.conn_periph, &info); /* Check if there was an update in the connection parameters */ if (info.role != BT_CONN_ROLE_MASTER && (_ble_app_cb.conn_values.latency != SLAVE_LATENCY || _ble_app_cb.conn_values.supervision_to != CONN_SUP_TIMEOUT || _ble_app_cb.conn_values.interval < MIN_CONN_INTERVAL || _ble_app_cb.conn_values.interval > MAX_CONN_INTERVAL)) { /* Start the 30 seconds timer to retry updating the connection */ timer_start(_ble_app_cb.conn_timer, 30000, &os_err); /* Send request to update the connection */ bt_conn_le_param_update(_ble_app_cb.conn_periph, &conn_params); } else ble_app_delete_conn_timer(); }
static void ipsp_connected(struct bt_l2cap_chan *chan) { struct bt_context *ctxt = CHAN_CTXT(chan); struct bt_conn_info info; #if defined(CONFIG_NET_DEBUG_L2_BLUETOOTH) char src[BT_ADDR_LE_STR_LEN]; char dst[BT_ADDR_LE_STR_LEN]; #endif bt_conn_get_info(chan->conn, &info); #if defined(CONFIG_NET_DEBUG_L2_BLUETOOTH) bt_addr_le_to_str(info.le.src, src, sizeof(src)); bt_addr_le_to_str(info.le.dst, dst, sizeof(dst)); NET_DBG("Channel %p Source %s connected to Destination %s", chan, src, dst); #endif /* Swap bytes since net APIs expect big endian address */ sys_memcpy_swap(ctxt->src.val, info.le.src->a.val, sizeof(ctxt->src)); sys_memcpy_swap(ctxt->dst.val, info.le.dst->a.val, sizeof(ctxt->dst)); net_if_set_link_addr(ctxt->iface, ctxt->src.val, sizeof(ctxt->src.val)); /* Set iface up */ net_if_up(ctxt->iface); }
static void on_connected(struct bt_conn *conn, uint8_t err) { struct bt_conn_info info = { 0 }; bt_conn_get_info(conn, &info); if (!err) { if (info.role == BT_CONN_ROLE_SLAVE) { bt_conn_ref(conn); _ble_app_cb.conn_periph = conn; /* stop adv timer */ ble_app_stop_advertisement(); _ble_app_cb.conn_values.interval = info.le.interval; _ble_app_cb.conn_values.latency = info.le.latency; _ble_app_cb.conn_values.supervision_to = info.le.timeout; /* If peripheral and connection values are not compliant with the PPCP */ /* Start a timer to configure the parameters */ _ble_app_cb.conn_timer = timer_create( conn_params_timer_handler, NULL, 5000, false, true, NULL); } #if !defined(BLE_APP_DEBUG) pr_info(LOG_MODULE_MAIN, "BLE connected (conn: %p, role: %d)", conn, info.role); #else pr_info( LOG_MODULE_MAIN, "BLE connected (conn: %p, role: %d) to " "%02x:%02x:%02x:%02x:%02x:%02x/%d", conn, info.role, info.le.src->val[5], info.le.src->val[4], info.le.src->val[3], info.le.src->val[2], info.le.src->val[1], info.le.src->val[0], info.type); #endif #ifdef CONFIG_SYSTEM_EVENTS system_event_push_ble_conn(true, (uint8_t *)&info.le.src->val[0]); #endif } else { pr_info(LOG_MODULE_MAIN, "BLE connection KO(conn: %p)", conn); } }
static void ipsp_connected(struct bt_l2cap_chan *chan) { struct bt_context *ctxt = CHAN_CTXT(chan); struct bt_conn_info info; struct net_linkaddr ll; struct in6_addr in6; #if defined(CONFIG_NET_DEBUG_L2_BLUETOOTH) char src[BT_ADDR_LE_STR_LEN]; char dst[BT_ADDR_LE_STR_LEN]; #endif bt_conn_get_info(chan->conn, &info); #if defined(CONFIG_NET_DEBUG_L2_BLUETOOTH) bt_addr_le_to_str(info.le.src, src, sizeof(src)); bt_addr_le_to_str(info.le.dst, dst, sizeof(dst)); NET_DBG("Channel %p Source %s connected to Destination %s", chan, src, dst); #endif /* Swap bytes since net APIs expect big endian address */ sys_memcpy_swap(ctxt->src.val, info.le.src->a.val, sizeof(ctxt->src)); sys_memcpy_swap(ctxt->dst.val, info.le.dst->a.val, sizeof(ctxt->dst)); net_if_set_link_addr(ctxt->iface, ctxt->src.val, sizeof(ctxt->src.val), NET_LINK_BLUETOOTH); ll.addr = ctxt->dst.val; ll.len = sizeof(ctxt->dst.val); /* Add remote link-local address to the nbr cache to avoid sending ns: * https://tools.ietf.org/html/rfc7668#section-3.2.3 * A Bluetooth LE 6LN MUST NOT register its link-local address. */ net_ipv6_addr_create_iid(&in6, &ll); net_ipv6_nbr_add(ctxt->iface, &in6, &ll, false, NET_IPV6_NBR_STATE_REACHABLE); /* Set iface up */ net_if_up(ctxt->iface); }
static void conn_addr_str(struct bt_conn *conn, char *addr, size_t len) { struct bt_conn_info info; if (bt_conn_get_info(conn, &info) < 0) { addr[0] = '\0'; return; } switch (info.type) { #if defined(CONFIG_BLUETOOTH_BREDR) case BT_CONN_TYPE_BR: bt_addr_to_str(info.br.dst, addr, len); break; #endif case BT_CONN_TYPE_LE: bt_addr_le_to_str(info.le.dst, addr, len); break; } }
static const char *current_prompt(void) { static char str[BT_ADDR_LE_STR_LEN + 2]; static struct bt_conn_info info; if (!default_conn) { return NULL; } if (bt_conn_get_info(default_conn, &info) < 0) { return NULL; } if (info.type != BT_CONN_TYPE_LE) { return NULL; } bt_addr_le_to_str(info.le.dst, str, sizeof(str) - 2); strcat(str, "> "); return str; }
static void on_disconnected(struct bt_conn *conn, uint8_t reason) { struct bt_conn_info info; bt_conn_get_info(conn, &info); if (info.role == BT_CONN_ROLE_SLAVE) { _ble_app_cb.conn_periph = NULL; bt_conn_unref(conn); ble_app_stop_advertisement(); ble_app_start_advertisement(BLE_ADV_DISCONNECT); } #ifdef CONFIG_SYSTEM_EVENTS system_event_push_ble_conn(false, NULL); #endif pr_info(LOG_MODULE_MAIN, "BLE disconnected(conn: %p, hci_reason: 0x%x)", conn, reason); ble_app_delete_conn_timer(); }