The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Fork of Cayenne-MQTT-mbed by
src/MQTTCommon/MQTTPacket.h@24:686d9ed1d192, 2017-04-25 (annotated)
- Committer:
- pferland
- Date:
- Tue Apr 25 15:32:32 2017 +0000
- Revision:
- 24:686d9ed1d192
- Parent:
- 0:09ef59d2d0f7
- Child:
- 22:0dbabcc6e7b2
Small changes for compatibility with MTSAS socket library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /******************************************************************************* |
jburhenn | 0:09ef59d2d0f7 | 2 | * Copyright (c) 2014 IBM Corp. |
jburhenn | 0:09ef59d2d0f7 | 3 | * |
jburhenn | 0:09ef59d2d0f7 | 4 | * All rights reserved. This program and the accompanying materials |
jburhenn | 0:09ef59d2d0f7 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
jburhenn | 0:09ef59d2d0f7 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
jburhenn | 0:09ef59d2d0f7 | 7 | * |
jburhenn | 0:09ef59d2d0f7 | 8 | * The Eclipse Public License is available at |
jburhenn | 0:09ef59d2d0f7 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
jburhenn | 0:09ef59d2d0f7 | 10 | * and the Eclipse Distribution License is available at |
jburhenn | 0:09ef59d2d0f7 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
jburhenn | 0:09ef59d2d0f7 | 12 | * |
jburhenn | 0:09ef59d2d0f7 | 13 | * Contributors: |
jburhenn | 0:09ef59d2d0f7 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
jburhenn | 0:09ef59d2d0f7 | 15 | * Xiang Rong - 442039 Add makefile to Embedded C client |
jburhenn | 0:09ef59d2d0f7 | 16 | *******************************************************************************/ |
jburhenn | 0:09ef59d2d0f7 | 17 | |
jburhenn | 0:09ef59d2d0f7 | 18 | #ifndef MQTTPACKET_H_ |
jburhenn | 0:09ef59d2d0f7 | 19 | #define MQTTPACKET_H_ |
jburhenn | 0:09ef59d2d0f7 | 20 | |
jburhenn | 0:09ef59d2d0f7 | 21 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ |
jburhenn | 0:09ef59d2d0f7 | 22 | extern "C" { |
jburhenn | 0:09ef59d2d0f7 | 23 | #endif |
jburhenn | 0:09ef59d2d0f7 | 24 | |
jburhenn | 0:09ef59d2d0f7 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) |
jburhenn | 0:09ef59d2d0f7 | 26 | #define DLLImport __declspec(dllimport) |
jburhenn | 0:09ef59d2d0f7 | 27 | #define DLLExport __declspec(dllexport) |
jburhenn | 0:09ef59d2d0f7 | 28 | #elif defined(LINUX_SO) |
jburhenn | 0:09ef59d2d0f7 | 29 | #define DLLImport extern |
jburhenn | 0:09ef59d2d0f7 | 30 | #define DLLExport __attribute__ ((visibility ("default"))) |
jburhenn | 0:09ef59d2d0f7 | 31 | #else |
jburhenn | 0:09ef59d2d0f7 | 32 | #define DLLImport |
jburhenn | 0:09ef59d2d0f7 | 33 | #define DLLExport |
jburhenn | 0:09ef59d2d0f7 | 34 | #endif |
jburhenn | 0:09ef59d2d0f7 | 35 | |
jburhenn | 0:09ef59d2d0f7 | 36 | enum errors |
jburhenn | 0:09ef59d2d0f7 | 37 | { |
jburhenn | 0:09ef59d2d0f7 | 38 | MQTTPACKET_BUFFER_TOO_SHORT = -2, |
jburhenn | 0:09ef59d2d0f7 | 39 | MQTTPACKET_READ_ERROR = -1, |
jburhenn | 0:09ef59d2d0f7 | 40 | MQTTPACKET_READ_COMPLETE |
jburhenn | 0:09ef59d2d0f7 | 41 | }; |
jburhenn | 0:09ef59d2d0f7 | 42 | |
jburhenn | 0:09ef59d2d0f7 | 43 | enum msgTypes |
jburhenn | 0:09ef59d2d0f7 | 44 | { |
jburhenn | 0:09ef59d2d0f7 | 45 | CONNECT_MSG = 1, CONNACK_MSG, PUBLISH_MSG, PUBACK_MSG, PUBREC_MSG, PUBREL_MSG, |
jburhenn | 0:09ef59d2d0f7 | 46 | PUBCOMP_MSG, SUBSCRIBE_MSG, SUBACK_MSG, UNSUBSCRIBE_MSG, UNSUBACK_MSG, |
jburhenn | 0:09ef59d2d0f7 | 47 | PINGREQ_MSG, PINGRESP_MSG, DISCONNECT_MSG |
jburhenn | 0:09ef59d2d0f7 | 48 | }; |
jburhenn | 0:09ef59d2d0f7 | 49 | |
jburhenn | 0:09ef59d2d0f7 | 50 | /** |
jburhenn | 0:09ef59d2d0f7 | 51 | * Bitfields for the MQTT header byte. |
jburhenn | 0:09ef59d2d0f7 | 52 | */ |
jburhenn | 0:09ef59d2d0f7 | 53 | typedef union |
jburhenn | 0:09ef59d2d0f7 | 54 | { |
jburhenn | 0:09ef59d2d0f7 | 55 | unsigned char byte; /**< the whole byte */ |
jburhenn | 0:09ef59d2d0f7 | 56 | #if defined(REVERSED) |
jburhenn | 0:09ef59d2d0f7 | 57 | struct |
jburhenn | 0:09ef59d2d0f7 | 58 | { |
jburhenn | 0:09ef59d2d0f7 | 59 | unsigned int type : 4; /**< message type nibble */ |
jburhenn | 0:09ef59d2d0f7 | 60 | unsigned int dup : 1; /**< DUP flag bit */ |
jburhenn | 0:09ef59d2d0f7 | 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
jburhenn | 0:09ef59d2d0f7 | 62 | unsigned int retain : 1; /**< retained flag bit */ |
jburhenn | 0:09ef59d2d0f7 | 63 | } bits; |
jburhenn | 0:09ef59d2d0f7 | 64 | #else |
jburhenn | 0:09ef59d2d0f7 | 65 | struct |
jburhenn | 0:09ef59d2d0f7 | 66 | { |
jburhenn | 0:09ef59d2d0f7 | 67 | unsigned int retain : 1; /**< retained flag bit */ |
jburhenn | 0:09ef59d2d0f7 | 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ |
jburhenn | 0:09ef59d2d0f7 | 69 | unsigned int dup : 1; /**< DUP flag bit */ |
jburhenn | 0:09ef59d2d0f7 | 70 | unsigned int type : 4; /**< message type nibble */ |
jburhenn | 0:09ef59d2d0f7 | 71 | } bits; |
jburhenn | 0:09ef59d2d0f7 | 72 | #endif |
jburhenn | 0:09ef59d2d0f7 | 73 | } MQTTHeader; |
jburhenn | 0:09ef59d2d0f7 | 74 | |
jburhenn | 0:09ef59d2d0f7 | 75 | typedef struct |
jburhenn | 0:09ef59d2d0f7 | 76 | { |
jburhenn | 0:09ef59d2d0f7 | 77 | int len; |
jburhenn | 0:09ef59d2d0f7 | 78 | char* data; |
jburhenn | 0:09ef59d2d0f7 | 79 | } MQTTLenString; |
jburhenn | 0:09ef59d2d0f7 | 80 | |
jburhenn | 0:09ef59d2d0f7 | 81 | typedef struct |
jburhenn | 0:09ef59d2d0f7 | 82 | { |
jburhenn | 0:09ef59d2d0f7 | 83 | char* cstring; |
jburhenn | 0:09ef59d2d0f7 | 84 | MQTTLenString lenstring; |
jburhenn | 0:09ef59d2d0f7 | 85 | } MQTTString; |
jburhenn | 0:09ef59d2d0f7 | 86 | |
jburhenn | 0:09ef59d2d0f7 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} |
jburhenn | 0:09ef59d2d0f7 | 88 | |
jburhenn | 0:09ef59d2d0f7 | 89 | int MQTTstrlen(MQTTString mqttstring); |
jburhenn | 0:09ef59d2d0f7 | 90 | |
jburhenn | 0:09ef59d2d0f7 | 91 | #include "MQTTConnect.h" |
jburhenn | 0:09ef59d2d0f7 | 92 | #include "MQTTPublish.h" |
jburhenn | 0:09ef59d2d0f7 | 93 | #include "MQTTSubscribe.h" |
jburhenn | 0:09ef59d2d0f7 | 94 | #include "MQTTUnsubscribe.h" |
jburhenn | 0:09ef59d2d0f7 | 95 | |
jburhenn | 0:09ef59d2d0f7 | 96 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); |
jburhenn | 0:09ef59d2d0f7 | 97 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); |
jburhenn | 0:09ef59d2d0f7 | 98 | |
jburhenn | 0:09ef59d2d0f7 | 99 | int MQTTPacket_len(int rem_len); |
jburhenn | 0:09ef59d2d0f7 | 100 | int MQTTPacket_equals(MQTTString* a, char* b); |
jburhenn | 0:09ef59d2d0f7 | 101 | |
jburhenn | 0:09ef59d2d0f7 | 102 | int MQTTPacket_encode(unsigned char* buf, int length); |
jburhenn | 0:09ef59d2d0f7 | 103 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); |
jburhenn | 0:09ef59d2d0f7 | 104 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); |
jburhenn | 0:09ef59d2d0f7 | 105 | |
jburhenn | 0:09ef59d2d0f7 | 106 | int readInt(unsigned char** pptr); |
jburhenn | 0:09ef59d2d0f7 | 107 | char readChar(unsigned char** pptr); |
jburhenn | 0:09ef59d2d0f7 | 108 | void writeChar(unsigned char** pptr, char c); |
jburhenn | 0:09ef59d2d0f7 | 109 | void writeInt(unsigned char** pptr, int anInt); |
jburhenn | 0:09ef59d2d0f7 | 110 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); |
jburhenn | 0:09ef59d2d0f7 | 111 | void writeCString(unsigned char** pptr, const char* string); |
jburhenn | 0:09ef59d2d0f7 | 112 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); |
jburhenn | 0:09ef59d2d0f7 | 113 | |
jburhenn | 0:09ef59d2d0f7 | 114 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); |
jburhenn | 0:09ef59d2d0f7 | 115 | |
jburhenn | 0:09ef59d2d0f7 | 116 | typedef struct { |
jburhenn | 0:09ef59d2d0f7 | 117 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ |
jburhenn | 0:09ef59d2d0f7 | 118 | void *sck; /* pointer to whatever the system may use to identify the transport */ |
jburhenn | 0:09ef59d2d0f7 | 119 | int multiplier; |
jburhenn | 0:09ef59d2d0f7 | 120 | int rem_len; |
jburhenn | 0:09ef59d2d0f7 | 121 | int len; |
jburhenn | 0:09ef59d2d0f7 | 122 | char state; |
jburhenn | 0:09ef59d2d0f7 | 123 | }MQTTTransport; |
jburhenn | 0:09ef59d2d0f7 | 124 | |
jburhenn | 0:09ef59d2d0f7 | 125 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); |
jburhenn | 0:09ef59d2d0f7 | 126 | |
jburhenn | 0:09ef59d2d0f7 | 127 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ |
jburhenn | 0:09ef59d2d0f7 | 128 | } |
jburhenn | 0:09ef59d2d0f7 | 129 | #endif |
jburhenn | 0:09ef59d2d0f7 | 130 | |
jburhenn | 0:09ef59d2d0f7 | 131 | |
jburhenn | 0:09ef59d2d0f7 | 132 | #endif /* MQTTPACKET_H_ */ |