Skip to content

Add pUserData field to MQTTContext_t for application use#378

Open
guclumhg wants to merge 1 commit into
FreeRTOS:mainfrom
guclumhg:feature/mqtt-user-context
Open

Add pUserData field to MQTTContext_t for application use#378
guclumhg wants to merge 1 commit into
FreeRTOS:mainfrom
guclumhg:feature/mqtt-user-context

Conversation

@guclumhg

Copy link
Copy Markdown

Description

Adds an application-defined void * pUserData field to MQTTContext_t, as requested in #256. The library never reads or modifies the field; it lets the application (for example a C++ wrapper passing its this pointer) access its own state from the MQTTEventCallback_t callback through the pContext parameter. Since MQTT_Init clears the whole context, the field is documented to be set after calling MQTT_Init.

A unit test verifies that MQTT_Init clears 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:

cmake -S test -B build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
  -DBUILD_CLONE_SUBMODULES=ON -DUNITTEST=1 [-DCOV_ANALYSIS=1] \
  -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DLIBRARY_LOG_LEVEL=LOG_DEBUG'
make -C build all
ctest --test-dir build -E system --output-on-failure

Formatting verified with uncrustify 0.69.0 using the config from FreeRTOS/CI-CD-Github-Actions.

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

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.

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
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.

coreMQTT context does not allow user parameters to be passed to the Event Callback

1 participant