Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
MQTTSManager/MQTTPacket/MQTTPacket.h@0:a1734fe1ec4b, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:43:14 2017 +0000
- Revision:
- 0:a1734fe1ec4b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:a1734fe1ec4b | 1 | /******************************************************************************* |
vpcola | 0:a1734fe1ec4b | 2 | * Copyright (c) 2014 IBM Corp. |
vpcola | 0:a1734fe1ec4b | 3 | * |
vpcola | 0:a1734fe1ec4b | 4 | * All rights reserved. This program and the accompanying materials |
vpcola | 0:a1734fe1ec4b | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
vpcola | 0:a1734fe1ec4b | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
vpcola | 0:a1734fe1ec4b | 7 | * |
vpcola | 0:a1734fe1ec4b | 8 | * The Eclipse Public License is available at |
vpcola | 0:a1734fe1ec4b | 9 | * http://www.eclipse.org/legal/epl-v10.html |
vpcola | 0:a1734fe1ec4b | 10 | * and the Eclipse Distribution License is available at |
vpcola | 0:a1734fe1ec4b | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
vpcola | 0:a1734fe1ec4b | 12 | * |
vpcola | 0:a1734fe1ec4b | 13 | * Contributors: |
vpcola | 0:a1734fe1ec4b | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
vpcola | 0:a1734fe1ec4b | 15 | *******************************************************************************/ |
vpcola | 0:a1734fe1ec4b | 16 | |
vpcola | 0:a1734fe1ec4b | 17 | #ifndef MQTTPACKET_H_ |
vpcola | 0:a1734fe1ec4b | 18 | #define MQTTPACKET_H_ |
vpcola | 0:a1734fe1ec4b | 19 | |
vpcola | 0:a1734fe1ec4b | 20 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ |
vpcola | 0:a1734fe1ec4b | 21 | extern "C" { |
vpcola | 0:a1734fe1ec4b | 22 | #endif |
vpcola | 0:a1734fe1ec4b | 23 | |
vpcola | 0:a1734fe1ec4b | 24 | enum errors |
vpcola | 0:a1734fe1ec4b | 25 | { |
vpcola | 0:a1734fe1ec4b | 26 | MQTTPACKET_BUFFER_TOO_SHORT = -2, |
vpcola | 0:a1734fe1ec4b | 27 | MQTTPACKET_READ_ERROR = -1, |
vpcola | 0:a1734fe1ec4b | 28 | MQTTPACKET_READ_COMPLETE, |
vpcola | 0:a1734fe1ec4b | 29 | }; |
vpcola | 0:a1734fe1ec4b | 30 | |
vpcola | 0:a1734fe1ec4b | 31 | enum msgTypes |
vpcola | 0:a1734fe1ec4b | 32 | { |
vpcola | 0:a1734fe1ec4b | 33 | CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, |
vpcola | 0:a1734fe1ec4b | 34 | PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, |
vpcola | 0:a1734fe1ec4b | 35 | PINGREQ, PINGRESP, DISCONNECT |
vpcola | 0:a1734fe1ec4b | 36 | }; |
vpcola | 0:a1734fe1ec4b | 37 | |
vpcola | 0:a1734fe1ec4b | 38 | /** |
vpcola | 0:a1734fe1ec4b | 39 | * Bitfields for the MQTT header byte. |
vpcola | 0:a1734fe1ec4b | 40 | */ |
vpcola | 0:a1734fe1ec4b | 41 | typedef union |
vpcola | 0:a1734fe1ec4b | 42 | { |
vpcola | 0:a1734fe1ec4b | 43 | unsigned char byte; /**< the whole byte */ |
vpcola | 0:a1734fe1ec4b | 44 | #if defined(REVERSED) |
vpcola | 0:a1734fe1ec4b | 45 | struct |
vpcola | 0:a1734fe1ec4b | 46 | { |
vpcola | 0:a1734fe1ec4b | 47 | unsigned int type : 4; /**< message type nibble */ |
vpcola | 0:a1734fe1ec4b | 48 | unsigned int dup : 1; /**< DUP flag bit */ |
vpcola | 0:a1734fe1ec4b | 49 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
vpcola | 0:a1734fe1ec4b | 50 | unsigned int retain : 1; /**< retained flag bit */ |
vpcola | 0:a1734fe1ec4b | 51 | } bits; |
vpcola | 0:a1734fe1ec4b | 52 | #else |
vpcola | 0:a1734fe1ec4b | 53 | struct |
vpcola | 0:a1734fe1ec4b | 54 | { |
vpcola | 0:a1734fe1ec4b | 55 | unsigned int retain : 1; /**< retained flag bit */ |
vpcola | 0:a1734fe1ec4b | 56 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
vpcola | 0:a1734fe1ec4b | 57 | unsigned int dup : 1; /**< DUP flag bit */ |
vpcola | 0:a1734fe1ec4b | 58 | unsigned int type : 4; /**< message type nibble */ |
vpcola | 0:a1734fe1ec4b | 59 | } bits; |
vpcola | 0:a1734fe1ec4b | 60 | #endif |
vpcola | 0:a1734fe1ec4b | 61 | } MQTTHeader; |
vpcola | 0:a1734fe1ec4b | 62 | |
vpcola | 0:a1734fe1ec4b | 63 | typedef struct |
vpcola | 0:a1734fe1ec4b | 64 | { |
vpcola | 0:a1734fe1ec4b | 65 | int len; |
vpcola | 0:a1734fe1ec4b | 66 | char* data; |
vpcola | 0:a1734fe1ec4b | 67 | } MQTTLenString; |
vpcola | 0:a1734fe1ec4b | 68 | |
vpcola | 0:a1734fe1ec4b | 69 | typedef struct |
vpcola | 0:a1734fe1ec4b | 70 | { |
vpcola | 0:a1734fe1ec4b | 71 | char* cstring; |
vpcola | 0:a1734fe1ec4b | 72 | MQTTLenString lenstring; |
vpcola | 0:a1734fe1ec4b | 73 | } MQTTString; |
vpcola | 0:a1734fe1ec4b | 74 | |
vpcola | 0:a1734fe1ec4b | 75 | #define MQTTString_initializer {NULL, {0, NULL}} |
vpcola | 0:a1734fe1ec4b | 76 | |
vpcola | 0:a1734fe1ec4b | 77 | int MQTTstrlen(MQTTString mqttstring); |
vpcola | 0:a1734fe1ec4b | 78 | |
vpcola | 0:a1734fe1ec4b | 79 | #include "MQTTConnect.h" |
vpcola | 0:a1734fe1ec4b | 80 | #include "MQTTPublish.h" |
vpcola | 0:a1734fe1ec4b | 81 | #include "MQTTSubscribe.h" |
vpcola | 0:a1734fe1ec4b | 82 | #include "MQTTUnsubscribe.h" |
vpcola | 0:a1734fe1ec4b | 83 | |
vpcola | 0:a1734fe1ec4b | 84 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); |
vpcola | 0:a1734fe1ec4b | 85 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); |
vpcola | 0:a1734fe1ec4b | 86 | |
vpcola | 0:a1734fe1ec4b | 87 | int MQTTPacket_len(int rem_len); |
vpcola | 0:a1734fe1ec4b | 88 | int MQTTPacket_equals(MQTTString* a, char* b); |
vpcola | 0:a1734fe1ec4b | 89 | |
vpcola | 0:a1734fe1ec4b | 90 | int MQTTPacket_encode(unsigned char* buf, int length); |
vpcola | 0:a1734fe1ec4b | 91 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); |
vpcola | 0:a1734fe1ec4b | 92 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); |
vpcola | 0:a1734fe1ec4b | 93 | |
vpcola | 0:a1734fe1ec4b | 94 | int readInt(unsigned char** pptr); |
vpcola | 0:a1734fe1ec4b | 95 | char readChar(unsigned char** pptr); |
vpcola | 0:a1734fe1ec4b | 96 | void writeChar(unsigned char** pptr, char c); |
vpcola | 0:a1734fe1ec4b | 97 | void writeInt(unsigned char** pptr, int anInt); |
vpcola | 0:a1734fe1ec4b | 98 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); |
vpcola | 0:a1734fe1ec4b | 99 | void writeCString(unsigned char** pptr, const char* string); |
vpcola | 0:a1734fe1ec4b | 100 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); |
vpcola | 0:a1734fe1ec4b | 101 | |
vpcola | 0:a1734fe1ec4b | 102 | int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); |
vpcola | 0:a1734fe1ec4b | 103 | |
vpcola | 0:a1734fe1ec4b | 104 | char* MQTTPacket_toString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); |
vpcola | 0:a1734fe1ec4b | 105 | |
vpcola | 0:a1734fe1ec4b | 106 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ |
vpcola | 0:a1734fe1ec4b | 107 | } |
vpcola | 0:a1734fe1ec4b | 108 | #endif |
vpcola | 0:a1734fe1ec4b | 109 | |
vpcola | 0:a1734fe1ec4b | 110 | |
vpcola | 0:a1734fe1ec4b | 111 | #endif /* MQTTPACKET_H_ */ |