source: azure_iot_hub/trunk/azure_iothub/iothub_client/src/iothubtransportmqtt_websockets.c@ 389

Last change on this file since 389 was 389, checked in by coas-nagasima, 5 years ago

ビルドが通るよう更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 16.2 KB
Line 
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/xio.h"
6#include "azure_c_shared_utility/wsio.h"
7#include "azure_c_shared_utility/tlsio.h"
8#include "azure_c_shared_utility/platform.h"
9#include "azure_c_shared_utility/http_proxy_io.h"
10#include "iothubtransportmqtt_websockets.h"
11#include "internal/iothubtransport_mqtt_common.h"
12
13static XIO_HANDLE getWebSocketsIOTransport(const char* fully_qualified_name, const MQTT_TRANSPORT_PROXY_OPTIONS* mqtt_transport_proxy_options)
14{
15 XIO_HANDLE result;
16 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_001: [ `getIoTransportProvider` shall obtain the WebSocket IO interface handle by calling `wsio_get_interface_description`. ]*/
17 const IO_INTERFACE_DESCRIPTION* io_interface_description = wsio_get_interface_description();
18 TLSIO_CONFIG tls_io_config;
19 HTTP_PROXY_IO_CONFIG http_proxy_io_config;
20
21 if (io_interface_description == NULL)
22 {
23 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_013: [ If `wsio_get_interface_description` returns NULL `getIoTransportProvider` shall return NULL. ] */
24 LogError("Failure constructing the provider interface");
25 result = NULL;
26 }
27 else
28 {
29 WSIO_CONFIG ws_io_config;
30
31 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_003: [ - `hostname` shall be set to `fully_qualified_name`. ]*/
32 ws_io_config.hostname = fully_qualified_name;
33 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_004: [ - `port` shall be set to 443. ]*/
34 ws_io_config.port = 443;
35 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_005: [ - `protocol` shall be set to `MQTT`. ]*/
36 ws_io_config.protocol = "MQTT";
37 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_006: [ - `resource_name` shall be set to `/$iothub/websocket`. ]*/
38 ws_io_config.resource_name = "/$iothub/websocket";
39 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_007: [ - `underlying_io_interface` shall be set to the TLS IO interface description. ]*/
40 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_009: [ `getIoTransportProvider` shall obtain the TLS IO interface handle by calling `platform_get_default_tlsio`. ]*/
41 ws_io_config.underlying_io_interface = platform_get_default_tlsio();
42
43 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_029: [ If `platform_get_default_tlsio` returns NULL, NULL shall be set in the WebSocket IO parameters structure for the interface description and parameters. ]*/
44 if (ws_io_config.underlying_io_interface == NULL)
45 {
46 ws_io_config.underlying_io_parameters = NULL;
47 }
48 else
49 {
50 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_008: [ - `underlying_io_parameters` shall be set to the TLS IO arguments. ]*/
51 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_010: [ The TLS IO parameters shall be a TLSIO_CONFIG structure filled as below: ]*/
52 ws_io_config.underlying_io_parameters = &tls_io_config;
53
54 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_011: [ - `hostname` shall be set to `fully_qualified_name`. ]*/
55 tls_io_config.hostname = fully_qualified_name;
56 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_012: [ - `port` shall be set to 443. ]*/
57 tls_io_config.port = 443;
58
59 if (mqtt_transport_proxy_options != NULL)
60 {
61 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_015: [ - If `mqtt_transport_proxy_options` is not NULL, `underlying_io_interface` shall be set to the HTTP proxy IO interface description. ]*/
62 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_022: [ `getIoTransportProvider` shall obtain the HTTP proxy IO interface handle by calling `http_proxy_io_get_interface_description`. ]*/
63 tls_io_config.underlying_io_interface = http_proxy_io_get_interface_description();
64
65 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_028: [ If `http_proxy_io_get_interface_description` returns NULL, NULL shall be set in the TLS IO parameters structure for the interface description and parameters. ]*/
66 if (tls_io_config.underlying_io_interface == NULL)
67 {
68 tls_io_config.underlying_io_parameters = NULL;
69 }
70 else
71 {
72 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_016: [ - If `mqtt_transport_proxy_options` is not NULL `underlying_io_parameters` shall be set to the HTTP proxy IO arguments. ]*/
73 tls_io_config.underlying_io_parameters = &http_proxy_io_config;
74
75 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_023: [ The HTTP proxy IO arguments shall be an `HTTP_PROXY_IO_CONFIG` structure, filled as below: ]*/
76 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_026: [ - `proxy_hostname`, `proxy_port`, `username` and `password` shall be copied from the `mqtt_transport_proxy_options` argument. ]*/
77 http_proxy_io_config.proxy_hostname = mqtt_transport_proxy_options->host_address;
78 http_proxy_io_config.proxy_port = mqtt_transport_proxy_options->port;
79 http_proxy_io_config.username = mqtt_transport_proxy_options->username;
80 http_proxy_io_config.password = mqtt_transport_proxy_options->password;
81 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_024: [ - `hostname` shall be set to `fully_qualified_name`. ]*/
82 http_proxy_io_config.hostname = fully_qualified_name;
83 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_025: [ - `port` shall be set to 443. ]*/
84 http_proxy_io_config.port = 443;
85 }
86 }
87 else
88 {
89 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_013: [ - If `mqtt_transport_proxy_options` is NULL, `underlying_io_interface` shall be set to NULL ]*/
90 tls_io_config.underlying_io_interface = NULL;
91 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_014: [ - If `mqtt_transport_proxy_options` is NULL `underlying_io_parameters` shall be set to NULL. ]*/
92 tls_io_config.underlying_io_parameters = NULL;
93 }
94 }
95
96 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_012: [ `getIoTransportProvider` shall return the `XIO_HANDLE` returned by `xio_create`. ] */
97 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_01_002: [ `getIoTransportProvider` shall call `xio_create` while passing the WebSocket IO interface description to it and the WebSocket configuration as a WSIO_CONFIG structure, filled as below ]*/
98 result = xio_create(io_interface_description, &ws_io_config);
99 }
100 return result;
101}
102
103/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_001: [ IoTHubTransportMqtt_WS_Create shall create a TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Create function. ] */
104static TRANSPORT_LL_HANDLE IoTHubTransportMqtt_WS_Create(const IOTHUBTRANSPORT_CONFIG* config, TRANSPORT_CALLBACKS_INFO* cb_info, void* ctx)
105{
106 return IoTHubTransport_MQTT_Common_Create(config, getWebSocketsIOTransport, cb_info, ctx);
107}
108
109/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_002: [ IoTHubTransportMqtt_WS_Destroy shall destroy the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Destroy function. ] */
110static void IoTHubTransportMqtt_WS_Destroy(TRANSPORT_LL_HANDLE handle)
111{
112 IoTHubTransport_MQTT_Common_Destroy(handle);
113}
114
115/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_005: [ IoTHubTransportMqtt_WS_Subscribe shall subscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Subscribe function. ] */
116static int IoTHubTransportMqtt_WS_Subscribe(IOTHUB_DEVICE_HANDLE handle)
117{
118 return IoTHubTransport_MQTT_Common_Subscribe(handle);
119}
120
121/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_006: [ IoTHubTransportMqtt_WS_Unsubscribe shall unsubscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unsubscribe function. ] */
122static void IoTHubTransportMqtt_WS_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
123{
124 IoTHubTransport_MQTT_Common_Unsubscribe(handle);
125}
126
127/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_015: [ IoTHubTransportMqtt_WS_Subscribe_DeviceMethod shall call into the IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod function ] */
128static int IoTHubTransportMqtt_WS_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
129{
130 return IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(handle);
131}
132
133/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_016: [ IoTHubTransportMqtt_WS_Unsubscribe_DeviceMethod shall call into the IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod ] */
134static void IoTHubTransportMqtt_WS_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
135{
136 IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(handle);
137}
138
139static int IoTHubTransportMqtt_WS_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response)
140{
141 return IoTHubTransport_MQTT_Common_DeviceMethod_Response(handle, methodId, response, response_size, status_response);
142}
143
144/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_017: [ IoTHubTransportMqtt_WS_Subscribe_DeviceTwin shall call into the IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin ] */
145static int IoTHubTransportMqtt_WS_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
146{
147 return IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(handle);
148}
149
150/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_018: [ IoTHubTransportMqtt_WS_Unsubscribe_DeviceTwin shall call into the IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin ] */
151static void IoTHubTransportMqtt_WS_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
152{
153 IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(handle);
154}
155
156// Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_09_001: [ IoTHubTransportMqtt_WS_GetTwinAsync shall call into the IoTHubTransport_MQTT_Common_GetTwinAsync ]
157static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_WS_GetTwinAsync(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK completionCallback, void* callbackContext)
158{
159 return IoTHubTransport_MQTT_Common_GetTwinAsync(handle, completionCallback, callbackContext);
160}
161
162/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_014: [ IoTHubTransportMqtt_WS_ProcessItem shall call into the IoTHubTransport_MQTT_Common_DoWork function ] */
163static IOTHUB_PROCESS_ITEM_RESULT IoTHubTransportMqtt_WS_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item)
164{
165 return IoTHubTransport_MQTT_Common_ProcessItem(handle, item_type, iothub_item);
166}
167
168/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_007: [ IoTHubTransportMqtt_WS_DoWork shall call into the IoTHubMqttAbstract_DoWork function. ] */
169static void IoTHubTransportMqtt_WS_DoWork(TRANSPORT_LL_HANDLE handle)
170{
171 IoTHubTransport_MQTT_Common_DoWork(handle);
172}
173
174/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_008: [ IoTHubTransportMqtt_WS_GetSendStatus shall get the send status by calling into the IoTHubMqttAbstract_GetSendStatus function. ] */
175static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_WS_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
176{
177 return IoTHubTransport_MQTT_Common_GetSendStatus(handle, iotHubClientStatus);
178}
179
180/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_009: [ IoTHubTransportMqtt_WS_SetOption shall set the options by calling into the IoTHubMqttAbstract_SetOption function. ] */
181static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_WS_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
182{
183 return IoTHubTransport_MQTT_Common_SetOption(handle, option, value);
184}
185
186/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_003: [ IoTHubTransportMqtt_WS_Register shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Register function. ]*/
187static IOTHUB_DEVICE_HANDLE IoTHubTransportMqtt_WS_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, PDLIST_ENTRY waitingToSend)
188{
189 return IoTHubTransport_MQTT_Common_Register(handle, device, waitingToSend);
190}
191
192/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_004: [ IoTHubTransportMqtt_WS_Unregister shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unregister function. ] */
193static void IoTHubTransportMqtt_WS_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
194{
195 IoTHubTransport_MQTT_Common_Unregister(deviceHandle);
196}
197
198/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_010: [ IoTHubTransportMqtt_WS_GetHostname shall get the hostname by calling into the IoTHubMqttAbstract_GetHostname function. ] */
199static STRING_HANDLE IoTHubTransportMqtt_WS_GetHostname(TRANSPORT_LL_HANDLE handle)
200{
201 return IoTHubTransport_MQTT_Common_GetHostname(handle);
202}
203
204static int IoTHubTransportMqtt_WS_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitinSeconds)
205{
206 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_25_012: [** IoTHubTransportMqtt_WS_SetRetryPolicy shall call into the IoTHubMqttAbstract_SetRetryPolicy function.]*/
207 return IoTHubTransport_MQTT_Common_SetRetryPolicy(handle, retryPolicy, retryTimeoutLimitinSeconds);
208}
209
210static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_WS_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
211{
212 /* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_10_001: [IoTHubTransportMqtt_WS_SendMessageDisposition shall send the message disposition by calling into the IoTHubTransport_MQTT_Common_SendMessageDisposition()]*/
213 return IoTHubTransport_MQTT_Common_SendMessageDisposition(message_data, disposition);
214}
215
216static int IoTHubTransportMqtt_WS_Subscribe_InputQueue(IOTHUB_DEVICE_HANDLE handle)
217{
218 (void)handle;
219 LogError("IoTHubTransportMqtt_WS_Subscribe_InputQueue not implemented\n");
220 return MU_FAILURE;
221}
222
223static void IoTHubTransportMqtt_WS_Unsubscribe_InputQueue(IOTHUB_DEVICE_HANDLE handle)
224{
225 LogError("IoTHubTransportMqtt_WS_Unsubscribe_InputQueue not implemented\n");
226 (void)handle;
227}
228
229static int IotHubTransportMqtt_WS_SetCallbackContext(TRANSPORT_LL_HANDLE handle, void* ctx)
230{
231 return IoTHubTransport_MQTT_SetCallbackContext(handle, ctx);
232}
233
234/* Codes_SRS_IOTHUB_MQTT_WEBSOCKET_TRANSPORT_07_011: [ This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:
235IoTHubTransport_SendMessageDisposition = IoTHubTransport_WS_SendMessageDisposition
236IoTHubTransport_Subscribe_DeviceMethod = IoTHubTransport_WS_Subscribe_DeviceMethod
237IoTHubTransport_Unsubscribe_DeviceMethod IoTHubTransport_WS_Unsubscribe_DeviceMethod
238IoTHubTransport_Subscribe_DeviceTwin IoTHubTransport_WS_Subscribe_DeviceTwin
239IoTHubTransport_Unsubscribe_DeviceTwin IoTHubTransport_WS_Unsubscribe_DeviceTwin
240IoTHubTransport_GetHostname = IoTHubTransportMqtt_WS_GetHostname
241IoTHubTransport_Create = IoTHubTransportMqtt_WS_Create
242IoTHubTransport_Destroy = IoTHubTransportMqtt_WS_Destroy
243IoTHubTransport_Subscribe = IoTHubTransportMqtt_WS_Subscribe
244IoTHubTransport_Unsubscribe = IoTHubTransportMqtt_WS_Unsubscribe
245IoTHubTransport_DoWork = IoTHubTransportMqtt_WS_DoWork
246IoTHubTransport_SetOption = IoTHubTransportMqtt_WS_SetOption ] */
247static TRANSPORT_PROVIDER thisTransportProvider_WebSocketsOverTls = {
248 IoTHubTransportMqtt_WS_SendMessageDisposition,
249 IoTHubTransportMqtt_WS_Subscribe_DeviceMethod,
250 IoTHubTransportMqtt_WS_Unsubscribe_DeviceMethod,
251 IoTHubTransportMqtt_WS_DeviceMethod_Response,
252 IoTHubTransportMqtt_WS_Subscribe_DeviceTwin,
253 IoTHubTransportMqtt_WS_Unsubscribe_DeviceTwin,
254 IoTHubTransportMqtt_WS_ProcessItem,
255 IoTHubTransportMqtt_WS_GetHostname,
256 IoTHubTransportMqtt_WS_SetOption,
257 IoTHubTransportMqtt_WS_Create,
258 IoTHubTransportMqtt_WS_Destroy,
259 IoTHubTransportMqtt_WS_Register,
260 IoTHubTransportMqtt_WS_Unregister,
261 IoTHubTransportMqtt_WS_Subscribe,
262 IoTHubTransportMqtt_WS_Unsubscribe,
263 IoTHubTransportMqtt_WS_DoWork,
264 IoTHubTransportMqtt_WS_SetRetryPolicy,
265 IoTHubTransportMqtt_WS_GetSendStatus,
266 IoTHubTransportMqtt_WS_Subscribe_InputQueue,
267 IoTHubTransportMqtt_WS_Unsubscribe_InputQueue,
268 IotHubTransportMqtt_WS_SetCallbackContext,
269 IoTHubTransportMqtt_WS_GetTwinAsync
270};
271
272const TRANSPORT_PROVIDER* MQTT_WebSocket_Protocol(void)
273{
274 return &thisTransportProvider_WebSocketsOverTls;
275}
Note: See TracBrowser for help on using the repository browser.