[457] | 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 |
|
---|
| 5 | // hint on compile; copy the source code here and paste onto ->
|
---|
| 6 | // azure-iot-sdk-c/provisioning_client/samples/prov_dev_client_ll_sample/prov_dev_client_ll_sample.c
|
---|
| 7 | // compile the whole SDK with provisioning enabled
|
---|
| 8 |
|
---|
| 9 | #include <stdio.h>
|
---|
| 10 | #include <stdlib.h>
|
---|
| 11 |
|
---|
| 12 | #include "iothub.h"
|
---|
| 13 | #include "iothub_message.h"
|
---|
| 14 | #include "iothub_client_version.h"
|
---|
| 15 | #include "azure_c_shared_utility/threadapi.h"
|
---|
| 16 | #include "azure_c_shared_utility/tickcounter.h"
|
---|
| 17 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
| 18 | #include "azure_c_shared_utility/http_proxy_io.h"
|
---|
| 19 |
|
---|
| 20 | #include "iothub_device_client_ll.h"
|
---|
| 21 | #include "iothub_client_options.h"
|
---|
| 22 | #include "azure_prov_client/prov_device_ll_client.h"
|
---|
| 23 | #include "azure_prov_client/prov_security_factory.h"
|
---|
| 24 |
|
---|
| 25 | #include "iothubtransporthttp.h"
|
---|
| 26 | #include "azure_prov_client/prov_transport_http_client.h"
|
---|
| 27 | #include "iothubtransportmqtt.h"
|
---|
| 28 | #include "azure_prov_client/prov_transport_mqtt_client.h"
|
---|
| 29 | #include "iothubtransportmqtt_websockets.h"
|
---|
| 30 | #include "azure_prov_client/prov_transport_mqtt_ws_client.h"
|
---|
| 31 | #include "client.h"
|
---|
| 32 |
|
---|
| 33 | #define SET_TRUSTED_CERT_IN_SAMPLES
|
---|
| 34 |
|
---|
| 35 | #ifdef SET_TRUSTED_CERT_IN_SAMPLES
|
---|
| 36 | #include "certs.h"
|
---|
| 37 | #endif // SET_TRUSTED_CERT_IN_SAMPLES
|
---|
| 38 |
|
---|
| 39 | extern char* connectionString;
|
---|
| 40 |
|
---|
| 41 | MU_DEFINE_ENUM_STRINGS(PROV_DEVICE_RESULT, PROV_DEVICE_RESULT_VALUE);
|
---|
| 42 | MU_DEFINE_ENUM_STRINGS(PROV_DEVICE_REG_STATUS, PROV_DEVICE_REG_STATUS_VALUES);
|
---|
| 43 |
|
---|
| 44 | static const char *global_prov_uri = "global.azure-devices-provisioning.net";
|
---|
| 45 | static char *g_access_key = NULL;
|
---|
| 46 |
|
---|
| 47 | extern bool g_use_proxy;
|
---|
| 48 | extern HTTP_PROXY_OPTIONS g_proxy_options;
|
---|
| 49 |
|
---|
| 50 | #define MESSAGES_TO_SEND 2
|
---|
| 51 | #define TIME_BETWEEN_MESSAGES 2
|
---|
| 52 |
|
---|
| 53 | typedef struct CLIENT_SAMPLE_INFO_TAG
|
---|
| 54 | {
|
---|
| 55 | unsigned int sleep_time;
|
---|
| 56 | char *iothub_uri;
|
---|
| 57 | char *access_key_name;
|
---|
| 58 | char *device_key;
|
---|
| 59 | char *device_id;
|
---|
| 60 | int registration_complete;
|
---|
| 61 | } CLIENT_SAMPLE_INFO;
|
---|
| 62 |
|
---|
| 63 | typedef struct IOTHUB_CLIENT_SAMPLE_INFO_TAG
|
---|
| 64 | {
|
---|
| 65 | int connected;
|
---|
| 66 | int stop_running;
|
---|
| 67 | } IOTHUB_CLIENT_SAMPLE_INFO;
|
---|
| 68 |
|
---|
| 69 | static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void *user_context)
|
---|
| 70 | {
|
---|
| 71 | (void)message;
|
---|
| 72 | IOTHUB_CLIENT_SAMPLE_INFO *iothub_info = (IOTHUB_CLIENT_SAMPLE_INFO *)user_context;
|
---|
| 73 | (void)printf("Stop message recieved from IoTHub\r\n");
|
---|
| 74 | iothub_info->stop_running = 1;
|
---|
| 75 | return IOTHUBMESSAGE_ACCEPTED;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static void registation_status_callback(PROV_DEVICE_REG_STATUS reg_status, void *user_context)
|
---|
| 79 | {
|
---|
| 80 | (void)user_context;
|
---|
| 81 | (void)printf(".");
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | static void iothub_connection_status(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void *user_context)
|
---|
| 85 | {
|
---|
| 86 | (void)reason;
|
---|
| 87 | if (user_context == NULL)
|
---|
| 88 | {
|
---|
| 89 | printf("iothub_connection_status user_context is NULL\r\n");
|
---|
| 90 | }
|
---|
| 91 | else
|
---|
| 92 | {
|
---|
| 93 | IOTHUB_CLIENT_SAMPLE_INFO *iothub_info = (IOTHUB_CLIENT_SAMPLE_INFO *)user_context;
|
---|
| 94 | if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
|
---|
| 95 | {
|
---|
| 96 | iothub_info->connected = 1;
|
---|
| 97 | }
|
---|
| 98 | else
|
---|
| 99 | {
|
---|
| 100 | iothub_info->connected = 0;
|
---|
| 101 | iothub_info->stop_running = 1;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | static void register_device_callback(PROV_DEVICE_RESULT register_result, const char *iothub_uri, const char *device_id, void *user_context)
|
---|
| 107 | {
|
---|
| 108 | if (user_context == NULL)
|
---|
| 109 | {
|
---|
| 110 | printf("user_context is NULL\r\n");
|
---|
| 111 | }
|
---|
| 112 | else
|
---|
| 113 | {
|
---|
| 114 | CLIENT_SAMPLE_INFO *user_ctx = (CLIENT_SAMPLE_INFO *)user_context;
|
---|
| 115 | if (register_result == PROV_DEVICE_RESULT_OK)
|
---|
| 116 | {
|
---|
| 117 | (void)printf("\nRegistration Information received from service: %s!\r\n", iothub_uri);
|
---|
| 118 | (void)mallocAndStrcpy_s(&user_ctx->iothub_uri, iothub_uri);
|
---|
| 119 | (void)mallocAndStrcpy_s(&user_ctx->device_id, device_id);
|
---|
| 120 | user_ctx->registration_complete = 1;
|
---|
| 121 |
|
---|
| 122 | size_t len = snprintf(NULL, 0,
|
---|
| 123 | "HostName=%s;DeviceId=%s;SharedAccessKey=%s",
|
---|
| 124 | iothub_uri,
|
---|
| 125 | device_id,
|
---|
| 126 | g_access_key);
|
---|
| 127 | char *conn_str = (char *)malloc(len + 1);
|
---|
| 128 | if (conn_str == NULL) {
|
---|
| 129 | printf("OOM while creating connection string\n");
|
---|
| 130 | exit(1);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | snprintf(conn_str, len + 1,
|
---|
| 134 | "HostName=%s;DeviceId=%s;SharedAccessKey=%s",
|
---|
| 135 | iothub_uri,
|
---|
| 136 | device_id,
|
---|
| 137 | g_access_key);
|
---|
| 138 | conn_str[len] = 0;
|
---|
| 139 | free(connectionString);
|
---|
| 140 | connectionString = conn_str;
|
---|
| 141 | printf("Connection String:\n%s\n", connectionString);
|
---|
| 142 | }
|
---|
| 143 | else
|
---|
| 144 | {
|
---|
| 145 | (void)printf("Failure encountered on registration %s\r\n", MU_ENUM_TO_STRING(PROV_DEVICE_RESULT, register_result));
|
---|
| 146 | user_ctx->registration_complete = 2;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | int dps_csgen_main(int argc, char *argv[])
|
---|
| 152 | {
|
---|
| 153 | if (argc < 5) {
|
---|
| 154 | printf("usage:\ndps_csgen [http|mqtt|mqttows] <scope_id> <device_id> <primary_key (groupSAS)>\n");
|
---|
| 155 | return 0;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | IOTHUB_CLIENT_TRANSPORT_PROVIDER iothub_transport;
|
---|
| 159 | PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION prov_device_protocol;
|
---|
| 160 |
|
---|
| 161 | if (strcmp(argv[1], "http") == 0) {
|
---|
| 162 | iothub_transport = HTTP_Protocol;
|
---|
| 163 | prov_device_protocol = Prov_Device_HTTP_Protocol;
|
---|
| 164 | }
|
---|
| 165 | else if (strcmp(argv[1], "mqtt") == 0) {
|
---|
| 166 | iothub_transport = MQTT_Protocol;
|
---|
| 167 | prov_device_protocol = Prov_Device_MQTT_Protocol;
|
---|
| 168 | }
|
---|
| 169 | else if (strcmp(argv[1], "mqttows") == 0) {
|
---|
| 170 | iothub_transport = MQTT_WebSocket_Protocol;
|
---|
| 171 | prov_device_protocol = Prov_Device_MQTT_WS_Protocol;
|
---|
| 172 | }
|
---|
| 173 | else {
|
---|
| 174 | iothub_transport = MQTT_Protocol;
|
---|
| 175 | prov_device_protocol = Prov_Device_MQTT_Protocol;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | const char *scope_id = argv[2];
|
---|
| 179 | const char *reg_id = argv[3];
|
---|
| 180 | g_access_key = argv[4];
|
---|
| 181 | prov_dev_set_symmetric_key_info(reg_id, g_access_key);
|
---|
| 182 |
|
---|
| 183 | bool traceOn = false;
|
---|
| 184 |
|
---|
| 185 | (void)IoTHub_Init();
|
---|
| 186 | (void)prov_dev_security_init(SECURE_DEVICE_TYPE_SYMMETRIC_KEY);
|
---|
| 187 | HTTP_PROXY_OPTIONS http_proxy;
|
---|
| 188 | CLIENT_SAMPLE_INFO user_ctx;
|
---|
| 189 |
|
---|
| 190 | memset(&http_proxy, 0, sizeof(HTTP_PROXY_OPTIONS));
|
---|
| 191 | memset(&user_ctx, 0, sizeof(CLIENT_SAMPLE_INFO));
|
---|
| 192 |
|
---|
| 193 | user_ctx.registration_complete = 0;
|
---|
| 194 | user_ctx.sleep_time = 10;
|
---|
| 195 |
|
---|
| 196 | PROV_DEVICE_LL_HANDLE handle;
|
---|
| 197 | if ((handle = Prov_Device_LL_Create(global_prov_uri, scope_id, prov_device_protocol)) == NULL)
|
---|
| 198 | {
|
---|
| 199 | (void)printf("failed calling Prov_Device_LL_Create\r\n");
|
---|
| 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | if (g_use_proxy)
|
---|
| 204 | {
|
---|
| 205 | Prov_Device_LL_SetOption(handle, OPTION_HTTP_PROXY, &g_proxy_options);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | #ifdef SET_TRUSTED_CERT_IN_SAMPLES
|
---|
| 209 | // For mbed add the certificate information
|
---|
| 210 | if (Prov_Device_LL_SetOption(handle, OPTION_TRUSTED_CERT, certificates) != PROV_DEVICE_RESULT_OK)
|
---|
| 211 | {
|
---|
| 212 | printf("failure to set option \"TrustedCerts\"\r\n");
|
---|
| 213 | }
|
---|
| 214 | #endif // SET_TRUSTED_CERT_IN_SAMPLES
|
---|
| 215 |
|
---|
| 216 | Prov_Device_LL_SetOption(handle, PROV_OPTION_LOG_TRACE, &traceOn);
|
---|
| 217 | if (Prov_Device_LL_Register_Device(handle, register_device_callback, &user_ctx, registation_status_callback, &user_ctx) != PROV_DEVICE_RESULT_OK)
|
---|
| 218 | {
|
---|
| 219 | (void)printf("failed calling Prov_Device_LL_Register_Device\r\n");
|
---|
| 220 | }
|
---|
| 221 | else
|
---|
| 222 | {
|
---|
| 223 | do
|
---|
| 224 | {
|
---|
| 225 | Prov_Device_LL_DoWork(handle);
|
---|
| 226 | ThreadAPI_Sleep(user_ctx.sleep_time);
|
---|
| 227 | } while (user_ctx.registration_complete == 0);
|
---|
| 228 | }
|
---|
| 229 | Prov_Device_LL_Destroy(handle);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (user_ctx.registration_complete != 1)
|
---|
| 233 | {
|
---|
| 234 | (void)printf("error: registration failed!\r\n");
|
---|
| 235 | }
|
---|
| 236 | else
|
---|
| 237 | {
|
---|
| 238 | IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
|
---|
| 239 |
|
---|
| 240 | if ((device_ll_handle = IoTHubDeviceClient_LL_CreateFromDeviceAuth(user_ctx.iothub_uri, user_ctx.device_id, iothub_transport)) == NULL)
|
---|
| 241 | {
|
---|
| 242 | (void)printf("failed create IoTHub client from connection string %s!\r\n", user_ctx.iothub_uri);
|
---|
| 243 | }
|
---|
| 244 | else
|
---|
| 245 | {
|
---|
| 246 | IOTHUB_CLIENT_SAMPLE_INFO iothub_info;
|
---|
| 247 | iothub_info.stop_running = 0;
|
---|
| 248 | iothub_info.connected = 0;
|
---|
| 249 |
|
---|
| 250 | (void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, iothub_connection_status, &iothub_info);
|
---|
| 251 | IoTHubDeviceClient_LL_Destroy(device_ll_handle);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | free(user_ctx.iothub_uri);
|
---|
| 255 | free(user_ctx.device_id);
|
---|
| 256 | prov_dev_security_deinit();
|
---|
| 257 |
|
---|
| 258 | IoTHub_Deinit();
|
---|
| 259 | return 0;
|
---|
| 260 | }
|
---|