This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Delete all but recent peers when PM_EVT_STORAGE_FULL event happens

Hi,

I'm using nRF52832 with IAR and SDK V15, S132.

I incorporated the peer manager in the uart serial profile example.

I assume, since I have 3 pages (inc. GC) set in FDS, that the Device and Central can store about 50 bonded peers.

As far as I understand, once a new bond request is received and there's no more space left I will get the PM_EVT_STORAGE_FULL event.

Currently, in that event the following code is implemented:

          err_code = fds_gc();

          if (err_code == FDS_ERR_NO_SPACE_IN_QUEUES)
          {
            err_code = fds_gc();
            APP_ERROR_CHECK(err_code);
          }
          else
            APP_ERROR_CHECK(err_code);

This makes no sense as the FDS is used only for bonded device information, so I assume I must delete the peers before running the fds_gc() function.

I read that the peers cannot be deleted when the device is connectable or connected, so is it safe to delete the peers in this event?

Also, is it safe to delete the peers in the PM_EVT_CONN_SEC_FAILED event?

Please answer the above regarding both Central and Peripheral.

The function pm_peers_delete() deletes all peers, how can I modify it to delete only the oldest peers? say, half of the stored peers?

Thank you

Parents
  • I am sure there will be different answer depending on who you ask, but I would only execute delete peer(s) between connections.

    There is support for a rank system in the peer manager module, such that each time you re-connect with previous bonded peer you may set it as the highest rank (call pm_peer_rank_highest()), thereby the oldest used peer will have the lowest rank (call pm_peer_ranks_get()). You can count number of available peers between connections by calling pm_peer_count(), thereby if you want to support 50 peers, then set the max to 51, and delete the oldest peer when pm_peer_count() return 51. A peer is deleted by calling pm_peer_delete() and you may run garbage collection.

    Alternatively to use the built-in rank system you may also iterate through peers and retrieve your own application data by something similar to:

        // Example on how to read stored peer ids and stored application data
        
        NRF_LOG_INFO("Show stored peer id and your application data");
        uint16_t stored_length = 4;
        uint8_t stored_data[4];
    
        pm_peer_id_t peer_id;
        peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);    
    
        while ((peer_id != PM_PEER_ID_INVALID))
        {
            err_code = pm_peer_data_app_data_load(peer_id, stored_data, &stored_length); // This can be data you load from the bond.
            if(err_code == NRF_SUCCESS)
            {
                NRF_LOG_INFO("Peer id: %d Your stored application data: %d", peer_id, stored_data[0]);
            }
            else
                NRF_LOG_INFO("No hit");
            
            peer_id = pm_next_peer_id_get(peer_id);
        }

    To store some application specific data to a peer (e.g. on PM_EVT_PEER_DATA_UPDATE_SUCCEEDED):

                    // Example on how to store data to a peer id
                    uint16_t stored_length = 4; 
                    __align(4) static uint8_t stored_data[4];
                    stored_data[0] = m_button; // As an example a button press
                    
                    err_code = pm_peer_data_app_data_store(m_peer_id, &stored_data, stored_length, NULL);
                    APP_ERROR_CHECK(err_code);   

    This will allow you to store and load any generic application data as you see fit to a bond, for instance you can make your own implementation of a rank system and/or store other application related data.

    Best regards,
    Kenneth

  • But am I right in my assumption that I must delete peers in order for the garbage collector to have any effect?
    And if I get PM_EVT_CONN_SEC_FAILED or PM_EVT_STORAGE_FULL event, is it safe to assume I'm not connected nor advertising?

  • Clonimmus74 said:
    But am I right in my assumption that I must delete peers in order for the garbage collector to have any effect?

     Not necessarily, there may also be other flash operations occurring, for instance if you are using the rank system. 

    Clonimmus74 said:
    And if I get PM_EVT_CONN_SEC_FAILED or PM_EVT_STORAGE_FULL event, is it safe to assume I'm not connected nor advertising?

    No. You will need to monitor disconnect event, delete, and then start advertising/scan.

    Best regards,
    Kenneth

Reply
  • Clonimmus74 said:
    But am I right in my assumption that I must delete peers in order for the garbage collector to have any effect?

     Not necessarily, there may also be other flash operations occurring, for instance if you are using the rank system. 

    Clonimmus74 said:
    And if I get PM_EVT_CONN_SEC_FAILED or PM_EVT_STORAGE_FULL event, is it safe to assume I'm not connected nor advertising?

    No. You will need to monitor disconnect event, delete, and then start advertising/scan.

    Best regards,
    Kenneth

Children
No Data
Related