A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Tue Jan 24 15:23:52 2017 -0800
Revision:
17:923575db8b2d
Parent:
12:b30dacf113f2
Child:
19:000ab4e6a2c1
1.1.5

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