Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSNPacket.h Source File

MQTTSNPacket.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  *    TomoakiiYamaguchi - modify for C++
00016  *******************************************************************************/
00017 
00018 #ifndef MQTTSNPACKET_H_
00019 #define MQTTSNPACKET_H_
00020 
00021 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
00022 extern "C" {
00023 #endif
00024 
00025 enum errors
00026 {
00027     MQTTSNPACKET_BUFFER_TOO_SHORT = -2,
00028     MQTTSNPACKET_READ_ERROR = -1,
00029     MQTTSNPACKET_READ_COMPLETE
00030 };
00031 
00032 #define MQTTSN_PROTOCOL_VERSION 0x01
00033 
00034 enum MQTTSN_connackCodes
00035 {
00036     MQTTSN_RC_ACCEPTED,
00037     MQTTSN_RC_REJECTED_CONGESTED,
00038     MQTTSN_RC_REJECTED_INVALID_TOPIC_ID,
00039     MQTTSN_RC_NOT_SUPPORTED
00040 };
00041 
00042 typedef enum
00043 {
00044     MQTTSN_TOPIC_TYPE_NORMAL, /* topic id in publish, topic name in subscribe */
00045     MQTTSN_TOPIC_TYPE_PREDEFINED,
00046     MQTTSN_TOPIC_TYPE_SHORT
00047 }MQTTSN_topicTypes;
00048 
00049 enum MQTTSN_msgTypes
00050 {
00051     MQTTSN_ADVERTISE, MQTTSN_SEARCHGW, MQTTSN_GWINFO, MQTTSN_RESERVED1,
00052     MQTTSN_CONNECT, MQTTSN_CONNACK,
00053     MQTTSN_WILLTOPICREQ, MQTTSN_WILLTOPIC, MQTTSN_WILLMSGREQ, MQTTSN_WILLMSG, 
00054     MQTTSN_REGISTER, MQTTSN_REGACK,
00055     MQTTSN_PUBLISH, MQTTSN_PUBACK, MQTTSN_PUBCOMP, MQTTSN_PUBREC, MQTTSN_PUBREL, MQTTSN_RESERVED2,
00056     MQTTSN_SUBSCRIBE, MQTTSN_SUBACK, MQTTSN_UNSUBSCRIBE, MQTTSN_UNSUBACK, 
00057     MQTTSN_PINGREQ, MQTTSN_PINGRESP,
00058     MQTTSN_DISCONNECT, MQTTSN_RESERVED3, 
00059     MQTTSN_WILLTOPICUPD, MQTTSN_WILLTOPICRESP, MQTTSN_WILLMSGUPD, MQTTSN_WILLMSGRESP,
00060     MQTTSN_ENCAPSULATED = 0xfe
00061 };
00062 
00063 
00064 typedef struct
00065 {
00066     MQTTSN_topicTypes type;
00067     union
00068     {
00069         unsigned short id;
00070         char short_name[2];
00071         struct
00072         {
00073             char* name;
00074             int len;
00075         } long_;
00076     } data;
00077 } MQTTSN_topicid;
00078 
00079 /**
00080  * Bitfields for the MQTT-SN flags byte.
00081  */
00082 typedef union
00083 {
00084     unsigned char all;
00085 #if defined(REVERSED)
00086     struct
00087     {
00088         int dup: 1;
00089         unsigned int QoS : 2;
00090         unsigned int retain : 1;
00091         unsigned int will : 1;
00092         unsigned int cleanSession : 1;
00093         unsigned int topicIdType : 2;
00094     } bits;
00095 #else
00096     struct
00097     {
00098         unsigned int topicIdType : 2;
00099         unsigned int cleanSession : 1;
00100         unsigned int will : 1;
00101         unsigned int retain : 1;
00102         unsigned int QoS : 2;
00103         int dup: 1;
00104     } bits;
00105 #endif
00106 } MQTTSNFlags;
00107 
00108 
00109 typedef struct
00110 {
00111     int len;
00112     char* data;
00113 } MQTTSNLenString;
00114 
00115 typedef struct
00116 {
00117     char* cstring;
00118     MQTTSNLenString lenstring;
00119 } MQTTSNString;
00120 
00121 #define MQTTSNString_initializer {NULL, {0, NULL}}
00122 
00123 int MQTTSNstrlen(MQTTSNString mqttsnstring);
00124 
00125 #include "MQTTSNConnect.h"
00126 #include "MQTTSNPublish.h"
00127 #include "MQTTSNSubscribe.h"
00128 #include "MQTTSNUnsubscribe.h"
00129 #include "MQTTSNSearch.h"
00130 
00131 const char* MQTTSNPacket_name(int ptype);
00132 int MQTTSNPacket_len(int length);
00133 
00134 int MQTTSNPacket_encode(unsigned char* buf, int length);
00135 int MQTTSNPacket_decode(unsigned char* buf, int buflen, int* value);
00136 int MQTTSNTopic_equals(const MQTTSN_topicid* const a, const MQTTSN_topicid* const b);
00137 
00138 int readInt(unsigned char** pptr);
00139 char readChar(unsigned char** pptr);
00140 void writeChar(unsigned char** pptr, char c);
00141 void writeInt(unsigned char** pptr, int anInt);
00142 int readMQTTSNString(MQTTSNString* mqttstring, unsigned char** pptr, unsigned char* enddata);
00143 void writeCString(unsigned char** pptr, const char* string);
00144 void writeMQTTSNString(unsigned char** pptr, MQTTSNString mqttstring);
00145 
00146 int MQTTSNPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
00147 int MQTTSNPacket_read_nb(unsigned char* buf, int buflen);
00148 
00149 
00150 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
00151 }
00152 #endif
00153 
00154 
00155 #endif /* MQTTSNPACKET_H_ */