Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers peer_id.c Source File

peer_id.c

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 
00034 #include "peer_id.h"
00035 
00036 #include <stdint.h>
00037 #include <string.h>
00038 #include "sdk_errors.h "
00039 #include "peer_manager_types.h "
00040 #include "pm_mutex.h"
00041 
00042 
00043 typedef struct
00044 {
00045     uint8_t peer_ids[MUTEX_STORAGE_SIZE(PM_PEER_ID_N_AVAILABLE_IDS)]; /*< bitmap. */
00046 } pi_t;
00047 
00048 
00049 static pi_t m_pi = {.peer_ids = {0}};
00050 
00051 
00052 static void internal_state_reset(pi_t * p_pi)
00053 {
00054     memset(p_pi, 0, sizeof(pi_t));
00055 }
00056 
00057 
00058 void peer_id_init(void)
00059 {
00060     internal_state_reset(&m_pi);
00061     pm_mutex_init(m_pi.peer_ids, PM_PEER_ID_N_AVAILABLE_IDS);
00062 }
00063 
00064 
00065 pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id)
00066 {
00067     pm_peer_id_t allocated_peer_id = PM_PEER_ID_INVALID;
00068     if (peer_id == PM_PEER_ID_INVALID)
00069     {
00070         allocated_peer_id = pm_mutex_lock_first_available(m_pi.peer_ids, PM_PEER_ID_N_AVAILABLE_IDS);
00071         if (allocated_peer_id == PM_PEER_ID_N_AVAILABLE_IDS)
00072         {
00073             allocated_peer_id = PM_PEER_ID_INVALID;
00074         }
00075     }
00076     else if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
00077     {
00078         bool lock_success = pm_mutex_lock(m_pi.peer_ids, peer_id);
00079         allocated_peer_id = lock_success ? peer_id : PM_PEER_ID_INVALID;
00080     }
00081     return allocated_peer_id;
00082 }
00083 
00084 
00085 void peer_id_free(pm_peer_id_t peer_id)
00086 {
00087     if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
00088     {
00089         pm_mutex_unlock(m_pi.peer_ids, peer_id);
00090     }
00091 }
00092 
00093 
00094 bool peer_id_is_allocated(pm_peer_id_t peer_id)
00095 {
00096     if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS)
00097     {
00098         return pm_mutex_lock_status_get(m_pi.peer_ids, peer_id);
00099     }
00100     return false;
00101 }
00102 
00103 
00104 pm_peer_id_t peer_id_next_id_get(pm_peer_id_t prev_peer_id)
00105 {
00106     pm_peer_id_t i = (prev_peer_id == PM_PEER_ID_INVALID) ? 0 : (prev_peer_id + 1);
00107     for (; i < PM_PEER_ID_N_AVAILABLE_IDS; i++)
00108     {
00109         if (pm_mutex_lock_status_get(m_pi.peer_ids, i))
00110         {
00111             return i;
00112         }
00113     }
00114 
00115     return PM_PEER_ID_INVALID;
00116 }
00117 
00118 
00119 uint32_t peer_id_n_ids(void)
00120 {
00121     uint32_t n_ids = 0;
00122 
00123     for (pm_peer_id_t i = 0; i < PM_PEER_ID_N_AVAILABLE_IDS; i++)
00124     {
00125         n_ids += pm_mutex_lock_status_get(m_pi.peer_ids, i);
00126     }
00127 
00128     return n_ids;
00129 }
00130