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:
6:641a9672db08
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>
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>
Azure.IoT Build 0:6ae2f7bca550 8 #include "azure_uamqp_c/cbs.h"
Azure.IoT Build 0:6ae2f7bca550 9 #include "azure_uamqp_c/amqp_management.h"
Azure.IoT Build 0:6ae2f7bca550 10 #include "azure_uamqp_c/amqpalloc.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/session.h"
Azure.IoT Build 0:6ae2f7bca550 12
Azure.IoT Build 0:6ae2f7bca550 13 typedef struct CBS_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 14 {
AzureIoTClient 6:641a9672db08 15 AMQP_MANAGEMENT_HANDLE amqp_management;
Azure.IoT Build 0:6ae2f7bca550 16 } CBS_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 17
Azure.IoT Build 0:6ae2f7bca550 18 static int add_string_key_value_pair_to_map(AMQP_VALUE map, const char* key, const char* value)
Azure.IoT Build 0:6ae2f7bca550 19 {
AzureIoTClient 6:641a9672db08 20 int result;
Azure.IoT Build 0:6ae2f7bca550 21
AzureIoTClient 6:641a9672db08 22 AMQP_VALUE key_value = amqpvalue_create_string(key);
AzureIoTClient 6:641a9672db08 23 if (key == NULL)
AzureIoTClient 6:641a9672db08 24 {
AzureIoTClient 6:641a9672db08 25 result = __LINE__;
AzureIoTClient 6:641a9672db08 26 }
AzureIoTClient 6:641a9672db08 27 else
AzureIoTClient 6:641a9672db08 28 {
AzureIoTClient 6:641a9672db08 29 AMQP_VALUE value_value = amqpvalue_create_string(value);
AzureIoTClient 6:641a9672db08 30 if (value_value == NULL)
AzureIoTClient 6:641a9672db08 31 {
AzureIoTClient 6:641a9672db08 32 result = __LINE__;
AzureIoTClient 6:641a9672db08 33 }
AzureIoTClient 6:641a9672db08 34 else
AzureIoTClient 6:641a9672db08 35 {
AzureIoTClient 6:641a9672db08 36 if (amqpvalue_set_map_value(map, key_value, value_value) != 0)
AzureIoTClient 6:641a9672db08 37 {
AzureIoTClient 6:641a9672db08 38 result = __LINE__;
AzureIoTClient 6:641a9672db08 39 }
AzureIoTClient 6:641a9672db08 40 else
AzureIoTClient 6:641a9672db08 41 {
AzureIoTClient 6:641a9672db08 42 result = 0;
AzureIoTClient 6:641a9672db08 43 }
Azure.IoT Build 0:6ae2f7bca550 44
AzureIoTClient 6:641a9672db08 45 amqpvalue_destroy(key_value);
AzureIoTClient 6:641a9672db08 46 }
Azure.IoT Build 0:6ae2f7bca550 47
AzureIoTClient 6:641a9672db08 48 amqpvalue_destroy(value_value);
AzureIoTClient 6:641a9672db08 49 }
Azure.IoT Build 0:6ae2f7bca550 50
AzureIoTClient 6:641a9672db08 51 return result;
Azure.IoT Build 0:6ae2f7bca550 52 }
Azure.IoT Build 0:6ae2f7bca550 53
Azure.IoT Build 0:6ae2f7bca550 54 static int set_pending_operation_properties(MESSAGE_HANDLE message)
Azure.IoT Build 0:6ae2f7bca550 55 {
AzureIoTClient 6:641a9672db08 56 int result = 0;
Azure.IoT Build 0:6ae2f7bca550 57
AzureIoTClient 6:641a9672db08 58 PROPERTIES_HANDLE properties = properties_create();
AzureIoTClient 6:641a9672db08 59 if (properties == NULL)
AzureIoTClient 6:641a9672db08 60 {
AzureIoTClient 6:641a9672db08 61 result = __LINE__;
AzureIoTClient 6:641a9672db08 62 }
AzureIoTClient 6:641a9672db08 63 else
AzureIoTClient 6:641a9672db08 64 {
AzureIoTClient 6:641a9672db08 65 AMQP_VALUE reply_to = amqpvalue_create_address_string("cbs");
AzureIoTClient 6:641a9672db08 66 if (reply_to == NULL)
AzureIoTClient 6:641a9672db08 67 {
AzureIoTClient 6:641a9672db08 68 result = __LINE__;
AzureIoTClient 6:641a9672db08 69 }
AzureIoTClient 6:641a9672db08 70 else
AzureIoTClient 6:641a9672db08 71 {
AzureIoTClient 6:641a9672db08 72 if (properties_set_reply_to(properties, reply_to) != 0)
AzureIoTClient 6:641a9672db08 73 {
AzureIoTClient 6:641a9672db08 74 result = __LINE__;
AzureIoTClient 6:641a9672db08 75 }
Azure.IoT Build 0:6ae2f7bca550 76
AzureIoTClient 6:641a9672db08 77 amqpvalue_destroy(reply_to);
AzureIoTClient 6:641a9672db08 78 }
Azure.IoT Build 0:6ae2f7bca550 79
AzureIoTClient 6:641a9672db08 80 AMQP_VALUE message_id = amqpvalue_create_message_id_ulong(0x43);
AzureIoTClient 6:641a9672db08 81 if (message_id == NULL)
AzureIoTClient 6:641a9672db08 82 {
AzureIoTClient 6:641a9672db08 83 result = __LINE__;
AzureIoTClient 6:641a9672db08 84 }
AzureIoTClient 6:641a9672db08 85 else
AzureIoTClient 6:641a9672db08 86 {
AzureIoTClient 6:641a9672db08 87 if (properties_set_message_id(properties, message_id) != 0)
AzureIoTClient 6:641a9672db08 88 {
AzureIoTClient 6:641a9672db08 89 result = __LINE__;
AzureIoTClient 6:641a9672db08 90 }
Azure.IoT Build 0:6ae2f7bca550 91
AzureIoTClient 6:641a9672db08 92 amqpvalue_destroy(message_id);
AzureIoTClient 6:641a9672db08 93 }
Azure.IoT Build 0:6ae2f7bca550 94
AzureIoTClient 6:641a9672db08 95 if (message_set_properties(message, properties) != 0)
AzureIoTClient 6:641a9672db08 96 {
AzureIoTClient 6:641a9672db08 97 result = __LINE__;
AzureIoTClient 6:641a9672db08 98 }
Azure.IoT Build 0:6ae2f7bca550 99
AzureIoTClient 6:641a9672db08 100 properties_destroy(properties);
AzureIoTClient 6:641a9672db08 101 }
Azure.IoT Build 0:6ae2f7bca550 102
AzureIoTClient 6:641a9672db08 103 return result;
Azure.IoT Build 0:6ae2f7bca550 104 }
Azure.IoT Build 0:6ae2f7bca550 105
Azure.IoT Build 0:6ae2f7bca550 106 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 107 {
AzureIoTClient 6:641a9672db08 108 CBS_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 109
AzureIoTClient 6:641a9672db08 110 if (session == NULL)
AzureIoTClient 6:641a9672db08 111 {
AzureIoTClient 6:641a9672db08 112 result = NULL;
AzureIoTClient 6:641a9672db08 113 }
AzureIoTClient 6:641a9672db08 114 else
AzureIoTClient 6:641a9672db08 115 {
AzureIoTClient 6:641a9672db08 116 result = (CBS_INSTANCE*)amqpalloc_malloc(sizeof(CBS_INSTANCE));
AzureIoTClient 6:641a9672db08 117 if (result != NULL)
AzureIoTClient 6:641a9672db08 118 {
AzureIoTClient 6:641a9672db08 119 result->amqp_management = amqpmanagement_create(session, "$cbs", on_amqp_management_state_changed, callback_context);
AzureIoTClient 6:641a9672db08 120 if (result->amqp_management == NULL)
AzureIoTClient 6:641a9672db08 121 {
AzureIoTClient 6:641a9672db08 122 amqpalloc_free(result);
AzureIoTClient 6:641a9672db08 123 result = NULL;
AzureIoTClient 6:641a9672db08 124 }
AzureIoTClient 6:641a9672db08 125 }
AzureIoTClient 6:641a9672db08 126 }
AzureIoTClient 6:641a9672db08 127 return result;
Azure.IoT Build 0:6ae2f7bca550 128 }
Azure.IoT Build 0:6ae2f7bca550 129
Azure.IoT Build 0:6ae2f7bca550 130 void cbs_destroy(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 131 {
AzureIoTClient 6:641a9672db08 132 if (cbs != NULL)
AzureIoTClient 6:641a9672db08 133 {
AzureIoTClient 6:641a9672db08 134 (void)cbs_close(cbs);
AzureIoTClient 6:641a9672db08 135 amqpmanagement_destroy(cbs->amqp_management);
AzureIoTClient 6:641a9672db08 136 amqpalloc_free(cbs);
AzureIoTClient 6:641a9672db08 137 }
Azure.IoT Build 0:6ae2f7bca550 138 }
Azure.IoT Build 0:6ae2f7bca550 139
Azure.IoT Build 0:6ae2f7bca550 140 int cbs_open(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 141 {
AzureIoTClient 6:641a9672db08 142 int result;
Azure.IoT Build 0:6ae2f7bca550 143
AzureIoTClient 6:641a9672db08 144 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 145 {
AzureIoTClient 6:641a9672db08 146 result = __LINE__;
AzureIoTClient 6:641a9672db08 147 }
AzureIoTClient 6:641a9672db08 148 else
AzureIoTClient 6:641a9672db08 149 {
AzureIoTClient 6:641a9672db08 150 if (amqpmanagement_open(cbs->amqp_management) != 0)
AzureIoTClient 6:641a9672db08 151 {
AzureIoTClient 6:641a9672db08 152 result = __LINE__;
AzureIoTClient 6:641a9672db08 153 }
AzureIoTClient 6:641a9672db08 154 else
AzureIoTClient 6:641a9672db08 155 {
AzureIoTClient 6:641a9672db08 156 result = 0;
AzureIoTClient 6:641a9672db08 157 }
AzureIoTClient 6:641a9672db08 158 }
Azure.IoT Build 0:6ae2f7bca550 159
AzureIoTClient 6:641a9672db08 160 return result;
Azure.IoT Build 0:6ae2f7bca550 161 }
Azure.IoT Build 0:6ae2f7bca550 162
Azure.IoT Build 0:6ae2f7bca550 163 int cbs_close(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 164 {
AzureIoTClient 6:641a9672db08 165 int result;
Azure.IoT Build 0:6ae2f7bca550 166
AzureIoTClient 6:641a9672db08 167 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 168 {
AzureIoTClient 6:641a9672db08 169 result = __LINE__;
AzureIoTClient 6:641a9672db08 170 }
AzureIoTClient 6:641a9672db08 171 else
AzureIoTClient 6:641a9672db08 172 {
AzureIoTClient 6:641a9672db08 173 if (amqpmanagement_close(cbs->amqp_management) != 0)
AzureIoTClient 6:641a9672db08 174 {
AzureIoTClient 6:641a9672db08 175 result = __LINE__;
AzureIoTClient 6:641a9672db08 176 }
AzureIoTClient 6:641a9672db08 177 else
AzureIoTClient 6:641a9672db08 178 {
AzureIoTClient 6:641a9672db08 179 result = 0;
AzureIoTClient 6:641a9672db08 180 }
AzureIoTClient 6:641a9672db08 181 }
Azure.IoT Build 0:6ae2f7bca550 182
AzureIoTClient 6:641a9672db08 183 return result;
Azure.IoT Build 0:6ae2f7bca550 184 }
Azure.IoT Build 0:6ae2f7bca550 185
Azure.IoT Build 0:6ae2f7bca550 186 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 187 {
AzureIoTClient 6:641a9672db08 188 int result;
Azure.IoT Build 0:6ae2f7bca550 189
AzureIoTClient 6:641a9672db08 190 if ((cbs == NULL) ||
AzureIoTClient 6:641a9672db08 191 (token == NULL))
AzureIoTClient 6:641a9672db08 192 {
AzureIoTClient 6:641a9672db08 193 result = __LINE__;
AzureIoTClient 6:641a9672db08 194 }
AzureIoTClient 6:641a9672db08 195 else
AzureIoTClient 6:641a9672db08 196 {
AzureIoTClient 6:641a9672db08 197 MESSAGE_HANDLE message = message_create();
AzureIoTClient 6:641a9672db08 198 if (message == NULL)
AzureIoTClient 6:641a9672db08 199 {
AzureIoTClient 6:641a9672db08 200 result = __LINE__;
AzureIoTClient 6:641a9672db08 201 }
AzureIoTClient 6:641a9672db08 202 else
AzureIoTClient 6:641a9672db08 203 {
AzureIoTClient 6:641a9672db08 204 AMQP_VALUE token_value = amqpvalue_create_string(token);
AzureIoTClient 6:641a9672db08 205 if (token_value == NULL)
AzureIoTClient 6:641a9672db08 206 {
AzureIoTClient 6:641a9672db08 207 message_destroy(message);
AzureIoTClient 6:641a9672db08 208 result = __LINE__;
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 6:641a9672db08 214 result = __LINE__;
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 6:641a9672db08 221 result = __LINE__;
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 6:641a9672db08 227 result = __LINE__;
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 6:641a9672db08 233 result = __LINE__;
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 6:641a9672db08 239 result = __LINE__;
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 6:641a9672db08 245 result = __LINE__;
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 6:641a9672db08 275 result = __LINE__;
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 6:641a9672db08 282 result = __LINE__;
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 6:641a9672db08 289 result = __LINE__;
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 6:641a9672db08 295 result = __LINE__;
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 6:641a9672db08 301 result = __LINE__;
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 6:641a9672db08 307 result = __LINE__;
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 6:641a9672db08 313 result = __LINE__;
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 }