blob: dbc9e5d8aecb0f4b3db6eac982eea4fd86587207 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*******************************************************************************
2 * Copyright (c) 2014 IBM Corp.
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * Contributors:
14 * Ian Craggs - initial API and implementation and/or initial documentation
15 *******************************************************************************/
16
17#include "StackTrace.h"
18#include "MQTTPacket.h"
19#include <string.h>
20
21#define min(a, b) ((a < b) ? 1 : 0)
22
23/**
24 * Deserializes the supplied (wire) buffer into publish data
25 * @param dup returned integer - the MQTT dup flag
26 * @param qos returned integer - the MQTT QoS value
27 * @param retained returned integer - the MQTT retained flag
28 * @param packetid returned integer - the MQTT packet identifier
29 * @param topicName returned MQTTString - the MQTT topic in the publish
30 * @param payload returned byte buffer - the MQTT publish payload
31 * @param payloadlen returned integer - the length of the MQTT payload
32 * @param buf the raw buffer data, of the correct length determined by the remaining length field
33 * @param buflen the length in bytes of the data in the supplied buffer
34 * @return error code. 1 is success
35 */
36int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
37 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
38{
39 MQTTHeader header = {0};
40 unsigned char* curdata = buf;
41 unsigned char* enddata = NULL;
42 int rc = 0;
43 int mylen = 0;
44
45 FUNC_ENTRY;
46 header.byte = readChar(&curdata);
47 if (header.bits.type != PUBLISH)
48 goto exit;
49 *dup = header.bits.dup;
50 *qos = header.bits.qos;
51 *retained = header.bits.retain;
52
53 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
54 enddata = curdata + mylen;
55
56 if (!readMQTTLenString(topicName, &curdata, enddata) ||
57 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
58 goto exit;
59
60 if (*qos > 0)
61 *packetid = readInt(&curdata);
62
63 *payloadlen = enddata - curdata;
64 *payload = curdata;
65 rc = 1;
66exit:
67 FUNC_EXIT_RC(rc);
68 return rc;
69}
70
71
72
73/**
74 * Deserializes the supplied (wire) buffer into an ack
75 * @param packettype returned integer - the MQTT packet type
76 * @param dup returned integer - the MQTT dup flag
77 * @param packetid returned integer - the MQTT packet identifier
78 * @param buf the raw buffer data, of the correct length determined by the remaining length field
79 * @param buflen the length in bytes of the data in the supplied buffer
80 * @return error code. 1 is success, 0 is failure
81 */
82int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
83{
84 MQTTHeader header = {0};
85 unsigned char* curdata = buf;
86 unsigned char* enddata = NULL;
87 int rc = 0;
88 int mylen;
89
90 FUNC_ENTRY;
91 header.byte = readChar(&curdata);
92 *dup = header.bits.dup;
93 *packettype = header.bits.type;
94
95 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
96 enddata = curdata + mylen;
97
98 if (enddata - curdata < 2)
99 goto exit;
100 *packetid = readInt(&curdata);
101
102 rc = 1;
103exit:
104 FUNC_EXIT_RC(rc);
105 return rc;
106}
107