A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Jun 30 10:41:22 2017 -0700
Revision:
28:add19eb7defa
Parent:
25:1101516ee67d
Child:
29:4a11413cf217
1.1.18

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 28:add19eb7defa 286 AMQP_VALUE body_amqp_data;
AzureIoTClient 6:641a9672db08 287 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 288 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 289 binary_value.length = (uint32_t)binary_data.length;
AzureIoTClient 25:1101516ee67d 290 body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 291 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 292 {
Azure.IoT Build 0:6ae2f7bca550 293 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 294 }
Azure.IoT Build 0:6ae2f7bca550 295 else
Azure.IoT Build 0:6ae2f7bca550 296 {
Azure.IoT Build 0:6ae2f7bca550 297 if (amqpvalue_get_encoded_size(body_amqp_data, &encoded_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 298 {
Azure.IoT Build 0:6ae2f7bca550 299 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 300 }
Azure.IoT Build 0:6ae2f7bca550 301 else
Azure.IoT Build 0:6ae2f7bca550 302 {
Azure.IoT Build 0:6ae2f7bca550 303 total_encoded_size += encoded_size;
Azure.IoT Build 0:6ae2f7bca550 304 }
Azure.IoT Build 0:6ae2f7bca550 305
Azure.IoT Build 0:6ae2f7bca550 306 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 307 }
Azure.IoT Build 0:6ae2f7bca550 308 }
Azure.IoT Build 0:6ae2f7bca550 309 }
Azure.IoT Build 0:6ae2f7bca550 310 }
AzureIoTClient 6:641a9672db08 311 break;
AzureIoTClient 6:641a9672db08 312 }
AzureIoTClient 6:641a9672db08 313 }
Azure.IoT Build 0:6ae2f7bca550 314
AzureIoTClient 6:641a9672db08 315 if (result == 0)
AzureIoTClient 6:641a9672db08 316 {
AzureIoTClient 21:f9c433d8e6ca 317 void* data_bytes = malloc(total_encoded_size);
AzureIoTClient 6:641a9672db08 318 PAYLOAD payload;
AzureIoTClient 23:1111ee8bcba4 319 payload.bytes = (const unsigned char*)data_bytes;
AzureIoTClient 6:641a9672db08 320 payload.length = 0;
AzureIoTClient 6:641a9672db08 321 result = SEND_ONE_MESSAGE_OK;
Azure.IoT Build 0:6ae2f7bca550 322
AzureIoTClient 6:641a9672db08 323 if (header != NULL)
AzureIoTClient 6:641a9672db08 324 {
AzureIoTClient 6:641a9672db08 325 if (amqpvalue_encode(header_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 326 {
AzureIoTClient 6:641a9672db08 327 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 328 }
Azure.IoT Build 0:6ae2f7bca550 329
AzureIoTClient 6:641a9672db08 330 log_message_chunk(message_sender_instance, "Header:", header_amqp_value);
AzureIoTClient 6:641a9672db08 331 }
Azure.IoT Build 0:6ae2f7bca550 332
AzureIoTClient 13:9abd748f4e78 333 if ((result == SEND_ONE_MESSAGE_OK) && (msg_annotations != NULL))
AzureIoTClient 13:9abd748f4e78 334 {
AzureIoTClient 13:9abd748f4e78 335 if (amqpvalue_encode(msg_annotations, encode_bytes, &payload) != 0)
AzureIoTClient 13:9abd748f4e78 336 {
AzureIoTClient 13:9abd748f4e78 337 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 13:9abd748f4e78 338 }
AzureIoTClient 13:9abd748f4e78 339
AzureIoTClient 13:9abd748f4e78 340 log_message_chunk(message_sender_instance, "Message Annotations:", msg_annotations);
AzureIoTClient 13:9abd748f4e78 341 }
AzureIoTClient 13:9abd748f4e78 342
AzureIoTClient 6:641a9672db08 343 if ((result == SEND_ONE_MESSAGE_OK) && (properties != NULL))
AzureIoTClient 6:641a9672db08 344 {
AzureIoTClient 6:641a9672db08 345 if (amqpvalue_encode(properties_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 346 {
AzureIoTClient 6:641a9672db08 347 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 348 }
Azure.IoT Build 0:6ae2f7bca550 349
AzureIoTClient 6:641a9672db08 350 log_message_chunk(message_sender_instance, "Properties:", properties_amqp_value);
AzureIoTClient 6:641a9672db08 351 }
Azure.IoT Build 0:6ae2f7bca550 352
AzureIoTClient 6:641a9672db08 353 if ((result == SEND_ONE_MESSAGE_OK) && (application_properties != NULL))
AzureIoTClient 6:641a9672db08 354 {
AzureIoTClient 6:641a9672db08 355 if (amqpvalue_encode(application_properties_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 356 {
AzureIoTClient 6:641a9672db08 357 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 358 }
Azure.IoT Build 0:6ae2f7bca550 359
AzureIoTClient 6:641a9672db08 360 log_message_chunk(message_sender_instance, "Application properties:", application_properties_value);
AzureIoTClient 6:641a9672db08 361 }
Azure.IoT Build 0:6ae2f7bca550 362
AzureIoTClient 6:641a9672db08 363 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 364 {
AzureIoTClient 6:641a9672db08 365 switch (message_body_type)
AzureIoTClient 6:641a9672db08 366 {
AzureIoTClient 17:923575db8b2d 367 default:
AzureIoTClient 17:923575db8b2d 368 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 17:923575db8b2d 369 break;
AzureIoTClient 17:923575db8b2d 370
AzureIoTClient 6:641a9672db08 371 case MESSAGE_BODY_TYPE_VALUE:
AzureIoTClient 6:641a9672db08 372 {
AzureIoTClient 6:641a9672db08 373 if (amqpvalue_encode(body_amqp_value, encode_bytes, &payload) != 0)
AzureIoTClient 6:641a9672db08 374 {
AzureIoTClient 6:641a9672db08 375 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 376 }
Azure.IoT Build 0:6ae2f7bca550 377
AzureIoTClient 6:641a9672db08 378 log_message_chunk(message_sender_instance, "Body - amqp value:", body_amqp_value);
AzureIoTClient 6:641a9672db08 379 break;
AzureIoTClient 6:641a9672db08 380 }
AzureIoTClient 6:641a9672db08 381 case MESSAGE_BODY_TYPE_DATA:
AzureIoTClient 6:641a9672db08 382 {
Azure.IoT Build 0:6ae2f7bca550 383 BINARY_DATA binary_data;
Azure.IoT Build 0:6ae2f7bca550 384 size_t i;
Azure.IoT Build 0:6ae2f7bca550 385
Azure.IoT Build 0:6ae2f7bca550 386 for (i = 0; i < body_data_count; i++)
Azure.IoT Build 0:6ae2f7bca550 387 {
AzureIoTClient 24:2c59c2d43ebf 388 if (message_get_body_amqp_data_in_place(message, i, &binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 389 {
Azure.IoT Build 0:6ae2f7bca550 390 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 391 }
Azure.IoT Build 0:6ae2f7bca550 392 else
Azure.IoT Build 0:6ae2f7bca550 393 {
AzureIoTClient 28:add19eb7defa 394 AMQP_VALUE body_amqp_data;
AzureIoTClient 6:641a9672db08 395 amqp_binary binary_value;
AzureIoTClient 6:641a9672db08 396 binary_value.bytes = binary_data.bytes;
AzureIoTClient 6:641a9672db08 397 binary_value.length = (uint32_t)binary_data.length;
AzureIoTClient 25:1101516ee67d 398 body_amqp_data = amqpvalue_create_data(binary_value);
Azure.IoT Build 0:6ae2f7bca550 399 if (body_amqp_data == NULL)
Azure.IoT Build 0:6ae2f7bca550 400 {
Azure.IoT Build 0:6ae2f7bca550 401 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 402 }
Azure.IoT Build 0:6ae2f7bca550 403 else
Azure.IoT Build 0:6ae2f7bca550 404 {
Azure.IoT Build 0:6ae2f7bca550 405 if (amqpvalue_encode(body_amqp_data, encode_bytes, &payload) != 0)
Azure.IoT Build 0:6ae2f7bca550 406 {
Azure.IoT Build 0:6ae2f7bca550 407 result = SEND_ONE_MESSAGE_ERROR;
Azure.IoT Build 0:6ae2f7bca550 408 break;
Azure.IoT Build 0:6ae2f7bca550 409 }
Azure.IoT Build 0:6ae2f7bca550 410
Azure.IoT Build 0:6ae2f7bca550 411 amqpvalue_destroy(body_amqp_data);
Azure.IoT Build 0:6ae2f7bca550 412 }
Azure.IoT Build 0:6ae2f7bca550 413 }
Azure.IoT Build 0:6ae2f7bca550 414 }
AzureIoTClient 6:641a9672db08 415 break;
AzureIoTClient 6:641a9672db08 416 }
AzureIoTClient 6:641a9672db08 417 }
AzureIoTClient 6:641a9672db08 418 }
Azure.IoT Build 0:6ae2f7bca550 419
AzureIoTClient 6:641a9672db08 420 if (result == SEND_ONE_MESSAGE_OK)
AzureIoTClient 6:641a9672db08 421 {
AzureIoTClient 6:641a9672db08 422 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 423 switch (link_transfer(message_sender_instance->link, message_format, &payload, 1, on_delivery_settled, message_with_callback))
AzureIoTClient 6:641a9672db08 424 {
AzureIoTClient 6:641a9672db08 425 default:
AzureIoTClient 6:641a9672db08 426 case LINK_TRANSFER_ERROR:
AzureIoTClient 6:641a9672db08 427 result = SEND_ONE_MESSAGE_ERROR;
AzureIoTClient 6:641a9672db08 428 break;
Azure.IoT Build 0:6ae2f7bca550 429
AzureIoTClient 6:641a9672db08 430 case LINK_TRANSFER_BUSY:
AzureIoTClient 6:641a9672db08 431 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 432 result = SEND_ONE_MESSAGE_BUSY;
AzureIoTClient 6:641a9672db08 433 break;
Azure.IoT Build 0:6ae2f7bca550 434
AzureIoTClient 6:641a9672db08 435 case LINK_TRANSFER_OK:
AzureIoTClient 6:641a9672db08 436 result = SEND_ONE_MESSAGE_OK;
AzureIoTClient 6:641a9672db08 437 break;
AzureIoTClient 6:641a9672db08 438 }
AzureIoTClient 6:641a9672db08 439 }
Azure.IoT Build 0:6ae2f7bca550 440
AzureIoTClient 21:f9c433d8e6ca 441 free(data_bytes);
Azure.IoT Build 0:6ae2f7bca550 442
AzureIoTClient 6:641a9672db08 443 if (body_amqp_value != NULL)
AzureIoTClient 6:641a9672db08 444 {
AzureIoTClient 6:641a9672db08 445 amqpvalue_destroy(body_amqp_value);
AzureIoTClient 6:641a9672db08 446 }
AzureIoTClient 20:206846c14c80 447 }
Azure.IoT Build 0:6ae2f7bca550 448
AzureIoTClient 23:1111ee8bcba4 449 if (application_properties != NULL)
AzureIoTClient 23:1111ee8bcba4 450 {
AzureIoTClient 23:1111ee8bcba4 451 amqpvalue_destroy(application_properties);
AzureIoTClient 23:1111ee8bcba4 452 }
AzureIoTClient 23:1111ee8bcba4 453 if (application_properties_value != NULL)
AzureIoTClient 23:1111ee8bcba4 454 {
AzureIoTClient 23:1111ee8bcba4 455 amqpvalue_destroy(application_properties_value);
AzureIoTClient 23:1111ee8bcba4 456 }
AzureIoTClient 23:1111ee8bcba4 457 if (properties_amqp_value != NULL)
AzureIoTClient 23:1111ee8bcba4 458 {
AzureIoTClient 23:1111ee8bcba4 459 amqpvalue_destroy(properties_amqp_value);
AzureIoTClient 23:1111ee8bcba4 460 }
AzureIoTClient 23:1111ee8bcba4 461 if (properties != NULL)
AzureIoTClient 23:1111ee8bcba4 462 {
AzureIoTClient 23:1111ee8bcba4 463 properties_destroy(properties);
AzureIoTClient 23:1111ee8bcba4 464 }
AzureIoTClient 6:641a9672db08 465 }
Azure.IoT Build 0:6ae2f7bca550 466
AzureIoTClient 6:641a9672db08 467 return result;
Azure.IoT Build 0:6ae2f7bca550 468 }
Azure.IoT Build 0:6ae2f7bca550 469
Azure.IoT Build 0:6ae2f7bca550 470 static void send_all_pending_messages(MESSAGE_SENDER_INSTANCE* message_sender_instance)
Azure.IoT Build 0:6ae2f7bca550 471 {
AzureIoTClient 6:641a9672db08 472 size_t i;
Azure.IoT Build 0:6ae2f7bca550 473
AzureIoTClient 6:641a9672db08 474 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 6:641a9672db08 475 {
AzureIoTClient 6:641a9672db08 476 if (message_sender_instance->messages[i]->message_send_state == MESSAGE_SEND_STATE_NOT_SENT)
AzureIoTClient 6:641a9672db08 477 {
AzureIoTClient 6:641a9672db08 478 switch (send_one_message(message_sender_instance, message_sender_instance->messages[i], message_sender_instance->messages[i]->message))
AzureIoTClient 6:641a9672db08 479 {
AzureIoTClient 6:641a9672db08 480 default:
AzureIoTClient 6:641a9672db08 481 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 6:641a9672db08 482 {
AzureIoTClient 6:641a9672db08 483 ON_MESSAGE_SEND_COMPLETE on_message_send_complete = message_sender_instance->messages[i]->on_message_send_complete;
AzureIoTClient 6:641a9672db08 484 void* context = message_sender_instance->messages[i]->context;
AzureIoTClient 6:641a9672db08 485 remove_pending_message_by_index(message_sender_instance, i);
Azure.IoT Build 0:6ae2f7bca550 486
AzureIoTClient 23:1111ee8bcba4 487 if (on_message_send_complete != NULL)
AzureIoTClient 23:1111ee8bcba4 488 {
AzureIoTClient 23:1111ee8bcba4 489 on_message_send_complete(context, MESSAGE_SEND_ERROR);
AzureIoTClient 23:1111ee8bcba4 490 }
AzureIoTClient 18:a922a4a30a82 491
AzureIoTClient 6:641a9672db08 492 i = message_sender_instance->message_count;
AzureIoTClient 6:641a9672db08 493 break;
AzureIoTClient 6:641a9672db08 494 }
AzureIoTClient 6:641a9672db08 495 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 496 i = message_sender_instance->message_count + 1;
AzureIoTClient 6:641a9672db08 497 break;
Azure.IoT Build 0:6ae2f7bca550 498
AzureIoTClient 6:641a9672db08 499 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 500 break;
AzureIoTClient 6:641a9672db08 501 }
Azure.IoT Build 0:6ae2f7bca550 502
AzureIoTClient 6:641a9672db08 503 i--;
AzureIoTClient 6:641a9672db08 504 }
AzureIoTClient 6:641a9672db08 505 }
Azure.IoT Build 0:6ae2f7bca550 506 }
Azure.IoT Build 0:6ae2f7bca550 507
Azure.IoT Build 0:6ae2f7bca550 508 static void set_message_sender_state(MESSAGE_SENDER_INSTANCE* message_sender_instance, MESSAGE_SENDER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 509 {
AzureIoTClient 6:641a9672db08 510 MESSAGE_SENDER_STATE previous_state = message_sender_instance->message_sender_state;
AzureIoTClient 6:641a9672db08 511 message_sender_instance->message_sender_state = new_state;
AzureIoTClient 6:641a9672db08 512 if (message_sender_instance->on_message_sender_state_changed != NULL)
AzureIoTClient 6:641a9672db08 513 {
AzureIoTClient 6:641a9672db08 514 message_sender_instance->on_message_sender_state_changed(message_sender_instance->on_message_sender_state_changed_context, new_state, previous_state);
AzureIoTClient 6:641a9672db08 515 }
Azure.IoT Build 0:6ae2f7bca550 516 }
Azure.IoT Build 0:6ae2f7bca550 517
AzureIoTClient 2:64b4feb67cd3 518 static void indicate_all_messages_as_error(MESSAGE_SENDER_INSTANCE* message_sender_instance)
AzureIoTClient 2:64b4feb67cd3 519 {
AzureIoTClient 2:64b4feb67cd3 520 size_t i;
AzureIoTClient 2:64b4feb67cd3 521
AzureIoTClient 2:64b4feb67cd3 522 for (i = 0; i < message_sender_instance->message_count; i++)
AzureIoTClient 2:64b4feb67cd3 523 {
AzureIoTClient 2:64b4feb67cd3 524 if (message_sender_instance->messages[i]->on_message_send_complete != NULL)
AzureIoTClient 2:64b4feb67cd3 525 {
AzureIoTClient 2:64b4feb67cd3 526 message_sender_instance->messages[i]->on_message_send_complete(message_sender_instance->messages[i]->context, MESSAGE_SEND_ERROR);
AzureIoTClient 2:64b4feb67cd3 527 }
AzureIoTClient 2:64b4feb67cd3 528
AzureIoTClient 2:64b4feb67cd3 529 message_destroy(message_sender_instance->messages[i]->message);
AzureIoTClient 21:f9c433d8e6ca 530 free(message_sender_instance->messages[i]);
AzureIoTClient 2:64b4feb67cd3 531 }
AzureIoTClient 2:64b4feb67cd3 532
AzureIoTClient 2:64b4feb67cd3 533 if (message_sender_instance->messages != NULL)
AzureIoTClient 2:64b4feb67cd3 534 {
AzureIoTClient 2:64b4feb67cd3 535 message_sender_instance->message_count = 0;
AzureIoTClient 2:64b4feb67cd3 536
AzureIoTClient 21:f9c433d8e6ca 537 free(message_sender_instance->messages);
AzureIoTClient 2:64b4feb67cd3 538 message_sender_instance->messages = NULL;
AzureIoTClient 2:64b4feb67cd3 539 }
AzureIoTClient 2:64b4feb67cd3 540 }
AzureIoTClient 2:64b4feb67cd3 541
Azure.IoT Build 0:6ae2f7bca550 542 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 543 {
AzureIoTClient 6:641a9672db08 544 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 545 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 546
AzureIoTClient 6:641a9672db08 547 switch (new_link_state)
AzureIoTClient 6:641a9672db08 548 {
AzureIoTClient 17:923575db8b2d 549 default:
AzureIoTClient 17:923575db8b2d 550 break;
AzureIoTClient 17:923575db8b2d 551
AzureIoTClient 6:641a9672db08 552 case LINK_STATE_ATTACHED:
AzureIoTClient 6:641a9672db08 553 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING)
AzureIoTClient 6:641a9672db08 554 {
AzureIoTClient 6:641a9672db08 555 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPEN);
AzureIoTClient 6:641a9672db08 556 }
AzureIoTClient 6:641a9672db08 557 break;
AzureIoTClient 6:641a9672db08 558 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 559 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 560 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 561 {
Azure.IoT Build 0:6ae2f7bca550 562 /* User initiated transition, we should be good */
AzureIoTClient 23:1111ee8bcba4 563 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_IDLE);
AzureIoTClient 2:64b4feb67cd3 564 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 565 }
Azure.IoT Build 0:6ae2f7bca550 566 else if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 567 {
Azure.IoT Build 0:6ae2f7bca550 568 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 569 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 570 }
Azure.IoT Build 0:6ae2f7bca550 571 break;
AzureIoTClient 6:641a9672db08 572 case LINK_STATE_ERROR:
AzureIoTClient 6:641a9672db08 573 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 574 {
AzureIoTClient 23:1111ee8bcba4 575 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 2:64b4feb67cd3 576 indicate_all_messages_as_error(message_sender_instance);
AzureIoTClient 6:641a9672db08 577 }
AzureIoTClient 6:641a9672db08 578 break;
AzureIoTClient 6:641a9672db08 579 }
Azure.IoT Build 0:6ae2f7bca550 580 }
Azure.IoT Build 0:6ae2f7bca550 581
Azure.IoT Build 0:6ae2f7bca550 582 static void on_link_flow_on(void* context)
Azure.IoT Build 0:6ae2f7bca550 583 {
AzureIoTClient 6:641a9672db08 584 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 585 send_all_pending_messages(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 586 }
Azure.IoT Build 0:6ae2f7bca550 587
Azure.IoT Build 5:ae49385aff34 588 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 589 {
AzureIoTClient 23:1111ee8bcba4 590 MESSAGE_SENDER_INSTANCE* result = (MESSAGE_SENDER_INSTANCE*)malloc(sizeof(MESSAGE_SENDER_INSTANCE));
AzureIoTClient 6:641a9672db08 591 if (result != NULL)
AzureIoTClient 6:641a9672db08 592 {
AzureIoTClient 6:641a9672db08 593 result->messages = NULL;
AzureIoTClient 6:641a9672db08 594 result->message_count = 0;
AzureIoTClient 6:641a9672db08 595 result->link = link;
AzureIoTClient 6:641a9672db08 596 result->on_message_sender_state_changed = on_message_sender_state_changed;
AzureIoTClient 6:641a9672db08 597 result->on_message_sender_state_changed_context = context;
AzureIoTClient 6:641a9672db08 598 result->message_sender_state = MESSAGE_SENDER_STATE_IDLE;
AzureIoTClient 6:641a9672db08 599 result->is_trace_on = 0;
AzureIoTClient 6:641a9672db08 600 }
Azure.IoT Build 0:6ae2f7bca550 601
AzureIoTClient 6:641a9672db08 602 return result;
Azure.IoT Build 0:6ae2f7bca550 603 }
Azure.IoT Build 0:6ae2f7bca550 604
Azure.IoT Build 0:6ae2f7bca550 605 void messagesender_destroy(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 606 {
AzureIoTClient 6:641a9672db08 607 if (message_sender != NULL)
AzureIoTClient 6:641a9672db08 608 {
AzureIoTClient 6:641a9672db08 609 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 610
AzureIoTClient 6:641a9672db08 611 messagesender_close(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 612
AzureIoTClient 2:64b4feb67cd3 613 indicate_all_messages_as_error(message_sender_instance);
Azure.IoT Build 0:6ae2f7bca550 614
AzureIoTClient 21:f9c433d8e6ca 615 free(message_sender);
AzureIoTClient 6:641a9672db08 616 }
Azure.IoT Build 0:6ae2f7bca550 617 }
Azure.IoT Build 0:6ae2f7bca550 618
Azure.IoT Build 0:6ae2f7bca550 619 int messagesender_open(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 620 {
AzureIoTClient 6:641a9672db08 621 int result;
Azure.IoT Build 0:6ae2f7bca550 622
AzureIoTClient 6:641a9672db08 623 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 624 {
AzureIoTClient 19:000ab4e6a2c1 625 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 626 }
AzureIoTClient 6:641a9672db08 627 else
AzureIoTClient 6:641a9672db08 628 {
AzureIoTClient 6:641a9672db08 629 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 630
AzureIoTClient 6:641a9672db08 631 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_IDLE)
AzureIoTClient 6:641a9672db08 632 {
AzureIoTClient 6:641a9672db08 633 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_OPENING);
AzureIoTClient 6:641a9672db08 634 if (link_attach(message_sender_instance->link, NULL, on_link_state_changed, on_link_flow_on, message_sender_instance) != 0)
AzureIoTClient 6:641a9672db08 635 {
AzureIoTClient 19:000ab4e6a2c1 636 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 637 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 638 }
AzureIoTClient 6:641a9672db08 639 else
AzureIoTClient 6:641a9672db08 640 {
AzureIoTClient 6:641a9672db08 641 result = 0;
AzureIoTClient 6:641a9672db08 642 }
AzureIoTClient 6:641a9672db08 643 }
AzureIoTClient 6:641a9672db08 644 else
AzureIoTClient 6:641a9672db08 645 {
AzureIoTClient 6:641a9672db08 646 result = 0;
AzureIoTClient 6:641a9672db08 647 }
AzureIoTClient 6:641a9672db08 648 }
Azure.IoT Build 0:6ae2f7bca550 649
AzureIoTClient 6:641a9672db08 650 return result;
Azure.IoT Build 0:6ae2f7bca550 651 }
Azure.IoT Build 0:6ae2f7bca550 652
Azure.IoT Build 0:6ae2f7bca550 653 int messagesender_close(MESSAGE_SENDER_HANDLE message_sender)
Azure.IoT Build 0:6ae2f7bca550 654 {
AzureIoTClient 6:641a9672db08 655 int result;
Azure.IoT Build 0:6ae2f7bca550 656
AzureIoTClient 6:641a9672db08 657 if (message_sender == NULL)
AzureIoTClient 6:641a9672db08 658 {
AzureIoTClient 19:000ab4e6a2c1 659 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 660 }
AzureIoTClient 6:641a9672db08 661 else
AzureIoTClient 6:641a9672db08 662 {
AzureIoTClient 6:641a9672db08 663 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
Azure.IoT Build 0:6ae2f7bca550 664
AzureIoTClient 6:641a9672db08 665 if ((message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPENING) ||
AzureIoTClient 6:641a9672db08 666 (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN))
AzureIoTClient 6:641a9672db08 667 {
AzureIoTClient 6:641a9672db08 668 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_CLOSING);
AzureIoTClient 12:b30dacf113f2 669 if (link_detach(message_sender_instance->link, true) != 0)
AzureIoTClient 6:641a9672db08 670 {
AzureIoTClient 19:000ab4e6a2c1 671 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 672 set_message_sender_state(message_sender_instance, MESSAGE_SENDER_STATE_ERROR);
AzureIoTClient 6:641a9672db08 673 }
AzureIoTClient 6:641a9672db08 674 else
AzureIoTClient 6:641a9672db08 675 {
AzureIoTClient 6:641a9672db08 676 result = 0;
AzureIoTClient 6:641a9672db08 677 }
AzureIoTClient 6:641a9672db08 678 }
AzureIoTClient 6:641a9672db08 679 else
AzureIoTClient 6:641a9672db08 680 {
AzureIoTClient 6:641a9672db08 681 result = 0;
AzureIoTClient 6:641a9672db08 682 }
AzureIoTClient 6:641a9672db08 683 }
Azure.IoT Build 0:6ae2f7bca550 684
AzureIoTClient 6:641a9672db08 685 return result;
Azure.IoT Build 0:6ae2f7bca550 686 }
Azure.IoT Build 0:6ae2f7bca550 687
Azure.IoT Build 0:6ae2f7bca550 688 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 689 {
AzureIoTClient 6:641a9672db08 690 int result;
Azure.IoT Build 0:6ae2f7bca550 691
AzureIoTClient 6:641a9672db08 692 if ((message_sender == NULL) ||
AzureIoTClient 6:641a9672db08 693 (message == NULL))
AzureIoTClient 6:641a9672db08 694 {
AzureIoTClient 19:000ab4e6a2c1 695 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 696 }
AzureIoTClient 6:641a9672db08 697 else
AzureIoTClient 6:641a9672db08 698 {
AzureIoTClient 6:641a9672db08 699 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 700 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_ERROR)
AzureIoTClient 6:641a9672db08 701 {
AzureIoTClient 19:000ab4e6a2c1 702 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 703 }
AzureIoTClient 6:641a9672db08 704 else
AzureIoTClient 6:641a9672db08 705 {
AzureIoTClient 21:f9c433d8e6ca 706 MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)malloc(sizeof(MESSAGE_WITH_CALLBACK));
AzureIoTClient 6:641a9672db08 707 if (message_with_callback == NULL)
AzureIoTClient 6:641a9672db08 708 {
AzureIoTClient 19:000ab4e6a2c1 709 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 710 }
AzureIoTClient 6:641a9672db08 711 else
AzureIoTClient 6:641a9672db08 712 {
AzureIoTClient 21:f9c433d8e6ca 713 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 714 if (new_messages == NULL)
AzureIoTClient 6:641a9672db08 715 {
AzureIoTClient 21:f9c433d8e6ca 716 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 717 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 718 }
AzureIoTClient 6:641a9672db08 719 else
AzureIoTClient 6:641a9672db08 720 {
AzureIoTClient 6:641a9672db08 721 result = 0;
Azure.IoT Build 0:6ae2f7bca550 722
AzureIoTClient 6:641a9672db08 723 message_sender_instance->messages = new_messages;
AzureIoTClient 6:641a9672db08 724 if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 725 {
AzureIoTClient 6:641a9672db08 726 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 727 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 728 {
AzureIoTClient 21:f9c433d8e6ca 729 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 730 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 731 }
AzureIoTClient 6:641a9672db08 732
AzureIoTClient 6:641a9672db08 733 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 734 }
AzureIoTClient 6:641a9672db08 735 else
AzureIoTClient 6:641a9672db08 736 {
AzureIoTClient 6:641a9672db08 737 message_with_callback->message = NULL;
AzureIoTClient 6:641a9672db08 738 message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING;
AzureIoTClient 6:641a9672db08 739 }
Azure.IoT Build 0:6ae2f7bca550 740
AzureIoTClient 6:641a9672db08 741 if (result == 0)
AzureIoTClient 6:641a9672db08 742 {
AzureIoTClient 6:641a9672db08 743 message_with_callback->on_message_send_complete = on_message_send_complete;
AzureIoTClient 6:641a9672db08 744 message_with_callback->context = callback_context;
AzureIoTClient 6:641a9672db08 745 message_with_callback->message_sender = message_sender_instance;
AzureIoTClient 6:641a9672db08 746
AzureIoTClient 6:641a9672db08 747 message_sender_instance->messages[message_sender_instance->message_count] = message_with_callback;
AzureIoTClient 6:641a9672db08 748 message_sender_instance->message_count++;
Azure.IoT Build 0:6ae2f7bca550 749
AzureIoTClient 6:641a9672db08 750 if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN)
AzureIoTClient 6:641a9672db08 751 {
AzureIoTClient 6:641a9672db08 752 switch (send_one_message(message_sender_instance, message_with_callback, message))
AzureIoTClient 6:641a9672db08 753 {
AzureIoTClient 6:641a9672db08 754 default:
AzureIoTClient 6:641a9672db08 755 case SEND_ONE_MESSAGE_ERROR:
AzureIoTClient 23:1111ee8bcba4 756
AzureIoTClient 6:641a9672db08 757 remove_pending_message_by_index(message_sender_instance, message_sender_instance->message_count - 1);
AzureIoTClient 19:000ab4e6a2c1 758 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 759 break;
Azure.IoT Build 0:6ae2f7bca550 760
AzureIoTClient 6:641a9672db08 761 case SEND_ONE_MESSAGE_BUSY:
AzureIoTClient 6:641a9672db08 762 message_with_callback->message = message_clone(message);
AzureIoTClient 6:641a9672db08 763 if (message_with_callback->message == NULL)
AzureIoTClient 6:641a9672db08 764 {
AzureIoTClient 21:f9c433d8e6ca 765 free(message_with_callback);
AzureIoTClient 19:000ab4e6a2c1 766 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 767 }
AzureIoTClient 6:641a9672db08 768 else
AzureIoTClient 6:641a9672db08 769 {
AzureIoTClient 6:641a9672db08 770 message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT;
AzureIoTClient 6:641a9672db08 771 result = 0;
AzureIoTClient 6:641a9672db08 772 }
AzureIoTClient 6:641a9672db08 773 break;
Azure.IoT Build 0:6ae2f7bca550 774
AzureIoTClient 6:641a9672db08 775 case SEND_ONE_MESSAGE_OK:
AzureIoTClient 6:641a9672db08 776 result = 0;
AzureIoTClient 6:641a9672db08 777 break;
AzureIoTClient 6:641a9672db08 778 }
AzureIoTClient 6:641a9672db08 779 }
AzureIoTClient 6:641a9672db08 780 }
AzureIoTClient 6:641a9672db08 781 }
AzureIoTClient 6:641a9672db08 782 }
AzureIoTClient 6:641a9672db08 783 }
AzureIoTClient 6:641a9672db08 784 }
AzureIoTClient 6:641a9672db08 785 return result;
AzureIoTClient 6:641a9672db08 786 }
Azure.IoT Build 0:6ae2f7bca550 787
AzureIoTClient 6:641a9672db08 788 void messagesender_set_trace(MESSAGE_SENDER_HANDLE message_sender, bool traceOn)
AzureIoTClient 6:641a9672db08 789 {
AzureIoTClient 6:641a9672db08 790 MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender;
AzureIoTClient 6:641a9672db08 791 if (message_sender_instance != NULL)
AzureIoTClient 6:641a9672db08 792 {
AzureIoTClient 6:641a9672db08 793 message_sender_instance->is_trace_on = traceOn ? 1 : 0;
AzureIoTClient 6:641a9672db08 794 }
Azure.IoT Build 0:6ae2f7bca550 795 }