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
MQTTSNDeserializePublish.c@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 | #include "StackTrace.h" |
icraggs | 0:c524a894b5e8 | 18 | #include "MQTTSNPacket.h" |
icraggs | 0:c524a894b5e8 | 19 | #include <string.h> |
icraggs | 0:c524a894b5e8 | 20 | |
icraggs | 0:c524a894b5e8 | 21 | #define min(a, b) ((a < b) ? 1 : 0) |
icraggs | 0:c524a894b5e8 | 22 | |
icraggs | 0:c524a894b5e8 | 23 | /** |
icraggs | 0:c524a894b5e8 | 24 | * Deserializes the supplied (wire) buffer into publish data |
icraggs | 0:c524a894b5e8 | 25 | * @param dup returned integer - the MQTT dup flag |
icraggs | 0:c524a894b5e8 | 26 | * @param qos returned integer - the MQTT QoS value |
icraggs | 0:c524a894b5e8 | 27 | * @param retained returned integer - the MQTT retained flag |
icraggs | 0:c524a894b5e8 | 28 | * @param packetid returned integer - the MQTT packet identifier |
icraggs | 0:c524a894b5e8 | 29 | * @param topicName returned MQTTSNString - the MQTT topic in the publish |
icraggs | 0:c524a894b5e8 | 30 | * @param payload returned byte buffer - the MQTT publish payload |
icraggs | 0:c524a894b5e8 | 31 | * @param payloadlen returned integer - the length of the MQTT payload |
icraggs | 0:c524a894b5e8 | 32 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 33 | * @param buflen the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 34 | * @return error code. 1 is success |
icraggs | 0:c524a894b5e8 | 35 | */ |
icraggs | 0:c524a894b5e8 | 36 | int MQTTSNDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTSN_topicid* topic, |
icraggs | 0:c524a894b5e8 | 37 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 38 | { |
icraggs | 0:c524a894b5e8 | 39 | MQTTSNFlags flags; |
icraggs | 0:c524a894b5e8 | 40 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 41 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 42 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 43 | int mylen = 0; |
icraggs | 0:c524a894b5e8 | 44 | |
icraggs | 0:c524a894b5e8 | 45 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 46 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 47 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 48 | if (enddata - curdata > buflen) |
icraggs | 0:c524a894b5e8 | 49 | goto exit; |
icraggs | 0:c524a894b5e8 | 50 | |
icraggs | 0:c524a894b5e8 | 51 | if (readChar(&curdata) != MQTTSN_PUBLISH) |
icraggs | 0:c524a894b5e8 | 52 | goto exit; |
icraggs | 0:c524a894b5e8 | 53 | |
icraggs | 0:c524a894b5e8 | 54 | flags.all = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 55 | *dup = flags.bits.dup; |
icraggs | 0:c524a894b5e8 | 56 | *qos = flags.bits.QoS; |
icraggs | 0:c524a894b5e8 | 57 | *retained = flags.bits.retain; |
icraggs | 0:c524a894b5e8 | 58 | |
icraggs | 0:c524a894b5e8 | 59 | topic->type = flags.bits.topicIdType; |
icraggs | 0:c524a894b5e8 | 60 | if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) |
icraggs | 0:c524a894b5e8 | 61 | { |
icraggs | 0:c524a894b5e8 | 62 | /* special arrangement for long topic names in QoS -1 publishes. The length of the topic is in the topicid field */ |
icraggs | 0:c524a894b5e8 | 63 | topic->data.long_.len = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 64 | } |
icraggs | 0:c524a894b5e8 | 65 | else if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL || topic->type == MQTTSN_TOPIC_TYPE_PREDEFINED) |
icraggs | 0:c524a894b5e8 | 66 | topic->data.id = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 67 | else |
icraggs | 0:c524a894b5e8 | 68 | { |
icraggs | 0:c524a894b5e8 | 69 | topic->data.short_name[0] = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 70 | topic->data.short_name[1] = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 71 | } |
icraggs | 0:c524a894b5e8 | 72 | *packetid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 73 | |
icraggs | 0:c524a894b5e8 | 74 | if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) |
icraggs | 0:c524a894b5e8 | 75 | { |
icraggs | 0:c524a894b5e8 | 76 | topic->data.long_.name = (char*)curdata; |
icraggs | 0:c524a894b5e8 | 77 | curdata += topic->data.long_.len; |
icraggs | 0:c524a894b5e8 | 78 | } |
icraggs | 0:c524a894b5e8 | 79 | |
icraggs | 0:c524a894b5e8 | 80 | *payloadlen = enddata - curdata; |
icraggs | 0:c524a894b5e8 | 81 | *payload = curdata; |
icraggs | 0:c524a894b5e8 | 82 | rc = 1; |
icraggs | 0:c524a894b5e8 | 83 | exit: |
icraggs | 0:c524a894b5e8 | 84 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 85 | return rc; |
icraggs | 0:c524a894b5e8 | 86 | } |
icraggs | 0:c524a894b5e8 | 87 | |
icraggs | 0:c524a894b5e8 | 88 | |
icraggs | 0:c524a894b5e8 | 89 | int MQTTSNDeserialize_puback(unsigned short* topicid, unsigned short* packetid, |
icraggs | 0:c524a894b5e8 | 90 | unsigned char* returncode, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 91 | { |
icraggs | 0:c524a894b5e8 | 92 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 93 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 94 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 95 | int mylen = 0; |
icraggs | 0:c524a894b5e8 | 96 | |
icraggs | 0:c524a894b5e8 | 97 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 98 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 99 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 100 | if (enddata - curdata > buflen) |
icraggs | 0:c524a894b5e8 | 101 | goto exit; |
icraggs | 0:c524a894b5e8 | 102 | |
icraggs | 0:c524a894b5e8 | 103 | if (readChar(&curdata) != MQTTSN_PUBACK) |
icraggs | 0:c524a894b5e8 | 104 | goto exit; |
icraggs | 0:c524a894b5e8 | 105 | |
icraggs | 0:c524a894b5e8 | 106 | *topicid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 107 | *packetid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 108 | *returncode = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 109 | |
icraggs | 0:c524a894b5e8 | 110 | rc = 1; |
icraggs | 0:c524a894b5e8 | 111 | exit: |
icraggs | 0:c524a894b5e8 | 112 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 113 | return rc; |
icraggs | 0:c524a894b5e8 | 114 | } |
icraggs | 0:c524a894b5e8 | 115 | |
icraggs | 0:c524a894b5e8 | 116 | |
icraggs | 0:c524a894b5e8 | 117 | /** |
icraggs | 0:c524a894b5e8 | 118 | * Deserializes the supplied (wire) buffer into an ack |
icraggs | 0:c524a894b5e8 | 119 | * @param packettype returned integer - the MQTT packet type |
icraggs | 0:c524a894b5e8 | 120 | * @param packetid returned integer - the MQTT packet identifier |
icraggs | 0:c524a894b5e8 | 121 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 122 | * @param buflen the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 123 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 124 | */ |
icraggs | 0:c524a894b5e8 | 125 | int MQTTSNDeserialize_ack(unsigned char* type, unsigned short* packetid, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 126 | { |
icraggs | 0:c524a894b5e8 | 127 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 128 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 129 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 130 | int mylen = 0; |
icraggs | 0:c524a894b5e8 | 131 | |
icraggs | 0:c524a894b5e8 | 132 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 133 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 134 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 135 | if (enddata - curdata > buflen) |
icraggs | 0:c524a894b5e8 | 136 | goto exit; |
icraggs | 0:c524a894b5e8 | 137 | |
icraggs | 0:c524a894b5e8 | 138 | *type = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 139 | if (*type != MQTTSN_PUBREL && *type != MQTTSN_PUBREC && *type != MQTTSN_PUBCOMP) |
icraggs | 0:c524a894b5e8 | 140 | goto exit; |
icraggs | 0:c524a894b5e8 | 141 | |
icraggs | 0:c524a894b5e8 | 142 | *packetid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 143 | |
icraggs | 0:c524a894b5e8 | 144 | rc = 1; |
icraggs | 0:c524a894b5e8 | 145 | exit: |
icraggs | 0:c524a894b5e8 | 146 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 147 | return rc; |
icraggs | 0:c524a894b5e8 | 148 | } |
icraggs | 0:c524a894b5e8 | 149 | |
icraggs | 0:c524a894b5e8 | 150 | |
icraggs | 0:c524a894b5e8 | 151 | /** |
icraggs | 0:c524a894b5e8 | 152 | * Deserializes the supplied (wire) buffer into register data |
icraggs | 0:c524a894b5e8 | 153 | * @param topicid returned topic id |
icraggs | 0:c524a894b5e8 | 154 | * @param packetid returned integer - the MQTT packet identifier |
icraggs | 0:c524a894b5e8 | 155 | * @param topicName returned MQTTSNString - the MQTT topic in the register |
icraggs | 0:c524a894b5e8 | 156 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 157 | * @param buflen the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 158 | * @return error code. 1 is success |
icraggs | 0:c524a894b5e8 | 159 | */ |
icraggs | 0:c524a894b5e8 | 160 | int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTSNString* topicname, |
icraggs | 0:c524a894b5e8 | 161 | unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 162 | { |
icraggs | 0:c524a894b5e8 | 163 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 164 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 165 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 166 | int mylen = 0; |
icraggs | 0:c524a894b5e8 | 167 | |
icraggs | 0:c524a894b5e8 | 168 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 169 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 170 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 171 | if (enddata - curdata > buflen) |
icraggs | 0:c524a894b5e8 | 172 | goto exit; |
icraggs | 0:c524a894b5e8 | 173 | |
icraggs | 0:c524a894b5e8 | 174 | if (readChar(&curdata) != MQTTSN_REGISTER) |
icraggs | 0:c524a894b5e8 | 175 | goto exit; |
icraggs | 0:c524a894b5e8 | 176 | |
icraggs | 0:c524a894b5e8 | 177 | *topicid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 178 | *packetid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 179 | |
icraggs | 0:c524a894b5e8 | 180 | topicname->lenstring.data = (char*)curdata; |
icraggs | 0:c524a894b5e8 | 181 | topicname->lenstring.len = enddata - curdata; |
icraggs | 0:c524a894b5e8 | 182 | topicname->cstring = NULL; |
icraggs | 0:c524a894b5e8 | 183 | |
icraggs | 0:c524a894b5e8 | 184 | rc = 1; |
icraggs | 0:c524a894b5e8 | 185 | exit: |
icraggs | 0:c524a894b5e8 | 186 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 187 | return rc; |
icraggs | 0:c524a894b5e8 | 188 | } |
icraggs | 0:c524a894b5e8 | 189 | |
icraggs | 0:c524a894b5e8 | 190 | |
icraggs | 0:c524a894b5e8 | 191 | /** |
icraggs | 0:c524a894b5e8 | 192 | * Deserializes the supplied (wire) buffer into register data |
icraggs | 0:c524a894b5e8 | 193 | * @param topicid returned topic id |
icraggs | 0:c524a894b5e8 | 194 | * @param packetid returned integer - the MQTT packet identifier |
icraggs | 0:c524a894b5e8 | 195 | * @param return_code returned integer return code |
icraggs | 0:c524a894b5e8 | 196 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 197 | * @param buflen the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 198 | * @return error code. 1 is success |
icraggs | 0:c524a894b5e8 | 199 | */ |
icraggs | 0:c524a894b5e8 | 200 | int MQTTSNDeserialize_regack(unsigned short* topicid, unsigned short* packetid, unsigned char* return_code, |
icraggs | 0:c524a894b5e8 | 201 | unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 202 | { |
icraggs | 0:c524a894b5e8 | 203 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 204 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 205 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 206 | int mylen = 0; |
icraggs | 0:c524a894b5e8 | 207 | |
icraggs | 0:c524a894b5e8 | 208 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 209 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 210 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 211 | if (enddata - curdata > buflen) |
icraggs | 0:c524a894b5e8 | 212 | goto exit; |
icraggs | 0:c524a894b5e8 | 213 | |
icraggs | 0:c524a894b5e8 | 214 | if (readChar(&curdata) != MQTTSN_REGACK) |
icraggs | 0:c524a894b5e8 | 215 | goto exit; |
icraggs | 0:c524a894b5e8 | 216 | |
icraggs | 0:c524a894b5e8 | 217 | *topicid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 218 | *packetid = readInt(&curdata); |
icraggs | 0:c524a894b5e8 | 219 | *return_code = readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 220 | |
icraggs | 0:c524a894b5e8 | 221 | rc = 1; |
icraggs | 0:c524a894b5e8 | 222 | exit: |
icraggs | 0:c524a894b5e8 | 223 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 224 | return rc; |
icraggs | 0:c524a894b5e8 | 225 | } |
icraggs | 0:c524a894b5e8 | 226 |