Skip to content

Commit db463cb

Browse files
committed
feat(mqtt): Add paho mqtt to third_party
internal: 0d398ac6
1 parent 436a668 commit db463cb

26 files changed

+3686
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,4 @@ INCLUDES += -I $(SDK_PATH)/include/spiffs
425425
INCLUDES += -I $(SDK_PATH)/include/ssl
426426
INCLUDES += -I $(SDK_PATH)/include/json
427427
INCLUDES += -I $(SDK_PATH)/include/openssl
428+
INCLUDES += -I $(SDK_PATH)/include/mqtt

include/mqtt/MQTTClient.h

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2017 IBM Corp.
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+
* Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
15+
* Ian Craggs - documentation and platform specific header
16+
* Ian Craggs - add setMessageHandler function
17+
*******************************************************************************/
18+
19+
#if !defined(__MQTT_CLIENT_C_)
20+
#define __MQTT_CLIENT_C_
21+
22+
#if defined(__cplusplus)
23+
extern "C" {
24+
#endif
25+
26+
#if defined(WIN32_DLL) || defined(WIN64_DLL)
27+
#define DLLImport __declspec(dllimport)
28+
#define DLLExport __declspec(dllexport)
29+
#elif defined(LINUX_SO)
30+
#define DLLImport extern
31+
#define DLLExport __attribute__ ((visibility ("default")))
32+
#else
33+
#define DLLImport
34+
#define DLLExport
35+
#endif
36+
37+
#include "MQTTPacket.h"
38+
#include "stdio.h"
39+
#include "MQTTFreeRTOS.h"
40+
41+
#if defined(MQTTCLIENT_PLATFORM_HEADER)
42+
/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value
43+
* into a string constant suitable for use with include.
44+
*/
45+
#define xstr(s) str(s)
46+
#define str(s) #s
47+
#include xstr(MQTTCLIENT_PLATFORM_HEADER)
48+
#endif
49+
50+
#define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */
51+
52+
#if !defined(MAX_MESSAGE_HANDLERS)
53+
#define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */
54+
#endif
55+
56+
enum QoS { QOS0, QOS1, QOS2, SUBFAIL=0x80 };
57+
58+
/* all failure return codes must be negative */
59+
enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
60+
61+
/* The Platform specific header must define the Network and Timer structures and functions
62+
* which operate on them.
63+
*
64+
typedef struct Network
65+
{
66+
int (*mqttread)(Network*, unsigned char* read_buffer, int, int);
67+
int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int);
68+
} Network;*/
69+
70+
/* The Timer structure must be defined in the platform specific header,
71+
* and have the following functions to operate on it. */
72+
extern void TimerInit(Timer*);
73+
extern char TimerIsExpired(Timer*);
74+
extern void TimerCountdownMS(Timer*, unsigned int);
75+
extern void TimerCountdown(Timer*, unsigned int);
76+
extern int TimerLeftMS(Timer*);
77+
78+
typedef struct MQTTMessage
79+
{
80+
enum QoS qos;
81+
unsigned char retained;
82+
unsigned char dup;
83+
unsigned short id;
84+
void *payload;
85+
size_t payloadlen;
86+
} MQTTMessage;
87+
88+
typedef struct MessageData
89+
{
90+
MQTTMessage* message;
91+
MQTTString* topicName;
92+
} MessageData;
93+
94+
typedef struct MQTTConnackData
95+
{
96+
unsigned char rc;
97+
unsigned char sessionPresent;
98+
} MQTTConnackData;
99+
100+
typedef struct MQTTSubackData
101+
{
102+
enum QoS grantedQoS;
103+
} MQTTSubackData;
104+
105+
typedef void (*messageHandler)(MessageData*);
106+
107+
typedef struct MQTTClient
108+
{
109+
unsigned int next_packetid,
110+
command_timeout_ms;
111+
size_t buf_size,
112+
readbuf_size;
113+
unsigned char *buf,
114+
*readbuf;
115+
unsigned int keepAliveInterval;
116+
char ping_outstanding;
117+
int isconnected;
118+
int cleansession;
119+
120+
struct MessageHandlers
121+
{
122+
const char* topicFilter;
123+
void (*fp) (MessageData*);
124+
} messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */
125+
126+
void (*defaultMessageHandler) (MessageData*);
127+
128+
Network* ipstack;
129+
Timer last_sent, last_received;
130+
#if defined(MQTT_TASK)
131+
Mutex mutex;
132+
Thread thread;
133+
#endif
134+
} MQTTClient;
135+
136+
#define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
137+
138+
139+
/**
140+
* Create an MQTT client object
141+
* @param client
142+
* @param network
143+
* @param command_timeout_ms
144+
* @param
145+
*/
146+
DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms,
147+
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size);
148+
149+
/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
150+
* The nework object must be connected to the network endpoint before calling this
151+
* @param options - connect options
152+
* @return success code
153+
*/
154+
DLLExport int MQTTConnectWithResults(MQTTClient* client, MQTTPacket_connectData* options,
155+
MQTTConnackData* data);
156+
157+
/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
158+
* The nework object must be connected to the network endpoint before calling this
159+
* @param options - connect options
160+
* @return success code
161+
*/
162+
DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options);
163+
164+
/** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
165+
* @param client - the client object to use
166+
* @param topic - the topic to publish to
167+
* @param message - the message to send
168+
* @return success code
169+
*/
170+
DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*);
171+
172+
/** MQTT SetMessageHandler - set or remove a per topic message handler
173+
* @param client - the client object to use
174+
* @param topicFilter - the topic filter set the message handler for
175+
* @param messageHandler - pointer to the message handler function or NULL to remove
176+
* @return success code
177+
*/
178+
DLLExport int MQTTSetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler);
179+
180+
/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
181+
* @param client - the client object to use
182+
* @param topicFilter - the topic filter to subscribe to
183+
* @param message - the message to send
184+
* @return success code
185+
*/
186+
DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler);
187+
188+
/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
189+
* @param client - the client object to use
190+
* @param topicFilter - the topic filter to subscribe to
191+
* @param message - the message to send
192+
* @param data - suback granted QoS returned
193+
* @return success code
194+
*/
195+
DLLExport int MQTTSubscribeWithResults(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler, MQTTSubackData* data);
196+
197+
/** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning.
198+
* @param client - the client object to use
199+
* @param topicFilter - the topic filter to unsubscribe from
200+
* @return success code
201+
*/
202+
DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter);
203+
204+
/** MQTT Disconnect - send an MQTT disconnect packet and close the connection
205+
* @param client - the client object to use
206+
* @return success code
207+
*/
208+
DLLExport int MQTTDisconnect(MQTTClient* client);
209+
210+
/** MQTT Yield - MQTT background
211+
* @param client - the client object to use
212+
* @param time - the time, in milliseconds, to yield for
213+
* @return success code
214+
*/
215+
DLLExport int MQTTYield(MQTTClient* client, int time);
216+
217+
/** MQTT isConnected
218+
* @param client - the client object to use
219+
* @return truth value indicating whether the client is connected to the server
220+
*/
221+
static inline DLLExport int MQTTIsConnected(MQTTClient* client)
222+
{
223+
return client->isconnected;
224+
}
225+
226+
#if defined(MQTT_TASK)
227+
/** MQTT start background thread for a client. After this, MQTTYield should not be called.
228+
* @param client - the client object to use
229+
* @return success code
230+
*/
231+
DLLExport int MQTTStartTask(MQTTClient* client);
232+
#endif
233+
234+
#if defined(__cplusplus)
235+
}
236+
#endif
237+
238+
#endif

