R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers peer_data_storage.c Source File

peer_data_storage.c

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 
00034 #include "peer_data_storage.h"
00035 
00036 #include <stdint.h>
00037 #include <string.h>
00038 #include "sdk_errors.h "
00039 #include "peer_manager_types.h "
00040 #include "peer_id.h"
00041 #include "peer_data.h"
00042 #include "fds.h"
00043 
00044 #define MAX_REGISTRANTS    6                         /**< The number of user that can register with the module. */
00045 
00046 #define MODULE_INITIALIZED (m_pds.n_registrants > 0) /**< Expression which is true when the module is initialized. */
00047 
00048 /**@brief Macro for verifying that the module is initialized. It will cause the function to return
00049  *        @ref NRF_ERROR_INVALID_STATE if not.
00050  */
00051 #define VERIFY_MODULE_INITIALIZED()         \
00052 do                                          \
00053 {                                           \
00054     if (!MODULE_INITIALIZED)                \
00055     {                                       \
00056         return NRF_ERROR_INVALID_STATE;     \
00057     }                                       \
00058 } while(0)
00059 
00060 
00061 /**@brief Macro for verifying that the module is initialized. It will cause the function to return
00062  *        if not.
00063  */
00064 #define VERIFY_MODULE_INITIALIZED_VOID()    \
00065 do                                          \
00066 {                                           \
00067     if (!MODULE_INITIALIZED)                \
00068     {                                       \
00069         return;                             \
00070     }                                       \
00071 } while(0)
00072 
00073 
00074 /**@brief Macro for verifying that the param is not NULL. It will cause the function to return
00075  *        if not.
00076  *
00077  * @param[in] param  The variable to check if is NULL.
00078  */
00079 #define VERIFY_PARAM_NOT_NULL(param)        \
00080 do                                          \
00081 {                                           \
00082     if (param == NULL)                      \
00083     {                                       \
00084         return NRF_ERROR_NULL;              \
00085     }                                       \
00086 } while(0)
00087 
00088 
00089 /**@brief Macro for verifying that param is not zero. It will cause the function to return
00090  *        if not.
00091  *
00092  * @param[in] param  The variable to check if is zero.
00093  */
00094 #define VERIFY_PARAM_NOT_ZERO(param)        \
00095 do                                          \
00096 {                                           \
00097     if (param == 0)                         \
00098     {                                       \
00099         return NRF_ERROR_NULL;              \
00100     }                                       \
00101 } while(0)
00102 
00103 
00104 /**@brief Macro for verifying that the peer id is within a valid range
00105  *
00106  * @param[in]   id      The peer data id to check.
00107  */
00108 #define VERIFY_PEER_ID_IN_RANGE(id)         \
00109 do                                          \
00110 {                                           \
00111     if ((id >= PM_PEER_ID_N_AVAILABLE_IDS)) \
00112     {                                       \
00113         return NRF_ERROR_INVALID_PARAM;     \
00114     }                                       \
00115 } while (0)
00116 
00117 
00118 /**@brief Macro for verifying that the peer data id is withing a valid range
00119  *
00120  * @param[in]   id      The peer data id to check.
00121  */
00122 #define VERIFY_PEER_DATA_ID_IN_RANGE(id)    \
00123 do                                          \
00124 {                                           \
00125     if (!PM_PEER_DATA_ID_IS_VALID(id))      \
00126     {                                       \
00127         return NRF_ERROR_INVALID_PARAM;     \
00128     }                                       \
00129 } while (0)
00130 
00131 
00132 #define PEER_IDS_INITIALIZE()               \
00133 do                                          \
00134 {                                           \
00135     if (!m_pds.peer_ids_initialized)        \
00136     {                                       \
00137         peer_ids_init();                    \
00138     }                                       \
00139 } while (0)
00140 
00141 
00142 typedef struct
00143 {
00144     bool                peer_ids_initialized;
00145     pds_evt_handler_t   evt_handlers[MAX_REGISTRANTS];
00146     uint8_t             n_registrants;
00147 } pds_t;
00148 
00149 static pds_t m_pds = {.n_registrants = 0};
00150 
00151 static void internal_state_reset(pds_t * p_pds)
00152 {
00153     memset(p_pds, 0, sizeof(pds_t));
00154 }
00155 
00156 /**@brief Function for dispatching outbound events to all registered event handlers.
00157  *
00158  * @param[in]  p_event  The event to dispatch.
00159  */
00160 static void pds_evt_send(pds_evt_t * p_event)
00161 {
00162     for (int i = 0; i < m_pds.n_registrants; i++)
00163     {
00164         m_pds.evt_handlers[i](p_event);
00165     }
00166 }
00167 
00168 /**@brief Function to convert peer id to instance id
00169  *
00170  * @param[in] peer_id   Peer id to convert to instance id
00171  *
00172  * @return  Value as instance id
00173  */
00174 static fds_instance_id_t convert_peer_id_to_instance_id(pm_peer_id_t peer_id)
00175 {
00176     return (fds_instance_id_t)(peer_id + peer_id_to_instance_id);
00177 }
00178 
00179 /**@brief Function to convert peer data id to type id
00180  *
00181  * @param[in]   peer_data_id    Peer data id to convert to type_id
00182  *
00183  * @return Value as type id
00184  */
00185 static fds_type_id_t convert_peer_data_id_to_type_id(pm_peer_data_id_t peer_data_id)
00186 {
00187     return (fds_type_id_t)peer_data_id + (fds_type_id_t)peer_data_id_to_type_id;
00188 }
00189 
00190 
00191 /**@brief Function to convert peer data id to type id
00192  *
00193  * @param[in]   peer_data_id    Peer data id to convert to type_id
00194  *
00195  * @return Value as type id
00196  */
00197 static pm_peer_id_t convert_instance_id_to_peer_id(fds_instance_id_t instance_id)
00198 {
00199     return (pm_peer_id_t)(instance_id + instance_id_to_peer_id);
00200 }
00201 
00202 
00203 /**@brief Function to type id to peer data id
00204  *
00205  * @param[in]   type_id    Type id to convert to peer data id
00206  *
00207  * @return Value as peer data id
00208  */
00209 static pm_peer_data_id_t convert_type_id_to_peer_data_id(fds_type_id_t type_id)
00210 {
00211     return (pm_peer_data_id_t)(type_id + instance_id_to_peer_id);
00212 }
00213 
00214 
00215 static ret_code_t find_fds_item(pm_peer_id_t              peer_id,
00216                                 pm_peer_data_id_t         data_id,
00217                                 fds_record_desc_t * const p_desc)
00218 {
00219     fds_find_token_t find_tok;
00220 
00221     VERIFY_PEER_ID_IN_RANGE(peer_id);
00222     VERIFY_PEER_DATA_ID_IN_RANGE(data_id);
00223     // pp_record verified outside
00224 
00225     fds_type_id_t       type_id     = convert_peer_data_id_to_type_id(data_id);
00226     fds_instance_id_t   instance_id = convert_peer_id_to_instance_id(peer_id);
00227 
00228     return fds_find(type_id, instance_id, p_desc, &find_tok);
00229 }
00230 
00231 
00232 static void peer_ids_init()
00233 {
00234     fds_record_t            record;
00235     fds_record_desc_t       record_desc;
00236     fds_find_token_t        find_tok;
00237     fds_type_id_t     const type_id = convert_peer_data_id_to_type_id(PM_PEER_DATA_ID_BONDING);
00238     pm_peer_id_t            peer_id;
00239 
00240     if (!m_pds.peer_ids_initialized)
00241     {
00242         while(fds_find_by_type(type_id, &record_desc, &find_tok) == NRF_SUCCESS)
00243         {
00244             fds_open(&record_desc, &record);
00245             fds_close(&record_desc);
00246             peer_id = convert_instance_id_to_peer_id(record.header.ic.instance);
00247             peer_id_allocate(peer_id);
00248         }
00249 
00250         m_pds.peer_ids_initialized = true;
00251     }
00252 }
00253 
00254 //uint32_t size_pad_to_mult_of_four(uint32_t unpadded_size)
00255 //{
00256 //    return (unpadded_size + 3) & 3;
00257 //}
00258 
00259 static void fds_evt_handler(ret_code_t          result,
00260                             fds_cmd_id_t         cmd,
00261                             fds_record_id_t     record_id,
00262                             fds_record_key_t    record_key
00263                             /*fds_record_t  const * const p_record*/)
00264 {
00265     pds_evt_t evt;
00266     switch(cmd)
00267     {
00268         case FDS_CMD_INIT:
00269 
00270             break;
00271 
00272         case FDS_CMD_UPDATE:
00273         case FDS_CMD_WRITE:
00274             evt.peer_id = convert_instance_id_to_peer_id(record_key.instance);
00275             evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_STORED : PDS_EVT_ERROR_STORE;
00276             evt.data_id = convert_type_id_to_peer_data_id(record_key.type);
00277             evt.store_token = record_id;
00278             pds_evt_send(&evt);
00279             break;
00280 
00281         case FDS_CMD_CLEAR:
00282             evt.peer_id = convert_instance_id_to_peer_id(record_key.instance);
00283             evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_CLEARED : PDS_EVT_ERROR_CLEAR;
00284             evt.data_id = convert_type_id_to_peer_data_id(record_key.type);
00285             evt.store_token = record_id;
00286             pds_evt_send(&evt);
00287             break;
00288 
00289         case FDS_CMD_CLEAR_INST:
00290             {
00291                 if ((record_key.type     == FDS_TYPE_ID_INVALID) &&
00292                     (record_key.instance != FDS_TYPE_ID_INVALID))
00293                 {
00294                     pm_peer_id_t peer_id = convert_instance_id_to_peer_id(record_key.instance);
00295 
00296                     evt.peer_id = peer_id;
00297                     evt.data_id = PM_PEER_DATA_ID_INVALID;
00298                     if (result == NRF_SUCCESS)
00299                     {
00300                         evt.evt_id = PDS_EVT_PEER_ID_CLEAR;
00301                         peer_id_free(peer_id);
00302                     }
00303                     else
00304                     {
00305                         evt.evt_id = PDS_EVT_ERROR_PEER_ID_CLEAR;
00306                     }
00307                 }
00308                 else
00309                 {
00310                     // TODO: Not supported yet (clear many without clearing peer_id)
00311                 }
00312 
00313                 pds_evt_send(&evt);
00314             }
00315             break;
00316 
00317         case FDS_CMD_GC:
00318             evt.peer_id = convert_instance_id_to_peer_id(record_key.instance);
00319             evt.evt_id = PDS_EVT_COMPRESSED;
00320             evt.data_id = convert_type_id_to_peer_data_id(record_key.type);
00321             evt.store_token = record_id;
00322             pds_evt_send(&evt);
00323             break;
00324 
00325         default:
00326 
00327             break;
00328     }
00329 }
00330 
00331 
00332 ret_code_t pds_register(pds_evt_handler_t evt_handler)
00333 {
00334     if (m_pds.n_registrants >= MAX_REGISTRANTS)
00335     {
00336         return NRF_ERROR_NO_MEM;
00337     }
00338 
00339     VERIFY_PARAM_NOT_NULL(evt_handler);
00340 
00341     if (!MODULE_INITIALIZED)
00342     {
00343         ret_code_t retval;
00344         internal_state_reset(&m_pds);
00345         peer_id_init();
00346 
00347         fds_cb_t cb = fds_evt_handler;
00348         retval = fds_register(cb);
00349         if(retval != NRF_SUCCESS)
00350         {
00351             return retval;
00352         }
00353 
00354         retval = fds_init();
00355         if(retval != NRF_SUCCESS)
00356         {
00357             return retval;
00358         }
00359     }
00360 
00361     m_pds.evt_handlers[m_pds.n_registrants] = evt_handler;
00362     m_pds.n_registrants += 1;
00363 
00364     return NRF_SUCCESS;
00365 
00366 }
00367 
00368 
00369 ret_code_t pds_peer_data_read_ptr_get(pm_peer_id_t            peer_id,
00370                                       pm_peer_data_id_t       data_id,
00371                                       pm_peer_data_flash_t  * p_data,
00372                                       pm_store_token_t      * p_token)
00373 {
00374     ret_code_t retval;
00375 
00376     fds_record_t      record;
00377     fds_record_desc_t record_desc;
00378 
00379     VERIFY_MODULE_INITIALIZED();
00380     VERIFY_PEER_ID_IN_RANGE(peer_id);
00381     VERIFY_PEER_DATA_ID_IN_RANGE(data_id);
00382 
00383     retval = find_fds_item(peer_id, data_id, &record_desc);
00384     if (retval != NRF_SUCCESS)
00385     {
00386         return retval;
00387     }
00388 
00389     // Shouldn't fail, unless record is cleared.
00390     fds_open(&record_desc, &record);
00391     // No need to keep it open, since we are not reading.
00392     fds_close(&record_desc);
00393 
00394     //NRF_LOG_PRINTF("Found item with peer_id: %d, data_id: %d, Address: %p\r\n", record.p_data);
00395 
00396     if (p_data != NULL)
00397     {
00398         p_data->data_type    = data_id;
00399         p_data->length_words = record.header.tl.length_words;
00400 
00401         p_data->data.p_application_data = (uint8_t const*)record.p_data;
00402     }
00403 
00404     if (p_token != NULL)
00405     {
00406         *p_token = (uint32_t)record.header.id;
00407     }
00408 
00409     return retval;
00410 }
00411 
00412 
00413 ret_code_t pds_peer_data_lock(pm_store_token_t store_token)
00414 {
00415     VERIFY_MODULE_INITIALIZED();
00416     VERIFY_PARAM_NOT_ZERO(store_token);
00417 
00418     // TODO: Not implemented yet in fds
00419 
00420     return NRF_SUCCESS;
00421 }
00422 
00423 
00424 ret_code_t pds_peer_data_verify(pm_store_token_t store_token)
00425 {
00426     VERIFY_MODULE_INITIALIZED();
00427     VERIFY_PARAM_NOT_ZERO(store_token);
00428 
00429     // TODO: Not implemented yet in fds
00430 
00431     return NRF_SUCCESS;
00432 }
00433 
00434 
00435 ret_code_t pds_peer_data_read(pm_peer_id_t          peer_id,
00436                               pm_peer_data_id_t     data_id,
00437                               pm_peer_data_t      * p_data,
00438                               fds_length_t        * p_len_words)
00439 {
00440     VERIFY_PEER_ID_IN_RANGE(peer_id);
00441     VERIFY_PEER_DATA_ID_IN_RANGE(data_id);
00442     VERIFY_PARAM_NOT_NULL(p_len_words);
00443     VERIFY_PARAM_NOT_NULL(p_data);
00444 
00445     ret_code_t err_code;
00446     pm_peer_data_flash_t peer_data_flash;
00447 
00448     err_code = pds_peer_data_read_ptr_get(peer_id, data_id, &peer_data_flash, NULL);
00449 
00450     if (err_code != NRF_SUCCESS)
00451     {
00452         return err_code;
00453     }
00454 
00455     if ((*p_len_words) == 0)
00456     {
00457         (*p_len_words) = peer_data_flash.length_words;
00458         return NRF_SUCCESS;
00459     }
00460     else if ((*p_len_words) < peer_data_flash.length_words)
00461     {
00462         return NRF_ERROR_NO_MEM;
00463     }
00464 
00465     VERIFY_PARAM_NOT_NULL(p_data->data.p_application_data);
00466 
00467     err_code = peer_data_deserialize(&peer_data_flash, p_data);
00468 
00469     return err_code;
00470 }
00471 
00472 
00473 ret_code_t pds_peer_data_write_prepare(pm_peer_data_const_t const * p_peer_data,
00474                                        pm_prepare_token_t         * p_prepare_token)
00475 {
00476     ret_code_t retval;
00477 
00478     VERIFY_MODULE_INITIALIZED();
00479     VERIFY_PARAM_NOT_NULL(p_peer_data);
00480     VERIFY_PARAM_NOT_NULL(p_prepare_token);
00481     VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type);
00482 
00483     retval = fds_reserve((fds_write_token_t*)p_prepare_token, p_peer_data->length_words);
00484     return retval;
00485 }
00486 
00487 
00488 ret_code_t pds_peer_data_write_prepare_cancel(pm_prepare_token_t prepare_token)
00489 {
00490     ret_code_t retval;
00491 
00492     VERIFY_MODULE_INITIALIZED();
00493     VERIFY_PARAM_NOT_ZERO(prepare_token);
00494 
00495     retval = fds_reserve_cancel((fds_write_token_t*)&prepare_token);
00496     return retval;
00497 }
00498 
00499 
00500 ret_code_t pds_peer_data_write_prepared(pm_peer_id_t                    peer_id,
00501                                         pm_peer_data_const_t    const * p_peer_data,
00502                                         pm_prepare_token_t              prepare_token,
00503                                         pm_store_token_t              * p_store_token)
00504 {
00505     ret_code_t         retval;
00506     fds_record_desc_t  record_desc;
00507     fds_record_key_t   record_key;
00508     fds_record_chunk_t chunks[2];
00509     uint16_t           n_chunks;
00510 
00511     VERIFY_MODULE_INITIALIZED();
00512     //VERIFY_PARAM_NOT_ZERO(prepare_token);
00513     VERIFY_PARAM_NOT_NULL(p_peer_data);
00514     VERIFY_PEER_ID_IN_RANGE(peer_id);
00515     VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type);
00516 
00517     // Fill in the keys.
00518     record_key.type     = convert_peer_data_id_to_type_id(p_peer_data->data_type);
00519     record_key.instance = convert_peer_id_to_instance_id(peer_id);
00520 
00521     // Create chunks.
00522     peer_data_parts_get(p_peer_data, chunks, &n_chunks);
00523 
00524     retval = fds_write_reserved((fds_write_token_t*)&prepare_token, &record_desc,
00525                                 record_key, n_chunks, chunks);
00526 
00527     if ((retval == NRF_SUCCESS) && (p_store_token != NULL))
00528     {
00529         fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token);
00530     }
00531 
00532     return retval;
00533 }
00534 
00535 
00536 ret_code_t pds_peer_data_write(pm_peer_id_t                 peer_id,
00537                                pm_peer_data_const_t const * p_peer_data,
00538                                pm_store_token_t           * p_store_token)
00539 {
00540     ret_code_t          retval;
00541     fds_record_desc_t   record_desc;
00542     fds_record_key_t    record_key;
00543     fds_record_chunk_t  chunks[2];
00544     uint16_t            n_chunks;
00545 
00546     VERIFY_MODULE_INITIALIZED();
00547     VERIFY_PEER_ID_IN_RANGE(peer_id);
00548     VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type);
00549 
00550     // Fill in the keys.
00551     record_key.type     = convert_peer_data_id_to_type_id(p_peer_data->data_type);
00552     record_key.instance = convert_peer_id_to_instance_id(peer_id);
00553 
00554     // Create chunks
00555     peer_data_parts_get(p_peer_data, chunks, &n_chunks);
00556 
00557     // Request write
00558     retval = fds_write(&record_desc, record_key, n_chunks, chunks);
00559 
00560     if ((retval == NRF_SUCCESS) && (p_store_token != NULL))
00561     {
00562         fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token);
00563     }
00564 
00565     return retval;
00566 }
00567 
00568 
00569 ret_code_t pds_peer_data_update(pm_peer_id_t                 peer_id,
00570                                 pm_peer_data_const_t const * p_peer_data,
00571                                 pm_store_token_t             old_token,
00572                                 pm_store_token_t           * p_store_token)
00573 {
00574     ret_code_t         retval;
00575     fds_record_desc_t  record_desc;
00576     fds_record_key_t   record_key;
00577     fds_record_chunk_t chunks[2];
00578     uint16_t           n_chunks;
00579 
00580     VERIFY_MODULE_INITIALIZED();
00581     VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type);
00582     VERIFY_PARAM_NOT_NULL(p_peer_data);
00583 
00584     record_key.type     = convert_peer_data_id_to_type_id(p_peer_data->data_type);
00585     record_key.instance = convert_peer_id_to_instance_id(peer_id);
00586 
00587     // Create chunks
00588     peer_data_parts_get(p_peer_data, chunks, &n_chunks);
00589 
00590     fds_descriptor_from_rec_id(&record_desc, (fds_record_id_t)old_token);
00591 
00592     retval = fds_update(&record_desc, record_key, n_chunks, chunks);
00593 
00594     if ((retval == NRF_SUCCESS) && (p_store_token != NULL))
00595     {
00596         fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token);
00597     }
00598 
00599     return retval;
00600 }
00601 
00602 ret_code_t pds_peer_data_clear(pm_peer_id_t peer_id, pm_peer_data_id_t data_id)
00603 {
00604     ret_code_t        retval;
00605     fds_type_id_t     type_id;
00606     fds_instance_id_t instance_id;
00607     fds_record_desc_t record_desc;
00608     fds_find_token_t  find_tok;
00609 
00610     VERIFY_MODULE_INITIALIZED();
00611     VERIFY_PEER_ID_IN_RANGE(peer_id);
00612     VERIFY_PEER_DATA_ID_IN_RANGE(data_id);
00613 
00614     type_id     = convert_peer_data_id_to_type_id(data_id);
00615     instance_id = convert_peer_id_to_instance_id(peer_id);
00616 
00617     retval = fds_find(type_id, instance_id, &record_desc, &find_tok);
00618     if(retval != NRF_SUCCESS)
00619     {
00620         return retval;
00621     }
00622 
00623     retval = fds_clear(&record_desc);
00624     return retval;
00625 }
00626 
00627 
00628 pm_peer_id_t pds_peer_id_allocate(void)
00629 {
00630     if (!MODULE_INITIALIZED)
00631     {
00632         return PM_PEER_ID_INVALID;
00633     }
00634     PEER_IDS_INITIALIZE();
00635     return peer_id_allocate(PM_PEER_ID_INVALID);
00636 }
00637 
00638 
00639 ret_code_t pds_peer_id_free(pm_peer_id_t peer_id)
00640 {
00641     ret_code_t retval;
00642     fds_instance_id_t instance_id;
00643 
00644     VERIFY_MODULE_INITIALIZED();
00645     VERIFY_PEER_ID_IN_RANGE(peer_id);
00646     PEER_IDS_INITIALIZE();
00647 
00648     instance_id = convert_peer_id_to_instance_id(peer_id);
00649 
00650     retval = fds_clear_by_instance(instance_id);
00651     return retval;
00652 }
00653 
00654 
00655 bool pds_peer_id_is_allocated(pm_peer_id_t peer_id)
00656 {
00657     if (!MODULE_INITIALIZED)
00658     {
00659         return false;
00660     }
00661     PEER_IDS_INITIALIZE();
00662 
00663     return peer_id_is_allocated(peer_id);
00664 }
00665 
00666 
00667 pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id)
00668 {
00669     if (!MODULE_INITIALIZED)
00670     {
00671         return PM_PEER_ID_INVALID;
00672     }
00673     PEER_IDS_INITIALIZE();
00674 
00675     return peer_id_next_id_get(prev_peer_id);
00676 }
00677 
00678 
00679 uint32_t pds_n_peers(void)
00680 {
00681     if (!MODULE_INITIALIZED)
00682     {
00683         return 0;
00684     }
00685     PEER_IDS_INITIALIZE();
00686     return peer_id_n_ids();
00687 }
00688