Nordic stack and drivers for the mbed BLE API
Fork of nRF51822 by
TARGET_MCU_NRF51822/sdk/source/ble/peer_manager/peer_data_storage.c@640:c90ae1400bf2, 2016-09-14 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Wed Sep 14 14:39:43 2016 +0100
- Revision:
- 640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
640:c90ae1400bf2 | 1 | /* |
Vincent Coubard |
640:c90ae1400bf2 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Vincent Coubard |
640:c90ae1400bf2 | 3 | * All rights reserved. |
Vincent Coubard |
640:c90ae1400bf2 | 4 | * |
Vincent Coubard |
640:c90ae1400bf2 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Vincent Coubard |
640:c90ae1400bf2 | 6 | * are permitted provided that the following conditions are met: |
Vincent Coubard |
640:c90ae1400bf2 | 7 | * |
Vincent Coubard |
640:c90ae1400bf2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 9 | * list of conditions and the following disclaimer. |
Vincent Coubard |
640:c90ae1400bf2 | 10 | * |
Vincent Coubard |
640:c90ae1400bf2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Vincent Coubard |
640:c90ae1400bf2 | 13 | * other materials provided with the distribution. |
Vincent Coubard |
640:c90ae1400bf2 | 14 | * |
Vincent Coubard |
640:c90ae1400bf2 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Vincent Coubard |
640:c90ae1400bf2 | 16 | * contributors to this software may be used to endorse or promote products |
Vincent Coubard |
640:c90ae1400bf2 | 17 | * derived from this software without specific prior written permission. |
Vincent Coubard |
640:c90ae1400bf2 | 18 | * |
Vincent Coubard |
640:c90ae1400bf2 | 19 | * |
Vincent Coubard |
640:c90ae1400bf2 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Vincent Coubard |
640:c90ae1400bf2 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
640:c90ae1400bf2 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
640:c90ae1400bf2 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Vincent Coubard |
640:c90ae1400bf2 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
640:c90ae1400bf2 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
640:c90ae1400bf2 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Vincent Coubard |
640:c90ae1400bf2 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
640:c90ae1400bf2 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
640:c90ae1400bf2 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
640:c90ae1400bf2 | 30 | * |
Vincent Coubard |
640:c90ae1400bf2 | 31 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 32 | |
Vincent Coubard |
640:c90ae1400bf2 | 33 | |
Vincent Coubard |
640:c90ae1400bf2 | 34 | #include "peer_data_storage.h" |
Vincent Coubard |
640:c90ae1400bf2 | 35 | |
Vincent Coubard |
640:c90ae1400bf2 | 36 | #include <stdint.h> |
Vincent Coubard |
640:c90ae1400bf2 | 37 | #include <string.h> |
Vincent Coubard |
640:c90ae1400bf2 | 38 | #include "sdk_errors.h" |
Vincent Coubard |
640:c90ae1400bf2 | 39 | #include "peer_manager_types.h" |
Vincent Coubard |
640:c90ae1400bf2 | 40 | #include "peer_id.h" |
Vincent Coubard |
640:c90ae1400bf2 | 41 | #include "peer_data.h" |
Vincent Coubard |
640:c90ae1400bf2 | 42 | #include "fds.h" |
Vincent Coubard |
640:c90ae1400bf2 | 43 | |
Vincent Coubard |
640:c90ae1400bf2 | 44 | #define MAX_REGISTRANTS 6 /**< The number of user that can register with the module. */ |
Vincent Coubard |
640:c90ae1400bf2 | 45 | |
Vincent Coubard |
640:c90ae1400bf2 | 46 | #define MODULE_INITIALIZED (m_pds.n_registrants > 0) /**< Expression which is true when the module is initialized. */ |
Vincent Coubard |
640:c90ae1400bf2 | 47 | |
Vincent Coubard |
640:c90ae1400bf2 | 48 | /**@brief Macro for verifying that the module is initialized. It will cause the function to return |
Vincent Coubard |
640:c90ae1400bf2 | 49 | * @ref NRF_ERROR_INVALID_STATE if not. |
Vincent Coubard |
640:c90ae1400bf2 | 50 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 51 | #define VERIFY_MODULE_INITIALIZED() \ |
Vincent Coubard |
640:c90ae1400bf2 | 52 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 53 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 54 | if (!MODULE_INITIALIZED) \ |
Vincent Coubard |
640:c90ae1400bf2 | 55 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 56 | return NRF_ERROR_INVALID_STATE; \ |
Vincent Coubard |
640:c90ae1400bf2 | 57 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 58 | } while(0) |
Vincent Coubard |
640:c90ae1400bf2 | 59 | |
Vincent Coubard |
640:c90ae1400bf2 | 60 | |
Vincent Coubard |
640:c90ae1400bf2 | 61 | /**@brief Macro for verifying that the module is initialized. It will cause the function to return |
Vincent Coubard |
640:c90ae1400bf2 | 62 | * if not. |
Vincent Coubard |
640:c90ae1400bf2 | 63 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 64 | #define VERIFY_MODULE_INITIALIZED_VOID() \ |
Vincent Coubard |
640:c90ae1400bf2 | 65 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 66 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 67 | if (!MODULE_INITIALIZED) \ |
Vincent Coubard |
640:c90ae1400bf2 | 68 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 69 | return; \ |
Vincent Coubard |
640:c90ae1400bf2 | 70 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 71 | } while(0) |
Vincent Coubard |
640:c90ae1400bf2 | 72 | |
Vincent Coubard |
640:c90ae1400bf2 | 73 | |
Vincent Coubard |
640:c90ae1400bf2 | 74 | /**@brief Macro for verifying that the param is not NULL. It will cause the function to return |
Vincent Coubard |
640:c90ae1400bf2 | 75 | * if not. |
Vincent Coubard |
640:c90ae1400bf2 | 76 | * |
Vincent Coubard |
640:c90ae1400bf2 | 77 | * @param[in] param The variable to check if is NULL. |
Vincent Coubard |
640:c90ae1400bf2 | 78 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 79 | #define VERIFY_PARAM_NOT_NULL(param) \ |
Vincent Coubard |
640:c90ae1400bf2 | 80 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 81 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 82 | if (param == NULL) \ |
Vincent Coubard |
640:c90ae1400bf2 | 83 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 84 | return NRF_ERROR_NULL; \ |
Vincent Coubard |
640:c90ae1400bf2 | 85 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 86 | } while(0) |
Vincent Coubard |
640:c90ae1400bf2 | 87 | |
Vincent Coubard |
640:c90ae1400bf2 | 88 | |
Vincent Coubard |
640:c90ae1400bf2 | 89 | /**@brief Macro for verifying that param is not zero. It will cause the function to return |
Vincent Coubard |
640:c90ae1400bf2 | 90 | * if not. |
Vincent Coubard |
640:c90ae1400bf2 | 91 | * |
Vincent Coubard |
640:c90ae1400bf2 | 92 | * @param[in] param The variable to check if is zero. |
Vincent Coubard |
640:c90ae1400bf2 | 93 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 94 | #define VERIFY_PARAM_NOT_ZERO(param) \ |
Vincent Coubard |
640:c90ae1400bf2 | 95 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 96 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 97 | if (param == 0) \ |
Vincent Coubard |
640:c90ae1400bf2 | 98 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 99 | return NRF_ERROR_NULL; \ |
Vincent Coubard |
640:c90ae1400bf2 | 100 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 101 | } while(0) |
Vincent Coubard |
640:c90ae1400bf2 | 102 | |
Vincent Coubard |
640:c90ae1400bf2 | 103 | |
Vincent Coubard |
640:c90ae1400bf2 | 104 | /**@brief Macro for verifying that the peer id is within a valid range |
Vincent Coubard |
640:c90ae1400bf2 | 105 | * |
Vincent Coubard |
640:c90ae1400bf2 | 106 | * @param[in] id The peer data id to check. |
Vincent Coubard |
640:c90ae1400bf2 | 107 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 108 | #define VERIFY_PEER_ID_IN_RANGE(id) \ |
Vincent Coubard |
640:c90ae1400bf2 | 109 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 110 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 111 | if ((id >= PM_PEER_ID_N_AVAILABLE_IDS)) \ |
Vincent Coubard |
640:c90ae1400bf2 | 112 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 113 | return NRF_ERROR_INVALID_PARAM; \ |
Vincent Coubard |
640:c90ae1400bf2 | 114 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 115 | } while (0) |
Vincent Coubard |
640:c90ae1400bf2 | 116 | |
Vincent Coubard |
640:c90ae1400bf2 | 117 | |
Vincent Coubard |
640:c90ae1400bf2 | 118 | /**@brief Macro for verifying that the peer data id is withing a valid range |
Vincent Coubard |
640:c90ae1400bf2 | 119 | * |
Vincent Coubard |
640:c90ae1400bf2 | 120 | * @param[in] id The peer data id to check. |
Vincent Coubard |
640:c90ae1400bf2 | 121 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 122 | #define VERIFY_PEER_DATA_ID_IN_RANGE(id) \ |
Vincent Coubard |
640:c90ae1400bf2 | 123 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 124 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 125 | if (!PM_PEER_DATA_ID_IS_VALID(id)) \ |
Vincent Coubard |
640:c90ae1400bf2 | 126 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 127 | return NRF_ERROR_INVALID_PARAM; \ |
Vincent Coubard |
640:c90ae1400bf2 | 128 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 129 | } while (0) |
Vincent Coubard |
640:c90ae1400bf2 | 130 | |
Vincent Coubard |
640:c90ae1400bf2 | 131 | |
Vincent Coubard |
640:c90ae1400bf2 | 132 | #define PEER_IDS_INITIALIZE() \ |
Vincent Coubard |
640:c90ae1400bf2 | 133 | do \ |
Vincent Coubard |
640:c90ae1400bf2 | 134 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 135 | if (!m_pds.peer_ids_initialized) \ |
Vincent Coubard |
640:c90ae1400bf2 | 136 | { \ |
Vincent Coubard |
640:c90ae1400bf2 | 137 | peer_ids_init(); \ |
Vincent Coubard |
640:c90ae1400bf2 | 138 | } \ |
Vincent Coubard |
640:c90ae1400bf2 | 139 | } while (0) |
Vincent Coubard |
640:c90ae1400bf2 | 140 | |
Vincent Coubard |
640:c90ae1400bf2 | 141 | |
Vincent Coubard |
640:c90ae1400bf2 | 142 | typedef struct |
Vincent Coubard |
640:c90ae1400bf2 | 143 | { |
Vincent Coubard |
640:c90ae1400bf2 | 144 | bool peer_ids_initialized; |
Vincent Coubard |
640:c90ae1400bf2 | 145 | pds_evt_handler_t evt_handlers[MAX_REGISTRANTS]; |
Vincent Coubard |
640:c90ae1400bf2 | 146 | uint8_t n_registrants; |
Vincent Coubard |
640:c90ae1400bf2 | 147 | } pds_t; |
Vincent Coubard |
640:c90ae1400bf2 | 148 | |
Vincent Coubard |
640:c90ae1400bf2 | 149 | static pds_t m_pds = {.n_registrants = 0}; |
Vincent Coubard |
640:c90ae1400bf2 | 150 | |
Vincent Coubard |
640:c90ae1400bf2 | 151 | static void internal_state_reset(pds_t * p_pds) |
Vincent Coubard |
640:c90ae1400bf2 | 152 | { |
Vincent Coubard |
640:c90ae1400bf2 | 153 | memset(p_pds, 0, sizeof(pds_t)); |
Vincent Coubard |
640:c90ae1400bf2 | 154 | } |
Vincent Coubard |
640:c90ae1400bf2 | 155 | |
Vincent Coubard |
640:c90ae1400bf2 | 156 | /**@brief Function for dispatching outbound events to all registered event handlers. |
Vincent Coubard |
640:c90ae1400bf2 | 157 | * |
Vincent Coubard |
640:c90ae1400bf2 | 158 | * @param[in] p_event The event to dispatch. |
Vincent Coubard |
640:c90ae1400bf2 | 159 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 160 | static void pds_evt_send(pds_evt_t * p_event) |
Vincent Coubard |
640:c90ae1400bf2 | 161 | { |
Vincent Coubard |
640:c90ae1400bf2 | 162 | for (int i = 0; i < m_pds.n_registrants; i++) |
Vincent Coubard |
640:c90ae1400bf2 | 163 | { |
Vincent Coubard |
640:c90ae1400bf2 | 164 | m_pds.evt_handlers[i](p_event); |
Vincent Coubard |
640:c90ae1400bf2 | 165 | } |
Vincent Coubard |
640:c90ae1400bf2 | 166 | } |
Vincent Coubard |
640:c90ae1400bf2 | 167 | |
Vincent Coubard |
640:c90ae1400bf2 | 168 | /**@brief Function to convert peer id to instance id |
Vincent Coubard |
640:c90ae1400bf2 | 169 | * |
Vincent Coubard |
640:c90ae1400bf2 | 170 | * @param[in] peer_id Peer id to convert to instance id |
Vincent Coubard |
640:c90ae1400bf2 | 171 | * |
Vincent Coubard |
640:c90ae1400bf2 | 172 | * @return Value as instance id |
Vincent Coubard |
640:c90ae1400bf2 | 173 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 174 | static fds_instance_id_t convert_peer_id_to_instance_id(pm_peer_id_t peer_id) |
Vincent Coubard |
640:c90ae1400bf2 | 175 | { |
Vincent Coubard |
640:c90ae1400bf2 | 176 | return (fds_instance_id_t)(peer_id + peer_id_to_instance_id); |
Vincent Coubard |
640:c90ae1400bf2 | 177 | } |
Vincent Coubard |
640:c90ae1400bf2 | 178 | |
Vincent Coubard |
640:c90ae1400bf2 | 179 | /**@brief Function to convert peer data id to type id |
Vincent Coubard |
640:c90ae1400bf2 | 180 | * |
Vincent Coubard |
640:c90ae1400bf2 | 181 | * @param[in] peer_data_id Peer data id to convert to type_id |
Vincent Coubard |
640:c90ae1400bf2 | 182 | * |
Vincent Coubard |
640:c90ae1400bf2 | 183 | * @return Value as type id |
Vincent Coubard |
640:c90ae1400bf2 | 184 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 185 | static fds_type_id_t convert_peer_data_id_to_type_id(pm_peer_data_id_t peer_data_id) |
Vincent Coubard |
640:c90ae1400bf2 | 186 | { |
Vincent Coubard |
640:c90ae1400bf2 | 187 | return (fds_type_id_t)peer_data_id + (fds_type_id_t)peer_data_id_to_type_id; |
Vincent Coubard |
640:c90ae1400bf2 | 188 | } |
Vincent Coubard |
640:c90ae1400bf2 | 189 | |
Vincent Coubard |
640:c90ae1400bf2 | 190 | |
Vincent Coubard |
640:c90ae1400bf2 | 191 | /**@brief Function to convert peer data id to type id |
Vincent Coubard |
640:c90ae1400bf2 | 192 | * |
Vincent Coubard |
640:c90ae1400bf2 | 193 | * @param[in] peer_data_id Peer data id to convert to type_id |
Vincent Coubard |
640:c90ae1400bf2 | 194 | * |
Vincent Coubard |
640:c90ae1400bf2 | 195 | * @return Value as type id |
Vincent Coubard |
640:c90ae1400bf2 | 196 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 197 | static pm_peer_id_t convert_instance_id_to_peer_id(fds_instance_id_t instance_id) |
Vincent Coubard |
640:c90ae1400bf2 | 198 | { |
Vincent Coubard |
640:c90ae1400bf2 | 199 | return (pm_peer_id_t)(instance_id + instance_id_to_peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 200 | } |
Vincent Coubard |
640:c90ae1400bf2 | 201 | |
Vincent Coubard |
640:c90ae1400bf2 | 202 | |
Vincent Coubard |
640:c90ae1400bf2 | 203 | /**@brief Function to type id to peer data id |
Vincent Coubard |
640:c90ae1400bf2 | 204 | * |
Vincent Coubard |
640:c90ae1400bf2 | 205 | * @param[in] type_id Type id to convert to peer data id |
Vincent Coubard |
640:c90ae1400bf2 | 206 | * |
Vincent Coubard |
640:c90ae1400bf2 | 207 | * @return Value as peer data id |
Vincent Coubard |
640:c90ae1400bf2 | 208 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 209 | static pm_peer_data_id_t convert_type_id_to_peer_data_id(fds_type_id_t type_id) |
Vincent Coubard |
640:c90ae1400bf2 | 210 | { |
Vincent Coubard |
640:c90ae1400bf2 | 211 | return (pm_peer_data_id_t)(type_id + instance_id_to_peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 212 | } |
Vincent Coubard |
640:c90ae1400bf2 | 213 | |
Vincent Coubard |
640:c90ae1400bf2 | 214 | |
Vincent Coubard |
640:c90ae1400bf2 | 215 | static ret_code_t find_fds_item(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 216 | pm_peer_data_id_t data_id, |
Vincent Coubard |
640:c90ae1400bf2 | 217 | fds_record_desc_t * const p_desc) |
Vincent Coubard |
640:c90ae1400bf2 | 218 | { |
Vincent Coubard |
640:c90ae1400bf2 | 219 | fds_find_token_t find_tok; |
Vincent Coubard |
640:c90ae1400bf2 | 220 | |
Vincent Coubard |
640:c90ae1400bf2 | 221 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 222 | VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 223 | // pp_record verified outside |
Vincent Coubard |
640:c90ae1400bf2 | 224 | |
Vincent Coubard |
640:c90ae1400bf2 | 225 | fds_type_id_t type_id = convert_peer_data_id_to_type_id(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 226 | fds_instance_id_t instance_id = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 227 | |
Vincent Coubard |
640:c90ae1400bf2 | 228 | return fds_find(type_id, instance_id, p_desc, &find_tok); |
Vincent Coubard |
640:c90ae1400bf2 | 229 | } |
Vincent Coubard |
640:c90ae1400bf2 | 230 | |
Vincent Coubard |
640:c90ae1400bf2 | 231 | |
Vincent Coubard |
640:c90ae1400bf2 | 232 | static void peer_ids_init() |
Vincent Coubard |
640:c90ae1400bf2 | 233 | { |
Vincent Coubard |
640:c90ae1400bf2 | 234 | fds_record_t record; |
Vincent Coubard |
640:c90ae1400bf2 | 235 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 236 | fds_find_token_t find_tok; |
Vincent Coubard |
640:c90ae1400bf2 | 237 | fds_type_id_t const type_id = convert_peer_data_id_to_type_id(PM_PEER_DATA_ID_BONDING); |
Vincent Coubard |
640:c90ae1400bf2 | 238 | pm_peer_id_t peer_id; |
Vincent Coubard |
640:c90ae1400bf2 | 239 | |
Vincent Coubard |
640:c90ae1400bf2 | 240 | if (!m_pds.peer_ids_initialized) |
Vincent Coubard |
640:c90ae1400bf2 | 241 | { |
Vincent Coubard |
640:c90ae1400bf2 | 242 | while(fds_find_by_type(type_id, &record_desc, &find_tok) == NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 243 | { |
Vincent Coubard |
640:c90ae1400bf2 | 244 | fds_open(&record_desc, &record); |
Vincent Coubard |
640:c90ae1400bf2 | 245 | fds_close(&record_desc); |
Vincent Coubard |
640:c90ae1400bf2 | 246 | peer_id = convert_instance_id_to_peer_id(record.header.ic.instance); |
Vincent Coubard |
640:c90ae1400bf2 | 247 | peer_id_allocate(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 248 | } |
Vincent Coubard |
640:c90ae1400bf2 | 249 | |
Vincent Coubard |
640:c90ae1400bf2 | 250 | m_pds.peer_ids_initialized = true; |
Vincent Coubard |
640:c90ae1400bf2 | 251 | } |
Vincent Coubard |
640:c90ae1400bf2 | 252 | } |
Vincent Coubard |
640:c90ae1400bf2 | 253 | |
Vincent Coubard |
640:c90ae1400bf2 | 254 | //uint32_t size_pad_to_mult_of_four(uint32_t unpadded_size) |
Vincent Coubard |
640:c90ae1400bf2 | 255 | //{ |
Vincent Coubard |
640:c90ae1400bf2 | 256 | // return (unpadded_size + 3) & 3; |
Vincent Coubard |
640:c90ae1400bf2 | 257 | //} |
Vincent Coubard |
640:c90ae1400bf2 | 258 | |
Vincent Coubard |
640:c90ae1400bf2 | 259 | static void fds_evt_handler(ret_code_t result, |
Vincent Coubard |
640:c90ae1400bf2 | 260 | fds_cmd_id_t cmd, |
Vincent Coubard |
640:c90ae1400bf2 | 261 | fds_record_id_t record_id, |
Vincent Coubard |
640:c90ae1400bf2 | 262 | fds_record_key_t record_key |
Vincent Coubard |
640:c90ae1400bf2 | 263 | /*fds_record_t const * const p_record*/) |
Vincent Coubard |
640:c90ae1400bf2 | 264 | { |
Vincent Coubard |
640:c90ae1400bf2 | 265 | pds_evt_t evt; |
Vincent Coubard |
640:c90ae1400bf2 | 266 | switch(cmd) |
Vincent Coubard |
640:c90ae1400bf2 | 267 | { |
Vincent Coubard |
640:c90ae1400bf2 | 268 | case FDS_CMD_INIT: |
Vincent Coubard |
640:c90ae1400bf2 | 269 | |
Vincent Coubard |
640:c90ae1400bf2 | 270 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 271 | |
Vincent Coubard |
640:c90ae1400bf2 | 272 | case FDS_CMD_UPDATE: |
Vincent Coubard |
640:c90ae1400bf2 | 273 | case FDS_CMD_WRITE: |
Vincent Coubard |
640:c90ae1400bf2 | 274 | evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
Vincent Coubard |
640:c90ae1400bf2 | 275 | evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_STORED : PDS_EVT_ERROR_STORE; |
Vincent Coubard |
640:c90ae1400bf2 | 276 | evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
Vincent Coubard |
640:c90ae1400bf2 | 277 | evt.store_token = record_id; |
Vincent Coubard |
640:c90ae1400bf2 | 278 | pds_evt_send(&evt); |
Vincent Coubard |
640:c90ae1400bf2 | 279 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 280 | |
Vincent Coubard |
640:c90ae1400bf2 | 281 | case FDS_CMD_CLEAR: |
Vincent Coubard |
640:c90ae1400bf2 | 282 | evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
Vincent Coubard |
640:c90ae1400bf2 | 283 | evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_CLEARED : PDS_EVT_ERROR_CLEAR; |
Vincent Coubard |
640:c90ae1400bf2 | 284 | evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
Vincent Coubard |
640:c90ae1400bf2 | 285 | evt.store_token = record_id; |
Vincent Coubard |
640:c90ae1400bf2 | 286 | pds_evt_send(&evt); |
Vincent Coubard |
640:c90ae1400bf2 | 287 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 288 | |
Vincent Coubard |
640:c90ae1400bf2 | 289 | case FDS_CMD_CLEAR_INST: |
Vincent Coubard |
640:c90ae1400bf2 | 290 | { |
Vincent Coubard |
640:c90ae1400bf2 | 291 | if ((record_key.type == FDS_TYPE_ID_INVALID) && |
Vincent Coubard |
640:c90ae1400bf2 | 292 | (record_key.instance != FDS_TYPE_ID_INVALID)) |
Vincent Coubard |
640:c90ae1400bf2 | 293 | { |
Vincent Coubard |
640:c90ae1400bf2 | 294 | pm_peer_id_t peer_id = convert_instance_id_to_peer_id(record_key.instance); |
Vincent Coubard |
640:c90ae1400bf2 | 295 | |
Vincent Coubard |
640:c90ae1400bf2 | 296 | evt.peer_id = peer_id; |
Vincent Coubard |
640:c90ae1400bf2 | 297 | evt.data_id = PM_PEER_DATA_ID_INVALID; |
Vincent Coubard |
640:c90ae1400bf2 | 298 | if (result == NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 299 | { |
Vincent Coubard |
640:c90ae1400bf2 | 300 | evt.evt_id = PDS_EVT_PEER_ID_CLEAR; |
Vincent Coubard |
640:c90ae1400bf2 | 301 | peer_id_free(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 302 | } |
Vincent Coubard |
640:c90ae1400bf2 | 303 | else |
Vincent Coubard |
640:c90ae1400bf2 | 304 | { |
Vincent Coubard |
640:c90ae1400bf2 | 305 | evt.evt_id = PDS_EVT_ERROR_PEER_ID_CLEAR; |
Vincent Coubard |
640:c90ae1400bf2 | 306 | } |
Vincent Coubard |
640:c90ae1400bf2 | 307 | } |
Vincent Coubard |
640:c90ae1400bf2 | 308 | else |
Vincent Coubard |
640:c90ae1400bf2 | 309 | { |
Vincent Coubard |
640:c90ae1400bf2 | 310 | // TODO: Not supported yet (clear many without clearing peer_id) |
Vincent Coubard |
640:c90ae1400bf2 | 311 | } |
Vincent Coubard |
640:c90ae1400bf2 | 312 | |
Vincent Coubard |
640:c90ae1400bf2 | 313 | pds_evt_send(&evt); |
Vincent Coubard |
640:c90ae1400bf2 | 314 | } |
Vincent Coubard |
640:c90ae1400bf2 | 315 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 316 | |
Vincent Coubard |
640:c90ae1400bf2 | 317 | case FDS_CMD_GC: |
Vincent Coubard |
640:c90ae1400bf2 | 318 | evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
Vincent Coubard |
640:c90ae1400bf2 | 319 | evt.evt_id = PDS_EVT_COMPRESSED; |
Vincent Coubard |
640:c90ae1400bf2 | 320 | evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
Vincent Coubard |
640:c90ae1400bf2 | 321 | evt.store_token = record_id; |
Vincent Coubard |
640:c90ae1400bf2 | 322 | pds_evt_send(&evt); |
Vincent Coubard |
640:c90ae1400bf2 | 323 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 324 | |
Vincent Coubard |
640:c90ae1400bf2 | 325 | default: |
Vincent Coubard |
640:c90ae1400bf2 | 326 | |
Vincent Coubard |
640:c90ae1400bf2 | 327 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 328 | } |
Vincent Coubard |
640:c90ae1400bf2 | 329 | } |
Vincent Coubard |
640:c90ae1400bf2 | 330 | |
Vincent Coubard |
640:c90ae1400bf2 | 331 | |
Vincent Coubard |
640:c90ae1400bf2 | 332 | ret_code_t pds_register(pds_evt_handler_t evt_handler) |
Vincent Coubard |
640:c90ae1400bf2 | 333 | { |
Vincent Coubard |
640:c90ae1400bf2 | 334 | if (m_pds.n_registrants >= MAX_REGISTRANTS) |
Vincent Coubard |
640:c90ae1400bf2 | 335 | { |
Vincent Coubard |
640:c90ae1400bf2 | 336 | return NRF_ERROR_NO_MEM; |
Vincent Coubard |
640:c90ae1400bf2 | 337 | } |
Vincent Coubard |
640:c90ae1400bf2 | 338 | |
Vincent Coubard |
640:c90ae1400bf2 | 339 | VERIFY_PARAM_NOT_NULL(evt_handler); |
Vincent Coubard |
640:c90ae1400bf2 | 340 | |
Vincent Coubard |
640:c90ae1400bf2 | 341 | if (!MODULE_INITIALIZED) |
Vincent Coubard |
640:c90ae1400bf2 | 342 | { |
Vincent Coubard |
640:c90ae1400bf2 | 343 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 344 | internal_state_reset(&m_pds); |
Vincent Coubard |
640:c90ae1400bf2 | 345 | peer_id_init(); |
Vincent Coubard |
640:c90ae1400bf2 | 346 | |
Vincent Coubard |
640:c90ae1400bf2 | 347 | fds_cb_t cb = fds_evt_handler; |
Vincent Coubard |
640:c90ae1400bf2 | 348 | retval = fds_register(cb); |
Vincent Coubard |
640:c90ae1400bf2 | 349 | if(retval != NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 350 | { |
Vincent Coubard |
640:c90ae1400bf2 | 351 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 352 | } |
Vincent Coubard |
640:c90ae1400bf2 | 353 | |
Vincent Coubard |
640:c90ae1400bf2 | 354 | retval = fds_init(); |
Vincent Coubard |
640:c90ae1400bf2 | 355 | if(retval != NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 356 | { |
Vincent Coubard |
640:c90ae1400bf2 | 357 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 358 | } |
Vincent Coubard |
640:c90ae1400bf2 | 359 | } |
Vincent Coubard |
640:c90ae1400bf2 | 360 | |
Vincent Coubard |
640:c90ae1400bf2 | 361 | m_pds.evt_handlers[m_pds.n_registrants] = evt_handler; |
Vincent Coubard |
640:c90ae1400bf2 | 362 | m_pds.n_registrants += 1; |
Vincent Coubard |
640:c90ae1400bf2 | 363 | |
Vincent Coubard |
640:c90ae1400bf2 | 364 | return NRF_SUCCESS; |
Vincent Coubard |
640:c90ae1400bf2 | 365 | |
Vincent Coubard |
640:c90ae1400bf2 | 366 | } |
Vincent Coubard |
640:c90ae1400bf2 | 367 | |
Vincent Coubard |
640:c90ae1400bf2 | 368 | |
Vincent Coubard |
640:c90ae1400bf2 | 369 | ret_code_t pds_peer_data_read_ptr_get(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 370 | pm_peer_data_id_t data_id, |
Vincent Coubard |
640:c90ae1400bf2 | 371 | pm_peer_data_flash_t * p_data, |
Vincent Coubard |
640:c90ae1400bf2 | 372 | pm_store_token_t * p_token) |
Vincent Coubard |
640:c90ae1400bf2 | 373 | { |
Vincent Coubard |
640:c90ae1400bf2 | 374 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 375 | |
Vincent Coubard |
640:c90ae1400bf2 | 376 | fds_record_t record; |
Vincent Coubard |
640:c90ae1400bf2 | 377 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 378 | |
Vincent Coubard |
640:c90ae1400bf2 | 379 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 380 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 381 | VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 382 | |
Vincent Coubard |
640:c90ae1400bf2 | 383 | retval = find_fds_item(peer_id, data_id, &record_desc); |
Vincent Coubard |
640:c90ae1400bf2 | 384 | if (retval != NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 385 | { |
Vincent Coubard |
640:c90ae1400bf2 | 386 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 387 | } |
Vincent Coubard |
640:c90ae1400bf2 | 388 | |
Vincent Coubard |
640:c90ae1400bf2 | 389 | // Shouldn't fail, unless record is cleared. |
Vincent Coubard |
640:c90ae1400bf2 | 390 | fds_open(&record_desc, &record); |
Vincent Coubard |
640:c90ae1400bf2 | 391 | // No need to keep it open, since we are not reading. |
Vincent Coubard |
640:c90ae1400bf2 | 392 | fds_close(&record_desc); |
Vincent Coubard |
640:c90ae1400bf2 | 393 | |
Vincent Coubard |
640:c90ae1400bf2 | 394 | //NRF_LOG_PRINTF("Found item with peer_id: %d, data_id: %d, Address: %p\r\n", record.p_data); |
Vincent Coubard |
640:c90ae1400bf2 | 395 | |
Vincent Coubard |
640:c90ae1400bf2 | 396 | if (p_data != NULL) |
Vincent Coubard |
640:c90ae1400bf2 | 397 | { |
Vincent Coubard |
640:c90ae1400bf2 | 398 | p_data->data_type = data_id; |
Vincent Coubard |
640:c90ae1400bf2 | 399 | p_data->length_words = record.header.tl.length_words; |
Vincent Coubard |
640:c90ae1400bf2 | 400 | |
Vincent Coubard |
640:c90ae1400bf2 | 401 | p_data->data.p_application_data = (uint8_t const*)record.p_data; |
Vincent Coubard |
640:c90ae1400bf2 | 402 | } |
Vincent Coubard |
640:c90ae1400bf2 | 403 | |
Vincent Coubard |
640:c90ae1400bf2 | 404 | if (p_token != NULL) |
Vincent Coubard |
640:c90ae1400bf2 | 405 | { |
Vincent Coubard |
640:c90ae1400bf2 | 406 | *p_token = (uint32_t)record.header.id; |
Vincent Coubard |
640:c90ae1400bf2 | 407 | } |
Vincent Coubard |
640:c90ae1400bf2 | 408 | |
Vincent Coubard |
640:c90ae1400bf2 | 409 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 410 | } |
Vincent Coubard |
640:c90ae1400bf2 | 411 | |
Vincent Coubard |
640:c90ae1400bf2 | 412 | |
Vincent Coubard |
640:c90ae1400bf2 | 413 | ret_code_t pds_peer_data_lock(pm_store_token_t store_token) |
Vincent Coubard |
640:c90ae1400bf2 | 414 | { |
Vincent Coubard |
640:c90ae1400bf2 | 415 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 416 | VERIFY_PARAM_NOT_ZERO(store_token); |
Vincent Coubard |
640:c90ae1400bf2 | 417 | |
Vincent Coubard |
640:c90ae1400bf2 | 418 | // TODO: Not implemented yet in fds |
Vincent Coubard |
640:c90ae1400bf2 | 419 | |
Vincent Coubard |
640:c90ae1400bf2 | 420 | return NRF_SUCCESS; |
Vincent Coubard |
640:c90ae1400bf2 | 421 | } |
Vincent Coubard |
640:c90ae1400bf2 | 422 | |
Vincent Coubard |
640:c90ae1400bf2 | 423 | |
Vincent Coubard |
640:c90ae1400bf2 | 424 | ret_code_t pds_peer_data_verify(pm_store_token_t store_token) |
Vincent Coubard |
640:c90ae1400bf2 | 425 | { |
Vincent Coubard |
640:c90ae1400bf2 | 426 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 427 | VERIFY_PARAM_NOT_ZERO(store_token); |
Vincent Coubard |
640:c90ae1400bf2 | 428 | |
Vincent Coubard |
640:c90ae1400bf2 | 429 | // TODO: Not implemented yet in fds |
Vincent Coubard |
640:c90ae1400bf2 | 430 | |
Vincent Coubard |
640:c90ae1400bf2 | 431 | return NRF_SUCCESS; |
Vincent Coubard |
640:c90ae1400bf2 | 432 | } |
Vincent Coubard |
640:c90ae1400bf2 | 433 | |
Vincent Coubard |
640:c90ae1400bf2 | 434 | |
Vincent Coubard |
640:c90ae1400bf2 | 435 | ret_code_t pds_peer_data_read(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 436 | pm_peer_data_id_t data_id, |
Vincent Coubard |
640:c90ae1400bf2 | 437 | pm_peer_data_t * p_data, |
Vincent Coubard |
640:c90ae1400bf2 | 438 | fds_length_t * p_len_words) |
Vincent Coubard |
640:c90ae1400bf2 | 439 | { |
Vincent Coubard |
640:c90ae1400bf2 | 440 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 441 | VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 442 | VERIFY_PARAM_NOT_NULL(p_len_words); |
Vincent Coubard |
640:c90ae1400bf2 | 443 | VERIFY_PARAM_NOT_NULL(p_data); |
Vincent Coubard |
640:c90ae1400bf2 | 444 | |
Vincent Coubard |
640:c90ae1400bf2 | 445 | ret_code_t err_code; |
Vincent Coubard |
640:c90ae1400bf2 | 446 | pm_peer_data_flash_t peer_data_flash; |
Vincent Coubard |
640:c90ae1400bf2 | 447 | |
Vincent Coubard |
640:c90ae1400bf2 | 448 | err_code = pds_peer_data_read_ptr_get(peer_id, data_id, &peer_data_flash, NULL); |
Vincent Coubard |
640:c90ae1400bf2 | 449 | |
Vincent Coubard |
640:c90ae1400bf2 | 450 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 451 | { |
Vincent Coubard |
640:c90ae1400bf2 | 452 | return err_code; |
Vincent Coubard |
640:c90ae1400bf2 | 453 | } |
Vincent Coubard |
640:c90ae1400bf2 | 454 | |
Vincent Coubard |
640:c90ae1400bf2 | 455 | if ((*p_len_words) == 0) |
Vincent Coubard |
640:c90ae1400bf2 | 456 | { |
Vincent Coubard |
640:c90ae1400bf2 | 457 | (*p_len_words) = peer_data_flash.length_words; |
Vincent Coubard |
640:c90ae1400bf2 | 458 | return NRF_SUCCESS; |
Vincent Coubard |
640:c90ae1400bf2 | 459 | } |
Vincent Coubard |
640:c90ae1400bf2 | 460 | else if ((*p_len_words) < peer_data_flash.length_words) |
Vincent Coubard |
640:c90ae1400bf2 | 461 | { |
Vincent Coubard |
640:c90ae1400bf2 | 462 | return NRF_ERROR_NO_MEM; |
Vincent Coubard |
640:c90ae1400bf2 | 463 | } |
Vincent Coubard |
640:c90ae1400bf2 | 464 | |
Vincent Coubard |
640:c90ae1400bf2 | 465 | VERIFY_PARAM_NOT_NULL(p_data->data.p_application_data); |
Vincent Coubard |
640:c90ae1400bf2 | 466 | |
Vincent Coubard |
640:c90ae1400bf2 | 467 | err_code = peer_data_deserialize(&peer_data_flash, p_data); |
Vincent Coubard |
640:c90ae1400bf2 | 468 | |
Vincent Coubard |
640:c90ae1400bf2 | 469 | return err_code; |
Vincent Coubard |
640:c90ae1400bf2 | 470 | } |
Vincent Coubard |
640:c90ae1400bf2 | 471 | |
Vincent Coubard |
640:c90ae1400bf2 | 472 | |
Vincent Coubard |
640:c90ae1400bf2 | 473 | ret_code_t pds_peer_data_write_prepare(pm_peer_data_const_t const * p_peer_data, |
Vincent Coubard |
640:c90ae1400bf2 | 474 | pm_prepare_token_t * p_prepare_token) |
Vincent Coubard |
640:c90ae1400bf2 | 475 | { |
Vincent Coubard |
640:c90ae1400bf2 | 476 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 477 | |
Vincent Coubard |
640:c90ae1400bf2 | 478 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 479 | VERIFY_PARAM_NOT_NULL(p_peer_data); |
Vincent Coubard |
640:c90ae1400bf2 | 480 | VERIFY_PARAM_NOT_NULL(p_prepare_token); |
Vincent Coubard |
640:c90ae1400bf2 | 481 | VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 482 | |
Vincent Coubard |
640:c90ae1400bf2 | 483 | retval = fds_reserve((fds_write_token_t*)p_prepare_token, p_peer_data->length_words); |
Vincent Coubard |
640:c90ae1400bf2 | 484 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 485 | } |
Vincent Coubard |
640:c90ae1400bf2 | 486 | |
Vincent Coubard |
640:c90ae1400bf2 | 487 | |
Vincent Coubard |
640:c90ae1400bf2 | 488 | ret_code_t pds_peer_data_write_prepare_cancel(pm_prepare_token_t prepare_token) |
Vincent Coubard |
640:c90ae1400bf2 | 489 | { |
Vincent Coubard |
640:c90ae1400bf2 | 490 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 491 | |
Vincent Coubard |
640:c90ae1400bf2 | 492 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 493 | VERIFY_PARAM_NOT_ZERO(prepare_token); |
Vincent Coubard |
640:c90ae1400bf2 | 494 | |
Vincent Coubard |
640:c90ae1400bf2 | 495 | retval = fds_reserve_cancel((fds_write_token_t*)&prepare_token); |
Vincent Coubard |
640:c90ae1400bf2 | 496 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 497 | } |
Vincent Coubard |
640:c90ae1400bf2 | 498 | |
Vincent Coubard |
640:c90ae1400bf2 | 499 | |
Vincent Coubard |
640:c90ae1400bf2 | 500 | ret_code_t pds_peer_data_write_prepared(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 501 | pm_peer_data_const_t const * p_peer_data, |
Vincent Coubard |
640:c90ae1400bf2 | 502 | pm_prepare_token_t prepare_token, |
Vincent Coubard |
640:c90ae1400bf2 | 503 | pm_store_token_t * p_store_token) |
Vincent Coubard |
640:c90ae1400bf2 | 504 | { |
Vincent Coubard |
640:c90ae1400bf2 | 505 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 506 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 507 | fds_record_key_t record_key; |
Vincent Coubard |
640:c90ae1400bf2 | 508 | fds_record_chunk_t chunks[2]; |
Vincent Coubard |
640:c90ae1400bf2 | 509 | uint16_t n_chunks; |
Vincent Coubard |
640:c90ae1400bf2 | 510 | |
Vincent Coubard |
640:c90ae1400bf2 | 511 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 512 | //VERIFY_PARAM_NOT_ZERO(prepare_token); |
Vincent Coubard |
640:c90ae1400bf2 | 513 | VERIFY_PARAM_NOT_NULL(p_peer_data); |
Vincent Coubard |
640:c90ae1400bf2 | 514 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 515 | VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 516 | |
Vincent Coubard |
640:c90ae1400bf2 | 517 | // Fill in the keys. |
Vincent Coubard |
640:c90ae1400bf2 | 518 | record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 519 | record_key.instance = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 520 | |
Vincent Coubard |
640:c90ae1400bf2 | 521 | // Create chunks. |
Vincent Coubard |
640:c90ae1400bf2 | 522 | peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 523 | |
Vincent Coubard |
640:c90ae1400bf2 | 524 | retval = fds_write_reserved((fds_write_token_t*)&prepare_token, &record_desc, |
Vincent Coubard |
640:c90ae1400bf2 | 525 | record_key, n_chunks, chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 526 | |
Vincent Coubard |
640:c90ae1400bf2 | 527 | if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
Vincent Coubard |
640:c90ae1400bf2 | 528 | { |
Vincent Coubard |
640:c90ae1400bf2 | 529 | fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
Vincent Coubard |
640:c90ae1400bf2 | 530 | } |
Vincent Coubard |
640:c90ae1400bf2 | 531 | |
Vincent Coubard |
640:c90ae1400bf2 | 532 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 533 | } |
Vincent Coubard |
640:c90ae1400bf2 | 534 | |
Vincent Coubard |
640:c90ae1400bf2 | 535 | |
Vincent Coubard |
640:c90ae1400bf2 | 536 | ret_code_t pds_peer_data_write(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 537 | pm_peer_data_const_t const * p_peer_data, |
Vincent Coubard |
640:c90ae1400bf2 | 538 | pm_store_token_t * p_store_token) |
Vincent Coubard |
640:c90ae1400bf2 | 539 | { |
Vincent Coubard |
640:c90ae1400bf2 | 540 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 541 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 542 | fds_record_key_t record_key; |
Vincent Coubard |
640:c90ae1400bf2 | 543 | fds_record_chunk_t chunks[2]; |
Vincent Coubard |
640:c90ae1400bf2 | 544 | uint16_t n_chunks; |
Vincent Coubard |
640:c90ae1400bf2 | 545 | |
Vincent Coubard |
640:c90ae1400bf2 | 546 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 547 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 548 | VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 549 | |
Vincent Coubard |
640:c90ae1400bf2 | 550 | // Fill in the keys. |
Vincent Coubard |
640:c90ae1400bf2 | 551 | record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 552 | record_key.instance = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 553 | |
Vincent Coubard |
640:c90ae1400bf2 | 554 | // Create chunks |
Vincent Coubard |
640:c90ae1400bf2 | 555 | peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 556 | |
Vincent Coubard |
640:c90ae1400bf2 | 557 | // Request write |
Vincent Coubard |
640:c90ae1400bf2 | 558 | retval = fds_write(&record_desc, record_key, n_chunks, chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 559 | |
Vincent Coubard |
640:c90ae1400bf2 | 560 | if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
Vincent Coubard |
640:c90ae1400bf2 | 561 | { |
Vincent Coubard |
640:c90ae1400bf2 | 562 | fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
Vincent Coubard |
640:c90ae1400bf2 | 563 | } |
Vincent Coubard |
640:c90ae1400bf2 | 564 | |
Vincent Coubard |
640:c90ae1400bf2 | 565 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 566 | } |
Vincent Coubard |
640:c90ae1400bf2 | 567 | |
Vincent Coubard |
640:c90ae1400bf2 | 568 | |
Vincent Coubard |
640:c90ae1400bf2 | 569 | ret_code_t pds_peer_data_update(pm_peer_id_t peer_id, |
Vincent Coubard |
640:c90ae1400bf2 | 570 | pm_peer_data_const_t const * p_peer_data, |
Vincent Coubard |
640:c90ae1400bf2 | 571 | pm_store_token_t old_token, |
Vincent Coubard |
640:c90ae1400bf2 | 572 | pm_store_token_t * p_store_token) |
Vincent Coubard |
640:c90ae1400bf2 | 573 | { |
Vincent Coubard |
640:c90ae1400bf2 | 574 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 575 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 576 | fds_record_key_t record_key; |
Vincent Coubard |
640:c90ae1400bf2 | 577 | fds_record_chunk_t chunks[2]; |
Vincent Coubard |
640:c90ae1400bf2 | 578 | uint16_t n_chunks; |
Vincent Coubard |
640:c90ae1400bf2 | 579 | |
Vincent Coubard |
640:c90ae1400bf2 | 580 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 581 | VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 582 | VERIFY_PARAM_NOT_NULL(p_peer_data); |
Vincent Coubard |
640:c90ae1400bf2 | 583 | |
Vincent Coubard |
640:c90ae1400bf2 | 584 | record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
Vincent Coubard |
640:c90ae1400bf2 | 585 | record_key.instance = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 586 | |
Vincent Coubard |
640:c90ae1400bf2 | 587 | // Create chunks |
Vincent Coubard |
640:c90ae1400bf2 | 588 | peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 589 | |
Vincent Coubard |
640:c90ae1400bf2 | 590 | fds_descriptor_from_rec_id(&record_desc, (fds_record_id_t)old_token); |
Vincent Coubard |
640:c90ae1400bf2 | 591 | |
Vincent Coubard |
640:c90ae1400bf2 | 592 | retval = fds_update(&record_desc, record_key, n_chunks, chunks); |
Vincent Coubard |
640:c90ae1400bf2 | 593 | |
Vincent Coubard |
640:c90ae1400bf2 | 594 | if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
Vincent Coubard |
640:c90ae1400bf2 | 595 | { |
Vincent Coubard |
640:c90ae1400bf2 | 596 | fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
Vincent Coubard |
640:c90ae1400bf2 | 597 | } |
Vincent Coubard |
640:c90ae1400bf2 | 598 | |
Vincent Coubard |
640:c90ae1400bf2 | 599 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 600 | } |
Vincent Coubard |
640:c90ae1400bf2 | 601 | |
Vincent Coubard |
640:c90ae1400bf2 | 602 | ret_code_t pds_peer_data_clear(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) |
Vincent Coubard |
640:c90ae1400bf2 | 603 | { |
Vincent Coubard |
640:c90ae1400bf2 | 604 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 605 | fds_type_id_t type_id; |
Vincent Coubard |
640:c90ae1400bf2 | 606 | fds_instance_id_t instance_id; |
Vincent Coubard |
640:c90ae1400bf2 | 607 | fds_record_desc_t record_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 608 | fds_find_token_t find_tok; |
Vincent Coubard |
640:c90ae1400bf2 | 609 | |
Vincent Coubard |
640:c90ae1400bf2 | 610 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 611 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 612 | VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 613 | |
Vincent Coubard |
640:c90ae1400bf2 | 614 | type_id = convert_peer_data_id_to_type_id(data_id); |
Vincent Coubard |
640:c90ae1400bf2 | 615 | instance_id = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 616 | |
Vincent Coubard |
640:c90ae1400bf2 | 617 | retval = fds_find(type_id, instance_id, &record_desc, &find_tok); |
Vincent Coubard |
640:c90ae1400bf2 | 618 | if(retval != NRF_SUCCESS) |
Vincent Coubard |
640:c90ae1400bf2 | 619 | { |
Vincent Coubard |
640:c90ae1400bf2 | 620 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 621 | } |
Vincent Coubard |
640:c90ae1400bf2 | 622 | |
Vincent Coubard |
640:c90ae1400bf2 | 623 | retval = fds_clear(&record_desc); |
Vincent Coubard |
640:c90ae1400bf2 | 624 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 625 | } |
Vincent Coubard |
640:c90ae1400bf2 | 626 | |
Vincent Coubard |
640:c90ae1400bf2 | 627 | |
Vincent Coubard |
640:c90ae1400bf2 | 628 | pm_peer_id_t pds_peer_id_allocate(void) |
Vincent Coubard |
640:c90ae1400bf2 | 629 | { |
Vincent Coubard |
640:c90ae1400bf2 | 630 | if (!MODULE_INITIALIZED) |
Vincent Coubard |
640:c90ae1400bf2 | 631 | { |
Vincent Coubard |
640:c90ae1400bf2 | 632 | return PM_PEER_ID_INVALID; |
Vincent Coubard |
640:c90ae1400bf2 | 633 | } |
Vincent Coubard |
640:c90ae1400bf2 | 634 | PEER_IDS_INITIALIZE(); |
Vincent Coubard |
640:c90ae1400bf2 | 635 | return peer_id_allocate(PM_PEER_ID_INVALID); |
Vincent Coubard |
640:c90ae1400bf2 | 636 | } |
Vincent Coubard |
640:c90ae1400bf2 | 637 | |
Vincent Coubard |
640:c90ae1400bf2 | 638 | |
Vincent Coubard |
640:c90ae1400bf2 | 639 | ret_code_t pds_peer_id_free(pm_peer_id_t peer_id) |
Vincent Coubard |
640:c90ae1400bf2 | 640 | { |
Vincent Coubard |
640:c90ae1400bf2 | 641 | ret_code_t retval; |
Vincent Coubard |
640:c90ae1400bf2 | 642 | fds_instance_id_t instance_id; |
Vincent Coubard |
640:c90ae1400bf2 | 643 | |
Vincent Coubard |
640:c90ae1400bf2 | 644 | VERIFY_MODULE_INITIALIZED(); |
Vincent Coubard |
640:c90ae1400bf2 | 645 | VERIFY_PEER_ID_IN_RANGE(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 646 | PEER_IDS_INITIALIZE(); |
Vincent Coubard |
640:c90ae1400bf2 | 647 | |
Vincent Coubard |
640:c90ae1400bf2 | 648 | instance_id = convert_peer_id_to_instance_id(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 649 | |
Vincent Coubard |
640:c90ae1400bf2 | 650 | retval = fds_clear_by_instance(instance_id); |
Vincent Coubard |
640:c90ae1400bf2 | 651 | return retval; |
Vincent Coubard |
640:c90ae1400bf2 | 652 | } |
Vincent Coubard |
640:c90ae1400bf2 | 653 | |
Vincent Coubard |
640:c90ae1400bf2 | 654 | |
Vincent Coubard |
640:c90ae1400bf2 | 655 | bool pds_peer_id_is_allocated(pm_peer_id_t peer_id) |
Vincent Coubard |
640:c90ae1400bf2 | 656 | { |
Vincent Coubard |
640:c90ae1400bf2 | 657 | if (!MODULE_INITIALIZED) |
Vincent Coubard |
640:c90ae1400bf2 | 658 | { |
Vincent Coubard |
640:c90ae1400bf2 | 659 | return false; |
Vincent Coubard |
640:c90ae1400bf2 | 660 | } |
Vincent Coubard |
640:c90ae1400bf2 | 661 | PEER_IDS_INITIALIZE(); |
Vincent Coubard |
640:c90ae1400bf2 | 662 | |
Vincent Coubard |
640:c90ae1400bf2 | 663 | return peer_id_is_allocated(peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 664 | } |
Vincent Coubard |
640:c90ae1400bf2 | 665 | |
Vincent Coubard |
640:c90ae1400bf2 | 666 | |
Vincent Coubard |
640:c90ae1400bf2 | 667 | pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id) |
Vincent Coubard |
640:c90ae1400bf2 | 668 | { |
Vincent Coubard |
640:c90ae1400bf2 | 669 | if (!MODULE_INITIALIZED) |
Vincent Coubard |
640:c90ae1400bf2 | 670 | { |
Vincent Coubard |
640:c90ae1400bf2 | 671 | return PM_PEER_ID_INVALID; |
Vincent Coubard |
640:c90ae1400bf2 | 672 | } |
Vincent Coubard |
640:c90ae1400bf2 | 673 | PEER_IDS_INITIALIZE(); |
Vincent Coubard |
640:c90ae1400bf2 | 674 | |
Vincent Coubard |
640:c90ae1400bf2 | 675 | return peer_id_next_id_get(prev_peer_id); |
Vincent Coubard |
640:c90ae1400bf2 | 676 | } |
Vincent Coubard |
640:c90ae1400bf2 | 677 | |
Vincent Coubard |
640:c90ae1400bf2 | 678 | |
Vincent Coubard |
640:c90ae1400bf2 | 679 | uint32_t pds_n_peers(void) |
Vincent Coubard |
640:c90ae1400bf2 | 680 | { |
Vincent Coubard |
640:c90ae1400bf2 | 681 | if (!MODULE_INITIALIZED) |
Vincent Coubard |
640:c90ae1400bf2 | 682 | { |
Vincent Coubard |
640:c90ae1400bf2 | 683 | return 0; |
Vincent Coubard |
640:c90ae1400bf2 | 684 | } |
Vincent Coubard |
640:c90ae1400bf2 | 685 | PEER_IDS_INITIALIZE(); |
Vincent Coubard |
640:c90ae1400bf2 | 686 | return peer_id_n_ids(); |
Vincent Coubard |
640:c90ae1400bf2 | 687 | } |
Vincent Coubard |
640:c90ae1400bf2 | 688 |