<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://test-devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/74638/when-are-peer-managers-connection-handles-invalidated</link><description>Hi, 
 I have a central that uses PM to connect to some peripherals. 
 If I connect to a peer (connection handle 0), and disconnect this device (e.g. by shutting it off). I receive BLE_GAP_EVT_DISCONNECTED and all works fine. 
 But later when I call 
</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Thu, 09 Jun 2022 07:32:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/74638/when-are-peer-managers-connection-handles-invalidated" /><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/371554?ContentTypeID=1</link><pubDate>Thu, 09 Jun 2022 07:32:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12bb920a-a489-4c46-9f1c-a28f24aafdca</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;Unfortunately I can&amp;#39;t mark Sigurds answer (&amp;quot;it&amp;#39;s a documentation error&amp;quot;) as verified. But report is &amp;quot;fixed&amp;quot; with that.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/310992?ContentTypeID=1</link><pubDate>Fri, 21 May 2021 10:24:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82554eeb-d884-44c4-ba35-bf4927991b21</guid><dc:creator>user15146</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I agree. I have reported this to the SDK team as a documentation bug.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/309387?ContentTypeID=1</link><pubDate>Wed, 12 May 2021 07:51:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0c438d2d-f364-47a3-835e-b4588465a8b1</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;I found a reason why it was implemented like this.&lt;/p&gt;
&lt;p&gt;im_peer_free() uses im_conn_handle_get() and this does not work for disconnected peers with my fix.&lt;/p&gt;
&lt;p&gt;But then the docs of pm_peer_handle_get() are wrong:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt; * @param[out] p_conn_handle  Connection handle, or @ref BLE_CONN_HANDLE_INVALID if the peer
 *                            is not connected.&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;On the other hand the docs for im_conn_handle_get() states that the conn_handle can refer to a recently disconnceted connection.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt; * @return The corresponding connection handle, or @ref BLE_CONN_HANDLE_INVALID if none could be
 *         resolved. The conn_handle can refer to a recently disconnected connection.&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307991?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 15:36:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1537834a-6e75-4fad-999c-1bf583a803c7</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;Yes that would be possible too.&lt;/p&gt;
