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