Hi,
I am using CC3200 to implement applicant which use MQTT client lib provided by SDK.
By doing some experiment on SDK MQTT client example, I found some weird case.
Please note I already patch SDK know issues for MQTT lib.
Followings is my experiment:
In MQTT client example, rewrite it to let it not enter main while loop but just repeat to connect and disconnect from broker
==================================================
repeat2:
//
// Initialze MQTT client lib
//
lRetVal = sl_ExtLib_MqttClientInit(&Mqtt_Client);
if(lRetVal != 0)
{
// lib initialization failed
UART_PRINT("MQTT Client lib initialization failed\n\r");
LOOP_FOREVER();
}
repeat1:
/******************* connection to the broker ***************************/
iNumBroker = sizeof(usr_connect_config)/sizeof(connect_config);
if(iNumBroker > MAX_BROKER_CONN)
{
UART_PRINT("Num of brokers are more then max num of brokers\n\r");
LOOP_FOREVER();
}
while(iCount < iNumBroker)
{
//create client context
local_con_conf[iCount].clt_ctx =
sl_ExtLib_MqttClientCtxCreate(&local_con_conf[iCount].broker_config,
&local_con_conf[iCount].CallBAcks,
&(local_con_conf[iCount]));
UART_PRINT("Johnson 01\n\r");
//
// Set Client ID
//
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_CLIENT_ID,
local_con_conf[iCount].client_id,
strlen((char*)(local_con_conf[iCount].client_id)));
//
// Set will Params
//
if(local_con_conf[iCount].will_params.will_topic != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_WILL_PARAM,
&(local_con_conf[iCount].will_params),
sizeof(SlMqttWill_t));
}
//
// setting username and password
//
if(local_con_conf[iCount].usr_name != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_USER_NAME,
local_con_conf[iCount].usr_name,
strlen((char*)local_con_conf[iCount].usr_name));
if(local_con_conf[iCount].usr_pwd != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_PASS_WORD,
local_con_conf[iCount].usr_pwd,
strlen((char*)local_con_conf[iCount].usr_pwd));
}
}
UART_PRINT("Johnson 02\n\r");
//
// connectin to the broker
//
if((sl_ExtLib_MqttClientConnect((void*)local_con_conf[iCount].clt_ctx,
local_con_conf[iCount].is_clean,
local_con_conf[iCount].keep_alive_time) & 0xFF) != 0)
{
UART_PRINT("\n\rBroker connect fail for conn no. %d \n\r",iCount+1);
//delete the context for this connection
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
break;
}
else
{
UART_PRINT("\n\rSuccess: conn to Broker no. %d\n\r ", iCount+1);
local_con_conf[iCount].is_connected = true;
iConnBroker++;
}
UART_PRINT("Johnson 03\n\r");
//
// Subscribe to topics
//
if(sl_ExtLib_MqttClientSub((void*)local_con_conf[iCount].clt_ctx,
local_con_conf[iCount].topic,
local_con_conf[iCount].qos, TOPIC_COUNT) < 0)
{
UART_PRINT("\n\r Subscription Error for conn no. %d\n\r", iCount+1);
UART_PRINT("Disconnecting from the broker\r\n");
sl_ExtLib_MqttClientDisconnect(local_con_conf[iCount].clt_ctx);
local_con_conf[iCount].is_connected = false;
//delete the context for this connection
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
iConnBroker--;
break;
}
else
{
int iSub;
UART_PRINT("Client subscribed on following topics:\n\r");
for(iSub = 0; iSub < local_con_conf[iCount].num_topics; iSub++)
{
UART_PRINT("%s\n\r", local_con_conf[iCount].topic[iSub]);
}
}
iCount++;
}
if(iConnBroker < 1)
{
//
// no succesful connection to broker
//
goto end;
}
iCount = 0;
iNumBroker = 0;
iConnBroker = 0;
osi_Sleep(5);
sl_ExtLib_MqttClientDisconnect(local_con_conf[iCount].clt_ctx);
local_con_conf[iCount].is_connected = false;
//delete the context for this connection
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
osi_Sleep(5);
goto repeat1;
lRetVal = sl_ExtLib_MqttClientExit();
if (lRetVal != 0)
UART_PRINT("MQTT exit failed\n\r");
osi_Sleep(5);
goto repeat2;
=================================================
So far, every thing is good, CC3200 will connect to desired AP and connect to desired broker, and then disconnect and then connect.
However, if I comment out the "goto repeat1" line in the code, and run into repeat2 loop, this time, it will blocked while it is trying to reconnect
to broker after doing sl_ExtLib_MqttClientExit(). The sl_ExtLib_MqttClientConnect function is never return.
Is there something not clean in sl_ExtLib_MqttClientExit() ?
I also encounter the same behavior while I am following experiment:
1. Connect to desired AP
2. Connect to desired broker
3. unplug the power supply of the AP to cause a wlan disconnect.
4. My code will then take recover procedure and reconnect to AP (by repeat the exactly same procedure in step 1)
5. MQTT client then try to reconnect to broker without doing sl_ExtLib_MqttClientExit (the same procedure in previous repeat1 loop)
6. dada, sl_ExtLib_MqttClientConnect is no return like repeat2 case.
Please point out if there is any thing I am missed to build such kind of application.
By the way, the client example will also hang after broker connection is drop and it receive disconnect callback.
Maybe this is caused by doing sl_ExtLib_MqttClientCtxDelete in mqtt lib's context.