static void peer_ids_init()
{
    fds_record_desc_t  record_desc;
    fds_flash_record_t record;
    fds_find_token_t   find_tok = {0};

    uint16_t const record_key = peer_data_id_to_record_key(PM_PEER_DATA_ID_BONDING);

    if (!m_pds.peer_ids_initialized)
    {
        while(fds_record_find_by_key(record_key, &record_desc, &find_tok) == FDS_SUCCESS)
        {
            pm_peer_id_t peer_id;

            // It is safe to ignore the return value since we just obtained
            // this descriptor and also 'record' is different from NULL.
            (void)fds_record_open(&record_desc, &record);
            peer_id = file_id_to_peer_id(record.p_header->ic.file_id);
            (void)fds_record_close(&record_desc);

            (void)peer_id_allocate(peer_id);
        }

        m_pds.peer_ids_initialized = true;
    }
}
Example #2
0
bool pds_peer_data_iterate(pm_peer_data_id_t            data_id,
                           pm_peer_id_t         * const p_peer_id,
                           pm_peer_data_flash_t * const p_data)
{
    ret_code_t         ret;
    uint16_t           rec_key;
    fds_record_desc_t  rec_desc;
    fds_flash_record_t rec_flash;

    NRF_PM_DEBUG_CHECK(m_module_initialized);
    NRF_PM_DEBUG_CHECK(p_peer_id != NULL);
    NRF_PM_DEBUG_CHECK(p_data    != NULL);

    // @note emdi: should we check the data_id ?
    rec_key = peer_data_id_to_record_key(data_id);

    if (fds_record_find_by_key(rec_key, &rec_desc, &m_fds_ftok) != NRF_SUCCESS)
    {
        return false;
    }

    ret = fds_record_open(&rec_desc, &rec_flash);

    if (ret != NRF_SUCCESS)
    {
        // It can only happen if the record was deleted after the call to fds_record_find_by_key(),
        // before we could open it, or if CRC support was enabled in Flash Data Storage at compile
        // time and the CRC check failed.
        return false;
    }

    p_data->data_id      = data_id;
    p_data->length_words = rec_flash.p_header->tl.length_words;
    p_data->p_all_data   = rec_flash.p_data;

    *p_peer_id           = file_id_to_peer_id(rec_flash.p_header->ic.file_id);

    (void)fds_record_close(&rec_desc);

    return true;
}
Example #3
0
static void peer_ids_load()
{
    fds_record_desc_t  record_desc;
    fds_flash_record_t record;
    fds_find_token_t   ftok;

    memset(&ftok, 0x00, sizeof(fds_find_token_t));

    uint16_t const record_key = peer_data_id_to_record_key(PM_PEER_DATA_ID_BONDING);

    while (fds_record_find_by_key(record_key, &record_desc, &ftok) == FDS_SUCCESS)
    {
        pm_peer_id_t peer_id;

        // It is safe to ignore the return value since the descriptor was
        // just obtained and also 'record' is different from NULL.
        (void)fds_record_open(&record_desc, &record);
        peer_id = file_id_to_peer_id(record.p_header->ic.file_id);
        (void)fds_record_close(&record_desc);

        (void)peer_id_allocate(peer_id);
    }
}
static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
    pds_evt_t pds_evt;
    bool      send_event = true;

    pds_evt.result = (p_fds_evt->result == FDS_SUCCESS);

    switch(p_fds_evt->id)
    {
        case FDS_EVT_WRITE:
            pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_STORED :
                                                                  PDS_EVT_ERROR_STORE;

            pds_evt.peer_id     = file_id_to_peer_id(p_fds_evt->write.file_id);
            pds_evt.data_id     = record_key_to_peer_data_id(p_fds_evt->write.record_key);
            pds_evt.result      = p_fds_evt->result;
            pds_evt.store_token = p_fds_evt->write.record_id;
            break;

        case FDS_EVT_UPDATE:
            pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_UPDATED :
                                                                  PDS_EVT_ERROR_UPDATE;

            pds_evt.peer_id     = file_id_to_peer_id(p_fds_evt->write.file_id);
            pds_evt.data_id     = record_key_to_peer_data_id(p_fds_evt->write.record_key);
            pds_evt.result      = p_fds_evt->result;
            pds_evt.store_token = p_fds_evt->write.record_id;
            break;

        case FDS_EVT_DEL_RECORD:
            pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_CLEARED :
                                                                  PDS_EVT_ERROR_CLEAR;

            pds_evt.peer_id     = file_id_to_peer_id(p_fds_evt->del.file_id);
            pds_evt.data_id     = record_key_to_peer_data_id(p_fds_evt->del.record_key);
            pds_evt.store_token = p_fds_evt->del.record_id;
            break;

        case FDS_EVT_DEL_FILE:
            {
                if ((p_fds_evt->del.record_key == FDS_RECORD_KEY_DIRTY) &&
                    (p_fds_evt->del.file_id    != FDS_FILE_ID_INVALID))
                {
                    pds_evt.peer_id = file_id_to_peer_id(p_fds_evt->del.file_id);
                    pds_evt.data_id = record_key_to_peer_data_id(p_fds_evt->del.record_key);

                    pds_evt.data_id = PM_PEER_DATA_ID_INVALID;
                    if (p_fds_evt->result == FDS_SUCCESS)
                    {
                        pds_evt.evt_id = PDS_EVT_PEER_ID_CLEAR;
                        peer_id_free(pds_evt.peer_id);
                    }
                    else
                    {
                        pds_evt.evt_id = PDS_EVT_ERROR_PEER_ID_CLEAR;
                    }
                    m_pds.clearing = false;
                    m_pds.clear_queued = false;

                    peer_data_clear();
                }
            }
            break;

        case FDS_EVT_GC:
            pds_evt.evt_id = PDS_EVT_COMPRESSED;
            break;

        default:
            send_event = false;
            break;
    }

    if (send_event)
    {
        pds_evt_send(&pds_evt);
    }

    if (m_pds.clear_queued)
    {
        m_pds.clear_queued = false;
        peer_data_clear();
    }
}
Example #5
0
static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
    pds_evt_t pds_evt;

    pds_evt.result = (p_fds_evt->result == FDS_SUCCESS);

    switch (p_fds_evt->id)
    {
        case FDS_EVT_WRITE:
        case FDS_EVT_UPDATE:
            if (   file_id_within_pm_range(p_fds_evt->write.file_id)
                || record_key_within_pm_range(p_fds_evt->write.record_key))
            {
                pds_evt.peer_id = file_id_to_peer_id(p_fds_evt->write.file_id);
                pds_evt.data_id = record_key_to_peer_data_id(p_fds_evt->write.record_key);

                if (p_fds_evt->id == FDS_EVT_WRITE)
                {
                    pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_STORED :
                                                                          PDS_EVT_ERROR_STORE;
                }
                else
                {
                    pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_UPDATED :
                                                                          PDS_EVT_ERROR_UPDATE;
                }

                pds_evt.result      = p_fds_evt->result;
                pds_evt.store_token = p_fds_evt->write.record_id;

                pds_evt_send(&pds_evt);
            }
            break;

        case FDS_EVT_DEL_RECORD:
            if (   file_id_within_pm_range(p_fds_evt->del.file_id)
                || record_key_within_pm_range(p_fds_evt->del.record_key))
            {
                pds_evt.peer_id = file_id_to_peer_id(p_fds_evt->del.file_id);
                pds_evt.data_id = record_key_to_peer_data_id(p_fds_evt->del.record_key);

                pds_evt.evt_id = (p_fds_evt->result == FDS_SUCCESS) ? PDS_EVT_CLEARED :
                                                                      PDS_EVT_ERROR_CLEAR;

                pds_evt.store_token = p_fds_evt->del.record_id;

                pds_evt_send(&pds_evt);
            }
            break;

        case FDS_EVT_DEL_FILE:
            {
                if (    file_id_within_pm_range(p_fds_evt->del.file_id)
                    && (p_fds_evt->del.record_key == FDS_RECORD_KEY_DIRTY))
                {
                    pds_evt.peer_id = file_id_to_peer_id(p_fds_evt->del.file_id);
                    pds_evt.data_id = record_key_to_peer_data_id(p_fds_evt->del.record_key);

                    pds_evt.data_id = PM_PEER_DATA_ID_INVALID;
                    if (p_fds_evt->result == FDS_SUCCESS)
                    {
                        pds_evt.evt_id = PDS_EVT_PEER_ID_CLEAR;
                        peer_id_free(pds_evt.peer_id);
                    }
                    else
                    {
                        pds_evt.evt_id = PDS_EVT_ERROR_PEER_ID_CLEAR;
                    }

                    m_peer_delete_queued  = false;
                    m_peer_delete_ongoing = false;

                    peer_data_delete();

                    pds_evt_send(&pds_evt);
                }
            }
            break;

        case FDS_EVT_GC:
            pds_evt.evt_id = PDS_EVT_COMPRESSED;
            pds_evt_send(&pds_evt);
            break;

        default:
            break;
    }

    if (m_peer_delete_queued)
    {
        m_peer_delete_queued  = false;
        peer_data_delete();
    }
}