A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:00:58 2017 -0800
Revision:
19:000ab4e6a2c1
Parent:
17:923575db8b2d
Child:
20:206846c14c80
1.1.8

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 19:000ab4e6a2c1 5 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 0:6ae2f7bca550 6 #include "azure_uamqp_c/message_receiver.h"
Azure.IoT Build 0:6ae2f7bca550 7 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 8 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/amqp_definitions.h"
Azure.IoT Build 0:6ae2f7bca550 10
Azure.IoT Build 0:6ae2f7bca550 11 typedef struct MESSAGE_RECEIVER_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 12 {
Azure.IoT Build 0:6ae2f7bca550 13 LINK_HANDLE link;
Azure.IoT Build 0:6ae2f7bca550 14 ON_MESSAGE_RECEIVED on_message_received;
Azure.IoT Build 0:6ae2f7bca550 15 ON_MESSAGE_RECEIVER_STATE_CHANGED on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 16 MESSAGE_RECEIVER_STATE message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 17 const void* on_message_receiver_state_changed_context;
Azure.IoT Build 0:6ae2f7bca550 18 const void* callback_context;
Azure.IoT Build 0:6ae2f7bca550 19 MESSAGE_HANDLE decoded_message;
Azure.IoT Build 0:6ae2f7bca550 20 bool decode_error;
Azure.IoT Build 0:6ae2f7bca550 21 } MESSAGE_RECEIVER_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 22
Azure.IoT Build 0:6ae2f7bca550 23 static void set_message_receiver_state(MESSAGE_RECEIVER_INSTANCE* message_receiver_instance, MESSAGE_RECEIVER_STATE new_state)
Azure.IoT Build 0:6ae2f7bca550 24 {
Azure.IoT Build 0:6ae2f7bca550 25 MESSAGE_RECEIVER_STATE previous_state = message_receiver_instance->message_receiver_state;
Azure.IoT Build 0:6ae2f7bca550 26 message_receiver_instance->message_receiver_state = new_state;
Azure.IoT Build 0:6ae2f7bca550 27 if (message_receiver_instance->on_message_receiver_state_changed != NULL)
Azure.IoT Build 0:6ae2f7bca550 28 {
Azure.IoT Build 0:6ae2f7bca550 29 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 30 }
Azure.IoT Build 0:6ae2f7bca550 31 }
Azure.IoT Build 0:6ae2f7bca550 32
Azure.IoT Build 0:6ae2f7bca550 33 static void decode_message_value_callback(void* context, AMQP_VALUE decoded_value)
Azure.IoT Build 0:6ae2f7bca550 34 {
Azure.IoT Build 0:6ae2f7bca550 35 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
Azure.IoT Build 0:6ae2f7bca550 36 MESSAGE_HANDLE decoded_message = message_receiver_instance->decoded_message;
Azure.IoT Build 0:6ae2f7bca550 37 AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 38
Azure.IoT Build 0:6ae2f7bca550 39 if (is_application_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 40 {
Azure.IoT Build 0:6ae2f7bca550 41 if (message_set_application_properties(decoded_message, decoded_value) != 0)
Azure.IoT Build 0:6ae2f7bca550 42 {
Azure.IoT Build 0:6ae2f7bca550 43 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 44 }
Azure.IoT Build 0:6ae2f7bca550 45 }
Azure.IoT Build 0:6ae2f7bca550 46 else if (is_properties_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 47 {
Azure.IoT Build 0:6ae2f7bca550 48 PROPERTIES_HANDLE properties;
Azure.IoT Build 0:6ae2f7bca550 49 if (amqpvalue_get_properties(decoded_value, &properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 50 {
Azure.IoT Build 0:6ae2f7bca550 51 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 52 }
Azure.IoT Build 0:6ae2f7bca550 53 else
Azure.IoT Build 0:6ae2f7bca550 54 {
Azure.IoT Build 0:6ae2f7bca550 55 if (message_set_properties(decoded_message, properties) != 0)
Azure.IoT Build 0:6ae2f7bca550 56 {
Azure.IoT Build 0:6ae2f7bca550 57 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 58 }
Azure.IoT Build 0:6ae2f7bca550 59
Azure.IoT Build 0:6ae2f7bca550 60 properties_destroy(properties);
Azure.IoT Build 0:6ae2f7bca550 61 }
Azure.IoT Build 0:6ae2f7bca550 62 }
Azure.IoT Build 0:6ae2f7bca550 63 else if (is_delivery_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 64 {
Azure.IoT Build 0:6ae2f7bca550 65 annotations delivery_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 66 if ((delivery_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 67 (message_set_delivery_annotations(decoded_message, delivery_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 68 {
Azure.IoT Build 0:6ae2f7bca550 69 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 70 }
Azure.IoT Build 0:6ae2f7bca550 71 }
Azure.IoT Build 0:6ae2f7bca550 72 else if (is_message_annotations_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 73 {
Azure.IoT Build 0:6ae2f7bca550 74 annotations message_annotations = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 75 if ((message_annotations == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 76 (message_set_message_annotations(decoded_message, message_annotations) != 0))
Azure.IoT Build 0:6ae2f7bca550 77 {
Azure.IoT Build 0:6ae2f7bca550 78 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 79 }
Azure.IoT Build 0:6ae2f7bca550 80 }
Azure.IoT Build 0:6ae2f7bca550 81 else if (is_header_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 82 {
Azure.IoT Build 0:6ae2f7bca550 83 HEADER_HANDLE header;
Azure.IoT Build 0:6ae2f7bca550 84 if (amqpvalue_get_header(decoded_value, &header) != 0)
Azure.IoT Build 0:6ae2f7bca550 85 {
Azure.IoT Build 0:6ae2f7bca550 86 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 87 }
Azure.IoT Build 0:6ae2f7bca550 88 else
Azure.IoT Build 0:6ae2f7bca550 89 {
Azure.IoT Build 0:6ae2f7bca550 90 if (message_set_header(decoded_message, header) != 0)
Azure.IoT Build 0:6ae2f7bca550 91 {
Azure.IoT Build 0:6ae2f7bca550 92 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 93 }
Azure.IoT Build 0:6ae2f7bca550 94
Azure.IoT Build 0:6ae2f7bca550 95 header_destroy(header);
Azure.IoT Build 0:6ae2f7bca550 96 }
Azure.IoT Build 0:6ae2f7bca550 97 }
Azure.IoT Build 0:6ae2f7bca550 98 else if (is_footer_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 99 {
Azure.IoT Build 0:6ae2f7bca550 100 annotations footer = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 101 if ((footer == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 102 (message_set_footer(decoded_message, footer) != 0))
Azure.IoT Build 0:6ae2f7bca550 103 {
Azure.IoT Build 0:6ae2f7bca550 104 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 105 }
Azure.IoT Build 0:6ae2f7bca550 106 }
Azure.IoT Build 0:6ae2f7bca550 107 else if (is_amqp_value_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 108 {
Azure.IoT Build 0:6ae2f7bca550 109 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 110 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 111 if (body_type != MESSAGE_BODY_TYPE_NONE)
Azure.IoT Build 0:6ae2f7bca550 112 {
Azure.IoT Build 0:6ae2f7bca550 113 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 114 }
Azure.IoT Build 0:6ae2f7bca550 115 else
Azure.IoT Build 0:6ae2f7bca550 116 {
Azure.IoT Build 0:6ae2f7bca550 117 AMQP_VALUE body_amqp_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 118 if ((body_amqp_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 119 (message_set_body_amqp_value(decoded_message, body_amqp_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 120 {
Azure.IoT Build 0:6ae2f7bca550 121 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 122 }
Azure.IoT Build 0:6ae2f7bca550 123 }
Azure.IoT Build 0:6ae2f7bca550 124 }
Azure.IoT Build 0:6ae2f7bca550 125 else if (is_data_type_by_descriptor(descriptor))
Azure.IoT Build 0:6ae2f7bca550 126 {
Azure.IoT Build 0:6ae2f7bca550 127 MESSAGE_BODY_TYPE body_type;
Azure.IoT Build 0:6ae2f7bca550 128 message_get_body_type(decoded_message, &body_type);
Azure.IoT Build 0:6ae2f7bca550 129 if ((body_type != MESSAGE_BODY_TYPE_NONE) &&
Azure.IoT Build 0:6ae2f7bca550 130 (body_type != MESSAGE_BODY_TYPE_DATA))
Azure.IoT Build 0:6ae2f7bca550 131 {
Azure.IoT Build 0:6ae2f7bca550 132 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 133 }
Azure.IoT Build 0:6ae2f7bca550 134 else
Azure.IoT Build 0:6ae2f7bca550 135 {
Azure.IoT Build 0:6ae2f7bca550 136 AMQP_VALUE body_data_value = amqpvalue_get_inplace_described_value(decoded_value);
Azure.IoT Build 0:6ae2f7bca550 137 data data_value;
Azure.IoT Build 0:6ae2f7bca550 138
Azure.IoT Build 0:6ae2f7bca550 139 if ((body_data_value == NULL) ||
Azure.IoT Build 0:6ae2f7bca550 140 (amqpvalue_get_data(body_data_value, &data_value) != 0))
Azure.IoT Build 0:6ae2f7bca550 141 {
Azure.IoT Build 0:6ae2f7bca550 142 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 143 }
Azure.IoT Build 0:6ae2f7bca550 144 else
Azure.IoT Build 0:6ae2f7bca550 145 {
AzureIoTClient 6:641a9672db08 146 BINARY_DATA binary_data;
AzureIoTClient 6:641a9672db08 147 binary_data.bytes = data_value.bytes;
AzureIoTClient 6:641a9672db08 148 binary_data.length = data_value.length;
Azure.IoT Build 0:6ae2f7bca550 149 if (message_add_body_amqp_data(decoded_message, binary_data) != 0)
Azure.IoT Build 0:6ae2f7bca550 150 {
Azure.IoT Build 0:6ae2f7bca550 151 message_receiver_instance->decode_error = true;
Azure.IoT Build 0:6ae2f7bca550 152 }
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 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 159 {
Azure.IoT Build 0:6ae2f7bca550 160 AMQP_VALUE result = NULL;
Azure.IoT Build 0:6ae2f7bca550 161
AzureIoTClient 1:eab586236bfe 162 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 163 (void)transfer;
AzureIoTClient 1:eab586236bfe 164 if (message_receiver_instance->on_message_received != NULL)
AzureIoTClient 1:eab586236bfe 165 {
AzureIoTClient 1:eab586236bfe 166 MESSAGE_HANDLE message = message_create();
Azure.IoT Build 0:6ae2f7bca550 167 if (message == NULL)
Azure.IoT Build 0:6ae2f7bca550 168 {
Azure.IoT Build 0:6ae2f7bca550 169 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 170 }
Azure.IoT Build 0:6ae2f7bca550 171 else
Azure.IoT Build 0:6ae2f7bca550 172 {
Azure.IoT Build 0:6ae2f7bca550 173 AMQPVALUE_DECODER_HANDLE amqpvalue_decoder = amqpvalue_decoder_create(decode_message_value_callback, message_receiver_instance);
Azure.IoT Build 0:6ae2f7bca550 174 if (amqpvalue_decoder == NULL)
Azure.IoT Build 0:6ae2f7bca550 175 {
Azure.IoT Build 0:6ae2f7bca550 176 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 177 }
Azure.IoT Build 0:6ae2f7bca550 178 else
Azure.IoT Build 0:6ae2f7bca550 179 {
Azure.IoT Build 0:6ae2f7bca550 180 message_receiver_instance->decoded_message = message;
Azure.IoT Build 0:6ae2f7bca550 181 message_receiver_instance->decode_error = false;
Azure.IoT Build 0:6ae2f7bca550 182 if (amqpvalue_decode_bytes(amqpvalue_decoder, payload_bytes, payload_size) != 0)
Azure.IoT Build 0:6ae2f7bca550 183 {
Azure.IoT Build 0:6ae2f7bca550 184 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 185 }
Azure.IoT Build 0:6ae2f7bca550 186 else
Azure.IoT Build 0:6ae2f7bca550 187 {
Azure.IoT Build 0:6ae2f7bca550 188 if (message_receiver_instance->decode_error)
Azure.IoT Build 0:6ae2f7bca550 189 {
Azure.IoT Build 0:6ae2f7bca550 190 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 191 }
Azure.IoT Build 0:6ae2f7bca550 192 else
Azure.IoT Build 0:6ae2f7bca550 193 {
Azure.IoT Build 0:6ae2f7bca550 194 result = message_receiver_instance->on_message_received(message_receiver_instance->callback_context, message);
Azure.IoT Build 0:6ae2f7bca550 195 }
Azure.IoT Build 0:6ae2f7bca550 196 }
Azure.IoT Build 0:6ae2f7bca550 197
Azure.IoT Build 0:6ae2f7bca550 198 amqpvalue_decoder_destroy(amqpvalue_decoder);
Azure.IoT Build 0:6ae2f7bca550 199 }
Azure.IoT Build 0:6ae2f7bca550 200
Azure.IoT Build 0:6ae2f7bca550 201 message_destroy(message);
Azure.IoT Build 0:6ae2f7bca550 202 }
AzureIoTClient 1:eab586236bfe 203 }
Azure.IoT Build 0:6ae2f7bca550 204
Azure.IoT Build 0:6ae2f7bca550 205 return result;
Azure.IoT Build 0:6ae2f7bca550 206 }
Azure.IoT Build 0:6ae2f7bca550 207
Azure.IoT Build 0:6ae2f7bca550 208 static void on_link_state_changed(void* context, LINK_STATE new_link_state, LINK_STATE previous_link_state)
Azure.IoT Build 0:6ae2f7bca550 209 {
Azure.IoT Build 0:6ae2f7bca550 210 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)context;
AzureIoTClient 6:641a9672db08 211 (void)previous_link_state;
Azure.IoT Build 0:6ae2f7bca550 212
Azure.IoT Build 0:6ae2f7bca550 213 switch (new_link_state)
Azure.IoT Build 0:6ae2f7bca550 214 {
AzureIoTClient 17:923575db8b2d 215 default:
AzureIoTClient 17:923575db8b2d 216 break;
AzureIoTClient 17:923575db8b2d 217
Azure.IoT Build 0:6ae2f7bca550 218 case LINK_STATE_ATTACHED:
Azure.IoT Build 0:6ae2f7bca550 219 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING)
Azure.IoT Build 0:6ae2f7bca550 220 {
Azure.IoT Build 0:6ae2f7bca550 221 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPEN);
Azure.IoT Build 0:6ae2f7bca550 222 }
Azure.IoT Build 0:6ae2f7bca550 223 break;
Azure.IoT Build 0:6ae2f7bca550 224 case LINK_STATE_DETACHED:
Azure.IoT Build 0:6ae2f7bca550 225 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN) ||
Azure.IoT Build 0:6ae2f7bca550 226 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_CLOSING))
Azure.IoT Build 0:6ae2f7bca550 227 {
Azure.IoT Build 0:6ae2f7bca550 228 /* User initiated transition, we should be good */
Azure.IoT Build 0:6ae2f7bca550 229 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_IDLE);
Azure.IoT Build 0:6ae2f7bca550 230 }
Azure.IoT Build 0:6ae2f7bca550 231 else if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 232 {
Azure.IoT Build 0:6ae2f7bca550 233 /* Any other transition must be an error */
Azure.IoT Build 0:6ae2f7bca550 234 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 235 }
Azure.IoT Build 0:6ae2f7bca550 236 break;
AzureIoTClient 1:eab586236bfe 237 case LINK_STATE_ERROR:
AzureIoTClient 1:eab586236bfe 238 if (message_receiver_instance->message_receiver_state != MESSAGE_RECEIVER_STATE_ERROR)
AzureIoTClient 1:eab586236bfe 239 {
AzureIoTClient 1:eab586236bfe 240 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
AzureIoTClient 1:eab586236bfe 241 }
AzureIoTClient 1:eab586236bfe 242 break;
Azure.IoT Build 0:6ae2f7bca550 243 }
Azure.IoT Build 0:6ae2f7bca550 244 }
Azure.IoT Build 0:6ae2f7bca550 245
Azure.IoT Build 0:6ae2f7bca550 246 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 247 {
Azure.IoT Build 0:6ae2f7bca550 248 MESSAGE_RECEIVER_INSTANCE* result = (MESSAGE_RECEIVER_INSTANCE*)amqpalloc_malloc(sizeof(MESSAGE_RECEIVER_INSTANCE));
Azure.IoT Build 0:6ae2f7bca550 249 if (result != NULL)
Azure.IoT Build 0:6ae2f7bca550 250 {
Azure.IoT Build 0:6ae2f7bca550 251 result->link = link;
Azure.IoT Build 0:6ae2f7bca550 252 result->on_message_receiver_state_changed = on_message_receiver_state_changed;
Azure.IoT Build 0:6ae2f7bca550 253 result->on_message_receiver_state_changed_context = context;
Azure.IoT Build 0:6ae2f7bca550 254 result->message_receiver_state = MESSAGE_RECEIVER_STATE_IDLE;
Azure.IoT Build 0:6ae2f7bca550 255 }
Azure.IoT Build 0:6ae2f7bca550 256
Azure.IoT Build 0:6ae2f7bca550 257 return result;
Azure.IoT Build 0:6ae2f7bca550 258 }
Azure.IoT Build 0:6ae2f7bca550 259
Azure.IoT Build 0:6ae2f7bca550 260 void messagereceiver_destroy(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 261 {
Azure.IoT Build 0:6ae2f7bca550 262 if (message_receiver != NULL)
Azure.IoT Build 0:6ae2f7bca550 263 {
AzureIoTClient 1:eab586236bfe 264 (void)messagereceiver_close(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 265 amqpalloc_free(message_receiver);
Azure.IoT Build 0:6ae2f7bca550 266 }
Azure.IoT Build 0:6ae2f7bca550 267 }
Azure.IoT Build 0:6ae2f7bca550 268
Azure.IoT Build 0:6ae2f7bca550 269 int messagereceiver_open(MESSAGE_RECEIVER_HANDLE message_receiver, ON_MESSAGE_RECEIVED on_message_received, const void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 270 {
Azure.IoT Build 0:6ae2f7bca550 271 int result;
Azure.IoT Build 0:6ae2f7bca550 272
Azure.IoT Build 0:6ae2f7bca550 273 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 274 {
AzureIoTClient 19:000ab4e6a2c1 275 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 276 }
Azure.IoT Build 0:6ae2f7bca550 277 else
Azure.IoT Build 0:6ae2f7bca550 278 {
Azure.IoT Build 0:6ae2f7bca550 279 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 280
Azure.IoT Build 0:6ae2f7bca550 281 if (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_IDLE)
Azure.IoT Build 0:6ae2f7bca550 282 {
Azure.IoT Build 0:6ae2f7bca550 283 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_OPENING);
Azure.IoT Build 0:6ae2f7bca550 284 if (link_attach(message_receiver_instance->link, on_transfer_received, on_link_state_changed, NULL, message_receiver_instance) != 0)
Azure.IoT Build 0:6ae2f7bca550 285 {
AzureIoTClient 19:000ab4e6a2c1 286 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 287 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 288 }
Azure.IoT Build 0:6ae2f7bca550 289 else
Azure.IoT Build 0:6ae2f7bca550 290 {
Azure.IoT Build 0:6ae2f7bca550 291 message_receiver_instance->on_message_received = on_message_received;
Azure.IoT Build 0:6ae2f7bca550 292 message_receiver_instance->callback_context = callback_context;
Azure.IoT Build 0:6ae2f7bca550 293
Azure.IoT Build 0:6ae2f7bca550 294 result = 0;
Azure.IoT Build 0:6ae2f7bca550 295 }
Azure.IoT Build 0:6ae2f7bca550 296 }
Azure.IoT Build 0:6ae2f7bca550 297 else
Azure.IoT Build 0:6ae2f7bca550 298 {
Azure.IoT Build 0:6ae2f7bca550 299 result = 0;
Azure.IoT Build 0:6ae2f7bca550 300 }
Azure.IoT Build 0:6ae2f7bca550 301 }
Azure.IoT Build 0:6ae2f7bca550 302
Azure.IoT Build 0:6ae2f7bca550 303 return result;
Azure.IoT Build 0:6ae2f7bca550 304 }
Azure.IoT Build 0:6ae2f7bca550 305
Azure.IoT Build 0:6ae2f7bca550 306 int messagereceiver_close(MESSAGE_RECEIVER_HANDLE message_receiver)
Azure.IoT Build 0:6ae2f7bca550 307 {
Azure.IoT Build 0:6ae2f7bca550 308 int result;
Azure.IoT Build 0:6ae2f7bca550 309
Azure.IoT Build 0:6ae2f7bca550 310 if (message_receiver == NULL)
Azure.IoT Build 0:6ae2f7bca550 311 {
AzureIoTClient 19:000ab4e6a2c1 312 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 313 }
Azure.IoT Build 0:6ae2f7bca550 314 else
Azure.IoT Build 0:6ae2f7bca550 315 {
Azure.IoT Build 0:6ae2f7bca550 316 MESSAGE_RECEIVER_INSTANCE* message_receiver_instance = (MESSAGE_RECEIVER_INSTANCE*)message_receiver;
Azure.IoT Build 0:6ae2f7bca550 317
Azure.IoT Build 0:6ae2f7bca550 318 if ((message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPENING) ||
Azure.IoT Build 0:6ae2f7bca550 319 (message_receiver_instance->message_receiver_state == MESSAGE_RECEIVER_STATE_OPEN))
Azure.IoT Build 0:6ae2f7bca550 320 {
Azure.IoT Build 0:6ae2f7bca550 321 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_CLOSING);
Azure.IoT Build 0:6ae2f7bca550 322
AzureIoTClient 12:b30dacf113f2 323 if (link_detach(message_receiver_instance->link, true) != 0)
Azure.IoT Build 0:6ae2f7bca550 324 {
AzureIoTClient 19:000ab4e6a2c1 325 result = __FAILURE__;
Azure.IoT Build 0:6ae2f7bca550 326 set_message_receiver_state(message_receiver_instance, MESSAGE_RECEIVER_STATE_ERROR);
Azure.IoT Build 0:6ae2f7bca550 327 }
Azure.IoT Build 0:6ae2f7bca550 328 else
Azure.IoT Build 0:6ae2f7bca550 329 {
Azure.IoT Build 0:6ae2f7bca550 330 result = 0;
Azure.IoT Build 0:6ae2f7bca550 331 }
Azure.IoT Build 0:6ae2f7bca550 332 }
Azure.IoT Build 0:6ae2f7bca550 333 else
Azure.IoT Build 0:6ae2f7bca550 334 {
Azure.IoT Build 0:6ae2f7bca550 335 result = 0;
Azure.IoT Build 0:6ae2f7bca550 336 }
Azure.IoT Build 0:6ae2f7bca550 337 }
Azure.IoT Build 0:6ae2f7bca550 338
Azure.IoT Build 0:6ae2f7bca550 339 return result;
Azure.IoT Build 0:6ae2f7bca550 340 }