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