Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adituc
Copy link

@adituc adituc commented Apr 14, 2025

Upgrading MQTT Demos to support v5 features

Description

  • This PR adds a demo application to showcase MQTT version 5.0 functionality using the coreMQTT library. It demonstrates establishing a connection with a broker, subscribing to a topic, publishing a message, receiving Incoming publishes, unsubscribing from a topic, and finally disconnecting.

  • Existing data structures and functions are modified, and some new functions are added.

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@adituc adituc marked this pull request as draft April 14, 2025 06:44
@@ -0,0 +1,1339 @@


/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The v5 demo code should contain a similar licensing information as the previous v3 demo.


/* Demo Specific configs. */
#include "demo_config.h"
#include "core_mqtt_config.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to include core_mqtt_config.h explicitly. It will be included through the core_mqtt.h file.


/* MQTT library includes. */
#include "core_mqtt.h"
#include "core_mqtt_utils.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we include core_mqtt_utils.h in the core_mqtt_serializer.h then we do not need to make this extra include here. This will be one less change that the user needs to do while migrating from v3 to v5.

Comment on lines 85 to 99
#define mqttexampleCONNACK_RECV_TIMEOUT_MS ( 100000U )

/**
* @brief The prefix to the topic(s) subscribe(d) to and publish(ed) to in the example.
*
* The topic name starts with the client identifier to ensure that each demo
* interacts with a unique topic name.
*/
#define mqttexampleTOPIC_PREFIX "test"

/**
* @brief The number of topic filters to subscribe.
*/
#define mqttexampleTOPIC_COUNT ( 2 )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we are done with the testing, we should change these values back to the original values present in the v3 demo.

MQTTContext_t xMQTTContext = { 0 };
MQTTStatus_t xMQTTStatus;
PlaintextTransportStatus_t xNetworkStatus;
MQTTConnectProperties_t xProperties;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we have added the MQTTConnectProperties in the Context itself, we do not need this variable here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree, It has been removed

MQTTStatus_t xMQTTStatus;
PlaintextTransportStatus_t xNetworkStatus;
MQTTConnectProperties_t xProperties;
MQTTReasonCodeInfo_t disconnect = { 0 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we require this MQTTReasonCodeInfo_t disconnect = { 0 }; variable?

Comment on lines 588 to 591
MQTTReasonCodeInfo_t disconnect;
PlaintextTransportStatus_t xNetworkStatus;
MQTTPublishInfo_t willInfo;
MQTTAuthInfo_t auth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see the disconnect, xNetworkStatus and auth vairables being used in this function. If they are not to be used, we can remove them from here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.



MqttPropBuilder_t propBuilder;
uint8_t buf[500];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good practice to use macros instead of direct contants, so that the reader of the code (which will be a user in case of a demo) can understand what this macro is for. We should define a macro in this demo file, something like MAX_PROPERTY_BUFFER_LENGTH, and map it to a constant like 500 in this case. The user can change this macro if they wish to.



xResult = MQTTPropAdd_UserProp(&(propBuilder), &xUserProperty);
xResult = MQTTPropAdd_ConnSessionExpiry(&(propBuilder), sessionExpiry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be asserts after each of these lines like assert( xResult == MQTTSuccess )

Comment on lines 646 to 647
//xResult = MQTTPropAdd_ConnAuthMethod(&(propBuilder), "SCRAM-SHA-1 ", 11);
//xResult = MQTTPropAdd_ConnAuthData(&(propBuilder), "test", 4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove all the code that is not needed and is commented out.

@DakshitBabbar
Copy link
Member

The changes in this PR are moved to the PR #1354. Hence closing this PR. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants