Commit 2d2bd98c authored by Ian Craggs's avatar Ian Craggs

MQTT 3.1.1 support - sessionPresent for MQTTClient

Bug: 436049
parent 3b79c88c
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* @cond MQTTAsync_main * @cond MQTTAsync_main
* @mainpage Asynchronous MQTT client library for C * @mainpage Asynchronous MQTT client library for C
* *
* © Copyright IBM Corp. 2009, 2013 * © Copyright IBM Corp. 2009, 2014
* *
* @brief An Asynchronous MQTT client library for C. * @brief An Asynchronous MQTT client library for C.
* *
...@@ -333,6 +333,13 @@ typedef struct ...@@ -333,6 +333,13 @@ typedef struct
MQTTAsync_message message; MQTTAsync_message message;
char* destinationName; char* destinationName;
} pub; } pub;
/* For connect, the server connected to, MQTT version used, and sessionPresent flag */
struct
{
char* serverURI;
int MQTTVersion;
int sessionPresent;
} connect;
} alt; } alt;
} MQTTAsync_successData; } MQTTAsync_successData;
......
...@@ -762,6 +762,7 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o ...@@ -762,6 +762,7 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o
{ {
MQTTClients* m = handle; MQTTClients* m = handle;
int rc = SOCKET_ERROR; int rc = SOCKET_ERROR;
int sessionPresent = 0;
FUNC_ENTRY; FUNC_ENTRY;
if (m->ma && !running) if (m->ma && !running)
...@@ -890,6 +891,8 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o ...@@ -890,6 +891,8 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o
m->c->connected = 1; m->c->connected = 1;
m->c->good = 1; m->c->good = 1;
m->c->connect_state = 0; m->c->connect_state = 0;
if (MQTTVersion == 4)
sessionPresent = connack->flags.bits.sessionPresent;
if (m->c->cleansession) if (m->c->cleansession)
rc = MQTTClient_cleanSession(m->c); rc = MQTTClient_cleanSession(m->c);
if (m->c->outboundMsgs->count > 0) if (m->c->outboundMsgs->count > 0)
...@@ -910,9 +913,17 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o ...@@ -910,9 +913,17 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o
m->pack = NULL; m->pack = NULL;
} }
} }
exit: exit:
if (rc != MQTTCLIENT_SUCCESS) if (rc == MQTTCLIENT_SUCCESS)
{
if (options->struct_version == 4) /* means we have to fill out return values */
{
options->returned.serverURI = serverURI;
options->returned.MQTTVersion = MQTTVersion;
options->returned.sessionPresent = sessionPresent;
}
}
else
{ {
Thread_unlock_mutex(mqttclient_mutex); Thread_unlock_mutex(mqttclient_mutex);
MQTTClient_disconnect1(handle, 0, 0, (MQTTVersion == 3)); /* not "internal" because we don't want to call connection lost */ MQTTClient_disconnect1(handle, 0, 0, (MQTTVersion == 3)); /* not "internal" because we don't want to call connection lost */
...@@ -1046,7 +1057,7 @@ int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) ...@@ -1046,7 +1057,7 @@ int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
if (strncmp(options->struct_id, "MQTC", 4) != 0 || if (strncmp(options->struct_id, "MQTC", 4) != 0 ||
(options->struct_version != 0 && options->struct_version != 1 && options->struct_version != 2 (options->struct_version != 0 && options->struct_version != 1 && options->struct_version != 2
&& options->struct_version != 3)) && options->struct_version != 3 && options->struct_version != 4))
{ {
rc = MQTTCLIENT_BAD_STRUCTURE; rc = MQTTCLIENT_BAD_STRUCTURE;
goto exit; goto exit;
...@@ -1298,6 +1309,8 @@ int MQTTClient_subscribe(MQTTClient handle, char* topic, int qos) ...@@ -1298,6 +1309,8 @@ int MQTTClient_subscribe(MQTTClient handle, char* topic, int qos)
FUNC_ENTRY; FUNC_ENTRY;
rc = MQTTClient_subscribeMany(handle, 1, &topic, &qos); rc = MQTTClient_subscribeMany(handle, 1, &topic, &qos);
if (qos == MQTT_BAD_SUBSCRIBE) /* addition for MQTT 3.1.1 - error code from subscribe */
rc = MQTT_BAD_SUBSCRIBE;
FUNC_EXIT_RC(rc); FUNC_EXIT_RC(rc);
return rc; return rc;
} }
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
* @endcond * @endcond
* @cond MQTTClient_main * @cond MQTTClient_main
* @mainpage MQTT Client library for C * @mainpage MQTT Client library for C
* © Copyright IBM Corp. 2009, 2013 * © Copyright IBM Corp. 2009, 2014
* *
* @brief An MQTT client library in C. * @brief An MQTT client library in C.
* *
...@@ -173,6 +173,10 @@ ...@@ -173,6 +173,10 @@
* MQTT version to connect with: 3.1.1 * MQTT version to connect with: 3.1.1
*/ */
#define MQTTVERSION_3_1_1 4 #define MQTTVERSION_3_1_1 4
/**
* Bad return code from subscribe, as defined in the 3.1.1 specification
*/
#define MQTT_BAD_SUBSCRIBE 0x80
/** /**
* A handle representing an MQTT client. A valid client handle is available * A handle representing an MQTT client. A valid client handle is available
...@@ -504,11 +508,12 @@ typedef struct ...@@ -504,11 +508,12 @@ typedef struct
{ {
/** The eyecatcher for this structure. must be MQTC. */ /** The eyecatcher for this structure. must be MQTC. */
char struct_id[4]; char struct_id[4];
/** The version number of this structure. Must be 0, 1, 2 or 3. /** The version number of this structure. Must be 0, 1, 2, 3 or 4.
* 0 signifies no SSL options and no serverURIs * 0 signifies no SSL options and no serverURIs
* 1 signifies no serverURIs * 1 signifies no serverURIs
* 2 signifies no MQTTVersion * 2 signifies no MQTTVersion
*/ * 3 signifies no returned values
*/
int struct_version; int struct_version;
/** The "keep alive" interval, measured in seconds, defines the maximum time /** The "keep alive" interval, measured in seconds, defines the maximum time
* that should pass without communication between the client and the server * that should pass without communication between the client and the server
...@@ -599,15 +604,24 @@ typedef struct ...@@ -599,15 +604,24 @@ typedef struct
*/ */
char** serverURIs; char** serverURIs;
/** /**
* Sets the version of MQTT to be used on the connect. * Sets the version of MQTT to be used on the connect.
* MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that fails, fall back to 3.1 * MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that fails, fall back to 3.1
* MQTTVERSION_3_1 (3) = only try version 3.1 * MQTTVERSION_3_1 (3) = only try version 3.1
* MQTTVERSION_3_1_1 (4) = only try version 3.1.1 * MQTTVERSION_3_1_1 (4) = only try version 3.1.1
*/ */
int MQTTVersion; int MQTTVersion;
/**
* Returned from the connect when the MQTT version used to connect is 3.1.1
*/
struct
{
char* serverURI; /**< the serverURI connected to */
int MQTTVersion; /**< the MQTT version used to connect with */
int sessionPresent; /**< if the MQTT version is 3.1.1, the value of sessionPresent returned in the connack */
} returned;
} MQTTClient_connectOptions; } MQTTClient_connectOptions;
#define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 3, 60, 1, 1, NULL, NULL, NULL, 30, 20, NULL, 0, NULL, 0} #define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 4, 60, 1, 1, NULL, NULL, NULL, 30, 20, NULL, 0, NULL, 0}
/** /**
* MQTTClient_libraryInfo is used to store details relating to the currently used * MQTTClient_libraryInfo is used to store details relating to the currently used
......
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2013 IBM Corp. * Copyright (c) 2009, 2014 IBM Corp.
* *
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* Contributors: * Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation * Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs, Allan Stockdill-Mander - SSL updates * Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - MQTT 3.1.1 support
*******************************************************************************/ *******************************************************************************/
#if !defined(MQTTPACKET_H) #if !defined(MQTTPACKET_H)
...@@ -120,6 +121,23 @@ typedef struct ...@@ -120,6 +121,23 @@ typedef struct
typedef struct typedef struct
{ {
Header header; /**< MQTT header byte */ Header header; /**< MQTT header byte */
union
{
unsigned char all; /**< all connack flags */
#if defined(REVERSED)
struct
{
unsigned int reserved : 7; /**< message type nibble */
bool sessionPresent : 1; /**< was a session found on the server? */
} bits;
#else
struct
{
bool sessionPresent : 1; /**< was a session found on the server? */
unsigned int reserved : 7; /**< message type nibble */
} bits;
#endif
} flags; /**< connack flags byte */
char rc; /**< connack return code */ char rc; /**< connack return code */
} Connack; } Connack;
......
...@@ -123,7 +123,7 @@ void* MQTTPacket_connack(unsigned char aHeader, char* data, int datalen) ...@@ -123,7 +123,7 @@ void* MQTTPacket_connack(unsigned char aHeader, char* data, int datalen)
FUNC_ENTRY; FUNC_ENTRY;
pack->header.byte = aHeader; pack->header.byte = aHeader;
readChar(&curdata); /* reserved byte */ pack->flags.all = readChar(&curdata);
pack->rc = readChar(&curdata); pack->rc = readChar(&curdata);
FUNC_EXIT; FUNC_EXIT;
return pack; return pack;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment