Nordic stack and drivers for the mbed BLE API
Dependents: Sensen-classic-v8-IAQ_copy_4
Fork of nRF51822 by
TARGET_MCU_NRF51822/sdk/source/ble/peer_manager/pm_buffer.c@639:7f459d371b75, 2017-11-10 (annotated)
- Committer:
- amithy
- Date:
- Fri Nov 10 20:27:44 2017 +0000
- Revision:
- 639:7f459d371b75
- Parent:
- 638:c90ae1400bf2
t
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 "pm_buffer.h" |
Vincent Coubard |
638:c90ae1400bf2 | 35 | |
Vincent Coubard |
638:c90ae1400bf2 | 36 | #include <stdbool.h> |
Vincent Coubard |
638:c90ae1400bf2 | 37 | #include <string.h> |
Vincent Coubard |
638:c90ae1400bf2 | 38 | #include "nrf_error.h" |
Vincent Coubard |
638:c90ae1400bf2 | 39 | #include "pm_mutex.h" |
Vincent Coubard |
638:c90ae1400bf2 | 40 | |
Vincent Coubard |
638:c90ae1400bf2 | 41 | |
Vincent Coubard |
638:c90ae1400bf2 | 42 | #define BUFFER_IS_VALID(p_buffer) ((p_buffer != NULL) \ |
Vincent Coubard |
638:c90ae1400bf2 | 43 | && (p_buffer->p_memory != NULL) \ |
Vincent Coubard |
638:c90ae1400bf2 | 44 | && (p_buffer->p_mutex != NULL)) |
Vincent Coubard |
638:c90ae1400bf2 | 45 | |
Vincent Coubard |
638:c90ae1400bf2 | 46 | |
Vincent Coubard |
638:c90ae1400bf2 | 47 | |
Vincent Coubard |
638:c90ae1400bf2 | 48 | ret_code_t pm_buffer_init(pm_buffer_t * p_buffer, |
Vincent Coubard |
638:c90ae1400bf2 | 49 | uint8_t * p_buffer_memory, |
Vincent Coubard |
638:c90ae1400bf2 | 50 | uint32_t buffer_memory_size, |
Vincent Coubard |
638:c90ae1400bf2 | 51 | uint8_t * p_mutex_memory, |
Vincent Coubard |
638:c90ae1400bf2 | 52 | uint32_t mutex_memory_size, |
Vincent Coubard |
638:c90ae1400bf2 | 53 | uint32_t n_blocks, |
Vincent Coubard |
638:c90ae1400bf2 | 54 | uint32_t block_size) |
Vincent Coubard |
638:c90ae1400bf2 | 55 | { |
Vincent Coubard |
638:c90ae1400bf2 | 56 | if ( (p_buffer != NULL) |
Vincent Coubard |
638:c90ae1400bf2 | 57 | && (p_buffer_memory != NULL) |
Vincent Coubard |
638:c90ae1400bf2 | 58 | && (p_mutex_memory != NULL) |
Vincent Coubard |
638:c90ae1400bf2 | 59 | && (buffer_memory_size >= (n_blocks*block_size)) |
Vincent Coubard |
638:c90ae1400bf2 | 60 | && (mutex_memory_size >= MUTEX_STORAGE_SIZE(n_blocks)) |
Vincent Coubard |
638:c90ae1400bf2 | 61 | && (n_blocks != 0) |
Vincent Coubard |
638:c90ae1400bf2 | 62 | && (block_size != 0)) |
Vincent Coubard |
638:c90ae1400bf2 | 63 | { |
Vincent Coubard |
638:c90ae1400bf2 | 64 | p_buffer->p_memory = p_buffer_memory; |
Vincent Coubard |
638:c90ae1400bf2 | 65 | p_buffer->p_mutex = p_mutex_memory; |
Vincent Coubard |
638:c90ae1400bf2 | 66 | p_buffer->n_blocks = n_blocks; |
Vincent Coubard |
638:c90ae1400bf2 | 67 | p_buffer->block_size = block_size; |
Vincent Coubard |
638:c90ae1400bf2 | 68 | pm_mutex_init(p_buffer->p_mutex, n_blocks); |
Vincent Coubard |
638:c90ae1400bf2 | 69 | |
Vincent Coubard |
638:c90ae1400bf2 | 70 | return NRF_SUCCESS; |
Vincent Coubard |
638:c90ae1400bf2 | 71 | } |
Vincent Coubard |
638:c90ae1400bf2 | 72 | else |
Vincent Coubard |
638:c90ae1400bf2 | 73 | { |
Vincent Coubard |
638:c90ae1400bf2 | 74 | return NRF_ERROR_INVALID_PARAM; |
Vincent Coubard |
638:c90ae1400bf2 | 75 | } |
Vincent Coubard |
638:c90ae1400bf2 | 76 | } |
Vincent Coubard |
638:c90ae1400bf2 | 77 | |
Vincent Coubard |
638:c90ae1400bf2 | 78 | |
Vincent Coubard |
638:c90ae1400bf2 | 79 | uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks) |
Vincent Coubard |
638:c90ae1400bf2 | 80 | { |
Vincent Coubard |
638:c90ae1400bf2 | 81 | if (!BUFFER_IS_VALID(p_buffer)) |
Vincent Coubard |
638:c90ae1400bf2 | 82 | { |
Vincent Coubard |
638:c90ae1400bf2 | 83 | return ( BUFFER_INVALID_ID ); |
Vincent Coubard |
638:c90ae1400bf2 | 84 | } |
Vincent Coubard |
638:c90ae1400bf2 | 85 | |
Vincent Coubard |
638:c90ae1400bf2 | 86 | uint8_t first_locked_mutex = BUFFER_INVALID_ID; |
Vincent Coubard |
638:c90ae1400bf2 | 87 | |
Vincent Coubard |
638:c90ae1400bf2 | 88 | for (uint8_t i = 0; i < p_buffer->n_blocks; i++) |
Vincent Coubard |
638:c90ae1400bf2 | 89 | { |
Vincent Coubard |
638:c90ae1400bf2 | 90 | if (pm_mutex_lock(p_buffer->p_mutex, i)) |
Vincent Coubard |
638:c90ae1400bf2 | 91 | { |
Vincent Coubard |
638:c90ae1400bf2 | 92 | if (first_locked_mutex == BUFFER_INVALID_ID) |
Vincent Coubard |
638:c90ae1400bf2 | 93 | { |
Vincent Coubard |
638:c90ae1400bf2 | 94 | first_locked_mutex = i; |
Vincent Coubard |
638:c90ae1400bf2 | 95 | } |
Vincent Coubard |
638:c90ae1400bf2 | 96 | if ((i - first_locked_mutex + 1) == n_blocks) |
Vincent Coubard |
638:c90ae1400bf2 | 97 | { |
Vincent Coubard |
638:c90ae1400bf2 | 98 | return first_locked_mutex; |
Vincent Coubard |
638:c90ae1400bf2 | 99 | } |
Vincent Coubard |
638:c90ae1400bf2 | 100 | } |
Vincent Coubard |
638:c90ae1400bf2 | 101 | else if (first_locked_mutex != BUFFER_INVALID_ID) |
Vincent Coubard |
638:c90ae1400bf2 | 102 | { |
Vincent Coubard |
638:c90ae1400bf2 | 103 | for (uint8_t j = first_locked_mutex; j < i; j++) |
Vincent Coubard |
638:c90ae1400bf2 | 104 | { |
Vincent Coubard |
638:c90ae1400bf2 | 105 | pm_buffer_release(p_buffer, j); |
Vincent Coubard |
638:c90ae1400bf2 | 106 | } |
Vincent Coubard |
638:c90ae1400bf2 | 107 | first_locked_mutex = BUFFER_INVALID_ID; |
Vincent Coubard |
638:c90ae1400bf2 | 108 | } |
Vincent Coubard |
638:c90ae1400bf2 | 109 | } |
Vincent Coubard |
638:c90ae1400bf2 | 110 | |
Vincent Coubard |
638:c90ae1400bf2 | 111 | return ( BUFFER_INVALID_ID ); |
Vincent Coubard |
638:c90ae1400bf2 | 112 | } |
Vincent Coubard |
638:c90ae1400bf2 | 113 | |
Vincent Coubard |
638:c90ae1400bf2 | 114 | |
Vincent Coubard |
638:c90ae1400bf2 | 115 | uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id) |
Vincent Coubard |
638:c90ae1400bf2 | 116 | { |
Vincent Coubard |
638:c90ae1400bf2 | 117 | if (!BUFFER_IS_VALID(p_buffer)) |
Vincent Coubard |
638:c90ae1400bf2 | 118 | { |
Vincent Coubard |
638:c90ae1400bf2 | 119 | return ( NULL ); |
Vincent Coubard |
638:c90ae1400bf2 | 120 | } |
Vincent Coubard |
638:c90ae1400bf2 | 121 | |
Vincent Coubard |
638:c90ae1400bf2 | 122 | if ( (id != BUFFER_INVALID_ID) |
Vincent Coubard |
638:c90ae1400bf2 | 123 | && pm_mutex_lock_status_get(p_buffer->p_mutex, id) ) |
Vincent Coubard |
638:c90ae1400bf2 | 124 | { |
Vincent Coubard |
638:c90ae1400bf2 | 125 | return ( &p_buffer->p_memory[id*p_buffer->block_size] ); |
Vincent Coubard |
638:c90ae1400bf2 | 126 | } |
Vincent Coubard |
638:c90ae1400bf2 | 127 | else |
Vincent Coubard |
638:c90ae1400bf2 | 128 | { |
Vincent Coubard |
638:c90ae1400bf2 | 129 | return ( NULL ); |
Vincent Coubard |
638:c90ae1400bf2 | 130 | } |
Vincent Coubard |
638:c90ae1400bf2 | 131 | } |
Vincent Coubard |
638:c90ae1400bf2 | 132 | |
Vincent Coubard |
638:c90ae1400bf2 | 133 | |
Vincent Coubard |
638:c90ae1400bf2 | 134 | void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id) |
Vincent Coubard |
638:c90ae1400bf2 | 135 | { |
Vincent Coubard |
638:c90ae1400bf2 | 136 | if ( BUFFER_IS_VALID(p_buffer) |
Vincent Coubard |
638:c90ae1400bf2 | 137 | && (id != BUFFER_INVALID_ID) |
Vincent Coubard |
638:c90ae1400bf2 | 138 | && pm_mutex_lock_status_get(p_buffer->p_mutex, id)) |
Vincent Coubard |
638:c90ae1400bf2 | 139 | { |
Vincent Coubard |
638:c90ae1400bf2 | 140 | pm_mutex_unlock(p_buffer->p_mutex, id); |
Vincent Coubard |
638:c90ae1400bf2 | 141 | } |
Vincent Coubard |
638:c90ae1400bf2 | 142 | } |