Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mqtt_common.h Source File

mqtt_common.h

Go to the documentation of this file.
00001 /**
00002  * @file mqtt_common.h
00003  * @brief Definitions common to MQTT client and server
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _MQTT_COMMON_H
00030 #define _MQTT_COMMON_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 
00035 //MQTT port numbers
00036 #define MQTT_PORT 1883
00037 //MQTT over SSL/TLS port number
00038 #define MQTT_TLS_PORT 8883
00039 
00040 //MQTT 3.1 protocol name
00041 #define MQTT_PROTOCOL_NAME_3_1 "MQIsdp"
00042 //MQTT 3.1.1 protocol name
00043 #define MQTT_PROTOCOL_NAME_3_1_1 "MQTT"
00044 
00045 //Minimum size of MQTT header
00046 #define MQTT_MIN_HEADER_SIZE 2
00047 //Maximum size of MQTT header
00048 #define MQTT_MAX_HEADER_SIZE 5
00049 
00050 
00051 /**
00052  * @brief MQTT protocol level
00053  */
00054 
00055 typedef enum
00056 {
00057    MQTT_PROTOCOL_LEVEL_3_1   = 3, ///<MQTT version 3.1
00058    MQTT_PROTOCOL_LEVEL_3_1_1 = 4  ///<MQTT version 3.1.1
00059 } MqttProtocolLevel;
00060 
00061 
00062 /**
00063  * @brief Transport protocol
00064  **/
00065 
00066 typedef enum
00067 {
00068    MQTT_TRANSPORT_PROTOCOL_TCP = 1, ///TCP protocol
00069    MQTT_TRANSPORT_PROTOCOL_TLS = 2, ///SSL protocol
00070    MQTT_TRANSPORT_PROTOCOL_WS  = 3, ///WebSocket protocol
00071    MQTT_TRANSPORT_PROTOCOL_WSS = 4, ///Secure WebSocket protocol
00072 } MqttTransportProtocol;
00073 
00074 
00075 /**
00076  * @brief Quality of service level
00077  **/
00078 
00079 typedef enum
00080 {
00081    MQTT_QOS_LEVEL_0 = 0, ///<At most once delivery
00082    MQTT_QOS_LEVEL_1 = 1, ///<At least once delivery
00083    MQTT_QOS_LEVEL_2 = 2  ///<Exactly once delivery
00084 } MqttQosLevel;
00085 
00086 
00087 /**
00088  * @brief MQTT control packet type
00089  **/
00090 
00091 typedef enum
00092 {
00093    MQTT_PACKET_TYPE_INVALID     = 0,  ///<Invalid packet
00094    MQTT_PACKET_TYPE_CONNECT     = 1,  ///<Client request to connect to server
00095    MQTT_PACKET_TYPE_CONNACK     = 2,  ///<Connect acknowledgment
00096    MQTT_PACKET_TYPE_PUBLISH     = 3,  ///<Publish message
00097    MQTT_PACKET_TYPE_PUBACK      = 4,  ///<Publish acknowledgment
00098    MQTT_PACKET_TYPE_PUBREC      = 5,  ///<Publish received (assured delivery part 1)
00099    MQTT_PACKET_TYPE_PUBREL      = 6,  ///<Publish release (assured delivery part 2)
00100    MQTT_PACKET_TYPE_PUBCOMP     = 7,  ///<Publish complete (assured delivery part 3)
00101    MQTT_PACKET_TYPE_SUBSCRIBE   = 8,  ///<Client subscribe request
00102    MQTT_PACKET_TYPE_SUBACK      = 9,  ///<Subscribe acknowledgment
00103    MQTT_PACKET_TYPE_UNSUBSCRIBE = 10, ///<Unsubscribe request
00104    MQTT_PACKET_TYPE_UNSUBACK    = 11, ///<Unsubscribe acknowledgment
00105    MQTT_PACKET_TYPE_PINGREQ     = 12, ///<Ping request
00106    MQTT_PACKET_TYPE_PINGRESP    = 13, ///<Ping response
00107    MQTT_PACKET_TYPE_DISCONNECT  = 14  ///<Client is disconnecting
00108 } MqttPacketType;
00109 
00110 
00111 /**
00112  * @brief Connect flags
00113  **/
00114 
00115 typedef enum
00116 {
00117    MQTT_CONNECT_FLAG_CLEAN_SESSION = 0x02,
00118    MQTT_CONNECT_FLAG_WILL          = 0x04,
00119    MQTT_CONNECT_FLAG_WILL_QOS_0    = 0x00,
00120    MQTT_CONNECT_FLAG_WILL_QOS_1    = 0x08,
00121    MQTT_CONNECT_FLAG_WILL_QOS_2    = 0x10,
00122    MQTT_CONNECT_FLAG_WILL_RETAIN   = 0x20,
00123    MQTT_CONNECT_FLAG_PASSWORD      = 0x40,
00124    MQTT_CONNECT_FLAG_USERNAME      = 0x80
00125 } MqttConnectFlags;
00126 
00127 
00128 /**
00129  * @brief Connect Acknowledge flags
00130  **/
00131 
00132 typedef enum
00133 {
00134    MQTT_CONNECT_ACK_FLAG_SESSION_PRESENT = 0x01
00135 } MqttConnectAckFlags;
00136 
00137 
00138 /**
00139  * @brief Connect return codes
00140  **/
00141 
00142 typedef enum
00143 {
00144    MQTT_CONNECT_RET_CODE_ACCEPTED             = 0,
00145    MQTT_CONNECT_RET_CODE_UNACCEPTABLE_VERSION = 1,
00146    MQTT_CONNECT_RET_CODE_ID_REJECTED          = 2,
00147    MQTT_CONNECT_RET_CODE_SERVER_UNAVAILABLE   = 3,
00148    MQTT_CONNECT_RET_CODE_BAD_USER_NAME        = 4,
00149    MQTT_CONNECT_RET_CODE_NOT_AUTHORIZED       = 5
00150 } MqttConnectRetCode;
00151 
00152 
00153 //CodeWarrior or Win32 compiler?
00154 #if defined(__CWCC__) || defined(_WIN32)
00155    #pragma pack(push, 1)
00156 #endif
00157 
00158 
00159 /**
00160  * @brief Fixed header
00161  **/
00162 
00163 typedef __start_packed struct
00164 {
00165 #ifdef _CPU_BIG_ENDIAN
00166    uint8_t type : 4;   //0
00167    uint8_t dup : 1;
00168    uint8_t qos : 2;
00169    uint8_t retain: 1;
00170 #else
00171    uint8_t retain : 1; //0
00172    uint8_t qos : 2;
00173    uint8_t dup : 1;
00174    uint8_t type : 4;
00175 #endif
00176    uint8_t length[];   //1
00177 } __end_packed MqttPacketHeader;
00178 
00179 
00180 /**
00181  * @brief UTF-8 encoded string
00182  **/
00183 
00184 typedef __start_packed struct
00185 {
00186    uint16_t length; //0-1
00187    uint8_t data[];  //2
00188 } __end_packed MqttString;
00189 
00190 
00191 //CodeWarrior or Win32 compiler?
00192 #if defined(__CWCC__) || defined(_WIN32)
00193    #pragma pack(pop)
00194 #endif
00195 
00196 #endif
00197