This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

p_storage conflict with Device Manager

I have migrated to SDK 9.0 primarily to take advantage of some of the Advertising Mode functionality in Device Manager. While I was still in SDK 8.1, I had implemented functionality to change the Advertising Name from an IOS App. The new name was stored in pstorage and all was well. However, when I integrated the Device Manager, there are now two pstorage_init calls. I tried to combine the code from init_ p_storage with the device_manager_init, but can't seem to get the right order of things without breaking the code. When I try to combine these modules, either my Bonding/Pairing breaks or the p_storage functions don't work properly...part of the string that I try to write to flash gets written and the latter part gets jumbled. Code snippets below:

void init_p_storage(void)
{
	uint32_t                	retval;
	pstorage_module_param_t 	param;

	retval = pstorage_init();

	if(retval != NRF_SUCCESS)
	{
		printf("p_storage init error\r\n");
		bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
	}
	param.block_size  = 16;                    //Select block size of 16 bytes
	param.block_count = 4;                     //Select 10 blocks, total of 160 bytes
	param.cb          = pstorage_cb_handler;   //Set the pstorage callback handler

	//printf("P_Storage initializing ... \r\n");
	//
	// Register for pstorage
	//
	retval = pstorage_register(&param, &pstorage_handle);
	if (retval != NRF_SUCCESS)
	{
		printf("p_storage registration error\r\n");
		bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
	}
	//
	//  Get the Block Handle
	//
	retval = pstorage_block_identifier_get(&pstorage_handle, block_num, &p_block_handle);
	APP_ERROR_CHECK(retval);
	if (retval != NRF_SUCCESS)
	{
	   	printf("Error: %lu\n", retval);
	}
}

static void device_manager_init(bool erase_bonds)
{
    uint32_t               err_code;
    dm_init_param_t        init_param = {.clear_persistent_data = erase_bonds};
    dm_application_param_t register_param;

    // Initialize peer device handle.
    err_code = dm_handle_initialize(&m_bonded_peer_handle);
    APP_ERROR_CHECK(err_code);

    // Initialize persistent storage module.

    err_code = pstorage_init();
    APP_ERROR_CHECK(err_code);

    err_code = dm_init(&init_param);
    APP_ERROR_CHECK(err_code);

    memset(&register_param.sec_param, 0, sizeof(ble_gap_sec_params_t));

    register_param.sec_param.bond         = SEC_PARAM_BOND;
    register_param.sec_param.mitm         = SEC_PARAM_MITM;
    register_param.sec_param.io_caps      = SEC_PARAM_IO_CAPABILITIES;
    register_param.sec_param.oob          = SEC_PARAM_OOB;
    register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
    register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
    register_param.evt_handler            = device_manager_evt_handler;
    register_param.service_type           = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;

    err_code = dm_register(&m_app_handle, &register_param);
    APP_ERROR_CHECK(err_code);

    //app_bond_init(&m_app_bond_table);

    for(uint8_t i = 0; i < DEVICE_MANAGER_MAX_BONDS; i++)
    {
        //APP_LOG("[APP][ID: %d], Application context : %08X\r\n",m_app_bond_table.device_id[i],(unsigned int) m_app_bond_table.app_bond_cnt[i]);
    }
}


void store_DeviceName(void)
{
	uint32_t err_code;

	pstorage_clear(&p_block_handle, block_size);

	//printf("pstorage wait for clear to complete ... \r\n");

	while(pstorage_wait_flag) { power_manage(); }

	err_code = pstorage_store(&p_block_handle, pstorage_buffer, block_size, block_offset);
	if (err_code != NRF_SUCCESS)
	{
    	printf("pstorage Store Error!\r\n");
	}
}

//
// Initiate Read of Device Name from pstorage
//
void read_DeviceName(void)
{
	uint32_t err_code;

	err_code = pstorage_load(pstorage_buffer, &p_block_handle, 16, 0);

	if (err_code != NRF_SUCCESS)
	{
	    printf("pstorage Read Error!\r\n");
	}
}

Any guidance on how to combine the init functions to have only one pstorage_init() function without breaking things would be appreciated.

Thank you.

Parents Reply Children
No Data
Related