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 {
48 
53 
58 
63 
69 
73 typedef struct
74 {
75  struct
76  {
77  az_span iot_hub_hostname;
78  az_span device_id;
80  } _internal;
82 
91 
113  az_iot_hub_client* client,
114  az_span iot_hub_hostname,
115  az_span device_id,
116  az_iot_hub_client_options const* options);
117 
121 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "/$iothub/websocket"
122 
129 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH_NO_X509_CLIENT_CERT \
130  AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "?iothub-no-client-cert=true"
131 
158  az_iot_hub_client const* client,
159  char* mqtt_user_name,
160  size_t mqtt_user_name_size,
161  size_t* out_mqtt_user_name_length);
162 
189  az_iot_hub_client const* client,
190  char* mqtt_client_id,
191  size_t mqtt_client_id_size,
192  size_t* out_mqtt_client_id_length);
193 
194 /*
195  *
196  * SAS Token APIs
197  *
198  * Use the following APIs when the Shared Access Key is available to the application or stored
199  * within a Hardware Security Module. The APIs are not necessary if X509 Client Certificate
200  * Authentication is used.
201  */
202 
252  az_iot_hub_client const* client,
253  uint64_t token_expiration_epoch_time,
254  az_span signature,
255  az_span* out_signature);
256 
286  az_iot_hub_client const* client,
287  uint64_t token_expiration_epoch_time,
288  az_span base64_hmac_sha256_signature,
289  az_span key_name,
290  char* mqtt_password,
291  size_t mqtt_password_size,
292  size_t* out_mqtt_password_length);
293 
294 /*
295  *
296  * Telemetry APIs
297  *
298  */
299 
318  az_iot_hub_client const* client,
319  az_iot_message_properties const* properties,
320  char* mqtt_topic,
321  size_t mqtt_topic_size,
322  size_t* out_mqtt_topic_length);
323 
324 /*
325  *
326  * Cloud-to-device (C2D) APIs
327  *
328  */
329 
334 #define AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC "devices/+/messages/devicebound/#"
335 
340 typedef struct
341 {
347 
369  az_iot_hub_client const* client,
370  az_span received_topic,
371  az_iot_hub_client_c2d_request* out_request);
372 
373 /*
374  *
375  * Methods APIs
376  *
377  */
378 
383 #define AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC "$iothub/methods/POST/#"
384 
389 typedef struct
390 {
396 
402 
424  az_iot_hub_client const* client,
425  az_span received_topic,
426  az_iot_hub_client_method_request* out_request);
427 
449  az_iot_hub_client const* client,
450  az_span request_id,
451  uint16_t status,
452  char* mqtt_topic,
453  size_t mqtt_topic_size,
454  size_t* out_mqtt_topic_length);
455 
456 /*
457  *
458  * Commands APIs
459  *
460  */
461 
466 #define AZ_IOT_HUB_CLIENT_COMMANDS_SUBSCRIBE_TOPIC AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC
467 
472 typedef struct
473 {
479 
485 
491 
513  az_iot_hub_client const* client,
514  az_span received_topic,
515  az_iot_hub_client_command_request* out_request);
516 
539  az_iot_hub_client const* client,
540  az_span request_id,
541  uint16_t status,
542  char* mqtt_topic,
543  size_t mqtt_topic_size,
544  size_t* out_mqtt_topic_length);
545 
546 /*
547  *
548  * Twin APIs
549  *
550  */
551 
556 #define AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC "$iothub/twin/res/#"
557 
562 #define AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC "$iothub/twin/PATCH/properties/desired/#"
563 
568 typedef enum
569 {
570  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_GET = 1,
571  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_DESIRED_PROPERTIES = 2,
572  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REPORTED_PROPERTIES = 3,
573  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REQUEST_ERROR = 4,
575 
580 typedef struct
581 {
586 
587  // Avoid using enum as the first field within structs, to allow for { 0 } initialization.
588  // This is a workaround for IAR compiler warning [Pe188]: enumerated type mixed with another type.
589 
594 
599 
609 
631  az_iot_hub_client const* client,
632  az_span received_topic,
633  az_iot_hub_client_twin_response* out_response);
634 
655  az_iot_hub_client const* client,
656  az_span request_id,
657  char* mqtt_topic,
658  size_t mqtt_topic_size,
659  size_t* out_mqtt_topic_length);
660 
682  az_iot_hub_client const* client,
683  az_span request_id,
684  char* mqtt_topic,
685  size_t mqtt_topic_size,
686  size_t* out_mqtt_topic_length);
687 
688 /*
689  *
690  * Properties APIs
691  *
692  */
693 
698 #define AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_SUBSCRIBE_TOPIC \
699  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC
700 
705 #define AZ_IOT_HUB_CLIENT_PROPERTIES_WRITABLE_UPDATES_SUBSCRIBE_TOPIC \
706  AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC
707 
712 typedef enum
713 {
715  = 1,
717  = 2,
720  = 3,
722  = 4,
724 
729 typedef struct
730 {
736 
758  az_iot_hub_client const* client,
759  az_span received_topic,
761 
783  az_iot_hub_client const* client,
784  az_span request_id,
785  char* mqtt_topic,
786  size_t mqtt_topic_size,
787  size_t* out_mqtt_topic_length);
788 
812  az_iot_hub_client const* client,
813  az_span request_id,
814  char* mqtt_topic,
815  size_t mqtt_topic_size,
816  size_t* out_mqtt_topic_length);
817 
818 #include <azure/core/_az_cfg_suffix.h>
819 
820 #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:345
az_iot_hub_client_method_request::name
az_span name
Definition: az_iot_hub_client.h:400
az_iot_hub_client_properties_message
Properties message.
Definition: az_iot_hub_client.h:730
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:62
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:719
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:585
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:716
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:598
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:721
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:733
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:478
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:341
az_iot_message_properties
Telemetry or C2D properties.
Definition: az_iot_common.h:153
az_iot_hub_client_twin_response_type
az_iot_hub_client_twin_response_type
Twin response type.
Definition: az_iot_hub_client.h:569
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:593
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:732
az_iot_hub_client_command_request::command_name
az_span command_name
Definition: az_iot_hub_client.h:489
az_iot_hub_client_options::user_agent
az_span user_agent
Definition: az_iot_hub_client.h:52
az_iot_hub_client_options::module_id
az_span module_id
Definition: az_iot_hub_client.h:47
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:395
az_iot_hub_client_command_request::component_name
az_span component_name
Definition: az_iot_hub_client.h:484
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:607
az_iot_hub_client_options::component_names_length
int32_t component_names_length
Definition: az_iot_hub_client.h:67
az_iot_hub_client_properties_message::message_type
az_iot_hub_client_properties_message_type message_type
Definition: az_iot_hub_client.h:731
az_iot_hub_client_command_request
A command request received from IoT Hub.
Definition: az_iot_hub_client.h:473
az_iot_hub_client_method_request
A method request received from IoT Hub.
Definition: az_iot_hub_client.h:390
az_iot_hub_client_twin_response
Twin response.
Definition: az_iot_hub_client.h:581
az_iot_status
az_iot_status
Azure IoT service status codes.
Definition: az_iot_common.h:75
AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE
@ AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE
Definition: az_iot_hub_client.h:714
az_iot_hub_client
Azure IoT Hub Client.
Definition: az_iot_hub_client.h:74
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:713
az_iot_hub_client_options::model_id
az_span model_id
Definition: az_iot_hub_client.h:57
az_span
Represents a "view" over a byte buffer that represents a contiguous region of memory....
Definition: az_span.h:33