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