SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
SharkMQTT.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 ****************************************************************************
9 * HEADER
10 *
11 * This file is part of SharkMQTT:
12 * https://realtimelogic.com/products/sharkmqtt/
13 *
14 * $Id: SharkMQTT.h 6825 2026-11-10 21:17:44Z wini $
15 *
16 * COPYRIGHT: Real Time Logic, 2015 - 2026
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://www.realtimelogic.com
34 */
35
36#ifndef _SharkMQTT_h
37#define _SharkMQTT_h
38
39#include <selib.h>
40#include <DoubleList.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46
47typedef enum
48{
55
56
59typedef struct
60{
61 DoubleLink super;
63 U16 packetId; /* [MQTT-2.3.1] Packet Identifier */
64 U8 state; /* type MqttState */
66
67
68
82#define SMQTTE_SOCKET -1
83
86#define SMQTTE_ERRORBASE -10000
87
90#define SMQTTE_TIMEOUT -10001
91
94#define SMQTTE_PONGTIMEOUT -10002
95
98#define SMQTTE_PROTOCOL_ERROR -10003
99
103#define SMQTTE_OVERFLOW -10005
104
107#define SMQTTE_ALREADYCON -10006
108
112#define SMQTTE_CONREFUSED -10007
113
121#define SMQTTE_SERVERNOTTRUSTED -10008
122
124#define SMQTTE_NOMEM -10009
125
126 /* end SharkMQTTErrorCodes */
128
129
140#define SMQTT_TIMEOUT -20000
141
145#define SMQTT_SUBACK -20001
146
150#define SMQTT_UNSUBACK -20002
151 /* end SharkMQTTRespCodes */
153
154
157typedef struct
158{
159 const char* topic;
160 const U8* message;
161 U16 msgLen;
162 U8 qos;
166 BaBool retain;
168
169
172typedef struct
173{
175 const char* username;
180 const U8* password;
184
185
189typedef struct
190{
192
193 DoubleList recQueue; /* type MqttInFlightMsg: for QOS > 0 */
194 DoubleList sendQueue; /* type MqttInFlightMsg: for QOS > 0 */
195
197
203
206
207 U8* recPackHeader; /* from constructor: buf */
208 U16 recPackHeaderLen; /* from constructor: bufLen */
209
210 U16 packetId; /* send packet ID counter */
211
215
221
225
226 U8* overflowPtr; /* Set if: consumed more data from stream than frame len */
227 int overflowLen; /* overflowPtr len is used internally in wsRawRead */
228 int messageHeaderIx; /* Cursor used when reading recPackHeader from socket */
229 int packetHeaderLen; /* Fixed header length while collecting packet fields */
230 U8 packetFieldsPending; /* Header fields retained across receive timeouts */
231
236
237 U16 recPacketId; /* For received packets with QOS 1 or 2 */
238 U8 recQOS; /* For received packets with QOS 1 or 2 */
239} SharkMQTT;
240
241
250void SharkMQTT_constructor(SharkMQTT* o, U8* buf, U16 bufLen);
251
257#define SharkMQTT_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
258
259
264
265
270#define SharkMQTT_msgConsumed(o) (((o)->packetLen - (o)->bytesRead) == 0)
271
272
286#define SharkMQTT_connackCode(o) (o)->recPackHeader[3]
287
288
298#define SharkMQTT_sessionPresent(o) ((o)->recPackHeader[2] & 1)
299
305#define SharkMQTT_trustInfo(o) ((SharkSslConTrust)(o)->packetLen)
306
307
342 const char* address, U16 port,
343 const char* clientId, MqttCredentials* cred,
344 BaBool cleanSession, MqttWillMsg* wm);
345
346
364int SharkMQTT_subscribe(SharkMQTT* o, const char* topic, U8 qos, U16* packetId);
365
366
379int SharkMQTT_unsubscribe(SharkMQTT* o, const char* topic, U16* packetId);
380
381
411 SharkMQTT* o, const char* topic, U8 qos, int* maxMsgLen);
412
413
423#define SharkMQTT_pub0(o, topic, msg, msgLen) \
424 SharkMQTT_publish(o, topic, msg, msgLen, FALSE, 0)
425
426
445int SharkMQTT_publish(SharkMQTT* o, const char* topic,
446 const void* msg, int msgLen, U8 retain, U8 qos);
447
448
454#define SharkMQTT_packetId(o) (o)->recPacketId
455
467#define SharkMQTT_subAckCode(o) ((U16)(o)->recPackHeader[4])
468
469
476#define SharkMQTT_dup(o) (((o)->recPackHeader[0] & 8) ? TRUE : FALSE)
477
483#define SharkMQTT_QOS(o) (((o)->recPackHeader[0] >> 1) & 3)
484
490#define SharkMQTT_retain(o) ((o)->recPackHeader[0] & 1)
491
496#define SharkMQTT_topic(o) ((const char*)((o)->recPackHeader+1))
497
528
529
534 /* end group MQTTLib */
536
537#ifdef __cplusplus
538}
539#endif
540
541#endif
MqttState
Definition: SharkMQTT.h:48
@ MqttState_SubOrUnsub
Definition: SharkMQTT.h:53
@ MqttState_PubRel
Definition: SharkMQTT.h:51
@ MqttState_PubRec
Definition: SharkMQTT.h:52
@ MqttState_Invalid
Definition: SharkMQTT.h:49
@ MqttState_Pub
Definition: SharkMQTT.h:50
U8 * SharkMQTT_zeroCopyPub(SharkMQTT *o, const char *topic, U8 qos, int *maxMsgLen)
Prepare a zero copy publish by fetching the SharkSSL send buffer pointer and offset the pointer to th...
void SharkMQTT_destructor(SharkMQTT *o)
Terminate a SharkMQTT instance.
void SharkMQTT_constructor(SharkMQTT *o, U8 *buf, U16 bufLen)
Create a SharkMQTT client instance.
int SharkMQTT_unsubscribe(SharkMQTT *o, const char *topic, U16 *packetId)
Requests the broker to unsubscribe the client from a topic.
int SharkMQTT_subscribe(SharkMQTT *o, const char *topic, U8 qos, U16 *packetId)
Subscribe to topic.
void SharkMQTT_disconnect(SharkMQTT *o)
Send a disconnect command to the broker and gracefully close the connection.
int SharkMQTT_getMessage(SharkMQTT *o, U8 **msg)
Wait for messages sent from the broker.
int SharkMQTT_connect(SharkMQTT *o, SharkSslCon *scon, const char *address, U16 port, const char *clientId, MqttCredentials *cred, BaBool cleanSession, MqttWillMsg *wm)
Establish (or re-establish) an MQTT session.
int SharkMQTT_publish(SharkMQTT *o, const char *topic, const void *msg, int msgLen, U8 retain, U8 qos)
Publish messages to a topic.
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:495
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:103
Optional credentials used when calling function SharkMQTT_connect.
Definition: SharkMQTT.h:173
const char * username
Optional User Name (utf-8).
Definition: SharkMQTT.h:175
U16 pwdlen
You must provide a password length if 'password' is non NULL.
Definition: SharkMQTT.h:182
const U8 * password
Optional Password (binary).
Definition: SharkMQTT.h:180
MqttInFlightMsg.
Definition: SharkMQTT.h:60
U32 timeStamp
Definition: SharkMQTT.h:62
U16 packetId
Definition: SharkMQTT.h:63
U8 state
Definition: SharkMQTT.h:64
DoubleLink super
Definition: SharkMQTT.h:61
Optional MQTT Will Message; used when calling function SharkMQTT_connect.
Definition: SharkMQTT.h:158
BaBool retain
Specifies if the Will Message is to be Retained when it is published.
Definition: SharkMQTT.h:166
const U8 * message
Definition: SharkMQTT.h:160
U8 qos
Definition: SharkMQTT.h:162
const char * topic
Definition: SharkMQTT.h:159
U16 msgLen
Definition: SharkMQTT.h:161
SharkMQTT structure.
Definition: SharkMQTT.h:190
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SharkMQTT.h:235
U8 * overflowPtr
Definition: SharkMQTT.h:226
int overflowLen
Definition: SharkMQTT.h:227
S32 pingTmo
Time interval in seconds for MQTT PING messages sent to the server.
Definition: SharkMQTT.h:202
DoubleList sendQueue
Definition: SharkMQTT.h:194
int messageHeaderIx
Definition: SharkMQTT.h:228
S32 pongTmoCounter
Definition: SharkMQTT.h:204
int packetHeaderLen
Definition: SharkMQTT.h:229
U8 * recPackHeader
Definition: SharkMQTT.h:207
U32 packetLen
MQTT packet length.
Definition: SharkMQTT.h:214
U32 bytesRead
Read packet data using SharkMQTT_getMessage until: packetLen-bytesRead == 0.
Definition: SharkMQTT.h:220
SharkSslCon * scon
Definition: SharkMQTT.h:196
SOCKET sock
Definition: SharkMQTT.h:191
U32 msgLen
MQTT message length.
Definition: SharkMQTT.h:224
U32 pingTickTime
Definition: SharkMQTT.h:205
U16 recPacketId
Definition: SharkMQTT.h:237
U8 recQOS
Definition: SharkMQTT.h:238
U16 recPackHeaderLen
Definition: SharkMQTT.h:208
U8 packetFieldsPending
Definition: SharkMQTT.h:230
DoubleList recQueue
Definition: SharkMQTT.h:193
U16 packetId
Definition: SharkMQTT.h:210