A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:16:13 2018 -0700
Revision:
47:365a93fdb5bb
Parent:
34:6be9c2058664
1.2.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 34:6be9c2058664 5 #include <stdint.h>
Azure.IoT Build 0:6ae2f7bca550 6 #include <stdbool.h>
AzureIoTClient 34:6be9c2058664 7 #include "azure_c_shared_utility/xlogging.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 AMQP_VALUE messaging_create_source(const char* address)
Azure.IoT Build 0:6ae2f7bca550 12 {
AzureIoTClient 28:add19eb7defa 13 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 14 SOURCE_HANDLE source = source_create();
AzureIoTClient 34:6be9c2058664 15
AzureIoTClient 28:add19eb7defa 16 if (source == NULL)
AzureIoTClient 28:add19eb7defa 17 {
AzureIoTClient 34:6be9c2058664 18 LogError("NULL source");
AzureIoTClient 28:add19eb7defa 19 result = NULL;
AzureIoTClient 28:add19eb7defa 20 }
AzureIoTClient 28:add19eb7defa 21 else
AzureIoTClient 28:add19eb7defa 22 {
AzureIoTClient 28:add19eb7defa 23 AMQP_VALUE address_value = amqpvalue_create_string(address);
AzureIoTClient 28:add19eb7defa 24 if (address_value == NULL)
AzureIoTClient 28:add19eb7defa 25 {
AzureIoTClient 34:6be9c2058664 26 LogError("Cannot create address AMQP string");
AzureIoTClient 28:add19eb7defa 27 result = NULL;
AzureIoTClient 28:add19eb7defa 28 }
AzureIoTClient 28:add19eb7defa 29 else
AzureIoTClient 28:add19eb7defa 30 {
AzureIoTClient 28:add19eb7defa 31 if (source_set_address(source, address_value) != 0)
AzureIoTClient 28:add19eb7defa 32 {
AzureIoTClient 34:6be9c2058664 33 LogError("Cannot set address on source");
AzureIoTClient 28:add19eb7defa 34 result = NULL;
AzureIoTClient 28:add19eb7defa 35 }
AzureIoTClient 28:add19eb7defa 36 else
AzureIoTClient 28:add19eb7defa 37 {
AzureIoTClient 28:add19eb7defa 38 result = amqpvalue_create_source(source);
AzureIoTClient 34:6be9c2058664 39 if (result == NULL)
AzureIoTClient 34:6be9c2058664 40 {
AzureIoTClient 34:6be9c2058664 41 LogError("Cannot create source");
AzureIoTClient 34:6be9c2058664 42 }
AzureIoTClient 34:6be9c2058664 43 else
AzureIoTClient 34:6be9c2058664 44 {
AzureIoTClient 34:6be9c2058664 45 /* all ok */
AzureIoTClient 34:6be9c2058664 46 }
AzureIoTClient 28:add19eb7defa 47 }
Azure.IoT Build 0:6ae2f7bca550 48
AzureIoTClient 28:add19eb7defa 49 amqpvalue_destroy(address_value);
AzureIoTClient 28:add19eb7defa 50 }
Azure.IoT Build 0:6ae2f7bca550 51
AzureIoTClient 28:add19eb7defa 52 source_destroy(source);
AzureIoTClient 28:add19eb7defa 53 }
Azure.IoT Build 0:6ae2f7bca550 54
AzureIoTClient 28:add19eb7defa 55 return result;
Azure.IoT Build 0:6ae2f7bca550 56 }
Azure.IoT Build 0:6ae2f7bca550 57
Azure.IoT Build 0:6ae2f7bca550 58 AMQP_VALUE messaging_create_target(const char* address)
Azure.IoT Build 0:6ae2f7bca550 59 {
AzureIoTClient 28:add19eb7defa 60 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 61 TARGET_HANDLE target = target_create();
AzureIoTClient 34:6be9c2058664 62
AzureIoTClient 28:add19eb7defa 63 if (target == NULL)
AzureIoTClient 28:add19eb7defa 64 {
AzureIoTClient 34:6be9c2058664 65 LogError("NULL target");
AzureIoTClient 28:add19eb7defa 66 result = NULL;
AzureIoTClient 28:add19eb7defa 67 }
AzureIoTClient 28:add19eb7defa 68 else
AzureIoTClient 28:add19eb7defa 69 {
AzureIoTClient 28:add19eb7defa 70 AMQP_VALUE address_value = amqpvalue_create_string(address);
AzureIoTClient 28:add19eb7defa 71 if (address_value == NULL)
AzureIoTClient 28:add19eb7defa 72 {
AzureIoTClient 34:6be9c2058664 73 LogError("Cannot create address AMQP string");
AzureIoTClient 28:add19eb7defa 74 result = NULL;
AzureIoTClient 28:add19eb7defa 75 }
AzureIoTClient 28:add19eb7defa 76 else
AzureIoTClient 28:add19eb7defa 77 {
AzureIoTClient 28:add19eb7defa 78 if (target_set_address(target, address_value) != 0)
AzureIoTClient 28:add19eb7defa 79 {
AzureIoTClient 34:6be9c2058664 80 LogError("Cannot set address on target");
AzureIoTClient 28:add19eb7defa 81 result = NULL;
AzureIoTClient 28:add19eb7defa 82 }
AzureIoTClient 28:add19eb7defa 83 else
AzureIoTClient 28:add19eb7defa 84 {
AzureIoTClient 28:add19eb7defa 85 result = amqpvalue_create_target(target);
AzureIoTClient 34:6be9c2058664 86 if (result == NULL)
AzureIoTClient 34:6be9c2058664 87 {
AzureIoTClient 34:6be9c2058664 88 LogError("Cannot create target");
AzureIoTClient 34:6be9c2058664 89 }
AzureIoTClient 34:6be9c2058664 90 else
AzureIoTClient 34:6be9c2058664 91 {
AzureIoTClient 34:6be9c2058664 92 /* all ok */
AzureIoTClient 34:6be9c2058664 93 }
AzureIoTClient 28:add19eb7defa 94 }
Azure.IoT Build 0:6ae2f7bca550 95
AzureIoTClient 28:add19eb7defa 96 amqpvalue_destroy(address_value);
AzureIoTClient 28:add19eb7defa 97 }
Azure.IoT Build 0:6ae2f7bca550 98
AzureIoTClient 28:add19eb7defa 99 target_destroy(target);
AzureIoTClient 28:add19eb7defa 100 }
Azure.IoT Build 0:6ae2f7bca550 101
AzureIoTClient 28:add19eb7defa 102 return result;
Azure.IoT Build 0:6ae2f7bca550 103 }
Azure.IoT Build 0:6ae2f7bca550 104
Azure.IoT Build 0:6ae2f7bca550 105 AMQP_VALUE messaging_delivery_received(uint32_t section_number, uint64_t section_offset)
Azure.IoT Build 0:6ae2f7bca550 106 {
AzureIoTClient 28:add19eb7defa 107 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 108 RECEIVED_HANDLE received = received_create(section_number, section_offset);
AzureIoTClient 28:add19eb7defa 109 if (received == NULL)
AzureIoTClient 28:add19eb7defa 110 {
AzureIoTClient 34:6be9c2058664 111 LogError("Cannot create RECEIVED delivery state handle");
AzureIoTClient 28:add19eb7defa 112 result = NULL;
AzureIoTClient 28:add19eb7defa 113 }
AzureIoTClient 28:add19eb7defa 114 else
AzureIoTClient 28:add19eb7defa 115 {
AzureIoTClient 28:add19eb7defa 116 result = amqpvalue_create_received(received);
AzureIoTClient 34:6be9c2058664 117 if (result == NULL)
AzureIoTClient 34:6be9c2058664 118 {
AzureIoTClient 34:6be9c2058664 119 LogError("Cannot create RECEIVED delivery state AMQP value");
AzureIoTClient 34:6be9c2058664 120 }
AzureIoTClient 34:6be9c2058664 121 else
AzureIoTClient 34:6be9c2058664 122 {
AzureIoTClient 34:6be9c2058664 123 /* all ok */
AzureIoTClient 34:6be9c2058664 124 }
AzureIoTClient 34:6be9c2058664 125
AzureIoTClient 28:add19eb7defa 126 received_destroy(received);
AzureIoTClient 28:add19eb7defa 127 }
Azure.IoT Build 0:6ae2f7bca550 128
AzureIoTClient 28:add19eb7defa 129 return result;
Azure.IoT Build 0:6ae2f7bca550 130 }
Azure.IoT Build 0:6ae2f7bca550 131
Azure.IoT Build 0:6ae2f7bca550 132 AMQP_VALUE messaging_delivery_accepted(void)
Azure.IoT Build 0:6ae2f7bca550 133 {
AzureIoTClient 28:add19eb7defa 134 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 135 ACCEPTED_HANDLE accepted = accepted_create();
AzureIoTClient 28:add19eb7defa 136 if (accepted == NULL)
AzureIoTClient 28:add19eb7defa 137 {
AzureIoTClient 34:6be9c2058664 138 LogError("Cannot create ACCEPTED delivery state handle");
AzureIoTClient 28:add19eb7defa 139 result = NULL;
AzureIoTClient 28:add19eb7defa 140 }
AzureIoTClient 28:add19eb7defa 141 else
AzureIoTClient 28:add19eb7defa 142 {
AzureIoTClient 28:add19eb7defa 143 result = amqpvalue_create_accepted(accepted);
AzureIoTClient 34:6be9c2058664 144 if (result == NULL)
AzureIoTClient 34:6be9c2058664 145 {
AzureIoTClient 34:6be9c2058664 146 LogError("Cannot create ACCEPTED delivery state AMQP value");
AzureIoTClient 34:6be9c2058664 147 }
AzureIoTClient 34:6be9c2058664 148 else
AzureIoTClient 34:6be9c2058664 149 {
AzureIoTClient 34:6be9c2058664 150 /* all ok */
AzureIoTClient 34:6be9c2058664 151 }
AzureIoTClient 34:6be9c2058664 152
AzureIoTClient 28:add19eb7defa 153 accepted_destroy(accepted);
AzureIoTClient 28:add19eb7defa 154 }
Azure.IoT Build 0:6ae2f7bca550 155
AzureIoTClient 28:add19eb7defa 156 return result;
Azure.IoT Build 0:6ae2f7bca550 157 }
Azure.IoT Build 0:6ae2f7bca550 158
Azure.IoT Build 0:6ae2f7bca550 159 AMQP_VALUE messaging_delivery_rejected(const char* error_condition, const char* error_description)
Azure.IoT Build 0:6ae2f7bca550 160 {
AzureIoTClient 28:add19eb7defa 161 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 162 REJECTED_HANDLE rejected = rejected_create();
AzureIoTClient 28:add19eb7defa 163 if (rejected == NULL)
AzureIoTClient 28:add19eb7defa 164 {
AzureIoTClient 34:6be9c2058664 165 LogError("Cannot create REJECTED delivery state handle");
AzureIoTClient 28:add19eb7defa 166 result = NULL;
AzureIoTClient 28:add19eb7defa 167 }
AzureIoTClient 28:add19eb7defa 168 else
AzureIoTClient 28:add19eb7defa 169 {
AzureIoTClient 28:add19eb7defa 170 ERROR_HANDLE error_handle = NULL;
AzureIoTClient 28:add19eb7defa 171 bool error_constructing = false;
Azure.IoT Build 0:6ae2f7bca550 172
AzureIoTClient 28:add19eb7defa 173 if (error_condition != NULL)
AzureIoTClient 28:add19eb7defa 174 {
AzureIoTClient 28:add19eb7defa 175 error_handle = error_create(error_condition);
AzureIoTClient 28:add19eb7defa 176 if (error_handle == NULL)
AzureIoTClient 28:add19eb7defa 177 {
AzureIoTClient 34:6be9c2058664 178 LogError("Cannot create error AMQP value for REJECTED state");
AzureIoTClient 28:add19eb7defa 179 error_constructing = true;
AzureIoTClient 28:add19eb7defa 180 }
AzureIoTClient 28:add19eb7defa 181 else
AzureIoTClient 28:add19eb7defa 182 {
AzureIoTClient 28:add19eb7defa 183 if ((error_description != NULL) &&
AzureIoTClient 28:add19eb7defa 184 (error_set_description(error_handle, error_description) != 0))
AzureIoTClient 28:add19eb7defa 185 {
AzureIoTClient 34:6be9c2058664 186 LogError("Cannot set error description on error AMQP value for REJECTED state");
AzureIoTClient 28:add19eb7defa 187 error_constructing = true;
AzureIoTClient 28:add19eb7defa 188 }
AzureIoTClient 28:add19eb7defa 189 else
AzureIoTClient 28:add19eb7defa 190 {
AzureIoTClient 28:add19eb7defa 191 if (rejected_set_error(rejected, error_handle) != 0)
AzureIoTClient 28:add19eb7defa 192 {
AzureIoTClient 34:6be9c2058664 193 LogError("Cannot set error on REJECTED state handle");
AzureIoTClient 28:add19eb7defa 194 error_constructing = true;
AzureIoTClient 28:add19eb7defa 195 }
AzureIoTClient 28:add19eb7defa 196 }
Azure.IoT Build 0:6ae2f7bca550 197
AzureIoTClient 28:add19eb7defa 198 error_destroy(error_handle);
AzureIoTClient 28:add19eb7defa 199 }
AzureIoTClient 28:add19eb7defa 200 }
Azure.IoT Build 0:6ae2f7bca550 201
AzureIoTClient 28:add19eb7defa 202 if (error_constructing)
AzureIoTClient 28:add19eb7defa 203 {
AzureIoTClient 28:add19eb7defa 204 result = NULL;
AzureIoTClient 28:add19eb7defa 205 }
AzureIoTClient 28:add19eb7defa 206 else
AzureIoTClient 28:add19eb7defa 207 {
AzureIoTClient 28:add19eb7defa 208 result = amqpvalue_create_rejected(rejected);
AzureIoTClient 34:6be9c2058664 209 if (result == NULL)
AzureIoTClient 34:6be9c2058664 210 {
AzureIoTClient 34:6be9c2058664 211 LogError("Cannot create REJECTED delivery state AMQP value");
AzureIoTClient 34:6be9c2058664 212 }
AzureIoTClient 34:6be9c2058664 213 else
AzureIoTClient 34:6be9c2058664 214 {
AzureIoTClient 34:6be9c2058664 215 /* all ok */
AzureIoTClient 34:6be9c2058664 216 }
AzureIoTClient 28:add19eb7defa 217 }
Azure.IoT Build 0:6ae2f7bca550 218
AzureIoTClient 28:add19eb7defa 219 rejected_destroy(rejected);
AzureIoTClient 28:add19eb7defa 220 }
Azure.IoT Build 0:6ae2f7bca550 221
AzureIoTClient 28:add19eb7defa 222 return result;
Azure.IoT Build 0:6ae2f7bca550 223 }
Azure.IoT Build 0:6ae2f7bca550 224
Azure.IoT Build 0:6ae2f7bca550 225 AMQP_VALUE messaging_delivery_released(void)
Azure.IoT Build 0:6ae2f7bca550 226 {
AzureIoTClient 28:add19eb7defa 227 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 228 RELEASED_HANDLE released = released_create();
AzureIoTClient 28:add19eb7defa 229 if (released == NULL)
AzureIoTClient 28:add19eb7defa 230 {
AzureIoTClient 34:6be9c2058664 231 LogError("Cannot create RELEASED delivery state handle");
AzureIoTClient 28:add19eb7defa 232 result = NULL;
AzureIoTClient 28:add19eb7defa 233 }
AzureIoTClient 28:add19eb7defa 234 else
AzureIoTClient 28:add19eb7defa 235 {
AzureIoTClient 28:add19eb7defa 236 result = amqpvalue_create_released(released);
AzureIoTClient 34:6be9c2058664 237 if (result == NULL)
AzureIoTClient 34:6be9c2058664 238 {
AzureIoTClient 34:6be9c2058664 239 LogError("Cannot create RELEASED delivery state AMQP value");
AzureIoTClient 34:6be9c2058664 240 }
AzureIoTClient 34:6be9c2058664 241 else
AzureIoTClient 34:6be9c2058664 242 {
AzureIoTClient 34:6be9c2058664 243 /* all ok */
AzureIoTClient 34:6be9c2058664 244 }
AzureIoTClient 34:6be9c2058664 245
AzureIoTClient 28:add19eb7defa 246 released_destroy(released);
AzureIoTClient 28:add19eb7defa 247 }
Azure.IoT Build 0:6ae2f7bca550 248
AzureIoTClient 28:add19eb7defa 249 return result;
Azure.IoT Build 0:6ae2f7bca550 250 }
Azure.IoT Build 0:6ae2f7bca550 251
Azure.IoT Build 0:6ae2f7bca550 252 AMQP_VALUE messaging_delivery_modified(bool delivery_failed, bool undeliverable_here, fields message_annotations)
Azure.IoT Build 0:6ae2f7bca550 253 {
AzureIoTClient 28:add19eb7defa 254 AMQP_VALUE result;
AzureIoTClient 28:add19eb7defa 255 MODIFIED_HANDLE modified = modified_create();
AzureIoTClient 28:add19eb7defa 256 if (modified == NULL)
AzureIoTClient 28:add19eb7defa 257 {
AzureIoTClient 34:6be9c2058664 258 LogError("Cannot create MODIFIED delivery state handle");
AzureIoTClient 28:add19eb7defa 259 result = NULL;
AzureIoTClient 28:add19eb7defa 260 }
AzureIoTClient 28:add19eb7defa 261 else
AzureIoTClient 28:add19eb7defa 262 {
AzureIoTClient 34:6be9c2058664 263 if (modified_set_delivery_failed(modified, delivery_failed) != 0)
AzureIoTClient 34:6be9c2058664 264 {
AzureIoTClient 34:6be9c2058664 265 LogError("Cannot set delivery failed on MODIFIED delivery state");
AzureIoTClient 34:6be9c2058664 266 result = NULL;
AzureIoTClient 34:6be9c2058664 267 }
AzureIoTClient 34:6be9c2058664 268 else if (modified_set_undeliverable_here(modified, undeliverable_here) != 0)
AzureIoTClient 28:add19eb7defa 269 {
AzureIoTClient 34:6be9c2058664 270 LogError("Cannot set undeliverable here on MODIFIED delivery state");
AzureIoTClient 34:6be9c2058664 271 result = NULL;
AzureIoTClient 34:6be9c2058664 272 }
AzureIoTClient 34:6be9c2058664 273 else if ((message_annotations != NULL) && (modified_set_message_annotations(modified, message_annotations) != 0))
AzureIoTClient 34:6be9c2058664 274 {
AzureIoTClient 34:6be9c2058664 275 LogError("Cannot set message annotations on MODIFIED delivery state");
AzureIoTClient 28:add19eb7defa 276 result = NULL;
AzureIoTClient 28:add19eb7defa 277 }
AzureIoTClient 28:add19eb7defa 278 else
AzureIoTClient 28:add19eb7defa 279 {
AzureIoTClient 28:add19eb7defa 280 result = amqpvalue_create_modified(modified);
AzureIoTClient 34:6be9c2058664 281 if (result == NULL)
AzureIoTClient 34:6be9c2058664 282 {
AzureIoTClient 34:6be9c2058664 283 LogError("Cannot create MODIFIED delivery state AMQP value");
AzureIoTClient 34:6be9c2058664 284 }
AzureIoTClient 34:6be9c2058664 285 else
AzureIoTClient 34:6be9c2058664 286 {
AzureIoTClient 34:6be9c2058664 287 /* all ok */
AzureIoTClient 34:6be9c2058664 288 }
AzureIoTClient 28:add19eb7defa 289 }
Azure.IoT Build 0:6ae2f7bca550 290
AzureIoTClient 28:add19eb7defa 291 modified_destroy(modified);
AzureIoTClient 28:add19eb7defa 292 }
Azure.IoT Build 0:6ae2f7bca550 293
AzureIoTClient 28:add19eb7defa 294 return result;
Azure.IoT Build 0:6ae2f7bca550 295 }