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

Why is DEVICE_MANAGER_APP_CONTEXT_SIZE set to 16 bytes in hrs?

Hi,

I've read a couple of threads in this forum that suggested that the DEVICE_MANAGER_APP_CONTEXT_SIZE value (which should be a multiple of 4) is supposed to be 16 bytes as it is set in the hrs with dfu example.

I don't understand, however, why DEVICE_MANAGER_APP_CONTEXT_SIZE is 16 and not 4 bytes in the hrs sample app. It seems that that reserved space is only used to store (and check/clear) the value of DFU_APP_ATT_TABLE_CHANGED in the dfu_app_peer_data_set() of dfu_app_hander.c and app_context_load() in the main.c .

Are the rest 3 words in the reserved space per bonded device just spare space?

I also should mention that we are currently working with the SDK 10.0, and s110 v8.0.0 on an nrf51 based peripheral node. We set DEVICE_MANAGER_APP_CONTEXT_SIZE to 4 bytes, which seemed to be sufficient for our custom app storage per bonded node requirements, but then we noticed that this was conflicting with the 4 bytes for the DFU_APP_ATT_TABLE_CHANGED (which not only overwrite our data but are also deleted at some point with a dm_application_context_delete() in app_context_load).

So I'd like to better understand what is accounted for in the 16 bytes assigned to DEVICE_MANAGER_APP_CONTEXT_SIZE, in case we are missing something else, too.

If we only need to reserve 4 bytes per bonded device in our custom app, would setting DEVICE_MANAGER_APP_CONTEXT_SIZE to 8 (and then making the adjustments to ensure that we don't conflict with or overwrite the DFU_APP_ATT_TABLE_CHANGED flag) would that be ok for us?


Edit: I'm updating this after accepting Vidar's answer, in case it helps someone else:

As a follow-up to this. I've tried 8 bytes and the 16 bytes approaches for the DEVICE_MANAGER_APP_CONTEXT_SIZE. Both seem to work fine; eventually we went with 16 bytes to allow for some spare space possible future small persistent storage per bonded device purposes.

As noted, we had to change the code for methods that were getting and setting application context such as dfu_app_peer_data_set() in dfu_app_handler.c and app_context_load() in main.c, and our own custom methods that stored app context data.

So for example the updated app_context_load() in main.c looks like this now:

static void app_context_load(dm_handle_t const * p_handle)
{
    uint32_t                 err_code;
    static uint32_t          context_data[DEVICE_MANAGER_APP_CONTEXT_SIZE / sizeof(uint32_t)];	// allow for >4  bytes context data  // new
    dm_application_context_t context;

    context.len    = DEVICE_MANAGER_APP_CONTEXT_SIZE; // sizeof(context_data); // this is redundant here since the call to dm_application_context_get will update the len value
    context.p_data = (uint8_t *)context_data; // new 

    err_code = dm_application_context_get(p_handle, &context);
    if (err_code == NRF_SUCCESS)
    {
        // Send Service Changed Indication if ATT table has changed.
        if ((context_data[0] & (DFU_APP_ATT_TABLE_CHANGED << DFU_APP_ATT_TABLE_POS)) != 0)
        {
            err_code = sd_ble_gatts_service_changed(m_conn_handle, APP_SERVICE_HANDLE_START, BLE_HANDLE_MAX);
            if (checkForCommonBLEErrorCode(err_code)) // 
            {
                APP_ERROR_HANDLER(err_code);
            }
        }
	    // commented out the dm_application_context_delete() call here
        // err_code = dm_application_context_delete(p_handle); 
		// new: don't delete, just clear the flag position
		context.len    = DEVICE_MANAGER_APP_CONTEXT_SIZE;
		context_data[0] = 0;
		// context_data[1] - context_data[(DEVICE_MANAGER_APP_CONTEXT_SIZE/sizeof(context_data)) -1]  is maintained
		err_code = dm_application_context_set(p_handle,&context);
        APP_ERROR_CHECK(err_code);
    }
    else if (err_code == DM_NO_APP_CONTEXT)
    {
        // No context available. Ignore.
    }
    else
    {
        APP_ERROR_HANDLER(err_code);
    }
}
Related