ret_code_t pds_register(pds_evt_handler_t evt_handler) { if (m_pds.n_registrants >= MAX_REGISTRANTS) { return NRF_ERROR_NO_MEM; } VERIFY_PARAM_NOT_NULL(evt_handler); if (!MODULE_INITIALIZED) { ret_code_t retval; internal_state_reset(&m_pds); peer_id_init(); retval = fds_register(fds_evt_handler); if (retval != FDS_SUCCESS) { return NRF_ERROR_NO_MEM; } retval = fds_init(); if (retval != FDS_SUCCESS) { return NRF_ERROR_INTERNAL; } } m_pds.evt_handlers[m_pds.n_registrants] = evt_handler; m_pds.n_registrants += 1; return NRF_SUCCESS; }
ret_code_t im_register(im_evt_handler_t evt_handler) { VERIFY_PARAM_NOT_NULL(evt_handler); ret_code_t err_code = NRF_SUCCESS; if (!MODULE_INITIALIZED) { internal_state_reset(); m_im.conn_state_user_flag_id = ble_conn_state_user_flag_acquire(); if (m_im.conn_state_user_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { err_code = NRF_ERROR_NO_MEM; } else { err_code = pdb_register(pdb_evt_handler); } } if (err_code == NRF_SUCCESS) { if ((m_im.n_registrants < MAX_REGISTRANTS)) { m_im.evt_handlers[m_im.n_registrants++] = evt_handler; } else { err_code = NRF_ERROR_NO_MEM; } } return err_code; }
ret_code_t gcm_init(gcm_evt_handler_t evt_handler) { VERIFY_PARAM_NOT_NULL(evt_handler); ret_code_t err_code; err_code = gscm_init(gscm_evt_handler); VERIFY_SUCCESS(err_code); err_code = gccm_init(gccm_evt_handler); VERIFY_SUCCESS(err_code); internal_state_reset(&m_gcm); m_gcm.evt_handler = evt_handler; err_code = im_register(im_evt_handler); VERIFY_SUCCESS(err_code); err_code = smd_register(smd_evt_handler); VERIFY_SUCCESS(err_code); m_gcm.flag_id_local_db_update_pending = ble_conn_state_user_flag_acquire(); m_gcm.flag_id_local_db_apply_pending = ble_conn_state_user_flag_acquire(); m_gcm.flag_id_service_changed_pending = ble_conn_state_user_flag_acquire(); m_gcm.flag_id_service_changed_sent = ble_conn_state_user_flag_acquire(); if ((m_gcm.flag_id_local_db_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || (m_gcm.flag_id_local_db_apply_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || (m_gcm.flag_id_service_changed_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || (m_gcm.flag_id_service_changed_sent == BLE_CONN_STATE_USER_FLAG_INVALID)) { err_code = NRF_ERROR_INTERNAL; } return err_code; }
ret_code_t im_init(void) { NRF_PM_DEBUG_CHECK(!m_module_initialized); internal_state_reset(); m_conn_state_user_flag_id = ble_conn_state_user_flag_acquire(); if (m_conn_state_user_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { return NRF_ERROR_INTERNAL; } #if (NRF_SD_BLE_API_VERSION == 2) ret_code_t ret_code = sd_ble_gap_address_get(&m_current_id_addr); if (ret_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } #endif m_module_initialized = true; return NRF_SUCCESS; }
void peer_id_init(void) { internal_state_reset(&m_pi); pm_mutex_init(m_pi.active_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); pm_mutex_init(m_pi.deleted_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); }
ret_code_t pm_init(void) { ret_code_t err_code; err_code = pds_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = pdb_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = sm_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = smd_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = gcm_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = gscm_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } err_code = im_init(); if (err_code != NRF_SUCCESS) { return NRF_ERROR_INTERNAL; } internal_state_reset(); m_pairing_flag_id = ble_conn_state_user_flag_acquire(); if (m_pairing_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { return NRF_ERROR_INTERNAL; } m_bonding_flag_id = ble_conn_state_user_flag_acquire(); if (m_bonding_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { return NRF_ERROR_INTERNAL; } m_peer_rank_initialized = false; m_module_initialized = true; return NRF_SUCCESS; }
/**@brief Function for handling events from the Peer Data Storage module. * * @param[in] p_event The event to handle. */ static void pds_evt_handler(pds_evt_t const * p_event) { ret_code_t err_code; pdb_buffer_record_t * p_write_buffer_record; bool retry_flash_full = false; pdb_evt_t event = { .peer_id = p_event->peer_id, .data_id = p_event->data_id, }; p_write_buffer_record = write_buffer_record_find(p_event->peer_id, p_event->data_id); switch (p_event->evt_id) { case PDS_EVT_STORED: if ( (p_write_buffer_record != NULL) //&& (p_write_buffer_record->store_token == p_event->store_token) && (p_write_buffer_record->store_requested)) { write_buffer_record_release(p_write_buffer_record); event.evt_id = PDB_EVT_WRITE_BUF_STORED; pdb_evt_send(&event); } else { event.evt_id = PDB_EVT_RAW_STORED; pdb_evt_send(&event); } break; case PDS_EVT_ERROR_STORE: if ( (p_write_buffer_record != NULL) && (p_write_buffer_record->store_token == p_event->store_token) && (p_write_buffer_record->store_requested)) { // Retry if internal buffer. m_pdb.n_writes++; p_write_buffer_record->store_requested = false; p_write_buffer_record->store_busy = true; } else { event.evt_id = PDB_EVT_RAW_STORE_FAILED; pdb_evt_send(&event); } break; case PDS_EVT_CLEARED: event.evt_id = PDB_EVT_CLEARED; pdb_evt_send(&event); break; case PDS_EVT_ERROR_CLEAR: event.evt_id = PDB_EVT_CLEAR_FAILED; pdb_evt_send(&event); break; case PDS_EVT_COMPRESSED: retry_flash_full = true; event.evt_id = PDB_EVT_COMPRESSED; pdb_evt_send(&event); break; default: break; } if (m_pdb.n_writes > 0) { for (int i = 0; i < N_WRITE_BUFFER_RECORDS; i++) { if ((m_pdb.write_buffer_records[i].store_busy) || (m_pdb.write_buffer_records[i].store_flash_full && retry_flash_full)) { err_code = pdb_write_buf_store(m_pdb.write_buffer_records[i].peer_id, m_pdb.write_buffer_records[i].data_id); if (err_code != NRF_SUCCESS) { event.peer_id = m_pdb.write_buffer_records[i].peer_id; event.data_id = m_pdb.write_buffer_records[i].data_id; if (err_code == NRF_ERROR_NO_MEM) { event.evt_id = PDB_EVT_ERROR_NO_MEM; } else { event.evt_id = PDB_EVT_ERROR_UNEXPECTED; } pdb_evt_send(&event); break; } } } } } ret_code_t pdb_register(pdb_evt_handler_t evt_handler) { if (m_pdb.n_registrants >= MAX_REGISTRANTS) { return NRF_ERROR_NO_MEM; } VERIFY_PARAM_NOT_NULL(evt_handler); if (!MODULE_INITIALIZED) { ret_code_t err_code; internal_state_reset(&m_pdb); err_code = pds_register(pds_evt_handler); if (err_code != NRF_SUCCESS) { return err_code; } PM_BUFFER_INIT(&m_pdb.write_buffer, N_WRITE_BUFFERS, PDB_WRITE_BUF_SIZE, err_code); if (err_code != NRF_SUCCESS) { return err_code; } } m_pdb.evt_handlers[m_pdb.n_registrants] = evt_handler; m_pdb.n_registrants += 1; return NRF_SUCCESS; } pm_peer_id_t pdb_peer_allocate(void) { if (!MODULE_INITIALIZED) { return PM_PEER_ID_INVALID; } return pds_peer_id_allocate(); } ret_code_t pdb_peer_free(pm_peer_id_t peer_id) { VERIFY_MODULE_INITIALIZED(); return pds_peer_id_free(peer_id); }