This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

nRF9160 azure_iot_hub change DPS username during run time

I have been working with the Azure iot hub sample appilcation since version 1.5.0 and use an SD card to store the authentication certificates and other information to connect to Azure.

This is done so the application programmed to each nRF9160 is identical and the only differences are on the SD card.  During first run and initial Azure provisioning I copy the certificates from the SD card into the nRF9160 and mark them as copied, then proceed to the Azure provisioning, which results are also stored on the SD card.  The results returned from the DPS registration are also then stored on the SD card for future connections.

Since version 1.5.0 I have always had to make a change to the file /opt/nordic/ncs/v1.9.1/nrf/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c to allow setting the DPS username at run time instead of in the defines.  This is required as the username is different depending on which Azure IoT hub the device will be provisioning to.  May I propose the following change, or something similar to allow setting of the Azure provisioning username at runtime?

This was also added to the prj.conf file. CONFIG_AZURE_IOT_HUB_DPS_ID_SCOPE="%s"

I created and am currently using "azure_provisioning_idscope" but presently it requires it to be defined somewhere, but this could be implemented in another way if possible.

I am proposing a change in later releases if possible, I am including the changes I have been making in azure_iot_hub.c up to this point.

Basically a way to dynamically set the MQTT user name used during Azure DPS at run time before provisioning occurs.  In my case from the provisioning data on the SD card.

Sincerely,

Allan

diff --git a/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c b/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c
index f31d62830..74d41e6b7 100644
--- a/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c
+++ b/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c
@@ -866,9 +866,19 @@ static int client_broker_init(struct mqtt_client *const client, bool dps)
                        .utf8 = dps_user_name_buf,
                };
 
+           // EDITED TO ALLOW DYNAMIC SETTING FROM APPLICATION
+        /* BEGIN ORIGINAL
                len = snprintk(dps_user_name_buf,
                               sizeof(dps_user_name_buf),
                               DPS_USER_NAME, conn_config.device_id);
+               END ORIGINAL */
+               /* BEGIN INSERT */
+               extern uint8_t azure_provisioning_idscope[32];
+               len = snprintk(dps_user_name_buf,
+                              sizeof(dps_user_name_buf),
+                              DPS_USER_NAME, azure_provisioning_idscope, conn_config.device_id);
+        printk("dps_user_name_buf = %s\n", dps_user_name_buf);
+               /* END INSERT */
                if ((len < 0) || len > sizeof(dps_user_name_buf)) {
                        LOG_ERR("Failed to set DPS user name");
                        return -EFAULT;

Related