paho mqtt调用MQTTClient_connect失败,返回MQTTCLIENT_BAD_STRUCTURE(-8)

1、问题描述
使用编译好的库文件,从githup上下载头文件,运行时MQTTClient_connect调用失败,返回MQTTCLIENT_BAD_STRUCTURE;
MQTTCLIENT_BAD_STRUCTURE在MQTTClient.h中是struct_id或struct_version 不正确,其定义为:

/**
 * Return code: A structure parameter does not have the correct eyecatcher
 * and version number.
 */
#define MQTTCLIENT_BAD_STRUCTURE -8

2、问题原因
该问题是由于mqtt库版本为1.1,而头文件版本为1.3,1.3版本的mqtt struct_version 值为6,在1.1版本不兼容,以下是mqtt 1.1和1.3版本MQTTClient_connect函数部分代码:

//paho mqtt 1.1版本
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
	MQTTClients* m = handle;
	int rc = SOCKET_ERROR;

	FUNC_ENTRY;
	Thread_lock_mutex(connect_mutex);
	Thread_lock_mutex(mqttclient_mutex);

	if (options == NULL)
	{
		rc = MQTTCLIENT_NULL_PARAMETER;
		goto exit;
	}

	if (strncmp(options->struct_id, "MQTC", 4) != 0 || 
		(options->struct_version != 0 && options->struct_version != 1 && options->struct_version != 2
			&& options->struct_version != 3 && options->struct_version != 4))
	{
		rc = MQTTCLIENT_BAD_STRUCTURE;
		goto exit;
	}
	.......
}
//paho mqtt 1.3版本
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
	MQTTClients* m = handle;

	if (m->c->MQTTVersion >= MQTTVERSION_5)
		return MQTTCLIENT_WRONG_MQTT_VERSION;

	MQTTResponse response = MQTTClient_connectAll(handle, options, NULL, NULL);

	return response.reasonCode;
}

MQTTResponse MQTTClient_connectAll(MQTTClient handle, MQTTClient_connectOptions* options,
		MQTTProperties* connectProperties, MQTTProperties* willProperties)
{
	MQTTClients* m = handle;
	MQTTResponse rc = MQTTResponse_initializer;

	FUNC_ENTRY;
	Thread_lock_mutex(connect_mutex);
	Thread_lock_mutex(mqttclient_mutex);

	rc.reasonCode = SOCKET_ERROR;
	if (options == NULL)
	{
		rc.reasonCode = MQTTCLIENT_NULL_PARAMETER;
		goto exit;
	}

	if (strncmp(options->struct_id, "MQTC", 4) != 0 || options->struct_version < 0 || options->struct_version > 6)
	{
		rc.reasonCode = MQTTCLIENT_BAD_STRUCTURE;
		goto exit;
	}
	.....
}

你可能感兴趣的:(linux,错误记录)