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 azure_iot_hub reconnect failed not notifying application

SDK version 1.7.1

I am working on a project involving a nRF9160 and the sample application nrf/samples/nrf9160/azire_iot_hub and working on reliability issues with disconnections and reconnections with Azure.

I have slightly modified the sample application to suit the project needs but the basic functionality remains the same.

When Azure is connected I receive an event AZURE_IOT_HUB_EVT_CONNECTED then AZURE_IOT_HUB_EVT_READY when it is ready to relay data.

When Azure is disconnected I also receive the event AZURE_IOT_HUB_EVT_DISCONNECTED in which case the application attempts to reconnect.

However, when the reconnect fails I receive no notification that the connection has failed to connect.

If I open up the source code located at nrf/subsys/net/lib/azure_iot_hub/src/azure_iot_hub.c I find on the following lines at about 1237...

int azure_iot_hub_connect(void)
{
	int err;

	if (connection_state_verify(STATE_CONNECTING)) {
		LOG_WRN("Azure IoT Hub connection establishment in progress");
		return -EINPROGRESS;
	} else if (!connection_state_verify(STATE_INIT)) {
		LOG_WRN("Azure IoT Hub is not in the initialized state");
		return -ENOENT;
	}

	connection_state_set(STATE_CONNECTING);

	err = connect_client(&conn_config);
	if (err) {
		LOG_ERR("Failed to connect MQTT client, error: %d", err);
		connection_state_set(STATE_INIT);
		return err;
	}

	k_sem_give(&connection_poll_sem);

	return 0;
}

Internal connection state is set to connection_state_set(STATE_INIT); however, there is no notification to the application layer using azure_iot_hub_notify_event() to notify the application layer of the connect failure, it seems, can this be verified?

Also, is there a recommended solution besides modification of the library?  I am considering adding the appropriate azure_iot_hub_notify_event() when connection fails.

Initially I was under the assumption that once a connection was established to the Azure service, it would retain that connection, even through reconnection if the connection was lost, but now it appears that a disconnection needs to be handled with the application layer.

With this, can I also verify the reconnection ability of the LTE connection?  If the LTE connection is interrupted will it automatically reconnect and perform a DHCP request?  If not, will this need the be handled with the application layer as well?

Sincerely,

Allan

Parents
  • I am adding the debug log that I recorded during the disconnect/reconnect event.

    [2021-11-27 04:45:55+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > MODEM_INFO_DATE_TIME = 21/11/26,19:45:53+36
    [2021-11-27 04:45:55+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > Sending event:
    [2021-11-27 04:46:53+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > AZURE_IOT_HUB_EVT_DISCONNECTED
    [2021-11-27 04:46:53+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > Failed to send event
    [2021-11-27 04:47:01+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > RRC mode: Connected
    [2021-11-27 04:47:01+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > AZURE_IOT_HUB_EVT_CONNECTING
    [2021-11-27 04:47:01+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > azure_iot_hub_connect failed: -12
    
    [2021-11-27 04:47:02+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > [32:16:02.584,136] <err> azure_iot_hub: mqtt_connect, error: -12
    [2021-11-27 04:47:02+0900][<__main__.UartLogger object at 0xb6450b08>][UartLogger]/dev/ttyUSB1 > [32:16:02.584,136] <err> azure_iot_hub: Failed to connect MQTT client, error: -12
    

  • Hello Allan, 

    I have slightly modified the sample application to suit the project needs but the basic functionality remains the same.

    Can you please elaborate more on what this means? I have forwarded this to our team

    With regards to the error azure_iot_hub: mqtt_connect, error: -12 --> #define ENOMEM 12 /* Not enough core */

    Kind regards,
    Øyvind

  • Hello again Allan, 
    Our team gives the following answers to your questions.

    Internal connection state is set to connection_state_set(STATE_INIT); however, there is no notification to the application layer using azure_iot_hub_notify_event() to notify the application layer of the connect failure, it seems, can this be verified?
    It's correct that there is no event if the transport fails to connect to Azure IoT Hub. Instead, the return code from azure_iot_hub_connect() can be checked to verify if the connection was successful or not.
    If the transport is able to connect, meaning a TLS session has been established, there will be an AZURE_IOT_HUB_EVT_CONNECTION_FAILED event if the MQTT-level connection request fails. This happens if the CONNACK message from the broker contains an erorr code, and this error code is forwarded in the AZURE_IOT_HUB_EVT_CONNECTION_FAILED event.
    Initially I was under the assumption that once a connection was established to the Azure service, it would retain that connection, even through reconnection if the connection was lost, but now it appears that a disconnection needs to be handled with the application layer.
    This is correct, connection management is all up to the application.
    With this, can I also verify the reconnection ability of the LTE connection?  If the LTE connection is interrupted will it automatically reconnect and perform a DHCP request?  If not, will this need the be handled with the application layer as well?
    The LTE connection needs to be monitored separately from the IoT Hub connection. The LTE connection may be dropped and recover again without losing the IP, but this is all up to the network and operator.
    A better approach than monitoring the LTE registration status, is to listen for PDN events. The PDN sample and library documentation may be of help to see how that can be done:
    Kind regards,
    Øyvind
  • Hi Øyvind,

    Thank you very much for the detailed responses!

    The functionality makes more sense now and I will change the application layer implement the functionality described above.

    I believe I am monitoring the PDN events (and output status changes to UART), but do not act on them as I was under the impression that reconnection, etc, was automatic.  I will look into this as well and add the necessary code to handle PDN events.

    Sincerely,
    Allan

  • Reply
    • Hi Øyvind,

      Thank you very much for the detailed responses!

      The functionality makes more sense now and I will change the application layer implement the functionality described above.

      I believe I am monitoring the PDN events (and output status changes to UART), but do not act on them as I was under the impression that reconnection, etc, was automatic.  I will look into this as well and add the necessary code to handle PDN events.

      Sincerely,
      Allan

    Children
    No Data
    Related