include/mqtt/MQTTConnect.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2017 IBM Corp.
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+
* Ian Craggs - add connack return code definitions
16+
* Xiang Rong - 442039 Add makefile to Embedded C client
17+
* Ian Craggs - fix for issue #64, bit order in connack response
18+
*******************************************************************************/
19+
20+
#ifndef MQTTCONNECT_H_
21+
#define MQTTCONNECT_H_
22+
23+
enum connack_return_codes
24+
{
25+
MQTT_CONNECTION_ACCEPTED = 0,
26+
MQTT_UNNACCEPTABLE_PROTOCOL = 1,
27+
MQTT_CLIENTID_REJECTED = 2,
28+
MQTT_SERVER_UNAVAILABLE = 3,
29+
MQTT_BAD_USERNAME_OR_PASSWORD = 4,
30+
MQTT_NOT_AUTHORIZED = 5,
31+
};
32+
33+
#if !defined(DLLImport)
34+
#define DLLImport
35+
#endif
36+
#if !defined(DLLExport)
37+
#define DLLExport
38+
#endif
39+
40+
41+
typedef union
42+
{
43+
unsigned char all; /**< all connect flags */
44+
#if defined(REVERSED)
45+
struct
46+
{
47+
unsigned int username : 1; /**< 3.1 user name */
48+
unsigned int password : 1; /**< 3.1 password */
49+
unsigned int willRetain : 1; /**< will retain setting */
50+
unsigned int willQoS : 2; /**< will QoS value */
51+
unsigned int will : 1; /**< will flag */
52+
unsigned int cleansession : 1; /**< clean session flag */
53+
unsigned int : 1; /**< unused */
54+
} bits;
55+
#else
56+
struct
57+
{
58+
unsigned int : 1; /**< unused */
59+
unsigned int cleansession : 1; /**< cleansession flag */
60+
unsigned int will : 1; /**< will flag */
61+
unsigned int willQoS : 2; /**< will QoS value */
62+
unsigned int willRetain : 1; /**< will retain setting */
63+
unsigned int password : 1; /**< 3.1 password */
64+
unsigned int username : 1; /**< 3.1 user name */
65+
} bits;
66+
#endif
67+
} MQTTConnectFlags; /**< connect flags byte */
68+
69+
70+
71+
/**
72+
* Defines the MQTT "Last Will and Testament" (LWT) settings for
73+
* the connect packet.
74+
*/
75+
typedef struct
76+
{
77+
/** The eyecatcher for this structure. must be MQTW. */
78+
char struct_id[4];
79+
/** The version number of this structure. Must be 0 */
80+
int struct_version;
81+
/** The LWT topic to which the LWT message will be published. */
82+
MQTTString topicName;
83+
/** The LWT payload. */
84+
MQTTString message;
85+
/**
86+
* The retained flag for the LWT message (see MQTTAsync_message.retained).
87+
*/
88+
unsigned char retained;
89+
/**
90+
* The quality of service setting for the LWT message (see
91+
* MQTTAsync_message.qos and @ref qos).
92+
*/
93+
char qos;
94+
} MQTTPacket_willOptions;
95+
96+
97+
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
98+
99+
100+
typedef struct
101+
{
102+
/** The eyecatcher for this structure. must be MQTC. */
103+
char struct_id[4];
104+
/** The version number of this structure. Must be 0 */
105+
int struct_version;
106+
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
107+
*/
108+
unsigned char MQTTVersion;
109+
MQTTString clientID;
110+
unsigned short keepAliveInterval;
111+
unsigned char cleansession;
112+
unsigned char willFlag;
113+
MQTTPacket_willOptions will;
114+
MQTTString username;
115+
MQTTString password;
116+
} MQTTPacket_connectData;
117+
118+
typedef union
119+
{
120+
unsigned char all; /**< all connack flags */
121+
#if defined(REVERSED)
122+
struct
123+
{
124+
unsigned int reserved : 7; /**< unused */
125+
unsigned int sessionpresent : 1; /**< session present flag */
126+
} bits;
127+
#else
128+
struct
129+
{
130+
unsigned int sessionpresent : 1; /**< session present flag */
131+
unsigned int reserved: 7; /**< unused */
132+
} bits;
133+
#endif
134+
} MQTTConnackFlags; /**< connack flags byte */
135+
136+
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
137+
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
138+
139+
DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
140+
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
141+
142+
DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
143+
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
144+
145+
DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
146+
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
147+
148+
#endif /* MQTTCONNECT_H_ */

0 commit comments

Comments
 (0)