1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _IOT_EXPORT_MQTT_H_
6 #define _IOT_EXPORT_MQTT_H_
7 
8 #if defined(__cplusplus)
9 extern "C" {
10 #endif
11 
12 #include "linkkit/infra/infra_types.h"
13 #include "linkkit/infra/infra_defs.h"
14 
15 #define MUTLI_SUBSCIRBE_MAX (5)
16 
17 /* From mqtt_client.h */
18 typedef enum {
19     IOTX_MQTT_QOS0 = 0,
20     IOTX_MQTT_QOS1,
21     IOTX_MQTT_QOS2,
22     IOTX_MQTT_QOS3_SUB_LOCAL
23 } iotx_mqtt_qos_t;
24 
25 typedef enum {
26     /* Undefined event */
27     IOTX_MQTT_EVENT_UNDEF = 0,
28 
29     /* MQTT disconnect event */
30     IOTX_MQTT_EVENT_DISCONNECT = 1,
31 
32     /* MQTT reconnect event */
33     IOTX_MQTT_EVENT_RECONNECT = 2,
34 
35     /* A ACK to the specific subscribe which specify by packet-id be received */
36     IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS = 3,
37 
38     /* No ACK to the specific subscribe which specify by packet-id be received
39      * in timeout period */
40     IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT = 4,
41 
42     /* A failed ACK to the specific subscribe which specify by packet-id be
43      * received*/
44     IOTX_MQTT_EVENT_SUBCRIBE_NACK = 5,
45 
46     /* A ACK to the specific unsubscribe which specify by packet-id be received
47      */
48     IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS = 6,
49 
50     /* No ACK to the specific unsubscribe which specify by packet-id be received
51      * in timeout period */
52     IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT = 7,
53 
54     /* A failed ACK to the specific unsubscribe which specify by packet-id be
55      * received*/
56     IOTX_MQTT_EVENT_UNSUBCRIBE_NACK = 8,
57 
58     /* A ACK to the specific publish which specify by packet-id be received */
59     IOTX_MQTT_EVENT_PUBLISH_SUCCESS = 9,
60 
61     /* No ACK to the specific publish which specify by packet-id be received in
62      * timeout period */
63     IOTX_MQTT_EVENT_PUBLISH_TIMEOUT = 10,
64 
65     /* A failed ACK to the specific publish which specify by packet-id be
66      * received*/
67     IOTX_MQTT_EVENT_PUBLISH_NACK = 11,
68 
69     /* MQTT packet published from MQTT remote broker be received */
70     IOTX_MQTT_EVENT_PUBLISH_RECEIVED = 12,
71 
72     /* MQTT packet buffer overflow which the remaining space less than to
73      * receive byte */
74     IOTX_MQTT_EVENT_BUFFER_OVERFLOW = 13,
75 } iotx_mqtt_event_type_t;
76 
77 /* topic information */
78 typedef struct {
79     uint16_t packet_id;
80     uint8_t qos;
81     uint8_t dup;
82     uint8_t retain;
83     uint16_t topic_len;
84     uint32_t payload_len;
85     const char *ptopic;
86     const char *payload;
87 } iotx_mqtt_topic_info_t, *iotx_mqtt_topic_info_pt;
88 
89 typedef struct {
90     /* Specify the event type */
91     iotx_mqtt_event_type_t event_type;
92 
93     /*
94      * Specify the detail event information. @msg means different to different
95      * event types:
96      *
97      * 1) IOTX_MQTT_EVENT_UNKNOWN,
98      *    IOTX_MQTT_EVENT_DISCONNECT,
99      *    IOTX_MQTT_EVENT_RECONNECT :
100      *      Its data type is string and the value is detail information.
101      *
102      * 2) IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS,
103      *    IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT,
104      *    IOTX_MQTT_EVENT_SUBCRIBE_NACK,
105      *    IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS,
106      *    IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT,
107      *    IOTX_MQTT_EVENT_UNSUBCRIBE_NACK
108      *    IOTX_MQTT_EVENT_PUBLISH_SUCCESS,
109      *    IOTX_MQTT_EVENT_PUBLISH_TIMEOUT,
110      *    IOTX_MQTT_EVENT_PUBLISH_NACK :
111      *      Its data type is @uint32_t and the value is MQTT packet identifier.
112      *
113      * 3) IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
114      *      Its data type is @iotx_mqtt_topic_info_pt and see detail at the
115      * declare of this type.
116      *
117      * */
118     void *msg;
119 } iotx_mqtt_event_msg_t, *iotx_mqtt_event_msg_pt;
120 
121 /**
122  * @brief It define a datatype of function pointer.
123  *        This type of function will be called when a related event occur.
124  *
125  * @param pcontext : The program context.
126  * @param pclient : The MQTT client.
127  * @param msg : The event message.
128  *
129  * @return none
130  */
131 typedef void (*iotx_mqtt_event_handle_func_fpt)(void *pcontext, void *pclient,
132                                                 iotx_mqtt_event_msg_pt msg);
133 
134 /* The structure of MQTT event handle */
135 typedef struct {
136     iotx_mqtt_event_handle_func_fpt h_fp;
137     void *pcontext;
138 } iotx_mqtt_event_handle_t, *iotx_mqtt_event_handle_pt;
139 
140 /* The structure of MQTT initial parameter */
141 typedef struct {
142     uint16_t port;              /* Specify MQTT broker port */
143     const char *host;           /* Specify MQTT broker host */
144     const char *client_id;      /* Specify MQTT connection client id*/
145     const char *username;       /* Specify MQTT user name */
146     const char *password;       /* Specify MQTT password */
147     const char *customize_info; /* Specify User custom information */
148     /* Specify MQTT transport channel and key.
149      * If the value is NULL, it means that use TCP channel,
150      * If the value is NOT NULL, it means that use SSL/TLS channel and
151      *   @pub_key point to the CA certification */
152 
153     const char *pub_key;
154 
155     uint8_t clean_session;          /* Specify MQTT clean session or not*/
156     uint32_t request_timeout_ms;    /* Specify timeout of a MQTT request in
157                                        millisecond */
158     uint32_t keepalive_interval_ms; /* Specify MQTT keep-alive interval in
159                                        millisecond */
160     uint32_t write_buf_size;        /* Specify size of write-buffer in byte */
161     uint32_t read_buf_size;         /* Specify size of read-buffer in byte */
162 
163     iotx_mqtt_event_handle_t handle_event; /* Specify MQTT event handle */
164 
165 } iotx_mqtt_param_t, *iotx_mqtt_param_pt;
166 
167 typedef enum {
168     IOTX_MQTT_SOC_CONNECTED,
169     IOTX_MQTT_SOC_CLOSE,
170     IOTX_MQTT_SOC_READ,
171     IOTX_MQTT_SOC_WRITE,
172     IOTX_MQTT_SOC_MAX
173 } iotx_mqtt_nwk_event_t;
174 
175 typedef struct {
176     uintptr_t fd;
177 } iotx_mqtt_nwk_param_t;
178 
179 /** @defgroup group_api api
180  *  @{
181  */
182 
183 /** @defgroup group_api_mqtt mqtt
184  *  @{
185  */
186 
187 /**
188  * @brief Construct the MQTT client
189  *        This function initialize the data structures, establish MQTT
190  * connection.
191  *
192  * @param [in] pInitParams: specify the MQTT client parameter.
193  *
194  * @retval     NULL : Construct failed.
195  * @retval NOT_NULL : The handle of MQTT client.
196  * @see None.
197  */
198 void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams);
199 
200 /**
201  * @brief Deconstruct the MQTT client
202  *        This function disconnect MQTT connection and release the related
203  * resource.
204  *
205  * @param [in] phandle: pointer of handle, specify the MQTT client.
206  *
207  * @retval  0 : Deconstruct success.
208  * @retval -1 : Deconstruct failed.
209  * @see None.
210  */
211 int IOT_MQTT_Destroy(void **phandle);
212 
213 /**
214  * @brief Handle MQTT packet from remote server and process timeout request
215  *        which include the MQTT subscribe, unsubscribe, publish(QOS >= 1),
216  * reconnect, etc..
217  *
218  * @param [in] handle: specify the MQTT client.
219  * @param [in] timeout_ms: specify the timeout in millisecond in this loop.
220  *
221  * @return status.
222  * @see None.
223  */
224 int IOT_MQTT_Yield(void *handle, int timeout_ms);
225 
226 /**
227  * @brief check whether MQTT connection is established or not.
228  *
229  * @param [in] handle: specify the MQTT client.
230  *
231  * @retval true  : MQTT in normal state.
232  * @retval false : MQTT in abnormal state.
233  * @see None.
234  */
235 int IOT_MQTT_CheckStateNormal(void *handle);
236 
237 /**
238  * @brief Subscribe MQTT topic.
239  *
240  * @param [in] handle: specify the MQTT client.
241  * @param [in] topic_filter: specify the topic filter.
242  * @param [in] qos: specify the MQTT Requested QoS.
243  * @param [in] topic_handle_func: specify the topic handle callback-function.
244  * @param [in] pcontext: specify context. When call 'topic_handle_func', it will
245  be passed back.
246  *
247  * @retval -1  : Subscribe failed.
248  * @retval >=0 : Subscribe successful.
249           The value is a unique ID of this request.
250           The ID will be passed back when callback
251  'iotx_mqtt_param_t:handle_event'.
252  * @see None.
253  */
254 int IOT_MQTT_Subscribe(void *handle, const char *topic_filter,
255                        iotx_mqtt_qos_t qos,
256                        iotx_mqtt_event_handle_func_fpt topic_handle_func,
257                        void *pcontext);
258 
259 /**
260  * @brief Subscribe MQTT topic and wait suback.
261  *
262  * @param [in] handle: specify the MQTT client.
263  * @param [in] topic_filter: specify the topic filter.
264  * @param [in] qos: specify the MQTT Requested QoS.
265  * @param [in] topic_handle_func: specify the topic handle callback-function.
266  * @param [in] pcontext: specify context. When call 'topic_handle_func', it will
267  be passed back.
268  * @param [in] timeout_ms: time in ms to wait.
269  *
270  * @retval -1  : Subscribe failed.
271  * @retval >=0 : Subscribe successful.
272           The value is a unique ID of this request.
273           The ID will be passed back when callback
274  'iotx_mqtt_param_t:handle_event'.
275  * @see None.
276  */
277 int IOT_MQTT_Subscribe_Sync(void *handle, const char *topic_filter,
278                             iotx_mqtt_qos_t qos,
279                             iotx_mqtt_event_handle_func_fpt topic_handle_func,
280                             void *pcontext, int timeout_ms);
281 
282 /**
283  * @brief Unsubscribe MQTT topic.
284  *
285  * @param [in] handle: specify the MQTT client.
286  * @param [in] topic_filter: specify the topic filter.
287  *
288  * @retval -1  : Unsubscribe failed.
289  * @retval >=0 : Unsubscribe successful.
290           The value is a unique ID of this request.
291           The ID will be passed back when callback
292  'iotx_mqtt_param_t:handle_event'.
293  * @see None.
294  */
295 int IOT_MQTT_Unsubscribe(void *handle, const char *topic_filter);
296 
297 /**
298  * @brief Publish message to specific topic.
299  *
300  * @param [in] handle: specify the MQTT client.
301  * @param [in] topic_name: specify the topic name.
302  * @param [in] topic_msg: specify the topic message.
303  *
304  * @retval -1 :  Publish failed.
305  * @retval  0 :  Publish successful, where QoS is 0.
306  * @retval >0 :  Publish successful, where QoS is >= 0.
307         The value is a unique ID of this request.
308         The ID will be passed back when callback
309  'iotx_mqtt_param_t:handle_event'.
310  * @see None.
311  */
312 int IOT_MQTT_Publish(void *handle, const char *topic_name,
313                      iotx_mqtt_topic_info_pt topic_msg);
314 /**
315  * @brief Publish message to specific topic.
316  *
317  * @param [in] handle: specify the MQTT client.
318  * @param [in] topic_name: specify the topic name.
319  * @param [in] qos: specify the MQTT Requested QoS.
320  * @param [in] data: specify the topic message payload.
321  * @param [in] len: specify the topic message payload len.
322  *
323  * @retval -1 :  Publish failed.
324  * @retval  0 :  Publish successful, where QoS is 0.
325  * @retval >0 :  Publish successful, where QoS is >= 0.
326         The value is a unique ID of this request.
327         The ID will be passed back when callback
328  'iotx_mqtt_param_t:handle_event'.
329  * @see None.
330  */
331 int IOT_MQTT_Publish_Simple(void *handle, const char *topic_name, int qos,
332                             void *data, int len);
333 /* From mqtt_client.h */
334 /** @} */ /* end of api_mqtt */
335 
336 /** @} */ /* end of api */
337 
338 /**
339  * @brief Only used in async network stack and FEATURE_ASYNC_PROTOCOL_STACK must
340  * be selected
341  *
342  * @param [in] handle: specify the MQTT client.
343  * @param [in] event: specify the network event.
344  * @param [in] param: specify the network params.
345  *
346  * @retval -1 :  Handle failed.
347  * @retval  0 :  Handle successful.
348  *
349  */
350 int IOT_MQTT_Nwk_Event_Handler(void *handle, iotx_mqtt_nwk_event_t event,
351                                iotx_mqtt_nwk_param_t *param);
352 
353 /* MQTT Configurations
354  *
355  * These switches will affect mqtt_api.c and IOT_MQTT_XXX() functions' behaviour
356  *
357  */
358 
359 /* Default message length in bytes when PLATFORM_HAS_DYNMEM is not set */
360 #define CONFIG_MQTT_MESSAGE_MAXLEN (3072)
361 
362 /* Default maximum length of topic name in byte when PLATFORM_HAS_DYNMEM is not
363  * set */
364 #ifdef PLATFORM_HAS_DYNMEM
365 #define CONFIG_MQTT_TOPIC_MAXLEN (128)
366 #else
367 #define CONFIG_MQTT_TOPIC_MAXLEN (128)
368 #endif
369 
370 /* Default keepalive interval of MQTT request in second */
371 #define CONFIG_MQTT_KEEPALIVE_INTERVAL     (60)
372 
373 /* Default timeout interval of MQTT request in millisecond */
374 #define CONFIG_MQTT_REQUEST_TIMEOUT        (5000)
375 
376 /* Minimum timeout interval of MQTT request in millisecond */
377 #define CONFIG_MQTT_REQ_TIMEOUT_MIN        (1000)
378 
379 /* Maximum timeout interval of MQTT request in millisecond */
380 #define CONFIG_MQTT_REQ_TIMEOUT_MAX        (10000)
381 
382 /* Minimum keepalive interval of MQTT request in second */
383 #define CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN (30)
384 
385 /* Maximum keepalive interval of MQTT request in second */
386 #define CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX (1200)
387 
388 #if defined(__cplusplus)
389 }
390 #endif
391 #endif
392