| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include "azure_c_shared_utility/gballoc.h" |
| 8 |
#include "azure_c_shared_utility/platform.h" |
| 9 |
#include "azure_c_shared_utility/tlsio.h" |
| 10 |
#include "azure_uamqp_c/uamqp.h" |
| 11 |
|
| 12 |
/* This sample connects to an IoTHub, authenticates using a CBS token and sends one message */ |
| 13 |
/* Replace the below settings with your own.*/ |
| 14 |
|
| 15 |
#define IOT_HUB_HOST "<<<Replace with your own IoTHub host (like myiothub.azure-devices.net)>>>" |
| 16 |
#define IOT_HUB_DEVICE_NAME "<<<Replace with your device Id (like test_Device)>>>" |
| 17 |
#define IOT_HUB_DEVICE_SAS_TOKEN "<<<Replace with your own device SAS token (needs to be generated)>>>" |
| 18 |
|
| 19 |
static unsigned int sent_messages = 0; |
| 20 |
static const size_t msg_count = 1; |
| 21 |
static bool auth = false; |
| 22 |
|
| 23 |
static void on_cbs_open_complete(void* context, CBS_OPEN_COMPLETE_RESULT open_complete_result) |
| 24 |
{ |
| 25 |
(void)context; |
| 26 |
(void)open_complete_result; |
| 27 |
(void)printf("CBS instance open.\r\n"); |
| 28 |
} |
| 29 |
|
| 30 |
static void on_cbs_error(void* context) |
| 31 |
{ |
| 32 |
(void)context; |
| 33 |
(void)printf("CBS error.\r\n"); |
| 34 |
} |
| 35 |
|
| 36 |
static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state) |
| 37 |
{ |
| 38 |
(void)send_result; |
| 39 |
(void)context; |
| 40 |
(void)delivery_state; |
| 41 |
|
| 42 |
printf("Sent.\r\n"); |
| 43 |
sent_messages++; |
| 44 |
} |
| 45 |
|
| 46 |
static void on_cbs_operation_complete(void* context, CBS_OPERATION_RESULT cbs_operation_result, unsigned int status_code, const char* status_description) |
| 47 |
{ |
| 48 |
(void)context; |
| 49 |
(void)status_code; |
| 50 |
(void)status_description; |
| 51 |
|
| 52 |
if (cbs_operation_result == CBS_OPERATION_RESULT_OK) |
| 53 |
{ |
| 54 |
auth = true; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
int main(int argc, char** argv) |
| 59 |
{ |
| 60 |
int result; |
| 61 |
|
| 62 |
(void)argc; |
| 63 |
(void)argv; |
| 64 |
|
| 65 |
if (platform_init() != 0) |
| 66 |
{ |
| 67 |
result = -1; |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
XIO_HANDLE sasl_io; |
| 72 |
CONNECTION_HANDLE connection; |
| 73 |
SESSION_HANDLE session; |
| 74 |
LINK_HANDLE link; |
| 75 |
MESSAGE_SENDER_HANDLE message_sender; |
| 76 |
MESSAGE_HANDLE message; |
| 77 |
SASL_MECHANISM_HANDLE sasl_mechanism_handle; |
| 78 |
size_t last_memory_used = 0; |
| 79 |
XIO_HANDLE tls_io; |
| 80 |
TLSIO_CONFIG tls_io_config = { IOT_HUB_HOST, 5671 }; |
| 81 |
const IO_INTERFACE_DESCRIPTION* tlsio_interface; |
| 82 |
SASLCLIENTIO_CONFIG sasl_io_config; |
| 83 |
CBS_HANDLE cbs; |
| 84 |
AMQP_VALUE source; |
| 85 |
AMQP_VALUE target; |
| 86 |
unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; |
| 87 |
BINARY_DATA binary_data; |
| 88 |
fields attach_properties; |
| 89 |
AMQP_VALUE test_attach_property_key; |
| 90 |
AMQP_VALUE test_attach_property_value; |
| 91 |
|
| 92 |
gballoc_init(); |
| 93 |
|
| 94 |
/* create SASL MSSBCBS handler */ |
| 95 |
sasl_mechanism_handle = saslmechanism_create(saslmssbcbs_get_interface(), NULL); |
| 96 |
|
| 97 |
/* create the TLS IO */ |
| 98 |
tlsio_interface = platform_get_default_tlsio(); |
| 99 |
tls_io = xio_create(tlsio_interface, &tls_io_config); |
| 100 |
|
| 101 |
/* create the SASL client IO using the TLS IO */ |
| 102 |
sasl_io_config.underlying_io = tls_io; |
| 103 |
sasl_io_config.sasl_mechanism = sasl_mechanism_handle; |
| 104 |
sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); |
| 105 |
|
| 106 |
/* create the connection, session and link */ |
| 107 |
connection = connection_create(sasl_io, IOT_HUB_HOST, "some", NULL, NULL); |
| 108 |
session = session_create(connection, NULL, NULL); |
| 109 |
session_set_incoming_window(session, 2147483647); |
| 110 |
session_set_outgoing_window(session, 2); |
| 111 |
|
| 112 |
cbs = cbs_create(session); |
| 113 |
if (cbs_open_async(cbs, on_cbs_open_complete, cbs, on_cbs_error, cbs) == 0) |
| 114 |
{ |
| 115 |
(void)cbs_put_token_async(cbs, "servicebus.windows.net:sastoken", IOT_HUB_HOST "/devices/" IOT_HUB_DEVICE_NAME, IOT_HUB_DEVICE_SAS_TOKEN, on_cbs_operation_complete, cbs); |
| 116 |
|
| 117 |
while (!auth) |
| 118 |
{ |
| 119 |
size_t current_memory_used; |
| 120 |
size_t maximum_memory_used; |
| 121 |
connection_dowork(connection); |
| 122 |
|
| 123 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 124 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 125 |
|
| 126 |
if (current_memory_used != last_memory_used) |
| 127 |
{ |
| 128 |
printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 129 |
last_memory_used = current_memory_used; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
source = messaging_create_source("ingress"); |
| 135 |
target = messaging_create_target("amqps://" IOT_HUB_HOST "/devices/" IOT_HUB_DEVICE_NAME "/messages/events"); |
| 136 |
link = link_create(session, "sender-link", role_sender, source, target); |
| 137 |
(void)link_set_max_message_size(link, 65536); |
| 138 |
|
| 139 |
amqpvalue_destroy(source); |
| 140 |
amqpvalue_destroy(target); |
| 141 |
|
| 142 |
message = message_create(); |
| 143 |
|
| 144 |
binary_data.bytes = hello; |
| 145 |
binary_data.length = sizeof(hello); |
| 146 |
message_add_body_amqp_data(message, binary_data); |
| 147 |
|
| 148 |
attach_properties = amqpvalue_create_map(); |
| 149 |
test_attach_property_key = amqpvalue_create_string("test_attach_property_key"); |
| 150 |
test_attach_property_value = amqpvalue_create_string("a_test_property"); |
| 151 |
(void)amqpvalue_set_map_value(attach_properties, test_attach_property_key, test_attach_property_value); |
| 152 |
|
| 153 |
link_set_attach_properties(link, attach_properties); |
| 154 |
|
| 155 |
amqpvalue_destroy(test_attach_property_key); |
| 156 |
amqpvalue_destroy(test_attach_property_value); |
| 157 |
amqpvalue_destroy(attach_properties); |
| 158 |
|
| 159 |
/* create a message sender */ |
| 160 |
message_sender = messagesender_create(link, NULL, NULL); |
| 161 |
if (messagesender_open(message_sender) == 0) |
| 162 |
{ |
| 163 |
uint32_t i; |
| 164 |
bool keep_running = true; |
| 165 |
|
| 166 |
for (i = 0; i < msg_count; i++) |
| 167 |
{ |
| 168 |
(void)messagesender_send_async(message_sender, message, on_message_send_complete, message, 0); |
| 169 |
} |
| 170 |
|
| 171 |
message_destroy(message); |
| 172 |
|
| 173 |
while (keep_running) |
| 174 |
{ |
| 175 |
size_t current_memory_used; |
| 176 |
size_t maximum_memory_used; |
| 177 |
connection_dowork(connection); |
| 178 |
|
| 179 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 180 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 181 |
|
| 182 |
if (current_memory_used != last_memory_used) |
| 183 |
{ |
| 184 |
printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 185 |
last_memory_used = current_memory_used; |
| 186 |
} |
| 187 |
|
| 188 |
if (sent_messages == msg_count) |
| 189 |
{ |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
message_destroy(message); |
| 197 |
} |
| 198 |
|
| 199 |
cbs_destroy(cbs); |
| 200 |
messagesender_destroy(message_sender); |
| 201 |
link_destroy(link); |
| 202 |
session_destroy(session); |
| 203 |
connection_destroy(connection); |
| 204 |
xio_destroy(sasl_io); |
| 205 |
xio_destroy(tls_io); |
| 206 |
saslmechanism_destroy(sasl_mechanism_handle); |
| 207 |
platform_deinit(); |
| 208 |
|
| 209 |
printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed()); |
| 210 |
printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed()); |
| 211 |
|
| 212 |
gballoc_deinit(); |
| 213 |
} |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |