A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Committer:
AzureIoTClient
Date:
Thu Apr 06 14:11:27 2017 -0700
Revision:
22:524bded3f7a8
Parent:
21:f9c433d8e6ca
Child:
23:1111ee8bcba4
1.1.11

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"
AzureIoTClient 21:f9c433d8e6ca 9 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 21:f9c433d8e6ca 10 #include "azure_c_shared_utility/singlylinkedlist.h"
Azure.IoT Build 0:6ae2f7bca550 11 #include "azure_uamqp_c/cbs.h"
Azure.IoT Build 0:6ae2f7bca550 12 #include "azure_uamqp_c/amqp_management.h"
Azure.IoT Build 0:6ae2f7bca550 13 #include "azure_uamqp_c/session.h"
Azure.IoT Build 0:6ae2f7bca550 14
AzureIoTClient 21:f9c433d8e6ca 15 typedef enum CBS_STATE_TAG
AzureIoTClient 21:f9c433d8e6ca 16 {
AzureIoTClient 21:f9c433d8e6ca 17 CBS_STATE_CLOSED,
AzureIoTClient 21:f9c433d8e6ca 18 CBS_STATE_OPENING,
AzureIoTClient 21:f9c433d8e6ca 19 CBS_STATE_OPEN,
AzureIoTClient 21:f9c433d8e6ca 20 CBS_STATE_ERROR
AzureIoTClient 21:f9c433d8e6ca 21 } CBS_STATE;
AzureIoTClient 21:f9c433d8e6ca 22
AzureIoTClient 21:f9c433d8e6ca 23 typedef struct CBS_OPERATION_TAG
AzureIoTClient 21:f9c433d8e6ca 24 {
AzureIoTClient 21:f9c433d8e6ca 25 ON_CBS_OPERATION_COMPLETE on_cbs_operation_complete;
AzureIoTClient 21:f9c433d8e6ca 26 void* on_cbs_operation_complete_context;
AzureIoTClient 21:f9c433d8e6ca 27 SINGLYLINKEDLIST_HANDLE pending_operations;
AzureIoTClient 21:f9c433d8e6ca 28 } CBS_OPERATION;
AzureIoTClient 21:f9c433d8e6ca 29
Azure.IoT Build 0:6ae2f7bca550 30 typedef struct CBS_INSTANCE_TAG
Azure.IoT Build 0:6ae2f7bca550 31 {
AzureIoTClient 6:641a9672db08 32 AMQP_MANAGEMENT_HANDLE amqp_management;
AzureIoTClient 21:f9c433d8e6ca 33 CBS_STATE cbs_state;
AzureIoTClient 21:f9c433d8e6ca 34 ON_CBS_OPEN_COMPLETE on_cbs_open_complete;
AzureIoTClient 21:f9c433d8e6ca 35 void* on_cbs_open_complete_context;
AzureIoTClient 21:f9c433d8e6ca 36 ON_CBS_ERROR on_cbs_error;
AzureIoTClient 21:f9c433d8e6ca 37 void* on_cbs_error_context;
AzureIoTClient 21:f9c433d8e6ca 38 SINGLYLINKEDLIST_HANDLE pending_operations;
Azure.IoT Build 0:6ae2f7bca550 39 } CBS_INSTANCE;
Azure.IoT Build 0:6ae2f7bca550 40
Azure.IoT Build 0:6ae2f7bca550 41 static int add_string_key_value_pair_to_map(AMQP_VALUE map, const char* key, const char* value)
Azure.IoT Build 0:6ae2f7bca550 42 {
AzureIoTClient 6:641a9672db08 43 int result;
Azure.IoT Build 0:6ae2f7bca550 44
AzureIoTClient 6:641a9672db08 45 AMQP_VALUE key_value = amqpvalue_create_string(key);
AzureIoTClient 21:f9c433d8e6ca 46 if (key_value == NULL)
AzureIoTClient 6:641a9672db08 47 {
AzureIoTClient 21:f9c433d8e6ca 48 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 49 LogError("Failed creating value for property key %s", key);
AzureIoTClient 19:000ab4e6a2c1 50 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 51 }
AzureIoTClient 6:641a9672db08 52 else
AzureIoTClient 6:641a9672db08 53 {
AzureIoTClient 6:641a9672db08 54 AMQP_VALUE value_value = amqpvalue_create_string(value);
AzureIoTClient 6:641a9672db08 55 if (value_value == NULL)
AzureIoTClient 6:641a9672db08 56 {
AzureIoTClient 21:f9c433d8e6ca 57 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 58 LogError("Failed creating value for property value %s", value);
AzureIoTClient 19:000ab4e6a2c1 59 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 60 }
AzureIoTClient 6:641a9672db08 61 else
AzureIoTClient 6:641a9672db08 62 {
AzureIoTClient 6:641a9672db08 63 if (amqpvalue_set_map_value(map, key_value, value_value) != 0)
AzureIoTClient 6:641a9672db08 64 {
AzureIoTClient 21:f9c433d8e6ca 65 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 66 LogError("Failed inserting key/value pair in the map");
AzureIoTClient 19:000ab4e6a2c1 67 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 68 }
AzureIoTClient 6:641a9672db08 69 else
AzureIoTClient 6:641a9672db08 70 {
AzureIoTClient 6:641a9672db08 71 result = 0;
AzureIoTClient 6:641a9672db08 72 }
Azure.IoT Build 0:6ae2f7bca550 73
AzureIoTClient 21:f9c433d8e6ca 74 amqpvalue_destroy(value_value);
AzureIoTClient 6:641a9672db08 75 }
Azure.IoT Build 0:6ae2f7bca550 76
AzureIoTClient 21:f9c433d8e6ca 77 amqpvalue_destroy(key_value);
AzureIoTClient 6:641a9672db08 78 }
Azure.IoT Build 0:6ae2f7bca550 79
AzureIoTClient 6:641a9672db08 80 return result;
Azure.IoT Build 0:6ae2f7bca550 81 }
Azure.IoT Build 0:6ae2f7bca550 82
AzureIoTClient 22:524bded3f7a8 83 static void on_underlying_amqp_management_open_complete(void* context, AMQP_MANAGEMENT_OPEN_RESULT open_result)
Azure.IoT Build 0:6ae2f7bca550 84 {
AzureIoTClient 21:f9c433d8e6ca 85 if (context == NULL)
AzureIoTClient 6:641a9672db08 86 {
AzureIoTClient 22:524bded3f7a8 87 /* Codes_SRS_CBS_01_105: [ When `on_amqp_management_open_complete` is called with NULL `context`, it shall do nothing. ]*/
AzureIoTClient 22:524bded3f7a8 88 LogError("on_underlying_amqp_management_open_complete called with NULL context");
AzureIoTClient 6:641a9672db08 89 }
AzureIoTClient 6:641a9672db08 90 else
AzureIoTClient 6:641a9672db08 91 {
AzureIoTClient 21:f9c433d8e6ca 92 CBS_HANDLE cbs = (CBS_HANDLE)context;
AzureIoTClient 21:f9c433d8e6ca 93
AzureIoTClient 22:524bded3f7a8 94 switch (cbs->cbs_state)
AzureIoTClient 6:641a9672db08 95 {
AzureIoTClient 22:524bded3f7a8 96 default:
AzureIoTClient 22:524bded3f7a8 97 LogError("AMQP management open complete in unknown state");
AzureIoTClient 22:524bded3f7a8 98 break;
AzureIoTClient 22:524bded3f7a8 99
AzureIoTClient 22:524bded3f7a8 100 case CBS_STATE_CLOSED:
AzureIoTClient 22:524bded3f7a8 101 case CBS_STATE_ERROR:
AzureIoTClient 22:524bded3f7a8 102 LogError("Unexpected AMQP management open complete");
AzureIoTClient 22:524bded3f7a8 103 break;
AzureIoTClient 22:524bded3f7a8 104
AzureIoTClient 22:524bded3f7a8 105 case CBS_STATE_OPEN:
AzureIoTClient 22:524bded3f7a8 106 LogError("Unexpected AMQP management open complete in OPEN");
AzureIoTClient 22:524bded3f7a8 107 /* Codes_SRS_CBS_01_109: [ When `on_amqp_management_open_complete` is called when the CBS is OPEN, the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 108 cbs->cbs_state = CBS_STATE_ERROR;
AzureIoTClient 22:524bded3f7a8 109 cbs->on_cbs_error(cbs->on_cbs_error_context);
AzureIoTClient 22:524bded3f7a8 110 break;
AzureIoTClient 22:524bded3f7a8 111
AzureIoTClient 22:524bded3f7a8 112 case CBS_STATE_OPENING:
AzureIoTClient 22:524bded3f7a8 113 {
AzureIoTClient 22:524bded3f7a8 114 switch (open_result)
AzureIoTClient 21:f9c433d8e6ca 115 {
AzureIoTClient 21:f9c433d8e6ca 116 default:
AzureIoTClient 22:524bded3f7a8 117 LogError("Unknown AMQP management state");
AzureIoTClient 22:524bded3f7a8 118
AzureIoTClient 22:524bded3f7a8 119 case AMQP_MANAGEMENT_OPEN_ERROR:
AzureIoTClient 22:524bded3f7a8 120 cbs->cbs_state = CBS_STATE_CLOSED;
AzureIoTClient 22:524bded3f7a8 121 /* Codes_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
AzureIoTClient 22:524bded3f7a8 122 (void)amqp_management_close(cbs->amqp_management);
AzureIoTClient 22:524bded3f7a8 123 /* Codes_SRS_CBS_01_107: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_ERROR` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 124 cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_ERROR);
AzureIoTClient 21:f9c433d8e6ca 125 break;
AzureIoTClient 21:f9c433d8e6ca 126
AzureIoTClient 22:524bded3f7a8 127 case AMQP_MANAGEMENT_OPEN_CANCELLED:
AzureIoTClient 22:524bded3f7a8 128 cbs->cbs_state = CBS_STATE_CLOSED;
AzureIoTClient 22:524bded3f7a8 129 /* Codes_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
AzureIoTClient 22:524bded3f7a8 130 (void)amqp_management_close(cbs->amqp_management);
AzureIoTClient 22:524bded3f7a8 131 /* Codes_SRS_CBS_01_108: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_CANCELLED` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_CANCELLED` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 132 cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_CANCELLED);
AzureIoTClient 21:f9c433d8e6ca 133 break;
AzureIoTClient 21:f9c433d8e6ca 134
AzureIoTClient 22:524bded3f7a8 135 case AMQP_MANAGEMENT_OPEN_OK:
AzureIoTClient 22:524bded3f7a8 136 /* Codes_SRS_CBS_01_106: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_OK` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_OK` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 137 cbs->cbs_state = CBS_STATE_OPEN;
AzureIoTClient 22:524bded3f7a8 138 cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_OK);
AzureIoTClient 21:f9c433d8e6ca 139 break;
AzureIoTClient 21:f9c433d8e6ca 140 }
AzureIoTClient 22:524bded3f7a8 141 break;
AzureIoTClient 21:f9c433d8e6ca 142 }
AzureIoTClient 21:f9c433d8e6ca 143 }
AzureIoTClient 21:f9c433d8e6ca 144 }
AzureIoTClient 21:f9c433d8e6ca 145 }
AzureIoTClient 21:f9c433d8e6ca 146
AzureIoTClient 22:524bded3f7a8 147 static void on_underlying_amqp_management_error(void* context)
AzureIoTClient 21:f9c433d8e6ca 148 {
AzureIoTClient 21:f9c433d8e6ca 149 if (context == NULL)
AzureIoTClient 21:f9c433d8e6ca 150 {
AzureIoTClient 22:524bded3f7a8 151 /* Codes_SRS_CBS_01_110: [ When `on_amqp_management_error` is called with NULL `context`, it shall do nothing. ]*/
AzureIoTClient 22:524bded3f7a8 152 LogError("on_underlying_amqp_management_error called with NULL context");
AzureIoTClient 22:524bded3f7a8 153 }
AzureIoTClient 22:524bded3f7a8 154 else
AzureIoTClient 22:524bded3f7a8 155 {
AzureIoTClient 22:524bded3f7a8 156 CBS_HANDLE cbs = (CBS_HANDLE)context;
AzureIoTClient 22:524bded3f7a8 157
AzureIoTClient 22:524bded3f7a8 158 switch (cbs->cbs_state)
AzureIoTClient 22:524bded3f7a8 159 {
AzureIoTClient 22:524bded3f7a8 160 default:
AzureIoTClient 22:524bded3f7a8 161 LogError("AMQP management error in unknown state");
AzureIoTClient 22:524bded3f7a8 162 break;
AzureIoTClient 22:524bded3f7a8 163
AzureIoTClient 22:524bded3f7a8 164 case CBS_STATE_CLOSED:
AzureIoTClient 22:524bded3f7a8 165 LogError("Unexpected AMQP error in CLOSED state");
AzureIoTClient 22:524bded3f7a8 166 break;
AzureIoTClient 22:524bded3f7a8 167
AzureIoTClient 22:524bded3f7a8 168 case CBS_STATE_OPENING:
AzureIoTClient 22:524bded3f7a8 169 cbs->cbs_state = CBS_STATE_CLOSED;
AzureIoTClient 22:524bded3f7a8 170 /* Codes_SRS_CBS_01_114: [ Additionally the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
AzureIoTClient 22:524bded3f7a8 171 (void)amqp_management_close(cbs->amqp_management);
AzureIoTClient 22:524bded3f7a8 172 /* Codes_SRS_CBS_01_111: [ If CBS is OPENING the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 173 cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_ERROR);
AzureIoTClient 22:524bded3f7a8 174 break;
AzureIoTClient 22:524bded3f7a8 175
AzureIoTClient 22:524bded3f7a8 176 case CBS_STATE_OPEN:
AzureIoTClient 22:524bded3f7a8 177 /* Codes_SRS_CBS_01_112: [ If CBS is OPEN the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/
AzureIoTClient 22:524bded3f7a8 178 cbs->cbs_state = CBS_STATE_ERROR;
AzureIoTClient 22:524bded3f7a8 179 cbs->on_cbs_error(cbs->on_cbs_error_context);
AzureIoTClient 22:524bded3f7a8 180 break;
AzureIoTClient 22:524bded3f7a8 181 }
AzureIoTClient 22:524bded3f7a8 182 }
AzureIoTClient 22:524bded3f7a8 183 }
AzureIoTClient 22:524bded3f7a8 184
AzureIoTClient 22:524bded3f7a8 185 static void on_amqp_management_execute_operation_complete(void* context, AMQP_MANAGEMENT_EXECUTE_OPERATION_RESULT execute_operation_result, unsigned int status_code, const char* status_description)
AzureIoTClient 22:524bded3f7a8 186 {
AzureIoTClient 22:524bded3f7a8 187 if (context == NULL)
AzureIoTClient 22:524bded3f7a8 188 {
AzureIoTClient 22:524bded3f7a8 189 /* Codes_SRS_CBS_01_091: [ When `on_amqp_management_execute_operation_complete` is called with a NULL context it shall do nothing. ]*/
AzureIoTClient 22:524bded3f7a8 190 LogError("on_amqp_management_execute_operation_complete called with NULL context");
AzureIoTClient 21:f9c433d8e6ca 191 }
AzureIoTClient 21:f9c433d8e6ca 192 else
AzureIoTClient 21:f9c433d8e6ca 193 {
AzureIoTClient 21:f9c433d8e6ca 194 /* Codes_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
AzureIoTClient 21:f9c433d8e6ca 195 CBS_OPERATION* cbs_operation = (CBS_OPERATION*)singlylinkedlist_item_get_value((LIST_ITEM_HANDLE)context);
AzureIoTClient 21:f9c433d8e6ca 196 CBS_OPERATION_RESULT cbs_operation_result;
AzureIoTClient 21:f9c433d8e6ca 197
AzureIoTClient 21:f9c433d8e6ca 198 if (cbs_operation == NULL)
AzureIoTClient 21:f9c433d8e6ca 199 {
AzureIoTClient 21:f9c433d8e6ca 200 LogError("NULL cbs_operation");
AzureIoTClient 6:641a9672db08 201 }
AzureIoTClient 6:641a9672db08 202 else
AzureIoTClient 6:641a9672db08 203 {
AzureIoTClient 22:524bded3f7a8 204 switch (execute_operation_result)
AzureIoTClient 6:641a9672db08 205 {
AzureIoTClient 21:f9c433d8e6ca 206 default:
AzureIoTClient 21:f9c433d8e6ca 207 cbs_operation_result = CBS_OPERATION_RESULT_CBS_ERROR;
AzureIoTClient 21:f9c433d8e6ca 208 break;
AzureIoTClient 21:f9c433d8e6ca 209
AzureIoTClient 22:524bded3f7a8 210 case AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS:
AzureIoTClient 22:524bded3f7a8 211 /* Tests_SRS_CBS_01_094: [ When `on_amqp_management_execute_operation_complete` is called with `OPERATION_RESULT_OPERATION_FAILED`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OPERATION_FAILED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 212 cbs_operation_result = CBS_OPERATION_RESULT_OPERATION_FAILED;
AzureIoTClient 21:f9c433d8e6ca 213 break;
AzureIoTClient 21:f9c433d8e6ca 214
AzureIoTClient 22:524bded3f7a8 215 case AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR:
AzureIoTClient 22:524bded3f7a8 216 /* Tests_SRS_CBS_01_093: [ When `on_amqp_management_execute_operation_complete` is called with `OPERATION_RESULT_ERROR`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_CBS_ERROR` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 217 cbs_operation_result = CBS_OPERATION_RESULT_CBS_ERROR;
AzureIoTClient 21:f9c433d8e6ca 218 break;
AzureIoTClient 21:f9c433d8e6ca 219
AzureIoTClient 22:524bded3f7a8 220 case AMQP_MANAGEMENT_EXECUTE_OPERATION_OK:
AzureIoTClient 22:524bded3f7a8 221 /* Codes_SRS_CBS_01_092: [ When `on_amqp_management_execute_operation_complete` is called with `OPERATION_RESULT_OK`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OK` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 222 cbs_operation_result = CBS_OPERATION_RESULT_OK;
AzureIoTClient 21:f9c433d8e6ca 223 break;
AzureIoTClient 6:641a9672db08 224 }
Azure.IoT Build 0:6ae2f7bca550 225
AzureIoTClient 21:f9c433d8e6ca 226 /* Codes_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
AzureIoTClient 21:f9c433d8e6ca 227 /* Codes_SRS_CBS_01_014: [ The response message has the following application-properties: ]*/
AzureIoTClient 21:f9c433d8e6ca 228 /* Codes_SRS_CBS_01_013: [ status-code No int HTTP response code [RFC2616]. ]*/
AzureIoTClient 21:f9c433d8e6ca 229 /* Codes_SRS_CBS_01_015: [ status-description Yes string Description of the status. ]*/
AzureIoTClient 21:f9c433d8e6ca 230 /* Codes_SRS_CBS_01_016: [ The body of the message MUST be empty. ]*/
AzureIoTClient 21:f9c433d8e6ca 231 /* Codes_SRS_CBS_01_026: [ The response message has the following application-properties: ]*/
AzureIoTClient 21:f9c433d8e6ca 232 /* Codes_SRS_CBS_01_027: [ status-code Yes int HTTP response code [RFC2616]. ]*/
AzureIoTClient 21:f9c433d8e6ca 233 /* Codes_SRS_CBS_01_028: [ status-description No string Description of the status. ]*/
AzureIoTClient 21:f9c433d8e6ca 234 /* Codes_SRS_CBS_01_029: [ The body of the message MUST be empty. ]*/
AzureIoTClient 21:f9c433d8e6ca 235 cbs_operation->on_cbs_operation_complete(cbs_operation->on_cbs_operation_complete_context, cbs_operation_result, status_code, status_description);
Azure.IoT Build 0:6ae2f7bca550 236
AzureIoTClient 21:f9c433d8e6ca 237 /* Codes_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
AzureIoTClient 21:f9c433d8e6ca 238 if (singlylinkedlist_remove(cbs_operation->pending_operations, (LIST_ITEM_HANDLE)context) != 0)
AzureIoTClient 6:641a9672db08 239 {
AzureIoTClient 21:f9c433d8e6ca 240 LogError("Failed removing operation from the pending list");
AzureIoTClient 6:641a9672db08 241 }
Azure.IoT Build 0:6ae2f7bca550 242
AzureIoTClient 21:f9c433d8e6ca 243 /* Codes_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
AzureIoTClient 21:f9c433d8e6ca 244 free(cbs_operation);
AzureIoTClient 6:641a9672db08 245 }
AzureIoTClient 6:641a9672db08 246 }
Azure.IoT Build 0:6ae2f7bca550 247 }
Azure.IoT Build 0:6ae2f7bca550 248
AzureIoTClient 21:f9c433d8e6ca 249 CBS_HANDLE cbs_create(SESSION_HANDLE session)
Azure.IoT Build 0:6ae2f7bca550 250 {
AzureIoTClient 6:641a9672db08 251 CBS_INSTANCE* result;
Azure.IoT Build 0:6ae2f7bca550 252
AzureIoTClient 6:641a9672db08 253 if (session == NULL)
AzureIoTClient 6:641a9672db08 254 {
AzureIoTClient 21:f9c433d8e6ca 255 /* Codes_SRS_CBS_01_033: [** If `session` is NULL then `cbs_create` shall fail and return NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 256 LogError("NULL session handle");
AzureIoTClient 6:641a9672db08 257 result = NULL;
AzureIoTClient 6:641a9672db08 258 }
AzureIoTClient 6:641a9672db08 259 else
AzureIoTClient 6:641a9672db08 260 {
AzureIoTClient 21:f9c433d8e6ca 261 result = (CBS_INSTANCE*)malloc(sizeof(CBS_INSTANCE));
AzureIoTClient 21:f9c433d8e6ca 262 if (result == NULL)
AzureIoTClient 21:f9c433d8e6ca 263 {
AzureIoTClient 21:f9c433d8e6ca 264 /* Codes_SRS_CBS_01_076: [ If allocating memory for the new handle fails, `cbs_create` shall fail and return NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 265 LogError("Cannot allocate memory for cbs instance.");
AzureIoTClient 21:f9c433d8e6ca 266 }
AzureIoTClient 21:f9c433d8e6ca 267 else
AzureIoTClient 6:641a9672db08 268 {
AzureIoTClient 21:f9c433d8e6ca 269 /* Codes_SRS_CBS_01_097: [ `cbs_create` shall create a singly linked list for pending operations by calling `singlylinkedlist_create`. ]*/
AzureIoTClient 21:f9c433d8e6ca 270 result->pending_operations = singlylinkedlist_create();
AzureIoTClient 21:f9c433d8e6ca 271 if (result->pending_operations == NULL)
AzureIoTClient 21:f9c433d8e6ca 272 {
AzureIoTClient 21:f9c433d8e6ca 273 /* Codes_SRS_CBS_01_101: [ If `singlylinkedlist_create` fails, `cbs_create` shall fail and return NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 274 LogError("Cannot allocate pending operations list.");
AzureIoTClient 21:f9c433d8e6ca 275 free(result);
AzureIoTClient 21:f9c433d8e6ca 276 result = NULL;
AzureIoTClient 21:f9c433d8e6ca 277 }
AzureIoTClient 21:f9c433d8e6ca 278 else
AzureIoTClient 6:641a9672db08 279 {
AzureIoTClient 22:524bded3f7a8 280 /* Codes_SRS_CBS_01_034: [ `cbs_create` shall create an AMQP management handle by calling `amqp_management_create`. ]*/
AzureIoTClient 21:f9c433d8e6ca 281 /* Codes_SRS_CBS_01_002: [ Tokens are communicated between AMQP peers by sending specially-formatted AMQP messages to the Claims-based Security Node. ]*/
AzureIoTClient 21:f9c433d8e6ca 282 /* Codes_SRS_CBS_01_003: [ The mechanism follows the scheme defined in the AMQP Management specification [AMQPMAN]. ]*/
AzureIoTClient 22:524bded3f7a8 283 result->amqp_management = amqp_management_create(session, "$cbs");
AzureIoTClient 21:f9c433d8e6ca 284 if (result->amqp_management == NULL)
AzureIoTClient 21:f9c433d8e6ca 285 {
AzureIoTClient 21:f9c433d8e6ca 286 free(result);
AzureIoTClient 21:f9c433d8e6ca 287 result = NULL;
AzureIoTClient 21:f9c433d8e6ca 288 }
AzureIoTClient 21:f9c433d8e6ca 289 else
AzureIoTClient 21:f9c433d8e6ca 290 {
AzureIoTClient 21:f9c433d8e6ca 291 result->cbs_state = CBS_STATE_CLOSED;
AzureIoTClient 21:f9c433d8e6ca 292 }
AzureIoTClient 6:641a9672db08 293 }
AzureIoTClient 6:641a9672db08 294 }
AzureIoTClient 6:641a9672db08 295 }
AzureIoTClient 21:f9c433d8e6ca 296
AzureIoTClient 21:f9c433d8e6ca 297 /* Codes_SRS_CBS_01_001: [ `cbs_create` shall create a new CBS instance and on success return a non-NULL handle to it. ]*/
AzureIoTClient 6:641a9672db08 298 return result;
Azure.IoT Build 0:6ae2f7bca550 299 }
Azure.IoT Build 0:6ae2f7bca550 300
Azure.IoT Build 0:6ae2f7bca550 301 void cbs_destroy(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 302 {
AzureIoTClient 21:f9c433d8e6ca 303 if (cbs == NULL)
AzureIoTClient 21:f9c433d8e6ca 304 {
AzureIoTClient 21:f9c433d8e6ca 305 /* Codes_SRS_CBS_01_037: [ If `cbs` is NULL, `cbs_destroy` shall do nothing. ]*/
AzureIoTClient 21:f9c433d8e6ca 306 LogError("NULL cbs handle");
AzureIoTClient 21:f9c433d8e6ca 307 }
AzureIoTClient 21:f9c433d8e6ca 308 else
AzureIoTClient 6:641a9672db08 309 {
AzureIoTClient 21:f9c433d8e6ca 310 LIST_ITEM_HANDLE first_pending_operation;
AzureIoTClient 21:f9c433d8e6ca 311
AzureIoTClient 21:f9c433d8e6ca 312 /* Codes_SRS_CBS_01_100: [ If the CBS instance is not closed, all actions performed by `cbs_close` shall be performed. ]*/
AzureIoTClient 21:f9c433d8e6ca 313 if (cbs->cbs_state != CBS_STATE_CLOSED)
AzureIoTClient 21:f9c433d8e6ca 314 {
AzureIoTClient 22:524bded3f7a8 315 (void)amqp_management_close(cbs->amqp_management);
AzureIoTClient 21:f9c433d8e6ca 316 }
AzureIoTClient 21:f9c433d8e6ca 317
AzureIoTClient 21:f9c433d8e6ca 318 /* Codes_SRS_CBS_01_036: [ `cbs_destroy` shall free all resources associated with the handle `cbs`. ]*/
AzureIoTClient 22:524bded3f7a8 319 /* Codes_SRS_CBS_01_038: [ `cbs_destroy` shall free the AMQP management handle created in `cbs_create` by calling `amqp_management_destroy`. ]*/
AzureIoTClient 22:524bded3f7a8 320 amqp_management_destroy(cbs->amqp_management);
AzureIoTClient 21:f9c433d8e6ca 321
AzureIoTClient 21:f9c433d8e6ca 322 /* Codes_SRS_CBS_01_099: [ All pending operations shall be freed. ]*/
AzureIoTClient 21:f9c433d8e6ca 323 while ((first_pending_operation = singlylinkedlist_get_head_item(cbs->pending_operations)) != NULL)
AzureIoTClient 21:f9c433d8e6ca 324 {
AzureIoTClient 21:f9c433d8e6ca 325 CBS_OPERATION* pending_operation = (CBS_OPERATION*)singlylinkedlist_item_get_value(first_pending_operation);
AzureIoTClient 21:f9c433d8e6ca 326 if (pending_operation != NULL)
AzureIoTClient 21:f9c433d8e6ca 327 {
AzureIoTClient 21:f9c433d8e6ca 328 pending_operation->on_cbs_operation_complete(pending_operation->on_cbs_operation_complete_context, CBS_OPERATION_RESULT_BECAUSE_DESTROY, 0, NULL);
AzureIoTClient 21:f9c433d8e6ca 329 free(pending_operation);
AzureIoTClient 21:f9c433d8e6ca 330 }
AzureIoTClient 21:f9c433d8e6ca 331
AzureIoTClient 21:f9c433d8e6ca 332 singlylinkedlist_remove(cbs->pending_operations, first_pending_operation);
AzureIoTClient 21:f9c433d8e6ca 333 }
AzureIoTClient 21:f9c433d8e6ca 334
AzureIoTClient 21:f9c433d8e6ca 335 /* Codes_SRS_CBS_01_098: [ `cbs_destroy` shall free the pending operations list by calling `singlylinkedlist_destroy`. ]*/
AzureIoTClient 21:f9c433d8e6ca 336 singlylinkedlist_destroy(cbs->pending_operations);
AzureIoTClient 21:f9c433d8e6ca 337 free(cbs);
AzureIoTClient 6:641a9672db08 338 }
Azure.IoT Build 0:6ae2f7bca550 339 }
Azure.IoT Build 0:6ae2f7bca550 340
AzureIoTClient 21:f9c433d8e6ca 341 int cbs_open_async(CBS_HANDLE cbs, ON_CBS_OPEN_COMPLETE on_cbs_open_complete, void* on_cbs_open_complete_context, ON_CBS_ERROR on_cbs_error, void* on_cbs_error_context)
Azure.IoT Build 0:6ae2f7bca550 342 {
AzureIoTClient 6:641a9672db08 343 int result;
Azure.IoT Build 0:6ae2f7bca550 344
AzureIoTClient 21:f9c433d8e6ca 345 /* Codes_SRS_CBS_01_042: [ `on_cbs_open_complete_context` and `on_cbs_error_context` shall be allowed to be NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 346 if ((cbs == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 347 (on_cbs_open_complete == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 348 (on_cbs_error == NULL))
AzureIoTClient 6:641a9672db08 349 {
AzureIoTClient 21:f9c433d8e6ca 350 /* Codes_SRS_CBS_01_040: [ If any of the arguments `cbs`, `on_cbs_open_complete` or `on_cbs_error` is NULL, then `cbs_open_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 351 LogError("Bad arguments: cbs = %p, on_cbs_open_complete = %p, on_cbs_error = %p",
AzureIoTClient 21:f9c433d8e6ca 352 cbs, on_cbs_open_complete, on_cbs_error);
AzureIoTClient 21:f9c433d8e6ca 353 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 354 }
AzureIoTClient 21:f9c433d8e6ca 355 else if (cbs->cbs_state != CBS_STATE_CLOSED)
AzureIoTClient 21:f9c433d8e6ca 356 {
AzureIoTClient 21:f9c433d8e6ca 357 /* Codes_SRS_CBS_01_043: [ `cbs_open_async` while already open or opening shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 358 LogError("cbs instance already open");
AzureIoTClient 19:000ab4e6a2c1 359 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 360 }
AzureIoTClient 6:641a9672db08 361 else
AzureIoTClient 6:641a9672db08 362 {
AzureIoTClient 22:524bded3f7a8 363 /* Codes_SRS_CBS_01_078: [ The cbs instance shall be considered OPENING until the `on_amqp_management_open_complete` callback is called by the AMQP management instance indicating that the open has completed (succesfull or not). ]*/
AzureIoTClient 21:f9c433d8e6ca 364 cbs->cbs_state = CBS_STATE_OPENING;
AzureIoTClient 21:f9c433d8e6ca 365 cbs->on_cbs_open_complete = on_cbs_open_complete;
AzureIoTClient 21:f9c433d8e6ca 366 cbs->on_cbs_open_complete_context = on_cbs_open_complete_context;
AzureIoTClient 21:f9c433d8e6ca 367 cbs->on_cbs_error = on_cbs_error;
AzureIoTClient 21:f9c433d8e6ca 368 cbs->on_cbs_error_context = on_cbs_error_context;
AzureIoTClient 21:f9c433d8e6ca 369
AzureIoTClient 22:524bded3f7a8 370 /* Codes_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/
AzureIoTClient 22:524bded3f7a8 371 if (amqp_management_open_async(cbs->amqp_management, on_underlying_amqp_management_open_complete, cbs, on_underlying_amqp_management_error, cbs) != 0)
AzureIoTClient 6:641a9672db08 372 {
AzureIoTClient 22:524bded3f7a8 373 /* Codes_SRS_CBS_01_041: [ If `amqp_management_open_async` fails, shall fail and return a non-zero value. ]*/
AzureIoTClient 19:000ab4e6a2c1 374 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 375 }
AzureIoTClient 6:641a9672db08 376 else
AzureIoTClient 6:641a9672db08 377 {
AzureIoTClient 21:f9c433d8e6ca 378 /* Codes_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/
AzureIoTClient 6:641a9672db08 379 result = 0;
AzureIoTClient 6:641a9672db08 380 }
AzureIoTClient 6:641a9672db08 381 }
Azure.IoT Build 0:6ae2f7bca550 382
AzureIoTClient 6:641a9672db08 383 return result;
Azure.IoT Build 0:6ae2f7bca550 384 }
Azure.IoT Build 0:6ae2f7bca550 385
Azure.IoT Build 0:6ae2f7bca550 386 int cbs_close(CBS_HANDLE cbs)
Azure.IoT Build 0:6ae2f7bca550 387 {
AzureIoTClient 6:641a9672db08 388 int result;
Azure.IoT Build 0:6ae2f7bca550 389
AzureIoTClient 6:641a9672db08 390 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 391 {
AzureIoTClient 21:f9c433d8e6ca 392 /* Codes_SRS_CBS_01_045: [ If the argument `cbs` is NULL, `cbs_close` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 393 LogError("NULL cbs handle");
AzureIoTClient 21:f9c433d8e6ca 394 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 395 }
AzureIoTClient 21:f9c433d8e6ca 396 else if (cbs->cbs_state == CBS_STATE_CLOSED)
AzureIoTClient 21:f9c433d8e6ca 397 {
AzureIoTClient 21:f9c433d8e6ca 398 /* Codes_SRS_CBS_01_047: [ `cbs_close` when closed shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 399 /* Codes_SRS_CBS_01_048: [ `cbs_close` when not opened shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 400 LogError("Already closed");
AzureIoTClient 19:000ab4e6a2c1 401 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 402 }
AzureIoTClient 6:641a9672db08 403 else
AzureIoTClient 6:641a9672db08 404 {
AzureIoTClient 22:524bded3f7a8 405 /* Codes_SRS_CBS_01_044: [ `cbs_close` shall close the CBS instance by calling `amqp_management_close` on the underlying AMQP management handle. ]*/
AzureIoTClient 22:524bded3f7a8 406 if (amqp_management_close(cbs->amqp_management) != 0)
AzureIoTClient 6:641a9672db08 407 {
AzureIoTClient 22:524bded3f7a8 408 /* Codes_SRS_CBS_01_046: [ If `amqp_management_close` fails, `cbs_close` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 409 LogError("Failed closing AMQP management instance");
AzureIoTClient 19:000ab4e6a2c1 410 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 411 }
AzureIoTClient 6:641a9672db08 412 else
AzureIoTClient 6:641a9672db08 413 {
AzureIoTClient 21:f9c433d8e6ca 414 if (cbs->cbs_state == CBS_STATE_OPENING)
AzureIoTClient 21:f9c433d8e6ca 415 {
AzureIoTClient 21:f9c433d8e6ca 416 /* Codes_SRS_CBS_01_079: [ If `cbs_close` is called while OPENING, the `on_cbs_open_complete` callback shall be called with `CBS_OPEN_CANCELLED`, while passing the `on_cbs_open_complete_context` as argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 417 cbs->on_cbs_open_complete(cbs->on_cbs_open_complete_context, CBS_OPEN_CANCELLED);
AzureIoTClient 21:f9c433d8e6ca 418 }
AzureIoTClient 21:f9c433d8e6ca 419
AzureIoTClient 21:f9c433d8e6ca 420 cbs->cbs_state = CBS_STATE_CLOSED;
AzureIoTClient 21:f9c433d8e6ca 421
AzureIoTClient 21:f9c433d8e6ca 422 /* Codes_SRS_CBS_01_080: [ On success, `cbs_close` shall return 0. ]*/
AzureIoTClient 6:641a9672db08 423 result = 0;
AzureIoTClient 6:641a9672db08 424 }
AzureIoTClient 6:641a9672db08 425 }
Azure.IoT Build 0:6ae2f7bca550 426
AzureIoTClient 6:641a9672db08 427 return result;
Azure.IoT Build 0:6ae2f7bca550 428 }
Azure.IoT Build 0:6ae2f7bca550 429
AzureIoTClient 21:f9c433d8e6ca 430 int cbs_put_token_async(CBS_HANDLE cbs, const char* type, const char* audience, const char* token, ON_CBS_OPERATION_COMPLETE on_cbs_put_token_complete, void* on_cbs_put_token_complete_context)
Azure.IoT Build 0:6ae2f7bca550 431 {
AzureIoTClient 6:641a9672db08 432 int result;
Azure.IoT Build 0:6ae2f7bca550 433
AzureIoTClient 21:f9c433d8e6ca 434 /* Codes_SRS_CBS_01_083: [ `on_cbs_put_token_complete_context` shall be allowed to be NULL. ]*/
AzureIoTClient 6:641a9672db08 435 if ((cbs == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 436 (type == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 437 (audience == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 438 (token == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 439 (on_cbs_put_token_complete == NULL))
AzureIoTClient 6:641a9672db08 440 {
AzureIoTClient 21:f9c433d8e6ca 441 /* Codes_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 442 LogError("Bad arguments: cbs = %p, type = %p, audience = %p, token = %p, on_cbs_put_token_complete = %p",
AzureIoTClient 21:f9c433d8e6ca 443 cbs, type, audience, token, on_cbs_put_token_complete);
AzureIoTClient 21:f9c433d8e6ca 444 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 445 }
AzureIoTClient 21:f9c433d8e6ca 446 else if ((cbs->cbs_state == CBS_STATE_CLOSED) ||
AzureIoTClient 21:f9c433d8e6ca 447 (cbs->cbs_state == CBS_STATE_ERROR))
AzureIoTClient 21:f9c433d8e6ca 448 {
AzureIoTClient 21:f9c433d8e6ca 449 /* Codes_SRS_CBS_01_058: [ If `cbs_put_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 450 LogError("put token called while closed or in error");
AzureIoTClient 19:000ab4e6a2c1 451 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 452 }
AzureIoTClient 6:641a9672db08 453 else
AzureIoTClient 6:641a9672db08 454 {
AzureIoTClient 21:f9c433d8e6ca 455 /* Codes_SRS_CBS_01_049: [ `cbs_put_token_async` shall construct a request message for the `put-token` operation. ]*/
AzureIoTClient 6:641a9672db08 456 MESSAGE_HANDLE message = message_create();
AzureIoTClient 6:641a9672db08 457 if (message == NULL)
AzureIoTClient 6:641a9672db08 458 {
AzureIoTClient 21:f9c433d8e6ca 459 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 460 LogError("message_create failed");
AzureIoTClient 19:000ab4e6a2c1 461 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 462 }
AzureIoTClient 6:641a9672db08 463 else
AzureIoTClient 6:641a9672db08 464 {
AzureIoTClient 6:641a9672db08 465 AMQP_VALUE token_value = amqpvalue_create_string(token);
AzureIoTClient 6:641a9672db08 466 if (token_value == NULL)
AzureIoTClient 6:641a9672db08 467 {
AzureIoTClient 21:f9c433d8e6ca 468 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 469 LogError("Failed creating token AMQP value");
AzureIoTClient 19:000ab4e6a2c1 470 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 471 }
AzureIoTClient 6:641a9672db08 472 else
AzureIoTClient 6:641a9672db08 473 {
AzureIoTClient 21:f9c433d8e6ca 474 /* Codes_SRS_CBS_01_009: [ The body of the message MUST contain the token. ]*/
AzureIoTClient 6:641a9672db08 475 if (message_set_body_amqp_value(message, token_value) != 0)
AzureIoTClient 6:641a9672db08 476 {
AzureIoTClient 21:f9c433d8e6ca 477 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 478 LogError("Failed setting the token in the message body");
AzureIoTClient 19:000ab4e6a2c1 479 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 480 }
AzureIoTClient 6:641a9672db08 481 else
AzureIoTClient 6:641a9672db08 482 {
AzureIoTClient 6:641a9672db08 483 AMQP_VALUE application_properties = amqpvalue_create_map();
AzureIoTClient 6:641a9672db08 484 if (application_properties == NULL)
AzureIoTClient 6:641a9672db08 485 {
AzureIoTClient 21:f9c433d8e6ca 486 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 487 LogError("Failed creating application properties map");
AzureIoTClient 19:000ab4e6a2c1 488 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 489 }
AzureIoTClient 6:641a9672db08 490 else
AzureIoTClient 6:641a9672db08 491 {
AzureIoTClient 6:641a9672db08 492 if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0)
AzureIoTClient 6:641a9672db08 493 {
AzureIoTClient 19:000ab4e6a2c1 494 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 495 }
AzureIoTClient 6:641a9672db08 496 else
AzureIoTClient 6:641a9672db08 497 {
AzureIoTClient 6:641a9672db08 498 if (message_set_application_properties(message, application_properties) != 0)
AzureIoTClient 6:641a9672db08 499 {
AzureIoTClient 21:f9c433d8e6ca 500 /* Codes_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 501 LogError("Failed setting message application properties");
AzureIoTClient 19:000ab4e6a2c1 502 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 503 }
AzureIoTClient 6:641a9672db08 504 else
AzureIoTClient 6:641a9672db08 505 {
AzureIoTClient 21:f9c433d8e6ca 506 CBS_OPERATION* cbs_operation = (CBS_OPERATION*)malloc(sizeof(CBS_OPERATION));
AzureIoTClient 21:f9c433d8e6ca 507 if (cbs_operation == NULL)
AzureIoTClient 6:641a9672db08 508 {
AzureIoTClient 21:f9c433d8e6ca 509 LogError("Failed allocating CBS operation instance");
AzureIoTClient 19:000ab4e6a2c1 510 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 511 }
AzureIoTClient 6:641a9672db08 512 else
AzureIoTClient 6:641a9672db08 513 {
AzureIoTClient 21:f9c433d8e6ca 514 LIST_ITEM_HANDLE list_item;
AzureIoTClient 21:f9c433d8e6ca 515
AzureIoTClient 21:f9c433d8e6ca 516 cbs_operation->on_cbs_operation_complete = on_cbs_put_token_complete;
AzureIoTClient 21:f9c433d8e6ca 517 cbs_operation->on_cbs_operation_complete_context = on_cbs_put_token_complete_context;
AzureIoTClient 21:f9c433d8e6ca 518 cbs_operation->pending_operations = cbs->pending_operations;
AzureIoTClient 21:f9c433d8e6ca 519
AzureIoTClient 21:f9c433d8e6ca 520 list_item = singlylinkedlist_add(cbs->pending_operations, cbs_operation);
AzureIoTClient 21:f9c433d8e6ca 521 if (list_item == NULL)
AzureIoTClient 6:641a9672db08 522 {
AzureIoTClient 21:f9c433d8e6ca 523 free(cbs_operation);
AzureIoTClient 21:f9c433d8e6ca 524 LogError("Failed adding pending operation to list");
AzureIoTClient 19:000ab4e6a2c1 525 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 526 }
AzureIoTClient 6:641a9672db08 527 else
AzureIoTClient 6:641a9672db08 528 {
AzureIoTClient 22:524bded3f7a8 529 /* Codes_SRS_CBS_01_051: [ `cbs_put_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/
AzureIoTClient 21:f9c433d8e6ca 530 /* Codes_SRS_CBS_01_052: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/
AzureIoTClient 21:f9c433d8e6ca 531 /* Codes_SRS_CBS_01_053: [ The `operation` argument shall be `put-token`. ]*/
AzureIoTClient 21:f9c433d8e6ca 532 /* Codes_SRS_CBS_01_054: [ The `type` argument shall be set to the `type` argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 533 /* Codes_SRS_CBS_01_055: [ The `locales` argument shall be set to NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 534 /* Codes_SRS_CBS_01_056: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/
AzureIoTClient 22:524bded3f7a8 535 /* Codes_SRS_CBS_01_057: [ The arguments `on_execute_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/
AzureIoTClient 21:f9c433d8e6ca 536 /* Codes_SRS_CBS_01_005: [ operation No string "put-token" ]*/
AzureIoTClient 21:f9c433d8e6ca 537 /* Codes_SRS_CBS_01_006: [ Type No string The type of the token being put, e.g., "amqp:jwt". ]*/
AzureIoTClient 21:f9c433d8e6ca 538 /* Codes_SRS_CBS_01_007: [ name No string The "audience" to which the token applies. ]*/
AzureIoTClient 22:524bded3f7a8 539 if (amqp_management_execute_operation_async(cbs->amqp_management, "put-token", type, NULL, message, on_amqp_management_execute_operation_complete, list_item) != 0)
AzureIoTClient 21:f9c433d8e6ca 540 {
AzureIoTClient 21:f9c433d8e6ca 541 singlylinkedlist_remove(cbs->pending_operations, list_item);
AzureIoTClient 21:f9c433d8e6ca 542 free(cbs_operation);
AzureIoTClient 22:524bded3f7a8 543 /* Codes_SRS_CBS_01_084: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 544 LogError("Failed starting AMQP management operation");
AzureIoTClient 21:f9c433d8e6ca 545 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 546 }
AzureIoTClient 21:f9c433d8e6ca 547 else
AzureIoTClient 21:f9c433d8e6ca 548 {
AzureIoTClient 21:f9c433d8e6ca 549 /* Codes_SRS_CBS_01_081: [ On success `cbs_put_token_async` shall return 0. ]*/
AzureIoTClient 21:f9c433d8e6ca 550 result = 0;
AzureIoTClient 21:f9c433d8e6ca 551 }
AzureIoTClient 6:641a9672db08 552 }
AzureIoTClient 6:641a9672db08 553 }
AzureIoTClient 6:641a9672db08 554 }
AzureIoTClient 6:641a9672db08 555 }
Azure.IoT Build 0:6ae2f7bca550 556
AzureIoTClient 6:641a9672db08 557 amqpvalue_destroy(application_properties);
AzureIoTClient 6:641a9672db08 558 }
Azure.IoT Build 0:6ae2f7bca550 559
AzureIoTClient 6:641a9672db08 560 amqpvalue_destroy(token_value);
AzureIoTClient 6:641a9672db08 561 }
AzureIoTClient 6:641a9672db08 562 }
Azure.IoT Build 0:6ae2f7bca550 563
AzureIoTClient 6:641a9672db08 564 message_destroy(message);
AzureIoTClient 6:641a9672db08 565 }
AzureIoTClient 6:641a9672db08 566 }
Azure.IoT Build 0:6ae2f7bca550 567
AzureIoTClient 6:641a9672db08 568 return result;
Azure.IoT Build 0:6ae2f7bca550 569 }
Azure.IoT Build 0:6ae2f7bca550 570
AzureIoTClient 21:f9c433d8e6ca 571 int cbs_delete_token_async(CBS_HANDLE cbs, const char* type, const char* audience, ON_CBS_OPERATION_COMPLETE on_cbs_delete_token_complete, void* on_cbs_delete_token_complete_context)
Azure.IoT Build 0:6ae2f7bca550 572 {
AzureIoTClient 6:641a9672db08 573 int result;
Azure.IoT Build 0:6ae2f7bca550 574
AzureIoTClient 21:f9c433d8e6ca 575 /* Codes_SRS_CBS_01_086: [ `on_cbs_delete_token_complete_context` shall be allowed to be NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 576 if ((cbs == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 577 (type == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 578 (audience == NULL) ||
AzureIoTClient 21:f9c433d8e6ca 579 (on_cbs_delete_token_complete == NULL))
AzureIoTClient 6:641a9672db08 580 {
AzureIoTClient 21:f9c433d8e6ca 581 /* Codes_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 582 LogError("Bad arguments: cbs = %p, type = %p, audience = %p, on_cbs_delete_token_complete = %p",
AzureIoTClient 21:f9c433d8e6ca 583 cbs, type, audience, on_cbs_delete_token_complete);
AzureIoTClient 21:f9c433d8e6ca 584 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 585 }
AzureIoTClient 21:f9c433d8e6ca 586 else if ((cbs->cbs_state == CBS_STATE_CLOSED) ||
AzureIoTClient 21:f9c433d8e6ca 587 (cbs->cbs_state == CBS_STATE_ERROR))
AzureIoTClient 21:f9c433d8e6ca 588 {
AzureIoTClient 21:f9c433d8e6ca 589 /* Codes_SRS_CBS_01_067: [ If `cbs_delete_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 590 LogError("put token called while closed or in error");
AzureIoTClient 19:000ab4e6a2c1 591 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 592 }
AzureIoTClient 6:641a9672db08 593 else
AzureIoTClient 6:641a9672db08 594 {
AzureIoTClient 21:f9c433d8e6ca 595 /* Codes_SRS_CBS_01_025: [ The body of the message MUST be empty. ]*/
AzureIoTClient 21:f9c433d8e6ca 596 /* Codes_SRS_CBS_01_059: [ `cbs_delete_token_async` shall construct a request message for the `delete-token` operation. ]*/
AzureIoTClient 6:641a9672db08 597 MESSAGE_HANDLE message = message_create();
AzureIoTClient 6:641a9672db08 598 if (message == NULL)
AzureIoTClient 6:641a9672db08 599 {
AzureIoTClient 21:f9c433d8e6ca 600 /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 601 LogError("message_create failed");
AzureIoTClient 19:000ab4e6a2c1 602 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 603 }
AzureIoTClient 6:641a9672db08 604 else
AzureIoTClient 6:641a9672db08 605 {
AzureIoTClient 6:641a9672db08 606 AMQP_VALUE application_properties = amqpvalue_create_map();
AzureIoTClient 6:641a9672db08 607 if (application_properties == NULL)
AzureIoTClient 6:641a9672db08 608 {
AzureIoTClient 21:f9c433d8e6ca 609 /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 610 LogError("Failed creating application properties map");
AzureIoTClient 19:000ab4e6a2c1 611 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 612 }
AzureIoTClient 6:641a9672db08 613 else
AzureIoTClient 6:641a9672db08 614 {
AzureIoTClient 6:641a9672db08 615 if (add_string_key_value_pair_to_map(application_properties, "name", audience) != 0)
AzureIoTClient 6:641a9672db08 616 {
AzureIoTClient 19:000ab4e6a2c1 617 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 618 }
AzureIoTClient 6:641a9672db08 619 else
AzureIoTClient 6:641a9672db08 620 {
AzureIoTClient 21:f9c433d8e6ca 621 /* Codes_SRS_CBS_01_021: [ The request message has the following application-properties: ]*/
AzureIoTClient 6:641a9672db08 622 if (message_set_application_properties(message, application_properties) != 0)
AzureIoTClient 6:641a9672db08 623 {
AzureIoTClient 21:f9c433d8e6ca 624 /* Codes_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 625 LogError("Failed setting message application properties");
AzureIoTClient 19:000ab4e6a2c1 626 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 627 }
AzureIoTClient 6:641a9672db08 628 else
AzureIoTClient 6:641a9672db08 629 {
AzureIoTClient 21:f9c433d8e6ca 630 CBS_OPERATION* cbs_operation = (CBS_OPERATION*)malloc(sizeof(CBS_OPERATION));
AzureIoTClient 21:f9c433d8e6ca 631 if (cbs_operation == NULL)
AzureIoTClient 6:641a9672db08 632 {
AzureIoTClient 21:f9c433d8e6ca 633 LogError("Failed allocating CBS operation instance");
AzureIoTClient 19:000ab4e6a2c1 634 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 635 }
AzureIoTClient 6:641a9672db08 636 else
AzureIoTClient 6:641a9672db08 637 {
AzureIoTClient 21:f9c433d8e6ca 638 LIST_ITEM_HANDLE list_item;
AzureIoTClient 21:f9c433d8e6ca 639
AzureIoTClient 21:f9c433d8e6ca 640 cbs_operation->on_cbs_operation_complete = on_cbs_delete_token_complete;
AzureIoTClient 21:f9c433d8e6ca 641 cbs_operation->on_cbs_operation_complete_context = on_cbs_delete_token_complete_context;
AzureIoTClient 21:f9c433d8e6ca 642 cbs_operation->pending_operations = cbs->pending_operations;
AzureIoTClient 21:f9c433d8e6ca 643
AzureIoTClient 21:f9c433d8e6ca 644 list_item = singlylinkedlist_add(cbs->pending_operations, cbs_operation);
AzureIoTClient 21:f9c433d8e6ca 645 if (list_item == NULL)
AzureIoTClient 6:641a9672db08 646 {
AzureIoTClient 21:f9c433d8e6ca 647 free(cbs_operation);
AzureIoTClient 21:f9c433d8e6ca 648 LogError("Failed adding pending operation to list");
AzureIoTClient 19:000ab4e6a2c1 649 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 650 }
AzureIoTClient 6:641a9672db08 651 else
AzureIoTClient 6:641a9672db08 652 {
AzureIoTClient 22:524bded3f7a8 653 /* Codes_SRS_CBS_01_061: [ `cbs_delete_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/
AzureIoTClient 21:f9c433d8e6ca 654 /* Codes_SRS_CBS_01_085: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/
AzureIoTClient 21:f9c433d8e6ca 655 /* Codes_SRS_CBS_01_062: [ The `operation` argument shall be `delete-token`. ]*/
AzureIoTClient 21:f9c433d8e6ca 656 /* Codes_SRS_CBS_01_063: [ The `type` argument shall be set to the `type` argument. ]*/
AzureIoTClient 21:f9c433d8e6ca 657 /* Codes_SRS_CBS_01_064: [ The `locales` argument shall be set to NULL. ]*/
AzureIoTClient 21:f9c433d8e6ca 658 /* Codes_SRS_CBS_01_065: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/
AzureIoTClient 21:f9c433d8e6ca 659 /* Codes_SRS_CBS_01_066: [ The arguments `on_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/
AzureIoTClient 21:f9c433d8e6ca 660 /* Codes_SRS_CBS_01_020: [ To instruct a peer to delete a token associated with a specific audience, a "delete-token" message can be sent to the CBS Node ]*/
AzureIoTClient 21:f9c433d8e6ca 661 /* Codes_SRS_CBS_01_022: [ operation Yes string "delete-token" ]*/
AzureIoTClient 21:f9c433d8e6ca 662 /* Codes_SRS_CBS_01_023: [ Type Yes string The type of the token being deleted, e.g., "amqp:jwt". ]*/
AzureIoTClient 21:f9c433d8e6ca 663 /* Codes_SRS_CBS_01_024: [ name Yes string The "audience" of the token being deleted. ]*/
AzureIoTClient 22:524bded3f7a8 664 if (amqp_management_execute_operation_async(cbs->amqp_management, "delete-token", type, NULL, message, on_amqp_management_execute_operation_complete, list_item) != 0)
AzureIoTClient 21:f9c433d8e6ca 665 {
AzureIoTClient 22:524bded3f7a8 666 /* Codes_SRS_CBS_01_087: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 667 singlylinkedlist_remove(cbs->pending_operations, list_item);
AzureIoTClient 21:f9c433d8e6ca 668 free(cbs_operation);
AzureIoTClient 21:f9c433d8e6ca 669 LogError("Failed starting AMQP management operation");
AzureIoTClient 21:f9c433d8e6ca 670 result = __FAILURE__;
AzureIoTClient 21:f9c433d8e6ca 671 }
AzureIoTClient 21:f9c433d8e6ca 672 else
AzureIoTClient 21:f9c433d8e6ca 673 {
AzureIoTClient 21:f9c433d8e6ca 674 /* Codes_SRS_CBS_01_082: [ On success `cbs_delete_token_async` shall return 0. ]*/
AzureIoTClient 21:f9c433d8e6ca 675 result = 0;
AzureIoTClient 21:f9c433d8e6ca 676 }
AzureIoTClient 6:641a9672db08 677 }
AzureIoTClient 6:641a9672db08 678 }
AzureIoTClient 6:641a9672db08 679 }
AzureIoTClient 6:641a9672db08 680 }
Azure.IoT Build 0:6ae2f7bca550 681
AzureIoTClient 6:641a9672db08 682 amqpvalue_destroy(application_properties);
AzureIoTClient 6:641a9672db08 683 }
Azure.IoT Build 0:6ae2f7bca550 684
AzureIoTClient 6:641a9672db08 685 message_destroy(message);
AzureIoTClient 6:641a9672db08 686 }
AzureIoTClient 6:641a9672db08 687 }
AzureIoTClient 6:641a9672db08 688 return result;
AzureIoTClient 6:641a9672db08 689 }
Azure.IoT Build 0:6ae2f7bca550 690
AzureIoTClient 21:f9c433d8e6ca 691 int cbs_set_trace(CBS_HANDLE cbs, bool trace_on)
AzureIoTClient 6:641a9672db08 692 {
AzureIoTClient 21:f9c433d8e6ca 693 int result;
AzureIoTClient 21:f9c433d8e6ca 694
AzureIoTClient 21:f9c433d8e6ca 695 if (cbs == NULL)
AzureIoTClient 6:641a9672db08 696 {
AzureIoTClient 21:f9c433d8e6ca 697 /* Codes_SRS_CBS_01_090: [ If the argument `cbs` is NULL, `cbs_set_trace` shall fail and return a non-zero value. ]*/
AzureIoTClient 21:f9c433d8e6ca 698 LogError("NULL cbs handle");
AzureIoTClient 21:f9c433d8e6ca 699 result = __FAILURE__;
AzureIoTClient 6:641a9672db08 700 }
AzureIoTClient 21:f9c433d8e6ca 701 else
AzureIoTClient 21:f9c433d8e6ca 702 {
AzureIoTClient 22:524bded3f7a8 703 /* Codes_SRS_CBS_01_088: [ `cbs_set_trace` shall enable or disable tracing by calling `amqp_management_set_trace` to pass down the `trace_on` value. ]*/
AzureIoTClient 22:524bded3f7a8 704 amqp_management_set_trace(cbs->amqp_management, trace_on);
AzureIoTClient 21:f9c433d8e6ca 705
AzureIoTClient 21:f9c433d8e6ca 706 /* Codes_SRS_CBS_01_089: [ On success, `cbs_set_trace` shall return 0. ]*/
AzureIoTClient 21:f9c433d8e6ca 707 result = 0;
AzureIoTClient 21:f9c433d8e6ca 708 }
AzureIoTClient 21:f9c433d8e6ca 709
AzureIoTClient 21:f9c433d8e6ca 710 return result;
Azure.IoT Build 0:6ae2f7bca550 711 }