MQTT client to test the ENC28J60-EMAC on NUCLEO-F446RE.

Dependencies:   ENC28J60-EMAC

Committer:
hudakz
Date:
Fri Mar 26 16:15:14 2021 +0000
Revision:
0:238f0d0c0ba3
MQTT client to test the ENC28J60-EMAC on NUCLEO-F446RE.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:238f0d0c0ba3 1 /*******************************************************************************
hudakz 0:238f0d0c0ba3 2 * Copyright (c) 2014 IBM Corp.
hudakz 0:238f0d0c0ba3 3 *
hudakz 0:238f0d0c0ba3 4 * All rights reserved. This program and the accompanying materials
hudakz 0:238f0d0c0ba3 5 * are made available under the terms of the Eclipse Public License v1.0
hudakz 0:238f0d0c0ba3 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
hudakz 0:238f0d0c0ba3 7 *
hudakz 0:238f0d0c0ba3 8 * The Eclipse Public License is available at
hudakz 0:238f0d0c0ba3 9 * http://www.eclipse.org/legal/epl-v10.html
hudakz 0:238f0d0c0ba3 10 * and the Eclipse Distribution License is available at
hudakz 0:238f0d0c0ba3 11 * http://www.eclipse.org/org/documents/edl-v10.php.
hudakz 0:238f0d0c0ba3 12 *
hudakz 0:238f0d0c0ba3 13 * Contributors:
hudakz 0:238f0d0c0ba3 14 * Ian Craggs - initial API and implementation and/or initial documentation
hudakz 0:238f0d0c0ba3 15 *******************************************************************************/
hudakz 0:238f0d0c0ba3 16
hudakz 0:238f0d0c0ba3 17 #include "MQTTPacket.h"
hudakz 0:238f0d0c0ba3 18 #include "StackTrace.h"
hudakz 0:238f0d0c0ba3 19
hudakz 0:238f0d0c0ba3 20 #include <string.h>
hudakz 0:238f0d0c0ba3 21
hudakz 0:238f0d0c0ba3 22
hudakz 0:238f0d0c0ba3 23 /**
hudakz 0:238f0d0c0ba3 24 * Deserializes the supplied (wire) buffer into subscribe data
hudakz 0:238f0d0c0ba3 25 * @param dup integer returned - the MQTT dup flag
hudakz 0:238f0d0c0ba3 26 * @param packetid integer returned - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 27 * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
hudakz 0:238f0d0c0ba3 28 * @param count - number of members in the topicFilters and requestedQoSs arrays
hudakz 0:238f0d0c0ba3 29 * @param topicFilters - array of topic filter names
hudakz 0:238f0d0c0ba3 30 * @param requestedQoSs - array of requested QoS
hudakz 0:238f0d0c0ba3 31 * @param buf the raw buffer data, of the correct length determined by the remaining length field
hudakz 0:238f0d0c0ba3 32 * @param buflen the length in bytes of the data in the supplied buffer
hudakz 0:238f0d0c0ba3 33 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 34 */
hudakz 0:238f0d0c0ba3 35 int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
hudakz 0:238f0d0c0ba3 36 int requestedQoSs[], unsigned char* buf, int buflen)
hudakz 0:238f0d0c0ba3 37 {
hudakz 0:238f0d0c0ba3 38 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 39 unsigned char* curdata = buf;
hudakz 0:238f0d0c0ba3 40 unsigned char* enddata = NULL;
hudakz 0:238f0d0c0ba3 41 int rc = -1;
hudakz 0:238f0d0c0ba3 42 int mylen = 0;
hudakz 0:238f0d0c0ba3 43
hudakz 0:238f0d0c0ba3 44 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 45 header.byte = readChar(&curdata);
hudakz 0:238f0d0c0ba3 46 if (header.bits.type != SUBSCRIBE)
hudakz 0:238f0d0c0ba3 47 goto exit;
hudakz 0:238f0d0c0ba3 48 *dup = header.bits.dup;
hudakz 0:238f0d0c0ba3 49
hudakz 0:238f0d0c0ba3 50 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
hudakz 0:238f0d0c0ba3 51 enddata = curdata + mylen;
hudakz 0:238f0d0c0ba3 52
hudakz 0:238f0d0c0ba3 53 *packetid = readInt(&curdata);
hudakz 0:238f0d0c0ba3 54
hudakz 0:238f0d0c0ba3 55 *count = 0;
hudakz 0:238f0d0c0ba3 56 while (curdata < enddata)
hudakz 0:238f0d0c0ba3 57 {
hudakz 0:238f0d0c0ba3 58 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
hudakz 0:238f0d0c0ba3 59 goto exit;
hudakz 0:238f0d0c0ba3 60 if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
hudakz 0:238f0d0c0ba3 61 goto exit;
hudakz 0:238f0d0c0ba3 62 requestedQoSs[*count] = readChar(&curdata);
hudakz 0:238f0d0c0ba3 63 (*count)++;
hudakz 0:238f0d0c0ba3 64 }
hudakz 0:238f0d0c0ba3 65
hudakz 0:238f0d0c0ba3 66 rc = 1;
hudakz 0:238f0d0c0ba3 67 exit:
hudakz 0:238f0d0c0ba3 68 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 69 return rc;
hudakz 0:238f0d0c0ba3 70 }
hudakz 0:238f0d0c0ba3 71
hudakz 0:238f0d0c0ba3 72
hudakz 0:238f0d0c0ba3 73 /**
hudakz 0:238f0d0c0ba3 74 * Serializes the supplied suback data into the supplied buffer, ready for sending
hudakz 0:238f0d0c0ba3 75 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 76 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 77 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 78 * @param count - number of members in the grantedQoSs array
hudakz 0:238f0d0c0ba3 79 * @param grantedQoSs - array of granted QoS
hudakz 0:238f0d0c0ba3 80 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 81 */
hudakz 0:238f0d0c0ba3 82 int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
hudakz 0:238f0d0c0ba3 83 {
hudakz 0:238f0d0c0ba3 84 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 85 int rc = -1;
hudakz 0:238f0d0c0ba3 86 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 87 int i;
hudakz 0:238f0d0c0ba3 88
hudakz 0:238f0d0c0ba3 89 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 90 if (buflen < 2 + count)
hudakz 0:238f0d0c0ba3 91 {
hudakz 0:238f0d0c0ba3 92 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 93 goto exit;
hudakz 0:238f0d0c0ba3 94 }
hudakz 0:238f0d0c0ba3 95 header.byte = 0;
hudakz 0:238f0d0c0ba3 96 header.bits.type = SUBACK;
hudakz 0:238f0d0c0ba3 97 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 98
hudakz 0:238f0d0c0ba3 99 ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
hudakz 0:238f0d0c0ba3 100
hudakz 0:238f0d0c0ba3 101 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 102
hudakz 0:238f0d0c0ba3 103 for (i = 0; i < count; ++i)
hudakz 0:238f0d0c0ba3 104 writeChar(&ptr, grantedQoSs[i]);
hudakz 0:238f0d0c0ba3 105
hudakz 0:238f0d0c0ba3 106 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 107 exit:
hudakz 0:238f0d0c0ba3 108 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 109 return rc;
hudakz 0:238f0d0c0ba3 110 }
hudakz 0:238f0d0c0ba3 111
hudakz 0:238f0d0c0ba3 112