Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp iothub_client_sample_amqp ... more
iothubtransport_amqp_common.c@46:c688c75b63b9, 2017-12-15 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Dec 15 14:08:19 2017 -0800
- Revision:
- 46:c688c75b63b9
- Parent:
- 45:c09fac270e60
- Child:
- 47:8a238e75a0f7
1.1.29
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| AzureIoTClient | 25:63f7d371c030 | 1 | // Copyright (c) Microsoft. All rights reserved. | 
| AzureIoTClient | 25:63f7d371c030 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. | 
| AzureIoTClient | 25:63f7d371c030 | 3 | |
| AzureIoTClient | 25:63f7d371c030 | 4 | #include <stdlib.h> | 
| AzureIoTClient | 25:63f7d371c030 | 5 | #include <stdint.h> | 
| AzureIoTClient | 31:adadaef857c1 | 6 | #include <stdbool.h> | 
| AzureIoTClient | 25:63f7d371c030 | 7 | #include <time.h> | 
| AzureIoTClient | 25:63f7d371c030 | 8 | #include <limits.h> | 
| AzureIoTClient | 29:7e8852b14e3b | 9 | #include "azure_c_shared_utility/optimize_size.h" | 
| AzureIoTClient | 25:63f7d371c030 | 10 | #include "azure_c_shared_utility/agenttime.h" | 
| AzureIoTClient | 25:63f7d371c030 | 11 | #include "azure_c_shared_utility/gballoc.h" | 
| AzureIoTClient | 25:63f7d371c030 | 12 | #include "azure_c_shared_utility/crt_abstractions.h" | 
| AzureIoTClient | 30:20a85b733111 | 13 | #include "azure_c_shared_utility/singlylinkedlist.h" | 
| AzureIoTClient | 25:63f7d371c030 | 14 | #include "azure_c_shared_utility/doublylinkedlist.h" | 
| AzureIoTClient | 25:63f7d371c030 | 15 | #include "azure_c_shared_utility/xlogging.h" | 
| AzureIoTClient | 25:63f7d371c030 | 16 | #include "azure_c_shared_utility/platform.h" | 
| AzureIoTClient | 25:63f7d371c030 | 17 | #include "azure_c_shared_utility/strings.h" | 
| AzureIoTClient | 25:63f7d371c030 | 18 | #include "azure_c_shared_utility/urlencode.h" | 
| AzureIoTClient | 25:63f7d371c030 | 19 | #include "azure_c_shared_utility/tlsio.h" | 
| AzureIoTClient | 30:20a85b733111 | 20 | #include "azure_c_shared_utility/optionhandler.h" | 
| AzureIoTClient | 31:adadaef857c1 | 21 | #include "azure_c_shared_utility/shared_util_options.h" | 
| AzureIoTClient | 31:adadaef857c1 | 22 | #include "azure_c_shared_utility/macro_utils.h" | 
| AzureIoTClient | 25:63f7d371c030 | 23 | |
| AzureIoTClient | 25:63f7d371c030 | 24 | #include "azure_uamqp_c/cbs.h" | 
| AzureIoTClient | 30:20a85b733111 | 25 | #include "azure_uamqp_c/session.h" | 
| AzureIoTClient | 25:63f7d371c030 | 26 | #include "azure_uamqp_c/message.h" | 
| AzureIoTClient | 25:63f7d371c030 | 27 | #include "azure_uamqp_c/messaging.h" | 
| AzureIoTClient | 25:63f7d371c030 | 28 | |
| AzureIoTClient | 25:63f7d371c030 | 29 | #include "iothub_client_ll.h" | 
| AzureIoTClient | 25:63f7d371c030 | 30 | #include "iothub_client_options.h" | 
| AzureIoTClient | 25:63f7d371c030 | 31 | #include "iothub_client_private.h" | 
| AzureIoTClient | 25:63f7d371c030 | 32 | #include "iothubtransportamqp_methods.h" | 
| AzureIoTClient | 32:babfd49032ff | 33 | #include "iothub_client_retry_control.h" | 
| AzureIoTClient | 25:63f7d371c030 | 34 | #include "iothubtransport_amqp_common.h" | 
| AzureIoTClient | 30:20a85b733111 | 35 | #include "iothubtransport_amqp_connection.h" | 
| AzureIoTClient | 30:20a85b733111 | 36 | #include "iothubtransport_amqp_device.h" | 
| AzureIoTClient | 25:63f7d371c030 | 37 | #include "iothub_client_version.h" | 
| AzureIoTClient | 25:63f7d371c030 | 38 | |
| AzureIoTClient | 30:20a85b733111 | 39 | #define RESULT_OK 0 | 
| AzureIoTClient | 30:20a85b733111 | 40 | #define INDEFINITE_TIME ((time_t)(-1)) | 
| AzureIoTClient | 30:20a85b733111 | 41 | #define DEFAULT_CBS_REQUEST_TIMEOUT_SECS 30 | 
| AzureIoTClient | 30:20a85b733111 | 42 | #define DEFAULT_DEVICE_STATE_CHANGE_TIMEOUT_SECS 60 | 
| AzureIoTClient | 30:20a85b733111 | 43 | #define DEFAULT_EVENT_SEND_TIMEOUT_SECS 300 | 
| AzureIoTClient | 30:20a85b733111 | 44 | #define DEFAULT_SAS_TOKEN_LIFETIME_SECS 3600 | 
| AzureIoTClient | 30:20a85b733111 | 45 | #define DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS 1800 | 
| AzureIoTClient | 30:20a85b733111 | 46 | #define MAX_NUMBER_OF_DEVICE_FAILURES 5 | 
| AzureIoTClient | 37:abd16824f63b | 47 | #define DEFAULT_C2D_KEEP_ALIVE_FREQ_SECS 240 | 
| AzureIoTClient | 32:babfd49032ff | 48 | #define DEFAULT_RETRY_POLICY IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER | 
| AzureIoTClient | 32:babfd49032ff | 49 | // DEFAULT_MAX_RETRY_TIME_IN_SECS = 0 means infinite retry. | 
| AzureIoTClient | 32:babfd49032ff | 50 | #define DEFAULT_MAX_RETRY_TIME_IN_SECS 0 | 
| AzureIoTClient | 25:63f7d371c030 | 51 | |
| AzureIoTClient | 30:20a85b733111 | 52 | // ---------- Data Definitions ---------- // | 
| AzureIoTClient | 25:63f7d371c030 | 53 | |
| AzureIoTClient | 30:20a85b733111 | 54 | typedef enum AMQP_TRANSPORT_AUTHENTICATION_MODE_TAG | 
| AzureIoTClient | 25:63f7d371c030 | 55 | { | 
| AzureIoTClient | 34:51d158b409d2 | 56 | AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET, | 
| AzureIoTClient | 34:51d158b409d2 | 57 | AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS, | 
| AzureIoTClient | 34:51d158b409d2 | 58 | AMQP_TRANSPORT_AUTHENTICATION_MODE_X509 | 
| AzureIoTClient | 30:20a85b733111 | 59 | } AMQP_TRANSPORT_AUTHENTICATION_MODE; | 
| AzureIoTClient | 39:e98d5df6dc74 | 60 | |
| AzureIoTClient | 46:c688c75b63b9 | 61 | /* | 
| AzureIoTClient | 46:c688c75b63b9 | 62 | Definition of transport states: | 
| AzureIoTClient | 32:babfd49032ff | 63 | |
| AzureIoTClient | 46:c688c75b63b9 | 64 | AMQP_TRANSPORT_STATE_NOT_CONNECTED: Initial state when the transport is created. | 
| AzureIoTClient | 46:c688c75b63b9 | 65 | AMQP_TRANSPORT_STATE_CONNECTING: First connection ever. | 
| AzureIoTClient | 46:c688c75b63b9 | 66 | AMQP_TRANSPORT_STATE_CONNECTED: Transition from AMQP_TRANSPORT_STATE_CONNECTING or AMQP_TRANSPORT_STATE_RECONNECTING. | 
| AzureIoTClient | 46:c688c75b63b9 | 67 | AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED: When a failure occurred and the transport identifies a reconnection is needed. | 
| AzureIoTClient | 46:c688c75b63b9 | 68 | AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION: Transition from AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED after all prep is done (transient instances are destroyed, devices are stopped). | 
| AzureIoTClient | 46:c688c75b63b9 | 69 | AMQP_TRANSPORT_STATE_RECONNECTING: Transition from AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION. | 
| AzureIoTClient | 46:c688c75b63b9 | 70 | AMQP_TRANSPORT_STATE_NOT_CONNECTED_NO_MORE_RETRIES: State reached if the maximum number/length of reconnections has been reached. | 
| AzureIoTClient | 46:c688c75b63b9 | 71 | AMQP_TRANSPORT_STATE_BEING_DESTROYED: State set if IoTHubTransport_AMQP_Common_Destroy function is invoked. | 
| AzureIoTClient | 46:c688c75b63b9 | 72 | */ | 
| AzureIoTClient | 46:c688c75b63b9 | 73 | |
| AzureIoTClient | 46:c688c75b63b9 | 74 | #define AMQP_TRANSPORT_STATE_STRINGS \ | 
| AzureIoTClient | 46:c688c75b63b9 | 75 | AMQP_TRANSPORT_STATE_NOT_CONNECTED, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 76 | AMQP_TRANSPORT_STATE_CONNECTING, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 77 | AMQP_TRANSPORT_STATE_CONNECTED, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 78 | AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 79 | AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 80 | AMQP_TRANSPORT_STATE_RECONNECTING, \ | 
| AzureIoTClient | 46:c688c75b63b9 | 81 | AMQP_TRANSPORT_STATE_NOT_CONNECTED_NO_MORE_RETRIES, \ | 
| AzureIoTClient | 34:51d158b409d2 | 82 | AMQP_TRANSPORT_STATE_BEING_DESTROYED | 
| AzureIoTClient | 32:babfd49032ff | 83 | |
| AzureIoTClient | 32:babfd49032ff | 84 | DEFINE_LOCAL_ENUM(AMQP_TRANSPORT_STATE, AMQP_TRANSPORT_STATE_STRINGS); | 
| AzureIoTClient | 32:babfd49032ff | 85 | |
| AzureIoTClient | 30:20a85b733111 | 86 | typedef struct AMQP_TRANSPORT_INSTANCE_TAG | 
| AzureIoTClient | 30:20a85b733111 | 87 | { | 
| AzureIoTClient | 30:20a85b733111 | 88 | STRING_HANDLE iothub_host_fqdn; // FQDN of the IoT Hub. | 
| AzureIoTClient | 30:20a85b733111 | 89 | XIO_HANDLE tls_io; // TSL I/O transport. | 
| AzureIoTClient | 30:20a85b733111 | 90 | AMQP_GET_IO_TRANSPORT underlying_io_transport_provider; // Pointer to the function that creates the TLS I/O (internal use only). | 
| AzureIoTClient | 34:51d158b409d2 | 91 | AMQP_CONNECTION_HANDLE amqp_connection; // Base amqp connection with service. | 
| AzureIoTClient | 34:51d158b409d2 | 92 | AMQP_CONNECTION_STATE amqp_connection_state; // Current state of the amqp_connection. | 
| AzureIoTClient | 34:51d158b409d2 | 93 | AMQP_TRANSPORT_AUTHENTICATION_MODE preferred_authentication_mode; // Used to avoid registered devices using different authentication modes. | 
| AzureIoTClient | 34:51d158b409d2 | 94 | SINGLYLINKEDLIST_HANDLE registered_devices; // List of devices currently registered in this transport. | 
| AzureIoTClient | 30:20a85b733111 | 95 | bool is_trace_on; // Turns logging on and off. | 
| AzureIoTClient | 30:20a85b733111 | 96 | OPTIONHANDLER_HANDLE saved_tls_options; // Here are the options from the xio layer if any is saved. | 
| AzureIoTClient | 34:51d158b409d2 | 97 | AMQP_TRANSPORT_STATE state; // Current state of the transport. | 
| AzureIoTClient | 34:51d158b409d2 | 98 | RETRY_CONTROL_HANDLE connection_retry_control; // Controls when the re-connection attempt should occur. | 
| AzureIoTClient | 37:abd16824f63b | 99 | size_t c2d_keep_alive_freq_secs; // Service to device keep alive frequency | 
| AzureIoTClient | 31:adadaef857c1 | 100 | |
| AzureIoTClient | 31:adadaef857c1 | 101 | char* http_proxy_hostname; | 
| AzureIoTClient | 31:adadaef857c1 | 102 | int http_proxy_port; | 
| AzureIoTClient | 31:adadaef857c1 | 103 | char* http_proxy_username; | 
| AzureIoTClient | 31:adadaef857c1 | 104 | char* http_proxy_password; | 
| AzureIoTClient | 34:51d158b409d2 | 105 | |
| AzureIoTClient | 34:51d158b409d2 | 106 | size_t option_sas_token_lifetime_secs; // Device-specific option. | 
| AzureIoTClient | 34:51d158b409d2 | 107 | size_t option_sas_token_refresh_time_secs; // Device-specific option. | 
| AzureIoTClient | 34:51d158b409d2 | 108 | size_t option_cbs_request_timeout_secs; // Device-specific option. | 
| AzureIoTClient | 34:51d158b409d2 | 109 | size_t option_send_event_timeout_secs; // Device-specific option. | 
| AzureIoTClient | 34:51d158b409d2 | 110 | |
| AzureIoTClient | 34:51d158b409d2 | 111 | // Auth module used to generating handle authorization | 
| AzureIoTClient | 34:51d158b409d2 | 112 | IOTHUB_AUTHORIZATION_HANDLE authorization_module; // with either SAS Token, x509 Certs, and Device SAS Token | 
| AzureIoTClient | 25:63f7d371c030 | 113 | } AMQP_TRANSPORT_INSTANCE; | 
| AzureIoTClient | 25:63f7d371c030 | 114 | |
| AzureIoTClient | 30:20a85b733111 | 115 | typedef struct AMQP_TRANSPORT_DEVICE_INSTANCE_TAG | 
| AzureIoTClient | 25:63f7d371c030 | 116 | { | 
| AzureIoTClient | 30:20a85b733111 | 117 | STRING_HANDLE device_id; // Identity of the device. | 
| AzureIoTClient | 34:51d158b409d2 | 118 | DEVICE_HANDLE device_handle; // Logic unit that performs authentication, messaging, etc. | 
| AzureIoTClient | 30:20a85b733111 | 119 | IOTHUB_CLIENT_LL_HANDLE iothub_client_handle; // Saved reference to the IoTHub LL Client. | 
| AzureIoTClient | 30:20a85b733111 | 120 | AMQP_TRANSPORT_INSTANCE* transport_instance; // Saved reference to the transport the device is registered on. | 
| AzureIoTClient | 34:51d158b409d2 | 121 | PDLIST_ENTRY waiting_to_send; // List of events waiting to be sent to the iot hub (i.e., haven't been processed by the transport yet). | 
| AzureIoTClient | 34:51d158b409d2 | 122 | DEVICE_STATE device_state; // Current state of the device_handle instance. | 
| AzureIoTClient | 34:51d158b409d2 | 123 | size_t number_of_previous_failures; // Number of times the device has failed in sequence; this value is reset to 0 if device succeeds to authenticate, send and/or recv messages. | 
| AzureIoTClient | 34:51d158b409d2 | 124 | size_t number_of_send_event_complete_failures; // Number of times on_event_send_complete was called in row with an error. | 
| AzureIoTClient | 34:51d158b409d2 | 125 | time_t time_of_last_state_change; // Time the device_handle last changed state; used to track timeouts of device_start_async and device_stop. | 
| AzureIoTClient | 34:51d158b409d2 | 126 | unsigned int max_state_change_timeout_secs; // Maximum number of seconds allowed for device_handle to complete start and stop state changes. | 
| AzureIoTClient | 25:63f7d371c030 | 127 | // the methods portion | 
| AzureIoTClient | 30:20a85b733111 | 128 | IOTHUBTRANSPORT_AMQP_METHODS_HANDLE methods_handle; // Handle to instance of module that deals with device methods for AMQP. | 
| AzureIoTClient | 25:63f7d371c030 | 129 | // is subscription for methods needed? | 
| AzureIoTClient | 30:20a85b733111 | 130 | bool subscribe_methods_needed; // Indicates if should subscribe for device methods. | 
| AzureIoTClient | 25:63f7d371c030 | 131 | // is the transport subscribed for methods? | 
| AzureIoTClient | 30:20a85b733111 | 132 | bool subscribed_for_methods; // Indicates if device is subscribed for device methods. | 
| AzureIoTClient | 30:20a85b733111 | 133 | } AMQP_TRANSPORT_DEVICE_INSTANCE; | 
| AzureIoTClient | 25:63f7d371c030 | 134 | |
| AzureIoTClient | 30:20a85b733111 | 135 | typedef struct MESSAGE_DISPOSITION_CONTEXT_TAG | 
| AzureIoTClient | 30:20a85b733111 | 136 | { | 
| AzureIoTClient | 34:51d158b409d2 | 137 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state; | 
| AzureIoTClient | 30:20a85b733111 | 138 | char* link_name; | 
| AzureIoTClient | 30:20a85b733111 | 139 | delivery_number message_id; | 
| AzureIoTClient | 30:20a85b733111 | 140 | } MESSAGE_DISPOSITION_CONTEXT; | 
| AzureIoTClient | 25:63f7d371c030 | 141 | |
| AzureIoTClient | 39:e98d5df6dc74 | 142 | typedef struct AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT_TAG | 
| AzureIoTClient | 39:e98d5df6dc74 | 143 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 144 | uint32_t item_id; | 
| AzureIoTClient | 41:71c01aa3df1a | 145 | IOTHUB_CLIENT_LL_HANDLE client_handle; | 
| AzureIoTClient | 39:e98d5df6dc74 | 146 | } AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT; | 
| AzureIoTClient | 39:e98d5df6dc74 | 147 | |
| AzureIoTClient | 39:e98d5df6dc74 | 148 | |
| AzureIoTClient | 30:20a85b733111 | 149 | // ---------- General Helpers ---------- // | 
| AzureIoTClient | 25:63f7d371c030 | 150 | |
| AzureIoTClient | 31:adadaef857c1 | 151 | static void free_proxy_data(AMQP_TRANSPORT_INSTANCE* amqp_transport_instance) | 
| AzureIoTClient | 31:adadaef857c1 | 152 | { | 
| AzureIoTClient | 31:adadaef857c1 | 153 | if (amqp_transport_instance->http_proxy_hostname != NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 154 | { | 
| AzureIoTClient | 31:adadaef857c1 | 155 | free(amqp_transport_instance->http_proxy_hostname); | 
| AzureIoTClient | 31:adadaef857c1 | 156 | amqp_transport_instance->http_proxy_hostname = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 157 | } | 
| AzureIoTClient | 31:adadaef857c1 | 158 | |
| AzureIoTClient | 31:adadaef857c1 | 159 | if (amqp_transport_instance->http_proxy_username != NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 160 | { | 
| AzureIoTClient | 31:adadaef857c1 | 161 | free(amqp_transport_instance->http_proxy_username); | 
| AzureIoTClient | 31:adadaef857c1 | 162 | amqp_transport_instance->http_proxy_username = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 163 | } | 
| AzureIoTClient | 31:adadaef857c1 | 164 | |
| AzureIoTClient | 31:adadaef857c1 | 165 | if (amqp_transport_instance->http_proxy_password != NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 166 | { | 
| AzureIoTClient | 31:adadaef857c1 | 167 | free(amqp_transport_instance->http_proxy_password); | 
| AzureIoTClient | 31:adadaef857c1 | 168 | amqp_transport_instance->http_proxy_password = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 169 | } | 
| AzureIoTClient | 31:adadaef857c1 | 170 | } | 
| AzureIoTClient | 31:adadaef857c1 | 171 | |
| AzureIoTClient | 30:20a85b733111 | 172 | static STRING_HANDLE get_target_iothub_fqdn(const IOTHUBTRANSPORT_CONFIG* config) | 
| AzureIoTClient | 25:63f7d371c030 | 173 | { | 
| AzureIoTClient | 34:51d158b409d2 | 174 | STRING_HANDLE fqdn; | 
| AzureIoTClient | 30:20a85b733111 | 175 | |
| AzureIoTClient | 34:51d158b409d2 | 176 | if (config->upperConfig->protocolGatewayHostName == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 177 | { | 
| AzureIoTClient | 34:51d158b409d2 | 178 | if ((fqdn = STRING_construct_sprintf("%s.%s", config->upperConfig->iotHubName, config->upperConfig->iotHubSuffix)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 179 | { | 
| AzureIoTClient | 34:51d158b409d2 | 180 | LogError("Failed to copy iotHubName and iotHubSuffix (STRING_construct_sprintf failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 181 | } | 
| AzureIoTClient | 34:51d158b409d2 | 182 | } | 
| AzureIoTClient | 34:51d158b409d2 | 183 | else if ((fqdn = STRING_construct(config->upperConfig->protocolGatewayHostName)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 184 | { | 
| AzureIoTClient | 34:51d158b409d2 | 185 | LogError("Failed to copy protocolGatewayHostName (STRING_construct failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 186 | } | 
| AzureIoTClient | 30:20a85b733111 | 187 | |
| AzureIoTClient | 34:51d158b409d2 | 188 | return fqdn; | 
| AzureIoTClient | 25:63f7d371c030 | 189 | } | 
| AzureIoTClient | 25:63f7d371c030 | 190 | |
| AzureIoTClient | 32:babfd49032ff | 191 | static void update_state(AMQP_TRANSPORT_INSTANCE* transport_instance, AMQP_TRANSPORT_STATE new_state) | 
| AzureIoTClient | 32:babfd49032ff | 192 | { | 
| AzureIoTClient | 34:51d158b409d2 | 193 | transport_instance->state = new_state; | 
| AzureIoTClient | 32:babfd49032ff | 194 | } | 
| AzureIoTClient | 32:babfd49032ff | 195 | |
| AzureIoTClient | 32:babfd49032ff | 196 | static void reset_retry_control(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device) | 
| AzureIoTClient | 32:babfd49032ff | 197 | { | 
| AzureIoTClient | 34:51d158b409d2 | 198 | retry_control_reset(registered_device->transport_instance->connection_retry_control); | 
| AzureIoTClient | 32:babfd49032ff | 199 | } | 
| AzureIoTClient | 32:babfd49032ff | 200 | |
| AzureIoTClient | 25:63f7d371c030 | 201 | |
| AzureIoTClient | 30:20a85b733111 | 202 | // ---------- Register/Unregister Helpers ---------- // | 
| AzureIoTClient | 25:63f7d371c030 | 203 | |
| AzureIoTClient | 30:20a85b733111 | 204 | static void internal_destroy_amqp_device_instance(AMQP_TRANSPORT_DEVICE_INSTANCE *trdev_inst) | 
| AzureIoTClient | 25:63f7d371c030 | 205 | { | 
| AzureIoTClient | 34:51d158b409d2 | 206 | if (trdev_inst->methods_handle != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 207 | { | 
| AzureIoTClient | 34:51d158b409d2 | 208 | iothubtransportamqp_methods_destroy(trdev_inst->methods_handle); | 
| AzureIoTClient | 34:51d158b409d2 | 209 | } | 
| AzureIoTClient | 42:c2eaa912a28c | 210 | |
| AzureIoTClient | 34:51d158b409d2 | 211 | if (trdev_inst->device_handle != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 212 | { | 
| AzureIoTClient | 34:51d158b409d2 | 213 | device_destroy(trdev_inst->device_handle); | 
| AzureIoTClient | 34:51d158b409d2 | 214 | } | 
| AzureIoTClient | 25:63f7d371c030 | 215 | |
| AzureIoTClient | 34:51d158b409d2 | 216 | if (trdev_inst->device_id != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 217 | { | 
| AzureIoTClient | 34:51d158b409d2 | 218 | STRING_delete(trdev_inst->device_id); | 
| AzureIoTClient | 34:51d158b409d2 | 219 | } | 
| AzureIoTClient | 25:63f7d371c030 | 220 | |
| AzureIoTClient | 34:51d158b409d2 | 221 | free(trdev_inst); | 
| AzureIoTClient | 25:63f7d371c030 | 222 | } | 
| AzureIoTClient | 25:63f7d371c030 | 223 | |
| AzureIoTClient | 41:71c01aa3df1a | 224 | static DEVICE_AUTH_MODE get_authentication_mode(const IOTHUB_DEVICE_CONFIG* device) | 
| AzureIoTClient | 41:71c01aa3df1a | 225 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 226 | DEVICE_AUTH_MODE result; | 
| AzureIoTClient | 41:71c01aa3df1a | 227 | |
| AzureIoTClient | 41:71c01aa3df1a | 228 | if (device->deviceKey != NULL || device->deviceSasToken != NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 229 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 230 | result = DEVICE_AUTH_MODE_CBS; | 
| AzureIoTClient | 41:71c01aa3df1a | 231 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 232 | else if (IoTHubClient_Auth_Get_Credential_Type(device->authorization_module) == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH) | 
| AzureIoTClient | 41:71c01aa3df1a | 233 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 234 | result = DEVICE_AUTH_MODE_CBS; | 
| AzureIoTClient | 41:71c01aa3df1a | 235 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 236 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 237 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 238 | result = DEVICE_AUTH_MODE_X509; | 
| AzureIoTClient | 41:71c01aa3df1a | 239 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 240 | return result; | 
| AzureIoTClient | 41:71c01aa3df1a | 241 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 242 | |
| AzureIoTClient | 46:c688c75b63b9 | 243 | static void raise_connection_status_callback_retry_expired(const void* item, const void* action_context, bool* continue_processing) | 
| AzureIoTClient | 46:c688c75b63b9 | 244 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 245 | (void)action_context; | 
| AzureIoTClient | 46:c688c75b63b9 | 246 | |
| AzureIoTClient | 46:c688c75b63b9 | 247 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)item; | 
| AzureIoTClient | 46:c688c75b63b9 | 248 | |
| AzureIoTClient | 46:c688c75b63b9 | 249 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_RETRY_EXPIRED); | 
| AzureIoTClient | 46:c688c75b63b9 | 250 | |
| AzureIoTClient | 46:c688c75b63b9 | 251 | *continue_processing = true; | 
| AzureIoTClient | 46:c688c75b63b9 | 252 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 253 | |
| AzureIoTClient | 30:20a85b733111 | 254 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 255 | // Saves the new state, if it is different than the previous one. | 
| AzureIoTClient | 30:20a85b733111 | 256 | static void on_device_state_changed_callback(void* context, DEVICE_STATE previous_state, DEVICE_STATE new_state) | 
| AzureIoTClient | 25:63f7d371c030 | 257 | { | 
| AzureIoTClient | 34:51d158b409d2 | 258 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_061: [If `new_state` is the same as `previous_state`, on_device_state_changed_callback shall return] | 
| AzureIoTClient | 34:51d158b409d2 | 259 | if (context != NULL && new_state != previous_state) | 
| AzureIoTClient | 34:51d158b409d2 | 260 | { | 
| AzureIoTClient | 34:51d158b409d2 | 261 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 34:51d158b409d2 | 262 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_062: [If `new_state` shall be saved into the `registered_device` instance] | 
| AzureIoTClient | 34:51d158b409d2 | 263 | registered_device->device_state = new_state; | 
| AzureIoTClient | 34:51d158b409d2 | 264 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_063: [If `registered_device->time_of_last_state_change` shall be set using get_time()] | 
| AzureIoTClient | 34:51d158b409d2 | 265 | registered_device->time_of_last_state_change = get_time(NULL); | 
| AzureIoTClient | 31:adadaef857c1 | 266 | |
| AzureIoTClient | 34:51d158b409d2 | 267 | if (new_state == DEVICE_STATE_STARTED) | 
| AzureIoTClient | 34:51d158b409d2 | 268 | { | 
| AzureIoTClient | 34:51d158b409d2 | 269 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_127: [If `new_state` is DEVICE_STATE_STARTED, retry_control_reset() shall be invoked passing `instance->connection_retry_control`] | 
| AzureIoTClient | 34:51d158b409d2 | 270 | reset_retry_control(registered_device); | 
| AzureIoTClient | 32:babfd49032ff | 271 | |
| AzureIoTClient | 34:51d158b409d2 | 272 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_120: [If `new_state` is DEVICE_STATE_STARTED, IoTHubClient_LL_ConnectionStatusCallBack shall be invoked with IOTHUB_CLIENT_CONNECTION_AUTHENTICATED and IOTHUB_CLIENT_CONNECTION_OK] | 
| AzureIoTClient | 34:51d158b409d2 | 273 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_AUTHENTICATED, IOTHUB_CLIENT_CONNECTION_OK); | 
| AzureIoTClient | 34:51d158b409d2 | 274 | } | 
| AzureIoTClient | 34:51d158b409d2 | 275 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_121: [If `new_state` is DEVICE_STATE_STOPPED, IoTHubClient_LL_ConnectionStatusCallBack shall be invoked with IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED and IOTHUB_CLIENT_CONNECTION_OK] | 
| AzureIoTClient | 34:51d158b409d2 | 276 | else if (new_state == DEVICE_STATE_STOPPED) | 
| AzureIoTClient | 34:51d158b409d2 | 277 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 278 | if (registered_device->transport_instance->state == AMQP_TRANSPORT_STATE_CONNECTED || | 
| AzureIoTClient | 46:c688c75b63b9 | 279 | registered_device->transport_instance->state == AMQP_TRANSPORT_STATE_BEING_DESTROYED) | 
| AzureIoTClient | 46:c688c75b63b9 | 280 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 281 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_OK); | 
| AzureIoTClient | 46:c688c75b63b9 | 282 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 283 | else if (registered_device->transport_instance->state == AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED || | 
| AzureIoTClient | 46:c688c75b63b9 | 284 | registered_device->transport_instance->state == AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION || | 
| AzureIoTClient | 46:c688c75b63b9 | 285 | registered_device->transport_instance->state == AMQP_TRANSPORT_STATE_RECONNECTING) | 
| AzureIoTClient | 46:c688c75b63b9 | 286 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 287 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_NO_NETWORK); | 
| AzureIoTClient | 46:c688c75b63b9 | 288 | } | 
| AzureIoTClient | 34:51d158b409d2 | 289 | } | 
| AzureIoTClient | 34:51d158b409d2 | 290 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_122: [If `new_state` is DEVICE_STATE_ERROR_AUTH, IoTHubClient_LL_ConnectionStatusCallBack shall be invoked with IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED and IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL] | 
| AzureIoTClient | 34:51d158b409d2 | 291 | else if (new_state == DEVICE_STATE_ERROR_AUTH) | 
| AzureIoTClient | 34:51d158b409d2 | 292 | { | 
| AzureIoTClient | 34:51d158b409d2 | 293 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL); | 
| AzureIoTClient | 34:51d158b409d2 | 294 | } | 
| AzureIoTClient | 34:51d158b409d2 | 295 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_123: [If `new_state` is DEVICE_STATE_ERROR_AUTH_TIMEOUT or DEVICE_STATE_ERROR_MSG, IoTHubClient_LL_ConnectionStatusCallBack shall be invoked with IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED and IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 296 | else if (new_state == DEVICE_STATE_ERROR_AUTH_TIMEOUT || new_state == DEVICE_STATE_ERROR_MSG) | 
| AzureIoTClient | 34:51d158b409d2 | 297 | { | 
| AzureIoTClient | 34:51d158b409d2 | 298 | IoTHubClient_LL_ConnectionStatusCallBack(registered_device->iothub_client_handle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR); | 
| AzureIoTClient | 34:51d158b409d2 | 299 | } | 
| AzureIoTClient | 34:51d158b409d2 | 300 | } | 
| AzureIoTClient | 25:63f7d371c030 | 301 | } | 
| AzureIoTClient | 25:63f7d371c030 | 302 | |
| AzureIoTClient | 30:20a85b733111 | 303 | // @brief Auxiliary function to be used to find a device in the registered_devices list. | 
| AzureIoTClient | 30:20a85b733111 | 304 | // @returns true if the device ids match, false otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 305 | static bool find_device_by_id_callback(LIST_ITEM_HANDLE list_item, const void* match_context) | 
| AzureIoTClient | 25:63f7d371c030 | 306 | { | 
| AzureIoTClient | 34:51d158b409d2 | 307 | bool result; | 
| AzureIoTClient | 30:20a85b733111 | 308 | |
| AzureIoTClient | 34:51d158b409d2 | 309 | if (match_context == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 310 | { | 
| AzureIoTClient | 34:51d158b409d2 | 311 | result = false; | 
| AzureIoTClient | 34:51d158b409d2 | 312 | } | 
| AzureIoTClient | 34:51d158b409d2 | 313 | else | 
| AzureIoTClient | 34:51d158b409d2 | 314 | { | 
| AzureIoTClient | 34:51d158b409d2 | 315 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item); | 
| AzureIoTClient | 25:63f7d371c030 | 316 | |
| AzureIoTClient | 34:51d158b409d2 | 317 | if (device_instance == NULL || | 
| AzureIoTClient | 34:51d158b409d2 | 318 | device_instance->device_id == NULL || | 
| AzureIoTClient | 34:51d158b409d2 | 319 | STRING_c_str(device_instance->device_id) != match_context) | 
| AzureIoTClient | 34:51d158b409d2 | 320 | { | 
| AzureIoTClient | 34:51d158b409d2 | 321 | result = false; | 
| AzureIoTClient | 34:51d158b409d2 | 322 | } | 
| AzureIoTClient | 34:51d158b409d2 | 323 | else | 
| AzureIoTClient | 34:51d158b409d2 | 324 | { | 
| AzureIoTClient | 34:51d158b409d2 | 325 | result = true; | 
| AzureIoTClient | 34:51d158b409d2 | 326 | } | 
| AzureIoTClient | 34:51d158b409d2 | 327 | } | 
| AzureIoTClient | 30:20a85b733111 | 328 | |
| AzureIoTClient | 34:51d158b409d2 | 329 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 330 | } | 
| AzureIoTClient | 25:63f7d371c030 | 331 | |
| AzureIoTClient | 30:20a85b733111 | 332 | // @brief Verifies if a device is already registered within the transport that owns the list of registered devices. | 
| AzureIoTClient | 30:20a85b733111 | 333 | // @remarks Returns the correspoding LIST_ITEM_HANDLE in registered_devices, if found. | 
| AzureIoTClient | 30:20a85b733111 | 334 | // @returns true if the device is already in the list, false otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 335 | static bool is_device_registered_ex(SINGLYLINKEDLIST_HANDLE registered_devices, const char* device_id, LIST_ITEM_HANDLE *list_item) | 
| AzureIoTClient | 25:63f7d371c030 | 336 | { | 
| AzureIoTClient | 34:51d158b409d2 | 337 | return ((*list_item = singlylinkedlist_find(registered_devices, find_device_by_id_callback, device_id)) != NULL ? 1 : 0); | 
| AzureIoTClient | 30:20a85b733111 | 338 | } | 
| AzureIoTClient | 25:63f7d371c030 | 339 | |
| AzureIoTClient | 30:20a85b733111 | 340 | // @brief Verifies if a device is already registered within the transport that owns the list of registered devices. | 
| AzureIoTClient | 30:20a85b733111 | 341 | // @returns true if the device is already in the list, false otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 342 | static bool is_device_registered(AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance) | 
| AzureIoTClient | 30:20a85b733111 | 343 | { | 
| AzureIoTClient | 34:51d158b409d2 | 344 | LIST_ITEM_HANDLE list_item; | 
| AzureIoTClient | 34:51d158b409d2 | 345 | const char* device_id = STRING_c_str(amqp_device_instance->device_id); | 
| AzureIoTClient | 34:51d158b409d2 | 346 | return is_device_registered_ex(amqp_device_instance->transport_instance->registered_devices, device_id, &list_item); | 
| AzureIoTClient | 25:63f7d371c030 | 347 | } | 
| AzureIoTClient | 25:63f7d371c030 | 348 | |
| AzureIoTClient | 39:e98d5df6dc74 | 349 | static size_t get_number_of_registered_devices(AMQP_TRANSPORT_INSTANCE* transport) | 
| AzureIoTClient | 39:e98d5df6dc74 | 350 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 351 | size_t result = 0; | 
| AzureIoTClient | 39:e98d5df6dc74 | 352 | |
| AzureIoTClient | 41:71c01aa3df1a | 353 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(transport->registered_devices); | 
| AzureIoTClient | 39:e98d5df6dc74 | 354 | |
| AzureIoTClient | 41:71c01aa3df1a | 355 | while (list_item != NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 356 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 357 | result++; | 
| AzureIoTClient | 41:71c01aa3df1a | 358 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 41:71c01aa3df1a | 359 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 360 | |
| AzureIoTClient | 41:71c01aa3df1a | 361 | return result; | 
| AzureIoTClient | 39:e98d5df6dc74 | 362 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 363 | |
| AzureIoTClient | 30:20a85b733111 | 364 | |
| AzureIoTClient | 30:20a85b733111 | 365 | // ---------- Callbacks ---------- // | 
| AzureIoTClient | 30:20a85b733111 | 366 | |
| AzureIoTClient | 30:20a85b733111 | 367 | static MESSAGE_CALLBACK_INFO* MESSAGE_CALLBACK_INFO_Create(IOTHUB_MESSAGE_HANDLE message, DEVICE_MESSAGE_DISPOSITION_INFO* disposition_info, AMQP_TRANSPORT_DEVICE_INSTANCE* device_state) | 
| AzureIoTClient | 25:63f7d371c030 | 368 | { | 
| AzureIoTClient | 34:51d158b409d2 | 369 | MESSAGE_CALLBACK_INFO* result; | 
| AzureIoTClient | 25:63f7d371c030 | 370 | |
| AzureIoTClient | 34:51d158b409d2 | 371 | if ((result = (MESSAGE_CALLBACK_INFO*)malloc(sizeof(MESSAGE_CALLBACK_INFO))) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 372 | { | 
| AzureIoTClient | 34:51d158b409d2 | 373 | LogError("Failed creating MESSAGE_CALLBACK_INFO (malloc failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 374 | } | 
| AzureIoTClient | 34:51d158b409d2 | 375 | else | 
| AzureIoTClient | 34:51d158b409d2 | 376 | { | 
| AzureIoTClient | 34:51d158b409d2 | 377 | memset(result, 0, sizeof(MESSAGE_CALLBACK_INFO)); | 
| AzureIoTClient | 34:51d158b409d2 | 378 | MESSAGE_DISPOSITION_CONTEXT* tc; | 
| AzureIoTClient | 25:63f7d371c030 | 379 | |
| AzureIoTClient | 34:51d158b409d2 | 380 | if ((tc = (MESSAGE_DISPOSITION_CONTEXT*)malloc(sizeof(MESSAGE_DISPOSITION_CONTEXT))) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 381 | { | 
| AzureIoTClient | 34:51d158b409d2 | 382 | LogError("Failed creating MESSAGE_DISPOSITION_CONTEXT (malloc failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 383 | free(result); | 
| AzureIoTClient | 34:51d158b409d2 | 384 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 385 | } | 
| AzureIoTClient | 34:51d158b409d2 | 386 | else | 
| AzureIoTClient | 34:51d158b409d2 | 387 | { | 
| AzureIoTClient | 34:51d158b409d2 | 388 | memset(tc, 0, sizeof(MESSAGE_DISPOSITION_CONTEXT)); | 
| AzureIoTClient | 34:51d158b409d2 | 389 | if (mallocAndStrcpy_s(&(tc->link_name), disposition_info->source) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 390 | { | 
| AzureIoTClient | 34:51d158b409d2 | 391 | tc->device_state = device_state; | 
| AzureIoTClient | 34:51d158b409d2 | 392 | tc->message_id = disposition_info->message_id; | 
| AzureIoTClient | 25:63f7d371c030 | 393 | |
| AzureIoTClient | 34:51d158b409d2 | 394 | result->messageHandle = message; | 
| AzureIoTClient | 34:51d158b409d2 | 395 | result->transportContext = tc; | 
| AzureIoTClient | 34:51d158b409d2 | 396 | } | 
| AzureIoTClient | 34:51d158b409d2 | 397 | else | 
| AzureIoTClient | 34:51d158b409d2 | 398 | { | 
| AzureIoTClient | 34:51d158b409d2 | 399 | LogError("Failed creating MESSAGE_CALLBACK_INFO (mallocAndStrcyp_s failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 400 | free(tc); | 
| AzureIoTClient | 34:51d158b409d2 | 401 | free(result); | 
| AzureIoTClient | 34:51d158b409d2 | 402 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 403 | } | 
| AzureIoTClient | 34:51d158b409d2 | 404 | } | 
| AzureIoTClient | 34:51d158b409d2 | 405 | } | 
| AzureIoTClient | 25:63f7d371c030 | 406 | |
| AzureIoTClient | 34:51d158b409d2 | 407 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 408 | } | 
| AzureIoTClient | 30:20a85b733111 | 409 | |
| AzureIoTClient | 30:20a85b733111 | 410 | static void MESSAGE_CALLBACK_INFO_Destroy(MESSAGE_CALLBACK_INFO* message_callback_info) | 
| AzureIoTClient | 30:20a85b733111 | 411 | { | 
| AzureIoTClient | 34:51d158b409d2 | 412 | if (message_callback_info->transportContext != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 413 | { | 
| AzureIoTClient | 34:51d158b409d2 | 414 | free(message_callback_info->transportContext->link_name); | 
| AzureIoTClient | 34:51d158b409d2 | 415 | free(message_callback_info->transportContext); | 
| AzureIoTClient | 34:51d158b409d2 | 416 | } | 
| AzureIoTClient | 34:51d158b409d2 | 417 | free(message_callback_info); | 
| AzureIoTClient | 25:63f7d371c030 | 418 | } | 
| AzureIoTClient | 25:63f7d371c030 | 419 | |
| AzureIoTClient | 30:20a85b733111 | 420 | static DEVICE_MESSAGE_DISPOSITION_RESULT get_device_disposition_result_from(IOTHUBMESSAGE_DISPOSITION_RESULT iothubclient_disposition_result) | 
| AzureIoTClient | 25:63f7d371c030 | 421 | { | 
| AzureIoTClient | 34:51d158b409d2 | 422 | DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result; | 
| AzureIoTClient | 25:63f7d371c030 | 423 | |
| AzureIoTClient | 34:51d158b409d2 | 424 | if (iothubclient_disposition_result == IOTHUBMESSAGE_ACCEPTED) | 
| AzureIoTClient | 34:51d158b409d2 | 425 | { | 
| AzureIoTClient | 34:51d158b409d2 | 426 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_ACCEPTED; | 
| AzureIoTClient | 34:51d158b409d2 | 427 | } | 
| AzureIoTClient | 34:51d158b409d2 | 428 | else if (iothubclient_disposition_result == IOTHUBMESSAGE_ABANDONED) | 
| AzureIoTClient | 34:51d158b409d2 | 429 | { | 
| AzureIoTClient | 34:51d158b409d2 | 430 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED; | 
| AzureIoTClient | 34:51d158b409d2 | 431 | } | 
| AzureIoTClient | 34:51d158b409d2 | 432 | else if (iothubclient_disposition_result == IOTHUBMESSAGE_REJECTED) | 
| AzureIoTClient | 34:51d158b409d2 | 433 | { | 
| AzureIoTClient | 34:51d158b409d2 | 434 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_REJECTED; | 
| AzureIoTClient | 34:51d158b409d2 | 435 | } | 
| AzureIoTClient | 34:51d158b409d2 | 436 | else | 
| AzureIoTClient | 34:51d158b409d2 | 437 | { | 
| AzureIoTClient | 34:51d158b409d2 | 438 | LogError("Failed getting corresponding DEVICE_MESSAGE_DISPOSITION_RESULT for IOTHUBMESSAGE_DISPOSITION_RESULT (%d is not supported)", iothubclient_disposition_result); | 
| AzureIoTClient | 34:51d158b409d2 | 439 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED; | 
| AzureIoTClient | 34:51d158b409d2 | 440 | } | 
| AzureIoTClient | 25:63f7d371c030 | 441 | |
| AzureIoTClient | 34:51d158b409d2 | 442 | return device_disposition_result; | 
| AzureIoTClient | 25:63f7d371c030 | 443 | } | 
| AzureIoTClient | 25:63f7d371c030 | 444 | |
| AzureIoTClient | 30:20a85b733111 | 445 | static DEVICE_MESSAGE_DISPOSITION_RESULT on_message_received(IOTHUB_MESSAGE_HANDLE message, DEVICE_MESSAGE_DISPOSITION_INFO* disposition_info, void* context) | 
| AzureIoTClient | 25:63f7d371c030 | 446 | { | 
| AzureIoTClient | 34:51d158b409d2 | 447 | AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 34:51d158b409d2 | 448 | DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result; | 
| AzureIoTClient | 34:51d158b409d2 | 449 | MESSAGE_CALLBACK_INFO* message_data; | 
| AzureIoTClient | 25:63f7d371c030 | 450 | |
| AzureIoTClient | 34:51d158b409d2 | 451 | if ((message_data = MESSAGE_CALLBACK_INFO_Create(message, disposition_info, amqp_device_instance)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 452 | { | 
| AzureIoTClient | 34:51d158b409d2 | 453 | LogError("Failed processing message received (failed to assemble callback info)"); | 
| AzureIoTClient | 34:51d158b409d2 | 454 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED; | 
| AzureIoTClient | 34:51d158b409d2 | 455 | } | 
| AzureIoTClient | 34:51d158b409d2 | 456 | else | 
| AzureIoTClient | 34:51d158b409d2 | 457 | { | 
| AzureIoTClient | 34:51d158b409d2 | 458 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_089: [IoTHubClient_LL_MessageCallback() shall be invoked passing the client and the incoming message handles as parameters] | 
| AzureIoTClient | 34:51d158b409d2 | 459 | if (IoTHubClient_LL_MessageCallback(amqp_device_instance->iothub_client_handle, message_data) != true) | 
| AzureIoTClient | 34:51d158b409d2 | 460 | { | 
| AzureIoTClient | 34:51d158b409d2 | 461 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_090: [If IoTHubClient_LL_MessageCallback() fails, on_message_received_callback shall return DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED] | 
| AzureIoTClient | 34:51d158b409d2 | 462 | LogError("Failed processing message received (IoTHubClient_LL_MessageCallback failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 463 | IoTHubMessage_Destroy(message); | 
| AzureIoTClient | 34:51d158b409d2 | 464 | MESSAGE_CALLBACK_INFO_Destroy(message_data); | 
| AzureIoTClient | 34:51d158b409d2 | 465 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED; | 
| AzureIoTClient | 34:51d158b409d2 | 466 | } | 
| AzureIoTClient | 34:51d158b409d2 | 467 | else | 
| AzureIoTClient | 34:51d158b409d2 | 468 | { | 
| AzureIoTClient | 34:51d158b409d2 | 469 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_091: [If IoTHubClient_LL_MessageCallback() succeeds, on_message_received_callback shall return DEVICE_MESSAGE_DISPOSITION_RESULT_NONE] | 
| AzureIoTClient | 34:51d158b409d2 | 470 | device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_NONE; | 
| AzureIoTClient | 34:51d158b409d2 | 471 | } | 
| AzureIoTClient | 34:51d158b409d2 | 472 | } | 
| AzureIoTClient | 30:20a85b733111 | 473 | |
| AzureIoTClient | 30:20a85b733111 | 474 | return device_disposition_result; | 
| AzureIoTClient | 25:63f7d371c030 | 475 | } | 
| AzureIoTClient | 25:63f7d371c030 | 476 | |
| AzureIoTClient | 25:63f7d371c030 | 477 | static void on_methods_error(void* context) | 
| AzureIoTClient | 25:63f7d371c030 | 478 | { | 
| AzureIoTClient | 25:63f7d371c030 | 479 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_030: [ `on_methods_error` shall do nothing. ]*/ | 
| AzureIoTClient | 25:63f7d371c030 | 480 | (void)context; | 
| AzureIoTClient | 25:63f7d371c030 | 481 | } | 
| AzureIoTClient | 25:63f7d371c030 | 482 | |
| AzureIoTClient | 29:7e8852b14e3b | 483 | static void on_methods_unsubscribed(void* context) | 
| AzureIoTClient | 29:7e8852b14e3b | 484 | { | 
| AzureIoTClient | 31:adadaef857c1 | 485 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_METHODS_12_001: [ `on_methods_unsubscribed` calls iothubtransportamqp_methods_unsubscribe. ]*/ | 
| AzureIoTClient | 30:20a85b733111 | 486 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 31:adadaef857c1 | 487 | |
| AzureIoTClient | 34:51d158b409d2 | 488 | iothubtransportamqp_methods_unsubscribe(device_state->methods_handle); | 
| AzureIoTClient | 34:51d158b409d2 | 489 | device_state->subscribed_for_methods = false; | 
| AzureIoTClient | 29:7e8852b14e3b | 490 | } | 
| AzureIoTClient | 29:7e8852b14e3b | 491 | |
| AzureIoTClient | 25:63f7d371c030 | 492 | static int on_method_request_received(void* context, const char* method_name, const unsigned char* request, size_t request_size, IOTHUBTRANSPORT_AMQP_METHOD_HANDLE method_handle) | 
| AzureIoTClient | 25:63f7d371c030 | 493 | { | 
| AzureIoTClient | 25:63f7d371c030 | 494 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 495 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 25:63f7d371c030 | 496 | |
| AzureIoTClient | 26:ee14eed604f6 | 497 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_017: [ `on_methods_request_received` shall call the `IoTHubClient_LL_DeviceMethodComplete` passing the method name, request buffer and size and the newly created BUFFER handle. ]*/ | 
| AzureIoTClient | 26:ee14eed604f6 | 498 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_022: [ The status code shall be the return value of the call to `IoTHubClient_LL_DeviceMethodComplete`. ]*/ | 
| AzureIoTClient | 26:ee14eed604f6 | 499 | if (IoTHubClient_LL_DeviceMethodComplete(device_state->iothub_client_handle, method_name, request, request_size, (void*)method_handle) != 0) | 
| AzureIoTClient | 25:63f7d371c030 | 500 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 501 | LogError("Failure: IoTHubClient_LL_DeviceMethodComplete"); | 
| AzureIoTClient | 29:7e8852b14e3b | 502 | result = __FAILURE__; | 
| AzureIoTClient | 25:63f7d371c030 | 503 | } | 
| AzureIoTClient | 25:63f7d371c030 | 504 | else | 
| AzureIoTClient | 25:63f7d371c030 | 505 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 506 | result = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 507 | } | 
| AzureIoTClient | 25:63f7d371c030 | 508 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 509 | } | 
| AzureIoTClient | 25:63f7d371c030 | 510 | |
| AzureIoTClient | 30:20a85b733111 | 511 | static int subscribe_methods(AMQP_TRANSPORT_DEVICE_INSTANCE* deviceState) | 
| AzureIoTClient | 25:63f7d371c030 | 512 | { | 
| AzureIoTClient | 25:63f7d371c030 | 513 | int result; | 
| AzureIoTClient | 25:63f7d371c030 | 514 | |
| AzureIoTClient | 29:7e8852b14e3b | 515 | if (deviceState->subscribed_for_methods) | 
| AzureIoTClient | 25:63f7d371c030 | 516 | { | 
| AzureIoTClient | 25:63f7d371c030 | 517 | result = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 518 | } | 
| AzureIoTClient | 25:63f7d371c030 | 519 | else | 
| AzureIoTClient | 25:63f7d371c030 | 520 | { | 
| AzureIoTClient | 34:51d158b409d2 | 521 | SESSION_HANDLE session_handle; | 
| AzureIoTClient | 30:20a85b733111 | 522 | |
| AzureIoTClient | 34:51d158b409d2 | 523 | if ((amqp_connection_get_session_handle(deviceState->transport_instance->amqp_connection, &session_handle)) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 524 | { | 
| AzureIoTClient | 34:51d158b409d2 | 525 | LogError("Device '%s' failed subscribing for methods (failed getting session handle)", STRING_c_str(deviceState->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 526 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 527 | } | 
| AzureIoTClient | 25:63f7d371c030 | 528 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_024: [ If the device authentication status is AUTHENTICATION_STATUS_OK and `IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod` was called to register for methods, `IoTHubTransport_AMQP_Common_DoWork` shall call `iothubtransportamqp_methods_subscribe`. ]*/ | 
| AzureIoTClient | 25:63f7d371c030 | 529 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_027: [ The current session handle shall be passed to `iothubtransportamqp_methods_subscribe`. ]*/ | 
| AzureIoTClient | 30:20a85b733111 | 530 | else if (iothubtransportamqp_methods_subscribe(deviceState->methods_handle, session_handle, on_methods_error, deviceState, on_method_request_received, deviceState, on_methods_unsubscribed, deviceState) != 0) | 
| AzureIoTClient | 25:63f7d371c030 | 531 | { | 
| AzureIoTClient | 25:63f7d371c030 | 532 | LogError("Cannot subscribe for methods"); | 
| AzureIoTClient | 29:7e8852b14e3b | 533 | result = __FAILURE__; | 
| AzureIoTClient | 25:63f7d371c030 | 534 | } | 
| AzureIoTClient | 25:63f7d371c030 | 535 | else | 
| AzureIoTClient | 25:63f7d371c030 | 536 | { | 
| AzureIoTClient | 29:7e8852b14e3b | 537 | deviceState->subscribed_for_methods = true; | 
| AzureIoTClient | 25:63f7d371c030 | 538 | result = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 539 | } | 
| AzureIoTClient | 25:63f7d371c030 | 540 | } | 
| AzureIoTClient | 25:63f7d371c030 | 541 | |
| AzureIoTClient | 25:63f7d371c030 | 542 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 543 | } | 
| AzureIoTClient | 25:63f7d371c030 | 544 | |
| AzureIoTClient | 39:e98d5df6dc74 | 545 | static void on_device_send_twin_update_complete_callback(DEVICE_TWIN_UPDATE_RESULT result, int status_code, void* context) | 
| AzureIoTClient | 39:e98d5df6dc74 | 546 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 547 | (void)result; | 
| AzureIoTClient | 39:e98d5df6dc74 | 548 | |
| AzureIoTClient | 41:71c01aa3df1a | 549 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_151: [If `context` is NULL, the callback shall return] | 
| AzureIoTClient | 41:71c01aa3df1a | 550 | if (context == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 551 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 552 | LogError("Invalid argument (context is NULL)"); | 
| AzureIoTClient | 41:71c01aa3df1a | 553 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 554 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 555 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 556 | AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT* dev_twin_ctx = (AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT*)context; | 
| AzureIoTClient | 39:e98d5df6dc74 | 557 | |
| AzureIoTClient | 41:71c01aa3df1a | 558 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_152: [`IoTHubClient_LL_ReportedStateComplete` shall be invoked passing `status_code` and `context` details] | 
| AzureIoTClient | 41:71c01aa3df1a | 559 | IoTHubClient_LL_ReportedStateComplete(dev_twin_ctx->client_handle, dev_twin_ctx->item_id, status_code); | 
| AzureIoTClient | 39:e98d5df6dc74 | 560 | |
| AzureIoTClient | 41:71c01aa3df1a | 561 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_153: [The memory allocated for `context` shall be released] | 
| AzureIoTClient | 41:71c01aa3df1a | 562 | free(dev_twin_ctx); | 
| AzureIoTClient | 41:71c01aa3df1a | 563 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 564 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 565 | |
| AzureIoTClient | 39:e98d5df6dc74 | 566 | static void on_device_twin_update_received_callback(DEVICE_TWIN_UPDATE_TYPE update_type, const unsigned char* message, size_t length, void* context) | 
| AzureIoTClient | 39:e98d5df6dc74 | 567 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 568 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_137: [If `context` is NULL, the callback shall return.] | 
| AzureIoTClient | 41:71c01aa3df1a | 569 | if (context == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 570 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 571 | LogError("Invalid argument (context is NULL)"); | 
| AzureIoTClient | 41:71c01aa3df1a | 572 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 573 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 574 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 575 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 39:e98d5df6dc74 | 576 | |
| AzureIoTClient | 41:71c01aa3df1a | 577 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_138: [If `update_type` is DEVICE_TWIN_UPDATE_TYPE_PARTIAL IoTHubClient_LL_RetrievePropertyComplete shall be invoked passing `context` as handle, `DEVICE_TWIN_UPDATE_PARTIAL`, `payload` and `size`.] | 
| AzureIoTClient | 41:71c01aa3df1a | 578 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_139: [If `update_type` is DEVICE_TWIN_UPDATE_TYPE_COMPLETE IoTHubClient_LL_RetrievePropertyComplete shall be invoked passing `context` as handle, `DEVICE_TWIN_UPDATE_COMPLETE`, `payload` and `size`.] | 
| AzureIoTClient | 41:71c01aa3df1a | 579 | IoTHubClient_LL_RetrievePropertyComplete( | 
| AzureIoTClient | 41:71c01aa3df1a | 580 | registered_device->iothub_client_handle, | 
| AzureIoTClient | 41:71c01aa3df1a | 581 | (update_type == DEVICE_TWIN_UPDATE_TYPE_COMPLETE ? DEVICE_TWIN_UPDATE_COMPLETE : DEVICE_TWIN_UPDATE_PARTIAL), | 
| AzureIoTClient | 41:71c01aa3df1a | 582 | message, length); | 
| AzureIoTClient | 41:71c01aa3df1a | 583 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 584 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 585 | |
| AzureIoTClient | 30:20a85b733111 | 586 | |
| AzureIoTClient | 30:20a85b733111 | 587 | // ---------- Underlying TLS I/O Helpers ---------- // | 
| AzureIoTClient | 30:20a85b733111 | 588 | |
| AzureIoTClient | 30:20a85b733111 | 589 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 590 | // Retrieves the options of the current underlying TLS I/O instance and saves in the transport instance. | 
| AzureIoTClient | 30:20a85b733111 | 591 | // @remarks | 
| AzureIoTClient | 30:20a85b733111 | 592 | // This is used when the new underlying I/O transport (TLS I/O, or WebSockets, etc) needs to be recreated, | 
| AzureIoTClient | 30:20a85b733111 | 593 | // and the options previously set must persist. | 
| AzureIoTClient | 30:20a85b733111 | 594 | // | 
| AzureIoTClient | 30:20a85b733111 | 595 | // If no TLS I/O instance was created yet, results in failure. | 
| AzureIoTClient | 30:20a85b733111 | 596 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 597 | // 0 if succeeds, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 598 | static int save_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance) | 
| AzureIoTClient | 25:63f7d371c030 | 599 | { | 
| AzureIoTClient | 34:51d158b409d2 | 600 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 601 | |
| AzureIoTClient | 34:51d158b409d2 | 602 | if (transport_instance->tls_io == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 603 | { | 
| AzureIoTClient | 34:51d158b409d2 | 604 | LogError("failed saving underlying I/O transport options (tls_io instance is NULL)"); | 
| AzureIoTClient | 34:51d158b409d2 | 605 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 606 | } | 
| AzureIoTClient | 34:51d158b409d2 | 607 | else | 
| AzureIoTClient | 34:51d158b409d2 | 608 | { | 
| AzureIoTClient | 34:51d158b409d2 | 609 | OPTIONHANDLER_HANDLE fresh_options; | 
| AzureIoTClient | 25:63f7d371c030 | 610 | |
| AzureIoTClient | 34:51d158b409d2 | 611 | if ((fresh_options = xio_retrieveoptions(transport_instance->tls_io)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 612 | { | 
| AzureIoTClient | 34:51d158b409d2 | 613 | LogError("failed saving underlying I/O transport options (tls_io instance is NULL)"); | 
| AzureIoTClient | 34:51d158b409d2 | 614 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 615 | } | 
| AzureIoTClient | 34:51d158b409d2 | 616 | else | 
| AzureIoTClient | 34:51d158b409d2 | 617 | { | 
| AzureIoTClient | 34:51d158b409d2 | 618 | OPTIONHANDLER_HANDLE previous_options = transport_instance->saved_tls_options; | 
| AzureIoTClient | 34:51d158b409d2 | 619 | transport_instance->saved_tls_options = fresh_options; | 
| AzureIoTClient | 30:20a85b733111 | 620 | |
| AzureIoTClient | 34:51d158b409d2 | 621 | if (previous_options != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 622 | { | 
| AzureIoTClient | 34:51d158b409d2 | 623 | OptionHandler_Destroy(previous_options); | 
| AzureIoTClient | 34:51d158b409d2 | 624 | } | 
| AzureIoTClient | 30:20a85b733111 | 625 | |
| AzureIoTClient | 34:51d158b409d2 | 626 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 627 | } | 
| AzureIoTClient | 34:51d158b409d2 | 628 | } | 
| AzureIoTClient | 30:20a85b733111 | 629 | |
| AzureIoTClient | 34:51d158b409d2 | 630 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 631 | } | 
| AzureIoTClient | 30:20a85b733111 | 632 | |
| AzureIoTClient | 30:20a85b733111 | 633 | static void destroy_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance) | 
| AzureIoTClient | 30:20a85b733111 | 634 | { | 
| AzureIoTClient | 34:51d158b409d2 | 635 | if (transport_instance->saved_tls_options != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 636 | { | 
| AzureIoTClient | 34:51d158b409d2 | 637 | OptionHandler_Destroy(transport_instance->saved_tls_options); | 
| AzureIoTClient | 34:51d158b409d2 | 638 | transport_instance->saved_tls_options = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 639 | } | 
| AzureIoTClient | 25:63f7d371c030 | 640 | } | 
| AzureIoTClient | 25:63f7d371c030 | 641 | |
| AzureIoTClient | 30:20a85b733111 | 642 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 643 | // Applies TLS I/O options if previously saved to a new TLS I/O instance. | 
| AzureIoTClient | 30:20a85b733111 | 644 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 645 | // 0 if succeeds, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 646 | static int restore_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance, XIO_HANDLE xio_handle) | 
| AzureIoTClient | 30:20a85b733111 | 647 | { | 
| AzureIoTClient | 34:51d158b409d2 | 648 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 649 | |
| AzureIoTClient | 34:51d158b409d2 | 650 | if (transport_instance->saved_tls_options == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 651 | { | 
| AzureIoTClient | 34:51d158b409d2 | 652 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 653 | } | 
| AzureIoTClient | 34:51d158b409d2 | 654 | else | 
| AzureIoTClient | 34:51d158b409d2 | 655 | { | 
| AzureIoTClient | 34:51d158b409d2 | 656 | if (OptionHandler_FeedOptions(transport_instance->saved_tls_options, xio_handle) != OPTIONHANDLER_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 657 | { | 
| AzureIoTClient | 34:51d158b409d2 | 658 | LogError("Failed feeding existing options to new TLS instance."); | 
| AzureIoTClient | 34:51d158b409d2 | 659 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 660 | } | 
| AzureIoTClient | 34:51d158b409d2 | 661 | else | 
| AzureIoTClient | 34:51d158b409d2 | 662 | { | 
| AzureIoTClient | 34:51d158b409d2 | 663 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 664 | } | 
| AzureIoTClient | 34:51d158b409d2 | 665 | } | 
| AzureIoTClient | 30:20a85b733111 | 666 | |
| AzureIoTClient | 34:51d158b409d2 | 667 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 668 | } | 
| AzureIoTClient | 30:20a85b733111 | 669 | |
| AzureIoTClient | 30:20a85b733111 | 670 | // @brief Destroys the XIO_HANDLE obtained with underlying_io_transport_provider(), saving its options beforehand. | 
| AzureIoTClient | 30:20a85b733111 | 671 | static void destroy_underlying_io_transport(AMQP_TRANSPORT_INSTANCE* transport_instance) | 
| AzureIoTClient | 30:20a85b733111 | 672 | { | 
| AzureIoTClient | 34:51d158b409d2 | 673 | if (transport_instance->tls_io != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 674 | { | 
| AzureIoTClient | 34:51d158b409d2 | 675 | xio_destroy(transport_instance->tls_io); | 
| AzureIoTClient | 34:51d158b409d2 | 676 | transport_instance->tls_io = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 677 | } | 
| AzureIoTClient | 30:20a85b733111 | 678 | } | 
| AzureIoTClient | 30:20a85b733111 | 679 | |
| AzureIoTClient | 30:20a85b733111 | 680 | // @brief Invokes underlying_io_transport_provider() and retrieves a new XIO_HANDLE to use for I/O (TLS, or websockets, or w/e is supported). | 
| AzureIoTClient | 30:20a85b733111 | 681 | // @param xio_handle: if successfull, set with the new XIO_HANDLE acquired; not changed otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 682 | // @returns 0 if successfull, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 683 | static int get_new_underlying_io_transport(AMQP_TRANSPORT_INSTANCE* transport_instance, XIO_HANDLE *xio_handle) | 
| AzureIoTClient | 30:20a85b733111 | 684 | { | 
| AzureIoTClient | 34:51d158b409d2 | 685 | int result; | 
| AzureIoTClient | 31:adadaef857c1 | 686 | AMQP_TRANSPORT_PROXY_OPTIONS amqp_transport_proxy_options; | 
| AzureIoTClient | 30:20a85b733111 | 687 | |
| AzureIoTClient | 31:adadaef857c1 | 688 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_041: [ If the `proxy_data` option has been set, the proxy options shall be filled in the argument `amqp_transport_proxy_options` when calling the function `underlying_io_transport_provider()` to obtain the underlying IO handle. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 689 | amqp_transport_proxy_options.host_address = transport_instance->http_proxy_hostname; | 
| AzureIoTClient | 31:adadaef857c1 | 690 | amqp_transport_proxy_options.port = transport_instance->http_proxy_port; | 
| AzureIoTClient | 31:adadaef857c1 | 691 | amqp_transport_proxy_options.username = transport_instance->http_proxy_username; | 
| AzureIoTClient | 31:adadaef857c1 | 692 | amqp_transport_proxy_options.password = transport_instance->http_proxy_password; | 
| AzureIoTClient | 31:adadaef857c1 | 693 | |
| AzureIoTClient | 31:adadaef857c1 | 694 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_042: [ If no `proxy_data` option has been set, NULL shall be passed as the argument `amqp_transport_proxy_options` when calling the function `underlying_io_transport_provider()`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 695 | if ((*xio_handle = transport_instance->underlying_io_transport_provider(STRING_c_str(transport_instance->iothub_host_fqdn), amqp_transport_proxy_options.host_address == NULL ? NULL : &amqp_transport_proxy_options)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 696 | { | 
| AzureIoTClient | 34:51d158b409d2 | 697 | LogError("Failed to obtain a TLS I/O transport layer (underlying_io_transport_provider() failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 698 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 699 | } | 
| AzureIoTClient | 34:51d158b409d2 | 700 | else | 
| AzureIoTClient | 34:51d158b409d2 | 701 | { | 
| AzureIoTClient | 44:9213db3aced6 | 702 | // If this is the HSM x509 ECC certificate | 
| AzureIoTClient | 45:c09fac270e60 | 703 | if (transport_instance->authorization_module != NULL && IoTHubClient_Auth_Get_Credential_Type(transport_instance->authorization_module) == IOTHUB_CREDENTIAL_TYPE_X509_ECC) | 
| AzureIoTClient | 41:71c01aa3df1a | 704 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 705 | // Set the xio_handle | 
| AzureIoTClient | 41:71c01aa3df1a | 706 | if (IoTHubClient_Auth_Set_xio_Certificate(transport_instance->authorization_module, *xio_handle) != 0) | 
| AzureIoTClient | 41:71c01aa3df1a | 707 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 708 | LogError("Unable to create the lower level TLS layer."); | 
| AzureIoTClient | 41:71c01aa3df1a | 709 | result = __FAILURE__; | 
| AzureIoTClient | 41:71c01aa3df1a | 710 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 711 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 712 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 713 | result = 0; | 
| AzureIoTClient | 41:71c01aa3df1a | 714 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 715 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 716 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 717 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 718 | result = 0; | 
| AzureIoTClient | 41:71c01aa3df1a | 719 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 720 | |
| AzureIoTClient | 34:51d158b409d2 | 721 | if (restore_underlying_io_transport_options(transport_instance, *xio_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 722 | { | 
| AzureIoTClient | 34:51d158b409d2 | 723 | /*pessimistically hope TLS will fail, be recreated and options re-given*/ | 
| AzureIoTClient | 34:51d158b409d2 | 724 | LogError("Failed to apply options previous saved to new underlying I/O transport instance."); | 
| AzureIoTClient | 34:51d158b409d2 | 725 | } | 
| AzureIoTClient | 30:20a85b733111 | 726 | |
| AzureIoTClient | 34:51d158b409d2 | 727 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 728 | } | 
| AzureIoTClient | 30:20a85b733111 | 729 | |
| AzureIoTClient | 34:51d158b409d2 | 730 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 731 | } | 
| AzureIoTClient | 30:20a85b733111 | 732 | |
| AzureIoTClient | 30:20a85b733111 | 733 | |
| AzureIoTClient | 30:20a85b733111 | 734 | // ---------- AMQP connection establishment/tear-down, connectry retry ---------- // | 
| AzureIoTClient | 30:20a85b733111 | 735 | |
| AzureIoTClient | 30:20a85b733111 | 736 | static void on_amqp_connection_state_changed(const void* context, AMQP_CONNECTION_STATE previous_state, AMQP_CONNECTION_STATE new_state) | 
| AzureIoTClient | 30:20a85b733111 | 737 | { | 
| AzureIoTClient | 34:51d158b409d2 | 738 | if (context != NULL && new_state != previous_state) | 
| AzureIoTClient | 34:51d158b409d2 | 739 | { | 
| AzureIoTClient | 34:51d158b409d2 | 740 | AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)context; | 
| AzureIoTClient | 30:20a85b733111 | 741 | |
| AzureIoTClient | 34:51d158b409d2 | 742 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_059: [`new_state` shall be saved in to the transport instance] | 
| AzureIoTClient | 34:51d158b409d2 | 743 | transport_instance->amqp_connection_state = new_state; | 
| AzureIoTClient | 30:20a85b733111 | 744 | |
| AzureIoTClient | 34:51d158b409d2 | 745 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_060: [If `new_state` is AMQP_CONNECTION_STATE_ERROR, the connection shall be flagged as faulty (so the connection retry logic can be triggered)] | 
| AzureIoTClient | 34:51d158b409d2 | 746 | if (new_state == AMQP_CONNECTION_STATE_ERROR) | 
| AzureIoTClient | 34:51d158b409d2 | 747 | { | 
| AzureIoTClient | 34:51d158b409d2 | 748 | LogError("Transport received an ERROR from the amqp_connection (state changed %s -> %s); it will be flagged for connection retry.", ENUM_TO_STRING(AMQP_CONNECTION_STATE, previous_state), ENUM_TO_STRING(AMQP_CONNECTION_STATE, new_state)); | 
| AzureIoTClient | 31:adadaef857c1 | 749 | |
| AzureIoTClient | 34:51d158b409d2 | 750 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED); | 
| AzureIoTClient | 34:51d158b409d2 | 751 | } | 
| AzureIoTClient | 34:51d158b409d2 | 752 | else if (new_state == AMQP_CONNECTION_STATE_OPENED) | 
| AzureIoTClient | 34:51d158b409d2 | 753 | { | 
| AzureIoTClient | 34:51d158b409d2 | 754 | update_state(transport_instance, AMQP_TRANSPORT_STATE_CONNECTED); | 
| AzureIoTClient | 34:51d158b409d2 | 755 | } | 
| AzureIoTClient | 34:51d158b409d2 | 756 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_115: [If the AMQP connection is closed by the service side, the connection retry logic shall be triggered] | 
| AzureIoTClient | 34:51d158b409d2 | 757 | else if (new_state == AMQP_CONNECTION_STATE_CLOSED && previous_state == AMQP_CONNECTION_STATE_OPENED && transport_instance->state != AMQP_TRANSPORT_STATE_BEING_DESTROYED) | 
| AzureIoTClient | 34:51d158b409d2 | 758 | { | 
| AzureIoTClient | 34:51d158b409d2 | 759 | LogError("amqp_connection was closed unexpectedly; connection retry will be triggered."); | 
| AzureIoTClient | 30:20a85b733111 | 760 | |
| AzureIoTClient | 34:51d158b409d2 | 761 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED); | 
| AzureIoTClient | 34:51d158b409d2 | 762 | } | 
| AzureIoTClient | 34:51d158b409d2 | 763 | } | 
| AzureIoTClient | 30:20a85b733111 | 764 | } | 
| AzureIoTClient | 30:20a85b733111 | 765 | |
| AzureIoTClient | 30:20a85b733111 | 766 | static int establish_amqp_connection(AMQP_TRANSPORT_INSTANCE* transport_instance) | 
| AzureIoTClient | 25:63f7d371c030 | 767 | { | 
| AzureIoTClient | 25:63f7d371c030 | 768 | int result; | 
| AzureIoTClient | 25:63f7d371c030 | 769 | |
| AzureIoTClient | 34:51d158b409d2 | 770 | if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET) | 
| AzureIoTClient | 34:51d158b409d2 | 771 | { | 
| AzureIoTClient | 34:51d158b409d2 | 772 | LogError("Failed establishing connection (transport doesn't have a preferred authentication mode set; unexpected!)."); | 
| AzureIoTClient | 34:51d158b409d2 | 773 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 774 | } | 
| AzureIoTClient | 34:51d158b409d2 | 775 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_023: [If `instance->tls_io` is NULL, it shall be set invoking instance->underlying_io_transport_provider()] | 
| AzureIoTClient | 34:51d158b409d2 | 776 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_025: [When `instance->tls_io` is created, it shall be set with `instance->saved_tls_options` using OptionHandler_FeedOptions()] | 
| AzureIoTClient | 34:51d158b409d2 | 777 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_111: [If OptionHandler_FeedOptions() fails, it shall be ignored] | 
| AzureIoTClient | 34:51d158b409d2 | 778 | else if (transport_instance->tls_io == NULL && | 
| AzureIoTClient | 34:51d158b409d2 | 779 | get_new_underlying_io_transport(transport_instance, &transport_instance->tls_io) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 780 | { | 
| AzureIoTClient | 34:51d158b409d2 | 781 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_024: [If instance->underlying_io_transport_provider() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return] | 
| AzureIoTClient | 34:51d158b409d2 | 782 | LogError("Failed establishing connection (failed to obtain a TLS I/O transport layer)."); | 
| AzureIoTClient | 34:51d158b409d2 | 783 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 784 | } | 
| AzureIoTClient | 34:51d158b409d2 | 785 | else | 
| AzureIoTClient | 34:51d158b409d2 | 786 | { | 
| AzureIoTClient | 34:51d158b409d2 | 787 | AMQP_CONNECTION_CONFIG amqp_connection_config; | 
| AzureIoTClient | 34:51d158b409d2 | 788 | amqp_connection_config.iothub_host_fqdn = STRING_c_str(transport_instance->iothub_host_fqdn); | 
| AzureIoTClient | 34:51d158b409d2 | 789 | amqp_connection_config.underlying_io_transport = transport_instance->tls_io; | 
| AzureIoTClient | 34:51d158b409d2 | 790 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_029: [`instance->is_trace_on` shall be set into `AMQP_CONNECTION_CONFIG->is_trace_on`] | 
| AzureIoTClient | 34:51d158b409d2 | 791 | amqp_connection_config.is_trace_on = transport_instance->is_trace_on; | 
| AzureIoTClient | 34:51d158b409d2 | 792 | amqp_connection_config.on_state_changed_callback = on_amqp_connection_state_changed; | 
| AzureIoTClient | 34:51d158b409d2 | 793 | amqp_connection_config.on_state_changed_context = transport_instance; | 
| AzureIoTClient | 37:abd16824f63b | 794 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_12_003: [AMQP connection will be configured using the `c2d_keep_alive_freq_secs` value from SetOption ] | 
| AzureIoTClient | 37:abd16824f63b | 795 | amqp_connection_config.c2d_keep_alive_freq_secs = transport_instance->c2d_keep_alive_freq_secs; | 
| AzureIoTClient | 25:63f7d371c030 | 796 | |
| AzureIoTClient | 34:51d158b409d2 | 797 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_027: [If `transport->preferred_authentication_method` is CBS, AMQP_CONNECTION_CONFIG shall be set with `create_sasl_io` = true and `create_cbs_connection` = true] | 
| AzureIoTClient | 34:51d158b409d2 | 798 | if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS) | 
| AzureIoTClient | 34:51d158b409d2 | 799 | { | 
| AzureIoTClient | 34:51d158b409d2 | 800 | amqp_connection_config.create_sasl_io = true; | 
| AzureIoTClient | 34:51d158b409d2 | 801 | amqp_connection_config.create_cbs_connection = true; | 
| AzureIoTClient | 34:51d158b409d2 | 802 | } | 
| AzureIoTClient | 34:51d158b409d2 | 803 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_028: [If `transport->preferred_credential_method` is X509, AMQP_CONNECTION_CONFIG shall be set with `create_sasl_io` = false and `create_cbs_connection` = false] | 
| AzureIoTClient | 34:51d158b409d2 | 804 | else if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_X509) | 
| AzureIoTClient | 34:51d158b409d2 | 805 | { | 
| AzureIoTClient | 34:51d158b409d2 | 806 | amqp_connection_config.create_sasl_io = false; | 
| AzureIoTClient | 34:51d158b409d2 | 807 | amqp_connection_config.create_cbs_connection = false; | 
| AzureIoTClient | 34:51d158b409d2 | 808 | } | 
| AzureIoTClient | 34:51d158b409d2 | 809 | // If new AMQP_TRANSPORT_AUTHENTICATION_MODE values are added, they need to be covered here. | 
| AzureIoTClient | 25:63f7d371c030 | 810 | |
| AzureIoTClient | 34:51d158b409d2 | 811 | transport_instance->amqp_connection_state = AMQP_CONNECTION_STATE_CLOSED; | 
| AzureIoTClient | 30:20a85b733111 | 812 | |
| AzureIoTClient | 34:51d158b409d2 | 813 | if (transport_instance->state == AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION) | 
| AzureIoTClient | 34:51d158b409d2 | 814 | { | 
| AzureIoTClient | 34:51d158b409d2 | 815 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTING); | 
| AzureIoTClient | 34:51d158b409d2 | 816 | } | 
| AzureIoTClient | 34:51d158b409d2 | 817 | else | 
| AzureIoTClient | 34:51d158b409d2 | 818 | { | 
| AzureIoTClient | 34:51d158b409d2 | 819 | update_state(transport_instance, AMQP_TRANSPORT_STATE_CONNECTING); | 
| AzureIoTClient | 34:51d158b409d2 | 820 | } | 
| AzureIoTClient | 32:babfd49032ff | 821 | |
| AzureIoTClient | 34:51d158b409d2 | 822 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_026: [If `transport->connection` is NULL, it shall be created using amqp_connection_create()] | 
| AzureIoTClient | 34:51d158b409d2 | 823 | if ((transport_instance->amqp_connection = amqp_connection_create(&amqp_connection_config)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 824 | { | 
| AzureIoTClient | 34:51d158b409d2 | 825 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_030: [If amqp_connection_create() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return] | 
| AzureIoTClient | 34:51d158b409d2 | 826 | LogError("Failed establishing connection (failed to create the amqp_connection instance)."); | 
| AzureIoTClient | 34:51d158b409d2 | 827 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 828 | } | 
| AzureIoTClient | 34:51d158b409d2 | 829 | else | 
| AzureIoTClient | 34:51d158b409d2 | 830 | { | 
| AzureIoTClient | 34:51d158b409d2 | 831 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_110: [If amqp_connection_create() succeeds, IoTHubTransport_AMQP_Common_DoWork shall proceed to invoke amqp_connection_do_work] | 
| AzureIoTClient | 34:51d158b409d2 | 832 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 833 | } | 
| AzureIoTClient | 34:51d158b409d2 | 834 | } | 
| AzureIoTClient | 25:63f7d371c030 | 835 | |
| AzureIoTClient | 25:63f7d371c030 | 836 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 837 | } | 
| AzureIoTClient | 25:63f7d371c030 | 838 | |
| AzureIoTClient | 30:20a85b733111 | 839 | static void prepare_device_for_connection_retry(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device) | 
| AzureIoTClient | 25:63f7d371c030 | 840 | { | 
| AzureIoTClient | 34:51d158b409d2 | 841 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_032: [ Each `instance->registered_devices` shall unsubscribe from receiving C2D method requests by calling `iothubtransportamqp_methods_unsubscribe`] | 
| AzureIoTClient | 30:20a85b733111 | 842 | iothubtransportamqp_methods_unsubscribe(registered_device->methods_handle); | 
| AzureIoTClient | 34:51d158b409d2 | 843 | registered_device->subscribed_for_methods = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 844 | |
| AzureIoTClient | 34:51d158b409d2 | 845 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_031: [device_stop() shall be invoked on all `instance->registered_devices` that are not already stopped] | 
| AzureIoTClient | 34:51d158b409d2 | 846 | if (registered_device->device_state != DEVICE_STATE_STOPPED) | 
| AzureIoTClient | 34:51d158b409d2 | 847 | { | 
| AzureIoTClient | 34:51d158b409d2 | 848 | if (device_stop(registered_device->device_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 849 | { | 
| AzureIoTClient | 34:51d158b409d2 | 850 | LogError("Failed preparing device '%s' for connection retry (device_stop failed)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 851 | } | 
| AzureIoTClient | 34:51d158b409d2 | 852 | } | 
| AzureIoTClient | 25:63f7d371c030 | 853 | |
| AzureIoTClient | 34:51d158b409d2 | 854 | registered_device->number_of_previous_failures = 0; | 
| AzureIoTClient | 34:51d158b409d2 | 855 | registered_device->number_of_send_event_complete_failures = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 856 | } | 
| AzureIoTClient | 25:63f7d371c030 | 857 | |
| AzureIoTClient | 30:20a85b733111 | 858 | static void prepare_for_connection_retry(AMQP_TRANSPORT_INSTANCE* transport_instance) | 
| AzureIoTClient | 25:63f7d371c030 | 859 | { | 
| AzureIoTClient | 34:51d158b409d2 | 860 | LogInfo("Preparing transport for re-connection"); | 
| AzureIoTClient | 32:babfd49032ff | 861 | |
| AzureIoTClient | 34:51d158b409d2 | 862 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_034: [`instance->tls_io` options shall be saved on `instance->saved_tls_options` using xio_retrieveoptions()] | 
| AzureIoTClient | 34:51d158b409d2 | 863 | if (save_underlying_io_transport_options(transport_instance) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 864 | { | 
| AzureIoTClient | 34:51d158b409d2 | 865 | LogError("Failed saving TLS I/O options while preparing for connection retry; failure will be ignored"); | 
| AzureIoTClient | 34:51d158b409d2 | 866 | } | 
| AzureIoTClient | 25:63f7d371c030 | 867 | |
| AzureIoTClient | 34:51d158b409d2 | 868 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(transport_instance->registered_devices); | 
| AzureIoTClient | 30:20a85b733111 | 869 | |
| AzureIoTClient | 34:51d158b409d2 | 870 | while (list_item != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 871 | { | 
| AzureIoTClient | 34:51d158b409d2 | 872 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item); | 
| AzureIoTClient | 25:63f7d371c030 | 873 | |
| AzureIoTClient | 34:51d158b409d2 | 874 | if (registered_device == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 875 | { | 
| AzureIoTClient | 34:51d158b409d2 | 876 | LogError("Failed preparing device for connection retry (singlylinkedlist_item_get_value failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 877 | } | 
| AzureIoTClient | 34:51d158b409d2 | 878 | else | 
| AzureIoTClient | 34:51d158b409d2 | 879 | { | 
| AzureIoTClient | 34:51d158b409d2 | 880 | prepare_device_for_connection_retry(registered_device); | 
| AzureIoTClient | 34:51d158b409d2 | 881 | } | 
| AzureIoTClient | 25:63f7d371c030 | 882 | |
| AzureIoTClient | 34:51d158b409d2 | 883 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 34:51d158b409d2 | 884 | } | 
| AzureIoTClient | 25:63f7d371c030 | 885 | |
| AzureIoTClient | 34:51d158b409d2 | 886 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_033: [`instance->connection` shall be destroyed using amqp_connection_destroy()] | 
| AzureIoTClient | 34:51d158b409d2 | 887 | amqp_connection_destroy(transport_instance->amqp_connection); | 
| AzureIoTClient | 34:51d158b409d2 | 888 | transport_instance->amqp_connection = NULL; | 
| AzureIoTClient | 30:20a85b733111 | 889 | transport_instance->amqp_connection_state = AMQP_CONNECTION_STATE_CLOSED; | 
| AzureIoTClient | 25:63f7d371c030 | 890 | |
| AzureIoTClient | 34:51d158b409d2 | 891 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_035: [`instance->tls_io` shall be destroyed using xio_destroy()] | 
| AzureIoTClient | 34:51d158b409d2 | 892 | destroy_underlying_io_transport(transport_instance); | 
| AzureIoTClient | 32:babfd49032ff | 893 | |
| AzureIoTClient | 34:51d158b409d2 | 894 | update_state(transport_instance, AMQP_TRANSPORT_STATE_READY_FOR_RECONNECTION); | 
| AzureIoTClient | 25:63f7d371c030 | 895 | } | 
| AzureIoTClient | 25:63f7d371c030 | 896 | |
| AzureIoTClient | 30:20a85b733111 | 897 | |
| AzureIoTClient | 30:20a85b733111 | 898 | // @brief Verifies if the crendentials used by the device match the requirements and authentication mode currently supported by the transport. | 
| AzureIoTClient | 30:20a85b733111 | 899 | // @returns true if credentials are good, false otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 900 | static bool is_device_credential_acceptable(const IOTHUB_DEVICE_CONFIG* device_config, AMQP_TRANSPORT_AUTHENTICATION_MODE preferred_authentication_mode) | 
| AzureIoTClient | 25:63f7d371c030 | 901 | { | 
| AzureIoTClient | 30:20a85b733111 | 902 | bool result; | 
| AzureIoTClient | 25:63f7d371c030 | 903 | |
| AzureIoTClient | 34:51d158b409d2 | 904 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_003: [IoTHubTransport_AMQP_Common_Register shall return NULL if both deviceKey and deviceSasToken are not NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 905 | if ((device_config->deviceSasToken != NULL) && (device_config->deviceKey != NULL)) | 
| AzureIoTClient | 34:51d158b409d2 | 906 | { | 
| AzureIoTClient | 34:51d158b409d2 | 907 | LogError("Credential of device '%s' is not acceptable (must provide EITHER deviceSasToken OR deviceKey)", device_config->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 908 | result = false; | 
| AzureIoTClient | 34:51d158b409d2 | 909 | } | 
| AzureIoTClient | 30:20a85b733111 | 910 | else if (preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET) | 
| AzureIoTClient | 26:ee14eed604f6 | 911 | { | 
| AzureIoTClient | 30:20a85b733111 | 912 | result = true; | 
| AzureIoTClient | 30:20a85b733111 | 913 | } | 
| AzureIoTClient | 30:20a85b733111 | 914 | else if (preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_X509 && (device_config->deviceKey != NULL || device_config->deviceSasToken != NULL)) | 
| AzureIoTClient | 30:20a85b733111 | 915 | { | 
| AzureIoTClient | 34:51d158b409d2 | 916 | LogError("Credential of device '%s' is not acceptable (transport is using X509 certificate authentication, but device config contains deviceKey or sasToken)", device_config->deviceId); | 
| AzureIoTClient | 30:20a85b733111 | 917 | result = false; | 
| AzureIoTClient | 30:20a85b733111 | 918 | } | 
| AzureIoTClient | 30:20a85b733111 | 919 | else if (preferred_authentication_mode != AMQP_TRANSPORT_AUTHENTICATION_MODE_X509 && (device_config->deviceKey == NULL && device_config->deviceSasToken == NULL)) | 
| AzureIoTClient | 30:20a85b733111 | 920 | { | 
| AzureIoTClient | 34:51d158b409d2 | 921 | LogError("Credential of device '%s' is not acceptable (transport is using CBS authentication, but device config does not contain deviceKey nor sasToken)", device_config->deviceId); | 
| AzureIoTClient | 30:20a85b733111 | 922 | result = false; | 
| AzureIoTClient | 26:ee14eed604f6 | 923 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 924 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 925 | { | 
| AzureIoTClient | 30:20a85b733111 | 926 | result = true; | 
| AzureIoTClient | 26:ee14eed604f6 | 927 | } | 
| AzureIoTClient | 25:63f7d371c030 | 928 | |
| AzureIoTClient | 26:ee14eed604f6 | 929 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 930 | } | 
| AzureIoTClient | 25:63f7d371c030 | 931 | |
| AzureIoTClient | 25:63f7d371c030 | 932 | |
| AzureIoTClient | 30:20a85b733111 | 933 | |
| AzureIoTClient | 30:20a85b733111 | 934 | //---------- DoWork Helpers ----------// | 
| AzureIoTClient | 30:20a85b733111 | 935 | |
| AzureIoTClient | 30:20a85b733111 | 936 | static IOTHUB_MESSAGE_LIST* get_next_event_to_send(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device) | 
| AzureIoTClient | 30:20a85b733111 | 937 | { | 
| AzureIoTClient | 34:51d158b409d2 | 938 | IOTHUB_MESSAGE_LIST* message; | 
| AzureIoTClient | 30:20a85b733111 | 939 | |
| AzureIoTClient | 34:51d158b409d2 | 940 | if (!DList_IsListEmpty(registered_device->waiting_to_send)) | 
| AzureIoTClient | 34:51d158b409d2 | 941 | { | 
| AzureIoTClient | 34:51d158b409d2 | 942 | PDLIST_ENTRY list_entry = registered_device->waiting_to_send->Flink; | 
| AzureIoTClient | 34:51d158b409d2 | 943 | message = containingRecord(list_entry, IOTHUB_MESSAGE_LIST, entry); | 
| AzureIoTClient | 34:51d158b409d2 | 944 | (void)DList_RemoveEntryList(list_entry); | 
| AzureIoTClient | 34:51d158b409d2 | 945 | } | 
| AzureIoTClient | 34:51d158b409d2 | 946 | else | 
| AzureIoTClient | 34:51d158b409d2 | 947 | { | 
| AzureIoTClient | 34:51d158b409d2 | 948 | message = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 949 | } | 
| AzureIoTClient | 30:20a85b733111 | 950 | |
| AzureIoTClient | 34:51d158b409d2 | 951 | return message; | 
| AzureIoTClient | 30:20a85b733111 | 952 | } | 
| AzureIoTClient | 30:20a85b733111 | 953 | |
| AzureIoTClient | 30:20a85b733111 | 954 | // @brief "Parses" the D2C_EVENT_SEND_RESULT (from iothubtransport_amqp_device module) into a IOTHUB_CLIENT_CONFIRMATION_RESULT. | 
| AzureIoTClient | 30:20a85b733111 | 955 | static IOTHUB_CLIENT_CONFIRMATION_RESULT get_iothub_client_confirmation_result_from(D2C_EVENT_SEND_RESULT result) | 
| AzureIoTClient | 30:20a85b733111 | 956 | { | 
| AzureIoTClient | 34:51d158b409d2 | 957 | IOTHUB_CLIENT_CONFIRMATION_RESULT iothub_send_result; | 
| AzureIoTClient | 30:20a85b733111 | 958 | |
| AzureIoTClient | 34:51d158b409d2 | 959 | switch (result) | 
| AzureIoTClient | 34:51d158b409d2 | 960 | { | 
| AzureIoTClient | 34:51d158b409d2 | 961 | case D2C_EVENT_SEND_COMPLETE_RESULT_OK: | 
| AzureIoTClient | 34:51d158b409d2 | 962 | iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 963 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 964 | case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_CANNOT_PARSE: | 
| AzureIoTClient | 34:51d158b409d2 | 965 | case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING: | 
| AzureIoTClient | 34:51d158b409d2 | 966 | iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 967 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 968 | case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_TIMEOUT: | 
| AzureIoTClient | 34:51d158b409d2 | 969 | iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT; | 
| AzureIoTClient | 34:51d158b409d2 | 970 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 971 | case D2C_EVENT_SEND_COMPLETE_RESULT_DEVICE_DESTROYED: | 
| AzureIoTClient | 34:51d158b409d2 | 972 | iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY; | 
| AzureIoTClient | 34:51d158b409d2 | 973 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 974 | case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_UNKNOWN: | 
| AzureIoTClient | 34:51d158b409d2 | 975 | default: | 
| AzureIoTClient | 34:51d158b409d2 | 976 | iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 977 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 978 | } | 
| AzureIoTClient | 30:20a85b733111 | 979 | |
| AzureIoTClient | 34:51d158b409d2 | 980 | return iothub_send_result; | 
| AzureIoTClient | 30:20a85b733111 | 981 | } | 
| AzureIoTClient | 30:20a85b733111 | 982 | |
| AzureIoTClient | 30:20a85b733111 | 983 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 984 | // Callback function for device_send_event_async. | 
| AzureIoTClient | 30:20a85b733111 | 985 | static void on_event_send_complete(IOTHUB_MESSAGE_LIST* message, D2C_EVENT_SEND_RESULT result, void* context) | 
| AzureIoTClient | 30:20a85b733111 | 986 | { | 
| AzureIoTClient | 34:51d158b409d2 | 987 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context; | 
| AzureIoTClient | 30:20a85b733111 | 988 | |
| AzureIoTClient | 34:51d158b409d2 | 989 | if (result != D2C_EVENT_SEND_COMPLETE_RESULT_OK && result != D2C_EVENT_SEND_COMPLETE_RESULT_DEVICE_DESTROYED) | 
| AzureIoTClient | 34:51d158b409d2 | 990 | { | 
| AzureIoTClient | 34:51d158b409d2 | 991 | registered_device->number_of_send_event_complete_failures++; | 
| AzureIoTClient | 34:51d158b409d2 | 992 | } | 
| AzureIoTClient | 34:51d158b409d2 | 993 | else | 
| AzureIoTClient | 34:51d158b409d2 | 994 | { | 
| AzureIoTClient | 34:51d158b409d2 | 995 | registered_device->number_of_send_event_complete_failures = 0; | 
| AzureIoTClient | 34:51d158b409d2 | 996 | } | 
| AzureIoTClient | 30:20a85b733111 | 997 | |
| AzureIoTClient | 34:51d158b409d2 | 998 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_056: [If `message->callback` is not NULL, it shall invoked with the `iothub_send_result`] | 
| AzureIoTClient | 34:51d158b409d2 | 999 | if (message->callback != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1000 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1001 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_050: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_OK, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_OK] | 
| AzureIoTClient | 34:51d158b409d2 | 1002 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_051: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_CANNOT_PARSE, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 1003 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_052: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 1004 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_053: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_TIMEOUT, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT] | 
| AzureIoTClient | 34:51d158b409d2 | 1005 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_054: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_DEVICE_DESTROYED, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY] | 
| AzureIoTClient | 34:51d158b409d2 | 1006 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_055: [If result is D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_UNKNOWN, `iothub_send_result` shall be set using IOTHUB_CLIENT_CONFIRMATION_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 1007 | IOTHUB_CLIENT_CONFIRMATION_RESULT iothub_send_result = get_iothub_client_confirmation_result_from(result); | 
| AzureIoTClient | 30:20a85b733111 | 1008 | |
| AzureIoTClient | 34:51d158b409d2 | 1009 | message->callback(iothub_send_result, message->context); | 
| AzureIoTClient | 34:51d158b409d2 | 1010 | } | 
| AzureIoTClient | 30:20a85b733111 | 1011 | |
| AzureIoTClient | 34:51d158b409d2 | 1012 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_057: [`message->messageHandle` shall be destroyed using IoTHubMessage_Destroy] | 
| AzureIoTClient | 34:51d158b409d2 | 1013 | IoTHubMessage_Destroy(message->messageHandle); | 
| AzureIoTClient | 34:51d158b409d2 | 1014 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_058: [`message` shall be destroyed using free] | 
| AzureIoTClient | 34:51d158b409d2 | 1015 | free(message); | 
| AzureIoTClient | 30:20a85b733111 | 1016 | } | 
| AzureIoTClient | 30:20a85b733111 | 1017 | |
| AzureIoTClient | 30:20a85b733111 | 1018 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 1019 | // Gets events from wait to send list and sends to service in the order they were added. | 
| AzureIoTClient | 30:20a85b733111 | 1020 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 1021 | // 0 if all events could be sent to the next layer successfully, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 1022 | static int send_pending_events(AMQP_TRANSPORT_DEVICE_INSTANCE* device_state) | 
| AzureIoTClient | 30:20a85b733111 | 1023 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1024 | int result; | 
| AzureIoTClient | 34:51d158b409d2 | 1025 | IOTHUB_MESSAGE_LIST* message; | 
| AzureIoTClient | 30:20a85b733111 | 1026 | |
| AzureIoTClient | 34:51d158b409d2 | 1027 | result = RESULT_OK; | 
| AzureIoTClient | 30:20a85b733111 | 1028 | |
| AzureIoTClient | 34:51d158b409d2 | 1029 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_047: [If the registered device is started, each event on `registered_device->wait_to_send_list` shall be removed from the list and sent using device_send_event_async()] | 
| AzureIoTClient | 34:51d158b409d2 | 1030 | while ((message = get_next_event_to_send(device_state)) != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1031 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1032 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_048: [device_send_event_async() shall be invoked passing `on_event_send_complete`] | 
| AzureIoTClient | 34:51d158b409d2 | 1033 | if (device_send_event_async(device_state->device_handle, message, on_event_send_complete, device_state) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1034 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1035 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_049: [If device_send_event_async() fails, `on_event_send_complete` shall be invoked passing EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING and return] | 
| AzureIoTClient | 34:51d158b409d2 | 1036 | LogError("Device '%s' failed to send message (device_send_event_async failed)", STRING_c_str(device_state->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1037 | result = __FAILURE__; | 
| AzureIoTClient | 30:20a85b733111 | 1038 | |
| AzureIoTClient | 34:51d158b409d2 | 1039 | on_event_send_complete(message, D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING, device_state); | 
| AzureIoTClient | 34:51d158b409d2 | 1040 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 1041 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1042 | } | 
| AzureIoTClient | 30:20a85b733111 | 1043 | |
| AzureIoTClient | 34:51d158b409d2 | 1044 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 1045 | } | 
| AzureIoTClient | 30:20a85b733111 | 1046 | |
| AzureIoTClient | 30:20a85b733111 | 1047 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 1048 | // Auxiliary function for the public DoWork API, performing DoWork activities (authenticate, messaging) for a specific device. | 
| AzureIoTClient | 30:20a85b733111 | 1049 | // @requires | 
| AzureIoTClient | 30:20a85b733111 | 1050 | // The transport to have a valid instance of AMQP_CONNECTION (from which to obtain SESSION_HANDLE and CBS_HANDLE) | 
| AzureIoTClient | 30:20a85b733111 | 1051 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 1052 | // 0 if no errors occur, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 1053 | static int IoTHubTransport_AMQP_Common_Device_DoWork(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device) | 
| AzureIoTClient | 30:20a85b733111 | 1054 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1055 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 1056 | |
| AzureIoTClient | 34:51d158b409d2 | 1057 | if (registered_device->device_state != DEVICE_STATE_STARTED) | 
| AzureIoTClient | 34:51d158b409d2 | 1058 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1059 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_036: [If the device state is DEVICE_STATE_STOPPED, it shall be started] | 
| AzureIoTClient | 34:51d158b409d2 | 1060 | if (registered_device->device_state == DEVICE_STATE_STOPPED) | 
| AzureIoTClient | 34:51d158b409d2 | 1061 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1062 | SESSION_HANDLE session_handle; | 
| AzureIoTClient | 34:51d158b409d2 | 1063 | CBS_HANDLE cbs_handle = NULL; | 
| AzureIoTClient | 30:20a85b733111 | 1064 | |
| AzureIoTClient | 34:51d158b409d2 | 1065 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_039: [amqp_connection_get_session_handle() shall be invoked on `instance->connection`] | 
| AzureIoTClient | 34:51d158b409d2 | 1066 | if (amqp_connection_get_session_handle(registered_device->transport_instance->amqp_connection, &session_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1067 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1068 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_040: [If amqp_connection_get_session_handle() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return] | 
| AzureIoTClient | 34:51d158b409d2 | 1069 | LogError("Failed performing DoWork for device '%s' (failed to get the amqp_connection session_handle)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1070 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1071 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1072 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_037: [If transport is using CBS authentication, amqp_connection_get_cbs_handle() shall be invoked on `instance->connection`] | 
| AzureIoTClient | 34:51d158b409d2 | 1073 | else if (registered_device->transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS && | 
| AzureIoTClient | 34:51d158b409d2 | 1074 | amqp_connection_get_cbs_handle(registered_device->transport_instance->amqp_connection, &cbs_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1075 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1076 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_038: [If amqp_connection_get_cbs_handle() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return] | 
| AzureIoTClient | 34:51d158b409d2 | 1077 | LogError("Failed performing DoWork for device '%s' (failed to get the amqp_connection cbs_handle)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1078 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1079 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1080 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_041: [The device handle shall be started using device_start_async()] | 
| AzureIoTClient | 34:51d158b409d2 | 1081 | else if (device_start_async(registered_device->device_handle, session_handle, cbs_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1082 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1083 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_042: [If device_start_async() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and skip to the next registered device] | 
| AzureIoTClient | 34:51d158b409d2 | 1084 | LogError("Failed performing DoWork for device '%s' (failed to start device)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1085 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1086 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1087 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1088 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1089 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1090 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1091 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1092 | else if (registered_device->device_state == DEVICE_STATE_STARTING || | 
| AzureIoTClient | 30:20a85b733111 | 1093 | registered_device->device_state == DEVICE_STATE_STOPPING) | 
| AzureIoTClient | 34:51d158b409d2 | 1094 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1095 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_043: [If the device handle is in state DEVICE_STATE_STARTING or DEVICE_STATE_STOPPING, it shall be checked for state change timeout] | 
| AzureIoTClient | 34:51d158b409d2 | 1096 | bool is_timed_out; | 
| AzureIoTClient | 34:51d158b409d2 | 1097 | if (is_timeout_reached(registered_device->time_of_last_state_change, registered_device->max_state_change_timeout_secs, &is_timed_out) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1098 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1099 | LogError("Failed performing DoWork for device '%s' (failed tracking timeout of device %d state)", STRING_c_str(registered_device->device_id), registered_device->device_state); | 
| AzureIoTClient | 34:51d158b409d2 | 1100 | registered_device->device_state = DEVICE_STATE_ERROR_AUTH; // if time could not be calculated, the worst must be assumed. | 
| AzureIoTClient | 34:51d158b409d2 | 1101 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1102 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1103 | else if (is_timed_out) | 
| AzureIoTClient | 34:51d158b409d2 | 1104 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1105 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_044: [If the device times out in state DEVICE_STATE_STARTING or DEVICE_STATE_STOPPING, the registered device shall be marked with failure] | 
| AzureIoTClient | 34:51d158b409d2 | 1106 | LogError("Failed performing DoWork for device '%s' (device failed to start or stop within expected timeout)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1107 | registered_device->device_state = DEVICE_STATE_ERROR_AUTH; // this will cause device to be stopped bellow on the next call to this function. | 
| AzureIoTClient | 34:51d158b409d2 | 1108 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1109 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1110 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1111 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1112 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1113 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1114 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1115 | else // i.e., DEVICE_STATE_ERROR_AUTH || DEVICE_STATE_ERROR_AUTH_TIMEOUT || DEVICE_STATE_ERROR_MSG | 
| AzureIoTClient | 34:51d158b409d2 | 1116 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1117 | LogError("Failed performing DoWork for device '%s' (device reported state %d; number of previous failures: %d)", | 
| AzureIoTClient | 34:51d158b409d2 | 1118 | STRING_c_str(registered_device->device_id), registered_device->device_state, registered_device->number_of_previous_failures); | 
| AzureIoTClient | 30:20a85b733111 | 1119 | |
| AzureIoTClient | 34:51d158b409d2 | 1120 | registered_device->number_of_previous_failures++; | 
| AzureIoTClient | 30:20a85b733111 | 1121 | |
| AzureIoTClient | 34:51d158b409d2 | 1122 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_046: [If the device has failed for MAX_NUMBER_OF_DEVICE_FAILURES in a row, it shall trigger a connection retry on the transport] | 
| AzureIoTClient | 34:51d158b409d2 | 1123 | if (registered_device->number_of_previous_failures >= MAX_NUMBER_OF_DEVICE_FAILURES) | 
| AzureIoTClient | 34:51d158b409d2 | 1124 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1125 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1126 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1127 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_045: [If the registered device has a failure, it shall be stopped using device_stop()] | 
| AzureIoTClient | 34:51d158b409d2 | 1128 | else if (device_stop(registered_device->device_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1129 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1130 | LogError("Failed to stop reset device '%s' (device_stop failed)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1131 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1132 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1133 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1134 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1135 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1136 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1137 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1138 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1139 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_031: [ Once the device is authenticated, `iothubtransportamqp_methods_subscribe` shall be invoked (subsequent DoWork calls shall not call it if already subscribed). ] | 
| AzureIoTClient | 34:51d158b409d2 | 1140 | else if (registered_device->subscribe_methods_needed && | 
| AzureIoTClient | 34:51d158b409d2 | 1141 | !registered_device->subscribed_for_methods && | 
| AzureIoTClient | 34:51d158b409d2 | 1142 | subscribe_methods(registered_device) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1143 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1144 | LogError("Failed performing DoWork for device '%s' (failed registering for device methods)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1145 | registered_device->number_of_previous_failures++; | 
| AzureIoTClient | 34:51d158b409d2 | 1146 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1147 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1148 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1149 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1150 | if (send_pending_events(registered_device) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1151 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1152 | LogError("Failed performing DoWork for device '%s' (failed sending pending events)", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1153 | registered_device->number_of_previous_failures++; | 
| AzureIoTClient | 34:51d158b409d2 | 1154 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1155 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1156 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1157 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1158 | registered_device->number_of_previous_failures = 0; | 
| AzureIoTClient | 34:51d158b409d2 | 1159 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1160 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1161 | } | 
| AzureIoTClient | 30:20a85b733111 | 1162 | |
| AzureIoTClient | 34:51d158b409d2 | 1163 | // No harm in invoking this as API will simply exit if the state is not "started". | 
| AzureIoTClient | 34:51d158b409d2 | 1164 | device_do_work(registered_device->device_handle); | 
| AzureIoTClient | 30:20a85b733111 | 1165 | |
| AzureIoTClient | 34:51d158b409d2 | 1166 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 1167 | } | 
| AzureIoTClient | 30:20a85b733111 | 1168 | |
| AzureIoTClient | 30:20a85b733111 | 1169 | |
| AzureIoTClient | 30:20a85b733111 | 1170 | //---------- SetOption-ish Helpers ----------// | 
| AzureIoTClient | 30:20a85b733111 | 1171 | |
| AzureIoTClient | 30:20a85b733111 | 1172 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 1173 | // Gets all the device-specific options and replicates them into this new registered device. | 
| AzureIoTClient | 30:20a85b733111 | 1174 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 1175 | // 0 if the function succeeds, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 1176 | static int replicate_device_options_to(AMQP_TRANSPORT_DEVICE_INSTANCE* dev_instance, DEVICE_AUTH_MODE auth_mode) | 
| AzureIoTClient | 30:20a85b733111 | 1177 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1178 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 1179 | |
| AzureIoTClient | 34:51d158b409d2 | 1180 | if (device_set_option( | 
| AzureIoTClient | 34:51d158b409d2 | 1181 | dev_instance->device_handle, | 
| AzureIoTClient | 34:51d158b409d2 | 1182 | DEVICE_OPTION_EVENT_SEND_TIMEOUT_SECS, | 
| AzureIoTClient | 34:51d158b409d2 | 1183 | &dev_instance->transport_instance->option_send_event_timeout_secs) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1184 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1185 | LogError("Failed to apply option DEVICE_OPTION_EVENT_SEND_TIMEOUT_SECS to device '%s' (device_set_option failed)", STRING_c_str(dev_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1186 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1187 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1188 | else if (auth_mode == DEVICE_AUTH_MODE_CBS) | 
| AzureIoTClient | 34:51d158b409d2 | 1189 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1190 | if (device_set_option( | 
| AzureIoTClient | 34:51d158b409d2 | 1191 | dev_instance->device_handle, | 
| AzureIoTClient | 34:51d158b409d2 | 1192 | DEVICE_OPTION_CBS_REQUEST_TIMEOUT_SECS, | 
| AzureIoTClient | 34:51d158b409d2 | 1193 | &dev_instance->transport_instance->option_cbs_request_timeout_secs) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1194 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1195 | LogError("Failed to apply option DEVICE_OPTION_CBS_REQUEST_TIMEOUT_SECS to device '%s' (device_set_option failed)", STRING_c_str(dev_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1196 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1197 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1198 | else if (device_set_option( | 
| AzureIoTClient | 34:51d158b409d2 | 1199 | dev_instance->device_handle, | 
| AzureIoTClient | 34:51d158b409d2 | 1200 | DEVICE_OPTION_SAS_TOKEN_LIFETIME_SECS, | 
| AzureIoTClient | 34:51d158b409d2 | 1201 | &dev_instance->transport_instance->option_sas_token_lifetime_secs) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1202 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1203 | LogError("Failed to apply option DEVICE_OPTION_SAS_TOKEN_LIFETIME_SECS to device '%s' (device_set_option failed)", STRING_c_str(dev_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1204 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1205 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1206 | else if (device_set_option( | 
| AzureIoTClient | 34:51d158b409d2 | 1207 | dev_instance->device_handle, | 
| AzureIoTClient | 34:51d158b409d2 | 1208 | DEVICE_OPTION_SAS_TOKEN_REFRESH_TIME_SECS, | 
| AzureIoTClient | 34:51d158b409d2 | 1209 | &dev_instance->transport_instance->option_sas_token_refresh_time_secs) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1210 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1211 | LogError("Failed to apply option DEVICE_OPTION_SAS_TOKEN_REFRESH_TIME_SECS to device '%s' (device_set_option failed)", STRING_c_str(dev_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1212 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1213 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1214 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1215 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1216 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1217 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1218 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1219 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1220 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1221 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1222 | } | 
| AzureIoTClient | 30:20a85b733111 | 1223 | |
| AzureIoTClient | 34:51d158b409d2 | 1224 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 1225 | } | 
| AzureIoTClient | 30:20a85b733111 | 1226 | |
| AzureIoTClient | 30:20a85b733111 | 1227 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 1228 | // Translates from the option names supported by iothubtransport_amqp_common to the ones supported by iothubtransport_amqp_device. | 
| AzureIoTClient | 30:20a85b733111 | 1229 | static const char* get_device_option_name_from(const char* iothubclient_option_name) | 
| AzureIoTClient | 30:20a85b733111 | 1230 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1231 | const char* device_option_name; | 
| AzureIoTClient | 30:20a85b733111 | 1232 | |
| AzureIoTClient | 34:51d158b409d2 | 1233 | if (strcmp(OPTION_SAS_TOKEN_LIFETIME, iothubclient_option_name) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1234 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1235 | device_option_name = DEVICE_OPTION_SAS_TOKEN_LIFETIME_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1236 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1237 | else if (strcmp(OPTION_SAS_TOKEN_REFRESH_TIME, iothubclient_option_name) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1238 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1239 | device_option_name = DEVICE_OPTION_SAS_TOKEN_REFRESH_TIME_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1240 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1241 | else if (strcmp(OPTION_CBS_REQUEST_TIMEOUT, iothubclient_option_name) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1242 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1243 | device_option_name = DEVICE_OPTION_CBS_REQUEST_TIMEOUT_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1244 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1245 | else if (strcmp(OPTION_EVENT_SEND_TIMEOUT_SECS, iothubclient_option_name) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1246 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1247 | device_option_name = DEVICE_OPTION_EVENT_SEND_TIMEOUT_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1248 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1249 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1250 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1251 | device_option_name = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1252 | } | 
| AzureIoTClient | 30:20a85b733111 | 1253 | |
| AzureIoTClient | 34:51d158b409d2 | 1254 | return device_option_name; | 
| AzureIoTClient | 30:20a85b733111 | 1255 | } | 
| AzureIoTClient | 30:20a85b733111 | 1256 | |
| AzureIoTClient | 30:20a85b733111 | 1257 | // @brief | 
| AzureIoTClient | 30:20a85b733111 | 1258 | // Auxiliary function invoked by IoTHubTransport_AMQP_Common_SetOption to set an option on every registered device. | 
| AzureIoTClient | 30:20a85b733111 | 1259 | // @returns | 
| AzureIoTClient | 30:20a85b733111 | 1260 | // 0 if it succeeds, non-zero otherwise. | 
| AzureIoTClient | 30:20a85b733111 | 1261 | static int IoTHubTransport_AMQP_Common_Device_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, void* value) | 
| AzureIoTClient | 30:20a85b733111 | 1262 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1263 | int result; | 
| AzureIoTClient | 34:51d158b409d2 | 1264 | const char* device_option; | 
| AzureIoTClient | 30:20a85b733111 | 1265 | |
| AzureIoTClient | 34:51d158b409d2 | 1266 | if ((device_option = get_device_option_name_from(option)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1267 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1268 | LogError("failed setting option '%s' to registered device (could not match name to options supported by device)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 1269 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1270 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1271 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1272 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1273 | AMQP_TRANSPORT_INSTANCE* instance = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 34:51d158b409d2 | 1274 | result = RESULT_OK; | 
| AzureIoTClient | 30:20a85b733111 | 1275 | |
| AzureIoTClient | 34:51d158b409d2 | 1276 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(instance->registered_devices); | 
| AzureIoTClient | 30:20a85b733111 | 1277 | |
| AzureIoTClient | 34:51d158b409d2 | 1278 | while (list_item != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1279 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1280 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device; | 
| AzureIoTClient | 30:20a85b733111 | 1281 | |
| AzureIoTClient | 34:51d158b409d2 | 1282 | if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1283 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1284 | LogError("failed setting option '%s' to registered device (singlylinkedlist_item_get_value failed)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 1285 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1286 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 1287 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1288 | else if (device_set_option(registered_device->device_handle, device_option, value) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1289 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1290 | LogError("failed setting option '%s' to registered device '%s' (device_set_option failed)", | 
| AzureIoTClient | 34:51d158b409d2 | 1291 | option, STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1292 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1293 | break; | 
| AzureIoTClient | 34:51d158b409d2 | 1294 | } | 
| AzureIoTClient | 30:20a85b733111 | 1295 | |
| AzureIoTClient | 34:51d158b409d2 | 1296 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 34:51d158b409d2 | 1297 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1298 | } | 
| AzureIoTClient | 30:20a85b733111 | 1299 | |
| AzureIoTClient | 34:51d158b409d2 | 1300 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 1301 | } | 
| AzureIoTClient | 30:20a85b733111 | 1302 | |
| AzureIoTClient | 30:20a85b733111 | 1303 | static void internal_destroy_instance(AMQP_TRANSPORT_INSTANCE* instance) | 
| AzureIoTClient | 30:20a85b733111 | 1304 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1305 | if (instance != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1306 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1307 | update_state(instance, AMQP_TRANSPORT_STATE_BEING_DESTROYED); | 
| AzureIoTClient | 32:babfd49032ff | 1308 | |
| AzureIoTClient | 34:51d158b409d2 | 1309 | if (instance->registered_devices != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1310 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1311 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(instance->registered_devices); | 
| AzureIoTClient | 30:20a85b733111 | 1312 | |
| AzureIoTClient | 34:51d158b409d2 | 1313 | while (list_item != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1314 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1315 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item); | 
| AzureIoTClient | 34:51d158b409d2 | 1316 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 34:51d158b409d2 | 1317 | IoTHubTransport_AMQP_Common_Unregister(registered_device); | 
| AzureIoTClient | 34:51d158b409d2 | 1318 | } | 
| AzureIoTClient | 30:20a85b733111 | 1319 | |
| AzureIoTClient | 34:51d158b409d2 | 1320 | singlylinkedlist_destroy(instance->registered_devices); | 
| AzureIoTClient | 34:51d158b409d2 | 1321 | } | 
| AzureIoTClient | 30:20a85b733111 | 1322 | |
| AzureIoTClient | 34:51d158b409d2 | 1323 | if (instance->amqp_connection != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1324 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1325 | amqp_connection_destroy(instance->amqp_connection); | 
| AzureIoTClient | 34:51d158b409d2 | 1326 | } | 
| AzureIoTClient | 30:20a85b733111 | 1327 | |
| AzureIoTClient | 34:51d158b409d2 | 1328 | destroy_underlying_io_transport(instance); | 
| AzureIoTClient | 34:51d158b409d2 | 1329 | destroy_underlying_io_transport_options(instance); | 
| AzureIoTClient | 34:51d158b409d2 | 1330 | retry_control_destroy(instance->connection_retry_control); | 
| AzureIoTClient | 30:20a85b733111 | 1331 | |
| AzureIoTClient | 34:51d158b409d2 | 1332 | STRING_delete(instance->iothub_host_fqdn); | 
| AzureIoTClient | 30:20a85b733111 | 1333 | |
| AzureIoTClient | 32:babfd49032ff | 1334 | /* SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_043: [ `IoTHubTransport_AMQP_Common_Destroy` shall free the stored proxy options. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1335 | free_proxy_data(instance); | 
| AzureIoTClient | 31:adadaef857c1 | 1336 | |
| AzureIoTClient | 34:51d158b409d2 | 1337 | free(instance); | 
| AzureIoTClient | 34:51d158b409d2 | 1338 | } | 
| AzureIoTClient | 30:20a85b733111 | 1339 | } | 
| AzureIoTClient | 30:20a85b733111 | 1340 | |
| AzureIoTClient | 32:babfd49032ff | 1341 | // ---------- SendMessageDisposition helpers ---------- // | 
| AzureIoTClient | 32:babfd49032ff | 1342 | |
| AzureIoTClient | 32:babfd49032ff | 1343 | static DEVICE_MESSAGE_DISPOSITION_INFO* create_device_message_disposition_info_from(MESSAGE_CALLBACK_INFO* message_data) | 
| AzureIoTClient | 32:babfd49032ff | 1344 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1345 | DEVICE_MESSAGE_DISPOSITION_INFO* result; | 
| AzureIoTClient | 32:babfd49032ff | 1346 | |
| AzureIoTClient | 34:51d158b409d2 | 1347 | if ((result = (DEVICE_MESSAGE_DISPOSITION_INFO*)malloc(sizeof(DEVICE_MESSAGE_DISPOSITION_INFO))) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1348 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1349 | LogError("Failed creating DEVICE_MESSAGE_DISPOSITION_INFO (malloc failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 1350 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1351 | else if (mallocAndStrcpy_s(&result->source, message_data->transportContext->link_name) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1352 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1353 | LogError("Failed creating DEVICE_MESSAGE_DISPOSITION_INFO (mallocAndStrcpy_s failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 1354 | free(result); | 
| AzureIoTClient | 34:51d158b409d2 | 1355 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1356 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1357 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1358 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1359 | result->message_id = message_data->transportContext->message_id; | 
| AzureIoTClient | 34:51d158b409d2 | 1360 | } | 
| AzureIoTClient | 32:babfd49032ff | 1361 | |
| AzureIoTClient | 34:51d158b409d2 | 1362 | return result; | 
| AzureIoTClient | 32:babfd49032ff | 1363 | } | 
| AzureIoTClient | 32:babfd49032ff | 1364 | |
| AzureIoTClient | 32:babfd49032ff | 1365 | static void destroy_device_message_disposition_info(DEVICE_MESSAGE_DISPOSITION_INFO* device_message_disposition_info) | 
| AzureIoTClient | 32:babfd49032ff | 1366 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1367 | free(device_message_disposition_info->source); | 
| AzureIoTClient | 34:51d158b409d2 | 1368 | free(device_message_disposition_info); | 
| AzureIoTClient | 32:babfd49032ff | 1369 | } | 
| AzureIoTClient | 32:babfd49032ff | 1370 | |
| AzureIoTClient | 30:20a85b733111 | 1371 | |
| AzureIoTClient | 30:20a85b733111 | 1372 | // ---------- API functions ---------- // | 
| AzureIoTClient | 25:63f7d371c030 | 1373 | |
| AzureIoTClient | 25:63f7d371c030 | 1374 | TRANSPORT_LL_HANDLE IoTHubTransport_AMQP_Common_Create(const IOTHUBTRANSPORT_CONFIG* config, AMQP_GET_IO_TRANSPORT get_io_transport) | 
| AzureIoTClient | 25:63f7d371c030 | 1375 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1376 | TRANSPORT_LL_HANDLE result; | 
| AzureIoTClient | 25:63f7d371c030 | 1377 | |
| AzureIoTClient | 34:51d158b409d2 | 1378 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_001: [If `config` or `config->upperConfig` or `get_io_transport` are NULL then IoTHubTransport_AMQP_Common_Create shall fail and return NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 1379 | if (config == NULL || config->upperConfig == NULL || get_io_transport == NULL) | 
| AzureIoTClient | 30:20a85b733111 | 1380 | { | 
| AzureIoTClient | 30:20a85b733111 | 1381 | LogError("IoTHub AMQP client transport null configuration parameter (config=%p, get_io_transport=%p).", config, get_io_transport); | 
| AzureIoTClient | 34:51d158b409d2 | 1382 | result = NULL; | 
| AzureIoTClient | 30:20a85b733111 | 1383 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1384 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_002: [IoTHubTransport_AMQP_Common_Create shall fail and return NULL if `config->upperConfig->protocol` is NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 1385 | else if (config->upperConfig->protocol == NULL) | 
| AzureIoTClient | 30:20a85b733111 | 1386 | { | 
| AzureIoTClient | 30:20a85b733111 | 1387 | LogError("Failed to create the AMQP transport common instance (NULL parameter received: protocol=%p, iotHubName=%p, iotHubSuffix=%p)", | 
| AzureIoTClient | 34:51d158b409d2 | 1388 | config->upperConfig->protocol, config->upperConfig->iotHubName, config->upperConfig->iotHubSuffix); | 
| AzureIoTClient | 34:51d158b409d2 | 1389 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1390 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1391 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1392 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1393 | AMQP_TRANSPORT_INSTANCE* instance; | 
| AzureIoTClient | 25:63f7d371c030 | 1394 | |
| AzureIoTClient | 34:51d158b409d2 | 1395 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_003: [Memory shall be allocated for the transport's internal state structure (`instance`)] | 
| AzureIoTClient | 34:51d158b409d2 | 1396 | if ((instance = (AMQP_TRANSPORT_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_INSTANCE))) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1397 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1398 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_004: [If malloc() fails, IoTHubTransport_AMQP_Common_Create shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 1399 | LogError("Could not allocate AMQP transport state (malloc failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 1400 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1401 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1402 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1403 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1404 | memset(instance, 0, sizeof(AMQP_TRANSPORT_INSTANCE)); | 
| AzureIoTClient | 34:51d158b409d2 | 1405 | instance->amqp_connection_state = AMQP_CONNECTION_STATE_CLOSED; | 
| AzureIoTClient | 34:51d158b409d2 | 1406 | instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET; | 
| AzureIoTClient | 34:51d158b409d2 | 1407 | instance->state = AMQP_TRANSPORT_STATE_NOT_CONNECTED; | 
| AzureIoTClient | 34:51d158b409d2 | 1408 | instance->authorization_module = config->auth_module_handle; | 
| AzureIoTClient | 25:63f7d371c030 | 1409 | |
| AzureIoTClient | 34:51d158b409d2 | 1410 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_124: [`instance->connection_retry_control` shall be set using retry_control_create(), passing defaults EXPONENTIAL_BACKOFF_WITH_JITTER and 0] | 
| AzureIoTClient | 34:51d158b409d2 | 1411 | if ((instance->connection_retry_control = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_MAX_RETRY_TIME_IN_SECS)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1412 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1413 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_125: [If retry_control_create() fails, IoTHubTransport_AMQP_Common_Create shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 1414 | LogError("Failed to create the connection retry control."); | 
| AzureIoTClient | 34:51d158b409d2 | 1415 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1416 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1417 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_005: [If `config->upperConfig->protocolGatewayHostName` is NULL, `instance->iothub_target_fqdn` shall be set as `config->upperConfig->iotHubName` + "." + `config->upperConfig->iotHubSuffix`] | 
| AzureIoTClient | 34:51d158b409d2 | 1418 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_006: [If `config->upperConfig->protocolGatewayHostName` is not NULL, `instance->iothub_target_fqdn` shall be set with a copy of it] | 
| AzureIoTClient | 34:51d158b409d2 | 1419 | else if ((instance->iothub_host_fqdn = get_target_iothub_fqdn(config)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1420 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1421 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_007: [If `instance->iothub_target_fqdn` fails to be set, IoTHubTransport_AMQP_Common_Create shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 1422 | LogError("Failed to obtain the iothub target fqdn."); | 
| AzureIoTClient | 34:51d158b409d2 | 1423 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1424 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1425 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_008: [`instance->registered_devices` shall be set using singlylinkedlist_create()] | 
| AzureIoTClient | 34:51d158b409d2 | 1426 | else if ((instance->registered_devices = singlylinkedlist_create()) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1427 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1428 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_009: [If singlylinkedlist_create() fails, IoTHubTransport_AMQP_Common_Create shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 1429 | LogError("Failed to initialize the internal list of registered devices (singlylinkedlist_create failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 1430 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 1431 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1432 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1433 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1434 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_010: [`get_io_transport` shall be saved on `instance->underlying_io_transport_provider`] | 
| AzureIoTClient | 34:51d158b409d2 | 1435 | instance->underlying_io_transport_provider = get_io_transport; | 
| AzureIoTClient | 34:51d158b409d2 | 1436 | instance->is_trace_on = false; | 
| AzureIoTClient | 34:51d158b409d2 | 1437 | instance->option_sas_token_lifetime_secs = DEFAULT_SAS_TOKEN_LIFETIME_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1438 | instance->option_sas_token_refresh_time_secs = DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1439 | instance->option_cbs_request_timeout_secs = DEFAULT_CBS_REQUEST_TIMEOUT_SECS; | 
| AzureIoTClient | 34:51d158b409d2 | 1440 | instance->option_send_event_timeout_secs = DEFAULT_EVENT_SEND_TIMEOUT_SECS; | 
| AzureIoTClient | 37:abd16824f63b | 1441 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_12_002: [The connection idle timeout parameter default value shall be set to 240000 milliseconds using connection_set_idle_timeout()] | 
| AzureIoTClient | 37:abd16824f63b | 1442 | instance->c2d_keep_alive_freq_secs = DEFAULT_C2D_KEEP_ALIVE_FREQ_SECS; | 
| AzureIoTClient | 32:babfd49032ff | 1443 | |
| AzureIoTClient | 34:51d158b409d2 | 1444 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_012: [If IoTHubTransport_AMQP_Common_Create succeeds it shall return a pointer to `instance`.] | 
| AzureIoTClient | 34:51d158b409d2 | 1445 | result = (TRANSPORT_LL_HANDLE)instance; | 
| AzureIoTClient | 34:51d158b409d2 | 1446 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1447 | |
| AzureIoTClient | 34:51d158b409d2 | 1448 | if (result == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1449 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1450 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_011: [If IoTHubTransport_AMQP_Common_Create fails it shall free any memory it allocated] | 
| AzureIoTClient | 34:51d158b409d2 | 1451 | internal_destroy_instance(instance); | 
| AzureIoTClient | 34:51d158b409d2 | 1452 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1453 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1454 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1455 | |
| AzureIoTClient | 26:ee14eed604f6 | 1456 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1457 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1458 | |
| AzureIoTClient | 25:63f7d371c030 | 1459 | IOTHUB_PROCESS_ITEM_RESULT IoTHubTransport_AMQP_Common_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item) | 
| AzureIoTClient | 25:63f7d371c030 | 1460 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1461 | IOTHUB_PROCESS_ITEM_RESULT result; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1462 | |
| AzureIoTClient | 41:71c01aa3df1a | 1463 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_144: [If `handle` or `iothub_item` are NULL, `IoTHubTransport_AMQP_Common_ProcessItem` shall fail and return IOTHUB_PROCESS_ERROR.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1464 | if (handle == NULL || iothub_item == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1465 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1466 | LogError("Invalid argument (handle=%p, iothub_item=%p)", handle, iothub_item); | 
| AzureIoTClient | 41:71c01aa3df1a | 1467 | result = IOTHUB_PROCESS_ERROR; | 
| AzureIoTClient | 41:71c01aa3df1a | 1468 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1469 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1470 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1471 | if (item_type == IOTHUB_TYPE_DEVICE_TWIN) | 
| AzureIoTClient | 41:71c01aa3df1a | 1472 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1473 | AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT* dev_twin_ctx; | 
| AzureIoTClient | 41:71c01aa3df1a | 1474 | |
| AzureIoTClient | 41:71c01aa3df1a | 1475 | if ((dev_twin_ctx = (AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT*)malloc(sizeof(AMQP_TRANSPORT_DEVICE_TWIN_CONTEXT))) == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1476 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1477 | LogError("Failed allocating context for TWIN message"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1478 | result = IOTHUB_PROCESS_ERROR; | 
| AzureIoTClient | 41:71c01aa3df1a | 1479 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1480 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1481 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1482 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)iothub_item->device_twin->device_handle; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1483 | |
| AzureIoTClient | 41:71c01aa3df1a | 1484 | dev_twin_ctx->client_handle = iothub_item->device_twin->client_handle; | 
| AzureIoTClient | 41:71c01aa3df1a | 1485 | dev_twin_ctx->item_id = iothub_item->device_twin->item_id; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1486 | |
| AzureIoTClient | 41:71c01aa3df1a | 1487 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_146: [device_send_twin_update_async() shall be invoked passing `iothub_item->device_twin->report_data_handle` and `on_device_send_twin_update_complete_callback`] | 
| AzureIoTClient | 41:71c01aa3df1a | 1488 | if (device_send_twin_update_async( | 
| AzureIoTClient | 41:71c01aa3df1a | 1489 | registered_device->device_handle, | 
| AzureIoTClient | 41:71c01aa3df1a | 1490 | iothub_item->device_twin->report_data_handle, | 
| AzureIoTClient | 41:71c01aa3df1a | 1491 | on_device_send_twin_update_complete_callback, (void*)dev_twin_ctx) != RESULT_OK) | 
| AzureIoTClient | 41:71c01aa3df1a | 1492 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1493 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_147: [If device_send_twin_update_async() fails, `IoTHubTransport_AMQP_Common_ProcessItem` shall fail and return IOTHUB_PROCESS_ERROR.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1494 | LogError("Failed sending TWIN update"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1495 | free(dev_twin_ctx); | 
| AzureIoTClient | 41:71c01aa3df1a | 1496 | result = IOTHUB_PROCESS_ERROR; | 
| AzureIoTClient | 41:71c01aa3df1a | 1497 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1498 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1499 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1500 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_150: [If no errors occur, `IoTHubTransport_AMQP_Common_ProcessItem` shall return IOTHUB_PROCESS_OK.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1501 | result = IOTHUB_PROCESS_OK; | 
| AzureIoTClient | 41:71c01aa3df1a | 1502 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1503 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1504 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1505 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_145: [If `item_type` is not IOTHUB_TYPE_DEVICE_TWIN, `IoTHubTransport_AMQP_Common_ProcessItem` shall fail and return IOTHUB_PROCESS_ERROR.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1506 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1507 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1508 | LogError("Item type not supported (%d)", item_type); | 
| AzureIoTClient | 41:71c01aa3df1a | 1509 | result = IOTHUB_PROCESS_ERROR; | 
| AzureIoTClient | 41:71c01aa3df1a | 1510 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1511 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 1512 | |
| AzureIoTClient | 41:71c01aa3df1a | 1513 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1514 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1515 | |
| AzureIoTClient | 25:63f7d371c030 | 1516 | void IoTHubTransport_AMQP_Common_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) | 
| AzureIoTClient | 25:63f7d371c030 | 1517 | { | 
| AzureIoTClient | 30:20a85b733111 | 1518 | (void)iotHubClientHandle; // unused as of now. | 
| AzureIoTClient | 30:20a85b733111 | 1519 | |
| AzureIoTClient | 34:51d158b409d2 | 1520 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_016: [If `handle` is NULL, IoTHubTransport_AMQP_Common_DoWork shall return without doing any work] | 
| AzureIoTClient | 25:63f7d371c030 | 1521 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1522 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1523 | LogError("IoTHubClient DoWork failed: transport handle parameter is NULL."); | 
| AzureIoTClient | 25:63f7d371c030 | 1524 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1525 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1526 | { | 
| AzureIoTClient | 30:20a85b733111 | 1527 | AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 34:51d158b409d2 | 1528 | LIST_ITEM_HANDLE list_item; | 
| AzureIoTClient | 30:20a85b733111 | 1529 | |
| AzureIoTClient | 46:c688c75b63b9 | 1530 | if (transport_instance->state == AMQP_TRANSPORT_STATE_NOT_CONNECTED_NO_MORE_RETRIES) | 
| AzureIoTClient | 46:c688c75b63b9 | 1531 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1532 | // Nothing to be done. | 
| AzureIoTClient | 46:c688c75b63b9 | 1533 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1534 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_017: [If `instance->state` is `RECONNECTION_REQUIRED`, IoTHubTransport_AMQP_Common_DoWork shall attempt to trigger the connection-retry logic and return] | 
| AzureIoTClient | 46:c688c75b63b9 | 1535 | else if (transport_instance->state == AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED) | 
| AzureIoTClient | 34:51d158b409d2 | 1536 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1537 | RETRY_ACTION retry_action; | 
| AzureIoTClient | 46:c688c75b63b9 | 1538 | |
| AzureIoTClient | 34:51d158b409d2 | 1539 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_126: [The connection retry shall be attempted only if retry_control_should_retry() returns RETRY_ACTION_NOW, or if it fails] | 
| AzureIoTClient | 34:51d158b409d2 | 1540 | if (retry_control_should_retry(transport_instance->connection_retry_control, &retry_action) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1541 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1542 | LogError("retry_control_should_retry() failed; assuming immediate connection retry for safety."); | 
| AzureIoTClient | 34:51d158b409d2 | 1543 | retry_action = RETRY_ACTION_RETRY_NOW; | 
| AzureIoTClient | 34:51d158b409d2 | 1544 | } | 
| AzureIoTClient | 30:20a85b733111 | 1545 | |
| AzureIoTClient | 34:51d158b409d2 | 1546 | if (retry_action == RETRY_ACTION_RETRY_NOW) | 
| AzureIoTClient | 34:51d158b409d2 | 1547 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1548 | prepare_for_connection_retry(transport_instance); | 
| AzureIoTClient | 34:51d158b409d2 | 1549 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1550 | else if (retry_action == RETRY_ACTION_STOP_RETRYING) | 
| AzureIoTClient | 46:c688c75b63b9 | 1551 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1552 | update_state(transport_instance, AMQP_TRANSPORT_STATE_NOT_CONNECTED_NO_MORE_RETRIES); | 
| AzureIoTClient | 25:63f7d371c030 | 1553 | |
| AzureIoTClient | 46:c688c75b63b9 | 1554 | (void)singlylinkedlist_foreach(transport_instance->registered_devices, raise_connection_status_callback_retry_expired, NULL); | 
| AzureIoTClient | 34:51d158b409d2 | 1555 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1556 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1557 | else | 
| AzureIoTClient | 46:c688c75b63b9 | 1558 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1559 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_018: [If there are no devices registered on the transport, IoTHubTransport_AMQP_Common_DoWork shall skip do_work for devices] | 
| AzureIoTClient | 46:c688c75b63b9 | 1560 | if ((list_item = singlylinkedlist_get_head_item(transport_instance->registered_devices)) != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1561 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1562 | // We need to check if there are devices, otherwise the amqp_connection won't be able to be created since | 
| AzureIoTClient | 46:c688c75b63b9 | 1563 | // there is not a preferred authentication mode set yet on the transport. | 
| AzureIoTClient | 30:20a85b733111 | 1564 | |
| AzureIoTClient | 46:c688c75b63b9 | 1565 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_019: [If `instance->amqp_connection` is NULL, it shall be established] | 
| AzureIoTClient | 46:c688c75b63b9 | 1566 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_12_003: [AMQP connection will be configured using the `c2d_keep_alive_freq_secs` value from SetOption ] | 
| AzureIoTClient | 46:c688c75b63b9 | 1567 | if (transport_instance->amqp_connection == NULL && establish_amqp_connection(transport_instance) != RESULT_OK) | 
| AzureIoTClient | 46:c688c75b63b9 | 1568 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1569 | LogError("AMQP transport failed to establish connection with service."); | 
| AzureIoTClient | 46:c688c75b63b9 | 1570 | |
| AzureIoTClient | 46:c688c75b63b9 | 1571 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED); | 
| AzureIoTClient | 46:c688c75b63b9 | 1572 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1573 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_020: [If the amqp_connection is OPENED, the transport shall iterate through each registered device and perform a device-specific do_work on each] | 
| AzureIoTClient | 46:c688c75b63b9 | 1574 | else if (transport_instance->amqp_connection_state == AMQP_CONNECTION_STATE_OPENED) | 
| AzureIoTClient | 46:c688c75b63b9 | 1575 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1576 | while (list_item != NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1577 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1578 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device; | 
| AzureIoTClient | 30:20a85b733111 | 1579 | |
| AzureIoTClient | 46:c688c75b63b9 | 1580 | if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 1581 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1582 | LogError("Transport had an unexpected failure during DoWork (failed to fetch a registered_devices list item value)"); | 
| AzureIoTClient | 46:c688c75b63b9 | 1583 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1584 | else if (registered_device->number_of_send_event_complete_failures >= MAX_NUMBER_OF_DEVICE_FAILURES) | 
| AzureIoTClient | 46:c688c75b63b9 | 1585 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1586 | LogError("Device '%s' reported a critical failure (events completed sending with failures); connection retry will be triggered.", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 30:20a85b733111 | 1587 | |
| AzureIoTClient | 34:51d158b409d2 | 1588 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED); | 
| AzureIoTClient | 34:51d158b409d2 | 1589 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1590 | else if (IoTHubTransport_AMQP_Common_Device_DoWork(registered_device) != RESULT_OK) | 
| AzureIoTClient | 46:c688c75b63b9 | 1591 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1592 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_021: [If DoWork fails for the registered device for more than MAX_NUMBER_OF_DEVICE_FAILURES, connection retry shall be triggered] | 
| AzureIoTClient | 46:c688c75b63b9 | 1593 | if (registered_device->number_of_previous_failures >= MAX_NUMBER_OF_DEVICE_FAILURES) | 
| AzureIoTClient | 46:c688c75b63b9 | 1594 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1595 | LogError("Device '%s' reported a critical failure; connection retry will be triggered.", STRING_c_str(registered_device->device_id)); | 
| AzureIoTClient | 30:20a85b733111 | 1596 | |
| AzureIoTClient | 46:c688c75b63b9 | 1597 | update_state(transport_instance, AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED); | 
| AzureIoTClient | 46:c688c75b63b9 | 1598 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1599 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 1600 | |
| AzureIoTClient | 46:c688c75b63b9 | 1601 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 46:c688c75b63b9 | 1602 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1603 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1604 | } | 
| AzureIoTClient | 30:20a85b733111 | 1605 | |
| AzureIoTClient | 46:c688c75b63b9 | 1606 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_022: [If `instance->amqp_connection` is not NULL, amqp_connection_do_work shall be invoked] | 
| AzureIoTClient | 46:c688c75b63b9 | 1607 | if (transport_instance->amqp_connection != NULL) | 
| AzureIoTClient | 46:c688c75b63b9 | 1608 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 1609 | amqp_connection_do_work(transport_instance->amqp_connection); | 
| AzureIoTClient | 46:c688c75b63b9 | 1610 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1611 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1612 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1613 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1614 | |
| AzureIoTClient | 25:63f7d371c030 | 1615 | int IoTHubTransport_AMQP_Common_Subscribe(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1616 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1617 | int result; | 
| AzureIoTClient | 25:63f7d371c030 | 1618 | |
| AzureIoTClient | 34:51d158b409d2 | 1619 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_084: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Subscribe shall return a non-zero result] | 
| AzureIoTClient | 25:63f7d371c030 | 1620 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1621 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1622 | LogError("Invalid handle to IoTHubClient AMQP transport device handle."); | 
| AzureIoTClient | 29:7e8852b14e3b | 1623 | result = __FAILURE__; | 
| AzureIoTClient | 25:63f7d371c030 | 1624 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1625 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1626 | { | 
| AzureIoTClient | 30:20a85b733111 | 1627 | AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 30:20a85b733111 | 1628 | |
| AzureIoTClient | 34:51d158b409d2 | 1629 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_085: [If `amqp_device_instance` is not registered, IoTHubTransport_AMQP_Common_Subscribe shall return a non-zero result] | 
| AzureIoTClient | 34:51d158b409d2 | 1630 | if (!is_device_registered(amqp_device_instance)) | 
| AzureIoTClient | 34:51d158b409d2 | 1631 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1632 | LogError("Device '%s' failed subscribing to cloud-to-device messages (device is not registered)", STRING_c_str(amqp_device_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1633 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1634 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1635 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_086: [device_subscribe_message() shall be invoked passing `on_message_received_callback`] | 
| AzureIoTClient | 34:51d158b409d2 | 1636 | else if (device_subscribe_message(amqp_device_instance->device_handle, on_message_received, amqp_device_instance) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1637 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1638 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_087: [If device_subscribe_message() fails, IoTHubTransport_AMQP_Common_Subscribe shall return a non-zero result] | 
| AzureIoTClient | 34:51d158b409d2 | 1639 | LogError("Device '%s' failed subscribing to cloud-to-device messages (device_subscribe_message failed)", STRING_c_str(amqp_device_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1640 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 1641 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1642 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1643 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1644 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_088: [If no failures occur, IoTHubTransport_AMQP_Common_Subscribe shall return 0] | 
| AzureIoTClient | 34:51d158b409d2 | 1645 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1646 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1647 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1648 | |
| AzureIoTClient | 25:63f7d371c030 | 1649 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1650 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1651 | |
| AzureIoTClient | 25:63f7d371c030 | 1652 | void IoTHubTransport_AMQP_Common_Unsubscribe(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1653 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1654 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_093: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Subscribe shall return] | 
| AzureIoTClient | 25:63f7d371c030 | 1655 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1656 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1657 | LogError("Invalid handle to IoTHubClient AMQP transport device handle."); | 
| AzureIoTClient | 25:63f7d371c030 | 1658 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1659 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1660 | { | 
| AzureIoTClient | 30:20a85b733111 | 1661 | AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 30:20a85b733111 | 1662 | |
| AzureIoTClient | 34:51d158b409d2 | 1663 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_094: [If `amqp_device_instance` is not registered, IoTHubTransport_AMQP_Common_Subscribe shall return] | 
| AzureIoTClient | 34:51d158b409d2 | 1664 | if (!is_device_registered(amqp_device_instance)) | 
| AzureIoTClient | 34:51d158b409d2 | 1665 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1666 | LogError("Device '%s' failed unsubscribing to cloud-to-device messages (device is not registered)", STRING_c_str(amqp_device_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1667 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1668 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_095: [device_unsubscribe_message() shall be invoked passing `amqp_device_instance->device_handle`] | 
| AzureIoTClient | 34:51d158b409d2 | 1669 | else if (device_unsubscribe_message(amqp_device_instance->device_handle) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1670 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1671 | LogError("Device '%s' failed unsubscribing to cloud-to-device messages (device_unsubscribe_message failed)", STRING_c_str(amqp_device_instance->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 1672 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1673 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1674 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1675 | |
| AzureIoTClient | 25:63f7d371c030 | 1676 | int IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1677 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1678 | int result; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1679 | |
| AzureIoTClient | 41:71c01aa3df1a | 1680 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_131: [If `handle` is NULL, `IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin` shall fail and return non-zero.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1681 | if (handle == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1682 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1683 | LogError("Invalid argument (handle is NULL"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1684 | result = __FAILURE__; | 
| AzureIoTClient | 41:71c01aa3df1a | 1685 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1686 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1687 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1688 | AMQP_TRANSPORT_INSTANCE* transport = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1689 | |
| AzureIoTClient | 41:71c01aa3df1a | 1690 | if (get_number_of_registered_devices(transport) != 1) | 
| AzureIoTClient | 41:71c01aa3df1a | 1691 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1692 | LogError("Device Twin not supported on device multiplexing scenario"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1693 | result = __FAILURE__; | 
| AzureIoTClient | 41:71c01aa3df1a | 1694 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1695 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1696 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1697 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(transport->registered_devices); | 
| AzureIoTClient | 39:e98d5df6dc74 | 1698 | |
| AzureIoTClient | 41:71c01aa3df1a | 1699 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_136: [If no errors occur, `IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin` shall return zero.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1700 | result = RESULT_OK; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1701 | |
| AzureIoTClient | 41:71c01aa3df1a | 1702 | while (list_item != NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1703 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1704 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1705 | |
| AzureIoTClient | 41:71c01aa3df1a | 1706 | if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1707 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1708 | LogError("Failed retrieving registered device information"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1709 | result = __FAILURE__; | 
| AzureIoTClient | 41:71c01aa3df1a | 1710 | break; | 
| AzureIoTClient | 41:71c01aa3df1a | 1711 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1712 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_134: [device_subscribe_for_twin_updates() shall be invoked for the registered device, passing `on_device_twin_update_received_callback`] | 
| AzureIoTClient | 41:71c01aa3df1a | 1713 | else if (device_subscribe_for_twin_updates(registered_device->device_handle, on_device_twin_update_received_callback, (void*)registered_device) != RESULT_OK) | 
| AzureIoTClient | 41:71c01aa3df1a | 1714 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1715 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_135: [If device_subscribe_for_twin_updates() fails, `IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin` shall fail and return non-zero.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1716 | LogError("Failed subscribing for device Twin updates"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1717 | result = __FAILURE__; | 
| AzureIoTClient | 41:71c01aa3df1a | 1718 | break; | 
| AzureIoTClient | 41:71c01aa3df1a | 1719 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 1720 | |
| AzureIoTClient | 41:71c01aa3df1a | 1721 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 41:71c01aa3df1a | 1722 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1723 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1724 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 1725 | |
| AzureIoTClient | 41:71c01aa3df1a | 1726 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1727 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1728 | |
| AzureIoTClient | 25:63f7d371c030 | 1729 | void IoTHubTransport_AMQP_Common_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1730 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1731 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_140: [If `handle` is NULL, `IoTHubTransport_AMQP_Common_Unsubscribe_DeviceTwin` shall return.] | 
| AzureIoTClient | 41:71c01aa3df1a | 1732 | if (handle == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1733 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1734 | LogError("Invalid argument (handle is NULL"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1735 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1736 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1737 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1738 | AMQP_TRANSPORT_INSTANCE* transport = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1739 | |
| AzureIoTClient | 41:71c01aa3df1a | 1740 | if (get_number_of_registered_devices(transport) != 1) | 
| AzureIoTClient | 41:71c01aa3df1a | 1741 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1742 | LogError("Device Twin not supported on device multiplexing scenario"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1743 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1744 | else | 
| AzureIoTClient | 41:71c01aa3df1a | 1745 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1746 | LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(transport->registered_devices); | 
| AzureIoTClient | 39:e98d5df6dc74 | 1747 | |
| AzureIoTClient | 41:71c01aa3df1a | 1748 | while (list_item != NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1749 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1750 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device; | 
| AzureIoTClient | 39:e98d5df6dc74 | 1751 | |
| AzureIoTClient | 41:71c01aa3df1a | 1752 | if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL) | 
| AzureIoTClient | 41:71c01aa3df1a | 1753 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1754 | LogError("Failed retrieving registered device information"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1755 | break; | 
| AzureIoTClient | 41:71c01aa3df1a | 1756 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1757 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_142: [device_unsubscribe_for_twin_updates() shall be invoked for the registered device] | 
| AzureIoTClient | 41:71c01aa3df1a | 1758 | else if (device_unsubscribe_for_twin_updates(registered_device->device_handle) != RESULT_OK) | 
| AzureIoTClient | 41:71c01aa3df1a | 1759 | { | 
| AzureIoTClient | 41:71c01aa3df1a | 1760 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_143: [If `device_unsubscribe_for_twin_updates` fails, the error shall be ignored] | 
| AzureIoTClient | 41:71c01aa3df1a | 1761 | LogError("Failed unsubscribing for device Twin updates"); | 
| AzureIoTClient | 41:71c01aa3df1a | 1762 | break; | 
| AzureIoTClient | 41:71c01aa3df1a | 1763 | } | 
| AzureIoTClient | 39:e98d5df6dc74 | 1764 | |
| AzureIoTClient | 41:71c01aa3df1a | 1765 | list_item = singlylinkedlist_get_next_item(list_item); | 
| AzureIoTClient | 41:71c01aa3df1a | 1766 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1767 | } | 
| AzureIoTClient | 41:71c01aa3df1a | 1768 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1769 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1770 | |
| AzureIoTClient | 25:63f7d371c030 | 1771 | int IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1772 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1773 | int result; | 
| AzureIoTClient | 25:63f7d371c030 | 1774 | |
| AzureIoTClient | 25:63f7d371c030 | 1775 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1776 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1777 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_004: [ If `handle` is NULL, `IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod` shall fail and return a non-zero value. ] */ | 
| AzureIoTClient | 25:63f7d371c030 | 1778 | LogError("NULL handle"); | 
| AzureIoTClient | 29:7e8852b14e3b | 1779 | result = __FAILURE__; | 
| AzureIoTClient | 25:63f7d371c030 | 1780 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1781 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1782 | { | 
| AzureIoTClient | 30:20a85b733111 | 1783 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 25:63f7d371c030 | 1784 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_026: [ `IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod` shall remember that a subscribe is to be performed in the next call to DoWork and on success it shall return 0. ]*/ | 
| AzureIoTClient | 25:63f7d371c030 | 1785 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_005: [ If the transport is already subscribed to receive C2D method requests, `IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod` shall perform no additional action and return 0. ]*/ | 
| AzureIoTClient | 29:7e8852b14e3b | 1786 | device_state->subscribe_methods_needed = true; | 
| AzureIoTClient | 29:7e8852b14e3b | 1787 | device_state->subscribed_for_methods = false; | 
| AzureIoTClient | 25:63f7d371c030 | 1788 | result = 0; | 
| AzureIoTClient | 25:63f7d371c030 | 1789 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1790 | |
| AzureIoTClient | 25:63f7d371c030 | 1791 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1792 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1793 | |
| AzureIoTClient | 25:63f7d371c030 | 1794 | void IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 1795 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1796 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1797 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1798 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_006: [ If `handle` is NULL, `IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod` shall do nothing. ]*/ | 
| AzureIoTClient | 25:63f7d371c030 | 1799 | LogError("NULL handle"); | 
| AzureIoTClient | 25:63f7d371c030 | 1800 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1801 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1802 | { | 
| AzureIoTClient | 30:20a85b733111 | 1803 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 25:63f7d371c030 | 1804 | |
| AzureIoTClient | 25:63f7d371c030 | 1805 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_008: [ If the transport is not subscribed to receive C2D method requests then `IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod` shall do nothing. ]*/ | 
| AzureIoTClient | 29:7e8852b14e3b | 1806 | if (device_state->subscribe_methods_needed) | 
| AzureIoTClient | 25:63f7d371c030 | 1807 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1808 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_007: [ `IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod` shall unsubscribe from receiving C2D method requests by calling `iothubtransportamqp_methods_unsubscribe`. ]*/ | 
| AzureIoTClient | 34:51d158b409d2 | 1809 | device_state->subscribed_for_methods = false; | 
| AzureIoTClient | 34:51d158b409d2 | 1810 | device_state->subscribe_methods_needed = false; | 
| AzureIoTClient | 34:51d158b409d2 | 1811 | iothubtransportamqp_methods_unsubscribe(device_state->methods_handle); | 
| AzureIoTClient | 25:63f7d371c030 | 1812 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1813 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1814 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1815 | |
| AzureIoTClient | 26:ee14eed604f6 | 1816 | int IoTHubTransport_AMQP_Common_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response) | 
| AzureIoTClient | 26:ee14eed604f6 | 1817 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 1818 | (void)response; | 
| AzureIoTClient | 26:ee14eed604f6 | 1819 | (void)response_size; | 
| AzureIoTClient | 26:ee14eed604f6 | 1820 | (void)status_response; | 
| AzureIoTClient | 26:ee14eed604f6 | 1821 | (void)methodId; | 
| AzureIoTClient | 26:ee14eed604f6 | 1822 | int result; | 
| AzureIoTClient | 30:20a85b733111 | 1823 | AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 26:ee14eed604f6 | 1824 | if (device_state != NULL) | 
| AzureIoTClient | 26:ee14eed604f6 | 1825 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 1826 | IOTHUBTRANSPORT_AMQP_METHOD_HANDLE saved_handle = (IOTHUBTRANSPORT_AMQP_METHOD_HANDLE)methodId; | 
| AzureIoTClient | 26:ee14eed604f6 | 1827 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_019: [ `IoTHubTransport_AMQP_Common_DeviceMethod_Response` shall call `iothubtransportamqp_methods_respond` passing to it the `method_handle` argument, the response bytes, response size and the status code. ]*/ | 
| AzureIoTClient | 26:ee14eed604f6 | 1828 | if (iothubtransportamqp_methods_respond(saved_handle, response, response_size, status_response) != 0) | 
| AzureIoTClient | 26:ee14eed604f6 | 1829 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 1830 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_029: [ If `iothubtransportamqp_methods_respond` fails, `on_methods_request_received` shall return a non-zero value. ]*/ | 
| AzureIoTClient | 26:ee14eed604f6 | 1831 | LogError("iothubtransportamqp_methods_respond failed"); | 
| AzureIoTClient | 29:7e8852b14e3b | 1832 | result = __FAILURE__; | 
| AzureIoTClient | 26:ee14eed604f6 | 1833 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1834 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 1835 | { | 
| AzureIoTClient | 26:ee14eed604f6 | 1836 | result = 0; | 
| AzureIoTClient | 26:ee14eed604f6 | 1837 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1838 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1839 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 1840 | { | 
| AzureIoTClient | 29:7e8852b14e3b | 1841 | result = __FAILURE__; | 
| AzureIoTClient | 26:ee14eed604f6 | 1842 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1843 | return result; | 
| AzureIoTClient | 26:ee14eed604f6 | 1844 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 1845 | |
| AzureIoTClient | 25:63f7d371c030 | 1846 | IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) | 
| AzureIoTClient | 25:63f7d371c030 | 1847 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1848 | IOTHUB_CLIENT_RESULT result; | 
| AzureIoTClient | 25:63f7d371c030 | 1849 | |
| AzureIoTClient | 34:51d158b409d2 | 1850 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_096: [If `handle` or `iotHubClientStatus` are NULL, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG] | 
| AzureIoTClient | 34:51d158b409d2 | 1851 | if (handle == NULL || iotHubClientStatus == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 1852 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1853 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 30:20a85b733111 | 1854 | LogError("Failed retrieving the device send status (either handle (%p) or iotHubClientStatus (%p) are NULL)", handle, iotHubClientStatus); | 
| AzureIoTClient | 25:63f7d371c030 | 1855 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1856 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1857 | { | 
| AzureIoTClient | 30:20a85b733111 | 1858 | AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle; | 
| AzureIoTClient | 25:63f7d371c030 | 1859 | |
| AzureIoTClient | 34:51d158b409d2 | 1860 | DEVICE_SEND_STATUS device_send_status; | 
| AzureIoTClient | 34:51d158b409d2 | 1861 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_097: [IoTHubTransport_AMQP_Common_GetSendStatus shall invoke device_get_send_status()] | 
| AzureIoTClient | 34:51d158b409d2 | 1862 | if (device_get_send_status(amqp_device_state->device_handle, &device_send_status) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1863 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1864 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_098: [If device_get_send_status() fails, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 1865 | LogError("Failed retrieving the device send status (device_get_send_status failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 1866 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 1867 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1868 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1869 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1870 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_099: [If device_get_send_status() returns DEVICE_SEND_STATUS_BUSY, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY] | 
| AzureIoTClient | 34:51d158b409d2 | 1871 | if (device_send_status == DEVICE_SEND_STATUS_BUSY) | 
| AzureIoTClient | 34:51d158b409d2 | 1872 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1873 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY; | 
| AzureIoTClient | 34:51d158b409d2 | 1874 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1875 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_100: [If device_get_send_status() returns DEVICE_SEND_STATUS_IDLE, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE] | 
| AzureIoTClient | 34:51d158b409d2 | 1876 | else // DEVICE_SEND_STATUS_IDLE | 
| AzureIoTClient | 34:51d158b409d2 | 1877 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1878 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE; | 
| AzureIoTClient | 34:51d158b409d2 | 1879 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1880 | |
| AzureIoTClient | 34:51d158b409d2 | 1881 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_109: [If no failures occur, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_OK] | 
| AzureIoTClient | 34:51d158b409d2 | 1882 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1883 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1884 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1885 | |
| AzureIoTClient | 25:63f7d371c030 | 1886 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 1887 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1888 | |
| AzureIoTClient | 25:63f7d371c030 | 1889 | IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value) | 
| AzureIoTClient | 25:63f7d371c030 | 1890 | { | 
| AzureIoTClient | 25:63f7d371c030 | 1891 | IOTHUB_CLIENT_RESULT result; | 
| AzureIoTClient | 25:63f7d371c030 | 1892 | |
| AzureIoTClient | 34:51d158b409d2 | 1893 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_101: [If `handle`, `option` or `value` are NULL then IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] | 
| AzureIoTClient | 30:20a85b733111 | 1894 | if ((handle == NULL) || (option == NULL) || (value == NULL)) | 
| AzureIoTClient | 25:63f7d371c030 | 1895 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1896 | LogError("Invalid parameter (NULL) passed to AMQP transport SetOption (handle=%p, options=%p, value=%p)", handle, option, value); | 
| AzureIoTClient | 34:51d158b409d2 | 1897 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 25:63f7d371c030 | 1898 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1899 | else | 
| AzureIoTClient | 25:63f7d371c030 | 1900 | { | 
| AzureIoTClient | 30:20a85b733111 | 1901 | AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 34:51d158b409d2 | 1902 | bool is_device_specific_option; | 
| AzureIoTClient | 25:63f7d371c030 | 1903 | |
| AzureIoTClient | 34:51d158b409d2 | 1904 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_102: [If `option` is a device-specific option, it shall be saved and applied to each registered device using device_set_option()] | 
| AzureIoTClient | 34:51d158b409d2 | 1905 | if (strcmp(OPTION_SAS_TOKEN_LIFETIME, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1906 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1907 | is_device_specific_option = true; | 
| AzureIoTClient | 34:51d158b409d2 | 1908 | transport_instance->option_sas_token_lifetime_secs = *(size_t*)value; | 
| AzureIoTClient | 34:51d158b409d2 | 1909 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1910 | else if (strcmp(OPTION_SAS_TOKEN_REFRESH_TIME, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1911 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1912 | is_device_specific_option = true; | 
| AzureIoTClient | 34:51d158b409d2 | 1913 | transport_instance->option_sas_token_refresh_time_secs = *(size_t*)value; | 
| AzureIoTClient | 34:51d158b409d2 | 1914 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1915 | else if (strcmp(OPTION_CBS_REQUEST_TIMEOUT, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1916 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1917 | is_device_specific_option = true; | 
| AzureIoTClient | 34:51d158b409d2 | 1918 | transport_instance->option_cbs_request_timeout_secs = *(size_t*)value; | 
| AzureIoTClient | 34:51d158b409d2 | 1919 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1920 | else if (strcmp(OPTION_EVENT_SEND_TIMEOUT_SECS, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1921 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1922 | is_device_specific_option = true; | 
| AzureIoTClient | 34:51d158b409d2 | 1923 | transport_instance->option_send_event_timeout_secs = *(size_t*)value; | 
| AzureIoTClient | 34:51d158b409d2 | 1924 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1925 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1926 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1927 | is_device_specific_option = false; | 
| AzureIoTClient | 34:51d158b409d2 | 1928 | } | 
| AzureIoTClient | 25:63f7d371c030 | 1929 | |
| AzureIoTClient | 34:51d158b409d2 | 1930 | if (is_device_specific_option) | 
| AzureIoTClient | 34:51d158b409d2 | 1931 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1932 | if (IoTHubTransport_AMQP_Common_Device_SetOption(handle, option, (void*)value) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1933 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1934 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_103: [If device_set_option() fails, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 1935 | LogError("transport failed setting option '%s' (failed setting option on one or more registered devices)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 1936 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 1937 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1938 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1939 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1940 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1941 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1942 | } | 
| AzureIoTClient | 38:6435c6e3d55c | 1943 | else if (strcmp(OPTION_C2D_KEEP_ALIVE_FREQ_SECS, option) == 0) | 
| AzureIoTClient | 38:6435c6e3d55c | 1944 | { | 
| AzureIoTClient | 38:6435c6e3d55c | 1945 | transport_instance->c2d_keep_alive_freq_secs = *(size_t*)value; | 
| AzureIoTClient | 38:6435c6e3d55c | 1946 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 38:6435c6e3d55c | 1947 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1948 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_104: [If `option` is `logtrace`, `value` shall be saved and applied to `instance->connection` using amqp_connection_set_logging()] | 
| AzureIoTClient | 34:51d158b409d2 | 1949 | else if (strcmp(OPTION_LOG_TRACE, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 1950 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1951 | transport_instance->is_trace_on = *((bool*)value); | 
| AzureIoTClient | 25:63f7d371c030 | 1952 | |
| AzureIoTClient | 34:51d158b409d2 | 1953 | if (transport_instance->amqp_connection != NULL && | 
| AzureIoTClient | 34:51d158b409d2 | 1954 | amqp_connection_set_logging(transport_instance->amqp_connection, transport_instance->is_trace_on) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 1955 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1956 | LogError("transport failed setting option '%s' (amqp_connection_set_logging failed)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 1957 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 1958 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1959 | else | 
| AzureIoTClient | 34:51d158b409d2 | 1960 | { | 
| AzureIoTClient | 34:51d158b409d2 | 1961 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 1962 | } | 
| AzureIoTClient | 34:51d158b409d2 | 1963 | } | 
| AzureIoTClient | 31:adadaef857c1 | 1964 | else if (strcmp(OPTION_HTTP_PROXY, option) == 0) | 
| AzureIoTClient | 31:adadaef857c1 | 1965 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1966 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_032: [ If `option` is `proxy_data`, `value` shall be used as an `HTTP_PROXY_OPTIONS*`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1967 | HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value; | 
| AzureIoTClient | 31:adadaef857c1 | 1968 | |
| AzureIoTClient | 31:adadaef857c1 | 1969 | if (transport_instance->tls_io != NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 1970 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1971 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_038: [ If the underlying IO has already been created, then `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1972 | LogError("Cannot set proxy option once the underlying IO is created"); | 
| AzureIoTClient | 31:adadaef857c1 | 1973 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 31:adadaef857c1 | 1974 | } | 
| AzureIoTClient | 31:adadaef857c1 | 1975 | else if (proxy_options->host_address == NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 1976 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1977 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_034: [ If `host_address` is NULL, `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1978 | LogError("NULL host_address in proxy options"); | 
| AzureIoTClient | 31:adadaef857c1 | 1979 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 31:adadaef857c1 | 1980 | } | 
| AzureIoTClient | 31:adadaef857c1 | 1981 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_037: [ If only one of `username` and `password` is NULL, `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1982 | else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) && | 
| AzureIoTClient | 31:adadaef857c1 | 1983 | (proxy_options->username != proxy_options->password)) | 
| AzureIoTClient | 31:adadaef857c1 | 1984 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1985 | LogError("Only one of username and password for proxy settings was NULL"); | 
| AzureIoTClient | 31:adadaef857c1 | 1986 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 31:adadaef857c1 | 1987 | } | 
| AzureIoTClient | 31:adadaef857c1 | 1988 | else | 
| AzureIoTClient | 31:adadaef857c1 | 1989 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1990 | char* copied_proxy_hostname = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 1991 | char* copied_proxy_username = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 1992 | char* copied_proxy_password = NULL; | 
| AzureIoTClient | 31:adadaef857c1 | 1993 | |
| AzureIoTClient | 31:adadaef857c1 | 1994 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_033: [ The fields `host_address`, `port`, `username` and `password` shall be saved for later used (needed when creating the underlying IO to be used by the transport). ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1995 | transport_instance->http_proxy_port = proxy_options->port; | 
| AzureIoTClient | 31:adadaef857c1 | 1996 | if (mallocAndStrcpy_s(&copied_proxy_hostname, proxy_options->host_address) != 0) | 
| AzureIoTClient | 31:adadaef857c1 | 1997 | { | 
| AzureIoTClient | 31:adadaef857c1 | 1998 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_035: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 1999 | LogError("Cannot copy HTTP proxy hostname"); | 
| AzureIoTClient | 31:adadaef857c1 | 2000 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 31:adadaef857c1 | 2001 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2002 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_036: [ `username` and `password` shall be allowed to be NULL. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2003 | else if ((proxy_options->username != NULL) && (mallocAndStrcpy_s(&copied_proxy_username, proxy_options->username) != 0)) | 
| AzureIoTClient | 31:adadaef857c1 | 2004 | { | 
| AzureIoTClient | 31:adadaef857c1 | 2005 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_035: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2006 | free(copied_proxy_hostname); | 
| AzureIoTClient | 31:adadaef857c1 | 2007 | LogError("Cannot copy HTTP proxy username"); | 
| AzureIoTClient | 31:adadaef857c1 | 2008 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 31:adadaef857c1 | 2009 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2010 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_036: [ `username` and `password` shall be allowed to be NULL. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2011 | else if ((proxy_options->password != NULL) && (mallocAndStrcpy_s(&copied_proxy_password, proxy_options->password) != 0)) | 
| AzureIoTClient | 31:adadaef857c1 | 2012 | { | 
| AzureIoTClient | 31:adadaef857c1 | 2013 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_035: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_AMQP_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2014 | if (copied_proxy_username != NULL) | 
| AzureIoTClient | 31:adadaef857c1 | 2015 | { | 
| AzureIoTClient | 31:adadaef857c1 | 2016 | free(copied_proxy_username); | 
| AzureIoTClient | 31:adadaef857c1 | 2017 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2018 | free(copied_proxy_hostname); | 
| AzureIoTClient | 31:adadaef857c1 | 2019 | LogError("Cannot copy HTTP proxy password"); | 
| AzureIoTClient | 31:adadaef857c1 | 2020 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 31:adadaef857c1 | 2021 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2022 | else | 
| AzureIoTClient | 31:adadaef857c1 | 2023 | { | 
| AzureIoTClient | 31:adadaef857c1 | 2024 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_040: [ When setting the proxy options succeeds any previously saved proxy options shall be freed. ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2025 | free_proxy_data(transport_instance); | 
| AzureIoTClient | 31:adadaef857c1 | 2026 | |
| AzureIoTClient | 31:adadaef857c1 | 2027 | transport_instance->http_proxy_hostname = copied_proxy_hostname; | 
| AzureIoTClient | 31:adadaef857c1 | 2028 | transport_instance->http_proxy_username = copied_proxy_username; | 
| AzureIoTClient | 31:adadaef857c1 | 2029 | transport_instance->http_proxy_password = copied_proxy_password; | 
| AzureIoTClient | 31:adadaef857c1 | 2030 | |
| AzureIoTClient | 31:adadaef857c1 | 2031 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_039: [ If setting the `proxy_data` option succeeds, `IoTHubTransport_AMQP_Common_SetOption` shall return `IOTHUB_CLIENT_OK` ]*/ | 
| AzureIoTClient | 31:adadaef857c1 | 2032 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 31:adadaef857c1 | 2033 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2034 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2035 | } | 
| AzureIoTClient | 31:adadaef857c1 | 2036 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2037 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2038 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 30:20a85b733111 | 2039 | |
| AzureIoTClient | 34:51d158b409d2 | 2040 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_007: [ If `option` is `x509certificate` and the transport preferred authentication method is not x509 then IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ] | 
| AzureIoTClient | 34:51d158b409d2 | 2041 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_008: [ If `option` is `x509privatekey` and the transport preferred authentication method is not x509 then IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ] | 
| AzureIoTClient | 34:51d158b409d2 | 2042 | if (strcmp(OPTION_X509_CERT, option) == 0 || strcmp(OPTION_X509_PRIVATE_KEY, option) == 0) | 
| AzureIoTClient | 34:51d158b409d2 | 2043 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2044 | if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET) | 
| AzureIoTClient | 34:51d158b409d2 | 2045 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2046 | transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_X509; | 
| AzureIoTClient | 34:51d158b409d2 | 2047 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2048 | else if (transport_instance->preferred_authentication_mode != AMQP_TRANSPORT_AUTHENTICATION_MODE_X509) | 
| AzureIoTClient | 34:51d158b409d2 | 2049 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2050 | LogError("transport failed setting option '%s' (preferred authentication method is not x509)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 2051 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 34:51d158b409d2 | 2052 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2053 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2054 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2055 | IoTHubClient_Auth_Set_x509_Type(transport_instance->authorization_module, true); | 
| AzureIoTClient | 34:51d158b409d2 | 2056 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2057 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2058 | |
| AzureIoTClient | 34:51d158b409d2 | 2059 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_105: [If `option` does not match one of the options handled by this module, it shall be passed to `instance->tls_io` using xio_setoption()] | 
| AzureIoTClient | 34:51d158b409d2 | 2060 | if (result != IOTHUB_CLIENT_INVALID_ARG) | 
| AzureIoTClient | 34:51d158b409d2 | 2061 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2062 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_106: [If `instance->tls_io` is NULL, it shall be set invoking instance->underlying_io_transport_provider()] | 
| AzureIoTClient | 34:51d158b409d2 | 2063 | if (transport_instance->tls_io == NULL && | 
| AzureIoTClient | 34:51d158b409d2 | 2064 | get_new_underlying_io_transport(transport_instance, &transport_instance->tls_io) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2065 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2066 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_107: [If instance->underlying_io_transport_provider() fails, IoTHubTransport_AMQP_Common_SetOption shall fail and return IOTHUB_CLIENT_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 2067 | LogError("transport failed setting option '%s' (failed to obtain a TLS I/O transport).", option); | 
| AzureIoTClient | 34:51d158b409d2 | 2068 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 2069 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2070 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_108: [When `instance->tls_io` is created, IoTHubTransport_AMQP_Common_SetOption shall apply `instance->saved_tls_options` with OptionHandler_FeedOptions()] | 
| AzureIoTClient | 34:51d158b409d2 | 2071 | else if (xio_setoption(transport_instance->tls_io, option, value) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2072 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2073 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_001: [If xio_setoption fails, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_ERROR.] | 
| AzureIoTClient | 34:51d158b409d2 | 2074 | LogError("transport failed setting option '%s' (xio_setoption failed)", option); | 
| AzureIoTClient | 34:51d158b409d2 | 2075 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 2076 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2077 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2078 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2079 | if (save_underlying_io_transport_options(transport_instance) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2080 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2081 | LogError("IoTHubTransport_AMQP_Common_SetOption failed to save underlying I/O options; failure will be ignored"); | 
| AzureIoTClient | 34:51d158b409d2 | 2082 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2083 | |
| AzureIoTClient | 34:51d158b409d2 | 2084 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_001: [If no failures occur, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_OK.] | 
| AzureIoTClient | 34:51d158b409d2 | 2085 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 2086 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2087 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2088 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2089 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2090 | |
| AzureIoTClient | 25:63f7d371c030 | 2091 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 2092 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2093 | |
| AzureIoTClient | 25:63f7d371c030 | 2094 | IOTHUB_DEVICE_HANDLE IoTHubTransport_AMQP_Common_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend) | 
| AzureIoTClient | 25:63f7d371c030 | 2095 | { | 
| AzureIoTClient | 25:63f7d371c030 | 2096 | #ifdef NO_LOGGING | 
| AzureIoTClient | 25:63f7d371c030 | 2097 | UNUSED(iotHubClientHandle); | 
| AzureIoTClient | 25:63f7d371c030 | 2098 | #endif | 
| AzureIoTClient | 25:63f7d371c030 | 2099 | |
| AzureIoTClient | 30:20a85b733111 | 2100 | IOTHUB_DEVICE_HANDLE result; | 
| AzureIoTClient | 30:20a85b733111 | 2101 | |
| AzureIoTClient | 34:51d158b409d2 | 2102 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_17_005: [If `handle`, `device`, `iotHubClientHandle` or `waitingToSend` is NULL, IoTHubTransport_AMQP_Common_Register shall return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 2103 | if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL) || (iotHubClientHandle == NULL)) | 
| AzureIoTClient | 25:63f7d371c030 | 2104 | { | 
| AzureIoTClient | 30:20a85b733111 | 2105 | LogError("invalid parameter TRANSPORT_LL_HANDLE handle=%p, const IOTHUB_DEVICE_CONFIG* device=%p, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle=%p, PDLIST_ENTRY waiting_to_send=%p", | 
| AzureIoTClient | 25:63f7d371c030 | 2106 | handle, device, iotHubClientHandle, waitingToSend); | 
| AzureIoTClient | 34:51d158b409d2 | 2107 | result = NULL; | 
| AzureIoTClient | 25:63f7d371c030 | 2108 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2109 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_002: [IoTHubTransport_AMQP_Common_Register shall return NULL if `device->deviceId` is NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 2110 | else if (device->deviceId == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2111 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2112 | LogError("Transport failed to register device (device_id provided is NULL)"); | 
| AzureIoTClient | 34:51d158b409d2 | 2113 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2114 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2115 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 2116 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2117 | LIST_ITEM_HANDLE list_item; | 
| AzureIoTClient | 30:20a85b733111 | 2118 | AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 25:63f7d371c030 | 2119 | |
| AzureIoTClient | 34:51d158b409d2 | 2120 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_064: [If the device is already registered, IoTHubTransport_AMQP_Common_Register shall fail and return NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 2121 | if (is_device_registered_ex(transport_instance->registered_devices, device->deviceId, &list_item)) | 
| AzureIoTClient | 34:51d158b409d2 | 2122 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2123 | LogError("IoTHubTransport_AMQP_Common_Register failed (device '%s' already registered on this transport instance)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2124 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2125 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2126 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_065: [IoTHubTransport_AMQP_Common_Register shall fail and return NULL if the device is not using an authentication mode compatible with the currently used by the transport.] | 
| AzureIoTClient | 34:51d158b409d2 | 2127 | else if (!is_device_credential_acceptable(device, transport_instance->preferred_authentication_mode)) | 
| AzureIoTClient | 26:ee14eed604f6 | 2128 | { | 
| AzureIoTClient | 30:20a85b733111 | 2129 | LogError("Transport failed to register device '%s' (device credential was not accepted)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2130 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2131 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2132 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 2133 | { | 
| AzureIoTClient | 30:20a85b733111 | 2134 | AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance; | 
| AzureIoTClient | 25:63f7d371c030 | 2135 | |
| AzureIoTClient | 34:51d158b409d2 | 2136 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_066: [IoTHubTransport_AMQP_Common_Register shall allocate an instance of AMQP_TRANSPORT_DEVICE_INSTANCE to store the state of the new registered device.] | 
| AzureIoTClient | 30:20a85b733111 | 2137 | if ((amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_DEVICE_INSTANCE))) == NULL) | 
| AzureIoTClient | 26:ee14eed604f6 | 2138 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2139 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_067: [If malloc fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 2140 | LogError("Transport failed to register device '%s' (failed to create the device state instance; malloc failed)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2141 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2142 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2143 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 2144 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2145 | memset(amqp_device_instance, 0, sizeof(AMQP_TRANSPORT_DEVICE_INSTANCE)); | 
| AzureIoTClient | 34:51d158b409d2 | 2146 | |
| AzureIoTClient | 34:51d158b409d2 | 2147 | char* local_product_info = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2148 | void* product_info; | 
| AzureIoTClient | 34:51d158b409d2 | 2149 | if ((IoTHubClient_LL_GetOption(iotHubClientHandle, OPTION_PRODUCT_INFO, &product_info) != IOTHUB_CLIENT_OK) || (product_info == NULL)) | 
| AzureIoTClient | 34:51d158b409d2 | 2150 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2151 | mallocAndStrcpy_s(&local_product_info, CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION); | 
| AzureIoTClient | 34:51d158b409d2 | 2152 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2153 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2154 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2155 | mallocAndStrcpy_s(&local_product_info, STRING_c_str((STRING_HANDLE)product_info)); | 
| AzureIoTClient | 34:51d158b409d2 | 2156 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2157 | |
| AzureIoTClient | 34:51d158b409d2 | 2158 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_068: [IoTHubTransport_AMQP_Common_Register shall save the handle references to the IoTHubClient, transport, waitingToSend list on `amqp_device_instance`.] | 
| AzureIoTClient | 34:51d158b409d2 | 2159 | amqp_device_instance->iothub_client_handle = iotHubClientHandle; | 
| AzureIoTClient | 30:20a85b733111 | 2160 | amqp_device_instance->transport_instance = transport_instance; | 
| AzureIoTClient | 30:20a85b733111 | 2161 | amqp_device_instance->waiting_to_send = waitingToSend; | 
| AzureIoTClient | 34:51d158b409d2 | 2162 | amqp_device_instance->device_state = DEVICE_STATE_STOPPED; | 
| AzureIoTClient | 34:51d158b409d2 | 2163 | amqp_device_instance->max_state_change_timeout_secs = DEFAULT_DEVICE_STATE_CHANGE_TIMEOUT_SECS; | 
| AzureIoTClient | 30:20a85b733111 | 2164 | amqp_device_instance->subscribe_methods_needed = false; | 
| AzureIoTClient | 30:20a85b733111 | 2165 | amqp_device_instance->subscribed_for_methods = false; | 
| AzureIoTClient | 42:c2eaa912a28c | 2166 | |
| AzureIoTClient | 34:51d158b409d2 | 2167 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_069: [A copy of `config->deviceId` shall be saved into `device_state->device_id`] | 
| AzureIoTClient | 30:20a85b733111 | 2168 | if ((amqp_device_instance->device_id = STRING_construct(device->deviceId)) == NULL) | 
| AzureIoTClient | 26:ee14eed604f6 | 2169 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2170 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_070: [If STRING_construct() fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 2171 | LogError("Transport failed to register device '%s' (failed to copy the deviceId)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2172 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2173 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2174 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 2175 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2176 | DEVICE_CONFIG device_config; | 
| AzureIoTClient | 34:51d158b409d2 | 2177 | memset(&device_config, 0, sizeof(DEVICE_CONFIG)); | 
| AzureIoTClient | 34:51d158b409d2 | 2178 | device_config.iothub_host_fqdn = (char*)STRING_c_str(transport_instance->iothub_host_fqdn); | 
| AzureIoTClient | 34:51d158b409d2 | 2179 | device_config.authorization_module = device->authorization_module; | 
| AzureIoTClient | 34:51d158b409d2 | 2180 | |
| AzureIoTClient | 34:51d158b409d2 | 2181 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_072: [The configuration for device_create shall be set according to the authentication preferred by IOTHUB_DEVICE_CONFIG] | 
| AzureIoTClient | 41:71c01aa3df1a | 2182 | device_config.authentication_mode = get_authentication_mode(device); | 
| AzureIoTClient | 34:51d158b409d2 | 2183 | device_config.on_state_changed_callback = on_device_state_changed_callback; | 
| AzureIoTClient | 34:51d158b409d2 | 2184 | device_config.on_state_changed_context = amqp_device_instance; | 
| AzureIoTClient | 34:51d158b409d2 | 2185 | device_config.product_info = local_product_info; | 
| AzureIoTClient | 30:20a85b733111 | 2186 | |
| AzureIoTClient | 34:51d158b409d2 | 2187 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_071: [`amqp_device_instance->device_handle` shall be set using device_create()] | 
| AzureIoTClient | 34:51d158b409d2 | 2188 | if ((amqp_device_instance->device_handle = device_create(&device_config)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2189 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2190 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_073: [If device_create() fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 2191 | LogError("Transport failed to register device '%s' (failed to create the DEVICE_HANDLE instance)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2192 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2193 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2194 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2195 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2196 | bool is_first_device_being_registered = (singlylinkedlist_get_head_item(transport_instance->registered_devices) == NULL); | 
| AzureIoTClient | 30:20a85b733111 | 2197 | |
| AzureIoTClient | 34:51d158b409d2 | 2198 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_010: [ `IoTHubTransport_AMQP_Common_Create` shall create a new iothubtransportamqp_methods instance by calling `iothubtransportamqp_methods_create` while passing to it the the fully qualified domain name and the device Id. ]*/ | 
| AzureIoTClient | 34:51d158b409d2 | 2199 | amqp_device_instance->methods_handle = iothubtransportamqp_methods_create(STRING_c_str(transport_instance->iothub_host_fqdn), device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2200 | if (amqp_device_instance->methods_handle == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2201 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2202 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_011: [ If `iothubtransportamqp_methods_create` fails, `IoTHubTransport_AMQP_Common_Create` shall fail and return NULL. ]*/ | 
| AzureIoTClient | 34:51d158b409d2 | 2203 | LogError("Transport failed to register device '%s' (Cannot create the methods module)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2204 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2205 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2206 | else | 
| AzureIoTClient | 42:c2eaa912a28c | 2207 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2208 | if (replicate_device_options_to(amqp_device_instance, device_config.authentication_mode) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2209 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2210 | LogError("Transport failed to register device '%s' (failed to replicate options)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2211 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2212 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2213 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_074: [IoTHubTransport_AMQP_Common_Register shall add the `amqp_device_instance` to `instance->registered_devices`] | 
| AzureIoTClient | 34:51d158b409d2 | 2214 | else if (singlylinkedlist_add(transport_instance->registered_devices, amqp_device_instance) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2215 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2216 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_075: [If it fails to add `amqp_device_instance`, IoTHubTransport_AMQP_Common_Register shall fail and return NULL] | 
| AzureIoTClient | 34:51d158b409d2 | 2217 | LogError("Transport failed to register device '%s' (singlylinkedlist_add failed)", device->deviceId); | 
| AzureIoTClient | 34:51d158b409d2 | 2218 | result = NULL; | 
| AzureIoTClient | 34:51d158b409d2 | 2219 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2220 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2221 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2222 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_076: [If the device is the first being registered on the transport, IoTHubTransport_AMQP_Common_Register shall save its authentication mode as the transport preferred authentication mode] | 
| AzureIoTClient | 34:51d158b409d2 | 2223 | if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET && | 
| AzureIoTClient | 34:51d158b409d2 | 2224 | is_first_device_being_registered) | 
| AzureIoTClient | 34:51d158b409d2 | 2225 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2226 | if (device_config.authentication_mode == DEVICE_AUTH_MODE_CBS) | 
| AzureIoTClient | 34:51d158b409d2 | 2227 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2228 | transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS; | 
| AzureIoTClient | 34:51d158b409d2 | 2229 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2230 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2231 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2232 | transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_X509; | 
| AzureIoTClient | 34:51d158b409d2 | 2233 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2234 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2235 | |
| AzureIoTClient | 34:51d158b409d2 | 2236 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_078: [IoTHubTransport_AMQP_Common_Register shall return a handle to `amqp_device_instance` as a IOTHUB_DEVICE_HANDLE] | 
| AzureIoTClient | 34:51d158b409d2 | 2237 | result = (IOTHUB_DEVICE_HANDLE)amqp_device_instance; | 
| AzureIoTClient | 34:51d158b409d2 | 2238 | } | 
| AzureIoTClient | 42:c2eaa912a28c | 2239 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2240 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2241 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2242 | |
| AzureIoTClient | 30:20a85b733111 | 2243 | if (result == NULL) | 
| AzureIoTClient | 26:ee14eed604f6 | 2244 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2245 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_077: [If IoTHubTransport_AMQP_Common_Register fails, it shall free all memory it allocated] | 
| AzureIoTClient | 34:51d158b409d2 | 2246 | internal_destroy_amqp_device_instance(amqp_device_instance); | 
| AzureIoTClient | 34:51d158b409d2 | 2247 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2248 | free(local_product_info); | 
| AzureIoTClient | 26:ee14eed604f6 | 2249 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2250 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2251 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2252 | |
| AzureIoTClient | 26:ee14eed604f6 | 2253 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 2254 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2255 | |
| AzureIoTClient | 25:63f7d371c030 | 2256 | void IoTHubTransport_AMQP_Common_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle) | 
| AzureIoTClient | 25:63f7d371c030 | 2257 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2258 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_079: [if `deviceHandle` provided is NULL, IoTHubTransport_AMQP_Common_Unregister shall return.] | 
| AzureIoTClient | 26:ee14eed604f6 | 2259 | if (deviceHandle == NULL) | 
| AzureIoTClient | 26:ee14eed604f6 | 2260 | { | 
| AzureIoTClient | 30:20a85b733111 | 2261 | LogError("Failed to unregister device (deviceHandle is NULL)."); | 
| AzureIoTClient | 26:ee14eed604f6 | 2262 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2263 | else | 
| AzureIoTClient | 26:ee14eed604f6 | 2264 | { | 
| AzureIoTClient | 30:20a85b733111 | 2265 | AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)deviceHandle; | 
| AzureIoTClient | 34:51d158b409d2 | 2266 | const char* device_id; | 
| AzureIoTClient | 34:51d158b409d2 | 2267 | LIST_ITEM_HANDLE list_item; | 
| AzureIoTClient | 25:63f7d371c030 | 2268 | |
| AzureIoTClient | 34:51d158b409d2 | 2269 | if ((device_id = STRING_c_str(registered_device->device_id)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2270 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2271 | LogError("Failed to unregister device (failed to get device id char ptr)"); | 
| AzureIoTClient | 34:51d158b409d2 | 2272 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2273 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_080: [if `deviceHandle` has a NULL reference to its transport instance, IoTHubTransport_AMQP_Common_Unregister shall return.] | 
| AzureIoTClient | 34:51d158b409d2 | 2274 | else if (registered_device->transport_instance == NULL) | 
| AzureIoTClient | 30:20a85b733111 | 2275 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2276 | LogError("Failed to unregister device '%s' (deviceHandle does not have a transport state associated to).", device_id); | 
| AzureIoTClient | 34:51d158b409d2 | 2277 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2278 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_081: [If the device is not registered with this transport, IoTHubTransport_AMQP_Common_Unregister shall return] | 
| AzureIoTClient | 34:51d158b409d2 | 2279 | else if (!is_device_registered_ex(registered_device->transport_instance->registered_devices, device_id, &list_item)) | 
| AzureIoTClient | 34:51d158b409d2 | 2280 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2281 | LogError("Failed to unregister device '%s' (device is not registered within this transport).", device_id); | 
| AzureIoTClient | 30:20a85b733111 | 2282 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2283 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2284 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2285 | // Removing it first so the race hazzard is reduced between this function and DoWork. Best would be to use locks. | 
| AzureIoTClient | 34:51d158b409d2 | 2286 | if (singlylinkedlist_remove(registered_device->transport_instance->registered_devices, list_item) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2287 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2288 | LogError("Failed to unregister device '%s' (singlylinkedlist_remove failed).", device_id); | 
| AzureIoTClient | 34:51d158b409d2 | 2289 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2290 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2291 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2292 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_012: [IoTHubTransport_AMQP_Common_Unregister shall destroy the C2D methods handler by calling iothubtransportamqp_methods_destroy] | 
| AzureIoTClient | 34:51d158b409d2 | 2293 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_083: [IoTHubTransport_AMQP_Common_Unregister shall free all the memory allocated for the `device_instance`] | 
| AzureIoTClient | 34:51d158b409d2 | 2294 | internal_destroy_amqp_device_instance(registered_device); | 
| AzureIoTClient | 34:51d158b409d2 | 2295 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2296 | } | 
| AzureIoTClient | 26:ee14eed604f6 | 2297 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2298 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2299 | |
| AzureIoTClient | 25:63f7d371c030 | 2300 | void IoTHubTransport_AMQP_Common_Destroy(TRANSPORT_LL_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 2301 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2302 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_013: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Destroy shall return immediatelly] | 
| AzureIoTClient | 34:51d158b409d2 | 2303 | if (handle == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2304 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2305 | LogError("Failed to destroy AMQP transport instance (handle is NULL)"); | 
| AzureIoTClient | 34:51d158b409d2 | 2306 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2307 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2308 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2309 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_014: [IoTHubTransport_AMQP_Common_Destroy shall invoke IoTHubTransport_AMQP_Common_Unregister on each of its registered devices.] | 
| AzureIoTClient | 34:51d158b409d2 | 2310 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_015: [All members of `instance` (including tls_io) shall be destroyed and its memory released] | 
| AzureIoTClient | 34:51d158b409d2 | 2311 | internal_destroy_instance((AMQP_TRANSPORT_INSTANCE*)handle); | 
| AzureIoTClient | 26:ee14eed604f6 | 2312 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2313 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2314 | |
| AzureIoTClient | 25:63f7d371c030 | 2315 | int IoTHubTransport_AMQP_Common_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds) | 
| AzureIoTClient | 25:63f7d371c030 | 2316 | { | 
| AzureIoTClient | 25:63f7d371c030 | 2317 | int result; | 
| AzureIoTClient | 32:babfd49032ff | 2318 | |
| AzureIoTClient | 34:51d158b409d2 | 2319 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_128: [If `handle` is NULL, `IoTHubTransport_AMQP_Common_SetRetryPolicy` shall fail and return non-zero.] | 
| AzureIoTClient | 34:51d158b409d2 | 2320 | if (handle == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2321 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2322 | LogError("Cannot set retry policy (transport handle is NULL)"); | 
| AzureIoTClient | 34:51d158b409d2 | 2323 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 2324 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2325 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2326 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2327 | RETRY_CONTROL_HANDLE new_retry_control; | 
| AzureIoTClient | 25:63f7d371c030 | 2328 | |
| AzureIoTClient | 34:51d158b409d2 | 2329 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_129: [`transport_instance->connection_retry_control` shall be set using retry_control_create(), passing `retryPolicy` and `retryTimeoutLimitInSeconds`.] | 
| AzureIoTClient | 34:51d158b409d2 | 2330 | if ((new_retry_control = retry_control_create(retryPolicy, (unsigned int)retryTimeoutLimitInSeconds)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2331 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2332 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_130: [If retry_control_create() fails, `IoTHubTransport_AMQP_Common_SetRetryPolicy` shall fail and return non-zero.] | 
| AzureIoTClient | 34:51d158b409d2 | 2333 | LogError("Cannot set retry policy (retry_control_create failed)"); | 
| AzureIoTClient | 34:51d158b409d2 | 2334 | result = __FAILURE__; | 
| AzureIoTClient | 34:51d158b409d2 | 2335 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2336 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2337 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2338 | AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle; | 
| AzureIoTClient | 34:51d158b409d2 | 2339 | RETRY_CONTROL_HANDLE previous_retry_control = transport_instance->connection_retry_control; | 
| AzureIoTClient | 25:63f7d371c030 | 2340 | |
| AzureIoTClient | 34:51d158b409d2 | 2341 | transport_instance->connection_retry_control = new_retry_control; | 
| AzureIoTClient | 32:babfd49032ff | 2342 | |
| AzureIoTClient | 34:51d158b409d2 | 2343 | retry_control_destroy(previous_retry_control); | 
| AzureIoTClient | 32:babfd49032ff | 2344 | |
| AzureIoTClient | 34:51d158b409d2 | 2345 | LogInfo("Retry policy set (%d, timeout = %d)", retryPolicy, retryTimeoutLimitInSeconds); | 
| AzureIoTClient | 32:babfd49032ff | 2346 | |
| AzureIoTClient | 46:c688c75b63b9 | 2347 | if (transport_instance->state == AMQP_TRANSPORT_STATE_NOT_CONNECTED_NO_MORE_RETRIES) | 
| AzureIoTClient | 46:c688c75b63b9 | 2348 | { | 
| AzureIoTClient | 46:c688c75b63b9 | 2349 | transport_instance->state = AMQP_TRANSPORT_STATE_RECONNECTION_REQUIRED; | 
| AzureIoTClient | 46:c688c75b63b9 | 2350 | } | 
| AzureIoTClient | 46:c688c75b63b9 | 2351 | |
| AzureIoTClient | 34:51d158b409d2 | 2352 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_128: [If no errors occur, `IoTHubTransport_AMQP_Common_SetRetryPolicy` shall return zero.] | 
| AzureIoTClient | 34:51d158b409d2 | 2353 | result = RESULT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 2354 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2355 | } | 
| AzureIoTClient | 32:babfd49032ff | 2356 | |
| AzureIoTClient | 25:63f7d371c030 | 2357 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 2358 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2359 | |
| AzureIoTClient | 25:63f7d371c030 | 2360 | STRING_HANDLE IoTHubTransport_AMQP_Common_GetHostname(TRANSPORT_LL_HANDLE handle) | 
| AzureIoTClient | 25:63f7d371c030 | 2361 | { | 
| AzureIoTClient | 25:63f7d371c030 | 2362 | STRING_HANDLE result; | 
| AzureIoTClient | 30:20a85b733111 | 2363 | |
| AzureIoTClient | 34:51d158b409d2 | 2364 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_001: [If `handle` is NULL, `IoTHubTransport_AMQP_Common_GetHostname` shall return NULL.] | 
| AzureIoTClient | 34:51d158b409d2 | 2365 | if (handle == NULL) | 
| AzureIoTClient | 25:63f7d371c030 | 2366 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2367 | LogError("Cannot provide the target host name (transport handle is NULL)."); | 
| AzureIoTClient | 30:20a85b733111 | 2368 | |
| AzureIoTClient | 34:51d158b409d2 | 2369 | result = NULL; | 
| AzureIoTClient | 30:20a85b733111 | 2370 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2371 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_002: [IoTHubTransport_AMQP_Common_GetHostname shall return a copy of `instance->iothub_target_fqdn`.] | 
| AzureIoTClient | 34:51d158b409d2 | 2372 | else if ((result = STRING_clone(((AMQP_TRANSPORT_INSTANCE*)(handle))->iothub_host_fqdn)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2373 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2374 | LogError("Cannot provide the target host name (STRING_clone failed)."); | 
| AzureIoTClient | 34:51d158b409d2 | 2375 | } | 
| AzureIoTClient | 30:20a85b733111 | 2376 | |
| AzureIoTClient | 30:20a85b733111 | 2377 | return result; | 
| AzureIoTClient | 30:20a85b733111 | 2378 | } | 
| AzureIoTClient | 30:20a85b733111 | 2379 | |
| AzureIoTClient | 30:20a85b733111 | 2380 | IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition) | 
| AzureIoTClient | 30:20a85b733111 | 2381 | { | 
| AzureIoTClient | 30:20a85b733111 | 2382 | IOTHUB_CLIENT_RESULT result; | 
| AzureIoTClient | 30:20a85b733111 | 2383 | if (message_data == NULL) | 
| AzureIoTClient | 30:20a85b733111 | 2384 | { | 
| AzureIoTClient | 30:20a85b733111 | 2385 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_10_001: [If messageData is NULL, IoTHubTransport_AMQP_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_INVALID_ARG.] */ | 
| AzureIoTClient | 30:20a85b733111 | 2386 | LogError("Failed sending message disposition (message_data is NULL)"); | 
| AzureIoTClient | 30:20a85b733111 | 2387 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 25:63f7d371c030 | 2388 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2389 | else | 
| AzureIoTClient | 25:63f7d371c030 | 2390 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2391 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_10_002: [If any of the messageData fields are NULL, IoTHubTransport_AMQP_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_INVALID_ARG.] */ | 
| AzureIoTClient | 30:20a85b733111 | 2392 | if (message_data->messageHandle == NULL || message_data->transportContext == NULL) | 
| AzureIoTClient | 30:20a85b733111 | 2393 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2394 | LogError("Failed sending message disposition (message_data->messageHandle (%p) or message_data->transportContext (%p) are NULL)", message_data->messageHandle, message_data->transportContext); | 
| AzureIoTClient | 30:20a85b733111 | 2395 | result = IOTHUB_CLIENT_INVALID_ARG; | 
| AzureIoTClient | 30:20a85b733111 | 2396 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2397 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2398 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2399 | DEVICE_MESSAGE_DISPOSITION_INFO* device_message_disposition_info; | 
| AzureIoTClient | 30:20a85b733111 | 2400 | |
| AzureIoTClient | 34:51d158b409d2 | 2401 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_10_004: [IoTHubTransport_AMQP_Common_SendMessageDisposition shall convert the given IOTHUBMESSAGE_DISPOSITION_RESULT to the equivalent AMQP_VALUE and will return the result of calling messagereceiver_send_message_disposition. ] */ | 
| AzureIoTClient | 34:51d158b409d2 | 2402 | DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result = get_device_disposition_result_from(disposition); | 
| AzureIoTClient | 30:20a85b733111 | 2403 | |
| AzureIoTClient | 34:51d158b409d2 | 2404 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_112: [A DEVICE_MESSAGE_DISPOSITION_INFO instance shall be created with a copy of the `link_name` and `message_id` contained in `message_data`] | 
| AzureIoTClient | 34:51d158b409d2 | 2405 | if ((device_message_disposition_info = create_device_message_disposition_info_from(message_data)) == NULL) | 
| AzureIoTClient | 34:51d158b409d2 | 2406 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2407 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_113: [If the DEVICE_MESSAGE_DISPOSITION_INFO fails to be created, `IoTHubTransport_AMQP_Common_SendMessageDisposition()` shall fail and return IOTHUB_CLIENT_ERROR] | 
| AzureIoTClient | 34:51d158b409d2 | 2408 | LogError("Device '%s' failed sending message disposition (failed creating DEVICE_MESSAGE_DISPOSITION_RESULT)", STRING_c_str(message_data->transportContext->device_state->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 2409 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 2410 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2411 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2412 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2413 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_10_003: [IoTHubTransport_AMQP_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_ERROR if the POST message fails, otherwise return IOTHUB_CLIENT_OK.] */ | 
| AzureIoTClient | 34:51d158b409d2 | 2414 | if (device_send_message_disposition(message_data->transportContext->device_state->device_handle, device_message_disposition_info, device_disposition_result) != RESULT_OK) | 
| AzureIoTClient | 34:51d158b409d2 | 2415 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2416 | LogError("Device '%s' failed sending message disposition (device_send_message_disposition failed)", STRING_c_str(message_data->transportContext->device_state->device_id)); | 
| AzureIoTClient | 34:51d158b409d2 | 2417 | result = IOTHUB_CLIENT_ERROR; | 
| AzureIoTClient | 34:51d158b409d2 | 2418 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2419 | else | 
| AzureIoTClient | 34:51d158b409d2 | 2420 | { | 
| AzureIoTClient | 34:51d158b409d2 | 2421 | IoTHubMessage_Destroy(message_data->messageHandle); | 
| AzureIoTClient | 34:51d158b409d2 | 2422 | result = IOTHUB_CLIENT_OK; | 
| AzureIoTClient | 34:51d158b409d2 | 2423 | } | 
| AzureIoTClient | 30:20a85b733111 | 2424 | |
| AzureIoTClient | 34:51d158b409d2 | 2425 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_114: [`IoTHubTransport_AMQP_Common_SendMessageDisposition()` shall destroy the DEVICE_MESSAGE_DISPOSITION_INFO instance] | 
| AzureIoTClient | 34:51d158b409d2 | 2426 | destroy_device_message_disposition_info(device_message_disposition_info); | 
| AzureIoTClient | 34:51d158b409d2 | 2427 | } | 
| AzureIoTClient | 30:20a85b733111 | 2428 | } | 
| AzureIoTClient | 34:51d158b409d2 | 2429 | |
| AzureIoTClient | 34:51d158b409d2 | 2430 | MESSAGE_CALLBACK_INFO_Destroy(message_data); | 
| AzureIoTClient | 25:63f7d371c030 | 2431 | } | 
| AzureIoTClient | 30:20a85b733111 | 2432 | |
| AzureIoTClient | 25:63f7d371c030 | 2433 | return result; | 
| AzureIoTClient | 25:63f7d371c030 | 2434 | } | 
| AzureIoTClient | 25:63f7d371c030 | 2435 | 
