v2.9.0: azure_iot_hub_dps_reset() does not reset DPS registration

According to the documentation, you should run azure_iot_hub_dps_reset() in order to force DPS registration again.

But if the device has previously been enrolled and a device created using DPS, and then the keys changed on the device, the reset function doesn't remove the required keys to trigger DPS.

Temporarily fixed by adding a "return 0" as very first thing in dps_settings_handler() in order for the clauses where reg status = AZURE_IOT_HUB_DPS_REG_STATUS_ASSIGNED is set.

The proper fix is for the reset function, or rather, azure_iot_hub_dps_hostname_delete() and azure_iot_hub_dps_device_id_delete(), to _not_ return err on unsuccessful deletion of the first part (key), so that second key (len) can be correctly deleted, i.e.:

void azure_iot_hub_dps_hostname_delete(void)
{
	settings_delete(DPS_SETTINGS_KEY "/" DPS_SETTINGS_HOSTNAME_KEY);
	settings_delete(DPS_SETTINGS_KEY "/" DPS_SETTINGS_HOSTNAME_LEN_KEY);
	dps_reg_ctx.assigned_hub = AZ_SPAN_EMPTY;
}

or possibly:

int azure_iot_hub_dps_hostname_delete(void)
{
	int err1 = 0, err2 = 0;

	err1 = settings_delete(DPS_SETTINGS_KEY "/" DPS_SETTINGS_HOSTNAME_KEY);
	if (err1) {
		LOG_ERR("Failed to delete Azure IoT Hub hostname, error: %d", err1);
	}

	err2 = settings_delete(DPS_SETTINGS_KEY "/" DPS_SETTINGS_HOSTNAME_LEN_KEY);
	if (err2) {
		LOG_ERR("Failed to delete Azure IoT Hub hostname length, error: %d", err2);
	}

	dps_reg_ctx.assigned_hub = AZ_SPAN_EMPTY;

	return err1 + err2;
}

This issue took me quite some time to figure out and it essentially bricks a device, so I hope it'll be fixed in an upcoming release.

Parents
  • Hi,

    According to the documentation, you should run azure_iot_hub_dps_reset() in order to force DPS registration again.

    But if the device has previously been enrolled and a device created using DPS, and then the keys changed on the device, the reset function doesn't remove the required keys to trigger DPS.

    Which keys are you referring to? Can you elaborate on these steps?

    Here is what Azure DPS service says about deletion of the stored assignment information:

    To delete the stored assignment information, call the azure_iot_hub_dps_reset() function. Alternatively, you can call the functions azure_iot_hub_dps_hostname_delete() or azure_iot_hub_dps_device_id_delete() to delete specific information. After calling the azure_iot_hub_dps_reset() function, the library must be initialized again. After the initialization, a new registration with the DPS can be started by calling the azure_iot_hub_dps_start() function.

    Have you followed these steps but have not been able to delete stored assignment information? Have you re-initialized the library after using azure_iot_hub_dps_reset()?

    Best regards,
    Dejan

  • To delete the stored assignment information, call the azure_iot_hub_dps_reset() function

    I called this function the very first thing in main, prior to my normal code that works perfectly fine for performing DPS registration. No DPS registration was performed.

    I rebooted the device. Same result.

    I rebooted again, in debugger, rebooting directly after azure_iot_hub_dps_reset() was called, modified the program to remove that call, loaded into the device, started, still no DPS registration.

    Which keys are you referring to?

    DPS_SETTINGS_foo in azure_iot_hub_dps_private.h

    ----

    To clarify:

    1. The device performed DPS registration and created a device in IoTHub.

    2. I changed the keys on the device using nrfcredstore and updated the DPS enrollment thingy on Azure.

    3. When I started again, DPS was not performed.

    I'm suspecting that because that DPS was performed previously, and even though I did run the reset function, the setings table got into an undefined state that caused DPS to never run.

    The workaround I did was to make sure the DPS code thought it had loaded empty keys, forcing it to run DPS registration, specifically so "dps_reg_ctx.status = AZURE_IOT_HUB_DPS_REG_STATUS_ASSIGNED;" was never called.

    As to why, I'm just speculating. I followed the instructions by calling the reset function first thing, and the code didn't do what it said it should do. I'd say that's a bug.

  • Hi,

    Thank you for additional information and clarification.

    I will look further into this issue. I expect to get back to you by the end of the week.

    Best regards,
    Dejan

  • Hi,

    dejans said:

    Here is what Azure DPS service says about deletion of the stored assignment information:

    To delete the stored assignment information, call the azure_iot_hub_dps_reset() function. Alternatively, you can call the functions azure_iot_hub_dps_hostname_delete() or azure_iot_hub_dps_device_id_delete() to delete specific information. After calling the azure_iot_hub_dps_reset() function, the library must be initialized again. After the initialization, a new registration with the DPS can be started by calling the azure_iot_hub_dps_start() function.

    Have you followed these steps but have not been able to delete stored assignment information? Have you re-initialized the library after using azure_iot_hub_dps_reset()?

    Can you please verify that you initialized the library prior to using azure_iot_hub_dps_reset()? If you have not done that, the API is expected to fail.
    The initialization of the library can be done in 2 ways:
    1. If you set azure_iot_hub_config.use_dps, DPS will be initialized when azure_iot_hub_init() is called.
    2. if not, you need to call azure_iot_hub_dps_init() yourself

    Best regards,
    Dejan

  • I had .use_dps = true.

    Unrolling my code (based on the iothub example):

    azure_iot_hub_dps_reset();
    
    /* Using the device ID as DPS registration ID. */
    int err = dps_run(&cfg.hostname, &cfg.device_id);
    if (err) {
    LOG_ERR("Failed to run DPS, error: %d, terminating connection attempt", err);
    return -1;
    }
    
    err = azure_iot_hub_init(azure_event_handler);
    if (err) {
    LOG_ERR("Azure IoT Hub could not be initialized, error: %d", err);
    return -2;
    }

    The documentation does _not_ state that azure_iot_hub_init() has to be called prior to calling azure_iot_hub_dps_reset().

    Since that was not stated, I assumed it had to be called before loading the data.

    So it would be a good idea to make a note in the documentation that azure_iot_hub_dps_reset() is valid in this order, and this order only:

    azure_iot_hub_init() 
    
    //azure_iot_hub_dps_init(); // if use_dps == false
    
    azure_iot_hub_dps_reset()
    
    azure_iot_hub_init() 
    
    //azure_iot_hub_dps_init(); // if use_dps == false

  • Hi,

    azure_iot_hub_init()
    
    //azure_iot_hub_dps_init(); // if use_dps == false
    
    azure_iot_hub_dps_reset()
    
    azure_iot_hub_init()
    
    //azure_iot_hub_dps_init(); // if use_dps == false


    Did your problem get resolved when you used mentioned order?

    Best regards,
    Dejan

Reply Children
Related