Microsoft Azure IoTHub client MQTT transport
Dependents: STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more
Diff: iothubtransport_mqtt_common.c
- Revision:
- 31:d6198e67d1eb
- Parent:
- 30:52ff609606c8
- Child:
- 32:103a46ed8822
--- a/iothubtransport_mqtt_common.c Sat Oct 21 20:11:19 2017 +0000 +++ b/iothubtransport_mqtt_common.c Fri Nov 03 13:17:53 2017 -0700 @@ -266,6 +266,37 @@ transport->saved_tls_options = new_options; } +static void free_transport_handle_data(MQTTTRANSPORT_HANDLE_DATA* transport_data) +{ + if (transport_data->mqttClient != NULL) + { + mqtt_client_deinit(transport_data->mqttClient); + } + + if (transport_data->retry_control_handle != NULL) + { + retry_control_destroy(transport_data->retry_control_handle); + } + + set_saved_tls_options(transport_data, NULL); + + tickcounter_destroy(transport_data->msgTickCounter); + + free_proxy_data(transport_data); + + STRING_delete(transport_data->devicesPath); + STRING_delete(transport_data->topic_MqttEvent); + STRING_delete(transport_data->topic_MqttMessage); + STRING_delete(transport_data->device_id); + STRING_delete(transport_data->hostAddress); + STRING_delete(transport_data->configPassedThroughUsername); + STRING_delete(transport_data->topic_GetState); + STRING_delete(transport_data->topic_NotifyState); + STRING_delete(transport_data->topic_DeviceMethods); + + free(transport_data); +} + int IoTHubTransport_MQTT_Common_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds) { int result; @@ -914,6 +945,56 @@ return result; } +static int setMqttMessagePropertyIfPossible(IOTHUB_MESSAGE_HANDLE IoTHubMessage, const char* propName, const char* propValue, size_t nameLen) +{ + // Not finding a system property to map to isn't an error. + int result = 0; + + if (nameLen > 2) + { + if (nameLen > 3) + { + if (strcmp((const char*)&propName[nameLen - 3], MESSAGE_ID_PROPERTY) == 0) + { + if (IoTHubMessage_SetMessageId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) + { + LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'messageId' property."); + result = __FAILURE__; + } + } + else if (strcmp((const char*)&propName[nameLen - 3], CORRELATION_ID_PROPERTY) == 0) + { + if (IoTHubMessage_SetCorrelationId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) + { + LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'correlationId' property."); + result = __FAILURE__; + } + } + } + + // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_012: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ct` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentType property ] + if (strcmp((const char*)&propName[nameLen - 2], CONTENT_TYPE_PROPERTY) == 0) + { + if (IoTHubMessage_SetContentTypeSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) + { + LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'customContentType' property."); + result = __FAILURE__; + } + } + // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_013: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ce` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentEncoding property ] + else if (strcmp((const char*)&propName[nameLen - 2], CONTENT_ENCODING_PROPERTY) == 0) + { + if (IoTHubMessage_SetContentEncodingSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) + { + LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'contentEncoding' property."); + result = __FAILURE__; + } + } + } + + return result; +} + static int extractMqttProperties(IOTHUB_MESSAGE_HANDLE IoTHubMessage, const char* topic_name) { int result; @@ -982,46 +1063,10 @@ strncpy(propValue, iterator + 1, valLen); propValue[valLen] = '\0'; - if (nameLen > 2) + if (setMqttMessagePropertyIfPossible(IoTHubMessage, propName, propValue, nameLen) != 0) { - if (nameLen > 3) - { - if (strcmp((const char*)&propName[nameLen - 3], MESSAGE_ID_PROPERTY) == 0) - { - if (IoTHubMessage_SetMessageId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) - { - LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'messageId' property."); - result = __FAILURE__; - } - } - else if (strcmp((const char*)&propName[nameLen - 3], CORRELATION_ID_PROPERTY) == 0) - { - if (IoTHubMessage_SetCorrelationId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) - { - LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'correlationId' property."); - result = __FAILURE__; - } - } - } - - // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_012: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ct` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentType property ] - if (strcmp((const char*)&propName[nameLen - 2], CONTENT_TYPE_PROPERTY) == 0) - { - if (IoTHubMessage_SetContentTypeSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) - { - LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'customContentType' property."); - result = __FAILURE__; - } - } - // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_013: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ce` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentEncoding property ] - else if (strcmp((const char*)&propName[nameLen - 2], CONTENT_ENCODING_PROPERTY) == 0) - { - if (IoTHubMessage_SetContentEncodingSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) - { - LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'contentEncoding' property."); - result = __FAILURE__; - } - } + LogError("Unable to set message property"); + result = __FAILURE__; } } free(propName); @@ -1867,31 +1912,26 @@ if ((state->msgTickCounter = tickcounter_create()) == NULL) { LogError("Invalid Argument: iotHubName is empty"); - free(state); + free_transport_handle_data(state); state = NULL; } // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_005: [ MQTT transport shall use EXPONENTIAL_WITH_BACK_OFF as default retry policy ] else if ((state->retry_control_handle = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_RETRY_TIMEOUT_IN_SECONDS)) == NULL) { LogError("Failed creating default retry control"); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL) { LogError("failure constructing device_id."); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else if ((state->devicesPath = STRING_construct_sprintf("%s.%s/devices/%s", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId)) == NULL) { - STRING_delete(state->device_id); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + LogError("failure constructing devicesPath."); + free_transport_handle_data(state); state = NULL; } else @@ -1899,11 +1939,7 @@ if ( (state->topic_MqttEvent = STRING_construct_sprintf(TOPIC_DEVICE_DEVICE, upperConfig->deviceId) ) == NULL) { LogError("Could not create topic_MqttEvent for MQTT"); - STRING_delete(state->devicesPath); - STRING_delete(state->device_id); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else @@ -1912,12 +1948,7 @@ if (state->mqttClient == NULL) { LogError("failure initializing mqtt client."); - STRING_delete(state->devicesPath); - STRING_delete(state->topic_MqttEvent); - STRING_delete(state->device_id); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else @@ -1935,25 +1966,12 @@ if (state->hostAddress == NULL) { LogError("failure constructing host address."); - mqtt_client_deinit(state->mqttClient); - STRING_delete(state->devicesPath); - STRING_delete(state->topic_MqttEvent); - STRING_delete(state->device_id); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else if ((state->configPassedThroughUsername = buildConfigForUsername(upperConfig)) == NULL) { - STRING_delete(state->hostAddress); - mqtt_client_deinit(state->mqttClient); - STRING_delete(state->devicesPath); - STRING_delete(state->topic_MqttEvent); - STRING_delete(state->device_id); - retry_control_destroy(state->retry_control_handle); - tickcounter_destroy(state->msgTickCounter); - free(state); + free_transport_handle_data(state); state = NULL; } else @@ -2097,26 +2115,9 @@ free(mqtt_device_twin); } - STRING_delete(transport_data->devicesPath); - /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransport_MQTT_Common_Destroy shall free all the resources currently in use.] */ - mqtt_client_deinit(transport_data->mqttClient); - retry_control_destroy(transport_data->retry_control_handle); - STRING_delete(transport_data->topic_MqttEvent); - STRING_delete(transport_data->topic_MqttMessage); - STRING_delete(transport_data->device_id); - STRING_delete(transport_data->hostAddress); - STRING_delete(transport_data->configPassedThroughUsername); - STRING_delete(transport_data->topic_GetState); - STRING_delete(transport_data->topic_NotifyState); - STRING_delete(transport_data->topic_DeviceMethods); - - set_saved_tls_options(transport_data, NULL); - - tickcounter_destroy(transport_data->msgTickCounter); /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_012: [ `IoTHubTransport_MQTT_Common_Destroy` shall free the stored proxy options. ]*/ - free_proxy_data(transport_data); - free(transport_data); + free_transport_handle_data(transport_data); } }