2022-04-02 09:47 AM
In this loop the mqtt client is created, connected and as long as the mqtt server/broker is up, the client continues processing messages. But when the remote mqtt server/broker is manually shut down, and after the timeout expires, nxd_mqtt_client_connect is called repeatedly. For the first six iterations it acquires socket descriptors starting with the value of 0 and that increases to 5. There are only 6 sockets available in the Murata modem. After that the socket descriptor is not correct. The mqtt client cannot reconnect when the remote mqtt server/broker is again started.
*************************** Trace ****************
client disconnected from server
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 0
LOG ERROR #7: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 1
LOG ERROR #8: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 2
LOG ERROR #9: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 3
LOG ERROR #10: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 4
LOG ERROR #11: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 5
LOG ERROR #12: channel=1 / errorId=7 / gravity=2
com connect status 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
Modem state : 5
IP address : 100.96.129.90
Trying to connect MQTT broker
MQTT client socket binding status 0
com socket create success socket fd 4294967295
MQTT client socket 1413697568
MQTT tcp client socket connect status 86
MQTT tcp client socket unbind status 0
MQTT connect status 65541
***************** Loop fragment ********
while (1) {
switch (state) {
case 0:
/* Wait Link/Data is enabled before to do/send data on network. */
status = nx_ip_interface_status_check(p_IpInstance, 0U, NX_IP_LINK_ENABLED, &link_status,
10000); /* 3); => MISRAC2012-Rule-7.2 */
if (status == (UINT)NX_SUCCESS)
{
state = 2;
}
case 1:
t4 = HAL_GetTick();
if ((t4-t3)>(1*10*1000)) {
t3 = HAL_GetTick();
state = 2;
}
break;
case 2:
(void)printf("App starting MQTT creation\n\r");
status = nxd_mqtt_client_create(&mqtt_client, "my_client",
CLIENT_ID_STRING, STRLEN(CLIENT_ID_STRING), p_IpInstance, p_AppPool,
(VOID*)mqtt_client_stack, sizeof(mqtt_client_stack),
MQTT_THREAD_PRIORTY, NX_NULL, 0);
if (status == NXD_MQTT_SUCCESS) {
nxd_mqtt_client_login_set(&mqtt_client,
MQTTNAME, STRLEN(MQTTNAME),
MQTTPASS, STRLEN(MQTTPASS));
/* Register the disconnect notification function. */
nxd_mqtt_client_disconnect_notify_set(&mqtt_client, my_disconnect_func);
/* Create an event flag for this demo. */
ev_status = tx_event_flags_create(&mqtt_app_flag, "my app event");
server_ip.nxd_ip_version = 4;
server_ip.nxd_ip_address.v4 = MQTT_SERVER_ADDRESS;
state = 3;
t5 = HAL_GetTick();
}
break;
case 3:
/* Start the connection to the server. */
(void)printf("Trying to connect MQTT broker\n\r");
nxd_mqtt_client_login_set(&mqtt_client,
MQTTNAME, STRLEN(MQTTNAME),
MQTTPASS, STRLEN(MQTTPASS));
status = nxd_mqtt_client_connect(&mqtt_client, &server_ip, NXD_MQTT_PORT,
MQTT_KEEP_ALIVE_TIMER, 0, 10000);//NX_WAIT_FOREVER
(void)printf("MQTT connect status %u \n\r", status);
if (status == NXD_MQTT_SUCCESS || status == NXD_MQTT_ALREADY_CONNECTED) {
/* Subscribe to the topic with QoS level 0. */
nxd_mqtt_client_subscribe(&mqtt_client, TOPIC_NAME2, STRLEN(TOPIC_NAME2),
QOS0);
/* Set the receive notify function. */
nxd_mqtt_client_receive_notify_set(&mqtt_client, my_notify_func);
state = 4;
break;
} else if (status == NXD_MQTT_CONNECTING) {
state = 3;
break;
}
else {
t6 = HAL_GetTick();
if ((t6-t5)>(1*15*1000)) {
nxd_mqtt_client_delete(&mqtt_client);
state = 2;
t5 = HAL_GetTick();
break;
}
}
*/
break;
case 4:
/* Publish a message with QoS Level 1. */
state = 6;
t2 = HAL_GetTick();
if ((t2-t1)>(1*30*1000)) {
t1 = HAL_GetTick();
(void)printf("MQTT publishing start \n\r");
status = nxd_mqtt_client_publish(&mqtt_client, TOPIC_NAME,
STRLEN(TOPIC_NAME), (CHAR*)MESSAGE_STRING,
STRLEN(MESSAGE_STRING), 0, QOS1, 10000);
(void)printf("MQTT publish status %u \n\r", status);
if (status == NXD_MQTT_SUCCESS) {
state = 6;
} else
{
state = 3;
t5 = HAL_GetTick();
}
}
tx_event_flags_get(&mqtt_app_flag, DEMO_ALL_EVENTS,
TX_OR_CLEAR, &events, 5000);
break;
case 5:
break;
case 6:
if(events & DEMO_MESSAGE_EVENT)
{
nxd_mqtt_client_message_get(&mqtt_client, topic_buffer,
sizeof(topic_buffer), &topic_length, message_buffer,
sizeof(message_buffer), &message_length);
topic_buffer[topic_length] = 0;
message_buffer[message_length] = 0;
printf("topic = %s, message = %s\n\r", topic_buffer, message_buffer);
}
state = 4;
break;
}
}