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