This repository was archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathiothub_ll_client_x509_sample.c
More file actions
291 lines (253 loc) · 11.1 KB
/
iothub_ll_client_x509_sample.c
File metadata and controls
291 lines (253 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
#include "wizchip_conf.h"
#include "netif.h"
#include "iothub.h"
#include "iothub_device_client_ll.h"
#include "iothub_client_options.h"
#include "iothub_message.h"
#include "iothub_client_version.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/tickcounter.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "azure_c_shared_utility/agenttime.h"
#include "azure_c_shared_utility/http_proxy_io.h"
#include "azure_prov_client/prov_device_ll_client.h"
#include "azure_prov_client/prov_security_factory.h"
#include "mbedtls/debug.h"
#include "azure_samples.h"
/* This sample uses the _LL APIs of iothub_client for example purposes.
Simply changing the using the convenience layer (functions not having _LL)
and removing calls to _DoWork will yield the same results. */
// The protocol you wish to use should be uncommented
//
#define SAMPLE_MQTT
//#define SAMPLE_MQTT_OVER_WEBSOCKETS
//#define SAMPLE_AMQP
//#define SAMPLE_AMQP_OVER_WEBSOCKETS
//#define SAMPLE_HTTP
#ifdef SAMPLE_MQTT
#include "iothubtransportmqtt.h"
#endif // SAMPLE_MQTT
#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
#include "iothubtransportmqtt_websockets.h"
#endif // SAMPLE_MQTT_OVER_WEBSOCKETS
#ifdef SAMPLE_AMQP
#include "iothubtransportamqp.h"
#endif // SAMPLE_AMQP
#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
#include "iothubtransportamqp_websockets.h"
#endif // SAMPLE_AMQP_OVER_WEBSOCKETS
#ifdef SAMPLE_HTTP
#include "iothubtransporthttp.h"
#endif // SAMPLE_HTTP
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
#include "certs.h"
#endif // SET_TRUSTED_CERT_IN_SAMPLES
/* Paste in the your iothub connection string */
//static const char* connectionString = "[device connection string]";
//static const char* x509certificate =
//"-----BEGIN CERTIFICATE-----""\n"
//"-----END CERTIFICATE-----";
//static const char* x509privatekey =
//"-----BEGIN PRIVATE KEY-----""\n"
//"-----END PRIVATE KEY-----";
static const char *connectionString = pico_az_x509connectionString;
static const char *x509certificate = pico_az_x509certificate;
static const char *x509privatekey = pico_az_x509privatekey;
#define MESSAGE_COUNT 3
static bool g_continueRunning = true;
static size_t g_message_count_send_confirmations = 0;
static size_t g_message_recv_count = 0;
static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userContextCallback)
{
(void)userContextCallback;
// When a message is sent this callback will get envoked
g_message_count_send_confirmations++;
(void)printf("Confirmation callback received for message %lu with result %s\r\n", (unsigned long)g_message_count_send_confirmations, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}
static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void *user_context)
{
(void)reason;
(void)user_context;
// This sample DOES NOT take into consideration network outages.
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
{
(void)printf("The device client is connected to iothub\r\n");
}
else
{
(void)printf("The device client has been disconnected\r\n");
}
}
static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void *user_context)
{
(void)user_context;
const char *messageId;
const char *correlationId;
// Message properties
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
{
messageId = "<unavailable>";
}
if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
{
correlationId = "<unavailable>";
}
IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message);
if (content_type == IOTHUBMESSAGE_BYTEARRAY)
{
const unsigned char *buff_msg;
size_t buff_len;
if (IoTHubMessage_GetByteArray(message, &buff_msg, &buff_len) != IOTHUB_MESSAGE_OK)
{
(void)printf("Failure retrieving byte array message\r\n");
}
else
{
(void)printf("Received Binary message\r\nMessage ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", messageId, correlationId, (int)buff_len, buff_msg, (int)buff_len);
}
}
else
{
const char *string_msg = IoTHubMessage_GetString(message);
if (string_msg == NULL)
{
(void)printf("Failure retrieving byte array message\r\n");
}
else
{
(void)printf("Received String Message\r\nMessage ID: %s\r\n Correlation ID: %s\r\n Data: <<<%s>>>\r\n", messageId, correlationId, string_msg);
}
}
const char *property_value = "property_value";
const char *property_key = IoTHubMessage_GetProperty(message, property_value);
if (property_key != NULL)
{
printf("\r\nMessage Properties:\r\n");
printf("\tKey: %s Value: %s\r\n", property_value, property_key);
}
g_message_recv_count++;
return IOTHUBMESSAGE_ACCEPTED;
}
void iothub_ll_client_x509_sample(void)
{
IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
//size_t messages_count = 0;
IOTHUB_MESSAGE_HANDLE message_handle;
size_t messages_sent = 0;
float telemetry_temperature;
float telemetry_humidity;
const char *telemetry_scale = "Celsius";
//const char* telemetry_msg = "test_message";
char telemetry_msg_buffer[80];
// Select the Protocol to use with the connection
#ifdef SAMPLE_MQTT
protocol = MQTT_Protocol;
#endif // SAMPLE_MQTT
#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
protocol = MQTT_WebSocket_Protocol;
#endif // SAMPLE_MQTT_OVER_WEBSOCKETS
#ifdef SAMPLE_AMQP
protocol = AMQP_Protocol;
#endif // SAMPLE_AMQP
#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
protocol = AMQP_Protocol_over_WebSocketsTls;
#endif // SAMPLE_AMQP_OVER_WEBSOCKETS
#ifdef SAMPLE_HTTP
protocol = HTTP_Protocol;
#endif // SAMPLE_HTTP
// Used to initialize IoTHub SDK subsystem
(void)IoTHub_Init();
IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
(void)printf("Creating IoTHub Device handle\r\n");
// Create the iothub handle here
device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
if (device_ll_handle == NULL)
{
(void)printf("Failure creating IotHub device. Hint: Check your connection string.\r\n");
}
else
{
// Set any option that are neccessary.
// For available options please see the iothub_sdk_options.md documentation
#ifndef SAMPLE_HTTP
// Can not set this options in HTTP
bool traceOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
#endif
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
// Setting the Trusted Certificate. This is only necessary on systems without
// built in certificate stores.
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);
#endif // SET_TRUSTED_CERT_IN_SAMPLES
#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_OVER_WEBSOCKETS
// Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
// you are URL Encoding inputs yourself.
// ONLY valid for use with MQTT
bool urlEncodeOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
#endif
// Setting connection status callback to get indication of connection to iothub
(void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL);
// if (IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle, receive_msg_callback, &messages_count) != IOTHUB_CLIENT_OK)
// Set the X509 certificates in the SDK
if (
(IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_X509_CERT, x509certificate) != IOTHUB_CLIENT_OK) ||
(IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_X509_PRIVATE_KEY, x509privatekey) != IOTHUB_CLIENT_OK))
{
printf("failure to set options for x509, aborting\r\n");
}
else
{
do
{
if (messages_sent < MESSAGE_COUNT)
{
// // Construct the iothub message from a string or a byte array
//message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
// //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
// Construct the iothub message
telemetry_temperature = 20.0f + ((float)rand() / RAND_MAX) * 15.0f;
telemetry_humidity = 60.0f + ((float)rand() / RAND_MAX) * 20.0f;
sprintf(telemetry_msg_buffer, "{\"temperature\":%.3f,\"humidity\":%.3f,\"scale\":\"%s\"}",
telemetry_temperature, telemetry_humidity, telemetry_scale);
message_handle = IoTHubMessage_CreateFromString(telemetry_msg_buffer);
// Set Message property
(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
(void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
(void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
(void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
// Add custom properties to message
//(void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value");
// dont use blank, special char. need encoding
(void)IoTHubMessage_SetProperty(message_handle, "display_message", "Hello_RP2040_W5100S");
//(void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1));
//IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
(void)printf("\r\nSending message %d to IoTHub\r\nMessage: %s\r\n", (int)(messages_sent + 1), telemetry_msg_buffer);
//IoTHubDeviceClient_SendEventAsync(device_handle, message_handle, send_confirm_callback, NULL);
IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
// The message is copied to the sdk so the we can destroy it
IoTHubMessage_Destroy(message_handle);
messages_sent++;
}
else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
{
// After all messages are all received stop running
g_continueRunning = false;
}
IoTHubDeviceClient_LL_DoWork(device_ll_handle);
sleep_ms(500); // wait for
} while (g_continueRunning);
// Clean up the iothub sdk handle
IoTHubDeviceClient_LL_Destroy(device_ll_handle);
}
}
// Free all the sdk subsystem
IoTHub_Deinit();
}
//===========================