blob: 6a55f3f940129b157a686ef27db8e29aabeed64e [file] [log] [blame]
yuezonghec78e2ef2025-02-13 17:57:46 -08001/*******************************************************************************
2 * Copyright (c) 2014, 2023 IBM Corp., Ian Craggs and others
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * Contributors:
14 * Ian Craggs - initial API and implementation and/or initial documentation
15 * Xiang Rong - 442039 Add makefile to Embedded C client
16 *******************************************************************************/
17
18#ifndef MQTTPACKET_H_
19#define MQTTPACKET_H_
20
21#include <stdint.h>
22
23#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
24extern "C" {
25#endif
26
27#if defined(WIN32_DLL) || defined(WIN64_DLL)
28 #define DLLImport __declspec(dllimport)
29 #define DLLExport __declspec(dllexport)
30#elif defined(LINUX_SO)
31 #define DLLImport extern
32 #define DLLExport __attribute__ ((visibility ("default")))
33#else
34 #define DLLImport
35 #define DLLExport
36#endif
37
38enum errors
39{
40 MQTTPACKET_BUFFER_TOO_SHORT = -2,
41 MQTTPACKET_READ_ERROR = -1,
42 MQTTPACKET_READ_COMPLETE
43};
44
45enum msgTypes
46{
47 CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
48 PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
49 PINGREQ, PINGRESP, DISCONNECT
50#if defined(MQTTV5)
51 , AUTH
52#endif
53};
54
55/**
56 * Bitfields for the MQTT header byte.
57 */
58typedef union
59{
60 unsigned char byte; /**< the whole byte */
61#if defined(REVERSED)
62 struct
63 {
64 unsigned int type : 4; /**< message type nibble */
65 unsigned int dup : 1; /**< DUP flag bit */
66 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
67 unsigned int retain : 1; /**< retained flag bit */
68 } bits;
69#else
70 struct
71 {
72 unsigned int retain : 1; /**< retained flag bit */
73 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
74 unsigned int dup : 1; /**< DUP flag bit */
75 unsigned int type : 4; /**< message type nibble */
76 } bits;
77#endif
78} MQTTHeader;
79
80typedef struct
81{
82 int32_t len;
83 char* data;
84} MQTTLenString;
85
86typedef struct
87{
88 char* cstring;
89 MQTTLenString lenstring;
90} MQTTString;
91
92#define MQTTString_initializer {NULL, {0, NULL}}
93
94int MQTTstrlen(MQTTString mqttstring);
95
96#include "MQTTConnect.h"
97#include "MQTTPublish.h"
98#include "MQTTSubscribe.h"
99#include "MQTTUnsubscribe.h"
100#include "MQTTFormat.h"
101
102DLLExport int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char type, unsigned char dup, unsigned short packetid);
103DLLExport int32_t MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int32_t buflen);
104
105int32_t MQTTPacket_VBIlen(int32_t rem_len);
106int32_t MQTTPacket_len(int32_t rem_len);
107DLLExport int MQTTPacket_equals(MQTTString* a, char* b);
108
109DLLExport int32_t MQTTPacket_encode(unsigned char* buf, int32_t length);
110int32_t MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int32_t* value);
111int32_t MQTTPacket_decodeBuf(unsigned char* buf, int32_t* value);
112
113int readInt(unsigned char** pptr);
114char readChar(unsigned char** pptr);
115void writeChar(unsigned char** pptr, char c);
116void writeInt(unsigned char** pptr, int anInt);
117int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
118void writeCString(unsigned char** pptr, const char* string);
119void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
120
121DLLExport int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int));
122
123typedef struct {
124 int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
125 void *sck; /* pointer to whatever the system may use to identify the transport */
126 int multiplier;
127 int rem_len;
128 int32_t len;
129 char state;
130}MQTTTransport;
131
132int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp);
133
134#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
135}
136#endif
137
138
139#endif /* MQTTPACKET_H_ */