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