Analog Devices Wi-Fi Software Reference Manual  Release 1.0.0
MQTTPacket.h
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 
53 typedef union
54 {
55  unsigned char byte;
56 #if defined(REVERSED)
57  struct
58  {
59  unsigned int type : 4;
60  unsigned int dup : 1;
61  unsigned int qos : 2;
62  unsigned int retain : 1;
63  } bits;
64 #else
65  struct
66  {
67  unsigned int retain : 1;
68  unsigned int qos : 2;
69  unsigned int dup : 1;
70  unsigned int type : 4;
71  } bits;
72 #endif
73 } MQTTHeader;
74 
75 typedef struct
76 {
77  int len;
78  char* data;
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 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
98 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 int MQTTPacket_equals(MQTTString* a, char* b);
102 
103 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 int MQTTPacket_Parse(unsigned char* buf, int buflen);
117 
118 typedef struct {
119  int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
120  void *sck; /* pointer to whatever the system may use to identify the transport */
121  int multiplier;
122  int rem_len;
123  int len;
124  char state;
126 
127 int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
128 
129 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
130 }
131 #endif
132 
133 
134 #endif /* MQTTPACKET_H_ */
unsigned char byte
Definition: MQTTPacket.h:55
Definition: MQTTPacket.h:81
Definition: MQTTPacket.h:118
Definition: MQTTPacket.h:75
Definition: MQTTPacket.h:53