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