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 6677 2026-10-29 19:41:17Z 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
43
44typedef enum
45{
52
53
56typedef struct
57{
58 DoubleLink super;
60 U16 packetId; /* [MQTT-2.3.1] Packet Identifier */
61 U8 state; /* type MqttState */
63
64
65
79#define SMQTTE_SOCKET -1
80
83#define SMQTTE_ERRORBASE -10000
84
87#define SMQTTE_TIMEOUT -10001
88
91#define SMQTTE_PONGTIMEOUT -10002
92
95#define SMQTTE_PROTOCOL_ERROR -10003
96
100#define SMQTTE_OVERFLOW -10005
101
104#define SMQTTE_ALREADYCON -10006
105
109#define SMQTTE_CONREFUSED -10007
110
118#define SMQTTE_SERVERNOTTRUSTED -10008
119
121#define SMQTTE_NOMEM -10009
122
123 /* end SharkMQTTErrorCodes */
125
126
137#define SMQTT_TIMEOUT -20000
138
142#define SMQTT_SUBACK -20001
143
147#define SMQTT_UNSUBACK -20002
148 /* end SharkMQTTRespCodes */
150
151
154typedef struct
155{
156 const char* topic;
157 const U8* message;
158 U16 msgLen;
159 U8 qos;
163 BaBool retain;
165
166
169typedef struct
170{
172 const char* username;
177 const U8* password;
181
182
186typedef struct
187{
189
190 DoubleList recQueue; /* type MqttInFlightMsg: for QOS > 0 */
191 DoubleList sendQueue; /* type MqttInFlightMsg: for QOS > 0 */
192
194
200
203
204 U8* recPackHeader; /* from constructor: buf */
205 U16 recPackHeaderLen; /* from constructor: bufLen */
206
207 U16 packetId; /* send packet ID counter */
208
212
218
222
223 U8* overflowPtr; /* Set if: consumed more data from stream than frame len */
224 int overflowLen; /* overflowPtr len is used internally in wsRawRead */
225 int messageHeaderIx; /* Cursor used when reading recPackHeader from socket */
226 int packetHeaderLen; /* Fixed header length while collecting packet fields */
227 U8 packetFieldsPending; /* Header fields retained across receive timeouts */
228
233
234 U16 recPacketId; /* For received packets with QOS 1 or 2 */
235 U8 recQOS; /* For received packets with QOS 1 or 2 */
236} SharkMQTT;
237
238
247void SharkMQTT_constructor(SharkMQTT* o, U8* buf, U16 bufLen);
248
254#define SharkMQTT_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
255
256
261
262
267#define SharkMQTT_msgConsumed(o) (((o)->packetLen - (o)->bytesRead) == 0)
268
269
283#define SharkMQTT_connackCode(o) (o)->recPackHeader[3]
284
285
295#define SharkMQTT_sessionPresent(o) ((o)->recPackHeader[2] & 1)
296
302#define SharkMQTT_trustInfo(o) ((SharkSslConTrust)(o)->packetLen)
303
304
339 const char* address, U16 port,
340 const char* clientId, MqttCredentials* cred,
341 BaBool cleanSession, MqttWillMsg* wm);
342
343
361int SharkMQTT_subscribe(SharkMQTT* o, const char* topic, U8 qos, U16* packetId);
362
363
376int SharkMQTT_unsubscribe(SharkMQTT* o, const char* topic, U16* packetId);
377
378
408 SharkMQTT* o, const char* topic, U8 qos, int* maxMsgLen);
409
410
420#define SharkMQTT_pub0(o, topic, msg, msgLen) \
421 SharkMQTT_publish(o, topic, msg, msgLen, FALSE, 0)
422
423
442int SharkMQTT_publish(SharkMQTT* o, const char* topic,
443 const void* msg, int msgLen, U8 retain, U8 qos);
444
445
451#define SharkMQTT_packetId(o) (o)->recPacketId
452
464#define SharkMQTT_subAckCode(o) ((U16)(o)->recPackHeader[4])
465
466
473#define SharkMQTT_dup(o) (((o)->recPackHeader[0] & 8) ? TRUE : FALSE)
474
480#define SharkMQTT_QOS(o) (((o)->recPackHeader[0] >> 1) & 3)
481
487#define SharkMQTT_retain(o) ((o)->recPackHeader[0] & 1)
488
493#define SharkMQTT_topic(o) ((const char*)((o)->recPackHeader+1))
494
525
526
531 /* end group MQTTLib */
533
534#endif
MqttState
Definition: SharkMQTT.h:45
@ MqttState_SubOrUnsub
Definition: SharkMQTT.h:50
@ MqttState_PubRel
Definition: SharkMQTT.h:48
@ MqttState_PubRec
Definition: SharkMQTT.h:49
@ MqttState_Invalid
Definition: SharkMQTT.h:46
@ MqttState_Pub
Definition: SharkMQTT.h:47
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:496
#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:170
const char * username
Optional User Name (utf-8).
Definition: SharkMQTT.h:172
U16 pwdlen
You must provide a password length if 'password' is non NULL.
Definition: SharkMQTT.h:179
const U8 * password
Optional Password (binary).
Definition: SharkMQTT.h:177
MqttInFlightMsg.
Definition: SharkMQTT.h:57
U32 timeStamp
Definition: SharkMQTT.h:59
U16 packetId
Definition: SharkMQTT.h:60
U8 state
Definition: SharkMQTT.h:61
DoubleLink super
Definition: SharkMQTT.h:58
Optional MQTT Will Message; used when calling function SharkMQTT_connect.
Definition: SharkMQTT.h:155
BaBool retain
Specifies if the Will Message is to be Retained when it is published.
Definition: SharkMQTT.h:163
const U8 * message
Definition: SharkMQTT.h:157
U8 qos
Definition: SharkMQTT.h:159
const char * topic
Definition: SharkMQTT.h:156
U16 msgLen
Definition: SharkMQTT.h:158
SharkMQTT structure.
Definition: SharkMQTT.h:187
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SharkMQTT.h:232
U8 * overflowPtr
Definition: SharkMQTT.h:223
int overflowLen
Definition: SharkMQTT.h:224
S32 pingTmo
Time interval in seconds for MQTT PING messages sent to the server.
Definition: SharkMQTT.h:199
DoubleList sendQueue
Definition: SharkMQTT.h:191
int messageHeaderIx
Definition: SharkMQTT.h:225
S32 pongTmoCounter
Definition: SharkMQTT.h:201
int packetHeaderLen
Definition: SharkMQTT.h:226
U8 * recPackHeader
Definition: SharkMQTT.h:204
U32 packetLen
MQTT packet length.
Definition: SharkMQTT.h:211
U32 bytesRead
Read packet data using SharkMQTT_getMessage until: packetLen-bytesRead == 0.
Definition: SharkMQTT.h:217
SharkSslCon * scon
Definition: SharkMQTT.h:193
SOCKET sock
Definition: SharkMQTT.h:188
U32 msgLen
MQTT message length.
Definition: SharkMQTT.h:221
U32 pingTickTime
Definition: SharkMQTT.h:202
U16 recPacketId
Definition: SharkMQTT.h:234
U8 recQOS
Definition: SharkMQTT.h:235
U16 recPackHeaderLen
Definition: SharkMQTT.h:205
U8 packetFieldsPending
Definition: SharkMQTT.h:227
DoubleList recQueue
Definition: SharkMQTT.h:190
U16 packetId
Definition: SharkMQTT.h:207