SharkSSL™ Embedded SSL/TLS Stack
SharkMQTT.h
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 5100 2022-02-19 16:23:57Z wini $
15 *
16 * COPYRIGHT: Real Time Logic, 2015 - 2022
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{
46 MqttState_Invalid,
47 MqttState_Pub,
48 MqttState_PubRel,
49 MqttState_PubRec,
50 MqttState_SubOrUnsub
51} MqttState;
52
53
56typedef struct
57{
58 DoubleLink super;
59 U32 timeStamp;
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
120 /* end SharkMQTTErrorCodes */
122
123
134#define SMQTT_TIMEOUT -20000
135
139#define SMQTT_SUBACK -20001
140
144#define SMQTT_UNSUBACK -20002
145 /* end SharkMQTTRespCodes */
147
148
151typedef struct
152{
153 const char* topic;
154 const U8* message;
155 U16 msgLen;
156 U8 qos;
160 BaBool retain;
162
163
166typedef struct
167{
169 const char* username;
174 const U8* password;
178
179
183typedef struct
184{
185 SOCKET sock;
186
187 DoubleList recQueue; /* type MqttInFlightMsg: for QOS > 0 */
188 DoubleList sendQueue; /* type MqttInFlightMsg: for QOS > 0 */
189
190 SharkSslCon* scon;
191
197
198 S32 pongTmoCounter;
199 U32 pingTickTime;
200
201 U8* recPackHeader; /* from constructor: buf */
202 U16 recPackHeaderLen; /* from constructor: bufLen */
203
204 U16 packetId; /* send packet ID counter */
205
209
215
219
220 U8* overflowPtr; /* Set if: consumed more data from stream than frame len */
221 int overflowLen; /* overflowPtr len is used internally in wsRawRead */
222 int messageHeaderIx; /* Cursor used when reading recPackHeader from socket */
223
228
229 U16 recPacketId; /* For received packets with QOS 1 or 2 */
230 U8 recQOS; /* For received packets with QOS 1 or 2 */
231} SharkMQTT;
232
233
242void SharkMQTT_constructor(SharkMQTT* o, U8* buf, U16 bufLen);
243
249#define SharkMQTT_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
250
251
256
257
262#define SharkMQTT_msgConsumed(o) (((o)->packetLen - (o)->bytesRead) == 0)
263
264
278#define SharkMQTT_connackCode(o) (o)->recPackHeader[3]
279
280
290#define SharkMQTT_sessionPresent(o) ((o)->recPackHeader[2] & 1)
291
297#define SharkMQTT_trustInfo(o) ((SharkSslConTrust)(o)->packetLen)
298
299
334 const char* address, U16 port,
335 const char* clientId, MqttCredentials* cred,
336 BaBool cleanSession, MqttWillMsg* wm);
337
338
355int SharkMQTT_subscribe(SharkMQTT* o, const char* topic, U8 qos, U16* packetId);
356
357
369int SharkMQTT_unsubscribe(SharkMQTT* o, const char* topic, U16* packetId);
370
371
397 SharkMQTT* o, const char* topic, U8 qos, int* maxMsgLen);
398
399
409#define SharkMQTT_pub0(o, topic, msg, msgLen) \
410 SharkMQTT_publish(o, topic, msg, msgLen, FALSE, 0)
411
412
430int SharkMQTT_publish(SharkMQTT* o, const char* topic,
431 const void* msg, int msgLen, U8 retain, U8 qos);
432
433
439#define SharkMQTT_packetId(o) (o)->recPacketId
440
452#define SharkMQTT_subAckCode(o) ((U16)(o)->recPackHeader[4])
453
454
461#define SharkMQTT_dup(o) (((o)->recPackHeader[0] & 8) ? TRUE : FALSE)
462
468#define SharkMQTT_QOS(o) (((o)->recPackHeader[0] >> 1) & 3)
469
475#define SharkMQTT_retain(o) ((o)->recPackHeader[0] & 1)
476
481#define SharkMQTT_topic(o) ((const char*)((o)->recPackHeader+1))
482
513
514
519 /* end group MQTTLib */
521
522#endif
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:553
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:102
Optional credentials used when calling function SharkMQTT_connect.
Definition: SharkMQTT.h:167
const char * username
Optional User Name (utf-8).
Definition: SharkMQTT.h:169
U16 pwdlen
You must provide a password length if 'password' is non NULL.
Definition: SharkMQTT.h:176
const U8 * password
Optional Password (binary).
Definition: SharkMQTT.h:174
MqttInFlightMsg.
Definition: SharkMQTT.h:57
Optional MQTT Will Message; used when calling function SharkMQTT_connect.
Definition: SharkMQTT.h:152
BaBool retain
Specifies if the Will Message is to be Retained when it is published.
Definition: SharkMQTT.h:160
const U8 * message
Definition: SharkMQTT.h:154
U8 qos
Definition: SharkMQTT.h:156
const char * topic
Definition: SharkMQTT.h:153
U16 msgLen
Definition: SharkMQTT.h:155
SharkMQTT structure.
Definition: SharkMQTT.h:184
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SharkMQTT.h:227
S32 pingTmo
Time interval in seconds for MQTT PING messages sent to the server.
Definition: SharkMQTT.h:196
U32 packetLen
MQTT packet length.
Definition: SharkMQTT.h:208
U32 bytesRead
Read packet data using SharkMQTT_getMessage until: packetLen-bytesRead == 0.
Definition: SharkMQTT.h:214
U32 msgLen
MQTT message length.
Definition: SharkMQTT.h:218