An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

Issue: The client tends to hang under certain conditions

With STM32F746ZG-nucleo under the mbed environment, if the callback function specified by the subscribe() method is empty or has a small number of instructions in its body the program hangs after the firs publish. This issue does not happen if a small wait of at least 10ms or a printf is put in the callback's body. Further investigations lead to locate the problem in MQTTNetwork::read() function: if a dummy wait is put before socket->recv() all works as expected. Application code used in this case:

/*******************************************************************************
 * Copyright (c) 2014, 2015 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial API and implementation and/or initial documentation
 *    Ian Craggs - make sure QoS2 processing works, and add device headers
 *******************************************************************************/
 
 /**
  This is a sample program to illustrate the use of the MQTT Client library
  on the mbed platform.  The Client class requires two classes which mediate
  access to system interfaces for networking and timing.  As long as these two
  classes provide the required public programming interfaces, it does not matter
  what facilities they use underneath. In this program, they use the mbed
  system libraries.
 
 */
 
 
#define logMessage printf
#define MQTTCLIENT_QOS2 1
 
#include "NetworkInterface.h"
#include "EthernetInterface.h"
#include "MQTTNetwork.h"
#include "MQTT/MQTTClient.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
 
int arrivedcount = 0;

void messageArrived(MQTT::MessageData& md)
{
// 	wait(0.01);
	
    ++arrivedcount;
}
 
 
int main(int argc, char* argv[])
{
    float version = 0.6;
    char* topic = "mbed-sample";
 
    logMessage("HelloMQTT: version is %.2f\r\n", version);
 
	EthernetInterface eth;
	eth.set_dhcp(true);
	eth.connect();
	
	printf("got ip addr %s\r\n",eth.get_ip_address());
	
    NetworkInterface* network = ð		
    if (!network) {
        return -1;
    }
 
    MQTTNetwork mqttNetwork(network);

    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
    const char* hostname = "10.0.5.251";
    int port = 1883;
    logMessage("Connecting to %s:%d\r\n", hostname, port);
    int rc = mqttNetwork.connect(hostname, port);
	
    logMessage("rc from TCP connect is %d\r\n", rc);
	
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "mbed-sample";
    data.username.cstring = "testuser";
    data.password.cstring = "testpassword";
    rc = client.connect(data);
	logMessage("rc from MQTT connect is %d\r\n", rc);
 
    rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
	logMessage("rc from MQTT subscribe is %d\r\n", rc);
 
    MQTT::Message message;
 
	int i = 1;
	
    while(1)
	{		
		char buf[100];
		sprintf(buf, "message n° %d\r\n", i);
		message.qos = MQTT::QOS0;
		message.retained = false;
		message.dup = false;
		message.payload = (void*)buf;
		message.payloadlen = strlen(buf)+1;		
		rc = client.publish(topic, message);		
		int rval = client.yield(10);

		printf("OK! i %d, rval %d\r\n", i++, rval);
		
		if(i > 3)
		{
			break;
		}
		
		wait(1);
	}
 
 
    if ((rc = client.unsubscribe(topic)) != 0)
        logMessage("rc from unsubscribe was %d\r\n", rc);
 
    if ((rc = client.disconnect()) != 0)
        logMessage("rc from disconnect was %d\r\n", rc);
 
    mqttNetwork.disconnect();
 
    logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
 
    return 0;
}