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

Committer:
AzureIoTClient
Date:
Fri Mar 10 11:46:55 2017 -0800
Revision:
30:20a85b733111
Parent:
29:7e8852b14e3b
Child:
31:adadaef857c1
1.1.9

Who changed what in which revision?

UserRevisionLine numberNew 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 25:63f7d371c030 6 #include <time.h>
AzureIoTClient 25:63f7d371c030 7 #include <limits.h>
AzureIoTClient 29:7e8852b14e3b 8 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 25:63f7d371c030 9 #include "azure_c_shared_utility/agenttime.h"
AzureIoTClient 25:63f7d371c030 10 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 25:63f7d371c030 11 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 30:20a85b733111 12 #include "azure_c_shared_utility/singlylinkedlist.h"
AzureIoTClient 25:63f7d371c030 13 #include "azure_c_shared_utility/doublylinkedlist.h"
AzureIoTClient 25:63f7d371c030 14 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 25:63f7d371c030 15 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 25:63f7d371c030 16 #include "azure_c_shared_utility/strings.h"
AzureIoTClient 25:63f7d371c030 17 #include "azure_c_shared_utility/urlencode.h"
AzureIoTClient 25:63f7d371c030 18 #include "azure_c_shared_utility/tlsio.h"
AzureIoTClient 30:20a85b733111 19 #include "azure_c_shared_utility/optionhandler.h"
AzureIoTClient 25:63f7d371c030 20
AzureIoTClient 25:63f7d371c030 21 #include "azure_uamqp_c/cbs.h"
AzureIoTClient 30:20a85b733111 22 #include "azure_uamqp_c/session.h"
AzureIoTClient 25:63f7d371c030 23 #include "azure_uamqp_c/message.h"
AzureIoTClient 25:63f7d371c030 24 #include "azure_uamqp_c/messaging.h"
AzureIoTClient 25:63f7d371c030 25
AzureIoTClient 25:63f7d371c030 26 #include "iothub_client_ll.h"
AzureIoTClient 25:63f7d371c030 27 #include "iothub_client_options.h"
AzureIoTClient 25:63f7d371c030 28 #include "iothub_client_private.h"
AzureIoTClient 25:63f7d371c030 29 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 25:63f7d371c030 30 #include "iothubtransportamqp_methods.h"
AzureIoTClient 25:63f7d371c030 31 #endif
AzureIoTClient 25:63f7d371c030 32 #include "iothubtransport_amqp_common.h"
AzureIoTClient 30:20a85b733111 33 #include "iothubtransport_amqp_connection.h"
AzureIoTClient 30:20a85b733111 34 #include "iothubtransport_amqp_device.h"
AzureIoTClient 25:63f7d371c030 35 #include "iothub_client_version.h"
AzureIoTClient 25:63f7d371c030 36
AzureIoTClient 30:20a85b733111 37 #define RESULT_OK 0
AzureIoTClient 30:20a85b733111 38 #define INDEFINITE_TIME ((time_t)(-1))
AzureIoTClient 30:20a85b733111 39 #define DEFAULT_CBS_REQUEST_TIMEOUT_SECS 30
AzureIoTClient 30:20a85b733111 40 #define DEFAULT_DEVICE_STATE_CHANGE_TIMEOUT_SECS 60
AzureIoTClient 30:20a85b733111 41 #define DEFAULT_EVENT_SEND_TIMEOUT_SECS 300
AzureIoTClient 30:20a85b733111 42 #define DEFAULT_SAS_TOKEN_LIFETIME_SECS 3600
AzureIoTClient 30:20a85b733111 43 #define DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS 1800
AzureIoTClient 30:20a85b733111 44 #define MAX_NUMBER_OF_DEVICE_FAILURES 5
AzureIoTClient 25:63f7d371c030 45
AzureIoTClient 25:63f7d371c030 46
AzureIoTClient 30:20a85b733111 47 // ---------- Data Definitions ---------- //
AzureIoTClient 25:63f7d371c030 48
AzureIoTClient 30:20a85b733111 49 typedef enum AMQP_TRANSPORT_AUTHENTICATION_MODE_TAG
AzureIoTClient 25:63f7d371c030 50 {
AzureIoTClient 30:20a85b733111 51 AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET,
AzureIoTClient 30:20a85b733111 52 AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS,
AzureIoTClient 30:20a85b733111 53 AMQP_TRANSPORT_AUTHENTICATION_MODE_X509
AzureIoTClient 30:20a85b733111 54 } AMQP_TRANSPORT_AUTHENTICATION_MODE;
AzureIoTClient 25:63f7d371c030 55
AzureIoTClient 30:20a85b733111 56 typedef struct AMQP_TRANSPORT_INSTANCE_TAG
AzureIoTClient 30:20a85b733111 57 {
AzureIoTClient 30:20a85b733111 58 STRING_HANDLE iothub_host_fqdn; // FQDN of the IoT Hub.
AzureIoTClient 30:20a85b733111 59 XIO_HANDLE tls_io; // TSL I/O transport.
AzureIoTClient 30:20a85b733111 60 AMQP_GET_IO_TRANSPORT underlying_io_transport_provider; // Pointer to the function that creates the TLS I/O (internal use only).
AzureIoTClient 30:20a85b733111 61 AMQP_CONNECTION_HANDLE amqp_connection; // Base amqp connection with service.
AzureIoTClient 30:20a85b733111 62 AMQP_CONNECTION_STATE amqp_connection_state; // Current state of the amqp_connection.
AzureIoTClient 30:20a85b733111 63 AMQP_TRANSPORT_AUTHENTICATION_MODE preferred_authentication_mode; // Used to avoid registered devices using different authentication modes.
AzureIoTClient 30:20a85b733111 64 SINGLYLINKEDLIST_HANDLE registered_devices; // List of devices currently registered in this transport.
AzureIoTClient 30:20a85b733111 65 bool is_trace_on; // Turns logging on and off.
AzureIoTClient 30:20a85b733111 66 OPTIONHANDLER_HANDLE saved_tls_options; // Here are the options from the xio layer if any is saved.
AzureIoTClient 30:20a85b733111 67 bool is_connection_retry_required; // Flag that controls whether the connection should be restablished or not.
AzureIoTClient 30:20a85b733111 68
AzureIoTClient 30:20a85b733111 69 size_t option_sas_token_lifetime_secs; // Device-specific option.
AzureIoTClient 30:20a85b733111 70 size_t option_sas_token_refresh_time_secs; // Device-specific option.
AzureIoTClient 30:20a85b733111 71 size_t option_cbs_request_timeout_secs; // Device-specific option.
AzureIoTClient 30:20a85b733111 72 size_t option_send_event_timeout_secs; // Device-specific option.
AzureIoTClient 25:63f7d371c030 73 } AMQP_TRANSPORT_INSTANCE;
AzureIoTClient 25:63f7d371c030 74
AzureIoTClient 30:20a85b733111 75 typedef struct AMQP_TRANSPORT_DEVICE_INSTANCE_TAG
AzureIoTClient 25:63f7d371c030 76 {
AzureIoTClient 30:20a85b733111 77 STRING_HANDLE device_id; // Identity of the device.
AzureIoTClient 30:20a85b733111 78 DEVICE_HANDLE device_handle; // Logic unit that performs authentication, messaging, etc.
AzureIoTClient 30:20a85b733111 79 IOTHUB_CLIENT_LL_HANDLE iothub_client_handle; // Saved reference to the IoTHub LL Client.
AzureIoTClient 30:20a85b733111 80 AMQP_TRANSPORT_INSTANCE* transport_instance; // Saved reference to the transport the device is registered on.
AzureIoTClient 30:20a85b733111 81 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 30:20a85b733111 82 DEVICE_STATE device_state; // Current state of the device_handle instance.
AzureIoTClient 30:20a85b733111 83 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 30:20a85b733111 84 size_t number_of_send_event_complete_failures; // Number of times on_event_send_complete was called in row with an error.
AzureIoTClient 30:20a85b733111 85 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 30:20a85b733111 86 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 87 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 25:63f7d371c030 88 // the methods portion
AzureIoTClient 30:20a85b733111 89 IOTHUBTRANSPORT_AMQP_METHODS_HANDLE methods_handle; // Handle to instance of module that deals with device methods for AMQP.
AzureIoTClient 25:63f7d371c030 90 // is subscription for methods needed?
AzureIoTClient 30:20a85b733111 91 bool subscribe_methods_needed; // Indicates if should subscribe for device methods.
AzureIoTClient 25:63f7d371c030 92 // is the transport subscribed for methods?
AzureIoTClient 30:20a85b733111 93 bool subscribed_for_methods; // Indicates if device is subscribed for device methods.
AzureIoTClient 25:63f7d371c030 94 #endif
AzureIoTClient 30:20a85b733111 95 } AMQP_TRANSPORT_DEVICE_INSTANCE;
AzureIoTClient 25:63f7d371c030 96
AzureIoTClient 30:20a85b733111 97 typedef struct MESSAGE_DISPOSITION_CONTEXT_TAG
AzureIoTClient 30:20a85b733111 98 {
AzureIoTClient 30:20a85b733111 99 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state;
AzureIoTClient 30:20a85b733111 100 char* link_name;
AzureIoTClient 30:20a85b733111 101 delivery_number message_id;
AzureIoTClient 30:20a85b733111 102 } MESSAGE_DISPOSITION_CONTEXT;
AzureIoTClient 25:63f7d371c030 103
AzureIoTClient 30:20a85b733111 104 // ---------- General Helpers ---------- //
AzureIoTClient 25:63f7d371c030 105
AzureIoTClient 30:20a85b733111 106 // @brief
AzureIoTClient 30:20a85b733111 107 // Evaluates if the ammount of time since start_time is greater or lesser than timeout_in_secs.
AzureIoTClient 30:20a85b733111 108 // @param is_timed_out
AzureIoTClient 30:20a85b733111 109 // Set to true if a timeout has been reached, false otherwise. Not set if any failure occurs.
AzureIoTClient 30:20a85b733111 110 // @returns
AzureIoTClient 30:20a85b733111 111 // 0 if no failures occur, non-zero otherwise.
AzureIoTClient 30:20a85b733111 112 static int is_timeout_reached(time_t start_time, unsigned int timeout_in_secs, bool *is_timed_out)
AzureIoTClient 25:63f7d371c030 113 {
AzureIoTClient 30:20a85b733111 114 int result;
AzureIoTClient 30:20a85b733111 115
AzureIoTClient 30:20a85b733111 116 if (start_time == INDEFINITE_TIME)
AzureIoTClient 30:20a85b733111 117 {
AzureIoTClient 30:20a85b733111 118 LogError("Failed to verify timeout (start_time is INDEFINITE)");
AzureIoTClient 30:20a85b733111 119 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 120 }
AzureIoTClient 30:20a85b733111 121 else
AzureIoTClient 30:20a85b733111 122 {
AzureIoTClient 30:20a85b733111 123 time_t current_time;
AzureIoTClient 25:63f7d371c030 124
AzureIoTClient 30:20a85b733111 125 if ((current_time = get_time(NULL)) == INDEFINITE_TIME)
AzureIoTClient 30:20a85b733111 126 {
AzureIoTClient 30:20a85b733111 127 LogError("Failed to verify timeout (get_time failed)");
AzureIoTClient 30:20a85b733111 128 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 129 }
AzureIoTClient 30:20a85b733111 130 else
AzureIoTClient 30:20a85b733111 131 {
AzureIoTClient 30:20a85b733111 132 if (get_difftime(current_time, start_time) >= timeout_in_secs)
AzureIoTClient 30:20a85b733111 133 {
AzureIoTClient 30:20a85b733111 134 *is_timed_out = 1;
AzureIoTClient 30:20a85b733111 135 }
AzureIoTClient 30:20a85b733111 136 else
AzureIoTClient 30:20a85b733111 137 {
AzureIoTClient 30:20a85b733111 138 *is_timed_out = 0;
AzureIoTClient 30:20a85b733111 139 }
AzureIoTClient 25:63f7d371c030 140
AzureIoTClient 30:20a85b733111 141 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 142 }
AzureIoTClient 30:20a85b733111 143 }
AzureIoTClient 30:20a85b733111 144
AzureIoTClient 30:20a85b733111 145 return result;
AzureIoTClient 25:63f7d371c030 146 }
AzureIoTClient 25:63f7d371c030 147
AzureIoTClient 30:20a85b733111 148 static STRING_HANDLE get_target_iothub_fqdn(const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 25:63f7d371c030 149 {
AzureIoTClient 30:20a85b733111 150 STRING_HANDLE fqdn;
AzureIoTClient 30:20a85b733111 151
AzureIoTClient 30:20a85b733111 152 if (config->upperConfig->protocolGatewayHostName == NULL)
AzureIoTClient 30:20a85b733111 153 {
AzureIoTClient 30:20a85b733111 154 if ((fqdn = STRING_construct_sprintf("%s.%s", config->upperConfig->iotHubName, config->upperConfig->iotHubSuffix)) == NULL)
AzureIoTClient 30:20a85b733111 155 {
AzureIoTClient 30:20a85b733111 156 LogError("Failed to copy iotHubName and iotHubSuffix (STRING_construct_sprintf failed)");
AzureIoTClient 30:20a85b733111 157 }
AzureIoTClient 30:20a85b733111 158 }
AzureIoTClient 30:20a85b733111 159 else if ((fqdn = STRING_construct(config->upperConfig->protocolGatewayHostName)) == NULL)
AzureIoTClient 30:20a85b733111 160 {
AzureIoTClient 30:20a85b733111 161 LogError("Failed to copy protocolGatewayHostName (STRING_construct failed)");
AzureIoTClient 30:20a85b733111 162 }
AzureIoTClient 30:20a85b733111 163
AzureIoTClient 30:20a85b733111 164 return fqdn;
AzureIoTClient 25:63f7d371c030 165 }
AzureIoTClient 25:63f7d371c030 166
AzureIoTClient 25:63f7d371c030 167
AzureIoTClient 30:20a85b733111 168 // ---------- Register/Unregister Helpers ---------- //
AzureIoTClient 25:63f7d371c030 169
AzureIoTClient 30:20a85b733111 170 static void internal_destroy_amqp_device_instance(AMQP_TRANSPORT_DEVICE_INSTANCE *trdev_inst)
AzureIoTClient 25:63f7d371c030 171 {
AzureIoTClient 30:20a85b733111 172 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 173 if (trdev_inst->methods_handle != NULL)
AzureIoTClient 30:20a85b733111 174 {
AzureIoTClient 30:20a85b733111 175 iothubtransportamqp_methods_destroy(trdev_inst->methods_handle);
AzureIoTClient 30:20a85b733111 176 }
AzureIoTClient 30:20a85b733111 177 #endif
AzureIoTClient 30:20a85b733111 178 if (trdev_inst->device_handle != NULL)
AzureIoTClient 30:20a85b733111 179 {
AzureIoTClient 30:20a85b733111 180 device_destroy(trdev_inst->device_handle);
AzureIoTClient 30:20a85b733111 181 }
AzureIoTClient 25:63f7d371c030 182
AzureIoTClient 30:20a85b733111 183 if (trdev_inst->device_id != NULL)
AzureIoTClient 30:20a85b733111 184 {
AzureIoTClient 30:20a85b733111 185 STRING_delete(trdev_inst->device_id);
AzureIoTClient 30:20a85b733111 186 }
AzureIoTClient 25:63f7d371c030 187
AzureIoTClient 30:20a85b733111 188 free(trdev_inst);
AzureIoTClient 25:63f7d371c030 189 }
AzureIoTClient 25:63f7d371c030 190
AzureIoTClient 30:20a85b733111 191 // @brief
AzureIoTClient 30:20a85b733111 192 // Saves the new state, if it is different than the previous one.
AzureIoTClient 30:20a85b733111 193 static void on_device_state_changed_callback(void* context, DEVICE_STATE previous_state, DEVICE_STATE new_state)
AzureIoTClient 25:63f7d371c030 194 {
AzureIoTClient 30:20a85b733111 195 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_061: [If `new_state` is the same as `previous_state`, on_device_state_changed_callback shall return]
AzureIoTClient 30:20a85b733111 196 if (context != NULL && new_state != previous_state)
AzureIoTClient 30:20a85b733111 197 {
AzureIoTClient 30:20a85b733111 198 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context;
AzureIoTClient 30:20a85b733111 199 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_062: [If `new_state` shall be saved into the `registered_device` instance]
AzureIoTClient 30:20a85b733111 200 registered_device->device_state = new_state;
AzureIoTClient 30:20a85b733111 201 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_063: [If `registered_device->time_of_last_state_change` shall be set using get_time()]
AzureIoTClient 30:20a85b733111 202 registered_device->time_of_last_state_change = get_time(NULL);
AzureIoTClient 30:20a85b733111 203 }
AzureIoTClient 25:63f7d371c030 204 }
AzureIoTClient 25:63f7d371c030 205
AzureIoTClient 30:20a85b733111 206 // @brief Auxiliary function to be used to find a device in the registered_devices list.
AzureIoTClient 30:20a85b733111 207 // @returns true if the device ids match, false otherwise.
AzureIoTClient 30:20a85b733111 208 static bool find_device_by_id_callback(LIST_ITEM_HANDLE list_item, const void* match_context)
AzureIoTClient 25:63f7d371c030 209 {
AzureIoTClient 30:20a85b733111 210 bool result;
AzureIoTClient 30:20a85b733111 211
AzureIoTClient 30:20a85b733111 212 if (match_context == NULL)
AzureIoTClient 30:20a85b733111 213 {
AzureIoTClient 30:20a85b733111 214 result = false;
AzureIoTClient 30:20a85b733111 215 }
AzureIoTClient 30:20a85b733111 216 else
AzureIoTClient 30:20a85b733111 217 {
AzureIoTClient 30:20a85b733111 218 AMQP_TRANSPORT_DEVICE_INSTANCE* device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item);
AzureIoTClient 25:63f7d371c030 219
AzureIoTClient 30:20a85b733111 220 if (device_instance == NULL ||
AzureIoTClient 30:20a85b733111 221 device_instance->device_id == NULL ||
AzureIoTClient 30:20a85b733111 222 STRING_c_str(device_instance->device_id) != match_context)
AzureIoTClient 30:20a85b733111 223 {
AzureIoTClient 30:20a85b733111 224 result = false;
AzureIoTClient 30:20a85b733111 225 }
AzureIoTClient 30:20a85b733111 226 else
AzureIoTClient 30:20a85b733111 227 {
AzureIoTClient 30:20a85b733111 228 result = true;
AzureIoTClient 30:20a85b733111 229 }
AzureIoTClient 30:20a85b733111 230 }
AzureIoTClient 30:20a85b733111 231
AzureIoTClient 30:20a85b733111 232 return result;
AzureIoTClient 25:63f7d371c030 233 }
AzureIoTClient 25:63f7d371c030 234
AzureIoTClient 30:20a85b733111 235 // @brief Verifies if a device is already registered within the transport that owns the list of registered devices.
AzureIoTClient 30:20a85b733111 236 // @remarks Returns the correspoding LIST_ITEM_HANDLE in registered_devices, if found.
AzureIoTClient 30:20a85b733111 237 // @returns true if the device is already in the list, false otherwise.
AzureIoTClient 30:20a85b733111 238 static bool is_device_registered_ex(SINGLYLINKEDLIST_HANDLE registered_devices, const char* device_id, LIST_ITEM_HANDLE *list_item)
AzureIoTClient 25:63f7d371c030 239 {
AzureIoTClient 30:20a85b733111 240 return ((*list_item = singlylinkedlist_find(registered_devices, find_device_by_id_callback, device_id)) != NULL ? 1 : 0);
AzureIoTClient 30:20a85b733111 241 }
AzureIoTClient 25:63f7d371c030 242
AzureIoTClient 30:20a85b733111 243 // @brief Verifies if a device is already registered within the transport that owns the list of registered devices.
AzureIoTClient 30:20a85b733111 244 // @returns true if the device is already in the list, false otherwise.
AzureIoTClient 30:20a85b733111 245 static bool is_device_registered(AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance)
AzureIoTClient 30:20a85b733111 246 {
AzureIoTClient 30:20a85b733111 247 LIST_ITEM_HANDLE list_item;
AzureIoTClient 30:20a85b733111 248 const char* device_id = STRING_c_str(amqp_device_instance->device_id);
AzureIoTClient 30:20a85b733111 249 return is_device_registered_ex(amqp_device_instance->transport_instance->registered_devices, device_id, &list_item);
AzureIoTClient 25:63f7d371c030 250 }
AzureIoTClient 25:63f7d371c030 251
AzureIoTClient 30:20a85b733111 252
AzureIoTClient 30:20a85b733111 253 // ---------- Callbacks ---------- //
AzureIoTClient 30:20a85b733111 254
AzureIoTClient 30:20a85b733111 255 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 256 {
AzureIoTClient 30:20a85b733111 257 MESSAGE_CALLBACK_INFO* result;
AzureIoTClient 25:63f7d371c030 258
AzureIoTClient 30:20a85b733111 259 if ((result = (MESSAGE_CALLBACK_INFO*)malloc(sizeof(MESSAGE_CALLBACK_INFO))) == NULL)
AzureIoTClient 30:20a85b733111 260 {
AzureIoTClient 30:20a85b733111 261 LogError("Failed creating MESSAGE_CALLBACK_INFO (malloc failed)");
AzureIoTClient 30:20a85b733111 262 }
AzureIoTClient 30:20a85b733111 263 else
AzureIoTClient 30:20a85b733111 264 {
AzureIoTClient 30:20a85b733111 265 MESSAGE_DISPOSITION_CONTEXT* tc;
AzureIoTClient 25:63f7d371c030 266
AzureIoTClient 30:20a85b733111 267 if ((tc = (MESSAGE_DISPOSITION_CONTEXT*)malloc(sizeof(MESSAGE_DISPOSITION_CONTEXT))) == NULL)
AzureIoTClient 30:20a85b733111 268 {
AzureIoTClient 30:20a85b733111 269 LogError("Failed creating MESSAGE_DISPOSITION_CONTEXT (malloc failed)");
AzureIoTClient 30:20a85b733111 270 free(result);
AzureIoTClient 30:20a85b733111 271 result = NULL;
AzureIoTClient 30:20a85b733111 272 }
AzureIoTClient 30:20a85b733111 273 else
AzureIoTClient 30:20a85b733111 274 {
AzureIoTClient 30:20a85b733111 275 if (mallocAndStrcpy_s(&(tc->link_name), disposition_info->source) == 0)
AzureIoTClient 30:20a85b733111 276 {
AzureIoTClient 30:20a85b733111 277 tc->device_state = device_state;
AzureIoTClient 30:20a85b733111 278 tc->message_id = disposition_info->message_id;
AzureIoTClient 25:63f7d371c030 279
AzureIoTClient 30:20a85b733111 280 result->messageHandle = message;
AzureIoTClient 30:20a85b733111 281 result->transportContext = tc;
AzureIoTClient 30:20a85b733111 282 }
AzureIoTClient 30:20a85b733111 283 else
AzureIoTClient 30:20a85b733111 284 {
AzureIoTClient 30:20a85b733111 285 LogError("Failed creating MESSAGE_CALLBACK_INFO (mallocAndStrcyp_s failed)");
AzureIoTClient 30:20a85b733111 286 free(tc);
AzureIoTClient 30:20a85b733111 287 free(result);
AzureIoTClient 30:20a85b733111 288 result = NULL;
AzureIoTClient 30:20a85b733111 289 }
AzureIoTClient 30:20a85b733111 290 }
AzureIoTClient 30:20a85b733111 291 }
AzureIoTClient 25:63f7d371c030 292
AzureIoTClient 30:20a85b733111 293 return result;
AzureIoTClient 30:20a85b733111 294 }
AzureIoTClient 30:20a85b733111 295
AzureIoTClient 30:20a85b733111 296 static void MESSAGE_CALLBACK_INFO_Destroy(MESSAGE_CALLBACK_INFO* message_callback_info)
AzureIoTClient 30:20a85b733111 297 {
AzureIoTClient 30:20a85b733111 298 free(message_callback_info->transportContext->link_name);
AzureIoTClient 30:20a85b733111 299 free(message_callback_info->transportContext);
AzureIoTClient 30:20a85b733111 300 free(message_callback_info);
AzureIoTClient 25:63f7d371c030 301 }
AzureIoTClient 25:63f7d371c030 302
AzureIoTClient 30:20a85b733111 303 static DEVICE_MESSAGE_DISPOSITION_RESULT get_device_disposition_result_from(IOTHUBMESSAGE_DISPOSITION_RESULT iothubclient_disposition_result)
AzureIoTClient 25:63f7d371c030 304 {
AzureIoTClient 30:20a85b733111 305 DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result;
AzureIoTClient 25:63f7d371c030 306
AzureIoTClient 30:20a85b733111 307 if (iothubclient_disposition_result == IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 30:20a85b733111 308 {
AzureIoTClient 30:20a85b733111 309 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_ACCEPTED;
AzureIoTClient 30:20a85b733111 310 }
AzureIoTClient 30:20a85b733111 311 else if (iothubclient_disposition_result == IOTHUBMESSAGE_ABANDONED)
AzureIoTClient 30:20a85b733111 312 {
AzureIoTClient 30:20a85b733111 313 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED;
AzureIoTClient 30:20a85b733111 314 }
AzureIoTClient 30:20a85b733111 315 else if (iothubclient_disposition_result == IOTHUBMESSAGE_REJECTED)
AzureIoTClient 30:20a85b733111 316 {
AzureIoTClient 30:20a85b733111 317 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_REJECTED;
AzureIoTClient 30:20a85b733111 318 }
AzureIoTClient 30:20a85b733111 319 else
AzureIoTClient 30:20a85b733111 320 {
AzureIoTClient 30:20a85b733111 321 LogError("Failed getting corresponding DEVICE_MESSAGE_DISPOSITION_RESULT for IOTHUBMESSAGE_DISPOSITION_RESULT (%d is not supported)", iothubclient_disposition_result);
AzureIoTClient 30:20a85b733111 322 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED;
AzureIoTClient 30:20a85b733111 323 }
AzureIoTClient 25:63f7d371c030 324
AzureIoTClient 30:20a85b733111 325 return device_disposition_result;
AzureIoTClient 25:63f7d371c030 326 }
AzureIoTClient 25:63f7d371c030 327
AzureIoTClient 30:20a85b733111 328 static DEVICE_MESSAGE_DISPOSITION_RESULT on_message_received(IOTHUB_MESSAGE_HANDLE message, DEVICE_MESSAGE_DISPOSITION_INFO* disposition_info, void* context)
AzureIoTClient 25:63f7d371c030 329 {
AzureIoTClient 30:20a85b733111 330 AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context;
AzureIoTClient 30:20a85b733111 331 DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result;
AzureIoTClient 30:20a85b733111 332 MESSAGE_CALLBACK_INFO* message_data;
AzureIoTClient 25:63f7d371c030 333
AzureIoTClient 30:20a85b733111 334 if ((message_data = MESSAGE_CALLBACK_INFO_Create(message, disposition_info, amqp_device_instance)) == NULL)
AzureIoTClient 30:20a85b733111 335 {
AzureIoTClient 30:20a85b733111 336 LogError("Failed processing message received (failed to assemble callback info)");
AzureIoTClient 30:20a85b733111 337 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED;
AzureIoTClient 30:20a85b733111 338 }
AzureIoTClient 30:20a85b733111 339 else
AzureIoTClient 30:20a85b733111 340 {
AzureIoTClient 30:20a85b733111 341 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_089: [IoTHubClient_LL_MessageCallback() shall be invoked passing the client and the incoming message handles as parameters]
AzureIoTClient 30:20a85b733111 342 if (IoTHubClient_LL_MessageCallback(amqp_device_instance->iothub_client_handle, message_data) != true)
AzureIoTClient 30:20a85b733111 343 {
AzureIoTClient 30:20a85b733111 344 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_090: [If IoTHubClient_LL_MessageCallback() fails, on_message_received_callback shall return DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED]
AzureIoTClient 30:20a85b733111 345 LogError("Failed processing message received (IoTHubClient_LL_MessageCallback failed)");
AzureIoTClient 30:20a85b733111 346 IoTHubMessage_Destroy(message);
AzureIoTClient 30:20a85b733111 347 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_RELEASED;
AzureIoTClient 30:20a85b733111 348 }
AzureIoTClient 30:20a85b733111 349 else
AzureIoTClient 30:20a85b733111 350 {
AzureIoTClient 30:20a85b733111 351 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_091: [If IoTHubClient_LL_MessageCallback() succeeds, on_message_received_callback shall return DEVICE_MESSAGE_DISPOSITION_RESULT_NONE]
AzureIoTClient 30:20a85b733111 352 device_disposition_result = DEVICE_MESSAGE_DISPOSITION_RESULT_NONE;
AzureIoTClient 30:20a85b733111 353 }
AzureIoTClient 30:20a85b733111 354 }
AzureIoTClient 30:20a85b733111 355
AzureIoTClient 30:20a85b733111 356 return device_disposition_result;
AzureIoTClient 25:63f7d371c030 357 }
AzureIoTClient 25:63f7d371c030 358
AzureIoTClient 25:63f7d371c030 359
AzureIoTClient 25:63f7d371c030 360 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 25:63f7d371c030 361 static void on_methods_error(void* context)
AzureIoTClient 25:63f7d371c030 362 {
AzureIoTClient 25:63f7d371c030 363 /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_030: [ `on_methods_error` shall do nothing. ]*/
AzureIoTClient 25:63f7d371c030 364 (void)context;
AzureIoTClient 25:63f7d371c030 365 }
AzureIoTClient 25:63f7d371c030 366
AzureIoTClient 29:7e8852b14e3b 367 static void on_methods_unsubscribed(void* context)
AzureIoTClient 29:7e8852b14e3b 368 {
AzureIoTClient 29:7e8852b14e3b 369 /* Codess_SRS_IOTHUBTRANSPORT_AMQP_METHODS_12_001: [ `on_methods_unsubscribed` calls iothubtransportamqp_methods_unsubscribe. ]*/
AzureIoTClient 30:20a85b733111 370 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context;
AzureIoTClient 29:7e8852b14e3b 371 IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod(device_state);
AzureIoTClient 29:7e8852b14e3b 372 }
AzureIoTClient 29:7e8852b14e3b 373
AzureIoTClient 25:63f7d371c030 374 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 375 {
AzureIoTClient 25:63f7d371c030 376 int result;
AzureIoTClient 30:20a85b733111 377 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context;
AzureIoTClient 25:63f7d371c030 378
AzureIoTClient 26:ee14eed604f6 379 /* 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 380 /* 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 381 if (IoTHubClient_LL_DeviceMethodComplete(device_state->iothub_client_handle, method_name, request, request_size, (void*)method_handle) != 0)
AzureIoTClient 25:63f7d371c030 382 {
AzureIoTClient 26:ee14eed604f6 383 LogError("Failure: IoTHubClient_LL_DeviceMethodComplete");
AzureIoTClient 29:7e8852b14e3b 384 result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 385 }
AzureIoTClient 25:63f7d371c030 386 else
AzureIoTClient 25:63f7d371c030 387 {
AzureIoTClient 26:ee14eed604f6 388 result = 0;
AzureIoTClient 25:63f7d371c030 389 }
AzureIoTClient 25:63f7d371c030 390 return result;
AzureIoTClient 25:63f7d371c030 391 }
AzureIoTClient 25:63f7d371c030 392
AzureIoTClient 30:20a85b733111 393 static int subscribe_methods(AMQP_TRANSPORT_DEVICE_INSTANCE* deviceState)
AzureIoTClient 25:63f7d371c030 394 {
AzureIoTClient 25:63f7d371c030 395 int result;
AzureIoTClient 25:63f7d371c030 396
AzureIoTClient 29:7e8852b14e3b 397 if (deviceState->subscribed_for_methods)
AzureIoTClient 25:63f7d371c030 398 {
AzureIoTClient 25:63f7d371c030 399 result = 0;
AzureIoTClient 25:63f7d371c030 400 }
AzureIoTClient 25:63f7d371c030 401 else
AzureIoTClient 25:63f7d371c030 402 {
AzureIoTClient 30:20a85b733111 403 SESSION_HANDLE session_handle;
AzureIoTClient 30:20a85b733111 404
AzureIoTClient 30:20a85b733111 405 if ((amqp_connection_get_session_handle(deviceState->transport_instance->amqp_connection, &session_handle)) != RESULT_OK)
AzureIoTClient 30:20a85b733111 406 {
AzureIoTClient 30:20a85b733111 407 LogError("Device '%s' failed subscribing for methods (failed getting session handle)", STRING_c_str(deviceState->device_id));
AzureIoTClient 30:20a85b733111 408 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 409 }
AzureIoTClient 25:63f7d371c030 410 /* 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 411 /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_027: [ The current session handle shall be passed to `iothubtransportamqp_methods_subscribe`. ]*/
AzureIoTClient 30:20a85b733111 412 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 413 {
AzureIoTClient 25:63f7d371c030 414 LogError("Cannot subscribe for methods");
AzureIoTClient 29:7e8852b14e3b 415 result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 416 }
AzureIoTClient 25:63f7d371c030 417 else
AzureIoTClient 25:63f7d371c030 418 {
AzureIoTClient 29:7e8852b14e3b 419 deviceState->subscribed_for_methods = true;
AzureIoTClient 25:63f7d371c030 420 result = 0;
AzureIoTClient 25:63f7d371c030 421 }
AzureIoTClient 25:63f7d371c030 422 }
AzureIoTClient 25:63f7d371c030 423
AzureIoTClient 25:63f7d371c030 424 return result;
AzureIoTClient 25:63f7d371c030 425 }
AzureIoTClient 25:63f7d371c030 426 #endif
AzureIoTClient 25:63f7d371c030 427
AzureIoTClient 30:20a85b733111 428
AzureIoTClient 30:20a85b733111 429 // ---------- Underlying TLS I/O Helpers ---------- //
AzureIoTClient 30:20a85b733111 430
AzureIoTClient 30:20a85b733111 431 // @brief
AzureIoTClient 30:20a85b733111 432 // Retrieves the options of the current underlying TLS I/O instance and saves in the transport instance.
AzureIoTClient 30:20a85b733111 433 // @remarks
AzureIoTClient 30:20a85b733111 434 // This is used when the new underlying I/O transport (TLS I/O, or WebSockets, etc) needs to be recreated,
AzureIoTClient 30:20a85b733111 435 // and the options previously set must persist.
AzureIoTClient 30:20a85b733111 436 //
AzureIoTClient 30:20a85b733111 437 // If no TLS I/O instance was created yet, results in failure.
AzureIoTClient 30:20a85b733111 438 // @returns
AzureIoTClient 30:20a85b733111 439 // 0 if succeeds, non-zero otherwise.
AzureIoTClient 30:20a85b733111 440 static int save_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance)
AzureIoTClient 25:63f7d371c030 441 {
AzureIoTClient 30:20a85b733111 442 int result;
AzureIoTClient 30:20a85b733111 443
AzureIoTClient 30:20a85b733111 444 if (transport_instance->tls_io == NULL)
AzureIoTClient 30:20a85b733111 445 {
AzureIoTClient 30:20a85b733111 446 LogError("failed saving underlying I/O transport options (tls_io instance is NULL)");
AzureIoTClient 30:20a85b733111 447 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 448 }
AzureIoTClient 30:20a85b733111 449 else
AzureIoTClient 30:20a85b733111 450 {
AzureIoTClient 30:20a85b733111 451 OPTIONHANDLER_HANDLE fresh_options;
AzureIoTClient 25:63f7d371c030 452
AzureIoTClient 30:20a85b733111 453 if ((fresh_options = xio_retrieveoptions(transport_instance->tls_io)) == NULL)
AzureIoTClient 30:20a85b733111 454 {
AzureIoTClient 30:20a85b733111 455 LogError("failed saving underlying I/O transport options (tls_io instance is NULL)");
AzureIoTClient 30:20a85b733111 456 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 457 }
AzureIoTClient 30:20a85b733111 458 else
AzureIoTClient 30:20a85b733111 459 {
AzureIoTClient 30:20a85b733111 460 OPTIONHANDLER_HANDLE previous_options = transport_instance->saved_tls_options;
AzureIoTClient 30:20a85b733111 461 transport_instance->saved_tls_options = fresh_options;
AzureIoTClient 30:20a85b733111 462
AzureIoTClient 30:20a85b733111 463 if (previous_options != NULL)
AzureIoTClient 30:20a85b733111 464 {
AzureIoTClient 30:20a85b733111 465 OptionHandler_Destroy(previous_options);
AzureIoTClient 30:20a85b733111 466 }
AzureIoTClient 30:20a85b733111 467
AzureIoTClient 30:20a85b733111 468 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 469 }
AzureIoTClient 30:20a85b733111 470 }
AzureIoTClient 30:20a85b733111 471
AzureIoTClient 30:20a85b733111 472 return result;
AzureIoTClient 30:20a85b733111 473 }
AzureIoTClient 30:20a85b733111 474
AzureIoTClient 30:20a85b733111 475 static void destroy_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance)
AzureIoTClient 30:20a85b733111 476 {
AzureIoTClient 30:20a85b733111 477 if (transport_instance->saved_tls_options != NULL)
AzureIoTClient 30:20a85b733111 478 {
AzureIoTClient 30:20a85b733111 479 OptionHandler_Destroy(transport_instance->saved_tls_options);
AzureIoTClient 30:20a85b733111 480 transport_instance->saved_tls_options = NULL;
AzureIoTClient 30:20a85b733111 481 }
AzureIoTClient 25:63f7d371c030 482 }
AzureIoTClient 25:63f7d371c030 483
AzureIoTClient 30:20a85b733111 484 // @brief
AzureIoTClient 30:20a85b733111 485 // Applies TLS I/O options if previously saved to a new TLS I/O instance.
AzureIoTClient 30:20a85b733111 486 // @returns
AzureIoTClient 30:20a85b733111 487 // 0 if succeeds, non-zero otherwise.
AzureIoTClient 30:20a85b733111 488 static int restore_underlying_io_transport_options(AMQP_TRANSPORT_INSTANCE* transport_instance, XIO_HANDLE xio_handle)
AzureIoTClient 30:20a85b733111 489 {
AzureIoTClient 30:20a85b733111 490 int result;
AzureIoTClient 30:20a85b733111 491
AzureIoTClient 30:20a85b733111 492 if (transport_instance->saved_tls_options == NULL)
AzureIoTClient 30:20a85b733111 493 {
AzureIoTClient 30:20a85b733111 494 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 495 }
AzureIoTClient 30:20a85b733111 496 else
AzureIoTClient 30:20a85b733111 497 {
AzureIoTClient 30:20a85b733111 498 if (OptionHandler_FeedOptions(transport_instance->saved_tls_options, xio_handle) != OPTIONHANDLER_OK)
AzureIoTClient 30:20a85b733111 499 {
AzureIoTClient 30:20a85b733111 500 LogError("Failed feeding existing options to new TLS instance.");
AzureIoTClient 30:20a85b733111 501 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 502 }
AzureIoTClient 30:20a85b733111 503 else
AzureIoTClient 30:20a85b733111 504 {
AzureIoTClient 30:20a85b733111 505 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 506 }
AzureIoTClient 30:20a85b733111 507 }
AzureIoTClient 30:20a85b733111 508
AzureIoTClient 30:20a85b733111 509 return result;
AzureIoTClient 30:20a85b733111 510 }
AzureIoTClient 30:20a85b733111 511
AzureIoTClient 30:20a85b733111 512 // @brief Destroys the XIO_HANDLE obtained with underlying_io_transport_provider(), saving its options beforehand.
AzureIoTClient 30:20a85b733111 513 static void destroy_underlying_io_transport(AMQP_TRANSPORT_INSTANCE* transport_instance)
AzureIoTClient 30:20a85b733111 514 {
AzureIoTClient 30:20a85b733111 515 if (transport_instance->tls_io != NULL)
AzureIoTClient 30:20a85b733111 516 {
AzureIoTClient 30:20a85b733111 517 xio_destroy(transport_instance->tls_io);
AzureIoTClient 30:20a85b733111 518 transport_instance->tls_io = NULL;
AzureIoTClient 30:20a85b733111 519 }
AzureIoTClient 30:20a85b733111 520 }
AzureIoTClient 30:20a85b733111 521
AzureIoTClient 30:20a85b733111 522 // @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 523 // @param xio_handle: if successfull, set with the new XIO_HANDLE acquired; not changed otherwise.
AzureIoTClient 30:20a85b733111 524 // @returns 0 if successfull, non-zero otherwise.
AzureIoTClient 30:20a85b733111 525 static int get_new_underlying_io_transport(AMQP_TRANSPORT_INSTANCE* transport_instance, XIO_HANDLE *xio_handle)
AzureIoTClient 30:20a85b733111 526 {
AzureIoTClient 30:20a85b733111 527 int result;
AzureIoTClient 30:20a85b733111 528
AzureIoTClient 30:20a85b733111 529 if ((*xio_handle = transport_instance->underlying_io_transport_provider(STRING_c_str(transport_instance->iothub_host_fqdn))) == NULL)
AzureIoTClient 30:20a85b733111 530 {
AzureIoTClient 30:20a85b733111 531 LogError("Failed to obtain a TLS I/O transport layer (underlying_io_transport_provider() failed)");
AzureIoTClient 30:20a85b733111 532 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 533 }
AzureIoTClient 30:20a85b733111 534 else
AzureIoTClient 30:20a85b733111 535 {
AzureIoTClient 30:20a85b733111 536 if (restore_underlying_io_transport_options(transport_instance, *xio_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 537 {
AzureIoTClient 30:20a85b733111 538 /*pessimistically hope TLS will fail, be recreated and options re-given*/
AzureIoTClient 30:20a85b733111 539 LogError("Failed to apply options previous saved to new underlying I/O transport instance.");
AzureIoTClient 30:20a85b733111 540 }
AzureIoTClient 30:20a85b733111 541
AzureIoTClient 30:20a85b733111 542 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 543 }
AzureIoTClient 30:20a85b733111 544
AzureIoTClient 30:20a85b733111 545 return result;
AzureIoTClient 30:20a85b733111 546 }
AzureIoTClient 30:20a85b733111 547
AzureIoTClient 30:20a85b733111 548
AzureIoTClient 30:20a85b733111 549 // ---------- AMQP connection establishment/tear-down, connectry retry ---------- //
AzureIoTClient 30:20a85b733111 550
AzureIoTClient 30:20a85b733111 551 static void on_amqp_connection_state_changed(const void* context, AMQP_CONNECTION_STATE previous_state, AMQP_CONNECTION_STATE new_state)
AzureIoTClient 30:20a85b733111 552 {
AzureIoTClient 30:20a85b733111 553 if (context != NULL && new_state != previous_state)
AzureIoTClient 30:20a85b733111 554 {
AzureIoTClient 30:20a85b733111 555 AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)context;
AzureIoTClient 30:20a85b733111 556
AzureIoTClient 30:20a85b733111 557 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_059: [`new_state` shall be saved in to the transport instance]
AzureIoTClient 30:20a85b733111 558 transport_instance->amqp_connection_state = new_state;
AzureIoTClient 30:20a85b733111 559
AzureIoTClient 30:20a85b733111 560 // 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 30:20a85b733111 561 if (new_state == AMQP_CONNECTION_STATE_ERROR)
AzureIoTClient 30:20a85b733111 562 {
AzureIoTClient 30:20a85b733111 563 LogError("Transport received an ERROR from the amqp_connection (state changed %d->%d); it will be flagged for connection retry.", previous_state, new_state);
AzureIoTClient 30:20a85b733111 564
AzureIoTClient 30:20a85b733111 565 transport_instance->is_connection_retry_required = true;
AzureIoTClient 30:20a85b733111 566 }
AzureIoTClient 30:20a85b733111 567 }
AzureIoTClient 30:20a85b733111 568 }
AzureIoTClient 30:20a85b733111 569
AzureIoTClient 30:20a85b733111 570 static int establish_amqp_connection(AMQP_TRANSPORT_INSTANCE* transport_instance)
AzureIoTClient 25:63f7d371c030 571 {
AzureIoTClient 25:63f7d371c030 572 int result;
AzureIoTClient 25:63f7d371c030 573
AzureIoTClient 30:20a85b733111 574 if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET)
AzureIoTClient 30:20a85b733111 575 {
AzureIoTClient 30:20a85b733111 576 LogError("Failed establishing connection (transport doesn't have a preferred authentication mode set; unexpected!).");
AzureIoTClient 30:20a85b733111 577 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 578 }
AzureIoTClient 30:20a85b733111 579 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_023: [If `instance->tls_io` is NULL, it shall be set invoking instance->underlying_io_transport_provider()]
AzureIoTClient 30:20a85b733111 580 // 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 30:20a85b733111 581 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_111: [If OptionHandler_FeedOptions() fails, it shall be ignored]
AzureIoTClient 30:20a85b733111 582 else if (transport_instance->tls_io == NULL &&
AzureIoTClient 30:20a85b733111 583 get_new_underlying_io_transport(transport_instance, &transport_instance->tls_io) != RESULT_OK)
AzureIoTClient 30:20a85b733111 584 {
AzureIoTClient 30:20a85b733111 585 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_024: [If instance->underlying_io_transport_provider() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return]
AzureIoTClient 30:20a85b733111 586 LogError("Failed establishing connection (failed to obtain a TLS I/O transport layer).");
AzureIoTClient 30:20a85b733111 587 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 588 }
AzureIoTClient 30:20a85b733111 589 else
AzureIoTClient 30:20a85b733111 590 {
AzureIoTClient 30:20a85b733111 591 AMQP_CONNECTION_CONFIG amqp_connection_config;
AzureIoTClient 30:20a85b733111 592 amqp_connection_config.iothub_host_fqdn = STRING_c_str(transport_instance->iothub_host_fqdn);
AzureIoTClient 30:20a85b733111 593 amqp_connection_config.underlying_io_transport = transport_instance->tls_io;
AzureIoTClient 30:20a85b733111 594 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_029: [`instance->is_trace_on` shall be set into `AMQP_CONNECTION_CONFIG->is_trace_on`]
AzureIoTClient 30:20a85b733111 595 amqp_connection_config.is_trace_on = transport_instance->is_trace_on;
AzureIoTClient 30:20a85b733111 596 amqp_connection_config.on_state_changed_callback = on_amqp_connection_state_changed;
AzureIoTClient 30:20a85b733111 597 amqp_connection_config.on_state_changed_context = transport_instance;
AzureIoTClient 25:63f7d371c030 598
AzureIoTClient 30:20a85b733111 599 // 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 30:20a85b733111 600 if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS)
AzureIoTClient 30:20a85b733111 601 {
AzureIoTClient 30:20a85b733111 602 amqp_connection_config.create_sasl_io = true;
AzureIoTClient 30:20a85b733111 603 amqp_connection_config.create_cbs_connection = true;
AzureIoTClient 30:20a85b733111 604 }
AzureIoTClient 30:20a85b733111 605 // 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 30:20a85b733111 606 else if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_X509)
AzureIoTClient 30:20a85b733111 607 {
AzureIoTClient 30:20a85b733111 608 amqp_connection_config.create_sasl_io = false;
AzureIoTClient 30:20a85b733111 609 amqp_connection_config.create_cbs_connection = false;
AzureIoTClient 30:20a85b733111 610 }
AzureIoTClient 30:20a85b733111 611 // If new AMQP_TRANSPORT_AUTHENTICATION_MODE values are added, they need to be covered here.
AzureIoTClient 25:63f7d371c030 612
AzureIoTClient 30:20a85b733111 613 transport_instance->amqp_connection_state = AMQP_CONNECTION_STATE_CLOSED;
AzureIoTClient 30:20a85b733111 614
AzureIoTClient 30:20a85b733111 615 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_026: [If `transport->connection` is NULL, it shall be created using amqp_connection_create()]
AzureIoTClient 30:20a85b733111 616 if ((transport_instance->amqp_connection = amqp_connection_create(&amqp_connection_config)) == NULL)
AzureIoTClient 30:20a85b733111 617 {
AzureIoTClient 30:20a85b733111 618 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_030: [If amqp_connection_create() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return]
AzureIoTClient 30:20a85b733111 619 LogError("Failed establishing connection (failed to create the amqp_connection instance).");
AzureIoTClient 30:20a85b733111 620 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 621 }
AzureIoTClient 30:20a85b733111 622 else
AzureIoTClient 30:20a85b733111 623 {
AzureIoTClient 30:20a85b733111 624 // 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 30:20a85b733111 625 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 626 }
AzureIoTClient 30:20a85b733111 627 }
AzureIoTClient 25:63f7d371c030 628
AzureIoTClient 25:63f7d371c030 629 return result;
AzureIoTClient 25:63f7d371c030 630 }
AzureIoTClient 25:63f7d371c030 631
AzureIoTClient 30:20a85b733111 632 static void prepare_device_for_connection_retry(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device)
AzureIoTClient 25:63f7d371c030 633 {
AzureIoTClient 30:20a85b733111 634 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 635 // 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 636 iothubtransportamqp_methods_unsubscribe(registered_device->methods_handle);
AzureIoTClient 30:20a85b733111 637 registered_device->subscribed_for_methods = 0;
AzureIoTClient 30:20a85b733111 638 #endif
AzureIoTClient 25:63f7d371c030 639
AzureIoTClient 30:20a85b733111 640 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_031: [device_stop() shall be invoked on all `instance->registered_devices` that are not already stopped]
AzureIoTClient 30:20a85b733111 641 if (registered_device->device_state != DEVICE_STATE_STOPPED)
AzureIoTClient 30:20a85b733111 642 {
AzureIoTClient 30:20a85b733111 643 if (device_stop(registered_device->device_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 644 {
AzureIoTClient 30:20a85b733111 645 LogError("Failed preparing device '%s' for connection retry (device_stop failed)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 646 }
AzureIoTClient 30:20a85b733111 647 }
AzureIoTClient 25:63f7d371c030 648
AzureIoTClient 30:20a85b733111 649 registered_device->number_of_previous_failures = 0;
AzureIoTClient 30:20a85b733111 650 registered_device->number_of_send_event_complete_failures = 0;
AzureIoTClient 25:63f7d371c030 651 }
AzureIoTClient 25:63f7d371c030 652
AzureIoTClient 30:20a85b733111 653 static void prepare_for_connection_retry(AMQP_TRANSPORT_INSTANCE* transport_instance)
AzureIoTClient 25:63f7d371c030 654 {
AzureIoTClient 30:20a85b733111 655 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_034: [`instance->tls_io` options shall be saved on `instance->saved_tls_options` using xio_retrieveoptions()]
AzureIoTClient 30:20a85b733111 656 if (save_underlying_io_transport_options(transport_instance) != RESULT_OK)
AzureIoTClient 30:20a85b733111 657 {
AzureIoTClient 30:20a85b733111 658 LogError("Failed saving TLS I/O options while preparing for connection retry; failure will be ignored");
AzureIoTClient 30:20a85b733111 659 }
AzureIoTClient 25:63f7d371c030 660
AzureIoTClient 30:20a85b733111 661 LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(transport_instance->registered_devices);
AzureIoTClient 30:20a85b733111 662
AzureIoTClient 30:20a85b733111 663 while (list_item != NULL)
AzureIoTClient 30:20a85b733111 664 {
AzureIoTClient 30:20a85b733111 665 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item);
AzureIoTClient 25:63f7d371c030 666
AzureIoTClient 30:20a85b733111 667 if (registered_device == NULL)
AzureIoTClient 30:20a85b733111 668 {
AzureIoTClient 30:20a85b733111 669 LogError("Failed preparing device for connection retry (singlylinkedlist_item_get_value failed)");
AzureIoTClient 30:20a85b733111 670 }
AzureIoTClient 30:20a85b733111 671 else
AzureIoTClient 30:20a85b733111 672 {
AzureIoTClient 30:20a85b733111 673 prepare_device_for_connection_retry(registered_device);
AzureIoTClient 30:20a85b733111 674 }
AzureIoTClient 25:63f7d371c030 675
AzureIoTClient 30:20a85b733111 676 list_item = singlylinkedlist_get_next_item(list_item);
AzureIoTClient 30:20a85b733111 677 }
AzureIoTClient 25:63f7d371c030 678
AzureIoTClient 30:20a85b733111 679 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_033: [`instance->connection` shall be destroyed using amqp_connection_destroy()]
AzureIoTClient 30:20a85b733111 680 amqp_connection_destroy(transport_instance->amqp_connection);
AzureIoTClient 30:20a85b733111 681 transport_instance->amqp_connection = NULL;
AzureIoTClient 30:20a85b733111 682 transport_instance->amqp_connection_state = AMQP_CONNECTION_STATE_CLOSED;
AzureIoTClient 25:63f7d371c030 683
AzureIoTClient 30:20a85b733111 684 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_035: [`instance->tls_io` shall be destroyed using xio_destroy()]
AzureIoTClient 30:20a85b733111 685 destroy_underlying_io_transport(transport_instance);
AzureIoTClient 25:63f7d371c030 686 }
AzureIoTClient 25:63f7d371c030 687
AzureIoTClient 30:20a85b733111 688
AzureIoTClient 30:20a85b733111 689 // @brief Verifies if the crendentials used by the device match the requirements and authentication mode currently supported by the transport.
AzureIoTClient 30:20a85b733111 690 // @returns true if credentials are good, false otherwise.
AzureIoTClient 30:20a85b733111 691 static bool is_device_credential_acceptable(const IOTHUB_DEVICE_CONFIG* device_config, AMQP_TRANSPORT_AUTHENTICATION_MODE preferred_authentication_mode)
AzureIoTClient 25:63f7d371c030 692 {
AzureIoTClient 30:20a85b733111 693 bool result;
AzureIoTClient 25:63f7d371c030 694
AzureIoTClient 30:20a85b733111 695 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_003: [IoTHubTransport_AMQP_Common_Register shall return NULL if both deviceKey and deviceSasToken are not NULL.]
AzureIoTClient 30:20a85b733111 696 if ((device_config->deviceSasToken != NULL) && (device_config->deviceKey != NULL))
AzureIoTClient 30:20a85b733111 697 {
AzureIoTClient 30:20a85b733111 698 LogError("Credential of device '%s' is not acceptable (must provide EITHER deviceSasToken OR deviceKey)", device_config->deviceId);
AzureIoTClient 30:20a85b733111 699 result = false;
AzureIoTClient 30:20a85b733111 700 }
AzureIoTClient 30:20a85b733111 701 else if (preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET)
AzureIoTClient 26:ee14eed604f6 702 {
AzureIoTClient 30:20a85b733111 703 result = true;
AzureIoTClient 30:20a85b733111 704 }
AzureIoTClient 30:20a85b733111 705 else if (preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_X509 && (device_config->deviceKey != NULL || device_config->deviceSasToken != NULL))
AzureIoTClient 30:20a85b733111 706 {
AzureIoTClient 30:20a85b733111 707 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 708 result = false;
AzureIoTClient 30:20a85b733111 709 }
AzureIoTClient 30:20a85b733111 710 else if (preferred_authentication_mode != AMQP_TRANSPORT_AUTHENTICATION_MODE_X509 && (device_config->deviceKey == NULL && device_config->deviceSasToken == NULL))
AzureIoTClient 30:20a85b733111 711 {
AzureIoTClient 30:20a85b733111 712 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 713 result = false;
AzureIoTClient 26:ee14eed604f6 714 }
AzureIoTClient 26:ee14eed604f6 715 else
AzureIoTClient 26:ee14eed604f6 716 {
AzureIoTClient 30:20a85b733111 717 result = true;
AzureIoTClient 26:ee14eed604f6 718 }
AzureIoTClient 25:63f7d371c030 719
AzureIoTClient 26:ee14eed604f6 720 return result;
AzureIoTClient 25:63f7d371c030 721 }
AzureIoTClient 25:63f7d371c030 722
AzureIoTClient 25:63f7d371c030 723
AzureIoTClient 30:20a85b733111 724
AzureIoTClient 30:20a85b733111 725 //---------- DoWork Helpers ----------//
AzureIoTClient 30:20a85b733111 726
AzureIoTClient 30:20a85b733111 727 static IOTHUB_MESSAGE_LIST* get_next_event_to_send(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device)
AzureIoTClient 30:20a85b733111 728 {
AzureIoTClient 30:20a85b733111 729 IOTHUB_MESSAGE_LIST* message;
AzureIoTClient 30:20a85b733111 730
AzureIoTClient 30:20a85b733111 731 if (!DList_IsListEmpty(registered_device->waiting_to_send))
AzureIoTClient 30:20a85b733111 732 {
AzureIoTClient 30:20a85b733111 733 PDLIST_ENTRY list_entry = registered_device->waiting_to_send->Flink;
AzureIoTClient 30:20a85b733111 734 message = containingRecord(list_entry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 30:20a85b733111 735 (void)DList_RemoveEntryList(list_entry);
AzureIoTClient 30:20a85b733111 736 }
AzureIoTClient 30:20a85b733111 737 else
AzureIoTClient 30:20a85b733111 738 {
AzureIoTClient 30:20a85b733111 739 message = NULL;
AzureIoTClient 30:20a85b733111 740 }
AzureIoTClient 30:20a85b733111 741
AzureIoTClient 30:20a85b733111 742 return message;
AzureIoTClient 30:20a85b733111 743 }
AzureIoTClient 30:20a85b733111 744
AzureIoTClient 30:20a85b733111 745 // @brief "Parses" the D2C_EVENT_SEND_RESULT (from iothubtransport_amqp_device module) into a IOTHUB_CLIENT_CONFIRMATION_RESULT.
AzureIoTClient 30:20a85b733111 746 static IOTHUB_CLIENT_CONFIRMATION_RESULT get_iothub_client_confirmation_result_from(D2C_EVENT_SEND_RESULT result)
AzureIoTClient 30:20a85b733111 747 {
AzureIoTClient 30:20a85b733111 748 IOTHUB_CLIENT_CONFIRMATION_RESULT iothub_send_result;
AzureIoTClient 30:20a85b733111 749
AzureIoTClient 30:20a85b733111 750 switch (result)
AzureIoTClient 30:20a85b733111 751 {
AzureIoTClient 30:20a85b733111 752 case D2C_EVENT_SEND_COMPLETE_RESULT_OK:
AzureIoTClient 30:20a85b733111 753 iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_OK;
AzureIoTClient 30:20a85b733111 754 break;
AzureIoTClient 30:20a85b733111 755 case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_CANNOT_PARSE:
AzureIoTClient 30:20a85b733111 756 case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING:
AzureIoTClient 30:20a85b733111 757 iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR;
AzureIoTClient 30:20a85b733111 758 break;
AzureIoTClient 30:20a85b733111 759 case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_TIMEOUT:
AzureIoTClient 30:20a85b733111 760 iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT;
AzureIoTClient 30:20a85b733111 761 break;
AzureIoTClient 30:20a85b733111 762 case D2C_EVENT_SEND_COMPLETE_RESULT_DEVICE_DESTROYED:
AzureIoTClient 30:20a85b733111 763 iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY;
AzureIoTClient 30:20a85b733111 764 break;
AzureIoTClient 30:20a85b733111 765 case D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_UNKNOWN:
AzureIoTClient 30:20a85b733111 766 default:
AzureIoTClient 30:20a85b733111 767 iothub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR;
AzureIoTClient 30:20a85b733111 768 break;
AzureIoTClient 30:20a85b733111 769 }
AzureIoTClient 30:20a85b733111 770
AzureIoTClient 30:20a85b733111 771 return iothub_send_result;
AzureIoTClient 30:20a85b733111 772 }
AzureIoTClient 30:20a85b733111 773
AzureIoTClient 30:20a85b733111 774 // @brief
AzureIoTClient 30:20a85b733111 775 // Callback function for device_send_event_async.
AzureIoTClient 30:20a85b733111 776 static void on_event_send_complete(IOTHUB_MESSAGE_LIST* message, D2C_EVENT_SEND_RESULT result, void* context)
AzureIoTClient 30:20a85b733111 777 {
AzureIoTClient 30:20a85b733111 778 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)context;
AzureIoTClient 30:20a85b733111 779
AzureIoTClient 30:20a85b733111 780 if (result != D2C_EVENT_SEND_COMPLETE_RESULT_OK && result != D2C_EVENT_SEND_COMPLETE_RESULT_DEVICE_DESTROYED)
AzureIoTClient 30:20a85b733111 781 {
AzureIoTClient 30:20a85b733111 782 registered_device->number_of_send_event_complete_failures++;
AzureIoTClient 30:20a85b733111 783 }
AzureIoTClient 30:20a85b733111 784 else
AzureIoTClient 30:20a85b733111 785 {
AzureIoTClient 30:20a85b733111 786 registered_device->number_of_send_event_complete_failures = 0;
AzureIoTClient 30:20a85b733111 787 }
AzureIoTClient 30:20a85b733111 788
AzureIoTClient 30:20a85b733111 789 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_056: [If `message->callback` is not NULL, it shall invoked with the `iothub_send_result`]
AzureIoTClient 30:20a85b733111 790 if (message->callback != NULL)
AzureIoTClient 30:20a85b733111 791 {
AzureIoTClient 30:20a85b733111 792 // 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 30:20a85b733111 793 // 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 30:20a85b733111 794 // 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 30:20a85b733111 795 // 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 30:20a85b733111 796 // 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 30:20a85b733111 797 // 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 30:20a85b733111 798 IOTHUB_CLIENT_CONFIRMATION_RESULT iothub_send_result = get_iothub_client_confirmation_result_from(result);
AzureIoTClient 30:20a85b733111 799
AzureIoTClient 30:20a85b733111 800 message->callback(iothub_send_result, message->context);
AzureIoTClient 30:20a85b733111 801 }
AzureIoTClient 30:20a85b733111 802
AzureIoTClient 30:20a85b733111 803 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_057: [`message->messageHandle` shall be destroyed using IoTHubMessage_Destroy]
AzureIoTClient 30:20a85b733111 804 IoTHubMessage_Destroy(message->messageHandle);
AzureIoTClient 30:20a85b733111 805 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_058: [`message` shall be destroyed using free]
AzureIoTClient 30:20a85b733111 806 free(message);
AzureIoTClient 30:20a85b733111 807 }
AzureIoTClient 30:20a85b733111 808
AzureIoTClient 30:20a85b733111 809 // @brief
AzureIoTClient 30:20a85b733111 810 // Gets events from wait to send list and sends to service in the order they were added.
AzureIoTClient 30:20a85b733111 811 // @returns
AzureIoTClient 30:20a85b733111 812 // 0 if all events could be sent to the next layer successfully, non-zero otherwise.
AzureIoTClient 30:20a85b733111 813 static int send_pending_events(AMQP_TRANSPORT_DEVICE_INSTANCE* device_state)
AzureIoTClient 30:20a85b733111 814 {
AzureIoTClient 30:20a85b733111 815 int result;
AzureIoTClient 30:20a85b733111 816 IOTHUB_MESSAGE_LIST* message;
AzureIoTClient 30:20a85b733111 817
AzureIoTClient 30:20a85b733111 818 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 819
AzureIoTClient 30:20a85b733111 820 // 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 30:20a85b733111 821 while ((message = get_next_event_to_send(device_state)) != NULL)
AzureIoTClient 30:20a85b733111 822 {
AzureIoTClient 30:20a85b733111 823 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_048: [device_send_event_async() shall be invoked passing `on_event_send_complete`]
AzureIoTClient 30:20a85b733111 824 if (device_send_event_async(device_state->device_handle, message, on_event_send_complete, device_state) != RESULT_OK)
AzureIoTClient 30:20a85b733111 825 {
AzureIoTClient 30:20a85b733111 826 // 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 30:20a85b733111 827 LogError("Device '%s' failed to send message (device_send_event_async failed)", STRING_c_str(device_state->device_id));
AzureIoTClient 30:20a85b733111 828 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 829
AzureIoTClient 30:20a85b733111 830 on_event_send_complete(message, D2C_EVENT_SEND_COMPLETE_RESULT_ERROR_FAIL_SENDING, device_state);
AzureIoTClient 30:20a85b733111 831 break;
AzureIoTClient 30:20a85b733111 832 }
AzureIoTClient 30:20a85b733111 833 }
AzureIoTClient 30:20a85b733111 834
AzureIoTClient 30:20a85b733111 835 return result;
AzureIoTClient 30:20a85b733111 836 }
AzureIoTClient 30:20a85b733111 837
AzureIoTClient 30:20a85b733111 838 // @brief
AzureIoTClient 30:20a85b733111 839 // Auxiliary function for the public DoWork API, performing DoWork activities (authenticate, messaging) for a specific device.
AzureIoTClient 30:20a85b733111 840 // @requires
AzureIoTClient 30:20a85b733111 841 // The transport to have a valid instance of AMQP_CONNECTION (from which to obtain SESSION_HANDLE and CBS_HANDLE)
AzureIoTClient 30:20a85b733111 842 // @returns
AzureIoTClient 30:20a85b733111 843 // 0 if no errors occur, non-zero otherwise.
AzureIoTClient 30:20a85b733111 844 static int IoTHubTransport_AMQP_Common_Device_DoWork(AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device)
AzureIoTClient 30:20a85b733111 845 {
AzureIoTClient 30:20a85b733111 846 int result;
AzureIoTClient 30:20a85b733111 847
AzureIoTClient 30:20a85b733111 848 if (registered_device->device_state != DEVICE_STATE_STARTED)
AzureIoTClient 30:20a85b733111 849 {
AzureIoTClient 30:20a85b733111 850 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_036: [If the device state is DEVICE_STATE_STOPPED, it shall be started]
AzureIoTClient 30:20a85b733111 851 if (registered_device->device_state == DEVICE_STATE_STOPPED)
AzureIoTClient 30:20a85b733111 852 {
AzureIoTClient 30:20a85b733111 853 SESSION_HANDLE session_handle;
AzureIoTClient 30:20a85b733111 854 CBS_HANDLE cbs_handle = NULL;
AzureIoTClient 30:20a85b733111 855
AzureIoTClient 30:20a85b733111 856 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_039: [amqp_connection_get_session_handle() shall be invoked on `instance->connection`]
AzureIoTClient 30:20a85b733111 857 if (amqp_connection_get_session_handle(registered_device->transport_instance->amqp_connection, &session_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 858 {
AzureIoTClient 30:20a85b733111 859 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_040: [If amqp_connection_get_session_handle() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return]
AzureIoTClient 30:20a85b733111 860 LogError("Failed performing DoWork for device '%s' (failed to get the amqp_connection session_handle)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 861 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 862 }
AzureIoTClient 30:20a85b733111 863 // 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 30:20a85b733111 864 else if (registered_device->transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS &&
AzureIoTClient 30:20a85b733111 865 amqp_connection_get_cbs_handle(registered_device->transport_instance->amqp_connection, &cbs_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 866 {
AzureIoTClient 30:20a85b733111 867 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_038: [If amqp_connection_get_cbs_handle() fails, IoTHubTransport_AMQP_Common_DoWork shall fail and return]
AzureIoTClient 30:20a85b733111 868 LogError("Failed performing DoWork for device '%s' (failed to get the amqp_connection cbs_handle)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 869 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 870 }
AzureIoTClient 30:20a85b733111 871 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_041: [The device handle shall be started using device_start_async()]
AzureIoTClient 30:20a85b733111 872 else if (device_start_async(registered_device->device_handle, session_handle, cbs_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 873 {
AzureIoTClient 30:20a85b733111 874 // 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 30:20a85b733111 875 LogError("Failed performing DoWork for device '%s' (failed to start device)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 876 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 877 }
AzureIoTClient 30:20a85b733111 878 else
AzureIoTClient 30:20a85b733111 879 {
AzureIoTClient 30:20a85b733111 880 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 881 }
AzureIoTClient 30:20a85b733111 882 }
AzureIoTClient 30:20a85b733111 883 else if (registered_device->device_state == DEVICE_STATE_STARTING ||
AzureIoTClient 30:20a85b733111 884 registered_device->device_state == DEVICE_STATE_STOPPING)
AzureIoTClient 30:20a85b733111 885 {
AzureIoTClient 30:20a85b733111 886 // 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 30:20a85b733111 887 bool is_timed_out;
AzureIoTClient 30:20a85b733111 888 if (is_timeout_reached(registered_device->time_of_last_state_change, registered_device->max_state_change_timeout_secs, &is_timed_out) != RESULT_OK)
AzureIoTClient 30:20a85b733111 889 {
AzureIoTClient 30:20a85b733111 890 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 30:20a85b733111 891 registered_device->device_state = DEVICE_STATE_ERROR_AUTH; // if time could not be calculated, the worst must be assumed.
AzureIoTClient 30:20a85b733111 892 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 893 }
AzureIoTClient 30:20a85b733111 894 else if (is_timed_out)
AzureIoTClient 30:20a85b733111 895 {
AzureIoTClient 30:20a85b733111 896 // 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 30:20a85b733111 897 LogError("Failed performing DoWork for device '%s' (device failed to start or stop within expected timeout)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 898 registered_device->device_state = DEVICE_STATE_ERROR_AUTH; // this will cause device to be stopped bellow on the next call to this function.
AzureIoTClient 30:20a85b733111 899 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 900 }
AzureIoTClient 30:20a85b733111 901 else
AzureIoTClient 30:20a85b733111 902 {
AzureIoTClient 30:20a85b733111 903 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 904 }
AzureIoTClient 30:20a85b733111 905 }
AzureIoTClient 30:20a85b733111 906 else // i.e., DEVICE_STATE_ERROR_AUTH || DEVICE_STATE_ERROR_AUTH_TIMEOUT || DEVICE_STATE_ERROR_MSG
AzureIoTClient 30:20a85b733111 907 {
AzureIoTClient 30:20a85b733111 908 LogError("Failed performing DoWork for device '%s' (device reported state %d; number of previous failures: %d)",
AzureIoTClient 30:20a85b733111 909 STRING_c_str(registered_device->device_id), registered_device->device_state, registered_device->number_of_previous_failures);
AzureIoTClient 30:20a85b733111 910
AzureIoTClient 30:20a85b733111 911 registered_device->number_of_previous_failures++;
AzureIoTClient 30:20a85b733111 912
AzureIoTClient 30:20a85b733111 913 // 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 30:20a85b733111 914 if (registered_device->number_of_previous_failures >= MAX_NUMBER_OF_DEVICE_FAILURES)
AzureIoTClient 30:20a85b733111 915 {
AzureIoTClient 30:20a85b733111 916 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 917 }
AzureIoTClient 30:20a85b733111 918 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_045: [If the registered device has a failure, it shall be stopped using device_stop()]
AzureIoTClient 30:20a85b733111 919 else if (device_stop(registered_device->device_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 920 {
AzureIoTClient 30:20a85b733111 921 LogError("Failed to stop reset device '%s' (device_stop failed)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 922 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 923 }
AzureIoTClient 30:20a85b733111 924 else
AzureIoTClient 30:20a85b733111 925 {
AzureIoTClient 30:20a85b733111 926 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 927 }
AzureIoTClient 30:20a85b733111 928 }
AzureIoTClient 30:20a85b733111 929 }
AzureIoTClient 30:20a85b733111 930 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 931 // 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 30:20a85b733111 932 else if (registered_device->subscribe_methods_needed &&
AzureIoTClient 30:20a85b733111 933 !registered_device->subscribed_for_methods &&
AzureIoTClient 30:20a85b733111 934 subscribe_methods(registered_device) != RESULT_OK)
AzureIoTClient 30:20a85b733111 935 {
AzureIoTClient 30:20a85b733111 936 LogError("Failed performing DoWork for device '%s' (failed registering for device methods)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 937 registered_device->number_of_previous_failures++;
AzureIoTClient 30:20a85b733111 938 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 939 }
AzureIoTClient 30:20a85b733111 940 #endif
AzureIoTClient 30:20a85b733111 941 else
AzureIoTClient 30:20a85b733111 942 {
AzureIoTClient 30:20a85b733111 943 if (send_pending_events(registered_device) != RESULT_OK)
AzureIoTClient 30:20a85b733111 944 {
AzureIoTClient 30:20a85b733111 945 LogError("Failed performing DoWork for device '%s' (failed sending pending events)", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 946 registered_device->number_of_previous_failures++;
AzureIoTClient 30:20a85b733111 947 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 948 }
AzureIoTClient 30:20a85b733111 949 else
AzureIoTClient 30:20a85b733111 950 {
AzureIoTClient 30:20a85b733111 951 registered_device->number_of_previous_failures = 0;
AzureIoTClient 30:20a85b733111 952 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 953 }
AzureIoTClient 30:20a85b733111 954 }
AzureIoTClient 30:20a85b733111 955
AzureIoTClient 30:20a85b733111 956 // No harm in invoking this as API will simply exit if the state is not "started".
AzureIoTClient 30:20a85b733111 957 device_do_work(registered_device->device_handle);
AzureIoTClient 30:20a85b733111 958
AzureIoTClient 30:20a85b733111 959 return result;
AzureIoTClient 30:20a85b733111 960 }
AzureIoTClient 30:20a85b733111 961
AzureIoTClient 30:20a85b733111 962
AzureIoTClient 30:20a85b733111 963 //---------- SetOption-ish Helpers ----------//
AzureIoTClient 30:20a85b733111 964
AzureIoTClient 30:20a85b733111 965 // @brief
AzureIoTClient 30:20a85b733111 966 // Gets all the device-specific options and replicates them into this new registered device.
AzureIoTClient 30:20a85b733111 967 // @returns
AzureIoTClient 30:20a85b733111 968 // 0 if the function succeeds, non-zero otherwise.
AzureIoTClient 30:20a85b733111 969 static int replicate_device_options_to(AMQP_TRANSPORT_DEVICE_INSTANCE* dev_instance, DEVICE_AUTH_MODE auth_mode)
AzureIoTClient 30:20a85b733111 970 {
AzureIoTClient 30:20a85b733111 971 int result;
AzureIoTClient 30:20a85b733111 972
AzureIoTClient 30:20a85b733111 973 if (device_set_option(
AzureIoTClient 30:20a85b733111 974 dev_instance->device_handle,
AzureIoTClient 30:20a85b733111 975 DEVICE_OPTION_EVENT_SEND_TIMEOUT_SECS,
AzureIoTClient 30:20a85b733111 976 &dev_instance->transport_instance->option_send_event_timeout_secs) != RESULT_OK)
AzureIoTClient 30:20a85b733111 977 {
AzureIoTClient 30:20a85b733111 978 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 30:20a85b733111 979 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 980 }
AzureIoTClient 30:20a85b733111 981 else if (auth_mode == DEVICE_AUTH_MODE_CBS)
AzureIoTClient 30:20a85b733111 982 {
AzureIoTClient 30:20a85b733111 983 if (device_set_option(
AzureIoTClient 30:20a85b733111 984 dev_instance->device_handle,
AzureIoTClient 30:20a85b733111 985 DEVICE_OPTION_CBS_REQUEST_TIMEOUT_SECS,
AzureIoTClient 30:20a85b733111 986 &dev_instance->transport_instance->option_cbs_request_timeout_secs) != RESULT_OK)
AzureIoTClient 30:20a85b733111 987 {
AzureIoTClient 30:20a85b733111 988 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 30:20a85b733111 989 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 990 }
AzureIoTClient 30:20a85b733111 991 else if (device_set_option(
AzureIoTClient 30:20a85b733111 992 dev_instance->device_handle,
AzureIoTClient 30:20a85b733111 993 DEVICE_OPTION_SAS_TOKEN_LIFETIME_SECS,
AzureIoTClient 30:20a85b733111 994 &dev_instance->transport_instance->option_sas_token_lifetime_secs) != RESULT_OK)
AzureIoTClient 30:20a85b733111 995 {
AzureIoTClient 30:20a85b733111 996 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 30:20a85b733111 997 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 998 }
AzureIoTClient 30:20a85b733111 999 else if (device_set_option(
AzureIoTClient 30:20a85b733111 1000 dev_instance->device_handle,
AzureIoTClient 30:20a85b733111 1001 DEVICE_OPTION_SAS_TOKEN_REFRESH_TIME_SECS,
AzureIoTClient 30:20a85b733111 1002 &dev_instance->transport_instance->option_sas_token_refresh_time_secs) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1003 {
AzureIoTClient 30:20a85b733111 1004 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 30:20a85b733111 1005 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1006 }
AzureIoTClient 30:20a85b733111 1007 else
AzureIoTClient 30:20a85b733111 1008 {
AzureIoTClient 30:20a85b733111 1009 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 1010 }
AzureIoTClient 30:20a85b733111 1011 }
AzureIoTClient 30:20a85b733111 1012 else
AzureIoTClient 30:20a85b733111 1013 {
AzureIoTClient 30:20a85b733111 1014 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 1015 }
AzureIoTClient 30:20a85b733111 1016
AzureIoTClient 30:20a85b733111 1017 return result;
AzureIoTClient 30:20a85b733111 1018 }
AzureIoTClient 30:20a85b733111 1019
AzureIoTClient 30:20a85b733111 1020 // @brief
AzureIoTClient 30:20a85b733111 1021 // Translates from the option names supported by iothubtransport_amqp_common to the ones supported by iothubtransport_amqp_device.
AzureIoTClient 30:20a85b733111 1022 static const char* get_device_option_name_from(const char* iothubclient_option_name)
AzureIoTClient 30:20a85b733111 1023 {
AzureIoTClient 30:20a85b733111 1024 const char* device_option_name;
AzureIoTClient 30:20a85b733111 1025
AzureIoTClient 30:20a85b733111 1026 if (strcmp(OPTION_SAS_TOKEN_LIFETIME, iothubclient_option_name) == 0)
AzureIoTClient 30:20a85b733111 1027 {
AzureIoTClient 30:20a85b733111 1028 device_option_name = DEVICE_OPTION_SAS_TOKEN_LIFETIME_SECS;
AzureIoTClient 30:20a85b733111 1029 }
AzureIoTClient 30:20a85b733111 1030 else if (strcmp(OPTION_SAS_TOKEN_REFRESH_TIME, iothubclient_option_name) == 0)
AzureIoTClient 30:20a85b733111 1031 {
AzureIoTClient 30:20a85b733111 1032 device_option_name = DEVICE_OPTION_SAS_TOKEN_REFRESH_TIME_SECS;
AzureIoTClient 30:20a85b733111 1033 }
AzureIoTClient 30:20a85b733111 1034 else if (strcmp(OPTION_CBS_REQUEST_TIMEOUT, iothubclient_option_name) == 0)
AzureIoTClient 30:20a85b733111 1035 {
AzureIoTClient 30:20a85b733111 1036 device_option_name = DEVICE_OPTION_CBS_REQUEST_TIMEOUT_SECS;
AzureIoTClient 30:20a85b733111 1037 }
AzureIoTClient 30:20a85b733111 1038 else if (strcmp(OPTION_EVENT_SEND_TIMEOUT_SECS, iothubclient_option_name) == 0)
AzureIoTClient 30:20a85b733111 1039 {
AzureIoTClient 30:20a85b733111 1040 device_option_name = DEVICE_OPTION_EVENT_SEND_TIMEOUT_SECS;
AzureIoTClient 30:20a85b733111 1041 }
AzureIoTClient 30:20a85b733111 1042 else
AzureIoTClient 30:20a85b733111 1043 {
AzureIoTClient 30:20a85b733111 1044 device_option_name = NULL;
AzureIoTClient 30:20a85b733111 1045 }
AzureIoTClient 30:20a85b733111 1046
AzureIoTClient 30:20a85b733111 1047 return device_option_name;
AzureIoTClient 30:20a85b733111 1048 }
AzureIoTClient 30:20a85b733111 1049
AzureIoTClient 30:20a85b733111 1050 // @brief
AzureIoTClient 30:20a85b733111 1051 // Auxiliary function invoked by IoTHubTransport_AMQP_Common_SetOption to set an option on every registered device.
AzureIoTClient 30:20a85b733111 1052 // @returns
AzureIoTClient 30:20a85b733111 1053 // 0 if it succeeds, non-zero otherwise.
AzureIoTClient 30:20a85b733111 1054 static int IoTHubTransport_AMQP_Common_Device_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, void* value)
AzureIoTClient 30:20a85b733111 1055 {
AzureIoTClient 30:20a85b733111 1056 int result;
AzureIoTClient 30:20a85b733111 1057 const char* device_option;
AzureIoTClient 30:20a85b733111 1058
AzureIoTClient 30:20a85b733111 1059 if ((device_option = get_device_option_name_from(option)) == NULL)
AzureIoTClient 30:20a85b733111 1060 {
AzureIoTClient 30:20a85b733111 1061 LogError("failed setting option '%s' to registered device (could not match name to options supported by device)", option);
AzureIoTClient 30:20a85b733111 1062 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1063 }
AzureIoTClient 30:20a85b733111 1064 else
AzureIoTClient 30:20a85b733111 1065 {
AzureIoTClient 30:20a85b733111 1066 AMQP_TRANSPORT_INSTANCE* instance = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 30:20a85b733111 1067 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 1068
AzureIoTClient 30:20a85b733111 1069 LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(instance->registered_devices);
AzureIoTClient 30:20a85b733111 1070
AzureIoTClient 30:20a85b733111 1071 while (list_item != NULL)
AzureIoTClient 30:20a85b733111 1072 {
AzureIoTClient 30:20a85b733111 1073 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device;
AzureIoTClient 30:20a85b733111 1074
AzureIoTClient 30:20a85b733111 1075 if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL)
AzureIoTClient 30:20a85b733111 1076 {
AzureIoTClient 30:20a85b733111 1077 LogError("failed setting option '%s' to registered device (singlylinkedlist_item_get_value failed)", option);
AzureIoTClient 30:20a85b733111 1078 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1079 break;
AzureIoTClient 30:20a85b733111 1080 }
AzureIoTClient 30:20a85b733111 1081 else if (device_set_option(registered_device->device_handle, device_option, value) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1082 {
AzureIoTClient 30:20a85b733111 1083 LogError("failed setting option '%s' to registered device '%s' (device_set_option failed)",
AzureIoTClient 30:20a85b733111 1084 option, STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 1085 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1086 break;
AzureIoTClient 30:20a85b733111 1087 }
AzureIoTClient 30:20a85b733111 1088
AzureIoTClient 30:20a85b733111 1089 list_item = singlylinkedlist_get_next_item(list_item);
AzureIoTClient 30:20a85b733111 1090 }
AzureIoTClient 30:20a85b733111 1091 }
AzureIoTClient 30:20a85b733111 1092
AzureIoTClient 30:20a85b733111 1093 return result;
AzureIoTClient 30:20a85b733111 1094 }
AzureIoTClient 30:20a85b733111 1095
AzureIoTClient 30:20a85b733111 1096 static void internal_destroy_instance(AMQP_TRANSPORT_INSTANCE* instance)
AzureIoTClient 30:20a85b733111 1097 {
AzureIoTClient 30:20a85b733111 1098 if (instance != NULL)
AzureIoTClient 30:20a85b733111 1099 {
AzureIoTClient 30:20a85b733111 1100 if (instance->registered_devices != NULL)
AzureIoTClient 30:20a85b733111 1101 {
AzureIoTClient 30:20a85b733111 1102 LIST_ITEM_HANDLE list_item = singlylinkedlist_get_head_item(instance->registered_devices);
AzureIoTClient 30:20a85b733111 1103
AzureIoTClient 30:20a85b733111 1104 while (list_item != NULL)
AzureIoTClient 30:20a85b733111 1105 {
AzureIoTClient 30:20a85b733111 1106 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item);
AzureIoTClient 30:20a85b733111 1107 list_item = singlylinkedlist_get_next_item(list_item);
AzureIoTClient 30:20a85b733111 1108 IoTHubTransport_AMQP_Common_Unregister(registered_device);
AzureIoTClient 30:20a85b733111 1109 }
AzureIoTClient 30:20a85b733111 1110
AzureIoTClient 30:20a85b733111 1111 singlylinkedlist_destroy(instance->registered_devices);
AzureIoTClient 30:20a85b733111 1112 }
AzureIoTClient 30:20a85b733111 1113
AzureIoTClient 30:20a85b733111 1114 if (instance->amqp_connection != NULL)
AzureIoTClient 30:20a85b733111 1115 {
AzureIoTClient 30:20a85b733111 1116 amqp_connection_destroy(instance->amqp_connection);
AzureIoTClient 30:20a85b733111 1117 }
AzureIoTClient 30:20a85b733111 1118
AzureIoTClient 30:20a85b733111 1119 destroy_underlying_io_transport(instance);
AzureIoTClient 30:20a85b733111 1120 destroy_underlying_io_transport_options(instance);
AzureIoTClient 30:20a85b733111 1121
AzureIoTClient 30:20a85b733111 1122 STRING_delete(instance->iothub_host_fqdn);
AzureIoTClient 30:20a85b733111 1123
AzureIoTClient 30:20a85b733111 1124 free(instance);
AzureIoTClient 30:20a85b733111 1125 }
AzureIoTClient 30:20a85b733111 1126 }
AzureIoTClient 30:20a85b733111 1127
AzureIoTClient 30:20a85b733111 1128
AzureIoTClient 30:20a85b733111 1129 // ---------- API functions ---------- //
AzureIoTClient 25:63f7d371c030 1130
AzureIoTClient 25:63f7d371c030 1131 TRANSPORT_LL_HANDLE IoTHubTransport_AMQP_Common_Create(const IOTHUBTRANSPORT_CONFIG* config, AMQP_GET_IO_TRANSPORT get_io_transport)
AzureIoTClient 25:63f7d371c030 1132 {
AzureIoTClient 30:20a85b733111 1133 TRANSPORT_LL_HANDLE result;
AzureIoTClient 25:63f7d371c030 1134
AzureIoTClient 30:20a85b733111 1135 // 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 30:20a85b733111 1136 if (config == NULL || config->upperConfig == NULL || get_io_transport == NULL)
AzureIoTClient 30:20a85b733111 1137 {
AzureIoTClient 30:20a85b733111 1138 LogError("IoTHub AMQP client transport null configuration parameter (config=%p, get_io_transport=%p).", config, get_io_transport);
AzureIoTClient 30:20a85b733111 1139 result = NULL;
AzureIoTClient 30:20a85b733111 1140 }
AzureIoTClient 30:20a85b733111 1141 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_002: [IoTHubTransport_AMQP_Common_Create shall fail and return NULL if `config->upperConfig->protocol` is NULL]
AzureIoTClient 30:20a85b733111 1142 else if (config->upperConfig->protocol == NULL)
AzureIoTClient 30:20a85b733111 1143 {
AzureIoTClient 30:20a85b733111 1144 LogError("Failed to create the AMQP transport common instance (NULL parameter received: protocol=%p, iotHubName=%p, iotHubSuffix=%p)",
AzureIoTClient 30:20a85b733111 1145 config->upperConfig->protocol, config->upperConfig->iotHubName, config->upperConfig->iotHubSuffix);
AzureIoTClient 30:20a85b733111 1146 result = NULL;
AzureIoTClient 30:20a85b733111 1147 }
AzureIoTClient 30:20a85b733111 1148 else
AzureIoTClient 30:20a85b733111 1149 {
AzureIoTClient 30:20a85b733111 1150 AMQP_TRANSPORT_INSTANCE* instance;
AzureIoTClient 25:63f7d371c030 1151
AzureIoTClient 30:20a85b733111 1152 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_003: [Memory shall be allocated for the transport's internal state structure (`instance`)]
AzureIoTClient 30:20a85b733111 1153 if ((instance = (AMQP_TRANSPORT_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_INSTANCE))) == NULL)
AzureIoTClient 30:20a85b733111 1154 {
AzureIoTClient 30:20a85b733111 1155 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_004: [If malloc() fails, IoTHubTransport_AMQP_Common_Create shall fail and return NULL]
AzureIoTClient 30:20a85b733111 1156 LogError("Could not allocate AMQP transport state (malloc failed)");
AzureIoTClient 30:20a85b733111 1157 result = NULL;
AzureIoTClient 30:20a85b733111 1158 }
AzureIoTClient 30:20a85b733111 1159 else
AzureIoTClient 30:20a85b733111 1160 {
AzureIoTClient 30:20a85b733111 1161 memset(instance, 0, sizeof(AMQP_TRANSPORT_INSTANCE));
AzureIoTClient 25:63f7d371c030 1162
AzureIoTClient 30:20a85b733111 1163 // 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 30:20a85b733111 1164 // 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 30:20a85b733111 1165 if ((instance->iothub_host_fqdn = get_target_iothub_fqdn(config)) == NULL)
AzureIoTClient 30:20a85b733111 1166 {
AzureIoTClient 30:20a85b733111 1167 // 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 30:20a85b733111 1168 LogError("Failed to obtain the iothub target fqdn.");
AzureIoTClient 30:20a85b733111 1169 result = NULL;
AzureIoTClient 30:20a85b733111 1170 }
AzureIoTClient 30:20a85b733111 1171 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_008: [`instance->registered_devices` shall be set using singlylinkedlist_create()]
AzureIoTClient 30:20a85b733111 1172 else if ((instance->registered_devices = singlylinkedlist_create()) == NULL)
AzureIoTClient 30:20a85b733111 1173 {
AzureIoTClient 30:20a85b733111 1174 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_009: [If singlylinkedlist_create() fails, IoTHubTransport_AMQP_Common_Create shall fail and return NULL]
AzureIoTClient 30:20a85b733111 1175 LogError("Failed to initialize the internal list of registered devices (singlylinkedlist_create failed)");
AzureIoTClient 30:20a85b733111 1176 result = NULL;
AzureIoTClient 30:20a85b733111 1177 }
AzureIoTClient 30:20a85b733111 1178 else
AzureIoTClient 30:20a85b733111 1179 {
AzureIoTClient 30:20a85b733111 1180 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_010: [`get_io_transport` shall be saved on `instance->underlying_io_transport_provider`]
AzureIoTClient 30:20a85b733111 1181 instance->underlying_io_transport_provider = get_io_transport;
AzureIoTClient 30:20a85b733111 1182 instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET;
AzureIoTClient 30:20a85b733111 1183 instance->option_sas_token_lifetime_secs = DEFAULT_SAS_TOKEN_LIFETIME_SECS;
AzureIoTClient 30:20a85b733111 1184 instance->option_sas_token_refresh_time_secs = DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS;
AzureIoTClient 30:20a85b733111 1185 instance->option_cbs_request_timeout_secs = DEFAULT_CBS_REQUEST_TIMEOUT_SECS;
AzureIoTClient 30:20a85b733111 1186 instance->option_send_event_timeout_secs = DEFAULT_EVENT_SEND_TIMEOUT_SECS;
AzureIoTClient 30:20a85b733111 1187
AzureIoTClient 30:20a85b733111 1188 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_012: [If IoTHubTransport_AMQP_Common_Create succeeds it shall return a pointer to `instance`.]
AzureIoTClient 30:20a85b733111 1189 result = (TRANSPORT_LL_HANDLE)instance;
AzureIoTClient 30:20a85b733111 1190 }
AzureIoTClient 25:63f7d371c030 1191
AzureIoTClient 30:20a85b733111 1192 if (result == NULL)
AzureIoTClient 30:20a85b733111 1193 {
AzureIoTClient 30:20a85b733111 1194 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_011: [If IoTHubTransport_AMQP_Common_Create fails it shall free any memory it allocated]
AzureIoTClient 30:20a85b733111 1195 internal_destroy_instance(instance);
AzureIoTClient 30:20a85b733111 1196 }
AzureIoTClient 30:20a85b733111 1197 }
AzureIoTClient 30:20a85b733111 1198 }
AzureIoTClient 25:63f7d371c030 1199
AzureIoTClient 26:ee14eed604f6 1200 return result;
AzureIoTClient 25:63f7d371c030 1201 }
AzureIoTClient 25:63f7d371c030 1202
AzureIoTClient 25:63f7d371c030 1203 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 1204 {
AzureIoTClient 25:63f7d371c030 1205 (void)handle;
AzureIoTClient 25:63f7d371c030 1206 (void)item_type;
AzureIoTClient 25:63f7d371c030 1207 (void)iothub_item;
AzureIoTClient 25:63f7d371c030 1208 LogError("Currently Not Supported.");
AzureIoTClient 28:dc01bcb2cb01 1209 return IOTHUB_PROCESS_ERROR;
AzureIoTClient 25:63f7d371c030 1210 }
AzureIoTClient 25:63f7d371c030 1211
AzureIoTClient 25:63f7d371c030 1212 void IoTHubTransport_AMQP_Common_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 25:63f7d371c030 1213 {
AzureIoTClient 30:20a85b733111 1214 (void)iotHubClientHandle; // unused as of now.
AzureIoTClient 30:20a85b733111 1215
AzureIoTClient 30:20a85b733111 1216 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_016: [If `handle` is NULL, IoTHubTransport_AMQP_Common_DoWork shall return without doing any work]
AzureIoTClient 25:63f7d371c030 1217 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1218 {
AzureIoTClient 25:63f7d371c030 1219 LogError("IoTHubClient DoWork failed: transport handle parameter is NULL.");
AzureIoTClient 25:63f7d371c030 1220 }
AzureIoTClient 25:63f7d371c030 1221 else
AzureIoTClient 25:63f7d371c030 1222 {
AzureIoTClient 30:20a85b733111 1223 AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 30:20a85b733111 1224 LIST_ITEM_HANDLE list_item;
AzureIoTClient 30:20a85b733111 1225
AzureIoTClient 30:20a85b733111 1226 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_017: [If `instance->is_connection_retry_required` is true, IoTHubTransport_AMQP_Common_DoWork shall trigger the connection-retry logic and return]
AzureIoTClient 30:20a85b733111 1227 if (transport_instance->is_connection_retry_required)
AzureIoTClient 30:20a85b733111 1228 {
AzureIoTClient 30:20a85b733111 1229 LogError("An error occured on AMQP connection. The connection will be restablished.");
AzureIoTClient 30:20a85b733111 1230
AzureIoTClient 30:20a85b733111 1231 prepare_for_connection_retry(transport_instance);
AzureIoTClient 30:20a85b733111 1232
AzureIoTClient 30:20a85b733111 1233 transport_instance->is_connection_retry_required = false;
AzureIoTClient 30:20a85b733111 1234 }
AzureIoTClient 30:20a85b733111 1235 // 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 30:20a85b733111 1236 else if ((list_item = singlylinkedlist_get_head_item(transport_instance->registered_devices)) != NULL)
AzureIoTClient 30:20a85b733111 1237 {
AzureIoTClient 30:20a85b733111 1238 // We need to check if there are devices, otherwise the amqp_connection won't be able to be created since
AzureIoTClient 30:20a85b733111 1239 // there is not a preferred authentication mode set yet on the transport.
AzureIoTClient 25:63f7d371c030 1240
AzureIoTClient 30:20a85b733111 1241 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_019: [If `instance->amqp_connection` is NULL, it shall be established]
AzureIoTClient 30:20a85b733111 1242 if (transport_instance->amqp_connection == NULL && establish_amqp_connection(transport_instance) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1243 {
AzureIoTClient 30:20a85b733111 1244 LogError("AMQP transport failed to establish connection with service.");
AzureIoTClient 30:20a85b733111 1245 }
AzureIoTClient 30:20a85b733111 1246 // 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 30:20a85b733111 1247 else if (transport_instance->amqp_connection_state == AMQP_CONNECTION_STATE_OPENED)
AzureIoTClient 30:20a85b733111 1248 {
AzureIoTClient 30:20a85b733111 1249 while (list_item != NULL)
AzureIoTClient 30:20a85b733111 1250 {
AzureIoTClient 30:20a85b733111 1251 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device;
AzureIoTClient 30:20a85b733111 1252
AzureIoTClient 30:20a85b733111 1253 if ((registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)singlylinkedlist_item_get_value(list_item)) == NULL)
AzureIoTClient 30:20a85b733111 1254 {
AzureIoTClient 30:20a85b733111 1255 LogError("Transport had an unexpected failure during DoWork (failed to fetch a registered_devices list item value)");
AzureIoTClient 30:20a85b733111 1256 }
AzureIoTClient 30:20a85b733111 1257 else if (registered_device->number_of_send_event_complete_failures >= MAX_NUMBER_OF_DEVICE_FAILURES)
AzureIoTClient 30:20a85b733111 1258 {
AzureIoTClient 30:20a85b733111 1259 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 1260
AzureIoTClient 30:20a85b733111 1261 transport_instance->is_connection_retry_required = true;
AzureIoTClient 30:20a85b733111 1262 }
AzureIoTClient 30:20a85b733111 1263 else if (IoTHubTransport_AMQP_Common_Device_DoWork(registered_device) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1264 {
AzureIoTClient 30:20a85b733111 1265 // 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 30:20a85b733111 1266 if (registered_device->number_of_previous_failures >= MAX_NUMBER_OF_DEVICE_FAILURES)
AzureIoTClient 30:20a85b733111 1267 {
AzureIoTClient 30:20a85b733111 1268 LogError("Device '%s' reported a critical failure; connection retry will be triggered.", STRING_c_str(registered_device->device_id));
AzureIoTClient 30:20a85b733111 1269
AzureIoTClient 30:20a85b733111 1270 transport_instance->is_connection_retry_required = true;
AzureIoTClient 30:20a85b733111 1271 }
AzureIoTClient 30:20a85b733111 1272 }
AzureIoTClient 30:20a85b733111 1273
AzureIoTClient 30:20a85b733111 1274 list_item = singlylinkedlist_get_next_item(list_item);
AzureIoTClient 30:20a85b733111 1275 }
AzureIoTClient 30:20a85b733111 1276 }
AzureIoTClient 30:20a85b733111 1277 }
AzureIoTClient 30:20a85b733111 1278
AzureIoTClient 30:20a85b733111 1279 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_022: [If `instance->amqp_connection` is not NULL, amqp_connection_do_work shall be invoked]
AzureIoTClient 30:20a85b733111 1280 if (transport_instance->amqp_connection != NULL)
AzureIoTClient 26:ee14eed604f6 1281 {
AzureIoTClient 30:20a85b733111 1282 amqp_connection_do_work(transport_instance->amqp_connection);
AzureIoTClient 26:ee14eed604f6 1283 }
AzureIoTClient 25:63f7d371c030 1284 }
AzureIoTClient 25:63f7d371c030 1285 }
AzureIoTClient 25:63f7d371c030 1286
AzureIoTClient 25:63f7d371c030 1287 int IoTHubTransport_AMQP_Common_Subscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1288 {
AzureIoTClient 25:63f7d371c030 1289 int result;
AzureIoTClient 25:63f7d371c030 1290
AzureIoTClient 30:20a85b733111 1291 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_084: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Subscribe shall return a non-zero result]
AzureIoTClient 25:63f7d371c030 1292 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1293 {
AzureIoTClient 25:63f7d371c030 1294 LogError("Invalid handle to IoTHubClient AMQP transport device handle.");
AzureIoTClient 29:7e8852b14e3b 1295 result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 1296 }
AzureIoTClient 25:63f7d371c030 1297 else
AzureIoTClient 25:63f7d371c030 1298 {
AzureIoTClient 30:20a85b733111 1299 AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 30:20a85b733111 1300
AzureIoTClient 30:20a85b733111 1301 // 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 30:20a85b733111 1302 if (!is_device_registered(amqp_device_instance))
AzureIoTClient 30:20a85b733111 1303 {
AzureIoTClient 30:20a85b733111 1304 LogError("Device '%s' failed subscribing to cloud-to-device messages (device is not registered)", STRING_c_str(amqp_device_instance->device_id));
AzureIoTClient 30:20a85b733111 1305 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1306 }
AzureIoTClient 30:20a85b733111 1307 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_086: [device_subscribe_message() shall be invoked passing `on_message_received_callback`]
AzureIoTClient 30:20a85b733111 1308 else if (device_subscribe_message(amqp_device_instance->device_handle, on_message_received, amqp_device_instance) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1309 {
AzureIoTClient 30:20a85b733111 1310 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_087: [If device_subscribe_message() fails, IoTHubTransport_AMQP_Common_Subscribe shall return a non-zero result]
AzureIoTClient 30:20a85b733111 1311 LogError("Device '%s' failed subscribing to cloud-to-device messages (device_subscribe_message failed)", STRING_c_str(amqp_device_instance->device_id));
AzureIoTClient 30:20a85b733111 1312 result = __FAILURE__;
AzureIoTClient 30:20a85b733111 1313 }
AzureIoTClient 30:20a85b733111 1314 else
AzureIoTClient 30:20a85b733111 1315 {
AzureIoTClient 30:20a85b733111 1316 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_088: [If no failures occur, IoTHubTransport_AMQP_Common_Subscribe shall return 0]
AzureIoTClient 30:20a85b733111 1317 result = RESULT_OK;
AzureIoTClient 30:20a85b733111 1318 }
AzureIoTClient 25:63f7d371c030 1319 }
AzureIoTClient 25:63f7d371c030 1320
AzureIoTClient 25:63f7d371c030 1321 return result;
AzureIoTClient 25:63f7d371c030 1322 }
AzureIoTClient 25:63f7d371c030 1323
AzureIoTClient 25:63f7d371c030 1324 void IoTHubTransport_AMQP_Common_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1325 {
AzureIoTClient 30:20a85b733111 1326 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_093: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Subscribe shall return]
AzureIoTClient 25:63f7d371c030 1327 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1328 {
AzureIoTClient 25:63f7d371c030 1329 LogError("Invalid handle to IoTHubClient AMQP transport device handle.");
AzureIoTClient 25:63f7d371c030 1330 }
AzureIoTClient 25:63f7d371c030 1331 else
AzureIoTClient 25:63f7d371c030 1332 {
AzureIoTClient 30:20a85b733111 1333 AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 30:20a85b733111 1334
AzureIoTClient 30:20a85b733111 1335 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_094: [If `amqp_device_instance` is not registered, IoTHubTransport_AMQP_Common_Subscribe shall return]
AzureIoTClient 30:20a85b733111 1336 if (!is_device_registered(amqp_device_instance))
AzureIoTClient 30:20a85b733111 1337 {
AzureIoTClient 30:20a85b733111 1338 LogError("Device '%s' failed unsubscribing to cloud-to-device messages (device is not registered)", STRING_c_str(amqp_device_instance->device_id));
AzureIoTClient 30:20a85b733111 1339 }
AzureIoTClient 30:20a85b733111 1340 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_095: [device_unsubscribe_message() shall be invoked passing `amqp_device_instance->device_handle`]
AzureIoTClient 30:20a85b733111 1341 else if (device_unsubscribe_message(amqp_device_instance->device_handle) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1342 {
AzureIoTClient 30:20a85b733111 1343 LogError("Device '%s' failed unsubscribing to cloud-to-device messages (device_unsubscribe_message failed)", STRING_c_str(amqp_device_instance->device_id));
AzureIoTClient 30:20a85b733111 1344 }
AzureIoTClient 25:63f7d371c030 1345 }
AzureIoTClient 25:63f7d371c030 1346 }
AzureIoTClient 25:63f7d371c030 1347
AzureIoTClient 25:63f7d371c030 1348 int IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1349 {
AzureIoTClient 25:63f7d371c030 1350 (void)handle;
AzureIoTClient 25:63f7d371c030 1351 /*Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_009: [ IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin shall return a non-zero value. ]*/
AzureIoTClient 29:7e8852b14e3b 1352 int result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 1353 LogError("IoTHubTransport_AMQP_Common_Subscribe_DeviceTwin Not supported");
AzureIoTClient 25:63f7d371c030 1354 return result;
AzureIoTClient 25:63f7d371c030 1355 }
AzureIoTClient 25:63f7d371c030 1356
AzureIoTClient 25:63f7d371c030 1357 void IoTHubTransport_AMQP_Common_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1358 {
AzureIoTClient 25:63f7d371c030 1359 (void)handle;
AzureIoTClient 25:63f7d371c030 1360 /*Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_010: [ IoTHubTransport_AMQP_Common_Unsubscribe_DeviceTwin shall return. ]*/
AzureIoTClient 25:63f7d371c030 1361 LogError("IoTHubTransport_AMQP_Common_Unsubscribe_DeviceTwin Not supported");
AzureIoTClient 25:63f7d371c030 1362 }
AzureIoTClient 25:63f7d371c030 1363
AzureIoTClient 25:63f7d371c030 1364 int IoTHubTransport_AMQP_Common_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1365 {
AzureIoTClient 25:63f7d371c030 1366 int result;
AzureIoTClient 25:63f7d371c030 1367
AzureIoTClient 25:63f7d371c030 1368 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1369 {
AzureIoTClient 25:63f7d371c030 1370 /* 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 1371 LogError("NULL handle");
AzureIoTClient 29:7e8852b14e3b 1372 result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 1373 }
AzureIoTClient 25:63f7d371c030 1374 else
AzureIoTClient 25:63f7d371c030 1375 {
AzureIoTClient 25:63f7d371c030 1376 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 1377 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 25:63f7d371c030 1378 /* 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 1379 /* 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 1380 device_state->subscribe_methods_needed = true;
AzureIoTClient 29:7e8852b14e3b 1381 device_state->subscribed_for_methods = false;
AzureIoTClient 25:63f7d371c030 1382 result = 0;
AzureIoTClient 25:63f7d371c030 1383 #else
AzureIoTClient 25:63f7d371c030 1384 LogError("Not implemented");
AzureIoTClient 29:7e8852b14e3b 1385 result = __FAILURE__;
AzureIoTClient 25:63f7d371c030 1386 #endif
AzureIoTClient 25:63f7d371c030 1387 }
AzureIoTClient 25:63f7d371c030 1388
AzureIoTClient 25:63f7d371c030 1389 return result;
AzureIoTClient 25:63f7d371c030 1390 }
AzureIoTClient 25:63f7d371c030 1391
AzureIoTClient 25:63f7d371c030 1392 void IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1393 {
AzureIoTClient 25:63f7d371c030 1394 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1395 {
AzureIoTClient 25:63f7d371c030 1396 /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_006: [ If `handle` is NULL, `IoTHubTransport_AMQP_Common_Unsubscribe_DeviceMethod` shall do nothing. ]*/
AzureIoTClient 25:63f7d371c030 1397 LogError("NULL handle");
AzureIoTClient 25:63f7d371c030 1398 }
AzureIoTClient 25:63f7d371c030 1399 else
AzureIoTClient 25:63f7d371c030 1400 {
AzureIoTClient 25:63f7d371c030 1401 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 1402 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 25:63f7d371c030 1403
AzureIoTClient 25:63f7d371c030 1404 /* 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 1405 if (device_state->subscribe_methods_needed)
AzureIoTClient 25:63f7d371c030 1406 {
AzureIoTClient 25:63f7d371c030 1407 /* 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 30:20a85b733111 1408 device_state->subscribed_for_methods = false;
AzureIoTClient 30:20a85b733111 1409 device_state->subscribe_methods_needed = false;
AzureIoTClient 30:20a85b733111 1410 iothubtransportamqp_methods_unsubscribe(device_state->methods_handle);
AzureIoTClient 25:63f7d371c030 1411 }
AzureIoTClient 25:63f7d371c030 1412 #else
AzureIoTClient 25:63f7d371c030 1413 LogError("Not implemented");
AzureIoTClient 25:63f7d371c030 1414 #endif
AzureIoTClient 25:63f7d371c030 1415 }
AzureIoTClient 25:63f7d371c030 1416 }
AzureIoTClient 25:63f7d371c030 1417
AzureIoTClient 26:ee14eed604f6 1418 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 1419 {
AzureIoTClient 26:ee14eed604f6 1420 (void)response;
AzureIoTClient 26:ee14eed604f6 1421 (void)response_size;
AzureIoTClient 26:ee14eed604f6 1422 (void)status_response;
AzureIoTClient 26:ee14eed604f6 1423 (void)methodId;
AzureIoTClient 26:ee14eed604f6 1424 int result;
AzureIoTClient 30:20a85b733111 1425 AMQP_TRANSPORT_DEVICE_INSTANCE* device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 26:ee14eed604f6 1426 if (device_state != NULL)
AzureIoTClient 26:ee14eed604f6 1427 {
AzureIoTClient 26:ee14eed604f6 1428 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 26:ee14eed604f6 1429 IOTHUBTRANSPORT_AMQP_METHOD_HANDLE saved_handle = (IOTHUBTRANSPORT_AMQP_METHOD_HANDLE)methodId;
AzureIoTClient 26:ee14eed604f6 1430 /* 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 1431 if (iothubtransportamqp_methods_respond(saved_handle, response, response_size, status_response) != 0)
AzureIoTClient 26:ee14eed604f6 1432 {
AzureIoTClient 26:ee14eed604f6 1433 /* 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 1434 LogError("iothubtransportamqp_methods_respond failed");
AzureIoTClient 29:7e8852b14e3b 1435 result = __FAILURE__;
AzureIoTClient 26:ee14eed604f6 1436 }
AzureIoTClient 26:ee14eed604f6 1437 else
AzureIoTClient 26:ee14eed604f6 1438 {
AzureIoTClient 26:ee14eed604f6 1439 result = 0;
AzureIoTClient 26:ee14eed604f6 1440 }
AzureIoTClient 26:ee14eed604f6 1441 #else
AzureIoTClient 26:ee14eed604f6 1442 result = 0;
AzureIoTClient 26:ee14eed604f6 1443 LogError("Not implemented");
AzureIoTClient 26:ee14eed604f6 1444 #endif
AzureIoTClient 26:ee14eed604f6 1445 }
AzureIoTClient 26:ee14eed604f6 1446 else
AzureIoTClient 26:ee14eed604f6 1447 {
AzureIoTClient 29:7e8852b14e3b 1448 result = __FAILURE__;
AzureIoTClient 26:ee14eed604f6 1449 }
AzureIoTClient 26:ee14eed604f6 1450 return result;
AzureIoTClient 26:ee14eed604f6 1451 }
AzureIoTClient 26:ee14eed604f6 1452
AzureIoTClient 25:63f7d371c030 1453 IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 25:63f7d371c030 1454 {
AzureIoTClient 25:63f7d371c030 1455 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 25:63f7d371c030 1456
AzureIoTClient 30:20a85b733111 1457 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_096: [If `handle` or `iotHubClientStatus` are NULL, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG]
AzureIoTClient 30:20a85b733111 1458 if (handle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 25:63f7d371c030 1459 {
AzureIoTClient 25:63f7d371c030 1460 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 30:20a85b733111 1461 LogError("Failed retrieving the device send status (either handle (%p) or iotHubClientStatus (%p) are NULL)", handle, iotHubClientStatus);
AzureIoTClient 25:63f7d371c030 1462 }
AzureIoTClient 25:63f7d371c030 1463 else
AzureIoTClient 25:63f7d371c030 1464 {
AzureIoTClient 30:20a85b733111 1465 AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_state = (AMQP_TRANSPORT_DEVICE_INSTANCE*)handle;
AzureIoTClient 25:63f7d371c030 1466
AzureIoTClient 30:20a85b733111 1467 DEVICE_SEND_STATUS device_send_status;
AzureIoTClient 30:20a85b733111 1468 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_097: [IoTHubTransport_AMQP_Common_GetSendStatus shall invoke device_get_send_status()]
AzureIoTClient 30:20a85b733111 1469 if (device_get_send_status(amqp_device_state->device_handle, &device_send_status) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1470 {
AzureIoTClient 30:20a85b733111 1471 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_098: [If device_get_send_status() fails, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_ERROR]
AzureIoTClient 30:20a85b733111 1472 LogError("Failed retrieving the device send status (device_get_send_status failed)");
AzureIoTClient 30:20a85b733111 1473 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1474 }
AzureIoTClient 30:20a85b733111 1475 else
AzureIoTClient 30:20a85b733111 1476 {
AzureIoTClient 30:20a85b733111 1477 // 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 30:20a85b733111 1478 if (device_send_status == DEVICE_SEND_STATUS_BUSY)
AzureIoTClient 30:20a85b733111 1479 {
AzureIoTClient 30:20a85b733111 1480 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 30:20a85b733111 1481 }
AzureIoTClient 30:20a85b733111 1482 // 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 30:20a85b733111 1483 else // DEVICE_SEND_STATUS_IDLE
AzureIoTClient 30:20a85b733111 1484 {
AzureIoTClient 30:20a85b733111 1485 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 30:20a85b733111 1486 }
AzureIoTClient 25:63f7d371c030 1487
AzureIoTClient 30:20a85b733111 1488 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_109: [If no failures occur, IoTHubTransport_AMQP_Common_GetSendStatus shall return IOTHUB_CLIENT_OK]
AzureIoTClient 30:20a85b733111 1489 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1490 }
AzureIoTClient 25:63f7d371c030 1491 }
AzureIoTClient 25:63f7d371c030 1492
AzureIoTClient 25:63f7d371c030 1493 return result;
AzureIoTClient 25:63f7d371c030 1494 }
AzureIoTClient 25:63f7d371c030 1495
AzureIoTClient 25:63f7d371c030 1496 IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
AzureIoTClient 25:63f7d371c030 1497 {
AzureIoTClient 25:63f7d371c030 1498 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 25:63f7d371c030 1499
AzureIoTClient 30:20a85b733111 1500 // 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 1501 if ((handle == NULL) || (option == NULL) || (value == NULL))
AzureIoTClient 25:63f7d371c030 1502 {
AzureIoTClient 30:20a85b733111 1503 LogError("Invalid parameter (NULL) passed to AMQP transport SetOption (handle=%p, options=%p, value=%p)", handle, option, value);
AzureIoTClient 30:20a85b733111 1504 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 25:63f7d371c030 1505 }
AzureIoTClient 25:63f7d371c030 1506 else
AzureIoTClient 25:63f7d371c030 1507 {
AzureIoTClient 30:20a85b733111 1508 AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 30:20a85b733111 1509 bool is_device_specific_option;
AzureIoTClient 25:63f7d371c030 1510
AzureIoTClient 30:20a85b733111 1511 // 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 30:20a85b733111 1512 if (strcmp(OPTION_SAS_TOKEN_LIFETIME, option) == 0)
AzureIoTClient 30:20a85b733111 1513 {
AzureIoTClient 30:20a85b733111 1514 is_device_specific_option = true;
AzureIoTClient 30:20a85b733111 1515 transport_instance->option_sas_token_lifetime_secs = *(size_t*)value;
AzureIoTClient 30:20a85b733111 1516 }
AzureIoTClient 30:20a85b733111 1517 else if (strcmp(OPTION_SAS_TOKEN_REFRESH_TIME, option) == 0)
AzureIoTClient 30:20a85b733111 1518 {
AzureIoTClient 30:20a85b733111 1519 is_device_specific_option = true;
AzureIoTClient 30:20a85b733111 1520 transport_instance->option_sas_token_refresh_time_secs = *(size_t*)value;
AzureIoTClient 30:20a85b733111 1521 }
AzureIoTClient 30:20a85b733111 1522 else if (strcmp(OPTION_CBS_REQUEST_TIMEOUT, option) == 0)
AzureIoTClient 30:20a85b733111 1523 {
AzureIoTClient 30:20a85b733111 1524 is_device_specific_option = true;
AzureIoTClient 30:20a85b733111 1525 transport_instance->option_cbs_request_timeout_secs = *(size_t*)value;
AzureIoTClient 30:20a85b733111 1526 }
AzureIoTClient 30:20a85b733111 1527 else if (strcmp(OPTION_EVENT_SEND_TIMEOUT_SECS, option) == 0)
AzureIoTClient 30:20a85b733111 1528 {
AzureIoTClient 30:20a85b733111 1529 is_device_specific_option = true;
AzureIoTClient 30:20a85b733111 1530 transport_instance->option_send_event_timeout_secs = *(size_t*)value;
AzureIoTClient 30:20a85b733111 1531 }
AzureIoTClient 30:20a85b733111 1532 else
AzureIoTClient 30:20a85b733111 1533 {
AzureIoTClient 30:20a85b733111 1534 is_device_specific_option = false;
AzureIoTClient 30:20a85b733111 1535 }
AzureIoTClient 25:63f7d371c030 1536
AzureIoTClient 30:20a85b733111 1537 if (is_device_specific_option)
AzureIoTClient 30:20a85b733111 1538 {
AzureIoTClient 30:20a85b733111 1539 if (IoTHubTransport_AMQP_Common_Device_SetOption(handle, option, (void*)value) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1540 {
AzureIoTClient 30:20a85b733111 1541 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_103: [If device_set_option() fails, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_ERROR]
AzureIoTClient 30:20a85b733111 1542 LogError("transport failed setting option '%s' (failed setting option on one or more registered devices)", option);
AzureIoTClient 30:20a85b733111 1543 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1544 }
AzureIoTClient 30:20a85b733111 1545 else
AzureIoTClient 30:20a85b733111 1546 {
AzureIoTClient 30:20a85b733111 1547 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1548 }
AzureIoTClient 30:20a85b733111 1549 }
AzureIoTClient 30:20a85b733111 1550 // 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 30:20a85b733111 1551 else if (strcmp(OPTION_LOG_TRACE, option) == 0)
AzureIoTClient 30:20a85b733111 1552 {
AzureIoTClient 30:20a85b733111 1553 transport_instance->is_trace_on = *((bool*)value);
AzureIoTClient 25:63f7d371c030 1554
AzureIoTClient 30:20a85b733111 1555 if (transport_instance->amqp_connection != NULL &&
AzureIoTClient 30:20a85b733111 1556 amqp_connection_set_logging(transport_instance->amqp_connection, transport_instance->is_trace_on) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1557 {
AzureIoTClient 30:20a85b733111 1558 LogError("transport failed setting option '%s' (amqp_connection_set_logging failed)", option);
AzureIoTClient 30:20a85b733111 1559 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1560 }
AzureIoTClient 30:20a85b733111 1561 else
AzureIoTClient 30:20a85b733111 1562 {
AzureIoTClient 30:20a85b733111 1563 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1564 }
AzureIoTClient 30:20a85b733111 1565 }
AzureIoTClient 30:20a85b733111 1566 else
AzureIoTClient 30:20a85b733111 1567 {
AzureIoTClient 30:20a85b733111 1568 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1569
AzureIoTClient 30:20a85b733111 1570 // 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 30:20a85b733111 1571 // 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 30:20a85b733111 1572 if (strcmp(OPTION_X509_CERT, option) == 0 || strcmp(OPTION_X509_PRIVATE_KEY, option) == 0)
AzureIoTClient 30:20a85b733111 1573 {
AzureIoTClient 30:20a85b733111 1574 if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET)
AzureIoTClient 30:20a85b733111 1575 {
AzureIoTClient 30:20a85b733111 1576 transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_X509;
AzureIoTClient 30:20a85b733111 1577 }
AzureIoTClient 30:20a85b733111 1578 else if (transport_instance->preferred_authentication_mode != AMQP_TRANSPORT_AUTHENTICATION_MODE_X509)
AzureIoTClient 30:20a85b733111 1579 {
AzureIoTClient 30:20a85b733111 1580 LogError("transport failed setting option '%s' (preferred authentication method is not x509)", option);
AzureIoTClient 30:20a85b733111 1581 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 30:20a85b733111 1582 }
AzureIoTClient 30:20a85b733111 1583 }
AzureIoTClient 25:63f7d371c030 1584
AzureIoTClient 30:20a85b733111 1585 // 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 30:20a85b733111 1586 if (result != IOTHUB_CLIENT_INVALID_ARG)
AzureIoTClient 30:20a85b733111 1587 {
AzureIoTClient 30:20a85b733111 1588 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_106: [If `instance->tls_io` is NULL, it shall be set invoking instance->underlying_io_transport_provider()]
AzureIoTClient 30:20a85b733111 1589 if (transport_instance->tls_io == NULL &&
AzureIoTClient 30:20a85b733111 1590 get_new_underlying_io_transport(transport_instance, &transport_instance->tls_io) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1591 {
AzureIoTClient 30:20a85b733111 1592 // 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 30:20a85b733111 1593 LogError("transport failed setting option '%s' (failed to obtain a TLS I/O transport).", option);
AzureIoTClient 30:20a85b733111 1594 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1595 }
AzureIoTClient 30:20a85b733111 1596 // 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 30:20a85b733111 1597 else if (xio_setoption(transport_instance->tls_io, option, value) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1598 {
AzureIoTClient 30:20a85b733111 1599 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_001: [If xio_setoption fails, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_ERROR.]
AzureIoTClient 30:20a85b733111 1600 LogError("transport failed setting option '%s' (xio_setoption failed)", option);
AzureIoTClient 30:20a85b733111 1601 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1602 }
AzureIoTClient 30:20a85b733111 1603 else
AzureIoTClient 30:20a85b733111 1604 {
AzureIoTClient 30:20a85b733111 1605 if (save_underlying_io_transport_options(transport_instance) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1606 {
AzureIoTClient 30:20a85b733111 1607 LogError("IoTHubTransport_AMQP_Common_SetOption failed to save underlying I/O options; failure will be ignored");
AzureIoTClient 30:20a85b733111 1608 }
AzureIoTClient 25:63f7d371c030 1609
AzureIoTClient 30:20a85b733111 1610 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_001: [If no failures occur, IoTHubTransport_AMQP_Common_SetOption shall return IOTHUB_CLIENT_OK.]
AzureIoTClient 30:20a85b733111 1611 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1612 }
AzureIoTClient 30:20a85b733111 1613 }
AzureIoTClient 30:20a85b733111 1614 }
AzureIoTClient 30:20a85b733111 1615 }
AzureIoTClient 25:63f7d371c030 1616
AzureIoTClient 25:63f7d371c030 1617 return result;
AzureIoTClient 25:63f7d371c030 1618 }
AzureIoTClient 25:63f7d371c030 1619
AzureIoTClient 25:63f7d371c030 1620 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 1621 {
AzureIoTClient 25:63f7d371c030 1622 #ifdef NO_LOGGING
AzureIoTClient 25:63f7d371c030 1623 UNUSED(iotHubClientHandle);
AzureIoTClient 25:63f7d371c030 1624 #endif
AzureIoTClient 25:63f7d371c030 1625
AzureIoTClient 30:20a85b733111 1626 IOTHUB_DEVICE_HANDLE result;
AzureIoTClient 30:20a85b733111 1627
AzureIoTClient 30:20a85b733111 1628 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_17_005: [If `handle`, `device`, `iotHubClientHandle` or `waitingToSend` is NULL, IoTHubTransport_AMQP_Common_Register shall return NULL]
AzureIoTClient 30:20a85b733111 1629 if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL) || (iotHubClientHandle == NULL))
AzureIoTClient 25:63f7d371c030 1630 {
AzureIoTClient 30:20a85b733111 1631 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 1632 handle, device, iotHubClientHandle, waitingToSend);
AzureIoTClient 30:20a85b733111 1633 result = NULL;
AzureIoTClient 25:63f7d371c030 1634 }
AzureIoTClient 30:20a85b733111 1635 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_03_002: [IoTHubTransport_AMQP_Common_Register shall return NULL if `device->deviceId` is NULL.]
AzureIoTClient 30:20a85b733111 1636 else if (device->deviceId == NULL)
AzureIoTClient 30:20a85b733111 1637 {
AzureIoTClient 30:20a85b733111 1638 LogError("Transport failed to register device (device_id provided is NULL)");
AzureIoTClient 30:20a85b733111 1639 result = NULL;
AzureIoTClient 30:20a85b733111 1640 }
AzureIoTClient 26:ee14eed604f6 1641 else
AzureIoTClient 26:ee14eed604f6 1642 {
AzureIoTClient 30:20a85b733111 1643 LIST_ITEM_HANDLE list_item;
AzureIoTClient 30:20a85b733111 1644 AMQP_TRANSPORT_INSTANCE* transport_instance = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 25:63f7d371c030 1645
AzureIoTClient 30:20a85b733111 1646 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_064: [If the device is already registered, IoTHubTransport_AMQP_Common_Register shall fail and return NULL.]
AzureIoTClient 30:20a85b733111 1647 if (is_device_registered_ex(transport_instance->registered_devices, device->deviceId, &list_item))
AzureIoTClient 30:20a85b733111 1648 {
AzureIoTClient 30:20a85b733111 1649 LogError("IoTHubTransport_AMQP_Common_Register failed (device '%s' already registered on this transport instance)", device->deviceId);
AzureIoTClient 30:20a85b733111 1650 result = NULL;
AzureIoTClient 30:20a85b733111 1651 }
AzureIoTClient 30:20a85b733111 1652 // 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 30:20a85b733111 1653 else if (!is_device_credential_acceptable(device, transport_instance->preferred_authentication_mode))
AzureIoTClient 26:ee14eed604f6 1654 {
AzureIoTClient 30:20a85b733111 1655 LogError("Transport failed to register device '%s' (device credential was not accepted)", device->deviceId);
AzureIoTClient 30:20a85b733111 1656 result = NULL;
AzureIoTClient 30:20a85b733111 1657 }
AzureIoTClient 26:ee14eed604f6 1658 else
AzureIoTClient 26:ee14eed604f6 1659 {
AzureIoTClient 30:20a85b733111 1660 AMQP_TRANSPORT_DEVICE_INSTANCE* amqp_device_instance;
AzureIoTClient 25:63f7d371c030 1661
AzureIoTClient 30:20a85b733111 1662 // 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 1663 if ((amqp_device_instance = (AMQP_TRANSPORT_DEVICE_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_DEVICE_INSTANCE))) == NULL)
AzureIoTClient 26:ee14eed604f6 1664 {
AzureIoTClient 30:20a85b733111 1665 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_067: [If malloc fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL.]
AzureIoTClient 30:20a85b733111 1666 LogError("Transport failed to register device '%s' (failed to create the device state instance; malloc failed)", device->deviceId);
AzureIoTClient 30:20a85b733111 1667 result = NULL;
AzureIoTClient 30:20a85b733111 1668 }
AzureIoTClient 26:ee14eed604f6 1669 else
AzureIoTClient 26:ee14eed604f6 1670 {
AzureIoTClient 30:20a85b733111 1671 memset(amqp_device_instance, 0, sizeof(AMQP_TRANSPORT_DEVICE_INSTANCE));
AzureIoTClient 30:20a85b733111 1672
AzureIoTClient 30:20a85b733111 1673 // 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 30:20a85b733111 1674 amqp_device_instance->iothub_client_handle = iotHubClientHandle;
AzureIoTClient 30:20a85b733111 1675 amqp_device_instance->transport_instance = transport_instance;
AzureIoTClient 30:20a85b733111 1676 amqp_device_instance->waiting_to_send = waitingToSend;
AzureIoTClient 30:20a85b733111 1677 amqp_device_instance->device_state = DEVICE_STATE_STOPPED;
AzureIoTClient 30:20a85b733111 1678 amqp_device_instance->max_state_change_timeout_secs = DEFAULT_DEVICE_STATE_CHANGE_TIMEOUT_SECS;
AzureIoTClient 30:20a85b733111 1679
AzureIoTClient 25:63f7d371c030 1680 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 1681 amqp_device_instance->subscribe_methods_needed = false;
AzureIoTClient 30:20a85b733111 1682 amqp_device_instance->subscribed_for_methods = false;
AzureIoTClient 25:63f7d371c030 1683 #endif
AzureIoTClient 30:20a85b733111 1684 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_069: [A copy of `config->deviceId` shall be saved into `device_state->device_id`]
AzureIoTClient 30:20a85b733111 1685 if ((amqp_device_instance->device_id = STRING_construct(device->deviceId)) == NULL)
AzureIoTClient 26:ee14eed604f6 1686 {
AzureIoTClient 30:20a85b733111 1687 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_070: [If STRING_construct() fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL]
AzureIoTClient 30:20a85b733111 1688 LogError("Transport failed to register device '%s' (failed to copy the deviceId)", device->deviceId);
AzureIoTClient 30:20a85b733111 1689 result = NULL;
AzureIoTClient 30:20a85b733111 1690 }
AzureIoTClient 26:ee14eed604f6 1691 else
AzureIoTClient 26:ee14eed604f6 1692 {
AzureIoTClient 30:20a85b733111 1693 DEVICE_CONFIG device_config;
AzureIoTClient 30:20a85b733111 1694 memset(&device_config, 0, sizeof(DEVICE_CONFIG));
AzureIoTClient 30:20a85b733111 1695 device_config.device_id = (char*)device->deviceId;
AzureIoTClient 30:20a85b733111 1696 device_config.iothub_host_fqdn = (char*)STRING_c_str(transport_instance->iothub_host_fqdn);
AzureIoTClient 30:20a85b733111 1697 device_config.device_primary_key = (char*)device->deviceKey;
AzureIoTClient 30:20a85b733111 1698 device_config.device_sas_token = (char*)device->deviceSasToken;
AzureIoTClient 30:20a85b733111 1699 // 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 30:20a85b733111 1700 device_config.authentication_mode = (device->deviceKey != NULL || device->deviceSasToken != NULL ? DEVICE_AUTH_MODE_CBS : DEVICE_AUTH_MODE_X509);
AzureIoTClient 30:20a85b733111 1701 device_config.on_state_changed_callback = on_device_state_changed_callback;
AzureIoTClient 30:20a85b733111 1702 device_config.on_state_changed_context = amqp_device_instance;
AzureIoTClient 30:20a85b733111 1703
AzureIoTClient 30:20a85b733111 1704 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_071: [`amqp_device_instance->device_handle` shall be set using device_create()]
AzureIoTClient 30:20a85b733111 1705 if ((amqp_device_instance->device_handle = device_create(&device_config)) == NULL)
AzureIoTClient 30:20a85b733111 1706 {
AzureIoTClient 30:20a85b733111 1707 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_073: [If device_create() fails, IoTHubTransport_AMQP_Common_Register shall fail and return NULL]
AzureIoTClient 30:20a85b733111 1708 LogError("Transport failed to register device '%s' (failed to create the DEVICE_HANDLE instance)", device->deviceId);
AzureIoTClient 30:20a85b733111 1709 result = NULL;
AzureIoTClient 30:20a85b733111 1710 }
AzureIoTClient 30:20a85b733111 1711 else
AzureIoTClient 30:20a85b733111 1712 {
AzureIoTClient 30:20a85b733111 1713 bool is_first_device_being_registered = (singlylinkedlist_get_head_item(transport_instance->registered_devices) == NULL);
AzureIoTClient 30:20a85b733111 1714
AzureIoTClient 25:63f7d371c030 1715 #ifdef WIP_C2D_METHODS_AMQP /* This feature is WIP, do not use yet */
AzureIoTClient 30:20a85b733111 1716 /* 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 30:20a85b733111 1717 amqp_device_instance->methods_handle = iothubtransportamqp_methods_create(STRING_c_str(transport_instance->iothub_host_fqdn), device->deviceId);
AzureIoTClient 30:20a85b733111 1718 if (amqp_device_instance->methods_handle == NULL)
AzureIoTClient 30:20a85b733111 1719 {
AzureIoTClient 30:20a85b733111 1720 /* Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_011: [ If `iothubtransportamqp_methods_create` fails, `IoTHubTransport_AMQP_Common_Create` shall fail and return NULL. ]*/
AzureIoTClient 30:20a85b733111 1721 LogError("Transport failed to register device '%s' (Cannot create the methods module)", device->deviceId);
AzureIoTClient 30:20a85b733111 1722 result = NULL;
AzureIoTClient 30:20a85b733111 1723 }
AzureIoTClient 30:20a85b733111 1724 else
AzureIoTClient 25:63f7d371c030 1725 #endif
AzureIoTClient 30:20a85b733111 1726 if (replicate_device_options_to(amqp_device_instance, device_config.authentication_mode) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1727 {
AzureIoTClient 30:20a85b733111 1728 LogError("Transport failed to register device '%s' (failed to replicate options)", device->deviceId);
AzureIoTClient 30:20a85b733111 1729 result = NULL;
AzureIoTClient 30:20a85b733111 1730 }
AzureIoTClient 30:20a85b733111 1731 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_074: [IoTHubTransport_AMQP_Common_Register shall add the `amqp_device_instance` to `instance->registered_devices`]
AzureIoTClient 30:20a85b733111 1732 else if (singlylinkedlist_add(transport_instance->registered_devices, amqp_device_instance) == NULL)
AzureIoTClient 30:20a85b733111 1733 {
AzureIoTClient 30:20a85b733111 1734 // 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 30:20a85b733111 1735 LogError("Transport failed to register device '%s' (singlylinkedlist_add failed)", device->deviceId);
AzureIoTClient 30:20a85b733111 1736 result = NULL;
AzureIoTClient 30:20a85b733111 1737 }
AzureIoTClient 30:20a85b733111 1738 else
AzureIoTClient 30:20a85b733111 1739 {
AzureIoTClient 30:20a85b733111 1740 // 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 30:20a85b733111 1741 if (transport_instance->preferred_authentication_mode == AMQP_TRANSPORT_AUTHENTICATION_MODE_NOT_SET &&
AzureIoTClient 30:20a85b733111 1742 is_first_device_being_registered)
AzureIoTClient 30:20a85b733111 1743 {
AzureIoTClient 30:20a85b733111 1744 if (device_config.authentication_mode == DEVICE_AUTH_MODE_CBS)
AzureIoTClient 30:20a85b733111 1745 {
AzureIoTClient 30:20a85b733111 1746 transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_CBS;
AzureIoTClient 30:20a85b733111 1747 }
AzureIoTClient 30:20a85b733111 1748 else
AzureIoTClient 30:20a85b733111 1749 {
AzureIoTClient 30:20a85b733111 1750 transport_instance->preferred_authentication_mode = AMQP_TRANSPORT_AUTHENTICATION_MODE_X509;
AzureIoTClient 30:20a85b733111 1751 }
AzureIoTClient 30:20a85b733111 1752 }
AzureIoTClient 25:63f7d371c030 1753
AzureIoTClient 30:20a85b733111 1754 // 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 30:20a85b733111 1755 result = (IOTHUB_DEVICE_HANDLE)amqp_device_instance;
AzureIoTClient 30:20a85b733111 1756 }
AzureIoTClient 30:20a85b733111 1757 }
AzureIoTClient 26:ee14eed604f6 1758 }
AzureIoTClient 25:63f7d371c030 1759
AzureIoTClient 30:20a85b733111 1760 if (result == NULL)
AzureIoTClient 26:ee14eed604f6 1761 {
AzureIoTClient 30:20a85b733111 1762 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_077: [If IoTHubTransport_AMQP_Common_Register fails, it shall free all memory it allocated]
AzureIoTClient 30:20a85b733111 1763 internal_destroy_amqp_device_instance(amqp_device_instance);
AzureIoTClient 30:20a85b733111 1764 }
AzureIoTClient 26:ee14eed604f6 1765 }
AzureIoTClient 26:ee14eed604f6 1766 }
AzureIoTClient 26:ee14eed604f6 1767 }
AzureIoTClient 25:63f7d371c030 1768
AzureIoTClient 26:ee14eed604f6 1769 return result;
AzureIoTClient 25:63f7d371c030 1770 }
AzureIoTClient 25:63f7d371c030 1771
AzureIoTClient 25:63f7d371c030 1772 void IoTHubTransport_AMQP_Common_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
AzureIoTClient 25:63f7d371c030 1773 {
AzureIoTClient 30:20a85b733111 1774 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_079: [if `deviceHandle` provided is NULL, IoTHubTransport_AMQP_Common_Unregister shall return.]
AzureIoTClient 26:ee14eed604f6 1775 if (deviceHandle == NULL)
AzureIoTClient 26:ee14eed604f6 1776 {
AzureIoTClient 30:20a85b733111 1777 LogError("Failed to unregister device (deviceHandle is NULL).");
AzureIoTClient 26:ee14eed604f6 1778 }
AzureIoTClient 26:ee14eed604f6 1779 else
AzureIoTClient 26:ee14eed604f6 1780 {
AzureIoTClient 30:20a85b733111 1781 AMQP_TRANSPORT_DEVICE_INSTANCE* registered_device = (AMQP_TRANSPORT_DEVICE_INSTANCE*)deviceHandle;
AzureIoTClient 30:20a85b733111 1782 const char* device_id;
AzureIoTClient 30:20a85b733111 1783 LIST_ITEM_HANDLE list_item;
AzureIoTClient 25:63f7d371c030 1784
AzureIoTClient 30:20a85b733111 1785 if ((device_id = STRING_c_str(registered_device->device_id)) == NULL)
AzureIoTClient 30:20a85b733111 1786 {
AzureIoTClient 30:20a85b733111 1787 LogError("Failed to unregister device (failed to get device id char ptr)");
AzureIoTClient 30:20a85b733111 1788 }
AzureIoTClient 30:20a85b733111 1789 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_080: [if `deviceHandle` has a NULL reference to its transport instance, IoTHubTransport_AMQP_Common_Unregister shall return.]
AzureIoTClient 30:20a85b733111 1790 else if (registered_device->transport_instance == NULL)
AzureIoTClient 30:20a85b733111 1791 {
AzureIoTClient 30:20a85b733111 1792 LogError("Failed to unregister device '%s' (deviceHandle does not have a transport state associated to).", device_id);
AzureIoTClient 30:20a85b733111 1793 }
AzureIoTClient 30:20a85b733111 1794 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_081: [If the device is not registered with this transport, IoTHubTransport_AMQP_Common_Unregister shall return]
AzureIoTClient 30:20a85b733111 1795 else if (!is_device_registered_ex(registered_device->transport_instance->registered_devices, device_id, &list_item))
AzureIoTClient 30:20a85b733111 1796 {
AzureIoTClient 30:20a85b733111 1797 LogError("Failed to unregister device '%s' (device is not registered within this transport).", device_id);
AzureIoTClient 30:20a85b733111 1798 }
AzureIoTClient 30:20a85b733111 1799 else
AzureIoTClient 30:20a85b733111 1800 {
AzureIoTClient 30:20a85b733111 1801 // Removing it first so the race hazzard is reduced between this function and DoWork. Best would be to use locks.
AzureIoTClient 30:20a85b733111 1802 if (singlylinkedlist_remove(registered_device->transport_instance->registered_devices, list_item) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1803 {
AzureIoTClient 30:20a85b733111 1804 LogError("Failed to unregister device '%s' (singlylinkedlist_remove failed).", device_id);
AzureIoTClient 30:20a85b733111 1805 }
AzureIoTClient 30:20a85b733111 1806 else
AzureIoTClient 30:20a85b733111 1807 {
AzureIoTClient 30:20a85b733111 1808 // TODO: Q: should we go through waiting_to_send list and raise on_event_send_complete with BECAUSE_DESTROY ?
AzureIoTClient 25:63f7d371c030 1809
AzureIoTClient 30:20a85b733111 1810 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_01_012: [IoTHubTransport_AMQP_Common_Unregister shall destroy the C2D methods handler by calling iothubtransportamqp_methods_destroy]
AzureIoTClient 30:20a85b733111 1811 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_083: [IoTHubTransport_AMQP_Common_Unregister shall free all the memory allocated for the `device_instance`]
AzureIoTClient 30:20a85b733111 1812 internal_destroy_amqp_device_instance(registered_device);
AzureIoTClient 30:20a85b733111 1813 }
AzureIoTClient 30:20a85b733111 1814 }
AzureIoTClient 26:ee14eed604f6 1815 }
AzureIoTClient 25:63f7d371c030 1816 }
AzureIoTClient 25:63f7d371c030 1817
AzureIoTClient 25:63f7d371c030 1818 void IoTHubTransport_AMQP_Common_Destroy(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1819 {
AzureIoTClient 30:20a85b733111 1820 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_013: [If `handle` is NULL, IoTHubTransport_AMQP_Common_Destroy shall return immediatelly]
AzureIoTClient 30:20a85b733111 1821 if (handle == NULL)
AzureIoTClient 30:20a85b733111 1822 {
AzureIoTClient 30:20a85b733111 1823 LogError("Failed to destroy AMQP transport instance (handle is NULL)");
AzureIoTClient 30:20a85b733111 1824 }
AzureIoTClient 30:20a85b733111 1825 else
AzureIoTClient 30:20a85b733111 1826 {
AzureIoTClient 30:20a85b733111 1827 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_014: [IoTHubTransport_AMQP_Common_Destroy shall invoke IoTHubTransport_AMQP_Common_Unregister on each of its registered devices.]
AzureIoTClient 30:20a85b733111 1828 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_015: [All members of `instance` (including tls_io) shall be destroyed and its memory released]
AzureIoTClient 30:20a85b733111 1829 internal_destroy_instance((AMQP_TRANSPORT_INSTANCE*)handle);
AzureIoTClient 26:ee14eed604f6 1830 }
AzureIoTClient 25:63f7d371c030 1831 }
AzureIoTClient 25:63f7d371c030 1832
AzureIoTClient 25:63f7d371c030 1833 int IoTHubTransport_AMQP_Common_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 25:63f7d371c030 1834 {
AzureIoTClient 25:63f7d371c030 1835 int result;
AzureIoTClient 25:63f7d371c030 1836 (void)handle;
AzureIoTClient 25:63f7d371c030 1837 (void)retryPolicy;
AzureIoTClient 25:63f7d371c030 1838 (void)retryTimeoutLimitInSeconds;
AzureIoTClient 25:63f7d371c030 1839
AzureIoTClient 25:63f7d371c030 1840 /* Retry Policy is currently not available for AMQP */
AzureIoTClient 25:63f7d371c030 1841
AzureIoTClient 25:63f7d371c030 1842 result = 0;
AzureIoTClient 25:63f7d371c030 1843 return result;
AzureIoTClient 25:63f7d371c030 1844 }
AzureIoTClient 25:63f7d371c030 1845
AzureIoTClient 25:63f7d371c030 1846 STRING_HANDLE IoTHubTransport_AMQP_Common_GetHostname(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 25:63f7d371c030 1847 {
AzureIoTClient 25:63f7d371c030 1848 STRING_HANDLE result;
AzureIoTClient 30:20a85b733111 1849
AzureIoTClient 30:20a85b733111 1850 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_001: [If `handle` is NULL, `IoTHubTransport_AMQP_Common_GetHostname` shall return NULL.]
AzureIoTClient 30:20a85b733111 1851 if (handle == NULL)
AzureIoTClient 25:63f7d371c030 1852 {
AzureIoTClient 30:20a85b733111 1853 LogError("Cannot provide the target host name (transport handle is NULL).");
AzureIoTClient 30:20a85b733111 1854
AzureIoTClient 30:20a85b733111 1855 result = NULL;
AzureIoTClient 30:20a85b733111 1856 }
AzureIoTClient 30:20a85b733111 1857 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_02_002: [IoTHubTransport_AMQP_Common_GetHostname shall return a copy of `instance->iothub_target_fqdn`.]
AzureIoTClient 30:20a85b733111 1858 else if ((result = STRING_clone(((AMQP_TRANSPORT_INSTANCE*)(handle))->iothub_host_fqdn)) == NULL)
AzureIoTClient 30:20a85b733111 1859 {
AzureIoTClient 30:20a85b733111 1860 LogError("Cannot provide the target host name (STRING_clone failed).");
AzureIoTClient 30:20a85b733111 1861 }
AzureIoTClient 30:20a85b733111 1862
AzureIoTClient 30:20a85b733111 1863 return result;
AzureIoTClient 30:20a85b733111 1864 }
AzureIoTClient 30:20a85b733111 1865
AzureIoTClient 30:20a85b733111 1866 static DEVICE_MESSAGE_DISPOSITION_INFO* create_device_message_disposition_info_from(MESSAGE_CALLBACK_INFO* message_data)
AzureIoTClient 30:20a85b733111 1867 {
AzureIoTClient 30:20a85b733111 1868 DEVICE_MESSAGE_DISPOSITION_INFO* result;
AzureIoTClient 30:20a85b733111 1869
AzureIoTClient 30:20a85b733111 1870 if ((result = (DEVICE_MESSAGE_DISPOSITION_INFO*)malloc(sizeof(DEVICE_MESSAGE_DISPOSITION_INFO))) == NULL)
AzureIoTClient 30:20a85b733111 1871 {
AzureIoTClient 30:20a85b733111 1872 LogError("Failed creating DEVICE_MESSAGE_DISPOSITION_INFO (malloc failed)");
AzureIoTClient 30:20a85b733111 1873 }
AzureIoTClient 30:20a85b733111 1874 else if (mallocAndStrcpy_s(&result->source, message_data->transportContext->link_name) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1875 {
AzureIoTClient 30:20a85b733111 1876 LogError("Failed creating DEVICE_MESSAGE_DISPOSITION_INFO (mallocAndStrcpy_s failed)");
AzureIoTClient 30:20a85b733111 1877 free(result);
AzureIoTClient 30:20a85b733111 1878 result = NULL;
AzureIoTClient 30:20a85b733111 1879 }
AzureIoTClient 30:20a85b733111 1880 else
AzureIoTClient 30:20a85b733111 1881 {
AzureIoTClient 30:20a85b733111 1882 result->message_id = message_data->transportContext->message_id;
AzureIoTClient 30:20a85b733111 1883 }
AzureIoTClient 30:20a85b733111 1884
AzureIoTClient 30:20a85b733111 1885 return result;
AzureIoTClient 30:20a85b733111 1886 }
AzureIoTClient 30:20a85b733111 1887
AzureIoTClient 30:20a85b733111 1888 static void destroy_device_message_disposition_info(DEVICE_MESSAGE_DISPOSITION_INFO* device_message_disposition_info)
AzureIoTClient 30:20a85b733111 1889 {
AzureIoTClient 30:20a85b733111 1890 free(device_message_disposition_info->source);
AzureIoTClient 30:20a85b733111 1891 free(device_message_disposition_info);
AzureIoTClient 30:20a85b733111 1892 }
AzureIoTClient 30:20a85b733111 1893
AzureIoTClient 30:20a85b733111 1894 IOTHUB_CLIENT_RESULT IoTHubTransport_AMQP_Common_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
AzureIoTClient 30:20a85b733111 1895 {
AzureIoTClient 30:20a85b733111 1896 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 30:20a85b733111 1897 if (message_data == NULL)
AzureIoTClient 30:20a85b733111 1898 {
AzureIoTClient 30:20a85b733111 1899 /* 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 1900 LogError("Failed sending message disposition (message_data is NULL)");
AzureIoTClient 30:20a85b733111 1901 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 25:63f7d371c030 1902 }
AzureIoTClient 25:63f7d371c030 1903 else
AzureIoTClient 25:63f7d371c030 1904 {
AzureIoTClient 30:20a85b733111 1905 /* 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 1906 if (message_data->messageHandle == NULL || message_data->transportContext == NULL)
AzureIoTClient 30:20a85b733111 1907 {
AzureIoTClient 30:20a85b733111 1908 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 1909 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 30:20a85b733111 1910 }
AzureIoTClient 30:20a85b733111 1911 else
AzureIoTClient 30:20a85b733111 1912 {
AzureIoTClient 30:20a85b733111 1913 DEVICE_MESSAGE_DISPOSITION_INFO* device_message_disposition_info;
AzureIoTClient 30:20a85b733111 1914
AzureIoTClient 30:20a85b733111 1915 /* 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 30:20a85b733111 1916 DEVICE_MESSAGE_DISPOSITION_RESULT device_disposition_result = get_device_disposition_result_from(disposition);
AzureIoTClient 30:20a85b733111 1917
AzureIoTClient 30:20a85b733111 1918 // 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 30:20a85b733111 1919 if ((device_message_disposition_info = create_device_message_disposition_info_from(message_data)) == NULL)
AzureIoTClient 30:20a85b733111 1920 {
AzureIoTClient 30:20a85b733111 1921 // 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 30:20a85b733111 1922 LogError("Device '%s' failed sending message disposition (failed creating DEVICE_MESSAGE_DISPOSITION_RESULT)", STRING_c_str(message_data->transportContext->device_state->device_id));
AzureIoTClient 30:20a85b733111 1923 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1924 }
AzureIoTClient 30:20a85b733111 1925 else
AzureIoTClient 30:20a85b733111 1926 {
AzureIoTClient 30:20a85b733111 1927 /* 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 30:20a85b733111 1928 if (device_send_message_disposition(message_data->transportContext->device_state->device_handle, device_message_disposition_info, device_disposition_result) != RESULT_OK)
AzureIoTClient 30:20a85b733111 1929 {
AzureIoTClient 30:20a85b733111 1930 LogError("Device '%s' failed sending message disposition (device_send_message_disposition failed)", STRING_c_str(message_data->transportContext->device_state->device_id));
AzureIoTClient 30:20a85b733111 1931 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 30:20a85b733111 1932 }
AzureIoTClient 30:20a85b733111 1933 else
AzureIoTClient 30:20a85b733111 1934 {
AzureIoTClient 30:20a85b733111 1935 IoTHubMessage_Destroy(message_data->messageHandle);
AzureIoTClient 30:20a85b733111 1936 MESSAGE_CALLBACK_INFO_Destroy(message_data);
AzureIoTClient 30:20a85b733111 1937 result = IOTHUB_CLIENT_OK;
AzureIoTClient 30:20a85b733111 1938 }
AzureIoTClient 30:20a85b733111 1939
AzureIoTClient 30:20a85b733111 1940 // Codes_SRS_IOTHUBTRANSPORT_AMQP_COMMON_09_114: [`IoTHubTransport_AMQP_Common_SendMessageDisposition()` shall destroy the DEVICE_MESSAGE_DISPOSITION_INFO instance]
AzureIoTClient 30:20a85b733111 1941 destroy_device_message_disposition_info(device_message_disposition_info);
AzureIoTClient 30:20a85b733111 1942 }
AzureIoTClient 30:20a85b733111 1943 }
AzureIoTClient 25:63f7d371c030 1944 }
AzureIoTClient 30:20a85b733111 1945
AzureIoTClient 25:63f7d371c030 1946 return result;
AzureIoTClient 25:63f7d371c030 1947 }
AzureIoTClient 25:63f7d371c030 1948