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