Skip to content

Commit

Permalink
WIP: Adding AUTH and connect APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Jul 25, 2023
1 parent 6b00baa commit 9a85bc8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
16 changes: 14 additions & 2 deletions MQTTClient-C/src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeou
c->readbuf = readbuf;
c->readbuf_size = readbuf_size;
c->isconnected = 0;
#if defined(MQTTV5)
c->cleanstart = 0;
#else
c->cleansession = 0;
#endif
c->ping_outstanding = 0;
c->defaultMessageHandler = NULL;
c->next_packetid = 1;
Expand Down Expand Up @@ -269,11 +273,15 @@ void MQTTCloseSession(MQTTClient* c)
{
c->ping_outstanding = 0;
c->isconnected = 0;

#if defined(MQTTV5)
//TODO: Session cleanup happens only if Clean Start = 1 and Session Expiry is 0.
#else
if (c->cleansession)
MQTTCleanSession(c);
#endif
}


int cycle(MQTTClient* c, Timer* timer)
{
int32_t len = 0,
Expand Down Expand Up @@ -344,7 +352,7 @@ int cycle(MQTTClient* c, Timer* timer)
break;
#if defined(MQTTV5)
case DISCONNECT:
// TODO: not implemented.
// TODO: implement DISCONNECTv5 and callback to expose reason code and properties.
break;
#endif
}
Expand Down Expand Up @@ -455,7 +463,11 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC
options = &default_options; /* set default options if none were supplied */

c->keepAliveInterval = options->keepAliveInterval;
#if defined(MQTTV5)
c->cleanstart = options->cleanstart;
#else
c->cleansession = options->cleansession;
#endif
TimerCountdown(&c->last_received, c->keepAliveInterval);
if ((len = MQTTSerialize_connect(c->buf, c->buf_size, options)) <= 0)
goto exit;
Expand Down
6 changes: 5 additions & 1 deletion MQTTClient-C/src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "V5/MQTTV5Packet.h"
#else
#include "MQTTPacket.h"
#endif
#endif /* MQTTV5 */

#if defined(MQTTCLIENT_PLATFORM_HEADER)
/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value
Expand Down Expand Up @@ -118,6 +118,10 @@ typedef struct MQTTSubackData

typedef void (*messageHandler)(MessageData*);

#if defined(MQTTV5)
typedef void (*authHandler)(MQTTProperties*, unsigned char reasonCode);
#endif /* MQTTV5 */

typedef struct MQTTClient
{
unsigned int next_packetid,
Expand Down
28 changes: 24 additions & 4 deletions MQTTClient-C/src/V5/MQTTV5Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,23 @@
* @return DLLExport
*/
DLLExport void MQTTV5ClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms,
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size);
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size, MQTTProperties* recvProperties);

/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
* The nework object must be connected to the network endpoint before calling this
* @param options - connect options
* @return success code
*/
DLLExport int MQTTV5ConnectWithResults(MQTTClient* client, MQTTPacket_connectData* options,
MQTTConnackData* data);
MQTTProperties* connectProperties, MQTTProperties* willProperties, MQTTConnackData* data);

/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
* The nework object must be connected to the network endpoint before calling this
* @param options - connect options
* @return success code
*/
DLLExport int MQTTV5Connect(MQTTClient* client, MQTTPacket_connectData* options);
DLLExport int MQTTV5Connect(MQTTClient* client, MQTTPacket_connectData* options,
MQTTProperties* connectProperties, MQTTProperties* willProperties);

/** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
* @param client - the client object to use
Expand All @@ -68,6 +69,25 @@ DLLExport int MQTTV5Publish(MQTTClient* client, const char*, MQTTMessage*, MQTTP
*/
DLLExport int MQTTV5SetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler);

/**
* @brief MQTT Auth - send an MQTT AUTH packet
*
* @param client - the client object to use
* @param reasonCode - the reason code to send
* @param properties - the properties to send
* @return success code
*/
DLLExport int MQTTV5Auth(MQTTClient* client, unsigned char reasonCode, MQTTProperties* properties);

/**
* @brief
*
* @param c
* @param authHandler
* @return DLLExport
*/
DLLExport int MQTTV5SetAuthHandler(MQTTClient* c, authHandler authHandler);

/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
* @param client - the client object to use
* @param topicFilter - the topic filter to subscribe to
Expand Down Expand Up @@ -96,7 +116,7 @@ DLLExport int MQTTV5Unsubscribe(MQTTClient* client, const char* topicFilter);
* @param client - the client object to use
* @return success code
*/
DLLExport int MQTTV5Disconnect(MQTTClient* client);
DLLExport int MQTTV5Disconnect(MQTTClient* client, unsigned char reasonCode, MQTTProperties* properties);

#if defined(__cplusplus)
}
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/src/MQTTConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ typedef struct
unsigned char cleanstart;
#else
unsigned char cleansession;
#endif
#endif /* MQTTV5 */
unsigned char willFlag;
MQTTPacket_willOptions will;
MQTTString username;
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/src/MQTTPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum msgTypes
PINGREQ, PINGRESP, DISCONNECT
#if defined(MQTTV5)
, AUTH
#endif
#endif /* MQTTV5 */
};

/**
Expand Down

0 comments on commit 9a85bc8

Please sign in to comment.