Skip to content

Commit e48d941

Browse files
Add MQTT_InIt API
1 parent 08510aa commit e48d941

File tree

4 files changed

+176
-19
lines changed

4 files changed

+176
-19
lines changed

source/core_mqtt.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@
3535
/* Include config defaults header to get default values of configs. */
3636
#include "core_mqtt_config_defaults.h"
3737

38-
#ifndef MQTT_PRE_SEND_HOOK
39-
40-
/**
41-
* @brief Hook called before a 'send' operation is executed.
42-
*/
43-
#define MQTT_PRE_SEND_HOOK( pContext )
44-
#endif /* !MQTT_PRE_SEND_HOOK */
45-
46-
#ifndef MQTT_POST_SEND_HOOK
47-
48-
/**
49-
* @brief Hook called after the 'send' operation is complete.
50-
*/
51-
#define MQTT_POST_SEND_HOOK( pContext )
52-
#endif /* !MQTT_POST_SEND_HOOK */
53-
5438
#ifndef MQTT_PRE_STATE_UPDATE_HOOK
5539

5640
/**
@@ -78,7 +62,7 @@
7862
* @brief Number of vectors required to encode one topic filter in a subscribe
7963
* request. Three vectors are required as there are three fields in the
8064
* subscribe request namely:
81-
* 1. Topic filter length; 2. Topic filter; and 3. QoS in this order.
65+
* 1. Topic filter length; 2. Topic filter; and 3. Subscription options in this order.
8266
*/
8367
#define CORE_MQTT_SUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 3U )
8468

