A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Mar 24 16:35:45 2017 -0700
Revision:
21:f9c433d8e6ca
Parent:
20:206846c14c80
Child:
23:1111ee8bcba4
1.1.10

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 20:206846c14c80 5
AzureIoTClient 19:000ab4e6a2c1 6 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 21:f9c433d8e6ca 7 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:6ae2f7bca550 8 #include "azure_uamqp_c/message_receiver.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqp_definitions.h"
Azure.IoT Build 0:6ae2f7bca550 11
Azure.IoT Build 0:6ae2f7bca550 12 typedef struct MESSAGE_RECEIVER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 13 {
Azure.IoT Build 0:6ae2f7bca550 14 LINK_HANDLE link;
Azure.IoT Build 0:6ae2f7bca550 15 ON_MESSAGE_RECEIVED on_message_received;
Azure.IoT Build 0:6ae2f7bca550 16 ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 17 MESSAGE_RECEIVER_STATE message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 18 const void* on_message_receiver_state_changed_context;
Azure.IoT Build 0:6ae2f7bca550 19 const void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 20 MESSAGE_HANDLE decoded_message;
Azure.IoT Build 0:6ae2f7bca550 21 bool decode_error;
Azure.IoT Build 0:6ae2f7bca550 22 } MESSAGE_RECEIVER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 23
Azure.IoT Build 0:6ae2f7bca550 24 static void set_message_receiver_state(MESSAGE_RECEIVER_INSTANCE* message_receiver_instance, MESSAGE_RECEIVER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 25 {
Azure.IoT Build 0:6ae2f7bca550 26 MESSAGE_RECEIVER_STATE previous_state = message_receiver_instance->message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 27 message_receiver_instance->message_receiver_state = new_state;
Azure.IoT Build 0:6ae2f7bca550 28 if (message_receiver_instance->on_message_receiver_state_changed != NULL)
Azure.IoT Build 0:6ae2f7bca550 29 {
Azure.IoT Build 0:6ae2f7bca550 30 message_receiver_instance->on_message_receiver_state_changed(message_receiver_instance->on_message_receiver_state_changed_context, new_state, previous_state);
Azure.IoT Build 0:6ae2f7bca550 31 }
Azure.IoT Build 0:6ae2f7bca550 32 }
Azure.IoT Build 0:6ae2f7bca550 33
Azure.IoT Build 0:6ae2f7bca550 34 static void decode_message_value_callback(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 35 {
Azure.IoT Build 0:6ae2f7bca550 36 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 37 MESSAGE_HANDLE decoded_message = message_receiver_instance->decoded_message;
Azure.IoT Build 0:6ae2f7bca550 38 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 39
Azure.IoT Build 0:6ae2f7bca550 40 if (is_application_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 41 {
Azure.IoT Build 0:6ae2f7bca550 42 if (message_set_application_properties(decoded_message, decoded_value) != 0)
Azure.IoT Build 0:6ae2f7bca550 43 {
Azure.IoT Build 0:6ae2f7bca550 44 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 45 }
Azure.IoT Build 0:6ae2f7bca550 46 }
Azure.IoT Build 0:6ae2f7bca550 47 else if (is_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 48 {
Azure.IoT Build 0:6ae2f7bca550 49 PROPERTIES_HANDLE properties;
Azure.IoT Build 0:6ae2f7bca550 50 if (amqpvalue_get_properties(decoded_value, &properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 51 {
Azure.IoT Build 0:6ae2f7bca550 52 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 53 }
Azure.IoT Build 0:6ae2f7bca550 54 else
Azure.IoT Build 0:6ae2f7bca550 55 {
Azure.IoT Build 0:6ae2f7bca550 56 if (message_set_properties(decoded_message, properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 57 {
Azure.IoT Build 0:6ae2f7bca550 58 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 59 }
Azure.IoT Build 0:6ae2f7bca550 60
Azure.IoT Build 0:6ae2f7bca550 61 properties_destroy(properties);
Azure.IoT Build 0:6ae2f7bca550 62 }
Azure.IoT Build 0:6ae2f7bca550 63 }
Azure.IoT Build 0:6ae2f7bca550 64 else if (is_delivery_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 65 {
Azure.IoT Build 0:6ae2f7bca550 66 annotations delivery_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 67 if ((delivery_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 68 (message_set_delivery_annotations(decoded_message, delivery_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 69 {
Azure.IoT Build 0:6ae2f7bca550 70 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 71 }
Azure.IoT Build 0:6ae2f7bca550 72 }
Azure.IoT Build 0:6ae2f7bca550 73 else if (is_message_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 74 {
Azure.IoT Build 0:6ae2f7bca550 75 annotations message_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 76 if ((message_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 77 (message_set_message_annotations(decoded_message, message_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 78 {
Azure.IoT Build 0:6ae2f7bca550 79 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 80 }
Azure.IoT Build 0:6ae2f7bca550 81 }
Azure.IoT Build 0:6ae2f7bca550 82 else if (is_header_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 83 {
Azure.IoT Build 0:6ae2f7bca550 84 HEADER_HANDLE header;
Azure.IoT Build 0:6ae2f7bca550 85 if (amqpvalue_get_header(decoded_value, &header) != 0)
Azure.IoT Build 0:6ae2f7bca550 86 {
Azure.IoT Build 0:6ae2f7bca550 87 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 88 }
Azure.IoT Build 0:6ae2f7bca550 89 else
Azure.IoT Build 0:6ae2f7bca550 90 {
Azure.IoT Build 0:6ae2f7bca550 91 if (message_set_header(decoded_message, header) != 0)
Azure.IoT Build 0:6ae2f7bca550 92 {
Azure.IoT Build 0:6ae2f7bca550 93 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 94 }
Azure.IoT Build 0:6ae2f7bca550 95
Azure.IoT Build 0:6ae2f7bca550 96 header_destroy(header);
Azure.IoT Build 0:6ae2f7bca550 97 }
Azure.IoT Build 0:6ae2f7bca550 98 }
Azure.IoT Build 0:6ae2f7bca550 99 else if (is_footer_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 100 {
Azure.IoT Build 0:6ae2f7bca550 101 annotations footer = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 102 if ((footer == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 103 (message_set_footer(decoded_message, footer) != 0))
Azure.IoT Build 0:6ae2f7bca550 104 {
Azure.IoT Build 0:6ae2f7bca550 105 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 106 }
Azure.IoT Build 0:6ae2f7bca550 107 }
Azure.IoT Build 0:6ae2f7bca550 108 else if (is_amqp_value_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 109 {
Azure.IoT Build 0:6ae2f7bca550 110 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 111 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 112 if (body_type != MESSAGE_BODY_TYPE_NONE)
Azure.IoT Build 0:6ae2f7bca550 113 {
Azure.IoT Build 0:6ae2f7bca550 114 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 115 }
Azure.IoT Build 0:6ae2f7bca550 116 else
Azure.IoT Build 0:6ae2f7bca550 117 {
Azure.IoT Build 0:6ae2f7bca550 118 AMQP_VALUE body_amqp_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 119 if ((body_amqp_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 120 (message_set_body_amqp_value(decoded_message, body_amqp_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 121 {
Azure.IoT Build 0:6ae2f7bca550 122 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 123 }
Azure.IoT Build 0:6ae2f7bca550 124 }
Azure.IoT Build 0:6ae2f7bca550 125 }
Azure.IoT Build 0:6ae2f7bca550 126 else if (is_data_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 127 {
Azure.IoT Build 0:6ae2f7bca550 128 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 129 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 130 if ((body_type != MESSAGE_BODY_TYPE_NONE) &&
Azure.IoT Build 0:6ae2f7bca550 131 (body_type != MESSAGE_BODY_TYPE_DATA))
Azure.IoT Build 0:6ae2f7bca550 132 {
Azure.IoT Build 0:6ae2f7bca550 133 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 134 }
Azure.IoT Build 0:6ae2f7bca550 135 else
Azure.IoT Build 0:6ae2f7bca550 136 {
Azure.IoT Build 0:6ae2f7bca550 137 AMQP_VALUE body_data_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 138 data data_value;
Azure.IoT Build 0:6ae2f7bca550 139
Azure.IoT Build 0:6ae2f7bca550 140 if ((body_data_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 141 (amqpvalue_get_data(body_data_value, &data_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 142 {
Azure.IoT Build 0:6ae2f7bca550 143 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 144 }
Azure.IoT Build 0:6ae2f7bca550 145 else
Azure.IoT Build 0:6ae2f7bca550 146 {
AzureIoTClient 6:641a9672db08 147 BINARY_DATA binary_data;
AzureIoTClient 6:641a9672db08 148 binary_data.bytes = data_value.bytes;
AzureIoTClient 6:641a9672db08 149 binary_data.length = data_value.length;
Azure.IoT Build 0:6ae2f7bca550 150 if (message_add_body_amqp_data(decoded_message, binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 151 {
Azure.IoT Build 0:6ae2f7bca550 152 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 153 }
Azure.IoT Build 0:6ae2f7bca550 154 }
Azure.IoT Build 0:6ae2f7bca550 155 }
Azure.IoT Build 0:6ae2f7bca550 156 }
Azure.IoT Build 0:6ae2f7bca550 157 }
Azure.IoT Build 0:6ae2f7bca550 158
Azure.IoT Build 0:6ae2f7bca550 159 static AMQP_VALUE on_transfer_received(void* context, TRANSFER_HANDLE transfer, uint32_t payload_size, const unsigned char* payload_bytes)
Azure.IoT Build 0:6ae2f7bca550 160 {
Azure.IoT Build 0:6ae2f7bca550 161 AMQP_VALUE result = NULL;
Azure.IoT Build 0:6ae2f7bca550 162
AzureIoTClient 1:eab586236bfe 163 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 164 (void)transfer;
AzureIoTClient 1:eab586236bfe 165 if (message_receiver_instance->on_message_received != NULL)
AzureIoTClient 1:eab586236bfe 166 {
AzureIoTClient 1:eab586236bfe 167 MESSAGE_HANDLE message = message_create();
Azure.IoT Build 0:6ae2f7bca550 168 if (message == NULL)
Azure.IoT Build 0:6ae2f7bca550 169 {
Azure.IoT Build 0:6ae2f7bca550 170 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 171 }
Azure.IoT Build 0:6ae2f7bca550 172 else
Azure.IoT Build 0:6ae2f7bca550 173 {
Azure.IoT Build 0:6ae2f7bca550 174 AMQPVALUE_DECODER_HANDLE amqpvalue_decoder = amqpvalue_decoder_create(decode_message_value_callback, message_receiver_instance);
Azure.IoT Build 0:6ae2f7bca550 175 if (amqpvalue_decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 176 {
Azure.IoT Build 0:6ae2f7bca550 177 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 178 }
Azure.IoT Build 0:6ae2f7bca550 179 else
Azure.IoT Build 0:6ae2f7bca550 180 {
Azure.IoT Build 0:6ae2f7bca550 181 message_receiver_instance->decoded_message = message;
Azure.IoT Build 0:6ae2f7bca550 182 message_receiver_instance->decode_error = false;
Azure.IoT Build 0:6ae2f7bca550 183 if (amqpvalue_decode_bytes(amqpvalue_decoder, payload_bytes, payload_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 184 {
Azure.IoT Build 0:6ae2f7bca550 185 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 186 }
Azure.IoT Build 0:6ae2f7bca550 187 else
Azure.IoT Build 0:6ae2f7bca550 188 {
Azure.IoT Build 0:6ae2f7bca550 189 if (message_receiver_instance->decode_error)
Azure.IoT Build 0:6ae2f7bca550 190 {
Azure.IoT Build 0:6ae2f7bca550 191 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 192 }
Azure.IoT Build 0:6ae2f7bca550 193 else
Azure.IoT Build 0:6ae2f7bca550 194 {
AzureIoTClient 20:206846c14c80 195 result = message_receiver_instance->on_message_received(message_receiver_instance->callback_context, message);
Azure.IoT Build 0:6ae2f7bca550 196 }
Azure.IoT Build 0:6ae2f7bca550 197 }
Azure.IoT Build 0:6ae2f7bca550 198
Azure.IoT Build 0:6ae2f7bca550 199 amqpvalue_decoder_destroy(amqpvalue_decoder);
Azure.IoT Build 0:6ae2f7bca550 200 }
Azure.IoT Build 0:6ae2f7bca550 201
Azure.IoT Build 0:6ae2f7bca550 202 message_destroy(message);
Azure.IoT Build 0:6ae2f7bca550 203 }
AzureIoTClient 1:eab586236bfe 204 }
Azure.IoT Build 0:6ae2f7bca550 205
Azure.IoT Build 0:6ae2f7bca550 206 return result;
Azure.IoT Build 0:6ae2f7bca550 207 }
Azure.IoT Build 0:6ae2f7bca550 208
Azure.IoT Build 0:6ae2f7bca550 209 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 210 {
Azure.IoT Build 0:6ae2f7bca550 211 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 212 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 213
Azure.IoT Build 0:6ae2f7bca550 214 switch (new_link_state)
Azure.IoT Build 0:6ae2f7bca550 215 {
AzureIoTClient 17:923575db8b2d 216 default:
AzureIoTClient 17:923575db8b2d 217 break;
AzureIoTClient 17:923575db8b2d 218
Azure.IoT Build 0:6ae2f7bca550 219 case LINK_STATE_ATTACHED:
Azure.IoT Build 0:6ae2f7bca550 220 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING)
Azure.IoT Build 0:6ae2f7bca550 221 {
Azure.IoT Build 0:6ae2f7bca550 222 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPEN);
Azure.IoT Build 0:6ae2f7bca550 223 }
Azure.IoT Build 0:6ae2f7bca550 224 break;
Azure.IoT Build 0:6ae2f7bca550 225 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 226 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 227 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 228 {
Azure.IoT Build 0:6ae2f7bca550 229 /* User initiated transition, we should be good */
Azure.IoT Build 0:6ae2f7bca550 230 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 231 }
Azure.IoT Build 0:6ae2f7bca550 232 else if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 233 {
Azure.IoT Build 0:6ae2f7bca550 234 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 235 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 236 }
Azure.IoT Build 0:6ae2f7bca550 237 break;
AzureIoTClient 1:eab586236bfe 238 case LINK_STATE_ERROR:
AzureIoTClient 1:eab586236bfe 239 if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_ERROR)
AzureIoTClient 1:eab586236bfe 240 {
AzureIoTClient 1:eab586236bfe 241 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 1:eab586236bfe 242 }
AzureIoTClient 1:eab586236bfe 243 break;
Azure.IoT Build 0:6ae2f7bca550 244 }
Azure.IoT Build 0:6ae2f7bca550 245 }
Azure.IoT Build 0:6ae2f7bca550 246
Azure.IoT Build 0:6ae2f7bca550 247 MESSAGE_RECEIVER_HANDLE messagereceiver_create(LINK_HANDLE link, ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed, void* context)
Azure.IoT Build 0:6ae2f7bca550 248 {
AzureIoTClient 21:f9c433d8e6ca 249 MESSAGE_RECEIVER_INSTANCE* result = (MESSAGE_RECEIVER_INSTANCE*)malloc(sizeof(MESSAGE_RECEIVER_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 250 if (result != NULL)
Azure.IoT Build 0:6ae2f7bca550 251 {
Azure.IoT Build 0:6ae2f7bca550 252 result->link = link;
Azure.IoT Build 0:6ae2f7bca550 253 result->on_message_receiver_state_changed = on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 254 result->on_message_receiver_state_changed_context = context;
Azure.IoT Build 0:6ae2f7bca550 255 result->message_receiver_state = MESSAGE_RECEIVER_STATE_IDLE;
Azure.IoT Build 0:6ae2f7bca550 256 }
Azure.IoT Build 0:6ae2f7bca550 257
Azure.IoT Build 0:6ae2f7bca550 258 return result;
Azure.IoT Build 0:6ae2f7bca550 259 }
Azure.IoT Build 0:6ae2f7bca550 260
Azure.IoT Build 0:6ae2f7bca550 261 void messagereceiver_destroy(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 262 {
Azure.IoT Build 0:6ae2f7bca550 263 if (message_receiver != NULL)
Azure.IoT Build 0:6ae2f7bca550 264 {
AzureIoTClient 1:eab586236bfe 265 (void)messagereceiver_close(message_receiver);
AzureIoTClient 21:f9c433d8e6ca 266 free(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 267 }
Azure.IoT Build 0:6ae2f7bca550 268 }
Azure.IoT Build 0:6ae2f7bca550 269
Azure.IoT Build 0:6ae2f7bca550 270 int messagereceiver_open(MESSAGE_RECEIVER_HANDLE message_receiver, ON_MESSAGE_RECEIVED on_message_received, const void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 271 {
Azure.IoT Build 0:6ae2f7bca550 272 int result;
Azure.IoT Build 0:6ae2f7bca550 273
Azure.IoT Build 0:6ae2f7bca550 274 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 275 {
AzureIoTClient 19:000ab4e6a2c1 276 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 277 }
Azure.IoT Build 0:6ae2f7bca550 278 else
Azure.IoT Build 0:6ae2f7bca550 279 {
Azure.IoT Build 0:6ae2f7bca550 280 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 281
Azure.IoT Build 0:6ae2f7bca550 282 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 283 {
Azure.IoT Build 0:6ae2f7bca550 284 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPENING);
Azure.IoT Build 0:6ae2f7bca550 285 if (link_attach(message_receiver_instance->link, on_transfer_received, on_link_state_changed, NULL, message_receiver_instance) != 0)
Azure.IoT Build 0:6ae2f7bca550 286 {
AzureIoTClient 19:000ab4e6a2c1 287 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 288 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 289 }
Azure.IoT Build 0:6ae2f7bca550 290 else
Azure.IoT Build 0:6ae2f7bca550 291 {
Azure.IoT Build 0:6ae2f7bca550 292 message_receiver_instance->on_message_received = on_message_received;
Azure.IoT Build 0:6ae2f7bca550 293 message_receiver_instance->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 294
Azure.IoT Build 0:6ae2f7bca550 295 result = 0;
Azure.IoT Build 0:6ae2f7bca550 296 }
Azure.IoT Build 0:6ae2f7bca550 297 }
Azure.IoT Build 0:6ae2f7bca550 298 else
Azure.IoT Build 0:6ae2f7bca550 299 {
Azure.IoT Build 0:6ae2f7bca550 300 result = 0;
Azure.IoT Build 0:6ae2f7bca550 301 }
Azure.IoT Build 0:6ae2f7bca550 302 }
Azure.IoT Build 0:6ae2f7bca550 303
Azure.IoT Build 0:6ae2f7bca550 304 return result;
Azure.IoT Build 0:6ae2f7bca550 305 }
Azure.IoT Build 0:6ae2f7bca550 306
Azure.IoT Build 0:6ae2f7bca550 307 int messagereceiver_close(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 308 {
Azure.IoT Build 0:6ae2f7bca550 309 int result;
Azure.IoT Build 0:6ae2f7bca550 310
Azure.IoT Build 0:6ae2f7bca550 311 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 312 {
AzureIoTClient 19:000ab4e6a2c1 313 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 314 }
Azure.IoT Build 0:6ae2f7bca550 315 else
Azure.IoT Build 0:6ae2f7bca550 316 {
Azure.IoT Build 0:6ae2f7bca550 317 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 318
Azure.IoT Build 0:6ae2f7bca550 319 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING) ||
Azure.IoT Build 0:6ae2f7bca550 320 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN))
Azure.IoT Build 0:6ae2f7bca550 321 {
Azure.IoT Build 0:6ae2f7bca550 322 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_CLOSING);
Azure.IoT Build 0:6ae2f7bca550 323
AzureIoTClient 12:b30dacf113f2 324 if (link_detach(message_receiver_instance->link, true) != 0)
Azure.IoT Build 0:6ae2f7bca550 325 {
AzureIoTClient 19:000ab4e6a2c1 326 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 327 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 328 }
Azure.IoT Build 0:6ae2f7bca550 329 else
Azure.IoT Build 0:6ae2f7bca550 330 {
Azure.IoT Build 0:6ae2f7bca550 331 result = 0;
Azure.IoT Build 0:6ae2f7bca550 332 }
Azure.IoT Build 0:6ae2f7bca550 333 }
Azure.IoT Build 0:6ae2f7bca550 334 else
Azure.IoT Build 0:6ae2f7bca550 335 {
Azure.IoT Build 0:6ae2f7bca550 336 result = 0;
Azure.IoT Build 0:6ae2f7bca550 337 }
Azure.IoT Build 0:6ae2f7bca550 338 }
Azure.IoT Build 0:6ae2f7bca550 339
Azure.IoT Build 0:6ae2f7bca550 340 return result;
Azure.IoT Build 0:6ae2f7bca550 341 }
AzureIoTClient 20:206846c14c80 342
AzureIoTClient 20:206846c14c80 343 int messagereceiver_get_link_name(MESSAGE_RECEIVER_HANDLE message_receiver, const char** link_name)
AzureIoTClient 20:206846c14c80 344 {
AzureIoTClient 20:206846c14c80 345 int result;
AzureIoTClient 20:206846c14c80 346
AzureIoTClient 20:206846c14c80 347 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 348 {
AzureIoTClient 20:206846c14c80 349 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 350 }
AzureIoTClient 20:206846c14c80 351 else
AzureIoTClient 20:206846c14c80 352 {
AzureIoTClient 20:206846c14c80 353 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
AzureIoTClient 20:206846c14c80 354 if (link_get_name(message_receiver_instance->link, link_name) != 0)
AzureIoTClient 20:206846c14c80 355 {
AzureIoTClient 20:206846c14c80 356 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 357 }
AzureIoTClient 20:206846c14c80 358 else
AzureIoTClient 20:206846c14c80 359 {
AzureIoTClient 20:206846c14c80 360 result = 0;
AzureIoTClient 20:206846c14c80 361 }
AzureIoTClient 20:206846c14c80 362 }
AzureIoTClient 20:206846c14c80 363
AzureIoTClient 20:206846c14c80 364 return result;
AzureIoTClient 20:206846c14c80 365 }
AzureIoTClient 20:206846c14c80 366
AzureIoTClient 20:206846c14c80 367 int messagereceiver_get_received_message_id(MESSAGE_RECEIVER_HANDLE message_receiver, delivery_number* message_id)
AzureIoTClient 20:206846c14c80 368 {
AzureIoTClient 20:206846c14c80 369 int result;
AzureIoTClient 20:206846c14c80 370
AzureIoTClient 20:206846c14c80 371 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 372 {
AzureIoTClient 20:206846c14c80 373 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 374 }
AzureIoTClient 20:206846c14c80 375 else
AzureIoTClient 20:206846c14c80 376 {
AzureIoTClient 20:206846c14c80 377 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
AzureIoTClient 20:206846c14c80 378 if (link_get_received_message_id(message_receiver_instance->link, message_id) != 0)
AzureIoTClient 20:206846c14c80 379 {
AzureIoTClient 20:206846c14c80 380 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 381 }
AzureIoTClient 20:206846c14c80 382 else
AzureIoTClient 20:206846c14c80 383 {
AzureIoTClient 20:206846c14c80 384 result = 0;
AzureIoTClient 20:206846c14c80 385 }
AzureIoTClient 20:206846c14c80 386 }
AzureIoTClient 20:206846c14c80 387
AzureIoTClient 20:206846c14c80 388 return result;
AzureIoTClient 20:206846c14c80 389 }
AzureIoTClient 20:206846c14c80 390
AzureIoTClient 20:206846c14c80 391 int messagereceiver_send_message_disposition(MESSAGE_RECEIVER_HANDLE message_receiver, const char* link_name, delivery_number message_number, AMQP_VALUE delivery_state)
AzureIoTClient 20:206846c14c80 392 {
AzureIoTClient 20:206846c14c80 393 int result;
AzureIoTClient 20:206846c14c80 394
AzureIoTClient 20:206846c14c80 395 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 396 {
AzureIoTClient 20:206846c14c80 397 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 398 }
AzureIoTClient 20:206846c14c80 399 else
AzureIoTClient 20:206846c14c80 400 {
AzureIoTClient 20:206846c14c80 401 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
AzureIoTClient 20:206846c14c80 402 if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_OPEN)
AzureIoTClient 20:206846c14c80 403 {
AzureIoTClient 20:206846c14c80 404 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 405 }
AzureIoTClient 20:206846c14c80 406 else
AzureIoTClient 20:206846c14c80 407 {
AzureIoTClient 20:206846c14c80 408 const char* my_name;
AzureIoTClient 20:206846c14c80 409 if (link_get_name(message_receiver_instance->link, &my_name) != 0)
AzureIoTClient 20:206846c14c80 410 {
AzureIoTClient 20:206846c14c80 411 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 412 }
AzureIoTClient 20:206846c14c80 413 else
AzureIoTClient 20:206846c14c80 414 {
AzureIoTClient 20:206846c14c80 415 if (strcmp(link_name, my_name) != 0)
AzureIoTClient 20:206846c14c80 416 {
AzureIoTClient 20:206846c14c80 417 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 418 }
AzureIoTClient 20:206846c14c80 419 else
AzureIoTClient 20:206846c14c80 420 {
AzureIoTClient 20:206846c14c80 421 if (link_send_disposition(message_receiver_instance->link, message_number, delivery_state) != 0)
AzureIoTClient 20:206846c14c80 422 {
AzureIoTClient 20:206846c14c80 423 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 424 }
AzureIoTClient 20:206846c14c80 425 else
AzureIoTClient 20:206846c14c80 426 {
AzureIoTClient 20:206846c14c80 427 result = 0;
AzureIoTClient 20:206846c14c80 428 }
AzureIoTClient 20:206846c14c80 429 }
AzureIoTClient 20:206846c14c80 430 }
AzureIoTClient 20:206846c14c80 431 }
AzureIoTClient 20:206846c14c80 432 }
AzureIoTClient 20:206846c14c80 433
AzureIoTClient 20:206846c14c80 434 return result;
AzureIoTClient 20:206846c14c80 435 }