cvb

Fork of nrf51-sdk by Lancaster University

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?

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