Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nrf51-sdk by
source/nordic_sdk/components/ble/peer_manager/peer_id.c@0:bc2961fa1ef0, 2016-04-06 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Wed Apr 06 23:55:04 2016 +0100
- Revision:
- 0:bc2961fa1ef0
Synchronized with git rev 90647e3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jonathan Austin |
0:bc2961fa1ef0 | 1 | /* |
| Jonathan Austin |
0:bc2961fa1ef0 | 2 | * Copyright (c) Nordic Semiconductor ASA |
| Jonathan Austin |
0:bc2961fa1ef0 | 3 | * All rights reserved. |
| Jonathan Austin |
0:bc2961fa1ef0 | 4 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
| Jonathan Austin |
0:bc2961fa1ef0 | 6 | * are permitted provided that the following conditions are met: |
| Jonathan Austin |
0:bc2961fa1ef0 | 7 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| Jonathan Austin |
0:bc2961fa1ef0 | 9 | * list of conditions and the following disclaimer. |
| Jonathan Austin |
0:bc2961fa1ef0 | 10 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
| Jonathan Austin |
0:bc2961fa1ef0 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
| Jonathan Austin |
0:bc2961fa1ef0 | 13 | * other materials provided with the distribution. |
| Jonathan Austin |
0:bc2961fa1ef0 | 14 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
| Jonathan Austin |
0:bc2961fa1ef0 | 16 | * contributors to this software may be used to endorse or promote products |
| Jonathan Austin |
0:bc2961fa1ef0 | 17 | * derived from this software without specific prior written permission. |
| Jonathan Austin |
0:bc2961fa1ef0 | 18 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 19 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| Jonathan Austin |
0:bc2961fa1ef0 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| Jonathan Austin |
0:bc2961fa1ef0 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| Jonathan Austin |
0:bc2961fa1ef0 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| Jonathan Austin |
0:bc2961fa1ef0 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| Jonathan Austin |
0:bc2961fa1ef0 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| Jonathan Austin |
0:bc2961fa1ef0 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| Jonathan Austin |
0:bc2961fa1ef0 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| Jonathan Austin |
0:bc2961fa1ef0 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| Jonathan Austin |
0:bc2961fa1ef0 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Jonathan Austin |
0:bc2961fa1ef0 | 30 | * |
| Jonathan Austin |
0:bc2961fa1ef0 | 31 | */ |
| Jonathan Austin |
0:bc2961fa1ef0 | 32 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 33 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 34 | #include "peer_id.h" |
| Jonathan Austin |
0:bc2961fa1ef0 | 35 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 36 | #include <stdint.h> |
| Jonathan Austin |
0:bc2961fa1ef0 | 37 | #include <string.h> |
| Jonathan Austin |
0:bc2961fa1ef0 | 38 | #include "sdk_errors.h" |
| Jonathan Austin |
0:bc2961fa1ef0 | 39 | #include "peer_manager_types.h" |
| Jonathan Austin |
0:bc2961fa1ef0 | 40 | #include "pm_mutex.h" |
| Jonathan Austin |
0:bc2961fa1ef0 | 41 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 42 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 43 | typedef struct |
| Jonathan Austin |
0:bc2961fa1ef0 | 44 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 45 | uint8_t peer_ids[MUTEX_STORAGE_SIZE(PM_PEER_ID_N_AVAILABLE_IDS)]; /*< bitmap. */ |
| Jonathan Austin |
0:bc2961fa1ef0 | 46 | } pi_t; |
| Jonathan Austin |
0:bc2961fa1ef0 | 47 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 48 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 49 | static pi_t m_pi = {.peer_ids = {0}}; |
| Jonathan Austin |
0:bc2961fa1ef0 | 50 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 51 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 52 | static void internal_state_reset(pi_t * p_pi) |
| Jonathan Austin |
0:bc2961fa1ef0 | 53 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 54 | memset(p_pi, 0, sizeof(pi_t)); |
| Jonathan Austin |
0:bc2961fa1ef0 | 55 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 56 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 57 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 58 | void peer_id_init(void) |
| Jonathan Austin |
0:bc2961fa1ef0 | 59 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 60 | internal_state_reset(&m_pi); |
| Jonathan Austin |
0:bc2961fa1ef0 | 61 | pm_mutex_init(m_pi.peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); |
| Jonathan Austin |
0:bc2961fa1ef0 | 62 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 63 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 64 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 65 | pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id) |
| Jonathan Austin |
0:bc2961fa1ef0 | 66 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 67 | pm_peer_id_t allocated_peer_id = PM_PEER_ID_INVALID; |
| Jonathan Austin |
0:bc2961fa1ef0 | 68 | if (peer_id == PM_PEER_ID_INVALID) |
| Jonathan Austin |
0:bc2961fa1ef0 | 69 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 70 | allocated_peer_id = pm_mutex_lock_first_available(m_pi.peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); |
| Jonathan Austin |
0:bc2961fa1ef0 | 71 | if (allocated_peer_id == PM_PEER_ID_N_AVAILABLE_IDS) |
| Jonathan Austin |
0:bc2961fa1ef0 | 72 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 73 | allocated_peer_id = PM_PEER_ID_INVALID; |
| Jonathan Austin |
0:bc2961fa1ef0 | 74 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 75 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 76 | else if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) |
| Jonathan Austin |
0:bc2961fa1ef0 | 77 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 78 | bool lock_success = pm_mutex_lock(m_pi.peer_ids, peer_id); |
| Jonathan Austin |
0:bc2961fa1ef0 | 79 | allocated_peer_id = lock_success ? peer_id : PM_PEER_ID_INVALID; |
| Jonathan Austin |
0:bc2961fa1ef0 | 80 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 81 | return allocated_peer_id; |
| Jonathan Austin |
0:bc2961fa1ef0 | 82 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 83 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 84 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 85 | void peer_id_free(pm_peer_id_t peer_id) |
| Jonathan Austin |
0:bc2961fa1ef0 | 86 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 87 | if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) |
| Jonathan Austin |
0:bc2961fa1ef0 | 88 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 89 | pm_mutex_unlock(m_pi.peer_ids, peer_id); |
| Jonathan Austin |
0:bc2961fa1ef0 | 90 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 91 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 92 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 93 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 94 | bool peer_id_is_allocated(pm_peer_id_t peer_id) |
| Jonathan Austin |
0:bc2961fa1ef0 | 95 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 96 | if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) |
| Jonathan Austin |
0:bc2961fa1ef0 | 97 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 98 | return pm_mutex_lock_status_get(m_pi.peer_ids, peer_id); |
| Jonathan Austin |
0:bc2961fa1ef0 | 99 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 100 | return false; |
| Jonathan Austin |
0:bc2961fa1ef0 | 101 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 102 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 103 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 104 | pm_peer_id_t peer_id_next_id_get(pm_peer_id_t prev_peer_id) |
| Jonathan Austin |
0:bc2961fa1ef0 | 105 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 106 | pm_peer_id_t i = (prev_peer_id == PM_PEER_ID_INVALID) ? 0 : (prev_peer_id + 1); |
| Jonathan Austin |
0:bc2961fa1ef0 | 107 | for (; i < PM_PEER_ID_N_AVAILABLE_IDS; i++) |
| Jonathan Austin |
0:bc2961fa1ef0 | 108 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 109 | if (pm_mutex_lock_status_get(m_pi.peer_ids, i)) |
| Jonathan Austin |
0:bc2961fa1ef0 | 110 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 111 | return i; |
| Jonathan Austin |
0:bc2961fa1ef0 | 112 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 113 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 114 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 115 | return PM_PEER_ID_INVALID; |
| Jonathan Austin |
0:bc2961fa1ef0 | 116 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 117 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 118 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 119 | uint32_t peer_id_n_ids(void) |
| Jonathan Austin |
0:bc2961fa1ef0 | 120 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 121 | uint32_t n_ids = 0; |
| Jonathan Austin |
0:bc2961fa1ef0 | 122 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 123 | for (pm_peer_id_t i = 0; i < PM_PEER_ID_N_AVAILABLE_IDS; i++) |
| Jonathan Austin |
0:bc2961fa1ef0 | 124 | { |
| Jonathan Austin |
0:bc2961fa1ef0 | 125 | n_ids += pm_mutex_lock_status_get(m_pi.peer_ids, i); |
| Jonathan Austin |
0:bc2961fa1ef0 | 126 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 127 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 128 | return n_ids; |
| Jonathan Austin |
0:bc2961fa1ef0 | 129 | } |
| Jonathan Austin |
0:bc2961fa1ef0 | 130 | |
| Jonathan Austin |
0:bc2961fa1ef0 | 131 |
