1 /*******************************************************************************
2  * Copyright (c) 2014 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  *    Xiang Rong - 442039 Add makefile to Embedded C client
16  *******************************************************************************/
17 
18 #ifndef MQTTCONNECT_H_
19 #define MQTTCONNECT_H_
20 
21 #if !defined(DLLImport)
22 #define DLLImport
23 #endif
24 #if !defined(DLLExport)
25 #define DLLExport
26 #endif
27 
28 #if defined(__CC_ARM)
29 #ifdef __BIG_ENDIAN
30 #define MQTT_BIG_ENDIAN 1
31 #else
32 #define MQTT_BIG_ENDIAN 0
33 #endif
34 #elif defined(__ICCARM__)
35 #if (__LITTLE_ENDIAN__ == 0)
36 #define MQTT_BIG_ENDIAN 1
37 #else
38 #define MQTT_BIG_ENDIAN 0
39 #endif
40 #elif defined(__GNUC__)
41 #if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
42 #define MQTT_BIG_ENDIAN 1
43 #else
44 #define MQTT_BIG_ENDIAN 0
45 #endif
46 #endif
47 
48 #define MQTT_CONN_FLAG_USER_NAME     (0x80)
49 #define MQTT_CONN_FLAG_PASSWORD      (0x40)
50 #define MQTT_CONN_FLAG_WILL_RETAIN   (0x20)
51 #define MQTT_CONN_FLAG_WILL_QOS      (0x18)
52 #define MQTT_CONN_FLAG_WILL_FLAG     (0x04)
53 #define MQTT_CONN_FLAG_CLEAN_SESSION (0x02)
54 
55 typedef union {
56     unsigned char all; /**< all connect flags */
57 } MQTTConnectFlags;    /**< connect flags byte */
58 
59 /**
60  * Defines the MQTT "Last Will and Testament" (LWT) settings for
61  * the connect packet.
62  */
63 typedef struct {
64     /** The eyecatcher for this structure.  must be MQTW. */
65     char struct_id[4];
66     /** The version number of this structure.  Must be 0 */
67     int struct_version;
68     /** The LWT topic to which the LWT message will be published. */
69     MQTTString topicName;
70     /** The LWT payload. */
71     MQTTString message;
72     /**
73      * The retained flag for the LWT message (see MQTTAsync_message.retained).
74      */
75     unsigned char retained;
76     /**
77      * The quality of service setting for the LWT message (see
78      * MQTTAsync_message.qos and @ref qos).
79      */
80     char qos;
81 } MQTTPacket_willOptions;
82 
83 #define MQTTPacket_willOptions_initializer                \
84     {                                                     \
85         { 'M', 'Q', 'T', 'W' }, 0, { NULL, { 0, NULL } }, \
86             { NULL, { 0, NULL } }, 0, 0                   \
87     }
88 
89 typedef struct {
90     /** The eyecatcher for this structure.  must be MQTC. */
91     char struct_id[4];
92     /** The version number of this structure.  Must be 0 */
93     int struct_version;
94     /** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1
95      */
96     unsigned char MQTTVersion;
97     MQTTString clientID;
98     unsigned short keepAliveInterval; /* 单位s */
99     unsigned char cleansession;
100     unsigned char willFlag;
101     MQTTPacket_willOptions will;
102     MQTTString username;
103     MQTTString password;
104 } MQTTPacket_connectData;
105 
106 typedef union {
107     unsigned char all; /**< all connack flags */
108 #if MQTT_BIG_ENDIAN
109     struct {
110         unsigned int sessionpresent:1; /**< session present flag */
111         unsigned int:7;                /**< unused */
112     } bits;
113 #else
114     struct {
115         unsigned int:7;                /**< unused */
116         unsigned int sessionpresent:1; /**< session present flag */
117     } bits;
118 #endif
119 } MQTTConnackFlags; /**< connack flags byte */
120 
121 #define MQTTPacket_connectData_initializer                             \
122     {                                                                  \
123         { 'M', 'Q', 'T', 'C' }, 0, 4, { NULL, { 0, NULL } },           \
124             CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN, 1, 0,                  \
125             MQTTPacket_willOptions_initializer, { NULL, { 0, NULL } }, \
126         {                                                              \
127             NULL, { 0, NULL }                                          \
128         }                                                              \
129     }
130 
131 DLLExport int MQTTSerialize_connect(unsigned char *buf, int buflen,
132                                     MQTTPacket_connectData *options);
133 DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData *data,
134                                       unsigned char *buf, int len);
135 
136 DLLExport int MQTTSerialize_connack(unsigned char *buf, int buflen,
137                                     unsigned char connack_rc,
138                                     unsigned char sessionPresent);
139 DLLExport int MQTTDeserialize_connack(unsigned char *sessionPresent,
140                                       unsigned char *connack_rc,
141                                       unsigned char *buf, int buflen);
142 
143 DLLExport int MQTTSerialize_disconnect(unsigned char *buf, int buflen);
144 DLLExport int MQTTSerialize_pingreq(unsigned char *buf, int buflen);
145 
146 #endif /* MQTTCONNECT_H_ */
147