SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
SMQ.h
Go to the documentation of this file.
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 6825 2026-11-10 21:17:44Z wini $
16 *
17 * COPYRIGHT: Real Time Logic LLC, 2014 - 2026
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
72#define SMQE_BUF_OVERFLOW -10000
73
76#define SMQE_ONACK_DENIED -10000
77
80#define SMQE_INVALID_URL -10002
81
84#define SMQE_PROTOCOL_ERROR -10003
85
88#define SMQE_DISCONNECT -10004
89
90
93#define SMQE_PONGTIMEOUT -10005
94
97#define SMQE_TLS_AUTH_FAILED -10006
98 /* end SharkMQErrorCodes */
100
101
118#define SMQ_SUBACK -20000
119
130#define SMQ_CREATEACK -20001
131
139#define SMQ_CREATESUBACK -20002
140
147#define SMQ_SUBCHANGE -20003
148
152#define SMQ_TIMEOUT -20004
153 /* end SharkMQRespCodes */
155
156
157#define SMQSTR(str) str, (sizeof(str)-1)
158
161typedef struct SharkMQ
162{
165
166 U8* sharkBuf; /* SharkSSL read buffer is set when we have data from Shark */
167 U8* buf; /* buffer provided by user via constructor */
168
169 U16 sharkBufLen; /* TLS frame len */
170 U16 sharkBufIx; /* Current read index in sharkBuf */
171
172 U16 bufLen; /* User buffer total len */
173 U16 bufIx; /* Current read index in user buffer */
174
175
182 U32 tid;
183 U32 ptid;
184 U32 subtid;
185 int status;
190#ifdef ENABLE_PROXY
192#endif
193
194#ifdef __cplusplus
195
199 SharkMQ(U8* buf, U16 bufLen);
200
201
205 int init(SharkSslCon* scon, const char* url, U32* rnd);
206
210 int connect(const char* uid, int uidLen, const char* credentials,
211 U8 credLen, const char* info, int infoLen,
212 U16 maxTlsFrameSize);
213
214
218 void disconnect();
219
220
224 ~SharkMQ();
225
226
230 int create(const char* topic);
231
232
236 int createsub(const char* subtopic);
237
238
243 int subscribe(const char* topic);
244
245
249 int unsubscribe(U32 tid);
250
251
257 int publish(const void* data, int len, U32 tid, U32 subtid);
258
259
264 int wrtstr(const char* str);
265
270 int write( const void* data, int len);
271
276 int pubflush(U32 tid, U32 subtid);
277
278
285 int observe(U32 tid);
286
287
291 int unobserve(U32 tid);
292
293
297 int getMessage(U8** msg);
298
299
303 int getMsgSize();
304
305#endif
306
308
309#ifdef __cplusplus
310extern "C" {
311#endif
312
327void SharkMQ_constructor(SharkMQ* o, U8* buf, U16 bufLen);
328
329#define SharkMQ_setProxy(o, proxyMA) (o)->proxy=proxyMA
330
336#define SharkMQ_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
337
338
345#define SharkMQ_getSendBufPtr(o) (SharkSslCon_getEncBufPtr((o)->scon) + 15)
346
353#define SharkMQ_getSendBufSize(o) (SharkSslCon_getEncBufSize((o)->scon) - 15)
354
355
381int SharkMQ_init(SharkMQ* o, SharkSslCon* scon, const char* url, U32* rnd);
382
423int SharkMQ_connect(SharkMQ* o, const char* uid, int uidLen,
424 const char* credentials, U8 credLen,
425 const char* info, int infoLen,
426 U16 maxTlsFrameSize);
427
428
434
435
440
441
453int SharkMQ_create(SharkMQ* o, const char* topic);
454
455
464int SharkMQ_createsub(SharkMQ* o, const char* subtopic);
465
466
475int SharkMQ_subscribe(SharkMQ* o, const char* topic);
476
477
483
484
494int SharkMQ_publish(SharkMQ* o, const void* data, int len, U32 tid, U32 subtid);
495
496
511int SharkMQ_wrtstr(SharkMQ* o, const char* str);
512
526int SharkMQ_write(SharkMQ* o, const void* data, int len);
527
545int SharkMQ_pubflush(SharkMQ* o, U32 tid, U32 subtid);
546
547
565int SharkMQ_observe(SharkMQ* o, U32 tid);
566
567
572int SharkMQ_unobserve(SharkMQ* o, U32 tid);
573
574
607int SharkMQ_getMessage(SharkMQ* o, U8** msg);
608
609
613#define SharkMQ_getMsgSize(o) ((o)->frameLen-15)
614 /* end group SMQLib_C */
616
617#ifdef __cplusplus
618}
619
620inline SharkMQ::SharkMQ(U8* buf, U16 bufLen) {
622}
623
624inline int SharkMQ::init(SharkSslCon* scon, const char* url, U32* rnd) {
625 return SharkMQ_init(this, scon, url, rnd);
626}
627
628inline int SharkMQ::connect(const char* uid, int uidLen,
629 const char* credentials,
630 U8 credLen, const char* info, int infoLen,
631 U16 maxTlsFrameSize) {
632 return SharkMQ_connect(
633 this,uid,uidLen,credentials,credLen,info,infoLen,maxTlsFrameSize);
634}
635
636inline void SharkMQ::disconnect() {
637 return SharkMQ_disconnect(this);
638}
639
641 SharkMQ_destructor(this);
642}
643
644inline int SharkMQ::create(const char* topic) {
645 return SharkMQ_create(this, topic);
646}
647
648inline int SharkMQ::createsub(const char* subtopic) {
649 return SharkMQ_createsub(this, subtopic);
650}
651
652inline int SharkMQ::subscribe(const char* topic) {
653 return SharkMQ_subscribe(this, topic);
654}
655
656inline int SharkMQ::unsubscribe(U32 tid) {
657 return SharkMQ_unsubscribe(this, tid);
658}
659
660inline int SharkMQ::publish(const void* data, int len, U32 tid, U32 subtid) {
661 return SharkMQ_publish(this, data, len, tid, subtid);
662}
663
664inline int SharkMQ::wrtstr(const char* str) {
665 return SharkMQ_wrtstr(this, str);
666}
667
668inline int SharkMQ::write( const void* data, int len) {
669 return SharkMQ_write(this, data, len);
670}
671
672inline int SharkMQ::pubflush(U32 tid, U32 subtid) {
673 return SharkMQ_pubflush(this, tid, subtid);
674}
675
676inline int SharkMQ::observe(U32 tid) {
677 return SharkMQ_observe(this, tid);
678}
679
680inline int SharkMQ::unobserve(U32 tid) {
681 return SharkMQ_unobserve(this, tid);
682}
683
684inline int SharkMQ::getMessage(U8** msg) {
685 return SharkMQ_getMessage(this, msg);
686}
687
689 return SharkMQ_getMsgSize(this);
690}
691
692#endif
693
694
695 /* end group SMQLib */
697
698#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:613
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:624
int getMsgSize()
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:688
int createsub(const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
Definition: SMQ.h:648
int unsubscribe(U32 tid)
Requests the broker to unsubscribe the server from a topic.
Definition: SMQ.h:656
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:660
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:664
SharkMQ(U8 *buf, U16 bufLen)
Create a secure SMQ client instance.
Definition: SMQ.h:620
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:672
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:668
int getMessage(U8 **msg)
Wait for messages sent from the broker.
Definition: SMQ.h:684
int subscribe(const char *topic)
The response to SharkMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SharkMQ_getMe...
Definition: SMQ.h:652
int create(const char *topic)
Create a topic an fetch the topic ID (tid).
Definition: SMQ.h:644
~SharkMQ()
Terminate an SMQ instance.
Definition: SMQ.h:640
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:628
int observe(U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
Definition: SMQ.h:676
void disconnect()
Gracefully close the connection.
Definition: SMQ.h:636
int unobserve(U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
Definition: SMQ.h:680
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 proxy configuration for the SharkMQ example library.
Caller-owned proxy settings with borrowed string pointers.
Definition: proxy.h:103
SharkMQ structure.
Definition: SMQ.h:162
SharkSslCon * scon
Definition: SMQ.h:164
U8 * sharkBuf
Definition: SMQ.h:166
S32 pingTmo
Definition: SMQ.h:180
U16 bufIx
Definition: SMQ.h:173
SOCKET sock
Definition: SMQ.h:163
U16 sharkBufLen
Definition: SMQ.h:169
S32 pingTmoCounter
Definition: SMQ.h:180
U16 bufLen
Definition: SMQ.h:172
U8 * buf
Definition: SMQ.h:167
U32 clientTid
Client's unique topic ID.
Definition: SMQ.h:181
U16 sendBufIx
Definition: SMQ.h:186
U32 tid
Topic: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:182
U16 bytesRead
Read frame data using SharkMQ_getMessage until: frameLen-bytesRead = 0.
Definition: SMQ.h:189
U16 frameLen
The SMQ frame size for the incomming data.
Definition: SMQ.h:187
U32 ptid
Publisher's tid: Set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:183
int status
Last known error code.
Definition: SMQ.h:185
U32 subtid
Sub-tid: set when receiving MSG_PUBLISH from broker.
Definition: SMQ.h:184
U32 timeout
Timeout in milliseconds to wait in functions waiting for server data.
Definition: SMQ.h:179
U16 sharkBufIx
Definition: SMQ.h:170
Proxy * proxy
Optional borrowed settings; assign after construction and before init.
Definition: SMQ.h:191