# Using Device Shadow library's API with MQTT Agent

**URL:** <https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430>\
**Category:** AWS\
**Created:** [December 29, 2020, 5:07pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430 "2020-12-29T17:07:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 29, 2020, 5:07pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/1 "2020-12-29T17:07:52Z")

</div>

Continuing the discussion from [MQTT\_ProcessLoop vs MQTTAgent\_CommandLoop (or coreMQTT vs FreeRTOS-Plus-MQTT)](https://forums.freertos.org/t/mqtt-processloop-vs-mqttagent-commandloop-or-coremqtt-vs-freertos-plus-mqtt/11410/2):

I want to manage my shadow connection using the Device Shadow library’s API in conjunction with the MQTT agent but I can’t figure out how I can get access to the MQTT context that the agent is created internally and use for the shadow APIs (like SubscribeToTopic for example that take the MQTT context pointer as its first parameter)

---

<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:** [December 29, 2020, 6:52pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/2 "2020-12-29T18:52:04Z")

</div>

I assume you are talking about this function: [https://github.com/FreeRTOS/FreeRTOS/blob/master/FreeRTOS-Plus/Demo/AWS/Mqtt\_Demo\_Helpers/mqtt\_demo\_helpers.h#L88](https://github.com/FreeRTOS/FreeRTOS/blob/master/FreeRTOS-Plus/Demo/AWS/Mqtt_Demo_Helpers/mqtt_demo_helpers.h#L88)

This function is part of demo and you will need to modify it to work with the MQTT Agent. The parameter should be `MQTTContextHandle_t ` and not `MQTTContext_t *`. The implementation of the function will need to call `MQTTAgent_Subscribe` instead of `MQTT_Subscribe`: [https://github.com/FreeRTOS/FreeRTOS/blob/master/FreeRTOS-Plus/Demo/AWS/Mqtt\_Demo\_Helpers/mqtt\_demo\_helpers.c#L784](https://github.com/FreeRTOS/FreeRTOS/blob/master/FreeRTOS-Plus/Demo/AWS/Mqtt_Demo_Helpers/mqtt_demo_helpers.c#L784)

`coreMQTT` APIs (such as `MQTT_Subscribe`) are not thread-safe and **must** be accessed only from the MQTT Agen task. All the application tasks **must** use MQTT Agent APIs.

Thanks.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 29, 2020, 8:17pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/3 "2020-12-29T20:17:04Z")

</div>

Thank you for your answer, but how can I get the MQTTContextHandle\_t from the agent?

---

<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:** [December 29, 2020, 11:54pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/4 "2020-12-29T23:54:45Z")

</div>

As per the doc of [`MQTTAgent_Init`](https://github.com/FreeRTOS/Lab-Project-coreMQTT-Agent/blob/main/lib/FreeRTOS/freertos-plus-mqtt/freertos_mqtt_agent.h#L333) API, `MQTTContextHandle_t` is index of the MQTT context to be used. MQTT Agent manages an [internal context array](https://github.com/FreeRTOS/Lab-Project-coreMQTT-Agent/blob/main/lib/FreeRTOS/freertos-plus-mqtt/freertos_mqtt_agent.c#L424) and the application chooses which context to use (by choosing the index aka`MQTTContextHandle_t`). The size of the internal context array is controlled using `MQTT_AGENT_MAX_SIMULTANEOUS_CONNECTIONS ` configuration macro.

This sub-pub example uses the first MQTT context in the array: [https://github.com/FreeRTOS/Lab-Project-coreMQTT-Agent/blob/main/source/simple\_sub\_pub\_demo.c#L97](https://github.com/FreeRTOS/Lab-Project-coreMQTT-Agent/blob/main/source/simple_sub_pub_demo.c#L97)

Thanks.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 30, 2020, 6:46am UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/5 "2020-12-30T06:46:10Z")

</div>

Thank you,  
So I guess using the agent together with the shadow API will not work as this API calls MQTT core services like MQTT\_Subscribe and MQTT\_ProcessLoop and as you said the MQTT core is not thread safe so this will not work.

---

<div class="post-metadata">

**Author:** ![abhidixi](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/abhidixi/32/628_2.png) [@abhidixi](https://forums.freertos.org/u/abhidixi)\
**Post date:** [December 30, 2020, 6:17pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/6 "2020-12-30T18:17:29Z")

</div>

Hello @EyalG , The [Shadow API](https://github.com/aws/Device-Shadow-for-AWS-IoT-embedded-sdk/blob/main/source/include/shadow.h) is MQTT agnostic, i.e, you can use any MQTT stack with it. I believe you are referring to [xSubscribeToTopic()](https://github.com/FreeRTOS/FreeRTOS/blob/da9b9a800d03bd0f6ef249d9a0b690919fee34f0/FreeRTOS-Plus/Demo/AWS/Mqtt_Demo_Helpers/mqtt_demo_helpers.c#L760) function used in Shadow demo taking the context pointer. That function is not part of the Shadow API, it is just a common helper function used by our demos. You can instead use [MQTTAgent\_Subscribe](https://github.com/FreeRTOS/Lab-Project-coreMQTT-Agent/blob/main/lib/FreeRTOS/freertos-plus-mqtt/freertos_mqtt_agent.h#L177). You should able to use Shadow library with MQTT Agent API. I hope it answers your question. Please let us know if you have any further questions.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 30, 2020, 6:39pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/7 "2020-12-30T18:39:55Z")

</div>

Thank you for getting back to me on that.

I started to port my existing application that is based on 202002 by using the MQTT agent demo but after long hours of porting and trying to figure out what will be the best approach I abounded that direction, mostly because I saw that the agent is not done yet and I feel its too risky to port to it now.

I took the device shadow demo and I used it as my reference for the porting to 202012.

I believe that by calling the MQTT\_ProcessLoop in a single task that will handle both the MQTT receive messages (Acks and subscription responses, mainly delta messages) as well as request for publish (Mainly reported updates) I will have most of the agent functionality that I need and it will be based on the more mature core.

Any input on that approach?

Thanks

---

<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:** [December 30, 2020, 6:48pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/8 "2020-12-30T18:48:48Z")

</div>

I’d like to understand why you had to abandon the earlier approach. Do you have time for a quick call? If so, please DM me your email.

Thanks.

---

<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:** [December 30, 2020, 7:47pm UTC](https://forums.freertos.org/t/using-device-shadow-librarys-api-with-mqtt-agent/11430/9 "2020-12-30T19:47:55Z")

</div>

> [@EyalG](#):
>
> So I guess using the agent together with the shadow API will not work as this API calls MQTT core services like MQTT\_Subscribe and MQTT\_ProcessLoop and as you said the MQTT core is not thread safe so this will not work.

No, that is not correct. MQTT Agent APIs are thread safe and can be called from multiple threads. So you can have 2 threads both of which share the same `MQTTContextHandle_t`:

- MQTT Thread - Subscribes and publishes messages using MQTT Agent APIs (MQTTAgent\_Subscribe and MQTTAgent\_Publish).
- Shadow Thread - Subscribes and publishes shadow messages using MQTT Agent APIs (MQTTAgent\_Subscribe and MQTTAgent\_Publish).

You can share the `MQTTContextHandle_t` by defining it in a common header file. Lets say you have a file `app_config.h` which contains all the configurations for your application. You define the following in this file:

```c
#define appMQTT_CONTEXT_HANDLE ( ( MQTTContextHandle_t ) 0 )

```

The updated implementation of `xSubscribeToTopic` should look something like:

```c
BaseType_t xSubscribeToTopic( MQTTContextHandle_t mqttContextHandle,
                                                      const char * pcTopicFilter,
                                                      uint16_t usTopicFilterLength ) <-- May need additional callbacks too. 
{
...
/* Call MQTTAgent_Subscribe. */
...
}

```

The application should call `xSubscribeToTopic` like the following:

```auto
xSubscribeToTopic( appMQTT_CONTEXT_HANDLE, "/example/topic", sizeof( "/example/topic" ) - 1 );

```

Thanks.
