A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Mon May 08 10:50:45 2017 -0700
Revision:
24:2c59c2d43ebf
Parent:
23:1111ee8bcba4
Child:
25:1101516ee67d
1.1.14

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:6ae2f7bca550 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:6ae2f7bca550 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:6ae2f7bca550 3
Azure.IoT Build 0:6ae2f7bca550 4 #include <stdlib.h>
AzureIoTClient 6:641a9672db08 5 #include <stdbool.h>
Azure.IoT Build 0:6ae2f7bca550 6 #include <string.h>
AzureIoTClient 19:000ab4e6a2c1 7 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 21:f9c433d8e6ca 8 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 5:ae49385aff34 9 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/message_sender.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/amqpvalue_to_string.h"
Azure.IoT Build 0:6ae2f7bca550 12
Azure.IoT Build 0:6ae2f7bca550 13 typedef enum MESSAGE_SEND_STATE_TAG
Azure.IoT Build 0:6ae2f7bca550 14 {
AzureIoTClient 6:641a9672db08 15 MESSAGE_SEND_STATE_NOT_SENT,
AzureIoTClient 6:641a9672db08 16 MESSAGE_SEND_STATE_PENDING
Azure.IoT Build 0:6ae2f7bca550 17 } MESSAGE_SEND_STATE;
Azure.IoT Build 0:6ae2f7bca550 18
Azure.IoT Build 0:6ae2f7bca550 19 typedef enum SEND_ONE_MESSAGE_RESULT_TAG
Azure.IoT Build 0:6ae2f7bca550 20 {
AzureIoTClient 6:641a9672db08 21 SEND_ONE_MESSAGE_OK,
AzureIoTClient 6:641a9672db08 22 SEND_ONE_MESSAGE_ERROR,
AzureIoTClient 6:641a9672db08 23 SEND_ONE_MESSAGE_BUSY
Azure.IoT Build 0:6ae2f7bca550 24 } SEND_ONE_MESSAGE_RESULT;
Azure.IoT Build 0:6ae2f7bca550 25
Azure.IoT Build 0:6ae2f7bca550 26 typedef struct MESSAGE_WITH_CALLBACK_TAG
Azure.IoT Build 0:6ae2f7bca550 27 {
AzureIoTClient 6:641a9672db08 28 MESSAGE_HANDLE message;
AzureIoTClient 6:641a9672db08 29 ON_MESSAGE_SEND_COMPLETE on_message_send_complete;
AzureIoTClient 6:641a9672db08 30 void* context;
AzureIoTClient 6:641a9672db08 31 MESSAGE_SENDER_HANDLE message_sender;
AzureIoTClient 6:641a9672db08 32 MESSAGE_SEND_STATE message_send_state;
Azure.IoT Build 0:6ae2f7bca550 33 } MESSAGE_WITH_CALLBACK;
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 typedef struct MESSAGE_SENDER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 36 {
AzureIoTClient 6:641a9672db08 37 LINK_HANDLE link;
AzureIoTClient 6:641a9672db08 38 size_t message_count;
AzureIoTClient 6:641a9672db08 39 MESSAGE_WITH_CALLBACK** messages;
AzureIoTClient 6:641a9672db08 40 MESSAGE_SENDER_STATE message_sender_state;
AzureIoTClient 6:641a9672db08 41 ON_MESSAGE_SENDER_STATE_CHANGED on_message_sender_state_changed;
AzureIoTClient 6:641a9672db08 42 void* on_message_sender_state_changed_context;
AzureIoTClient 6:641a9672db08 43 unsigned int is_trace_on : 1;
Azure.IoT Build 0:6ae2f7bca550 44 } MESSAGE_SENDER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 45
Azure.IoT Build 0:6ae2f7bca550 46 static void remove_pending_message_by_index(MESSAGE_SENDER_INSTANCE* message_sender_instance, size_t index)
Azure.IoT Build 0:6ae2f7bca550 47 {
AzureIoTClient 6:641a9672db08 48 MESSAGE_WITH_CALLBACK** new_messages;
Azure.IoT Build 0:6ae2f7bca550 49
AzureIoTClient 6:641a9672db08 50 if (message_sender_instance->messages[index]->message != NULL)
AzureIoTClient 6:641a9672db08 51 {
AzureIoTClient 6:641a9672db08 52 message_destroy(message_sender_instance->messages[index]->message);
AzureIoTClient 6:641a9672db08 53 message_sender_instance->messages[index]->message = NULL;
AzureIoTClient 6:641a9672db08 54 }
Azure.IoT Build 0:6ae2f7bca550 55
AzureIoTClient 21:f9c433d8e6ca 56 free(message_sender_instance->messages[index]);
Azure.IoT Build 0:6ae2f7bca550 57
AzureIoTClient 6:641a9672db08 58 if (message_sender_instance->message_count - index > 1)
AzureIoTClient 6:641a9672db08 59 {
AzureIoTClient 6:641a9672db08 60 (void)memmove(&message_sender_instance->messages[index], &message_sender_instance->messages[index + 1], sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count - index - 1));
AzureIoTClient 6:641a9672db08 61 }
Azure.IoT Build 0:6ae2f7bca550 62
AzureIoTClient 6:641a9672db08 63 message_sender_instance->message_count--;
Azure.IoT Build 0:6ae2f7bca550 64
AzureIoTClient 6:641a9672db08 65 if (message_sender_instance->message_count > 0)
AzureIoTClient 6:641a9672db08 66 {
AzureIoTClient 21:f9c433d8e6ca 67 new_messages = (MESSAGE_WITH_CALLBACK**)realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count));
AzureIoTClient 6:641a9672db08 68 if (new_messages != NULL)
AzureIoTClient 6:641a9672db08 69 {
AzureIoTClient 6:641a9672db08 70 message_sender_instance->messages = new_messages;
AzureIoTClient 6:641a9672db08 71 }
AzureIoTClient 6:641a9672db08 72 }
AzureIoTClient 6:641a9672db08 73 else
AzureIoTClient 6:641a9672db08 74 {
AzureIoTClient 21:f9c433d8e6ca 75 free(message_sender_instance->messages);
AzureIoTClient 6:641a9672db08 76 message_sender_instance->messages = NULL;
AzureIoTClient 6:641a9672db08 77 }
Azure.IoT Build 0:6ae2f7bca550 78 }
Azure.IoT Build 0:6ae2f7bca550 79
Azure.IoT Build 0:6ae2f7bca550 80 static void remove_pending_message(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_WITH_CALLBACK* message_with_callback)
Azure.IoT Build 0:6ae2f7bca550 81 {
AzureIoTClient 6:641a9672db08 82 size_t i;
Azure.IoT Build 0:6ae2f7bca550 83
AzureIoTClient 6:641a9672db08 84 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 6:641a9672db08 85 {
AzureIoTClient 6:641a9672db08 86 if (message_sender_instance->messages[i] == message_with_callback)
AzureIoTClient 6:641a9672db08 87 {
AzureIoTClient 6:641a9672db08 88 remove_pending_message_by_index(message_sender_instance, i);
AzureIoTClient 6:641a9672db08 89 break;
AzureIoTClient 6:641a9672db08 90 }
AzureIoTClient 6:641a9672db08 91 }
Azure.IoT Build 0:6ae2f7bca550 92 }
Azure.IoT Build 0:6ae2f7bca550 93
AzureIoTClient 23:1111ee8bcba4 94 static void on_delivery_settled(void* context, delivery_number delivery_no, LINK_DELIVERY_SETTLE_REASON reason, AMQP_VALUE delivery_state)
Azure.IoT Build 0:6ae2f7bca550 95 {
AzureIoTClient 6:641a9672db08 96 MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)context;
AzureIoTClient 6:641a9672db08 97 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_with_callback->message_sender;
AzureIoTClient 6:641a9672db08 98 (void)delivery_no;
Azure.IoT Build 0:6ae2f7bca550 99
AzureIoTClient 6:641a9672db08 100 if (message_with_callback->on_message_send_complete != NULL)
AzureIoTClient 6:641a9672db08 101 {
AzureIoTClient 23:1111ee8bcba4 102 switch (reason)
AzureIoTClient 7:9e9ab3b0efef 103 {
AzureIoTClient 23:1111ee8bcba4 104 case LINK_DELIVERY_SETTLE_REASON_DISPOSITION_RECEIVED:
AzureIoTClient 23:1111ee8bcba4 105 if (delivery_state == NULL)
AzureIoTClient 7:9e9ab3b0efef 106 {
AzureIoTClient 23:1111ee8bcba4 107 LogError("delivery state not provided");
AzureIoTClient 7:9e9ab3b0efef 108 }
AzureIoTClient 7:9e9ab3b0efef 109 else
AzureIoTClient 7:9e9ab3b0efef 110 {
AzureIoTClient 23:1111ee8bcba4 111 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(delivery_state);
AzureIoTClient 23:1111ee8bcba4 112
AzureIoTClient 23:1111ee8bcba4 113 if (descriptor == NULL)
AzureIoTClient 23:1111ee8bcba4 114 {
AzureIoTClient 23:1111ee8bcba4 115 LogError("Error getting descriptor for delivery state");
AzureIoTClient 23:1111ee8bcba4 116 }
AzureIoTClient 23:1111ee8bcba4 117 else if (is_accepted_type_by_descriptor(descriptor))
AzureIoTClient 23:1111ee8bcba4 118 {
AzureIoTClient 23:1111ee8bcba4 119 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_OK);
AzureIoTClient 23:1111ee8bcba4 120 }
AzureIoTClient 23:1111ee8bcba4 121 else
AzureIoTClient 23:1111ee8bcba4 122 {
AzureIoTClient 23:1111ee8bcba4 123 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR);
AzureIoTClient 23:1111ee8bcba4 124 }
AzureIoTClient 7:9e9ab3b0efef 125 }
AzureIoTClient 7:9e9ab3b0efef 126
AzureIoTClient 23:1111ee8bcba4 127 break;
AzureIoTClient 23:1111ee8bcba4 128 case LINK_DELIVERY_SETTLE_REASON_SETTLED:
AzureIoTClient 23:1111ee8bcba4 129 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_OK);
AzureIoTClient 23:1111ee8bcba4 130 break;
AzureIoTClient 23:1111ee8bcba4 131 case LINK_DELIVERY_SETTLE_REASON_NOT_DELIVERED:
AzureIoTClient 23:1111ee8bcba4 132 default:
AzureIoTClient 23:1111ee8bcba4 133 message_with_callback->on_message_send_complete(message_with_callback->context, MESSAGE_SEND_ERROR);
AzureIoTClient 23:1111ee8bcba4 134 break;
AzureIoTClient 7:9e9ab3b0efef 135 }
AzureIoTClient 6:641a9672db08 136 }
Azure.IoT Build 0:6ae2f7bca550 137
AzureIoTClient 6:641a9672db08 138 remove_pending_message(message_sender_instance, message_with_callback);
Azure.IoT Build 0:6ae2f7bca550 139 }
Azure.IoT Build 0:6ae2f7bca550 140
Azure.IoT Build 0:6ae2f7bca550 141 static int encode_bytes(void* context, const unsigned char* bytes, size_t length)
Azure.IoT Build 0:6ae2f7bca550 142 {
AzureIoTClient 6:641a9672db08 143 PAYLOAD* payload = (PAYLOAD*)context;
AzureIoTClient 6:641a9672db08 144 (void)memcpy((unsigned char*)payload->bytes + payload->length, bytes, length);
AzureIoTClient 6:641a9672db08 145 payload->length += length;
AzureIoTClient 6:641a9672db08 146 return 0;
Azure.IoT Build 0:6ae2f7bca550 147 }
Azure.IoT Build 0:6ae2f7bca550 148
Azure.IoT Build 0:6ae2f7bca550 149 static void log_message_chunk(MESSAGE_SENDER_INSTANCE* message_sender_instance, const char* name, AMQP_VALUE value)
Azure.IoT Build 0:6ae2f7bca550 150 {
AzureIoTClient 9:c22db038556c 151 #ifdef NO_LOGGING
AzureIoTClient 9:c22db038556c 152 UNUSED(message_sender_instance);
AzureIoTClient 9:c22db038556c 153 UNUSED(name);
AzureIoTClient 9:c22db038556c 154 UNUSED(value);
AzureIoTClient 9:c22db038556c 155 #else
AzureIoTClient 6:641a9672db08 156 if (xlogging_get_log_function() != NULL && message_sender_instance->is_trace_on == 1)
AzureIoTClient 6:641a9672db08 157 {
AzureIoTClient 6:641a9672db08 158 char* value_as_string = NULL;
AzureIoTClient 16:22a72cf8e416 159 LOG(AZ_LOG_TRACE, 0, "%s", name);
AzureIoTClient 16:22a72cf8e416 160 LOG(AZ_LOG_TRACE, 0, "%s", (value_as_string = amqpvalue_to_string(value)));
AzureIoTClient 6:641a9672db08 161 if (value_as_string != NULL)
AzureIoTClient 6:641a9672db08 162 {
AzureIoTClient 21:f9c433d8e6ca 163 free(value_as_string);
AzureIoTClient 6:641a9672db08 164 }
AzureIoTClient 6:641a9672db08 165 }
AzureIoTClient 9:c22db038556c 166 #endif
Azure.IoT Build 0:6ae2f7bca550 167 }
Azure.IoT Build 0:6ae2f7bca550 168
Azure.IoT Build 0:6ae2f7bca550 169 static SEND_ONE_MESSAGE_RESULT send_one_message(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_WITH_CALLBACK* message_with_callback, MESSAGE_HANDLE message)
Azure.IoT Build 0:6ae2f7bca550 170 {
AzureIoTClient 6:641a9672db08 171 SEND_ONE_MESSAGE_RESULT result;
Azure.IoT Build 0:6ae2f7bca550 172
AzureIoTClient 6:641a9672db08 173 size_t encoded_size;
AzureIoTClient 6:641a9672db08 174 size_t total_encoded_size = 0;
AzureIoTClient 6:641a9672db08 175 MESSAGE_BODY_TYPE message_body_type;
Azure.IoT Build 0:6ae2f7bca550 176 message_format message_format;
Azure.IoT Build 0:6ae2f7bca550 177
AzureIoTClient 6:641a9672db08 178 if ((message_get_body_type(message, &message_body_type) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 179 (message_get_message_format(message, &message_format) != 0))
AzureIoTClient 6:641a9672db08 180 {
AzureIoTClient 6:641a9672db08 181 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 182 }
AzureIoTClient 6:641a9672db08 183 else
AzureIoTClient 6:641a9672db08 184 {
AzureIoTClient 6:641a9672db08 185 // header
AzureIoTClient 6:641a9672db08 186 HEADER_HANDLE header;
AzureIoTClient 6:641a9672db08 187 AMQP_VALUE header_amqp_value;
AzureIoTClient 6:641a9672db08 188 PROPERTIES_HANDLE properties;
AzureIoTClient 6:641a9672db08 189 AMQP_VALUE properties_amqp_value;
AzureIoTClient 6:641a9672db08 190 AMQP_VALUE application_properties;
AzureIoTClient 6:641a9672db08 191 AMQP_VALUE application_properties_value;
AzureIoTClient 6:641a9672db08 192 AMQP_VALUE body_amqp_value = NULL;
AzureIoTClient 6:641a9672db08 193 size_t body_data_count = 0;
AzureIoTClient 13:9abd748f4e78 194 AMQP_VALUE msg_annotations = NULL;
Azure.IoT Build 0:6ae2f7bca550 195
AzureIoTClient 6:641a9672db08 196 message_get_header(message, &header);
AzureIoTClient 6:641a9672db08 197 header_amqp_value = amqpvalue_create_header(header);
AzureIoTClient 6:641a9672db08 198 if (header != NULL)
AzureIoTClient 6:641a9672db08 199 {
AzureIoTClient 6:641a9672db08 200 amqpvalue_get_encoded_size(header_amqp_value, &encoded_size);
AzureIoTClient 6:641a9672db08 201 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 202 }
Azure.IoT Build 0:6ae2f7bca550 203
AzureIoTClient 13:9abd748f4e78 204 // message annotations
AzureIoTClient 13:9abd748f4e78 205 if ((message_get_message_annotations(message, &msg_annotations) == 0) &&
AzureIoTClient 13:9abd748f4e78 206 (msg_annotations != NULL))
AzureIoTClient 13:9abd748f4e78 207 {
AzureIoTClient 13:9abd748f4e78 208 amqpvalue_get_encoded_size(msg_annotations, &encoded_size);
AzureIoTClient 13:9abd748f4e78 209 total_encoded_size += encoded_size;
AzureIoTClient 13:9abd748f4e78 210 }
AzureIoTClient 13:9abd748f4e78 211
AzureIoTClient 6:641a9672db08 212 // properties
AzureIoTClient 6:641a9672db08 213 message_get_properties(message, &properties);
AzureIoTClient 6:641a9672db08 214 properties_amqp_value = amqpvalue_create_properties(properties);
AzureIoTClient 6:641a9672db08 215 if (properties != NULL)
AzureIoTClient 6:641a9672db08 216 {
AzureIoTClient 6:641a9672db08 217 amqpvalue_get_encoded_size(properties_amqp_value, &encoded_size);
AzureIoTClient 6:641a9672db08 218 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 219 }
Azure.IoT Build 0:6ae2f7bca550 220
AzureIoTClient 6:641a9672db08 221 // application properties
AzureIoTClient 6:641a9672db08 222 message_get_application_properties(message, &application_properties);
AzureIoTClient 6:641a9672db08 223 if (application_properties != NULL)
AzureIoTClient 6:641a9672db08 224 {
AzureIoTClient 23:1111ee8bcba4 225 application_properties_value = amqpvalue_create_application_properties(application_properties);
AzureIoTClient 6:641a9672db08 226 amqpvalue_get_encoded_size(application_properties_value, &encoded_size);
AzureIoTClient 6:641a9672db08 227 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 228 }
AzureIoTClient 23:1111ee8bcba4 229 else
AzureIoTClient 23:1111ee8bcba4 230 {
AzureIoTClient 23:1111ee8bcba4 231 application_properties_value = NULL;
AzureIoTClient 23:1111ee8bcba4 232 }
Azure.IoT Build 0:6ae2f7bca550 233
AzureIoTClient 6:641a9672db08 234 result = SEND_ONE_MESSAGE_OK;
Azure.IoT Build 0:6ae2f7bca550 235
AzureIoTClient 6:641a9672db08 236 // body - amqp data
AzureIoTClient 6:641a9672db08 237 switch (message_body_type)
AzureIoTClient 6:641a9672db08 238 {
AzureIoTClient 6:641a9672db08 239 default:
AzureIoTClient 6:641a9672db08 240 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 241 break;
Azure.IoT Build 0:6ae2f7bca550 242
AzureIoTClient 6:641a9672db08 243 case MESSAGE_BODY_TYPE_VALUE:
AzureIoTClient 6:641a9672db08 244 {
AzureIoTClient 6:641a9672db08 245 AMQP_VALUE message_body_amqp_value;
AzureIoTClient 24:2c59c2d43ebf 246 if (message_get_body_amqp_value_in_place(message, &message_body_amqp_value) != 0)
AzureIoTClient 6:641a9672db08 247 {
AzureIoTClient 6:641a9672db08 248 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 249 }
AzureIoTClient 6:641a9672db08 250 else
AzureIoTClient 6:641a9672db08 251 {
AzureIoTClient 6:641a9672db08 252 body_amqp_value = amqpvalue_create_amqp_value(message_body_amqp_value);
AzureIoTClient 6:641a9672db08 253 if ((body_amqp_value == NULL) ||
AzureIoTClient 6:641a9672db08 254 (amqpvalue_get_encoded_size(body_amqp_value, &encoded_size) != 0))
AzureIoTClient 6:641a9672db08 255 {
AzureIoTClient 6:641a9672db08 256 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 257 }
AzureIoTClient 6:641a9672db08 258 else
AzureIoTClient 6:641a9672db08 259 {
AzureIoTClient 6:641a9672db08 260 total_encoded_size += encoded_size;
AzureIoTClient 6:641a9672db08 261 }
AzureIoTClient 6:641a9672db08 262 }
Azure.IoT Build 0:6ae2f7bca550 263
AzureIoTClient 6:641a9672db08 264 break;
AzureIoTClient 6:641a9672db08 265 }
Azure.IoT Build 0:6ae2f7bca550 266
AzureIoTClient 6:641a9672db08 267 case MESSAGE_BODY_TYPE_DATA:
AzureIoTClient 6:641a9672db08 268 {
AzureIoTClient 6:641a9672db08 269 BINARY_DATA binary_data;
Azure.IoT Build 0:6ae2f7bca550 270 size_t i;
Azure.IoT Build 0:6ae2f7bca550 271
Azure.IoT Build 0:6ae2f7bca550 272 if (message_get_body_amqp_data_count(message, &body_data_count) != 0)
Azure.IoT Build 0:6ae2f7bca550 273 {
Azure.IoT Build 0:6ae2f7bca550 274 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 275 }
Azure.IoT Build 0:6ae2f7bca550 276 else
Azure.IoT Build 0:6ae2f7bca550 277 {
Azure.IoT Build 0:6ae2f7bca550 278 for (i = 0; i < body_data_count; i++)
Azure.IoT Build 0:6ae2f7bca550 279 {
AzureIoTClient 24:2c59c2d43ebf 280 if (message_get_body_amqp_data_in_place(message, i, &binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 281 {
Azure.IoT Build 0:6ae2f7bca550 282 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 283 }
Azure.IoT Build 0:6ae2f7bca550 284 else
Azure.IoT Build 0:6ae2f7bca550 285 {
AzureIoTClient 6:641a9672db08 286 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 287 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 288 binary_value.length = (uint32_t)binary_data.length;
Azure.IoT Build 0:6ae2f7bca550 289 AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 290 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 291 {
Azure.IoT Build 0:6ae2f7bca550 292 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 293 }
Azure.IoT Build 0:6ae2f7bca550 294 else
Azure.IoT Build 0:6ae2f7bca550 295 {
Azure.IoT Build 0:6ae2f7bca550 296 if (amqpvalue_get_encoded_size(body_amqp_data, &encoded_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 297 {
Azure.IoT Build 0:6ae2f7bca550 298 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 299 }
Azure.IoT Build 0:6ae2f7bca550 300 else
Azure.IoT Build 0:6ae2f7bca550 301 {
Azure.IoT Build 0:6ae2f7bca550 302 total_encoded_size += encoded_size;
Azure.IoT Build 0:6ae2f7bca550 303 }
Azure.IoT Build 0:6ae2f7bca550 304
Azure.IoT Build 0:6ae2f7bca550 305 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 306 }
Azure.IoT Build 0:6ae2f7bca550 307 }
Azure.IoT Build 0:6ae2f7bca550 308 }
Azure.IoT Build 0:6ae2f7bca550 309 }
AzureIoTClient 6:641a9672db08 310 break;
AzureIoTClient 6:641a9672db08 311 }
AzureIoTClient 6:641a9672db08 312 }
Azure.IoT Build 0:6ae2f7bca550 313
AzureIoTClient 6:641a9672db08 314 if (result == 0)
AzureIoTClient 6:641a9672db08 315 {
AzureIoTClient 21:f9c433d8e6ca 316 void* data_bytes = malloc(total_encoded_size);
AzureIoTClient 6:641a9672db08 317 PAYLOAD payload;
AzureIoTClient 23:1111ee8bcba4 318 payload.bytes = (const unsigned char*)data_bytes;
AzureIoTClient 6:641a9672db08 319 payload.length = 0;
AzureIoTClient 6:641a9672db08 320 result = SEND_ONE_MESSAGE_OK;
Azure.IoT Build 0:6ae2f7bca550 321
AzureIoTClient 6:641a9672db08 322 if (header != NULL)
AzureIoTClient 6:641a9672db08 323 {
AzureIoTClient 6:641a9672db08 324 if (amqpvalue_encode(header_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 325 {
AzureIoTClient 6:641a9672db08 326 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 327 }
Azure.IoT Build 0:6ae2f7bca550 328
AzureIoTClient 6:641a9672db08 329 log_message_chunk(message_sender_instance, "Header:", header_amqp_value);
AzureIoTClient 6:641a9672db08 330 }
Azure.IoT Build 0:6ae2f7bca550 331
AzureIoTClient 13:9abd748f4e78 332 if ((result == SEND_ONE_MESSAGE_OK) && (msg_annotations != NULL))
AzureIoTClient 13:9abd748f4e78 333 {
AzureIoTClient 13:9abd748f4e78 334 if (amqpvalue_encode(msg_annotations, encode_bytes, &payload) != 0)
AzureIoTClient 13:9abd748f4e78 335 {
AzureIoTClient 13:9abd748f4e78 336 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 13:9abd748f4e78 337 }
AzureIoTClient 13:9abd748f4e78 338
AzureIoTClient 13:9abd748f4e78 339 log_message_chunk(message_sender_instance, "Message Annotations:", msg_annotations);
AzureIoTClient 13:9abd748f4e78 340 }
AzureIoTClient 13:9abd748f4e78 341
AzureIoTClient 6:641a9672db08 342 if ((result == SEND_ONE_MESSAGE_OK) && (properties != NULL))
AzureIoTClient 6:641a9672db08 343 {
AzureIoTClient 6:641a9672db08 344 if (amqpvalue_encode(properties_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 345 {
AzureIoTClient 6:641a9672db08 346 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 347 }
Azure.IoT Build 0:6ae2f7bca550 348
AzureIoTClient 6:641a9672db08 349 log_message_chunk(message_sender_instance, "Properties:", properties_amqp_value);
AzureIoTClient 6:641a9672db08 350 }
Azure.IoT Build 0:6ae2f7bca550 351
AzureIoTClient 6:641a9672db08 352 if ((result == SEND_ONE_MESSAGE_OK) && (application_properties != NULL))
AzureIoTClient 6:641a9672db08 353 {
AzureIoTClient 6:641a9672db08 354 if (amqpvalue_encode(application_properties_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 355 {
AzureIoTClient 6:641a9672db08 356 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 357 }
Azure.IoT Build 0:6ae2f7bca550 358
AzureIoTClient 6:641a9672db08 359 log_message_chunk(message_sender_instance, "Application properties:", application_properties_value);
AzureIoTClient 6:641a9672db08 360 }
Azure.IoT Build 0:6ae2f7bca550 361
AzureIoTClient 6:641a9672db08 362 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 363 {
AzureIoTClient 6:641a9672db08 364 switch (message_body_type)
AzureIoTClient 6:641a9672db08 365 {
AzureIoTClient 17:923575db8b2d 366 default:
AzureIoTClient 17:923575db8b2d 367 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 17:923575db8b2d 368 break;
AzureIoTClient 17:923575db8b2d 369
AzureIoTClient 6:641a9672db08 370 case MESSAGE_BODY_TYPE_VALUE:
AzureIoTClient 6:641a9672db08 371 {
AzureIoTClient 6:641a9672db08 372 if (amqpvalue_encode(body_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 373 {
AzureIoTClient 6:641a9672db08 374 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 375 }
Azure.IoT Build 0:6ae2f7bca550 376
AzureIoTClient 6:641a9672db08 377 log_message_chunk(message_sender_instance, "Body - amqp value:", body_amqp_value);
AzureIoTClient 6:641a9672db08 378 break;
AzureIoTClient 6:641a9672db08 379 }
AzureIoTClient 6:641a9672db08 380 case MESSAGE_BODY_TYPE_DATA:
AzureIoTClient 6:641a9672db08 381 {
Azure.IoT Build 0:6ae2f7bca550 382 BINARY_DATA binary_data;
Azure.IoT Build 0:6ae2f7bca550 383 size_t i;
Azure.IoT Build 0:6ae2f7bca550 384
Azure.IoT Build 0:6ae2f7bca550 385 for (i = 0; i < body_data_count; i++)
Azure.IoT Build 0:6ae2f7bca550 386 {
AzureIoTClient 24:2c59c2d43ebf 387 if (message_get_body_amqp_data_in_place(message, i, &binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 388 {
Azure.IoT Build 0:6ae2f7bca550 389 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 390 }
Azure.IoT Build 0:6ae2f7bca550 391 else
Azure.IoT Build 0:6ae2f7bca550 392 {
AzureIoTClient 6:641a9672db08 393 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 394 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 395 binary_value.length = (uint32_t)binary_data.length;
Azure.IoT Build 0:6ae2f7bca550 396 AMQP_VALUE body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 397 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 398 {
Azure.IoT Build 0:6ae2f7bca550 399 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 400 }
Azure.IoT Build 0:6ae2f7bca550 401 else
Azure.IoT Build 0:6ae2f7bca550 402 {
Azure.IoT Build 0:6ae2f7bca550 403 if (amqpvalue_encode(body_amqp_data, encode_bytes, &payload) != 0)
Azure.IoT Build 0:6ae2f7bca550 404 {
Azure.IoT Build 0:6ae2f7bca550 405 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 406 break;
Azure.IoT Build 0:6ae2f7bca550 407 }
Azure.IoT Build 0:6ae2f7bca550 408
Azure.IoT Build 0:6ae2f7bca550 409 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 410 }
Azure.IoT Build 0:6ae2f7bca550 411 }
Azure.IoT Build 0:6ae2f7bca550 412 }
AzureIoTClient 6:641a9672db08 413 break;
AzureIoTClient 6:641a9672db08 414 }
AzureIoTClient 6:641a9672db08 415 }
AzureIoTClient 6:641a9672db08 416 }
Azure.IoT Build 0:6ae2f7bca550 417
AzureIoTClient 6:641a9672db08 418 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 419 {
AzureIoTClient 6:641a9672db08 420 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 421 switch (link_transfer(message_sender_instance->link, message_format, &payload, 1, on_delivery_settled, message_with_callback))
AzureIoTClient 6:641a9672db08 422 {
AzureIoTClient 6:641a9672db08 423 default:
AzureIoTClient 6:641a9672db08 424 case LINK_TRANSFER_ERROR:
AzureIoTClient 6:641a9672db08 425 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 426 break;
Azure.IoT Build 0:6ae2f7bca550 427
AzureIoTClient 6:641a9672db08 428 case LINK_TRANSFER_BUSY:
AzureIoTClient 6:641a9672db08 429 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 430 result = SEND_ONE_MESSAGE_BUSY;
AzureIoTClient 6:641a9672db08 431 break;
Azure.IoT Build 0:6ae2f7bca550 432
AzureIoTClient 6:641a9672db08 433 case LINK_TRANSFER_OK:
AzureIoTClient 6:641a9672db08 434 result = SEND_ONE_MESSAGE_OK;
AzureIoTClient 6:641a9672db08 435 break;
AzureIoTClient 6:641a9672db08 436 }
AzureIoTClient 6:641a9672db08 437 }
Azure.IoT Build 0:6ae2f7bca550 438
AzureIoTClient 21:f9c433d8e6ca 439 free(data_bytes);
Azure.IoT Build 0:6ae2f7bca550 440
AzureIoTClient 6:641a9672db08 441 if (body_amqp_value != NULL)
AzureIoTClient 6:641a9672db08 442 {
AzureIoTClient 6:641a9672db08 443 amqpvalue_destroy(body_amqp_value);
AzureIoTClient 6:641a9672db08 444 }
AzureIoTClient 20:206846c14c80 445 }
Azure.IoT Build 0:6ae2f7bca550 446
AzureIoTClient 23:1111ee8bcba4 447 if (application_properties != NULL)
AzureIoTClient 23:1111ee8bcba4 448 {
AzureIoTClient 23:1111ee8bcba4 449 amqpvalue_destroy(application_properties);
AzureIoTClient 23:1111ee8bcba4 450 }
AzureIoTClient 23:1111ee8bcba4 451 if (application_properties_value != NULL)
AzureIoTClient 23:1111ee8bcba4 452 {
AzureIoTClient 23:1111ee8bcba4 453 amqpvalue_destroy(application_properties_value);
AzureIoTClient 23:1111ee8bcba4 454 }
AzureIoTClient 23:1111ee8bcba4 455 if (properties_amqp_value != NULL)
AzureIoTClient 23:1111ee8bcba4 456 {
AzureIoTClient 23:1111ee8bcba4 457 amqpvalue_destroy(properties_amqp_value);
AzureIoTClient 23:1111ee8bcba4 458 }
AzureIoTClient 23:1111ee8bcba4 459 if (properties != NULL)
AzureIoTClient 23:1111ee8bcba4 460 {
AzureIoTClient 23:1111ee8bcba4 461 properties_destroy(properties);
AzureIoTClient 23:1111ee8bcba4 462 }
AzureIoTClient 6:641a9672db08 463 }
Azure.IoT Build 0:6ae2f7bca550 464
AzureIoTClient 6:641a9672db08 465 return result;
Azure.IoT Build 0:6ae2f7bca550 466 }
Azure.IoT Build 0:6ae2f7bca550 467
Azure.IoT Build 0:6ae2f7bca550 468 static void send_all_pending_messages(MESSAGE_SENDER_INSTANCE* message_sender_instance)
Azure.IoT Build 0:6ae2f7bca550 469 {
AzureIoTClient 6:641a9672db08 470 size_t i;
Azure.IoT Build 0:6ae2f7bca550 471
AzureIoTClient 6:641a9672db08 472 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 6:641a9672db08 473 {
AzureIoTClient 6:641a9672db08 474 if (message_sender_instance->messages[i]->message_send_state == MESSAGE_SEND_STATE_NOT_SENT)
AzureIoTClient 6:641a9672db08 475 {
AzureIoTClient 6:641a9672db08 476 switch (send_one_message(message_sender_instance, message_sender_instance->messages[i], message_sender_instance->messages[i]->message))
AzureIoTClient 6:641a9672db08 477 {
AzureIoTClient 6:641a9672db08 478 default:
AzureIoTClient 6:641a9672db08 479 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 6:641a9672db08 480 {
AzureIoTClient 6:641a9672db08 481 ON_MESSAGE_SEND_COMPLETE on_message_send_complete = message_sender_instance->messages[i]->on_message_send_complete;
AzureIoTClient 6:641a9672db08 482 void* context = message_sender_instance->messages[i]->context;
AzureIoTClient 6:641a9672db08 483 remove_pending_message_by_index(message_sender_instance, i);
Azure.IoT Build 0:6ae2f7bca550 484
AzureIoTClient 23:1111ee8bcba4 485 if (on_message_send_complete != NULL)
AzureIoTClient 23:1111ee8bcba4 486 {
AzureIoTClient 23:1111ee8bcba4 487 on_message_send_complete(context, MESSAGE_SEND_ERROR);
AzureIoTClient 23:1111ee8bcba4 488 }
AzureIoTClient 18:a922a4a30a82 489
AzureIoTClient 6:641a9672db08 490 i = message_sender_instance->message_count;
AzureIoTClient 6:641a9672db08 491 break;
AzureIoTClient 6:641a9672db08 492 }
AzureIoTClient 6:641a9672db08 493 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 494 i = message_sender_instance->message_count + 1;
AzureIoTClient 6:641a9672db08 495 break;
Azure.IoT Build 0:6ae2f7bca550 496
AzureIoTClient 6:641a9672db08 497 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 498 break;
AzureIoTClient 6:641a9672db08 499 }
Azure.IoT Build 0:6ae2f7bca550 500
AzureIoTClient 6:641a9672db08 501 i--;
AzureIoTClient 6:641a9672db08 502 }
AzureIoTClient 6:641a9672db08 503 }
Azure.IoT Build 0:6ae2f7bca550 504 }
Azure.IoT Build 0:6ae2f7bca550 505
Azure.IoT Build 0:6ae2f7bca550 506 static void set_message_sender_state(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_SENDER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 507 {
AzureIoTClient 6:641a9672db08 508 MESSAGE_SENDER_STATE previous_state = message_sender_instance->message_sender_state;
AzureIoTClient 6:641a9672db08 509 message_sender_instance->message_sender_state = new_state;
AzureIoTClient 6:641a9672db08 510 if (message_sender_instance->on_message_sender_state_changed != NULL)
AzureIoTClient 6:641a9672db08 511 {
AzureIoTClient 6:641a9672db08 512 message_sender_instance->on_message_sender_state_changed(message_sender_instance->on_message_sender_state_changed_context, new_state, previous_state);
AzureIoTClient 6:641a9672db08 513 }
Azure.IoT Build 0:6ae2f7bca550 514 }
Azure.IoT Build 0:6ae2f7bca550 515
AzureIoTClient 2:64b4feb67cd3 516 static void indicate_all_messages_as_error(MESSAGE_SENDER_INSTANCE* message_sender_instance)
AzureIoTClient 2:64b4feb67cd3 517 {
AzureIoTClient 2:64b4feb67cd3 518 size_t i;
AzureIoTClient 2:64b4feb67cd3 519
AzureIoTClient 2:64b4feb67cd3 520 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 2:64b4feb67cd3 521 {
AzureIoTClient 2:64b4feb67cd3 522 if (message_sender_instance->messages[i]->on_message_send_complete != NULL)
AzureIoTClient 2:64b4feb67cd3 523 {
AzureIoTClient 2:64b4feb67cd3 524 message_sender_instance->messages[i]->on_message_send_complete(message_sender_instance->messages[i]->context, MESSAGE_SEND_ERROR);
AzureIoTClient 2:64b4feb67cd3 525 }
AzureIoTClient 2:64b4feb67cd3 526
AzureIoTClient 2:64b4feb67cd3 527 message_destroy(message_sender_instance->messages[i]->message);
AzureIoTClient 21:f9c433d8e6ca 528 free(message_sender_instance->messages[i]);
AzureIoTClient 2:64b4feb67cd3 529 }
AzureIoTClient 2:64b4feb67cd3 530
AzureIoTClient 2:64b4feb67cd3 531 if (message_sender_instance->messages != NULL)
AzureIoTClient 2:64b4feb67cd3 532 {
AzureIoTClient 2:64b4feb67cd3 533 message_sender_instance->message_count = 0;
AzureIoTClient 2:64b4feb67cd3 534
AzureIoTClient 21:f9c433d8e6ca 535 free(message_sender_instance->messages);
AzureIoTClient 2:64b4feb67cd3 536 message_sender_instance->messages = NULL;
AzureIoTClient 2:64b4feb67cd3 537 }
AzureIoTClient 2:64b4feb67cd3 538 }
AzureIoTClient 2:64b4feb67cd3 539
Azure.IoT Build 0:6ae2f7bca550 540 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 541 {
AzureIoTClient 6:641a9672db08 542 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 543 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 544
AzureIoTClient 6:641a9672db08 545 switch (new_link_state)
AzureIoTClient 6:641a9672db08 546 {
AzureIoTClient 17:923575db8b2d 547 default:
AzureIoTClient 17:923575db8b2d 548 break;
AzureIoTClient 17:923575db8b2d 549
AzureIoTClient 6:641a9672db08 550 case LINK_STATE_ATTACHED:
AzureIoTClient 6:641a9672db08 551 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING)
AzureIoTClient 6:641a9672db08 552 {
AzureIoTClient 6:641a9672db08 553 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPEN);
AzureIoTClient 6:641a9672db08 554 }
AzureIoTClient 6:641a9672db08 555 break;
AzureIoTClient 6:641a9672db08 556 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 557 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 558 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 559 {
Azure.IoT Build 0:6ae2f7bca550 560 /* User initiated transition, we should be good */
AzureIoTClient 23:1111ee8bcba4 561 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_IDLE);
AzureIoTClient 2:64b4feb67cd3 562 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 563 }
Azure.IoT Build 0:6ae2f7bca550 564 else if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 565 {
Azure.IoT Build 0:6ae2f7bca550 566 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 567 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 568 }
Azure.IoT Build 0:6ae2f7bca550 569 break;
AzureIoTClient 6:641a9672db08 570 case LINK_STATE_ERROR:
AzureIoTClient 6:641a9672db08 571 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 572 {
AzureIoTClient 23:1111ee8bcba4 573 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 2:64b4feb67cd3 574 indicate_all_messages_as_error(message_sender_instance);
AzureIoTClient 6:641a9672db08 575 }
AzureIoTClient 6:641a9672db08 576 break;
AzureIoTClient 6:641a9672db08 577 }
Azure.IoT Build 0:6ae2f7bca550 578 }
Azure.IoT Build 0:6ae2f7bca550 579
Azure.IoT Build 0:6ae2f7bca550 580 static void on_link_flow_on(void* context)
Azure.IoT Build 0:6ae2f7bca550 581 {
AzureIoTClient 6:641a9672db08 582 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 583 send_all_pending_messages(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 584 }
Azure.IoT Build 0:6ae2f7bca550 585
Azure.IoT Build 5:ae49385aff34 586 MESSAGE_SENDER_HANDLE messagesender_create(LINK_HANDLE link, ON_MESSAGE_SENDER_STATE_CHANGED on_message_sender_state_changed, void* context)
Azure.IoT Build 0:6ae2f7bca550 587 {
AzureIoTClient 23:1111ee8bcba4 588 MESSAGE_SENDER_INSTANCE* result = (MESSAGE_SENDER_INSTANCE*)malloc(sizeof(MESSAGE_SENDER_INSTANCE));
AzureIoTClient 6:641a9672db08 589 if (result != NULL)
AzureIoTClient 6:641a9672db08 590 {
AzureIoTClient 6:641a9672db08 591 result->messages = NULL;
AzureIoTClient 6:641a9672db08 592 result->message_count = 0;
AzureIoTClient 6:641a9672db08 593 result->link = link;
AzureIoTClient 6:641a9672db08 594 result->on_message_sender_state_changed = on_message_sender_state_changed;
AzureIoTClient 6:641a9672db08 595 result->on_message_sender_state_changed_context = context;
AzureIoTClient 6:641a9672db08 596 result->message_sender_state = MESSAGE_SENDER_STATE_IDLE;
AzureIoTClient 6:641a9672db08 597 result->is_trace_on = 0;
AzureIoTClient 6:641a9672db08 598 }
Azure.IoT Build 0:6ae2f7bca550 599
AzureIoTClient 6:641a9672db08 600 return result;
Azure.IoT Build 0:6ae2f7bca550 601 }
Azure.IoT Build 0:6ae2f7bca550 602
Azure.IoT Build 0:6ae2f7bca550 603 void messagesender_destroy(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 604 {
AzureIoTClient 6:641a9672db08 605 if (message_sender != NULL)
AzureIoTClient 6:641a9672db08 606 {
AzureIoTClient 6:641a9672db08 607 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 608
AzureIoTClient 6:641a9672db08 609 messagesender_close(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 610
AzureIoTClient 2:64b4feb67cd3 611 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 612
AzureIoTClient 21:f9c433d8e6ca 613 free(message_sender);
AzureIoTClient 6:641a9672db08 614 }
Azure.IoT Build 0:6ae2f7bca550 615 }
Azure.IoT Build 0:6ae2f7bca550 616
Azure.IoT Build 0:6ae2f7bca550 617 int messagesender_open(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 618 {
AzureIoTClient 6:641a9672db08 619 int result;
Azure.IoT Build 0:6ae2f7bca550 620
AzureIoTClient 6:641a9672db08 621 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 622 {
AzureIoTClient 19:000ab4e6a2c1 623 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 624 }
AzureIoTClient 6:641a9672db08 625 else
AzureIoTClient 6:641a9672db08 626 {
AzureIoTClient 6:641a9672db08 627 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 628
AzureIoTClient 6:641a9672db08 629 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_IDLE)
AzureIoTClient 6:641a9672db08 630 {
AzureIoTClient 6:641a9672db08 631 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPENING);
AzureIoTClient 6:641a9672db08 632 if (link_attach(message_sender_instance->link, NULL, on_link_state_changed, on_link_flow_on, message_sender_instance) != 0)
AzureIoTClient 6:641a9672db08 633 {
AzureIoTClient 19:000ab4e6a2c1 634 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 635 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 636 }
AzureIoTClient 6:641a9672db08 637 else
AzureIoTClient 6:641a9672db08 638 {
AzureIoTClient 6:641a9672db08 639 result = 0;
AzureIoTClient 6:641a9672db08 640 }
AzureIoTClient 6:641a9672db08 641 }
AzureIoTClient 6:641a9672db08 642 else
AzureIoTClient 6:641a9672db08 643 {
AzureIoTClient 6:641a9672db08 644 result = 0;
AzureIoTClient 6:641a9672db08 645 }
AzureIoTClient 6:641a9672db08 646 }
Azure.IoT Build 0:6ae2f7bca550 647
AzureIoTClient 6:641a9672db08 648 return result;
Azure.IoT Build 0:6ae2f7bca550 649 }
Azure.IoT Build 0:6ae2f7bca550 650
Azure.IoT Build 0:6ae2f7bca550 651 int messagesender_close(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 652 {
AzureIoTClient 6:641a9672db08 653 int result;
Azure.IoT Build 0:6ae2f7bca550 654
AzureIoTClient 6:641a9672db08 655 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 656 {
AzureIoTClient 19:000ab4e6a2c1 657 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 658 }
AzureIoTClient 6:641a9672db08 659 else
AzureIoTClient 6:641a9672db08 660 {
AzureIoTClient 6:641a9672db08 661 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 662
AzureIoTClient 6:641a9672db08 663 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING) ||
AzureIoTClient 6:641a9672db08 664 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN))
AzureIoTClient 6:641a9672db08 665 {
AzureIoTClient 6:641a9672db08 666 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_CLOSING);
AzureIoTClient 12:b30dacf113f2 667 if (link_detach(message_sender_instance->link, true) != 0)
AzureIoTClient 6:641a9672db08 668 {
AzureIoTClient 19:000ab4e6a2c1 669 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 670 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 671 }
AzureIoTClient 6:641a9672db08 672 else
AzureIoTClient 6:641a9672db08 673 {
AzureIoTClient 6:641a9672db08 674 result = 0;
AzureIoTClient 6:641a9672db08 675 }
AzureIoTClient 6:641a9672db08 676 }
AzureIoTClient 6:641a9672db08 677 else
AzureIoTClient 6:641a9672db08 678 {
AzureIoTClient 6:641a9672db08 679 result = 0;
AzureIoTClient 6:641a9672db08 680 }
AzureIoTClient 6:641a9672db08 681 }
Azure.IoT Build 0:6ae2f7bca550 682
AzureIoTClient 6:641a9672db08 683 return result;
Azure.IoT Build 0:6ae2f7bca550 684 }
Azure.IoT Build 0:6ae2f7bca550 685
Azure.IoT Build 0:6ae2f7bca550 686 int messagesender_send(MESSAGE_SENDER_HANDLE message_sender, MESSAGE_HANDLE message, ON_MESSAGE_SEND_COMPLETE on_message_send_complete, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 687 {
AzureIoTClient 6:641a9672db08 688 int result;
Azure.IoT Build 0:6ae2f7bca550 689
AzureIoTClient 6:641a9672db08 690 if ((message_sender == NULL) ||
AzureIoTClient 6:641a9672db08 691 (message == NULL))
AzureIoTClient 6:641a9672db08 692 {
AzureIoTClient 19:000ab4e6a2c1 693 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 694 }
AzureIoTClient 6:641a9672db08 695 else
AzureIoTClient 6:641a9672db08 696 {
AzureIoTClient 6:641a9672db08 697 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 698 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 699 {
AzureIoTClient 19:000ab4e6a2c1 700 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 701 }
AzureIoTClient 6:641a9672db08 702 else
AzureIoTClient 6:641a9672db08 703 {
AzureIoTClient 21:f9c433d8e6ca 704 MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)malloc(sizeof(MESSAGE_WITH_CALLBACK));
AzureIoTClient 6:641a9672db08 705 if (message_with_callback == NULL)
AzureIoTClient 6:641a9672db08 706 {
AzureIoTClient 19:000ab4e6a2c1 707 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 708 }
AzureIoTClient 6:641a9672db08 709 else
AzureIoTClient 6:641a9672db08 710 {
AzureIoTClient 21:f9c433d8e6ca 711 MESSAGE_WITH_CALLBACK** new_messages = (MESSAGE_WITH_CALLBACK**)realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count + 1));
AzureIoTClient 6:641a9672db08 712 if (new_messages == NULL)
AzureIoTClient 6:641a9672db08 713 {
AzureIoTClient 21:f9c433d8e6ca 714 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 715 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 716 }
AzureIoTClient 6:641a9672db08 717 else
AzureIoTClient 6:641a9672db08 718 {
AzureIoTClient 6:641a9672db08 719 result = 0;
Azure.IoT Build 0:6ae2f7bca550 720
AzureIoTClient 6:641a9672db08 721 message_sender_instance->messages = new_messages;
AzureIoTClient 6:641a9672db08 722 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 723 {
AzureIoTClient 6:641a9672db08 724 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 725 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 726 {
AzureIoTClient 21:f9c433d8e6ca 727 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 728 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 729 }
AzureIoTClient 6:641a9672db08 730
AzureIoTClient 6:641a9672db08 731 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 732 }
AzureIoTClient 6:641a9672db08 733 else
AzureIoTClient 6:641a9672db08 734 {
AzureIoTClient 6:641a9672db08 735 message_with_callback->message = NULL;
AzureIoTClient 6:641a9672db08 736 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 737 }
Azure.IoT Build 0:6ae2f7bca550 738
AzureIoTClient 6:641a9672db08 739 if (result == 0)
AzureIoTClient 6:641a9672db08 740 {
AzureIoTClient 6:641a9672db08 741 message_with_callback->on_message_send_complete = on_message_send_complete;
AzureIoTClient 6:641a9672db08 742 message_with_callback->context = callback_context;
AzureIoTClient 6:641a9672db08 743 message_with_callback->message_sender = message_sender_instance;
AzureIoTClient 6:641a9672db08 744
AzureIoTClient 6:641a9672db08 745 message_sender_instance->messages[message_sender_instance->message_count] = message_with_callback;
AzureIoTClient 6:641a9672db08 746 message_sender_instance->message_count++;
Azure.IoT Build 0:6ae2f7bca550 747
AzureIoTClient 6:641a9672db08 748 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 749 {
AzureIoTClient 6:641a9672db08 750 switch (send_one_message(message_sender_instance, message_with_callback, message))
AzureIoTClient 6:641a9672db08 751 {
AzureIoTClient 6:641a9672db08 752 default:
AzureIoTClient 6:641a9672db08 753 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 23:1111ee8bcba4 754
AzureIoTClient 6:641a9672db08 755 remove_pending_message_by_index(message_sender_instance, message_sender_instance->message_count - 1);
AzureIoTClient 19:000ab4e6a2c1 756 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 757 break;
Azure.IoT Build 0:6ae2f7bca550 758
AzureIoTClient 6:641a9672db08 759 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 760 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 761 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 762 {
AzureIoTClient 21:f9c433d8e6ca 763 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 764 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 765 }
AzureIoTClient 6:641a9672db08 766 else
AzureIoTClient 6:641a9672db08 767 {
AzureIoTClient 6:641a9672db08 768 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 769 result = 0;
AzureIoTClient 6:641a9672db08 770 }
AzureIoTClient 6:641a9672db08 771 break;
Azure.IoT Build 0:6ae2f7bca550 772
AzureIoTClient 6:641a9672db08 773 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 774 result = 0;
AzureIoTClient 6:641a9672db08 775 break;
AzureIoTClient 6:641a9672db08 776 }
AzureIoTClient 6:641a9672db08 777 }
AzureIoTClient 6:641a9672db08 778 }
AzureIoTClient 6:641a9672db08 779 }
AzureIoTClient 6:641a9672db08 780 }
AzureIoTClient 6:641a9672db08 781 }
AzureIoTClient 6:641a9672db08 782 }
AzureIoTClient 6:641a9672db08 783 return result;
AzureIoTClient 6:641a9672db08 784 }
Azure.IoT Build 0:6ae2f7bca550 785
AzureIoTClient 6:641a9672db08 786 void messagesender_set_trace(MESSAGE_SENDER_HANDLE message_sender, bool traceOn)
AzureIoTClient 6:641a9672db08 787 {
AzureIoTClient 6:641a9672db08 788 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 789 if (message_sender_instance != NULL)
AzureIoTClient 6:641a9672db08 790 {
AzureIoTClient 6:641a9672db08 791 message_sender_instance->is_trace_on = traceOn ? 1 : 0;
AzureIoTClient 6:641a9672db08 792 }
Azure.IoT Build 0:6ae2f7bca550 793 }