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 5029 2022-01-16 21:32:09Z 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
278 int observe(U32 tid);
279
280
284 int unobserve(U32 tid);
285
286
290 int getMessage(U8** msg);
291
292
296 int getMsgSize();
297
298#endif
299
301
302#ifdef __cplusplus
303extern "C" {
304#endif
305
320void SharkMQ_constructor(SharkMQ* o, U8* buf, U16 bufLen);
321
322#define SharkMQ_setProxy(o, proxyMA) (o)->proxy=proxyMA
323
329#define SharkMQ_setCtx(o, ctx) SOCKET_constructor(&(o)->sock, ctx)
330
331
338#define SharkMQ_getSendBufPtr(o) (SharkSslCon_getEncBufPtr((o)->scon) + 15)
339
346#define SharkMQ_getSendBufSize(o) (SharkSslCon_getEncBufSize((o)->scon) - 15)
347
348
372int SharkMQ_init(SharkMQ* o, SharkSslCon* scon, const char* url, U32* rnd);
373
407int SharkMQ_connect(SharkMQ* o, const char* uid, int uidLen,
408 const char* credentials, U8 credLen,
409 const char* info, int infoLen,
410 U16 maxTlsFrameSize);
411
412
418
419
424
425
437int SharkMQ_create(SharkMQ* o, const char* topic);
438
439
448int SharkMQ_createsub(SharkMQ* o, const char* subtopic);
449
450
459int SharkMQ_subscribe(SharkMQ* o, const char* topic);
460
461
467
468
479int SharkMQ_publish(SharkMQ* o, const void* data, int len, U32 tid, U32 subtid);
480
481
493int SharkMQ_wrtstr(SharkMQ* o, const char* str);
494
508int SharkMQ_write(SharkMQ* o, const void* data, int len);
509
528int SharkMQ_pubflush(SharkMQ* o, U32 tid, U32 subtid);
529
530
545int SharkMQ_observe(SharkMQ* o, U32 tid);
546
547
552int SharkMQ_unobserve(SharkMQ* o, U32 tid);
553
554
585int SharkMQ_getMessage(SharkMQ* o, U8** msg);
586
587
591#define SharkMQ_getMsgSize(o) ((o)->frameLen-15)
592 /* end group SMQLib_C */
594
595#ifdef __cplusplus
596}
597
598inline SharkMQ::SharkMQ(U8* buf, U16 bufLen) {
599 SharkMQ_constructor(this,buf, bufLen);
600}
601
602inline int SharkMQ::init(SharkSslCon* scon, const char* url, U32* rnd) {
603 return SharkMQ_init(this, scon, url, rnd);
604}
605
606inline int SharkMQ::connect(const char* uid, int uidLen,
607 const char* credentials,
608 U8 credLen, const char* info, int infoLen,
609 U16 maxTlsFrameSize) {
610 return SharkMQ_connect(
611 this,uid,uidLen,credentials,credLen,info,infoLen,maxTlsFrameSize);
612}
613
614inline void SharkMQ::disconnect() {
615 return SharkMQ_disconnect(this);
616}
617
619 SharkMQ_destructor(this);
620}
621
622inline int SharkMQ::create(const char* topic) {
623 return SharkMQ_create(this, topic);
624}
625
626inline int SharkMQ::createsub(const char* subtopic) {
627 return SharkMQ_createsub(this, subtopic);
628}
629
630inline int SharkMQ::subscribe(const char* topic) {
631 return SharkMQ_subscribe(this, topic);
632}
633
634inline int SharkMQ::unsubscribe(U32 tid) {
635 return SharkMQ_unsubscribe(this, tid);
636}
637
638inline int SharkMQ::publish(const void* data, int len, U32 tid, U32 subtid) {
639 return SharkMQ_publish(this, data, len, tid, subtid);
640}
641
642inline int SharkMQ::wrtstr(const char* str) {
643 return SharkMQ_wrtstr(this, str);
644}
645
646inline int SharkMQ::write( const void* data, int len) {
647 return SharkMQ_write(this, data, len);
648}
649
650inline int SharkMQ::pubflush(U32 tid, U32 subtid) {
651 return SharkMQ_pubflush(this, tid, subtid);
652}
653
654inline int SharkMQ::observe(U32 tid) {
655 return SharkMQ_observe(this, tid);
656}
657
658inline int SharkMQ::unobserve(U32 tid) {
659 return SharkMQ_unobserve(this, tid);
660}
661
662inline int SharkMQ::getMessage(U8** msg) {
663 return SharkMQ_getMessage(this, msg);
664}
665
667 return SharkMQ_getMsgSize(this);
668}
669
670#endif
671
672
673 /* end group SMQLib */
675
676#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:591
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:602
int getMsgSize()
Returns the message size, which is SMQ::frameLen - 15.
Definition: SMQ.h:666
int createsub(const char *subtopic)
Create a sub-topic and fetch the subtopic ID.
Definition: SMQ.h:626
int unsubscribe(U32 tid)
Requests the broker to unsubscribe the server from a topic.
Definition: SMQ.h:634
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:638
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:642
SharkMQ(U8 *buf, U16 bufLen)
Create a secure SMQ client instance.
Definition: SMQ.h:598
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:650
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:646
int getMessage(U8 **msg)
Wait for messages sent from the broker.
Definition: SMQ.h:662
int subscribe(const char *topic)
The response to SharkMQ_subscribe is asynchronous and returned as status SMQ_SUBACK via SharkMQ_getMe...
Definition: SMQ.h:630
int create(const char *topic)
Create a topic an fetch the topic ID (tid).
Definition: SMQ.h:622
~SharkMQ()
Terminate an SMQ instance.
Definition: SMQ.h:618
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:606
int observe(U32 tid)
Request the broker to provide change notification events when the number of subscribers to a specific...
Definition: SMQ.h:654
void disconnect()
Gracefully close the connection.
Definition: SMQ.h:614
int unobserve(U32 tid)
Stop receiving change notifications for a topic ID or ephemeral topic ID.
Definition: SMQ.h:658
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
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