Microsoft Azure IoTHub client MQTT transport
Dependents: STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more
iothubtransport_mqtt_common.c@39:6231984e0179, 2018-06-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Mon Jun 11 15:38:57 2018 -0700
- Revision:
- 39:6231984e0179
- Parent:
- 38:369a06de92aa
- Child:
- 40:cb03d6a6f46d
1.2.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 11:31ebaeb51e1e | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 11:31ebaeb51e1e | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 11:31ebaeb51e1e | 3 | |
AzureIoTClient | 11:31ebaeb51e1e | 4 | #include <stdlib.h> |
AzureIoTClient | 12:658ca6865de2 | 5 | #include <ctype.h> |
AzureIoTClient | 18:ec8e5e97c6a4 | 6 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 11:31ebaeb51e1e | 7 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 11:31ebaeb51e1e | 8 | |
AzureIoTClient | 11:31ebaeb51e1e | 9 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 11:31ebaeb51e1e | 10 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 11:31ebaeb51e1e | 11 | #include "azure_c_shared_utility/doublylinkedlist.h" |
AzureIoTClient | 11:31ebaeb51e1e | 12 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 11:31ebaeb51e1e | 13 | |
AzureIoTClient | 39:6231984e0179 | 14 | #include "iothub_client_core_ll.h" |
AzureIoTClient | 11:31ebaeb51e1e | 15 | #include "iothub_client_options.h" |
AzureIoTClient | 39:6231984e0179 | 16 | #include "internal/iothub_client_private.h" |
AzureIoTClient | 11:31ebaeb51e1e | 17 | #include "azure_umqtt_c/mqtt_client.h" |
AzureIoTClient | 11:31ebaeb51e1e | 18 | #include "azure_c_shared_utility/sastoken.h" |
AzureIoTClient | 11:31ebaeb51e1e | 19 | #include "azure_c_shared_utility/tickcounter.h" |
AzureIoTClient | 11:31ebaeb51e1e | 20 | |
AzureIoTClient | 11:31ebaeb51e1e | 21 | #include "azure_c_shared_utility/tlsio.h" |
AzureIoTClient | 11:31ebaeb51e1e | 22 | #include "azure_c_shared_utility/platform.h" |
AzureIoTClient | 11:31ebaeb51e1e | 23 | |
AzureIoTClient | 11:31ebaeb51e1e | 24 | #include "azure_c_shared_utility/string_tokenizer.h" |
AzureIoTClient | 20:594780be216d | 25 | #include "azure_c_shared_utility/shared_util_options.h" |
AzureIoTClient | 22:07fec4d325b6 | 26 | #include "azure_c_shared_utility/urlencode.h" |
AzureIoTClient | 11:31ebaeb51e1e | 27 | #include "iothub_client_version.h" |
AzureIoTClient | 39:6231984e0179 | 28 | #include "internal/iothub_client_retry_control.h" |
AzureIoTClient | 11:31ebaeb51e1e | 29 | |
AzureIoTClient | 39:6231984e0179 | 30 | #include "internal/iothubtransport_mqtt_common.h" |
AzureIoTClient | 11:31ebaeb51e1e | 31 | |
AzureIoTClient | 11:31ebaeb51e1e | 32 | #include <stdarg.h> |
AzureIoTClient | 11:31ebaeb51e1e | 33 | #include <stdio.h> |
AzureIoTClient | 11:31ebaeb51e1e | 34 | |
AzureIoTClient | 11:31ebaeb51e1e | 35 | #include <limits.h> |
AzureIoTClient | 12:658ca6865de2 | 36 | #include <inttypes.h> |
AzureIoTClient | 11:31ebaeb51e1e | 37 | |
AzureIoTClient | 25:a35763780a87 | 38 | #define SAS_TOKEN_DEFAULT_LIFETIME 3600 |
AzureIoTClient | 25:a35763780a87 | 39 | #define SAS_REFRESH_MULTIPLIER .8 |
AzureIoTClient | 25:a35763780a87 | 40 | #define EPOCH_TIME_T_VALUE 0 |
AzureIoTClient | 25:a35763780a87 | 41 | #define DEFAULT_MQTT_KEEPALIVE 4*60 // 4 min |
AzureIoTClient | 32:103a46ed8822 | 42 | #define DEFAULT_CONNACK_TIMEOUT 30 // 30 seconds |
AzureIoTClient | 25:a35763780a87 | 43 | #define BUILD_CONFIG_USERNAME 24 |
AzureIoTClient | 25:a35763780a87 | 44 | #define SAS_TOKEN_DEFAULT_LEN 10 |
AzureIoTClient | 25:a35763780a87 | 45 | #define RESEND_TIMEOUT_VALUE_MIN 1*60 |
AzureIoTClient | 25:a35763780a87 | 46 | #define MAX_SEND_RECOUNT_LIMIT 2 |
AzureIoTClient | 25:a35763780a87 | 47 | #define DEFAULT_CONNECTION_INTERVAL 30 |
AzureIoTClient | 25:a35763780a87 | 48 | #define FAILED_CONN_BACKOFF_VALUE 5 |
AzureIoTClient | 25:a35763780a87 | 49 | #define STATUS_CODE_FAILURE_VALUE 500 |
AzureIoTClient | 25:a35763780a87 | 50 | #define STATUS_CODE_TIMEOUT_VALUE 408 |
AzureIoTClient | 25:a35763780a87 | 51 | |
AzureIoTClient | 25:a35763780a87 | 52 | #define DEFAULT_RETRY_POLICY IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER |
AzureIoTClient | 25:a35763780a87 | 53 | #define DEFAULT_RETRY_TIMEOUT_IN_SECONDS 0 |
AzureIoTClient | 12:658ca6865de2 | 54 | |
AzureIoTClient | 12:658ca6865de2 | 55 | static const char TOPIC_DEVICE_TWIN_PREFIX[] = "$iothub/twin"; |
AzureIoTClient | 12:658ca6865de2 | 56 | static const char TOPIC_DEVICE_METHOD_PREFIX[] = "$iothub/methods"; |
AzureIoTClient | 12:658ca6865de2 | 57 | |
AzureIoTClient | 12:658ca6865de2 | 58 | static const char* TOPIC_GET_DESIRED_STATE = "$iothub/twin/res/#"; |
AzureIoTClient | 12:658ca6865de2 | 59 | static const char* TOPIC_NOTIFICATION_STATE = "$iothub/twin/PATCH/properties/desired/#"; |
AzureIoTClient | 11:31ebaeb51e1e | 60 | |
AzureIoTClient | 11:31ebaeb51e1e | 61 | static const char* TOPIC_DEVICE_MSG = "devices/%s/messages/devicebound/#"; |
AzureIoTClient | 11:31ebaeb51e1e | 62 | static const char* TOPIC_DEVICE_DEVICE = "devices/%s/messages/events/"; |
AzureIoTClient | 12:658ca6865de2 | 63 | |
AzureIoTClient | 12:658ca6865de2 | 64 | static const char* TOPIC_DEVICE_METHOD_SUBSCRIBE = "$iothub/methods/POST/#"; |
AzureIoTClient | 12:658ca6865de2 | 65 | |
AzureIoTClient | 12:658ca6865de2 | 66 | static const char* IOTHUB_API_VERSION = "2016-11-14"; |
AzureIoTClient | 12:658ca6865de2 | 67 | |
AzureIoTClient | 11:31ebaeb51e1e | 68 | static const char* PROPERTY_SEPARATOR = "&"; |
AzureIoTClient | 12:658ca6865de2 | 69 | static const char* REPORTED_PROPERTIES_TOPIC = "$iothub/twin/PATCH/properties/reported/?$rid=%"PRIu16; |
AzureIoTClient | 12:658ca6865de2 | 70 | static const char* GET_PROPERTIES_TOPIC = "$iothub/twin/GET/?$rid=%"PRIu16; |
AzureIoTClient | 14:4dc2b011be33 | 71 | static const char* DEVICE_METHOD_RESPONSE_TOPIC = "$iothub/methods/res/%d/?$rid=%s"; |
AzureIoTClient | 12:658ca6865de2 | 72 | |
AzureIoTClient | 12:658ca6865de2 | 73 | static const char* REQUEST_ID_PROPERTY = "?$rid="; |
AzureIoTClient | 11:31ebaeb51e1e | 74 | |
AzureIoTClient | 18:ec8e5e97c6a4 | 75 | static const char* MESSAGE_ID_PROPERTY = "mid"; |
AzureIoTClient | 18:ec8e5e97c6a4 | 76 | static const char* CORRELATION_ID_PROPERTY = "cid"; |
AzureIoTClient | 27:04de3c0bf1db | 77 | static const char* CONTENT_TYPE_PROPERTY = "ct"; |
AzureIoTClient | 27:04de3c0bf1db | 78 | static const char* CONTENT_ENCODING_PROPERTY = "ce"; |
AzureIoTClient | 30:52ff609606c8 | 79 | static const char* DIAGNOSTIC_ID_PROPERTY = "diagid"; |
AzureIoTClient | 30:52ff609606c8 | 80 | static const char* DIAGNOSTIC_CONTEXT_PROPERTY = "diagctx"; |
AzureIoTClient | 30:52ff609606c8 | 81 | |
AzureIoTClient | 30:52ff609606c8 | 82 | static const char* DIAGNOSTIC_CONTEXT_CREATION_TIME_UTC_PROPERTY = "creationtimeutc"; |
AzureIoTClient | 18:ec8e5e97c6a4 | 83 | |
AzureIoTClient | 11:31ebaeb51e1e | 84 | #define UNSUBSCRIBE_FROM_TOPIC 0x0000 |
AzureIoTClient | 12:658ca6865de2 | 85 | #define SUBSCRIBE_GET_REPORTED_STATE_TOPIC 0x0001 |
AzureIoTClient | 12:658ca6865de2 | 86 | #define SUBSCRIBE_NOTIFICATION_STATE_TOPIC 0x0002 |
AzureIoTClient | 11:31ebaeb51e1e | 87 | #define SUBSCRIBE_TELEMETRY_TOPIC 0x0004 |
AzureIoTClient | 12:658ca6865de2 | 88 | #define SUBSCRIBE_METHODS_TOPIC 0x0008 |
AzureIoTClient | 12:658ca6865de2 | 89 | #define SUBSCRIBE_DEVICE_METHOD_TOPIC 0x0010 |
AzureIoTClient | 12:658ca6865de2 | 90 | #define SUBSCRIBE_TOPIC_COUNT 4 |
AzureIoTClient | 11:31ebaeb51e1e | 91 | |
AzureIoTClient | 17:774695cb8554 | 92 | DEFINE_ENUM_STRINGS(MQTT_CLIENT_EVENT_ERROR, MQTT_CLIENT_EVENT_ERROR_VALUES) |
AzureIoTClient | 17:774695cb8554 | 93 | |
AzureIoTClient | 11:31ebaeb51e1e | 94 | typedef struct SYSTEM_PROPERTY_INFO_TAG |
AzureIoTClient | 11:31ebaeb51e1e | 95 | { |
AzureIoTClient | 11:31ebaeb51e1e | 96 | const char* propName; |
AzureIoTClient | 11:31ebaeb51e1e | 97 | size_t propLength; |
AzureIoTClient | 11:31ebaeb51e1e | 98 | } SYSTEM_PROPERTY_INFO; |
AzureIoTClient | 11:31ebaeb51e1e | 99 | |
AzureIoTClient | 11:31ebaeb51e1e | 100 | static SYSTEM_PROPERTY_INFO sysPropList[] = { |
AzureIoTClient | 11:31ebaeb51e1e | 101 | { "%24.exp", 7 }, |
AzureIoTClient | 11:31ebaeb51e1e | 102 | { "%24.mid", 7 }, |
AzureIoTClient | 11:31ebaeb51e1e | 103 | { "%24.uid", 7 }, |
AzureIoTClient | 11:31ebaeb51e1e | 104 | { "%24.to", 6 }, |
AzureIoTClient | 11:31ebaeb51e1e | 105 | { "%24.cid", 7 }, |
AzureIoTClient | 27:04de3c0bf1db | 106 | { "%24.ct", 6 }, |
AzureIoTClient | 27:04de3c0bf1db | 107 | { "%24.ce", 6 }, |
AzureIoTClient | 11:31ebaeb51e1e | 108 | { "devices/", 8 }, |
AzureIoTClient | 11:31ebaeb51e1e | 109 | { "iothub-operation", 16 }, |
AzureIoTClient | 11:31ebaeb51e1e | 110 | { "iothub-ack", 10 } |
AzureIoTClient | 11:31ebaeb51e1e | 111 | }; |
AzureIoTClient | 11:31ebaeb51e1e | 112 | |
AzureIoTClient | 12:658ca6865de2 | 113 | typedef enum DEVICE_TWIN_MSG_TYPE_TAG |
AzureIoTClient | 12:658ca6865de2 | 114 | { |
AzureIoTClient | 12:658ca6865de2 | 115 | REPORTED_STATE, |
AzureIoTClient | 12:658ca6865de2 | 116 | RETRIEVE_PROPERTIES |
AzureIoTClient | 12:658ca6865de2 | 117 | } DEVICE_TWIN_MSG_TYPE; |
AzureIoTClient | 12:658ca6865de2 | 118 | |
AzureIoTClient | 11:31ebaeb51e1e | 119 | typedef enum MQTT_TRANSPORT_CREDENTIAL_TYPE_TAG |
AzureIoTClient | 11:31ebaeb51e1e | 120 | { |
AzureIoTClient | 11:31ebaeb51e1e | 121 | CREDENTIAL_NOT_BUILD, |
AzureIoTClient | 11:31ebaeb51e1e | 122 | X509, |
AzureIoTClient | 11:31ebaeb51e1e | 123 | SAS_TOKEN_FROM_USER, |
AzureIoTClient | 11:31ebaeb51e1e | 124 | DEVICE_KEY, |
AzureIoTClient | 11:31ebaeb51e1e | 125 | } MQTT_TRANSPORT_CREDENTIAL_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 126 | |
AzureIoTClient | 20:594780be216d | 127 | typedef enum MQTT_CLIENT_STATUS_TAG |
AzureIoTClient | 20:594780be216d | 128 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 129 | MQTT_CLIENT_STATUS_NOT_CONNECTED, |
AzureIoTClient | 21:167f8fcf6fb1 | 130 | MQTT_CLIENT_STATUS_CONNECTING, |
AzureIoTClient | 33:b7dfb208ef0a | 131 | MQTT_CLIENT_STATUS_CONNECTED, |
AzureIoTClient | 33:b7dfb208ef0a | 132 | MQTT_CLIENT_STATUS_PENDING_CLOSE |
AzureIoTClient | 20:594780be216d | 133 | } MQTT_CLIENT_STATUS; |
AzureIoTClient | 20:594780be216d | 134 | |
AzureIoTClient | 11:31ebaeb51e1e | 135 | typedef struct MQTTTRANSPORT_HANDLE_DATA_TAG |
AzureIoTClient | 11:31ebaeb51e1e | 136 | { |
AzureIoTClient | 11:31ebaeb51e1e | 137 | // Topic control |
AzureIoTClient | 11:31ebaeb51e1e | 138 | STRING_HANDLE topic_MqttEvent; |
AzureIoTClient | 11:31ebaeb51e1e | 139 | STRING_HANDLE topic_MqttMessage; |
AzureIoTClient | 12:658ca6865de2 | 140 | STRING_HANDLE topic_GetState; |
AzureIoTClient | 12:658ca6865de2 | 141 | STRING_HANDLE topic_NotifyState; |
AzureIoTClient | 12:658ca6865de2 | 142 | |
AzureIoTClient | 12:658ca6865de2 | 143 | STRING_HANDLE topic_DeviceMethods; |
AzureIoTClient | 11:31ebaeb51e1e | 144 | |
AzureIoTClient | 11:31ebaeb51e1e | 145 | uint32_t topics_ToSubscribe; |
AzureIoTClient | 12:658ca6865de2 | 146 | |
AzureIoTClient | 11:31ebaeb51e1e | 147 | // Connection related constants |
AzureIoTClient | 11:31ebaeb51e1e | 148 | STRING_HANDLE hostAddress; |
AzureIoTClient | 11:31ebaeb51e1e | 149 | STRING_HANDLE device_id; |
AzureIoTClient | 11:31ebaeb51e1e | 150 | STRING_HANDLE devicesPath; |
AzureIoTClient | 11:31ebaeb51e1e | 151 | int portNum; |
AzureIoTClient | 11:31ebaeb51e1e | 152 | |
AzureIoTClient | 11:31ebaeb51e1e | 153 | MQTT_GET_IO_TRANSPORT get_io_transport; |
AzureIoTClient | 11:31ebaeb51e1e | 154 | |
AzureIoTClient | 11:31ebaeb51e1e | 155 | // The current mqtt iothub implementation requires that the hub name and the domain suffix be passed as the first of a series of segments |
AzureIoTClient | 11:31ebaeb51e1e | 156 | // passed through the username portion of the connection frame. |
AzureIoTClient | 11:31ebaeb51e1e | 157 | // The second segment will contain the device id. The two segments are delemited by a "/". |
AzureIoTClient | 11:31ebaeb51e1e | 158 | // The first segment can be a maximum 256 characters. |
AzureIoTClient | 11:31ebaeb51e1e | 159 | // The second segment can be a maximum 128 characters. |
AzureIoTClient | 11:31ebaeb51e1e | 160 | // With the / delimeter you have 384 chars (Plus a terminator of 0). |
AzureIoTClient | 11:31ebaeb51e1e | 161 | STRING_HANDLE configPassedThroughUsername; |
AzureIoTClient | 11:31ebaeb51e1e | 162 | |
AzureIoTClient | 11:31ebaeb51e1e | 163 | // Upper layer |
AzureIoTClient | 39:6231984e0179 | 164 | IOTHUB_CLIENT_CORE_LL_HANDLE llClientHandle; |
AzureIoTClient | 11:31ebaeb51e1e | 165 | |
AzureIoTClient | 11:31ebaeb51e1e | 166 | // Protocol |
AzureIoTClient | 11:31ebaeb51e1e | 167 | MQTT_CLIENT_HANDLE mqttClient; |
AzureIoTClient | 11:31ebaeb51e1e | 168 | XIO_HANDLE xioTransport; |
AzureIoTClient | 11:31ebaeb51e1e | 169 | |
AzureIoTClient | 11:31ebaeb51e1e | 170 | // Session - connection |
AzureIoTClient | 11:31ebaeb51e1e | 171 | uint16_t packetId; |
AzureIoTClient | 11:31ebaeb51e1e | 172 | |
AzureIoTClient | 11:31ebaeb51e1e | 173 | // Connection state control |
AzureIoTClient | 11:31ebaeb51e1e | 174 | bool isRegistered; |
AzureIoTClient | 20:594780be216d | 175 | MQTT_CLIENT_STATUS mqttClientStatus; |
AzureIoTClient | 11:31ebaeb51e1e | 176 | bool isDestroyCalled; |
AzureIoTClient | 12:658ca6865de2 | 177 | bool device_twin_get_sent; |
AzureIoTClient | 12:658ca6865de2 | 178 | bool isRecoverableError; |
AzureIoTClient | 11:31ebaeb51e1e | 179 | uint16_t keepAliveValue; |
AzureIoTClient | 32:103a46ed8822 | 180 | uint16_t connect_timeout_in_sec; |
Azure.IoT.Build | 13:606465879c57 | 181 | tickcounter_ms_t mqtt_connect_time; |
AzureIoTClient | 11:31ebaeb51e1e | 182 | size_t connectFailCount; |
Azure.IoT.Build | 13:606465879c57 | 183 | tickcounter_ms_t connectTick; |
AzureIoTClient | 11:31ebaeb51e1e | 184 | bool log_trace; |
AzureIoTClient | 11:31ebaeb51e1e | 185 | bool raw_trace; |
Azure.IoT.Build | 13:606465879c57 | 186 | TICK_COUNTER_HANDLE msgTickCounter; |
AzureIoTClient | 29:923be0c3998a | 187 | OPTIONHANDLER_HANDLE saved_tls_options; // Here are the options from the xio layer if any is saved. |
AzureIoTClient | 29:923be0c3998a | 188 | size_t option_sas_token_lifetime_secs; |
AzureIoTClient | 11:31ebaeb51e1e | 189 | |
AzureIoTClient | 11:31ebaeb51e1e | 190 | // Internal lists for message tracking |
AzureIoTClient | 11:31ebaeb51e1e | 191 | PDLIST_ENTRY waitingToSend; |
AzureIoTClient | 12:658ca6865de2 | 192 | DLIST_ENTRY ack_waiting_queue; |
AzureIoTClient | 12:658ca6865de2 | 193 | |
AzureIoTClient | 11:31ebaeb51e1e | 194 | // Message tracking |
AzureIoTClient | 11:31ebaeb51e1e | 195 | CONTROL_PACKET_TYPE currPacketState; |
AzureIoTClient | 11:31ebaeb51e1e | 196 | |
AzureIoTClient | 11:31ebaeb51e1e | 197 | // Telemetry specific |
AzureIoTClient | 11:31ebaeb51e1e | 198 | DLIST_ENTRY telemetry_waitingForAck; |
AzureIoTClient | 37:e6a799428f3d | 199 | bool auto_url_encode_decode; |
AzureIoTClient | 12:658ca6865de2 | 200 | |
AzureIoTClient | 25:a35763780a87 | 201 | // Controls frequency of reconnection logic. |
AzureIoTClient | 25:a35763780a87 | 202 | RETRY_CONTROL_HANDLE retry_control_handle; |
AzureIoTClient | 20:594780be216d | 203 | |
AzureIoTClient | 21:167f8fcf6fb1 | 204 | // Auth module used to generating handle authorization |
AzureIoTClient | 21:167f8fcf6fb1 | 205 | // with either SAS Token, x509 Certs, and Device SAS Token |
AzureIoTClient | 21:167f8fcf6fb1 | 206 | IOTHUB_AUTHORIZATION_HANDLE authorization_module; |
AzureIoTClient | 21:167f8fcf6fb1 | 207 | |
AzureIoTClient | 20:594780be216d | 208 | char* http_proxy_hostname; |
AzureIoTClient | 20:594780be216d | 209 | int http_proxy_port; |
AzureIoTClient | 20:594780be216d | 210 | char* http_proxy_username; |
AzureIoTClient | 20:594780be216d | 211 | char* http_proxy_password; |
AzureIoTClient | 24:4096249decf1 | 212 | bool isProductInfoSet; |
AzureIoTClient | 11:31ebaeb51e1e | 213 | } MQTTTRANSPORT_HANDLE_DATA, *PMQTTTRANSPORT_HANDLE_DATA; |
AzureIoTClient | 11:31ebaeb51e1e | 214 | |
AzureIoTClient | 12:658ca6865de2 | 215 | typedef struct MQTT_DEVICE_TWIN_ITEM_TAG |
AzureIoTClient | 12:658ca6865de2 | 216 | { |
Azure.IoT.Build | 13:606465879c57 | 217 | tickcounter_ms_t msgPublishTime; |
AzureIoTClient | 12:658ca6865de2 | 218 | size_t retryCount; |
AzureIoTClient | 12:658ca6865de2 | 219 | IOTHUB_IDENTITY_TYPE iothub_type; |
AzureIoTClient | 12:658ca6865de2 | 220 | uint16_t packet_id; |
AzureIoTClient | 12:658ca6865de2 | 221 | uint32_t iothub_msg_id; |
AzureIoTClient | 12:658ca6865de2 | 222 | IOTHUB_DEVICE_TWIN* device_twin_data; |
AzureIoTClient | 12:658ca6865de2 | 223 | DEVICE_TWIN_MSG_TYPE device_twin_msg_type; |
AzureIoTClient | 12:658ca6865de2 | 224 | DLIST_ENTRY entry; |
AzureIoTClient | 12:658ca6865de2 | 225 | } MQTT_DEVICE_TWIN_ITEM; |
AzureIoTClient | 12:658ca6865de2 | 226 | |
AzureIoTClient | 11:31ebaeb51e1e | 227 | typedef struct MQTT_MESSAGE_DETAILS_LIST_TAG |
AzureIoTClient | 11:31ebaeb51e1e | 228 | { |
Azure.IoT.Build | 13:606465879c57 | 229 | tickcounter_ms_t msgPublishTime; |
AzureIoTClient | 11:31ebaeb51e1e | 230 | size_t retryCount; |
AzureIoTClient | 11:31ebaeb51e1e | 231 | IOTHUB_MESSAGE_LIST* iotHubMessageEntry; |
AzureIoTClient | 11:31ebaeb51e1e | 232 | void* context; |
AzureIoTClient | 11:31ebaeb51e1e | 233 | uint16_t packet_id; |
AzureIoTClient | 11:31ebaeb51e1e | 234 | DLIST_ENTRY entry; |
AzureIoTClient | 11:31ebaeb51e1e | 235 | } MQTT_MESSAGE_DETAILS_LIST, *PMQTT_MESSAGE_DETAILS_LIST; |
AzureIoTClient | 11:31ebaeb51e1e | 236 | |
AzureIoTClient | 14:4dc2b011be33 | 237 | typedef struct DEVICE_METHOD_INFO_TAG |
AzureIoTClient | 14:4dc2b011be33 | 238 | { |
AzureIoTClient | 14:4dc2b011be33 | 239 | STRING_HANDLE request_id; |
AzureIoTClient | 14:4dc2b011be33 | 240 | } DEVICE_METHOD_INFO; |
AzureIoTClient | 14:4dc2b011be33 | 241 | |
AzureIoTClient | 20:594780be216d | 242 | static void free_proxy_data(MQTTTRANSPORT_HANDLE_DATA* mqtt_transport_instance) |
AzureIoTClient | 20:594780be216d | 243 | { |
AzureIoTClient | 20:594780be216d | 244 | if (mqtt_transport_instance->http_proxy_hostname != NULL) |
AzureIoTClient | 20:594780be216d | 245 | { |
AzureIoTClient | 20:594780be216d | 246 | free(mqtt_transport_instance->http_proxy_hostname); |
AzureIoTClient | 20:594780be216d | 247 | mqtt_transport_instance->http_proxy_hostname = NULL; |
AzureIoTClient | 20:594780be216d | 248 | } |
AzureIoTClient | 20:594780be216d | 249 | |
AzureIoTClient | 20:594780be216d | 250 | if (mqtt_transport_instance->http_proxy_username != NULL) |
AzureIoTClient | 20:594780be216d | 251 | { |
AzureIoTClient | 20:594780be216d | 252 | free(mqtt_transport_instance->http_proxy_username); |
AzureIoTClient | 20:594780be216d | 253 | mqtt_transport_instance->http_proxy_username = NULL; |
AzureIoTClient | 20:594780be216d | 254 | } |
AzureIoTClient | 20:594780be216d | 255 | |
AzureIoTClient | 20:594780be216d | 256 | if (mqtt_transport_instance->http_proxy_password != NULL) |
AzureIoTClient | 20:594780be216d | 257 | { |
AzureIoTClient | 20:594780be216d | 258 | free(mqtt_transport_instance->http_proxy_password); |
AzureIoTClient | 20:594780be216d | 259 | mqtt_transport_instance->http_proxy_password = NULL; |
AzureIoTClient | 20:594780be216d | 260 | } |
AzureIoTClient | 20:594780be216d | 261 | } |
AzureIoTClient | 20:594780be216d | 262 | |
AzureIoTClient | 28:0cd355c3294e | 263 | static void set_saved_tls_options(PMQTTTRANSPORT_HANDLE_DATA transport, OPTIONHANDLER_HANDLE new_options) |
AzureIoTClient | 28:0cd355c3294e | 264 | { |
AzureIoTClient | 28:0cd355c3294e | 265 | if (transport->saved_tls_options != NULL) |
AzureIoTClient | 28:0cd355c3294e | 266 | { |
AzureIoTClient | 28:0cd355c3294e | 267 | OptionHandler_Destroy(transport->saved_tls_options); |
AzureIoTClient | 28:0cd355c3294e | 268 | } |
AzureIoTClient | 28:0cd355c3294e | 269 | transport->saved_tls_options = new_options; |
AzureIoTClient | 28:0cd355c3294e | 270 | } |
AzureIoTClient | 28:0cd355c3294e | 271 | |
AzureIoTClient | 31:d6198e67d1eb | 272 | static void free_transport_handle_data(MQTTTRANSPORT_HANDLE_DATA* transport_data) |
AzureIoTClient | 31:d6198e67d1eb | 273 | { |
AzureIoTClient | 31:d6198e67d1eb | 274 | if (transport_data->mqttClient != NULL) |
AzureIoTClient | 31:d6198e67d1eb | 275 | { |
AzureIoTClient | 31:d6198e67d1eb | 276 | mqtt_client_deinit(transport_data->mqttClient); |
AzureIoTClient | 31:d6198e67d1eb | 277 | } |
AzureIoTClient | 31:d6198e67d1eb | 278 | |
AzureIoTClient | 31:d6198e67d1eb | 279 | if (transport_data->retry_control_handle != NULL) |
AzureIoTClient | 31:d6198e67d1eb | 280 | { |
AzureIoTClient | 31:d6198e67d1eb | 281 | retry_control_destroy(transport_data->retry_control_handle); |
AzureIoTClient | 31:d6198e67d1eb | 282 | } |
AzureIoTClient | 31:d6198e67d1eb | 283 | |
AzureIoTClient | 31:d6198e67d1eb | 284 | set_saved_tls_options(transport_data, NULL); |
AzureIoTClient | 31:d6198e67d1eb | 285 | |
AzureIoTClient | 31:d6198e67d1eb | 286 | tickcounter_destroy(transport_data->msgTickCounter); |
AzureIoTClient | 31:d6198e67d1eb | 287 | |
AzureIoTClient | 31:d6198e67d1eb | 288 | free_proxy_data(transport_data); |
AzureIoTClient | 31:d6198e67d1eb | 289 | |
AzureIoTClient | 31:d6198e67d1eb | 290 | STRING_delete(transport_data->devicesPath); |
AzureIoTClient | 31:d6198e67d1eb | 291 | STRING_delete(transport_data->topic_MqttEvent); |
AzureIoTClient | 31:d6198e67d1eb | 292 | STRING_delete(transport_data->topic_MqttMessage); |
AzureIoTClient | 31:d6198e67d1eb | 293 | STRING_delete(transport_data->device_id); |
AzureIoTClient | 31:d6198e67d1eb | 294 | STRING_delete(transport_data->hostAddress); |
AzureIoTClient | 31:d6198e67d1eb | 295 | STRING_delete(transport_data->configPassedThroughUsername); |
AzureIoTClient | 31:d6198e67d1eb | 296 | STRING_delete(transport_data->topic_GetState); |
AzureIoTClient | 31:d6198e67d1eb | 297 | STRING_delete(transport_data->topic_NotifyState); |
AzureIoTClient | 31:d6198e67d1eb | 298 | STRING_delete(transport_data->topic_DeviceMethods); |
AzureIoTClient | 31:d6198e67d1eb | 299 | |
AzureIoTClient | 31:d6198e67d1eb | 300 | free(transport_data); |
AzureIoTClient | 31:d6198e67d1eb | 301 | } |
AzureIoTClient | 31:d6198e67d1eb | 302 | |
AzureIoTClient | 25:a35763780a87 | 303 | int IoTHubTransport_MQTT_Common_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds) |
AzureIoTClient | 12:658ca6865de2 | 304 | { |
AzureIoTClient | 12:658ca6865de2 | 305 | int result; |
AzureIoTClient | 12:658ca6865de2 | 306 | |
AzureIoTClient | 25:a35763780a87 | 307 | if (handle == NULL) |
AzureIoTClient | 12:658ca6865de2 | 308 | { |
AzureIoTClient | 12:658ca6865de2 | 309 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_25_041: [**If any handle is NULL then IoTHubTransport_MQTT_Common_SetRetryPolicy shall return resultant line.] */ |
AzureIoTClient | 12:658ca6865de2 | 310 | LogError("Invalid handle parameter. NULL."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 311 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 312 | } |
AzureIoTClient | 12:658ca6865de2 | 313 | else |
AzureIoTClient | 12:658ca6865de2 | 314 | { |
AzureIoTClient | 25:a35763780a87 | 315 | RETRY_CONTROL_HANDLE new_retry_control_handle; |
AzureIoTClient | 25:a35763780a87 | 316 | |
AzureIoTClient | 25:a35763780a87 | 317 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_006: [ IoTHubTransport_MQTT_Common_SetRetryPolicy shall set the retry logic by calling retry_control_create() with retry policy and retryTimeout as parameters] |
AzureIoTClient | 25:a35763780a87 | 318 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_009: [ If retry_control_create() fails then IoTHubTransport_MQTT_Common_SetRetryPolicy shall revert to previous retry policy and return non-zero value ] |
AzureIoTClient | 25:a35763780a87 | 319 | if ((new_retry_control_handle = retry_control_create(retryPolicy, (unsigned int)retryTimeoutLimitInSeconds)) == NULL) |
AzureIoTClient | 12:658ca6865de2 | 320 | { |
AzureIoTClient | 25:a35763780a87 | 321 | LogError("Failed creating new retry control handle"); |
AzureIoTClient | 25:a35763780a87 | 322 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 323 | } |
AzureIoTClient | 12:658ca6865de2 | 324 | else |
AzureIoTClient | 12:658ca6865de2 | 325 | { |
AzureIoTClient | 25:a35763780a87 | 326 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 25:a35763780a87 | 327 | RETRY_CONTROL_HANDLE previous_retry_control_handle = transport_data->retry_control_handle; |
AzureIoTClient | 25:a35763780a87 | 328 | |
AzureIoTClient | 25:a35763780a87 | 329 | transport_data->retry_control_handle = new_retry_control_handle; |
AzureIoTClient | 25:a35763780a87 | 330 | retry_control_destroy(previous_retry_control_handle); |
AzureIoTClient | 25:a35763780a87 | 331 | |
AzureIoTClient | 25:a35763780a87 | 332 | /*Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_25_045: [**If retry logic for specified parameters of retry policy and retryTimeoutLimitInSeconds is created successfully then IoTHubTransport_MQTT_Common_SetRetryPolicy shall return 0]*/ |
AzureIoTClient | 25:a35763780a87 | 333 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 334 | } |
AzureIoTClient | 12:658ca6865de2 | 335 | } |
AzureIoTClient | 25:a35763780a87 | 336 | |
AzureIoTClient | 12:658ca6865de2 | 337 | return result; |
AzureIoTClient | 12:658ca6865de2 | 338 | } |
AzureIoTClient | 12:658ca6865de2 | 339 | |
AzureIoTClient | 11:31ebaeb51e1e | 340 | static uint16_t get_next_packet_id(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 11:31ebaeb51e1e | 341 | { |
AzureIoTClient | 11:31ebaeb51e1e | 342 | if (transport_data->packetId+1 >= USHRT_MAX) |
AzureIoTClient | 11:31ebaeb51e1e | 343 | { |
AzureIoTClient | 11:31ebaeb51e1e | 344 | transport_data->packetId = 1; |
AzureIoTClient | 11:31ebaeb51e1e | 345 | } |
AzureIoTClient | 11:31ebaeb51e1e | 346 | else |
AzureIoTClient | 11:31ebaeb51e1e | 347 | { |
AzureIoTClient | 11:31ebaeb51e1e | 348 | transport_data->packetId++; |
AzureIoTClient | 11:31ebaeb51e1e | 349 | } |
AzureIoTClient | 11:31ebaeb51e1e | 350 | return transport_data->packetId; |
AzureIoTClient | 11:31ebaeb51e1e | 351 | } |
AzureIoTClient | 11:31ebaeb51e1e | 352 | |
AzureIoTClient | 11:31ebaeb51e1e | 353 | static const char* retrieve_mqtt_return_codes(CONNECT_RETURN_CODE rtn_code) |
AzureIoTClient | 11:31ebaeb51e1e | 354 | { |
AzureIoTClient | 11:31ebaeb51e1e | 355 | switch (rtn_code) |
AzureIoTClient | 11:31ebaeb51e1e | 356 | { |
AzureIoTClient | 11:31ebaeb51e1e | 357 | case CONNECTION_ACCEPTED: |
AzureIoTClient | 11:31ebaeb51e1e | 358 | return "Accepted"; |
AzureIoTClient | 11:31ebaeb51e1e | 359 | case CONN_REFUSED_UNACCEPTABLE_VERSION: |
AzureIoTClient | 11:31ebaeb51e1e | 360 | return "Unacceptable Version"; |
AzureIoTClient | 11:31ebaeb51e1e | 361 | case CONN_REFUSED_ID_REJECTED: |
AzureIoTClient | 11:31ebaeb51e1e | 362 | return "Id Rejected"; |
AzureIoTClient | 11:31ebaeb51e1e | 363 | case CONN_REFUSED_SERVER_UNAVAIL: |
AzureIoTClient | 11:31ebaeb51e1e | 364 | return "Server Unavailable"; |
AzureIoTClient | 11:31ebaeb51e1e | 365 | case CONN_REFUSED_BAD_USERNAME_PASSWORD: |
AzureIoTClient | 11:31ebaeb51e1e | 366 | return "Bad Username/Password"; |
AzureIoTClient | 11:31ebaeb51e1e | 367 | case CONN_REFUSED_NOT_AUTHORIZED: |
AzureIoTClient | 11:31ebaeb51e1e | 368 | return "Not Authorized"; |
AzureIoTClient | 11:31ebaeb51e1e | 369 | case CONN_REFUSED_UNKNOWN: |
AzureIoTClient | 11:31ebaeb51e1e | 370 | default: |
AzureIoTClient | 11:31ebaeb51e1e | 371 | return "Unknown"; |
AzureIoTClient | 11:31ebaeb51e1e | 372 | } |
AzureIoTClient | 11:31ebaeb51e1e | 373 | } |
AzureIoTClient | 11:31ebaeb51e1e | 374 | |
AzureIoTClient | 14:4dc2b011be33 | 375 | static int retrieve_device_method_rid_info(const char* resp_topic, STRING_HANDLE method_name, STRING_HANDLE request_id) |
AzureIoTClient | 12:658ca6865de2 | 376 | { |
AzureIoTClient | 12:658ca6865de2 | 377 | int result; |
AzureIoTClient | 12:658ca6865de2 | 378 | STRING_TOKENIZER_HANDLE token_handle = STRING_TOKENIZER_create_from_char(resp_topic); |
AzureIoTClient | 12:658ca6865de2 | 379 | if (token_handle == NULL) |
AzureIoTClient | 12:658ca6865de2 | 380 | { |
AzureIoTClient | 12:658ca6865de2 | 381 | LogError("Failed creating token from device twin topic."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 382 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 383 | } |
AzureIoTClient | 12:658ca6865de2 | 384 | else |
AzureIoTClient | 12:658ca6865de2 | 385 | { |
AzureIoTClient | 12:658ca6865de2 | 386 | STRING_HANDLE token_value; |
AzureIoTClient | 12:658ca6865de2 | 387 | if ((token_value = STRING_new()) == NULL) |
AzureIoTClient | 12:658ca6865de2 | 388 | { |
AzureIoTClient | 12:658ca6865de2 | 389 | LogError("Failed allocating new string ."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 390 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 391 | } |
AzureIoTClient | 12:658ca6865de2 | 392 | else |
AzureIoTClient | 12:658ca6865de2 | 393 | { |
AzureIoTClient | 12:658ca6865de2 | 394 | size_t token_index = 0; |
AzureIoTClient | 12:658ca6865de2 | 395 | size_t request_id_length = strlen(REQUEST_ID_PROPERTY); |
AzureIoTClient | 18:ec8e5e97c6a4 | 396 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 397 | while (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/") == 0) |
AzureIoTClient | 12:658ca6865de2 | 398 | { |
AzureIoTClient | 12:658ca6865de2 | 399 | if (token_index == 3) |
AzureIoTClient | 12:658ca6865de2 | 400 | { |
AzureIoTClient | 12:658ca6865de2 | 401 | if (STRING_concat_with_STRING(method_name, token_value) != 0) |
AzureIoTClient | 12:658ca6865de2 | 402 | { |
AzureIoTClient | 14:4dc2b011be33 | 403 | LogError("Failed STRING_concat_with_STRING."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 404 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 405 | break; |
AzureIoTClient | 12:658ca6865de2 | 406 | } |
AzureIoTClient | 12:658ca6865de2 | 407 | } |
AzureIoTClient | 12:658ca6865de2 | 408 | else if (token_index == 4) |
AzureIoTClient | 12:658ca6865de2 | 409 | { |
AzureIoTClient | 12:658ca6865de2 | 410 | if (STRING_length(token_value) >= request_id_length) |
AzureIoTClient | 12:658ca6865de2 | 411 | { |
AzureIoTClient | 12:658ca6865de2 | 412 | const char* request_id_value = STRING_c_str(token_value); |
AzureIoTClient | 12:658ca6865de2 | 413 | if (memcmp(request_id_value, REQUEST_ID_PROPERTY, request_id_length) == 0) |
AzureIoTClient | 12:658ca6865de2 | 414 | { |
AzureIoTClient | 14:4dc2b011be33 | 415 | if (STRING_concat(request_id, request_id_value+request_id_length) != 0) |
AzureIoTClient | 14:4dc2b011be33 | 416 | { |
AzureIoTClient | 14:4dc2b011be33 | 417 | LogError("Failed STRING_concat failed."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 418 | result = __FAILURE__; |
AzureIoTClient | 14:4dc2b011be33 | 419 | } |
AzureIoTClient | 14:4dc2b011be33 | 420 | else |
AzureIoTClient | 14:4dc2b011be33 | 421 | { |
AzureIoTClient | 14:4dc2b011be33 | 422 | result = 0; |
AzureIoTClient | 14:4dc2b011be33 | 423 | } |
AzureIoTClient | 12:658ca6865de2 | 424 | break; |
AzureIoTClient | 12:658ca6865de2 | 425 | } |
AzureIoTClient | 12:658ca6865de2 | 426 | } |
AzureIoTClient | 12:658ca6865de2 | 427 | } |
AzureIoTClient | 12:658ca6865de2 | 428 | token_index++; |
AzureIoTClient | 12:658ca6865de2 | 429 | } |
AzureIoTClient | 12:658ca6865de2 | 430 | STRING_delete(token_value); |
AzureIoTClient | 12:658ca6865de2 | 431 | } |
AzureIoTClient | 12:658ca6865de2 | 432 | STRING_TOKENIZER_destroy(token_handle); |
AzureIoTClient | 12:658ca6865de2 | 433 | } |
AzureIoTClient | 12:658ca6865de2 | 434 | return result; |
AzureIoTClient | 12:658ca6865de2 | 435 | } |
AzureIoTClient | 12:658ca6865de2 | 436 | |
AzureIoTClient | 12:658ca6865de2 | 437 | static int parse_device_twin_topic_info(const char* resp_topic, bool* patch_msg, size_t* request_id, int* status_code) |
AzureIoTClient | 12:658ca6865de2 | 438 | { |
AzureIoTClient | 12:658ca6865de2 | 439 | int result; |
AzureIoTClient | 12:658ca6865de2 | 440 | STRING_TOKENIZER_HANDLE token_handle = STRING_TOKENIZER_create_from_char(resp_topic); |
AzureIoTClient | 12:658ca6865de2 | 441 | if (token_handle == NULL) |
AzureIoTClient | 12:658ca6865de2 | 442 | { |
AzureIoTClient | 12:658ca6865de2 | 443 | LogError("Failed creating token from device twin topic."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 444 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 445 | *status_code = 0; |
AzureIoTClient | 12:658ca6865de2 | 446 | *request_id = 0; |
AzureIoTClient | 12:658ca6865de2 | 447 | *patch_msg = false; |
AzureIoTClient | 12:658ca6865de2 | 448 | } |
AzureIoTClient | 12:658ca6865de2 | 449 | else |
AzureIoTClient | 12:658ca6865de2 | 450 | { |
AzureIoTClient | 12:658ca6865de2 | 451 | STRING_HANDLE token_value; |
AzureIoTClient | 12:658ca6865de2 | 452 | if ((token_value = STRING_new()) == NULL) |
AzureIoTClient | 12:658ca6865de2 | 453 | { |
AzureIoTClient | 12:658ca6865de2 | 454 | LogError("Failed allocating new string ."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 455 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 456 | *status_code = 0; |
AzureIoTClient | 12:658ca6865de2 | 457 | *request_id = 0; |
AzureIoTClient | 12:658ca6865de2 | 458 | *patch_msg = false; |
AzureIoTClient | 12:658ca6865de2 | 459 | } |
AzureIoTClient | 12:658ca6865de2 | 460 | else |
AzureIoTClient | 12:658ca6865de2 | 461 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 462 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 463 | size_t token_count = 0; |
AzureIoTClient | 12:658ca6865de2 | 464 | while (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/") == 0) |
AzureIoTClient | 12:658ca6865de2 | 465 | { |
AzureIoTClient | 12:658ca6865de2 | 466 | if (token_count == 2) |
AzureIoTClient | 12:658ca6865de2 | 467 | { |
AzureIoTClient | 12:658ca6865de2 | 468 | if (strcmp(STRING_c_str(token_value), "PATCH") == 0) |
AzureIoTClient | 12:658ca6865de2 | 469 | { |
AzureIoTClient | 12:658ca6865de2 | 470 | *patch_msg = true; |
AzureIoTClient | 12:658ca6865de2 | 471 | *status_code = 0; |
AzureIoTClient | 12:658ca6865de2 | 472 | *request_id = 0; |
AzureIoTClient | 12:658ca6865de2 | 473 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 474 | break; |
AzureIoTClient | 12:658ca6865de2 | 475 | } |
AzureIoTClient | 12:658ca6865de2 | 476 | else |
AzureIoTClient | 12:658ca6865de2 | 477 | { |
AzureIoTClient | 12:658ca6865de2 | 478 | *patch_msg = false; |
AzureIoTClient | 12:658ca6865de2 | 479 | } |
AzureIoTClient | 12:658ca6865de2 | 480 | } |
AzureIoTClient | 12:658ca6865de2 | 481 | else if (token_count == 3) |
AzureIoTClient | 12:658ca6865de2 | 482 | { |
AzureIoTClient | 34:9363cf585ef3 | 483 | *status_code = (int)atol(STRING_c_str(token_value)); |
AzureIoTClient | 12:658ca6865de2 | 484 | if (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/?$rid=") == 0) |
AzureIoTClient | 12:658ca6865de2 | 485 | { |
AzureIoTClient | 12:658ca6865de2 | 486 | *request_id = (size_t)atol(STRING_c_str(token_value)); |
AzureIoTClient | 12:658ca6865de2 | 487 | } |
AzureIoTClient | 12:658ca6865de2 | 488 | *patch_msg = false; |
AzureIoTClient | 12:658ca6865de2 | 489 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 490 | break; |
AzureIoTClient | 12:658ca6865de2 | 491 | } |
AzureIoTClient | 12:658ca6865de2 | 492 | token_count++; |
AzureIoTClient | 12:658ca6865de2 | 493 | } |
AzureIoTClient | 12:658ca6865de2 | 494 | STRING_delete(token_value); |
AzureIoTClient | 12:658ca6865de2 | 495 | } |
AzureIoTClient | 12:658ca6865de2 | 496 | STRING_TOKENIZER_destroy(token_handle); |
AzureIoTClient | 12:658ca6865de2 | 497 | } |
AzureIoTClient | 12:658ca6865de2 | 498 | return result; |
AzureIoTClient | 12:658ca6865de2 | 499 | } |
AzureIoTClient | 12:658ca6865de2 | 500 | |
AzureIoTClient | 12:658ca6865de2 | 501 | static IOTHUB_IDENTITY_TYPE retrieve_topic_type(const char* topic_resp) |
AzureIoTClient | 12:658ca6865de2 | 502 | { |
AzureIoTClient | 12:658ca6865de2 | 503 | IOTHUB_IDENTITY_TYPE type; |
AzureIoTClient | 12:658ca6865de2 | 504 | size_t dev_twin_topic_len = sizeof(TOPIC_DEVICE_TWIN_PREFIX)-1; |
AzureIoTClient | 12:658ca6865de2 | 505 | size_t dev_method_topic_len = sizeof(TOPIC_DEVICE_METHOD_PREFIX)-1; |
AzureIoTClient | 12:658ca6865de2 | 506 | size_t topic_len = strlen(topic_resp); |
AzureIoTClient | 12:658ca6865de2 | 507 | size_t len_to_use = (dev_twin_topic_len > dev_method_topic_len) ? dev_twin_topic_len : dev_method_topic_len; |
AzureIoTClient | 12:658ca6865de2 | 508 | |
AzureIoTClient | 12:658ca6865de2 | 509 | bool search_device_twin = true; |
AzureIoTClient | 12:658ca6865de2 | 510 | bool search_device_method = true; |
AzureIoTClient | 12:658ca6865de2 | 511 | if (topic_len < len_to_use) |
AzureIoTClient | 12:658ca6865de2 | 512 | { |
AzureIoTClient | 12:658ca6865de2 | 513 | search_device_twin = false; |
AzureIoTClient | 12:658ca6865de2 | 514 | search_device_method = false; |
AzureIoTClient | 12:658ca6865de2 | 515 | } |
AzureIoTClient | 12:658ca6865de2 | 516 | else |
AzureIoTClient | 12:658ca6865de2 | 517 | { |
AzureIoTClient | 16:14640ee83e99 | 518 | size_t index = 0; |
AzureIoTClient | 16:14640ee83e99 | 519 | for (index = 0; index < len_to_use && (search_device_twin || search_device_method); index++) |
AzureIoTClient | 12:658ca6865de2 | 520 | { |
AzureIoTClient | 12:658ca6865de2 | 521 | if (search_device_twin) |
AzureIoTClient | 12:658ca6865de2 | 522 | { |
AzureIoTClient | 12:658ca6865de2 | 523 | if (index > dev_twin_topic_len) |
AzureIoTClient | 12:658ca6865de2 | 524 | { |
AzureIoTClient | 12:658ca6865de2 | 525 | search_device_twin = false; |
AzureIoTClient | 12:658ca6865de2 | 526 | } |
AzureIoTClient | 12:658ca6865de2 | 527 | else |
AzureIoTClient | 12:658ca6865de2 | 528 | { |
AzureIoTClient | 12:658ca6865de2 | 529 | if (index == dev_twin_topic_len) |
AzureIoTClient | 12:658ca6865de2 | 530 | { |
AzureIoTClient | 12:658ca6865de2 | 531 | break; |
AzureIoTClient | 12:658ca6865de2 | 532 | } |
AzureIoTClient | 14:4dc2b011be33 | 533 | else if (TOUPPER(TOPIC_DEVICE_TWIN_PREFIX[index]) != TOUPPER(topic_resp[index])) |
AzureIoTClient | 12:658ca6865de2 | 534 | { |
AzureIoTClient | 12:658ca6865de2 | 535 | search_device_twin = false; |
AzureIoTClient | 12:658ca6865de2 | 536 | } |
AzureIoTClient | 12:658ca6865de2 | 537 | } |
AzureIoTClient | 12:658ca6865de2 | 538 | } |
AzureIoTClient | 12:658ca6865de2 | 539 | if (search_device_method) |
AzureIoTClient | 12:658ca6865de2 | 540 | { |
AzureIoTClient | 12:658ca6865de2 | 541 | if (index > dev_method_topic_len) |
AzureIoTClient | 12:658ca6865de2 | 542 | { |
AzureIoTClient | 12:658ca6865de2 | 543 | search_device_method = false; |
AzureIoTClient | 12:658ca6865de2 | 544 | } |
AzureIoTClient | 12:658ca6865de2 | 545 | else |
AzureIoTClient | 12:658ca6865de2 | 546 | { |
AzureIoTClient | 12:658ca6865de2 | 547 | if (index == dev_method_topic_len) |
AzureIoTClient | 12:658ca6865de2 | 548 | { |
AzureIoTClient | 12:658ca6865de2 | 549 | break; |
AzureIoTClient | 12:658ca6865de2 | 550 | } |
AzureIoTClient | 14:4dc2b011be33 | 551 | else if (TOUPPER(TOPIC_DEVICE_METHOD_PREFIX[index]) != TOUPPER(topic_resp[index])) |
AzureIoTClient | 12:658ca6865de2 | 552 | { |
AzureIoTClient | 12:658ca6865de2 | 553 | search_device_method = false; |
AzureIoTClient | 12:658ca6865de2 | 554 | } |
AzureIoTClient | 12:658ca6865de2 | 555 | } |
AzureIoTClient | 12:658ca6865de2 | 556 | } |
AzureIoTClient | 12:658ca6865de2 | 557 | } |
AzureIoTClient | 12:658ca6865de2 | 558 | } |
AzureIoTClient | 12:658ca6865de2 | 559 | if (search_device_twin) |
AzureIoTClient | 12:658ca6865de2 | 560 | { |
AzureIoTClient | 12:658ca6865de2 | 561 | type = IOTHUB_TYPE_DEVICE_TWIN; |
AzureIoTClient | 12:658ca6865de2 | 562 | } |
AzureIoTClient | 12:658ca6865de2 | 563 | else if (search_device_method) |
AzureIoTClient | 12:658ca6865de2 | 564 | { |
AzureIoTClient | 12:658ca6865de2 | 565 | type = IOTHUB_TYPE_DEVICE_METHODS; |
AzureIoTClient | 12:658ca6865de2 | 566 | } |
AzureIoTClient | 12:658ca6865de2 | 567 | else |
AzureIoTClient | 12:658ca6865de2 | 568 | { |
AzureIoTClient | 12:658ca6865de2 | 569 | type = IOTHUB_TYPE_TELEMETRY; |
AzureIoTClient | 12:658ca6865de2 | 570 | } |
AzureIoTClient | 12:658ca6865de2 | 571 | return type; |
AzureIoTClient | 12:658ca6865de2 | 572 | } |
AzureIoTClient | 12:658ca6865de2 | 573 | |
AzureIoTClient | 11:31ebaeb51e1e | 574 | static void sendMsgComplete(IOTHUB_MESSAGE_LIST* iothubMsgList, PMQTTTRANSPORT_HANDLE_DATA transport_data, IOTHUB_CLIENT_CONFIRMATION_RESULT confirmResult) |
AzureIoTClient | 11:31ebaeb51e1e | 575 | { |
AzureIoTClient | 11:31ebaeb51e1e | 576 | DLIST_ENTRY messageCompleted; |
AzureIoTClient | 11:31ebaeb51e1e | 577 | DList_InitializeListHead(&messageCompleted); |
AzureIoTClient | 11:31ebaeb51e1e | 578 | DList_InsertTailList(&messageCompleted, &(iothubMsgList->entry)); |
AzureIoTClient | 39:6231984e0179 | 579 | IoTHubClientCore_LL_SendComplete(transport_data->llClientHandle, &messageCompleted, confirmResult); |
AzureIoTClient | 11:31ebaeb51e1e | 580 | } |
AzureIoTClient | 11:31ebaeb51e1e | 581 | |
AzureIoTClient | 37:e6a799428f3d | 582 | static int addUserPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, STRING_HANDLE topic_string, size_t* index_ptr, bool urlencode) |
AzureIoTClient | 11:31ebaeb51e1e | 583 | { |
AzureIoTClient | 37:e6a799428f3d | 584 | int result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 585 | const char* const* propertyKeys; |
AzureIoTClient | 11:31ebaeb51e1e | 586 | const char* const* propertyValues; |
AzureIoTClient | 11:31ebaeb51e1e | 587 | size_t propertyCount; |
AzureIoTClient | 37:e6a799428f3d | 588 | size_t index = *index_ptr; |
AzureIoTClient | 11:31ebaeb51e1e | 589 | MAP_HANDLE properties_map = IoTHubMessage_Properties(iothub_message_handle); |
AzureIoTClient | 11:31ebaeb51e1e | 590 | if (properties_map != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 591 | { |
AzureIoTClient | 11:31ebaeb51e1e | 592 | if (Map_GetInternals(properties_map, &propertyKeys, &propertyValues, &propertyCount) != MAP_OK) |
AzureIoTClient | 11:31ebaeb51e1e | 593 | { |
AzureIoTClient | 11:31ebaeb51e1e | 594 | LogError("Failed to get the internals of the property map."); |
AzureIoTClient | 37:e6a799428f3d | 595 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 596 | } |
AzureIoTClient | 11:31ebaeb51e1e | 597 | else |
AzureIoTClient | 11:31ebaeb51e1e | 598 | { |
AzureIoTClient | 11:31ebaeb51e1e | 599 | if (propertyCount != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 600 | { |
AzureIoTClient | 37:e6a799428f3d | 601 | for (index = 0; index < propertyCount && result == 0; index++) |
AzureIoTClient | 11:31ebaeb51e1e | 602 | { |
AzureIoTClient | 37:e6a799428f3d | 603 | if (urlencode) |
AzureIoTClient | 11:31ebaeb51e1e | 604 | { |
AzureIoTClient | 37:e6a799428f3d | 605 | STRING_HANDLE property_key = URL_EncodeString(propertyKeys[index]); |
AzureIoTClient | 37:e6a799428f3d | 606 | STRING_HANDLE property_value = URL_EncodeString(propertyValues[index]); |
AzureIoTClient | 37:e6a799428f3d | 607 | if ((property_key == NULL) || (property_value == NULL)) |
AzureIoTClient | 37:e6a799428f3d | 608 | { |
AzureIoTClient | 37:e6a799428f3d | 609 | LogError("Failed URL Encoding properties"); |
AzureIoTClient | 37:e6a799428f3d | 610 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 611 | } |
AzureIoTClient | 37:e6a799428f3d | 612 | else if (STRING_sprintf(topic_string, "%s=%s%s", STRING_c_str(property_key), STRING_c_str(property_value), propertyCount - 1 == index ? "" : PROPERTY_SEPARATOR) != 0) |
AzureIoTClient | 37:e6a799428f3d | 613 | { |
AzureIoTClient | 37:e6a799428f3d | 614 | LogError("Failed constructing property string."); |
AzureIoTClient | 37:e6a799428f3d | 615 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 616 | } |
AzureIoTClient | 37:e6a799428f3d | 617 | STRING_delete(property_key); |
AzureIoTClient | 37:e6a799428f3d | 618 | STRING_delete(property_value); |
AzureIoTClient | 37:e6a799428f3d | 619 | } |
AzureIoTClient | 37:e6a799428f3d | 620 | else |
AzureIoTClient | 37:e6a799428f3d | 621 | { |
AzureIoTClient | 37:e6a799428f3d | 622 | if (STRING_sprintf(topic_string, "%s=%s%s", propertyKeys[index], propertyValues[index], propertyCount - 1 == index ? "" : PROPERTY_SEPARATOR) != 0) |
AzureIoTClient | 37:e6a799428f3d | 623 | { |
AzureIoTClient | 37:e6a799428f3d | 624 | LogError("Failed constructing property string."); |
AzureIoTClient | 37:e6a799428f3d | 625 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 626 | } |
AzureIoTClient | 11:31ebaeb51e1e | 627 | } |
AzureIoTClient | 11:31ebaeb51e1e | 628 | } |
AzureIoTClient | 11:31ebaeb51e1e | 629 | } |
AzureIoTClient | 11:31ebaeb51e1e | 630 | } |
AzureIoTClient | 11:31ebaeb51e1e | 631 | } |
AzureIoTClient | 37:e6a799428f3d | 632 | *index_ptr = index; |
AzureIoTClient | 37:e6a799428f3d | 633 | return result; |
AzureIoTClient | 37:e6a799428f3d | 634 | } |
AzureIoTClient | 37:e6a799428f3d | 635 | |
AzureIoTClient | 37:e6a799428f3d | 636 | static int addSystemPropertyToTopicString(STRING_HANDLE topic_string, size_t index, const char* property_key, const char* property_value, bool urlencode) |
AzureIoTClient | 37:e6a799428f3d | 637 | { |
AzureIoTClient | 37:e6a799428f3d | 638 | int result = 0; |
AzureIoTClient | 37:e6a799428f3d | 639 | |
AzureIoTClient | 37:e6a799428f3d | 640 | if (urlencode) |
AzureIoTClient | 37:e6a799428f3d | 641 | { |
AzureIoTClient | 37:e6a799428f3d | 642 | STRING_HANDLE encoded_property_value = URL_EncodeString(property_value); |
AzureIoTClient | 37:e6a799428f3d | 643 | if (encoded_property_value == NULL) |
AzureIoTClient | 37:e6a799428f3d | 644 | { |
AzureIoTClient | 37:e6a799428f3d | 645 | LogError("Failed URL encoding %s.", property_key); |
AzureIoTClient | 37:e6a799428f3d | 646 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 647 | } |
AzureIoTClient | 37:e6a799428f3d | 648 | else if (STRING_sprintf(topic_string, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, property_key, STRING_c_str(encoded_property_value)) != 0) |
AzureIoTClient | 37:e6a799428f3d | 649 | { |
AzureIoTClient | 37:e6a799428f3d | 650 | LogError("Failed setting %s.", property_key); |
AzureIoTClient | 37:e6a799428f3d | 651 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 652 | } |
AzureIoTClient | 37:e6a799428f3d | 653 | STRING_delete(encoded_property_value); |
AzureIoTClient | 37:e6a799428f3d | 654 | } |
AzureIoTClient | 37:e6a799428f3d | 655 | else |
AzureIoTClient | 37:e6a799428f3d | 656 | { |
AzureIoTClient | 37:e6a799428f3d | 657 | if (STRING_sprintf(topic_string, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, property_key, property_value) != 0) |
AzureIoTClient | 37:e6a799428f3d | 658 | { |
AzureIoTClient | 37:e6a799428f3d | 659 | LogError("Failed setting %s.", property_key); |
AzureIoTClient | 37:e6a799428f3d | 660 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 661 | } |
AzureIoTClient | 37:e6a799428f3d | 662 | } |
AzureIoTClient | 37:e6a799428f3d | 663 | return result; |
AzureIoTClient | 37:e6a799428f3d | 664 | } |
AzureIoTClient | 37:e6a799428f3d | 665 | |
AzureIoTClient | 37:e6a799428f3d | 666 | static int addSystemPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, STRING_HANDLE topic_string, size_t* index_ptr, bool urlencode) |
AzureIoTClient | 37:e6a799428f3d | 667 | { |
AzureIoTClient | 37:e6a799428f3d | 668 | (void)urlencode; |
AzureIoTClient | 37:e6a799428f3d | 669 | int result = 0; |
AzureIoTClient | 37:e6a799428f3d | 670 | size_t index = *index_ptr; |
AzureIoTClient | 23:84f4c36da8c1 | 671 | |
AzureIoTClient | 23:84f4c36da8c1 | 672 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_052: [ IoTHubTransport_MQTT_Common_DoWork shall check for the CorrelationId property and if found add the value as a system property in the format of $.cid=<id> ] */ |
AzureIoTClient | 37:e6a799428f3d | 673 | const char* correlation_id = IoTHubMessage_GetCorrelationId(iothub_message_handle); |
AzureIoTClient | 37:e6a799428f3d | 674 | if (correlation_id != NULL) |
AzureIoTClient | 23:84f4c36da8c1 | 675 | { |
AzureIoTClient | 37:e6a799428f3d | 676 | result = addSystemPropertyToTopicString(topic_string, index, CORRELATION_ID_PROPERTY, correlation_id, urlencode); |
AzureIoTClient | 37:e6a799428f3d | 677 | index++; |
AzureIoTClient | 23:84f4c36da8c1 | 678 | } |
AzureIoTClient | 23:84f4c36da8c1 | 679 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_053: [ IoTHubTransport_MQTT_Common_DoWork shall check for the MessageId property and if found add the value as a system property in the format of $.mid=<id> ] */ |
AzureIoTClient | 37:e6a799428f3d | 680 | if (result == 0) |
AzureIoTClient | 23:84f4c36da8c1 | 681 | { |
AzureIoTClient | 23:84f4c36da8c1 | 682 | const char* msg_id = IoTHubMessage_GetMessageId(iothub_message_handle); |
AzureIoTClient | 23:84f4c36da8c1 | 683 | if (msg_id != NULL) |
AzureIoTClient | 23:84f4c36da8c1 | 684 | { |
AzureIoTClient | 37:e6a799428f3d | 685 | result = addSystemPropertyToTopicString(topic_string, index, MESSAGE_ID_PROPERTY, msg_id, urlencode); |
AzureIoTClient | 27:04de3c0bf1db | 686 | index++; |
AzureIoTClient | 27:04de3c0bf1db | 687 | } |
AzureIoTClient | 27:04de3c0bf1db | 688 | } |
AzureIoTClient | 27:04de3c0bf1db | 689 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_010: [ `IoTHubTransport_MQTT_Common_DoWork` shall check for the ContentType property and if found add the `value` as a system property in the format of `$.ct=<value>` ] |
AzureIoTClient | 37:e6a799428f3d | 690 | if (result == 0) |
AzureIoTClient | 27:04de3c0bf1db | 691 | { |
AzureIoTClient | 27:04de3c0bf1db | 692 | const char* content_type = IoTHubMessage_GetContentTypeSystemProperty(iothub_message_handle); |
AzureIoTClient | 27:04de3c0bf1db | 693 | if (content_type != NULL) |
AzureIoTClient | 27:04de3c0bf1db | 694 | { |
AzureIoTClient | 37:e6a799428f3d | 695 | result = addSystemPropertyToTopicString(topic_string, index, CONTENT_TYPE_PROPERTY, content_type, urlencode); |
AzureIoTClient | 27:04de3c0bf1db | 696 | index++; |
AzureIoTClient | 23:84f4c36da8c1 | 697 | } |
AzureIoTClient | 23:84f4c36da8c1 | 698 | } |
AzureIoTClient | 27:04de3c0bf1db | 699 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_011: [ `IoTHubTransport_MQTT_Common_DoWork` shall check for the ContentEncoding property and if found add the `value` as a system property in the format of `$.ce=<value>` ] |
AzureIoTClient | 37:e6a799428f3d | 700 | if (result == 0) |
AzureIoTClient | 27:04de3c0bf1db | 701 | { |
AzureIoTClient | 27:04de3c0bf1db | 702 | const char* content_encoding = IoTHubMessage_GetContentEncodingSystemProperty(iothub_message_handle); |
AzureIoTClient | 27:04de3c0bf1db | 703 | if (content_encoding != NULL) |
AzureIoTClient | 27:04de3c0bf1db | 704 | { |
AzureIoTClient | 37:e6a799428f3d | 705 | result = addSystemPropertyToTopicString(topic_string, index, CONTENT_ENCODING_PROPERTY, content_encoding, urlencode); |
AzureIoTClient | 27:04de3c0bf1db | 706 | index++; |
AzureIoTClient | 27:04de3c0bf1db | 707 | } |
AzureIoTClient | 27:04de3c0bf1db | 708 | } |
AzureIoTClient | 37:e6a799428f3d | 709 | *index_ptr = index; |
AzureIoTClient | 37:e6a799428f3d | 710 | return result; |
AzureIoTClient | 37:e6a799428f3d | 711 | } |
AzureIoTClient | 37:e6a799428f3d | 712 | |
AzureIoTClient | 37:e6a799428f3d | 713 | static int addDiagnosticPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, STRING_HANDLE topic_string, size_t* index_ptr) |
AzureIoTClient | 37:e6a799428f3d | 714 | { |
AzureIoTClient | 37:e6a799428f3d | 715 | int result = 0; |
AzureIoTClient | 37:e6a799428f3d | 716 | size_t index = *index_ptr; |
AzureIoTClient | 27:04de3c0bf1db | 717 | |
AzureIoTClient | 30:52ff609606c8 | 718 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_014: [ `IoTHubTransport_MQTT_Common_DoWork` shall check for the diagnostic properties including diagid and diagCreationTimeUtc and if found both add them as system property in the format of `$.diagid` and `$.diagctx` respectively] |
AzureIoTClient | 37:e6a799428f3d | 719 | const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* diagnosticData = IoTHubMessage_GetDiagnosticPropertyData(iothub_message_handle); |
AzureIoTClient | 37:e6a799428f3d | 720 | if (diagnosticData != NULL) |
AzureIoTClient | 30:52ff609606c8 | 721 | { |
AzureIoTClient | 37:e6a799428f3d | 722 | const char* diag_id = diagnosticData->diagnosticId; |
AzureIoTClient | 37:e6a799428f3d | 723 | const char* creation_time_utc = diagnosticData->diagnosticCreationTimeUtc; |
AzureIoTClient | 37:e6a799428f3d | 724 | //diagid and creationtimeutc must be present/unpresent simultaneously |
AzureIoTClient | 37:e6a799428f3d | 725 | if (diag_id != NULL && creation_time_utc != NULL) |
AzureIoTClient | 30:52ff609606c8 | 726 | { |
AzureIoTClient | 37:e6a799428f3d | 727 | if (STRING_sprintf(topic_string, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, DIAGNOSTIC_ID_PROPERTY, diag_id) != 0) |
AzureIoTClient | 30:52ff609606c8 | 728 | { |
AzureIoTClient | 37:e6a799428f3d | 729 | LogError("Failed setting diagnostic id"); |
AzureIoTClient | 37:e6a799428f3d | 730 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 731 | } |
AzureIoTClient | 37:e6a799428f3d | 732 | index++; |
AzureIoTClient | 37:e6a799428f3d | 733 | |
AzureIoTClient | 37:e6a799428f3d | 734 | if (result == 0) |
AzureIoTClient | 37:e6a799428f3d | 735 | { |
AzureIoTClient | 37:e6a799428f3d | 736 | //construct diagnostic context, it should be urlencode(key1=value1,key2=value2) |
AzureIoTClient | 37:e6a799428f3d | 737 | STRING_HANDLE diagContextHandle = STRING_construct_sprintf("%s=%s", DIAGNOSTIC_CONTEXT_CREATION_TIME_UTC_PROPERTY, creation_time_utc); |
AzureIoTClient | 37:e6a799428f3d | 738 | if (diagContextHandle == NULL) |
AzureIoTClient | 30:52ff609606c8 | 739 | { |
AzureIoTClient | 37:e6a799428f3d | 740 | LogError("Failed constructing diagnostic context"); |
AzureIoTClient | 37:e6a799428f3d | 741 | result = __FAILURE__; |
AzureIoTClient | 30:52ff609606c8 | 742 | } |
AzureIoTClient | 37:e6a799428f3d | 743 | else |
AzureIoTClient | 30:52ff609606c8 | 744 | { |
AzureIoTClient | 37:e6a799428f3d | 745 | //Add other diagnostic context properties here if have more |
AzureIoTClient | 37:e6a799428f3d | 746 | STRING_HANDLE encodedContextValueHandle = URL_Encode(diagContextHandle); |
AzureIoTClient | 37:e6a799428f3d | 747 | const char* encodedContextValueString = NULL; |
AzureIoTClient | 37:e6a799428f3d | 748 | if (encodedContextValueHandle != NULL && |
AzureIoTClient | 37:e6a799428f3d | 749 | (encodedContextValueString = STRING_c_str(encodedContextValueHandle)) != NULL) |
AzureIoTClient | 30:52ff609606c8 | 750 | { |
AzureIoTClient | 37:e6a799428f3d | 751 | if (STRING_sprintf(topic_string, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, DIAGNOSTIC_CONTEXT_PROPERTY, encodedContextValueString) != 0) |
AzureIoTClient | 37:e6a799428f3d | 752 | { |
AzureIoTClient | 37:e6a799428f3d | 753 | LogError("Failed setting diagnostic context"); |
AzureIoTClient | 37:e6a799428f3d | 754 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 755 | } |
AzureIoTClient | 37:e6a799428f3d | 756 | STRING_delete(encodedContextValueHandle); |
AzureIoTClient | 37:e6a799428f3d | 757 | encodedContextValueHandle = NULL; |
AzureIoTClient | 30:52ff609606c8 | 758 | } |
AzureIoTClient | 30:52ff609606c8 | 759 | else |
AzureIoTClient | 30:52ff609606c8 | 760 | { |
AzureIoTClient | 37:e6a799428f3d | 761 | LogError("Failed encoding diagnostic context value"); |
AzureIoTClient | 37:e6a799428f3d | 762 | result = __FAILURE__; |
AzureIoTClient | 30:52ff609606c8 | 763 | } |
AzureIoTClient | 37:e6a799428f3d | 764 | STRING_delete(diagContextHandle); |
AzureIoTClient | 37:e6a799428f3d | 765 | diagContextHandle = NULL; |
AzureIoTClient | 37:e6a799428f3d | 766 | index++; |
AzureIoTClient | 30:52ff609606c8 | 767 | } |
AzureIoTClient | 30:52ff609606c8 | 768 | } |
AzureIoTClient | 37:e6a799428f3d | 769 | } |
AzureIoTClient | 37:e6a799428f3d | 770 | else if (diag_id != NULL || creation_time_utc != NULL) |
AzureIoTClient | 37:e6a799428f3d | 771 | { |
AzureIoTClient | 37:e6a799428f3d | 772 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_015: [ `IoTHubTransport_MQTT_Common_DoWork` shall check whether diagid and diagCreationTimeUtc be present simultaneously, treat as error if not] |
AzureIoTClient | 37:e6a799428f3d | 773 | LogError("diagid and diagcreationtimeutc must be present simultaneously."); |
AzureIoTClient | 37:e6a799428f3d | 774 | result = __FAILURE__; |
AzureIoTClient | 30:52ff609606c8 | 775 | } |
AzureIoTClient | 30:52ff609606c8 | 776 | } |
AzureIoTClient | 37:e6a799428f3d | 777 | return result; |
AzureIoTClient | 37:e6a799428f3d | 778 | } |
AzureIoTClient | 37:e6a799428f3d | 779 | |
AzureIoTClient | 37:e6a799428f3d | 780 | |
AzureIoTClient | 37:e6a799428f3d | 781 | static STRING_HANDLE addPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, const char* eventTopic, bool urlencode) |
AzureIoTClient | 37:e6a799428f3d | 782 | { |
AzureIoTClient | 37:e6a799428f3d | 783 | size_t index = 0; |
AzureIoTClient | 37:e6a799428f3d | 784 | STRING_HANDLE result = STRING_construct(eventTopic); |
AzureIoTClient | 37:e6a799428f3d | 785 | if (result == NULL) |
AzureIoTClient | 37:e6a799428f3d | 786 | { |
AzureIoTClient | 37:e6a799428f3d | 787 | LogError("Failed to create event topic string handle"); |
AzureIoTClient | 37:e6a799428f3d | 788 | } |
AzureIoTClient | 37:e6a799428f3d | 789 | else if (addUserPropertiesTouMqttMessage(iothub_message_handle, result, &index, urlencode) != 0) |
AzureIoTClient | 37:e6a799428f3d | 790 | { |
AzureIoTClient | 37:e6a799428f3d | 791 | LogError("Failed adding Properties to uMQTT Message"); |
AzureIoTClient | 37:e6a799428f3d | 792 | STRING_delete(result); |
AzureIoTClient | 37:e6a799428f3d | 793 | result = NULL; |
AzureIoTClient | 37:e6a799428f3d | 794 | } |
AzureIoTClient | 37:e6a799428f3d | 795 | else if (addSystemPropertiesTouMqttMessage(iothub_message_handle, result, &index, urlencode) != 0) |
AzureIoTClient | 37:e6a799428f3d | 796 | { |
AzureIoTClient | 37:e6a799428f3d | 797 | LogError("Failed adding System Properties to uMQTT Message"); |
AzureIoTClient | 37:e6a799428f3d | 798 | STRING_delete(result); |
AzureIoTClient | 37:e6a799428f3d | 799 | result = NULL; |
AzureIoTClient | 37:e6a799428f3d | 800 | } |
AzureIoTClient | 37:e6a799428f3d | 801 | else if (addDiagnosticPropertiesTouMqttMessage(iothub_message_handle, result, &index) != 0) |
AzureIoTClient | 37:e6a799428f3d | 802 | { |
AzureIoTClient | 37:e6a799428f3d | 803 | LogError("Failed adding Diagnostic Properties to uMQTT Message"); |
AzureIoTClient | 37:e6a799428f3d | 804 | STRING_delete(result); |
AzureIoTClient | 37:e6a799428f3d | 805 | result = NULL; |
AzureIoTClient | 37:e6a799428f3d | 806 | } |
AzureIoTClient | 30:52ff609606c8 | 807 | |
AzureIoTClient | 11:31ebaeb51e1e | 808 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 809 | } |
AzureIoTClient | 11:31ebaeb51e1e | 810 | |
AzureIoTClient | 11:31ebaeb51e1e | 811 | static int publish_mqtt_telemetry_msg(PMQTTTRANSPORT_HANDLE_DATA transport_data, MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry, const unsigned char* payload, size_t len) |
AzureIoTClient | 11:31ebaeb51e1e | 812 | { |
AzureIoTClient | 11:31ebaeb51e1e | 813 | int result; |
AzureIoTClient | 37:e6a799428f3d | 814 | STRING_HANDLE msgTopic = addPropertiesTouMqttMessage(mqttMsgEntry->iotHubMessageEntry->messageHandle, STRING_c_str(transport_data->topic_MqttEvent), transport_data->auto_url_encode_decode); |
AzureIoTClient | 11:31ebaeb51e1e | 815 | if (msgTopic == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 816 | { |
AzureIoTClient | 27:04de3c0bf1db | 817 | LogError("Failed adding properties to mqtt message"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 818 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 819 | } |
AzureIoTClient | 11:31ebaeb51e1e | 820 | else |
AzureIoTClient | 11:31ebaeb51e1e | 821 | { |
AzureIoTClient | 11:31ebaeb51e1e | 822 | MQTT_MESSAGE_HANDLE mqttMsg = mqttmessage_create(mqttMsgEntry->packet_id, STRING_c_str(msgTopic), DELIVER_AT_LEAST_ONCE, payload, len); |
AzureIoTClient | 11:31ebaeb51e1e | 823 | if (mqttMsg == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 824 | { |
AzureIoTClient | 27:04de3c0bf1db | 825 | LogError("Failed creating mqtt message"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 826 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 827 | } |
AzureIoTClient | 11:31ebaeb51e1e | 828 | else |
AzureIoTClient | 11:31ebaeb51e1e | 829 | { |
Azure.IoT.Build | 13:606465879c57 | 830 | if (tickcounter_get_current_ms(transport_data->msgTickCounter, &mqttMsgEntry->msgPublishTime) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 831 | { |
AzureIoTClient | 11:31ebaeb51e1e | 832 | LogError("Failed retrieving tickcounter info"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 833 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 834 | } |
AzureIoTClient | 11:31ebaeb51e1e | 835 | else |
AzureIoTClient | 11:31ebaeb51e1e | 836 | { |
AzureIoTClient | 11:31ebaeb51e1e | 837 | if (mqtt_client_publish(transport_data->mqttClient, mqttMsg) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 838 | { |
AzureIoTClient | 27:04de3c0bf1db | 839 | LogError("Failed attempting to publish mqtt message"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 840 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 841 | } |
AzureIoTClient | 11:31ebaeb51e1e | 842 | else |
AzureIoTClient | 11:31ebaeb51e1e | 843 | { |
AzureIoTClient | 11:31ebaeb51e1e | 844 | mqttMsgEntry->retryCount++; |
AzureIoTClient | 11:31ebaeb51e1e | 845 | result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 846 | } |
AzureIoTClient | 11:31ebaeb51e1e | 847 | } |
AzureIoTClient | 11:31ebaeb51e1e | 848 | mqttmessage_destroy(mqttMsg); |
AzureIoTClient | 11:31ebaeb51e1e | 849 | } |
AzureIoTClient | 11:31ebaeb51e1e | 850 | STRING_delete(msgTopic); |
AzureIoTClient | 11:31ebaeb51e1e | 851 | } |
AzureIoTClient | 11:31ebaeb51e1e | 852 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 853 | } |
AzureIoTClient | 11:31ebaeb51e1e | 854 | |
AzureIoTClient | 14:4dc2b011be33 | 855 | static int publish_device_method_message(MQTTTRANSPORT_HANDLE_DATA* transport_data, int status_code, STRING_HANDLE request_id, const unsigned char* response, size_t response_size) |
AzureIoTClient | 12:658ca6865de2 | 856 | { |
AzureIoTClient | 12:658ca6865de2 | 857 | int result; |
AzureIoTClient | 12:658ca6865de2 | 858 | uint16_t packet_id = get_next_packet_id(transport_data); |
AzureIoTClient | 12:658ca6865de2 | 859 | |
AzureIoTClient | 14:4dc2b011be33 | 860 | STRING_HANDLE msg_topic = STRING_construct_sprintf(DEVICE_METHOD_RESPONSE_TOPIC, status_code, STRING_c_str(request_id) ); |
AzureIoTClient | 12:658ca6865de2 | 861 | if (msg_topic == NULL) |
AzureIoTClient | 12:658ca6865de2 | 862 | { |
AzureIoTClient | 12:658ca6865de2 | 863 | LogError("Failed constructing message topic."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 864 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 865 | } |
AzureIoTClient | 12:658ca6865de2 | 866 | else |
AzureIoTClient | 12:658ca6865de2 | 867 | { |
AzureIoTClient | 14:4dc2b011be33 | 868 | MQTT_MESSAGE_HANDLE mqtt_get_msg = mqttmessage_create(packet_id, STRING_c_str(msg_topic), DELIVER_AT_MOST_ONCE, response, response_size); |
AzureIoTClient | 12:658ca6865de2 | 869 | if (mqtt_get_msg == NULL) |
AzureIoTClient | 12:658ca6865de2 | 870 | { |
AzureIoTClient | 12:658ca6865de2 | 871 | LogError("Failed constructing mqtt message."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 872 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 873 | } |
AzureIoTClient | 12:658ca6865de2 | 874 | else |
AzureIoTClient | 12:658ca6865de2 | 875 | { |
AzureIoTClient | 12:658ca6865de2 | 876 | if (mqtt_client_publish(transport_data->mqttClient, mqtt_get_msg) != 0) |
AzureIoTClient | 12:658ca6865de2 | 877 | { |
AzureIoTClient | 12:658ca6865de2 | 878 | LogError("Failed publishing to mqtt client."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 879 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 880 | } |
AzureIoTClient | 12:658ca6865de2 | 881 | else |
AzureIoTClient | 12:658ca6865de2 | 882 | { |
AzureIoTClient | 12:658ca6865de2 | 883 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 884 | } |
AzureIoTClient | 12:658ca6865de2 | 885 | mqttmessage_destroy(mqtt_get_msg); |
AzureIoTClient | 12:658ca6865de2 | 886 | } |
AzureIoTClient | 12:658ca6865de2 | 887 | STRING_delete(msg_topic); |
AzureIoTClient | 12:658ca6865de2 | 888 | } |
AzureIoTClient | 12:658ca6865de2 | 889 | return result; |
AzureIoTClient | 12:658ca6865de2 | 890 | } |
AzureIoTClient | 12:658ca6865de2 | 891 | |
AzureIoTClient | 12:658ca6865de2 | 892 | static int publish_device_twin_get_message(MQTTTRANSPORT_HANDLE_DATA* transport_data) |
AzureIoTClient | 12:658ca6865de2 | 893 | { |
AzureIoTClient | 12:658ca6865de2 | 894 | int result; |
AzureIoTClient | 12:658ca6865de2 | 895 | MQTT_DEVICE_TWIN_ITEM* mqtt_info = (MQTT_DEVICE_TWIN_ITEM*)malloc(sizeof(MQTT_DEVICE_TWIN_ITEM)); |
AzureIoTClient | 12:658ca6865de2 | 896 | if (mqtt_info == NULL) |
AzureIoTClient | 12:658ca6865de2 | 897 | { |
AzureIoTClient | 12:658ca6865de2 | 898 | LogError("Failed allocating device twin data."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 899 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 900 | } |
AzureIoTClient | 12:658ca6865de2 | 901 | else |
AzureIoTClient | 12:658ca6865de2 | 902 | { |
AzureIoTClient | 12:658ca6865de2 | 903 | mqtt_info->packet_id = get_next_packet_id(transport_data); |
AzureIoTClient | 12:658ca6865de2 | 904 | mqtt_info->iothub_msg_id = 0; |
AzureIoTClient | 12:658ca6865de2 | 905 | mqtt_info->device_twin_msg_type = RETRIEVE_PROPERTIES; |
AzureIoTClient | 12:658ca6865de2 | 906 | mqtt_info->retryCount = 0; |
AzureIoTClient | 12:658ca6865de2 | 907 | mqtt_info->msgPublishTime = 0; |
AzureIoTClient | 12:658ca6865de2 | 908 | mqtt_info->iothub_type = IOTHUB_TYPE_DEVICE_TWIN; |
AzureIoTClient | 12:658ca6865de2 | 909 | mqtt_info->device_twin_data = NULL; |
AzureIoTClient | 12:658ca6865de2 | 910 | STRING_HANDLE msg_topic = STRING_construct_sprintf(GET_PROPERTIES_TOPIC, mqtt_info->packet_id); |
AzureIoTClient | 12:658ca6865de2 | 911 | if (msg_topic == NULL) |
AzureIoTClient | 12:658ca6865de2 | 912 | { |
AzureIoTClient | 12:658ca6865de2 | 913 | LogError("Failed constructing get Prop topic."); |
AzureIoTClient | 12:658ca6865de2 | 914 | free(mqtt_info); |
AzureIoTClient | 18:ec8e5e97c6a4 | 915 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 916 | } |
AzureIoTClient | 12:658ca6865de2 | 917 | else |
AzureIoTClient | 12:658ca6865de2 | 918 | { |
AzureIoTClient | 12:658ca6865de2 | 919 | MQTT_MESSAGE_HANDLE mqtt_get_msg = mqttmessage_create(mqtt_info->packet_id, STRING_c_str(msg_topic), DELIVER_AT_MOST_ONCE, NULL, 0); |
AzureIoTClient | 12:658ca6865de2 | 920 | if (mqtt_get_msg == NULL) |
AzureIoTClient | 12:658ca6865de2 | 921 | { |
AzureIoTClient | 12:658ca6865de2 | 922 | LogError("Failed constructing mqtt message."); |
AzureIoTClient | 12:658ca6865de2 | 923 | free(mqtt_info); |
AzureIoTClient | 18:ec8e5e97c6a4 | 924 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 925 | } |
AzureIoTClient | 12:658ca6865de2 | 926 | else |
AzureIoTClient | 12:658ca6865de2 | 927 | { |
AzureIoTClient | 12:658ca6865de2 | 928 | if (mqtt_client_publish(transport_data->mqttClient, mqtt_get_msg) != 0) |
AzureIoTClient | 12:658ca6865de2 | 929 | { |
AzureIoTClient | 12:658ca6865de2 | 930 | LogError("Failed publishing to mqtt client."); |
AzureIoTClient | 12:658ca6865de2 | 931 | free(mqtt_info); |
AzureIoTClient | 18:ec8e5e97c6a4 | 932 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 933 | } |
AzureIoTClient | 12:658ca6865de2 | 934 | else |
AzureIoTClient | 12:658ca6865de2 | 935 | { |
AzureIoTClient | 12:658ca6865de2 | 936 | DList_InsertTailList(&transport_data->ack_waiting_queue, &mqtt_info->entry); |
AzureIoTClient | 12:658ca6865de2 | 937 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 938 | } |
AzureIoTClient | 12:658ca6865de2 | 939 | mqttmessage_destroy(mqtt_get_msg); |
AzureIoTClient | 12:658ca6865de2 | 940 | } |
AzureIoTClient | 12:658ca6865de2 | 941 | STRING_delete(msg_topic); |
AzureIoTClient | 12:658ca6865de2 | 942 | } |
AzureIoTClient | 12:658ca6865de2 | 943 | } |
AzureIoTClient | 12:658ca6865de2 | 944 | return result; |
AzureIoTClient | 12:658ca6865de2 | 945 | } |
AzureIoTClient | 12:658ca6865de2 | 946 | |
AzureIoTClient | 12:658ca6865de2 | 947 | static int publish_device_twin_message(MQTTTRANSPORT_HANDLE_DATA* transport_data, IOTHUB_DEVICE_TWIN* device_twin_info, MQTT_DEVICE_TWIN_ITEM* mqtt_info) |
AzureIoTClient | 12:658ca6865de2 | 948 | { |
AzureIoTClient | 12:658ca6865de2 | 949 | int result; |
AzureIoTClient | 12:658ca6865de2 | 950 | mqtt_info->packet_id = get_next_packet_id(transport_data); |
AzureIoTClient | 12:658ca6865de2 | 951 | mqtt_info->device_twin_msg_type = REPORTED_STATE; |
AzureIoTClient | 12:658ca6865de2 | 952 | STRING_HANDLE msgTopic = STRING_construct_sprintf(REPORTED_PROPERTIES_TOPIC, mqtt_info->packet_id); |
AzureIoTClient | 12:658ca6865de2 | 953 | if (msgTopic == NULL) |
AzureIoTClient | 12:658ca6865de2 | 954 | { |
AzureIoTClient | 12:658ca6865de2 | 955 | LogError("Failed constructing reported prop topic."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 956 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 957 | } |
AzureIoTClient | 12:658ca6865de2 | 958 | else |
AzureIoTClient | 12:658ca6865de2 | 959 | { |
AzureIoTClient | 12:658ca6865de2 | 960 | const CONSTBUFFER* data_buff = CONSTBUFFER_GetContent(device_twin_info->report_data_handle); |
AzureIoTClient | 12:658ca6865de2 | 961 | MQTT_MESSAGE_HANDLE mqtt_rpt_msg = mqttmessage_create(mqtt_info->packet_id, STRING_c_str(msgTopic), DELIVER_AT_MOST_ONCE, data_buff->buffer, data_buff->size); |
AzureIoTClient | 12:658ca6865de2 | 962 | if (mqtt_rpt_msg == NULL) |
AzureIoTClient | 12:658ca6865de2 | 963 | { |
AzureIoTClient | 12:658ca6865de2 | 964 | LogError("Failed creating mqtt message"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 965 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 966 | } |
AzureIoTClient | 12:658ca6865de2 | 967 | else |
AzureIoTClient | 12:658ca6865de2 | 968 | { |
Azure.IoT.Build | 13:606465879c57 | 969 | if (tickcounter_get_current_ms(transport_data->msgTickCounter, &mqtt_info->msgPublishTime) != 0) |
AzureIoTClient | 12:658ca6865de2 | 970 | { |
AzureIoTClient | 12:658ca6865de2 | 971 | LogError("Failed retrieving tickcounter info"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 972 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 973 | } |
AzureIoTClient | 12:658ca6865de2 | 974 | else |
AzureIoTClient | 12:658ca6865de2 | 975 | { |
AzureIoTClient | 12:658ca6865de2 | 976 | if (mqtt_client_publish(transport_data->mqttClient, mqtt_rpt_msg) != 0) |
AzureIoTClient | 12:658ca6865de2 | 977 | { |
AzureIoTClient | 12:658ca6865de2 | 978 | LogError("Failed publishing mqtt message"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 979 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 980 | } |
AzureIoTClient | 12:658ca6865de2 | 981 | else |
AzureIoTClient | 12:658ca6865de2 | 982 | { |
AzureIoTClient | 12:658ca6865de2 | 983 | mqtt_info->retryCount++; |
AzureIoTClient | 12:658ca6865de2 | 984 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 985 | } |
AzureIoTClient | 12:658ca6865de2 | 986 | } |
AzureIoTClient | 12:658ca6865de2 | 987 | mqttmessage_destroy(mqtt_rpt_msg); |
AzureIoTClient | 12:658ca6865de2 | 988 | } |
AzureIoTClient | 12:658ca6865de2 | 989 | STRING_delete(msgTopic); |
AzureIoTClient | 12:658ca6865de2 | 990 | } |
AzureIoTClient | 12:658ca6865de2 | 991 | return result; |
AzureIoTClient | 12:658ca6865de2 | 992 | } |
AzureIoTClient | 12:658ca6865de2 | 993 | |
AzureIoTClient | 11:31ebaeb51e1e | 994 | static bool isSystemProperty(const char* tokenData) |
AzureIoTClient | 11:31ebaeb51e1e | 995 | { |
AzureIoTClient | 11:31ebaeb51e1e | 996 | bool result = false; |
AzureIoTClient | 11:31ebaeb51e1e | 997 | size_t propCount = sizeof(sysPropList)/sizeof(sysPropList[0]); |
AzureIoTClient | 16:14640ee83e99 | 998 | size_t index = 0; |
AzureIoTClient | 16:14640ee83e99 | 999 | for (index = 0; index < propCount; index++) |
AzureIoTClient | 11:31ebaeb51e1e | 1000 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1001 | if (memcmp(tokenData, sysPropList[index].propName, sysPropList[index].propLength) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1002 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1003 | result = true; |
AzureIoTClient | 11:31ebaeb51e1e | 1004 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1005 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1006 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1007 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 1008 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1009 | |
AzureIoTClient | 31:d6198e67d1eb | 1010 | static int setMqttMessagePropertyIfPossible(IOTHUB_MESSAGE_HANDLE IoTHubMessage, const char* propName, const char* propValue, size_t nameLen) |
AzureIoTClient | 31:d6198e67d1eb | 1011 | { |
AzureIoTClient | 31:d6198e67d1eb | 1012 | // Not finding a system property to map to isn't an error. |
AzureIoTClient | 31:d6198e67d1eb | 1013 | int result = 0; |
AzureIoTClient | 31:d6198e67d1eb | 1014 | |
AzureIoTClient | 31:d6198e67d1eb | 1015 | if (nameLen > 2) |
AzureIoTClient | 31:d6198e67d1eb | 1016 | { |
AzureIoTClient | 31:d6198e67d1eb | 1017 | if (nameLen > 3) |
AzureIoTClient | 31:d6198e67d1eb | 1018 | { |
AzureIoTClient | 31:d6198e67d1eb | 1019 | if (strcmp((const char*)&propName[nameLen - 3], MESSAGE_ID_PROPERTY) == 0) |
AzureIoTClient | 31:d6198e67d1eb | 1020 | { |
AzureIoTClient | 31:d6198e67d1eb | 1021 | if (IoTHubMessage_SetMessageId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 31:d6198e67d1eb | 1022 | { |
AzureIoTClient | 31:d6198e67d1eb | 1023 | LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'messageId' property."); |
AzureIoTClient | 31:d6198e67d1eb | 1024 | result = __FAILURE__; |
AzureIoTClient | 31:d6198e67d1eb | 1025 | } |
AzureIoTClient | 31:d6198e67d1eb | 1026 | } |
AzureIoTClient | 31:d6198e67d1eb | 1027 | else if (strcmp((const char*)&propName[nameLen - 3], CORRELATION_ID_PROPERTY) == 0) |
AzureIoTClient | 31:d6198e67d1eb | 1028 | { |
AzureIoTClient | 31:d6198e67d1eb | 1029 | if (IoTHubMessage_SetCorrelationId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 31:d6198e67d1eb | 1030 | { |
AzureIoTClient | 31:d6198e67d1eb | 1031 | LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'correlationId' property."); |
AzureIoTClient | 31:d6198e67d1eb | 1032 | result = __FAILURE__; |
AzureIoTClient | 31:d6198e67d1eb | 1033 | } |
AzureIoTClient | 31:d6198e67d1eb | 1034 | } |
AzureIoTClient | 31:d6198e67d1eb | 1035 | } |
AzureIoTClient | 31:d6198e67d1eb | 1036 | |
AzureIoTClient | 31:d6198e67d1eb | 1037 | // 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 ] |
AzureIoTClient | 31:d6198e67d1eb | 1038 | if (strcmp((const char*)&propName[nameLen - 2], CONTENT_TYPE_PROPERTY) == 0) |
AzureIoTClient | 31:d6198e67d1eb | 1039 | { |
AzureIoTClient | 31:d6198e67d1eb | 1040 | if (IoTHubMessage_SetContentTypeSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 31:d6198e67d1eb | 1041 | { |
AzureIoTClient | 31:d6198e67d1eb | 1042 | LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'customContentType' property."); |
AzureIoTClient | 31:d6198e67d1eb | 1043 | result = __FAILURE__; |
AzureIoTClient | 31:d6198e67d1eb | 1044 | } |
AzureIoTClient | 31:d6198e67d1eb | 1045 | } |
AzureIoTClient | 31:d6198e67d1eb | 1046 | // 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 ] |
AzureIoTClient | 31:d6198e67d1eb | 1047 | else if (strcmp((const char*)&propName[nameLen - 2], CONTENT_ENCODING_PROPERTY) == 0) |
AzureIoTClient | 31:d6198e67d1eb | 1048 | { |
AzureIoTClient | 31:d6198e67d1eb | 1049 | if (IoTHubMessage_SetContentEncodingSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 31:d6198e67d1eb | 1050 | { |
AzureIoTClient | 31:d6198e67d1eb | 1051 | LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'contentEncoding' property."); |
AzureIoTClient | 31:d6198e67d1eb | 1052 | result = __FAILURE__; |
AzureIoTClient | 31:d6198e67d1eb | 1053 | } |
AzureIoTClient | 31:d6198e67d1eb | 1054 | } |
AzureIoTClient | 31:d6198e67d1eb | 1055 | } |
AzureIoTClient | 31:d6198e67d1eb | 1056 | |
AzureIoTClient | 31:d6198e67d1eb | 1057 | return result; |
AzureIoTClient | 31:d6198e67d1eb | 1058 | } |
AzureIoTClient | 31:d6198e67d1eb | 1059 | |
AzureIoTClient | 37:e6a799428f3d | 1060 | static int extractMqttProperties(IOTHUB_MESSAGE_HANDLE IoTHubMessage, const char* topic_name, bool urldecode) |
AzureIoTClient | 11:31ebaeb51e1e | 1061 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1062 | int result; |
AzureIoTClient | 11:31ebaeb51e1e | 1063 | STRING_HANDLE mqttTopic = STRING_construct(topic_name); |
AzureIoTClient | 11:31ebaeb51e1e | 1064 | if (mqttTopic == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1065 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1066 | LogError("Failure constructing string topic name."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1067 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1068 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1069 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1070 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1071 | STRING_TOKENIZER_HANDLE token = STRING_TOKENIZER_create(mqttTopic); |
AzureIoTClient | 11:31ebaeb51e1e | 1072 | if (token != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1073 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1074 | MAP_HANDLE propertyMap = IoTHubMessage_Properties(IoTHubMessage); |
AzureIoTClient | 11:31ebaeb51e1e | 1075 | if (propertyMap == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1076 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1077 | LogError("Failure to retrieve IoTHubMessage_properties."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1078 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1079 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1080 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1081 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1082 | STRING_HANDLE output = STRING_new(); |
AzureIoTClient | 11:31ebaeb51e1e | 1083 | if (output == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1084 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1085 | LogError("Failure to allocate STRING_new."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1086 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1087 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1088 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1089 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1090 | result = 0; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1091 | |
AzureIoTClient | 11:31ebaeb51e1e | 1092 | while (STRING_TOKENIZER_get_next_token(token, output, PROPERTY_SEPARATOR) == 0 && result == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1093 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1094 | const char* tokenData = STRING_c_str(output); |
AzureIoTClient | 11:31ebaeb51e1e | 1095 | size_t tokenLen = strlen(tokenData); |
AzureIoTClient | 11:31ebaeb51e1e | 1096 | if (tokenData == NULL || tokenLen == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1097 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1098 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1099 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1100 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1101 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1102 | if (isSystemProperty(tokenData)) |
AzureIoTClient | 11:31ebaeb51e1e | 1103 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1104 | const char* iterator = tokenData; |
AzureIoTClient | 11:31ebaeb51e1e | 1105 | while (iterator != NULL && *iterator != '\0' && result == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1106 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1107 | if (*iterator == '=') |
AzureIoTClient | 11:31ebaeb51e1e | 1108 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1109 | size_t nameLen = iterator - tokenData; |
AzureIoTClient | 11:31ebaeb51e1e | 1110 | char* propName = malloc(nameLen + 1); |
AzureIoTClient | 11:31ebaeb51e1e | 1111 | |
AzureIoTClient | 11:31ebaeb51e1e | 1112 | size_t valLen = tokenLen - (nameLen + 1) + 1; |
AzureIoTClient | 11:31ebaeb51e1e | 1113 | char* propValue = malloc(valLen + 1); |
AzureIoTClient | 11:31ebaeb51e1e | 1114 | |
AzureIoTClient | 11:31ebaeb51e1e | 1115 | if (propName == NULL || propValue == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1116 | { |
AzureIoTClient | 27:04de3c0bf1db | 1117 | LogError("Failed allocating property name (%p) and/or value (%p)", propName, propValue); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1118 | result = __FAILURE__; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1119 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1120 | else |
AzureIoTClient | 18:ec8e5e97c6a4 | 1121 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1122 | strncpy(propName, tokenData, nameLen); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1123 | propName[nameLen] = '\0'; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1124 | |
AzureIoTClient | 18:ec8e5e97c6a4 | 1125 | strncpy(propValue, iterator + 1, valLen); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1126 | propValue[valLen] = '\0'; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1127 | |
AzureIoTClient | 37:e6a799428f3d | 1128 | if (urldecode) |
AzureIoTClient | 18:ec8e5e97c6a4 | 1129 | { |
AzureIoTClient | 37:e6a799428f3d | 1130 | STRING_HANDLE propValue_decoded; |
AzureIoTClient | 37:e6a799428f3d | 1131 | if ((propValue_decoded = URL_DecodeString(propValue)) == NULL) |
AzureIoTClient | 37:e6a799428f3d | 1132 | { |
AzureIoTClient | 37:e6a799428f3d | 1133 | LogError("Failed to URL decode property value"); |
AzureIoTClient | 37:e6a799428f3d | 1134 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1135 | } |
AzureIoTClient | 37:e6a799428f3d | 1136 | else if (setMqttMessagePropertyIfPossible(IoTHubMessage, propName, STRING_c_str(propValue_decoded), nameLen) != 0) |
AzureIoTClient | 37:e6a799428f3d | 1137 | { |
AzureIoTClient | 37:e6a799428f3d | 1138 | LogError("Unable to set message property"); |
AzureIoTClient | 37:e6a799428f3d | 1139 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1140 | } |
AzureIoTClient | 37:e6a799428f3d | 1141 | STRING_delete(propValue_decoded); |
AzureIoTClient | 37:e6a799428f3d | 1142 | } |
AzureIoTClient | 37:e6a799428f3d | 1143 | else |
AzureIoTClient | 37:e6a799428f3d | 1144 | { |
AzureIoTClient | 37:e6a799428f3d | 1145 | if (setMqttMessagePropertyIfPossible(IoTHubMessage, propName, propValue, nameLen) != 0) |
AzureIoTClient | 37:e6a799428f3d | 1146 | { |
AzureIoTClient | 37:e6a799428f3d | 1147 | LogError("Unable to set message property"); |
AzureIoTClient | 37:e6a799428f3d | 1148 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1149 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1150 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1151 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1152 | free(propName); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1153 | free(propValue); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1154 | |
AzureIoTClient | 18:ec8e5e97c6a4 | 1155 | break; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1156 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1157 | iterator++; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1158 | } |
AzureIoTClient | 18:ec8e5e97c6a4 | 1159 | } |
AzureIoTClient | 37:e6a799428f3d | 1160 | else //User Properties |
AzureIoTClient | 18:ec8e5e97c6a4 | 1161 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1162 | const char* iterator = tokenData; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1163 | while (iterator != NULL && *iterator != '\0' && result == 0) |
AzureIoTClient | 18:ec8e5e97c6a4 | 1164 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1165 | if (*iterator == '=') |
AzureIoTClient | 18:ec8e5e97c6a4 | 1166 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1167 | size_t nameLen = iterator - tokenData; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1168 | char* propName = malloc(nameLen + 1); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1169 | |
AzureIoTClient | 18:ec8e5e97c6a4 | 1170 | size_t valLen = tokenLen - (nameLen + 1) + 1; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1171 | char* propValue = malloc(valLen + 1); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1172 | |
AzureIoTClient | 18:ec8e5e97c6a4 | 1173 | if (propName == NULL || propValue == NULL) |
AzureIoTClient | 18:ec8e5e97c6a4 | 1174 | { |
AzureIoTClient | 18:ec8e5e97c6a4 | 1175 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1176 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1177 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1178 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1179 | strncpy(propName, tokenData, nameLen); |
AzureIoTClient | 11:31ebaeb51e1e | 1180 | propName[nameLen] = '\0'; |
AzureIoTClient | 11:31ebaeb51e1e | 1181 | |
AzureIoTClient | 11:31ebaeb51e1e | 1182 | strncpy(propValue, iterator + 1, valLen); |
AzureIoTClient | 11:31ebaeb51e1e | 1183 | propValue[valLen] = '\0'; |
AzureIoTClient | 11:31ebaeb51e1e | 1184 | |
AzureIoTClient | 37:e6a799428f3d | 1185 | if (urldecode) |
AzureIoTClient | 11:31ebaeb51e1e | 1186 | { |
AzureIoTClient | 37:e6a799428f3d | 1187 | STRING_HANDLE propName_decoded = URL_DecodeString(propName); |
AzureIoTClient | 37:e6a799428f3d | 1188 | STRING_HANDLE propValue_decoded = URL_DecodeString(propValue); |
AzureIoTClient | 37:e6a799428f3d | 1189 | if (propName_decoded == NULL || propValue_decoded == NULL) |
AzureIoTClient | 37:e6a799428f3d | 1190 | { |
AzureIoTClient | 37:e6a799428f3d | 1191 | LogError("Failed to URL decode property"); |
AzureIoTClient | 37:e6a799428f3d | 1192 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1193 | } |
AzureIoTClient | 37:e6a799428f3d | 1194 | else if (Map_AddOrUpdate(propertyMap, STRING_c_str(propName_decoded), STRING_c_str(propValue_decoded)) != MAP_OK) |
AzureIoTClient | 37:e6a799428f3d | 1195 | { |
AzureIoTClient | 37:e6a799428f3d | 1196 | LogError("Map_AddOrUpdate failed."); |
AzureIoTClient | 37:e6a799428f3d | 1197 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1198 | } |
AzureIoTClient | 37:e6a799428f3d | 1199 | STRING_delete(propName_decoded); |
AzureIoTClient | 37:e6a799428f3d | 1200 | STRING_delete(propValue_decoded); |
AzureIoTClient | 37:e6a799428f3d | 1201 | } |
AzureIoTClient | 37:e6a799428f3d | 1202 | else |
AzureIoTClient | 37:e6a799428f3d | 1203 | { |
AzureIoTClient | 37:e6a799428f3d | 1204 | if (Map_AddOrUpdate(propertyMap, propName, propValue) != MAP_OK) |
AzureIoTClient | 37:e6a799428f3d | 1205 | { |
AzureIoTClient | 37:e6a799428f3d | 1206 | LogError("Map_AddOrUpdate failed."); |
AzureIoTClient | 37:e6a799428f3d | 1207 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1208 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1209 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1210 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1211 | free(propName); |
AzureIoTClient | 11:31ebaeb51e1e | 1212 | free(propValue); |
AzureIoTClient | 11:31ebaeb51e1e | 1213 | |
AzureIoTClient | 11:31ebaeb51e1e | 1214 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1215 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1216 | iterator++; |
AzureIoTClient | 11:31ebaeb51e1e | 1217 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1218 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1219 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1220 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1221 | STRING_delete(output); |
AzureIoTClient | 11:31ebaeb51e1e | 1222 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1223 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1224 | STRING_TOKENIZER_destroy(token); |
AzureIoTClient | 11:31ebaeb51e1e | 1225 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1226 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1227 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1228 | LogError("Unable to create Tokenizer object."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1229 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1230 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1231 | STRING_delete(mqttTopic); |
AzureIoTClient | 11:31ebaeb51e1e | 1232 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1233 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 1234 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1235 | |
AzureIoTClient | 11:31ebaeb51e1e | 1236 | static void mqtt_notification_callback(MQTT_MESSAGE_HANDLE msgHandle, void* callbackCtx) |
AzureIoTClient | 11:31ebaeb51e1e | 1237 | { |
AzureIoTClient | 14:4dc2b011be33 | 1238 | /* Tests_SRS_IOTHUB_MQTT_TRANSPORT_07_051: [ If msgHandle or callbackCtx is NULL, mqtt_notification_callback shall do nothing. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1239 | if (msgHandle != NULL && callbackCtx != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1240 | { |
AzureIoTClient | 14:4dc2b011be33 | 1241 | /* Tests_SRS_IOTHUB_MQTT_TRANSPORT_07_052: [ mqtt_notification_callback shall extract the topic Name from the MQTT_MESSAGE_HANDLE. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1242 | const char* topic_resp = mqttmessage_getTopicName(msgHandle); |
AzureIoTClient | 11:31ebaeb51e1e | 1243 | if (topic_resp == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1244 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1245 | LogError("Failure: NULL topic name encountered"); |
AzureIoTClient | 11:31ebaeb51e1e | 1246 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1247 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1248 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1249 | PMQTTTRANSPORT_HANDLE_DATA transportData = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx; |
AzureIoTClient | 11:31ebaeb51e1e | 1250 | |
AzureIoTClient | 12:658ca6865de2 | 1251 | IOTHUB_IDENTITY_TYPE type = retrieve_topic_type(topic_resp); |
AzureIoTClient | 12:658ca6865de2 | 1252 | if (type == IOTHUB_TYPE_DEVICE_TWIN) |
AzureIoTClient | 11:31ebaeb51e1e | 1253 | { |
AzureIoTClient | 12:658ca6865de2 | 1254 | size_t request_id; |
AzureIoTClient | 12:658ca6865de2 | 1255 | int status_code; |
AzureIoTClient | 12:658ca6865de2 | 1256 | bool notification_msg; |
AzureIoTClient | 12:658ca6865de2 | 1257 | if (parse_device_twin_topic_info(topic_resp, ¬ification_msg, &request_id, &status_code) != 0) |
AzureIoTClient | 12:658ca6865de2 | 1258 | { |
AzureIoTClient | 12:658ca6865de2 | 1259 | LogError("Failure: parsing device topic info"); |
AzureIoTClient | 12:658ca6865de2 | 1260 | } |
AzureIoTClient | 12:658ca6865de2 | 1261 | else |
AzureIoTClient | 12:658ca6865de2 | 1262 | { |
AzureIoTClient | 12:658ca6865de2 | 1263 | const APP_PAYLOAD* payload = mqttmessage_getApplicationMsg(msgHandle); |
AzureIoTClient | 12:658ca6865de2 | 1264 | if (notification_msg) |
AzureIoTClient | 12:658ca6865de2 | 1265 | { |
AzureIoTClient | 39:6231984e0179 | 1266 | IoTHubClientCore_LL_RetrievePropertyComplete(transportData->llClientHandle, DEVICE_TWIN_UPDATE_PARTIAL, payload->message, payload->length); |
AzureIoTClient | 12:658ca6865de2 | 1267 | } |
AzureIoTClient | 12:658ca6865de2 | 1268 | else |
AzureIoTClient | 12:658ca6865de2 | 1269 | { |
AzureIoTClient | 12:658ca6865de2 | 1270 | PDLIST_ENTRY dev_twin_item = transportData->ack_waiting_queue.Flink; |
AzureIoTClient | 12:658ca6865de2 | 1271 | while (dev_twin_item != &transportData->ack_waiting_queue) |
AzureIoTClient | 12:658ca6865de2 | 1272 | { |
AzureIoTClient | 12:658ca6865de2 | 1273 | DLIST_ENTRY saveListEntry; |
AzureIoTClient | 12:658ca6865de2 | 1274 | saveListEntry.Flink = dev_twin_item->Flink; |
AzureIoTClient | 12:658ca6865de2 | 1275 | MQTT_DEVICE_TWIN_ITEM* msg_entry = containingRecord(dev_twin_item, MQTT_DEVICE_TWIN_ITEM, entry); |
AzureIoTClient | 12:658ca6865de2 | 1276 | if (request_id == msg_entry->packet_id) |
AzureIoTClient | 12:658ca6865de2 | 1277 | { |
AzureIoTClient | 12:658ca6865de2 | 1278 | (void)DList_RemoveEntryList(dev_twin_item); |
AzureIoTClient | 12:658ca6865de2 | 1279 | if (msg_entry->device_twin_msg_type == RETRIEVE_PROPERTIES) |
AzureIoTClient | 12:658ca6865de2 | 1280 | { |
AzureIoTClient | 39:6231984e0179 | 1281 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ If type is IOTHUB_TYPE_DEVICE_TWIN, then on success if msg_type is RETRIEVE_PROPERTIES then mqtt_notification_callback shall call IoTHubClientCore_LL_RetrievePropertyComplete... ] */ |
AzureIoTClient | 39:6231984e0179 | 1282 | IoTHubClientCore_LL_RetrievePropertyComplete(transportData->llClientHandle, DEVICE_TWIN_UPDATE_COMPLETE, payload->message, payload->length); |
AzureIoTClient | 12:658ca6865de2 | 1283 | } |
AzureIoTClient | 12:658ca6865de2 | 1284 | else |
AzureIoTClient | 12:658ca6865de2 | 1285 | { |
AzureIoTClient | 39:6231984e0179 | 1286 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_055: [ if device_twin_msg_type is not RETRIEVE_PROPERTIES then mqtt_notification_callback shall call IoTHubClientCore_LL_ReportedStateComplete ] */ |
AzureIoTClient | 39:6231984e0179 | 1287 | IoTHubClientCore_LL_ReportedStateComplete(transportData->llClientHandle, msg_entry->iothub_msg_id, status_code); |
AzureIoTClient | 12:658ca6865de2 | 1288 | } |
AzureIoTClient | 12:658ca6865de2 | 1289 | free(msg_entry); |
AzureIoTClient | 12:658ca6865de2 | 1290 | break; |
AzureIoTClient | 12:658ca6865de2 | 1291 | } |
AzureIoTClient | 12:658ca6865de2 | 1292 | dev_twin_item = saveListEntry.Flink; |
AzureIoTClient | 12:658ca6865de2 | 1293 | } |
AzureIoTClient | 12:658ca6865de2 | 1294 | } |
AzureIoTClient | 12:658ca6865de2 | 1295 | } |
AzureIoTClient | 12:658ca6865de2 | 1296 | } |
AzureIoTClient | 12:658ca6865de2 | 1297 | else if (type == IOTHUB_TYPE_DEVICE_METHODS) |
AzureIoTClient | 12:658ca6865de2 | 1298 | { |
AzureIoTClient | 12:658ca6865de2 | 1299 | STRING_HANDLE method_name = STRING_new(); |
AzureIoTClient | 12:658ca6865de2 | 1300 | if (method_name == NULL) |
AzureIoTClient | 12:658ca6865de2 | 1301 | { |
AzureIoTClient | 12:658ca6865de2 | 1302 | LogError("Failure: allocating method_name string value"); |
AzureIoTClient | 12:658ca6865de2 | 1303 | } |
AzureIoTClient | 12:658ca6865de2 | 1304 | else |
AzureIoTClient | 12:658ca6865de2 | 1305 | { |
AzureIoTClient | 14:4dc2b011be33 | 1306 | DEVICE_METHOD_INFO* dev_method_info = malloc(sizeof(DEVICE_METHOD_INFO) ); |
AzureIoTClient | 14:4dc2b011be33 | 1307 | if (dev_method_info == NULL) |
AzureIoTClient | 12:658ca6865de2 | 1308 | { |
AzureIoTClient | 14:4dc2b011be33 | 1309 | LogError("Failure: allocating DEVICE_METHOD_INFO object"); |
AzureIoTClient | 12:658ca6865de2 | 1310 | } |
AzureIoTClient | 12:658ca6865de2 | 1311 | else |
AzureIoTClient | 12:658ca6865de2 | 1312 | { |
AzureIoTClient | 14:4dc2b011be33 | 1313 | dev_method_info->request_id = STRING_new(); |
AzureIoTClient | 14:4dc2b011be33 | 1314 | if (dev_method_info->request_id == NULL) |
AzureIoTClient | 14:4dc2b011be33 | 1315 | { |
AzureIoTClient | 14:4dc2b011be33 | 1316 | LogError("Failure constructing request_id string"); |
AzureIoTClient | 14:4dc2b011be33 | 1317 | free(dev_method_info); |
AzureIoTClient | 14:4dc2b011be33 | 1318 | } |
AzureIoTClient | 14:4dc2b011be33 | 1319 | else if (retrieve_device_method_rid_info(topic_resp, method_name, dev_method_info->request_id) != 0) |
AzureIoTClient | 12:658ca6865de2 | 1320 | { |
AzureIoTClient | 14:4dc2b011be33 | 1321 | LogError("Failure: retrieve device topic info"); |
AzureIoTClient | 14:4dc2b011be33 | 1322 | STRING_delete(dev_method_info->request_id); |
AzureIoTClient | 14:4dc2b011be33 | 1323 | free(dev_method_info); |
AzureIoTClient | 12:658ca6865de2 | 1324 | } |
AzureIoTClient | 14:4dc2b011be33 | 1325 | else |
AzureIoTClient | 14:4dc2b011be33 | 1326 | { |
AzureIoTClient | 39:6231984e0179 | 1327 | /* CodesSRS_IOTHUB_MQTT_TRANSPORT_07_053: [ If type is IOTHUB_TYPE_DEVICE_METHODS, then on success mqtt_notification_callback shall call IoTHubClientCore_LL_DeviceMethodComplete. ] */ |
AzureIoTClient | 14:4dc2b011be33 | 1328 | const APP_PAYLOAD* payload = mqttmessage_getApplicationMsg(msgHandle); |
AzureIoTClient | 39:6231984e0179 | 1329 | if (IoTHubClientCore_LL_DeviceMethodComplete(transportData->llClientHandle, STRING_c_str(method_name), payload->message, payload->length, (void*)dev_method_info) != 0) |
AzureIoTClient | 14:4dc2b011be33 | 1330 | { |
AzureIoTClient | 39:6231984e0179 | 1331 | LogError("Failure: IoTHubClientCore_LL_DeviceMethodComplete"); |
AzureIoTClient | 14:4dc2b011be33 | 1332 | STRING_delete(dev_method_info->request_id); |
AzureIoTClient | 14:4dc2b011be33 | 1333 | free(dev_method_info); |
AzureIoTClient | 14:4dc2b011be33 | 1334 | } |
AzureIoTClient | 14:4dc2b011be33 | 1335 | } |
AzureIoTClient | 12:658ca6865de2 | 1336 | } |
AzureIoTClient | 12:658ca6865de2 | 1337 | STRING_delete(method_name); |
AzureIoTClient | 12:658ca6865de2 | 1338 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1339 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1340 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1341 | { |
AzureIoTClient | 12:658ca6865de2 | 1342 | const APP_PAYLOAD* appPayload = mqttmessage_getApplicationMsg(msgHandle); |
AzureIoTClient | 12:658ca6865de2 | 1343 | IOTHUB_MESSAGE_HANDLE IoTHubMessage = IoTHubMessage_CreateFromByteArray(appPayload->message, appPayload->length); |
AzureIoTClient | 12:658ca6865de2 | 1344 | if (IoTHubMessage == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1345 | { |
AzureIoTClient | 12:658ca6865de2 | 1346 | LogError("Failure: IotHub Message creation has failed."); |
AzureIoTClient | 11:31ebaeb51e1e | 1347 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1348 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1349 | { |
AzureIoTClient | 12:658ca6865de2 | 1350 | // Will need to update this when the service has messages that can be rejected |
AzureIoTClient | 37:e6a799428f3d | 1351 | if (extractMqttProperties(IoTHubMessage, topic_resp, transportData->auto_url_encode_decode) != 0) |
AzureIoTClient | 12:658ca6865de2 | 1352 | { |
AzureIoTClient | 12:658ca6865de2 | 1353 | LogError("failure extracting mqtt properties."); |
AzureIoTClient | 12:658ca6865de2 | 1354 | } |
AzureIoTClient | 12:658ca6865de2 | 1355 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1356 | { |
AzureIoTClient | 19:f87dfe76bc70 | 1357 | MESSAGE_CALLBACK_INFO* messageData = (MESSAGE_CALLBACK_INFO*)malloc(sizeof(MESSAGE_CALLBACK_INFO)); |
AzureIoTClient | 19:f87dfe76bc70 | 1358 | if (messageData == NULL) |
AzureIoTClient | 19:f87dfe76bc70 | 1359 | { |
AzureIoTClient | 19:f87dfe76bc70 | 1360 | LogError("malloc failed"); |
AzureIoTClient | 19:f87dfe76bc70 | 1361 | } |
AzureIoTClient | 19:f87dfe76bc70 | 1362 | else |
AzureIoTClient | 12:658ca6865de2 | 1363 | { |
AzureIoTClient | 19:f87dfe76bc70 | 1364 | messageData->messageHandle = IoTHubMessage; |
AzureIoTClient | 19:f87dfe76bc70 | 1365 | messageData->transportContext = NULL; |
AzureIoTClient | 19:f87dfe76bc70 | 1366 | |
AzureIoTClient | 39:6231984e0179 | 1367 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_056: [ If type is IOTHUB_TYPE_TELEMETRY, then on success mqtt_notification_callback shall call IoTHubClientCore_LL_MessageCallback. ] */ |
AzureIoTClient | 39:6231984e0179 | 1368 | if (!IoTHubClientCore_LL_MessageCallback(transportData->llClientHandle, messageData)) |
AzureIoTClient | 19:f87dfe76bc70 | 1369 | { |
AzureIoTClient | 39:6231984e0179 | 1370 | LogError("IoTHubClientCore_LL_MessageCallback returned false"); |
AzureIoTClient | 19:f87dfe76bc70 | 1371 | |
AzureIoTClient | 19:f87dfe76bc70 | 1372 | IoTHubMessage_Destroy(IoTHubMessage); |
AzureIoTClient | 19:f87dfe76bc70 | 1373 | free(messageData); |
AzureIoTClient | 19:f87dfe76bc70 | 1374 | } |
AzureIoTClient | 12:658ca6865de2 | 1375 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1376 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1377 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1378 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1379 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1380 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1381 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1382 | |
AzureIoTClient | 11:31ebaeb51e1e | 1383 | static void mqtt_operation_complete_callback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_RESULT actionResult, const void* msgInfo, void* callbackCtx) |
AzureIoTClient | 11:31ebaeb51e1e | 1384 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1385 | (void)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 1386 | if (callbackCtx != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1387 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1388 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx; |
AzureIoTClient | 11:31ebaeb51e1e | 1389 | |
AzureIoTClient | 11:31ebaeb51e1e | 1390 | switch (actionResult) |
AzureIoTClient | 11:31ebaeb51e1e | 1391 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1392 | case MQTT_CLIENT_ON_PUBLISH_ACK: |
AzureIoTClient | 11:31ebaeb51e1e | 1393 | case MQTT_CLIENT_ON_PUBLISH_COMP: |
AzureIoTClient | 11:31ebaeb51e1e | 1394 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1395 | const PUBLISH_ACK* puback = (const PUBLISH_ACK*)msgInfo; |
AzureIoTClient | 11:31ebaeb51e1e | 1396 | if (puback != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1397 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1398 | PDLIST_ENTRY currentListEntry = transport_data->telemetry_waitingForAck.Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 1399 | while (currentListEntry != &transport_data->telemetry_waitingForAck) |
AzureIoTClient | 11:31ebaeb51e1e | 1400 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1401 | MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry); |
AzureIoTClient | 11:31ebaeb51e1e | 1402 | DLIST_ENTRY saveListEntry; |
AzureIoTClient | 11:31ebaeb51e1e | 1403 | saveListEntry.Flink = currentListEntry->Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 1404 | |
AzureIoTClient | 11:31ebaeb51e1e | 1405 | if (puback->packetId == mqttMsgEntry->packet_id) |
AzureIoTClient | 11:31ebaeb51e1e | 1406 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1407 | (void)DList_RemoveEntryList(currentListEntry); //First remove the item from Waiting for Ack List. |
AzureIoTClient | 11:31ebaeb51e1e | 1408 | sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_OK); |
AzureIoTClient | 11:31ebaeb51e1e | 1409 | free(mqttMsgEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 1410 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1411 | currentListEntry = saveListEntry.Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 1412 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1413 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1414 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1415 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1416 | LogError("Failure: MQTT_CLIENT_ON_PUBLISH_ACK publish_ack structure NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 1417 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1418 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1419 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1420 | case MQTT_CLIENT_ON_CONNACK: |
AzureIoTClient | 11:31ebaeb51e1e | 1421 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1422 | const CONNECT_ACK* connack = (const CONNECT_ACK*)msgInfo; |
AzureIoTClient | 11:31ebaeb51e1e | 1423 | if (connack != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1424 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1425 | if (connack->returnCode == CONNECTION_ACCEPTED) |
AzureIoTClient | 11:31ebaeb51e1e | 1426 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1427 | // The connect packet has been acked |
AzureIoTClient | 11:31ebaeb51e1e | 1428 | transport_data->currPacketState = CONNACK_TYPE; |
AzureIoTClient | 12:658ca6865de2 | 1429 | transport_data->isRecoverableError = true; |
AzureIoTClient | 20:594780be216d | 1430 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_CONNECTED; |
AzureIoTClient | 25:a35763780a87 | 1431 | |
AzureIoTClient | 25:a35763780a87 | 1432 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_008: [ Upon successful connection the retry control shall be reset using retry_control_reset() ] |
AzureIoTClient | 25:a35763780a87 | 1433 | retry_control_reset(transport_data->retry_control_handle); |
AzureIoTClient | 25:a35763780a87 | 1434 | |
AzureIoTClient | 39:6231984e0179 | 1435 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_AUTHENTICATED, IOTHUB_CLIENT_CONNECTION_OK); |
AzureIoTClient | 11:31ebaeb51e1e | 1436 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1437 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1438 | { |
AzureIoTClient | 35:c56b7cfcb90b | 1439 | if (connack->returnCode == CONN_REFUSED_SERVER_UNAVAIL) |
AzureIoTClient | 35:c56b7cfcb90b | 1440 | { |
AzureIoTClient | 39:6231984e0179 | 1441 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_DEVICE_DISABLED); |
AzureIoTClient | 35:c56b7cfcb90b | 1442 | } |
AzureIoTClient | 35:c56b7cfcb90b | 1443 | else if (connack->returnCode == CONN_REFUSED_BAD_USERNAME_PASSWORD || connack->returnCode == CONN_REFUSED_ID_REJECTED) |
AzureIoTClient | 12:658ca6865de2 | 1444 | { |
AzureIoTClient | 12:658ca6865de2 | 1445 | transport_data->isRecoverableError = false; |
AzureIoTClient | 39:6231984e0179 | 1446 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL); |
AzureIoTClient | 12:658ca6865de2 | 1447 | } |
AzureIoTClient | 12:658ca6865de2 | 1448 | else if (connack->returnCode == CONN_REFUSED_NOT_AUTHORIZED) |
AzureIoTClient | 12:658ca6865de2 | 1449 | { |
AzureIoTClient | 39:6231984e0179 | 1450 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_DEVICE_DISABLED); |
AzureIoTClient | 12:658ca6865de2 | 1451 | } |
AzureIoTClient | 12:658ca6865de2 | 1452 | else if (connack->returnCode == CONN_REFUSED_UNACCEPTABLE_VERSION) |
AzureIoTClient | 12:658ca6865de2 | 1453 | { |
AzureIoTClient | 12:658ca6865de2 | 1454 | transport_data->isRecoverableError = false; |
AzureIoTClient | 12:658ca6865de2 | 1455 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1456 | LogError("Connection Not Accepted: 0x%x: %s", connack->returnCode, retrieve_mqtt_return_codes(connack->returnCode) ); |
AzureIoTClient | 33:b7dfb208ef0a | 1457 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_PENDING_CLOSE; |
AzureIoTClient | 11:31ebaeb51e1e | 1458 | transport_data->currPacketState = PACKET_TYPE_ERROR; |
AzureIoTClient | 11:31ebaeb51e1e | 1459 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1460 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1461 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1462 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1463 | LogError("MQTT_CLIENT_ON_CONNACK CONNACK parameter is NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 1464 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1465 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1466 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1467 | case MQTT_CLIENT_ON_SUBSCRIBE_ACK: |
AzureIoTClient | 11:31ebaeb51e1e | 1468 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1469 | const SUBSCRIBE_ACK* suback = (const SUBSCRIBE_ACK*)msgInfo; |
AzureIoTClient | 11:31ebaeb51e1e | 1470 | if (suback != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1471 | { |
AzureIoTClient | 16:14640ee83e99 | 1472 | size_t index = 0; |
AzureIoTClient | 16:14640ee83e99 | 1473 | for (index = 0; index < suback->qosCount; index++) |
AzureIoTClient | 11:31ebaeb51e1e | 1474 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1475 | if (suback->qosReturn[index] == DELIVER_FAILURE) |
AzureIoTClient | 11:31ebaeb51e1e | 1476 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1477 | LogError("Subscribe delivery failure of subscribe %zu", index); |
AzureIoTClient | 11:31ebaeb51e1e | 1478 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1479 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1480 | // The connect packet has been acked |
AzureIoTClient | 11:31ebaeb51e1e | 1481 | transport_data->currPacketState = SUBACK_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 1482 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1483 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1484 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1485 | LogError("Failure: MQTT_CLIENT_ON_SUBSCRIBE_ACK SUBSCRIBE_ACK parameter is NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 1486 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1487 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1488 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1489 | case MQTT_CLIENT_ON_PUBLISH_RECV: |
AzureIoTClient | 11:31ebaeb51e1e | 1490 | case MQTT_CLIENT_ON_PUBLISH_REL: |
AzureIoTClient | 11:31ebaeb51e1e | 1491 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1492 | // Currently not used |
AzureIoTClient | 11:31ebaeb51e1e | 1493 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1494 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1495 | case MQTT_CLIENT_ON_DISCONNECT: |
AzureIoTClient | 11:31ebaeb51e1e | 1496 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1497 | // Close the client so we can reconnect again |
AzureIoTClient | 20:594780be216d | 1498 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 11:31ebaeb51e1e | 1499 | transport_data->currPacketState = DISCONNECT_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 1500 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1501 | } |
AzureIoTClient | 17:774695cb8554 | 1502 | case MQTT_CLIENT_ON_UNSUBSCRIBE_ACK: |
AzureIoTClient | 26:0dae84ecce27 | 1503 | case MQTT_CLIENT_ON_PING_RESPONSE: |
AzureIoTClient | 26:0dae84ecce27 | 1504 | default: |
AzureIoTClient | 17:774695cb8554 | 1505 | { |
AzureIoTClient | 17:774695cb8554 | 1506 | break; |
AzureIoTClient | 26:0dae84ecce27 | 1507 | } |
AzureIoTClient | 12:658ca6865de2 | 1508 | } |
AzureIoTClient | 12:658ca6865de2 | 1509 | } |
AzureIoTClient | 12:658ca6865de2 | 1510 | } |
AzureIoTClient | 12:658ca6865de2 | 1511 | |
AzureIoTClient | 37:e6a799428f3d | 1512 | // Prior to creating a new connection, if we have an existing xioTransport we need to clear |
AzureIoTClient | 37:e6a799428f3d | 1513 | // it now or else cached settings (especially TLS when communicating with HTTP proxies) will |
AzureIoTClient | 37:e6a799428f3d | 1514 | // break reconnection attempt. |
AzureIoTClient | 37:e6a799428f3d | 1515 | static void ResetConnectionIfNecessary(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 37:e6a799428f3d | 1516 | { |
AzureIoTClient | 37:e6a799428f3d | 1517 | if (transport_data->xioTransport != NULL) |
AzureIoTClient | 37:e6a799428f3d | 1518 | { |
AzureIoTClient | 37:e6a799428f3d | 1519 | OPTIONHANDLER_HANDLE options = xio_retrieveoptions(transport_data->xioTransport); |
AzureIoTClient | 37:e6a799428f3d | 1520 | set_saved_tls_options(transport_data, options); |
AzureIoTClient | 37:e6a799428f3d | 1521 | |
AzureIoTClient | 37:e6a799428f3d | 1522 | xio_destroy(transport_data->xioTransport); |
AzureIoTClient | 37:e6a799428f3d | 1523 | transport_data->xioTransport = NULL; |
AzureIoTClient | 37:e6a799428f3d | 1524 | } |
AzureIoTClient | 37:e6a799428f3d | 1525 | } |
AzureIoTClient | 37:e6a799428f3d | 1526 | |
AzureIoTClient | 25:a35763780a87 | 1527 | static void DisconnectFromClient(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 25:a35763780a87 | 1528 | { |
AzureIoTClient | 33:b7dfb208ef0a | 1529 | if (!transport_data->isDestroyCalled) |
AzureIoTClient | 33:b7dfb208ef0a | 1530 | { |
AzureIoTClient | 33:b7dfb208ef0a | 1531 | OPTIONHANDLER_HANDLE options = xio_retrieveoptions(transport_data->xioTransport); |
AzureIoTClient | 33:b7dfb208ef0a | 1532 | set_saved_tls_options(transport_data, options); |
AzureIoTClient | 33:b7dfb208ef0a | 1533 | } |
AzureIoTClient | 37:e6a799428f3d | 1534 | |
AzureIoTClient | 28:0cd355c3294e | 1535 | (void)mqtt_client_disconnect(transport_data->mqttClient, NULL, NULL); |
AzureIoTClient | 25:a35763780a87 | 1536 | xio_destroy(transport_data->xioTransport); |
AzureIoTClient | 25:a35763780a87 | 1537 | transport_data->xioTransport = NULL; |
AzureIoTClient | 25:a35763780a87 | 1538 | |
AzureIoTClient | 25:a35763780a87 | 1539 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 25:a35763780a87 | 1540 | transport_data->currPacketState = DISCONNECT_TYPE; |
AzureIoTClient | 25:a35763780a87 | 1541 | } |
AzureIoTClient | 25:a35763780a87 | 1542 | |
AzureIoTClient | 12:658ca6865de2 | 1543 | static void mqtt_error_callback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_ERROR error, void* callbackCtx) |
AzureIoTClient | 12:658ca6865de2 | 1544 | { |
AzureIoTClient | 12:658ca6865de2 | 1545 | (void)handle; |
AzureIoTClient | 12:658ca6865de2 | 1546 | if (callbackCtx != NULL) |
AzureIoTClient | 12:658ca6865de2 | 1547 | { |
AzureIoTClient | 12:658ca6865de2 | 1548 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx; |
AzureIoTClient | 12:658ca6865de2 | 1549 | switch (error) |
AzureIoTClient | 12:658ca6865de2 | 1550 | { |
AzureIoTClient | 12:658ca6865de2 | 1551 | case MQTT_CLIENT_CONNECTION_ERROR: |
AzureIoTClient | 11:31ebaeb51e1e | 1552 | { |
AzureIoTClient | 39:6231984e0179 | 1553 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_NO_NETWORK); |
AzureIoTClient | 12:658ca6865de2 | 1554 | break; |
AzureIoTClient | 12:658ca6865de2 | 1555 | } |
AzureIoTClient | 20:594780be216d | 1556 | case MQTT_CLIENT_COMMUNICATION_ERROR: |
AzureIoTClient | 20:594780be216d | 1557 | { |
AzureIoTClient | 39:6231984e0179 | 1558 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR); |
AzureIoTClient | 20:594780be216d | 1559 | break; |
AzureIoTClient | 20:594780be216d | 1560 | } |
AzureIoTClient | 12:658ca6865de2 | 1561 | case MQTT_CLIENT_NO_PING_RESPONSE: |
AzureIoTClient | 12:658ca6865de2 | 1562 | { |
AzureIoTClient | 12:658ca6865de2 | 1563 | LogError("Mqtt Ping Response was not encountered. Reconnecting device..."); |
AzureIoTClient | 25:a35763780a87 | 1564 | DisconnectFromClient(transport_data); |
AzureIoTClient | 12:658ca6865de2 | 1565 | break; |
AzureIoTClient | 11:31ebaeb51e1e | 1566 | } |
AzureIoTClient | 17:774695cb8554 | 1567 | case MQTT_CLIENT_PARSE_ERROR: |
AzureIoTClient | 17:774695cb8554 | 1568 | case MQTT_CLIENT_MEMORY_ERROR: |
AzureIoTClient | 17:774695cb8554 | 1569 | case MQTT_CLIENT_UNKNOWN_ERROR: |
AzureIoTClient | 17:774695cb8554 | 1570 | { |
AzureIoTClient | 17:774695cb8554 | 1571 | LogError("INTERNAL ERROR: unexpected error value received %s", ENUM_TO_STRING(MQTT_CLIENT_EVENT_ERROR, error)); |
AzureIoTClient | 20:594780be216d | 1572 | break; |
AzureIoTClient | 17:774695cb8554 | 1573 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1574 | } |
AzureIoTClient | 33:b7dfb208ef0a | 1575 | if (transport_data->mqttClientStatus != MQTT_CLIENT_STATUS_PENDING_CLOSE) |
AzureIoTClient | 33:b7dfb208ef0a | 1576 | { |
AzureIoTClient | 33:b7dfb208ef0a | 1577 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 33:b7dfb208ef0a | 1578 | } |
AzureIoTClient | 12:658ca6865de2 | 1579 | transport_data->currPacketState = PACKET_TYPE_ERROR; |
Azure.IoT.Build | 13:606465879c57 | 1580 | transport_data->device_twin_get_sent = false; |
AzureIoTClient | 12:658ca6865de2 | 1581 | if (transport_data->topic_MqttMessage != NULL) |
AzureIoTClient | 12:658ca6865de2 | 1582 | { |
Azure.IoT.Build | 13:606465879c57 | 1583 | transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC; |
Azure.IoT.Build | 13:606465879c57 | 1584 | } |
Azure.IoT.Build | 13:606465879c57 | 1585 | if (transport_data->topic_GetState != NULL) |
Azure.IoT.Build | 13:606465879c57 | 1586 | { |
Azure.IoT.Build | 13:606465879c57 | 1587 | transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 1588 | } |
Azure.IoT.Build | 13:606465879c57 | 1589 | if (transport_data->topic_NotifyState != NULL) |
Azure.IoT.Build | 13:606465879c57 | 1590 | { |
Azure.IoT.Build | 13:606465879c57 | 1591 | transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC; |
Azure.IoT.Build | 13:606465879c57 | 1592 | } |
Azure.IoT.Build | 13:606465879c57 | 1593 | if (transport_data->topic_DeviceMethods != NULL) |
Azure.IoT.Build | 13:606465879c57 | 1594 | { |
Azure.IoT.Build | 13:606465879c57 | 1595 | transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC; |
Azure.IoT.Build | 13:606465879c57 | 1596 | } |
Azure.IoT.Build | 13:606465879c57 | 1597 | } |
AzureIoTClient | 12:658ca6865de2 | 1598 | else |
AzureIoTClient | 12:658ca6865de2 | 1599 | { |
AzureIoTClient | 12:658ca6865de2 | 1600 | LogError("Failure: mqtt called back with null context."); |
AzureIoTClient | 11:31ebaeb51e1e | 1601 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1602 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1603 | |
AzureIoTClient | 11:31ebaeb51e1e | 1604 | static void SubscribeToMqttProtocol(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 11:31ebaeb51e1e | 1605 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1606 | if (transport_data->topics_ToSubscribe != UNSUBSCRIBE_FROM_TOPIC) |
AzureIoTClient | 11:31ebaeb51e1e | 1607 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1608 | uint32_t topic_subscription = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1609 | size_t subscribe_count = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1610 | SUBSCRIBE_PAYLOAD subscribe[SUBSCRIBE_TOPIC_COUNT]; |
AzureIoTClient | 11:31ebaeb51e1e | 1611 | if ((transport_data->topic_MqttMessage != NULL) && (SUBSCRIBE_TELEMETRY_TOPIC & transport_data->topics_ToSubscribe)) |
AzureIoTClient | 11:31ebaeb51e1e | 1612 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1613 | subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_MqttMessage); |
AzureIoTClient | 11:31ebaeb51e1e | 1614 | subscribe[subscribe_count].qosReturn = DELIVER_AT_LEAST_ONCE; |
AzureIoTClient | 11:31ebaeb51e1e | 1615 | topic_subscription |= SUBSCRIBE_TELEMETRY_TOPIC; |
AzureIoTClient | 11:31ebaeb51e1e | 1616 | subscribe_count++; |
AzureIoTClient | 11:31ebaeb51e1e | 1617 | } |
AzureIoTClient | 12:658ca6865de2 | 1618 | if ((transport_data->topic_GetState != NULL) && (SUBSCRIBE_GET_REPORTED_STATE_TOPIC & transport_data->topics_ToSubscribe)) |
AzureIoTClient | 12:658ca6865de2 | 1619 | { |
AzureIoTClient | 12:658ca6865de2 | 1620 | subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_GetState); |
AzureIoTClient | 12:658ca6865de2 | 1621 | subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE; |
AzureIoTClient | 12:658ca6865de2 | 1622 | topic_subscription |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 1623 | subscribe_count++; |
AzureIoTClient | 12:658ca6865de2 | 1624 | } |
AzureIoTClient | 12:658ca6865de2 | 1625 | if ((transport_data->topic_NotifyState != NULL) && (SUBSCRIBE_NOTIFICATION_STATE_TOPIC & transport_data->topics_ToSubscribe)) |
AzureIoTClient | 12:658ca6865de2 | 1626 | { |
AzureIoTClient | 12:658ca6865de2 | 1627 | subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_NotifyState); |
AzureIoTClient | 12:658ca6865de2 | 1628 | subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE; |
AzureIoTClient | 12:658ca6865de2 | 1629 | topic_subscription |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 1630 | subscribe_count++; |
AzureIoTClient | 12:658ca6865de2 | 1631 | } |
AzureIoTClient | 12:658ca6865de2 | 1632 | if ((transport_data->topic_DeviceMethods != NULL) && (SUBSCRIBE_DEVICE_METHOD_TOPIC & transport_data->topics_ToSubscribe)) |
AzureIoTClient | 12:658ca6865de2 | 1633 | { |
AzureIoTClient | 12:658ca6865de2 | 1634 | subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_DeviceMethods); |
AzureIoTClient | 12:658ca6865de2 | 1635 | subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE; |
AzureIoTClient | 12:658ca6865de2 | 1636 | topic_subscription |= SUBSCRIBE_DEVICE_METHOD_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 1637 | subscribe_count++; |
AzureIoTClient | 12:658ca6865de2 | 1638 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1639 | |
AzureIoTClient | 11:31ebaeb51e1e | 1640 | if (subscribe_count != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1641 | { |
AzureIoTClient | 12:658ca6865de2 | 1642 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransport_MQTT_Common_Subscribe shall call mqtt_client_subscribe to subscribe to the Message Topic.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1643 | if (mqtt_client_subscribe(transport_data->mqttClient, get_next_packet_id(transport_data), subscribe, subscribe_count) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1644 | { |
AzureIoTClient | 12:658ca6865de2 | 1645 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1646 | LogError("Failure: mqtt_client_subscribe returned error."); |
AzureIoTClient | 11:31ebaeb51e1e | 1647 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1648 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1649 | { |
AzureIoTClient | 12:658ca6865de2 | 1650 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_018: [On success IoTHubTransport_MQTT_Common_Subscribe shall return 0.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1651 | transport_data->topics_ToSubscribe &= ~topic_subscription; |
AzureIoTClient | 11:31ebaeb51e1e | 1652 | transport_data->currPacketState = SUBSCRIBE_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 1653 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1654 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1655 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1656 | { |
AzureIoTClient | 12:658ca6865de2 | 1657 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 1658 | LogError("Failure: subscribe_topic is empty."); |
AzureIoTClient | 11:31ebaeb51e1e | 1659 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1660 | transport_data->currPacketState = SUBSCRIBE_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 1661 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1662 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1663 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1664 | transport_data->currPacketState = PUBLISH_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 1665 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1666 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1667 | |
AzureIoTClient | 11:31ebaeb51e1e | 1668 | static const unsigned char* RetrieveMessagePayload(IOTHUB_MESSAGE_HANDLE messageHandle, size_t* length) |
AzureIoTClient | 11:31ebaeb51e1e | 1669 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1670 | const unsigned char* result; |
AzureIoTClient | 11:31ebaeb51e1e | 1671 | |
AzureIoTClient | 11:31ebaeb51e1e | 1672 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle); |
AzureIoTClient | 11:31ebaeb51e1e | 1673 | if (contentType == IOTHUBMESSAGE_BYTEARRAY) |
AzureIoTClient | 11:31ebaeb51e1e | 1674 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1675 | if (IoTHubMessage_GetByteArray(messageHandle, &result, length) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 11:31ebaeb51e1e | 1676 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1677 | LogError("Failure result from IoTHubMessage_GetByteArray"); |
AzureIoTClient | 11:31ebaeb51e1e | 1678 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 1679 | *length = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1680 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1681 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1682 | else if (contentType == IOTHUBMESSAGE_STRING) |
AzureIoTClient | 11:31ebaeb51e1e | 1683 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1684 | result = (const unsigned char*)IoTHubMessage_GetString(messageHandle); |
AzureIoTClient | 11:31ebaeb51e1e | 1685 | if (result == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1686 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1687 | LogError("Failure result from IoTHubMessage_GetString"); |
AzureIoTClient | 11:31ebaeb51e1e | 1688 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 1689 | *length = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1690 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1691 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1692 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1693 | *length = strlen((const char*)result); |
AzureIoTClient | 11:31ebaeb51e1e | 1694 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1695 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1696 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1697 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1698 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 1699 | *length = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1700 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1701 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 1702 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1703 | |
AzureIoTClient | 11:31ebaeb51e1e | 1704 | static int GetTransportProviderIfNecessary(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 11:31ebaeb51e1e | 1705 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1706 | int result; |
AzureIoTClient | 11:31ebaeb51e1e | 1707 | |
AzureIoTClient | 11:31ebaeb51e1e | 1708 | if (transport_data->xioTransport == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1709 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1710 | // construct address |
AzureIoTClient | 11:31ebaeb51e1e | 1711 | const char* hostAddress = STRING_c_str(transport_data->hostAddress); |
AzureIoTClient | 20:594780be216d | 1712 | MQTT_TRANSPORT_PROXY_OPTIONS mqtt_proxy_options; |
AzureIoTClient | 20:594780be216d | 1713 | |
AzureIoTClient | 20:594780be216d | 1714 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_011: [ If no `proxy_data` option has been set, NULL shall be passed as the argument `mqtt_transport_proxy_options` when calling the function `get_io_transport` passed in `IoTHubTransport_MQTT_Common__Create`. ]*/ |
AzureIoTClient | 20:594780be216d | 1715 | mqtt_proxy_options.host_address = transport_data->http_proxy_hostname; |
AzureIoTClient | 20:594780be216d | 1716 | mqtt_proxy_options.port = transport_data->http_proxy_port; |
AzureIoTClient | 20:594780be216d | 1717 | mqtt_proxy_options.username = transport_data->http_proxy_username; |
AzureIoTClient | 20:594780be216d | 1718 | mqtt_proxy_options.password = transport_data->http_proxy_password; |
AzureIoTClient | 20:594780be216d | 1719 | |
AzureIoTClient | 20:594780be216d | 1720 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_010: [ If the `proxy_data` option has been set, the proxy options shall be filled in the argument `mqtt_transport_proxy_options` when calling the function `get_io_transport` passed in `IoTHubTransport_MQTT_Common__Create` to obtain the underlying IO handle. ]*/ |
AzureIoTClient | 20:594780be216d | 1721 | transport_data->xioTransport = transport_data->get_io_transport(hostAddress, (transport_data->http_proxy_hostname == NULL) ? NULL : &mqtt_proxy_options); |
AzureIoTClient | 11:31ebaeb51e1e | 1722 | if (transport_data->xioTransport == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1723 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1724 | LogError("Unable to create the lower level TLS layer."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1725 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1726 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1727 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1728 | { |
AzureIoTClient | 37:e6a799428f3d | 1729 | if (transport_data->saved_tls_options != NULL) |
AzureIoTClient | 37:e6a799428f3d | 1730 | { |
AzureIoTClient | 37:e6a799428f3d | 1731 | if (OptionHandler_FeedOptions(transport_data->saved_tls_options, transport_data->xioTransport) != OPTIONHANDLER_OK) |
AzureIoTClient | 37:e6a799428f3d | 1732 | { |
AzureIoTClient | 37:e6a799428f3d | 1733 | LogError("Failed feeding existing options to new TLS instance."); |
AzureIoTClient | 37:e6a799428f3d | 1734 | result = __FAILURE__; |
AzureIoTClient | 37:e6a799428f3d | 1735 | } |
AzureIoTClient | 37:e6a799428f3d | 1736 | else |
AzureIoTClient | 37:e6a799428f3d | 1737 | { |
AzureIoTClient | 37:e6a799428f3d | 1738 | // The tlsio has the options, so our copy can be deleted |
AzureIoTClient | 37:e6a799428f3d | 1739 | set_saved_tls_options(transport_data, NULL); |
AzureIoTClient | 37:e6a799428f3d | 1740 | result = 0; |
AzureIoTClient | 37:e6a799428f3d | 1741 | } |
AzureIoTClient | 37:e6a799428f3d | 1742 | } |
AzureIoTClient | 37:e6a799428f3d | 1743 | else if (IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module) == IOTHUB_CREDENTIAL_TYPE_X509_ECC) |
AzureIoTClient | 28:0cd355c3294e | 1744 | { |
AzureIoTClient | 28:0cd355c3294e | 1745 | if (IoTHubClient_Auth_Set_xio_Certificate(transport_data->authorization_module, transport_data->xioTransport) != 0) |
AzureIoTClient | 28:0cd355c3294e | 1746 | { |
AzureIoTClient | 28:0cd355c3294e | 1747 | LogError("Unable to create the lower level TLS layer."); |
AzureIoTClient | 28:0cd355c3294e | 1748 | result = __FAILURE__; |
AzureIoTClient | 28:0cd355c3294e | 1749 | } |
AzureIoTClient | 28:0cd355c3294e | 1750 | else |
AzureIoTClient | 28:0cd355c3294e | 1751 | { |
AzureIoTClient | 28:0cd355c3294e | 1752 | result = 0; |
AzureIoTClient | 28:0cd355c3294e | 1753 | } |
AzureIoTClient | 28:0cd355c3294e | 1754 | } |
AzureIoTClient | 28:0cd355c3294e | 1755 | else |
AzureIoTClient | 28:0cd355c3294e | 1756 | { |
AzureIoTClient | 28:0cd355c3294e | 1757 | result = 0; |
AzureIoTClient | 28:0cd355c3294e | 1758 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1759 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1760 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1761 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1762 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1763 | result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1764 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1765 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 1766 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1767 | |
AzureIoTClient | 36:3b9944257dd5 | 1768 | //static int is_key_validate(const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 36:3b9944257dd5 | 1769 | //{ |
AzureIoTClient | 36:3b9944257dd5 | 1770 | // int result; |
AzureIoTClient | 36:3b9944257dd5 | 1771 | // IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(config->auth_module_handle); |
AzureIoTClient | 36:3b9944257dd5 | 1772 | // if (cred_type == IOTHUB_CREDENTIAL_TYPE_X509 || cred_type == IOTHUB_CREDENTIAL_TYPE_X509_ECC) |
AzureIoTClient | 36:3b9944257dd5 | 1773 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1774 | // result = 0; |
AzureIoTClient | 36:3b9944257dd5 | 1775 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1776 | // else |
AzureIoTClient | 36:3b9944257dd5 | 1777 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1778 | // if (config->upperConfig->deviceKey == NULL && config->upperConfig->deviceSasToken == NULL) |
AzureIoTClient | 36:3b9944257dd5 | 1779 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1780 | // if (IoTHubClient_Auth_Get_DeviceKey(config->auth_module_handle) == NULL) |
AzureIoTClient | 36:3b9944257dd5 | 1781 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1782 | // result = __FAILURE__; |
AzureIoTClient | 36:3b9944257dd5 | 1783 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1784 | // else |
AzureIoTClient | 36:3b9944257dd5 | 1785 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1786 | // result = 0; |
AzureIoTClient | 36:3b9944257dd5 | 1787 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1788 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1789 | // else |
AzureIoTClient | 36:3b9944257dd5 | 1790 | // { |
AzureIoTClient | 36:3b9944257dd5 | 1791 | // result = 0; |
AzureIoTClient | 36:3b9944257dd5 | 1792 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1793 | // } |
AzureIoTClient | 36:3b9944257dd5 | 1794 | // return result; |
AzureIoTClient | 36:3b9944257dd5 | 1795 | //} |
AzureIoTClient | 28:0cd355c3294e | 1796 | |
AzureIoTClient | 11:31ebaeb51e1e | 1797 | static int SendMqttConnectMsg(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 11:31ebaeb51e1e | 1798 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1799 | int result; |
AzureIoTClient | 11:31ebaeb51e1e | 1800 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1801 | char* sasToken = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 1802 | result = 0; |
AzureIoTClient | 21:167f8fcf6fb1 | 1803 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1804 | IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module); |
AzureIoTClient | 28:0cd355c3294e | 1805 | if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY || cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH) |
AzureIoTClient | 11:31ebaeb51e1e | 1806 | { |
AzureIoTClient | 39:6231984e0179 | 1807 | sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, STRING_c_str(transport_data->devicesPath), transport_data->option_sas_token_lifetime_secs, NULL); |
AzureIoTClient | 21:167f8fcf6fb1 | 1808 | if (sasToken == NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 1809 | { |
AzureIoTClient | 28:0cd355c3294e | 1810 | LogError("failure getting sas token from IoTHubClient_Auth_Get_SasToken."); |
AzureIoTClient | 21:167f8fcf6fb1 | 1811 | result = __FAILURE__; |
AzureIoTClient | 21:167f8fcf6fb1 | 1812 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1813 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1814 | else if (cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) |
AzureIoTClient | 11:31ebaeb51e1e | 1815 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1816 | SAS_TOKEN_STATUS token_status = IoTHubClient_Auth_Is_SasToken_Valid(transport_data->authorization_module); |
AzureIoTClient | 21:167f8fcf6fb1 | 1817 | if (token_status == SAS_TOKEN_STATUS_INVALID) |
AzureIoTClient | 21:167f8fcf6fb1 | 1818 | { |
AzureIoTClient | 39:6231984e0179 | 1819 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN); |
AzureIoTClient | 21:167f8fcf6fb1 | 1820 | result = __FAILURE__; |
AzureIoTClient | 21:167f8fcf6fb1 | 1821 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1822 | else if (token_status == SAS_TOKEN_STATUS_FAILED) |
AzureIoTClient | 11:31ebaeb51e1e | 1823 | { |
AzureIoTClient | 39:6231984e0179 | 1824 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL); |
AzureIoTClient | 21:167f8fcf6fb1 | 1825 | result = __FAILURE__; |
AzureIoTClient | 21:167f8fcf6fb1 | 1826 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1827 | else |
AzureIoTClient | 21:167f8fcf6fb1 | 1828 | { |
AzureIoTClient | 39:6231984e0179 | 1829 | sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, NULL, 0, NULL); |
AzureIoTClient | 21:167f8fcf6fb1 | 1830 | if (sasToken == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1831 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1832 | LogError("failure getting sas Token."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 1833 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 1834 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1835 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1836 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1837 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1838 | if (result == 0) |
AzureIoTClient | 21:167f8fcf6fb1 | 1839 | { |
AzureIoTClient | 24:4096249decf1 | 1840 | if (!transport_data->isProductInfoSet) |
AzureIoTClient | 22:07fec4d325b6 | 1841 | { |
AzureIoTClient | 24:4096249decf1 | 1842 | // This requires the iothubClientHandle, which sadly the MQTT transport only gets on DoWork, so this code still needs to remain here. |
AzureIoTClient | 24:4096249decf1 | 1843 | // The correct place for this would be in the Create method, but we don't get the client handle there. |
AzureIoTClient | 24:4096249decf1 | 1844 | // Also, when device multiplexing is used, the customer creates the transport directly and explicitly, when the client is still not created. |
AzureIoTClient | 24:4096249decf1 | 1845 | // This will be a major hurdle when we add device multiplexing to MQTT transport. |
AzureIoTClient | 24:4096249decf1 | 1846 | |
AzureIoTClient | 24:4096249decf1 | 1847 | void* product_info; |
AzureIoTClient | 24:4096249decf1 | 1848 | STRING_HANDLE clone; |
AzureIoTClient | 39:6231984e0179 | 1849 | if ((IoTHubClientCore_LL_GetOption(transport_data->llClientHandle, OPTION_PRODUCT_INFO, &product_info) == IOTHUB_CLIENT_ERROR) || (product_info == NULL)) |
AzureIoTClient | 24:4096249decf1 | 1850 | { |
AzureIoTClient | 24:4096249decf1 | 1851 | clone = STRING_construct_sprintf("%s%%2F%s", CLIENT_DEVICE_TYPE_PREFIX, IOTHUB_SDK_VERSION); |
AzureIoTClient | 24:4096249decf1 | 1852 | } |
AzureIoTClient | 24:4096249decf1 | 1853 | else |
AzureIoTClient | 24:4096249decf1 | 1854 | { |
AzureIoTClient | 24:4096249decf1 | 1855 | clone = URL_Encode(product_info); |
AzureIoTClient | 24:4096249decf1 | 1856 | } |
AzureIoTClient | 24:4096249decf1 | 1857 | |
AzureIoTClient | 24:4096249decf1 | 1858 | if (clone == NULL) |
AzureIoTClient | 24:4096249decf1 | 1859 | { |
AzureIoTClient | 24:4096249decf1 | 1860 | LogError("Failed obtaining the product info"); |
AzureIoTClient | 24:4096249decf1 | 1861 | } |
AzureIoTClient | 24:4096249decf1 | 1862 | else |
AzureIoTClient | 24:4096249decf1 | 1863 | { |
AzureIoTClient | 24:4096249decf1 | 1864 | if (STRING_concat_with_STRING(transport_data->configPassedThroughUsername, clone) != 0) |
AzureIoTClient | 24:4096249decf1 | 1865 | { |
AzureIoTClient | 24:4096249decf1 | 1866 | LogError("Failed concatenating the product info"); |
AzureIoTClient | 24:4096249decf1 | 1867 | } |
AzureIoTClient | 24:4096249decf1 | 1868 | else |
AzureIoTClient | 24:4096249decf1 | 1869 | { |
AzureIoTClient | 24:4096249decf1 | 1870 | transport_data->isProductInfoSet = true; |
AzureIoTClient | 24:4096249decf1 | 1871 | } |
AzureIoTClient | 24:4096249decf1 | 1872 | |
AzureIoTClient | 24:4096249decf1 | 1873 | STRING_delete(clone); |
AzureIoTClient | 24:4096249decf1 | 1874 | } |
AzureIoTClient | 22:07fec4d325b6 | 1875 | } |
AzureIoTClient | 22:07fec4d325b6 | 1876 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1877 | MQTT_CLIENT_OPTIONS options = { 0 }; |
AzureIoTClient | 21:167f8fcf6fb1 | 1878 | options.clientId = (char*)STRING_c_str(transport_data->device_id); |
AzureIoTClient | 21:167f8fcf6fb1 | 1879 | options.willMessage = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 1880 | options.username = (char*)STRING_c_str(transport_data->configPassedThroughUsername); |
AzureIoTClient | 21:167f8fcf6fb1 | 1881 | if (sasToken != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1882 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1883 | options.password = sasToken; |
AzureIoTClient | 21:167f8fcf6fb1 | 1884 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1885 | options.keepAliveInterval = transport_data->keepAliveValue; |
AzureIoTClient | 21:167f8fcf6fb1 | 1886 | options.useCleanSession = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 1887 | options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE; |
AzureIoTClient | 11:31ebaeb51e1e | 1888 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1889 | if (GetTransportProviderIfNecessary(transport_data) == 0) |
AzureIoTClient | 21:167f8fcf6fb1 | 1890 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1891 | if (mqtt_client_connect(transport_data->mqttClient, transport_data->xioTransport, &options) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1892 | { |
AzureIoTClient | 24:4096249decf1 | 1893 | LogError("failure connecting to address %s.", STRING_c_str(transport_data->hostAddress)); |
AzureIoTClient | 21:167f8fcf6fb1 | 1894 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1895 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1896 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1897 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1898 | (void)tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->mqtt_connect_time); |
AzureIoTClient | 21:167f8fcf6fb1 | 1899 | result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1900 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1901 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1902 | else |
AzureIoTClient | 21:167f8fcf6fb1 | 1903 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1904 | result = __FAILURE__; |
AzureIoTClient | 21:167f8fcf6fb1 | 1905 | } |
AzureIoTClient | 12:658ca6865de2 | 1906 | |
AzureIoTClient | 21:167f8fcf6fb1 | 1907 | if (sasToken != NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 1908 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 1909 | free(sasToken); |
AzureIoTClient | 11:31ebaeb51e1e | 1910 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1911 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1912 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 1913 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1914 | |
AzureIoTClient | 11:31ebaeb51e1e | 1915 | static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transport_data) |
AzureIoTClient | 11:31ebaeb51e1e | 1916 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1917 | int result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1918 | |
AzureIoTClient | 11:31ebaeb51e1e | 1919 | // Make sure we're not destroying the object |
AzureIoTClient | 11:31ebaeb51e1e | 1920 | if (!transport_data->isDestroyCalled) |
AzureIoTClient | 11:31ebaeb51e1e | 1921 | { |
AzureIoTClient | 25:a35763780a87 | 1922 | RETRY_ACTION retry_action = RETRY_ACTION_RETRY_LATER; |
AzureIoTClient | 25:a35763780a87 | 1923 | |
AzureIoTClient | 25:a35763780a87 | 1924 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_007: [ IoTHubTransport_MQTT_Common_DoWork shall try to reconnect according to the current retry policy set ] |
AzureIoTClient | 25:a35763780a87 | 1925 | if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_NOT_CONNECTED && transport_data->isRecoverableError && |
AzureIoTClient | 25:a35763780a87 | 1926 | (retry_control_should_retry(transport_data->retry_control_handle, &retry_action) != 0 || retry_action == RETRY_ACTION_RETRY_NOW)) |
AzureIoTClient | 11:31ebaeb51e1e | 1927 | { |
AzureIoTClient | 25:a35763780a87 | 1928 | // Note: in case retry_control_should_retry fails, the reconnection shall be attempted anyway (defaulting to policy IOTHUB_CLIENT_RETRY_IMMEDIATE). |
AzureIoTClient | 25:a35763780a87 | 1929 | |
Azure.IoT.Build | 13:606465879c57 | 1930 | if (tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->connectTick) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1931 | { |
AzureIoTClient | 12:658ca6865de2 | 1932 | transport_data->connectFailCount++; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1933 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1934 | } |
AzureIoTClient | 12:658ca6865de2 | 1935 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1936 | { |
AzureIoTClient | 37:e6a799428f3d | 1937 | ResetConnectionIfNecessary(transport_data); |
AzureIoTClient | 37:e6a799428f3d | 1938 | |
AzureIoTClient | 12:658ca6865de2 | 1939 | if (SendMqttConnectMsg(transport_data) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1940 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1941 | transport_data->connectFailCount++; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1942 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1943 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1944 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1945 | { |
AzureIoTClient | 20:594780be216d | 1946 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_CONNECTING; |
AzureIoTClient | 12:658ca6865de2 | 1947 | transport_data->connectFailCount = 0; |
AzureIoTClient | 12:658ca6865de2 | 1948 | result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 1949 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1950 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1951 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 1952 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_001: [ IoTHubTransport_MQTT_Common_DoWork shall trigger reconnection if the mqtt_client_connect does not complete within `keepalive` seconds] |
AzureIoTClient | 20:594780be216d | 1953 | else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTING) |
AzureIoTClient | 11:31ebaeb51e1e | 1954 | { |
AzureIoTClient | 20:594780be216d | 1955 | tickcounter_ms_t current_time; |
AzureIoTClient | 20:594780be216d | 1956 | if (tickcounter_get_current_ms(transport_data->msgTickCounter, ¤t_time) != 0) |
AzureIoTClient | 20:594780be216d | 1957 | { |
AzureIoTClient | 20:594780be216d | 1958 | LogError("failed verifying MQTT_CLIENT_STATUS_CONNECTING timeout"); |
AzureIoTClient | 20:594780be216d | 1959 | result = __FAILURE__; |
AzureIoTClient | 20:594780be216d | 1960 | } |
AzureIoTClient | 32:103a46ed8822 | 1961 | else if ((current_time - transport_data->mqtt_connect_time) / 1000 > transport_data->connect_timeout_in_sec) |
AzureIoTClient | 20:594780be216d | 1962 | { |
AzureIoTClient | 20:594780be216d | 1963 | LogError("mqtt_client timed out waiting for CONNACK"); |
AzureIoTClient | 20:594780be216d | 1964 | DisconnectFromClient(transport_data); |
AzureIoTClient | 38:369a06de92aa | 1965 | result = __FAILURE__; |
AzureIoTClient | 20:594780be216d | 1966 | } |
AzureIoTClient | 20:594780be216d | 1967 | } |
AzureIoTClient | 20:594780be216d | 1968 | else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTED) |
AzureIoTClient | 20:594780be216d | 1969 | { |
AzureIoTClient | 20:594780be216d | 1970 | // We are connected and not being closed, so does SAS need to reconnect? |
Azure.IoT.Build | 13:606465879c57 | 1971 | tickcounter_ms_t current_time; |
Azure.IoT.Build | 13:606465879c57 | 1972 | if (tickcounter_get_current_ms(transport_data->msgTickCounter, ¤t_time) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 1973 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1974 | transport_data->connectFailCount++; |
AzureIoTClient | 18:ec8e5e97c6a4 | 1975 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 1976 | } |
AzureIoTClient | 11:31ebaeb51e1e | 1977 | else |
AzureIoTClient | 11:31ebaeb51e1e | 1978 | { |
AzureIoTClient | 29:923be0c3998a | 1979 | if ((current_time - transport_data->mqtt_connect_time) / 1000 > (transport_data->option_sas_token_lifetime_secs*SAS_REFRESH_MULTIPLIER)) |
AzureIoTClient | 11:31ebaeb51e1e | 1980 | { |
AzureIoTClient | 27:04de3c0bf1db | 1981 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_058: [ If the sas token has timed out IoTHubTransport_MQTT_Common_DoWork shall disconnect from the mqtt client and destroy the transport information and wait for reconnect. ] */ |
AzureIoTClient | 28:0cd355c3294e | 1982 | OPTIONHANDLER_HANDLE options = xio_retrieveoptions(transport_data->xioTransport); |
AzureIoTClient | 28:0cd355c3294e | 1983 | set_saved_tls_options(transport_data, options); |
AzureIoTClient | 28:0cd355c3294e | 1984 | (void)mqtt_client_disconnect(transport_data->mqttClient, NULL, NULL); |
AzureIoTClient | 27:04de3c0bf1db | 1985 | xio_destroy(transport_data->xioTransport); |
AzureIoTClient | 27:04de3c0bf1db | 1986 | transport_data->xioTransport = NULL; |
AzureIoTClient | 27:04de3c0bf1db | 1987 | |
AzureIoTClient | 39:6231984e0179 | 1988 | IoTHubClientCore_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN); |
AzureIoTClient | 20:594780be216d | 1989 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 11:31ebaeb51e1e | 1990 | transport_data->currPacketState = UNKNOWN_TYPE; |
AzureIoTClient | 12:658ca6865de2 | 1991 | transport_data->device_twin_get_sent = false; |
AzureIoTClient | 11:31ebaeb51e1e | 1992 | if (transport_data->topic_MqttMessage != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 1993 | { |
AzureIoTClient | 11:31ebaeb51e1e | 1994 | transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC; |
AzureIoTClient | 11:31ebaeb51e1e | 1995 | } |
AzureIoTClient | 12:658ca6865de2 | 1996 | if (transport_data->topic_GetState != NULL) |
AzureIoTClient | 12:658ca6865de2 | 1997 | { |
AzureIoTClient | 12:658ca6865de2 | 1998 | transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 1999 | } |
AzureIoTClient | 12:658ca6865de2 | 2000 | if (transport_data->topic_NotifyState != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2001 | { |
AzureIoTClient | 12:658ca6865de2 | 2002 | transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2003 | } |
AzureIoTClient | 12:658ca6865de2 | 2004 | if (transport_data->topic_DeviceMethods != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2005 | { |
AzureIoTClient | 12:658ca6865de2 | 2006 | transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2007 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2008 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2009 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2010 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2011 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2012 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2013 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2014 | |
AzureIoTClient | 11:31ebaeb51e1e | 2015 | static STRING_HANDLE buildConfigForUsername(const IOTHUB_CLIENT_CONFIG* upperConfig) |
AzureIoTClient | 11:31ebaeb51e1e | 2016 | { |
AzureIoTClient | 22:07fec4d325b6 | 2017 | return STRING_construct_sprintf("%s.%s/%s/api-version=%s&DeviceClientType=", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId, IOTHUB_API_VERSION); |
AzureIoTClient | 11:31ebaeb51e1e | 2018 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2019 | |
AzureIoTClient | 21:167f8fcf6fb1 | 2020 | static PMQTTTRANSPORT_HANDLE_DATA InitializeTransportHandleData(const IOTHUB_CLIENT_CONFIG* upperConfig, PDLIST_ENTRY waitingToSend, IOTHUB_AUTHORIZATION_HANDLE auth_module) |
AzureIoTClient | 11:31ebaeb51e1e | 2021 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2022 | PMQTTTRANSPORT_HANDLE_DATA state = (PMQTTTRANSPORT_HANDLE_DATA)malloc(sizeof(MQTTTRANSPORT_HANDLE_DATA)); |
AzureIoTClient | 11:31ebaeb51e1e | 2023 | if (state == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2024 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2025 | LogError("Could not create MQTT transport state. Memory allocation failed."); |
AzureIoTClient | 11:31ebaeb51e1e | 2026 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2027 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2028 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2029 | memset(state, 0, sizeof(MQTTTRANSPORT_HANDLE_DATA) ); |
AzureIoTClient | 21:167f8fcf6fb1 | 2030 | if ((state->msgTickCounter = tickcounter_create()) == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2031 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2032 | LogError("Invalid Argument: iotHubName is empty"); |
AzureIoTClient | 31:d6198e67d1eb | 2033 | free_transport_handle_data(state); |
AzureIoTClient | 21:167f8fcf6fb1 | 2034 | state = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2035 | } |
AzureIoTClient | 25:a35763780a87 | 2036 | // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_005: [ MQTT transport shall use EXPONENTIAL_WITH_BACK_OFF as default retry policy ] |
AzureIoTClient | 25:a35763780a87 | 2037 | else if ((state->retry_control_handle = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_RETRY_TIMEOUT_IN_SECONDS)) == NULL) |
AzureIoTClient | 25:a35763780a87 | 2038 | { |
AzureIoTClient | 25:a35763780a87 | 2039 | LogError("Failed creating default retry control"); |
AzureIoTClient | 31:d6198e67d1eb | 2040 | free_transport_handle_data(state); |
AzureIoTClient | 25:a35763780a87 | 2041 | state = NULL; |
AzureIoTClient | 25:a35763780a87 | 2042 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2043 | else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 2044 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2045 | LogError("failure constructing device_id."); |
AzureIoTClient | 31:d6198e67d1eb | 2046 | free_transport_handle_data(state); |
AzureIoTClient | 11:31ebaeb51e1e | 2047 | state = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2048 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2049 | else if ((state->devicesPath = STRING_construct_sprintf("%s.%s/devices/%s", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId)) == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2050 | { |
AzureIoTClient | 31:d6198e67d1eb | 2051 | LogError("failure constructing devicesPath."); |
AzureIoTClient | 31:d6198e67d1eb | 2052 | free_transport_handle_data(state); |
AzureIoTClient | 11:31ebaeb51e1e | 2053 | state = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2054 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2055 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2056 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2057 | if ( (state->topic_MqttEvent = STRING_construct_sprintf(TOPIC_DEVICE_DEVICE, upperConfig->deviceId) ) == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2058 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2059 | LogError("Could not create topic_MqttEvent for MQTT"); |
AzureIoTClient | 31:d6198e67d1eb | 2060 | free_transport_handle_data(state); |
AzureIoTClient | 11:31ebaeb51e1e | 2061 | state = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2062 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2063 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2064 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2065 | state->mqttClient = mqtt_client_init(mqtt_notification_callback, mqtt_operation_complete_callback, state, mqtt_error_callback, state); |
AzureIoTClient | 21:167f8fcf6fb1 | 2066 | if (state->mqttClient == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2067 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2068 | LogError("failure initializing mqtt client."); |
AzureIoTClient | 31:d6198e67d1eb | 2069 | free_transport_handle_data(state); |
AzureIoTClient | 11:31ebaeb51e1e | 2070 | state = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2071 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2072 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2073 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2074 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [If the upperConfig contains a valid protocolGatewayHostName value the this shall be used for the hostname, otherwise the hostname shall be constructed using the iothubname and iothubSuffix.] */ |
AzureIoTClient | 21:167f8fcf6fb1 | 2075 | if (upperConfig->protocolGatewayHostName == NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 2076 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2077 | state->hostAddress = STRING_construct_sprintf("%s.%s", upperConfig->iotHubName, upperConfig->iotHubSuffix); |
AzureIoTClient | 21:167f8fcf6fb1 | 2078 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2079 | else |
AzureIoTClient | 21:167f8fcf6fb1 | 2080 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2081 | state->hostAddress = STRING_construct(upperConfig->protocolGatewayHostName); |
AzureIoTClient | 21:167f8fcf6fb1 | 2082 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2083 | |
AzureIoTClient | 21:167f8fcf6fb1 | 2084 | if (state->hostAddress == NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 2085 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2086 | LogError("failure constructing host address."); |
AzureIoTClient | 31:d6198e67d1eb | 2087 | free_transport_handle_data(state); |
AzureIoTClient | 21:167f8fcf6fb1 | 2088 | state = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2089 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2090 | else if ((state->configPassedThroughUsername = buildConfigForUsername(upperConfig)) == NULL) |
AzureIoTClient | 21:167f8fcf6fb1 | 2091 | { |
AzureIoTClient | 31:d6198e67d1eb | 2092 | free_transport_handle_data(state); |
AzureIoTClient | 21:167f8fcf6fb1 | 2093 | state = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2094 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2095 | else |
AzureIoTClient | 21:167f8fcf6fb1 | 2096 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2097 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [IoTHubTransport_MQTT_Common_Create shall allocate memory to save its internal state where all topics, hostname, device_id, device_key, sasTokenSr and client handle shall be saved.] */ |
AzureIoTClient | 21:167f8fcf6fb1 | 2098 | DList_InitializeListHead(&(state->telemetry_waitingForAck)); |
AzureIoTClient | 21:167f8fcf6fb1 | 2099 | DList_InitializeListHead(&(state->ack_waiting_queue)); |
AzureIoTClient | 21:167f8fcf6fb1 | 2100 | state->isDestroyCalled = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 2101 | state->isRegistered = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 2102 | state->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 21:167f8fcf6fb1 | 2103 | state->device_twin_get_sent = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 2104 | state->isRecoverableError = true; |
AzureIoTClient | 21:167f8fcf6fb1 | 2105 | state->packetId = 1; |
AzureIoTClient | 21:167f8fcf6fb1 | 2106 | state->llClientHandle = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2107 | state->xioTransport = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2108 | state->portNum = 0; |
AzureIoTClient | 21:167f8fcf6fb1 | 2109 | state->waitingToSend = waitingToSend; |
AzureIoTClient | 21:167f8fcf6fb1 | 2110 | state->currPacketState = CONNECT_TYPE; |
AzureIoTClient | 21:167f8fcf6fb1 | 2111 | state->keepAliveValue = DEFAULT_MQTT_KEEPALIVE; |
AzureIoTClient | 32:103a46ed8822 | 2112 | state->connect_timeout_in_sec = DEFAULT_CONNACK_TIMEOUT; |
AzureIoTClient | 21:167f8fcf6fb1 | 2113 | state->connectFailCount = 0; |
AzureIoTClient | 21:167f8fcf6fb1 | 2114 | state->connectTick = 0; |
AzureIoTClient | 21:167f8fcf6fb1 | 2115 | state->topic_MqttMessage = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2116 | state->topic_GetState = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2117 | state->topic_NotifyState = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2118 | state->topics_ToSubscribe = UNSUBSCRIBE_FROM_TOPIC; |
AzureIoTClient | 21:167f8fcf6fb1 | 2119 | state->topic_DeviceMethods = NULL; |
AzureIoTClient | 21:167f8fcf6fb1 | 2120 | state->log_trace = state->raw_trace = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 2121 | srand((unsigned int)get_time(NULL)); |
AzureIoTClient | 21:167f8fcf6fb1 | 2122 | state->authorization_module = auth_module; |
AzureIoTClient | 24:4096249decf1 | 2123 | state->isProductInfoSet = false; |
AzureIoTClient | 29:923be0c3998a | 2124 | state->option_sas_token_lifetime_secs = SAS_TOKEN_DEFAULT_LIFETIME; |
AzureIoTClient | 37:e6a799428f3d | 2125 | state->auto_url_encode_decode = false; |
AzureIoTClient | 21:167f8fcf6fb1 | 2126 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2127 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2128 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2129 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2130 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2131 | return state; |
AzureIoTClient | 11:31ebaeb51e1e | 2132 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2133 | |
AzureIoTClient | 11:31ebaeb51e1e | 2134 | TRANSPORT_LL_HANDLE IoTHubTransport_MQTT_Common_Create(const IOTHUBTRANSPORT_CONFIG* config, MQTT_GET_IO_TRANSPORT get_io_transport) |
AzureIoTClient | 11:31ebaeb51e1e | 2135 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2136 | PMQTTTRANSPORT_HANDLE_DATA result; |
AzureIoTClient | 11:31ebaeb51e1e | 2137 | size_t deviceIdSize; |
AzureIoTClient | 11:31ebaeb51e1e | 2138 | |
AzureIoTClient | 12:658ca6865de2 | 2139 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [If parameter config is NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2140 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_041: [ if get_io_transport is NULL then IoTHubTransport_MQTT_Common_Create shall return NULL. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2141 | if (config == NULL || get_io_transport == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2142 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2143 | LogError("Invalid Argument: Config Parameter is NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 2144 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2145 | } |
AzureIoTClient | 28:0cd355c3294e | 2146 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig, auth_module_handle or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 28:0cd355c3294e | 2147 | else if (config->auth_module_handle == NULL) |
AzureIoTClient | 28:0cd355c3294e | 2148 | { |
AzureIoTClient | 28:0cd355c3294e | 2149 | LogError("Invalid Argument: auth_module_handle is NULL)"); |
AzureIoTClient | 28:0cd355c3294e | 2150 | result = NULL; |
AzureIoTClient | 28:0cd355c3294e | 2151 | } |
AzureIoTClient | 12:658ca6865de2 | 2152 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 12:658ca6865de2 | 2153 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 12:658ca6865de2 | 2154 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_003: [If both deviceKey & deviceSasToken fields are NOT NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2155 | else if (config->upperConfig == NULL || |
AzureIoTClient | 11:31ebaeb51e1e | 2156 | config->upperConfig->protocol == NULL || |
AzureIoTClient | 11:31ebaeb51e1e | 2157 | config->upperConfig->deviceId == NULL || |
AzureIoTClient | 28:0cd355c3294e | 2158 | ((config->upperConfig->deviceKey != NULL) && (config->upperConfig->deviceSasToken != NULL)) || |
AzureIoTClient | 28:0cd355c3294e | 2159 | //is_key_validate(config) != 0 || |
AzureIoTClient | 28:0cd355c3294e | 2160 | config->upperConfig->iotHubName == NULL || |
AzureIoTClient | 28:0cd355c3294e | 2161 | config->upperConfig->iotHubSuffix == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2162 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2163 | LogError("Invalid Argument: upperConfig structure contains an invalid parameter"); |
AzureIoTClient | 11:31ebaeb51e1e | 2164 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2165 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2166 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig, auth_module_handle or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2167 | else if (config->waitingToSend == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2168 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2169 | LogError("Invalid Argument: waitingToSend is NULL)"); |
AzureIoTClient | 11:31ebaeb51e1e | 2170 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2171 | } |
AzureIoTClient | 12:658ca6865de2 | 2172 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_006: [If the upperConfig's variables deviceId is an empty strings or length is greater then 128 then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2173 | else if ( ( (deviceIdSize = strlen(config->upperConfig->deviceId)) > 128U) || (deviceIdSize == 0) ) |
AzureIoTClient | 11:31ebaeb51e1e | 2174 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2175 | LogError("Invalid Argument: DeviceId is of an invalid size"); |
AzureIoTClient | 11:31ebaeb51e1e | 2176 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2177 | } |
AzureIoTClient | 12:658ca6865de2 | 2178 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2179 | else if ((config->upperConfig->deviceKey != NULL) && (strlen(config->upperConfig->deviceKey) == 0)) |
AzureIoTClient | 11:31ebaeb51e1e | 2180 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2181 | LogError("Invalid Argument: deviceKey is empty"); |
AzureIoTClient | 11:31ebaeb51e1e | 2182 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2183 | } |
AzureIoTClient | 12:658ca6865de2 | 2184 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2185 | else if ((config->upperConfig->deviceSasToken != NULL) && (strlen(config->upperConfig->deviceSasToken) == 0)) |
AzureIoTClient | 11:31ebaeb51e1e | 2186 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2187 | LogError("Invalid Argument: deviceSasToken is empty"); |
AzureIoTClient | 11:31ebaeb51e1e | 2188 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2189 | } |
AzureIoTClient | 12:658ca6865de2 | 2190 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, deviceKey, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2191 | else if (strlen(config->upperConfig->iotHubName) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2192 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2193 | LogError("Invalid Argument: iotHubName is empty"); |
AzureIoTClient | 11:31ebaeb51e1e | 2194 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2195 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2196 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2197 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2198 | result = InitializeTransportHandleData(config->upperConfig, config->waitingToSend, config->auth_module_handle); |
Azure.IoT.Build | 13:606465879c57 | 2199 | if (result != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2200 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2201 | result->get_io_transport = get_io_transport; |
AzureIoTClient | 20:594780be216d | 2202 | result->http_proxy_hostname = NULL; |
AzureIoTClient | 20:594780be216d | 2203 | result->http_proxy_username = NULL; |
AzureIoTClient | 20:594780be216d | 2204 | result->http_proxy_password = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2205 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2206 | } |
AzureIoTClient | 12:658ca6865de2 | 2207 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [If any error is encountered then IoTHubTransport_MQTT_Common_Create shall return NULL.] */ |
AzureIoTClient | 12:658ca6865de2 | 2208 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_011: [On Success IoTHubTransport_MQTT_Common_Create shall return a non-NULL value.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2209 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2210 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2211 | |
AzureIoTClient | 11:31ebaeb51e1e | 2212 | void IoTHubTransport_MQTT_Common_Destroy(TRANSPORT_LL_HANDLE handle) |
AzureIoTClient | 11:31ebaeb51e1e | 2213 | { |
AzureIoTClient | 12:658ca6865de2 | 2214 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransport_MQTT_Common_Destroy shall do nothing if parameter handle is NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2215 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2216 | if (transport_data != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2217 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2218 | transport_data->isDestroyCalled = true; |
AzureIoTClient | 11:31ebaeb51e1e | 2219 | |
AzureIoTClient | 11:31ebaeb51e1e | 2220 | DisconnectFromClient(transport_data); |
AzureIoTClient | 11:31ebaeb51e1e | 2221 | |
AzureIoTClient | 11:31ebaeb51e1e | 2222 | //Empty the Waiting for Ack Messages. |
AzureIoTClient | 11:31ebaeb51e1e | 2223 | while (!DList_IsListEmpty(&transport_data->telemetry_waitingForAck)) |
AzureIoTClient | 11:31ebaeb51e1e | 2224 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2225 | PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->telemetry_waitingForAck); |
AzureIoTClient | 11:31ebaeb51e1e | 2226 | MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry); |
AzureIoTClient | 11:31ebaeb51e1e | 2227 | sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY); |
AzureIoTClient | 11:31ebaeb51e1e | 2228 | free(mqttMsgEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 2229 | } |
AzureIoTClient | 12:658ca6865de2 | 2230 | while (!DList_IsListEmpty(&transport_data->ack_waiting_queue)) |
AzureIoTClient | 12:658ca6865de2 | 2231 | { |
AzureIoTClient | 12:658ca6865de2 | 2232 | PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->ack_waiting_queue); |
AzureIoTClient | 12:658ca6865de2 | 2233 | MQTT_DEVICE_TWIN_ITEM* mqtt_device_twin = containingRecord(currentEntry, MQTT_DEVICE_TWIN_ITEM, entry); |
AzureIoTClient | 39:6231984e0179 | 2234 | IoTHubClientCore_LL_ReportedStateComplete(transport_data->llClientHandle, mqtt_device_twin->iothub_msg_id, STATUS_CODE_TIMEOUT_VALUE); |
AzureIoTClient | 12:658ca6865de2 | 2235 | free(mqtt_device_twin); |
AzureIoTClient | 12:658ca6865de2 | 2236 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2237 | |
AzureIoTClient | 12:658ca6865de2 | 2238 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransport_MQTT_Common_Destroy shall free all the resources currently in use.] */ |
AzureIoTClient | 20:594780be216d | 2239 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_012: [ `IoTHubTransport_MQTT_Common_Destroy` shall free the stored proxy options. ]*/ |
AzureIoTClient | 31:d6198e67d1eb | 2240 | free_transport_handle_data(transport_data); |
AzureIoTClient | 11:31ebaeb51e1e | 2241 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2242 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2243 | |
AzureIoTClient | 12:658ca6865de2 | 2244 | int IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 2245 | { |
AzureIoTClient | 12:658ca6865de2 | 2246 | int result; |
AzureIoTClient | 12:658ca6865de2 | 2247 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2248 | if (transport_data == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2249 | { |
AzureIoTClient | 12:658ca6865de2 | 2250 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_042: [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */ |
AzureIoTClient | 12:658ca6865de2 | 2251 | LogError("Invalid handle parameter. NULL."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2252 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 2253 | } |
AzureIoTClient | 12:658ca6865de2 | 2254 | else |
AzureIoTClient | 12:658ca6865de2 | 2255 | { |
AzureIoTClient | 12:658ca6865de2 | 2256 | if (transport_data->topic_GetState == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2257 | { |
AzureIoTClient | 12:658ca6865de2 | 2258 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_044: [IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall construct the get state topic string and the notify state topic string.] */ |
AzureIoTClient | 12:658ca6865de2 | 2259 | transport_data->topic_GetState = STRING_construct(TOPIC_GET_DESIRED_STATE); |
AzureIoTClient | 12:658ca6865de2 | 2260 | if (transport_data->topic_GetState == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2261 | { |
AzureIoTClient | 12:658ca6865de2 | 2262 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_046: [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return a non-zero value.] */ |
AzureIoTClient | 12:658ca6865de2 | 2263 | LogError("Failure: unable constructing reported state topic"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2264 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 2265 | } |
AzureIoTClient | 12:658ca6865de2 | 2266 | else |
AzureIoTClient | 12:658ca6865de2 | 2267 | { |
AzureIoTClient | 12:658ca6865de2 | 2268 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */ |
AzureIoTClient | 12:658ca6865de2 | 2269 | transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2270 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 2271 | } |
AzureIoTClient | 12:658ca6865de2 | 2272 | } |
AzureIoTClient | 12:658ca6865de2 | 2273 | else |
AzureIoTClient | 12:658ca6865de2 | 2274 | { |
AzureIoTClient | 12:658ca6865de2 | 2275 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 2276 | } |
AzureIoTClient | 12:658ca6865de2 | 2277 | if (result == 0 && transport_data->topic_NotifyState == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2278 | { |
AzureIoTClient | 12:658ca6865de2 | 2279 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_044: [`IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin` shall construct the get state topic string and the notify state topic string.] */ |
AzureIoTClient | 12:658ca6865de2 | 2280 | transport_data->topic_NotifyState = STRING_construct(TOPIC_NOTIFICATION_STATE); |
AzureIoTClient | 12:658ca6865de2 | 2281 | if (transport_data->topic_NotifyState == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2282 | { |
AzureIoTClient | 12:658ca6865de2 | 2283 | LogError("Failure: unable constructing notify state topic"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2284 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 2285 | } |
AzureIoTClient | 12:658ca6865de2 | 2286 | else |
AzureIoTClient | 12:658ca6865de2 | 2287 | { |
AzureIoTClient | 12:658ca6865de2 | 2288 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */ |
AzureIoTClient | 12:658ca6865de2 | 2289 | transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2290 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 2291 | } |
AzureIoTClient | 12:658ca6865de2 | 2292 | } |
AzureIoTClient | 12:658ca6865de2 | 2293 | if (result == 0) |
AzureIoTClient | 12:658ca6865de2 | 2294 | { |
AzureIoTClient | 12:658ca6865de2 | 2295 | if (transport_data->currPacketState != CONNACK_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2296 | transport_data->currPacketState != CONNECT_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2297 | transport_data->currPacketState != DISCONNECT_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2298 | transport_data->currPacketState != PACKET_TYPE_ERROR) |
AzureIoTClient | 12:658ca6865de2 | 2299 | { |
AzureIoTClient | 12:658ca6865de2 | 2300 | transport_data->currPacketState = SUBSCRIBE_TYPE; |
AzureIoTClient | 12:658ca6865de2 | 2301 | } |
AzureIoTClient | 12:658ca6865de2 | 2302 | } |
AzureIoTClient | 12:658ca6865de2 | 2303 | } |
AzureIoTClient | 12:658ca6865de2 | 2304 | return result; |
AzureIoTClient | 12:658ca6865de2 | 2305 | } |
AzureIoTClient | 12:658ca6865de2 | 2306 | |
AzureIoTClient | 12:658ca6865de2 | 2307 | void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 2308 | { |
AzureIoTClient | 12:658ca6865de2 | 2309 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2310 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_048: [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall do nothing.] */ |
AzureIoTClient | 12:658ca6865de2 | 2311 | if (transport_data != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2312 | { |
AzureIoTClient | 12:658ca6865de2 | 2313 | if (transport_data->topic_GetState != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2314 | { |
AzureIoTClient | 12:658ca6865de2 | 2315 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_049: [If subscribe_state is set to IOTHUB_DEVICE_TWIN_DESIRED_STATE then IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall unsubscribe from the topic_GetState to the mqtt client.] */ |
AzureIoTClient | 12:658ca6865de2 | 2316 | transport_data->topics_ToSubscribe &= ~SUBSCRIBE_GET_REPORTED_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2317 | STRING_delete(transport_data->topic_GetState); |
AzureIoTClient | 12:658ca6865de2 | 2318 | transport_data->topic_GetState = NULL; |
AzureIoTClient | 12:658ca6865de2 | 2319 | } |
AzureIoTClient | 12:658ca6865de2 | 2320 | if (transport_data->topic_NotifyState != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2321 | { |
AzureIoTClient | 12:658ca6865de2 | 2322 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_050: [If subscribe_state is set to IOTHUB_DEVICE_TWIN_NOTIFICATION_STATE then IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall unsubscribe from the topic_NotifyState to the mqtt client.] */ |
AzureIoTClient | 12:658ca6865de2 | 2323 | transport_data->topics_ToSubscribe &= ~SUBSCRIBE_NOTIFICATION_STATE_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2324 | STRING_delete(transport_data->topic_NotifyState); |
AzureIoTClient | 12:658ca6865de2 | 2325 | transport_data->topic_NotifyState = NULL; |
AzureIoTClient | 12:658ca6865de2 | 2326 | } |
AzureIoTClient | 12:658ca6865de2 | 2327 | } |
AzureIoTClient | 12:658ca6865de2 | 2328 | else |
AzureIoTClient | 12:658ca6865de2 | 2329 | { |
AzureIoTClient | 12:658ca6865de2 | 2330 | LogError("Invalid argument to unsubscribe (handle is NULL)."); |
AzureIoTClient | 12:658ca6865de2 | 2331 | } |
AzureIoTClient | 12:658ca6865de2 | 2332 | } |
AzureIoTClient | 12:658ca6865de2 | 2333 | |
AzureIoTClient | 12:658ca6865de2 | 2334 | int IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 2335 | { |
AzureIoTClient | 12:658ca6865de2 | 2336 | int result; |
AzureIoTClient | 12:658ca6865de2 | 2337 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2338 | |
AzureIoTClient | 12:658ca6865de2 | 2339 | if (transport_data == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2340 | { |
AzureIoTClient | 12:658ca6865de2 | 2341 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_001 : [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return a non - zero value.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2342 | LogError("Invalid handle parameter. NULL."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2343 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 2344 | } |
AzureIoTClient | 12:658ca6865de2 | 2345 | else |
AzureIoTClient | 12:658ca6865de2 | 2346 | { |
AzureIoTClient | 12:658ca6865de2 | 2347 | if (transport_data->topic_DeviceMethods == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2348 | { |
AzureIoTClient | 12:658ca6865de2 | 2349 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_004 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for subscribe.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2350 | transport_data->topic_DeviceMethods = STRING_construct(TOPIC_DEVICE_METHOD_SUBSCRIBE); |
AzureIoTClient | 12:658ca6865de2 | 2351 | if (transport_data->topic_DeviceMethods == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2352 | { |
AzureIoTClient | 12:658ca6865de2 | 2353 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_006 : [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return a non - zero value.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2354 | LogError("Failure: unable constructing device method subscribe topic"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2355 | result = __FAILURE__; |
AzureIoTClient | 12:658ca6865de2 | 2356 | } |
AzureIoTClient | 12:658ca6865de2 | 2357 | else |
AzureIoTClient | 12:658ca6865de2 | 2358 | { |
AzureIoTClient | 12:658ca6865de2 | 2359 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_003 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall set the signaling flag for DEVICE_METHOD topic for the receiver's topic list. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2360 | transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2361 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 2362 | } |
AzureIoTClient | 12:658ca6865de2 | 2363 | } |
AzureIoTClient | 12:658ca6865de2 | 2364 | else |
AzureIoTClient | 12:658ca6865de2 | 2365 | { |
AzureIoTClient | 12:658ca6865de2 | 2366 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_002 : [If the MQTT transport has been previously subscribed to DEVICE_METHOD topic IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall do nothing and return 0.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2367 | result = 0; |
AzureIoTClient | 12:658ca6865de2 | 2368 | } |
AzureIoTClient | 12:658ca6865de2 | 2369 | |
AzureIoTClient | 12:658ca6865de2 | 2370 | if (result == 0) |
AzureIoTClient | 12:658ca6865de2 | 2371 | { |
AzureIoTClient | 12:658ca6865de2 | 2372 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_005 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall schedule the send of the subscription.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2373 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_007 : [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return 0.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2374 | if (transport_data->currPacketState != CONNACK_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2375 | transport_data->currPacketState != CONNECT_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2376 | transport_data->currPacketState != DISCONNECT_TYPE && |
AzureIoTClient | 12:658ca6865de2 | 2377 | transport_data->currPacketState != PACKET_TYPE_ERROR) |
AzureIoTClient | 12:658ca6865de2 | 2378 | { |
AzureIoTClient | 12:658ca6865de2 | 2379 | transport_data->currPacketState = SUBSCRIBE_TYPE; |
AzureIoTClient | 12:658ca6865de2 | 2380 | } |
AzureIoTClient | 12:658ca6865de2 | 2381 | } |
AzureIoTClient | 12:658ca6865de2 | 2382 | } |
AzureIoTClient | 12:658ca6865de2 | 2383 | return result; |
AzureIoTClient | 12:658ca6865de2 | 2384 | } |
AzureIoTClient | 12:658ca6865de2 | 2385 | |
AzureIoTClient | 12:658ca6865de2 | 2386 | void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 2387 | { |
AzureIoTClient | 12:658ca6865de2 | 2388 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2389 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_008 : [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall do nothing and return.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2390 | if (transport_data != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2391 | { |
AzureIoTClient | 12:658ca6865de2 | 2392 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_009 : [If the MQTT transport has not been subscribed to DEVICE_METHOD topic IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall do nothing and return.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2393 | if (transport_data->topic_DeviceMethods != NULL) |
AzureIoTClient | 12:658ca6865de2 | 2394 | { |
AzureIoTClient | 12:658ca6865de2 | 2395 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_010 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for unsubscribe.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2396 | const char* unsubscribe[1]; |
AzureIoTClient | 12:658ca6865de2 | 2397 | unsubscribe[0] = STRING_c_str(transport_data->topic_DeviceMethods); |
AzureIoTClient | 12:658ca6865de2 | 2398 | |
AzureIoTClient | 12:658ca6865de2 | 2399 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_011 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall send the unsubscribe.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2400 | if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0) |
AzureIoTClient | 12:658ca6865de2 | 2401 | { |
AzureIoTClient | 12:658ca6865de2 | 2402 | LogError("Failure calling mqtt_client_unsubscribe"); |
AzureIoTClient | 12:658ca6865de2 | 2403 | } |
AzureIoTClient | 12:658ca6865de2 | 2404 | |
AzureIoTClient | 12:658ca6865de2 | 2405 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_012 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall removes the signaling flag for DEVICE_METHOD topic from the receiver's topic list. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2406 | STRING_delete(transport_data->topic_DeviceMethods); |
AzureIoTClient | 12:658ca6865de2 | 2407 | transport_data->topic_DeviceMethods = NULL; |
AzureIoTClient | 12:658ca6865de2 | 2408 | transport_data->topics_ToSubscribe &= ~SUBSCRIBE_DEVICE_METHOD_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2409 | } |
AzureIoTClient | 12:658ca6865de2 | 2410 | } |
AzureIoTClient | 12:658ca6865de2 | 2411 | else |
AzureIoTClient | 12:658ca6865de2 | 2412 | { |
AzureIoTClient | 12:658ca6865de2 | 2413 | LogError("Invalid argument to unsubscribe (NULL)."); |
AzureIoTClient | 12:658ca6865de2 | 2414 | } |
AzureIoTClient | 12:658ca6865de2 | 2415 | } |
AzureIoTClient | 12:658ca6865de2 | 2416 | |
AzureIoTClient | 14:4dc2b011be33 | 2417 | int IoTHubTransport_MQTT_Common_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t respSize, int status) |
AzureIoTClient | 14:4dc2b011be33 | 2418 | { |
AzureIoTClient | 14:4dc2b011be33 | 2419 | int result; |
AzureIoTClient | 14:4dc2b011be33 | 2420 | MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 14:4dc2b011be33 | 2421 | if (transport_data != NULL) |
AzureIoTClient | 14:4dc2b011be33 | 2422 | { |
AzureIoTClient | 14:4dc2b011be33 | 2423 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_042: [ IoTHubTransport_MQTT_Common_DeviceMethod_Response shall publish an mqtt message for the device method response. ] */ |
AzureIoTClient | 14:4dc2b011be33 | 2424 | DEVICE_METHOD_INFO* dev_method_info = (DEVICE_METHOD_INFO*)methodId; |
AzureIoTClient | 14:4dc2b011be33 | 2425 | if (dev_method_info == NULL) |
AzureIoTClient | 14:4dc2b011be33 | 2426 | { |
AzureIoTClient | 14:4dc2b011be33 | 2427 | LogError("Failure: DEVICE_METHOD_INFO was NULL"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2428 | result = __FAILURE__; |
AzureIoTClient | 14:4dc2b011be33 | 2429 | } |
AzureIoTClient | 14:4dc2b011be33 | 2430 | else |
AzureIoTClient | 14:4dc2b011be33 | 2431 | { |
AzureIoTClient | 14:4dc2b011be33 | 2432 | if (publish_device_method_message(transport_data, status, dev_method_info->request_id, response, respSize) != 0) |
AzureIoTClient | 14:4dc2b011be33 | 2433 | { |
AzureIoTClient | 14:4dc2b011be33 | 2434 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_051: [ If any error is encountered, IoTHubTransport_MQTT_Common_DeviceMethod_Response shall return a non-zero value. ] */ |
AzureIoTClient | 14:4dc2b011be33 | 2435 | LogError("Failure: publishing device method response"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2436 | result = __FAILURE__; |
AzureIoTClient | 14:4dc2b011be33 | 2437 | } |
AzureIoTClient | 14:4dc2b011be33 | 2438 | else |
AzureIoTClient | 14:4dc2b011be33 | 2439 | { |
AzureIoTClient | 14:4dc2b011be33 | 2440 | result = 0; |
AzureIoTClient | 14:4dc2b011be33 | 2441 | } |
AzureIoTClient | 14:4dc2b011be33 | 2442 | STRING_delete(dev_method_info->request_id); |
AzureIoTClient | 14:4dc2b011be33 | 2443 | free(dev_method_info); |
AzureIoTClient | 14:4dc2b011be33 | 2444 | } |
AzureIoTClient | 14:4dc2b011be33 | 2445 | } |
AzureIoTClient | 14:4dc2b011be33 | 2446 | else |
AzureIoTClient | 14:4dc2b011be33 | 2447 | { |
AzureIoTClient | 14:4dc2b011be33 | 2448 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_041: [ If the parameter handle is NULL than IoTHubTransport_MQTT_Common_DeviceMethod_Response shall return a non-zero value. ] */ |
AzureIoTClient | 18:ec8e5e97c6a4 | 2449 | result = __FAILURE__; |
AzureIoTClient | 14:4dc2b011be33 | 2450 | LogError("Failure: invalid IOTHUB_DEVICE_HANDLE parameter specified"); |
AzureIoTClient | 14:4dc2b011be33 | 2451 | } |
AzureIoTClient | 14:4dc2b011be33 | 2452 | return result; |
AzureIoTClient | 14:4dc2b011be33 | 2453 | } |
AzureIoTClient | 14:4dc2b011be33 | 2454 | |
AzureIoTClient | 11:31ebaeb51e1e | 2455 | int IoTHubTransport_MQTT_Common_Subscribe(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 11:31ebaeb51e1e | 2456 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2457 | int result; |
AzureIoTClient | 11:31ebaeb51e1e | 2458 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2459 | if (transport_data == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2460 | { |
AzureIoTClient | 12:658ca6865de2 | 2461 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_015: [If parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2462 | LogError("Invalid handle parameter. NULL."); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2463 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 2464 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2465 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2466 | { |
AzureIoTClient | 12:658ca6865de2 | 2467 | /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransport_MQTT_Common_Subscribe shall set a flag to enable mqtt_client_subscribe to be called to subscribe to the Message Topic.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2468 | transport_data->topic_MqttMessage = STRING_construct_sprintf(TOPIC_DEVICE_MSG, STRING_c_str(transport_data->device_id) ); |
AzureIoTClient | 11:31ebaeb51e1e | 2469 | if (transport_data->topic_MqttMessage == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2470 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2471 | LogError("Failure constructing Message Topic"); |
AzureIoTClient | 18:ec8e5e97c6a4 | 2472 | result = __FAILURE__; |
AzureIoTClient | 11:31ebaeb51e1e | 2473 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2474 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2475 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2476 | transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC; |
AzureIoTClient | 12:658ca6865de2 | 2477 | /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_035: [If current packet state is not CONNACT, DISCONNECT_TYPE, or PACKET_TYPE_ERROR then IoTHubTransport_MQTT_Common_Subscribe shall set the packet state to SUBSCRIBE_TYPE.]*/ |
AzureIoTClient | 11:31ebaeb51e1e | 2478 | if (transport_data->currPacketState != CONNACK_TYPE && |
AzureIoTClient | 11:31ebaeb51e1e | 2479 | transport_data->currPacketState != CONNECT_TYPE && |
AzureIoTClient | 11:31ebaeb51e1e | 2480 | transport_data->currPacketState != DISCONNECT_TYPE && |
AzureIoTClient | 11:31ebaeb51e1e | 2481 | transport_data->currPacketState != PACKET_TYPE_ERROR) |
AzureIoTClient | 11:31ebaeb51e1e | 2482 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2483 | transport_data->currPacketState = SUBSCRIBE_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 2484 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2485 | result = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 2486 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2487 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2488 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2489 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2490 | |
AzureIoTClient | 11:31ebaeb51e1e | 2491 | void IoTHubTransport_MQTT_Common_Unsubscribe(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 11:31ebaeb51e1e | 2492 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2493 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2494 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_019: [If parameter handle is NULL then IoTHubTransport_MQTT_Common_Unsubscribe shall do nothing.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2495 | if (transport_data != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2496 | { |
AzureIoTClient | 12:658ca6865de2 | 2497 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_020: [IoTHubTransport_MQTT_Common_Unsubscribe shall call mqtt_client_unsubscribe to unsubscribe the mqtt message topic.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2498 | const char* unsubscribe[1]; |
AzureIoTClient | 11:31ebaeb51e1e | 2499 | unsubscribe[0] = STRING_c_str(transport_data->topic_MqttMessage); |
AzureIoTClient | 11:31ebaeb51e1e | 2500 | if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2501 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2502 | LogError("Failure calling mqtt_client_unsubscribe"); |
AzureIoTClient | 11:31ebaeb51e1e | 2503 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2504 | STRING_delete(transport_data->topic_MqttMessage); |
AzureIoTClient | 11:31ebaeb51e1e | 2505 | transport_data->topic_MqttMessage = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2506 | transport_data->topics_ToSubscribe &= ~SUBSCRIBE_TELEMETRY_TOPIC; |
AzureIoTClient | 11:31ebaeb51e1e | 2507 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2508 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2509 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2510 | LogError("Invalid argument to unsubscribe (NULL)."); |
AzureIoTClient | 11:31ebaeb51e1e | 2511 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2512 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2513 | |
AzureIoTClient | 12:658ca6865de2 | 2514 | IOTHUB_PROCESS_ITEM_RESULT IoTHubTransport_MQTT_Common_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item) |
AzureIoTClient | 12:658ca6865de2 | 2515 | { |
AzureIoTClient | 12:658ca6865de2 | 2516 | IOTHUB_PROCESS_ITEM_RESULT result; |
AzureIoTClient | 12:658ca6865de2 | 2517 | /* Codes_SRS_IOTHUBCLIENT_LL_07_001: [ If handle or iothub_item are NULL then IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2518 | if (handle == NULL || iothub_item == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2519 | { |
AzureIoTClient | 12:658ca6865de2 | 2520 | LogError("Invalid handle parameter iothub_item=%p", iothub_item); |
AzureIoTClient | 12:658ca6865de2 | 2521 | result = IOTHUB_PROCESS_ERROR; |
AzureIoTClient | 12:658ca6865de2 | 2522 | } |
AzureIoTClient | 12:658ca6865de2 | 2523 | else |
AzureIoTClient | 12:658ca6865de2 | 2524 | { |
AzureIoTClient | 12:658ca6865de2 | 2525 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 12:658ca6865de2 | 2526 | |
AzureIoTClient | 12:658ca6865de2 | 2527 | if (transport_data->currPacketState == PUBLISH_TYPE) |
AzureIoTClient | 12:658ca6865de2 | 2528 | { |
AzureIoTClient | 12:658ca6865de2 | 2529 | if (item_type == IOTHUB_TYPE_DEVICE_TWIN) |
AzureIoTClient | 12:658ca6865de2 | 2530 | { |
AzureIoTClient | 12:658ca6865de2 | 2531 | MQTT_DEVICE_TWIN_ITEM* mqtt_info = (MQTT_DEVICE_TWIN_ITEM*)malloc(sizeof(MQTT_DEVICE_TWIN_ITEM)); |
AzureIoTClient | 12:658ca6865de2 | 2532 | if (mqtt_info == NULL) |
AzureIoTClient | 12:658ca6865de2 | 2533 | { |
AzureIoTClient | 12:658ca6865de2 | 2534 | /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2535 | result = IOTHUB_PROCESS_ERROR; |
AzureIoTClient | 12:658ca6865de2 | 2536 | } |
AzureIoTClient | 12:658ca6865de2 | 2537 | else |
AzureIoTClient | 12:658ca6865de2 | 2538 | { |
AzureIoTClient | 12:658ca6865de2 | 2539 | /*Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubTransport_MQTT_Common_ProcessItem shall publish a message to the mqtt protocol with the message topic for the message type.]*/ |
AzureIoTClient | 12:658ca6865de2 | 2540 | mqtt_info->iothub_type = item_type; |
AzureIoTClient | 12:658ca6865de2 | 2541 | mqtt_info->iothub_msg_id = iothub_item->device_twin->item_id; |
AzureIoTClient | 12:658ca6865de2 | 2542 | mqtt_info->retryCount = 0; |
AzureIoTClient | 12:658ca6865de2 | 2543 | |
AzureIoTClient | 12:658ca6865de2 | 2544 | /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ If successful IoTHubTransport_MQTT_Common_ProcessItem shall add mqtt info structure acknowledgement queue. ] */ |
AzureIoTClient | 12:658ca6865de2 | 2545 | DList_InsertTailList(&transport_data->ack_waiting_queue, &mqtt_info->entry); |
AzureIoTClient | 12:658ca6865de2 | 2546 | |
AzureIoTClient | 12:658ca6865de2 | 2547 | if (publish_device_twin_message(transport_data, iothub_item->device_twin, mqtt_info) != 0) |
AzureIoTClient | 12:658ca6865de2 | 2548 | { |
AzureIoTClient | 12:658ca6865de2 | 2549 | DList_RemoveEntryList(&mqtt_info->entry); |
AzureIoTClient | 12:658ca6865de2 | 2550 | |
AzureIoTClient | 12:658ca6865de2 | 2551 | free(mqtt_info); |
AzureIoTClient | 12:658ca6865de2 | 2552 | /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2553 | result = IOTHUB_PROCESS_ERROR; |
AzureIoTClient | 12:658ca6865de2 | 2554 | } |
AzureIoTClient | 12:658ca6865de2 | 2555 | else |
AzureIoTClient | 12:658ca6865de2 | 2556 | { |
AzureIoTClient | 12:658ca6865de2 | 2557 | result = IOTHUB_PROCESS_OK; |
AzureIoTClient | 12:658ca6865de2 | 2558 | } |
AzureIoTClient | 12:658ca6865de2 | 2559 | } |
AzureIoTClient | 12:658ca6865de2 | 2560 | } |
AzureIoTClient | 12:658ca6865de2 | 2561 | else |
AzureIoTClient | 12:658ca6865de2 | 2562 | { |
AzureIoTClient | 12:658ca6865de2 | 2563 | /* Codes_SRS_IOTHUBCLIENT_LL_07_006: [ If the item_type is not a supported type IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_CONTINUE. ]*/ |
AzureIoTClient | 12:658ca6865de2 | 2564 | result = IOTHUB_PROCESS_CONTINUE; |
AzureIoTClient | 12:658ca6865de2 | 2565 | } |
AzureIoTClient | 12:658ca6865de2 | 2566 | } |
AzureIoTClient | 12:658ca6865de2 | 2567 | else |
AzureIoTClient | 12:658ca6865de2 | 2568 | { |
AzureIoTClient | 12:658ca6865de2 | 2569 | /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ If the mqtt is not ready to publish messages IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_NOT_CONNECTED. ] */ |
AzureIoTClient | 12:658ca6865de2 | 2570 | result = IOTHUB_PROCESS_NOT_CONNECTED; |
AzureIoTClient | 12:658ca6865de2 | 2571 | } |
AzureIoTClient | 12:658ca6865de2 | 2572 | } |
AzureIoTClient | 12:658ca6865de2 | 2573 | return result; |
AzureIoTClient | 12:658ca6865de2 | 2574 | } |
AzureIoTClient | 12:658ca6865de2 | 2575 | |
AzureIoTClient | 12:658ca6865de2 | 2576 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ IoTHubTransport_MQTT_Common_DoWork shall subscribe to the Notification and get_state Topics if they are defined. ] */ |
AzureIoTClient | 39:6231984e0179 | 2577 | void IoTHubTransport_MQTT_Common_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 11:31ebaeb51e1e | 2578 | { |
AzureIoTClient | 12:658ca6865de2 | 2579 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_026: [IoTHubTransport_MQTT_Common_DoWork shall do nothing if parameter handle and/or iotHubClientHandle is NULL.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2580 | PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2581 | if (transport_data != NULL && iotHubClientHandle != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2582 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2583 | transport_data->llClientHandle = iotHubClientHandle; |
AzureIoTClient | 11:31ebaeb51e1e | 2584 | |
AzureIoTClient | 11:31ebaeb51e1e | 2585 | if (InitializeConnection(transport_data) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2586 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2587 | // Don't want to flood the logs with failures here |
AzureIoTClient | 11:31ebaeb51e1e | 2588 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2589 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2590 | { |
AzureIoTClient | 33:b7dfb208ef0a | 2591 | if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_PENDING_CLOSE) |
AzureIoTClient | 33:b7dfb208ef0a | 2592 | { |
AzureIoTClient | 33:b7dfb208ef0a | 2593 | mqtt_client_disconnect(transport_data->mqttClient, NULL, NULL); |
AzureIoTClient | 33:b7dfb208ef0a | 2594 | transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED; |
AzureIoTClient | 33:b7dfb208ef0a | 2595 | } |
AzureIoTClient | 33:b7dfb208ef0a | 2596 | else if (transport_data->currPacketState == CONNACK_TYPE || transport_data->currPacketState == SUBSCRIBE_TYPE) |
AzureIoTClient | 11:31ebaeb51e1e | 2597 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2598 | SubscribeToMqttProtocol(transport_data); |
AzureIoTClient | 11:31ebaeb51e1e | 2599 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2600 | else if (transport_data->currPacketState == SUBACK_TYPE) |
AzureIoTClient | 11:31ebaeb51e1e | 2601 | { |
AzureIoTClient | 12:658ca6865de2 | 2602 | if ((transport_data->topic_NotifyState != NULL || transport_data->topic_GetState != NULL) && |
AzureIoTClient | 12:658ca6865de2 | 2603 | !transport_data->device_twin_get_sent) |
AzureIoTClient | 12:658ca6865de2 | 2604 | { |
AzureIoTClient | 12:658ca6865de2 | 2605 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_055: [ IoTHubTransport_MQTT_Common_DoWork shall send a device twin get property message upon successfully retrieving a SUBACK on device twin topics. ] */ |
AzureIoTClient | 12:658ca6865de2 | 2606 | if (publish_device_twin_get_message(transport_data) == 0) |
AzureIoTClient | 12:658ca6865de2 | 2607 | { |
AzureIoTClient | 12:658ca6865de2 | 2608 | transport_data->device_twin_get_sent = true; |
AzureIoTClient | 12:658ca6865de2 | 2609 | } |
AzureIoTClient | 12:658ca6865de2 | 2610 | else |
AzureIoTClient | 12:658ca6865de2 | 2611 | { |
AzureIoTClient | 12:658ca6865de2 | 2612 | LogError("Failure: sending device twin get property command."); |
AzureIoTClient | 12:658ca6865de2 | 2613 | } |
AzureIoTClient | 12:658ca6865de2 | 2614 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2615 | // Publish can be called now |
AzureIoTClient | 11:31ebaeb51e1e | 2616 | transport_data->currPacketState = PUBLISH_TYPE; |
AzureIoTClient | 11:31ebaeb51e1e | 2617 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2618 | else if (transport_data->currPacketState == PUBLISH_TYPE) |
AzureIoTClient | 11:31ebaeb51e1e | 2619 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2620 | PDLIST_ENTRY currentListEntry = transport_data->telemetry_waitingForAck.Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 2621 | while (currentListEntry != &transport_data->telemetry_waitingForAck) |
AzureIoTClient | 11:31ebaeb51e1e | 2622 | { |
AzureIoTClient | 24:4096249decf1 | 2623 | tickcounter_ms_t current_ms; |
AzureIoTClient | 11:31ebaeb51e1e | 2624 | MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry); |
AzureIoTClient | 11:31ebaeb51e1e | 2625 | DLIST_ENTRY nextListEntry; |
AzureIoTClient | 11:31ebaeb51e1e | 2626 | nextListEntry.Flink = currentListEntry->Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 2627 | |
Azure.IoT.Build | 13:606465879c57 | 2628 | (void)tickcounter_get_current_ms(transport_data->msgTickCounter, ¤t_ms); |
AzureIoTClient | 12:658ca6865de2 | 2629 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_033: [IoTHubTransport_MQTT_Common_DoWork shall iterate through the Waiting Acknowledge messages looking for any message that has been waiting longer than 2 min.]*/ |
AzureIoTClient | 11:31ebaeb51e1e | 2630 | if (((current_ms - mqttMsgEntry->msgPublishTime) / 1000) > RESEND_TIMEOUT_VALUE_MIN) |
AzureIoTClient | 11:31ebaeb51e1e | 2631 | { |
AzureIoTClient | 24:4096249decf1 | 2632 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_034: [If IoTHubTransport_MQTT_Common_DoWork has resent the message two times then it shall fail the message and reconnect to IoTHub ... ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2633 | if (mqttMsgEntry->retryCount >= MAX_SEND_RECOUNT_LIMIT) |
AzureIoTClient | 11:31ebaeb51e1e | 2634 | { |
AzureIoTClient | 24:4096249decf1 | 2635 | PDLIST_ENTRY current_entry; |
AzureIoTClient | 11:31ebaeb51e1e | 2636 | (void)DList_RemoveEntryList(currentListEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 2637 | sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT); |
AzureIoTClient | 11:31ebaeb51e1e | 2638 | free(mqttMsgEntry); |
AzureIoTClient | 24:4096249decf1 | 2639 | |
AzureIoTClient | 24:4096249decf1 | 2640 | transport_data->currPacketState = PACKET_TYPE_ERROR; |
AzureIoTClient | 24:4096249decf1 | 2641 | transport_data->device_twin_get_sent = false; |
AzureIoTClient | 24:4096249decf1 | 2642 | DisconnectFromClient(transport_data); |
AzureIoTClient | 24:4096249decf1 | 2643 | |
AzureIoTClient | 24:4096249decf1 | 2644 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_057: [ ... then go through all the rest of the waiting messages and reset the retryCount on the message. ]*/ |
AzureIoTClient | 24:4096249decf1 | 2645 | current_entry = transport_data->telemetry_waitingForAck.Flink; |
AzureIoTClient | 24:4096249decf1 | 2646 | while (current_entry != &transport_data->telemetry_waitingForAck) |
AzureIoTClient | 24:4096249decf1 | 2647 | { |
AzureIoTClient | 24:4096249decf1 | 2648 | MQTT_MESSAGE_DETAILS_LIST* msg_reset_entry; |
AzureIoTClient | 24:4096249decf1 | 2649 | msg_reset_entry = containingRecord(current_entry, MQTT_MESSAGE_DETAILS_LIST, entry); |
AzureIoTClient | 24:4096249decf1 | 2650 | msg_reset_entry->retryCount = 0; |
AzureIoTClient | 24:4096249decf1 | 2651 | current_entry = current_entry->Flink; |
AzureIoTClient | 24:4096249decf1 | 2652 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2653 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2654 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2655 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2656 | size_t messageLength; |
AzureIoTClient | 11:31ebaeb51e1e | 2657 | const unsigned char* messagePayload = RetrieveMessagePayload(mqttMsgEntry->iotHubMessageEntry->messageHandle, &messageLength); |
AzureIoTClient | 11:31ebaeb51e1e | 2658 | if (messageLength == 0 || messagePayload == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2659 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2660 | LogError("Failure from creating Message IoTHubMessage_GetData"); |
AzureIoTClient | 11:31ebaeb51e1e | 2661 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2662 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2663 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2664 | if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2665 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2666 | (void)DList_RemoveEntryList(currentListEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 2667 | sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR); |
AzureIoTClient | 11:31ebaeb51e1e | 2668 | free(mqttMsgEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 2669 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2670 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2671 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2672 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2673 | currentListEntry = nextListEntry.Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 2674 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2675 | |
AzureIoTClient | 11:31ebaeb51e1e | 2676 | currentListEntry = transport_data->waitingToSend->Flink; |
AzureIoTClient | 12:658ca6865de2 | 2677 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2678 | while (currentListEntry != transport_data->waitingToSend) |
AzureIoTClient | 11:31ebaeb51e1e | 2679 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2680 | IOTHUB_MESSAGE_LIST* iothubMsgList = containingRecord(currentListEntry, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 11:31ebaeb51e1e | 2681 | DLIST_ENTRY savedFromCurrentListEntry; |
AzureIoTClient | 11:31ebaeb51e1e | 2682 | savedFromCurrentListEntry.Flink = currentListEntry->Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 2683 | |
AzureIoTClient | 12:658ca6865de2 | 2684 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2685 | size_t messageLength; |
AzureIoTClient | 11:31ebaeb51e1e | 2686 | const unsigned char* messagePayload = RetrieveMessagePayload(iothubMsgList->messageHandle, &messageLength); |
AzureIoTClient | 11:31ebaeb51e1e | 2687 | if (messageLength == 0 || messagePayload == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2688 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2689 | LogError("Failure result from IoTHubMessage_GetData"); |
AzureIoTClient | 11:31ebaeb51e1e | 2690 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2691 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2692 | { |
AzureIoTClient | 12:658ca6865de2 | 2693 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_029: [IoTHubTransport_MQTT_Common_DoWork shall create a MQTT_MESSAGE_HANDLE and pass this to a call to mqtt_client_publish.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2694 | MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = (MQTT_MESSAGE_DETAILS_LIST*)malloc(sizeof(MQTT_MESSAGE_DETAILS_LIST)); |
AzureIoTClient | 11:31ebaeb51e1e | 2695 | if (mqttMsgEntry == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2696 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2697 | LogError("Allocation Error: Failure allocating MQTT Message Detail List."); |
AzureIoTClient | 11:31ebaeb51e1e | 2698 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2699 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2700 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2701 | mqttMsgEntry->retryCount = 0; |
AzureIoTClient | 11:31ebaeb51e1e | 2702 | mqttMsgEntry->iotHubMessageEntry = iothubMsgList; |
AzureIoTClient | 11:31ebaeb51e1e | 2703 | mqttMsgEntry->packet_id = get_next_packet_id(transport_data); |
AzureIoTClient | 11:31ebaeb51e1e | 2704 | if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2705 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2706 | (void)(DList_RemoveEntryList(currentListEntry)); |
AzureIoTClient | 11:31ebaeb51e1e | 2707 | sendMsgComplete(iothubMsgList, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR); |
AzureIoTClient | 11:31ebaeb51e1e | 2708 | free(mqttMsgEntry); |
AzureIoTClient | 11:31ebaeb51e1e | 2709 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2710 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2711 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2712 | (void)(DList_RemoveEntryList(currentListEntry)); |
AzureIoTClient | 11:31ebaeb51e1e | 2713 | DList_InsertTailList(&(transport_data->telemetry_waitingForAck), &(mqttMsgEntry->entry)); |
AzureIoTClient | 11:31ebaeb51e1e | 2714 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2715 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2716 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2717 | currentListEntry = savedFromCurrentListEntry.Flink; |
AzureIoTClient | 11:31ebaeb51e1e | 2718 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2719 | } |
AzureIoTClient | 20:594780be216d | 2720 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_030: [IoTHubTransport_MQTT_Common_DoWork shall call mqtt_client_dowork everytime it is called if it is connected.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2721 | mqtt_client_dowork(transport_data->mqttClient); |
AzureIoTClient | 11:31ebaeb51e1e | 2722 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2723 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2724 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2725 | |
AzureIoTClient | 11:31ebaeb51e1e | 2726 | IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 11:31ebaeb51e1e | 2727 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2728 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 11:31ebaeb51e1e | 2729 | |
AzureIoTClient | 11:31ebaeb51e1e | 2730 | if (handle == NULL || iotHubClientStatus == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2731 | { |
AzureIoTClient | 12:658ca6865de2 | 2732 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_023: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2733 | LogError("invalid arument."); |
AzureIoTClient | 11:31ebaeb51e1e | 2734 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 11:31ebaeb51e1e | 2735 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2736 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2737 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2738 | MQTTTRANSPORT_HANDLE_DATA* handleData = (MQTTTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2739 | if (!DList_IsListEmpty(handleData->waitingToSend) || !DList_IsListEmpty(&(handleData->telemetry_waitingForAck))) |
AzureIoTClient | 11:31ebaeb51e1e | 2740 | { |
AzureIoTClient | 12:658ca6865de2 | 2741 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_025: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently event items to be sent or being sent.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2742 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY; |
AzureIoTClient | 11:31ebaeb51e1e | 2743 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2744 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2745 | { |
AzureIoTClient | 12:658ca6865de2 | 2746 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_024: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2747 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE; |
AzureIoTClient | 11:31ebaeb51e1e | 2748 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2749 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 11:31ebaeb51e1e | 2750 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2751 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2752 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2753 | |
AzureIoTClient | 11:31ebaeb51e1e | 2754 | IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value) |
AzureIoTClient | 11:31ebaeb51e1e | 2755 | { |
AzureIoTClient | 12:658ca6865de2 | 2756 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_021: [If any parameter is NULL then IoTHubTransport_MQTT_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2757 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 11:31ebaeb51e1e | 2758 | if ( |
AzureIoTClient | 11:31ebaeb51e1e | 2759 | (handle == NULL) || |
AzureIoTClient | 11:31ebaeb51e1e | 2760 | (option == NULL) || |
AzureIoTClient | 11:31ebaeb51e1e | 2761 | (value == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2762 | ) |
AzureIoTClient | 11:31ebaeb51e1e | 2763 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2764 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 20:594780be216d | 2765 | LogError("invalid parameter (NULL) passed to IoTHubTransport_MQTT_Common_SetOption."); |
AzureIoTClient | 11:31ebaeb51e1e | 2766 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2767 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2768 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2769 | MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 21:167f8fcf6fb1 | 2770 | |
AzureIoTClient | 21:167f8fcf6fb1 | 2771 | IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module); |
AzureIoTClient | 21:167f8fcf6fb1 | 2772 | |
AzureIoTClient | 11:31ebaeb51e1e | 2773 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_031: [If the option parameter is set to "logtrace" then the value shall be a bool_ptr and the value will determine if the mqtt client log is on or off.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2774 | if (strcmp(OPTION_LOG_TRACE, option) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2775 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2776 | transport_data->log_trace = *((bool*)value); |
AzureIoTClient | 11:31ebaeb51e1e | 2777 | mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace); |
AzureIoTClient | 11:31ebaeb51e1e | 2778 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 11:31ebaeb51e1e | 2779 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2780 | else if (strcmp("rawlogtrace", option) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2781 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2782 | transport_data->raw_trace = *((bool*)value); |
AzureIoTClient | 11:31ebaeb51e1e | 2783 | mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace); |
AzureIoTClient | 11:31ebaeb51e1e | 2784 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 11:31ebaeb51e1e | 2785 | } |
AzureIoTClient | 37:e6a799428f3d | 2786 | else if (strcmp(OPTION_AUTO_URL_ENCODE_DECODE, option) == 0) |
AzureIoTClient | 37:e6a799428f3d | 2787 | { |
AzureIoTClient | 37:e6a799428f3d | 2788 | transport_data->auto_url_encode_decode = *((bool*)value); |
AzureIoTClient | 37:e6a799428f3d | 2789 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 37:e6a799428f3d | 2790 | } |
AzureIoTClient | 29:923be0c3998a | 2791 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_052: [ If the option parameter is set to "sas_token_lifetime" then the value shall be a size_t_ptr and the value will determine the mqtt sas token lifetime.] */ |
AzureIoTClient | 29:923be0c3998a | 2792 | else if (strcmp(OPTION_SAS_TOKEN_LIFETIME, option) == 0) |
AzureIoTClient | 29:923be0c3998a | 2793 | { |
AzureIoTClient | 29:923be0c3998a | 2794 | size_t* sas_lifetime = (size_t*)value; |
AzureIoTClient | 29:923be0c3998a | 2795 | transport_data->option_sas_token_lifetime_secs = *sas_lifetime; |
AzureIoTClient | 29:923be0c3998a | 2796 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 29:923be0c3998a | 2797 | } |
AzureIoTClient | 32:103a46ed8822 | 2798 | else if (strcmp(OPTION_CONNECTION_TIMEOUT, option) == 0) |
AzureIoTClient | 32:103a46ed8822 | 2799 | { |
AzureIoTClient | 32:103a46ed8822 | 2800 | int* connection_time = (int*)value; |
AzureIoTClient | 32:103a46ed8822 | 2801 | if (*connection_time != transport_data->connect_timeout_in_sec) |
AzureIoTClient | 32:103a46ed8822 | 2802 | { |
AzureIoTClient | 32:103a46ed8822 | 2803 | transport_data->connect_timeout_in_sec = (uint16_t)(*connection_time); |
AzureIoTClient | 32:103a46ed8822 | 2804 | } |
AzureIoTClient | 32:103a46ed8822 | 2805 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 32:103a46ed8822 | 2806 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2807 | else if (strcmp(OPTION_KEEP_ALIVE, option) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2808 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2809 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_036: [If the option parameter is set to "keepalive" then the value shall be a int_ptr and the value will determine the mqtt keepalive time that is set for pings.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2810 | int* keepAliveOption = (int*)value; |
AzureIoTClient | 12:658ca6865de2 | 2811 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_037 : [If the option parameter is set to supplied int_ptr keepalive is the same value as the existing keepalive then IoTHubTransport_MQTT_Common_SetOption shall do nothing.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2812 | if (*keepAliveOption != transport_data->keepAliveValue) |
AzureIoTClient | 11:31ebaeb51e1e | 2813 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2814 | transport_data->keepAliveValue = (uint16_t)(*keepAliveOption); |
AzureIoTClient | 20:594780be216d | 2815 | if (transport_data->mqttClientStatus != MQTT_CLIENT_STATUS_NOT_CONNECTED) |
AzureIoTClient | 11:31ebaeb51e1e | 2816 | { |
AzureIoTClient | 20:594780be216d | 2817 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_038: [If the client is connected when the keepalive is set then IoTHubTransport_MQTT_Common_SetOption shall disconnect and reconnect with the specified keepalive value.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2818 | DisconnectFromClient(transport_data); |
AzureIoTClient | 11:31ebaeb51e1e | 2819 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2820 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2821 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 11:31ebaeb51e1e | 2822 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2823 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_039: [If the option parameter is set to "x509certificate" then the value shall be a const char of the certificate to be used for x509.] */ |
AzureIoTClient | 21:167f8fcf6fb1 | 2824 | else if ((strcmp(OPTION_X509_CERT, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN)) |
AzureIoTClient | 11:31ebaeb51e1e | 2825 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2826 | LogError("x509certificate specified, but authentication method is not x509"); |
AzureIoTClient | 11:31ebaeb51e1e | 2827 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 11:31ebaeb51e1e | 2828 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2829 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_040: [If the option parameter is set to "x509privatekey" then the value shall be a const char of the RSA Private Key to be used for x509.] */ |
AzureIoTClient | 21:167f8fcf6fb1 | 2830 | else if ((strcmp(OPTION_X509_PRIVATE_KEY, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN)) |
AzureIoTClient | 11:31ebaeb51e1e | 2831 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2832 | LogError("x509privatekey specified, but authentication method is not x509"); |
AzureIoTClient | 11:31ebaeb51e1e | 2833 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 11:31ebaeb51e1e | 2834 | } |
AzureIoTClient | 20:594780be216d | 2835 | else if (strcmp(OPTION_HTTP_PROXY, option) == 0) |
AzureIoTClient | 20:594780be216d | 2836 | { |
AzureIoTClient | 20:594780be216d | 2837 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_001: [ If `option` is `proxy_data`, `value` shall be used as an `HTTP_PROXY_OPTIONS*`. ]*/ |
AzureIoTClient | 20:594780be216d | 2838 | HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value; |
AzureIoTClient | 20:594780be216d | 2839 | |
AzureIoTClient | 20:594780be216d | 2840 | if (transport_data->xioTransport != NULL) |
AzureIoTClient | 20:594780be216d | 2841 | { |
AzureIoTClient | 20:594780be216d | 2842 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_007: [ If the underlying IO has already been created, then `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ |
AzureIoTClient | 20:594780be216d | 2843 | LogError("Cannot set proxy option once the underlying IO is created"); |
AzureIoTClient | 20:594780be216d | 2844 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 20:594780be216d | 2845 | } |
AzureIoTClient | 20:594780be216d | 2846 | else if (proxy_options->host_address == NULL) |
AzureIoTClient | 20:594780be216d | 2847 | { |
AzureIoTClient | 20:594780be216d | 2848 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_003: [ If `host_address` is NULL, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 20:594780be216d | 2849 | LogError("NULL host_address in proxy options"); |
AzureIoTClient | 20:594780be216d | 2850 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 20:594780be216d | 2851 | } |
AzureIoTClient | 20:594780be216d | 2852 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_006: [ If only one of `username` and `password` is NULL, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ |
AzureIoTClient | 20:594780be216d | 2853 | else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) && |
AzureIoTClient | 20:594780be216d | 2854 | (proxy_options->username != proxy_options->password)) |
AzureIoTClient | 20:594780be216d | 2855 | { |
AzureIoTClient | 20:594780be216d | 2856 | LogError("Only one of username and password for proxy settings was NULL"); |
AzureIoTClient | 20:594780be216d | 2857 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 20:594780be216d | 2858 | } |
AzureIoTClient | 20:594780be216d | 2859 | else |
AzureIoTClient | 20:594780be216d | 2860 | { |
AzureIoTClient | 20:594780be216d | 2861 | char* copied_proxy_hostname = NULL; |
AzureIoTClient | 20:594780be216d | 2862 | char* copied_proxy_username = NULL; |
AzureIoTClient | 20:594780be216d | 2863 | char* copied_proxy_password = NULL; |
AzureIoTClient | 20:594780be216d | 2864 | |
AzureIoTClient | 20:594780be216d | 2865 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_002: [ The fields `host_address`, `port`, `username` and `password` shall be saved for later used (needed when creating the underlying IO to be used by the transport). ]*/ |
AzureIoTClient | 20:594780be216d | 2866 | transport_data->http_proxy_port = proxy_options->port; |
AzureIoTClient | 20:594780be216d | 2867 | if (mallocAndStrcpy_s(&copied_proxy_hostname, proxy_options->host_address) != 0) |
AzureIoTClient | 20:594780be216d | 2868 | { |
AzureIoTClient | 20:594780be216d | 2869 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ |
AzureIoTClient | 20:594780be216d | 2870 | LogError("Cannot copy HTTP proxy hostname"); |
AzureIoTClient | 20:594780be216d | 2871 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 20:594780be216d | 2872 | } |
AzureIoTClient | 20:594780be216d | 2873 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/ |
AzureIoTClient | 20:594780be216d | 2874 | else if ((proxy_options->username != NULL) && (mallocAndStrcpy_s(&copied_proxy_username, proxy_options->username) != 0)) |
AzureIoTClient | 20:594780be216d | 2875 | { |
AzureIoTClient | 20:594780be216d | 2876 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ |
AzureIoTClient | 20:594780be216d | 2877 | free(copied_proxy_hostname); |
AzureIoTClient | 20:594780be216d | 2878 | LogError("Cannot copy HTTP proxy username"); |
AzureIoTClient | 20:594780be216d | 2879 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 20:594780be216d | 2880 | } |
AzureIoTClient | 20:594780be216d | 2881 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/ |
AzureIoTClient | 20:594780be216d | 2882 | else if ((proxy_options->password != NULL) && (mallocAndStrcpy_s(&copied_proxy_password, proxy_options->password) != 0)) |
AzureIoTClient | 20:594780be216d | 2883 | { |
AzureIoTClient | 20:594780be216d | 2884 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ |
AzureIoTClient | 20:594780be216d | 2885 | if (copied_proxy_username != NULL) |
AzureIoTClient | 20:594780be216d | 2886 | { |
AzureIoTClient | 20:594780be216d | 2887 | free(copied_proxy_username); |
AzureIoTClient | 20:594780be216d | 2888 | } |
AzureIoTClient | 20:594780be216d | 2889 | free(copied_proxy_hostname); |
AzureIoTClient | 20:594780be216d | 2890 | LogError("Cannot copy HTTP proxy password"); |
AzureIoTClient | 20:594780be216d | 2891 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 20:594780be216d | 2892 | } |
AzureIoTClient | 20:594780be216d | 2893 | else |
AzureIoTClient | 20:594780be216d | 2894 | { |
AzureIoTClient | 20:594780be216d | 2895 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_009: [ When setting the proxy options succeeds any previously saved proxy options shall be freed. ]*/ |
AzureIoTClient | 20:594780be216d | 2896 | free_proxy_data(transport_data); |
AzureIoTClient | 20:594780be216d | 2897 | |
AzureIoTClient | 20:594780be216d | 2898 | transport_data->http_proxy_hostname = copied_proxy_hostname; |
AzureIoTClient | 20:594780be216d | 2899 | transport_data->http_proxy_username = copied_proxy_username; |
AzureIoTClient | 20:594780be216d | 2900 | transport_data->http_proxy_password = copied_proxy_password; |
AzureIoTClient | 20:594780be216d | 2901 | |
AzureIoTClient | 20:594780be216d | 2902 | /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_008: [ If setting the `proxy_data` option suceeds, `IoTHubTransport_MQTT_Common_SetOption` shall return `IOTHUB_CLIENT_OK` ]*/ |
AzureIoTClient | 20:594780be216d | 2903 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 20:594780be216d | 2904 | } |
AzureIoTClient | 20:594780be216d | 2905 | } |
AzureIoTClient | 20:594780be216d | 2906 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2907 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2908 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2909 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_039: [If the option parameter is set to "x509certificate" then the value shall be a const char of the certificate to be used for x509.] */ |
AzureIoTClient | 21:167f8fcf6fb1 | 2910 | if ( ((strcmp(OPTION_X509_CERT, option) == 0) || (strcmp(OPTION_X509_PRIVATE_KEY, option) == 0)) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509) ) |
AzureIoTClient | 21:167f8fcf6fb1 | 2911 | { |
AzureIoTClient | 21:167f8fcf6fb1 | 2912 | IoTHubClient_Auth_Set_x509_Type(transport_data->authorization_module, true); |
AzureIoTClient | 21:167f8fcf6fb1 | 2913 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2914 | |
AzureIoTClient | 12:658ca6865de2 | 2915 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_032: [IoTHubTransport_MQTT_Common_SetOption shall pass down the option to xio_setoption if the option parameter is not a known option string for the MQTT transport.] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2916 | if (GetTransportProviderIfNecessary(transport_data) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2917 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2918 | if (xio_setoption(transport_data->xioTransport, option, value) == 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2919 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2920 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 11:31ebaeb51e1e | 2921 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2922 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2923 | { |
AzureIoTClient | 12:658ca6865de2 | 2924 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_132: [IoTHubTransport_MQTT_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG xio_setoption fails] */ |
AzureIoTClient | 11:31ebaeb51e1e | 2925 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 11:31ebaeb51e1e | 2926 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2927 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2928 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2929 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2930 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 11:31ebaeb51e1e | 2931 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2932 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2933 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2934 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2935 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2936 | |
AzureIoTClient | 39:6231984e0179 | 2937 | IOTHUB_DEVICE_HANDLE IoTHubTransport_MQTT_Common_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_CORE_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend) |
AzureIoTClient | 11:31ebaeb51e1e | 2938 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2939 | IOTHUB_DEVICE_HANDLE result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2940 | (void)iotHubClientHandle; |
AzureIoTClient | 11:31ebaeb51e1e | 2941 | |
AzureIoTClient | 12:658ca6865de2 | 2942 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_001: [ IoTHubTransport_MQTT_Common_Register shall return NULL if the TRANSPORT_LL_HANDLE is NULL.] |
AzureIoTClient | 12:658ca6865de2 | 2943 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if device or waitingToSend are NULL.] |
AzureIoTClient | 11:31ebaeb51e1e | 2944 | if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL)) |
AzureIoTClient | 11:31ebaeb51e1e | 2945 | { |
AzureIoTClient | 12:658ca6865de2 | 2946 | LogError("IoTHubTransport_MQTT_Common_Register: handle, device or waitingToSend is NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 2947 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2948 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2949 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2950 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2951 | MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2952 | |
AzureIoTClient | 12:658ca6865de2 | 2953 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_001: [ IoTHubTransport_MQTT_Common_Register shall return NULL if deviceId, or both deviceKey and deviceSasToken are NULL.] |
AzureIoTClient | 11:31ebaeb51e1e | 2954 | if (device->deviceId == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 2955 | { |
AzureIoTClient | 12:658ca6865de2 | 2956 | LogError("IoTHubTransport_MQTT_Common_Register: deviceId is NULL."); |
AzureIoTClient | 11:31ebaeb51e1e | 2957 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2958 | } |
AzureIoTClient | 12:658ca6865de2 | 2959 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if both deviceKey and deviceSasToken are provided.] |
AzureIoTClient | 11:31ebaeb51e1e | 2960 | else if ((device->deviceKey != NULL) && (device->deviceSasToken != NULL)) |
AzureIoTClient | 11:31ebaeb51e1e | 2961 | { |
AzureIoTClient | 12:658ca6865de2 | 2962 | LogError("IoTHubTransport_MQTT_Common_Register: Both deviceKey and deviceSasToken are defined. Only one can be used."); |
AzureIoTClient | 11:31ebaeb51e1e | 2963 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2964 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2965 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2966 | { |
AzureIoTClient | 12:658ca6865de2 | 2967 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_003: [ IoTHubTransport_MQTT_Common_Register shall return NULL if deviceId or deviceKey do not match the deviceId and deviceKey passed in during IoTHubTransport_MQTT_Common_Create.] |
AzureIoTClient | 11:31ebaeb51e1e | 2968 | if (strcmp(STRING_c_str(transport_data->device_id), device->deviceId) != 0) |
AzureIoTClient | 11:31ebaeb51e1e | 2969 | { |
AzureIoTClient | 12:658ca6865de2 | 2970 | LogError("IoTHubTransport_MQTT_Common_Register: deviceId does not match."); |
AzureIoTClient | 11:31ebaeb51e1e | 2971 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2972 | } |
AzureIoTClient | 21:167f8fcf6fb1 | 2973 | else if (IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module) == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY && |
AzureIoTClient | 21:167f8fcf6fb1 | 2974 | (strcmp(IoTHubClient_Auth_Get_DeviceKey(transport_data->authorization_module), device->deviceKey) != 0)) |
AzureIoTClient | 11:31ebaeb51e1e | 2975 | { |
AzureIoTClient | 12:658ca6865de2 | 2976 | LogError("IoTHubTransport_MQTT_Common_Register: deviceKey does not match."); |
AzureIoTClient | 11:31ebaeb51e1e | 2977 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2978 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2979 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2980 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2981 | if (transport_data->isRegistered == true) |
AzureIoTClient | 11:31ebaeb51e1e | 2982 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2983 | LogError("Transport already has device registered by id: [%s]", device->deviceId); |
AzureIoTClient | 11:31ebaeb51e1e | 2984 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 2985 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2986 | else |
AzureIoTClient | 11:31ebaeb51e1e | 2987 | { |
AzureIoTClient | 11:31ebaeb51e1e | 2988 | transport_data->isRegistered = true; |
AzureIoTClient | 12:658ca6865de2 | 2989 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_004: [ IoTHubTransport_MQTT_Common_Register shall return the TRANSPORT_LL_HANDLE as the IOTHUB_DEVICE_HANDLE. ] |
AzureIoTClient | 11:31ebaeb51e1e | 2990 | result = (IOTHUB_DEVICE_HANDLE)handle; |
AzureIoTClient | 11:31ebaeb51e1e | 2991 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2992 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2993 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2994 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2995 | |
AzureIoTClient | 11:31ebaeb51e1e | 2996 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 2997 | } |
AzureIoTClient | 11:31ebaeb51e1e | 2998 | |
AzureIoTClient | 11:31ebaeb51e1e | 2999 | void IoTHubTransport_MQTT_Common_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle) |
AzureIoTClient | 11:31ebaeb51e1e | 3000 | { |
AzureIoTClient | 14:4dc2b011be33 | 3001 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_005: [ If deviceHandle is NULL `IoTHubTransport_MQTT_Common_Unregister` shall do nothing. ] |
AzureIoTClient | 11:31ebaeb51e1e | 3002 | if (deviceHandle != NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 3003 | { |
AzureIoTClient | 11:31ebaeb51e1e | 3004 | MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)deviceHandle; |
AzureIoTClient | 11:31ebaeb51e1e | 3005 | |
AzureIoTClient | 11:31ebaeb51e1e | 3006 | transport_data->isRegistered = false; |
AzureIoTClient | 11:31ebaeb51e1e | 3007 | } |
AzureIoTClient | 11:31ebaeb51e1e | 3008 | } |
AzureIoTClient | 11:31ebaeb51e1e | 3009 | |
AzureIoTClient | 11:31ebaeb51e1e | 3010 | STRING_HANDLE IoTHubTransport_MQTT_Common_GetHostname(TRANSPORT_LL_HANDLE handle) |
AzureIoTClient | 11:31ebaeb51e1e | 3011 | { |
AzureIoTClient | 11:31ebaeb51e1e | 3012 | STRING_HANDLE result; |
AzureIoTClient | 12:658ca6865de2 | 3013 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_001: [ If handle is NULL then IoTHubTransport_MQTT_Common_GetHostname shall fail and return NULL. ]*/ |
AzureIoTClient | 11:31ebaeb51e1e | 3014 | if (handle == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 3015 | { |
AzureIoTClient | 11:31ebaeb51e1e | 3016 | result = NULL; |
AzureIoTClient | 11:31ebaeb51e1e | 3017 | } |
AzureIoTClient | 22:07fec4d325b6 | 3018 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_002: [ Otherwise IoTHubTransport_MQTT_Common_GetHostname shall return a non-NULL STRING_HANDLE containg the hostname. ]*/ |
AzureIoTClient | 22:07fec4d325b6 | 3019 | else if ((result = STRING_clone(((MQTTTRANSPORT_HANDLE_DATA*)(handle))->hostAddress)) == NULL) |
AzureIoTClient | 11:31ebaeb51e1e | 3020 | { |
AzureIoTClient | 22:07fec4d325b6 | 3021 | LogError("Cannot provide the target host name (STRING_clone failed)."); |
AzureIoTClient | 11:31ebaeb51e1e | 3022 | } |
AzureIoTClient | 22:07fec4d325b6 | 3023 | |
AzureIoTClient | 11:31ebaeb51e1e | 3024 | return result; |
AzureIoTClient | 11:31ebaeb51e1e | 3025 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3026 | |
AzureIoTClient | 19:f87dfe76bc70 | 3027 | IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition) |
AzureIoTClient | 19:f87dfe76bc70 | 3028 | { |
AzureIoTClient | 19:f87dfe76bc70 | 3029 | (void)disposition; |
AzureIoTClient | 19:f87dfe76bc70 | 3030 | |
AzureIoTClient | 19:f87dfe76bc70 | 3031 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 19:f87dfe76bc70 | 3032 | if (message_data) |
AzureIoTClient | 19:f87dfe76bc70 | 3033 | { |
AzureIoTClient | 19:f87dfe76bc70 | 3034 | if (message_data->messageHandle) |
AzureIoTClient | 19:f87dfe76bc70 | 3035 | { |
AzureIoTClient | 19:f87dfe76bc70 | 3036 | IoTHubMessage_Destroy(message_data->messageHandle); |
AzureIoTClient | 19:f87dfe76bc70 | 3037 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 19:f87dfe76bc70 | 3038 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3039 | else |
AzureIoTClient | 19:f87dfe76bc70 | 3040 | { |
AzureIoTClient | 19:f87dfe76bc70 | 3041 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_10_002: [If any of the messageData fields are NULL, IoTHubTransport_MQTT_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_ERROR. ]*/ |
AzureIoTClient | 19:f87dfe76bc70 | 3042 | LogError("message handle is NULL"); |
AzureIoTClient | 19:f87dfe76bc70 | 3043 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 19:f87dfe76bc70 | 3044 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3045 | free(message_data); |
AzureIoTClient | 19:f87dfe76bc70 | 3046 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3047 | else |
AzureIoTClient | 19:f87dfe76bc70 | 3048 | { |
AzureIoTClient | 19:f87dfe76bc70 | 3049 | /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_10_001: [If messageData is NULL, IoTHubTransport_MQTT_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_ERROR. ]*/ |
AzureIoTClient | 19:f87dfe76bc70 | 3050 | LogError("message_data is NULL"); |
AzureIoTClient | 19:f87dfe76bc70 | 3051 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 19:f87dfe76bc70 | 3052 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3053 | return result; |
AzureIoTClient | 19:f87dfe76bc70 | 3054 | } |
AzureIoTClient | 19:f87dfe76bc70 | 3055 |