None
Fork of nrf51-sdk by
source/nordic_sdk/components/ble/peer_manager/pm_buffer.h@9:118fd63123a5, 2017-01-13 (annotated)
- Committer:
- Asimov
- Date:
- Fri Jan 13 20:59:21 2017 +0000
- Revision:
- 9:118fd63123a5
- Parent:
- 0:bc2961fa1ef0
None
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 | #ifndef BUFFER_H__ |
Jonathan Austin |
0:bc2961fa1ef0 | 35 | #define BUFFER_H__ |
Jonathan Austin |
0:bc2961fa1ef0 | 36 | |
Jonathan Austin |
0:bc2961fa1ef0 | 37 | #include <stdint.h> |
Jonathan Austin |
0:bc2961fa1ef0 | 38 | #include "sdk_errors.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 39 | #include "pm_mutex.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 40 | |
Jonathan Austin |
0:bc2961fa1ef0 | 41 | |
Jonathan Austin |
0:bc2961fa1ef0 | 42 | /** |
Jonathan Austin |
0:bc2961fa1ef0 | 43 | * @defgroup pm_buffer Buffer |
Jonathan Austin |
0:bc2961fa1ef0 | 44 | * @ingroup peer_manager |
Jonathan Austin |
0:bc2961fa1ef0 | 45 | * @{ |
Jonathan Austin |
0:bc2961fa1ef0 | 46 | * @brief An internal module of @ref peer_manager. This module provides a simple buffer. |
Jonathan Austin |
0:bc2961fa1ef0 | 47 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 48 | |
Jonathan Austin |
0:bc2961fa1ef0 | 49 | |
Jonathan Austin |
0:bc2961fa1ef0 | 50 | #define BUFFER_INVALID_ID 0xFF |
Jonathan Austin |
0:bc2961fa1ef0 | 51 | |
Jonathan Austin |
0:bc2961fa1ef0 | 52 | #define PM_BUFFER_INIT(p_buffer, n_blocks, block_size, err_code) \ |
Jonathan Austin |
0:bc2961fa1ef0 | 53 | do \ |
Jonathan Austin |
0:bc2961fa1ef0 | 54 | { \ |
Jonathan Austin |
0:bc2961fa1ef0 | 55 | static uint8_t buffer_memory[(n_blocks) * (block_size)]; \ |
Jonathan Austin |
0:bc2961fa1ef0 | 56 | static uint8_t mutex_memory[MUTEX_STORAGE_SIZE(n_blocks)]; \ |
Jonathan Austin |
0:bc2961fa1ef0 | 57 | err_code = pm_buffer_init((p_buffer), \ |
Jonathan Austin |
0:bc2961fa1ef0 | 58 | buffer_memory, \ |
Jonathan Austin |
0:bc2961fa1ef0 | 59 | (n_blocks) * (block_size), \ |
Jonathan Austin |
0:bc2961fa1ef0 | 60 | mutex_memory, \ |
Jonathan Austin |
0:bc2961fa1ef0 | 61 | MUTEX_STORAGE_SIZE(n_blocks), \ |
Jonathan Austin |
0:bc2961fa1ef0 | 62 | (n_blocks), \ |
Jonathan Austin |
0:bc2961fa1ef0 | 63 | (block_size)); \ |
Jonathan Austin |
0:bc2961fa1ef0 | 64 | } while(0) |
Jonathan Austin |
0:bc2961fa1ef0 | 65 | |
Jonathan Austin |
0:bc2961fa1ef0 | 66 | |
Jonathan Austin |
0:bc2961fa1ef0 | 67 | typedef struct |
Jonathan Austin |
0:bc2961fa1ef0 | 68 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 69 | uint8_t * p_memory; /**< The storage for all buffer entries. The size of the buffer must be n_blocks*block_size. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 70 | uint8_t * p_mutex; /**< A mutex group with one mutex for each buffer entry. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 71 | uint32_t n_blocks; /**< The number of allocatable blocks in the buffer. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 72 | uint32_t block_size; /**< The size of each block in the buffer. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 73 | } pm_buffer_t; |
Jonathan Austin |
0:bc2961fa1ef0 | 74 | |
Jonathan Austin |
0:bc2961fa1ef0 | 75 | /**@brief Function for initializing a buffer instance. |
Jonathan Austin |
0:bc2961fa1ef0 | 76 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 77 | * @param[out] p_buffer The buffer instance to initialize. |
Jonathan Austin |
0:bc2961fa1ef0 | 78 | * @param[in] p_buffer_memory The memory this buffer will use. |
Jonathan Austin |
0:bc2961fa1ef0 | 79 | * @param[in] buffer_memory_size The size of p_buffer_memory. This must be at least |
Jonathan Austin |
0:bc2961fa1ef0 | 80 | * n_blocks*block_size. |
Jonathan Austin |
0:bc2961fa1ef0 | 81 | * @param[in] p_mutex_memory The memory for the mutexes. This must be at least |
Jonathan Austin |
0:bc2961fa1ef0 | 82 | * @ref MUTEX_STORAGE_SIZE(n_blocks). |
Jonathan Austin |
0:bc2961fa1ef0 | 83 | * @param[in] mutex_memory_size The size of p_mutex_memory. |
Jonathan Austin |
0:bc2961fa1ef0 | 84 | * @param[in] n_blocks The number of blocks in the buffer. |
Jonathan Austin |
0:bc2961fa1ef0 | 85 | * @param[in] block_size The size of each block. |
Jonathan Austin |
0:bc2961fa1ef0 | 86 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 87 | * @retval NRF_SUCCESS Successfully initialized buffer instance. |
Jonathan Austin |
0:bc2961fa1ef0 | 88 | * @retval NRF_ERROR_INVALID_PARAM A parameter was 0 or NULL or a size was too small. |
Jonathan Austin |
0:bc2961fa1ef0 | 89 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 90 | ret_code_t pm_buffer_init(pm_buffer_t * p_buffer, |
Jonathan Austin |
0:bc2961fa1ef0 | 91 | uint8_t * p_buffer_memory, |
Jonathan Austin |
0:bc2961fa1ef0 | 92 | uint32_t buffer_memory_size, |
Jonathan Austin |
0:bc2961fa1ef0 | 93 | uint8_t * p_mutex_memory, |
Jonathan Austin |
0:bc2961fa1ef0 | 94 | uint32_t mutex_memory_size, |
Jonathan Austin |
0:bc2961fa1ef0 | 95 | uint32_t n_blocks, |
Jonathan Austin |
0:bc2961fa1ef0 | 96 | uint32_t block_size); |
Jonathan Austin |
0:bc2961fa1ef0 | 97 | |
Jonathan Austin |
0:bc2961fa1ef0 | 98 | |
Jonathan Austin |
0:bc2961fa1ef0 | 99 | /**@brief Function for acquiring a buffer block in a buffer. |
Jonathan Austin |
0:bc2961fa1ef0 | 100 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 101 | * @param[in] p_buffer The buffer instance acquire from. |
Jonathan Austin |
0:bc2961fa1ef0 | 102 | * @param[in] n_blocks The number of contiguous blocks to acquire. |
Jonathan Austin |
0:bc2961fa1ef0 | 103 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 104 | * @return The id of the acquired block, if successful. |
Jonathan Austin |
0:bc2961fa1ef0 | 105 | * @retval BUFFER_INVALID_ID If unsuccessful. |
Jonathan Austin |
0:bc2961fa1ef0 | 106 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 107 | uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks); |
Jonathan Austin |
0:bc2961fa1ef0 | 108 | |
Jonathan Austin |
0:bc2961fa1ef0 | 109 | |
Jonathan Austin |
0:bc2961fa1ef0 | 110 | /**@brief Function for getting a pointer to a specific buffer block. |
Jonathan Austin |
0:bc2961fa1ef0 | 111 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 112 | * @param[in] p_buffer The buffer instance get from. |
Jonathan Austin |
0:bc2961fa1ef0 | 113 | * @param[in] id The id of the buffer to get the pointer for. |
Jonathan Austin |
0:bc2961fa1ef0 | 114 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 115 | * @return A pointer to the buffer for the specified id, if the id is valid. |
Jonathan Austin |
0:bc2961fa1ef0 | 116 | * @retval NULL If the id is invalid. |
Jonathan Austin |
0:bc2961fa1ef0 | 117 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 118 | uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id); |
Jonathan Austin |
0:bc2961fa1ef0 | 119 | |
Jonathan Austin |
0:bc2961fa1ef0 | 120 | |
Jonathan Austin |
0:bc2961fa1ef0 | 121 | /**@brief Function for releasing a buffer block. |
Jonathan Austin |
0:bc2961fa1ef0 | 122 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 123 | * @param[in] p_buffer The buffer instance containing the block to release. |
Jonathan Austin |
0:bc2961fa1ef0 | 124 | * @param[in] id The id of the block to release. |
Jonathan Austin |
0:bc2961fa1ef0 | 125 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 126 | void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id); |
Jonathan Austin |
0:bc2961fa1ef0 | 127 | |
Jonathan Austin |
0:bc2961fa1ef0 | 128 | |
Jonathan Austin |
0:bc2961fa1ef0 | 129 | #endif // BUFFER_H__ |
Jonathan Austin |
0:bc2961fa1ef0 | 130 | |
Jonathan Austin |
0:bc2961fa1ef0 | 131 | /** |
Jonathan Austin |
0:bc2961fa1ef0 | 132 | * @} |
Jonathan Austin |
0:bc2961fa1ef0 | 133 | */ |