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 
59 
63 typedef struct
64 {
65  struct
66  {
67  az_span iot_hub_hostname;
68  az_span device_id;
70  } _internal;
72 
81 
100  az_iot_hub_client* client,
101  az_span iot_hub_hostname,
102  az_span device_id,
103  az_iot_hub_client_options const* options);
104 
108 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "/$iothub/websocket"
109 
116 #define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH_NO_X509_CLIENT_CERT \
117  AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "?iothub-no-client-cert=true"
118 
142  az_iot_hub_client const* client,
143  char* mqtt_user_name,
144  size_t mqtt_user_name_size,
145  size_t* out_mqtt_user_name_length);
146 
170  az_iot_hub_client const* client,
171  char* mqtt_client_id,
172  size_t mqtt_client_id_size,
173  size_t* out_mqtt_client_id_length);
174 
175 /*
176  *
177  * SAS Token APIs
178  *
179  * Use the following APIs when the Shared Access Key is available to the application or stored
180  * within a Hardware Security Module. The APIs are not necessary if X509 Client Certificate
181  * Authentication is used.
182  */
183 
229  az_iot_hub_client const* client,
230  uint64_t token_expiration_epoch_time,
231  az_span signature,
232  az_span* out_signature);
233 
258  az_iot_hub_client const* client,
259  uint64_t token_expiration_epoch_time,
260  az_span base64_hmac_sha256_signature,
261  az_span key_name,
262  char* mqtt_password,
263  size_t mqtt_password_size,
264  size_t* out_mqtt_password_length);
265 
266 /*
267  *
268  * Telemetry APIs
269  *
270  */
271 
287  az_iot_hub_client const* client,
288  az_iot_message_properties const* properties,
289  char* mqtt_topic,
290  size_t mqtt_topic_size,
291  size_t* out_mqtt_topic_length);
292 
293 /*
294  *
295  * Cloud-to-device (C2D) APIs
296  *
297  */
298 
303 #define AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC "devices/+/messages/devicebound/#"
304 
309 typedef struct
310 {
316 
332  az_iot_hub_client const* client,
333  az_span received_topic,
334  az_iot_hub_client_c2d_request* out_request);
335 
336 /*
337  *
338  * Methods APIs
339  *
340  */
341 
346 #define AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC "$iothub/methods/POST/#"
347 
352 typedef struct
353 {
359 
365 
381  az_iot_hub_client const* client,
382  az_span received_topic,
383  az_iot_hub_client_method_request* out_request);
384 
401  az_iot_hub_client const* client,
402  az_span request_id,
403  uint16_t status,
404  char* mqtt_topic,
405  size_t mqtt_topic_size,
406  size_t* out_mqtt_topic_length);
407 
408 /*
409  *
410  * Twin APIs
411  *
412  */
413 
418 #define AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC "$iothub/twin/res/#"
419 
424 #define AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC "$iothub/twin/PATCH/properties/desired/#"
425 
430 typedef enum
431 {
432  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_GET = 1,
433  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_DESIRED_PROPERTIES = 2,
434  AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_TYPE_REPORTED_PROPERTIES = 3,
436 
441 typedef struct
442 {
447 
448  // Avoid using enum as the first field within structs, to allow for { 0 } initialization.
449  // This is a workaround for IAR compiler warning [Pe188]: enumerated type mixed with another type.
450 
455 
460 
470 
486  az_iot_hub_client const* client,
487  az_span received_topic,
488  az_iot_hub_client_twin_response* out_response);
489 
505  az_iot_hub_client const* client,
506  az_span request_id,
507  char* mqtt_topic,
508  size_t mqtt_topic_size,
509  size_t* out_mqtt_topic_length);
510 
527  az_iot_hub_client const* client,
528  az_span request_id,
529  char* mqtt_topic,
530  size_t mqtt_topic_size,
531  size_t* out_mqtt_topic_length);
532 
533 #include <azure/core/_az_cfg_suffix.h>
534 
535 #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:314
az_iot_hub_client_method_request::name
az_span name
Definition: az_iot_hub_client.h:363
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:446
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:459
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:310
az_iot_message_properties
Telemetry or C2D properties.
Definition: az_iot_common.h:148
az_iot_hub_client_twin_response_type
az_iot_hub_client_twin_response_type
Twin response type.
Definition: az_iot_hub_client.h:431
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:454
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:52
az_iot_hub_client_options::module_id
az_span module_id
Definition: az_iot_hub_client.h:47
az_iot_hub_client_method_request::request_id
az_span request_id
Definition: az_iot_hub_client.h:358
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:468
az_iot_hub_client_method_request
A method request received from IoT Hub.
Definition: az_iot_hub_client.h:353
az_iot_hub_client_twin_response
Twin response.
Definition: az_iot_hub_client.h:442
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:64
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:57
az_span
Represents a "view" over a byte buffer that represents a contiguous region of memory....
Definition: az_span.h:33