aws / aws-iot-device-sdk-embedded-C

SDK for connecting to AWS IoT from a device using embedded C.
MIT License
984 stars 633 forks source link

Facing issue with the aws_iot_mqtt_yield #1882

Closed SwamidasSilabs closed 1 year ago

SwamidasSilabs commented 1 year ago

Hi Team,

I am facing one issue with the _aws_iot_mqtt_internal_yield function, Here is my test case. My module is the client and has called aws_iot_mqtt_yield with a timeout of 1000 milliseconds. The _aws_iot_mqtt_internal_yield has a do-while that loops till the 1000 milliseconds time expires irrespective of whether the data is received. The 1st loop has received the data from the server whereas the 2nd loop hasn't, and it comes out with a timeout error, how can I consume or use the data received in the 1st loop?

kstribrnAmzn commented 1 year ago

Hello @SwamidasSilabs! To help. me in my investigation can you please provide which version of the SDK you are using? A specific commit hash/id for that release is even more helpful.

kstribrnAmzn commented 1 year ago

I took a look at the latest version of the code with the _aws_iot_mqtt_internal_yield function (v3.0.1) and I believe I found how the call to handle the data received is to work.

Overall the function trace looks like...

  1. aws_iot_mqtt_yield (line) which in turn calls
  2. _aws_iot_mqtt_internal_yield (line) in turn calling
  3. aws_iot_mqtt_internal_cycle_read (line) which calls
  4. _aws_iot_mqtt_internal_handle_publish (line) which calls
  5. _aws_iot_mqtt_internal_deliver_message (line) which calls a registered message handler provided by the application

The message handlers are set when subscribing to an MQTT topic.

Have you set up a message handler for the MQTT topic which you are expecting to receive data on?

SwamidasSilabs commented 1 year ago

Hi @kstribrnAmzn

Thanks for your reply, I have registered a message handler and the handler is getting triggered when the data arrives.

I have one more doubt here, Can't we receive the data without registering the message handler, You can assume like a synchronous receive.

Thanks, Swamidas

aggarg commented 1 year ago

Can't we receive the data without registering the message handler, You can assume like a synchronous receive.

MQTT is not a synchronous request/response protocol and the receive path is asynchronous. Once you have subscribed to a topic, data on that topic can arrive anytime. Therefore, you need to register a handler for receiving data.

Another question for you - I see that you are using V3 of the SDK. Is there a reason to not use the latest version?

SwamidasSilabs commented 1 year ago

Thanks @aggarg

I have registered the message handler and am able to process the data.

aggarg commented 1 year ago

Great. I am closing this issue. Feel free to reopen or create a new one if you need anything else.