# coreMQTT - How to pass user data to EventCallback?

**URL:** <https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843>\
**Category:** Libraries\
**Created:** [July 19, 2023, 12:22am UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843 "2023-07-19T00:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbicim](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/tbicim/32/2800_2.png) [@tbicim](https://forums.freertos.org/u/tbicim)\
**Post date:** [July 19, 2023, 12:22am UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/1 "2023-07-19T00:22:08Z")

</div>

Hello all,

We are using coreMQTT library on a C++ project and we need to pass the this pointer of a class to the callback. On the api for v3.1.1 I didn’t see any user parameters in [EventCallback function](https://aws.github.io/aws-iot-device-sdk-embedded-C/202012.00/libraries/standard/coreMQTT/docs/doxygen/output/html/group __mqtt__ callback__types.html#ga00d348277ed4fde23c95bfc749ae954a).

We were using coreMQTTAgent before which allows us to pass parameters to callbacks, however we are not able to use that library since we need to access the cell modem when we are running the Agent and this would create concurrency issues. We don’t want to implement locks around Cellular Interface since that would defeat the purpose of an Agent based design and might even lead to priority issues. We really want to avoid globals and singletons so I am open to suggestions on how to achieve this without using those.

Thank you,  
Tuna Bicim

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [July 19, 2023, 5:47am UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/2 "2023-07-19T05:47:19Z")

</div>

I do not think it is directly possible but can you use a workaround like the following -

```auto
/* Declare a struct with MQTTContext_t as the first member.
 * It is important to keep MQTTContext_t as the first member. */
typedef struct MQTTExtendedContext
{
    MQTTContext_t ctx;
    void* pUserData;
} MQTTExtendedContext_t;

MQTTExtendedContext_t extendedCtx = { 0 };

/* Initialize pUserData to whatever you want (likely this pointer
 * in your case). */
extendedCtx.pUserData = this;

/* Use the ctx member of the MQTTExtendedContext_t when interacting
 * with the coreMQTT APIs. */
MQTT_Init( &( extendedCtx.ctx ), ... );
MQTT_Connect( &( extendedCtx.ctx ), ... );

/* Cast MQTTContext_t back to MQTTExtendedContext_t in the event
 * callback and access pUserData. This cast is only possible
 * when MQTTContext_t is the first member of MQTTExtendedContext_t. */
void MQTTEventCallback( MQTTContext_t * pMQTTContext,
                        MQTTPacketInfo_t * pPacketInfo,
                        MQTTDeserializedInfo_t * pDeserializedInfo )
{
    MQTTExtendedContext_t * pMQTTExtendedContext = ( MQTTExtendedContext_t * ) pMQTTContext;

    /* You can now access pMQTTExtendedContext->pUserData. */
}

```

---

<div class="post-metadata">

**Author:** ![tbicim](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/tbicim/32/2800_2.png) [@tbicim](https://forums.freertos.org/u/tbicim)\
**Post date:** [July 19, 2023, 2:08pm UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/3 "2023-07-19T14:08:58Z")

</div>

Hi Gaurav,

Thanks for the suggestion, it should definitely work.  
I didn’t think about the classic C OOP Inheritance hack 😃

Is it possible to make a feature request here to have a pUserData field to be added to the library or do I need to make a Github issue to do that?

Thank you,  
Tuna

---

<div class="post-metadata">

**Author:** ![kanherea](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kanherea/32/4106_2.png) [@kanherea](https://forums.freertos.org/u/kanherea)\
**Post date:** [July 19, 2023, 6:45pm UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/4 "2023-07-19T18:45:33Z")

</div>

Hello @tbicim,

Yes, @aggarg is correct, there is no direct way to do this and the way he suggested should work.

To make your code ‘future-proof’, I suggest you utilize the `offsetof` function if your compiler supports it to get the pointer to `MQTTExtendedContext_t` from the given `MQTTContext_t *`.

You can go about it like so:

```c
/* Cast MQTTContext_t back to MQTTExtendedContext_t in the event
 * callback and access pUserData. */
void MQTTEventCallback( MQTTContext_t * pMQTTContext,
                        MQTTPacketInfo_t * pPacketInfo,
                        MQTTDeserializedInfo_t * pDeserializedInfo )
{
    MQTTExtendedContext_t ExtendedCtx = { 0 };
    /* Calculate offset of the member from the structure base. */
    ptrdiff_t offset = ( ( uint8_t * ) &( ExtendedCtx .ctx) )
                     - ( ( uint8_t * ) &( ExtendedCtx ) );

    /* Get the Extended Context by moving the pointer back by offset. */
    MQTTExtendedContext_t * pMQTTExtendedContext = ( MQTTExtendedContext_t * ) &( ( ( uint8_t * ) ctx )[0 - offset] );

    /* You can now access pMQTTExtendedContext->pUserData. */
}

```

This ensures that even if someone moves the `MQTTContext_t ctx;` around in the `MQTTExtendedContext` structure (or adds a member before it), you still get the correct pointer to `MQTTExtendedContext_t` which you can then dereference.

---

<div class="post-metadata">

**Author:** ![kanherea](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kanherea/32/4106_2.png) [@kanherea](https://forums.freertos.org/u/kanherea)\
**Post date:** [July 19, 2023, 6:53pm UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/5 "2023-07-19T18:53:20Z")

</div>

> [@tbicim](#):
>
> Is it possible to make a feature request here to have a pUserData field to be added to the library or do I need to make a Github issue to do that?

You can request that feature here, but a GitHub Issue helps us track the feature requests better and we can see which user created that and other users can add a comment saying that they also need it which allows us to gauge the interest of users in a given feature request.

Would you like to create a feature request on GitHub by following this link: [New Issue · FreeRTOS/coreMQTT](https://github.com/FreeRTOS/coreMQTT/issues/new)?

---

<div class="post-metadata">

**Author:** ![tbicim](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/tbicim/32/2800_2.png) [@tbicim](https://forums.freertos.org/u/tbicim)\
**Post date:** [July 20, 2023, 5:13am UTC](https://forums.freertos.org/t/coremqtt-how-to-pass-user-data-to-eventcallback/17843/6 "2023-07-20T05:13:59Z")

</div>

Hi Aniruddha,

Thanks for the suggestion. I think for now I will keep ctx to be the first member and add a static assert that checks offsetof context is always 0. That way I can catch if anyone adds a member beforehand. It is simple and self documenting 🙂

Thank you for the link, I created a Github issue about it.

> <https://github.com/FreeRTOS/coreMQTT/issues/256>
>
> Hello, 
> I have been using the library on a C++ project and would like to pass t…he this pointer to the Event Callback to be able to access my class instance. 
> Is it possible to add a void \* pUserData field to the context and pass it to the callback? 
> Thank you!

Thanks,  
Tuna
