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 unsubscribe 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 buf the raw buffer data, of the correct length determined by the remaining length field
hudakz 0:238f0d0c0ba3 31 * @param buflen the length in bytes of the data in the supplied buffer
hudakz 0:238f0d0c0ba3 32 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 33 */
hudakz 0:238f0d0c0ba3 34 int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
hudakz 0:238f0d0c0ba3 35 unsigned char* buf, int len)
hudakz 0:238f0d0c0ba3 36 {
hudakz 0:238f0d0c0ba3 37 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 38 unsigned char* curdata = buf;
hudakz 0:238f0d0c0ba3 39 unsigned char* enddata = NULL;
hudakz 0:238f0d0c0ba3 40 int rc = 0;
hudakz 0:238f0d0c0ba3 41 int mylen = 0;
hudakz 0:238f0d0c0ba3 42
hudakz 0:238f0d0c0ba3 43 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 44 header.byte = readChar(&curdata);
hudakz 0:238f0d0c0ba3 45 if (header.bits.type != UNSUBSCRIBE)
hudakz 0:238f0d0c0ba3 46 goto exit;
hudakz 0:238f0d0c0ba3 47 *dup = header.bits.dup;
hudakz 0:238f0d0c0ba3 48
hudakz 0:238f0d0c0ba3 49 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
hudakz 0:238f0d0c0ba3 50 enddata = curdata + mylen;
hudakz 0:238f0d0c0ba3 51
hudakz 0:238f0d0c0ba3 52 *packetid = readInt(&curdata);
hudakz 0:238f0d0c0ba3 53
hudakz 0:238f0d0c0ba3 54 *count = 0;
hudakz 0:238f0d0c0ba3 55 while (curdata < enddata)
hudakz 0:238f0d0c0ba3 56 {
hudakz 0:238f0d0c0ba3 57 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
hudakz 0:238f0d0c0ba3 58 goto exit;
hudakz 0:238f0d0c0ba3 59 (*count)++;
hudakz 0:238f0d0c0ba3 60 }
hudakz 0:238f0d0c0ba3 61
hudakz 0:238f0d0c0ba3 62 rc = 1;
hudakz 0:238f0d0c0ba3 63 exit:
hudakz 0:238f0d0c0ba3 64 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 65 return rc;
hudakz 0:238f0d0c0ba3 66 }
hudakz 0:238f0d0c0ba3 67
hudakz 0:238f0d0c0ba3 68
hudakz 0:238f0d0c0ba3 69 /**
hudakz 0:238f0d0c0ba3 70 * Serializes the supplied unsuback data into the supplied buffer, ready for sending
hudakz 0:238f0d0c0ba3 71 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 72 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 73 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 74 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 75 */
hudakz 0:238f0d0c0ba3 76 int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
hudakz 0:238f0d0c0ba3 77 {
hudakz 0:238f0d0c0ba3 78 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 79 int rc = 0;
hudakz 0:238f0d0c0ba3 80 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 81
hudakz 0:238f0d0c0ba3 82 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 83 if (buflen < 2)
hudakz 0:238f0d0c0ba3 84 {
hudakz 0:238f0d0c0ba3 85 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 86 goto exit;
hudakz 0:238f0d0c0ba3 87 }
hudakz 0:238f0d0c0ba3 88 header.byte = 0;
hudakz 0:238f0d0c0ba3 89 header.bits.type = UNSUBACK;
hudakz 0:238f0d0c0ba3 90 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 91
hudakz 0:238f0d0c0ba3 92 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
hudakz 0:238f0d0c0ba3 93
hudakz 0:238f0d0c0ba3 94 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 95
hudakz 0:238f0d0c0ba3 96 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 97 exit:
hudakz 0:238f0d0c0ba3 98 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 99 return rc;
hudakz 0:238f0d0c0ba3 100 }
hudakz 0:238f0d0c0ba3 101
hudakz 0:238f0d0c0ba3 102