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:
Mon May 09 14:36:31 2016 -0700
Revision:
14:8e8e42008807
Parent:
13:a4af7c301e02
Child:
15:d446b73a913d
1.0.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 4:57e049bce51e 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 4:57e049bce51e 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 4:57e049bce51e 3
AzureIoTClient 4:57e049bce51e 4 #include <stdlib.h>
AzureIoTClient 4:57e049bce51e 5 #ifdef _CRTDBG_MAP_ALLOC
AzureIoTClient 4:57e049bce51e 6 #include <crtdbg.h>
AzureIoTClient 4:57e049bce51e 7 #endif
Azure.IoT Build 10:75c5e0d8537d 8 #include <stdint.h>
Azure.IoT Build 10:75c5e0d8537d 9 #include <time.h>
Azure.IoT Build 10:75c5e0d8537d 10 #include <limits.h>
Azure.IoT Build 12:841a4c36bd36 11 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 12:841a4c36bd36 12 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 12:841a4c36bd36 13 #include "azure_c_shared_utility/doublylinkedlist.h"
Azure.IoT Build 12:841a4c36bd36 14 #include "azure_c_shared_utility/iot_logging.h"
Azure.IoT Build 12:841a4c36bd36 15 #include "azure_c_shared_utility/platform.h"
Azure.IoT Build 12:841a4c36bd36 16 #include "azure_c_shared_utility/sastoken.h"
Azure.IoT Build 12:841a4c36bd36 17 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 12:841a4c36bd36 18 #include "azure_c_shared_utility/urlencode.h"
Azure.IoT Build 12:841a4c36bd36 19 #include "azure_c_shared_utility/tlsio.h"
AzureIoTClient 4:57e049bce51e 20
Azure.IoT Build 12:841a4c36bd36 21 #include "azure_uamqp_c/cbs.h"
Azure.IoT Build 12:841a4c36bd36 22 #include "azure_uamqp_c/link.h"
Azure.IoT Build 12:841a4c36bd36 23 #include "azure_uamqp_c/message.h"
Azure.IoT Build 12:841a4c36bd36 24 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 12:841a4c36bd36 25 #include "azure_uamqp_c/message_receiver.h"
Azure.IoT Build 12:841a4c36bd36 26 #include "azure_uamqp_c/message_sender.h"
Azure.IoT Build 12:841a4c36bd36 27 #include "azure_uamqp_c/messaging.h"
Azure.IoT Build 12:841a4c36bd36 28 #include "azure_uamqp_c/sasl_mssbcbs.h"
Azure.IoT Build 12:841a4c36bd36 29 #include "azure_uamqp_c/saslclientio.h"
AzureIoTClient 4:57e049bce51e 30
AzureIoTClient 4:57e049bce51e 31 #include "iothub_client_ll.h"
AzureIoTClient 4:57e049bce51e 32 #include "iothub_client_private.h"
AzureIoTClient 4:57e049bce51e 33 #include "iothubtransportamqp.h"
Azure.IoT Build 7:07bc440836b3 34 #include "iothub_client_version.h"
Azure.IoT Build 7:07bc440836b3 35
Azure.IoT Build 7:07bc440836b3 36 #define RESULT_OK 0
Azure.IoT Build 7:07bc440836b3 37 #define RESULT_FAILURE 1
Azure.IoT Build 7:07bc440836b3 38 #define RESULT_TIMEOUT 2
Azure.IoT Build 7:07bc440836b3 39
Azure.IoT Build 7:07bc440836b3 40 #define RFC1035_MAX_FQDN_LENGTH 255
Azure.IoT Build 7:07bc440836b3 41 #define DEFAULT_IOTHUB_AMQP_PORT 5671
Azure.IoT Build 7:07bc440836b3 42 #define DEFAULT_SAS_TOKEN_LIFETIME_MS 3600000
Azure.IoT Build 7:07bc440836b3 43 #define DEFAULT_CBS_REQUEST_TIMEOUT_MS 30000
Azure.IoT Build 7:07bc440836b3 44 #define CBS_AUDIENCE "servicebus.windows.net:sastoken"
Azure.IoT Build 7:07bc440836b3 45 #define DEFAULT_CONTAINER_ID "default_container_id"
Azure.IoT Build 10:75c5e0d8537d 46 #define DEFAULT_INCOMING_WINDOW_SIZE UINT_MAX
Azure.IoT Build 7:07bc440836b3 47 #define DEFAULT_OUTGOING_WINDOW_SIZE 100
Azure.IoT Build 7:07bc440836b3 48 #define MESSAGE_RECEIVER_LINK_NAME "receiver-link"
Azure.IoT Build 7:07bc440836b3 49 #define MESSAGE_RECEIVER_TARGET_ADDRESS "ingress-rx"
Azure.IoT Build 7:07bc440836b3 50 #define MESSAGE_RECEIVER_MAX_LINK_SIZE 65536
Azure.IoT Build 7:07bc440836b3 51 #define MESSAGE_SENDER_LINK_NAME "sender-link"
Azure.IoT Build 7:07bc440836b3 52 #define MESSAGE_SENDER_SOURCE_ADDRESS "ingress"
Azure.IoT Build 11:62d7b956e76e 53 #define MESSAGE_SENDER_MAX_LINK_SIZE UINT64_MAX
Azure.IoT Build 7:07bc440836b3 54
Azure.IoT Build 11:62d7b956e76e 55 typedef XIO_HANDLE(*TLS_IO_TRANSPORT_PROVIDER)(const char* fqdn, int port);
Azure.IoT Build 7:07bc440836b3 56
Azure.IoT Build 7:07bc440836b3 57 typedef enum CBS_STATE_TAG
Azure.IoT Build 7:07bc440836b3 58 {
Azure.IoT Build 7:07bc440836b3 59 CBS_STATE_IDLE,
Azure.IoT Build 7:07bc440836b3 60 CBS_STATE_AUTH_IN_PROGRESS,
Azure.IoT Build 7:07bc440836b3 61 CBS_STATE_AUTHENTICATED
Azure.IoT Build 7:07bc440836b3 62 } CBS_STATE;
AzureIoTClient 4:57e049bce51e 63
Azure.IoT Build 7:07bc440836b3 64 typedef struct AMQP_TRANSPORT_STATE_TAG
Azure.IoT Build 7:07bc440836b3 65 {
Azure.IoT Build 10:75c5e0d8537d 66 // FQDN of the IoT Hub.
Azure.IoT Build 10:75c5e0d8537d 67 STRING_HANDLE iotHubHostFqdn;
Azure.IoT Build 10:75c5e0d8537d 68 // AMQP port of the IoT Hub.
Azure.IoT Build 10:75c5e0d8537d 69 int iotHubPort;
Azure.IoT Build 10:75c5e0d8537d 70 // Key associated to the device to be used.
Azure.IoT Build 10:75c5e0d8537d 71 STRING_HANDLE deviceKey;
AzureIoTClient 14:8e8e42008807 72 // SAS associated to the device to be used.
AzureIoTClient 14:8e8e42008807 73 STRING_HANDLE deviceSasToken;
Azure.IoT Build 10:75c5e0d8537d 74 // Address to which the transport will connect to and send events.
Azure.IoT Build 10:75c5e0d8537d 75 STRING_HANDLE targetAddress;
Azure.IoT Build 10:75c5e0d8537d 76 // Address to which the transport will connect to and receive messages from.
Azure.IoT Build 10:75c5e0d8537d 77 STRING_HANDLE messageReceiveAddress;
Azure.IoT Build 10:75c5e0d8537d 78 // A component of the SAS token. Currently this must be an empty string.
Azure.IoT Build 10:75c5e0d8537d 79 STRING_HANDLE sasTokenKeyName;
Azure.IoT Build 10:75c5e0d8537d 80 // Internal parameter that identifies the current logical device within the service.
Azure.IoT Build 10:75c5e0d8537d 81 STRING_HANDLE devicesPath;
Azure.IoT Build 10:75c5e0d8537d 82 // How long a SAS token created by the transport is valid, in milliseconds.
Azure.IoT Build 10:75c5e0d8537d 83 size_t sas_token_lifetime;
Azure.IoT Build 10:75c5e0d8537d 84 // Maximum period of time for the transport to wait before refreshing the SAS token it created previously, in milliseconds.
Azure.IoT Build 10:75c5e0d8537d 85 size_t sas_token_refresh_time;
Azure.IoT Build 10:75c5e0d8537d 86 // Maximum time the transport waits for uAMQP cbs_put_token() to complete before marking it a failure, in milliseconds.
Azure.IoT Build 10:75c5e0d8537d 87 size_t cbs_request_timeout;
Azure.IoT Build 10:75c5e0d8537d 88 // Maximum time for the connection establishment/retry logic should wait for a connection to succeed, in milliseconds.
Azure.IoT Build 10:75c5e0d8537d 89 size_t connection_timeout;
Azure.IoT Build 10:75c5e0d8537d 90 // Saved reference to the IoTHub LL Client.
Azure.IoT Build 10:75c5e0d8537d 91 IOTHUB_CLIENT_LL_HANDLE iothub_client_handle;
Azure.IoT Build 11:62d7b956e76e 92
Azure.IoT Build 10:75c5e0d8537d 93 // TSL I/O transport.
Azure.IoT Build 10:75c5e0d8537d 94 XIO_HANDLE tls_io;
Azure.IoT Build 10:75c5e0d8537d 95 // Pointer to the function that creates the TLS I/O (internal use only).
Azure.IoT Build 10:75c5e0d8537d 96 TLS_IO_TRANSPORT_PROVIDER tls_io_transport_provider;
Azure.IoT Build 10:75c5e0d8537d 97 // AMQP SASL I/O transport created on top of the TLS I/O layer.
Azure.IoT Build 10:75c5e0d8537d 98 XIO_HANDLE sasl_io;
Azure.IoT Build 10:75c5e0d8537d 99 // AMQP SASL I/O mechanism to be used.
Azure.IoT Build 10:75c5e0d8537d 100 SASL_MECHANISM_HANDLE sasl_mechanism;
Azure.IoT Build 10:75c5e0d8537d 101 // AMQP connection.
Azure.IoT Build 10:75c5e0d8537d 102 CONNECTION_HANDLE connection;
Azure.IoT Build 10:75c5e0d8537d 103 // Current AMQP connection state;
Azure.IoT Build 10:75c5e0d8537d 104 AMQP_MANAGEMENT_STATE connection_state;
Azure.IoT Build 10:75c5e0d8537d 105 // Last time the AMQP connection establishment was initiated.
Azure.IoT Build 10:75c5e0d8537d 106 size_t connection_establish_time;
Azure.IoT Build 10:75c5e0d8537d 107 // AMQP session.
Azure.IoT Build 10:75c5e0d8537d 108 SESSION_HANDLE session;
Azure.IoT Build 10:75c5e0d8537d 109 // AMQP link used by the event sender.
Azure.IoT Build 10:75c5e0d8537d 110 LINK_HANDLE sender_link;
Azure.IoT Build 10:75c5e0d8537d 111 // uAMQP event sender.
Azure.IoT Build 10:75c5e0d8537d 112 MESSAGE_SENDER_HANDLE message_sender;
Azure.IoT Build 10:75c5e0d8537d 113 // Internal flag that controls if messages should be received or not.
Azure.IoT Build 10:75c5e0d8537d 114 bool receive_messages;
Azure.IoT Build 10:75c5e0d8537d 115 // AMQP link used by the message receiver.
Azure.IoT Build 10:75c5e0d8537d 116 LINK_HANDLE receiver_link;
Azure.IoT Build 10:75c5e0d8537d 117 // uAMQP message receiver.
Azure.IoT Build 10:75c5e0d8537d 118 MESSAGE_RECEIVER_HANDLE message_receiver;
Azure.IoT Build 10:75c5e0d8537d 119 // List with events still pending to be sent. It is provided by the upper layer.
Azure.IoT Build 10:75c5e0d8537d 120 PDLIST_ENTRY waitingToSend;
Azure.IoT Build 10:75c5e0d8537d 121 // Internal list with the items currently being processed/sent through uAMQP.
Azure.IoT Build 10:75c5e0d8537d 122 DLIST_ENTRY inProgress;
Azure.IoT Build 10:75c5e0d8537d 123 // Connection instance with the Azure IoT CBS.
Azure.IoT Build 10:75c5e0d8537d 124 CBS_HANDLE cbs;
Azure.IoT Build 10:75c5e0d8537d 125 // Current state of the CBS connection.
Azure.IoT Build 10:75c5e0d8537d 126 CBS_STATE cbs_state;
Azure.IoT Build 10:75c5e0d8537d 127 // Time when the current SAS token was created, in seconds since epoch.
Azure.IoT Build 10:75c5e0d8537d 128 size_t current_sas_token_create_time;
Azure.IoT Build 10:75c5e0d8537d 129 // Mark if device is registered in transport (only one device per transport).
Azure.IoT Build 10:75c5e0d8537d 130 bool isRegistered;
Azure.IoT Build 7:07bc440836b3 131 } AMQP_TRANSPORT_INSTANCE;
Azure.IoT Build 7:07bc440836b3 132
AzureIoTClient 4:57e049bce51e 133
AzureIoTClient 4:57e049bce51e 134
Azure.IoT Build 7:07bc440836b3 135 // Auxiliary functions
AzureIoTClient 4:57e049bce51e 136
Azure.IoT Build 7:07bc440836b3 137 static STRING_HANDLE concat3Params(const char* prefix, const char* infix, const char* suffix)
AzureIoTClient 4:57e049bce51e 138 {
Azure.IoT Build 7:07bc440836b3 139 STRING_HANDLE result = NULL;
Azure.IoT Build 10:75c5e0d8537d 140 char* concat;
Azure.IoT Build 7:07bc440836b3 141 size_t totalLength = strlen(prefix) + strlen(infix) + strlen(suffix) + 1; // One extra for \0.
AzureIoTClient 5:8d58d20699dd 142
Azure.IoT Build 10:75c5e0d8537d 143 if ((concat = (char*)malloc(totalLength)) != NULL)
AzureIoTClient 5:8d58d20699dd 144 {
Azure.IoT Build 10:75c5e0d8537d 145 (void)strcpy(concat, prefix);
Azure.IoT Build 10:75c5e0d8537d 146 (void)strcat(concat, infix);
Azure.IoT Build 10:75c5e0d8537d 147 (void)strcat(concat, suffix);
Azure.IoT Build 10:75c5e0d8537d 148 result = STRING_construct(concat);
Azure.IoT Build 7:07bc440836b3 149 free(concat);
AzureIoTClient 5:8d58d20699dd 150 }
Azure.IoT Build 10:75c5e0d8537d 151 else
Azure.IoT Build 10:75c5e0d8537d 152 {
Azure.IoT Build 10:75c5e0d8537d 153 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 154 }
AzureIoTClient 5:8d58d20699dd 155
AzureIoTClient 4:57e049bce51e 156 return result;
AzureIoTClient 4:57e049bce51e 157 }
AzureIoTClient 4:57e049bce51e 158
Azure.IoT Build 10:75c5e0d8537d 159 static size_t getSecondsSinceEpoch(void)
AzureIoTClient 4:57e049bce51e 160 {
Azure.IoT Build 10:75c5e0d8537d 161 return (size_t)(difftime(get_time(NULL), (time_t)0));
AzureIoTClient 4:57e049bce51e 162 }
AzureIoTClient 4:57e049bce51e 163
Azure.IoT Build 12:841a4c36bd36 164 static void trackEventInProgress(IOTHUB_MESSAGE_LIST* message, AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 165 {
Azure.IoT Build 12:841a4c36bd36 166 DList_RemoveEntryList(&message->entry);
Azure.IoT Build 12:841a4c36bd36 167 DList_InsertTailList(&transport_state->inProgress, &message->entry);
AzureIoTClient 4:57e049bce51e 168 }
AzureIoTClient 4:57e049bce51e 169
Azure.IoT Build 7:07bc440836b3 170 static IOTHUB_MESSAGE_LIST* getNextEventToSend(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 171 {
Azure.IoT Build 10:75c5e0d8537d 172 IOTHUB_MESSAGE_LIST* message;
Azure.IoT Build 7:07bc440836b3 173
Azure.IoT Build 7:07bc440836b3 174 if (!DList_IsListEmpty(transport_state->waitingToSend))
AzureIoTClient 4:57e049bce51e 175 {
Azure.IoT Build 7:07bc440836b3 176 PDLIST_ENTRY list_entry = transport_state->waitingToSend->Flink;
Azure.IoT Build 7:07bc440836b3 177 message = containingRecord(list_entry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 4:57e049bce51e 178 }
Azure.IoT Build 10:75c5e0d8537d 179 else
Azure.IoT Build 10:75c5e0d8537d 180 {
Azure.IoT Build 10:75c5e0d8537d 181 message = NULL;
Azure.IoT Build 10:75c5e0d8537d 182 }
Azure.IoT Build 7:07bc440836b3 183
Azure.IoT Build 7:07bc440836b3 184 return message;
Azure.IoT Build 7:07bc440836b3 185 }
Azure.IoT Build 7:07bc440836b3 186
Azure.IoT Build 12:841a4c36bd36 187 static int isEventInInProgressList(IOTHUB_MESSAGE_LIST* message)
Azure.IoT Build 7:07bc440836b3 188 {
Azure.IoT Build 12:841a4c36bd36 189 return !DList_IsListEmpty(&message->entry);
AzureIoTClient 4:57e049bce51e 190 }
AzureIoTClient 4:57e049bce51e 191
Azure.IoT Build 12:841a4c36bd36 192 static void removeEventFromInProgressList(IOTHUB_MESSAGE_LIST* message)
AzureIoTClient 4:57e049bce51e 193 {
Azure.IoT Build 12:841a4c36bd36 194 DList_RemoveEntryList(&message->entry);
Azure.IoT Build 12:841a4c36bd36 195 DList_InitializeListHead(&message->entry);
AzureIoTClient 4:57e049bce51e 196 }
AzureIoTClient 4:57e049bce51e 197
Azure.IoT Build 12:841a4c36bd36 198 static void rollEventBackToWaitList(IOTHUB_MESSAGE_LIST* message, AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 199 {
Azure.IoT Build 12:841a4c36bd36 200 removeEventFromInProgressList(message);
Azure.IoT Build 12:841a4c36bd36 201 DList_InsertTailList(transport_state->waitingToSend, &message->entry);
Azure.IoT Build 7:07bc440836b3 202 }
Azure.IoT Build 7:07bc440836b3 203
Azure.IoT Build 7:07bc440836b3 204 static void rollEventsBackToWaitList(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 205 {
Azure.IoT Build 7:07bc440836b3 206 PDLIST_ENTRY entry = transport_state->inProgress.Blink;
Azure.IoT Build 7:07bc440836b3 207
Azure.IoT Build 7:07bc440836b3 208 while (entry != &transport_state->inProgress)
AzureIoTClient 4:57e049bce51e 209 {
Azure.IoT Build 12:841a4c36bd36 210 IOTHUB_MESSAGE_LIST* message = containingRecord(entry, IOTHUB_MESSAGE_LIST, entry);
Azure.IoT Build 7:07bc440836b3 211 entry = entry->Blink;
Azure.IoT Build 12:841a4c36bd36 212 rollEventBackToWaitList(message, transport_state);
AzureIoTClient 4:57e049bce51e 213 }
AzureIoTClient 4:57e049bce51e 214 }
AzureIoTClient 4:57e049bce51e 215
AzureIoTClient 14:8e8e42008807 216
AzureIoTClient 14:8e8e42008807 217 static int addPropertiesTouAMQPMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, MESSAGE_HANDLE uamqp_message)
AzureIoTClient 14:8e8e42008807 218 {
AzureIoTClient 14:8e8e42008807 219 int result;
AzureIoTClient 14:8e8e42008807 220 MAP_HANDLE properties_map;
AzureIoTClient 14:8e8e42008807 221 const char* const* propertyKeys;
AzureIoTClient 14:8e8e42008807 222 const char* const* propertyValues;
AzureIoTClient 14:8e8e42008807 223 size_t propertyCount;
AzureIoTClient 14:8e8e42008807 224
AzureIoTClient 14:8e8e42008807 225 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_007: [The IoTHub message properties shall be obtained by calling IoTHubMessage_Properties.] */
AzureIoTClient 14:8e8e42008807 226 properties_map = IoTHubMessage_Properties(iothub_message_handle);
AzureIoTClient 14:8e8e42008807 227 if (properties_map == NULL)
AzureIoTClient 14:8e8e42008807 228 {
AzureIoTClient 14:8e8e42008807 229 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 230 LogError("Failed to get property map from IoTHub message.");
AzureIoTClient 14:8e8e42008807 231 result = __LINE__;
AzureIoTClient 14:8e8e42008807 232 }
AzureIoTClient 14:8e8e42008807 233 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_015: [The actual keys and values, as well as the number of properties shall be obtained by calling Map_GetInternals on the handle obtained from IoTHubMessage_Properties.] */
AzureIoTClient 14:8e8e42008807 234 else if (Map_GetInternals(properties_map, &propertyKeys, &propertyValues, &propertyCount) != MAP_OK)
AzureIoTClient 14:8e8e42008807 235 {
AzureIoTClient 14:8e8e42008807 236 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 237 LogError("Failed to get the internals of the property map.");
AzureIoTClient 14:8e8e42008807 238 result = __LINE__;
AzureIoTClient 14:8e8e42008807 239 }
AzureIoTClient 14:8e8e42008807 240 else
AzureIoTClient 14:8e8e42008807 241 {
AzureIoTClient 14:8e8e42008807 242 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_016: [If the number of properties is 0, no uAMQP map shall be created and no application properties shall be set on the uAMQP message.] */
AzureIoTClient 14:8e8e42008807 243 if (propertyCount != 0)
AzureIoTClient 14:8e8e42008807 244 {
AzureIoTClient 14:8e8e42008807 245 size_t i;
AzureIoTClient 14:8e8e42008807 246 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_009: [The uAMQP map shall be created by calling amqpvalue_create_map.] */
AzureIoTClient 14:8e8e42008807 247 AMQP_VALUE uamqp_map = amqpvalue_create_map();
AzureIoTClient 14:8e8e42008807 248 if (uamqp_map == NULL)
AzureIoTClient 14:8e8e42008807 249 {
AzureIoTClient 14:8e8e42008807 250 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 251 LogError("Failed to create uAMQP map for the properties.");
AzureIoTClient 14:8e8e42008807 252 result = __LINE__;
AzureIoTClient 14:8e8e42008807 253 }
AzureIoTClient 14:8e8e42008807 254 else
AzureIoTClient 14:8e8e42008807 255 {
AzureIoTClient 14:8e8e42008807 256 for (i = 0; i < propertyCount; i++)
AzureIoTClient 14:8e8e42008807 257 {
AzureIoTClient 14:8e8e42008807 258 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_010: [A key uAMQP value shall be created by using amqpvalue_create_string.] */
AzureIoTClient 14:8e8e42008807 259 AMQP_VALUE map_key_value = amqpvalue_create_string(propertyKeys[i]);
AzureIoTClient 14:8e8e42008807 260 if (map_key_value == NULL)
AzureIoTClient 14:8e8e42008807 261 {
AzureIoTClient 14:8e8e42008807 262 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 263 LogError("Failed to create uAMQP property key value.");
AzureIoTClient 14:8e8e42008807 264 break;
AzureIoTClient 14:8e8e42008807 265 }
AzureIoTClient 14:8e8e42008807 266
AzureIoTClient 14:8e8e42008807 267 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_011: [A value uAMQP value shall be created by using amqpvalue_create_string.] */
AzureIoTClient 14:8e8e42008807 268 AMQP_VALUE map_value_value = amqpvalue_create_string(propertyValues[i]);
AzureIoTClient 14:8e8e42008807 269 if (map_value_value == NULL)
AzureIoTClient 14:8e8e42008807 270 {
AzureIoTClient 14:8e8e42008807 271 amqpvalue_destroy(map_key_value);
AzureIoTClient 14:8e8e42008807 272 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 273 LogError("Failed to create uAMQP property key value.");
AzureIoTClient 14:8e8e42008807 274 break;
AzureIoTClient 14:8e8e42008807 275 }
AzureIoTClient 14:8e8e42008807 276
AzureIoTClient 14:8e8e42008807 277 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_008: [All properties shall be transferred to a uAMQP map.] */
AzureIoTClient 14:8e8e42008807 278 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_012: [The key/value pair for the property shall be set into the uAMQP property map by calling amqpvalue_map_set_value.] */
AzureIoTClient 14:8e8e42008807 279 if (amqpvalue_set_map_value(uamqp_map, map_key_value, map_value_value) != 0)
AzureIoTClient 14:8e8e42008807 280 {
AzureIoTClient 14:8e8e42008807 281 amqpvalue_destroy(map_key_value);
AzureIoTClient 14:8e8e42008807 282 amqpvalue_destroy(map_value_value);
AzureIoTClient 14:8e8e42008807 283 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 284 LogError("Failed to create uAMQP property key value.");
AzureIoTClient 14:8e8e42008807 285 break;
AzureIoTClient 14:8e8e42008807 286 }
AzureIoTClient 14:8e8e42008807 287
AzureIoTClient 14:8e8e42008807 288 amqpvalue_destroy(map_key_value);
AzureIoTClient 14:8e8e42008807 289 amqpvalue_destroy(map_value_value);
AzureIoTClient 14:8e8e42008807 290 }
AzureIoTClient 14:8e8e42008807 291
AzureIoTClient 14:8e8e42008807 292 if (i < propertyCount)
AzureIoTClient 14:8e8e42008807 293 {
AzureIoTClient 14:8e8e42008807 294 result = __LINE__;
AzureIoTClient 14:8e8e42008807 295 }
AzureIoTClient 14:8e8e42008807 296 else
AzureIoTClient 14:8e8e42008807 297 {
AzureIoTClient 14:8e8e42008807 298 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_013: [After all properties have been filled in the uAMQP map, the uAMQP properties map shall be set on the uAMQP message by calling message_set_application_properties.] */
AzureIoTClient 14:8e8e42008807 299 if (message_set_application_properties(uamqp_message, uamqp_map) != 0)
AzureIoTClient 14:8e8e42008807 300 {
AzureIoTClient 14:8e8e42008807 301 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
AzureIoTClient 14:8e8e42008807 302 LogError("Failed to transfer the message properties to the uAMQP message.");
AzureIoTClient 14:8e8e42008807 303 result = __LINE__;
AzureIoTClient 14:8e8e42008807 304 }
AzureIoTClient 14:8e8e42008807 305 else
AzureIoTClient 14:8e8e42008807 306 {
AzureIoTClient 14:8e8e42008807 307 result = 0;
AzureIoTClient 14:8e8e42008807 308 }
AzureIoTClient 14:8e8e42008807 309 }
AzureIoTClient 14:8e8e42008807 310
AzureIoTClient 14:8e8e42008807 311 amqpvalue_destroy(uamqp_map);
AzureIoTClient 14:8e8e42008807 312 }
AzureIoTClient 14:8e8e42008807 313 }
AzureIoTClient 14:8e8e42008807 314 else
AzureIoTClient 14:8e8e42008807 315 {
AzureIoTClient 14:8e8e42008807 316 result = 0;
AzureIoTClient 14:8e8e42008807 317 }
AzureIoTClient 14:8e8e42008807 318 }
AzureIoTClient 14:8e8e42008807 319
AzureIoTClient 14:8e8e42008807 320 return result;
AzureIoTClient 14:8e8e42008807 321 }
AzureIoTClient 14:8e8e42008807 322
AzureIoTClient 14:8e8e42008807 323 static int readPropertiesFromuAMQPMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, MESSAGE_HANDLE uamqp_message)
AzureIoTClient 14:8e8e42008807 324 {
AzureIoTClient 14:8e8e42008807 325 int return_value;
AzureIoTClient 14:8e8e42008807 326 PROPERTIES_HANDLE uamqp_message_properties;
AzureIoTClient 14:8e8e42008807 327 AMQP_VALUE uamqp_message_property;
AzureIoTClient 14:8e8e42008807 328 char* uamqp_message_property_value;
AzureIoTClient 14:8e8e42008807 329 int api_call_result;
AzureIoTClient 14:8e8e42008807 330
AzureIoTClient 14:8e8e42008807 331 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_155: [uAMQP message properties shall be retrieved using message_get_properties.] */
AzureIoTClient 14:8e8e42008807 332 if ((api_call_result = message_get_properties(uamqp_message, &uamqp_message_properties)) != 0)
AzureIoTClient 14:8e8e42008807 333 {
AzureIoTClient 14:8e8e42008807 334 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_156: [If message_get_properties fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 335 LogError("Failed to get property properties map from uAMQP message (error code %d).", api_call_result);
AzureIoTClient 14:8e8e42008807 336 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 337 }
AzureIoTClient 14:8e8e42008807 338 else
AzureIoTClient 14:8e8e42008807 339 {
AzureIoTClient 14:8e8e42008807 340 return_value = 0; // Properties 'message-id' and 'correlation-id' are optional according to the AMQP 1.0 spec.
AzureIoTClient 14:8e8e42008807 341
AzureIoTClient 14:8e8e42008807 342 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_157: [The message-id property shall be read from the uAMQP message by calling properties_get_message_id.] */
AzureIoTClient 14:8e8e42008807 343 if ((api_call_result = properties_get_message_id(uamqp_message_properties, &uamqp_message_property)) != 0)
AzureIoTClient 14:8e8e42008807 344 {
AzureIoTClient 14:8e8e42008807 345 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_158: [If properties_get_message_id fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 346 LogInfo("Failed to get value of uAMQP message 'message-id' property (%d).", api_call_result);
AzureIoTClient 14:8e8e42008807 347 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 348 }
AzureIoTClient 14:8e8e42008807 349 else if (amqpvalue_get_type(uamqp_message_property) != AMQP_TYPE_NULL)
AzureIoTClient 14:8e8e42008807 350 {
AzureIoTClient 14:8e8e42008807 351 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_159: [The message-id value shall be retrieved from the AMQP_VALUE as char* by calling amqpvalue_get_string.] */
AzureIoTClient 14:8e8e42008807 352 if ((api_call_result = amqpvalue_get_string(uamqp_message_property, &uamqp_message_property_value)) != 0)
AzureIoTClient 14:8e8e42008807 353 {
AzureIoTClient 14:8e8e42008807 354 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_160: [If amqpvalue_get_string fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 355 LogError("Failed to get value of uAMQP message 'message-id' property (%d).", api_call_result);
AzureIoTClient 14:8e8e42008807 356 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 357 }
AzureIoTClient 14:8e8e42008807 358 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_161: [The message-id property shall be set on the IOTHUB_MESSAGE_HANDLE by calling IoTHubMessage_SetMessageId, passing the value read from the uAMQP message.] */
AzureIoTClient 14:8e8e42008807 359 else if (IoTHubMessage_SetMessageId(iothub_message_handle, uamqp_message_property_value) != IOTHUB_MESSAGE_OK)
AzureIoTClient 14:8e8e42008807 360 {
AzureIoTClient 14:8e8e42008807 361 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_162: [If IoTHubMessage_SetMessageId fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 362 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'message-id' property.");
AzureIoTClient 14:8e8e42008807 363 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 364 }
AzureIoTClient 14:8e8e42008807 365 }
AzureIoTClient 14:8e8e42008807 366
AzureIoTClient 14:8e8e42008807 367 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_163: [The correlation-id property shall be read from the uAMQP message by calling properties_get_correlation_id.] */
AzureIoTClient 14:8e8e42008807 368 if ((api_call_result = properties_get_correlation_id(uamqp_message_properties, &uamqp_message_property)) != 0)
AzureIoTClient 14:8e8e42008807 369 {
AzureIoTClient 14:8e8e42008807 370 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_164: [If properties_get_correlation_id fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 371 LogError("Failed to get value of uAMQP message 'correlation-id' property (%d).", api_call_result);
AzureIoTClient 14:8e8e42008807 372 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 373 }
AzureIoTClient 14:8e8e42008807 374 else if (amqpvalue_get_type(uamqp_message_property) != AMQP_TYPE_NULL)
AzureIoTClient 14:8e8e42008807 375 {
AzureIoTClient 14:8e8e42008807 376 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_165: [The correlation-id value shall be retrieved from the AMQP_VALUE as char* by calling amqpvalue_get_string.] */
AzureIoTClient 14:8e8e42008807 377 if ((api_call_result = amqpvalue_get_string(uamqp_message_property, &uamqp_message_property_value)) != 0)
AzureIoTClient 14:8e8e42008807 378 {
AzureIoTClient 14:8e8e42008807 379 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_166: [If amqpvalue_get_string fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 380 LogError("Failed to get value of uAMQP message 'correlation-id' property (%d).", api_call_result);
AzureIoTClient 14:8e8e42008807 381 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 382 }
AzureIoTClient 14:8e8e42008807 383 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_167: [The correlation-id property shall be set on the IOTHUB_MESSAGE_HANDLE by calling IoTHubMessage_SetCorrelationId, passing the value read from the uAMQP message.] */
AzureIoTClient 14:8e8e42008807 384 else if (IoTHubMessage_SetCorrelationId(iothub_message_handle, uamqp_message_property_value) != IOTHUB_MESSAGE_OK)
AzureIoTClient 14:8e8e42008807 385 {
AzureIoTClient 14:8e8e42008807 386 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_168: [If IoTHubMessage_SetCorrelationId fails, the error shall be notified and ‘on_message_received’ shall continue.] */
AzureIoTClient 14:8e8e42008807 387 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'correlation-id' property.");
AzureIoTClient 14:8e8e42008807 388 return_value = __LINE__;
AzureIoTClient 14:8e8e42008807 389 }
AzureIoTClient 14:8e8e42008807 390 }
AzureIoTClient 14:8e8e42008807 391 properties_destroy(uamqp_message_properties);
AzureIoTClient 14:8e8e42008807 392 }
AzureIoTClient 14:8e8e42008807 393
AzureIoTClient 14:8e8e42008807 394 return return_value;
AzureIoTClient 14:8e8e42008807 395 }
AzureIoTClient 14:8e8e42008807 396
AzureIoTClient 14:8e8e42008807 397 static int readApplicationPropertiesFromuAMQPMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, MESSAGE_HANDLE uamqp_message)
AzureIoTClient 14:8e8e42008807 398 {
AzureIoTClient 14:8e8e42008807 399 int result;
AzureIoTClient 14:8e8e42008807 400 AMQP_VALUE uamqp_app_properties;
AzureIoTClient 14:8e8e42008807 401 int property_count;
AzureIoTClient 14:8e8e42008807 402 MAP_HANDLE iothub_message_properties_map;
AzureIoTClient 14:8e8e42008807 403
AzureIoTClient 14:8e8e42008807 404 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_170: [The IOTHUB_MESSAGE_HANDLE properties shall be retrieved using IoTHubMessage_Properties.]
AzureIoTClient 14:8e8e42008807 405 if ((iothub_message_properties_map = IoTHubMessage_Properties(iothub_message_handle)) == NULL)
AzureIoTClient 14:8e8e42008807 406 {
AzureIoTClient 14:8e8e42008807 407 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_186: [If IoTHubMessage_Properties fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 408 LogError("Failed to get property map from IoTHub message.");
AzureIoTClient 14:8e8e42008807 409 result = __LINE__;
AzureIoTClient 14:8e8e42008807 410 }
AzureIoTClient 14:8e8e42008807 411 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_171: [uAMQP message application properties shall be retrieved using message_get_application_properties.]
AzureIoTClient 14:8e8e42008807 412 else if ((result = message_get_application_properties(uamqp_message, &uamqp_app_properties)) != 0)
AzureIoTClient 14:8e8e42008807 413 {
AzureIoTClient 14:8e8e42008807 414 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_172: [If message_get_application_properties fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 415 LogError("Failed reading the incoming uAMQP message properties (return code %d).", result);
AzureIoTClient 14:8e8e42008807 416 result = __LINE__;
AzureIoTClient 14:8e8e42008807 417 }
AzureIoTClient 14:8e8e42008807 418 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_187: [If message_get_application_properties succeeds but returns a NULL application properties map (there are no properties), ‘on_message_received’ shall continue normally.]
AzureIoTClient 14:8e8e42008807 419 else if (uamqp_app_properties == NULL)
AzureIoTClient 14:8e8e42008807 420 {
AzureIoTClient 14:8e8e42008807 421 result = 0;
AzureIoTClient 14:8e8e42008807 422 }
AzureIoTClient 14:8e8e42008807 423 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_173: [The actual uAMQP message application properties should be extracted from the result of message_get_application_properties using amqpvalue_get_inplace_described_value.]
AzureIoTClient 14:8e8e42008807 424 else if ((uamqp_app_properties = amqpvalue_get_inplace_described_value(uamqp_app_properties)) == NULL)
AzureIoTClient 14:8e8e42008807 425 {
AzureIoTClient 14:8e8e42008807 426 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_174: [If amqpvalue_get_inplace_described_value fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 427 LogError("Failed getting the map of uAMQP message application properties (return code %d).", result);
AzureIoTClient 14:8e8e42008807 428 result = __LINE__;
AzureIoTClient 14:8e8e42008807 429 }
AzureIoTClient 14:8e8e42008807 430 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_175: [The number of items in the uAMQP message application properties shall be obtained using amqpvalue_get_map_pair_count.]
AzureIoTClient 14:8e8e42008807 431 else if ((result = amqpvalue_get_map_pair_count(uamqp_app_properties, &property_count)) != 0)
AzureIoTClient 14:8e8e42008807 432 {
AzureIoTClient 14:8e8e42008807 433 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_176: [If amqpvalue_get_map_pair_count fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 434 LogError("Failed reading the number of values in the uAMQP property map (return code %d).", result);
AzureIoTClient 14:8e8e42008807 435 result = __LINE__;
AzureIoTClient 14:8e8e42008807 436 }
AzureIoTClient 14:8e8e42008807 437 else
AzureIoTClient 14:8e8e42008807 438 {
AzureIoTClient 14:8e8e42008807 439 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_177: [‘on_message_received’ shall iterate through each uAMQP application property and add it on IOTHUB_MESSAGE_HANDLE properties.]
AzureIoTClient 14:8e8e42008807 440 size_t i;
AzureIoTClient 14:8e8e42008807 441 for (i = 0; i < property_count; i++)
AzureIoTClient 14:8e8e42008807 442 {
AzureIoTClient 14:8e8e42008807 443 AMQP_VALUE map_key_name;
AzureIoTClient 14:8e8e42008807 444 AMQP_VALUE map_key_value;
AzureIoTClient 14:8e8e42008807 445 char *key_name;
AzureIoTClient 14:8e8e42008807 446 char* key_value;
AzureIoTClient 14:8e8e42008807 447
AzureIoTClient 14:8e8e42008807 448 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_178: [The uAMQP application property name and value shall be obtained using amqpvalue_get_map_key_value_pair.]
AzureIoTClient 14:8e8e42008807 449 if ((result = amqpvalue_get_map_key_value_pair(uamqp_app_properties, i, &map_key_name, &map_key_value)) != 0)
AzureIoTClient 14:8e8e42008807 450 {
AzureIoTClient 14:8e8e42008807 451 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_179: [If amqpvalue_get_map_key_value_pair fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 452 LogError("Failed reading the key/value pair from the uAMQP property map (return code %d).", result);
AzureIoTClient 14:8e8e42008807 453 result = __LINE__;
AzureIoTClient 14:8e8e42008807 454 break;
AzureIoTClient 14:8e8e42008807 455 }
AzureIoTClient 14:8e8e42008807 456 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_180: [The uAMQP application property name shall be extracted as string using amqpvalue_get_string.]
AzureIoTClient 14:8e8e42008807 457 else if ((result = amqpvalue_get_string(map_key_name, &key_name)) != 0)
AzureIoTClient 14:8e8e42008807 458 {
AzureIoTClient 14:8e8e42008807 459 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_181: [If amqpvalue_get_string fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 460 LogError("Failed parsing the uAMQP property name (return code %d).", result);
AzureIoTClient 14:8e8e42008807 461 result = __LINE__;
AzureIoTClient 14:8e8e42008807 462 break;
AzureIoTClient 14:8e8e42008807 463 }
AzureIoTClient 14:8e8e42008807 464 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_182: [The uAMQP application property value shall be extracted as string using amqpvalue_get_string.]
AzureIoTClient 14:8e8e42008807 465 else if ((result = amqpvalue_get_string(map_key_value, &key_value)) != 0)
AzureIoTClient 14:8e8e42008807 466 {
AzureIoTClient 14:8e8e42008807 467 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_183: [If amqpvalue_get_string fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 468 LogError("Failed parsing the uAMQP property value (return code %d).", result);
AzureIoTClient 14:8e8e42008807 469 result = __LINE__;
AzureIoTClient 14:8e8e42008807 470 break;
AzureIoTClient 14:8e8e42008807 471 }
AzureIoTClient 14:8e8e42008807 472 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_184: [The application property name and value shall be added to IOTHUB_MESSAGE_HANDLE properties using Map_AddOrUpdate.]
AzureIoTClient 14:8e8e42008807 473 else if (Map_AddOrUpdate(iothub_message_properties_map, key_name, key_value) != MAP_OK)
AzureIoTClient 14:8e8e42008807 474 {
AzureIoTClient 14:8e8e42008807 475 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_185: [If Map_AddOrUpdate fails, the error shall be notified and ‘on_message_received’ shall continue.]
AzureIoTClient 14:8e8e42008807 476 LogError("Failed to add/update IoTHub message property map.");
AzureIoTClient 14:8e8e42008807 477 result = __LINE__;
AzureIoTClient 14:8e8e42008807 478 break;
AzureIoTClient 14:8e8e42008807 479 }
AzureIoTClient 14:8e8e42008807 480 }
AzureIoTClient 14:8e8e42008807 481 }
AzureIoTClient 14:8e8e42008807 482
AzureIoTClient 14:8e8e42008807 483 return result;
AzureIoTClient 14:8e8e42008807 484 }
AzureIoTClient 14:8e8e42008807 485
Azure.IoT Build 10:75c5e0d8537d 486 static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result)
AzureIoTClient 4:57e049bce51e 487 {
Azure.IoT Build 12:841a4c36bd36 488 IOTHUB_MESSAGE_LIST* message = (IOTHUB_MESSAGE_LIST*)context;
Azure.IoT Build 10:75c5e0d8537d 489
Azure.IoT Build 10:75c5e0d8537d 490 IOTHUB_CLIENT_RESULT iot_hub_send_result;
Azure.IoT Build 10:75c5e0d8537d 491
Azure.IoT Build 10:75c5e0d8537d 492 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_142: [The callback 'on_message_send_complete' shall pass to the upper layer callback an IOTHUB_CLIENT_CONFIRMATION_OK if the result received is MESSAGE_SEND_OK]
Azure.IoT Build 10:75c5e0d8537d 493 if (send_result == MESSAGE_SEND_OK)
AzureIoTClient 4:57e049bce51e 494 {
Azure.IoT Build 10:75c5e0d8537d 495 iot_hub_send_result = IOTHUB_CLIENT_CONFIRMATION_OK;
Azure.IoT Build 10:75c5e0d8537d 496 }
Azure.IoT Build 10:75c5e0d8537d 497 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_143: [The callback 'on_message_send_complete' shall pass to the upper layer callback an IOTHUB_CLIENT_CONFIRMATION_ERROR if the result received is MESSAGE_SEND_ERROR]
Azure.IoT Build 12:841a4c36bd36 498 else
Azure.IoT Build 10:75c5e0d8537d 499 {
Azure.IoT Build 10:75c5e0d8537d 500 iot_hub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR;
Azure.IoT Build 10:75c5e0d8537d 501 }
Azure.IoT Build 7:07bc440836b3 502
Azure.IoT Build 10:75c5e0d8537d 503 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_102: [The callback 'on_message_send_complete' shall invoke the upper layer callback for message received if provided]
Azure.IoT Build 12:841a4c36bd36 504 if (message->callback != NULL)
Azure.IoT Build 10:75c5e0d8537d 505 {
Azure.IoT Build 12:841a4c36bd36 506 message->callback(iot_hub_send_result, message->context);
Azure.IoT Build 10:75c5e0d8537d 507 }
AzureIoTClient 4:57e049bce51e 508
Azure.IoT Build 12:841a4c36bd36 509 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_100: [The callback 'on_message_send_complete' shall remove the target message from the in-progress list after the upper layer callback]
Azure.IoT Build 12:841a4c36bd36 510 if (isEventInInProgressList(message))
Azure.IoT Build 12:841a4c36bd36 511 {
Azure.IoT Build 12:841a4c36bd36 512 removeEventFromInProgressList(message);
Azure.IoT Build 12:841a4c36bd36 513 }
Azure.IoT Build 12:841a4c36bd36 514
Azure.IoT Build 10:75c5e0d8537d 515 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_151: [The callback 'on_message_send_complete' shall destroy the message handle (IOTHUB_MESSAGE_HANDLE) using IoTHubMessage_Destroy()]
Azure.IoT Build 12:841a4c36bd36 516 IoTHubMessage_Destroy(message->messageHandle);
AzureIoTClient 4:57e049bce51e 517
Azure.IoT Build 12:841a4c36bd36 518 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_152: [The callback 'on_message_send_complete' shall destroy the IOTHUB_MESSAGE_LIST instance]
Azure.IoT Build 12:841a4c36bd36 519 free(message);
AzureIoTClient 4:57e049bce51e 520 }
AzureIoTClient 4:57e049bce51e 521
Azure.IoT Build 10:75c5e0d8537d 522 static void on_put_token_complete(void* context, CBS_OPERATION_RESULT operation_result, unsigned int status_code, const char* status_description)
AzureIoTClient 4:57e049bce51e 523 {
Azure.IoT Build 7:07bc440836b3 524 AMQP_TRANSPORT_INSTANCE* transportState = (AMQP_TRANSPORT_INSTANCE*)context;
Azure.IoT Build 7:07bc440836b3 525
Azure.IoT Build 12:841a4c36bd36 526 if (operation_result == CBS_OPERATION_RESULT_OK)
AzureIoTClient 4:57e049bce51e 527 {
Azure.IoT Build 7:07bc440836b3 528 transportState->cbs_state = CBS_STATE_AUTHENTICATED;
Azure.IoT Build 7:07bc440836b3 529 }
Azure.IoT Build 7:07bc440836b3 530 }
Azure.IoT Build 7:07bc440836b3 531
Azure.IoT Build 10:75c5e0d8537d 532 static AMQP_VALUE on_message_received(const void* context, MESSAGE_HANDLE message)
Azure.IoT Build 7:07bc440836b3 533 {
Azure.IoT Build 7:07bc440836b3 534 AMQP_VALUE result = NULL;
Azure.IoT Build 7:07bc440836b3 535
Azure.IoT Build 10:75c5e0d8537d 536 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_104: [The callback 'on_message_received' shall invoke IoTHubClient_LL_MessageCallback() passing the client and the incoming message handles as parameters]
Azure.IoT Build 7:07bc440836b3 537 IOTHUB_MESSAGE_HANDLE iothub_message = NULL;
Azure.IoT Build 7:07bc440836b3 538 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 11:62d7b956e76e 539
Azure.IoT Build 7:07bc440836b3 540 if (message_get_body_type(message, &body_type) != 0)
Azure.IoT Build 7:07bc440836b3 541 {
AzureIoTClient 13:a4af7c301e02 542 LogError("Failed to get the type of the message received by the transport.");
Azure.IoT Build 7:07bc440836b3 543 }
Azure.IoT Build 11:62d7b956e76e 544 else
Azure.IoT Build 7:07bc440836b3 545 {
Azure.IoT Build 7:07bc440836b3 546 if (body_type == MESSAGE_BODY_TYPE_DATA)
AzureIoTClient 4:57e049bce51e 547 {
Azure.IoT Build 7:07bc440836b3 548 BINARY_DATA binary_data;
Azure.IoT Build 7:07bc440836b3 549 if (message_get_body_amqp_data(message, 0, &binary_data) != 0)
Azure.IoT Build 7:07bc440836b3 550 {
AzureIoTClient 13:a4af7c301e02 551 LogError("Failed to get the body of the message received by the transport.");
Azure.IoT Build 7:07bc440836b3 552 }
Azure.IoT Build 7:07bc440836b3 553 else
Azure.IoT Build 7:07bc440836b3 554 {
Azure.IoT Build 7:07bc440836b3 555 iothub_message = IoTHubMessage_CreateFromByteArray(binary_data.bytes, binary_data.length);
Azure.IoT Build 7:07bc440836b3 556 }
AzureIoTClient 4:57e049bce51e 557 }
Azure.IoT Build 7:07bc440836b3 558 }
Azure.IoT Build 11:62d7b956e76e 559
Azure.IoT Build 7:07bc440836b3 560 if (iothub_message == NULL)
Azure.IoT Build 7:07bc440836b3 561 {
AzureIoTClient 13:a4af7c301e02 562 LogError("Transport failed processing the message received.");
AzureIoTClient 14:8e8e42008807 563
AzureIoTClient 14:8e8e42008807 564 result = messaging_delivery_rejected("Rejected due to failure reading AMQP message", "Failed reading message body");
Azure.IoT Build 7:07bc440836b3 565 }
AzureIoTClient 14:8e8e42008807 566 else
Azure.IoT Build 7:07bc440836b3 567 {
AzureIoTClient 14:8e8e42008807 568 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_153: [The callback ‘on_message_received’ shall read the message-id property from the uAMQP message and set it on the IoT Hub Message if the property is defined.] */
AzureIoTClient 14:8e8e42008807 569 /* Codes_SRS_IOTHUBTRANSPORTAMQP_09_154: [The callback ‘on_message_received’ shall read the correlation-id property from the uAMQP message and set it on the IoT Hub Message if the property is defined.] */
AzureIoTClient 14:8e8e42008807 570 if (readPropertiesFromuAMQPMessage(iothub_message, message) != 0)
AzureIoTClient 14:8e8e42008807 571 {
AzureIoTClient 14:8e8e42008807 572 LogError("Transport failed reading properties of the message received.");
AzureIoTClient 14:8e8e42008807 573 }
AzureIoTClient 14:8e8e42008807 574
AzureIoTClient 14:8e8e42008807 575 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_169: [The callback ‘on_message_received’ shall read the application properties from the uAMQP message and set it on the IoT Hub Message if any are provided.]
AzureIoTClient 14:8e8e42008807 576 if (readApplicationPropertiesFromuAMQPMessage(iothub_message, message) != 0)
AzureIoTClient 14:8e8e42008807 577 {
AzureIoTClient 14:8e8e42008807 578 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_188: [If ‘on_message_received’ fails reading the application properties from the uAMQP message, it shall NOT call IoTHubClient_LL_MessageCallback and shall reject the message.]
AzureIoTClient 14:8e8e42008807 579 LogError("Transport failed reading application properties of the message received.");
AzureIoTClient 14:8e8e42008807 580
AzureIoTClient 14:8e8e42008807 581 result = messaging_delivery_rejected("Rejected due to failure reading AMQP message", "Failed reading application properties");
AzureIoTClient 14:8e8e42008807 582 }
AzureIoTClient 14:8e8e42008807 583 else
AzureIoTClient 14:8e8e42008807 584 {
AzureIoTClient 14:8e8e42008807 585 IOTHUBMESSAGE_DISPOSITION_RESULT disposition_result;
AzureIoTClient 14:8e8e42008807 586
Azure.IoT Build 7:07bc440836b3 587 disposition_result = IoTHubClient_LL_MessageCallback((IOTHUB_CLIENT_LL_HANDLE)context, iothub_message);
Azure.IoT Build 7:07bc440836b3 588
Azure.IoT Build 10:75c5e0d8537d 589 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_105: [The callback 'on_message_received' shall return the result of messaging_delivery_accepted() if the IoTHubClient_LL_MessageCallback() returns IOTHUBMESSAGE_ACCEPTED]
Azure.IoT Build 7:07bc440836b3 590 if (disposition_result == IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 5:8d58d20699dd 591 {
Azure.IoT Build 7:07bc440836b3 592 result = messaging_delivery_accepted();
Azure.IoT Build 7:07bc440836b3 593 }
Azure.IoT Build 10:75c5e0d8537d 594 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_106: [The callback 'on_message_received' shall return the result of messaging_delivery_released() if the IoTHubClient_LL_MessageCallback() returns IOTHUBMESSAGE_ABANDONED]
Azure.IoT Build 7:07bc440836b3 595 else if (disposition_result == IOTHUBMESSAGE_ABANDONED)
Azure.IoT Build 7:07bc440836b3 596 {
Azure.IoT Build 7:07bc440836b3 597 result = messaging_delivery_released();
AzureIoTClient 5:8d58d20699dd 598 }
Azure.IoT Build 10:75c5e0d8537d 599 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_107: [The callback 'on_message_received' shall return the result of messaging_delivery_rejected("Rejected by application", "Rejected by application") if the IoTHubClient_LL_MessageCallback() returns IOTHUBMESSAGE_REJECTED]
Azure.IoT Build 7:07bc440836b3 600 else if (disposition_result == IOTHUBMESSAGE_REJECTED)
AzureIoTClient 4:57e049bce51e 601 {
Azure.IoT Build 7:07bc440836b3 602 result = messaging_delivery_rejected("Rejected by application", "Rejected by application");
AzureIoTClient 4:57e049bce51e 603 }
AzureIoTClient 14:8e8e42008807 604 }
AzureIoTClient 14:8e8e42008807 605
Azure.IoT Build 11:62d7b956e76e 606 IoTHubMessage_Destroy(iothub_message);
Azure.IoT Build 7:07bc440836b3 607 }
Azure.IoT Build 7:07bc440836b3 608
Azure.IoT Build 7:07bc440836b3 609 return result;
Azure.IoT Build 7:07bc440836b3 610 }
Azure.IoT Build 7:07bc440836b3 611
Azure.IoT Build 11:62d7b956e76e 612 static XIO_HANDLE getTLSIOTransport(const char* fqdn, int port)
Azure.IoT Build 7:07bc440836b3 613 {
Azure.IoT Build 10:75c5e0d8537d 614 TLSIO_CONFIG tls_io_config = { fqdn, port };
Azure.IoT Build 10:75c5e0d8537d 615 const IO_INTERFACE_DESCRIPTION* io_interface_description = platform_get_default_tlsio();
Azure.IoT Build 10:75c5e0d8537d 616 return xio_create(io_interface_description, &tls_io_config, NULL);
Azure.IoT Build 7:07bc440836b3 617 }
Azure.IoT Build 7:07bc440836b3 618
Azure.IoT Build 7:07bc440836b3 619 static void destroyConnection(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 620 {
Azure.IoT Build 10:75c5e0d8537d 621 if (transport_state->cbs != NULL)
Azure.IoT Build 7:07bc440836b3 622 {
Azure.IoT Build 10:75c5e0d8537d 623 cbs_destroy(transport_state->cbs);
Azure.IoT Build 10:75c5e0d8537d 624 transport_state->cbs = NULL;
Azure.IoT Build 10:75c5e0d8537d 625 }
Azure.IoT Build 7:07bc440836b3 626
Azure.IoT Build 10:75c5e0d8537d 627 if (transport_state->session != NULL)
Azure.IoT Build 10:75c5e0d8537d 628 {
Azure.IoT Build 10:75c5e0d8537d 629 session_destroy(transport_state->session);
Azure.IoT Build 10:75c5e0d8537d 630 transport_state->session = NULL;
Azure.IoT Build 10:75c5e0d8537d 631 }
Azure.IoT Build 7:07bc440836b3 632
Azure.IoT Build 10:75c5e0d8537d 633 if (transport_state->connection != NULL)
Azure.IoT Build 10:75c5e0d8537d 634 {
Azure.IoT Build 10:75c5e0d8537d 635 connection_destroy(transport_state->connection);
Azure.IoT Build 10:75c5e0d8537d 636 transport_state->connection = NULL;
Azure.IoT Build 10:75c5e0d8537d 637 }
Azure.IoT Build 7:07bc440836b3 638
Azure.IoT Build 10:75c5e0d8537d 639 if (transport_state->sasl_io != NULL)
Azure.IoT Build 10:75c5e0d8537d 640 {
Azure.IoT Build 10:75c5e0d8537d 641 xio_destroy(transport_state->sasl_io);
Azure.IoT Build 10:75c5e0d8537d 642 transport_state->sasl_io = NULL;
Azure.IoT Build 10:75c5e0d8537d 643 }
Azure.IoT Build 7:07bc440836b3 644
Azure.IoT Build 10:75c5e0d8537d 645 if (transport_state->sasl_mechanism != NULL)
Azure.IoT Build 10:75c5e0d8537d 646 {
Azure.IoT Build 10:75c5e0d8537d 647 saslmechanism_destroy(transport_state->sasl_mechanism);
Azure.IoT Build 10:75c5e0d8537d 648 transport_state->sasl_mechanism = NULL;
Azure.IoT Build 10:75c5e0d8537d 649 }
Azure.IoT Build 7:07bc440836b3 650
Azure.IoT Build 10:75c5e0d8537d 651 if (transport_state->tls_io != NULL)
Azure.IoT Build 10:75c5e0d8537d 652 {
Azure.IoT Build 10:75c5e0d8537d 653 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_034: [IoTHubTransportAMQP_Destroy shall destroy the AMQP TLS I/O transport.]
Azure.IoT Build 10:75c5e0d8537d 654 xio_destroy(transport_state->tls_io);
Azure.IoT Build 10:75c5e0d8537d 655 transport_state->tls_io = NULL;
Azure.IoT Build 10:75c5e0d8537d 656 }
Azure.IoT Build 7:07bc440836b3 657 }
Azure.IoT Build 7:07bc440836b3 658
Azure.IoT Build 7:07bc440836b3 659 static void on_amqp_management_state_changed(void* context, AMQP_MANAGEMENT_STATE new_amqp_management_state, AMQP_MANAGEMENT_STATE previous_amqp_management_state)
Azure.IoT Build 7:07bc440836b3 660 {
Azure.IoT Build 10:75c5e0d8537d 661 (void)previous_amqp_management_state;
Azure.IoT Build 10:75c5e0d8537d 662 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)context;
Azure.IoT Build 11:62d7b956e76e 663
Azure.IoT Build 10:75c5e0d8537d 664 if (transport_state != NULL)
Azure.IoT Build 7:07bc440836b3 665 {
Azure.IoT Build 7:07bc440836b3 666 transport_state->connection_state = new_amqp_management_state;
Azure.IoT Build 7:07bc440836b3 667 }
Azure.IoT Build 7:07bc440836b3 668 }
Azure.IoT Build 7:07bc440836b3 669
AzureIoTClient 13:a4af7c301e02 670 static void on_connection_io_error(void* context)
AzureIoTClient 13:a4af7c301e02 671 {
AzureIoTClient 13:a4af7c301e02 672 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)context;
AzureIoTClient 13:a4af7c301e02 673
AzureIoTClient 13:a4af7c301e02 674 if (transport_state != NULL)
AzureIoTClient 13:a4af7c301e02 675 {
AzureIoTClient 13:a4af7c301e02 676 transport_state->connection_state = AMQP_MANAGEMENT_STATE_ERROR;
AzureIoTClient 13:a4af7c301e02 677 }
AzureIoTClient 13:a4af7c301e02 678 }
AzureIoTClient 13:a4af7c301e02 679
Azure.IoT Build 7:07bc440836b3 680 static int establishConnection(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 681 {
Azure.IoT Build 7:07bc440836b3 682 int result;
Azure.IoT Build 7:07bc440836b3 683
Azure.IoT Build 7:07bc440836b3 684 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_110: [IoTHubTransportAMQP_DoWork shall create the TLS IO using transport_state->io_transport_provider callback function]
Azure.IoT Build 7:07bc440836b3 685 if (transport_state->tls_io == NULL &&
Azure.IoT Build 11:62d7b956e76e 686 (transport_state->tls_io = transport_state->tls_io_transport_provider(STRING_c_str(transport_state->iotHubHostFqdn), transport_state->iotHubPort)) == NULL)
Azure.IoT Build 7:07bc440836b3 687 {
Azure.IoT Build 7:07bc440836b3 688 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_136: [If transport_state->io_transport_provider_callback fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 689 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 690 LogError("Failed to obtain a TLS I/O transport layer.");
Azure.IoT Build 7:07bc440836b3 691 }
Azure.IoT Build 10:75c5e0d8537d 692 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_056: [IoTHubTransportAMQP_DoWork shall create the SASL mechanism using AMQP's saslmechanism_create() API]
Azure.IoT Build 7:07bc440836b3 693 else if ((transport_state->sasl_mechanism = saslmechanism_create(saslmssbcbs_get_interface(), NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 694 {
Azure.IoT Build 7:07bc440836b3 695 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_057: [If saslmechanism_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 696 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 697 LogError("Failed to create a SASL mechanism.");
Azure.IoT Build 7:07bc440836b3 698 }
Azure.IoT Build 7:07bc440836b3 699 else
Azure.IoT Build 7:07bc440836b3 700 {
Azure.IoT Build 7:07bc440836b3 701 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_060: [IoTHubTransportAMQP_DoWork shall create the SASL I / O layer using the xio_create() C Shared Utility API]
Azure.IoT Build 7:07bc440836b3 702 SASLCLIENTIO_CONFIG sasl_client_config = { transport_state->tls_io, transport_state->sasl_mechanism };
Azure.IoT Build 7:07bc440836b3 703 if ((transport_state->sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_client_config, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 704 {
Azure.IoT Build 7:07bc440836b3 705 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_061: [If xio_create() fails creating the SASL I/O layer, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 706 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 707 LogError("Failed to create a SASL I/O layer.");
AzureIoTClient 4:57e049bce51e 708 }
AzureIoTClient 13:a4af7c301e02 709 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_062: [IoTHubTransportAMQP_DoWork shall create the connection with the IoT service using connection_create2() AMQP API, passing the SASL I/O layer, IoT Hub FQDN and container ID as parameters (pass NULL for callbacks)]
AzureIoTClient 13:a4af7c301e02 710 else if ((transport_state->connection = connection_create2(transport_state->sasl_io, STRING_c_str(transport_state->iotHubHostFqdn), DEFAULT_CONTAINER_ID, NULL, NULL, NULL, NULL, on_connection_io_error, (void*)transport_state, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 711 {
AzureIoTClient 13:a4af7c301e02 712 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_063: [If connection_create2() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately.]
Azure.IoT Build 7:07bc440836b3 713 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 714 LogError("Failed to create the AMQP connection.");
Azure.IoT Build 7:07bc440836b3 715 }
Azure.IoT Build 7:07bc440836b3 716 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_137: [IoTHubTransportAMQP_DoWork shall create the AMQP session session_create() AMQP API, passing the connection instance as parameter]
Azure.IoT Build 7:07bc440836b3 717 else if ((transport_state->session = session_create(transport_state->connection, NULL, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 718 {
Azure.IoT Build 7:07bc440836b3 719 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_138 : [If session_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 720 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 721 LogError("Failed to create the AMQP session.");
AzureIoTClient 4:57e049bce51e 722 }
AzureIoTClient 4:57e049bce51e 723 else
AzureIoTClient 4:57e049bce51e 724 {
Azure.IoT Build 10:75c5e0d8537d 725 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_065: [IoTHubTransportAMQP_DoWork shall apply a default value of UINT_MAX for the parameter 'AMQP incoming window']
Azure.IoT Build 7:07bc440836b3 726 if (session_set_incoming_window(transport_state->session, (uint32_t)DEFAULT_INCOMING_WINDOW_SIZE) != 0)
Azure.IoT Build 7:07bc440836b3 727 {
AzureIoTClient 13:a4af7c301e02 728 LogError("Failed to set the AMQP incoming window size.");
Azure.IoT Build 7:07bc440836b3 729 }
Azure.IoT Build 7:07bc440836b3 730
Azure.IoT Build 10:75c5e0d8537d 731 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_115: [IoTHubTransportAMQP_DoWork shall apply a default value of 100 for the parameter 'AMQP outgoing window']
Azure.IoT Build 7:07bc440836b3 732 if (session_set_outgoing_window(transport_state->session, DEFAULT_OUTGOING_WINDOW_SIZE) != 0)
Azure.IoT Build 7:07bc440836b3 733 {
AzureIoTClient 13:a4af7c301e02 734 LogError("Failed to set the AMQP outgoing window size.");
Azure.IoT Build 7:07bc440836b3 735 }
Azure.IoT Build 7:07bc440836b3 736
Azure.IoT Build 7:07bc440836b3 737 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_066: [IoTHubTransportAMQP_DoWork shall establish the CBS connection using the cbs_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 738 if ((transport_state->cbs = cbs_create(transport_state->session, on_amqp_management_state_changed, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 739 {
Azure.IoT Build 7:07bc440836b3 740 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_067: [If cbs_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 741 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 742 LogError("Failed to create the CBS connection.");
Azure.IoT Build 7:07bc440836b3 743 }
Azure.IoT Build 7:07bc440836b3 744 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_139: [IoTHubTransportAMQP_DoWork shall open the CBS connection using the cbs_open() AMQP API]
Azure.IoT Build 10:75c5e0d8537d 745 else if (cbs_open(transport_state->cbs) != 0)
Azure.IoT Build 7:07bc440836b3 746 {
Azure.IoT Build 7:07bc440836b3 747 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_140: [If cbs_open() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 748 result = RESULT_FAILURE;
AzureIoTClient 13:a4af7c301e02 749 LogError("Failed to open the connection with CBS.");
Azure.IoT Build 7:07bc440836b3 750 }
Azure.IoT Build 7:07bc440836b3 751 else
Azure.IoT Build 7:07bc440836b3 752 {
Azure.IoT Build 7:07bc440836b3 753 transport_state->connection_establish_time = getSecondsSinceEpoch();
Azure.IoT Build 7:07bc440836b3 754 transport_state->cbs_state = CBS_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 755 result = RESULT_OK;
AzureIoTClient 4:57e049bce51e 756 }
AzureIoTClient 4:57e049bce51e 757 }
AzureIoTClient 4:57e049bce51e 758 }
Azure.IoT Build 7:07bc440836b3 759
Azure.IoT Build 7:07bc440836b3 760 if (result == RESULT_FAILURE)
Azure.IoT Build 7:07bc440836b3 761 {
Azure.IoT Build 7:07bc440836b3 762 destroyConnection(transport_state);
Azure.IoT Build 7:07bc440836b3 763 }
Azure.IoT Build 7:07bc440836b3 764
Azure.IoT Build 7:07bc440836b3 765 return result;
AzureIoTClient 4:57e049bce51e 766 }
AzureIoTClient 4:57e049bce51e 767
Azure.IoT Build 7:07bc440836b3 768 static int startAuthentication(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 769 {
Azure.IoT Build 10:75c5e0d8537d 770 int result;
Azure.IoT Build 7:07bc440836b3 771
Azure.IoT Build 7:07bc440836b3 772 size_t sas_token_create_time = getSecondsSinceEpoch(); // I.e.: NOW, in seconds since epoch.
Azure.IoT Build 11:62d7b956e76e 773
Azure.IoT Build 11:62d7b956e76e 774 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_083: [Each new SAS token created by the transport shall be valid for up to 'sas_token_lifetime' milliseconds from the time of creation]
Azure.IoT Build 7:07bc440836b3 775 size_t new_expiry_time = sas_token_create_time + (transport_state->sas_token_lifetime / 1000);
Azure.IoT Build 7:07bc440836b3 776
AzureIoTClient 14:8e8e42008807 777 STRING_HANDLE newSASToken;
AzureIoTClient 14:8e8e42008807 778
AzureIoTClient 14:8e8e42008807 779 if (transport_state->deviceSasToken == NULL)
AzureIoTClient 14:8e8e42008807 780 {
AzureIoTClient 14:8e8e42008807 781 newSASToken = SASToken_Create(transport_state->deviceKey, transport_state->devicesPath, transport_state->sasTokenKeyName, new_expiry_time);
AzureIoTClient 14:8e8e42008807 782 }
AzureIoTClient 14:8e8e42008807 783 else
AzureIoTClient 14:8e8e42008807 784 {
AzureIoTClient 14:8e8e42008807 785 newSASToken = STRING_clone(transport_state->deviceSasToken);
AzureIoTClient 14:8e8e42008807 786 }
Azure.IoT Build 7:07bc440836b3 787
Azure.IoT Build 7:07bc440836b3 788 if (newSASToken == NULL)
AzureIoTClient 4:57e049bce51e 789 {
AzureIoTClient 14:8e8e42008807 790 LogError("Could not generate a new SAS token for the CBS.");
Azure.IoT Build 10:75c5e0d8537d 791 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 792 }
Azure.IoT Build 7:07bc440836b3 793 else if (cbs_put_token(transport_state->cbs, CBS_AUDIENCE, STRING_c_str(transport_state->devicesPath), STRING_c_str(newSASToken), on_put_token_complete, transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 794 {
AzureIoTClient 14:8e8e42008807 795 LogError("Failed applying new SAS token to CBS.");
Azure.IoT Build 10:75c5e0d8537d 796 result = RESULT_FAILURE;
AzureIoTClient 4:57e049bce51e 797 }
AzureIoTClient 4:57e049bce51e 798 else
AzureIoTClient 4:57e049bce51e 799 {
Azure.IoT Build 7:07bc440836b3 800 transport_state->cbs_state = CBS_STATE_AUTH_IN_PROGRESS;
Azure.IoT Build 7:07bc440836b3 801 transport_state->current_sas_token_create_time = sas_token_create_time;
Azure.IoT Build 7:07bc440836b3 802 result = RESULT_OK;
AzureIoTClient 4:57e049bce51e 803 }
Azure.IoT Build 7:07bc440836b3 804
Azure.IoT Build 7:07bc440836b3 805 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_145: [Each new SAS token created shall be deleted from memory immediately after sending it to CBS]
Azure.IoT Build 7:07bc440836b3 806 if (newSASToken != NULL)
Azure.IoT Build 10:75c5e0d8537d 807 {
Azure.IoT Build 7:07bc440836b3 808 STRING_delete(newSASToken);
Azure.IoT Build 10:75c5e0d8537d 809 }
Azure.IoT Build 7:07bc440836b3 810
AzureIoTClient 4:57e049bce51e 811 return result;
AzureIoTClient 4:57e049bce51e 812 }
AzureIoTClient 4:57e049bce51e 813
Azure.IoT Build 7:07bc440836b3 814 static int verifyAuthenticationTimeout(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 815 {
Azure.IoT Build 7:07bc440836b3 816 return ((getSecondsSinceEpoch() - transport_state->current_sas_token_create_time) * 1000 >= transport_state->cbs_request_timeout) ? RESULT_TIMEOUT : RESULT_OK;
Azure.IoT Build 7:07bc440836b3 817 }
AzureIoTClient 4:57e049bce51e 818
Azure.IoT Build 7:07bc440836b3 819 static void attachDeviceClientTypeToLink(LINK_HANDLE link)
Azure.IoT Build 7:07bc440836b3 820 {
Azure.IoT Build 10:75c5e0d8537d 821 fields attach_properties;
Azure.IoT Build 10:75c5e0d8537d 822 AMQP_VALUE deviceClientTypeKeyName;
Azure.IoT Build 10:75c5e0d8537d 823 AMQP_VALUE deviceClientTypeValue;
Azure.IoT Build 7:07bc440836b3 824 int result;
Azure.IoT Build 7:07bc440836b3 825
Azure.IoT Build 7:07bc440836b3 826 //
Azure.IoT Build 7:07bc440836b3 827 // Attempt to add the device client type string to the attach properties.
Azure.IoT Build 7:07bc440836b3 828 // If this doesn't happen, well, this isn't that important. We can operate
Azure.IoT Build 7:07bc440836b3 829 // without this property. It's worth noting that even though we are going
Azure.IoT Build 7:07bc440836b3 830 // on, the reasons any of these operations fail don't bode well for the
Azure.IoT Build 7:07bc440836b3 831 // actual upcoming attach.
Azure.IoT Build 7:07bc440836b3 832 //
Azure.IoT Build 7:07bc440836b3 833
Azure.IoT Build 7:07bc440836b3 834 // Codes_SRS_IOTHUBTRANSPORTAMQP_06_187: [If IotHubTransportAMQP_DoWork fails to create an attach properties map and assign that map to the link the function will STILL proceed with the attempt to create the message sender.]
Azure.IoT Build 7:07bc440836b3 835
Azure.IoT Build 7:07bc440836b3 836 if ((attach_properties = amqpvalue_create_map()) == NULL)
AzureIoTClient 4:57e049bce51e 837 {
AzureIoTClient 13:a4af7c301e02 838 LogError("Failed to create the map for device client type.");
AzureIoTClient 4:57e049bce51e 839 }
Azure.IoT Build 10:75c5e0d8537d 840 else
Azure.IoT Build 7:07bc440836b3 841 {
Azure.IoT Build 10:75c5e0d8537d 842 if ((deviceClientTypeKeyName = amqpvalue_create_symbol("com.microsoft:client-version")) == NULL)
Azure.IoT Build 10:75c5e0d8537d 843 {
AzureIoTClient 13:a4af7c301e02 844 LogError("Failed to create the key name for the device client type.");
Azure.IoT Build 10:75c5e0d8537d 845 }
Azure.IoT Build 10:75c5e0d8537d 846 else
Azure.IoT Build 10:75c5e0d8537d 847 {
Azure.IoT Build 10:75c5e0d8537d 848 if ((deviceClientTypeValue = amqpvalue_create_string(CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION)) == NULL)
Azure.IoT Build 10:75c5e0d8537d 849 {
AzureIoTClient 13:a4af7c301e02 850 LogError("Failed to create the key value for the device client type.");
Azure.IoT Build 10:75c5e0d8537d 851 }
Azure.IoT Build 10:75c5e0d8537d 852 else
Azure.IoT Build 10:75c5e0d8537d 853 {
Azure.IoT Build 10:75c5e0d8537d 854 if ((result = amqpvalue_set_map_value(attach_properties, deviceClientTypeKeyName, deviceClientTypeValue)) != 0)
Azure.IoT Build 10:75c5e0d8537d 855 {
AzureIoTClient 13:a4af7c301e02 856 LogError("Failed to set the property map for the device client type. Error code is: %d", result);
Azure.IoT Build 10:75c5e0d8537d 857 }
Azure.IoT Build 10:75c5e0d8537d 858 else if ((result = link_set_attach_properties(link, attach_properties)) != 0)
Azure.IoT Build 10:75c5e0d8537d 859 {
AzureIoTClient 13:a4af7c301e02 860 LogError("Unable to attach the device client type to the link properties. Error code is: %d", result);
Azure.IoT Build 10:75c5e0d8537d 861 }
Azure.IoT Build 10:75c5e0d8537d 862
Azure.IoT Build 10:75c5e0d8537d 863 amqpvalue_destroy(deviceClientTypeValue);
Azure.IoT Build 10:75c5e0d8537d 864 }
Azure.IoT Build 10:75c5e0d8537d 865
Azure.IoT Build 10:75c5e0d8537d 866 amqpvalue_destroy(deviceClientTypeKeyName);
Azure.IoT Build 10:75c5e0d8537d 867 }
Azure.IoT Build 10:75c5e0d8537d 868
Azure.IoT Build 10:75c5e0d8537d 869 amqpvalue_destroy(attach_properties);
AzureIoTClient 4:57e049bce51e 870 }
Azure.IoT Build 7:07bc440836b3 871 }
Azure.IoT Build 7:07bc440836b3 872
Azure.IoT Build 10:75c5e0d8537d 873 static void destroyEventSender(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 874 {
Azure.IoT Build 7:07bc440836b3 875 if (transport_state->message_sender != NULL)
Azure.IoT Build 7:07bc440836b3 876 {
Azure.IoT Build 7:07bc440836b3 877 messagesender_destroy(transport_state->message_sender);
Azure.IoT Build 7:07bc440836b3 878 transport_state->message_sender = NULL;
Azure.IoT Build 7:07bc440836b3 879
Azure.IoT Build 7:07bc440836b3 880 link_destroy(transport_state->sender_link);
Azure.IoT Build 7:07bc440836b3 881 transport_state->sender_link = NULL;
AzureIoTClient 4:57e049bce51e 882 }
AzureIoTClient 4:57e049bce51e 883 }
AzureIoTClient 4:57e049bce51e 884
AzureIoTClient 13:a4af7c301e02 885 void on_event_sender_state_changed(void* context, MESSAGE_SENDER_STATE new_state, MESSAGE_SENDER_STATE previous_state)
AzureIoTClient 13:a4af7c301e02 886 {
AzureIoTClient 13:a4af7c301e02 887 LogInfo("Event sender state changed [%d->%d]", previous_state, new_state);
AzureIoTClient 13:a4af7c301e02 888 }
AzureIoTClient 13:a4af7c301e02 889
Azure.IoT Build 7:07bc440836b3 890 static int createEventSender(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 891 {
Azure.IoT Build 7:07bc440836b3 892 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 893
Azure.IoT Build 7:07bc440836b3 894 if (transport_state->message_sender == NULL)
AzureIoTClient 4:57e049bce51e 895 {
Azure.IoT Build 7:07bc440836b3 896 AMQP_VALUE source = NULL;
Azure.IoT Build 7:07bc440836b3 897 AMQP_VALUE target = NULL;
Azure.IoT Build 7:07bc440836b3 898
Azure.IoT Build 10:75c5e0d8537d 899 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_068: [IoTHubTransportAMQP_DoWork shall create the AMQP link for sending messages using 'source' as "ingress", target as the IoT hub FQDN, link name as "sender-link" and role as 'role_sender']
Azure.IoT Build 7:07bc440836b3 900 if ((source = messaging_create_source(MESSAGE_SENDER_SOURCE_ADDRESS)) == NULL)
AzureIoTClient 4:57e049bce51e 901 {
AzureIoTClient 13:a4af7c301e02 902 LogError("Failed creating AMQP messaging source attribute.");
AzureIoTClient 4:57e049bce51e 903 }
Azure.IoT Build 7:07bc440836b3 904 else if ((target = messaging_create_target(STRING_c_str(transport_state->targetAddress))) == NULL)
AzureIoTClient 4:57e049bce51e 905 {
AzureIoTClient 13:a4af7c301e02 906 LogError("Failed creating AMQP messaging target attribute.");
Azure.IoT Build 7:07bc440836b3 907 }
Azure.IoT Build 7:07bc440836b3 908 else if ((transport_state->sender_link = link_create(transport_state->session, MESSAGE_SENDER_LINK_NAME, role_sender, source, target)) == NULL)
Azure.IoT Build 7:07bc440836b3 909 {
Azure.IoT Build 7:07bc440836b3 910 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_069: [If IoTHubTransportAMQP_DoWork fails to create the AMQP link for sending messages, the function shall fail and return immediately, flagging the connection to be re-stablished]
AzureIoTClient 13:a4af7c301e02 911 LogError("Failed creating AMQP link for message sender.");
AzureIoTClient 4:57e049bce51e 912 }
AzureIoTClient 4:57e049bce51e 913 else
AzureIoTClient 4:57e049bce51e 914 {
Azure.IoT Build 10:75c5e0d8537d 915 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_119: [IoTHubTransportAMQP_DoWork shall apply a default value of 65536 for the parameter 'Link MAX message size']
Azure.IoT Build 7:07bc440836b3 916 if (link_set_max_message_size(transport_state->sender_link, MESSAGE_SENDER_MAX_LINK_SIZE) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 917 {
AzureIoTClient 13:a4af7c301e02 918 LogError("Failed setting AMQP link max message size.");
Azure.IoT Build 7:07bc440836b3 919 }
Azure.IoT Build 7:07bc440836b3 920
Azure.IoT Build 7:07bc440836b3 921 attachDeviceClientTypeToLink(transport_state->sender_link);
Azure.IoT Build 7:07bc440836b3 922
Azure.IoT Build 7:07bc440836b3 923 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_070: [IoTHubTransportAMQP_DoWork shall create the AMQP message sender using messagesender_create() AMQP API]
AzureIoTClient 13:a4af7c301e02 924 if ((transport_state->message_sender = messagesender_create(transport_state->sender_link, on_event_sender_state_changed, (void*)transport_state, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 925 {
Azure.IoT Build 7:07bc440836b3 926 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_071: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the AMQP message sender instance fails to be created, flagging the connection to be re-established]
AzureIoTClient 13:a4af7c301e02 927 LogError("Could not allocate AMQP message sender");
Azure.IoT Build 7:07bc440836b3 928 }
Azure.IoT Build 7:07bc440836b3 929 else
Azure.IoT Build 7:07bc440836b3 930 {
Azure.IoT Build 7:07bc440836b3 931 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_072: [IoTHubTransportAMQP_DoWork shall open the AMQP message sender using messagesender_open() AMQP API]
Azure.IoT Build 7:07bc440836b3 932 if (messagesender_open(transport_state->message_sender) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 933 {
Azure.IoT Build 7:07bc440836b3 934 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_073: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the AMQP message sender instance fails to be opened, flagging the connection to be re-established]
AzureIoTClient 13:a4af7c301e02 935 LogError("Failed opening the AMQP message sender.");
Azure.IoT Build 7:07bc440836b3 936 }
Azure.IoT Build 7:07bc440836b3 937 else
Azure.IoT Build 7:07bc440836b3 938 {
Azure.IoT Build 7:07bc440836b3 939 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 940 }
Azure.IoT Build 7:07bc440836b3 941 }
AzureIoTClient 4:57e049bce51e 942 }
Azure.IoT Build 7:07bc440836b3 943
Azure.IoT Build 7:07bc440836b3 944 if (source != NULL)
Azure.IoT Build 7:07bc440836b3 945 amqpvalue_destroy(source);
Azure.IoT Build 7:07bc440836b3 946 if (target != NULL)
Azure.IoT Build 7:07bc440836b3 947 amqpvalue_destroy(target);
AzureIoTClient 4:57e049bce51e 948 }
Azure.IoT Build 11:62d7b956e76e 949
Azure.IoT Build 7:07bc440836b3 950 return result;
AzureIoTClient 4:57e049bce51e 951 }
Azure.IoT Build 7:07bc440836b3 952
Azure.IoT Build 7:07bc440836b3 953 static int destroyMessageReceiver(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 954 {
Azure.IoT Build 7:07bc440836b3 955 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 956
Azure.IoT Build 7:07bc440836b3 957 if (transport_state->message_receiver != NULL)
AzureIoTClient 4:57e049bce51e 958 {
Azure.IoT Build 7:07bc440836b3 959 if (messagereceiver_close(transport_state->message_receiver) != RESULT_OK)
AzureIoTClient 4:57e049bce51e 960 {
AzureIoTClient 13:a4af7c301e02 961 LogError("Failed closing the AMQP message receiver.");
Azure.IoT Build 7:07bc440836b3 962 }
Azure.IoT Build 7:07bc440836b3 963
Azure.IoT Build 7:07bc440836b3 964 messagereceiver_destroy(transport_state->message_receiver);
Azure.IoT Build 7:07bc440836b3 965
Azure.IoT Build 7:07bc440836b3 966 transport_state->message_receiver = NULL;
Azure.IoT Build 7:07bc440836b3 967
Azure.IoT Build 7:07bc440836b3 968 link_destroy(transport_state->receiver_link);
Azure.IoT Build 7:07bc440836b3 969
Azure.IoT Build 7:07bc440836b3 970 transport_state->receiver_link = NULL;
Azure.IoT Build 7:07bc440836b3 971
Azure.IoT Build 7:07bc440836b3 972 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 973 }
Azure.IoT Build 7:07bc440836b3 974
Azure.IoT Build 7:07bc440836b3 975 return result;
Azure.IoT Build 7:07bc440836b3 976 }
Azure.IoT Build 7:07bc440836b3 977
Azure.IoT Build 7:07bc440836b3 978 static int createMessageReceiver(AMQP_TRANSPORT_INSTANCE* transport_state, IOTHUB_CLIENT_LL_HANDLE iothub_client_handle)
Azure.IoT Build 7:07bc440836b3 979 {
Azure.IoT Build 7:07bc440836b3 980 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 981
AzureIoTClient 14:8e8e42008807 982 if (transport_state->message_receiver == NULL)
Azure.IoT Build 7:07bc440836b3 983 {
Azure.IoT Build 7:07bc440836b3 984 AMQP_VALUE source = NULL;
Azure.IoT Build 7:07bc440836b3 985 AMQP_VALUE target = NULL;
Azure.IoT Build 7:07bc440836b3 986
Azure.IoT Build 10:75c5e0d8537d 987 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_074: [IoTHubTransportAMQP_DoWork shall create the AMQP link for receiving messages using 'source' as messageReceiveAddress, target as the "ingress-rx", link name as "receiver-link" and role as 'role_receiver']
Azure.IoT Build 7:07bc440836b3 988 if ((source = messaging_create_source(STRING_c_str(transport_state->messageReceiveAddress))) == NULL)
Azure.IoT Build 7:07bc440836b3 989 {
AzureIoTClient 13:a4af7c301e02 990 LogError("Failed creating AMQP message receiver source attribute.");
Azure.IoT Build 7:07bc440836b3 991 }
Azure.IoT Build 7:07bc440836b3 992 else if ((target = messaging_create_target(MESSAGE_RECEIVER_TARGET_ADDRESS)) == NULL)
Azure.IoT Build 7:07bc440836b3 993 {
AzureIoTClient 13:a4af7c301e02 994 LogError("Failed creating AMQP message receiver target attribute.");
Azure.IoT Build 7:07bc440836b3 995 }
Azure.IoT Build 7:07bc440836b3 996 else if ((transport_state->receiver_link = link_create(transport_state->session, MESSAGE_RECEIVER_LINK_NAME, role_receiver, source, target)) == NULL)
Azure.IoT Build 7:07bc440836b3 997 {
Azure.IoT Build 7:07bc440836b3 998 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_075: [If IoTHubTransportAMQP_DoWork fails to create the AMQP link for receiving messages, the function shall fail and return immediately, flagging the connection to be re-stablished]
AzureIoTClient 13:a4af7c301e02 999 LogError("Failed creating AMQP link for message receiver.");
Azure.IoT Build 7:07bc440836b3 1000 }
Azure.IoT Build 7:07bc440836b3 1001 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_076: [IoTHubTransportAMQP_DoWork shall set the receiver link settle mode as receiver_settle_mode_first]
Azure.IoT Build 7:07bc440836b3 1002 else if (link_set_rcv_settle_mode(transport_state->receiver_link, receiver_settle_mode_first) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1003 {
Azure.IoT Build 7:07bc440836b3 1004 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_141: [If IoTHubTransportAMQP_DoWork fails to set the settle mode on the AMQP link for receiving messages, the function shall fail and return immediately, flagging the connection to be re-stablished]
AzureIoTClient 13:a4af7c301e02 1005 LogError("Failed setting AMQP link settle mode for message receiver.");
AzureIoTClient 4:57e049bce51e 1006 }
AzureIoTClient 4:57e049bce51e 1007 else
AzureIoTClient 4:57e049bce51e 1008 {
Azure.IoT Build 10:75c5e0d8537d 1009 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_119: [IoTHubTransportAMQP_DoWork shall apply a default value of 65536 for the parameter 'Link MAX message size']
Azure.IoT Build 7:07bc440836b3 1010 if (link_set_max_message_size(transport_state->receiver_link, MESSAGE_RECEIVER_MAX_LINK_SIZE) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1011 {
AzureIoTClient 13:a4af7c301e02 1012 LogError("Failed setting AMQP link max message size for message receiver.");
Azure.IoT Build 7:07bc440836b3 1013 }
Azure.IoT Build 7:07bc440836b3 1014
Azure.IoT Build 7:07bc440836b3 1015 attachDeviceClientTypeToLink(transport_state->receiver_link);
AzureIoTClient 4:57e049bce51e 1016
Azure.IoT Build 7:07bc440836b3 1017 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_077: [IoTHubTransportAMQP_DoWork shall create the AMQP message receiver using messagereceiver_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 1018 if ((transport_state->message_receiver = messagereceiver_create(transport_state->receiver_link, NULL, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 1019 {
Azure.IoT Build 7:07bc440836b3 1020 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_078: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the AMQP message receiver instance fails to be created, flagging the connection to be re-established]
AzureIoTClient 13:a4af7c301e02 1021 LogError("Could not allocate AMQP message receiver.");
Azure.IoT Build 7:07bc440836b3 1022 }
Azure.IoT Build 7:07bc440836b3 1023 else
AzureIoTClient 4:57e049bce51e 1024 {
Azure.IoT Build 7:07bc440836b3 1025 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_079: [IoTHubTransportAMQP_DoWork shall open the AMQP message receiver using messagereceiver_open() AMQP API, passing a callback function for handling C2D incoming messages]
Azure.IoT Build 10:75c5e0d8537d 1026 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_123: [IoTHubTransportAMQP_DoWork shall create each AMQP message_receiver passing the 'on_message_received' as the callback function]
Azure.IoT Build 7:07bc440836b3 1027 if (messagereceiver_open(transport_state->message_receiver, on_message_received, (const void*)iothub_client_handle) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1028 {
Azure.IoT Build 7:07bc440836b3 1029 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_080: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the AMQP message receiver instance fails to be opened, flagging the connection to be re-established]
AzureIoTClient 13:a4af7c301e02 1030 LogError("Failed opening the AMQP message receiver.");
Azure.IoT Build 7:07bc440836b3 1031 }
Azure.IoT Build 7:07bc440836b3 1032 else
Azure.IoT Build 7:07bc440836b3 1033 {
Azure.IoT Build 7:07bc440836b3 1034 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 1035 }
AzureIoTClient 4:57e049bce51e 1036 }
AzureIoTClient 4:57e049bce51e 1037 }
Azure.IoT Build 7:07bc440836b3 1038
Azure.IoT Build 7:07bc440836b3 1039 if (source != NULL)
Azure.IoT Build 7:07bc440836b3 1040 amqpvalue_destroy(source);
Azure.IoT Build 7:07bc440836b3 1041 if (target != NULL)
Azure.IoT Build 7:07bc440836b3 1042 amqpvalue_destroy(target);
AzureIoTClient 4:57e049bce51e 1043 }
Azure.IoT Build 7:07bc440836b3 1044
Azure.IoT Build 7:07bc440836b3 1045 return result;
AzureIoTClient 4:57e049bce51e 1046 }
AzureIoTClient 4:57e049bce51e 1047
Azure.IoT Build 7:07bc440836b3 1048 static int sendPendingEvents(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 1049 {
Azure.IoT Build 7:07bc440836b3 1050 int result = RESULT_OK;
Azure.IoT Build 10:75c5e0d8537d 1051 IOTHUB_MESSAGE_LIST* message;
Azure.IoT Build 7:07bc440836b3 1052
Azure.IoT Build 7:07bc440836b3 1053 while ((message = getNextEventToSend(transport_state)) != NULL)
AzureIoTClient 4:57e049bce51e 1054 {
Azure.IoT Build 7:07bc440836b3 1055 result = RESULT_FAILURE;
Azure.IoT Build 11:62d7b956e76e 1056
Azure.IoT Build 7:07bc440836b3 1057 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
Azure.IoT Build 10:75c5e0d8537d 1058 const unsigned char* messageContent;
Azure.IoT Build 10:75c5e0d8537d 1059 size_t messageContentSize;
Azure.IoT Build 12:841a4c36bd36 1060 MESSAGE_HANDLE amqp_message = NULL;
Azure.IoT Build 7:07bc440836b3 1061 bool is_message_error = false;
Azure.IoT Build 7:07bc440836b3 1062
Azure.IoT Build 10:75c5e0d8537d 1063 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_086: [IoTHubTransportAMQP_DoWork shall move queued events to an "in-progress" list right before processing them for sending]
Azure.IoT Build 12:841a4c36bd36 1064 trackEventInProgress(message, transport_state);
Azure.IoT Build 12:841a4c36bd36 1065
Azure.IoT Build 7:07bc440836b3 1066 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_087: [If the event contains a message of type IOTHUBMESSAGE_BYTEARRAY, IoTHubTransportAMQP_DoWork shall obtain its char* representation and size using IoTHubMessage_GetByteArray()]
Azure.IoT Build 12:841a4c36bd36 1067 if (contentType == IOTHUBMESSAGE_BYTEARRAY &&
Azure.IoT Build 7:07bc440836b3 1068 IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &messageContentSize) != IOTHUB_MESSAGE_OK)
AzureIoTClient 4:57e049bce51e 1069 {
AzureIoTClient 13:a4af7c301e02 1070 LogError("Failed getting the BYTE array representation of the event content to be sent.");
Azure.IoT Build 7:07bc440836b3 1071 is_message_error = true;
Azure.IoT Build 7:07bc440836b3 1072 }
Azure.IoT Build 7:07bc440836b3 1073 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_089: [If the event contains a message of type IOTHUBMESSAGE_STRING, IoTHubTransportAMQP_DoWork shall obtain its char* representation using IoTHubMessage_GetString()]
Azure.IoT Build 7:07bc440836b3 1074 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_090: [If the event contains a message of type IOTHUBMESSAGE_STRING, IoTHubTransportAMQP_DoWork shall obtain the size of its char* representation using strlen()]
Azure.IoT Build 7:07bc440836b3 1075 else if (contentType == IOTHUBMESSAGE_STRING &&
Azure.IoT Build 10:75c5e0d8537d 1076 ((messageContent = IoTHubMessage_GetString(message->messageHandle)) == NULL))
Azure.IoT Build 7:07bc440836b3 1077 {
AzureIoTClient 13:a4af7c301e02 1078 LogError("Failed getting the STRING representation of the event content to be sent.");
Azure.IoT Build 7:07bc440836b3 1079 is_message_error = true;
AzureIoTClient 4:57e049bce51e 1080 }
Azure.IoT Build 7:07bc440836b3 1081 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_092: [If the event contains a message of type IOTHUBMESSAGE_UNKNOWN, IoTHubTransportAMQP_DoWork shall remove the event from the in-progress list and invoke the upper layer callback reporting the error]
Azure.IoT Build 7:07bc440836b3 1082 else if (contentType == IOTHUBMESSAGE_UNKNOWN)
AzureIoTClient 4:57e049bce51e 1083 {
AzureIoTClient 13:a4af7c301e02 1084 LogError("Cannot send events with content type IOTHUBMESSAGE_UNKNOWN.");
Azure.IoT Build 7:07bc440836b3 1085 is_message_error = true;
Azure.IoT Build 7:07bc440836b3 1086 }
Azure.IoT Build 10:75c5e0d8537d 1087 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_093: [IoTHubTransportAMQP_DoWork shall create an amqp message using message_create() uAMQP API]
Azure.IoT Build 7:07bc440836b3 1088 else if ((amqp_message = message_create()) == NULL)
Azure.IoT Build 7:07bc440836b3 1089 {
AzureIoTClient 13:a4af7c301e02 1090 LogError("Failed allocating the AMQP message for sending the event.");
AzureIoTClient 4:57e049bce51e 1091 }
AzureIoTClient 4:57e049bce51e 1092 else
AzureIoTClient 4:57e049bce51e 1093 {
Azure.IoT Build 10:75c5e0d8537d 1094 BINARY_DATA binary_data;
Azure.IoT Build 10:75c5e0d8537d 1095
Azure.IoT Build 10:75c5e0d8537d 1096 if (contentType == IOTHUBMESSAGE_STRING)
Azure.IoT Build 10:75c5e0d8537d 1097 {
Azure.IoT Build 10:75c5e0d8537d 1098 messageContentSize = strlen(messageContent);
Azure.IoT Build 10:75c5e0d8537d 1099 }
Azure.IoT Build 10:75c5e0d8537d 1100
Azure.IoT Build 10:75c5e0d8537d 1101 binary_data.bytes = messageContent;
Azure.IoT Build 10:75c5e0d8537d 1102 binary_data.length = messageContentSize;
Azure.IoT Build 11:62d7b956e76e 1103
Azure.IoT Build 10:75c5e0d8537d 1104 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_095: [IoTHubTransportAMQP_DoWork shall set the AMQP message body using message_add_body_amqp_data() uAMQP API]
Azure.IoT Build 7:07bc440836b3 1105 if (message_add_body_amqp_data(amqp_message, binary_data) != RESULT_OK)
AzureIoTClient 4:57e049bce51e 1106 {
AzureIoTClient 13:a4af7c301e02 1107 LogError("Failed setting the body of the AMQP message.");
AzureIoTClient 4:57e049bce51e 1108 }
Azure.IoT Build 7:07bc440836b3 1109 else
AzureIoTClient 4:57e049bce51e 1110 {
Azure.IoT Build 10:75c5e0d8537d 1111 if (addPropertiesTouAMQPMessage(message->messageHandle, amqp_message) != 0)
Azure.IoT Build 7:07bc440836b3 1112 {
Azure.IoT Build 10:75c5e0d8537d 1113 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_01_014: [If any of the APIs fails while building the property map and setting it on the uAMQP message, IoTHubTransportAMQP_DoWork shall notify the failure by invoking the upper layer message send callback with IOTHUB_CLIENT_CONFIRMATION_ERROR.] */
Azure.IoT Build 10:75c5e0d8537d 1114 is_message_error = true;
Azure.IoT Build 7:07bc440836b3 1115 }
Azure.IoT Build 7:07bc440836b3 1116 else
Azure.IoT Build 7:07bc440836b3 1117 {
Azure.IoT Build 10:75c5e0d8537d 1118 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_097: [IoTHubTransportAMQP_DoWork shall pass the encoded AMQP message to AMQP for sending (along with on_message_send_complete callback) using messagesender_send()]
Azure.IoT Build 12:841a4c36bd36 1119 if (messagesender_send(transport_state->message_sender, amqp_message, on_message_send_complete, message) != RESULT_OK)
Azure.IoT Build 10:75c5e0d8537d 1120 {
AzureIoTClient 13:a4af7c301e02 1121 LogError("Failed sending the AMQP message.");
Azure.IoT Build 10:75c5e0d8537d 1122 }
Azure.IoT Build 10:75c5e0d8537d 1123 else
Azure.IoT Build 10:75c5e0d8537d 1124 {
Azure.IoT Build 10:75c5e0d8537d 1125 result = RESULT_OK;
Azure.IoT Build 10:75c5e0d8537d 1126 }
Azure.IoT Build 7:07bc440836b3 1127 }
AzureIoTClient 4:57e049bce51e 1128 }
Azure.IoT Build 7:07bc440836b3 1129 }
Azure.IoT Build 7:07bc440836b3 1130
Azure.IoT Build 7:07bc440836b3 1131 if (amqp_message != NULL)
Azure.IoT Build 7:07bc440836b3 1132 {
Azure.IoT Build 7:07bc440836b3 1133 // It can be destroyed because AMQP keeps a clone of the message.
Azure.IoT Build 7:07bc440836b3 1134 message_destroy(amqp_message);
Azure.IoT Build 7:07bc440836b3 1135 }
Azure.IoT Build 7:07bc440836b3 1136
Azure.IoT Build 7:07bc440836b3 1137 if (result != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1138 {
Azure.IoT Build 12:841a4c36bd36 1139 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_088: [If IoTHubMessage_GetByteArray() fails, IoTHubTransportAMQP_DoWork shall remove the event from the in-progress list and invoke the upper layer callback reporting the error]
Azure.IoT Build 12:841a4c36bd36 1140 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_091: [If IoTHubMessage_GetString() fails, IoTHubTransportAMQP_DoWork shall remove the event from the in-progress list and invoke the upper layer callback reporting the error]
Azure.IoT Build 12:841a4c36bd36 1141 if (is_message_error)
AzureIoTClient 5:8d58d20699dd 1142 {
Azure.IoT Build 12:841a4c36bd36 1143 on_message_send_complete(message, MESSAGE_SEND_ERROR);
AzureIoTClient 5:8d58d20699dd 1144 }
AzureIoTClient 4:57e049bce51e 1145 else
AzureIoTClient 4:57e049bce51e 1146 {
Azure.IoT Build 12:841a4c36bd36 1147 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_111: [If message_create() fails, IoTHubTransportAMQP_DoWork notify the failure, roll back the event to waitToSent list and return]
Azure.IoT Build 12:841a4c36bd36 1148 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_112: [If message_add_body_amqp_data() fails, IoTHubTransportAMQP_DoWork notify the failure, roll back the event to waitToSent list and return]
Azure.IoT Build 12:841a4c36bd36 1149 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_113: [If messagesender_send() fails, IoTHubTransportAMQP_DoWork notify the failure, roll back the event to waitToSent list and return]
Azure.IoT Build 12:841a4c36bd36 1150 rollEventBackToWaitList(message, transport_state);
Azure.IoT Build 12:841a4c36bd36 1151 break;
AzureIoTClient 4:57e049bce51e 1152 }
AzureIoTClient 4:57e049bce51e 1153 }
AzureIoTClient 4:57e049bce51e 1154 }
Azure.IoT Build 7:07bc440836b3 1155
Azure.IoT Build 7:07bc440836b3 1156 return result;
Azure.IoT Build 7:07bc440836b3 1157 }
Azure.IoT Build 7:07bc440836b3 1158
Azure.IoT Build 7:07bc440836b3 1159 static bool isSasTokenRefreshRequired(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 1160 {
AzureIoTClient 14:8e8e42008807 1161 if (transport_state->deviceSasToken != NULL)
AzureIoTClient 14:8e8e42008807 1162 {
AzureIoTClient 14:8e8e42008807 1163 return false;
AzureIoTClient 14:8e8e42008807 1164 }
AzureIoTClient 14:8e8e42008807 1165 else
AzureIoTClient 14:8e8e42008807 1166 {
Azure.IoT Build 7:07bc440836b3 1167 return ((getSecondsSinceEpoch() - transport_state->current_sas_token_create_time) >= (transport_state->sas_token_refresh_time / 1000)) ? true : false;
Azure.IoT Build 7:07bc440836b3 1168 }
AzureIoTClient 14:8e8e42008807 1169 }
Azure.IoT Build 7:07bc440836b3 1170
Azure.IoT Build 7:07bc440836b3 1171 static void prepareForConnectionRetry(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 1172 {
AzureIoTClient 13:a4af7c301e02 1173 destroyMessageReceiver(transport_state);
AzureIoTClient 13:a4af7c301e02 1174 destroyEventSender(transport_state);
Azure.IoT Build 7:07bc440836b3 1175 destroyConnection(transport_state);
AzureIoTClient 13:a4af7c301e02 1176 transport_state->connection_state = AMQP_MANAGEMENT_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 1177 rollEventsBackToWaitList(transport_state);
AzureIoTClient 4:57e049bce51e 1178 }
AzureIoTClient 4:57e049bce51e 1179
Azure.IoT Build 7:07bc440836b3 1180
Azure.IoT Build 7:07bc440836b3 1181 // API functions
Azure.IoT Build 7:07bc440836b3 1182
Azure.IoT Build 11:62d7b956e76e 1183 static TRANSPORT_LL_HANDLE IoTHubTransportAMQP_Create(const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 4:57e049bce51e 1184 {
Azure.IoT Build 7:07bc440836b3 1185 AMQP_TRANSPORT_INSTANCE* transport_state = NULL;
Azure.IoT Build 7:07bc440836b3 1186 bool cleanup_required = false;
Azure.IoT Build 10:75c5e0d8537d 1187 size_t deviceIdLength;
AzureIoTClient 4:57e049bce51e 1188
Azure.IoT Build 7:07bc440836b3 1189 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_005: [If parameter config (or its fields) is NULL then IoTHubTransportAMQP_Create shall fail and return NULL.]
Azure.IoT Build 7:07bc440836b3 1190 if (config == NULL || config->upperConfig == NULL || config->waitingToSend == NULL)
Azure.IoT Build 7:07bc440836b3 1191 {
AzureIoTClient 13:a4af7c301e02 1192 LogError("IoTHub AMQP client transport null configuration parameter.");
Azure.IoT Build 7:07bc440836b3 1193 }
Azure.IoT Build 7:07bc440836b3 1194 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_006: [IoTHubTransportAMQP_Create shall fail and return NULL if any fields of the config structure are NULL.]
AzureIoTClient 14:8e8e42008807 1195 // Codes_SRS_IOTHUBTRANSPORTAMQP_03_001: [IoTHubTransportAMQP_Create shall fail and return NULL if both deviceKey & deviceSasToken fields are NOT NULL.]
Azure.IoT Build 7:07bc440836b3 1196 else if (config->upperConfig->protocol == NULL)
Azure.IoT Build 7:07bc440836b3 1197 {
AzureIoTClient 13:a4af7c301e02 1198 LogError("Invalid configuration (NULL protocol detected)");
Azure.IoT Build 7:07bc440836b3 1199 }
Azure.IoT Build 7:07bc440836b3 1200 else if (config->upperConfig->deviceId == NULL)
Azure.IoT Build 7:07bc440836b3 1201 {
AzureIoTClient 13:a4af7c301e02 1202 LogError("Invalid configuration (NULL deviceId detected)");
Azure.IoT Build 7:07bc440836b3 1203 }
AzureIoTClient 14:8e8e42008807 1204 else if (config->upperConfig->deviceKey == NULL && config->upperConfig->deviceSasToken == NULL)
Azure.IoT Build 7:07bc440836b3 1205 {
AzureIoTClient 14:8e8e42008807 1206 LogError("Invalid configuration (NULL deviceKey/deviceSasToken detected)");
AzureIoTClient 14:8e8e42008807 1207 }
AzureIoTClient 14:8e8e42008807 1208 else if (config->upperConfig->deviceKey != NULL && config->upperConfig->deviceSasToken != NULL)
AzureIoTClient 14:8e8e42008807 1209 {
AzureIoTClient 14:8e8e42008807 1210 LogError("Invalid configuration (Both deviceKey and deviceSasToken are defined)");
Azure.IoT Build 7:07bc440836b3 1211 }
Azure.IoT Build 7:07bc440836b3 1212 else if (config->upperConfig->iotHubName == NULL)
Azure.IoT Build 7:07bc440836b3 1213 {
AzureIoTClient 13:a4af7c301e02 1214 LogError("Invalid configuration (NULL iotHubName detected)");
AzureIoTClient 4:57e049bce51e 1215 }
Azure.IoT Build 7:07bc440836b3 1216 else if (config->upperConfig->iotHubSuffix == NULL)
Azure.IoT Build 7:07bc440836b3 1217 {
AzureIoTClient 13:a4af7c301e02 1218 LogError("Invalid configuration (NULL iotHubSuffix detected)");
Azure.IoT Build 7:07bc440836b3 1219 }
Azure.IoT Build 7:07bc440836b3 1220 else if (!config->waitingToSend)
AzureIoTClient 4:57e049bce51e 1221 {
AzureIoTClient 13:a4af7c301e02 1222 LogError("Invalid configuration (NULL waitingToSend list detected)");
Azure.IoT Build 7:07bc440836b3 1223 }
Azure.IoT Build 7:07bc440836b3 1224 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_008: [IoTHubTransportAMQP_Create shall fail and return NULL if any config field of type string is zero length.]
Azure.IoT Build 7:07bc440836b3 1225 else if ((deviceIdLength = strlen(config->upperConfig->deviceId)) == 0 ||
Azure.IoT Build 7:07bc440836b3 1226 (strlen(config->upperConfig->iotHubName) == 0) ||
Azure.IoT Build 7:07bc440836b3 1227 (strlen(config->upperConfig->iotHubSuffix) == 0))
Azure.IoT Build 7:07bc440836b3 1228 {
AzureIoTClient 14:8e8e42008807 1229 LogError("Zero-length config parameter (deviceId, iotHubName or iotHubSuffix)");
Azure.IoT Build 7:07bc440836b3 1230 }
AzureIoTClient 14:8e8e42008807 1231 else if ((config->upperConfig->deviceKey != NULL) && (strlen(config->upperConfig->deviceKey) == 0))
AzureIoTClient 14:8e8e42008807 1232 {
AzureIoTClient 14:8e8e42008807 1233 LogError("Zero-length config parameter (deviceKey)");
AzureIoTClient 14:8e8e42008807 1234 }
AzureIoTClient 14:8e8e42008807 1235 else if ((config->upperConfig->deviceSasToken != NULL) && (strlen(config->upperConfig->deviceSasToken) == 0))
AzureIoTClient 14:8e8e42008807 1236 {
AzureIoTClient 14:8e8e42008807 1237 LogError("Zero-length config parameter (deviceSasToken)");
AzureIoTClient 14:8e8e42008807 1238 }
Azure.IoT Build 7:07bc440836b3 1239 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_007: [IoTHubTransportAMQP_Create shall fail and return NULL if the deviceId length is greater than 128.]
Azure.IoT Build 7:07bc440836b3 1240 else if (deviceIdLength > 128U)
Azure.IoT Build 7:07bc440836b3 1241 {
AzureIoTClient 13:a4af7c301e02 1242 LogError("deviceId is too long");
Azure.IoT Build 7:07bc440836b3 1243 }
Azure.IoT Build 7:07bc440836b3 1244 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_134: [IoTHubTransportAMQP_Create shall fail and return NULL if the combined length of config->iotHubName and config->iotHubSuffix exceeds 254 bytes (RFC1035)]
Azure.IoT Build 7:07bc440836b3 1245 else if ((strlen(config->upperConfig->iotHubName) + strlen(config->upperConfig->iotHubSuffix)) > (RFC1035_MAX_FQDN_LENGTH - 1))
Azure.IoT Build 7:07bc440836b3 1246 {
AzureIoTClient 13:a4af7c301e02 1247 LogError("The lengths of iotHubName and iotHubSuffix together exceed the maximum FQDN length allowed (RFC 1035)");
AzureIoTClient 4:57e049bce51e 1248 }
AzureIoTClient 4:57e049bce51e 1249 else
AzureIoTClient 4:57e049bce51e 1250 {
Azure.IoT Build 10:75c5e0d8537d 1251 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_009: [IoTHubTransportAMQP_Create shall fail and return NULL if memory allocation of the transport's internal state structure fails.]
Azure.IoT Build 7:07bc440836b3 1252 transport_state = (AMQP_TRANSPORT_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_INSTANCE));
AzureIoTClient 4:57e049bce51e 1253
Azure.IoT Build 7:07bc440836b3 1254 if (transport_state == NULL)
AzureIoTClient 4:57e049bce51e 1255 {
AzureIoTClient 13:a4af7c301e02 1256 LogError("Could not allocate AMQP transport state");
AzureIoTClient 4:57e049bce51e 1257 }
AzureIoTClient 4:57e049bce51e 1258 else
AzureIoTClient 4:57e049bce51e 1259 {
Azure.IoT Build 7:07bc440836b3 1260 transport_state->iotHubHostFqdn = NULL;
Azure.IoT Build 7:07bc440836b3 1261 transport_state->iotHubPort = DEFAULT_IOTHUB_AMQP_PORT;
Azure.IoT Build 7:07bc440836b3 1262 transport_state->deviceKey = NULL;
AzureIoTClient 14:8e8e42008807 1263 transport_state->deviceSasToken = NULL;
Azure.IoT Build 7:07bc440836b3 1264 transport_state->devicesPath = NULL;
Azure.IoT Build 7:07bc440836b3 1265 transport_state->messageReceiveAddress = NULL;
Azure.IoT Build 7:07bc440836b3 1266 transport_state->sasTokenKeyName = NULL;
Azure.IoT Build 7:07bc440836b3 1267 transport_state->targetAddress = NULL;
Azure.IoT Build 7:07bc440836b3 1268 transport_state->waitingToSend = config->waitingToSend;
Azure.IoT Build 7:07bc440836b3 1269
Azure.IoT Build 7:07bc440836b3 1270 transport_state->cbs = NULL;
Azure.IoT Build 7:07bc440836b3 1271 transport_state->cbs_state = CBS_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 1272 transport_state->current_sas_token_create_time = 0;
Azure.IoT Build 7:07bc440836b3 1273 transport_state->connection = NULL;
Azure.IoT Build 7:07bc440836b3 1274 transport_state->connection_state = AMQP_MANAGEMENT_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 1275 transport_state->connection_establish_time = 0;
Azure.IoT Build 7:07bc440836b3 1276 transport_state->iothub_client_handle = NULL;
Azure.IoT Build 7:07bc440836b3 1277 transport_state->receive_messages = false;
Azure.IoT Build 7:07bc440836b3 1278 transport_state->message_receiver = NULL;
Azure.IoT Build 7:07bc440836b3 1279 transport_state->message_sender = NULL;
Azure.IoT Build 7:07bc440836b3 1280 transport_state->receiver_link = NULL;
Azure.IoT Build 7:07bc440836b3 1281 transport_state->sasl_io = NULL;
Azure.IoT Build 7:07bc440836b3 1282 transport_state->sasl_mechanism = NULL;
Azure.IoT Build 7:07bc440836b3 1283 transport_state->sender_link = NULL;
Azure.IoT Build 7:07bc440836b3 1284 transport_state->session = NULL;
Azure.IoT Build 7:07bc440836b3 1285 transport_state->tls_io = NULL;
Azure.IoT Build 10:75c5e0d8537d 1286 transport_state->tls_io_transport_provider = getTLSIOTransport;
Azure.IoT Build 10:75c5e0d8537d 1287 transport_state->isRegistered = false;
Azure.IoT Build 7:07bc440836b3 1288
Azure.IoT Build 7:07bc440836b3 1289 transport_state->waitingToSend = config->waitingToSend;
Azure.IoT Build 7:07bc440836b3 1290 DList_InitializeListHead(&transport_state->inProgress);
Azure.IoT Build 7:07bc440836b3 1291
Azure.IoT Build 7:07bc440836b3 1292 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_010: [IoTHubTransportAMQP_Create shall create an immutable string, referred to as iotHubHostFqdn, from the following pieces: config->iotHubName + "." + config->iotHubSuffix.]
Azure.IoT Build 7:07bc440836b3 1293 if ((transport_state->iotHubHostFqdn = concat3Params(config->upperConfig->iotHubName, ".", config->upperConfig->iotHubSuffix)) == NULL)
AzureIoTClient 4:57e049bce51e 1294 {
AzureIoTClient 13:a4af7c301e02 1295 LogError("Failed to set transport_state->iotHubHostFqdn.");
Azure.IoT Build 7:07bc440836b3 1296 cleanup_required = true;
AzureIoTClient 4:57e049bce51e 1297 }
Azure.IoT Build 10:75c5e0d8537d 1298 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_012: [IoTHubTransportAMQP_Create shall create an immutable string, referred to as devicesPath, from the following parts: host_fqdn + "/devices/" + deviceId.]
Azure.IoT Build 7:07bc440836b3 1299 else if ((transport_state->devicesPath = concat3Params(STRING_c_str(transport_state->iotHubHostFqdn), "/devices/", config->upperConfig->deviceId)) == NULL)
AzureIoTClient 4:57e049bce51e 1300 {
Azure.IoT Build 7:07bc440836b3 1301 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_013: [If creating devicesPath fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
AzureIoTClient 13:a4af7c301e02 1302 LogError("Failed to allocate transport_state->devicesPath.");
Azure.IoT Build 7:07bc440836b3 1303 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1304 }
Azure.IoT Build 7:07bc440836b3 1305 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_014: [IoTHubTransportAMQP_Create shall create an immutable string, referred to as targetAddress, from the following parts: "amqps://" + devicesPath + "/messages/events".]
Azure.IoT Build 7:07bc440836b3 1306 else if ((transport_state->targetAddress = concat3Params("amqps://", STRING_c_str(transport_state->devicesPath), "/messages/events")) == NULL)
Azure.IoT Build 7:07bc440836b3 1307 {
Azure.IoT Build 7:07bc440836b3 1308 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_015: [If creating the targetAddress fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
AzureIoTClient 13:a4af7c301e02 1309 LogError("Failed to allocate transport_state->targetAddress.");
Azure.IoT Build 7:07bc440836b3 1310 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1311 }
Azure.IoT Build 7:07bc440836b3 1312 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_053: [IoTHubTransportAMQP_Create shall define the source address for receiving messages as "amqps://" + devicesPath + "/messages/devicebound", stored in the transport handle as messageReceiveAddress]
Azure.IoT Build 7:07bc440836b3 1313 else if ((transport_state->messageReceiveAddress = concat3Params("amqps://", STRING_c_str(transport_state->devicesPath), "/messages/devicebound")) == NULL)
Azure.IoT Build 7:07bc440836b3 1314 {
Azure.IoT Build 7:07bc440836b3 1315 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_054: [If creating the messageReceiveAddress fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
AzureIoTClient 13:a4af7c301e02 1316 LogError("Failed to allocate transport_state->messageReceiveAddress.");
Azure.IoT Build 7:07bc440836b3 1317 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1318 }
Azure.IoT Build 7:07bc440836b3 1319 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_016: [IoTHubTransportAMQP_Create shall initialize handle->sasTokenKeyName with a zero-length STRING_HANDLE instance.]
Azure.IoT Build 7:07bc440836b3 1320 else if ((transport_state->sasTokenKeyName = STRING_new()) == NULL)
Azure.IoT Build 7:07bc440836b3 1321 {
Azure.IoT Build 7:07bc440836b3 1322 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_017: [If IoTHubTransportAMQP_Create fails to initialize handle->sasTokenKeyName with a zero-length STRING the function shall fail and return NULL.]
AzureIoTClient 13:a4af7c301e02 1323 LogError("Failed to allocate transport_state->sasTokenKeyName.");
Azure.IoT Build 7:07bc440836b3 1324 cleanup_required = true;
AzureIoTClient 14:8e8e42008807 1325 }
AzureIoTClient 14:8e8e42008807 1326 else if (config->upperConfig->deviceSasToken != NULL)
AzureIoTClient 14:8e8e42008807 1327 {
AzureIoTClient 14:8e8e42008807 1328 if ((transport_state->deviceSasToken = STRING_construct(config->upperConfig->deviceSasToken)) == NULL)
AzureIoTClient 14:8e8e42008807 1329 {
AzureIoTClient 14:8e8e42008807 1330 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_019: [If IoTHubTransportAMQP_Create fails to copy config->deviceKey, the function shall fail and return NULL.]
AzureIoTClient 14:8e8e42008807 1331 LogError("Failed to allocate transport_state->deviceSasToken.");
AzureIoTClient 14:8e8e42008807 1332 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1333 }
AzureIoTClient 14:8e8e42008807 1334 }
AzureIoTClient 14:8e8e42008807 1335 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_018: [IoTHubTransportAMQP_Create shall store a copy of config->deviceKey (passed by upper layer) into the transport’s own deviceKey field.]
AzureIoTClient 14:8e8e42008807 1336 else if ((config->upperConfig->deviceKey != NULL) && ((transport_state->deviceKey = STRING_new()) == NULL ||
AzureIoTClient 14:8e8e42008807 1337 STRING_copy(transport_state->deviceKey, config->upperConfig->deviceKey) != 0))
Azure.IoT Build 7:07bc440836b3 1338 {
Azure.IoT Build 7:07bc440836b3 1339 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_019: [If IoTHubTransportAMQP_Create fails to copy config->deviceKey, the function shall fail and return NULL.]
AzureIoTClient 13:a4af7c301e02 1340 LogError("Failed to allocate transport_state->deviceKey.");
Azure.IoT Build 7:07bc440836b3 1341 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1342 }
Azure.IoT Build 11:62d7b956e76e 1343 else
Azure.IoT Build 7:07bc440836b3 1344 {
Azure.IoT Build 7:07bc440836b3 1345 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_020: [IoTHubTransportAMQP_Create shall set parameter transport_state->sas_token_lifetime with the default value of 3600000 (milliseconds).]
Azure.IoT Build 7:07bc440836b3 1346 transport_state->sas_token_lifetime = DEFAULT_SAS_TOKEN_LIFETIME_MS;
Azure.IoT Build 7:07bc440836b3 1347
Azure.IoT Build 7:07bc440836b3 1348 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_128: [IoTHubTransportAMQP_Create shall set parameter transport_state->sas_token_refresh_time with the default value of sas_token_lifetime/2 (milliseconds).]
Azure.IoT Build 7:07bc440836b3 1349 transport_state->sas_token_refresh_time = transport_state->sas_token_lifetime / 2;
Azure.IoT Build 7:07bc440836b3 1350
Azure.IoT Build 7:07bc440836b3 1351 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_129 : [IoTHubTransportAMQP_Create shall set parameter transport_state->cbs_request_timeout with the default value of 30000 (milliseconds).]
Azure.IoT Build 7:07bc440836b3 1352 transport_state->cbs_request_timeout = DEFAULT_CBS_REQUEST_TIMEOUT_MS;
AzureIoTClient 4:57e049bce51e 1353 }
AzureIoTClient 4:57e049bce51e 1354 }
AzureIoTClient 4:57e049bce51e 1355 }
Azure.IoT Build 7:07bc440836b3 1356
Azure.IoT Build 7:07bc440836b3 1357 if (cleanup_required)
Azure.IoT Build 7:07bc440836b3 1358 {
AzureIoTClient 14:8e8e42008807 1359 if (transport_state->deviceSasToken != NULL)
AzureIoTClient 14:8e8e42008807 1360 STRING_delete(transport_state->deviceSasToken);
Azure.IoT Build 7:07bc440836b3 1361 if (transport_state->deviceKey != NULL)
Azure.IoT Build 7:07bc440836b3 1362 STRING_delete(transport_state->deviceKey);
Azure.IoT Build 7:07bc440836b3 1363 if (transport_state->sasTokenKeyName != NULL)
Azure.IoT Build 7:07bc440836b3 1364 STRING_delete(transport_state->sasTokenKeyName);
Azure.IoT Build 7:07bc440836b3 1365 if (transport_state->targetAddress != NULL)
Azure.IoT Build 7:07bc440836b3 1366 STRING_delete(transport_state->targetAddress);
Azure.IoT Build 7:07bc440836b3 1367 if (transport_state->messageReceiveAddress != NULL)
Azure.IoT Build 7:07bc440836b3 1368 STRING_delete(transport_state->messageReceiveAddress);
Azure.IoT Build 7:07bc440836b3 1369 if (transport_state->devicesPath != NULL)
Azure.IoT Build 7:07bc440836b3 1370 STRING_delete(transport_state->devicesPath);
Azure.IoT Build 7:07bc440836b3 1371 if (transport_state->iotHubHostFqdn != NULL)
Azure.IoT Build 7:07bc440836b3 1372 STRING_delete(transport_state->iotHubHostFqdn);
Azure.IoT Build 7:07bc440836b3 1373
Azure.IoT Build 7:07bc440836b3 1374 free(transport_state);
Azure.IoT Build 7:07bc440836b3 1375 transport_state = NULL;
Azure.IoT Build 7:07bc440836b3 1376 }
Azure.IoT Build 7:07bc440836b3 1377
Azure.IoT Build 7:07bc440836b3 1378 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_023: [If IoTHubTransportAMQP_Create succeeds it shall return a non-NULL pointer to the structure that represents the transport.]
Azure.IoT Build 7:07bc440836b3 1379 return transport_state;
AzureIoTClient 4:57e049bce51e 1380 }
AzureIoTClient 4:57e049bce51e 1381
Azure.IoT Build 11:62d7b956e76e 1382 static void IoTHubTransportAMQP_Destroy(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 4:57e049bce51e 1383 {
Azure.IoT Build 7:07bc440836b3 1384 if (handle != NULL)
Azure.IoT Build 7:07bc440836b3 1385 {
Azure.IoT Build 7:07bc440836b3 1386 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 11:62d7b956e76e 1387
Azure.IoT Build 7:07bc440836b3 1388 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_024: [IoTHubTransportAMQP_Destroy shall destroy the AMQP message_sender.]
Azure.IoT Build 7:07bc440836b3 1389 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_029 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP link.]
Azure.IoT Build 7:07bc440836b3 1390 destroyEventSender(transport_state);
Azure.IoT Build 7:07bc440836b3 1391
Azure.IoT Build 7:07bc440836b3 1392 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_025: [IoTHubTransportAMQP_Destroy shall destroy the AMQP message_receiver.]
Azure.IoT Build 7:07bc440836b3 1393 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_029 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP link.]
Azure.IoT Build 7:07bc440836b3 1394 destroyMessageReceiver(transport_state);
Azure.IoT Build 7:07bc440836b3 1395
Azure.IoT Build 7:07bc440836b3 1396 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_027 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP cbs instance]
Azure.IoT Build 7:07bc440836b3 1397 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_030 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP session.]
Azure.IoT Build 7:07bc440836b3 1398 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_031 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP connection.]
Azure.IoT Build 7:07bc440836b3 1399 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_032 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP SASL I / O transport.]
Azure.IoT Build 7:07bc440836b3 1400 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_033 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP SASL mechanism.]
Azure.IoT Build 7:07bc440836b3 1401 destroyConnection(transport_state);
Azure.IoT Build 7:07bc440836b3 1402
Azure.IoT Build 7:07bc440836b3 1403 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_035 : [IoTHubTransportAMQP_Destroy shall delete its internally - set parameters(deviceKey, targetAddress, devicesPath, sasTokenKeyName).]
Azure.IoT Build 7:07bc440836b3 1404 STRING_delete(transport_state->targetAddress);
Azure.IoT Build 7:07bc440836b3 1405 STRING_delete(transport_state->messageReceiveAddress);
Azure.IoT Build 7:07bc440836b3 1406 STRING_delete(transport_state->sasTokenKeyName);
Azure.IoT Build 7:07bc440836b3 1407 STRING_delete(transport_state->deviceKey);
Azure.IoT Build 7:07bc440836b3 1408 STRING_delete(transport_state->devicesPath);
Azure.IoT Build 7:07bc440836b3 1409 STRING_delete(transport_state->iotHubHostFqdn);
Azure.IoT Build 7:07bc440836b3 1410
Azure.IoT Build 7:07bc440836b3 1411 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_036 : [IoTHubTransportAMQP_Destroy shall return the remaining items in inProgress to waitingToSend list.]
Azure.IoT Build 7:07bc440836b3 1412 rollEventsBackToWaitList(transport_state);
Azure.IoT Build 7:07bc440836b3 1413
Azure.IoT Build 7:07bc440836b3 1414 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_150: [IoTHubTransportAMQP_Destroy shall destroy the transport instance]
Azure.IoT Build 7:07bc440836b3 1415 free(transport_state);
Azure.IoT Build 7:07bc440836b3 1416 }
Azure.IoT Build 7:07bc440836b3 1417 }
Azure.IoT Build 7:07bc440836b3 1418
Azure.IoT Build 11:62d7b956e76e 1419 static void IoTHubTransportAMQP_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
Azure.IoT Build 7:07bc440836b3 1420 {
Azure.IoT Build 7:07bc440836b3 1421 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_051: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the transport handle parameter is NULL]
AzureIoTClient 4:57e049bce51e 1422 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1423 {
AzureIoTClient 13:a4af7c301e02 1424 LogError("IoTHubClient DoWork failed: transport handle parameter is NULL.");
Azure.IoT Build 7:07bc440836b3 1425 }
Azure.IoT Build 7:07bc440836b3 1426 // Codes_[IoTHubTransportAMQP_DoWork shall fail and return immediately if the client handle parameter is NULL]
Azure.IoT Build 7:07bc440836b3 1427 else if (iotHubClientHandle == NULL)
Azure.IoT Build 7:07bc440836b3 1428 {
AzureIoTClient 13:a4af7c301e02 1429 LogError("IoTHubClient DoWork failed: client handle parameter is NULL.");
AzureIoTClient 4:57e049bce51e 1430 }
AzureIoTClient 4:57e049bce51e 1431 else
AzureIoTClient 4:57e049bce51e 1432 {
Azure.IoT Build 7:07bc440836b3 1433 bool trigger_connection_retry = false;
Azure.IoT Build 7:07bc440836b3 1434 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1435
Azure.IoT Build 7:07bc440836b3 1436 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_147: [IoTHubTransportAMQP_DoWork shall save a reference to the client handle in transport_state->iothub_client_handle]
Azure.IoT Build 7:07bc440836b3 1437 transport_state->iothub_client_handle = iotHubClientHandle;
Azure.IoT Build 7:07bc440836b3 1438
AzureIoTClient 13:a4af7c301e02 1439 if (transport_state->connection != NULL &&
AzureIoTClient 13:a4af7c301e02 1440 transport_state->connection_state == AMQP_MANAGEMENT_STATE_ERROR)
AzureIoTClient 13:a4af7c301e02 1441 {
AzureIoTClient 13:a4af7c301e02 1442 LogError("An error occured on AMQP connection. The connection will be restablished.");
AzureIoTClient 13:a4af7c301e02 1443 trigger_connection_retry = true;
AzureIoTClient 13:a4af7c301e02 1444 }
Azure.IoT Build 7:07bc440836b3 1445 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_055: [If the transport handle has a NULL connection, IoTHubTransportAMQP_DoWork shall instantiate and initialize the AMQP components and establish the connection]
AzureIoTClient 13:a4af7c301e02 1446 else if (transport_state->connection == NULL &&
Azure.IoT Build 7:07bc440836b3 1447 establishConnection(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1448 {
AzureIoTClient 13:a4af7c301e02 1449 LogError("AMQP transport failed to establish connection with service.");
Azure.IoT Build 7:07bc440836b3 1450 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1451 }
Azure.IoT Build 7:07bc440836b3 1452 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_081: [IoTHubTransportAMQP_DoWork shall put a new SAS token if the one has not been out already, or if the previous one failed to be put due to timeout of cbs_put_token().]
Azure.IoT Build 10:75c5e0d8537d 1453 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_082: [IoTHubTransportAMQP_DoWork shall refresh the SAS token if the current token has been used for more than 'sas_token_refresh_time' milliseconds]
Azure.IoT Build 7:07bc440836b3 1454 else if ((transport_state->cbs_state == CBS_STATE_IDLE || isSasTokenRefreshRequired(transport_state)) &&
Azure.IoT Build 11:62d7b956e76e 1455 startAuthentication(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1456 {
Azure.IoT Build 7:07bc440836b3 1457 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_146: [If the SAS token fails to be sent to CBS (cbs_put_token), IoTHubTransportAMQP_DoWork shall fail and exit immediately]
AzureIoTClient 13:a4af7c301e02 1458 LogError("Failed authenticating AMQP connection within CBS.");
Azure.IoT Build 7:07bc440836b3 1459 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1460 }
Azure.IoT Build 10:75c5e0d8537d 1461 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_084: [IoTHubTransportAMQP_DoWork shall wait for 'cbs_request_timeout' milliseconds for the cbs_put_token() to complete before failing due to timeout]
Azure.IoT Build 11:62d7b956e76e 1462 else if (transport_state->cbs_state == CBS_STATE_AUTH_IN_PROGRESS &&
Azure.IoT Build 11:62d7b956e76e 1463 verifyAuthenticationTimeout(transport_state) == RESULT_TIMEOUT)
Azure.IoT Build 7:07bc440836b3 1464 {
AzureIoTClient 13:a4af7c301e02 1465 LogError("AMQP transport authentication timed out.");
Azure.IoT Build 7:07bc440836b3 1466 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1467 }
Azure.IoT Build 7:07bc440836b3 1468 else if (transport_state->cbs_state == CBS_STATE_AUTHENTICATED)
Azure.IoT Build 7:07bc440836b3 1469 {
Azure.IoT Build 7:07bc440836b3 1470 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_121: [IoTHubTransportAMQP_DoWork shall create an AMQP message_receiver if transport_state->message_receive is NULL and transport_state->receive_messages is true]
Azure.IoT Build 7:07bc440836b3 1471 if (transport_state->receive_messages == true &&
Azure.IoT Build 7:07bc440836b3 1472 transport_state->message_receiver == NULL &&
Azure.IoT Build 7:07bc440836b3 1473 createMessageReceiver(transport_state, iotHubClientHandle) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1474 {
AzureIoTClient 13:a4af7c301e02 1475 LogError("Failed creating AMQP transport message receiver.");
Azure.IoT Build 7:07bc440836b3 1476 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1477 }
Azure.IoT Build 7:07bc440836b3 1478 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_122: [IoTHubTransportAMQP_DoWork shall destroy the transport_state->message_receiver (and set it to NULL) if it exists and transport_state->receive_messages is false]
Azure.IoT Build 7:07bc440836b3 1479 else if (transport_state->receive_messages == false &&
Azure.IoT Build 7:07bc440836b3 1480 transport_state->message_receiver != NULL &&
Azure.IoT Build 7:07bc440836b3 1481 destroyMessageReceiver(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1482 {
AzureIoTClient 13:a4af7c301e02 1483 LogError("Failed destroying AMQP transport message receiver.");
Azure.IoT Build 7:07bc440836b3 1484 }
Azure.IoT Build 7:07bc440836b3 1485
Azure.IoT Build 7:07bc440836b3 1486 if (transport_state->message_sender == NULL &&
Azure.IoT Build 7:07bc440836b3 1487 createEventSender(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1488 {
AzureIoTClient 13:a4af7c301e02 1489 LogError("Failed creating AMQP transport event sender.");
Azure.IoT Build 7:07bc440836b3 1490 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1491 }
Azure.IoT Build 7:07bc440836b3 1492 else if (sendPendingEvents(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1493 {
AzureIoTClient 13:a4af7c301e02 1494 LogError("AMQP transport failed sending events.");
Azure.IoT Build 7:07bc440836b3 1495 }
Azure.IoT Build 7:07bc440836b3 1496 }
Azure.IoT Build 7:07bc440836b3 1497
Azure.IoT Build 7:07bc440836b3 1498 if (trigger_connection_retry)
Azure.IoT Build 7:07bc440836b3 1499 {
Azure.IoT Build 7:07bc440836b3 1500 prepareForConnectionRetry(transport_state);
Azure.IoT Build 7:07bc440836b3 1501 }
Azure.IoT Build 7:07bc440836b3 1502 else
Azure.IoT Build 7:07bc440836b3 1503 {
Azure.IoT Build 7:07bc440836b3 1504 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_103: [IoTHubTransportAMQP_DoWork shall invoke connection_dowork() on AMQP for triggering sending and receiving messages]
Azure.IoT Build 7:07bc440836b3 1505 connection_dowork(transport_state->connection);
Azure.IoT Build 7:07bc440836b3 1506 }
Azure.IoT Build 7:07bc440836b3 1507 }
Azure.IoT Build 7:07bc440836b3 1508 }
Azure.IoT Build 7:07bc440836b3 1509
AzureIoTClient 14:8e8e42008807 1510 static int IoTHubTransportAMQP_Subscribe(TRANSPORT_LL_HANDLE handle)
Azure.IoT Build 7:07bc440836b3 1511 {
Azure.IoT Build 7:07bc440836b3 1512 int result;
Azure.IoT Build 11:62d7b956e76e 1513
Azure.IoT Build 7:07bc440836b3 1514 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_037: [IoTHubTransportAMQP_Subscribe shall fail if the transport handle parameter received is NULL.]
Azure.IoT Build 7:07bc440836b3 1515 if (handle == NULL)
Azure.IoT Build 7:07bc440836b3 1516 {
AzureIoTClient 13:a4af7c301e02 1517 LogError("Invalid handle to IoTHubClient AMQP transport.");
Azure.IoT Build 7:07bc440836b3 1518 result = __LINE__;
Azure.IoT Build 7:07bc440836b3 1519 }
Azure.IoT Build 7:07bc440836b3 1520 else
Azure.IoT Build 7:07bc440836b3 1521 {
Azure.IoT Build 7:07bc440836b3 1522 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_038: [IoTHubTransportAMQP_Subscribe shall set transport_handle->receive_messages to true and return success code.]
Azure.IoT Build 7:07bc440836b3 1523 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1524 transport_state->receive_messages = true;
AzureIoTClient 4:57e049bce51e 1525 result = 0;
AzureIoTClient 4:57e049bce51e 1526 }
Azure.IoT Build 7:07bc440836b3 1527
AzureIoTClient 4:57e049bce51e 1528 return result;
AzureIoTClient 4:57e049bce51e 1529 }
AzureIoTClient 4:57e049bce51e 1530
AzureIoTClient 14:8e8e42008807 1531 static void IoTHubTransportAMQP_Unsubscribe(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 4:57e049bce51e 1532 {
Azure.IoT Build 7:07bc440836b3 1533 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_039: [IoTHubTransportAMQP_Unsubscribe shall fail if the transport handle parameter received is NULL.]
Azure.IoT Build 7:07bc440836b3 1534 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1535 {
AzureIoTClient 13:a4af7c301e02 1536 LogError("Invalid handle to IoTHubClient AMQP transport.");
Azure.IoT Build 7:07bc440836b3 1537 }
Azure.IoT Build 7:07bc440836b3 1538 else
Azure.IoT Build 7:07bc440836b3 1539 {
Azure.IoT Build 7:07bc440836b3 1540 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_040: [IoTHubTransportAMQP_Unsubscribe shall set transport_handle->receive_messages to false and return success code.]
Azure.IoT Build 7:07bc440836b3 1541 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1542 transport_state->receive_messages = false;
AzureIoTClient 4:57e049bce51e 1543 }
AzureIoTClient 4:57e049bce51e 1544 }
AzureIoTClient 4:57e049bce51e 1545
Azure.IoT Build 10:75c5e0d8537d 1546 static IOTHUB_CLIENT_RESULT IoTHubTransportAMQP_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 4:57e049bce51e 1547 {
AzureIoTClient 4:57e049bce51e 1548 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 4:57e049bce51e 1549
Azure.IoT Build 7:07bc440836b3 1550 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_041: [IoTHubTransportAMQP_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter.]
AzureIoTClient 4:57e049bce51e 1551 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1552 {
AzureIoTClient 4:57e049bce51e 1553 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 13:a4af7c301e02 1554 LogError("Invalid handle to IoTHubClient AMQP transport instance.");
AzureIoTClient 4:57e049bce51e 1555 }
AzureIoTClient 4:57e049bce51e 1556 else if (iotHubClientStatus == NULL)
AzureIoTClient 4:57e049bce51e 1557 {
AzureIoTClient 4:57e049bce51e 1558 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 13:a4af7c301e02 1559 LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.");
AzureIoTClient 4:57e049bce51e 1560 }
AzureIoTClient 4:57e049bce51e 1561 else
AzureIoTClient 4:57e049bce51e 1562 {
Azure.IoT Build 7:07bc440836b3 1563 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 4:57e049bce51e 1564
Azure.IoT Build 7:07bc440836b3 1565 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_043: [IoTHubTransportAMQP_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently event items to be sent or being sent.]
Azure.IoT Build 7:07bc440836b3 1566 if (!DList_IsListEmpty(transport_state->waitingToSend) || !DList_IsListEmpty(&(transport_state->inProgress)))
AzureIoTClient 4:57e049bce51e 1567 {
AzureIoTClient 4:57e049bce51e 1568 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 4:57e049bce51e 1569 }
Azure.IoT Build 7:07bc440836b3 1570 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_042: [IoTHubTransportAMQP_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent.]
AzureIoTClient 4:57e049bce51e 1571 else
AzureIoTClient 4:57e049bce51e 1572 {
AzureIoTClient 4:57e049bce51e 1573 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 4:57e049bce51e 1574 }
AzureIoTClient 4:57e049bce51e 1575
AzureIoTClient 4:57e049bce51e 1576 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:57e049bce51e 1577 }
AzureIoTClient 4:57e049bce51e 1578
AzureIoTClient 4:57e049bce51e 1579 return result;
AzureIoTClient 4:57e049bce51e 1580 }
AzureIoTClient 4:57e049bce51e 1581
Azure.IoT Build 11:62d7b956e76e 1582 static IOTHUB_CLIENT_RESULT IoTHubTransportAMQP_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
AzureIoTClient 4:57e049bce51e 1583 {
AzureIoTClient 4:57e049bce51e 1584 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 4:57e049bce51e 1585
Azure.IoT Build 7:07bc440836b3 1586 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_044: [If handle parameter is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
Azure.IoT Build 7:07bc440836b3 1587 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_045: [If parameter optionName is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
Azure.IoT Build 7:07bc440836b3 1588 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_046: [If parameter value is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
AzureIoTClient 5:8d58d20699dd 1589 if (
AzureIoTClient 5:8d58d20699dd 1590 (handle == NULL) ||
AzureIoTClient 5:8d58d20699dd 1591 (option == NULL) ||
AzureIoTClient 5:8d58d20699dd 1592 (value == NULL)
AzureIoTClient 5:8d58d20699dd 1593 )
AzureIoTClient 5:8d58d20699dd 1594 {
AzureIoTClient 5:8d58d20699dd 1595 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 13:a4af7c301e02 1596 LogError("Invalid parameter (NULL) passed to AMQP transport SetOption()");
AzureIoTClient 5:8d58d20699dd 1597 }
AzureIoTClient 5:8d58d20699dd 1598 else
AzureIoTClient 5:8d58d20699dd 1599 {
Azure.IoT Build 7:07bc440836b3 1600 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 4:57e049bce51e 1601
Azure.IoT Build 10:75c5e0d8537d 1602 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_048: [IotHubTransportAMQP_SetOption shall save and apply the value if the option name is "sas_token_lifetime", returning IOTHUB_CLIENT_OK]
Azure.IoT Build 11:62d7b956e76e 1603 if (strcmp("sas_token_lifetime", option) == 0)
Azure.IoT Build 10:75c5e0d8537d 1604 {
Azure.IoT Build 10:75c5e0d8537d 1605 transport_state->sas_token_lifetime = *((size_t*)value);
Azure.IoT Build 10:75c5e0d8537d 1606 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 10:75c5e0d8537d 1607 }
Azure.IoT Build 10:75c5e0d8537d 1608 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_049: [IotHubTransportAMQP_SetOption shall save and apply the value if the option name is "sas_token_refresh_time", returning IOTHUB_CLIENT_OK]
Azure.IoT Build 10:75c5e0d8537d 1609 else if (strcmp("sas_token_refresh_time", option) == 0)
Azure.IoT Build 10:75c5e0d8537d 1610 {
Azure.IoT Build 10:75c5e0d8537d 1611 transport_state->sas_token_refresh_time = *((size_t*)value);
Azure.IoT Build 10:75c5e0d8537d 1612 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 10:75c5e0d8537d 1613 }
Azure.IoT Build 10:75c5e0d8537d 1614 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_148: [IotHubTransportAMQP_SetOption shall save and apply the value if the option name is "cbs_request_timeout", returning IOTHUB_CLIENT_OK]
Azure.IoT Build 10:75c5e0d8537d 1615 else if (strcmp("cbs_request_timeout", option) == 0)
Azure.IoT Build 10:75c5e0d8537d 1616 {
Azure.IoT Build 10:75c5e0d8537d 1617 transport_state->cbs_request_timeout = *((size_t*)value);
Azure.IoT Build 10:75c5e0d8537d 1618 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 10:75c5e0d8537d 1619 }
Azure.IoT Build 11:62d7b956e76e 1620 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_047: [If the option name does not match one of the options handled by this module, then IoTHubTransportAMQP_SetOption shall get the handle to the XIO and invoke the xio_setoption passing down the option name and value parameters.]
Azure.IoT Build 10:75c5e0d8537d 1621 else
Azure.IoT Build 10:75c5e0d8537d 1622 {
Azure.IoT Build 11:62d7b956e76e 1623 if (transport_state->tls_io == NULL &&
Azure.IoT Build 11:62d7b956e76e 1624 (transport_state->tls_io = transport_state->tls_io_transport_provider(STRING_c_str(transport_state->iotHubHostFqdn), transport_state->iotHubPort)) == NULL)
Azure.IoT Build 11:62d7b956e76e 1625 {
Azure.IoT Build 11:62d7b956e76e 1626 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 13:a4af7c301e02 1627 LogError("Failed to obtain a TLS I/O transport layer.");
Azure.IoT Build 11:62d7b956e76e 1628 }
Azure.IoT Build 11:62d7b956e76e 1629 else
Azure.IoT Build 11:62d7b956e76e 1630 {
Azure.IoT Build 11:62d7b956e76e 1631 /* Codes_SRS_IOTHUBTRANSPORTUAMQP_03_001: [If xio_setoption fails, IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_ERROR.] */
Azure.IoT Build 11:62d7b956e76e 1632 if (xio_setoption(transport_state->tls_io, option, value) == 0)
Azure.IoT Build 11:62d7b956e76e 1633 {
Azure.IoT Build 11:62d7b956e76e 1634 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 11:62d7b956e76e 1635 }
Azure.IoT Build 11:62d7b956e76e 1636 else
Azure.IoT Build 11:62d7b956e76e 1637 {
Azure.IoT Build 11:62d7b956e76e 1638 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 13:a4af7c301e02 1639 LogError("Invalid option (%s) passed to uAMQP transport SetOption()", option);
Azure.IoT Build 11:62d7b956e76e 1640 }
Azure.IoT Build 11:62d7b956e76e 1641 }
Azure.IoT Build 10:75c5e0d8537d 1642 }
Azure.IoT Build 10:75c5e0d8537d 1643 }
AzureIoTClient 4:57e049bce51e 1644
AzureIoTClient 4:57e049bce51e 1645 return result;
AzureIoTClient 4:57e049bce51e 1646 }
AzureIoTClient 4:57e049bce51e 1647
AzureIoTClient 14:8e8e42008807 1648 static IOTHUB_DEVICE_HANDLE IoTHubTransportAMQP_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend)
Azure.IoT Build 10:75c5e0d8537d 1649 {
Azure.IoT Build 10:75c5e0d8537d 1650 IOTHUB_DEVICE_HANDLE result;
AzureIoTClient 14:8e8e42008807 1651 // Codes_SRS_IOTHUBTRANSPORTUAMQP_17_001: [IoTHubTransportAMQP_Register shall return NULL if device, or waitingToSend are NULL.]
Azure.IoT Build 12:841a4c36bd36 1652 // Codes_SRS_IOTHUBTRANSPORTUAMQP_17_005: [IoTHubTransportAMQP_Register shall return NULL if the TRANSPORT_LL_HANDLE is NULL.]
AzureIoTClient 14:8e8e42008807 1653 if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL))
Azure.IoT Build 10:75c5e0d8537d 1654 {
Azure.IoT Build 10:75c5e0d8537d 1655 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 1656 }
Azure.IoT Build 10:75c5e0d8537d 1657 else
Azure.IoT Build 10:75c5e0d8537d 1658 {
Azure.IoT Build 10:75c5e0d8537d 1659 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 10:75c5e0d8537d 1660
AzureIoTClient 14:8e8e42008807 1661 // Codes_SRS_IOTHUBTRANSPORTUAMQP_03_002: [IoTHubTransportAMQP_Register shall return NULL if deviceId, or both deviceKey and deviceSasToken are NULL.**]
AzureIoTClient 14:8e8e42008807 1662 if ((device->deviceId == NULL) || (device->deviceSasToken == NULL && device->deviceKey == NULL))
AzureIoTClient 14:8e8e42008807 1663 {
AzureIoTClient 14:8e8e42008807 1664 result = NULL;
AzureIoTClient 14:8e8e42008807 1665 }
AzureIoTClient 14:8e8e42008807 1666 // Codes_SRS_IOTHUBTRANSPORTUAMQP_03_003: [IoTHubTransportAMQP_Register shall return NULL if both deviceKey and deviceSasToken are not NULL.]
AzureIoTClient 14:8e8e42008807 1667 else if ( (device->deviceSasToken != NULL) && (device->deviceKey != NULL) )
AzureIoTClient 14:8e8e42008807 1668 {
AzureIoTClient 14:8e8e42008807 1669 result = NULL;
AzureIoTClient 14:8e8e42008807 1670 }
AzureIoTClient 14:8e8e42008807 1671 else
AzureIoTClient 14:8e8e42008807 1672 {
AzureIoTClient 14:8e8e42008807 1673 STRING_HANDLE devicesPath = concat3Params(STRING_c_str(transport_state->iotHubHostFqdn), "/devices/", device->deviceId);
Azure.IoT Build 10:75c5e0d8537d 1674 if (devicesPath == NULL)
Azure.IoT Build 10:75c5e0d8537d 1675 {
AzureIoTClient 14:8e8e42008807 1676 LogError("Could not create devicesPath");
Azure.IoT Build 10:75c5e0d8537d 1677 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 1678 }
Azure.IoT Build 10:75c5e0d8537d 1679 else
Azure.IoT Build 10:75c5e0d8537d 1680 {
Azure.IoT Build 12:841a4c36bd36 1681 // Codes_SRS_IOTHUBTRANSPORTUAMQP_17_002: [IoTHubTransportAMQP_Register shall return NULL if deviceId or deviceKey do not match the deviceId and deviceKey passed in during IoTHubTransportAMQP_Create.]
Azure.IoT Build 10:75c5e0d8537d 1682 if (strcmp(STRING_c_str(transport_state->devicesPath), STRING_c_str(devicesPath)) != 0)
Azure.IoT Build 10:75c5e0d8537d 1683 {
Azure.IoT Build 10:75c5e0d8537d 1684 LogError("Attemping to add new device to AMQP transport, not allowed.");
Azure.IoT Build 10:75c5e0d8537d 1685 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 1686 }
AzureIoTClient 14:8e8e42008807 1687 else if ((transport_state->deviceSasToken == NULL) && strcmp(STRING_c_str(transport_state->deviceKey), device->deviceKey) != 0)
Azure.IoT Build 10:75c5e0d8537d 1688 {
Azure.IoT Build 10:75c5e0d8537d 1689 LogError("Attemping to add new device to AMQP transport, not allowed.");
Azure.IoT Build 10:75c5e0d8537d 1690 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 1691 }
Azure.IoT Build 10:75c5e0d8537d 1692 else
Azure.IoT Build 10:75c5e0d8537d 1693 {
Azure.IoT Build 10:75c5e0d8537d 1694 if (transport_state->isRegistered == true)
Azure.IoT Build 10:75c5e0d8537d 1695 {
AzureIoTClient 14:8e8e42008807 1696 LogError("Transport already has device registered by id: [%s]", device->deviceId);
Azure.IoT Build 10:75c5e0d8537d 1697 result = NULL;
Azure.IoT Build 10:75c5e0d8537d 1698 }
Azure.IoT Build 10:75c5e0d8537d 1699 else
Azure.IoT Build 10:75c5e0d8537d 1700 {
Azure.IoT Build 10:75c5e0d8537d 1701 transport_state->isRegistered = true;
Azure.IoT Build 12:841a4c36bd36 1702 // Codes_SRS_IOTHUBTRANSPORTUAMQP_17_003: [IoTHubTransportAMQP_Register shall return the TRANSPORT_LL_HANDLE as the IOTHUB_DEVICE_HANDLE.]
Azure.IoT Build 10:75c5e0d8537d 1703 result = (IOTHUB_DEVICE_HANDLE)handle;
Azure.IoT Build 10:75c5e0d8537d 1704 }
Azure.IoT Build 10:75c5e0d8537d 1705 }
Azure.IoT Build 10:75c5e0d8537d 1706 STRING_delete(devicesPath);
Azure.IoT Build 10:75c5e0d8537d 1707 }
Azure.IoT Build 10:75c5e0d8537d 1708 }
AzureIoTClient 14:8e8e42008807 1709 }
Azure.IoT Build 10:75c5e0d8537d 1710
Azure.IoT Build 10:75c5e0d8537d 1711 return result;
Azure.IoT Build 10:75c5e0d8537d 1712 }
Azure.IoT Build 10:75c5e0d8537d 1713
Azure.IoT Build 12:841a4c36bd36 1714 // Codes_SRS_IOTHUBTRANSPORTUAMQP_17_004: [IoTHubTransportAMQP_Unregister shall return.]
Azure.IoT Build 12:841a4c36bd36 1715 static void IoTHubTransportAMQP_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
Azure.IoT Build 10:75c5e0d8537d 1716 {
Azure.IoT Build 10:75c5e0d8537d 1717 if (deviceHandle != NULL)
Azure.IoT Build 10:75c5e0d8537d 1718 {
Azure.IoT Build 10:75c5e0d8537d 1719 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)deviceHandle;
Azure.IoT Build 10:75c5e0d8537d 1720
Azure.IoT Build 10:75c5e0d8537d 1721 transport_state->isRegistered = false;
Azure.IoT Build 10:75c5e0d8537d 1722 }
Azure.IoT Build 10:75c5e0d8537d 1723 }
Azure.IoT Build 10:75c5e0d8537d 1724
Azure.IoT Build 7:07bc440836b3 1725 static TRANSPORT_PROVIDER thisTransportProvider = {
Azure.IoT Build 11:62d7b956e76e 1726 IoTHubTransportAMQP_SetOption,
Azure.IoT Build 11:62d7b956e76e 1727 IoTHubTransportAMQP_Create,
Azure.IoT Build 11:62d7b956e76e 1728 IoTHubTransportAMQP_Destroy,
Azure.IoT Build 12:841a4c36bd36 1729 IoTHubTransportAMQP_Register,
Azure.IoT Build 12:841a4c36bd36 1730 IoTHubTransportAMQP_Unregister,
Azure.IoT Build 11:62d7b956e76e 1731 IoTHubTransportAMQP_Subscribe,
Azure.IoT Build 11:62d7b956e76e 1732 IoTHubTransportAMQP_Unsubscribe,
Azure.IoT Build 11:62d7b956e76e 1733 IoTHubTransportAMQP_DoWork,
Azure.IoT Build 7:07bc440836b3 1734 IoTHubTransportAMQP_GetSendStatus
AzureIoTClient 4:57e049bce51e 1735 };
AzureIoTClient 4:57e049bce51e 1736
AzureIoTClient 4:57e049bce51e 1737 extern const void* AMQP_Protocol(void)
AzureIoTClient 4:57e049bce51e 1738 {
Azure.IoT Build 7:07bc440836b3 1739 return &thisTransportProvider;
AzureIoTClient 4:57e049bce51e 1740 }