Azure SDK for Embedded C
az_iot_hub_client.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
17 #ifndef _az_IOT_HUB_CLIENT_H
18 #define _az_IOT_HUB_CLIENT_H
19 
20 #include <azure/core/az_result.h>
21 #include <azure/core/az_span.h>
23 
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 #include <azure/core/_az_cfg_prefix.h>
28 
33 enum
34 {
35  AZ_HUB_CLIENT_DEFAULT_MQTT_TELEMETRY_QOS = 0
36 };
37 
42 typedef struct
43 {
50 
54 typedef struct
55 {
56  struct
57  {
58  az_span iot_hub_hostname;
59  az_span device_id;
61  } _internal;
63 
72 
91  az_iot_hub_client* client,
92  az_span iot_hub_hostname,
93  az_span device_id,
94  az_iot_hub_client_options const* options);
95 
99 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "/$iothub/websocket"
100 
107 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH_NO_X509_CLIENT_CERT \
108  AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "?iothub-no-client-cert=true"
109 
128  az_iot_hub_client const* client,
129  char* mqtt_user_name,
130  size_t mqtt_user_name_size,
131  size_t* out_mqtt_user_name_length);
132 
150  az_iot_hub_client const* client,
151  char* mqtt_client_id,
152  size_t mqtt_client_id_size,
153  size_t* out_mqtt_client_id_length);
154 
155 /*
156  *
157  * SAS Token APIs
158  *
159  * Use the following APIs when the Shared Access Key is available to the application or stored
160  * within a Hardware Security Module. The APIs are not necessary if X509 Client Certificate
161  * Authentication is used.
162  */
163 
179  az_iot_hub_client const* client,
180  uint64_t token_expiration_epoch_time,
181  az_span signature,
182  az_span* out_signature);
183 
209  az_iot_hub_client const* client,
210  uint64_t token_expiration_epoch_time,
211  az_span base64_hmac_sha256_signature,
212  az_span key_name,
213  char* mqtt_password,
214  size_t mqtt_password_size,
215  size_t* out_mqtt_password_length);
216 
217 /*
218  *
219  * Telemetry APIs
220  *
221  */
222 
240  az_iot_hub_client const* client,
241  az_iot_message_properties const* properties,
242  char* mqtt_topic,
243  size_t mqtt_topic_size,
244  size_t* out_mqtt_topic_length);
245 
246 /*
247  *
248  * Cloud-to-device (C2D) APIs
249  *
250  */
251 
256 #define AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC "devices/+/messages/devicebound/#"
257 
262 typedef struct
263 {
266 
282  az_iot_hub_client const* client,
283  az_span received_topic,
284  az_iot_hub_client_c2d_request* out_request);
285 
286 /*
287  *
288  * Methods APIs
289  *
290  */
291 
296 #define AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC "$iothub/methods/POST/#"
297 
302 typedef struct
303 {
308 
324  az_iot_hub_client const* client,
325  az_span received_topic,
326  az_iot_hub_client_method_request* out_request);
327 
345  az_iot_hub_client const* client,
346  az_span request_id,
347  uint16_t status,
348  char* mqtt_topic,
349  size_t mqtt_topic_size,
350  size_t* out_mqtt_topic_length);
351 
352 /*
353  *
354  * Twin APIs
355  *
356  */
357 
362 #define AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC "$iothub/twin/res/#"
363 
368 #define AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC "$iothub/twin/PATCH/properties/desired/#"
369 
374 typedef enum
375 {
376  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_GET = 1,
377  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_DESIRED_PROPERTIES = 2,
378  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REPORTED_PROPERTIES = 3,
380 
385 typedef struct
386 {
387  az_span
390  // Avoid using enum as the first field within structs, to allow for { 0 } initialization.
391  // This is a workaround for IAR compiler warning [Pe188]: enumerated type mixed with another type.
392 
401 
417  az_iot_hub_client const* client,
418  az_span received_topic,
419  az_iot_hub_client_twin_response* out_response);
420 
437  az_iot_hub_client const* client,
438  az_span request_id,
439  char* mqtt_topic,
440  size_t mqtt_topic_size,
441  size_t* out_mqtt_topic_length);
442 
460  az_iot_hub_client const* client,
461  az_span request_id,
462  char* mqtt_topic,
463  size_t mqtt_topic_size,
464  size_t* out_mqtt_topic_length);
465 
466 #include <azure/core/_az_cfg_suffix.h>
467 
468 #endif // _az_IOT_HUB_CLIENT_H
az_iot_hub_client_methods_response_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_methods_response_get_publish_topic(az_iot_hub_client const *client, az_span request_id, uint16_t status, char *mqtt_topic, size_t mqtt_topic_size, size_t *out_mqtt_topic_length)
Gets the MQTT topic that must be used to respond to method requests.
az_result.h
Definition of az_result and helper functions.
az_iot_hub_client_c2d_request::properties
az_iot_message_properties properties
Definition: az_iot_hub_client.h:264
az_iot_hub_client_method_request::name
az_span name
Definition: az_iot_hub_client.h:306
az_iot_hub_client_init
AZ_NODISCARD az_result az_iot_hub_client_init(az_iot_hub_client *client, az_span iot_hub_hostname, az_span device_id, az_iot_hub_client_options const *options)
Initializes an Azure IoT Hub Client.
az_iot_hub_client_twin_document_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_twin_document_get_publish_topic(az_iot_hub_client const *client, az_span request_id, char *mqtt_topic, size_t mqtt_topic_size, size_t *out_mqtt_topic_length)
Gets the MQTT topic that must be used to submit a Twin GET request.
az_iot_hub_client_sas_get_signature
AZ_NODISCARD az_result az_iot_hub_client_sas_get_signature(az_iot_hub_client const *client, uint64_t token_expiration_epoch_time, az_span signature, az_span *out_signature)
Gets the Shared Access clear-text signature.
az_iot_hub_client_twin_response::request_id
az_span request_id
Definition: az_iot_hub_client.h:388
az_iot_hub_client_options_default
AZ_NODISCARD az_iot_hub_client_options az_iot_hub_client_options_default()
Gets the default Azure IoT Hub Client options.
az_iot_hub_client_twin_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_twin_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_twin_response *out_response)
Attempts to parse a received message's topic for twin features.
az_iot_hub_client_get_user_name
AZ_NODISCARD az_result az_iot_hub_client_get_user_name(az_iot_hub_client const *client, char *mqtt_user_name, size_t mqtt_user_name_size, size_t *out_mqtt_user_name_length)
Gets the MQTT user name.
az_iot_hub_client_twin_response::status
az_iot_status status
Definition: az_iot_hub_client.h:394
az_iot_hub_client_get_client_id
AZ_NODISCARD az_result az_iot_hub_client_get_client_id(az_iot_hub_client const *client, char *mqtt_client_id, size_t mqtt_client_id_size, size_t *out_mqtt_client_id_length)
Gets the MQTT client id.
az_result
int32_t az_result
The type represents the various success and error conditions.
Definition: az_result.h:45
az_iot_hub_client_telemetry_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_telemetry_get_publish_topic(az_iot_hub_client const *client, az_iot_message_properties const *properties, char *mqtt_topic, size_t mqtt_topic_size, size_t *out_mqtt_topic_length)
Gets the MQTT topic that must be used for device to cloud telemetry messages.
az_iot_hub_client_methods_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_methods_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_method_request *out_request)
Attempts to parse a received message's topic for method features.
az_iot_hub_client_c2d_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_c2d_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_c2d_request *out_request)
Attempts to parse a received message's topic for C2D features.
az_iot_hub_client_sas_get_password
AZ_NODISCARD az_result az_iot_hub_client_sas_get_password(az_iot_hub_client const *client, uint64_t token_expiration_epoch_time, az_span base64_hmac_sha256_signature, az_span key_name, char *mqtt_password, size_t mqtt_password_size, size_t *out_mqtt_password_length)
Gets the MQTT password.
az_iot_hub_client_c2d_request
The Cloud-To-Device Request.
Definition: az_iot_hub_client.h:263
az_iot_message_properties
Telemetry or C2D properties.
Definition: az_iot_common.h:133
az_iot_hub_client_twin_response_type
az_iot_hub_client_twin_response_type
Twin response type.
Definition: az_iot_hub_client.h:375
az_span.h
An az_span represents a contiguous byte buffer and is used for string manipulations,...
az_iot_hub_client_twin_response::response_type
az_iot_hub_client_twin_response_type response_type
Definition: az_iot_hub_client.h:393
az_iot_hub_client_twin_patch_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_twin_patch_get_publish_topic(az_iot_hub_client const *client, az_span request_id, char *mqtt_topic, size_t mqtt_topic_size, size_t *out_mqtt_topic_length)
Gets the MQTT topic that must be used to submit a Twin PATCH request.
az_iot_hub_client_options::user_agent
az_span user_agent
Definition: az_iot_hub_client.h:45
az_iot_hub_client_options::module_id
az_span module_id
Definition: az_iot_hub_client.h:44
az_iot_hub_client_method_request::request_id
az_span request_id
Definition: az_iot_hub_client.h:304
az_iot_hub_client_options
Azure IoT Hub Client options.
Definition: az_iot_hub_client.h:43
az_iot_hub_client_twin_response::version
az_span version
Definition: az_iot_hub_client.h:395
az_iot_hub_client_method_request
A method request received from IoT Hub.
Definition: az_iot_hub_client.h:303
az_iot_hub_client_twin_response
Twin response.
Definition: az_iot_hub_client.h:386
az_iot_status
az_iot_status
Azure IoT service status codes.
Definition: az_iot_common.h:75
az_iot_hub_client
Azure IoT Hub Client.
Definition: az_iot_hub_client.h:55
az_iot_common.h
Azure IoT common definitions.
az_iot_hub_client_options::model_id
az_span model_id
Definition: az_iot_hub_client.h:47
az_span
Represents a "view" over a byte buffer that represents a contiguous region of memory....
Definition: az_span.h:33