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:
21:f9c433d8e6ca
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 6:641a9672db08 5 #include <stdbool.h>
Azure.IoT Build 0:6ae2f7bca550 6 #include <stdio.h>
Azure.IoT Build 0:6ae2f7bca550 7 #include <string.h>
AzureIoTClient 19:000ab4e6a2c1 8 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/cbs.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqp_management.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 12 #include "azure_uamqp_c/session.h"
Azure.IoT Build 0:6ae2f7bca550 13
Azure.IoT Build 0:6ae2f7bca550 14 typedef struct CBS_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 15 {
AzureIoTClient 6:641a9672db08 16 AMQP_MANAGEMENT_HANDLE amqp_management;
Azure.IoT Build 0:6ae2f7bca550 17 } CBS_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 18
Azure.IoT Build 0:6ae2f7bca550 19 static int add_string_key_value_pair_to_map(AMQP_VALUE map, const char* key, const char* value)
Azure.IoT Build 0:6ae2f7bca550 20 {
AzureIoTClient 6:641a9672db08 21 int result;
Azure.IoT Build 0:6ae2f7bca550 22
AzureIoTClient 6:641a9672db08 23 AMQP_VALUE key_value = amqpvalue_create_string(key);
AzureIoTClient 6:641a9672db08 24 if (key == NULL)
AzureIoTClient 6:641a9672db08 25 {
AzureIoTClient 19:000ab4e6a2c1 26 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 27 }
AzureIoTClient 6:641a9672db08 28 else
AzureIoTClient 6:641a9672db08 29 {
AzureIoTClient 6:641a9672db08 30 AMQP_VALUE value_value = amqpvalue_create_string(value);
AzureIoTClient 6:641a9672db08 31 if (value_value == NULL)
AzureIoTClient 6:641a9672db08 32 {
AzureIoTClient 19:000ab4e6a2c1 33 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 34 }
AzureIoTClient 6:641a9672db08 35 else
AzureIoTClient 6:641a9672db08 36 {
AzureIoTClient 6:641a9672db08 37 if (amqpvalue_set_map_value(map, key_value, value_value) != 0)
AzureIoTClient 6:641a9672db08 38 {
AzureIoTClient 19:000ab4e6a2c1 39 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 40 }
AzureIoTClient 6:641a9672db08 41 else
AzureIoTClient 6:641a9672db08 42 {
AzureIoTClient 6:641a9672db08 43 result = 0;
AzureIoTClient 6:641a9672db08 44 }
Azure.IoT Build 0:6ae2f7bca550 45
AzureIoTClient 6:641a9672db08 46 amqpvalue_destroy(key_value);
AzureIoTClient 6:641a9672db08 47 }
Azure.IoT Build 0:6ae2f7bca550 48
AzureIoTClient 6:641a9672db08 49 amqpvalue_destroy(value_value);
AzureIoTClient 6:641a9672db08 50 }
Azure.IoT Build 0:6ae2f7bca550 51
AzureIoTClient 6:641a9672db08 52 return result;
Azure.IoT Build 0:6ae2f7bca550 53 }
Azure.IoT Build 0:6ae2f7bca550 54
Azure.IoT Build 0:6ae2f7bca550 55 static int set_pending_operation_properties(MESSAGE_HANDLE message)
Azure.IoT Build 0:6ae2f7bca550 56 {
AzureIoTClient 6:641a9672db08 57 int result = 0;
Azure.IoT Build 0:6ae2f7bca550 58
AzureIoTClient 6:641a9672db08 59 PROPERTIES_HANDLE properties = properties_create();
AzureIoTClient 6:641a9672db08 60 if (properties == NULL)
AzureIoTClient 6:641a9672db08 61 {
AzureIoTClient 19:000ab4e6a2c1 62 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 63 }
AzureIoTClient 6:641a9672db08 64 else
AzureIoTClient 6:641a9672db08 65 {
AzureIoTClient 6:641a9672db08 66 AMQP_VALUE reply_to = amqpvalue_create_address_string("cbs");
AzureIoTClient 6:641a9672db08 67 if (reply_to == NULL)
AzureIoTClient 6:641a9672db08 68 {
AzureIoTClient 19:000ab4e6a2c1 69 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 70 }
AzureIoTClient 6:641a9672db08 71 else
AzureIoTClient 6:641a9672db08 72 {
AzureIoTClient 6:641a9672db08 73 if (properties_set_reply_to(properties, reply_to) != 0)
AzureIoTClient 6:641a9672db08 74 {
AzureIoTClient 19:000ab4e6a2c1 75 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 76 }
Azure.IoT Build 0:6ae2f7bca550 77
AzureIoTClient 6:641a9672db08 78 amqpvalue_destroy(reply_to);
AzureIoTClient 6:641a9672db08 79 }
Azure.IoT Build 0:6ae2f7bca550 80
AzureIoTClient 6:641a9672db08 81 AMQP_VALUE message_id = amqpvalue_create_message_id_ulong(0x43);
AzureIoTClient 6:641a9672db08 82 if (message_id == NULL)
AzureIoTClient 6:641a9672db08 83 {
AzureIoTClient 19:000ab4e6a2c1 84 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 85 }
AzureIoTClient 6:641a9672db08 86 else
AzureIoTClient 6:641a9672db08 87 {
AzureIoTClient 6:641a9672db08 88 if (properties_set_message_id(properties, message_id) != 0)
AzureIoTClient 6:641a9672db08 89 {
AzureIoTClient 19:000ab4e6a2c1 90 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 91 }
Azure.IoT Build 0:6ae2f7bca550 92
AzureIoTClient 6:641a9672db08 93 amqpvalue_destroy(message_id);
AzureIoTClient 6:641a9672db08 94 }
Azure.IoT Build 0:6ae2f7bca550 95
AzureIoTClient 6:641a9672db08 96 if (message_set_properties(message, properties) != 0)
AzureIoTClient 6:641a9672db08 97 {
AzureIoTClient 19:000ab4e6a2c1 98 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 99 }
Azure.IoT Build 0:6ae2f7bca550 100
AzureIoTClient 6:641a9672db08 101 properties_destroy(properties);
AzureIoTClient 6:641a9672db08 102 }
Azure.IoT Build 0:6ae2f7bca550 103
AzureIoTClient 6:641a9672db08 104 return result;
Azure.IoT Build 0:6ae2f7bca550 105 }
Azure.IoT Build 0:6ae2f7bca550 106
Azure.IoT Build 0:6ae2f7bca550 107 CBS_HANDLE cbs_create(SESSION_HANDLE session, ON_AMQP_MANAGEMENT_STATE_CHANGED on_amqp_management_state_changed, void* callback_context)
Azure.IoT Build 0:6ae2f7bca550 108 {
AzureIoTClient 6:641a9672db08 109 CBS_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 110
AzureIoTClient 6:641a9672db08 111 if (session == NULL)
AzureIoTClient 6:641a9672db08 112 {
AzureIoTClient 6:641a9672db08 113 result = NULL;
AzureIoTClient 6:641a9672db08 114 }
AzureIoTClient 6:641a9672db08 115 else
AzureIoTClient 6:641a9672db08 116 {
AzureIoTClient 6:641a9672db08 117 result = (CBS_INSTANCE*)amqpalloc_malloc(sizeof(CBS_INSTANCE));
AzureIoTClient 6:641a9672db08 118 if (result != NULL)
AzureIoTClient 6:641a9672db08 119 {
AzureIoTClient 6:641a9672db08 120 result->amqp_management = amqpmanagement_create(session, "$cbs", on_amqp_management_state_changed, callback_context);
AzureIoTClient 6:641a9672db08 121 if (result->amqp_management == NULL)
AzureIoTClient 6:641a9672db08 122 {
AzureIoTClient 6:641a9672db08 123 amqpalloc_free(result);
AzureIoTClient 6:641a9672db08 124 result = NULL;
AzureIoTClient 6:641a9672db08 125 }
AzureIoTClient 6:641a9672db08 126 }
AzureIoTClient 6:641a9672db08 127 }
AzureIoTClient 6:641a9672db08 128 return result;
Azure.IoT Build 0:6ae2f7bca550 129 }
Azure.IoT Build 0:6ae2f7bca550 130
Azure.IoT Build 0:6ae2f7bca550 131 void cbs_destroy(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 132 {
AzureIoTClient 6:641a9672db08 133 if (cbs != NULL)
AzureIoTClient 6:641a9672db08 134 {
AzureIoTClient 6:641a9672db08 135 (void)cbs_close(cbs);
AzureIoTClient 6:641a9672db08 136 amqpmanagement_destroy(cbs->amqp_management);
AzureIoTClient 6:641a9672db08 137 amqpalloc_free(cbs);
AzureIoTClient 6:641a9672db08 138 }
Azure.IoT Build 0:6ae2f7bca550 139 }
Azure.IoT Build 0:6ae2f7bca550 140
Azure.IoT Build 0:6ae2f7bca550 141 int cbs_open(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 142 {
AzureIoTClient 6:641a9672db08 143 int result;
Azure.IoT Build 0:6ae2f7bca550 144
AzureIoTClient 6:641a9672db08 145 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 146 {
AzureIoTClient 19:000ab4e6a2c1 147 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 148 }
AzureIoTClient 6:641a9672db08 149 else
AzureIoTClient 6:641a9672db08 150 {
AzureIoTClient 6:641a9672db08 151 if (amqpmanagement_open(cbs->amqp_management) != 0)
AzureIoTClient 6:641a9672db08 152 {
AzureIoTClient 19:000ab4e6a2c1 153 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 154 }
AzureIoTClient 6:641a9672db08 155 else
AzureIoTClient 6:641a9672db08 156 {
AzureIoTClient 6:641a9672db08 157 result = 0;
AzureIoTClient 6:641a9672db08 158 }
AzureIoTClient 6:641a9672db08 159 }
Azure.IoT Build 0:6ae2f7bca550 160
AzureIoTClient 6:641a9672db08 161 return result;
Azure.IoT Build 0:6ae2f7bca550 162 }
Azure.IoT Build 0:6ae2f7bca550 163
Azure.IoT Build 0:6ae2f7bca550 164 int cbs_close(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 165 {
AzureIoTClient 6:641a9672db08 166 int result;
Azure.IoT Build 0:6ae2f7bca550 167
AzureIoTClient 6:641a9672db08 168 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 169 {
AzureIoTClient 19:000ab4e6a2c1 170 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 171 }
AzureIoTClient 6:641a9672db08 172 else
AzureIoTClient 6:641a9672db08 173 {
AzureIoTClient 6:641a9672db08 174 if (amqpmanagement_close(cbs->amqp_management) != 0)
AzureIoTClient 6:641a9672db08 175 {
AzureIoTClient 19:000ab4e6a2c1 176 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 177 }
AzureIoTClient 6:641a9672db08 178 else
AzureIoTClient 6:641a9672db08 179 {
AzureIoTClient 6:641a9672db08 180 result = 0;
AzureIoTClient 6:641a9672db08 181 }
AzureIoTClient 6:641a9672db08 182 }
Azure.IoT Build 0:6ae2f7bca550 183
AzureIoTClient 6:641a9672db08 184 return result;
Azure.IoT Build 0:6ae2f7bca550 185 }
Azure.IoT Build 0:6ae2f7bca550 186
Azure.IoT Build 0:6ae2f7bca550 187 int cbs_put_token(CBS_HANDLE cbs, const char* type, const char* audience, const char* token, ON_CBS_OPERATION_COMPLETE on_operation_complete, void* context)
Azure.IoT Build 0:6ae2f7bca550 188 {
AzureIoTClient 6:641a9672db08 189 int result;
Azure.IoT Build 0:6ae2f7bca550 190
AzureIoTClient 6:641a9672db08 191 if ((cbs == NULL) ||
AzureIoTClient 6:641a9672db08 192 (token == NULL))
AzureIoTClient 6:641a9672db08 193 {
AzureIoTClient 19:000ab4e6a2c1 194 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 195 }
AzureIoTClient 6:641a9672db08 196 else
AzureIoTClient 6:641a9672db08 197 {
AzureIoTClient 6:641a9672db08 198 MESSAGE_HANDLE message = message_create();
AzureIoTClient 6:641a9672db08 199 if (message == NULL)
AzureIoTClient 6:641a9672db08 200 {
AzureIoTClient 19:000ab4e6a2c1 201 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 202 }
AzureIoTClient 6:641a9672db08 203 else
AzureIoTClient 6:641a9672db08 204 {
AzureIoTClient 6:641a9672db08 205 AMQP_VALUE token_value = amqpvalue_create_string(token);
AzureIoTClient 6:641a9672db08 206 if (token_value == NULL)
AzureIoTClient 6:641a9672db08 207 {
AzureIoTClient 19:000ab4e6a2c1 208 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 209 }
AzureIoTClient 6:641a9672db08 210 else
AzureIoTClient 6:641a9672db08 211 {
AzureIoTClient 6:641a9672db08 212 if (message_set_body_amqp_value(message, token_value) != 0)
AzureIoTClient 6:641a9672db08 213 {
AzureIoTClient 19:000ab4e6a2c1 214 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 215 }
AzureIoTClient 6:641a9672db08 216 else
AzureIoTClient 6:641a9672db08 217 {
AzureIoTClient 6:641a9672db08 218 AMQP_VALUE application_properties = amqpvalue_create_map();
AzureIoTClient 6:641a9672db08 219 if (application_properties == NULL)
AzureIoTClient 6:641a9672db08 220 {
AzureIoTClient 19:000ab4e6a2c1 221 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 222 }
AzureIoTClient 6:641a9672db08 223 else
AzureIoTClient 6:641a9672db08 224 {
AzureIoTClient 6:641a9672db08 225 if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0)
AzureIoTClient 6:641a9672db08 226 {
AzureIoTClient 19:000ab4e6a2c1 227 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 228 }
AzureIoTClient 6:641a9672db08 229 else
AzureIoTClient 6:641a9672db08 230 {
AzureIoTClient 6:641a9672db08 231 if (message_set_application_properties(message, application_properties) != 0)
AzureIoTClient 6:641a9672db08 232 {
AzureIoTClient 19:000ab4e6a2c1 233 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 234 }
AzureIoTClient 6:641a9672db08 235 else
AzureIoTClient 6:641a9672db08 236 {
AzureIoTClient 6:641a9672db08 237 if (set_pending_operation_properties(message) != 0)
AzureIoTClient 6:641a9672db08 238 {
AzureIoTClient 19:000ab4e6a2c1 239 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 240 }
AzureIoTClient 6:641a9672db08 241 else
AzureIoTClient 6:641a9672db08 242 {
AzureIoTClient 6:641a9672db08 243 if (amqpmanagement_start_operation(cbs->amqp_management, "put-token", type, NULL, message, (ON_OPERATION_COMPLETE)on_operation_complete, context) != 0)
AzureIoTClient 6:641a9672db08 244 {
AzureIoTClient 19:000ab4e6a2c1 245 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 246 }
AzureIoTClient 6:641a9672db08 247 else
AzureIoTClient 6:641a9672db08 248 {
AzureIoTClient 6:641a9672db08 249 result = 0;
AzureIoTClient 6:641a9672db08 250 }
AzureIoTClient 6:641a9672db08 251 }
AzureIoTClient 6:641a9672db08 252 }
AzureIoTClient 6:641a9672db08 253 }
Azure.IoT Build 0:6ae2f7bca550 254
AzureIoTClient 6:641a9672db08 255 amqpvalue_destroy(application_properties);
AzureIoTClient 6:641a9672db08 256 }
Azure.IoT Build 0:6ae2f7bca550 257
AzureIoTClient 6:641a9672db08 258 amqpvalue_destroy(token_value);
AzureIoTClient 6:641a9672db08 259 }
AzureIoTClient 6:641a9672db08 260 }
Azure.IoT Build 0:6ae2f7bca550 261
AzureIoTClient 6:641a9672db08 262 message_destroy(message);
AzureIoTClient 6:641a9672db08 263 }
AzureIoTClient 6:641a9672db08 264 }
Azure.IoT Build 0:6ae2f7bca550 265
AzureIoTClient 6:641a9672db08 266 return result;
Azure.IoT Build 0:6ae2f7bca550 267 }
Azure.IoT Build 0:6ae2f7bca550 268
Azure.IoT Build 0:6ae2f7bca550 269 int cbs_delete_token(CBS_HANDLE cbs, const char* type, const char* audience, ON_CBS_OPERATION_COMPLETE on_operation_complete, void* context)
Azure.IoT Build 0:6ae2f7bca550 270 {
AzureIoTClient 6:641a9672db08 271 int result;
Azure.IoT Build 0:6ae2f7bca550 272
AzureIoTClient 6:641a9672db08 273 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 274 {
AzureIoTClient 19:000ab4e6a2c1 275 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 276 }
AzureIoTClient 6:641a9672db08 277 else
AzureIoTClient 6:641a9672db08 278 {
AzureIoTClient 6:641a9672db08 279 MESSAGE_HANDLE message = message_create();
AzureIoTClient 6:641a9672db08 280 if (message == NULL)
AzureIoTClient 6:641a9672db08 281 {
AzureIoTClient 19:000ab4e6a2c1 282 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 283 }
AzureIoTClient 6:641a9672db08 284 else
AzureIoTClient 6:641a9672db08 285 {
AzureIoTClient 6:641a9672db08 286 AMQP_VALUE application_properties = amqpvalue_create_map();
AzureIoTClient 6:641a9672db08 287 if (application_properties == NULL)
AzureIoTClient 6:641a9672db08 288 {
AzureIoTClient 19:000ab4e6a2c1 289 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 290 }
AzureIoTClient 6:641a9672db08 291 else
AzureIoTClient 6:641a9672db08 292 {
AzureIoTClient 6:641a9672db08 293 if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0)
AzureIoTClient 6:641a9672db08 294 {
AzureIoTClient 19:000ab4e6a2c1 295 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 296 }
AzureIoTClient 6:641a9672db08 297 else
AzureIoTClient 6:641a9672db08 298 {
AzureIoTClient 6:641a9672db08 299 if (message_set_application_properties(message, application_properties) != 0)
AzureIoTClient 6:641a9672db08 300 {
AzureIoTClient 19:000ab4e6a2c1 301 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 302 }
AzureIoTClient 6:641a9672db08 303 else
AzureIoTClient 6:641a9672db08 304 {
AzureIoTClient 6:641a9672db08 305 if (set_pending_operation_properties(message) != 0)
AzureIoTClient 6:641a9672db08 306 {
AzureIoTClient 19:000ab4e6a2c1 307 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 308 }
AzureIoTClient 6:641a9672db08 309 else
AzureIoTClient 6:641a9672db08 310 {
AzureIoTClient 6:641a9672db08 311 if (amqpmanagement_start_operation(cbs->amqp_management, "delete-token", type, NULL, message, (ON_OPERATION_COMPLETE)on_operation_complete, context) != 0)
AzureIoTClient 6:641a9672db08 312 {
AzureIoTClient 19:000ab4e6a2c1 313 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 314 }
AzureIoTClient 6:641a9672db08 315 else
AzureIoTClient 6:641a9672db08 316 {
AzureIoTClient 6:641a9672db08 317 result = 0;
AzureIoTClient 6:641a9672db08 318 }
AzureIoTClient 6:641a9672db08 319 }
AzureIoTClient 6:641a9672db08 320 }
AzureIoTClient 6:641a9672db08 321 }
Azure.IoT Build 0:6ae2f7bca550 322
AzureIoTClient 6:641a9672db08 323 amqpvalue_destroy(application_properties);
AzureIoTClient 6:641a9672db08 324 }
Azure.IoT Build 0:6ae2f7bca550 325
AzureIoTClient 6:641a9672db08 326 message_destroy(message);
AzureIoTClient 6:641a9672db08 327 }
AzureIoTClient 6:641a9672db08 328 }
AzureIoTClient 6:641a9672db08 329 return result;
AzureIoTClient 6:641a9672db08 330 }
Azure.IoT Build 0:6ae2f7bca550 331
AzureIoTClient 6:641a9672db08 332 void cbs_set_trace(CBS_HANDLE cbs, bool traceOn)
AzureIoTClient 6:641a9672db08 333 {
AzureIoTClient 6:641a9672db08 334 if (cbs != NULL)
AzureIoTClient 6:641a9672db08 335 {
AzureIoTClient 6:641a9672db08 336 amqpmanagement_set_trace(cbs->amqp_management, traceOn);
AzureIoTClient 6:641a9672db08 337 }
Azure.IoT Build 0:6ae2f7bca550 338 }