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