@@ -2693,6 +2677,7 @@ MQTTStatus_t MQTT_Init( MQTTContext_t * pContext,
26932677
const MQTTFixedBuffer_t * pNetworkBuffer )
26942678
{
26952679
MQTTStatus_t status = MQTTSuccess;
2680+
MQTTConnectionProperties_t connectionProperties;
26962681

26972682
/* Validate arguments. */
26982683
if( ( pContext == NULL ) || ( pTransportInterface == NULL ) ||
@@ -2735,9 +2720,18 @@ MQTTStatus_t MQTT_Init( MQTTContext_t * pContext,
27352720
pContext->getTime = getTimeFunction;
27362721
pContext->appCallback = userCallback;
27372722
pContext->networkBuffer = *pNetworkBuffer;
2723+
pContext->ackPropsBuffer.pBuffer = NULL;
27382724

27392725
/* Zero is not a valid packet ID per MQTT spec. Start from 1. */
27402726
pContext->nextPacketId = 1;
2727+
2728+
/* Setting default connect properties in our application */
2729+
status = MQTT_InitConnect( &connectionProperties );
2730+
2731+
if( status == MQTTSuccess )
2732+
{
2733+
pContext->connectionProperties = connectionProperties;
2734+
}
27412735
}
27422736

27432737
return status;

source/core_mqtt_serializer.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@
9696
*/
9797
#define MQTT_MAX_REMAINING_LENGTH ( 268435455UL )
9898

99+
/**
100+
* @brief Per the MQTT spec, the max packet size can be of max remaining length + 5 bytes.
101+
*/
102+
#define MQTT_MAX_PACKET_SIZE ( MQTT_MAX_REMAINING_LENGTH + 5U )
103+
99104
/**
100105
* @brief Set a bit in an 8-bit unsigned integer.
101106
*/
@@ -2709,3 +2714,36 @@ MQTTStatus_t MQTT_ProcessIncomingPacketTypeAndLength( const uint8_t * pBuffer,
27092714
}
27102715

27112716
/*-----------------------------------------------------------*/
2717+
2718+
MQTTStatus_t MQTT_InitConnect( MQTTConnectionProperties_t * pConnectProperties )
2719+
{
2720+
MQTTStatus_t status = MQTTSuccess;
2721+
2722+
if( pConnectProperties == NULL )
2723+
{
2724+
LogError( ( "pConnectProperties cannot be NULL." ) );
2725+
status = MQTTBadParameter;
2726+
}
2727+
else
2728+
{
2729+
pConnectProperties->receiveMax = UINT16_MAX;
2730+
pConnectProperties->maxPacketSize = MQTT_MAX_PACKET_SIZE;
2731+
pConnectProperties->requestProblemInfo = true;
2732+
pConnectProperties->serverReceiveMax = UINT16_MAX;
2733+
pConnectProperties->serverMaxQos = 2U;
2734+
pConnectProperties->serverMaxPacketSize = MQTT_MAX_PACKET_SIZE;
2735+
pConnectProperties->isWildcardAvailable = 1U;
2736+
pConnectProperties->isSubscriptionIdAvailable = 1U;
2737+
pConnectProperties->isSharedAvailable = 1U;
2738+
pConnectProperties->sessionExpiry = 0U;
2739+
pConnectProperties->topicAliasMax = 0U;
2740+
pConnectProperties->requestResponseInfo = false;
2741+
pConnectProperties->retainAvailable = 1U;
2742+
pConnectProperties->serverTopicAliasMax = 0U;
2743+
pConnectProperties->serverKeepAlive = UINT16_MAX;
2744+
}
2745+
2746+
return status;
2747+
}
2748+
2749+
/*-----------------------------------------------------------*/

source/include/core_mqtt.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ typedef struct MQTTContext
259259
*/
260260
MQTTFixedBuffer_t networkBuffer;
261261

262+
/**
263+
* @brief The buffer used to store properties for outgoing ack packets.
264+
*/
265+
MQTTPropBuilder_t ackPropsBuffer;
266+
262267
/**
263268
* @brief The next available ID for outgoing MQTT packets.
264269
*/
@@ -305,6 +310,11 @@ typedef struct MQTTContext
305310
uint32_t pingReqSendTimeMs; /**< @brief Timestamp of the last sent PINGREQ. */
306311
bool waitingForPingResp; /**< @brief If the library is currently awaiting a PINGRESP. */
307312

313+
/**
314+
* @brief Persistent Connection Properties, populated in the CONNECT and the CONNACK.
315+
*/
316+
MQTTConnectionProperties_t connectionProperties;
317+
308318
/**
309319
* @brief User defined API used to store outgoing publishes.
310320
*/
@@ -358,8 +368,8 @@ typedef struct MQTTDeserializedInfo
358368
* for the entire lifetime of the @p pContext and must not be used by another context and/or
359369
* application.
360370
*
361-
* @return #MQTTBadParameter if invalid parameters are passed;
362-
* #MQTTSuccess otherwise.
371+
* @return #MQTTBadParameter if invalid parameters are passed;<br>
372+
* #MQTTSuccess otherwise.<br>
363373
*
364374
* <b>Example</b>
365375
* @code{c}

source/include/core_mqtt_serializer.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,103 @@ typedef struct MQTTPacketInfo
270270
size_t headerLength;
271271
} MQTTPacketInfo_t;
272272

273+
/**
274+
* @ingroup mqtt_struct_types
275+
* @brief Property builder for MQTT packets.
276+
*/
277+
typedef struct MQTTPropBuilder
278+
{
279+
uint8_t * pBuffer; /**< @brief Pointer to the buffer for storing properties. */
280+
size_t bufferLength; /**< @brief Total length of the buffer available for properties. */
281+
size_t currentIndex; /**< @brief Current position in the buffer where next property will be written. */
282+
uint32_t fieldSet; /**< @brief Bitfield tracking which properties have been added. */
283+
} MQTTPropBuilder_t;
284+
285+
/**
286+
* @ingroup mqtt_struct_types
287+
* @brief Struct to hold connect and connack properties.
288+
*/
289+
typedef struct MQTTConnectProperties
290+
{
291+
/**
292+
* @brief Four Byte Integer representing the Session Expiry Interval in seconds.
293+
*/
294+
uint32_t sessionExpiry;
295+
296+
/**
297+
* @brief Maximum number of unacknowledged PUBLISH packets client is willing to receive.
298+
*/
299+
uint16_t receiveMax;
300+
301+
/**
302+
* @brief Four Byte Integer representing the Maximum Packet Size the Client is willing to accept.
303+
*/
304+
uint32_t maxPacketSize;
305+
306+
/**
307+
* @brief Two Byte Integer representing the Topic Alias Maximum value.
308+
*/
309+
uint16_t topicAliasMax;
310+
311+
/**
312+
* @brief A value of 0 indicates that the Server MUST NOT return Response Information.
313+
*/
314+
bool requestResponseInfo;
315+
316+
/**
317+
* @brief The Client uses this value to indicate whether the Reason String or User Properties
318+
* are sent in the case of failures
319+
*/
320+
bool requestProblemInfo;
321+
322+
/**
323+
* @brief Maximum number of unacknowledged PUBLISH packets server is willing to receive.
324+
*/
325+
uint16_t serverReceiveMax;
326+
327+
/**
328+
* @brief Max qos supported by the server.
329+
*/
330+
uint8_t serverMaxQos;
331+
332+
/**
333+
* @brief Byte declares whether the Server supports retained messages.
334+
*/
335+
uint8_t retainAvailable;
336+
337+
/**
338+
* @brief Four Byte Integer representing the Maximum Packet Size the Server is willing to accept.
339+
*/
340+
uint32_t serverMaxPacketSize;
341+
342+
/**
343+
* @brief Two Byte Integer representing the Topic Alias Maximum value.
344+
*/
345+
uint16_t serverTopicAliasMax;
346+
347+
/**
348+
* @brief Whether wildcard subscription is available.
349+
*/
350+
uint8_t isWildcardAvailable;
351+
352+
/**
353+
* @brief Whether the Server supports Subscription Identifiers.
354+
*/
355+
uint8_t isSubscriptionIdAvailable;
356+
357+
/**
358+
* @brief Whether the Server supports Shared Subscription.
359+
*/
360+
uint8_t isSharedAvailable;
361+
362+
/**
363+
* @brief Keep Alive value given by the server.
364+
*/
365+
uint16_t serverKeepAlive;
366+
367+
368+
} MQTTConnectionProperties_t;
369+
273370
/**
274371
* @brief Get the size and Remaining Length of an MQTT CONNECT packet.
275372
*
@@ -1305,6 +1402,24 @@ uint8_t * MQTT_SerializeUnsubscribeHeader( size_t remainingLength,
13051402
uint16_t packetId );
13061403
/** @endcond */
13071404

1405+
/**
1406+
* @brief Initialize an MQTTConnectionProperties_t.
1407+
*
1408+
* @note This function initializes the connect properties to default values.
1409+
* This function should only be used if using only serializer functions
1410+
* throughout the connection. It is also important to only call this function
1411+
* before sending the connect packet.
1412+
*
1413+
* @param[in] pConnectProperties The connect properties to initialize.
1414+
*
1415+
* @return
1416+
* - #MQTTBadParameter if pConnectProperties is NULL.
1417+
* - #MQTTSuccess otherwise.
1418+
*/
1419+
/* @[declare_mqtt_initconnect] */
1420+
MQTTStatus_t MQTT_InitConnect( MQTTConnectionProperties_t * pConnectProperties );
1421+
/* @[declare_mqtt_initconnect] */
1422+
13081423
/* *INDENT-OFF* */
13091424
#ifdef __cplusplus
13101425
}

0 commit comments

Comments
 (0)