Low level MQTTSN packet library, part of the Eclipse Paho project: http://eclipse.org/paho
Dependents: MQTTSN sara-n200-hello-mqtt-sn MQTTSN_2
The master source for this project is held at: https://github.com/eclipse/paho.mqtt-sn.embedded-c
MQTTSNPacket.h@0:c524a894b5e8, 2015-02-26 (annotated)
- Committer:
- icraggs
- Date:
- Thu Feb 26 15:59:36 2015 +0000
- Revision:
- 0:c524a894b5e8
- Child:
- 1:7fa362fa563f
First version that works nicely :-)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 0:c524a894b5e8 | 1 | /******************************************************************************* |
icraggs | 0:c524a894b5e8 | 2 | * Copyright (c) 2014 IBM Corp. |
icraggs | 0:c524a894b5e8 | 3 | * |
icraggs | 0:c524a894b5e8 | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 0:c524a894b5e8 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 0:c524a894b5e8 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
icraggs | 0:c524a894b5e8 | 7 | * |
icraggs | 0:c524a894b5e8 | 8 | * The Eclipse Public License is available at |
icraggs | 0:c524a894b5e8 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 0:c524a894b5e8 | 10 | * and the Eclipse Distribution License is available at |
icraggs | 0:c524a894b5e8 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
icraggs | 0:c524a894b5e8 | 12 | * |
icraggs | 0:c524a894b5e8 | 13 | * Contributors: |
icraggs | 0:c524a894b5e8 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 0:c524a894b5e8 | 15 | *******************************************************************************/ |
icraggs | 0:c524a894b5e8 | 16 | |
icraggs | 0:c524a894b5e8 | 17 | #ifndef MQTTSNPACKET_H_ |
icraggs | 0:c524a894b5e8 | 18 | #define MQTTSNPACKET_H_ |
icraggs | 0:c524a894b5e8 | 19 | |
icraggs | 0:c524a894b5e8 | 20 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ |
icraggs | 0:c524a894b5e8 | 21 | extern "C" { |
icraggs | 0:c524a894b5e8 | 22 | #endif |
icraggs | 0:c524a894b5e8 | 23 | |
icraggs | 0:c524a894b5e8 | 24 | enum errors |
icraggs | 0:c524a894b5e8 | 25 | { |
icraggs | 0:c524a894b5e8 | 26 | MQTTSNPACKET_BUFFER_TOO_SHORT = -2, |
icraggs | 0:c524a894b5e8 | 27 | MQTTSNPACKET_READ_ERROR = -1, |
icraggs | 0:c524a894b5e8 | 28 | MQTTSNPACKET_READ_COMPLETE, |
icraggs | 0:c524a894b5e8 | 29 | }; |
icraggs | 0:c524a894b5e8 | 30 | |
icraggs | 0:c524a894b5e8 | 31 | #define MQTTSN_PROTOCOL_VERSION 0x01 |
icraggs | 0:c524a894b5e8 | 32 | |
icraggs | 0:c524a894b5e8 | 33 | enum MQTTSN_connackCodes |
icraggs | 0:c524a894b5e8 | 34 | { |
icraggs | 0:c524a894b5e8 | 35 | MQTTSN_RC_ACCEPTED, |
icraggs | 0:c524a894b5e8 | 36 | MQTTSN_RC_REJECTED_CONGESTED, |
icraggs | 0:c524a894b5e8 | 37 | MQTTSN_RC_REJECTED_INVALID_TOPIC_ID, |
icraggs | 0:c524a894b5e8 | 38 | }; |
icraggs | 0:c524a894b5e8 | 39 | |
icraggs | 0:c524a894b5e8 | 40 | enum MQTTSN_topicTypes |
icraggs | 0:c524a894b5e8 | 41 | { |
icraggs | 0:c524a894b5e8 | 42 | MQTTSN_TOPIC_TYPE_NORMAL, /* topic id in publish, topic name in subscribe */ |
icraggs | 0:c524a894b5e8 | 43 | MQTTSN_TOPIC_TYPE_PREDEFINED, |
icraggs | 0:c524a894b5e8 | 44 | MQTTSN_TOPIC_TYPE_SHORT, |
icraggs | 0:c524a894b5e8 | 45 | }; |
icraggs | 0:c524a894b5e8 | 46 | |
icraggs | 0:c524a894b5e8 | 47 | |
icraggs | 0:c524a894b5e8 | 48 | enum MQTTSN_msgTypes |
icraggs | 0:c524a894b5e8 | 49 | { |
icraggs | 0:c524a894b5e8 | 50 | MQTTSN_ADVERTISE, MQTTSN_SEARCHGW, MQTTSN_GWINFO, MQTTSN_RESERVED1, |
icraggs | 0:c524a894b5e8 | 51 | MQTTSN_CONNECT, MQTTSN_CONNACK, |
icraggs | 0:c524a894b5e8 | 52 | MQTTSN_WILLTOPICREQ, MQTTSN_WILLTOPIC, MQTTSN_WILLMSGREQ, MQTTSN_WILLMSG, |
icraggs | 0:c524a894b5e8 | 53 | MQTTSN_REGISTER, MQTTSN_REGACK, |
icraggs | 0:c524a894b5e8 | 54 | MQTTSN_PUBLISH, MQTTSN_PUBACK, MQTTSN_PUBCOMP, MQTTSN_PUBREC, MQTTSN_PUBREL, MQTTSN_RESERVED2, |
icraggs | 0:c524a894b5e8 | 55 | MQTTSN_SUBSCRIBE, MQTTSN_SUBACK, MQTTSN_UNSUBSCRIBE, MQTTSN_UNSUBACK, |
icraggs | 0:c524a894b5e8 | 56 | MQTTSN_PINGREQ, MQTTSN_PINGRESP, |
icraggs | 0:c524a894b5e8 | 57 | MQTTSN_DISCONNECT, MQTTSN_RESERVED3, |
icraggs | 0:c524a894b5e8 | 58 | MQTTSN_WILLTOPICUPD, MQTTSN_WILLTOPICRESP, MQTTSN_WILLMSGUPD, MQTTSN_WILLMSGRESP, |
icraggs | 0:c524a894b5e8 | 59 | }; |
icraggs | 0:c524a894b5e8 | 60 | |
icraggs | 0:c524a894b5e8 | 61 | typedef struct |
icraggs | 0:c524a894b5e8 | 62 | { |
icraggs | 0:c524a894b5e8 | 63 | enum MQTTSN_topicTypes type; |
icraggs | 0:c524a894b5e8 | 64 | union |
icraggs | 0:c524a894b5e8 | 65 | { |
icraggs | 0:c524a894b5e8 | 66 | unsigned short id; |
icraggs | 0:c524a894b5e8 | 67 | char short_name[2]; |
icraggs | 0:c524a894b5e8 | 68 | struct |
icraggs | 0:c524a894b5e8 | 69 | { |
icraggs | 0:c524a894b5e8 | 70 | char* name; |
icraggs | 0:c524a894b5e8 | 71 | int len; |
icraggs | 0:c524a894b5e8 | 72 | } long_; |
icraggs | 0:c524a894b5e8 | 73 | } data; |
icraggs | 0:c524a894b5e8 | 74 | } MQTTSN_topicid; |
icraggs | 0:c524a894b5e8 | 75 | |
icraggs | 0:c524a894b5e8 | 76 | |
icraggs | 0:c524a894b5e8 | 77 | /** |
icraggs | 0:c524a894b5e8 | 78 | * Bitfields for the MQTT-SN flags byte. |
icraggs | 0:c524a894b5e8 | 79 | */ |
icraggs | 0:c524a894b5e8 | 80 | typedef union |
icraggs | 0:c524a894b5e8 | 81 | { |
icraggs | 0:c524a894b5e8 | 82 | unsigned char all; |
icraggs | 0:c524a894b5e8 | 83 | #if defined(REVERSED) |
icraggs | 0:c524a894b5e8 | 84 | struct |
icraggs | 0:c524a894b5e8 | 85 | { |
icraggs | 0:c524a894b5e8 | 86 | int dup: 1; |
icraggs | 0:c524a894b5e8 | 87 | unsigned int QoS : 2; |
icraggs | 0:c524a894b5e8 | 88 | unsigned int retain : 1; |
icraggs | 0:c524a894b5e8 | 89 | unsigned int will : 1; |
icraggs | 0:c524a894b5e8 | 90 | unsigned int cleanSession : 1; |
icraggs | 0:c524a894b5e8 | 91 | unsigned int topicIdType : 2; |
icraggs | 0:c524a894b5e8 | 92 | } bits; |
icraggs | 0:c524a894b5e8 | 93 | #else |
icraggs | 0:c524a894b5e8 | 94 | struct |
icraggs | 0:c524a894b5e8 | 95 | { |
icraggs | 0:c524a894b5e8 | 96 | unsigned int topicIdType : 2; |
icraggs | 0:c524a894b5e8 | 97 | unsigned int cleanSession : 1; |
icraggs | 0:c524a894b5e8 | 98 | unsigned int will : 1; |
icraggs | 0:c524a894b5e8 | 99 | unsigned int retain : 1; |
icraggs | 0:c524a894b5e8 | 100 | unsigned int QoS : 2; |
icraggs | 0:c524a894b5e8 | 101 | int dup: 1; |
icraggs | 0:c524a894b5e8 | 102 | } bits; |
icraggs | 0:c524a894b5e8 | 103 | #endif |
icraggs | 0:c524a894b5e8 | 104 | } MQTTSNFlags; |
icraggs | 0:c524a894b5e8 | 105 | |
icraggs | 0:c524a894b5e8 | 106 | |
icraggs | 0:c524a894b5e8 | 107 | typedef struct |
icraggs | 0:c524a894b5e8 | 108 | { |
icraggs | 0:c524a894b5e8 | 109 | int len; |
icraggs | 0:c524a894b5e8 | 110 | char* data; |
icraggs | 0:c524a894b5e8 | 111 | } MQTTSNLenString; |
icraggs | 0:c524a894b5e8 | 112 | |
icraggs | 0:c524a894b5e8 | 113 | typedef struct |
icraggs | 0:c524a894b5e8 | 114 | { |
icraggs | 0:c524a894b5e8 | 115 | char* cstring; |
icraggs | 0:c524a894b5e8 | 116 | MQTTSNLenString lenstring; |
icraggs | 0:c524a894b5e8 | 117 | } MQTTSNString; |
icraggs | 0:c524a894b5e8 | 118 | |
icraggs | 0:c524a894b5e8 | 119 | #define MQTTSNString_initializer {NULL, {0, NULL}} |
icraggs | 0:c524a894b5e8 | 120 | |
icraggs | 0:c524a894b5e8 | 121 | int MQTTSNstrlen(MQTTSNString mqttsnstring); |
icraggs | 0:c524a894b5e8 | 122 | |
icraggs | 0:c524a894b5e8 | 123 | #include "MQTTSNConnect.h" |
icraggs | 0:c524a894b5e8 | 124 | #include "MQTTSNPublish.h" |
icraggs | 0:c524a894b5e8 | 125 | #include "MQTTSNSubscribe.h" |
icraggs | 0:c524a894b5e8 | 126 | #include "MQTTSNUnsubscribe.h" |
icraggs | 0:c524a894b5e8 | 127 | #include "MQTTSNSearch.h" |
icraggs | 0:c524a894b5e8 | 128 | |
icraggs | 0:c524a894b5e8 | 129 | char* MQTTSNPacket_name(int ptype); |
icraggs | 0:c524a894b5e8 | 130 | int MQTTSNPacket_len(int length); |
icraggs | 0:c524a894b5e8 | 131 | |
icraggs | 0:c524a894b5e8 | 132 | int MQTTSNPacket_encode(unsigned char* buf, int length); |
icraggs | 0:c524a894b5e8 | 133 | int MQTTSNPacket_decode(unsigned char* buf, int buflen, int* value); |
icraggs | 0:c524a894b5e8 | 134 | |
icraggs | 0:c524a894b5e8 | 135 | int readInt(unsigned char** pptr); |
icraggs | 0:c524a894b5e8 | 136 | char readChar(unsigned char** pptr); |
icraggs | 0:c524a894b5e8 | 137 | void writeChar(unsigned char** pptr, char c); |
icraggs | 0:c524a894b5e8 | 138 | void writeInt(unsigned char** pptr, int anInt); |
icraggs | 0:c524a894b5e8 | 139 | int readMQTTSNString(MQTTSNString* mqttstring, unsigned char** pptr, unsigned char* enddata); |
icraggs | 0:c524a894b5e8 | 140 | void writeCString(unsigned char** pptr, char* string); |
icraggs | 0:c524a894b5e8 | 141 | void writeMQTTSNString(unsigned char** pptr, MQTTSNString mqttstring); |
icraggs | 0:c524a894b5e8 | 142 | |
icraggs | 0:c524a894b5e8 | 143 | int MQTTSNPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); |
icraggs | 0:c524a894b5e8 | 144 | |
icraggs | 0:c524a894b5e8 | 145 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ |
icraggs | 0:c524a894b5e8 | 146 | } |
icraggs | 0:c524a894b5e8 | 147 | #endif |
icraggs | 0:c524a894b5e8 | 148 | |
icraggs | 0:c524a894b5e8 | 149 | |
icraggs | 0:c524a894b5e8 | 150 | #endif /* MQTTSNPACKET_H_ */ |
icraggs | 0:c524a894b5e8 | 151 |