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:
0:6ae2f7bca550
Child:
28:add19eb7defa
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 <stdbool.h>
Azure.IoT Build 0:6ae2f7bca550 6 #include "azure_uamqp_c/amqpvalue.h"
Azure.IoT Build 0:6ae2f7bca550 7 #include "azure_uamqp_c/amqp_definitions.h"
Azure.IoT Build 0:6ae2f7bca550 8
Azure.IoT Build 0:6ae2f7bca550 9 AMQP_VALUE messaging_create_source(const char* address)
Azure.IoT Build 0:6ae2f7bca550 10 {
Azure.IoT Build 0:6ae2f7bca550 11 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 12 SOURCE_HANDLE source = source_create();
Azure.IoT Build 0:6ae2f7bca550 13 if (source == NULL)
Azure.IoT Build 0:6ae2f7bca550 14 {
Azure.IoT Build 0:6ae2f7bca550 15 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 16 }
Azure.IoT Build 0:6ae2f7bca550 17 else
Azure.IoT Build 0:6ae2f7bca550 18 {
Azure.IoT Build 0:6ae2f7bca550 19 AMQP_VALUE address_value = amqpvalue_create_string(address);
Azure.IoT Build 0:6ae2f7bca550 20 if (address_value == NULL)
Azure.IoT Build 0:6ae2f7bca550 21 {
Azure.IoT Build 0:6ae2f7bca550 22 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 23 }
Azure.IoT Build 0:6ae2f7bca550 24 else
Azure.IoT Build 0:6ae2f7bca550 25 {
Azure.IoT Build 0:6ae2f7bca550 26 if (source_set_address(source, address_value) != 0)
Azure.IoT Build 0:6ae2f7bca550 27 {
Azure.IoT Build 0:6ae2f7bca550 28 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 29 }
Azure.IoT Build 0:6ae2f7bca550 30 else
Azure.IoT Build 0:6ae2f7bca550 31 {
Azure.IoT Build 0:6ae2f7bca550 32 result = amqpvalue_create_source(source);
Azure.IoT Build 0:6ae2f7bca550 33 }
Azure.IoT Build 0:6ae2f7bca550 34
Azure.IoT Build 0:6ae2f7bca550 35 amqpvalue_destroy(address_value);
Azure.IoT Build 0:6ae2f7bca550 36 }
Azure.IoT Build 0:6ae2f7bca550 37
Azure.IoT Build 0:6ae2f7bca550 38 source_destroy(source);
Azure.IoT Build 0:6ae2f7bca550 39 }
Azure.IoT Build 0:6ae2f7bca550 40
Azure.IoT Build 0:6ae2f7bca550 41 return result;
Azure.IoT Build 0:6ae2f7bca550 42 }
Azure.IoT Build 0:6ae2f7bca550 43
Azure.IoT Build 0:6ae2f7bca550 44 AMQP_VALUE messaging_create_target(const char* address)
Azure.IoT Build 0:6ae2f7bca550 45 {
Azure.IoT Build 0:6ae2f7bca550 46 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 47 TARGET_HANDLE target = target_create();
Azure.IoT Build 0:6ae2f7bca550 48 if (target == NULL)
Azure.IoT Build 0:6ae2f7bca550 49 {
Azure.IoT Build 0:6ae2f7bca550 50 result = NULL;
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 AMQP_VALUE address_value = amqpvalue_create_string(address);
Azure.IoT Build 0:6ae2f7bca550 55 if (address_value == NULL)
Azure.IoT Build 0:6ae2f7bca550 56 {
Azure.IoT Build 0:6ae2f7bca550 57 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 58 }
Azure.IoT Build 0:6ae2f7bca550 59 else
Azure.IoT Build 0:6ae2f7bca550 60 {
Azure.IoT Build 0:6ae2f7bca550 61 if (target_set_address(target, address_value) != 0)
Azure.IoT Build 0:6ae2f7bca550 62 {
Azure.IoT Build 0:6ae2f7bca550 63 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 64 }
Azure.IoT Build 0:6ae2f7bca550 65 else
Azure.IoT Build 0:6ae2f7bca550 66 {
Azure.IoT Build 0:6ae2f7bca550 67 result = amqpvalue_create_target(target);
Azure.IoT Build 0:6ae2f7bca550 68 }
Azure.IoT Build 0:6ae2f7bca550 69
Azure.IoT Build 0:6ae2f7bca550 70 amqpvalue_destroy(address_value);
Azure.IoT Build 0:6ae2f7bca550 71 }
Azure.IoT Build 0:6ae2f7bca550 72
Azure.IoT Build 0:6ae2f7bca550 73 target_destroy(target);
Azure.IoT Build 0:6ae2f7bca550 74 }
Azure.IoT Build 0:6ae2f7bca550 75
Azure.IoT Build 0:6ae2f7bca550 76 return result;
Azure.IoT Build 0:6ae2f7bca550 77 }
Azure.IoT Build 0:6ae2f7bca550 78
Azure.IoT Build 0:6ae2f7bca550 79 AMQP_VALUE messaging_delivery_received(uint32_t section_number, uint64_t section_offset)
Azure.IoT Build 0:6ae2f7bca550 80 {
Azure.IoT Build 0:6ae2f7bca550 81 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 82 RECEIVED_HANDLE received = received_create(section_number, section_offset);
Azure.IoT Build 0:6ae2f7bca550 83 if (received == NULL)
Azure.IoT Build 0:6ae2f7bca550 84 {
Azure.IoT Build 0:6ae2f7bca550 85 result = NULL;
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 result = amqpvalue_create_received(received);
Azure.IoT Build 0:6ae2f7bca550 90 received_destroy(received);
Azure.IoT Build 0:6ae2f7bca550 91 }
Azure.IoT Build 0:6ae2f7bca550 92
Azure.IoT Build 0:6ae2f7bca550 93 return result;
Azure.IoT Build 0:6ae2f7bca550 94 }
Azure.IoT Build 0:6ae2f7bca550 95
Azure.IoT Build 0:6ae2f7bca550 96 AMQP_VALUE messaging_delivery_accepted(void)
Azure.IoT Build 0:6ae2f7bca550 97 {
Azure.IoT Build 0:6ae2f7bca550 98 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 99 ACCEPTED_HANDLE accepted = accepted_create();
Azure.IoT Build 0:6ae2f7bca550 100 if (accepted == NULL)
Azure.IoT Build 0:6ae2f7bca550 101 {
Azure.IoT Build 0:6ae2f7bca550 102 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 103 }
Azure.IoT Build 0:6ae2f7bca550 104 else
Azure.IoT Build 0:6ae2f7bca550 105 {
Azure.IoT Build 0:6ae2f7bca550 106 result = amqpvalue_create_accepted(accepted);
Azure.IoT Build 0:6ae2f7bca550 107 accepted_destroy(accepted);
Azure.IoT Build 0:6ae2f7bca550 108 }
Azure.IoT Build 0:6ae2f7bca550 109
Azure.IoT Build 0:6ae2f7bca550 110 return result;
Azure.IoT Build 0:6ae2f7bca550 111 }
Azure.IoT Build 0:6ae2f7bca550 112
Azure.IoT Build 0:6ae2f7bca550 113 AMQP_VALUE messaging_delivery_rejected(const char* error_condition, const char* error_description)
Azure.IoT Build 0:6ae2f7bca550 114 {
Azure.IoT Build 0:6ae2f7bca550 115 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 116 REJECTED_HANDLE rejected = rejected_create();
Azure.IoT Build 0:6ae2f7bca550 117 if (rejected == NULL)
Azure.IoT Build 0:6ae2f7bca550 118 {
Azure.IoT Build 0:6ae2f7bca550 119 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 120 }
Azure.IoT Build 0:6ae2f7bca550 121 else
Azure.IoT Build 0:6ae2f7bca550 122 {
Azure.IoT Build 0:6ae2f7bca550 123 ERROR_HANDLE error_handle = NULL;
Azure.IoT Build 0:6ae2f7bca550 124 bool error_constructing = false;
Azure.IoT Build 0:6ae2f7bca550 125
Azure.IoT Build 0:6ae2f7bca550 126 if (error_condition != NULL)
Azure.IoT Build 0:6ae2f7bca550 127 {
Azure.IoT Build 0:6ae2f7bca550 128 error_handle = error_create(error_condition);
Azure.IoT Build 0:6ae2f7bca550 129 if (error_handle == NULL)
Azure.IoT Build 0:6ae2f7bca550 130 {
Azure.IoT Build 0:6ae2f7bca550 131 error_constructing = 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 if ((error_description != NULL) &&
Azure.IoT Build 0:6ae2f7bca550 136 (error_set_description(error_handle, error_description) != 0))
Azure.IoT Build 0:6ae2f7bca550 137 {
Azure.IoT Build 0:6ae2f7bca550 138 error_constructing = true;
Azure.IoT Build 0:6ae2f7bca550 139 }
Azure.IoT Build 0:6ae2f7bca550 140 else
Azure.IoT Build 0:6ae2f7bca550 141 {
Azure.IoT Build 0:6ae2f7bca550 142 if (rejected_set_error(rejected, error_handle) != 0)
Azure.IoT Build 0:6ae2f7bca550 143 {
Azure.IoT Build 0:6ae2f7bca550 144 error_constructing = true;
Azure.IoT Build 0:6ae2f7bca550 145 }
Azure.IoT Build 0:6ae2f7bca550 146 }
Azure.IoT Build 0:6ae2f7bca550 147
Azure.IoT Build 0:6ae2f7bca550 148 error_destroy(error_handle);
Azure.IoT Build 0:6ae2f7bca550 149 }
Azure.IoT Build 0:6ae2f7bca550 150 }
Azure.IoT Build 0:6ae2f7bca550 151
Azure.IoT Build 0:6ae2f7bca550 152 if (error_constructing)
Azure.IoT Build 0:6ae2f7bca550 153 {
Azure.IoT Build 0:6ae2f7bca550 154 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 155 }
Azure.IoT Build 0:6ae2f7bca550 156 else
Azure.IoT Build 0:6ae2f7bca550 157 {
Azure.IoT Build 0:6ae2f7bca550 158 result = amqpvalue_create_rejected(rejected);
Azure.IoT Build 0:6ae2f7bca550 159 }
Azure.IoT Build 0:6ae2f7bca550 160
Azure.IoT Build 0:6ae2f7bca550 161 rejected_destroy(rejected);
Azure.IoT Build 0:6ae2f7bca550 162 }
Azure.IoT Build 0:6ae2f7bca550 163
Azure.IoT Build 0:6ae2f7bca550 164 return result;
Azure.IoT Build 0:6ae2f7bca550 165 }
Azure.IoT Build 0:6ae2f7bca550 166
Azure.IoT Build 0:6ae2f7bca550 167 AMQP_VALUE messaging_delivery_released(void)
Azure.IoT Build 0:6ae2f7bca550 168 {
Azure.IoT Build 0:6ae2f7bca550 169 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 170 RELEASED_HANDLE released = released_create();
Azure.IoT Build 0:6ae2f7bca550 171 if (released == NULL)
Azure.IoT Build 0:6ae2f7bca550 172 {
Azure.IoT Build 0:6ae2f7bca550 173 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 174 }
Azure.IoT Build 0:6ae2f7bca550 175 else
Azure.IoT Build 0:6ae2f7bca550 176 {
Azure.IoT Build 0:6ae2f7bca550 177 result = amqpvalue_create_released(released);
Azure.IoT Build 0:6ae2f7bca550 178 released_destroy(released);
Azure.IoT Build 0:6ae2f7bca550 179 }
Azure.IoT Build 0:6ae2f7bca550 180
Azure.IoT Build 0:6ae2f7bca550 181 return result;
Azure.IoT Build 0:6ae2f7bca550 182 }
Azure.IoT Build 0:6ae2f7bca550 183
Azure.IoT Build 0:6ae2f7bca550 184 AMQP_VALUE messaging_delivery_modified(bool delivery_failed, bool undeliverable_here, fields message_annotations)
Azure.IoT Build 0:6ae2f7bca550 185 {
Azure.IoT Build 0:6ae2f7bca550 186 AMQP_VALUE result;
Azure.IoT Build 0:6ae2f7bca550 187 MODIFIED_HANDLE modified = modified_create();
Azure.IoT Build 0:6ae2f7bca550 188 if (modified == NULL)
Azure.IoT Build 0:6ae2f7bca550 189 {
Azure.IoT Build 0:6ae2f7bca550 190 result = NULL;
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 if ((modified_set_delivery_failed(modified, delivery_failed) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 195 (modified_set_undeliverable_here(modified, undeliverable_here) != 0) ||
Azure.IoT Build 0:6ae2f7bca550 196 ((message_annotations != NULL) && (modified_set_message_annotations(modified, message_annotations) != 0)))
Azure.IoT Build 0:6ae2f7bca550 197 {
Azure.IoT Build 0:6ae2f7bca550 198 result = NULL;
Azure.IoT Build 0:6ae2f7bca550 199 }
Azure.IoT Build 0:6ae2f7bca550 200 else
Azure.IoT Build 0:6ae2f7bca550 201 {
Azure.IoT Build 0:6ae2f7bca550 202 result = amqpvalue_create_modified(modified);
Azure.IoT Build 0:6ae2f7bca550 203 }
Azure.IoT Build 0:6ae2f7bca550 204
Azure.IoT Build 0:6ae2f7bca550 205 modified_destroy(modified);
Azure.IoT Build 0:6ae2f7bca550 206 }
Azure.IoT Build 0:6ae2f7bca550 207
Azure.IoT Build 0:6ae2f7bca550 208 return result;
Azure.IoT Build 0:6ae2f7bca550 209 }