master
h 109 lines 3.77 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "common_public.h"
4
5 #define MQTT_NG_MSGGEN_OK 0
6 // MQTT_NG_MSGGEN_USER_ERROR means parameters given to this function
7 // do not make sense or are out of MQTT specs
8 #define MQTT_NG_MSGGEN_USER_ERROR 1
9 #define MQTT_NG_MSGGEN_BUFFER_OOM 2
10 #define MQTT_NG_MSGGEN_MSG_TOO_BIG 3
11
12 struct mqtt_ng_client;
13 extern time_t ping_timeout;
14 /* Converts integer to MQTT Variable Byte Integer as per 1.5.5 of MQTT 5 specs
15 * @param input value to be converted
16 * @param output pointer to memory where output will be written to. Must allow up to 4 bytes to be written.
17 * @return number of bytes written to output or <= 0 if error in which case contents of output are undefined
18 */
19 int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output);
20
21 struct mqtt_lwt_properties {
22 char *will_topic;
23 free_fnc_t will_topic_free;
24
25 void *will_message;
26 free_fnc_t will_message_free;
27 size_t will_message_size;
28
29 int will_qos;
30 int will_retain;
31 };
32
33 struct mqtt_auth_properties {
34 char *client_id;
35 free_fnc_t client_id_free;
36 char *username;
37 free_fnc_t username_free;
38 char *password;
39 free_fnc_t password_free;
40 };
41
42 int mqtt_ng_connect(struct mqtt_ng_client *client,
43 struct mqtt_auth_properties *auth,
44 struct mqtt_lwt_properties *lwt,
45 uint16_t keep_alive);
46
47 /* Publish an MQTT message.
48 *
49 * Ownership and cleanup on return:
50 * - MQTT_NG_MSGGEN_OK: msg is attached to the transaction buffer and freed
51 * via msg_free after the packet is ack'd; *packet_id is set to the queued
52 * packet id. The caller must not free msg.
53 * - Any non-OK return (MQTT_NG_MSGGEN_MSG_TOO_BIG, MQTT_NG_MSGGEN_BUFFER_OOM,
54 * ...): msg is NOT consumed -- ownership stays with the caller, who must
55 * invoke msg_free. *packet_id must be treated as undefined: the generator
56 * may have written a transient value before rolling back, so callers that
57 * care should reset it to a sentinel (e.g. 0) on the failure path.
58 *
59 * topic_free: ownership of topic is handled internally by the transaction
60 * buffer on the success path. On failure the topic may or may not have been
61 * attached depending on where generation failed; current callers must pass
62 * topic_free=NULL until the rollback path is made symmetric.
63 */
64 int mqtt_ng_publish(struct mqtt_ng_client *client,
65 char *topic,
66 free_fnc_t topic_free,
67 void *msg,
68 free_fnc_t msg_free,
69 size_t msg_len,
70 uint8_t publish_flags,
71 uint16_t *packet_id);
72
73 struct mqtt_sub {
74 char *topic;
75 free_fnc_t topic_free;
76 uint8_t options;
77 };
78
79 int mqtt_ng_subscribe(struct mqtt_ng_client *client, struct mqtt_sub *subscriptions, size_t subscription_count);
80
81 int mqtt_ng_ping(struct mqtt_ng_client *client);
82
83 typedef ssize_t (*mqtt_ng_send_fnc_t)(void *user_ctx, const void* buf, size_t len);
84
85 struct mqtt_ng_init {
86 rbuf_t data_in;
87 mqtt_ng_send_fnc_t data_out_fnc;
88 void *user_ctx;
89
90 void (*puback_callback)(uint16_t packet_id);
91 void (*connack_callback)(void* user_ctx, int connack_reply);
92 void (*msg_callback)(const char *topic, const void *msg, size_t msglen, int qos);
93 };
94
95 struct mqtt_ng_client *mqtt_ng_init(struct mqtt_ng_init *settings);
96
97 void mqtt_ng_destroy(struct mqtt_ng_client *client);
98
99 int mqtt_ng_disconnect(struct mqtt_ng_client *client, uint8_t reason_code);
100
101 int mqtt_ng_sync(struct mqtt_ng_client *client);
102
103 time_t mqtt_ng_last_send_time(struct mqtt_ng_client *client);
104
105 void mqtt_ng_set_max_mem(struct mqtt_ng_client *client, size_t bytes);
106
107 void mqtt_ng_get_stats(struct mqtt_ng_client *client, struct mqtt_ng_stats *stats);
108
109 int mqtt_ng_set_topic_alias(struct mqtt_ng_client *client, const char *topic);