TuyaOS
libemqtt.h
1/*
2 * This file is part of libemqtt.
3 *
4 * libemqtt is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * libemqtt is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with libemqtt. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 *
20 * Created by Filipe Varela on 09/10/16.
21 * Copyright 2009 Caixa Mágica Software. All rights reserved.
22 *
23 * Fork developed by Vicente Ruiz Rodríguez
24 * Copyright 2012 Vicente Ruiz Rodríguez <vruiz2.0@gmail.com>. All rights reserved.
25 *
26 */
27
28#ifndef __LIBEMQTT_H__
29#define __LIBEMQTT_H__
30
31#if 1
32#include <stdint.h>
33#else
34#include "espressif/c_types.h"
35#endif
36
37#define MQTT_CONF_USERNAME_LENGTH 40+1
38#ifndef MQTT_CONF_USERNAME_LENGTH
39#define MQTT_CONF_USERNAME_LENGTH 13 // Recommended by MQTT Specification (12 + '\0')
40#endif
41
42#define MQTT_CONF_PASSWORD_LENGTH 16+1
43#ifndef MQTT_CONF_PASSWORD_LENGTH
44#define MQTT_CONF_PASSWORD_LENGTH 13 // Recommended by MQTT Specification (12 + '\0')
45#endif
46
47
48#define MQTT_MSG_CONNECT 1<<4
49#define MQTT_MSG_CONNACK 2<<4
50#define MQTT_MSG_PUBLISH 3<<4
51#define MQTT_MSG_PUBACK 4<<4
52#define MQTT_MSG_PUBREC 5<<4
53#define MQTT_MSG_PUBREL 6<<4
54#define MQTT_MSG_PUBCOMP 7<<4
55#define MQTT_MSG_SUBSCRIBE 8<<4
56#define MQTT_MSG_SUBACK 9<<4
57#define MQTT_MSG_UNSUBSCRIBE 10<<4
58#define MQTT_MSG_UNSUBACK 11<<4
59#define MQTT_MSG_PINGREQ 12<<4
60#define MQTT_MSG_PINGRESP 13<<4
61#define MQTT_MSG_DISCONNECT 14<<4
62
63
69#define MQTTParseMessageType(buffer) ( *buffer & 0xF0 )
70
77#define MQTTParseMessageDuplicate(buffer) ( *buffer & 0x08 )
78
84#define MQTTParseMessageQos(buffer) ( (*buffer & 0x06) >> 1 )
85
92#define MQTTParseMessageRetain(buffer) ( *buffer & 0x01 )
93
94
105uint8_t mqtt_num_rem_len_bytes(const uint8_t* buf);
106
115uint32_t mqtt_parse_rem_len(const uint8_t* buf);
116
123uint16_t mqtt_parse_msg_id(const uint8_t* buf);
124
135uint16_t mqtt_parse_pub_topic(const uint8_t* buf, uint8_t* topic);
136
141uint16_t mqtt_parse_pub_topic_ptr(const uint8_t* buf, const uint8_t** topic_ptr);
142
153uint32_t mqtt_parse_publish_msg(const uint8_t* buf, uint8_t* msg);
154
159uint32_t mqtt_parse_pub_msg_ptr(const uint8_t* buf, const uint8_t** msg_ptr);
160
161
162typedef struct {
163 void* socket_info;
164 int (*sendBuf)(void* socket_info, const void* buf, unsigned int count);
165 // Connection info
166 char clientid[50];
167 // Auth fields
168 char username[MQTT_CONF_USERNAME_LENGTH];
169 char password[MQTT_CONF_PASSWORD_LENGTH];
170 // Will topic
171 uint8_t will_retain;
172 uint8_t will_qos;
173 uint8_t clean_session;
174 // Management fields
175 uint16_t seq;
176 uint16_t alive;
178
179
186void mqtt_init(mqtt_broker_handle_t* broker, const char* clientid);
187
195void mqtt_init_auth(mqtt_broker_handle_t* broker, const char* username, const char* password);
196
203void mqtt_set_alive(mqtt_broker_handle_t* broker, uint16_t alive);
204
212int mqtt_connect(mqtt_broker_handle_t* broker);
213
223int mqtt_disconnect(mqtt_broker_handle_t* broker);
224
225#if 0
236int mqtt_publish(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain);
237#endif
238
251#if 0
252int mqtt_publish_with_qos(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain, uint8_t qos, uint16_t* message_id);
253#endif
254
255void mqtt_get_qos_msg_id(mqtt_broker_handle_t* broker, uint8_t qos, uint16_t* message_id);
256
257int mqtt_pub_msg_with_qos(mqtt_broker_handle_t* broker, const char* topic, const unsigned char* msg, const uint32_t m_len, uint8_t retain, uint8_t qos, uint16_t message_id);
258
267int mqtt_puback(mqtt_broker_handle_t* broker, uint16_t message_id);
268
277int mqtt_pubrel(mqtt_broker_handle_t* broker, uint16_t message_id);
278
288int mqtt_subscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
289
299int mqtt_subscribe_ext(mqtt_broker_handle_t* broker, const char* topics[], uint8_t cnt, uint16_t* message_id);
300
310int mqtt_unsubscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
311
321int mqtt_unsubscribe_ext(mqtt_broker_handle_t* broker, const char* topics[], uint8_t cnt, uint16_t* message_id);
322
323
331int mqtt_ping(mqtt_broker_handle_t* broker);
332
333
334#endif // __LIBEMQTT_H__
#define mqtt_unsubscribe(client, topic, cb, arg)
Definition: mqtt.h:196
#define mqtt_subscribe(client, topic, qos, cb, arg)
Definition: mqtt.h:193
Definition: libemqtt.h:162