SharkSSL™ Embedded SSL/TLS Stack
SMQ.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 ****************************************************************************
10 * HEADER
11 *
12 * This file is part of SharkMQ:
13 * https://realtimelogic.com/products/sharkssl/SharkMQ/
14 *
15 * $Id: SMQ.h 5692 2025-10-30 20:22:59Z wini $
16 *
17 * COPYRIGHT: Real Time Logic LLC, 2014 - 2022
18 *
19 * This software is copyrighted by and is the sole property of Real
20 * Time Logic LLC. All rights, title, ownership, or other interests in
21 * the software remain the property of Real Time Logic LLC. This
22 * software may only be used in accordance with the terms and
23 * conditions stipulated in the corresponding license agreement under
24 * which the software has been supplied. Any unauthorized use,
25 * duplication, transmission, distribution, or disclosure of this
26 * software is expressly forbidden.
27 *
28 * This Copyright notice may not be removed or modified without prior
29 * written consent of Real Time Logic LLC.
30 *
31 * Real Time Logic LLC. reserves the right to modify this software
32 * without notice.
33 *
34 * http://sharkssl.com
35 ****************************************************************************
36 *
37 */
38
39#ifndef __SharkMQ_h
40#define __SharkMQ_h
41
42#define SMQ_SEC
43
44#include "selib.h"
45
46#ifdef ENABLE_PROXY
47#define ENABLE_HTTPS_PROXY
48#define ENABLE_SOCKS_PROXY
49#endif
50
51#if defined(ENABLE_HTTPS_PROXY) || defined(ENABLE_SOCKS_PROXY)
52#ifndef ENABLE_PROXY
53#define ENABLE_PROXY
54#endif
55#include "proxy.h"
56#endif
57
58
71#define SMQE_BUF_OVERFLOW -10000
72
75#define SMQE_ONACK_DENIED -10000
76
79#define SMQE_INVALID_URL -10002
80
83#define SMQE_PROTOCOL_ERROR -10003
84
87#define SMQE_DISCONNECT -10004
88
89
92#define SMQE_PONGTIMEOUT -10005
93 /* end SharkMQErrorCodes */
95
96
113#define SMQ_SUBACK -20000
114
125#define SMQ_CREATEACK -20001
126
134#define SMQ_CREATESUBACK -20002
135
142#define SMQ_SUBCHANGE -20003
143
146#define SMQ_TIMEOUT -20004
147 /* end SharkMQRespCodes */
149
150
151#define SMQSTR(str) str, (sizeof(str)-1)
152
155typedef struct SharkMQ
156{
157 SOCKET sock;
158 SharkSslCon* scon;
159
160 U8* sharkBuf; /* SharkSSL read buffer is set when we have data from Shark */
161 U8* buf; /* buffer provided by user via constructor */
162
163 U16 sharkBufLen; /* TLS frame len */
164 U16 sharkBufIx; /* Current read index in sharkBuf */
165
166 U16 bufLen; /* User buffer total len */
167 U16 bufIx; /* Current read index in user buffer */
168
169
174 S32 pingTmoCounter,pingTmo;
176 U32 tid;
177 U32 ptid;
178 U32 subtid;
179 int status;
180 U16 sendBufIx;
184#ifdef ENABLE_PROXY
185 Proxy* proxy;
186#endif
187
188#ifdef __cplusplus
189
193 SharkMQ(U8* buf, U16 bufLen);
194
195
199 int init(SharkSslCon* scon, const char* url, U32* rnd);
200
204 int connect(const char* uid, int uidLen, const char* credentials,
205 U8 credLen, const char* info, int infoLen,
206 U16 maxTlsFrameSize);
207
208
212 void disconnect();
213
214
218 ~SharkMQ();
219
220
224 int create(const char* topic);
225
226
230 int createsub(const char* subtopic);
231
232
237 int subscribe(const char* topic);
238
239
243 int unsubscribe(U32 tid);
244
245
251 int publish(const void* data, int len, U32 tid, U32 subtid);
252
253
258 int wrtstr(const char* str);
259
264 int write( const void* data, int len);
265
270 int pubflush(U32 tid, U32 subtid);
271
272
281 int observe(U32 tid);
282
283
287 int unobserve(U32 tid);
288
289
293 int getMessage(U8** msg);
294
295
299 int getMsgSize();
300
301#endif
302
304
305#ifdef __cplusplus
306extern "C" {
307#endif
308
323void SharkMQ_constructor(SharkMQ* o, U8* buf, U16 bufLen);
324
325#define SharkMQ_setProxy(o, proxyMA) (o)->proxy=proxyMA
326
332#define SharkMQ_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
333
334
341#define SharkMQ_getSendBufPtr(o) (SharkSslCon_getEncBufPtr((o)->scon) + 15)
342
349#define SharkMQ_getSendBufSize(o) (SharkSslCon_getEncBufSize((o)->scon) - 15)
350
351
375int SharkMQ_init(SharkMQ* o, SharkSslCon* scon, const char* url, U32* rnd);
376
410int SharkMQ_connect(SharkMQ* o, const char* uid, int uidLen,
411 const char* credentials, U8 credLen,
412 const char* info, int infoLen,
413 U16 maxTlsFrameSize);
414
415
421
422
427
428
440int SharkMQ_create(SharkMQ* o, const char* topic);
441
442
451int SharkMQ_createsub(SharkMQ* o, const char* subtopic);
452
453
462int SharkMQ_subscribe(SharkMQ* o, const char* topic);
463
464
470
471
482int SharkMQ_publish(SharkMQ* o, const void* data, int len, U32 tid, U32 subtid);
483
484
496int SharkMQ_wrtstr(SharkMQ* o, const char* str);
497
511int SharkMQ_write(SharkMQ* o, const void* data, int len);
512
531int SharkMQ_pubflush(SharkMQ* o, U32 tid, U32 subtid);
532
533
552int SharkMQ_observe(SharkMQ* o, U32 tid);
553
554
559int SharkMQ_unobserve(SharkMQ* o, U32 tid);
560
561
592int SharkMQ_getMessage(SharkMQ* o, U8** msg);
593
594
598#define SharkMQ_getMsgSize(o) ((o)->frameLen-15)
599 /* end group SMQLib_C */
601
602#ifdef __cplusplus
603}
604
605inline SharkMQ::SharkMQ(U8* buf, U16 bufLen) {
606 SharkMQ_constructor(this,buf, bufLen);
607}
608
609inline int SharkMQ::init(SharkSslCon* scon, const char* url, U32* rnd) {
610 return SharkMQ_init(this, scon, url, rnd);
611}
612
613inline int SharkMQ::connect(const char* uid, int uidLen,
614 const char* credentials,
615 U8 credLen, const char* info, int infoLen,
616 U16 maxTlsFrameSize) {
617 return SharkMQ_connect(
618 this,uid,uidLen,credentials,credLen,info,infoLen,maxTlsFrameSize);
619}
620
621inline void SharkMQ::disconnect() {
622 return SharkMQ_disconnect(this);
623}
624
626 SharkMQ_destructor(this);
627}
628
629inline int SharkMQ::create(const char* topic) {
630 return SharkMQ_create(this, topic);
631}
632
633inline int SharkMQ::createsub(const char* subtopic) {
634 return SharkMQ_createsub(this, subtopic);
635}
636
637inline int SharkMQ::subscribe(const char* topic) {
638 return SharkMQ_subscribe(this, topic);
639}
640
641inline int SharkMQ::unsubscribe(U32 tid) {
642 return SharkMQ_unsubscribe(this, tid);
643}
644
645inline int SharkMQ::publish(const void* data, int len, U32 tid, U32 subtid) {
646 return SharkMQ_publish(this, data, len, tid, subtid);
647}
648
649inline int SharkMQ::wrtstr(const char* str) {
650 return SharkMQ_wrtstr(this, str);
651}
652
653inline int SharkMQ::write( const void* data, int len) {
654 return SharkMQ_write(this, data, len);
655}
656
657inline int SharkMQ::pubflush(U32 tid, U32 subtid) {
658 return SharkMQ_pubflush(this, tid, subtid);
659}
660
661inline int SharkMQ::observe(U32 tid) {
662 return SharkMQ_observe(this, tid);
663}
664
665inline int SharkMQ::unobserve(U32 tid) {
666 return SharkMQ_unobserve(this, tid);
667}
668
669inline int SharkMQ::getMessage(U8** msg) {
670 return SharkMQ_getMessage(this, msg);
671}
672
674 return SharkMQ_getMsgSize(this);
675}
676
677#endif
678
679
680 /* end group SMQLib */
682
683#endif
int SharkMQ_create(SharkMQ *o, const char *topic)
Create a topic an fetch the topic ID (tid).
int SharkMQ_write(SharkMQ *o, const void *data, int len)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
int SharkMQ_subscribe(SharkMQ *o, const char *topic)
Subscribe to topic.
int SharkMQ_getMessage(SharkMQ *o, U8 **msg)
Wait for messages sent from the broker.
int SharkMQ_publish(SharkMQ *o, const void *data, int len, U32 tid, U32 subtid)
Publish messages to a topic and optionally to a sub-topic.
int SharkMQ_createsub(SharkMQ *o, const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
int SharkMQ_connect(SharkMQ *o, const char *uid, int uidLen, const char *credentials, U8 credLen, const char *info, int infoLen, U16 maxTlsFrameSize)
Connect/establish a persistent SMQ connection.
#define SharkMQ_getMsgSize(o)
Returns the message size, which is SharkMQ::frameLen - 15.
Definition: SMQ.h:598
void SharkMQ_constructor(SharkMQ *o, U8 *buf, U16 bufLen)
Create a secure SMQ client instance.
int SharkMQ_unsubscribe(SharkMQ *o, U32 tid)
Requests the broker to unsubscribe the client from a topic.
void SharkMQ_disconnect(SharkMQ *o)
Gracefully close the connection.
int SharkMQ_unobserve(SharkMQ *o, U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
int SharkMQ_wrtstr(SharkMQ *o, const char *str)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
int SharkMQ_pubflush(SharkMQ *o, U32 tid, U32 subtid)
Flush the internal buffer and request the broker to assemble all stored fragments as one message.
int SharkMQ_observe(SharkMQ *o, U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
int SharkMQ_init(SharkMQ *o, SharkSslCon *scon, const char *url, U32 *rnd)
Initiate the SharkMQ server connection.
void SharkMQ_destructor(SharkMQ *o)
Terminate an SMQ instance.
int init(SharkSslCon *scon, const char *url, U32 *rnd)
Initiate the SMQ server connection.
Definition: SMQ.h:609
int getMsgSize()
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:673
int createsub(const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
Definition: SMQ.h:633
int unsubscribe(U32 tid)
Requests the broker to unsubscribe the server from a topic.
Definition: SMQ.h:641
int publish(const void *data, int len, U32 tid, U32 subtid)
Publish messages to a topic and optionally to a sub-topic.
Definition: SMQ.h:645
struct SharkMQ SharkMQ
SharkMQ structure.
int wrtstr(const char *str)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
Definition: SMQ.h:649
SharkMQ(U8 *buf, U16 bufLen)
Create a secure SMQ client instance.
Definition: SMQ.h:605
int pubflush(U32 tid, U32 subtid)
Flush the internal buffer and request the broker to assemble all stored fragments as one message.
Definition: SMQ.h:657
int write(const void *data, int len)
Publish a message in chunks and request the broker to assemble the message before publishing to the s...
Definition: SMQ.h:653
int getMessage(U8 **msg)
Wait for messages sent from the broker.
Definition: SMQ.h:669
int subscribe(const char *topic)
The response to SharkMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SharkMQ_getMe...
Definition: SMQ.h:637
int create(const char *topic)
Create a topic an fetch the topic ID (tid).
Definition: SMQ.h:629
~SharkMQ()
Terminate an SMQ instance.
Definition: SMQ.h:625
int connect(const char *uid, int uidLen, const char *credentials, U8 credLen, const char *info, int infoLen, U16 maxTlsFrameSize)
Connect/establish a persistent SMQ connection.
Definition: SMQ.h:613
int observe(U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
Definition: SMQ.h:661
void disconnect()
Gracefully close the connection.
Definition: SMQ.h:621
int unobserve(U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
Definition: SMQ.h:665
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:559
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:102
SharkMQ structure.
Definition: SMQ.h:156
U32 clientTid
Client's unique topic ID.
Definition: SMQ.h:175
U32 tid
Topic: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:176
U16 bytesRead
Read frame data using SharkMQ_getMessage until: frameLen-bytesRead = 0.
Definition: SMQ.h:183
U16 frameLen
The SMQ frame size for the incomming data.
Definition: SMQ.h:181
U32 ptid
Publisher's tid: Set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:177
int status
Last known error code.
Definition: SMQ.h:179
U32 subtid
Sub-tid: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:178
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SMQ.h:173