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