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