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 "azure_c_shared_utility/gballoc.h"
|
---|
6 | #include "azure_prov_client/prov_transport_mqtt_client.h"
|
---|
7 | #include "azure_prov_client/internal/prov_transport_mqtt_common.h"
|
---|
8 | #include "azure_c_shared_utility/xio.h"
|
---|
9 | #include "azure_c_shared_utility/tlsio.h"
|
---|
10 | #include "azure_c_shared_utility/http_proxy_io.h"
|
---|
11 | #include "azure_c_shared_utility/platform.h"
|
---|
12 | #include "azure_c_shared_utility/xlogging.h"
|
---|
13 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
14 |
|
---|
15 | #define MQTT_PORT_NUM 8883
|
---|
16 |
|
---|
17 | static XIO_HANDLE mqtt_transport_io(const char* fqdn, const HTTP_PROXY_OPTIONS* proxy_info)
|
---|
18 | {
|
---|
19 | XIO_HANDLE result;
|
---|
20 | HTTP_PROXY_IO_CONFIG proxy_config;
|
---|
21 | TLSIO_CONFIG tls_io_config;
|
---|
22 |
|
---|
23 | memset(&tls_io_config, 0, sizeof(TLSIO_CONFIG));
|
---|
24 | tls_io_config.hostname = fqdn;
|
---|
25 | tls_io_config.port = MQTT_PORT_NUM;
|
---|
26 | if (proxy_info != NULL)
|
---|
27 | {
|
---|
28 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_012: [ If proxy_info is not NULL, amqp_transport_io shall construct a HTTP_PROXY_IO_CONFIG object and assign it to TLSIO_CONFIG underlying_io_parameters ] */
|
---|
29 | proxy_config.hostname = tls_io_config.hostname;
|
---|
30 | proxy_config.port = MQTT_PORT_NUM;
|
---|
31 | proxy_config.proxy_hostname = proxy_info->host_address;
|
---|
32 | proxy_config.proxy_port = proxy_info->port;
|
---|
33 | proxy_config.username = proxy_info->username;
|
---|
34 | proxy_config.password = proxy_info->password;
|
---|
35 |
|
---|
36 | tls_io_config.underlying_io_interface = http_proxy_io_get_interface_description();
|
---|
37 | tls_io_config.underlying_io_parameters = &proxy_config;
|
---|
38 | }
|
---|
39 |
|
---|
40 | const IO_INTERFACE_DESCRIPTION* tlsio_interface = platform_get_default_tlsio();
|
---|
41 | if (tlsio_interface == NULL)
|
---|
42 | {
|
---|
43 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_013: [ If any failure is encountered amqp_transport_io shall return NULL ]*/
|
---|
44 | LogError("platform_get_default_tlsio return NULL IO Interface");
|
---|
45 | result = NULL;
|
---|
46 | }
|
---|
47 | else
|
---|
48 | {
|
---|
49 | result = xio_create(tlsio_interface, &tls_io_config);
|
---|
50 | if (result == NULL)
|
---|
51 | {
|
---|
52 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_013: [ If any failure is encountered amqp_transport_io shall return NULL ]*/
|
---|
53 | LogError("failed calling xio_create on underlying io");
|
---|
54 | result = NULL;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_014: [ On success mqtt_transport_io shall return allocated XIO_HANDLE. ] */
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 |
|
---|
61 | PROV_DEVICE_TRANSPORT_HANDLE prov_transport_mqtt_create(const char* uri, TRANSPORT_HSM_TYPE type, const char* scope_id, const char* api_version, PROV_TRANSPORT_ERROR_CALLBACK error_cb, void* error_ctx)
|
---|
62 | {
|
---|
63 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_001: [ prov_transport_mqtt_create shall call the prov_transport_common_mqtt_create function with mqtt_transport_io transport IO estabishment. ] */
|
---|
64 | return prov_transport_common_mqtt_create(uri, type, scope_id, api_version, mqtt_transport_io, error_cb, error_ctx);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void prov_transport_mqtt_destroy(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
68 | {
|
---|
69 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_002: [ prov_transport_mqtt_destroy shall invoke the prov_transport_common_mqtt_destroy method ] */
|
---|
70 | prov_transport_common_mqtt_destroy(handle);
|
---|
71 | }
|
---|
72 |
|
---|
73 | int prov_transport_mqtt_open(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* registration_id, BUFFER_HANDLE ek, BUFFER_HANDLE srk, PROV_DEVICE_TRANSPORT_REGISTER_CALLBACK data_callback, void* user_ctx, PROV_DEVICE_TRANSPORT_STATUS_CALLBACK status_cb, void* status_ctx, PROV_TRANSPORT_CHALLENGE_CALLBACK reg_challenge_cb, void* challenge_ctx)
|
---|
74 | {
|
---|
75 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_003: [ prov_transport_mqtt_open shall invoke the prov_transport_common_mqtt_open method ] */
|
---|
76 | return prov_transport_common_mqtt_open(handle, registration_id, ek, srk, data_callback, user_ctx, status_cb, status_ctx, reg_challenge_cb, challenge_ctx);
|
---|
77 | }
|
---|
78 |
|
---|
79 | int prov_transport_mqtt_close(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
80 | {
|
---|
81 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_004: [ prov_transport_mqtt_close shall invoke the prov_transport_common_mqtt_close method ] */
|
---|
82 | return prov_transport_common_mqtt_close(handle);
|
---|
83 | }
|
---|
84 |
|
---|
85 | int prov_transport_mqtt_register_device(PROV_DEVICE_TRANSPORT_HANDLE handle, PROV_TRANSPORT_JSON_PARSE json_parse_cb, PROV_TRANSPORT_CREATE_JSON_PAYLOAD json_create_cb, void* json_ctx)
|
---|
86 | {
|
---|
87 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_005: [ prov_transport_mqtt_register_device shall invoke the prov_transport_common_mqtt_register_device method ] */
|
---|
88 | return prov_transport_common_mqtt_register_device(handle, json_parse_cb, json_create_cb, json_ctx);
|
---|
89 | }
|
---|
90 |
|
---|
91 | int prov_transport_mqtt_get_operation_status(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
92 | {
|
---|
93 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_006: [ prov_transport_mqtt_get_operation_status shall invoke the prov_transport_common_mqtt_get_operation_status method ] */
|
---|
94 | return prov_transport_common_mqtt_get_operation_status(handle);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void prov_transport_mqtt_dowork(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
98 | {
|
---|
99 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_007: [ prov_transport_mqtt_dowork shall invoke the prov_transport_common_mqtt_dowork method ] */
|
---|
100 | prov_transport_common_mqtt_dowork(handle);
|
---|
101 | }
|
---|
102 |
|
---|
103 | int prov_transport_mqtt_set_trace(PROV_DEVICE_TRANSPORT_HANDLE handle, bool trace_on)
|
---|
104 | {
|
---|
105 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_008: [ prov_transport_mqtt_set_trace shall invoke the prov_transport_common_mqtt_set_trace method ] */
|
---|
106 | return prov_transport_common_mqtt_set_trace(handle, trace_on);
|
---|
107 | }
|
---|
108 |
|
---|
109 | static int prov_transport_mqtt_x509_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate, const char* private_key)
|
---|
110 | {
|
---|
111 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_009: [ prov_transport_mqtt_x509_cert shall invoke the prov_trans_common_mqtt_x509_cert method ]*/
|
---|
112 | return prov_transport_common_mqtt_x509_cert(handle, certificate, private_key);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int prov_transport_mqtt_set_trusted_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate)
|
---|
116 | {
|
---|
117 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_010: [ prov_transport_mqtt_set_trusted_cert shall invoke the prov_transport_common_mqtt_set_trusted_cert method ] */
|
---|
118 | return prov_transport_common_mqtt_set_trusted_cert(handle, certificate);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static int prov_transport_mqtt_set_proxy(PROV_DEVICE_TRANSPORT_HANDLE handle, const HTTP_PROXY_OPTIONS* proxy_options)
|
---|
122 | {
|
---|
123 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_011: [ prov_transport_mqtt_set_proxy shall invoke the prov_transport_common_mqtt_set_proxy method ] */
|
---|
124 | return prov_transport_common_mqtt_set_proxy(handle, proxy_options);
|
---|
125 | }
|
---|
126 |
|
---|
127 | static int prov_transport_mqtt_set_option(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* option, const void* value)
|
---|
128 | {
|
---|
129 | return prov_transport_common_mqtt_set_option(handle, option, value);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static PROV_DEVICE_TRANSPORT_PROVIDER prov_mqtt_func =
|
---|
133 | {
|
---|
134 | prov_transport_mqtt_create,
|
---|
135 | prov_transport_mqtt_destroy,
|
---|
136 | prov_transport_mqtt_open,
|
---|
137 | prov_transport_mqtt_close,
|
---|
138 | prov_transport_mqtt_register_device,
|
---|
139 | prov_transport_mqtt_get_operation_status,
|
---|
140 | prov_transport_mqtt_dowork,
|
---|
141 | prov_transport_mqtt_set_trace,
|
---|
142 | prov_transport_mqtt_x509_cert,
|
---|
143 | prov_transport_mqtt_set_trusted_cert,
|
---|
144 | prov_transport_mqtt_set_proxy,
|
---|
145 | prov_transport_mqtt_set_option
|
---|
146 | };
|
---|
147 |
|
---|
148 | const PROV_DEVICE_TRANSPORT_PROVIDER* Prov_Device_MQTT_Protocol(void)
|
---|
149 | {
|
---|
150 | return &prov_mqtt_func;
|
---|
151 | }
|
---|