A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Sat Oct 21 20:12:19 2017 +0000
Revision:
34:6be9c2058664
Parent:
28:add19eb7defa
Child:
40:f0ceafa8d570
1.1.26

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"
AzureIoTClient 27:d74f1cea23e1 8 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/message_receiver.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/amqp_definitions.h"
Azure.IoT Build 0:6ae2f7bca550 12
Azure.IoT Build 0:6ae2f7bca550 13 typedef struct MESSAGE_RECEIVER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 14 {
AzureIoTClient 28:add19eb7defa 15 LINK_HANDLE link;
AzureIoTClient 28:add19eb7defa 16 ON_MESSAGE_RECEIVED on_message_received;
AzureIoTClient 28:add19eb7defa 17 ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed;
AzureIoTClient 28:add19eb7defa 18 MESSAGE_RECEIVER_STATE message_receiver_state;
AzureIoTClient 28:add19eb7defa 19 const void* on_message_receiver_state_changed_context;
AzureIoTClient 28:add19eb7defa 20 const void* callback_context;
AzureIoTClient 28:add19eb7defa 21 MESSAGE_HANDLE decoded_message;
AzureIoTClient 28:add19eb7defa 22 bool decode_error;
Azure.IoT Build 0:6ae2f7bca550 23 } MESSAGE_RECEIVER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 24
AzureIoTClient 34:6be9c2058664 25 static void set_message_receiver_state(MESSAGE_RECEIVER_INSTANCE* message_receiver, MESSAGE_RECEIVER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 26 {
AzureIoTClient 34:6be9c2058664 27 MESSAGE_RECEIVER_STATE previous_state = message_receiver->message_receiver_state;
AzureIoTClient 34:6be9c2058664 28 message_receiver->message_receiver_state = new_state;
AzureIoTClient 34:6be9c2058664 29 if (message_receiver->on_message_receiver_state_changed != NULL)
AzureIoTClient 28:add19eb7defa 30 {
AzureIoTClient 34:6be9c2058664 31 message_receiver->on_message_receiver_state_changed(message_receiver->on_message_receiver_state_changed_context, new_state, previous_state);
AzureIoTClient 28:add19eb7defa 32 }
Azure.IoT Build 0:6ae2f7bca550 33 }
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 static void decode_message_value_callback(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 36 {
AzureIoTClient 34:6be9c2058664 37 MESSAGE_RECEIVER_INSTANCE* message_receiver = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 34:6be9c2058664 38 MESSAGE_HANDLE decoded_message = message_receiver->decoded_message;
AzureIoTClient 28:add19eb7defa 39 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 40
AzureIoTClient 28:add19eb7defa 41 if (is_application_properties_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 42 {
AzureIoTClient 28:add19eb7defa 43 if (message_set_application_properties(decoded_message, decoded_value) != 0)
AzureIoTClient 28:add19eb7defa 44 {
AzureIoTClient 34:6be9c2058664 45 LogError("Error setting application properties on received message");
AzureIoTClient 34:6be9c2058664 46 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 47 }
AzureIoTClient 28:add19eb7defa 48 }
AzureIoTClient 28:add19eb7defa 49 else if (is_properties_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 50 {
AzureIoTClient 28:add19eb7defa 51 PROPERTIES_HANDLE properties;
AzureIoTClient 28:add19eb7defa 52 if (amqpvalue_get_properties(decoded_value, &properties) != 0)
AzureIoTClient 28:add19eb7defa 53 {
AzureIoTClient 34:6be9c2058664 54 LogError("Error getting message properties");
AzureIoTClient 34:6be9c2058664 55 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 56 }
AzureIoTClient 28:add19eb7defa 57 else
AzureIoTClient 28:add19eb7defa 58 {
AzureIoTClient 28:add19eb7defa 59 if (message_set_properties(decoded_message, properties) != 0)
AzureIoTClient 28:add19eb7defa 60 {
AzureIoTClient 34:6be9c2058664 61 LogError("Error setting message properties on received message");
AzureIoTClient 34:6be9c2058664 62 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 63 }
Azure.IoT Build 0:6ae2f7bca550 64
AzureIoTClient 28:add19eb7defa 65 properties_destroy(properties);
AzureIoTClient 28:add19eb7defa 66 }
AzureIoTClient 28:add19eb7defa 67 }
AzureIoTClient 28:add19eb7defa 68 else if (is_delivery_annotations_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 69 {
AzureIoTClient 28:add19eb7defa 70 annotations delivery_annotations = amqpvalue_get_inplace_described_value(decoded_value);
AzureIoTClient 34:6be9c2058664 71 if (delivery_annotations == NULL)
AzureIoTClient 28:add19eb7defa 72 {
AzureIoTClient 34:6be9c2058664 73 LogError("Error getting delivery annotations");
AzureIoTClient 34:6be9c2058664 74 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 75 }
AzureIoTClient 34:6be9c2058664 76 else
AzureIoTClient 34:6be9c2058664 77 {
AzureIoTClient 34:6be9c2058664 78 if (message_set_delivery_annotations(decoded_message, delivery_annotations) != 0)
AzureIoTClient 34:6be9c2058664 79 {
AzureIoTClient 34:6be9c2058664 80 LogError("Error setting delivery annotations on received message");
AzureIoTClient 34:6be9c2058664 81 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 82 }
AzureIoTClient 28:add19eb7defa 83 }
AzureIoTClient 28:add19eb7defa 84 }
AzureIoTClient 28:add19eb7defa 85 else if (is_message_annotations_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 86 {
AzureIoTClient 28:add19eb7defa 87 annotations message_annotations = amqpvalue_get_inplace_described_value(decoded_value);
AzureIoTClient 34:6be9c2058664 88 if (message_annotations == NULL)
AzureIoTClient 28:add19eb7defa 89 {
AzureIoTClient 34:6be9c2058664 90 LogError("Error getting message annotations");
AzureIoTClient 34:6be9c2058664 91 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 92 }
AzureIoTClient 34:6be9c2058664 93 else
AzureIoTClient 34:6be9c2058664 94 {
AzureIoTClient 34:6be9c2058664 95 if (message_set_message_annotations(decoded_message, message_annotations) != 0)
AzureIoTClient 34:6be9c2058664 96 {
AzureIoTClient 34:6be9c2058664 97 LogError("Error setting message annotations on received message");
AzureIoTClient 34:6be9c2058664 98 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 99 }
AzureIoTClient 28:add19eb7defa 100 }
AzureIoTClient 28:add19eb7defa 101 }
AzureIoTClient 28:add19eb7defa 102 else if (is_header_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 103 {
AzureIoTClient 28:add19eb7defa 104 HEADER_HANDLE header;
AzureIoTClient 28:add19eb7defa 105 if (amqpvalue_get_header(decoded_value, &header) != 0)
AzureIoTClient 28:add19eb7defa 106 {
AzureIoTClient 34:6be9c2058664 107 LogError("Error getting message header");
AzureIoTClient 34:6be9c2058664 108 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 109 }
AzureIoTClient 28:add19eb7defa 110 else
AzureIoTClient 28:add19eb7defa 111 {
AzureIoTClient 28:add19eb7defa 112 if (message_set_header(decoded_message, header) != 0)
AzureIoTClient 28:add19eb7defa 113 {
AzureIoTClient 34:6be9c2058664 114 LogError("Error setting message header on received message");
AzureIoTClient 34:6be9c2058664 115 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 116 }
Azure.IoT Build 0:6ae2f7bca550 117
AzureIoTClient 28:add19eb7defa 118 header_destroy(header);
AzureIoTClient 28:add19eb7defa 119 }
AzureIoTClient 28:add19eb7defa 120 }
AzureIoTClient 28:add19eb7defa 121 else if (is_footer_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 122 {
AzureIoTClient 28:add19eb7defa 123 annotations footer = amqpvalue_get_inplace_described_value(decoded_value);
AzureIoTClient 34:6be9c2058664 124 if (footer == NULL)
AzureIoTClient 28:add19eb7defa 125 {
AzureIoTClient 34:6be9c2058664 126 LogError("Error getting message footer");
AzureIoTClient 34:6be9c2058664 127 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 128 }
AzureIoTClient 34:6be9c2058664 129 else
AzureIoTClient 34:6be9c2058664 130 {
AzureIoTClient 34:6be9c2058664 131 if (message_set_footer(decoded_message, footer) != 0)
AzureIoTClient 34:6be9c2058664 132 {
AzureIoTClient 34:6be9c2058664 133 LogError("Error setting message footer on received message");
AzureIoTClient 34:6be9c2058664 134 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 135 }
AzureIoTClient 28:add19eb7defa 136 }
AzureIoTClient 28:add19eb7defa 137 }
AzureIoTClient 28:add19eb7defa 138 else if (is_amqp_value_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 139 {
AzureIoTClient 28:add19eb7defa 140 MESSAGE_BODY_TYPE body_type;
AzureIoTClient 34:6be9c2058664 141 if (message_get_body_type(decoded_message, &body_type) != 0)
AzureIoTClient 28:add19eb7defa 142 {
AzureIoTClient 34:6be9c2058664 143 LogError("Error getting message body type");
AzureIoTClient 34:6be9c2058664 144 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 145 }
AzureIoTClient 28:add19eb7defa 146 else
AzureIoTClient 28:add19eb7defa 147 {
AzureIoTClient 34:6be9c2058664 148 if (body_type != MESSAGE_BODY_TYPE_NONE)
AzureIoTClient 34:6be9c2058664 149 {
AzureIoTClient 34:6be9c2058664 150 LogError("Body already set on received message");
AzureIoTClient 34:6be9c2058664 151 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 152 }
AzureIoTClient 34:6be9c2058664 153 else
AzureIoTClient 28:add19eb7defa 154 {
AzureIoTClient 34:6be9c2058664 155 AMQP_VALUE body_amqp_value = amqpvalue_get_inplace_described_value(decoded_value);
AzureIoTClient 34:6be9c2058664 156 if (body_amqp_value == NULL)
AzureIoTClient 34:6be9c2058664 157 {
AzureIoTClient 34:6be9c2058664 158 LogError("Error getting body AMQP value");
AzureIoTClient 34:6be9c2058664 159 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 160 }
AzureIoTClient 34:6be9c2058664 161 else
AzureIoTClient 34:6be9c2058664 162 {
AzureIoTClient 34:6be9c2058664 163 if (message_set_body_amqp_value(decoded_message, body_amqp_value) != 0)
AzureIoTClient 34:6be9c2058664 164 {
AzureIoTClient 34:6be9c2058664 165 LogError("Error setting body AMQP value on received message");
AzureIoTClient 34:6be9c2058664 166 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 167 }
AzureIoTClient 34:6be9c2058664 168 }
AzureIoTClient 28:add19eb7defa 169 }
AzureIoTClient 28:add19eb7defa 170 }
AzureIoTClient 28:add19eb7defa 171 }
AzureIoTClient 28:add19eb7defa 172 else if (is_data_type_by_descriptor(descriptor))
AzureIoTClient 28:add19eb7defa 173 {
AzureIoTClient 28:add19eb7defa 174 MESSAGE_BODY_TYPE body_type;
AzureIoTClient 34:6be9c2058664 175 if (message_get_body_type(decoded_message, &body_type) != 0)
AzureIoTClient 28:add19eb7defa 176 {
AzureIoTClient 34:6be9c2058664 177 LogError("Error getting message body type");
AzureIoTClient 34:6be9c2058664 178 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 179 }
AzureIoTClient 28:add19eb7defa 180 else
AzureIoTClient 28:add19eb7defa 181 {
AzureIoTClient 34:6be9c2058664 182 if ((body_type != MESSAGE_BODY_TYPE_NONE) &&
AzureIoTClient 34:6be9c2058664 183 (body_type != MESSAGE_BODY_TYPE_DATA))
AzureIoTClient 28:add19eb7defa 184 {
AzureIoTClient 34:6be9c2058664 185 LogError("Message body type already set to something different than AMQP DATA");
AzureIoTClient 34:6be9c2058664 186 message_receiver->decode_error = true;
AzureIoTClient 28:add19eb7defa 187 }
AzureIoTClient 28:add19eb7defa 188 else
AzureIoTClient 28:add19eb7defa 189 {
AzureIoTClient 34:6be9c2058664 190 AMQP_VALUE body_data_value = amqpvalue_get_inplace_described_value(decoded_value);
AzureIoTClient 34:6be9c2058664 191 if (body_data_value == NULL)
AzureIoTClient 34:6be9c2058664 192 {
AzureIoTClient 34:6be9c2058664 193 LogError("Error getting body DATA value");
AzureIoTClient 34:6be9c2058664 194 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 195 }
AzureIoTClient 34:6be9c2058664 196 else
AzureIoTClient 28:add19eb7defa 197 {
AzureIoTClient 34:6be9c2058664 198 data data_value;
AzureIoTClient 34:6be9c2058664 199 if (amqpvalue_get_data(body_data_value, &data_value) != 0)
AzureIoTClient 34:6be9c2058664 200 {
AzureIoTClient 34:6be9c2058664 201 LogError("Error getting body DATA AMQP value");
AzureIoTClient 34:6be9c2058664 202 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 203 }
AzureIoTClient 34:6be9c2058664 204 else
AzureIoTClient 34:6be9c2058664 205 {
AzureIoTClient 34:6be9c2058664 206 BINARY_DATA binary_data;
AzureIoTClient 34:6be9c2058664 207 binary_data.bytes = (const unsigned char*)data_value.bytes;
AzureIoTClient 34:6be9c2058664 208 binary_data.length = data_value.length;
AzureIoTClient 34:6be9c2058664 209 if (message_add_body_amqp_data(decoded_message, binary_data) != 0)
AzureIoTClient 34:6be9c2058664 210 {
AzureIoTClient 34:6be9c2058664 211 LogError("Error adding body DATA to received message");
AzureIoTClient 34:6be9c2058664 212 message_receiver->decode_error = true;
AzureIoTClient 34:6be9c2058664 213 }
AzureIoTClient 34:6be9c2058664 214 }
AzureIoTClient 28:add19eb7defa 215 }
AzureIoTClient 28:add19eb7defa 216 }
AzureIoTClient 28:add19eb7defa 217 }
AzureIoTClient 28:add19eb7defa 218 }
Azure.IoT Build 0:6ae2f7bca550 219 }
Azure.IoT Build 0:6ae2f7bca550 220
Azure.IoT Build 0:6ae2f7bca550 221 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 222 {
AzureIoTClient 28:add19eb7defa 223 AMQP_VALUE result = NULL;
AzureIoTClient 34:6be9c2058664 224 MESSAGE_RECEIVER_INSTANCE* message_receiver = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 225
AzureIoTClient 6:641a9672db08 226 (void)transfer;
AzureIoTClient 34:6be9c2058664 227 if (message_receiver->on_message_received != NULL)
AzureIoTClient 28:add19eb7defa 228 {
AzureIoTClient 28:add19eb7defa 229 MESSAGE_HANDLE message = message_create();
AzureIoTClient 28:add19eb7defa 230 if (message == NULL)
AzureIoTClient 28:add19eb7defa 231 {
AzureIoTClient 34:6be9c2058664 232 LogError("Cannot create message");
AzureIoTClient 34:6be9c2058664 233 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 234 }
AzureIoTClient 28:add19eb7defa 235 else
AzureIoTClient 28:add19eb7defa 236 {
AzureIoTClient 34:6be9c2058664 237 AMQPVALUE_DECODER_HANDLE amqpvalue_decoder = amqpvalue_decoder_create(decode_message_value_callback, message_receiver);
AzureIoTClient 28:add19eb7defa 238 if (amqpvalue_decoder == NULL)
AzureIoTClient 28:add19eb7defa 239 {
AzureIoTClient 34:6be9c2058664 240 LogError("Cannot create AMQP value decoder");
AzureIoTClient 34:6be9c2058664 241 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 242 }
AzureIoTClient 28:add19eb7defa 243 else
AzureIoTClient 28:add19eb7defa 244 {
AzureIoTClient 34:6be9c2058664 245 message_receiver->decoded_message = message;
AzureIoTClient 34:6be9c2058664 246 message_receiver->decode_error = false;
AzureIoTClient 28:add19eb7defa 247 if (amqpvalue_decode_bytes(amqpvalue_decoder, payload_bytes, payload_size) != 0)
AzureIoTClient 28:add19eb7defa 248 {
AzureIoTClient 34:6be9c2058664 249 LogError("Cannot decode bytes");
AzureIoTClient 34:6be9c2058664 250 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 251 }
AzureIoTClient 28:add19eb7defa 252 else
AzureIoTClient 28:add19eb7defa 253 {
AzureIoTClient 34:6be9c2058664 254 if (message_receiver->decode_error)
AzureIoTClient 28:add19eb7defa 255 {
AzureIoTClient 34:6be9c2058664 256 LogError("Error decoding message");
AzureIoTClient 34:6be9c2058664 257 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 258 }
AzureIoTClient 28:add19eb7defa 259 else
AzureIoTClient 28:add19eb7defa 260 {
AzureIoTClient 34:6be9c2058664 261 result = message_receiver->on_message_received(message_receiver->callback_context, message);
AzureIoTClient 28:add19eb7defa 262 }
AzureIoTClient 28:add19eb7defa 263 }
Azure.IoT Build 0:6ae2f7bca550 264
AzureIoTClient 28:add19eb7defa 265 amqpvalue_decoder_destroy(amqpvalue_decoder);
AzureIoTClient 28:add19eb7defa 266 }
Azure.IoT Build 0:6ae2f7bca550 267
AzureIoTClient 28:add19eb7defa 268 message_destroy(message);
AzureIoTClient 28:add19eb7defa 269 }
AzureIoTClient 28:add19eb7defa 270 }
Azure.IoT Build 0:6ae2f7bca550 271
AzureIoTClient 28:add19eb7defa 272 return result;
Azure.IoT Build 0:6ae2f7bca550 273 }
Azure.IoT Build 0:6ae2f7bca550 274
Azure.IoT Build 0:6ae2f7bca550 275 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 276 {
AzureIoTClient 34:6be9c2058664 277 MESSAGE_RECEIVER_INSTANCE* message_receiver = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 278 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 279
AzureIoTClient 28:add19eb7defa 280 switch (new_link_state)
AzureIoTClient 28:add19eb7defa 281 {
AzureIoTClient 17:923575db8b2d 282 default:
AzureIoTClient 17:923575db8b2d 283 break;
AzureIoTClient 17:923575db8b2d 284
AzureIoTClient 28:add19eb7defa 285 case LINK_STATE_ATTACHED:
AzureIoTClient 34:6be9c2058664 286 if (message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING)
AzureIoTClient 28:add19eb7defa 287 {
AzureIoTClient 34:6be9c2058664 288 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_OPEN);
AzureIoTClient 28:add19eb7defa 289 }
AzureIoTClient 28:add19eb7defa 290 break;
AzureIoTClient 28:add19eb7defa 291 case LINK_STATE_DETACHED:
AzureIoTClient 34:6be9c2058664 292 if ((message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN) ||
AzureIoTClient 34:6be9c2058664 293 (message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 294 {
Azure.IoT Build 0:6ae2f7bca550 295 /* User initiated transition, we should be good */
AzureIoTClient 34:6be9c2058664 296 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 297 }
AzureIoTClient 34:6be9c2058664 298 else if (message_receiver->message_receiver_state != MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 299 {
Azure.IoT Build 0:6ae2f7bca550 300 /* Any other transition must be an error */
AzureIoTClient 34:6be9c2058664 301 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 302 }
Azure.IoT Build 0:6ae2f7bca550 303 break;
AzureIoTClient 1:eab586236bfe 304 case LINK_STATE_ERROR:
AzureIoTClient 34:6be9c2058664 305 if (message_receiver->message_receiver_state != MESSAGE_RECEIVER_STATE_ERROR)
AzureIoTClient 1:eab586236bfe 306 {
AzureIoTClient 34:6be9c2058664 307 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 1:eab586236bfe 308 }
AzureIoTClient 1:eab586236bfe 309 break;
AzureIoTClient 28:add19eb7defa 310 }
Azure.IoT Build 0:6ae2f7bca550 311 }
Azure.IoT Build 0:6ae2f7bca550 312
Azure.IoT Build 0:6ae2f7bca550 313 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 314 {
AzureIoTClient 34:6be9c2058664 315 MESSAGE_RECEIVER_INSTANCE* message_receiver = (MESSAGE_RECEIVER_INSTANCE*)malloc(sizeof(MESSAGE_RECEIVER_INSTANCE));
AzureIoTClient 34:6be9c2058664 316 if (message_receiver == NULL)
AzureIoTClient 28:add19eb7defa 317 {
AzureIoTClient 34:6be9c2058664 318 LogError("Error creating message receiver");
AzureIoTClient 34:6be9c2058664 319 }
AzureIoTClient 34:6be9c2058664 320 else
AzureIoTClient 34:6be9c2058664 321 {
AzureIoTClient 34:6be9c2058664 322 message_receiver->link = link;
AzureIoTClient 34:6be9c2058664 323 message_receiver->on_message_receiver_state_changed = on_message_receiver_state_changed;
AzureIoTClient 34:6be9c2058664 324 message_receiver->on_message_receiver_state_changed_context = context;
AzureIoTClient 34:6be9c2058664 325 message_receiver->message_receiver_state = MESSAGE_RECEIVER_STATE_IDLE;
AzureIoTClient 28:add19eb7defa 326 }
Azure.IoT Build 0:6ae2f7bca550 327
AzureIoTClient 34:6be9c2058664 328 return message_receiver;
Azure.IoT Build 0:6ae2f7bca550 329 }
Azure.IoT Build 0:6ae2f7bca550 330
Azure.IoT Build 0:6ae2f7bca550 331 void messagereceiver_destroy(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 332 {
AzureIoTClient 34:6be9c2058664 333 if (message_receiver == NULL)
AzureIoTClient 34:6be9c2058664 334 {
AzureIoTClient 34:6be9c2058664 335 LogError("NULL message_receiver");
AzureIoTClient 34:6be9c2058664 336 }
AzureIoTClient 34:6be9c2058664 337 else
AzureIoTClient 28:add19eb7defa 338 {
AzureIoTClient 28:add19eb7defa 339 (void)messagereceiver_close(message_receiver);
AzureIoTClient 28:add19eb7defa 340 free(message_receiver);
AzureIoTClient 28:add19eb7defa 341 }
Azure.IoT Build 0:6ae2f7bca550 342 }
Azure.IoT Build 0:6ae2f7bca550 343
AzureIoTClient 23:1111ee8bcba4 344 int messagereceiver_open(MESSAGE_RECEIVER_HANDLE message_receiver, ON_MESSAGE_RECEIVED on_message_received, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 345 {
AzureIoTClient 28:add19eb7defa 346 int result;
Azure.IoT Build 0:6ae2f7bca550 347
AzureIoTClient 28:add19eb7defa 348 if (message_receiver == NULL)
AzureIoTClient 28:add19eb7defa 349 {
AzureIoTClient 34:6be9c2058664 350 LogError("NULL message_receiver");
AzureIoTClient 28:add19eb7defa 351 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 352 }
AzureIoTClient 28:add19eb7defa 353 else
AzureIoTClient 28:add19eb7defa 354 {
AzureIoTClient 34:6be9c2058664 355 if (message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_IDLE)
AzureIoTClient 28:add19eb7defa 356 {
AzureIoTClient 34:6be9c2058664 357 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_OPENING);
AzureIoTClient 34:6be9c2058664 358 if (link_attach(message_receiver->link, on_transfer_received, on_link_state_changed, NULL, message_receiver) != 0)
AzureIoTClient 28:add19eb7defa 359 {
AzureIoTClient 34:6be9c2058664 360 LogError("Link attach failed");
AzureIoTClient 28:add19eb7defa 361 result = __FAILURE__;
AzureIoTClient 34:6be9c2058664 362 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 363 }
AzureIoTClient 28:add19eb7defa 364 else
AzureIoTClient 28:add19eb7defa 365 {
AzureIoTClient 34:6be9c2058664 366 message_receiver->on_message_received = on_message_received;
AzureIoTClient 34:6be9c2058664 367 message_receiver->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 368
AzureIoTClient 28:add19eb7defa 369 result = 0;
AzureIoTClient 28:add19eb7defa 370 }
AzureIoTClient 28:add19eb7defa 371 }
AzureIoTClient 28:add19eb7defa 372 else
AzureIoTClient 28:add19eb7defa 373 {
AzureIoTClient 28:add19eb7defa 374 result = 0;
AzureIoTClient 28:add19eb7defa 375 }
AzureIoTClient 28:add19eb7defa 376 }
Azure.IoT Build 0:6ae2f7bca550 377
AzureIoTClient 28:add19eb7defa 378 return result;
Azure.IoT Build 0:6ae2f7bca550 379 }
Azure.IoT Build 0:6ae2f7bca550 380
Azure.IoT Build 0:6ae2f7bca550 381 int messagereceiver_close(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 382 {
AzureIoTClient 28:add19eb7defa 383 int result;
Azure.IoT Build 0:6ae2f7bca550 384
AzureIoTClient 28:add19eb7defa 385 if (message_receiver == NULL)
AzureIoTClient 28:add19eb7defa 386 {
AzureIoTClient 34:6be9c2058664 387 LogError("NULL message_receiver");
AzureIoTClient 28:add19eb7defa 388 result = __FAILURE__;
AzureIoTClient 28:add19eb7defa 389 }
AzureIoTClient 28:add19eb7defa 390 else
AzureIoTClient 28:add19eb7defa 391 {
AzureIoTClient 34:6be9c2058664 392 if ((message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING) ||
AzureIoTClient 34:6be9c2058664 393 (message_receiver->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN))
AzureIoTClient 28:add19eb7defa 394 {
AzureIoTClient 34:6be9c2058664 395 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_CLOSING);
Azure.IoT Build 0:6ae2f7bca550 396
AzureIoTClient 34:6be9c2058664 397 if (link_detach(message_receiver->link, true) != 0)
AzureIoTClient 28:add19eb7defa 398 {
AzureIoTClient 34:6be9c2058664 399 LogError("link detach failed");
AzureIoTClient 28:add19eb7defa 400 result = __FAILURE__;
AzureIoTClient 34:6be9c2058664 401 set_message_receiver_state(message_receiver, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 28:add19eb7defa 402 }
AzureIoTClient 28:add19eb7defa 403 else
AzureIoTClient 28:add19eb7defa 404 {
AzureIoTClient 28:add19eb7defa 405 result = 0;
AzureIoTClient 28:add19eb7defa 406 }
AzureIoTClient 28:add19eb7defa 407 }
AzureIoTClient 28:add19eb7defa 408 else
AzureIoTClient 28:add19eb7defa 409 {
AzureIoTClient 28:add19eb7defa 410 result = 0;
AzureIoTClient 28:add19eb7defa 411 }
AzureIoTClient 28:add19eb7defa 412 }
Azure.IoT Build 0:6ae2f7bca550 413
AzureIoTClient 28:add19eb7defa 414 return result;
Azure.IoT Build 0:6ae2f7bca550 415 }
AzureIoTClient 20:206846c14c80 416
AzureIoTClient 20:206846c14c80 417 int messagereceiver_get_link_name(MESSAGE_RECEIVER_HANDLE message_receiver, const char** link_name)
AzureIoTClient 20:206846c14c80 418 {
AzureIoTClient 20:206846c14c80 419 int result;
AzureIoTClient 20:206846c14c80 420
AzureIoTClient 20:206846c14c80 421 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 422 {
AzureIoTClient 34:6be9c2058664 423 LogError("NULL message_receiver");
AzureIoTClient 20:206846c14c80 424 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 425 }
AzureIoTClient 20:206846c14c80 426 else
AzureIoTClient 20:206846c14c80 427 {
AzureIoTClient 34:6be9c2058664 428 if (link_get_name(message_receiver->link, link_name) != 0)
AzureIoTClient 20:206846c14c80 429 {
AzureIoTClient 34:6be9c2058664 430 LogError("Getting link name failed");
AzureIoTClient 20:206846c14c80 431 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 432 }
AzureIoTClient 20:206846c14c80 433 else
AzureIoTClient 20:206846c14c80 434 {
AzureIoTClient 20:206846c14c80 435 result = 0;
AzureIoTClient 20:206846c14c80 436 }
AzureIoTClient 20:206846c14c80 437 }
AzureIoTClient 20:206846c14c80 438
AzureIoTClient 20:206846c14c80 439 return result;
AzureIoTClient 20:206846c14c80 440 }
AzureIoTClient 20:206846c14c80 441
AzureIoTClient 20:206846c14c80 442 int messagereceiver_get_received_message_id(MESSAGE_RECEIVER_HANDLE message_receiver, delivery_number* message_id)
AzureIoTClient 20:206846c14c80 443 {
AzureIoTClient 20:206846c14c80 444 int result;
AzureIoTClient 20:206846c14c80 445
AzureIoTClient 20:206846c14c80 446 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 447 {
AzureIoTClient 34:6be9c2058664 448 LogError("NULL message_receiver");
AzureIoTClient 20:206846c14c80 449 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 450 }
AzureIoTClient 20:206846c14c80 451 else
AzureIoTClient 20:206846c14c80 452 {
AzureIoTClient 34:6be9c2058664 453 if (link_get_received_message_id(message_receiver->link, message_id) != 0)
AzureIoTClient 20:206846c14c80 454 {
AzureIoTClient 34:6be9c2058664 455 LogError("Failed getting received message Id");
AzureIoTClient 20:206846c14c80 456 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 457 }
AzureIoTClient 20:206846c14c80 458 else
AzureIoTClient 20:206846c14c80 459 {
AzureIoTClient 20:206846c14c80 460 result = 0;
AzureIoTClient 20:206846c14c80 461 }
AzureIoTClient 20:206846c14c80 462 }
AzureIoTClient 20:206846c14c80 463
AzureIoTClient 20:206846c14c80 464 return result;
AzureIoTClient 20:206846c14c80 465 }
AzureIoTClient 20:206846c14c80 466
AzureIoTClient 20:206846c14c80 467 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 468 {
AzureIoTClient 20:206846c14c80 469 int result;
AzureIoTClient 20:206846c14c80 470
AzureIoTClient 20:206846c14c80 471 if (message_receiver == NULL)
AzureIoTClient 20:206846c14c80 472 {
AzureIoTClient 34:6be9c2058664 473 LogError("NULL message_receiver");
AzureIoTClient 20:206846c14c80 474 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 475 }
AzureIoTClient 20:206846c14c80 476 else
AzureIoTClient 20:206846c14c80 477 {
AzureIoTClient 34:6be9c2058664 478 if (message_receiver->message_receiver_state != MESSAGE_RECEIVER_STATE_OPEN)
AzureIoTClient 20:206846c14c80 479 {
AzureIoTClient 34:6be9c2058664 480 LogError("Message received not open");
AzureIoTClient 20:206846c14c80 481 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 482 }
AzureIoTClient 20:206846c14c80 483 else
AzureIoTClient 20:206846c14c80 484 {
AzureIoTClient 20:206846c14c80 485 const char* my_name;
AzureIoTClient 34:6be9c2058664 486 if (link_get_name(message_receiver->link, &my_name) != 0)
AzureIoTClient 20:206846c14c80 487 {
AzureIoTClient 34:6be9c2058664 488 LogError("Failed getting link name");
AzureIoTClient 20:206846c14c80 489 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 490 }
AzureIoTClient 20:206846c14c80 491 else
AzureIoTClient 20:206846c14c80 492 {
AzureIoTClient 20:206846c14c80 493 if (strcmp(link_name, my_name) != 0)
AzureIoTClient 20:206846c14c80 494 {
AzureIoTClient 34:6be9c2058664 495 LogError("Link name does not match");
AzureIoTClient 20:206846c14c80 496 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 497 }
AzureIoTClient 20:206846c14c80 498 else
AzureIoTClient 20:206846c14c80 499 {
AzureIoTClient 34:6be9c2058664 500 if (link_send_disposition(message_receiver->link, message_number, delivery_state) != 0)
AzureIoTClient 20:206846c14c80 501 {
AzureIoTClient 34:6be9c2058664 502 LogError("Seding disposition failed");
AzureIoTClient 20:206846c14c80 503 result = __FAILURE__;
AzureIoTClient 20:206846c14c80 504 }
AzureIoTClient 20:206846c14c80 505 else
AzureIoTClient 20:206846c14c80 506 {
AzureIoTClient 20:206846c14c80 507 result = 0;
AzureIoTClient 20:206846c14c80 508 }
AzureIoTClient 20:206846c14c80 509 }
AzureIoTClient 20:206846c14c80 510 }
AzureIoTClient 20:206846c14c80 511 }
AzureIoTClient 20:206846c14c80 512 }
AzureIoTClient 20:206846c14c80 513
AzureIoTClient 20:206846c14c80 514 return result;
AzureIoTClient 20:206846c14c80 515 }
AzureIoTClient 23:1111ee8bcba4 516
AzureIoTClient 23:1111ee8bcba4 517 void messagereceiver_set_trace(MESSAGE_RECEIVER_HANDLE message_receiver, bool trace_on)
AzureIoTClient 23:1111ee8bcba4 518 {
AzureIoTClient 34:6be9c2058664 519 if (message_receiver == NULL)
AzureIoTClient 34:6be9c2058664 520 {
AzureIoTClient 34:6be9c2058664 521 LogError("NULL message_receiver");
AzureIoTClient 34:6be9c2058664 522 }
AzureIoTClient 34:6be9c2058664 523 else
AzureIoTClient 34:6be9c2058664 524 {
AzureIoTClient 34:6be9c2058664 525 /* No tracing is yet implemented for message receiver */
AzureIoTClient 34:6be9c2058664 526 (void)trace_on;
AzureIoTClient 34:6be9c2058664 527 }
AzureIoTClient 23:1111ee8bcba4 528 }