Add pUserData field to MQTTContext_t for application use#378
Open
guclumhg wants to merge 1 commit into
Open
Conversation
Applications, especially C++ ones, need to reach their own state from the MQTT event callback, but MQTTContext_t offered no place to store such a pointer. Add an application-owned pUserData field that the library never reads or modifies; the event callback can access it through its pContext parameter. MQTT_Init clears the field, so it is documented to be set after MQTT_Init. Fixes FreeRTOS#256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds an application-defined
void * pUserDatafield toMQTTContext_t, as requested in #256. The library never reads or modifies the field; it lets the application (for example a C++ wrapper passing itsthispointer) access its own state from theMQTTEventCallback_tcallback through thepContextparameter. SinceMQTT_Initclears the whole context, the field is documented to be set after callingMQTT_Init.A unit test verifies that
MQTT_Initclears the field and that the value set by the application is visible inside the event callback when a QoS 1 PUBLISH is received.There is no behavioral change for applications that do not use the field, and coverage is unchanged (97.5% lines / 93.8% branches).
Happy to rename the field or switch to a setter/getter API if the maintainers prefer.
Test Steps
Both CI unit-test configurations pass locally (5/5 test suites), including the new
test_MQTT_Init_UserData_AccessibleInEventCallback:Formatting verified with uncrustify 0.69.0 using the config from FreeRTOS/CI-CD-Github-Actions.
Checklist:
Related Issue
Fixes #256
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.