Basic C library for MQTT packet serialization and deserialization
Dependents: MQTT MQTT MQTT MQTT ... more
Fork of MQTTPacket by
This library is part of the EclipseTM Paho project; specifically the embedded client.
A basic MQTT library in C for packet serialization and deserialization
MQTTPacket.h@25:aedcaf7984d5, 2017-10-30 (annotated)
- Committer:
- Ian Craggs
- Date:
- Mon Oct 30 12:47:11 2017 +0000
- Revision:
- 25:aedcaf7984d5
- Parent:
- 23:7a52009beba1
Merging mine and Jan's changes
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 |
Jan Jongboom |
23:7a52009beba1 | 15 | * Xiang Rong - 442039 Add makefile to Embedded C client |
icraggs | 0:7734401cc1b4 | 16 | *******************************************************************************/ |
icraggs | 0:7734401cc1b4 | 17 | |
icraggs | 0:7734401cc1b4 | 18 | #ifndef MQTTPACKET_H_ |
icraggs | 0:7734401cc1b4 | 19 | #define MQTTPACKET_H_ |
icraggs | 0:7734401cc1b4 | 20 | |
icraggs | 0:7734401cc1b4 | 21 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ |
icraggs | 0:7734401cc1b4 | 22 | extern "C" { |
icraggs | 0:7734401cc1b4 | 23 | #endif |
icraggs | 0:7734401cc1b4 | 24 | |
Jan Jongboom |
23:7a52009beba1 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) |
Jan Jongboom |
23:7a52009beba1 | 26 | #define DLLImport __declspec(dllimport) |
Jan Jongboom |
23:7a52009beba1 | 27 | #define DLLExport __declspec(dllexport) |
Jan Jongboom |
23:7a52009beba1 | 28 | #elif defined(LINUX_SO) |
Jan Jongboom |
23:7a52009beba1 | 29 | #define DLLImport extern |
Jan Jongboom |
23:7a52009beba1 | 30 | #define DLLExport __attribute__ ((visibility ("default"))) |
Jan Jongboom |
23:7a52009beba1 | 31 | #else |
Jan Jongboom |
23:7a52009beba1 | 32 | #define DLLImport |
Jan Jongboom |
23:7a52009beba1 | 33 | #define DLLExport |
Jan Jongboom |
23:7a52009beba1 | 34 | #endif |
Jan Jongboom |
23:7a52009beba1 | 35 | |
icraggs | 0:7734401cc1b4 | 36 | enum errors |
icraggs | 0:7734401cc1b4 | 37 | { |
icraggs | 0:7734401cc1b4 | 38 | MQTTPACKET_BUFFER_TOO_SHORT = -2, |
icraggs | 0:7734401cc1b4 | 39 | MQTTPACKET_READ_ERROR = -1, |
Jan Jongboom |
23:7a52009beba1 | 40 | MQTTPACKET_READ_COMPLETE |
icraggs | 0:7734401cc1b4 | 41 | }; |
icraggs | 0:7734401cc1b4 | 42 | |
icraggs | 0:7734401cc1b4 | 43 | enum msgTypes |
icraggs | 0:7734401cc1b4 | 44 | { |
icraggs | 0:7734401cc1b4 | 45 | CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, |
icraggs | 0:7734401cc1b4 | 46 | PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, |
icraggs | 0:7734401cc1b4 | 47 | PINGREQ, PINGRESP, DISCONNECT |
icraggs | 0:7734401cc1b4 | 48 | }; |
icraggs | 0:7734401cc1b4 | 49 | |
icraggs | 0:7734401cc1b4 | 50 | /** |
icraggs | 0:7734401cc1b4 | 51 | * Bitfields for the MQTT header byte. |
icraggs | 0:7734401cc1b4 | 52 | */ |
icraggs | 0:7734401cc1b4 | 53 | typedef union |
icraggs | 0:7734401cc1b4 | 54 | { |
Ian Craggs |
12:cd99ac9cb25a | 55 | unsigned char byte; /**< the whole byte */ |
icraggs | 0:7734401cc1b4 | 56 | #if defined(REVERSED) |
icraggs | 0:7734401cc1b4 | 57 | struct |
icraggs | 0:7734401cc1b4 | 58 | { |
icraggs | 0:7734401cc1b4 | 59 | unsigned int type : 4; /**< message type nibble */ |
icraggs | 0:7734401cc1b4 | 60 | unsigned int dup : 1; /**< DUP flag bit */ |
icraggs | 0:7734401cc1b4 | 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
icraggs | 0:7734401cc1b4 | 62 | unsigned int retain : 1; /**< retained flag bit */ |
icraggs | 0:7734401cc1b4 | 63 | } bits; |
icraggs | 0:7734401cc1b4 | 64 | #else |
icraggs | 0:7734401cc1b4 | 65 | struct |
icraggs | 0:7734401cc1b4 | 66 | { |
icraggs | 0:7734401cc1b4 | 67 | unsigned int retain : 1; /**< retained flag bit */ |
icraggs | 0:7734401cc1b4 | 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
icraggs | 0:7734401cc1b4 | 69 | unsigned int dup : 1; /**< DUP flag bit */ |
icraggs | 0:7734401cc1b4 | 70 | unsigned int type : 4; /**< message type nibble */ |
icraggs | 0:7734401cc1b4 | 71 | } bits; |
icraggs | 0:7734401cc1b4 | 72 | #endif |
icraggs | 0:7734401cc1b4 | 73 | } MQTTHeader; |
icraggs | 0:7734401cc1b4 | 74 | |
icraggs | 0:7734401cc1b4 | 75 | typedef struct |
icraggs | 0:7734401cc1b4 | 76 | { |
icraggs | 0:7734401cc1b4 | 77 | int len; |
icraggs | 0:7734401cc1b4 | 78 | char* data; |
icraggs | 0:7734401cc1b4 | 79 | } MQTTLenString; |
icraggs | 0:7734401cc1b4 | 80 | |
icraggs | 0:7734401cc1b4 | 81 | typedef struct |
icraggs | 0:7734401cc1b4 | 82 | { |
icraggs | 0:7734401cc1b4 | 83 | char* cstring; |
icraggs | 0:7734401cc1b4 | 84 | MQTTLenString lenstring; |
icraggs | 0:7734401cc1b4 | 85 | } MQTTString; |
icraggs | 0:7734401cc1b4 | 86 | |
icraggs | 0:7734401cc1b4 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} |
icraggs | 0:7734401cc1b4 | 88 | |
icraggs | 0:7734401cc1b4 | 89 | int MQTTstrlen(MQTTString mqttstring); |
icraggs | 0:7734401cc1b4 | 90 | |
icraggs | 0:7734401cc1b4 | 91 | #include "MQTTConnect.h" |
icraggs | 0:7734401cc1b4 | 92 | #include "MQTTPublish.h" |
icraggs | 0:7734401cc1b4 | 93 | #include "MQTTSubscribe.h" |
icraggs | 0:7734401cc1b4 | 94 | #include "MQTTUnsubscribe.h" |
Jan Jongboom |
23:7a52009beba1 | 95 | #include "MQTTFormat.h" |
icraggs | 0:7734401cc1b4 | 96 | |
Jan Jongboom |
23:7a52009beba1 | 97 | DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); |
Jan Jongboom |
23:7a52009beba1 | 98 | DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); |
icraggs | 0:7734401cc1b4 | 99 | |
icraggs | 0:7734401cc1b4 | 100 | int MQTTPacket_len(int rem_len); |
Jan Jongboom |
23:7a52009beba1 | 101 | DLLExport int MQTTPacket_equals(MQTTString* a, char* b); |
icraggs | 0:7734401cc1b4 | 102 | |
Jan Jongboom |
23:7a52009beba1 | 103 | DLLExport int MQTTPacket_encode(unsigned char* buf, int length); |
Ian Craggs |
14:c2052aee81de | 104 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); |
Ian Craggs |
14:c2052aee81de | 105 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); |
icraggs | 0:7734401cc1b4 | 106 | |
Ian Craggs |
14:c2052aee81de | 107 | int readInt(unsigned char** pptr); |
Ian Craggs |
14:c2052aee81de | 108 | char readChar(unsigned char** pptr); |
Ian Craggs |
14:c2052aee81de | 109 | void writeChar(unsigned char** pptr, char c); |
Ian Craggs |
14:c2052aee81de | 110 | void writeInt(unsigned char** pptr, int anInt); |
Ian Craggs |
14:c2052aee81de | 111 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); |
Ian Craggs |
14:c2052aee81de | 112 | void writeCString(unsigned char** pptr, const char* string); |
Ian Craggs |
14:c2052aee81de | 113 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); |
icraggs | 0:7734401cc1b4 | 114 | |
Jan Jongboom |
23:7a52009beba1 | 115 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); |
Ian Craggs |
12:cd99ac9cb25a | 116 | |
Jan Jongboom |
23:7a52009beba1 | 117 | typedef struct { |
Jan Jongboom |
23:7a52009beba1 | 118 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ |
Jan Jongboom |
23:7a52009beba1 | 119 | void *sck; /* pointer to whatever the system may use to identify the transport */ |
Jan Jongboom |
23:7a52009beba1 | 120 | int multiplier; |
Jan Jongboom |
23:7a52009beba1 | 121 | int rem_len; |
Jan Jongboom |
23:7a52009beba1 | 122 | int len; |
Jan Jongboom |
23:7a52009beba1 | 123 | char state; |
Jan Jongboom |
23:7a52009beba1 | 124 | }MQTTTransport; |
Jan Jongboom |
23:7a52009beba1 | 125 | |
Jan Jongboom |
23:7a52009beba1 | 126 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); |
icraggs | 18:bf36e077e7b8 | 127 | |
icraggs | 0:7734401cc1b4 | 128 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ |
icraggs | 0:7734401cc1b4 | 129 | } |
icraggs | 0:7734401cc1b4 | 130 | #endif |
icraggs | 0:7734401cc1b4 | 131 | |
icraggs | 0:7734401cc1b4 | 132 | |
icraggs | 0:7734401cc1b4 | 133 | #endif /* MQTTPACKET_H_ */ |