Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Peers added to the scanning whitelist

Hi,

I am looking at and testing the ble_app_hrs_c_pca10040_s132 example.

When the whitelist ist set up with the "whitelist request" we execute the following two lines:

peer_list_get(peers, &peer_cnt);
ret = pm_whitelist_set(peers, peer_cnt);

However, the function "peer_list_get" does not return the peers in the order of their rank according to my observations and also the documentation page of pm_next_peer_id_get which is called by "peer_list_get":  The order in which peer IDs are returned should be considered unpredictable

So this means we just add the first 8 peers in the peer list which probably just correspond to the first 8 peers ever connected.

I think the example should use something like: 

pm_peer_data_load(p_all_peer_ids[all_peers_cnt-1], PM_PEER_DATA_ID_PEER_RANK, &rank, &rankLenght);

to get the peer ranks and then order the peers accordingly before adding them to the whitelist.

Are those observations and conclusions correct?

Thank you for your response.

  • Hi. 

    Sorry about the delay. 

    I'll take a look at your ticket, and get back to you today. 

    Br, 
    Joakim

  • Hi.

    I'm so sorry this wasn't handled long ago.

    You can use the function pm_peer_ranks_get();

    "Function for finding the highest and lowest ranked peers.

    The rank is saved in persistent storage under the data ID PM_PEER_DATA_ID_PEER_RANK.

    The interpretation of rank is up to the user, because the rank is only updated by calling pm_peer_rank_highest or by manipulating the value using a PM_PEER_DATA_FUNCTIONS function."

    As stated above; If you want to update the rank to highest:

    pm_peer_rank_highest()

    "Function for updating the rank of a peer to be highest among all stored peers. "

    You can find more information in the online documentation:
    https://infocenter.nordicsemi.com/index.jsp

    Br,
    Joakim

  • The problem with the function pm_peer_ranks_get() is that it only returns the highest and the lowest ranked peers. If you want to put the last connected 8 peers into the whitelist this does not help.

    All the nordic examples using a whitelist are actually just putting the first 8 connected peers into the whitelist and NOT the 8 peers the device has been connected to last.

    I would suggest to use something like this in all the nordic examples in the next SDK. Where can I propose this?

    +    /*************** ranks **************/
    +    UINT32_T ranks[MAX_PEERS_CHECKED];
    +    memset(ranks, 0xFF, sizeof(ranks));
    +    /*************** ranks **************/
     
         // get all the peer IDs from peerlist
         pm_peer_id_t current_peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
         while (current_peer_id != PM_PEER_ID_INVALID)
         {
    -	p_all_peer_ids[all_peers_cnt++] = current_peer_id;
    +	p_all_peer_ids[all_peers_cnt] = current_peer_id;
    +
    +	/******************************** printing MAC BEGIN *********************************************/
    +	NRF_LOG_DEBUG("  peer id in p_all_peer_ids =  %u",current_peer_id);
    +	pm_peer_data_app_data_load(p_all_peer_ids[all_peers_cnt], &sPeerAppData, &UINT32_PeerDataSize);
    +	NRF_LOG_DEBUG("  MAC in p_all_peer_ids =  %x %x %x %x",sPeerAppData.vUINT8_DeviceAddress[0], sPeerAppData.vUINT8_DeviceAddress[1], sPeerAppData.vUINT8_DeviceAddress[2], sPeerAppData.vUINT8_DeviceAddress[3]);
    +	/********************************  printing MAC BEGIN *********************************************/
    +
    +	/******************************** Peer ranks BEGIN *********************************************/
    +	UINT32_T rank;
    +	UINT16_T rankLenght = sizeof(rank);
    +	ret = pm_peer_data_load(p_all_peer_ids[all_peers_cnt], PM_PEER_DATA_ID_PEER_RANK, &rank, &rankLenght);
    +	ranks[all_peers_cnt] = rank;
    +	NRF_LOG_DEBUG("  return code of pm_peer_data_load %u",ret);
    +	// we could also use pm_peer_data_store/delete with the pm_peer_data_id PM_PEER_DATA_ID_PEER_RANK
    +	NRF_LOG_DEBUG("  rank in p_all_peer_ids %u",rank);
    +	NRF_LOG_DEBUG("---------------------");
    +	/******************************** Peer ranks END ***********************************************/
    +
     	current_peer_id = pm_next_peer_id_get(current_peer_id);
    +	all_peers_cnt++;
    +    }
    +
    +    /******************************** order the peers by rank BEGIN ******************************/
    +    // we have two arrays: ranks and p_all_peer_ids. we want to order p_all_peer_ids according the the ranks
    +    typedef struct
    +    {
    +        int index_from_A;
    +        int value_from_B;
    +    } index_value_wrapper;
    +
    +    index_value_wrapper index_wrapper_array[all_peers_cnt];
    +
    +    for (int i=0; i < all_peers_cnt ; i++)
    +    {
    +        index_wrapper_array[i].index_from_A = p_all_peer_ids[i];
    +        index_wrapper_array[i].value_from_B = ranks[p_all_peer_ids[i]];
         }
     
    +
    +    int comparator (const void* lhs, const void* rhs)
    +    {
    +      // TODO switch left and right to get the decreasing order
    +      int l = ((index_value_wrapper *)lhs)->value_from_B;
    +      int r = ((index_value_wrapper *)rhs)->value_from_B;
    +      return (r - l);
    +
    +      //return (lhs.value_from_B - rhs->value_from_B);
    +    }
    +
    +    // sort the struct with the peer ids according to ranks
    +    qsort(index_wrapper_array, all_peers_cnt, sizeof(index_value_wrapper), comparator);
    +
    +    // extract the sorted peers and save in p_all_peer_ids
    +    for(int extract_i=0; extract_i < all_peers_cnt; extract_i++){
    +	p_all_peer_ids[extract_i] = index_wrapper_array[extract_i].index_from_A;
    +	NRF_LOG_DEBUG("  peer id in p_all_peer_ids AFTER SORTING =  %u",p_all_peer_ids[extract_i]);
    +    }
    +
    +
    +    /******************************** order the peers by rank END ********************************/

Related