Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTConnect.h Source File

MQTTConnect.h

00001 /*******************************************************************************
00002  * Copyright (c) 2014, 2017 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Ian Craggs - add connack return code definitions 
00016  *    Xiang Rong - 442039 Add makefile to Embedded C client
00017  *    Ian Craggs - fix for issue #64, bit order in connack response
00018  *******************************************************************************/
00019 
00020 #ifndef MQTTCONNECT_H_
00021 #define MQTTCONNECT_H_
00022 
00023 enum connack_return_codes
00024 {
00025     MQTT_CONNECTION_ACCEPTED = 0,
00026     MQTT_UNNACCEPTABLE_PROTOCOL = 1,
00027     MQTT_CLIENTID_REJECTED = 2,
00028     MQTT_SERVER_UNAVAILABLE = 3,
00029     MQTT_BAD_USERNAME_OR_PASSWORD = 4,
00030     MQTT_NOT_AUTHORIZED = 5,
00031 };
00032 
00033 #if !defined(DLLImport)
00034   #define DLLImport
00035 #endif
00036 #if !defined(DLLExport)
00037   #define DLLExport
00038 #endif
00039 
00040 
00041 typedef union
00042 {
00043     unsigned char all;  /**< all connect flags */
00044 #if defined(REVERSED)
00045     struct
00046     {
00047         unsigned int username : 1;          /**< 3.1 user name */
00048         unsigned int password : 1;          /**< 3.1 password */
00049         unsigned int willRetain : 1;        /**< will retain setting */
00050         unsigned int willQoS : 2;               /**< will QoS value */
00051         unsigned int will : 1;              /**< will flag */
00052         unsigned int cleansession : 1;    /**< clean session flag */
00053         unsigned int : 1;                 /**< unused */
00054     } bits;
00055 #else
00056     struct
00057     {
00058         unsigned int : 1;                           /**< unused */
00059         unsigned int cleansession : 1;    /**< cleansession flag */
00060         unsigned int will : 1;              /**< will flag */
00061         unsigned int willQoS : 2;               /**< will QoS value */
00062         unsigned int willRetain : 1;        /**< will retain setting */
00063         unsigned int password : 1;          /**< 3.1 password */
00064         unsigned int username : 1;          /**< 3.1 user name */
00065     } bits;
00066 #endif
00067 } MQTTConnectFlags; /**< connect flags byte */
00068 
00069 
00070 
00071 /**
00072  * Defines the MQTT "Last Will and Testament" (LWT) settings for
00073  * the connect packet.
00074  */
00075 typedef struct
00076 {
00077     /** The eyecatcher for this structure.  must be MQTW. */
00078     char struct_id[4];
00079     /** The version number of this structure.  Must be 0 */
00080     int struct_version;
00081     /** The LWT topic to which the LWT message will be published. */
00082     MQTTString topicName;
00083     /** The LWT payload. */
00084     MQTTString message;
00085     /**
00086       * The retained flag for the LWT message (see MQTTAsync_message.retained).
00087       */
00088     unsigned char retained;
00089     /**
00090       * The quality of service setting for the LWT message (see
00091       * MQTTAsync_message.qos and @ref qos).
00092       */
00093     char qos;
00094 } MQTTPacket_willOptions;
00095 
00096 
00097 #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
00098 
00099 
00100 typedef struct
00101 {
00102     /** The eyecatcher for this structure.  must be MQTC. */
00103     char struct_id[4];
00104     /** The version number of this structure.  Must be 0 */
00105     int struct_version;
00106     /** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1
00107       */
00108     unsigned char MQTTVersion;
00109     MQTTString clientID;
00110     unsigned short keepAliveInterval;
00111     unsigned char cleansession;
00112     unsigned char willFlag;
00113     MQTTPacket_willOptions will;
00114     MQTTString username;
00115     MQTTString password;
00116 } MQTTPacket_connectData;
00117 
00118 typedef union
00119 {
00120     unsigned char all;  /**< all connack flags */
00121 #if defined(REVERSED)
00122     struct
00123     {
00124         unsigned int reserved : 7;          /**< unused */
00125         unsigned int sessionpresent : 1;    /**< session present flag */
00126     } bits;
00127 #else
00128     struct
00129     {
00130         unsigned int sessionpresent : 1;    /**< session present flag */
00131         unsigned int reserved: 7;           /**< unused */
00132     } bits;
00133 #endif
00134 } MQTTConnackFlags; /**< connack flags byte */
00135 
00136 #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
00137         MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
00138 
00139 DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
00140 DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
00141 
00142 DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
00143 DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
00144 
00145 DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
00146 DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
00147 
00148 #endif /* MQTTCONNECT_H_ */
00149