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 MQTTPACKET_BUFFER_TOO_SHORT = -2, 38 MQTTPACKET_READ_ERROR = -1, 39 MQTTPACKET_READ_COMPLETE 40 }; 41 42 /* CPT, control packet type */ 43 enum msgTypes { 44 MQTT_CPT_RESERVED = 0, 45 CONNECT = 1, 46 CONNACK, 47 PUBLISH, 48 PUBACK, 49 PUBREC, 50 PUBREL, 51 PUBCOMP, 52 SUBSCRIBE, 53 SUBACK, 54 UNSUBSCRIBE, 55 UNSUBACK, 56 PINGREQ, 57 PINGRESP, 58 DISCONNECT 59 }; 60 61 #define MQTT_HEADER_BIT_MASK_TYPE (0xF0) 62 #define MQTT_HEADER_BIT_MASK_DUP (0x08) 63 #define MQTT_HEADER_BIT_MASK_QOS (0x06) 64 #define MQTT_HEADER_BIT_MASK_RETAIN (0x01) 65 66 #define MQTT_HEADER_GET_TYPE(head) ((head & 0xF0) >> 4) 67 #define MQTT_HEADER_GET_DUP(head) ((head & 0x08) >> 3) 68 #define MQTT_HEADER_GET_QOS(head) ((head & 0x06) >> 1) 69 #define MQTT_HEADER_GET_RETAIN(head) (head & 0x01) 70 71 #define MQTT_HEADER_SET_TYPE(head, type) \ 72 do { \ 73 head |= ((type << 4) & 0xF0); \ 74 } while (0) 75 #define MQTT_HEADER_SET_DUP(head, dup) \ 76 do { \ 77 head |= ((dup << 3) & 0x08); \ 78 } while (0) 79 #define MQTT_HEADER_SET_QOS(head, qos) \ 80 do { \ 81 head |= ((qos << 1) & 0x06); \ 82 } while (0) 83 #define MQTT_HEADER_SET_RETAIN(head, retain) \ 84 do { \ 85 head |= (retain & 0x01); \ 86 } while (0) 87 88 /** 89 * Bitfields for the MQTT header byte. 90 */ 91 typedef union { 92 unsigned char byte; /**< the whole byte */ 93 } MQTTHeader; 94 95 typedef struct { 96 int len; 97 char *data; 98 } MQTTLenString; 99 100 typedef struct { 101 char *cstring; 102 MQTTLenString lenstring; 103 } MQTTString; 104 105 #define MQTTString_initializer \ 106 { \ 107 NULL, { 0, NULL } \ 108 } 109 110 int MQTTstrlen(MQTTString mqttstring); 111 112 #include "MQTTConnect.h" 113 #include "MQTTPublish.h" 114 #include "MQTTSubscribe.h" 115 #include "MQTTUnsubscribe.h" 116 117 int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char type, 118 unsigned char dup, unsigned short packetid); 119 int MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup, 120 unsigned short *packetid, unsigned char *buf, 121 int buflen); 122 123 int MQTTPacket_len(int rem_len); 124 int MQTTPacket_equals(MQTTString *a, char *b); 125 126 int MQTTPacket_encode(unsigned char *buf, int length); 127 int MQTTPacket_decode(int (*getcharfn)(unsigned char *, int), int *value); 128 int MQTTPacket_decodeBuf(unsigned char *buf, int *value); 129 130 int readInt(unsigned char **pptr); 131 char readChar(unsigned char **pptr); 132 void writeChar(unsigned char **pptr, char c); 133 void writeInt(unsigned char **pptr, int anInt); 134 int readMQTTLenString(MQTTString *mqttstring, unsigned char **pptr, 135 unsigned char *enddata); 136 void writeCString(unsigned char **pptr, const char *string); 137 void writeMQTTString(unsigned char **pptr, MQTTString mqttstring); 138 139 typedef struct { 140 int (*getfn)(void *, unsigned char *, 141 int); /* must return -1 for error, 0 for call again, or the 142 number of bytes read */ 143 void *sck; /* pointer to whatever the system may use to identify the 144 transport */ 145 int multiplier; 146 int rem_len; 147 int len; 148 char state; 149 } MQTTTransport; 150 151 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 152 } 153 #endif 154 155 #endif /* MQTTPACKET_H_ */ 156