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

Dependencies:   ENC28J60-EMAC

Committer:
hudakz
Date:
Mon Mar 29 09:32:44 2021 +0000
Revision:
5:d9570dbf2f82
Parent:
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 * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
hudakz 0:238f0d0c0ba3 24 * @param count the number of topic filter strings in topicFilters
hudakz 0:238f0d0c0ba3 25 * @param topicFilters the array of topic filter strings to be used in the publish
hudakz 0:238f0d0c0ba3 26 * @return the length of buffer needed to contain the serialized version of the packet
hudakz 0:238f0d0c0ba3 27 */
hudakz 0:238f0d0c0ba3 28 int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
hudakz 0:238f0d0c0ba3 29 {
hudakz 0:238f0d0c0ba3 30 int i;
hudakz 0:238f0d0c0ba3 31 int len = 2; /* packetid */
hudakz 0:238f0d0c0ba3 32
hudakz 0:238f0d0c0ba3 33 for (i = 0; i < count; ++i)
hudakz 0:238f0d0c0ba3 34 len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
hudakz 0:238f0d0c0ba3 35 return len;
hudakz 0:238f0d0c0ba3 36 }
hudakz 0:238f0d0c0ba3 37
hudakz 0:238f0d0c0ba3 38
hudakz 0:238f0d0c0ba3 39 /**
hudakz 0:238f0d0c0ba3 40 * Serializes the supplied subscribe data into the supplied buffer, ready for sending
hudakz 0:238f0d0c0ba3 41 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 42 * @param buflen the length in bytes of the supplied bufferr
hudakz 0:238f0d0c0ba3 43 * @param dup integer - the MQTT dup flag
hudakz 0:238f0d0c0ba3 44 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 45 * @param count - number of members in the topicFilters and reqQos arrays
hudakz 0:238f0d0c0ba3 46 * @param topicFilters - array of topic filter names
hudakz 0:238f0d0c0ba3 47 * @param requestedQoSs - array of requested QoS
hudakz 0:238f0d0c0ba3 48 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 49 */
hudakz 0:238f0d0c0ba3 50 int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
hudakz 0:238f0d0c0ba3 51 MQTTString topicFilters[], int requestedQoSs[])
hudakz 0:238f0d0c0ba3 52 {
hudakz 0:238f0d0c0ba3 53 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 54 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 55 int rem_len = 0;
hudakz 0:238f0d0c0ba3 56 int rc = 0;
hudakz 0:238f0d0c0ba3 57 int i = 0;
hudakz 0:238f0d0c0ba3 58
hudakz 0:238f0d0c0ba3 59 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 60 if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
hudakz 0:238f0d0c0ba3 61 {
hudakz 0:238f0d0c0ba3 62 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 63 goto exit;
hudakz 0:238f0d0c0ba3 64 }
hudakz 0:238f0d0c0ba3 65
hudakz 0:238f0d0c0ba3 66 header.byte = 0;
hudakz 0:238f0d0c0ba3 67 header.bits.type = SUBSCRIBE;
hudakz 0:238f0d0c0ba3 68 header.bits.dup = dup;
hudakz 0:238f0d0c0ba3 69 header.bits.qos = 1;
hudakz 0:238f0d0c0ba3 70 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 71
hudakz 0:238f0d0c0ba3 72 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
hudakz 0:238f0d0c0ba3 73
hudakz 0:238f0d0c0ba3 74 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 75
hudakz 0:238f0d0c0ba3 76 for (i = 0; i < count; ++i)
hudakz 0:238f0d0c0ba3 77 {
hudakz 0:238f0d0c0ba3 78 writeMQTTString(&ptr, topicFilters[i]);
hudakz 0:238f0d0c0ba3 79 writeChar(&ptr, requestedQoSs[i]);
hudakz 0:238f0d0c0ba3 80 }
hudakz 0:238f0d0c0ba3 81
hudakz 0:238f0d0c0ba3 82 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 83 exit:
hudakz 0:238f0d0c0ba3 84 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 85 return rc;
hudakz 0:238f0d0c0ba3 86 }
hudakz 0:238f0d0c0ba3 87
hudakz 0:238f0d0c0ba3 88
hudakz 0:238f0d0c0ba3 89
hudakz 0:238f0d0c0ba3 90 /**
hudakz 0:238f0d0c0ba3 91 * Deserializes the supplied (wire) buffer into suback data
hudakz 0:238f0d0c0ba3 92 * @param packetid returned integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 93 * @param maxcount - the maximum number of members allowed in the grantedQoSs array
hudakz 0:238f0d0c0ba3 94 * @param count returned integer - number of members in the grantedQoSs array
hudakz 0:238f0d0c0ba3 95 * @param grantedQoSs returned array of integers - the granted qualities of service
hudakz 0:238f0d0c0ba3 96 * @param buf the raw buffer data, of the correct length determined by the remaining length field
hudakz 0:238f0d0c0ba3 97 * @param buflen the length in bytes of the data in the supplied buffer
hudakz 0:238f0d0c0ba3 98 * @return error code. 1 is success, 0 is failure
hudakz 0:238f0d0c0ba3 99 */
hudakz 0:238f0d0c0ba3 100 int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
hudakz 0:238f0d0c0ba3 101 {
hudakz 0:238f0d0c0ba3 102 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 103 unsigned char* curdata = buf;
hudakz 0:238f0d0c0ba3 104 unsigned char* enddata = NULL;
hudakz 0:238f0d0c0ba3 105 int rc = 0;
hudakz 0:238f0d0c0ba3 106 int mylen;
hudakz 0:238f0d0c0ba3 107
hudakz 0:238f0d0c0ba3 108 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 109 header.byte = readChar(&curdata);
hudakz 0:238f0d0c0ba3 110 if (header.bits.type != SUBACK)
hudakz 0:238f0d0c0ba3 111 goto exit;
hudakz 0:238f0d0c0ba3 112
hudakz 0:238f0d0c0ba3 113 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
hudakz 0:238f0d0c0ba3 114 enddata = curdata + mylen;
hudakz 0:238f0d0c0ba3 115 if (enddata - curdata < 2)
hudakz 0:238f0d0c0ba3 116 goto exit;
hudakz 0:238f0d0c0ba3 117
hudakz 0:238f0d0c0ba3 118 *packetid = readInt(&curdata);
hudakz 0:238f0d0c0ba3 119
hudakz 0:238f0d0c0ba3 120 *count = 0;
hudakz 0:238f0d0c0ba3 121 while (curdata < enddata)
hudakz 0:238f0d0c0ba3 122 {
hudakz 0:238f0d0c0ba3 123 if (*count > maxcount)
hudakz 0:238f0d0c0ba3 124 {
hudakz 0:238f0d0c0ba3 125 rc = -1;
hudakz 0:238f0d0c0ba3 126 goto exit;
hudakz 0:238f0d0c0ba3 127 }
hudakz 0:238f0d0c0ba3 128 grantedQoSs[(*count)++] = readChar(&curdata);
hudakz 0:238f0d0c0ba3 129 }
hudakz 0:238f0d0c0ba3 130
hudakz 0:238f0d0c0ba3 131 rc = 1;
hudakz 0:238f0d0c0ba3 132 exit:
hudakz 0:238f0d0c0ba3 133 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 134 return rc;
hudakz 0:238f0d0c0ba3 135 }
hudakz 0:238f0d0c0ba3 136
hudakz 0:238f0d0c0ba3 137