-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtts.h
140 lines (119 loc) · 3.64 KB
/
mqtts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef MQTTS_H
#define MQTTS_H
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
#define MQTTS_MAX_PACKET_LENGTH (256)
#define MQTTS_TYPE_ADVERTISE (0x00)
#define MQTTS_TYPE_SEARCHGW (0x01)
#define MQTTS_TYPE_GWINFO (0x02)
#define MQTTS_TYPE_CONNECT (0x04)
#define MQTTS_TYPE_CONNACK (0x05)
#define MQTTS_TYPE_WILLTOPICREQ (0x06)
#define MQTTS_TYPE_WILLTOPIC (0x07)
#define MQTTS_TYPE_WILLMSGREQ (0x08)
#define MQTTS_TYPE_WILLMSG (0x09)
#define MQTTS_TYPE_REGISTER (0x0A)
#define MQTTS_TYPE_REGACK (0x0B)
#define MQTTS_TYPE_PUBLISH (0x0C)
#define MQTTS_TYPE_PUBACK (0x0D)
#define MQTTS_TYPE_PUBCOMP (0x0E)
#define MQTTS_TYPE_PUBREC (0x0F)
#define MQTTS_TYPE_PUBREL (0x10)
#define MQTTS_TYPE_SUBSCRIBE (0x12)
#define MQTTS_TYPE_SUBACK (0x13)
#define MQTTS_TYPE_UNSUBSCRIBE (0x14)
#define MQTTS_TYPE_UNSUBACK (0x15)
#define MQTTS_TYPE_PINGREQ (0x16)
#define MQTTS_TYPE_PINGRESP (0x17)
#define MQTTS_TYPE_DISCONNECT (0x18)
#define MQTTS_TYPE_WILLTOPICUPD (0x1A)
#define MQTTS_TYPE_WILLTOPICRESP (0x1B)
#define MQTTS_TYPE_WILLMSGUPD (0x1C)
#define MQTTS_TYPE_WILLMSGRESP (0x1D)
#define MQTTS_TOPIC_TYPE_NORMAL (0x00)
#define MQTTS_TOPIC_TYPE_PREDEFINED (0x01)
#define MQTTS_TOPIC_TYPE_SHORT (0x02)
#define MQTTS_FLAG_DUP (0x1 << 7)
#define MQTTS_FLAG_QOS_0 (0x0 << 5)
#define MQTTS_FLAG_QOS_1 (0x1 << 5)
#define MQTTS_FLAG_QOS_2 (0x2 << 5)
#define MQTTS_FLAG_QOS_N1 (0x3 << 5)
#define MQTTS_FLAG_RETAIN (0x1 << 4)
#define MQTTS_FLAG_WILL (0x1 << 3)
#define MQTTS_FLAG_CLEAN (0x1 << 2)
#define MQTTS_PROTOCOL_ID (0x01)
typedef struct {
uint8_t length;
uint8_t type;
uint8_t flags;
uint8_t protocol_id;
uint16_t duration;
char client_id[21];
} connect_packet_t;
typedef struct {
uint8_t length;
uint8_t type;
uint8_t return_code;
} connack_packet_t;
typedef struct {
uint8_t length;
uint8_t type;
uint16_t topic_id;
uint16_t message_id;
char topic_name[MQTTS_MAX_PACKET_LENGTH-6];
} register_packet_t;
typedef struct {
uint8_t length;
uint8_t type;
uint16_t topic_id;
uint16_t message_id;
uint8_t return_code;
} regack_packet_t;
typedef struct __attribute__((packed)) {
uint8_t length;
uint8_t type;
uint8_t flags;
uint16_t topic_id;
uint16_t message_id;
char data[MQTTS_MAX_PACKET_LENGTH-7];
} publish_packet_t;
typedef struct __attribute__((packed)) {
uint8_t length;
uint8_t type;
uint8_t flags;
uint16_t message_id;
char topic_name[MQTTS_MAX_PACKET_LENGTH-5];
} subscribe_packet_t;
typedef struct __attribute__((packed)) {
uint8_t length;
uint8_t type;
uint8_t flags;
uint16_t topic_id;
uint16_t message_id;
uint8_t return_code;
} suback_packet_t;
typedef struct {
uint8_t length;
uint8_t type;
uint16_t duration;
} disconnect_packet_t;
// Library functions
int mqtts_create_socket(const char* host, const char* port);
void mqtts_send_connect(int sock, const char* client_id, uint16_t keepalive);
void mqtts_send_register(int sock, const char* topic_name);
void mqtts_send_publish(int sock, uint16_t topic_id, uint8_t topic_type, const char* data, int8_t qos, uint8_t retain);
void mqtts_send_subscribe(int sock, const char* topic_name, uint8_t qos);
void mqtts_send_pingreq(int sock);
void mqtts_send_disconnect(int sock);
void mqtts_recieve_connack(int sock);
uint16_t mqtts_recieve_regack(int sock);
uint16_t mqtts_recieve_suback(int sock);
publish_packet_t* mqtts_loop(int sock, int timeout);
void mqtts_set_debug(uint8_t value);
const char* mqtts_type_string(uint8_t type);
const char* mqtts_return_code_string(uint8_t return_code);
#endif