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:
Azure.IoT Build
Date:
Thu Feb 04 15:56:59 2016 -0800
Revision:
8:450043aa3c5c
Parent:
7:07bc440836b3
Child:
9:8abcd13782f4
1.0.0

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
AzureIoTClient 4:57e049bce51e 8 #include "gballoc.h"
AzureIoTClient 4:57e049bce51e 9
Azure.IoT Build 7:07bc440836b3 10 #include "cbs.h"
Azure.IoT Build 7:07bc440836b3 11 #include "link.h"
Azure.IoT Build 7:07bc440836b3 12 #include "message.h"
Azure.IoT Build 7:07bc440836b3 13 #include "message_receiver.h"
Azure.IoT Build 7:07bc440836b3 14 #include "message_sender.h"
Azure.IoT Build 7:07bc440836b3 15 #include "messaging.h"
Azure.IoT Build 7:07bc440836b3 16 #include "sasl_mssbcbs.h"
Azure.IoT Build 7:07bc440836b3 17 #include "saslclientio.h"
AzureIoTClient 4:57e049bce51e 18
Azure.IoT Build 7:07bc440836b3 19 #include "crt_abstractions.h"
Azure.IoT Build 7:07bc440836b3 20 #include "doublylinkedlist.h"
AzureIoTClient 4:57e049bce51e 21 #include "iot_logging.h"
Azure.IoT Build 7:07bc440836b3 22 #include "platform.h"
Azure.IoT Build 7:07bc440836b3 23 #include "sastoken.h"
AzureIoTClient 4:57e049bce51e 24 #include "strings.h"
AzureIoTClient 4:57e049bce51e 25 #include "urlencode.h"
Azure.IoT Build 7:07bc440836b3 26
Azure.IoT Build 7:07bc440836b3 27 #include "tlsio.h"
AzureIoTClient 4:57e049bce51e 28
AzureIoTClient 4:57e049bce51e 29 #include "iothub_client_ll.h"
AzureIoTClient 4:57e049bce51e 30 #include "iothub_client_private.h"
AzureIoTClient 4:57e049bce51e 31 #include "iothubtransportamqp.h"
Azure.IoT Build 7:07bc440836b3 32 #include "iothub_client_version.h"
Azure.IoT Build 7:07bc440836b3 33
Azure.IoT Build 7:07bc440836b3 34 #define RESULT_OK 0
Azure.IoT Build 7:07bc440836b3 35 #define RESULT_FAILURE 1
Azure.IoT Build 7:07bc440836b3 36 #define RESULT_TIMEOUT 2
Azure.IoT Build 7:07bc440836b3 37
Azure.IoT Build 7:07bc440836b3 38 #define TIME_MAX SIZE_MAX
Azure.IoT Build 7:07bc440836b3 39 #define RFC1035_MAX_FQDN_LENGTH 255
Azure.IoT Build 7:07bc440836b3 40 #define DEFAULT_IOTHUB_AMQP_PORT 5671
Azure.IoT Build 7:07bc440836b3 41 #define DEFAULT_SAS_TOKEN_LIFETIME_MS 3600000
Azure.IoT Build 7:07bc440836b3 42 #define DEFAULT_CBS_REQUEST_TIMEOUT_MS 30000
Azure.IoT Build 7:07bc440836b3 43 #define DEFAULT_MESSAGE_SEND_TIMEOUT_MS 300000
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 7:07bc440836b3 46 #define DEFAULT_INCOMING_WINDOW_SIZE SIZE_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 7:07bc440836b3 53 #define MESSAGE_SENDER_MAX_LINK_SIZE 65536
Azure.IoT Build 7:07bc440836b3 54
Azure.IoT Build 7:07bc440836b3 55 typedef XIO_HANDLE(*TLS_IO_TRANSPORT_PROVIDER)(const char* fqdn, int port, const char* certificates);
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 7:07bc440836b3 66 // FQDN of the IoT Hub.
Azure.IoT Build 7:07bc440836b3 67 STRING_HANDLE iotHubHostFqdn;
Azure.IoT Build 7:07bc440836b3 68 // AMQP port of the IoT Hub.
Azure.IoT Build 7:07bc440836b3 69 int iotHubPort;
Azure.IoT Build 7:07bc440836b3 70 // Certificates to be used by the TLS I/O.
Azure.IoT Build 7:07bc440836b3 71 char* trusted_certificates;
Azure.IoT Build 7:07bc440836b3 72 // Key associated to the device to be used.
Azure.IoT Build 7:07bc440836b3 73 STRING_HANDLE deviceKey;
Azure.IoT Build 7:07bc440836b3 74 // Address to which the transport will connect to and send events.
Azure.IoT Build 7:07bc440836b3 75 STRING_HANDLE targetAddress;
Azure.IoT Build 7:07bc440836b3 76 // Address to which the transport will connect to and receive messages from.
Azure.IoT Build 7:07bc440836b3 77 STRING_HANDLE messageReceiveAddress;
Azure.IoT Build 7:07bc440836b3 78 // A component of the SAS token. Currently this must be an empty string.
Azure.IoT Build 7:07bc440836b3 79 STRING_HANDLE sasTokenKeyName;
Azure.IoT Build 7:07bc440836b3 80 // Internal parameter that identifies the current logical device within the service.
Azure.IoT Build 7:07bc440836b3 81 STRING_HANDLE devicesPath;
Azure.IoT Build 7:07bc440836b3 82 // How long a SAS token created by the transport is valid, in milliseconds.
Azure.IoT Build 7:07bc440836b3 83 size_t sas_token_lifetime;
Azure.IoT Build 7:07bc440836b3 84 // Maximum period of time for the transport to wait before refreshing the SAS token it created previously, in milliseconds.
Azure.IoT Build 7:07bc440836b3 85 size_t sas_token_refresh_time;
Azure.IoT Build 7:07bc440836b3 86 // Maximum time the transport waits for uAMQP cbs_put_token() to complete before marking it a failure, in milliseconds.
Azure.IoT Build 7:07bc440836b3 87 size_t cbs_request_timeout;
Azure.IoT Build 7:07bc440836b3 88 // Maximum time the transport waits for an event to be sent before marking it a failure, in milliseconds.
Azure.IoT Build 7:07bc440836b3 89 size_t message_send_timeout;
Azure.IoT Build 7:07bc440836b3 90 // Maximum time for the connection establishment/retry logic should wait for a connection to succeed, in milliseconds.
Azure.IoT Build 7:07bc440836b3 91 size_t connection_timeout;
Azure.IoT Build 7:07bc440836b3 92 // Saved reference to the IoTHub LL Client.
Azure.IoT Build 7:07bc440836b3 93 IOTHUB_CLIENT_LL_HANDLE iothub_client_handle;
Azure.IoT Build 7:07bc440836b3 94
Azure.IoT Build 7:07bc440836b3 95 // TSL I/O transport.
Azure.IoT Build 7:07bc440836b3 96 XIO_HANDLE tls_io;
Azure.IoT Build 7:07bc440836b3 97 // Pointer to the function that creates the TLS I/O (internal use only).
Azure.IoT Build 7:07bc440836b3 98 TLS_IO_TRANSPORT_PROVIDER tls_io_transport_provider;
Azure.IoT Build 7:07bc440836b3 99 // AMQP SASL I/O transport created on top of the TLS I/O layer.
Azure.IoT Build 7:07bc440836b3 100 XIO_HANDLE sasl_io;
Azure.IoT Build 7:07bc440836b3 101 // AMQP SASL I/O mechanism to be used.
Azure.IoT Build 7:07bc440836b3 102 SASL_MECHANISM_HANDLE sasl_mechanism;
Azure.IoT Build 7:07bc440836b3 103 // AMQP connection.
Azure.IoT Build 7:07bc440836b3 104 CONNECTION_HANDLE connection;
Azure.IoT Build 7:07bc440836b3 105 // Current AMQP connection state;
Azure.IoT Build 7:07bc440836b3 106 AMQP_MANAGEMENT_STATE connection_state;
Azure.IoT Build 7:07bc440836b3 107 // Last time the AMQP connection establishment was initiated.
Azure.IoT Build 7:07bc440836b3 108 size_t connection_establish_time;
Azure.IoT Build 7:07bc440836b3 109 // AMQP session.
Azure.IoT Build 7:07bc440836b3 110 SESSION_HANDLE session;
Azure.IoT Build 7:07bc440836b3 111 // AMQP link used by the event sender.
Azure.IoT Build 7:07bc440836b3 112 LINK_HANDLE sender_link;
Azure.IoT Build 7:07bc440836b3 113 // uAMQP event sender.
Azure.IoT Build 7:07bc440836b3 114 MESSAGE_SENDER_HANDLE message_sender;
Azure.IoT Build 7:07bc440836b3 115 // Internal flag that controls if messages should be received or not.
Azure.IoT Build 7:07bc440836b3 116 bool receive_messages;
Azure.IoT Build 7:07bc440836b3 117 // AMQP link used by the message receiver.
Azure.IoT Build 7:07bc440836b3 118 LINK_HANDLE receiver_link;
Azure.IoT Build 7:07bc440836b3 119 // uAMQP message receiver.
Azure.IoT Build 7:07bc440836b3 120 MESSAGE_RECEIVER_HANDLE message_receiver;
Azure.IoT Build 7:07bc440836b3 121 // List with events still pending to be sent. It is provided by the upper layer.
Azure.IoT Build 7:07bc440836b3 122 PDLIST_ENTRY waitingToSend;
Azure.IoT Build 7:07bc440836b3 123 // Internal list with the items currently being processed/sent through uAMQP.
Azure.IoT Build 7:07bc440836b3 124 DLIST_ENTRY inProgress;
Azure.IoT Build 7:07bc440836b3 125 // Connection instance with the Azure IoT CBS.
Azure.IoT Build 7:07bc440836b3 126 CBS_HANDLE cbs;
Azure.IoT Build 7:07bc440836b3 127 // Current state of the CBS connection.
Azure.IoT Build 7:07bc440836b3 128 CBS_STATE cbs_state;
Azure.IoT Build 7:07bc440836b3 129 // Time when the current SAS token was created, in seconds since epoch.
Azure.IoT Build 7:07bc440836b3 130 size_t current_sas_token_create_time;
Azure.IoT Build 7:07bc440836b3 131 } AMQP_TRANSPORT_INSTANCE;
Azure.IoT Build 7:07bc440836b3 132
Azure.IoT Build 7:07bc440836b3 133 // This structure is used to track an event being sent and the time it was sent.
Azure.IoT Build 7:07bc440836b3 134 typedef struct EVENT_TRACKER_TAG
Azure.IoT Build 7:07bc440836b3 135 {
Azure.IoT Build 7:07bc440836b3 136 IOTHUB_CLIENT_LL_HANDLE iothub_client_handle;
Azure.IoT Build 7:07bc440836b3 137 IOTHUB_MESSAGE_LIST* message;
Azure.IoT Build 7:07bc440836b3 138 time_t time_sent;
Azure.IoT Build 7:07bc440836b3 139 DLIST_ENTRY entry;
Azure.IoT Build 7:07bc440836b3 140 } EVENT_TRACKER;
AzureIoTClient 4:57e049bce51e 141
AzureIoTClient 4:57e049bce51e 142
Azure.IoT Build 7:07bc440836b3 143 // Auxiliary functions
AzureIoTClient 4:57e049bce51e 144
Azure.IoT Build 7:07bc440836b3 145 static STRING_HANDLE concat3Params(const char* prefix, const char* infix, const char* suffix)
AzureIoTClient 4:57e049bce51e 146 {
Azure.IoT Build 7:07bc440836b3 147 STRING_HANDLE result = NULL;
Azure.IoT Build 7:07bc440836b3 148 char* concat = NULL;
Azure.IoT Build 7:07bc440836b3 149 size_t totalLength = strlen(prefix) + strlen(infix) + strlen(suffix) + 1; // One extra for \0.
AzureIoTClient 5:8d58d20699dd 150
Azure.IoT Build 7:07bc440836b3 151 if ((concat = (char*)malloc(sizeof(char) * totalLength)) != NULL)
AzureIoTClient 5:8d58d20699dd 152 {
Azure.IoT Build 7:07bc440836b3 153 if (snprintf(concat, totalLength, "%s%s%s", prefix, infix, suffix) > 0)
AzureIoTClient 5:8d58d20699dd 154 {
Azure.IoT Build 7:07bc440836b3 155 result = STRING_construct(concat);
AzureIoTClient 5:8d58d20699dd 156 }
Azure.IoT Build 7:07bc440836b3 157 free(concat);
AzureIoTClient 5:8d58d20699dd 158 }
AzureIoTClient 5:8d58d20699dd 159
AzureIoTClient 4:57e049bce51e 160 return result;
AzureIoTClient 4:57e049bce51e 161 }
AzureIoTClient 4:57e049bce51e 162
Azure.IoT Build 7:07bc440836b3 163 static size_t getSecondsSinceEpoch()
AzureIoTClient 4:57e049bce51e 164 {
Azure.IoT Build 7:07bc440836b3 165 return (size_t)(difftime(get_time(NULL), (time_t)0) + 0);
AzureIoTClient 4:57e049bce51e 166 }
AzureIoTClient 4:57e049bce51e 167
Azure.IoT Build 7:07bc440836b3 168 static EVENT_TRACKER* trackEvent(IOTHUB_MESSAGE_LIST* message, AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 169 {
Azure.IoT Build 7:07bc440836b3 170 EVENT_TRACKER* event_tracker = NULL;
Azure.IoT Build 7:07bc440836b3 171
Azure.IoT Build 7:07bc440836b3 172 if ((event_tracker = (EVENT_TRACKER*)malloc(sizeof(EVENT_TRACKER))) != NULL)
Azure.IoT Build 7:07bc440836b3 173 {
Azure.IoT Build 7:07bc440836b3 174 event_tracker->iothub_client_handle = transport_state->iothub_client_handle;
Azure.IoT Build 7:07bc440836b3 175 event_tracker->message = message;
Azure.IoT Build 7:07bc440836b3 176 event_tracker->time_sent = get_time(NULL);
Azure.IoT Build 7:07bc440836b3 177 DList_InitializeListHead(&event_tracker->entry);
Azure.IoT Build 7:07bc440836b3 178
Azure.IoT Build 7:07bc440836b3 179 DList_RemoveEntryList(&message->entry);
Azure.IoT Build 7:07bc440836b3 180 DList_InsertTailList(&transport_state->inProgress, &event_tracker->entry);
Azure.IoT Build 7:07bc440836b3 181 }
Azure.IoT Build 7:07bc440836b3 182
Azure.IoT Build 7:07bc440836b3 183 return event_tracker;
Azure.IoT Build 7:07bc440836b3 184 }
Azure.IoT Build 7:07bc440836b3 185
Azure.IoT Build 7:07bc440836b3 186 static void destroyEventTracker(EVENT_TRACKER* event_tracker)
Azure.IoT Build 7:07bc440836b3 187 {
Azure.IoT Build 7:07bc440836b3 188 free(event_tracker);
AzureIoTClient 4:57e049bce51e 189 }
AzureIoTClient 4:57e049bce51e 190
Azure.IoT Build 7:07bc440836b3 191 static IOTHUB_MESSAGE_LIST* getNextEventToSend(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 192 {
Azure.IoT Build 7:07bc440836b3 193 IOTHUB_MESSAGE_LIST* message = NULL;
Azure.IoT Build 7:07bc440836b3 194
Azure.IoT Build 7:07bc440836b3 195 if (!DList_IsListEmpty(transport_state->waitingToSend))
AzureIoTClient 4:57e049bce51e 196 {
Azure.IoT Build 7:07bc440836b3 197 PDLIST_ENTRY list_entry = transport_state->waitingToSend->Flink;
Azure.IoT Build 7:07bc440836b3 198 message = containingRecord(list_entry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 4:57e049bce51e 199 }
Azure.IoT Build 7:07bc440836b3 200
Azure.IoT Build 7:07bc440836b3 201 return message;
Azure.IoT Build 7:07bc440836b3 202 }
Azure.IoT Build 7:07bc440836b3 203
Azure.IoT Build 7:07bc440836b3 204 static int isEventInInProgressList(EVENT_TRACKER* event_tracker)
Azure.IoT Build 7:07bc440836b3 205 {
Azure.IoT Build 7:07bc440836b3 206 return !DList_IsListEmpty(&event_tracker->entry);
AzureIoTClient 4:57e049bce51e 207 }
AzureIoTClient 4:57e049bce51e 208
Azure.IoT Build 7:07bc440836b3 209 static void removeEventFromInProgressList(EVENT_TRACKER* event_tracker)
AzureIoTClient 4:57e049bce51e 210 {
Azure.IoT Build 7:07bc440836b3 211 DList_RemoveEntryList(&event_tracker->entry);
Azure.IoT Build 7:07bc440836b3 212 DList_InitializeListHead(&event_tracker->entry);
AzureIoTClient 4:57e049bce51e 213 }
AzureIoTClient 4:57e049bce51e 214
Azure.IoT Build 7:07bc440836b3 215 static void rollEventBackToWaitList(EVENT_TRACKER* event_tracker, AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 216 {
Azure.IoT Build 7:07bc440836b3 217 removeEventFromInProgressList(event_tracker);
Azure.IoT Build 7:07bc440836b3 218 DList_InsertTailList(transport_state->waitingToSend, &event_tracker->message->entry);
Azure.IoT Build 7:07bc440836b3 219 destroyEventTracker(event_tracker);
Azure.IoT Build 7:07bc440836b3 220 }
Azure.IoT Build 7:07bc440836b3 221
Azure.IoT Build 7:07bc440836b3 222 static void rollEventsBackToWaitList(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 223 {
Azure.IoT Build 7:07bc440836b3 224 PDLIST_ENTRY entry = transport_state->inProgress.Blink;
Azure.IoT Build 7:07bc440836b3 225
Azure.IoT Build 7:07bc440836b3 226 while (entry != &transport_state->inProgress)
AzureIoTClient 4:57e049bce51e 227 {
Azure.IoT Build 7:07bc440836b3 228 EVENT_TRACKER* event_tracker = containingRecord(entry, EVENT_TRACKER, entry);
Azure.IoT Build 7:07bc440836b3 229 entry = entry->Blink;
Azure.IoT Build 7:07bc440836b3 230 rollEventBackToWaitList(event_tracker, transport_state);
AzureIoTClient 4:57e049bce51e 231 }
AzureIoTClient 4:57e049bce51e 232 }
AzureIoTClient 4:57e049bce51e 233
Azure.IoT Build 7:07bc440836b3 234 static void on_message_send_complete(const void* context, MESSAGE_SEND_RESULT send_result)
AzureIoTClient 4:57e049bce51e 235 {
Azure.IoT Build 7:07bc440836b3 236 if (context != NULL)
AzureIoTClient 4:57e049bce51e 237 {
Azure.IoT Build 7:07bc440836b3 238 EVENT_TRACKER* event_tracker = (EVENT_TRACKER*)context;
AzureIoTClient 4:57e049bce51e 239
Azure.IoT Build 7:07bc440836b3 240 IOTHUB_CLIENT_RESULT iot_hub_send_result;
Azure.IoT Build 7:07bc440836b3 241
Azure.IoT Build 7:07bc440836b3 242 // 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 7:07bc440836b3 243 if (send_result == MESSAGE_SEND_OK)
AzureIoTClient 4:57e049bce51e 244 {
Azure.IoT Build 7:07bc440836b3 245 iot_hub_send_result = IOTHUB_CLIENT_CONFIRMATION_OK;
AzureIoTClient 4:57e049bce51e 246 }
Azure.IoT Build 7:07bc440836b3 247 // 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 7:07bc440836b3 248 else if (send_result == MESSAGE_SEND_ERROR)
AzureIoTClient 4:57e049bce51e 249 {
Azure.IoT Build 7:07bc440836b3 250 iot_hub_send_result = IOTHUB_CLIENT_CONFIRMATION_ERROR;
AzureIoTClient 5:8d58d20699dd 251 }
AzureIoTClient 4:57e049bce51e 252
Azure.IoT Build 7:07bc440836b3 253 // 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 7:07bc440836b3 254 if (event_tracker->message->callback != NULL)
AzureIoTClient 4:57e049bce51e 255 {
Azure.IoT Build 7:07bc440836b3 256 event_tracker->message->callback(iot_hub_send_result, event_tracker->message->context);
Azure.IoT Build 7:07bc440836b3 257 }
AzureIoTClient 4:57e049bce51e 258
Azure.IoT Build 7:07bc440836b3 259 // 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 7:07bc440836b3 260 IoTHubMessage_Destroy(event_tracker->message->messageHandle);
Azure.IoT Build 7:07bc440836b3 261 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_152: [The callback ‘on_message_send_complete’ shall destroy the IOTHUB_MESSAGE_LIST instance]
Azure.IoT Build 7:07bc440836b3 262 free(event_tracker->message);
AzureIoTClient 4:57e049bce51e 263
Azure.IoT Build 7:07bc440836b3 264 // 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 7:07bc440836b3 265 if (isEventInInProgressList(event_tracker))
AzureIoTClient 4:57e049bce51e 266 {
Azure.IoT Build 7:07bc440836b3 267 removeEventFromInProgressList(event_tracker);
Azure.IoT Build 7:07bc440836b3 268 destroyEventTracker(event_tracker);
AzureIoTClient 4:57e049bce51e 269 }
AzureIoTClient 4:57e049bce51e 270 }
AzureIoTClient 4:57e049bce51e 271 }
AzureIoTClient 4:57e049bce51e 272
Azure.IoT Build 7:07bc440836b3 273 static void on_put_token_complete(const void* context, CBS_OPERATION_RESULT operation_result, unsigned int status_code, const char* status_description)
AzureIoTClient 4:57e049bce51e 274 {
Azure.IoT Build 7:07bc440836b3 275 AMQP_TRANSPORT_INSTANCE* transportState = (AMQP_TRANSPORT_INSTANCE*)context;
Azure.IoT Build 7:07bc440836b3 276
Azure.IoT Build 7:07bc440836b3 277 if (operation_result == OPERATION_RESULT_OK)
AzureIoTClient 4:57e049bce51e 278 {
Azure.IoT Build 7:07bc440836b3 279 transportState->cbs_state = CBS_STATE_AUTHENTICATED;
Azure.IoT Build 7:07bc440836b3 280 }
Azure.IoT Build 7:07bc440836b3 281 }
Azure.IoT Build 7:07bc440836b3 282
Azure.IoT Build 7:07bc440836b3 283 AMQP_VALUE on_message_received(const void* context, MESSAGE_HANDLE message)
Azure.IoT Build 7:07bc440836b3 284 {
Azure.IoT Build 7:07bc440836b3 285 AMQP_VALUE result = NULL;
Azure.IoT Build 7:07bc440836b3 286 IOTHUBMESSAGE_DISPOSITION_RESULT disposition_result = IOTHUBMESSAGE_REJECTED;
Azure.IoT Build 7:07bc440836b3 287
Azure.IoT Build 7:07bc440836b3 288 // 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 289 IOTHUB_CLIENT_LL_HANDLE iothub_client_handle = (IOTHUB_CLIENT_LL_HANDLE)context;
Azure.IoT Build 7:07bc440836b3 290 IOTHUB_MESSAGE_HANDLE iothub_message = NULL;
Azure.IoT Build 7:07bc440836b3 291 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 7:07bc440836b3 292
Azure.IoT Build 7:07bc440836b3 293
Azure.IoT Build 7:07bc440836b3 294 if (message_get_body_type(message, &body_type) != 0)
Azure.IoT Build 7:07bc440836b3 295 {
Azure.IoT Build 7:07bc440836b3 296 LogError("Failed to get the type of the message received by the transport.\r\n");
Azure.IoT Build 7:07bc440836b3 297 }
Azure.IoT Build 7:07bc440836b3 298 else
Azure.IoT Build 7:07bc440836b3 299 {
Azure.IoT Build 7:07bc440836b3 300 if (body_type == MESSAGE_BODY_TYPE_DATA)
AzureIoTClient 4:57e049bce51e 301 {
Azure.IoT Build 7:07bc440836b3 302 BINARY_DATA binary_data;
Azure.IoT Build 7:07bc440836b3 303 if (message_get_body_amqp_data(message, 0, &binary_data) != 0)
Azure.IoT Build 7:07bc440836b3 304 {
Azure.IoT Build 7:07bc440836b3 305 LogError("Failed to get the body of the message received by the transport.\r\n");
Azure.IoT Build 7:07bc440836b3 306 }
Azure.IoT Build 7:07bc440836b3 307 else
Azure.IoT Build 7:07bc440836b3 308 {
Azure.IoT Build 7:07bc440836b3 309 iothub_message = IoTHubMessage_CreateFromByteArray(binary_data.bytes, binary_data.length);
Azure.IoT Build 7:07bc440836b3 310 }
AzureIoTClient 4:57e049bce51e 311 }
Azure.IoT Build 7:07bc440836b3 312 }
Azure.IoT Build 7:07bc440836b3 313
Azure.IoT Build 7:07bc440836b3 314 if (iothub_message == NULL)
Azure.IoT Build 7:07bc440836b3 315 {
Azure.IoT Build 7:07bc440836b3 316 LogError("Transport failed processing the message received.\r\n");
Azure.IoT Build 7:07bc440836b3 317 }
Azure.IoT Build 7:07bc440836b3 318 else
Azure.IoT Build 7:07bc440836b3 319 {
Azure.IoT Build 7:07bc440836b3 320 disposition_result = IoTHubClient_LL_MessageCallback((IOTHUB_CLIENT_LL_HANDLE)context, iothub_message);
Azure.IoT Build 7:07bc440836b3 321
Azure.IoT Build 7:07bc440836b3 322 // 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 323 if (disposition_result == IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 5:8d58d20699dd 324 {
Azure.IoT Build 7:07bc440836b3 325 result = messaging_delivery_accepted();
Azure.IoT Build 7:07bc440836b3 326 }
Azure.IoT Build 7:07bc440836b3 327 // 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 328 else if (disposition_result == IOTHUBMESSAGE_ABANDONED)
Azure.IoT Build 7:07bc440836b3 329 {
Azure.IoT Build 7:07bc440836b3 330 result = messaging_delivery_released();
AzureIoTClient 5:8d58d20699dd 331 }
Azure.IoT Build 7:07bc440836b3 332 // 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 333 else if (disposition_result == IOTHUBMESSAGE_REJECTED)
AzureIoTClient 4:57e049bce51e 334 {
Azure.IoT Build 7:07bc440836b3 335 result = messaging_delivery_rejected("Rejected by application", "Rejected by application");
AzureIoTClient 4:57e049bce51e 336 }
Azure.IoT Build 7:07bc440836b3 337 }
Azure.IoT Build 7:07bc440836b3 338
Azure.IoT Build 7:07bc440836b3 339 return result;
Azure.IoT Build 7:07bc440836b3 340 }
Azure.IoT Build 7:07bc440836b3 341
Azure.IoT Build 7:07bc440836b3 342 XIO_HANDLE getTLSIOTransport(const char* fqdn, int port, const char* certificates)
Azure.IoT Build 7:07bc440836b3 343 {
Azure.IoT Build 7:07bc440836b3 344 TLSIO_CONFIG tls_io_config = { fqdn, port };
Azure.IoT Build 7:07bc440836b3 345 const IO_INTERFACE_DESCRIPTION* io_interface_description = platform_get_default_tlsio();
Azure.IoT Build 7:07bc440836b3 346 return xio_create(io_interface_description, &tls_io_config, NULL);
Azure.IoT Build 7:07bc440836b3 347 }
Azure.IoT Build 7:07bc440836b3 348
Azure.IoT Build 7:07bc440836b3 349 static void destroyConnection(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 350 {
Azure.IoT Build 7:07bc440836b3 351 if (transport_state != NULL)
Azure.IoT Build 7:07bc440836b3 352 {
Azure.IoT Build 7:07bc440836b3 353 if (transport_state->cbs != NULL)
AzureIoTClient 4:57e049bce51e 354 {
Azure.IoT Build 7:07bc440836b3 355 cbs_destroy(transport_state->cbs);
Azure.IoT Build 7:07bc440836b3 356 transport_state->cbs = NULL;
AzureIoTClient 4:57e049bce51e 357 }
Azure.IoT Build 7:07bc440836b3 358
Azure.IoT Build 7:07bc440836b3 359 if (transport_state->session != NULL)
Azure.IoT Build 7:07bc440836b3 360 {
Azure.IoT Build 7:07bc440836b3 361 session_destroy(transport_state->session);
Azure.IoT Build 7:07bc440836b3 362 transport_state->session = NULL;
Azure.IoT Build 7:07bc440836b3 363 }
Azure.IoT Build 7:07bc440836b3 364
Azure.IoT Build 7:07bc440836b3 365 if (transport_state->connection != NULL)
AzureIoTClient 4:57e049bce51e 366 {
Azure.IoT Build 7:07bc440836b3 367 connection_destroy(transport_state->connection);
Azure.IoT Build 7:07bc440836b3 368 transport_state->connection = NULL;
AzureIoTClient 4:57e049bce51e 369 }
Azure.IoT Build 7:07bc440836b3 370
Azure.IoT Build 7:07bc440836b3 371 if (transport_state->sasl_io != NULL)
AzureIoTClient 4:57e049bce51e 372 {
Azure.IoT Build 7:07bc440836b3 373 xio_destroy(transport_state->sasl_io);
Azure.IoT Build 7:07bc440836b3 374 transport_state->sasl_io = NULL;
Azure.IoT Build 7:07bc440836b3 375 }
Azure.IoT Build 7:07bc440836b3 376
Azure.IoT Build 7:07bc440836b3 377 if (transport_state->sasl_mechanism != NULL)
Azure.IoT Build 7:07bc440836b3 378 {
Azure.IoT Build 7:07bc440836b3 379 saslmechanism_destroy(transport_state->sasl_mechanism);
Azure.IoT Build 7:07bc440836b3 380 transport_state->sasl_mechanism = NULL;
AzureIoTClient 4:57e049bce51e 381 }
Azure.IoT Build 7:07bc440836b3 382
Azure.IoT Build 7:07bc440836b3 383 if (transport_state->tls_io != NULL)
Azure.IoT Build 7:07bc440836b3 384 {
Azure.IoT Build 7:07bc440836b3 385 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_034: [IoTHubTransportAMQP_Destroy shall destroy the AMQP TLS I/O transport.]
Azure.IoT Build 7:07bc440836b3 386 xio_destroy(transport_state->tls_io);
Azure.IoT Build 7:07bc440836b3 387 transport_state->tls_io = NULL;
Azure.IoT Build 7:07bc440836b3 388 }
Azure.IoT Build 7:07bc440836b3 389 }
Azure.IoT Build 7:07bc440836b3 390 }
Azure.IoT Build 7:07bc440836b3 391
Azure.IoT Build 7:07bc440836b3 392 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 393 {
Azure.IoT Build 7:07bc440836b3 394 if (context != NULL)
Azure.IoT Build 7:07bc440836b3 395 {
Azure.IoT Build 7:07bc440836b3 396 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)context;
Azure.IoT Build 7:07bc440836b3 397 transport_state->connection_state = new_amqp_management_state;
Azure.IoT Build 7:07bc440836b3 398 }
Azure.IoT Build 7:07bc440836b3 399 }
Azure.IoT Build 7:07bc440836b3 400
Azure.IoT Build 7:07bc440836b3 401 static int establishConnection(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 402 {
Azure.IoT Build 7:07bc440836b3 403 int result;
Azure.IoT Build 7:07bc440836b3 404
Azure.IoT Build 7:07bc440836b3 405 // 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 406 if (transport_state->tls_io == NULL &&
Azure.IoT Build 7:07bc440836b3 407 (transport_state->tls_io = transport_state->tls_io_transport_provider(STRING_c_str(transport_state->iotHubHostFqdn), transport_state->iotHubPort, transport_state->trusted_certificates)) == NULL)
Azure.IoT Build 7:07bc440836b3 408 {
Azure.IoT Build 7:07bc440836b3 409 // 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 410 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 411 LogError("Failed to obtain a TLS I/O transport layer.\r\n");
Azure.IoT Build 7:07bc440836b3 412 }
Azure.IoT Build 7:07bc440836b3 413 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_056: [IoTHubTransportAMQP_DoWork shall create the SASL mechanism using AMQP’s saslmechanism_create() API]
Azure.IoT Build 7:07bc440836b3 414 else if ((transport_state->sasl_mechanism = saslmechanism_create(saslmssbcbs_get_interface(), NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 415 {
Azure.IoT Build 7:07bc440836b3 416 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_057: [If saslmechanism_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 417 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 418 LogError("Failed to create a SASL mechanism.\r\n");
Azure.IoT Build 7:07bc440836b3 419 }
Azure.IoT Build 7:07bc440836b3 420 else
Azure.IoT Build 7:07bc440836b3 421 {
Azure.IoT Build 7:07bc440836b3 422 // 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 423 SASLCLIENTIO_CONFIG sasl_client_config = { transport_state->tls_io, transport_state->sasl_mechanism };
Azure.IoT Build 7:07bc440836b3 424 if ((transport_state->sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_client_config, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 425 {
Azure.IoT Build 7:07bc440836b3 426 // 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 427 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 428 LogError("Failed to create a SASL I/O layer.\r\n");
AzureIoTClient 4:57e049bce51e 429 }
Azure.IoT Build 7:07bc440836b3 430 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_062: [IoTHubTransportAMQP_DoWork shall create the connection with the IoT service using connection_create() AMQP API, passing the SASL I/O layer, IoT Hub FQDN and container ID as parameters (pass NULL for callbacks)]
Azure.IoT Build 7:07bc440836b3 431 else if ((transport_state->connection = connection_create(transport_state->sasl_io, STRING_c_str(transport_state->iotHubHostFqdn), DEFAULT_CONTAINER_ID, NULL, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 432 {
Azure.IoT Build 7:07bc440836b3 433 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_063: [If connection_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately.]
Azure.IoT Build 7:07bc440836b3 434 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 435 LogError("Failed to create the AMQP connection.\r\n");
Azure.IoT Build 7:07bc440836b3 436 }
Azure.IoT Build 7:07bc440836b3 437 // 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 438 else if ((transport_state->session = session_create(transport_state->connection, NULL, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 439 {
Azure.IoT Build 7:07bc440836b3 440 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_138 : [If session_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 441 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 442 LogError("Failed to create the AMQP session.\r\n");
AzureIoTClient 4:57e049bce51e 443 }
AzureIoTClient 4:57e049bce51e 444 else
AzureIoTClient 4:57e049bce51e 445 {
Azure.IoT Build 7:07bc440836b3 446 // 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 447 if (session_set_incoming_window(transport_state->session, (uint32_t)DEFAULT_INCOMING_WINDOW_SIZE) != 0)
Azure.IoT Build 7:07bc440836b3 448 {
Azure.IoT Build 7:07bc440836b3 449 LogError("Failed to set the AMQP incoming window size.\r\n");
Azure.IoT Build 7:07bc440836b3 450 }
Azure.IoT Build 7:07bc440836b3 451
Azure.IoT Build 7:07bc440836b3 452 // 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 453 if (session_set_outgoing_window(transport_state->session, DEFAULT_OUTGOING_WINDOW_SIZE) != 0)
Azure.IoT Build 7:07bc440836b3 454 {
Azure.IoT Build 7:07bc440836b3 455 LogError("Failed to set the AMQP outgoing window size.\r\n");
Azure.IoT Build 7:07bc440836b3 456 }
Azure.IoT Build 7:07bc440836b3 457
Azure.IoT Build 7:07bc440836b3 458 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_066: [IoTHubTransportAMQP_DoWork shall establish the CBS connection using the cbs_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 459 if ((transport_state->cbs = cbs_create(transport_state->session, on_amqp_management_state_changed, NULL)) == NULL)
AzureIoTClient 4:57e049bce51e 460 {
Azure.IoT Build 7:07bc440836b3 461 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_067: [If cbs_create() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 462 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 463 LogError("Failed to create the CBS connection.\r\n");
Azure.IoT Build 7:07bc440836b3 464 }
Azure.IoT Build 7:07bc440836b3 465 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_139: [IoTHubTransportAMQP_DoWork shall open the CBS connection using the cbs_open() AMQP API]
Azure.IoT Build 7:07bc440836b3 466 else if ((cbs_open(transport_state->cbs)) != 0)
Azure.IoT Build 7:07bc440836b3 467 {
Azure.IoT Build 7:07bc440836b3 468 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_140: [If cbs_open() fails, IoTHubTransportAMQP_DoWork shall fail and return immediately]
Azure.IoT Build 7:07bc440836b3 469 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 470 LogError("Failed to open the connection with CBS.\r\n");
Azure.IoT Build 7:07bc440836b3 471 }
Azure.IoT Build 7:07bc440836b3 472 else
Azure.IoT Build 7:07bc440836b3 473 {
Azure.IoT Build 7:07bc440836b3 474 transport_state->connection_establish_time = getSecondsSinceEpoch();
Azure.IoT Build 7:07bc440836b3 475 transport_state->cbs_state = CBS_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 476 result = RESULT_OK;
AzureIoTClient 4:57e049bce51e 477 }
AzureIoTClient 4:57e049bce51e 478 }
AzureIoTClient 4:57e049bce51e 479 }
Azure.IoT Build 7:07bc440836b3 480
Azure.IoT Build 7:07bc440836b3 481 if (result == RESULT_FAILURE)
Azure.IoT Build 7:07bc440836b3 482 {
Azure.IoT Build 7:07bc440836b3 483 destroyConnection(transport_state);
Azure.IoT Build 7:07bc440836b3 484 }
Azure.IoT Build 7:07bc440836b3 485
Azure.IoT Build 7:07bc440836b3 486 return result;
AzureIoTClient 4:57e049bce51e 487 }
AzureIoTClient 4:57e049bce51e 488
Azure.IoT Build 7:07bc440836b3 489 static int startAuthentication(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 490 {
Azure.IoT Build 7:07bc440836b3 491 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 492
Azure.IoT Build 7:07bc440836b3 493 size_t sas_token_create_time = getSecondsSinceEpoch(); // I.e.: NOW, in seconds since epoch.
Azure.IoT Build 7:07bc440836b3 494
Azure.IoT Build 7:07bc440836b3 495 // 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 496 size_t new_expiry_time = sas_token_create_time + (transport_state->sas_token_lifetime / 1000);
Azure.IoT Build 7:07bc440836b3 497
Azure.IoT Build 7:07bc440836b3 498 STRING_HANDLE newSASToken = SASToken_Create(transport_state->deviceKey, transport_state->devicesPath, transport_state->sasTokenKeyName, new_expiry_time);
Azure.IoT Build 7:07bc440836b3 499
Azure.IoT Build 7:07bc440836b3 500 if (newSASToken == NULL)
AzureIoTClient 4:57e049bce51e 501 {
Azure.IoT Build 7:07bc440836b3 502 LogError("Could not generate a new SAS token for the CBS\r\n");
Azure.IoT Build 7:07bc440836b3 503 }
Azure.IoT Build 7:07bc440836b3 504 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 505 {
Azure.IoT Build 7:07bc440836b3 506 LogError("Failed applying new SAS token to CBS\r\n");
AzureIoTClient 4:57e049bce51e 507 }
AzureIoTClient 4:57e049bce51e 508 else
AzureIoTClient 4:57e049bce51e 509 {
Azure.IoT Build 7:07bc440836b3 510 transport_state->cbs_state = CBS_STATE_AUTH_IN_PROGRESS;
Azure.IoT Build 7:07bc440836b3 511 transport_state->current_sas_token_create_time = sas_token_create_time;
Azure.IoT Build 7:07bc440836b3 512 result = RESULT_OK;
AzureIoTClient 4:57e049bce51e 513 }
Azure.IoT Build 7:07bc440836b3 514
Azure.IoT Build 7:07bc440836b3 515 // 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 516 if (newSASToken != NULL)
Azure.IoT Build 7:07bc440836b3 517 STRING_delete(newSASToken);
Azure.IoT Build 7:07bc440836b3 518
AzureIoTClient 4:57e049bce51e 519 return result;
AzureIoTClient 4:57e049bce51e 520 }
AzureIoTClient 4:57e049bce51e 521
Azure.IoT Build 7:07bc440836b3 522 static int verifyAuthenticationTimeout(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 523 {
Azure.IoT Build 7:07bc440836b3 524 return ((getSecondsSinceEpoch() - transport_state->current_sas_token_create_time) * 1000 >= transport_state->cbs_request_timeout) ? RESULT_TIMEOUT : RESULT_OK;
Azure.IoT Build 7:07bc440836b3 525 }
AzureIoTClient 4:57e049bce51e 526
Azure.IoT Build 7:07bc440836b3 527 static void handleEventSendTimeouts(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 528 {
Azure.IoT Build 7:07bc440836b3 529 time_t current_time = get_time(NULL);
Azure.IoT Build 7:07bc440836b3 530 PDLIST_ENTRY entry = transport_state->inProgress.Flink;
Azure.IoT Build 7:07bc440836b3 531
Azure.IoT Build 7:07bc440836b3 532 while (entry != &transport_state->inProgress)
AzureIoTClient 4:57e049bce51e 533 {
Azure.IoT Build 7:07bc440836b3 534 EVENT_TRACKER* event_tracker = containingRecord(entry, EVENT_TRACKER, entry);
Azure.IoT Build 7:07bc440836b3 535
Azure.IoT Build 7:07bc440836b3 536 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_085: [IoTHubTransportAMQP_DoWork shall attempt to send all the queued messages for up to ‘message_send_timeout’ milliseconds]
Azure.IoT Build 7:07bc440836b3 537 if (difftime(current_time, event_tracker->time_sent) * 1000 >= transport_state->message_send_timeout)
Azure.IoT Build 7:07bc440836b3 538 {
Azure.IoT Build 7:07bc440836b3 539 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_120: [If a ‘message_send_timeout’ occurs the timed out events removed from the inProgress and the upper layer notified of the send error]
Azure.IoT Build 7:07bc440836b3 540 on_message_send_complete(event_tracker, MESSAGE_SEND_ERROR);
Azure.IoT Build 7:07bc440836b3 541 }
Azure.IoT Build 7:07bc440836b3 542
Azure.IoT Build 7:07bc440836b3 543 entry = entry->Flink;
AzureIoTClient 4:57e049bce51e 544 }
Azure.IoT Build 7:07bc440836b3 545 }
Azure.IoT Build 7:07bc440836b3 546
Azure.IoT Build 7:07bc440836b3 547 static void attachDeviceClientTypeToLink(LINK_HANDLE link)
Azure.IoT Build 7:07bc440836b3 548 {
Azure.IoT Build 7:07bc440836b3 549 fields attach_properties = NULL;
Azure.IoT Build 7:07bc440836b3 550 AMQP_VALUE deviceClientTypeKeyName = NULL;
Azure.IoT Build 7:07bc440836b3 551 AMQP_VALUE deviceClientTypeValue = NULL;
Azure.IoT Build 7:07bc440836b3 552 int result;
Azure.IoT Build 7:07bc440836b3 553
Azure.IoT Build 7:07bc440836b3 554 //
Azure.IoT Build 7:07bc440836b3 555 // Attempt to add the device client type string to the attach properties.
Azure.IoT Build 7:07bc440836b3 556 // If this doesn't happen, well, this isn't that important. We can operate
Azure.IoT Build 7:07bc440836b3 557 // without this property. It's worth noting that even though we are going
Azure.IoT Build 7:07bc440836b3 558 // on, the reasons any of these operations fail don't bode well for the
Azure.IoT Build 7:07bc440836b3 559 // actual upcoming attach.
Azure.IoT Build 7:07bc440836b3 560 //
Azure.IoT Build 7:07bc440836b3 561
Azure.IoT Build 7:07bc440836b3 562 // 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 563
Azure.IoT Build 7:07bc440836b3 564 if ((attach_properties = amqpvalue_create_map()) == NULL)
AzureIoTClient 4:57e049bce51e 565 {
Azure.IoT Build 7:07bc440836b3 566 LogError("Failed to create the map for device client type.\r\n");
AzureIoTClient 4:57e049bce51e 567 }
Azure.IoT Build 7:07bc440836b3 568 else if ((deviceClientTypeKeyName = amqpvalue_create_symbol("com.microsoft:client-version")) == NULL)
Azure.IoT Build 7:07bc440836b3 569 {
Azure.IoT Build 7:07bc440836b3 570 LogError("Failed to create the key name for the device client type.\r\n");
Azure.IoT Build 7:07bc440836b3 571 }
Azure.IoT Build 7:07bc440836b3 572 else if ((deviceClientTypeValue = amqpvalue_create_string(CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION)) == NULL)
AzureIoTClient 4:57e049bce51e 573 {
Azure.IoT Build 7:07bc440836b3 574 LogError("Failed to create the key value for the device client type.\r\n");
Azure.IoT Build 7:07bc440836b3 575 }
Azure.IoT Build 7:07bc440836b3 576 else if ((result = amqpvalue_set_map_value(attach_properties, deviceClientTypeKeyName, deviceClientTypeValue)) != 0)
Azure.IoT Build 7:07bc440836b3 577 {
Azure.IoT Build 7:07bc440836b3 578 LogError("Failed to set the property map for the device client type. Error code is: %d\r\n", result);
AzureIoTClient 4:57e049bce51e 579 }
Azure.IoT Build 7:07bc440836b3 580 else if ((result = link_set_attach_properties(link, attach_properties)) != 0)
AzureIoTClient 4:57e049bce51e 581 {
Azure.IoT Build 7:07bc440836b3 582 LogError("Unable to attach the device client type to the link properties. Error code is: %d\r\n", result);
Azure.IoT Build 7:07bc440836b3 583 }
Azure.IoT Build 7:07bc440836b3 584 amqpvalue_destroy(attach_properties);
Azure.IoT Build 7:07bc440836b3 585 amqpvalue_destroy(deviceClientTypeKeyName);
Azure.IoT Build 7:07bc440836b3 586 amqpvalue_destroy(deviceClientTypeValue);
Azure.IoT Build 7:07bc440836b3 587
Azure.IoT Build 7:07bc440836b3 588 }
Azure.IoT Build 7:07bc440836b3 589
Azure.IoT Build 7:07bc440836b3 590 static int destroyEventSender(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 591 {
Azure.IoT Build 7:07bc440836b3 592 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 593
Azure.IoT Build 7:07bc440836b3 594 if (transport_state->message_sender != NULL)
Azure.IoT Build 7:07bc440836b3 595 {
Azure.IoT Build 7:07bc440836b3 596 messagesender_destroy(transport_state->message_sender);
Azure.IoT Build 7:07bc440836b3 597
Azure.IoT Build 7:07bc440836b3 598 transport_state->message_sender = NULL;
Azure.IoT Build 7:07bc440836b3 599
Azure.IoT Build 7:07bc440836b3 600 link_destroy(transport_state->sender_link);
Azure.IoT Build 7:07bc440836b3 601
Azure.IoT Build 7:07bc440836b3 602 transport_state->sender_link = NULL;
Azure.IoT Build 7:07bc440836b3 603
Azure.IoT Build 7:07bc440836b3 604 result = RESULT_OK;
AzureIoTClient 4:57e049bce51e 605 }
AzureIoTClient 4:57e049bce51e 606
AzureIoTClient 4:57e049bce51e 607 return result;
AzureIoTClient 4:57e049bce51e 608 }
AzureIoTClient 4:57e049bce51e 609
Azure.IoT Build 7:07bc440836b3 610 static int createEventSender(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 611 {
Azure.IoT Build 7:07bc440836b3 612 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 613
Azure.IoT Build 7:07bc440836b3 614 if (transport_state->message_sender == NULL)
AzureIoTClient 4:57e049bce51e 615 {
Azure.IoT Build 7:07bc440836b3 616 AMQP_VALUE source = NULL;
Azure.IoT Build 7:07bc440836b3 617 AMQP_VALUE target = NULL;
Azure.IoT Build 7:07bc440836b3 618
Azure.IoT Build 7:07bc440836b3 619 // 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 620 if ((source = messaging_create_source(MESSAGE_SENDER_SOURCE_ADDRESS)) == NULL)
AzureIoTClient 4:57e049bce51e 621 {
Azure.IoT Build 7:07bc440836b3 622 LogError("Failed creating AMQP messaging source attribute.\r\n");
AzureIoTClient 4:57e049bce51e 623 }
Azure.IoT Build 7:07bc440836b3 624 else if ((target = messaging_create_target(STRING_c_str(transport_state->targetAddress))) == NULL)
AzureIoTClient 4:57e049bce51e 625 {
Azure.IoT Build 7:07bc440836b3 626 LogError("Failed creating AMQP messaging target attribute.\r\n");
Azure.IoT Build 7:07bc440836b3 627 }
Azure.IoT Build 7:07bc440836b3 628 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 629 {
Azure.IoT Build 7:07bc440836b3 630 // 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]
Azure.IoT Build 7:07bc440836b3 631 LogError("Failed creating AMQP link for message sender.\r\n");
AzureIoTClient 4:57e049bce51e 632 }
AzureIoTClient 4:57e049bce51e 633 else
AzureIoTClient 4:57e049bce51e 634 {
Azure.IoT Build 7:07bc440836b3 635 // 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 636 if (link_set_max_message_size(transport_state->sender_link, MESSAGE_SENDER_MAX_LINK_SIZE) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 637 {
Azure.IoT Build 7:07bc440836b3 638 LogError("Failed setting AMQP link max message size.\r\n");
Azure.IoT Build 7:07bc440836b3 639 }
Azure.IoT Build 7:07bc440836b3 640
Azure.IoT Build 7:07bc440836b3 641 attachDeviceClientTypeToLink(transport_state->sender_link);
Azure.IoT Build 7:07bc440836b3 642
Azure.IoT Build 7:07bc440836b3 643 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_070: [IoTHubTransportAMQP_DoWork shall create the AMQP message sender using messagesender_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 644 if ((transport_state->message_sender = messagesender_create(transport_state->sender_link, NULL, NULL, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 645 {
Azure.IoT Build 7:07bc440836b3 646 // 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]
Azure.IoT Build 7:07bc440836b3 647 LogError("Could not allocate AMQP message sender\r\n");
Azure.IoT Build 7:07bc440836b3 648 }
Azure.IoT Build 7:07bc440836b3 649 else
Azure.IoT Build 7:07bc440836b3 650 {
Azure.IoT Build 7:07bc440836b3 651 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_072: [IoTHubTransportAMQP_DoWork shall open the AMQP message sender using messagesender_open() AMQP API]
Azure.IoT Build 7:07bc440836b3 652 if (messagesender_open(transport_state->message_sender) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 653 {
Azure.IoT Build 7:07bc440836b3 654 // 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]
Azure.IoT Build 7:07bc440836b3 655 LogError("Failed opening the AMQP message sender.\r\n");
Azure.IoT Build 7:07bc440836b3 656 }
Azure.IoT Build 7:07bc440836b3 657 else
Azure.IoT Build 7:07bc440836b3 658 {
Azure.IoT Build 7:07bc440836b3 659 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 660 }
Azure.IoT Build 7:07bc440836b3 661 }
AzureIoTClient 4:57e049bce51e 662 }
Azure.IoT Build 7:07bc440836b3 663
Azure.IoT Build 7:07bc440836b3 664 if (source != NULL)
Azure.IoT Build 7:07bc440836b3 665 amqpvalue_destroy(source);
Azure.IoT Build 7:07bc440836b3 666 if (target != NULL)
Azure.IoT Build 7:07bc440836b3 667 amqpvalue_destroy(target);
AzureIoTClient 4:57e049bce51e 668 }
Azure.IoT Build 7:07bc440836b3 669
Azure.IoT Build 7:07bc440836b3 670 return result;
AzureIoTClient 4:57e049bce51e 671 }
Azure.IoT Build 7:07bc440836b3 672
Azure.IoT Build 7:07bc440836b3 673 static int destroyMessageReceiver(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 674 {
Azure.IoT Build 7:07bc440836b3 675 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 676
Azure.IoT Build 7:07bc440836b3 677 if (transport_state->message_receiver != NULL)
AzureIoTClient 4:57e049bce51e 678 {
Azure.IoT Build 7:07bc440836b3 679 if (messagereceiver_close(transport_state->message_receiver) != RESULT_OK)
AzureIoTClient 4:57e049bce51e 680 {
Azure.IoT Build 7:07bc440836b3 681 LogError("Failed closing the AMQP message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 682 }
Azure.IoT Build 7:07bc440836b3 683
Azure.IoT Build 7:07bc440836b3 684 messagereceiver_destroy(transport_state->message_receiver);
Azure.IoT Build 7:07bc440836b3 685
Azure.IoT Build 7:07bc440836b3 686 transport_state->message_receiver = NULL;
Azure.IoT Build 7:07bc440836b3 687
Azure.IoT Build 7:07bc440836b3 688 link_destroy(transport_state->receiver_link);
Azure.IoT Build 7:07bc440836b3 689
Azure.IoT Build 7:07bc440836b3 690 transport_state->receiver_link = NULL;
Azure.IoT Build 7:07bc440836b3 691
Azure.IoT Build 7:07bc440836b3 692 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 693 }
Azure.IoT Build 7:07bc440836b3 694
Azure.IoT Build 7:07bc440836b3 695 return result;
Azure.IoT Build 7:07bc440836b3 696 }
Azure.IoT Build 7:07bc440836b3 697
Azure.IoT Build 7:07bc440836b3 698 static int createMessageReceiver(AMQP_TRANSPORT_INSTANCE* transport_state, IOTHUB_CLIENT_LL_HANDLE iothub_client_handle)
Azure.IoT Build 7:07bc440836b3 699 {
Azure.IoT Build 7:07bc440836b3 700 int result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 701
Azure.IoT Build 7:07bc440836b3 702 if (transport_state->message_sender == NULL)
Azure.IoT Build 7:07bc440836b3 703 {
Azure.IoT Build 7:07bc440836b3 704 AMQP_VALUE source = NULL;
Azure.IoT Build 7:07bc440836b3 705 AMQP_VALUE target = NULL;
Azure.IoT Build 7:07bc440836b3 706
Azure.IoT Build 7:07bc440836b3 707 // 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 708 if ((source = messaging_create_source(STRING_c_str(transport_state->messageReceiveAddress))) == NULL)
Azure.IoT Build 7:07bc440836b3 709 {
Azure.IoT Build 7:07bc440836b3 710 LogError("Failed creating AMQP message receiver source attribute.\r\n");
Azure.IoT Build 7:07bc440836b3 711 }
Azure.IoT Build 7:07bc440836b3 712 else if ((target = messaging_create_target(MESSAGE_RECEIVER_TARGET_ADDRESS)) == NULL)
Azure.IoT Build 7:07bc440836b3 713 {
Azure.IoT Build 7:07bc440836b3 714 LogError("Failed creating AMQP message receiver target attribute.\r\n");
Azure.IoT Build 7:07bc440836b3 715 }
Azure.IoT Build 7:07bc440836b3 716 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 717 {
Azure.IoT Build 7:07bc440836b3 718 // 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]
Azure.IoT Build 7:07bc440836b3 719 LogError("Failed creating AMQP link for message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 720 }
Azure.IoT Build 7:07bc440836b3 721 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_076: [IoTHubTransportAMQP_DoWork shall set the receiver link settle mode as receiver_settle_mode_first]
Azure.IoT Build 7:07bc440836b3 722 else if (link_set_rcv_settle_mode(transport_state->receiver_link, receiver_settle_mode_first) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 723 {
Azure.IoT Build 7:07bc440836b3 724 // 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]
Azure.IoT Build 7:07bc440836b3 725 LogError("Failed setting AMQP link settle mode for message receiver.\r\n");
AzureIoTClient 4:57e049bce51e 726 }
AzureIoTClient 4:57e049bce51e 727 else
AzureIoTClient 4:57e049bce51e 728 {
Azure.IoT Build 7:07bc440836b3 729 // 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 730 if (link_set_max_message_size(transport_state->receiver_link, MESSAGE_RECEIVER_MAX_LINK_SIZE) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 731 {
Azure.IoT Build 7:07bc440836b3 732 LogError("Failed setting AMQP link max message size for message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 733 }
Azure.IoT Build 7:07bc440836b3 734
Azure.IoT Build 7:07bc440836b3 735 attachDeviceClientTypeToLink(transport_state->receiver_link);
AzureIoTClient 4:57e049bce51e 736
Azure.IoT Build 7:07bc440836b3 737 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_077: [IoTHubTransportAMQP_DoWork shall create the AMQP message receiver using messagereceiver_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 738 if ((transport_state->message_receiver = messagereceiver_create(transport_state->receiver_link, NULL, NULL)) == NULL)
Azure.IoT Build 7:07bc440836b3 739 {
Azure.IoT Build 7:07bc440836b3 740 // 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]
Azure.IoT Build 7:07bc440836b3 741 LogError("Could not allocate AMQP message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 742 }
Azure.IoT Build 7:07bc440836b3 743 else
AzureIoTClient 4:57e049bce51e 744 {
Azure.IoT Build 7:07bc440836b3 745 // 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 7:07bc440836b3 746 // 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 747 if (messagereceiver_open(transport_state->message_receiver, on_message_received, (const void*)iothub_client_handle) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 748 {
Azure.IoT Build 7:07bc440836b3 749 // 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]
Azure.IoT Build 7:07bc440836b3 750 LogError("Failed opening the AMQP message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 751 }
Azure.IoT Build 7:07bc440836b3 752 else
Azure.IoT Build 7:07bc440836b3 753 {
Azure.IoT Build 7:07bc440836b3 754 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 755 }
AzureIoTClient 4:57e049bce51e 756 }
AzureIoTClient 4:57e049bce51e 757 }
Azure.IoT Build 7:07bc440836b3 758
Azure.IoT Build 7:07bc440836b3 759 if (source != NULL)
Azure.IoT Build 7:07bc440836b3 760 amqpvalue_destroy(source);
Azure.IoT Build 7:07bc440836b3 761 if (target != NULL)
Azure.IoT Build 7:07bc440836b3 762 amqpvalue_destroy(target);
AzureIoTClient 4:57e049bce51e 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 sendPendingEvents(AMQP_TRANSPORT_INSTANCE* transport_state)
AzureIoTClient 4:57e049bce51e 769 {
Azure.IoT Build 7:07bc440836b3 770 int result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 771 IOTHUB_MESSAGE_LIST* message = NULL;
Azure.IoT Build 7:07bc440836b3 772
Azure.IoT Build 7:07bc440836b3 773 while ((message = getNextEventToSend(transport_state)) != NULL)
AzureIoTClient 4:57e049bce51e 774 {
Azure.IoT Build 7:07bc440836b3 775 result = RESULT_FAILURE;
Azure.IoT Build 7:07bc440836b3 776
Azure.IoT Build 7:07bc440836b3 777 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
Azure.IoT Build 7:07bc440836b3 778 const unsigned char* messageContent = NULL;
Azure.IoT Build 7:07bc440836b3 779 size_t messageContentSize = 0;
Azure.IoT Build 7:07bc440836b3 780 MESSAGE_HANDLE amqp_message;
Azure.IoT Build 7:07bc440836b3 781 bool is_message_error = false;
Azure.IoT Build 7:07bc440836b3 782 EVENT_TRACKER* event_tracker;
Azure.IoT Build 7:07bc440836b3 783
Azure.IoT Build 7:07bc440836b3 784 // 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 7:07bc440836b3 785 if ((event_tracker = trackEvent(message, transport_state)) == NULL)
Azure.IoT Build 7:07bc440836b3 786 {
Azure.IoT Build 7:07bc440836b3 787 LogError("Failed tracking the event to be sent.\r\n");
Azure.IoT Build 7:07bc440836b3 788 }
Azure.IoT Build 7:07bc440836b3 789 // 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 7:07bc440836b3 790 else if (contentType == IOTHUBMESSAGE_BYTEARRAY &&
Azure.IoT Build 7:07bc440836b3 791 IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &messageContentSize) != IOTHUB_MESSAGE_OK)
AzureIoTClient 4:57e049bce51e 792 {
Azure.IoT Build 7:07bc440836b3 793 LogError("Failed getting the BYTE array representation of the event content to be sent.\r\n");
Azure.IoT Build 7:07bc440836b3 794 is_message_error = true;
Azure.IoT Build 7:07bc440836b3 795 }
Azure.IoT Build 7:07bc440836b3 796 // 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 797 // 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 798 else if (contentType == IOTHUBMESSAGE_STRING &&
Azure.IoT Build 7:07bc440836b3 799 ((messageContent = IoTHubMessage_GetString(message->messageHandle)) == NULL || (messageContentSize = strlen(messageContent)) <= 0))
Azure.IoT Build 7:07bc440836b3 800 {
Azure.IoT Build 7:07bc440836b3 801 LogError("Failed getting the STRING representation of the event content to be sent.\r\n");
Azure.IoT Build 7:07bc440836b3 802 is_message_error = true;
AzureIoTClient 4:57e049bce51e 803 }
Azure.IoT Build 7:07bc440836b3 804 // 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 805 else if (contentType == IOTHUBMESSAGE_UNKNOWN)
AzureIoTClient 4:57e049bce51e 806 {
Azure.IoT Build 7:07bc440836b3 807 LogError("Cannot send events with content type IOTHUBMESSAGE_UNKNOWN.\r\n");
Azure.IoT Build 7:07bc440836b3 808 is_message_error = true;
Azure.IoT Build 7:07bc440836b3 809 }
Azure.IoT Build 7:07bc440836b3 810 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_093: [IoTHubTransportAMQP_DoWork shall create an amqp message using message_create() AMQP API]
Azure.IoT Build 7:07bc440836b3 811 else if ((amqp_message = message_create()) == NULL)
Azure.IoT Build 7:07bc440836b3 812 {
Azure.IoT Build 7:07bc440836b3 813 LogError("Failed allocating the AMQP message for sending the event.\r\n");
AzureIoTClient 4:57e049bce51e 814 }
AzureIoTClient 4:57e049bce51e 815 else
AzureIoTClient 4:57e049bce51e 816 {
Azure.IoT Build 7:07bc440836b3 817 BINARY_DATA binary_data = { messageContent, messageContentSize };
Azure.IoT Build 7:07bc440836b3 818
Azure.IoT Build 7:07bc440836b3 819 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_095: [IoTHubTransportAMQP_DoWork shall set the AMQP message body using message_add_body_amqp_data() AMQP API]
Azure.IoT Build 7:07bc440836b3 820 if (message_add_body_amqp_data(amqp_message, binary_data) != RESULT_OK)
AzureIoTClient 4:57e049bce51e 821 {
Azure.IoT Build 7:07bc440836b3 822 LogError("Failed setting the body of the AMQP message.\r\n");
AzureIoTClient 4:57e049bce51e 823 }
Azure.IoT Build 7:07bc440836b3 824 else
AzureIoTClient 4:57e049bce51e 825 {
Azure.IoT Build 7:07bc440836b3 826 // 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 7:07bc440836b3 827 if (messagesender_send(transport_state->message_sender, amqp_message, on_message_send_complete, (const void*)event_tracker) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 828 {
Azure.IoT Build 7:07bc440836b3 829 LogError("Failed sending the AMQP message.\r\n");
Azure.IoT Build 7:07bc440836b3 830 }
Azure.IoT Build 7:07bc440836b3 831 else
Azure.IoT Build 7:07bc440836b3 832 {
Azure.IoT Build 7:07bc440836b3 833 result = RESULT_OK;
Azure.IoT Build 7:07bc440836b3 834 }
AzureIoTClient 4:57e049bce51e 835 }
Azure.IoT Build 7:07bc440836b3 836 }
Azure.IoT Build 7:07bc440836b3 837
Azure.IoT Build 7:07bc440836b3 838 if (amqp_message != NULL)
Azure.IoT Build 7:07bc440836b3 839 {
Azure.IoT Build 7:07bc440836b3 840 // It can be destroyed because AMQP keeps a clone of the message.
Azure.IoT Build 7:07bc440836b3 841 message_destroy(amqp_message);
Azure.IoT Build 7:07bc440836b3 842 }
Azure.IoT Build 7:07bc440836b3 843
Azure.IoT Build 7:07bc440836b3 844 if (result != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 845 {
Azure.IoT Build 7:07bc440836b3 846 if (event_tracker == NULL)
AzureIoTClient 5:8d58d20699dd 847 {
AzureIoTClient 5:8d58d20699dd 848 break;
AzureIoTClient 5:8d58d20699dd 849 }
AzureIoTClient 4:57e049bce51e 850 else
AzureIoTClient 4:57e049bce51e 851 {
Azure.IoT Build 7:07bc440836b3 852 // 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 7:07bc440836b3 853 // 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 7:07bc440836b3 854 if (is_message_error)
AzureIoTClient 4:57e049bce51e 855 {
Azure.IoT Build 7:07bc440836b3 856 on_message_send_complete(event_tracker, MESSAGE_SEND_ERROR);
AzureIoTClient 4:57e049bce51e 857 }
AzureIoTClient 4:57e049bce51e 858 else
AzureIoTClient 4:57e049bce51e 859 {
Azure.IoT Build 7:07bc440836b3 860 // 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 7:07bc440836b3 861 // 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 7:07bc440836b3 862 // 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 7:07bc440836b3 863 rollEventBackToWaitList(event_tracker, transport_state);
Azure.IoT Build 7:07bc440836b3 864 break;
AzureIoTClient 4:57e049bce51e 865 }
AzureIoTClient 4:57e049bce51e 866 }
AzureIoTClient 4:57e049bce51e 867 }
AzureIoTClient 4:57e049bce51e 868 }
Azure.IoT Build 7:07bc440836b3 869
Azure.IoT Build 7:07bc440836b3 870 return result;
Azure.IoT Build 7:07bc440836b3 871 }
Azure.IoT Build 7:07bc440836b3 872
Azure.IoT Build 7:07bc440836b3 873 static bool isSasTokenRefreshRequired(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 874 {
Azure.IoT Build 7:07bc440836b3 875 return ((getSecondsSinceEpoch() - transport_state->current_sas_token_create_time) >= (transport_state->sas_token_refresh_time / 1000)) ? true : false;
Azure.IoT Build 7:07bc440836b3 876 }
Azure.IoT Build 7:07bc440836b3 877
Azure.IoT Build 7:07bc440836b3 878 static void prepareForConnectionRetry(AMQP_TRANSPORT_INSTANCE* transport_state)
Azure.IoT Build 7:07bc440836b3 879 {
Azure.IoT Build 7:07bc440836b3 880 destroyConnection(transport_state);
Azure.IoT Build 7:07bc440836b3 881 rollEventsBackToWaitList(transport_state);
AzureIoTClient 4:57e049bce51e 882 }
AzureIoTClient 4:57e049bce51e 883
Azure.IoT Build 7:07bc440836b3 884
Azure.IoT Build 7:07bc440836b3 885 // API functions
Azure.IoT Build 7:07bc440836b3 886
Azure.IoT Build 7:07bc440836b3 887 static TRANSPORT_HANDLE IoTHubTransportAMQP_Create(const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 4:57e049bce51e 888 {
Azure.IoT Build 7:07bc440836b3 889 AMQP_TRANSPORT_INSTANCE* transport_state = NULL;
Azure.IoT Build 7:07bc440836b3 890 bool cleanup_required = false;
Azure.IoT Build 7:07bc440836b3 891 int deviceIdLength = -1;
AzureIoTClient 4:57e049bce51e 892
Azure.IoT Build 7:07bc440836b3 893 // 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 894 if (config == NULL || config->upperConfig == NULL || config->waitingToSend == NULL)
Azure.IoT Build 7:07bc440836b3 895 {
Azure.IoT Build 7:07bc440836b3 896 LogError("IoTHub AMQP client transport null configuration parameter.\r\n");
Azure.IoT Build 7:07bc440836b3 897 }
Azure.IoT Build 7:07bc440836b3 898 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_006: [IoTHubTransportAMQP_Create shall fail and return NULL if any fields of the config structure are NULL.]
Azure.IoT Build 7:07bc440836b3 899 else if (config->upperConfig->protocol == NULL)
Azure.IoT Build 7:07bc440836b3 900 {
Azure.IoT Build 7:07bc440836b3 901 LogError("Invalid configuration (NULL protocol detected)\r\n");
Azure.IoT Build 7:07bc440836b3 902 }
Azure.IoT Build 7:07bc440836b3 903 else if (config->upperConfig->deviceId == NULL)
Azure.IoT Build 7:07bc440836b3 904 {
Azure.IoT Build 7:07bc440836b3 905 LogError("Invalid configuration (NULL deviceId detected)\r\n");
Azure.IoT Build 7:07bc440836b3 906 }
Azure.IoT Build 7:07bc440836b3 907 else if (config->upperConfig->deviceKey == NULL)
Azure.IoT Build 7:07bc440836b3 908 {
Azure.IoT Build 7:07bc440836b3 909 LogError("Invalid configuration (NULL deviceKey detected)\r\n");
Azure.IoT Build 7:07bc440836b3 910 }
Azure.IoT Build 7:07bc440836b3 911 else if (config->upperConfig->iotHubName == NULL)
Azure.IoT Build 7:07bc440836b3 912 {
Azure.IoT Build 7:07bc440836b3 913 LogError("Invalid configuration (NULL iotHubName detected)\r\n");
AzureIoTClient 4:57e049bce51e 914 }
Azure.IoT Build 7:07bc440836b3 915 else if (config->upperConfig->iotHubSuffix == NULL)
Azure.IoT Build 7:07bc440836b3 916 {
Azure.IoT Build 7:07bc440836b3 917 LogError("Invalid configuration (NULL iotHubSuffix detected)\r\n");
Azure.IoT Build 7:07bc440836b3 918 }
Azure.IoT Build 7:07bc440836b3 919 else if (!config->waitingToSend)
AzureIoTClient 4:57e049bce51e 920 {
Azure.IoT Build 7:07bc440836b3 921 LogError("Invalid configuration (NULL waitingToSend list detected)\r\n");
Azure.IoT Build 7:07bc440836b3 922 }
Azure.IoT Build 7:07bc440836b3 923 // 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 924 else if ((deviceIdLength = strlen(config->upperConfig->deviceId)) == 0 ||
Azure.IoT Build 7:07bc440836b3 925 (strlen(config->upperConfig->deviceKey) == 0) ||
Azure.IoT Build 7:07bc440836b3 926 (strlen(config->upperConfig->iotHubName) == 0) ||
Azure.IoT Build 7:07bc440836b3 927 (strlen(config->upperConfig->iotHubSuffix) == 0))
Azure.IoT Build 7:07bc440836b3 928 {
Azure.IoT Build 7:07bc440836b3 929 LogError("Zero-length config parameter (deviceId, deviceKey, iotHubName or iotHubSuffix)\r\n");
Azure.IoT Build 7:07bc440836b3 930 }
Azure.IoT Build 7:07bc440836b3 931 // 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 932 else if (deviceIdLength > 128U)
Azure.IoT Build 7:07bc440836b3 933 {
Azure.IoT Build 7:07bc440836b3 934 LogError("deviceId is too long\r\n");
Azure.IoT Build 7:07bc440836b3 935 }
Azure.IoT Build 7:07bc440836b3 936 // 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 937 else if ((strlen(config->upperConfig->iotHubName) + strlen(config->upperConfig->iotHubSuffix)) > (RFC1035_MAX_FQDN_LENGTH - 1))
Azure.IoT Build 7:07bc440836b3 938 {
Azure.IoT Build 7:07bc440836b3 939 LogError("The lengths of iotHubName and iotHubSuffix together exceed the maximum FQDN length allowed (RFC 1035)\r\n");
AzureIoTClient 4:57e049bce51e 940 }
AzureIoTClient 4:57e049bce51e 941 else
AzureIoTClient 4:57e049bce51e 942 {
Azure.IoT Build 7:07bc440836b3 943 // 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 944 transport_state = (AMQP_TRANSPORT_INSTANCE*)malloc(sizeof(AMQP_TRANSPORT_INSTANCE));
AzureIoTClient 4:57e049bce51e 945
Azure.IoT Build 7:07bc440836b3 946 if (transport_state == NULL)
AzureIoTClient 4:57e049bce51e 947 {
AzureIoTClient 4:57e049bce51e 948 LogError("Could not allocate AMQP transport state\r\n");
AzureIoTClient 4:57e049bce51e 949 }
AzureIoTClient 4:57e049bce51e 950 else
AzureIoTClient 4:57e049bce51e 951 {
Azure.IoT Build 7:07bc440836b3 952 transport_state->iotHubHostFqdn = NULL;
Azure.IoT Build 7:07bc440836b3 953 transport_state->iotHubPort = DEFAULT_IOTHUB_AMQP_PORT;
Azure.IoT Build 7:07bc440836b3 954 transport_state->trusted_certificates = NULL;
Azure.IoT Build 7:07bc440836b3 955 transport_state->deviceKey = NULL;
Azure.IoT Build 7:07bc440836b3 956 transport_state->devicesPath = NULL;
Azure.IoT Build 7:07bc440836b3 957 transport_state->messageReceiveAddress = NULL;
Azure.IoT Build 7:07bc440836b3 958 transport_state->sasTokenKeyName = NULL;
Azure.IoT Build 7:07bc440836b3 959 transport_state->targetAddress = NULL;
Azure.IoT Build 7:07bc440836b3 960 transport_state->waitingToSend = config->waitingToSend;
Azure.IoT Build 7:07bc440836b3 961
Azure.IoT Build 7:07bc440836b3 962 transport_state->cbs = NULL;
Azure.IoT Build 7:07bc440836b3 963 transport_state->cbs_state = CBS_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 964 transport_state->current_sas_token_create_time = 0;
Azure.IoT Build 7:07bc440836b3 965 transport_state->connection = NULL;
Azure.IoT Build 7:07bc440836b3 966 transport_state->connection_state = AMQP_MANAGEMENT_STATE_IDLE;
Azure.IoT Build 7:07bc440836b3 967 transport_state->connection_establish_time = 0;
Azure.IoT Build 7:07bc440836b3 968 transport_state->iothub_client_handle = NULL;
Azure.IoT Build 7:07bc440836b3 969 transport_state->receive_messages = false;
Azure.IoT Build 7:07bc440836b3 970 transport_state->message_receiver = NULL;
Azure.IoT Build 7:07bc440836b3 971 transport_state->message_sender = NULL;
Azure.IoT Build 7:07bc440836b3 972 transport_state->receiver_link = NULL;
Azure.IoT Build 7:07bc440836b3 973 transport_state->sasl_io = NULL;
Azure.IoT Build 7:07bc440836b3 974 transport_state->sasl_mechanism = NULL;
Azure.IoT Build 7:07bc440836b3 975 transport_state->sender_link = NULL;
Azure.IoT Build 7:07bc440836b3 976 transport_state->session = NULL;
Azure.IoT Build 7:07bc440836b3 977 transport_state->tls_io = NULL;
Azure.IoT Build 7:07bc440836b3 978 transport_state->tls_io_transport_provider = getTLSIOTransport;
Azure.IoT Build 7:07bc440836b3 979
Azure.IoT Build 7:07bc440836b3 980 transport_state->waitingToSend = config->waitingToSend;
Azure.IoT Build 7:07bc440836b3 981 DList_InitializeListHead(&transport_state->inProgress);
Azure.IoT Build 7:07bc440836b3 982
Azure.IoT Build 7:07bc440836b3 983 // 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 984 if ((transport_state->iotHubHostFqdn = concat3Params(config->upperConfig->iotHubName, ".", config->upperConfig->iotHubSuffix)) == NULL)
AzureIoTClient 4:57e049bce51e 985 {
Azure.IoT Build 7:07bc440836b3 986 LogError("Failed to set transport_state->iotHubHostFqdn.\r\n");
Azure.IoT Build 7:07bc440836b3 987 cleanup_required = true;
AzureIoTClient 4:57e049bce51e 988 }
Azure.IoT Build 7:07bc440836b3 989 // 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 990 else if ((transport_state->devicesPath = concat3Params(STRING_c_str(transport_state->iotHubHostFqdn), "/devices/", config->upperConfig->deviceId)) == NULL)
AzureIoTClient 4:57e049bce51e 991 {
Azure.IoT Build 7:07bc440836b3 992 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_013: [If creating devicesPath fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
Azure.IoT Build 7:07bc440836b3 993 LogError("Failed to allocate transport_state->devicesPath.\r\n");
Azure.IoT Build 7:07bc440836b3 994 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 995 }
Azure.IoT Build 7:07bc440836b3 996 // 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 997 else if ((transport_state->targetAddress = concat3Params("amqps://", STRING_c_str(transport_state->devicesPath), "/messages/events")) == NULL)
Azure.IoT Build 7:07bc440836b3 998 {
Azure.IoT Build 7:07bc440836b3 999 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_015: [If creating the targetAddress fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
Azure.IoT Build 7:07bc440836b3 1000 LogError("Failed to allocate transport_state->targetAddress.\r\n");
Azure.IoT Build 7:07bc440836b3 1001 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1002 }
Azure.IoT Build 7:07bc440836b3 1003 // 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 1004 else if ((transport_state->messageReceiveAddress = concat3Params("amqps://", STRING_c_str(transport_state->devicesPath), "/messages/devicebound")) == NULL)
Azure.IoT Build 7:07bc440836b3 1005 {
Azure.IoT Build 7:07bc440836b3 1006 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_054: [If creating the messageReceiveAddress fails for any reason then IoTHubTransportAMQP_Create shall fail and return NULL.]
Azure.IoT Build 7:07bc440836b3 1007 LogError("Failed to allocate transport_state->messageReceiveAddress.\r\n");
Azure.IoT Build 7:07bc440836b3 1008 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1009 }
Azure.IoT Build 7:07bc440836b3 1010 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_016: [IoTHubTransportAMQP_Create shall initialize handle->sasTokenKeyName with a zero-length STRING_HANDLE instance.]
Azure.IoT Build 7:07bc440836b3 1011 else if ((transport_state->sasTokenKeyName = STRING_new()) == NULL)
Azure.IoT Build 7:07bc440836b3 1012 {
Azure.IoT Build 7:07bc440836b3 1013 // 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.]
Azure.IoT Build 7:07bc440836b3 1014 LogError("Failed to allocate transport_state->sasTokenKeyName.\r\n");
Azure.IoT Build 7:07bc440836b3 1015 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1016 }
Azure.IoT Build 7:07bc440836b3 1017 // 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]
Azure.IoT Build 7:07bc440836b3 1018 else if ((transport_state->deviceKey = STRING_new()) == NULL ||
Azure.IoT Build 7:07bc440836b3 1019 STRING_copy(transport_state->deviceKey, config->upperConfig->deviceKey) != 0)
Azure.IoT Build 7:07bc440836b3 1020 {
Azure.IoT Build 7:07bc440836b3 1021 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_019: [If IoTHubTransportAMQP_Create fails to copy config->deviceKey, the function shall fail and return NULL.]
Azure.IoT Build 7:07bc440836b3 1022 LogError("Failed to allocate transport_state->deviceKey.\r\n");
Azure.IoT Build 7:07bc440836b3 1023 cleanup_required = true;
Azure.IoT Build 7:07bc440836b3 1024 }
Azure.IoT Build 7:07bc440836b3 1025 else
Azure.IoT Build 7:07bc440836b3 1026 {
Azure.IoT Build 7:07bc440836b3 1027 // 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 1028 transport_state->sas_token_lifetime = DEFAULT_SAS_TOKEN_LIFETIME_MS;
Azure.IoT Build 7:07bc440836b3 1029
Azure.IoT Build 7:07bc440836b3 1030 // 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 1031 transport_state->sas_token_refresh_time = transport_state->sas_token_lifetime / 2;
Azure.IoT Build 7:07bc440836b3 1032
Azure.IoT Build 7:07bc440836b3 1033 // 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 1034 transport_state->cbs_request_timeout = DEFAULT_CBS_REQUEST_TIMEOUT_MS;
Azure.IoT Build 7:07bc440836b3 1035
Azure.IoT Build 7:07bc440836b3 1036 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_130 : [IoTHubTransportAMQP_Create shall set parameter transport_state->message_send_timeout with the default value of 300000 (milliseconds).]
Azure.IoT Build 7:07bc440836b3 1037 transport_state->message_send_timeout = DEFAULT_MESSAGE_SEND_TIMEOUT_MS;
AzureIoTClient 4:57e049bce51e 1038 }
AzureIoTClient 4:57e049bce51e 1039 }
AzureIoTClient 4:57e049bce51e 1040 }
Azure.IoT Build 7:07bc440836b3 1041
Azure.IoT Build 7:07bc440836b3 1042 if (cleanup_required)
Azure.IoT Build 7:07bc440836b3 1043 {
Azure.IoT Build 7:07bc440836b3 1044 if (transport_state->deviceKey != NULL)
Azure.IoT Build 7:07bc440836b3 1045 STRING_delete(transport_state->deviceKey);
Azure.IoT Build 7:07bc440836b3 1046 if (transport_state->sasTokenKeyName != NULL)
Azure.IoT Build 7:07bc440836b3 1047 STRING_delete(transport_state->sasTokenKeyName);
Azure.IoT Build 7:07bc440836b3 1048 if (transport_state->targetAddress != NULL)
Azure.IoT Build 7:07bc440836b3 1049 STRING_delete(transport_state->targetAddress);
Azure.IoT Build 7:07bc440836b3 1050 if (transport_state->messageReceiveAddress != NULL)
Azure.IoT Build 7:07bc440836b3 1051 STRING_delete(transport_state->messageReceiveAddress);
Azure.IoT Build 7:07bc440836b3 1052 if (transport_state->devicesPath != NULL)
Azure.IoT Build 7:07bc440836b3 1053 STRING_delete(transport_state->devicesPath);
Azure.IoT Build 7:07bc440836b3 1054 if (transport_state->iotHubHostFqdn != NULL)
Azure.IoT Build 7:07bc440836b3 1055 STRING_delete(transport_state->iotHubHostFqdn);
Azure.IoT Build 7:07bc440836b3 1056
Azure.IoT Build 7:07bc440836b3 1057 free(transport_state);
Azure.IoT Build 7:07bc440836b3 1058 transport_state = NULL;
Azure.IoT Build 7:07bc440836b3 1059 }
Azure.IoT Build 7:07bc440836b3 1060
Azure.IoT Build 7:07bc440836b3 1061 // 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 1062 return transport_state;
AzureIoTClient 4:57e049bce51e 1063 }
AzureIoTClient 4:57e049bce51e 1064
Azure.IoT Build 7:07bc440836b3 1065 static void IoTHubTransportAMQP_Destroy(TRANSPORT_HANDLE handle)
AzureIoTClient 4:57e049bce51e 1066 {
Azure.IoT Build 7:07bc440836b3 1067 if (handle != NULL)
Azure.IoT Build 7:07bc440836b3 1068 {
Azure.IoT Build 7:07bc440836b3 1069 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1070
Azure.IoT Build 7:07bc440836b3 1071 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_024: [IoTHubTransportAMQP_Destroy shall destroy the AMQP message_sender.]
Azure.IoT Build 7:07bc440836b3 1072 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_029 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP link.]
Azure.IoT Build 7:07bc440836b3 1073 destroyEventSender(transport_state);
Azure.IoT Build 7:07bc440836b3 1074
Azure.IoT Build 7:07bc440836b3 1075 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_025: [IoTHubTransportAMQP_Destroy shall destroy the AMQP message_receiver.]
Azure.IoT Build 7:07bc440836b3 1076 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_029 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP link.]
Azure.IoT Build 7:07bc440836b3 1077 destroyMessageReceiver(transport_state);
Azure.IoT Build 7:07bc440836b3 1078
Azure.IoT Build 7:07bc440836b3 1079 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_027 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP cbs instance]
Azure.IoT Build 7:07bc440836b3 1080 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_030 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP session.]
Azure.IoT Build 7:07bc440836b3 1081 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_031 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP connection.]
Azure.IoT Build 7:07bc440836b3 1082 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_032 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP SASL I / O transport.]
Azure.IoT Build 7:07bc440836b3 1083 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_033 : [IoTHubTransportAMQP_Destroy shall destroy the AMQP SASL mechanism.]
Azure.IoT Build 7:07bc440836b3 1084 destroyConnection(transport_state);
Azure.IoT Build 7:07bc440836b3 1085
Azure.IoT Build 7:07bc440836b3 1086 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_035 : [IoTHubTransportAMQP_Destroy shall delete its internally - set parameters(deviceKey, targetAddress, devicesPath, sasTokenKeyName).]
Azure.IoT Build 7:07bc440836b3 1087 STRING_delete(transport_state->targetAddress);
Azure.IoT Build 7:07bc440836b3 1088 STRING_delete(transport_state->messageReceiveAddress);
Azure.IoT Build 7:07bc440836b3 1089 STRING_delete(transport_state->sasTokenKeyName);
Azure.IoT Build 7:07bc440836b3 1090 STRING_delete(transport_state->deviceKey);
Azure.IoT Build 7:07bc440836b3 1091 STRING_delete(transport_state->devicesPath);
Azure.IoT Build 7:07bc440836b3 1092 STRING_delete(transport_state->iotHubHostFqdn);
Azure.IoT Build 7:07bc440836b3 1093
Azure.IoT Build 7:07bc440836b3 1094 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_036 : [IoTHubTransportAMQP_Destroy shall return the remaining items in inProgress to waitingToSend list.]
Azure.IoT Build 7:07bc440836b3 1095 rollEventsBackToWaitList(transport_state);
Azure.IoT Build 7:07bc440836b3 1096
Azure.IoT Build 7:07bc440836b3 1097 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_150: [IoTHubTransportAMQP_Destroy shall destroy the transport instance]
Azure.IoT Build 7:07bc440836b3 1098 free(transport_state);
Azure.IoT Build 7:07bc440836b3 1099 }
Azure.IoT Build 7:07bc440836b3 1100 }
Azure.IoT Build 7:07bc440836b3 1101
Azure.IoT Build 7:07bc440836b3 1102 static void IoTHubTransportAMQP_DoWork(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
Azure.IoT Build 7:07bc440836b3 1103 {
Azure.IoT Build 7:07bc440836b3 1104 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_051: [IoTHubTransportAMQP_DoWork shall fail and return immediately if the transport handle parameter is NULL]
AzureIoTClient 4:57e049bce51e 1105 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1106 {
Azure.IoT Build 7:07bc440836b3 1107 LogError("IoTHubClient DoWork failed: transport handle parameter is NULL.\r\n");
Azure.IoT Build 7:07bc440836b3 1108 }
Azure.IoT Build 7:07bc440836b3 1109 // Codes_[IoTHubTransportAMQP_DoWork shall fail and return immediately if the client handle parameter is NULL]
Azure.IoT Build 7:07bc440836b3 1110 else if (iotHubClientHandle == NULL)
Azure.IoT Build 7:07bc440836b3 1111 {
Azure.IoT Build 7:07bc440836b3 1112 LogError("IoTHubClient DoWork failed: client handle parameter is NULL.\r\n");
AzureIoTClient 4:57e049bce51e 1113 }
AzureIoTClient 4:57e049bce51e 1114 else
AzureIoTClient 4:57e049bce51e 1115 {
Azure.IoT Build 7:07bc440836b3 1116 bool trigger_connection_retry = false;
Azure.IoT Build 7:07bc440836b3 1117 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1118
Azure.IoT Build 7:07bc440836b3 1119 // 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 1120 transport_state->iothub_client_handle = iotHubClientHandle;
Azure.IoT Build 7:07bc440836b3 1121
Azure.IoT Build 7:07bc440836b3 1122 // 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]
Azure.IoT Build 7:07bc440836b3 1123 if (transport_state->connection == NULL &&
Azure.IoT Build 7:07bc440836b3 1124 establishConnection(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1125 {
Azure.IoT Build 7:07bc440836b3 1126 LogError("AMQP transport failed to establish connection with service.\r\n");
Azure.IoT Build 7:07bc440836b3 1127 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1128 }
Azure.IoT Build 7:07bc440836b3 1129 // 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 7:07bc440836b3 1130 // 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 1131 else if ((transport_state->cbs_state == CBS_STATE_IDLE || isSasTokenRefreshRequired(transport_state)) &&
Azure.IoT Build 7:07bc440836b3 1132 startAuthentication(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1133 {
Azure.IoT Build 7:07bc440836b3 1134 // 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]
Azure.IoT Build 7:07bc440836b3 1135 LogError("Failed authenticating AMQP connection within CBS.\r\n");
Azure.IoT Build 7:07bc440836b3 1136 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1137 }
Azure.IoT Build 7:07bc440836b3 1138 // 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 7:07bc440836b3 1139 else if (transport_state->cbs_state == CBS_STATE_AUTH_IN_PROGRESS &&
Azure.IoT Build 7:07bc440836b3 1140 verifyAuthenticationTimeout(transport_state) == RESULT_TIMEOUT)
Azure.IoT Build 7:07bc440836b3 1141 {
Azure.IoT Build 7:07bc440836b3 1142 LogError("AMQP transport authentication timed out.\r\n");
Azure.IoT Build 7:07bc440836b3 1143 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1144 }
Azure.IoT Build 7:07bc440836b3 1145 else if (transport_state->cbs_state == CBS_STATE_AUTHENTICATED)
Azure.IoT Build 7:07bc440836b3 1146 {
Azure.IoT Build 7:07bc440836b3 1147 // 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 1148 if (transport_state->receive_messages == true &&
Azure.IoT Build 7:07bc440836b3 1149 transport_state->message_receiver == NULL &&
Azure.IoT Build 7:07bc440836b3 1150 createMessageReceiver(transport_state, iotHubClientHandle) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1151 {
Azure.IoT Build 7:07bc440836b3 1152 LogError("Failed creating AMQP transport message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 1153 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1154 }
Azure.IoT Build 7:07bc440836b3 1155 // 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 1156 else if (transport_state->receive_messages == false &&
Azure.IoT Build 7:07bc440836b3 1157 transport_state->message_receiver != NULL &&
Azure.IoT Build 7:07bc440836b3 1158 destroyMessageReceiver(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1159 {
Azure.IoT Build 7:07bc440836b3 1160 LogError("Failed destroying AMQP transport message receiver.\r\n");
Azure.IoT Build 7:07bc440836b3 1161 }
Azure.IoT Build 7:07bc440836b3 1162
Azure.IoT Build 7:07bc440836b3 1163 if (transport_state->message_sender == NULL &&
Azure.IoT Build 7:07bc440836b3 1164 createEventSender(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1165 {
Azure.IoT Build 7:07bc440836b3 1166 LogError("Failed creating AMQP transport event sender.\r\n");
Azure.IoT Build 7:07bc440836b3 1167 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1168 }
Azure.IoT Build 7:07bc440836b3 1169 else if (sendPendingEvents(transport_state) != RESULT_OK)
Azure.IoT Build 7:07bc440836b3 1170 {
Azure.IoT Build 7:07bc440836b3 1171 LogError("AMQP transport failed sending events.\r\n");
Azure.IoT Build 7:07bc440836b3 1172 trigger_connection_retry = true;
Azure.IoT Build 7:07bc440836b3 1173 }
Azure.IoT Build 7:07bc440836b3 1174 }
Azure.IoT Build 7:07bc440836b3 1175
Azure.IoT Build 7:07bc440836b3 1176 if (trigger_connection_retry)
Azure.IoT Build 7:07bc440836b3 1177 {
Azure.IoT Build 7:07bc440836b3 1178 prepareForConnectionRetry(transport_state);
Azure.IoT Build 7:07bc440836b3 1179 }
Azure.IoT Build 7:07bc440836b3 1180 else
Azure.IoT Build 7:07bc440836b3 1181 {
Azure.IoT Build 7:07bc440836b3 1182 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_103: [IoTHubTransportAMQP_DoWork shall invoke connection_dowork() on AMQP for triggering sending and receiving messages]
Azure.IoT Build 7:07bc440836b3 1183 connection_dowork(transport_state->connection);
Azure.IoT Build 7:07bc440836b3 1184 }
Azure.IoT Build 7:07bc440836b3 1185
Azure.IoT Build 7:07bc440836b3 1186 handleEventSendTimeouts(transport_state);
Azure.IoT Build 7:07bc440836b3 1187 }
Azure.IoT Build 7:07bc440836b3 1188 }
Azure.IoT Build 7:07bc440836b3 1189
Azure.IoT Build 7:07bc440836b3 1190 static int IoTHubTransportAMQP_Subscribe(TRANSPORT_HANDLE handle)
Azure.IoT Build 7:07bc440836b3 1191 {
Azure.IoT Build 7:07bc440836b3 1192 int result;
Azure.IoT Build 7:07bc440836b3 1193
Azure.IoT Build 7:07bc440836b3 1194 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_037: [IoTHubTransportAMQP_Subscribe shall fail if the transport handle parameter received is NULL.]
Azure.IoT Build 7:07bc440836b3 1195 if (handle == NULL)
Azure.IoT Build 7:07bc440836b3 1196 {
Azure.IoT Build 7:07bc440836b3 1197 LogError("Invalid handle to IoTHubClient AMQP transport.\r\n");
Azure.IoT Build 7:07bc440836b3 1198 result = __LINE__;
Azure.IoT Build 7:07bc440836b3 1199 }
Azure.IoT Build 7:07bc440836b3 1200 else
Azure.IoT Build 7:07bc440836b3 1201 {
Azure.IoT Build 7:07bc440836b3 1202 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_038: [IoTHubTransportAMQP_Subscribe shall set transport_handle->receive_messages to true and return success code.]
Azure.IoT Build 7:07bc440836b3 1203 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1204 transport_state->receive_messages = true;
AzureIoTClient 4:57e049bce51e 1205 result = 0;
AzureIoTClient 4:57e049bce51e 1206 }
Azure.IoT Build 7:07bc440836b3 1207
AzureIoTClient 4:57e049bce51e 1208 return result;
AzureIoTClient 4:57e049bce51e 1209 }
AzureIoTClient 4:57e049bce51e 1210
Azure.IoT Build 7:07bc440836b3 1211 static void IoTHubTransportAMQP_Unsubscribe(TRANSPORT_HANDLE handle)
AzureIoTClient 4:57e049bce51e 1212 {
Azure.IoT Build 7:07bc440836b3 1213 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_039: [IoTHubTransportAMQP_Unsubscribe shall fail if the transport handle parameter received is NULL.]
Azure.IoT Build 7:07bc440836b3 1214 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1215 {
Azure.IoT Build 7:07bc440836b3 1216 LogError("Invalid handle to IoTHubClient AMQP transport.\r\n");
Azure.IoT Build 7:07bc440836b3 1217 }
Azure.IoT Build 7:07bc440836b3 1218 else
Azure.IoT Build 7:07bc440836b3 1219 {
Azure.IoT Build 7:07bc440836b3 1220 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_040: [IoTHubTransportAMQP_Unsubscribe shall set transport_handle->receive_messages to false and return success code.]
Azure.IoT Build 7:07bc440836b3 1221 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
Azure.IoT Build 7:07bc440836b3 1222 transport_state->receive_messages = false;
AzureIoTClient 4:57e049bce51e 1223 }
AzureIoTClient 4:57e049bce51e 1224 }
AzureIoTClient 4:57e049bce51e 1225
Azure.IoT Build 7:07bc440836b3 1226 static IOTHUB_CLIENT_RESULT IoTHubTransportAMQP_GetSendStatus(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 4:57e049bce51e 1227 {
AzureIoTClient 4:57e049bce51e 1228 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 4:57e049bce51e 1229
Azure.IoT Build 7:07bc440836b3 1230 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_041: [IoTHubTransportAMQP_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter.]
AzureIoTClient 4:57e049bce51e 1231 if (handle == NULL)
AzureIoTClient 4:57e049bce51e 1232 {
AzureIoTClient 4:57e049bce51e 1233 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 4:57e049bce51e 1234 LogError("Invalid handle to IoTHubClient AMQP transport instance.\r\n");
AzureIoTClient 4:57e049bce51e 1235 }
AzureIoTClient 4:57e049bce51e 1236 else if (iotHubClientStatus == NULL)
AzureIoTClient 4:57e049bce51e 1237 {
AzureIoTClient 4:57e049bce51e 1238 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 4:57e049bce51e 1239 LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.\r\n");
AzureIoTClient 4:57e049bce51e 1240 }
AzureIoTClient 4:57e049bce51e 1241 else
AzureIoTClient 4:57e049bce51e 1242 {
Azure.IoT Build 7:07bc440836b3 1243 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 4:57e049bce51e 1244
Azure.IoT Build 7:07bc440836b3 1245 // 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 1246 if (!DList_IsListEmpty(transport_state->waitingToSend) || !DList_IsListEmpty(&(transport_state->inProgress)))
AzureIoTClient 4:57e049bce51e 1247 {
AzureIoTClient 4:57e049bce51e 1248 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 4:57e049bce51e 1249 }
Azure.IoT Build 7:07bc440836b3 1250 // 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 1251 else
AzureIoTClient 4:57e049bce51e 1252 {
AzureIoTClient 4:57e049bce51e 1253 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 4:57e049bce51e 1254 }
AzureIoTClient 4:57e049bce51e 1255
AzureIoTClient 4:57e049bce51e 1256 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:57e049bce51e 1257 }
AzureIoTClient 4:57e049bce51e 1258
AzureIoTClient 4:57e049bce51e 1259 return result;
AzureIoTClient 4:57e049bce51e 1260 }
AzureIoTClient 4:57e049bce51e 1261
Azure.IoT Build 7:07bc440836b3 1262 static IOTHUB_CLIENT_RESULT IoTHubTransportAMQP_SetOption(TRANSPORT_HANDLE handle, const char* option, const void* value)
AzureIoTClient 4:57e049bce51e 1263 {
AzureIoTClient 4:57e049bce51e 1264 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 4:57e049bce51e 1265
Azure.IoT Build 7:07bc440836b3 1266 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_044: [If handle parameter is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
Azure.IoT Build 7:07bc440836b3 1267 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_045: [If parameter optionName is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
Azure.IoT Build 7:07bc440836b3 1268 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_046: [If parameter value is NULL then IoTHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
AzureIoTClient 5:8d58d20699dd 1269 if (
AzureIoTClient 5:8d58d20699dd 1270 (handle == NULL) ||
AzureIoTClient 5:8d58d20699dd 1271 (option == NULL) ||
AzureIoTClient 5:8d58d20699dd 1272 (value == NULL)
AzureIoTClient 5:8d58d20699dd 1273 )
AzureIoTClient 5:8d58d20699dd 1274 {
AzureIoTClient 5:8d58d20699dd 1275 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 7:07bc440836b3 1276 LogError("Invalid parameter (NULL) passed to AMQP transport SetOption()\r\n");
AzureIoTClient 5:8d58d20699dd 1277 }
AzureIoTClient 5:8d58d20699dd 1278 else
AzureIoTClient 5:8d58d20699dd 1279 {
Azure.IoT Build 7:07bc440836b3 1280 AMQP_TRANSPORT_INSTANCE* transport_state = (AMQP_TRANSPORT_INSTANCE*)handle;
AzureIoTClient 4:57e049bce51e 1281
Azure.IoT Build 7:07bc440836b3 1282 if (strcmp("trusted_certificates", option) == 0)
Azure.IoT Build 7:07bc440836b3 1283 {
Azure.IoT Build 7:07bc440836b3 1284 transport_state->trusted_certificates = (char*)value;
Azure.IoT Build 7:07bc440836b3 1285 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 7:07bc440836b3 1286 }
Azure.IoT Build 7:07bc440836b3 1287 // 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 7:07bc440836b3 1288 else if (strcmp("sas_token_lifetime", option) == 0)
Azure.IoT Build 7:07bc440836b3 1289 {
Azure.IoT Build 7:07bc440836b3 1290 transport_state->sas_token_lifetime = *((size_t*)value);
Azure.IoT Build 7:07bc440836b3 1291 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 7:07bc440836b3 1292 }
Azure.IoT Build 7:07bc440836b3 1293 // 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 7:07bc440836b3 1294 else if (strcmp("sas_token_refresh_time", option) == 0)
Azure.IoT Build 7:07bc440836b3 1295 {
Azure.IoT Build 7:07bc440836b3 1296 transport_state->sas_token_refresh_time = *((size_t*)value);
Azure.IoT Build 7:07bc440836b3 1297 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 7:07bc440836b3 1298 }
Azure.IoT Build 7:07bc440836b3 1299 // 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 7:07bc440836b3 1300 else if (strcmp("cbs_request_timeout", option) == 0)
Azure.IoT Build 7:07bc440836b3 1301 {
Azure.IoT Build 7:07bc440836b3 1302 transport_state->cbs_request_timeout = *((size_t*)value);
Azure.IoT Build 7:07bc440836b3 1303 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 7:07bc440836b3 1304 }
Azure.IoT Build 7:07bc440836b3 1305 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_149: [IotHubTransportAMQP_SetOption shall save and apply the value if the option name is "message_send_timeout", returning IOTHUB_CLIENT_OK]
Azure.IoT Build 7:07bc440836b3 1306 else if (strcmp("message_send_timeout", option) == 0)
Azure.IoT Build 7:07bc440836b3 1307 {
Azure.IoT Build 7:07bc440836b3 1308 transport_state->message_send_timeout = *((size_t*)value);
Azure.IoT Build 7:07bc440836b3 1309 result = IOTHUB_CLIENT_OK;
Azure.IoT Build 7:07bc440836b3 1310 }
Azure.IoT Build 7:07bc440836b3 1311 // Codes_SRS_IOTHUBTRANSPORTAMQP_09_047: [If optionName is not an option supported then IotHubTransportAMQP_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]
Azure.IoT Build 7:07bc440836b3 1312 else
Azure.IoT Build 7:07bc440836b3 1313 {
Azure.IoT Build 7:07bc440836b3 1314 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 7:07bc440836b3 1315 LogError("Invalid option (%s) passed to uAMQP transport SetOption()\r\n", option);
Azure.IoT Build 7:07bc440836b3 1316 }
Azure.IoT Build 7:07bc440836b3 1317 }
AzureIoTClient 4:57e049bce51e 1318
AzureIoTClient 4:57e049bce51e 1319 return result;
AzureIoTClient 4:57e049bce51e 1320 }
AzureIoTClient 4:57e049bce51e 1321
Azure.IoT Build 7:07bc440836b3 1322 static TRANSPORT_PROVIDER thisTransportProvider = {
Azure.IoT Build 7:07bc440836b3 1323 IoTHubTransportAMQP_SetOption,
Azure.IoT Build 7:07bc440836b3 1324 IoTHubTransportAMQP_Create,
Azure.IoT Build 7:07bc440836b3 1325 IoTHubTransportAMQP_Destroy,
Azure.IoT Build 7:07bc440836b3 1326 IoTHubTransportAMQP_Subscribe,
Azure.IoT Build 7:07bc440836b3 1327 IoTHubTransportAMQP_Unsubscribe,
Azure.IoT Build 7:07bc440836b3 1328 IoTHubTransportAMQP_DoWork,
Azure.IoT Build 7:07bc440836b3 1329 IoTHubTransportAMQP_GetSendStatus
AzureIoTClient 4:57e049bce51e 1330 };
AzureIoTClient 4:57e049bce51e 1331
AzureIoTClient 4:57e049bce51e 1332 extern const void* AMQP_Protocol(void)
AzureIoTClient 4:57e049bce51e 1333 {
Azure.IoT Build 7:07bc440836b3 1334 return &thisTransportProvider;
AzureIoTClient 4:57e049bce51e 1335 }