Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
585 stars 739 forks source link

Callback Desired properties will not call after callabck has been called from Send Reported Properties. #1870

Closed lefebvresam closed 2 years ago

lefebvresam commented 3 years ago

Development Machine, OS, Compiler (and Other Relevant Toolchain Info)

Sitara Cross Compiled using GCC 8.4.0

SDK Version (Please Give Commit SHA if Manually Compiling)

Release 1.4.1

Protocol

MQTT

Describe the Bug

When you attach the SetDeviceTwinCallback for desired properties AFTER the callback set by SendReportedState for reported properties has been called at least once, the callback for desired properties will never be called. When you switch the order, as soon as you attach the callback for desired properties has been set, it will be called with status DEVICE_TWIN_UPDATE_COMPLETE and works for successive calls.

        LOG_DEBUG(__("Set send reported properties callback"));
        if (IoTHubDeviceClient_SendReportedState(
              devclienthandle_, reinterpret_cast<const unsigned char*>(reported.c_str()),
              strlen(reported.c_str()), StaticReportedStateCallback,
              static_cast<void*>(&static_context_)) != IOTHUB_CLIENT_OK)
          LOG_ERROR(__("Unable to set send twin reported properties callback"));
        else
          LOG_DEBUG(__("The twin reported properties is waiting for answer"));

wait for the callback be called ... and then:

        LOG_DEBUG(__("Set receive twin desired properties callback"));
        if (IoTHubDeviceClient_SetDeviceTwinCallback(
              devclienthandle_, StaticReceiveDesiredPropertiesCallback,
              static_cast<void*>(&static_context_)) != IOTHUB_CLIENT_OK)
          LOG_ERROR(__("Unable to set receive twin desired properties callback"));
        else
          LOG_DEBUG(__("The receive twin desired properties is waiting for answer"));

and StaticReceiveDesiredPropertiesCallback will never be called

momuno commented 2 years ago

Hi @lefebvresam. Thank you for submitting this issue. I am investigating and will let you know what I find.

momuno commented 2 years ago

HI @lefebvresam. I am using the C SDK version tagged 1.4.1 and am unable to reproduce the issue. I am running the sample iothub_client_device_twin_and_methods_sample.c with a brand new logical device. I used the following changes in main():

-            (void)IoTHubDeviceClient_GetTwinAsync(iotHubClientHandle, getCompleteDeviceTwinOnDemandCallback, NULL);
-            (void)IoTHubDeviceClient_SendReportedState(iotHubClientHandle, (const unsigned char*)reportedProperties, strlen(reportedProperties), reportedStateCallback, NULL);
-            (void)IoTHubDeviceClient_SetDeviceMethodCallback(iotHubClientHandle, deviceMethodCallback, NULL);
-            (void)IoTHubDeviceClient_SetDeviceTwinCallback(iotHubClientHandle, deviceTwinCallback, &car);
+            //(void)IoTHubDeviceClient_GetTwinAsync(iotHubClientHandle, getCompleteDeviceTwinOnDemandCallback, NULL);
+            printf("Set send reported properties callback\n");
+            if (IoTHubDeviceClient_SendReportedState(iotHubClientHandle, (const unsigned char*)reportedProperties, strlen(reportedProperties), reportedStateCallback, NULL) != IOTHUB_CLIENT_OK)
+            {
+                printf("Unable to set send twin reported properties callback\n");
+            }
+            else
+            {
+                printf("The twin reported properties is waiting for answer\n");
+            }
+          //  (void)IoTHubDeviceClient_SetDeviceMethodCallback(iotHubClientHandle, deviceMethodCallback, NULL);
+            printf("Set receive twin desired properties callback\n");
+            if (IoTHubDeviceClient_SetDeviceTwinCallback(iotHubClientHandle, deviceTwinCallback, &car) != IOTHUB_CLIENT_OK)
+            {
+                printf("Unable to set receive twin desired properties callback\n");
+            }
+            else
+            {
+                printf("The receive twin desired properties is waiting for answer\n");
+            }

When I run the sample, the Terminal Output I see is:

Set send reported properties callback
The twin reported properties is waiting for answer
Set receive twin desired properties callback
The receive twin desired properties is waiting for answer
Device Twin reported properties update completed with result: 204

Via Azure IoT Explorer, when I add "changeOilReminder": "Nov 30, 2021", to the twin desired properties of my device, I see a twin desired properties response. Azure IoT Explorer twin for my device:

"properties": {
        "desired": {
            "changeOilReminder": "Nov 30, 2021",
...

Terminal Output:

Received a new changeOilReminder = Nov 30, 2021

Rerunning the sample I see in the Terminal Output:

Set send reported properties callback
The twin reported properties is waiting for answer
Set receive twin desired properties callback
The receive twin desired properties is waiting for answer
Received a new changeOilReminder = Nov 30, 2021
Device Twin reported properties update completed with result: 204

If I have misunderstood the issue you are experiencing, please let me know. Thanks!

lefebvresam commented 2 years ago

I'm no longer involved in the project, so I cannot give more details. As I remember I had to switch the order to have it working fine.

momuno commented 2 years ago

@lefebvresam Thank you for the feedback and I'm glad the workaround you found unblocked your progress. I reread the Issue and see my error in understanding. I have rerun the sample with the added change:

+static bool gReportedStateCallbackCalled = false;

Then in the reported state callback:

 {
     (void)userContextCallback;
     printf("Device Twin reported properties update completed with result: %d\r\n", status_code);
+    gReportedStateCallbackCalled = true;
 }

and in between calls to IoTHubDeviceClient_SendReportedState() and IoTHubDeviceClient_SetDeviceTwinCallback()

+            while (gReportedStateCallbackCalled != true)
+            {
+            }

I have reproduced the issue.

ericwolz commented 2 years ago

Added a backlog task.