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 {
52 
57 
62 
67 
73 
77 typedef struct
78 {
79  struct
80  {
81  az_span iot_hub_hostname;
82  az_span device_id;
84  } _internal;
86 
95 
115  az_iot_hub_client* client,
116  az_span iot_hub_hostname,
117  az_span device_id,
118  az_iot_hub_client_options const* options);
119 
123 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "/$iothub/websocket"
124 
131 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH_NO_X509_CLIENT_CERT \
132  AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "?iothub-no-client-cert=true"
133 
160  az_iot_hub_client const* client,
161  char* mqtt_user_name,
162  size_t mqtt_user_name_size,
163  size_t* out_mqtt_user_name_length);
164 
191  az_iot_hub_client const* client,
192  char* mqtt_client_id,
193  size_t mqtt_client_id_size,
194  size_t* out_mqtt_client_id_length);
195 
196 /*
197  *
198  * SAS Token APIs
199  *
200  * Use the following APIs when the Shared Access Key is available to the application or stored
201  * within a Hardware Security Module. The APIs are not necessary if X509 Client Certificate
202  * Authentication is used.
203  */
204 
254  az_iot_hub_client const* client,
255  uint64_t token_expiration_epoch_time,
256  az_span signature,
257  az_span* out_signature);
258 
288  az_iot_hub_client const* client,
289  uint64_t token_expiration_epoch_time,
290  az_span base64_hmac_sha256_signature,
291  az_span key_name,
292  char* mqtt_password,
293  size_t mqtt_password_size,
294  size_t* out_mqtt_password_length);
295 
296 /*
297  *
298  * Telemetry APIs
299  *
300  */
301 
320  az_iot_hub_client const* client,
321  az_iot_message_properties const* properties,
322  char* mqtt_topic,
323  size_t mqtt_topic_size,
324  size_t* out_mqtt_topic_length);
325 
326 /*
327  *
328  * Cloud-to-device (C2D) APIs
329  *
330  */
331 
336 #define AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC "devices/+/messages/devicebound/#"
337 
342 typedef struct
343 {
349 
371  az_iot_hub_client const* client,
372  az_span received_topic,
373  az_iot_hub_client_c2d_request* out_request);
374 
375 /*
376  *
377  * Methods APIs
378  *
379  */
380 
385 #define AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC "$iothub/methods/POST/#"
386 
391 typedef struct
392 {
398 
404 
426  az_iot_hub_client const* client,
427  az_span received_topic,
428  az_iot_hub_client_method_request* out_request);
429 
451  az_iot_hub_client const* client,
452  az_span request_id,
453  uint16_t status,
454  char* mqtt_topic,
455  size_t mqtt_topic_size,
456  size_t* out_mqtt_topic_length);
457 
458 /*
459  *
460  * Commands APIs
461  *
462  */
463 
468 #define AZ_IOT_HUB_CLIENT_COMMANDS_SUBSCRIBE_TOPIC AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC
469 
474 typedef struct
475 {
481 
487 
493 
515  az_iot_hub_client const* client,
516  az_span received_topic,
517  az_iot_hub_client_command_request* out_request);
518 
541  az_iot_hub_client const* client,
542  az_span request_id,
543  uint16_t status,
544  char* mqtt_topic,
545  size_t mqtt_topic_size,
546  size_t* out_mqtt_topic_length);
547 
548 /*
549  *
550  * Twin APIs
551  *
552  */
553 
558 #define AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC "$iothub/twin/res/#"
559 
564 #define AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC "$iothub/twin/PATCH/properties/desired/#"
565 
570 typedef enum
571 {
572  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_GET = 1,
573  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_DESIRED_PROPERTIES = 2,
574  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REPORTED_PROPERTIES = 3,
575  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REQUEST_ERROR = 4,
577 
582 typedef struct
583 {
588 
589  // Avoid using enum as the first field within structs, to allow for { 0 } initialization.
590  // This is a workaround for IAR compiler warning [Pe188]: enumerated type mixed with another type.
591 
596 
601 
611 
633  az_iot_hub_client const* client,
634  az_span received_topic,
635  az_iot_hub_client_twin_response* out_response);
636 
657  az_iot_hub_client const* client,
658  az_span request_id,
659  char* mqtt_topic,
660  size_t mqtt_topic_size,
661  size_t* out_mqtt_topic_length);
662 
684  az_iot_hub_client const* client,
685  az_span request_id,
686  char* mqtt_topic,
687  size_t mqtt_topic_size,
688  size_t* out_mqtt_topic_length);
689 
690 /*
691  *
692  * Properties APIs
693  *
694  */
695 
700 #define AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_SUBSCRIBE_TOPIC \
701  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC
702 
707 #define AZ_IOT_HUB_CLIENT_PROPERTIES_WRITABLE_UPDATES_SUBSCRIBE_TOPIC \
708  AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC
709 
714 typedef enum
715 {
717  = 1,
719  = 2,
722  = 3,
724  = 4,
726 
731 typedef struct
732 {
738 
760  az_iot_hub_client const* client,
761  az_span received_topic,
763 
785  az_iot_hub_client const* client,
786  az_span request_id,
787  char* mqtt_topic,
788  size_t mqtt_topic_size,
789  size_t* out_mqtt_topic_length);
790 
814  az_iot_hub_client const* client,
815  az_span request_id,
816  char* mqtt_topic,
817  size_t mqtt_topic_size,
818  size_t* out_mqtt_topic_length);
819 
820 #include <azure/core/_az_cfg_suffix.h>
821 
822 #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:347
az_iot_hub_client_method_request::name
az_span name
Definition: az_iot_hub_client.h:402
az_iot_hub_client_properties_message
Properties message.
Definition: az_iot_hub_client.h:732
az_iot_hub_client_commands_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_commands_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_command_request *out_request)
Attempts to parse a received message's topic for command features.
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_options::component_names
az_span * component_names
Definition: az_iot_hub_client.h:66
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_PROPERTIES_MESSAGE_TYPE_ACKNOWLEDGEMENT
@ AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_ACKNOWLEDGEMENT
Definition: az_iot_hub_client.h:721
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:587
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_PROPERTIES_MESSAGE_TYPE_WRITABLE_UPDATED
@ AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_WRITABLE_UPDATED
Definition: az_iot_hub_client.h:718
az_iot_hub_client_properties_get_reported_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_properties_get_reported_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 is used to send properties from the device to service.
az_iot_hub_client_twin_response::status
az_iot_status status
Definition: az_iot_hub_client.h:600
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:46
AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_ERROR
@ AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_ERROR
Definition: az_iot_hub_client.h:723
az_iot_hub_client_properties_document_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_properties_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 is used to submit a properties GET request.
az_iot_hub_client_properties_message::request_id
az_span request_id
Definition: az_iot_hub_client.h:735
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_command_request::request_id
az_span request_id
Definition: az_iot_hub_client.h:480
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:343
az_iot_message_properties
Telemetry or C2D properties.
Definition: az_iot_common.h:156
az_iot_hub_client_twin_response_type
az_iot_hub_client_twin_response_type
Twin response type.
Definition: az_iot_hub_client.h:571
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:595
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_properties_message::status
az_iot_status status
Definition: az_iot_hub_client.h:734
az_iot_hub_client_command_request::command_name
az_span command_name
Definition: az_iot_hub_client.h:491
az_iot_hub_client_options::user_agent
az_span user_agent
Definition: az_iot_hub_client.h:56
az_iot_hub_client_options::module_id
az_span module_id
Definition: az_iot_hub_client.h:51
az_iot_hub_client_commands_response_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_commands_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 is used to respond to command requests.
az_iot_hub_client_properties_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_properties_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_properties_message *out_message)
Attempts to parse a received message's topic for properties features.
az_iot_hub_client_method_request::request_id
az_span request_id
Definition: az_iot_hub_client.h:397
az_iot_hub_client_command_request::component_name
az_span component_name
Definition: az_iot_hub_client.h:486
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:609
az_iot_hub_client_options::component_names_length
int32_t component_names_length
Definition: az_iot_hub_client.h:71
az_iot_hub_client_properties_message::message_type
az_iot_hub_client_properties_message_type message_type
Definition: az_iot_hub_client.h:733
az_iot_hub_client_command_request
A command request received from IoT Hub.
Definition: az_iot_hub_client.h:475
az_iot_hub_client_method_request
A method request received from IoT Hub.
Definition: az_iot_hub_client.h:392
az_iot_hub_client_twin_response
Twin response.
Definition: az_iot_hub_client.h:583
az_iot_status
az_iot_status
Azure IoT service status codes.
Definition: az_iot_common.h:78
AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE
@ AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE
Definition: az_iot_hub_client.h:716
az_iot_hub_client
Azure IoT Hub Client.
Definition: az_iot_hub_client.h:78
az_iot_common.h
Azure IoT common definitions.
az_iot_hub_client_properties_message_type
az_iot_hub_client_properties_message_type
Properties message type.
Definition: az_iot_hub_client.h:715
az_iot_hub_client_options::model_id
az_span model_id
Definition: az_iot_hub_client.h:61
az_span
Represents a "view" over a byte buffer that represents a contiguous region of memory....
Definition: az_span.h:33