eric liang / MQTTPacket

Dependents:   MQTT

Fork of MQTTPacket by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTPacket.h Source File

MQTTPacket.h

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #ifndef MQTTPACKET_H_
00018 #define MQTTPACKET_H_
00019 
00020 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
00021 extern "C" {
00022 #endif
00023 
00024 #define ADV_MAX_PACKET_SIZE  400
00025 
00026 enum errors
00027 {
00028     MQTTPACKET_BUFFER_TOO_SHORT = -2,
00029     MQTTPACKET_READ_ERROR = -1,
00030     MQTTPACKET_READ_COMPLETE,
00031 };
00032 
00033 enum msgTypes
00034 {
00035     CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
00036     PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
00037     PINGREQ, PINGRESP, DISCONNECT
00038 };
00039 
00040 /**
00041  * Bitfields for the MQTT header byte.
00042  */
00043 typedef union
00044 {
00045     unsigned char byte;                 /**< the whole byte */
00046 #if defined(REVERSED)
00047     struct
00048     {
00049         unsigned int type : 4;          /**< message type nibble */
00050         unsigned int dup : 1;               /**< DUP flag bit */
00051         unsigned int qos : 2;               /**< QoS value, 0, 1 or 2 */
00052         unsigned int retain : 1;        /**< retained flag bit */
00053     } bits;
00054 #else
00055     struct
00056     {
00057         unsigned int retain : 1;        /**< retained flag bit */
00058         unsigned int qos : 2;               /**< QoS value, 0, 1 or 2 */
00059         unsigned int dup : 1;               /**< DUP flag bit */
00060         unsigned int type : 4;          /**< message type nibble */
00061     } bits;
00062 #endif
00063 } MQTTHeader;
00064 
00065 typedef struct
00066 {
00067     int len;
00068     char* data;
00069 } MQTTLenString;
00070 
00071 typedef struct
00072 {
00073     char* cstring;
00074     MQTTLenString lenstring;
00075 } MQTTString;
00076 
00077 #define MQTTString_initializer {NULL, {0, NULL}}
00078 
00079 int MQTTstrlen(MQTTString mqttstring);
00080 
00081 #include "MQTTConnect.h"
00082 #include "MQTTPublish.h"
00083 #include "MQTTSubscribe.h"
00084 #include "MQTTUnsubscribe.h"
00085 
00086 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
00087 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
00088 
00089 int MQTTPacket_len(int rem_len);
00090 int MQTTPacket_equals(MQTTString* a, char* b);
00091 
00092 int MQTTPacket_encode(unsigned char* buf, int length);
00093 int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
00094 int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
00095 
00096 int readInt(unsigned char** pptr);
00097 char readChar(unsigned char** pptr);
00098 void writeChar(unsigned char** pptr, char c);
00099 void writeInt(unsigned char** pptr, int anInt);
00100 int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
00101 void writeCString(unsigned char** pptr, const char* string);
00102 void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
00103 
00104 int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
00105 
00106 char* MQTTPacket_toString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
00107 
00108 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
00109 }
00110 #endif
00111 
00112 
00113 #endif /* MQTTPACKET_H_ */