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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright 2014 James Geboski <jgeboski@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file **/
#ifndef _FACEBOOK_MQTT_H
#define _FACEBOOK_MQTT_H
#include <glib.h>
#include <string.h>
#include "facebook-util.h"
#define FB_MQTT_NAME "MQIsdp"
#define FB_MQTT_VERS 3
#define FB_MQTT_KA 60
#define FB_MQTT_HOST "mqtt.facebook.com"
#define FB_MQTT_PORT 443
#define FB_MQTT_TIMEOUT (FB_MQTT_KA * 1000)
/**
* Executes one of the #fb_mqtt_funcs.
*
* @param m The #fb_mqtt.
* @param f The function to execute.
* @param ... The operational function arguments.
**/
#define FB_MQTT_FUNC(m, f, ...) \
G_STMT_START { \
if (G_LIKELY(m->funcs.f != NULL)) { \
m->funcs.f(m, ##__VA_ARGS__, m->data); \
} \
} G_STMT_END
/** The flags of #fb_mqtt CONNECT packets. **/
typedef enum fb_mqtt_connect_flags fb_mqtt_connect_flags_t;
/** The #GError codes of #fb_mqtt. **/
typedef enum fb_mqtt_error fb_mqtt_error_t;
/** The flags of #fb_mqtt messages. **/
typedef enum fb_mqtt_msg_flags fb_mqtt_msg_flags_t;
/** The type of #fb_mqtt messages. **/
typedef enum fb_mqtt_msg_type fb_mqtt_msg_type_t;
/** The main structure for #fb_mqtt callback functions. **/
typedef struct fb_mqtt_funcs fb_mqtt_funcs_t;
/** The structure for interacting with Facebook MQTT. **/
typedef struct fb_mqtt fb_mqtt_t;
/** The structure of a #fb_mqtt message. **/
typedef struct fb_mqtt_msg fb_mqtt_msg_t;
/**
* The flags of #fb_mqtt CONNECT packets.
**/
enum fb_mqtt_connect_flags
{
FB_MQTT_CONNECT_FLAG_CLR = 1 << 1, /** Clear session. **/
FB_MQTT_CONNECT_FLAG_WILL = 1 << 2, /** Will flag. **/
FB_MQTT_CONNECT_FLAG_RET = 1 << 5, /** Will retain. **/
FB_MQTT_CONNECT_FLAG_PASS = 1 << 6, /** Password. **/
FB_MQTT_CONNECT_FLAG_USER = 1 << 7, /** Username. **/
FB_MQTT_CONNECT_FLAG_QOS0 = 0 << 3, /** Fire and forget. **/
FB_MQTT_CONNECT_FLAG_QOS1 = 1 << 3, /** Acknowledge delivery. **/
FB_MQTT_CONNECT_FLAG_QOS2 = 2 << 3 /** Assure delivery. **/
};
/**
* The #GError codes of #fb_mqtt.
**/
enum fb_mqtt_error
{
FB_MQTT_ERROR_SUCCESS = 0, /** Success. **/
FB_MQTT_ERROR_PRTVERS = 1, /** Unacceptable protocol version. **/
FB_MQTT_ERROR_IDREJECT = 2, /** Identifier rejected. **/
FB_MQTT_ERROR_SRVGONE = 3, /** Server unavailable. **/
FB_MQTT_ERROR_USERPASS = 4, /** Bad username or password. **/
FB_MQTT_ERROR_UNAUTHORIZED = 5, /** Not authorized. **/
FB_MQTT_ERROR_GENERAL /** General. **/
};
/**
* The flags of #fb_mqtt messages.
**/
enum fb_mqtt_msg_flags
{
FB_MQTT_MSG_FLAG_RET = 1 << 0, /** Retain. **/
FB_MQTT_MSG_FLAG_DUP = 1 << 3, /** Duplicate delivery. **/
FB_MQTT_MSG_FLAG_QOS0 = 0 << 1, /** Fire and forget. **/
FB_MQTT_MSG_FLAG_QOS1 = 1 << 1, /** Acknowledge delivery. **/
FB_MQTT_MSG_FLAG_QOS2 = 2 << 1 /** Assure delivery. **/
};
/**
* The type of #fb_mqtt messages.
**/
enum fb_mqtt_msg_type
{
FB_MQTT_MSG_TYPE_CONNECT = 1, /** Connect to Server. **/
FB_MQTT_MSG_TYPE_CONNACK = 2, /** Connect Acknowledgment. **/
FB_MQTT_MSG_TYPE_PUBLISH = 3, /** Publish Message. **/
FB_MQTT_MSG_TYPE_PUBACK = 4, /** Publish Acknowledgment. **/
FB_MQTT_MSG_TYPE_PUBREC = 5, /** Publish Received. **/
FB_MQTT_MSG_TYPE_PUBREL = 6, /** Publish Release. **/
FB_MQTT_MSG_TYPE_PUBCOMP = 7, /** Publish Complete. **/
FB_MQTT_MSG_TYPE_SUBSCRIBE = 8, /** Client Subscribe request. **/
FB_MQTT_MSG_TYPE_SUBACK = 9, /** Subscribe Acknowledgment. **/
FB_MQTT_MSG_TYPE_UNSUBSCRIBE = 10, /** Client Unsubscribe request. **/
FB_MQTT_MSG_TYPE_UNSUBACK = 11, /** Unsubscribe Acknowledgment. **/
FB_MQTT_MSG_TYPE_PINGREQ = 12, /** PING Request. **/
FB_MQTT_MSG_TYPE_PINGRESP = 13, /** PING Response. **/
FB_MQTT_MSG_TYPE_DISCONNECT = 14 /** Client is Disconnecting. **/
};
/**
* The main structure for #fb_mqtt callback functions.
**/
struct fb_mqtt_funcs
{
/**
* The error function. This is called whenever an error occurs
* within the #fb_mqtt.
*
* @param mqtt The #fb_mqtt.
* @param err The #GError.
* @param data The user-defined data or NULL.
**/
void (*error) (fb_mqtt_t *mqtt, GError *err, gpointer data);
/**
* The open function. This is called when the connection to the
* MQTT has been initialized. This is called as a result of
* #fb_mqtt_open(). This function should call #fb_mqtt_connect().
*
* @param mqtt The #fb_mqtt.
* @param data The user-defined data or NULL.
**/
void (*open) (fb_mqtt_t *mqtt, gpointer data);
/**
* The connack function. This is called when a CONNACK packet is
* received. This is called as a result of #fb_mqtt_connect().
*
* @param mqtt The #fb_mqtt.
* @param data The user-defined data or NULL.
**/
void (*connack) (fb_mqtt_t *mqtt, gpointer data);
/**
* The publish function. This is called when a PUBLISH packet is
* received.
*
* @param mqtt The #fb_mqtt.
* @param topic The message topic.
* @param pload The message payload.
* @param data The user-defined data or NULL.
**/
void (*publish) (fb_mqtt_t *mqtt, const gchar *topic,
const GByteArray *pload, gpointer data);
};
/**
* The structure for interacting with Facebook MQTT.
**/
struct fb_mqtt
{
gboolean connected; /** TRUE if connected, otherwise FALSE. **/
fb_mqtt_funcs_t funcs; /** The #fb_mqtt_funcs. **/
gpointer data; /** The user defined data or NULL. **/
GError *err; /** The #GError or NULL. **/
gpointer ssl; /** The SSL connection or NULL. **/
gint tev; /** The timer event identifier. **/
gint rev; /** The read event identifier. **/
gint wev; /** The write event identifier. **/
GByteArray *rbuf; /** The read buffer. **/
GByteArray *wbuf; /** The write buffer. **/
gsize remz; /** The remaining read size. **/
guint16 mid; /** The message identifier. **/
};
/**
* The structure of a #fb_mqtt message.
**/
struct fb_mqtt_msg
{
fb_mqtt_msg_type_t type; /** The #fb_mqtt_msg_type. **/
fb_mqtt_msg_flags_t flags; /** The #fb_mqtt_msg_flags. **/
GByteArray *bytes; /** The #GByteArray of data. **/
guint offset; /** The offset of the data. **/
guint pos; /** The cursor position. **/
gboolean local; /** TRUE if the data is local. **/
};
#define FB_MQTT_ERROR fb_mqtt_error_quark()
GQuark fb_mqtt_error_quark(void);
fb_mqtt_t *fb_mqtt_new(const fb_mqtt_funcs_t *funcs, gpointer data);
void fb_mqtt_free(fb_mqtt_t *mqtt);
void fb_mqtt_close(fb_mqtt_t *mqtt);
void fb_mqtt_error(fb_mqtt_t *mqtt, fb_mqtt_error_t err,
const gchar *fmt, ...);
void fb_mqtt_read(fb_mqtt_t *mqtt, fb_mqtt_msg_t *msg);
void fb_mqtt_write(fb_mqtt_t *mqtt, fb_mqtt_msg_t *msg);
void fb_mqtt_open(fb_mqtt_t *mqtt, const gchar *host, gint port);
void fb_mqtt_connect(fb_mqtt_t *mqtt, guint8 flags, const gchar *cid, ...)
G_GNUC_NULL_TERMINATED;
gboolean fb_mqtt_connected(fb_mqtt_t *mqtt, gboolean error);
void fb_mqtt_disconnect(fb_mqtt_t *mqtt);
void fb_mqtt_publish(fb_mqtt_t *mqtt, const gchar *topic,
const GByteArray *bytes);
void fb_mqtt_subscribe(fb_mqtt_t *mqtt, const gchar *topic1, guint16 qos1, ...)
G_GNUC_NULL_TERMINATED;
void fb_mqtt_unsubscribe(fb_mqtt_t *mqtt, const gchar *topic1, ...)
G_GNUC_NULL_TERMINATED;
fb_mqtt_msg_t *fb_mqtt_msg_new(fb_mqtt_msg_type_t type,
fb_mqtt_msg_flags_t flags);
fb_mqtt_msg_t *fb_mqtt_msg_new_bytes(GByteArray *bytes);
void fb_mqtt_msg_free(fb_mqtt_msg_t *msg);
void fb_mqtt_msg_reset(fb_mqtt_msg_t *msg);
const GByteArray *fb_mqtt_msg_bytes(fb_mqtt_msg_t *msg);
gboolean fb_mqtt_msg_read(fb_mqtt_msg_t *msg, gpointer data, guint size);
gboolean fb_mqtt_msg_read_r(fb_mqtt_msg_t *msg, GByteArray *bytes);
gboolean fb_mqtt_msg_read_byte(fb_mqtt_msg_t *msg, guint8 *byte);
gboolean fb_mqtt_msg_read_mid(fb_mqtt_msg_t *msg, guint16 *mid);
gboolean fb_mqtt_msg_read_u16(fb_mqtt_msg_t *msg, guint16 *u16);
gboolean fb_mqtt_msg_read_str(fb_mqtt_msg_t *msg, gchar **str);
void fb_mqtt_msg_write(fb_mqtt_msg_t *msg, gconstpointer data, guint size);
void fb_mqtt_msg_write_byte(fb_mqtt_msg_t *msg, guint8 byte);
void fb_mqtt_msg_write_mid(fb_mqtt_msg_t *msg, guint16 *mid);
void fb_mqtt_msg_write_u16(fb_mqtt_msg_t *msg, guint16 u16);
void fb_mqtt_msg_write_str(fb_mqtt_msg_t *msg, const gchar *str);
#endif /* _FACEBOOK_MQTT_H */
|