AzureSphere/AzureSphereSampleDevice/main.c
static void DeviceTwinCallback(DEVICE_TWIN_UPDATE_STATE updateState, const unsigned char *payload,
                               size_t payloadSize, void *userContextCallback)
{
    // Statically allocate this for more predictable memory use patterns
    static char nullTerminatedJsonString[MAX_DEVICE_TWIN_PAYLOAD_SIZE + 1];

    if (payloadSize > MAX_DEVICE_TWIN_PAYLOAD_SIZE) {
        Log_Debug("ERROR: Device twin payload size (%u bytes) exceeds maximum (%u bytes).\n",
                  payloadSize, MAX_DEVICE_TWIN_PAYLOAD_SIZE);

        exitCode = ExitCode_PayloadSize_TooLarge;
        return;
    }

    // Copy the payload to local buffer for null-termination.
    memcpy(nullTerminatedJsonString, payload, payloadSize);

    // Add the null terminator at the end.
    nullTerminatedJsonString[payloadSize] = 0;

    JSON_Value *rootProperties = NULL;
    rootProperties = json_parse_string(nullTerminatedJsonString);
    if (rootProperties == NULL) {
        Log_Debug("WARNING: Cannot parse the string as JSON content.\n");
        goto cleanup;
    }

    JSON_Object *rootObject = json_value_get_object(rootProperties);
    JSON_Object *desiredProperties = json_object_dotget_object(rootObject, "desired");
    if (desiredProperties == NULL) {
        desiredProperties = rootObject;
    }

    // The desired properties should have a "StatusLED" object
    int statusLedValue = json_object_dotget_boolean(desiredProperties, "StatusLED");
    if (statusLedValue != -1) {
        statusLedOn = statusLedValue == 1;
        GPIO_SetValue(deviceTwinStatusLedGpioFd, statusLedOn ? GPIO_Value_Low : GPIO_Value_High);
    }

    // Report current status LED state
    if (statusLedOn) {
        TwinReportState("{\"StatusLED\":true}");
    } else {
        TwinReportState("{\"StatusLED\":false}");
    }

cleanup:
    // Release the allocated memory.
    json_value_free(rootProperties);
}
/// <summary>
///     Callback invoked when a Device Twin update is received from Azure IoT Hub.
/// </summary>

Callback invoked when a Device Twin update is received from Azure IoT Hub.
Callback invoked when a Device Twin update is received from Azure IoT Hub .
static void DeviceTwinCallback(DEVICE_TWIN_UPDATE_STATE updateState, const unsigned char *payload,
                               size_t payloadSize, void *userContextCallback)
{
    static char nullTerminatedJsonString[MAX_DEVICE_TWIN_PAYLOAD_SIZE + 1];
    if (payloadSize > MAX_DEVICE_TWIN_PAYLOAD_SIZE) {
        Log_Debug("ERROR: Device twin payload size (%u bytes) exceeds maximum (%u bytes).\n",
                  payloadSize, MAX_DEVICE_TWIN_PAYLOAD_SIZE);
        exitCode = ExitCode_PayloadSize_TooLarge;
        return;
    }
    memcpy(nullTerminatedJsonString, payload, payloadSize);
    nullTerminatedJsonString[payloadSize] = 0;
    JSON_Value *rootProperties = NULL;
    rootProperties = json_parse_string(nullTerminatedJsonString);
    if (rootProperties == NULL) {
        Log_Debug("WARNING: Cannot parse the string as JSON content.\n");
        goto cleanup;
    }
    JSON_Object *rootObject = json_value_get_object(rootProperties);
    JSON_Object *desiredProperties = json_object_dotget_object(rootObject, "desired");
    if (desiredProperties == NULL) {
        desiredProperties = rootObject;
    }
    int statusLedValue = json_object_dotget_boolean(desiredProperties, "StatusLED");
    if (statusLedValue != -1) {
        statusLedOn = statusLedValue == 1;
        GPIO_SetValue(deviceTwinStatusLedGpioFd, statusLedOn ? GPIO_Value_Low : GPIO_Value_High);
    }
    if (statusLedOn) {
        TwinReportState("{\"StatusLED\":true}");
    } else {
        TwinReportState("{\"StatusLED\":false}");
    }
cleanup:
    json_value_free(rootProperties);
}
static void DeviceTwinCallback ( DEVICE_TWIN_UPDATE_STATE updateState , const unsigned char * payload , size_t payloadSize , void * userContextCallback ) { static char nullTerminatedJsonString [ MAX_DEVICE_TWIN_PAYLOAD_SIZE + 1 ] ; if ( payloadSize > MAX_DEVICE_TWIN_PAYLOAD_SIZE ) { Log_Debug ( " \n " , payloadSize , MAX_DEVICE_TWIN_PAYLOAD_SIZE ) ; exitCode = ExitCode_PayloadSize_TooLarge ; return ; } memcpy ( nullTerminatedJsonString , payload , payloadSize ) ; nullTerminatedJsonString [ payloadSize ] = 0 ; JSON_Value * rootProperties = NULL ; rootProperties = json_parse_string ( nullTerminatedJsonString ) ; if ( rootProperties == NULL ) { Log_Debug ( " \n " ) ; goto cleanup ; } JSON_Object * rootObject = json_value_get_object ( rootProperties ) ; JSON_Object * desiredProperties = json_object_dotget_object ( rootObject , " " ) ; if ( desiredProperties == NULL ) { desiredProperties = rootObject ; } int statusLedValue = json_object_dotget_boolean ( desiredProperties , " " ) ; if ( statusLedValue != -1 ) { statusLedOn = statusLedValue == 1 ; GPIO_SetValue ( deviceTwinStatusLedGpioFd , statusLedOn ? GPIO_Value_Low : GPIO_Value_High ) ; } if ( statusLedOn ) { TwinReportState ( " \" \" " ) ; } else { TwinReportState ( " \" \" " ) ; } cleanup : json_value_free ( rootProperties ) ; }
<summary>
Callback invoked when a Device Twin update is received from Azure IoT Hub.
<summary > Callback invoked when a Device Twin update is received from Azure IoT Hub .
// Statically allocate this for more predictable memory use patterns // Copy the payload to local buffer for null-termination. // Add the null terminator at the end. // The desired properties should have a "StatusLED" object // Report current status LED state // Release the allocated memory.