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 MQTTPACKET_H_ 19 #define MQTTPACKET_H_ 20 21 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 22 extern "C" { 23 #endif 24 25 #if defined(WIN32_DLL) || defined(WIN64_DLL) 26 #define DLLImport __declspec(dllimport) 27 #define DLLExport __declspec(dllexport) 28 #elif defined(LINUX_SO) 29 #define DLLImport extern 30 #define DLLExport __attribute__ ((visibility ("default"))) 31 #else 32 #define DLLImport 33 #define DLLExport 34 #endif 35 36 enum errors 37 { 38 MQTTPACKET_BUFFER_TOO_SHORT = -2, 39 MQTTPACKET_READ_ERROR = -1, 40 MQTTPACKET_READ_COMPLETE 41 }; 42 43 enum msgTypes 44 { 45 CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, 46 PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, 47 PINGREQ, PINGRESP, DISCONNECT 48 }; 49 50 /** 51 * Bitfields for the MQTT header byte. 52 */ 53 typedef union 54 { 55 unsigned char byte; /**< the whole byte */ 56 #if defined(REVERSED) 57 struct 58 { 59 unsigned int type : 4; /**< message type nibble */ 60 unsigned int dup : 1; /**< DUP flag bit */ 61 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 62 unsigned int retain : 1; /**< retained flag bit */ 63 } bits; 64 #else 65 struct 66 { 67 unsigned int retain : 1; /**< retained flag bit */ 68 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 69 unsigned int dup : 1; /**< DUP flag bit */ 70 unsigned int type : 4; /**< message type nibble */ 71 } bits; 72 #endif 73 } MQTTHeader; 74 75 typedef struct 76 { 77 int len; 78 char* data; 79 } MQTTLenString; 80 81 typedef struct 82 { 83 char* cstring; 84 MQTTLenString lenstring; 85 } MQTTString; 86 87 #define MQTTString_initializer {NULL, {0, NULL}} 88 89 int MQTTstrlen(MQTTString mqttstring); 90 91 #include "MQTTConnect.h" 92 #include "MQTTPublish.h" 93 #include "MQTTSubscribe.h" 94 #include "MQTTUnsubscribe.h" 95 #include "MQTTFormat.h" 96 97 DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 98 DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 99 100 int MQTTPacket_len(int rem_len); 101 DLLExport int MQTTPacket_equals(MQTTString* a, char* b); 102 103 DLLExport int MQTTPacket_encode(unsigned char* buf, int length); 104 int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 105 int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 106 107 int readInt(unsigned char** pptr); 108 char readChar(unsigned char** pptr); 109 void writeChar(unsigned char** pptr, char c); 110 void writeInt(unsigned char** pptr, int anInt); 111 int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 112 void writeCString(unsigned char** pptr, const char* string); 113 void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 114 115 DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 116 117 typedef struct { 118 int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 119 void *sck; /* pointer to whatever the system may use to identify the transport */ 120 int multiplier; 121 int rem_len; 122 int len; 123 char state; 124 }MQTTTransport; 125 126 int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 127 128 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 129 } 130 #endif 131 132 133 #endif /* MQTTPACKET_H_ */ 134