&lt;p&gt;Though this looks like a bug in the SDK to me. It would be nice if you could report back when you know why it&amp;#39;s implemented like this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307990?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 15:22:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bd14ffe9-f144-4b12-b489-c20e0c83df63</guid><dc:creator>user15146</dc:creator><description>[quote user="mgux"]ble_conn_state_status() checks the valid_flags and connected_flags. This fixes my problem.[/quote]
&lt;p&gt;&amp;nbsp;Yeah, I see now that disconnected links are first invalidated when a device connects again (record_purge_disconnected() called on BLE_GAP_EVT_CONNECTED in&amp;nbsp;ble_conn_state.c). I haven&amp;#39;t looked into why it&amp;#39;s implemented like this yet, but an alternative to your patch, could be to call&amp;nbsp;ble_conn_state_status(connHandle) in your application code, and based on the return value of this, then do a disconnect and delete the bond info. With this approach, you don&amp;#39;t need to modify the&amp;nbsp;id_manager.c file&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307926?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 12:27:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:649a663b-56eb-4a61-9c8a-4b586fcd466f</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;Please find the patch attached.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://test-devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/0001_2D00_Fix_2D00_im_5F00_conn_5F00_handle_5F00_get_2D00_for_2D00_disconnected_2D00_peers.patch"&gt;test-devzone.nordicsemi.com/.../0001_2D00_Fix_2D00_im_5F00_conn_5F00_handle_5F00_get_2D00_for_2D00_disconnected_2D00_peers.patch&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307922?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 12:15:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89800e9f-9614-4ed7-8a88-0571f9146f5e</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;Sorry, that was a copy&amp;amp;paste error.&lt;/p&gt;
&lt;p&gt;I use connHandle everywhere.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I tried to change im__conn_handle_get to the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint16_t im_conn_handle_get(pm_peer_id_t peer_id)
{
    if (peer_id == PM_PEER_ID_INVALID)
    {
        return BLE_CONN_HANDLE_INVALID;
    }

    for (uint16_t conn_handle = 0; conn_handle &amp;lt; IM_MAX_CONN_HANDLES; conn_handle++)
    {
        if ((m_connections[conn_handle].peer_id == peer_id) &amp;amp;&amp;amp; ble_conn_state_status(conn_handle) == BLE_CONN_STATUS_CONNECTED)
        {
            return conn_handle;
        }
    }
    return BLE_CONN_HANDLE_INVALID;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;ble_conn_state_status() checks the valid_flags and connected_flags. This fixes my problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307914?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 12:10:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6613ffce-c80e-4377-94e7-58da1f3f0310</guid><dc:creator>user15146</dc:creator><description>[quote user="mgux"]I use this snippet to disconnect[/quote]
&lt;p&gt;Seems like you are using both connectionHandle and connHandle?&lt;/p&gt;
&lt;p&gt;Try this instead:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;// disconnect and remove peer
uint16_t connHandle = BLE_CONN_HANDLE_INVALID;
auto ret = pm_conn_handle_get(peerId, &amp;amp;connHandle);
APP_ERROR_CHECK(ret);

if(connHandle == BLE_CONN_HANDLE_INVALID)
{
    return;
}
ret = sd_ble_gap_disconnect(connHandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

APP_ERROR_CHECK(ret);
// The peer got removed from the list of bonds.
ret = pm_peer_delete(peerId);
APP_ERROR_CHECK(ret);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307912?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 12:09:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:206d61a8-1732-4229-8a3d-df6b3f1abf07</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;I found some more:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;in ble_conn_state.c:&lt;/p&gt;
&lt;pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:&amp;#39;JetBrains Mono&amp;#39;,monospace;font-size:9,8pt;"&gt;&lt;span style="color:#cc7832;"&gt;&lt;pre class="ui-code" data-mode="text"&gt;typedef struct
{
    nrf_atflags_t valid_flags;                                 /**&amp;lt; Flags indicating which connection handles are valid. */
    nrf_atflags_t connected_flags;                             /**&amp;lt; Flags indicating which connections are connected, since disconnected connection handles will not immediately be invalidated. */
    nrf_atflags_t central_flags;                               /**&amp;lt; Flags indicating in which connections the local device is the central. */
    nrf_atflags_t encrypted_flags;                             /**&amp;lt; Flags indicating which connections are encrypted. */
    nrf_atflags_t mitm_protected_flags;                        /**&amp;lt; Flags indicating which connections have encryption with protection from man-in-the-middle attacks. */
    nrf_atflags_t lesc_flags;                                  /**&amp;lt; Flags indicating which connections have bonded using LE Secure Connections (LESC). */
    nrf_atflags_t user_flags[BLE_CONN_STATE_USER_FLAG_COUNT];  /**&amp;lt; Flags that can be reserved by the user. The flags will be cleared when a connection is invalidated, otherwise, the user is wholly responsible for the flag states. */
} ble_conn_state_flag_collections_t;&lt;/pre&gt;&lt;/span&gt;&lt;span style="color:#cc7832;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void record_set_disconnected(uint16_t conn_handle)
{
    nrf_atflags_clear(&amp;amp;m_bcs.flags.connected_flags, conn_handle);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;bool ble_conn_state_valid(uint16_t conn_handle)
{
    if (conn_handle &amp;gt;= BLE_CONN_STATE_MAX_CONNECTIONS)
    {
        return false;
    }
    return nrf_atflags_get(&amp;amp;m_bcs.flags.valid_flags, conn_handle);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;ble_conn_state_valid() is used in pm_conn_handle_get() and checks the valid_flags.. But record_set_disconnected() clears the connected flag not the valid_flags.&lt;/p&gt;
&lt;p&gt;Also the comment for connected_flags says that connection handles will no immediately be invalidated.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So maybe one should check the connected flag in pm_conn_handle_get()?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307907?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 12:03:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b7df48c-37d6-4f6c-9c80-25340da22770</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;I can confirm that &lt;/p&gt;
&lt;p&gt;record_set_disconnected(conn_handle);&lt;/p&gt;
&lt;p&gt;is called in ble_conn_state.c.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307899?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 11:54:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b93beb1b-5b28-4254-b4d5-6b3fbd0770ac</guid><dc:creator>user84602</dc:creator><description>[quote userid="15146" url="~/f/nordic-q-a/74638/when-are-peer-managers-connection-handles-invalidated/307896#307896"]&amp;nbsp;1)So you are in a active connection to another device when you try to delete the bond from the previously connected device?[/quote]
&lt;p&gt;Sorry I was wrong. The command is sent via USB CDC. But it is possible to be connected to another peer while I delete the bond to another one.&lt;/p&gt;
&lt;p&gt;I use this snippet to disconnect (if still connected) and remove the bond:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// disconnect and remove peer
uint16_t connHandle = BLE_CONN_HANDLE_INVALID;
auto ret = pm_conn_handle_get(peerId, &amp;amp;connHandle);
APP_ERROR_CHECK(ret);

if(connectionHandle == BLE_CONN_HANDLE_INVALID)
{
    return;
}
ret = sd_ble_gap_disconnect(connectionHandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

APP_ERROR_CHECK(ret);
// The peer got removed from the list of bonds.
ret = pm_peer_delete(peerId);
APP_ERROR_CHECK(ret);&lt;/pre&gt;&lt;/code&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307896?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 11:47:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f943bc25-bf0d-4ded-8cbc-565592f7473f</guid><dc:creator>user15146</dc:creator><description>[quote user="mgux"]After disconnect I send an command via BLE nus service to the central to remove the bonds.[/quote]
&lt;p&gt;&amp;nbsp;1)So you are in a active connection to another device when you try to delete the bond from the previously connected device?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;2) Can you post the code you use to delete the bond ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307882?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 11:16:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1ecd910-f60a-4dc0-8c70-a0b3afe81e58</guid><dc:creator>user84602</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I am using SDK version 17.0.2. I don&amp;#39;t call &lt;span&gt;pm_conn_handle_get()&lt;/span&gt; on &lt;span&gt;BLE_GAP_EVT_DISCONNECTED.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;After disconnect I send an command via BLE nus service to the central to remove the bonds.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I can verify that this code exists on my side. I guess it is called via&lt;/span&gt;&lt;/p&gt;
&lt;pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:&amp;#39;JetBrains Mono&amp;#39;,monospace;font-size:9,8pt;"&gt;&lt;code&gt;NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, BLE_CONN_STATE_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);&lt;/code&gt;&lt;span style="color:#cc7832;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When are peer managers connection handles invalidated?</title><link>https://test-devzone.nordicsemi.com/thread/307880?ContentTypeID=1</link><pubDate>Mon, 03 May 2021 11:08:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b218437-05e7-4d5d-8310-bc5b816383d4</guid><dc:creator>user15146</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The Peer Manager uses the&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_ble_conn_state.html"&gt;Connection state&amp;nbsp;&lt;/a&gt;&lt;span&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_ble_conn_state.html"&gt;module&lt;/a&gt;&amp;nbsp;to check this. Snippet from&amp;nbsp;ble_conn_state.c where&amp;nbsp;connection handles are invalidated:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;        case BLE_GAP_EVT_DISCONNECTED:
            record_set_disconnected(conn_handle);
            break;&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;What SDK version are you using?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;When do you call&amp;nbsp;pm_conn_handle_get() ? On BLE_GAP_EVT_DISCONNECTED or some other&amp;nbsp;events?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>