Commit 77cc4fb9 authored by Ian Craggs's avatar Ian Craggs

Add cleanstart and session expiry to async test/client

parent ced0d0ef
......@@ -94,7 +94,8 @@ typedef struct
const char* username; /**< MQTT v3.1 user name */
int passwordlen; /**< MQTT password length */
const void* password; /**< MQTT v3.1 binary password */
unsigned int cleansession : 1; /**< MQTT clean session flag */
unsigned int cleansession : 1; /**< MQTT V3 clean session flag */
unsigned int cleanstart : 1; /**< MQTT V5 clean start flag */
unsigned int connected : 1; /**< whether it is currently connected */
unsigned int good : 1; /**< if we have an error on the socket we turn this off */
unsigned int ping_outstanding : 1;
......@@ -113,6 +114,7 @@ typedef struct
MQTTClient_persistence* persistence; /* a persistence implementation */
void* context; /* calling context - used when calling disconnect_internal */
int MQTTVersion;
int sessionExpiry; /**< MQTT 5 session expiry */
#if defined(OPENSSL)
MQTTClient_SSLOptions *sslopts;
SSL_SESSION* session; /***< SSL session pointer for fast handhake */
......
......@@ -1913,7 +1913,7 @@ static int MQTTAsync_completeConnection(MQTTAsyncs* m, MQTTPacket* pack)
m->c->connected = 1;
m->c->good = 1;
m->c->connect_state = 0;
if (m->c->cleansession)
if (m->c->cleansession || m->c->cleanstart)
rc = MQTTAsync_cleanSession(m->c);
if (m->c->outboundMsgs->count > 0)
{
......@@ -2360,7 +2360,8 @@ static void MQTTAsync_closeSession(Clients* client, enum MQTTReasonCodes reasonC
FUNC_ENTRY;
MQTTAsync_closeOnly(client, reasonCode, props);
if (client->cleansession)
if (client->cleansession ||
(client->MQTTVersion >= MQTTVERSION_5 && client->sessionExpiry == 0))
MQTTAsync_cleanSession(client);
FUNC_EXIT;
......@@ -2555,6 +2556,25 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
rc = MQTTASYNC_BAD_UTF8_STRING;
goto exit;
}
if (options->MQTTVersion >= MQTTVERSION_5 && options->struct_version < 6)
{
rc = MQTTASYNC_BAD_STRUCTURE;
goto exit;
}
if (options->MQTTVersion >= MQTTVERSION_5 && options->cleansession != 0)
{
rc = MQTTASYNC_BAD_MQTTV5_OPTIONS;
goto exit;
}
if (options->MQTTVersion < MQTTVERSION_5)
{
if (options->cleanstart != 0 || options->onFailure5 || options->onSuccess5 ||
options->connectProperties || options->willProperties)
{
rc = MQTTASYNC_BAD_MQTTV5_OPTIONS;
goto exit;
}
}
m->connect.onSuccess = options->onSuccess;
m->connect.onFailure = options->onFailure;
......@@ -2728,28 +2748,36 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
free(m->connectProps);
m->connectProps = NULL;
}
if (options->struct_version >=6 && options->connectProperties)
{
MQTTProperties initialized = MQTTProperties_initializer;
m->connectProps = malloc(sizeof(MQTTProperties));
*m->connectProps = initialized;
*m->connectProps = MQTTProperties_copy(options->connectProperties);
}
if (m->willProps)
{
MQTTProperties_free(m->willProps);
free(m->willProps);
m->willProps = NULL;
}
if (options->struct_version >=6 && options->willProperties)
if (options->struct_version >=6)
{
MQTTProperties initialized = MQTTProperties_initializer;
if (options->connectProperties)
{
MQTTProperties initialized = MQTTProperties_initializer;
m->willProps = malloc(sizeof(MQTTProperties));
*m->willProps = initialized;
*m->willProps = MQTTProperties_copy(options->willProperties);
m->connectProps = malloc(sizeof(MQTTProperties));
*m->connectProps = initialized;
*m->connectProps = MQTTProperties_copy(options->connectProperties);
if (MQTTProperties_hasProperty(options->connectProperties, SESSION_EXPIRY_INTERVAL))
m->c->sessionExpiry = MQTTProperties_getNumericValue(options->connectProperties,
SESSION_EXPIRY_INTERVAL);
}
if (options->willProperties)
{
MQTTProperties initialized = MQTTProperties_initializer;
m->willProps = malloc(sizeof(MQTTProperties));
*m->willProps = initialized;
*m->willProps = MQTTProperties_copy(options->willProperties);
}
m->c->cleanstart = options->cleanstart;
}
/* Add connect request to operation queue */
......
......@@ -180,6 +180,10 @@
* Return code: protocol prefix in serverURI should be tcp:// or ssl://
*/
#define MQTTASYNC_BAD_PROTOCOL -14
/**
* Return code: don't use MQTTV5 options if MQTT 3 is chosen
*/
#define MQTTASYNC_BAD_MQTTV5_OPTIONS -15
/**
......@@ -989,7 +993,11 @@ typedef struct
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 6, 60, 1, 10, NULL, NULL, NULL, 30, 0,\
NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 1, 60, {0, NULL}, 0, NULL, NULL, NULL, NULL}
NULL, NULL, NULL, NULL, 0, NULL, MQTTVERSION_DEFAULT, 0, 1, 60, {0, NULL}, 0, NULL, NULL, NULL, NULL}
#define MQTTAsync_connectOptions_initializer5 { {'M', 'Q', 'T', 'C'}, 6, 60, 0, 10, NULL, NULL, NULL, 30, 0,\
NULL, NULL, NULL, NULL, 0, NULL, MQTTVERSION_5, 0, 1, 60, {0, NULL}, 1, NULL, NULL, NULL, NULL}
/**
* This function attempts to connect a previously-created client (see
......
......@@ -85,7 +85,10 @@ int MQTTPacket_send_connect(Clients* client, int MQTTVersion,
goto exit;
packet.flags.all = 0;
packet.flags.bits.cleanstart = client->cleansession;
if (MQTTVersion >= MQTTVERSION_5)
packet.flags.bits.cleanstart = client->cleanstart;
else
packet.flags.bits.cleanstart = client->cleansession;
packet.flags.bits.will = (client->will) ? 1 : 0;
if (packet.flags.bits.will)
{
......
......@@ -414,3 +414,53 @@ MQTTProperties MQTTProperties_copy(const MQTTProperties* props)
FUNC_EXIT;
return result;
}
int MQTTProperties_hasProperty(MQTTProperties *props, int propid)
{
int i = 0;
int found = 0;
for (i = 0; i < props->count; ++i)
{
if (propid == props->array[i].identifier)
{
found = 1;
break;
}
}
return found;
}
int MQTTProperties_getNumericValue(MQTTProperties *props, int propid)
{
int i = 0;
int rc = -9999999;
for (i = 0; i < props->count; ++i)
{
int id = props->array[i].identifier;
if (id == propid)
{
switch (MQTTProperty_getType(id))
{
case PROPERTY_TYPE_BYTE:
rc = props->array[i].value.byte;
break;
case TWO_BYTE_INTEGER:
rc = props->array[i].value.integer2;
break;
case FOUR_BYTE_INTEGER:
case VARIABLE_BYTE_INTEGER:
rc = props->array[i].value.integer4;
break;
default:
rc = -999999;
break;
}
}
}
return rc;
}
......@@ -119,4 +119,7 @@ DLLExport void MQTTProperties_free(MQTTProperties* properties);
MQTTProperties MQTTProperties_copy(const MQTTProperties* props);
DLLExport int MQTTProperties_hasProperty(MQTTProperties *props, int propid);
DLLExport int MQTTProperties_getNumericValue(MQTTProperties *props, int propid);
#endif /* MQTTPROPERTIES_H */
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