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 unsubscribe 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_unsubscribeLength(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]); /* length + topic*/
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 unsubscribe data into the supplied buffer, ready for sending
hudakz 0:238f0d0c0ba3 41 * @param buf the raw buffer data, of the correct length determined by the remaining length field
hudakz 0:238f0d0c0ba3 42 * @param buflen the length in bytes of the data in the supplied buffer
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 array
hudakz 0:238f0d0c0ba3 46 * @param topicFilters - array of topic filter names
hudakz 0:238f0d0c0ba3 47 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 48 */
hudakz 0:238f0d0c0ba3 49 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
hudakz 0:238f0d0c0ba3 50 int count, MQTTString topicFilters[])
hudakz 0:238f0d0c0ba3 51 {
hudakz 0:238f0d0c0ba3 52 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 53 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 54 int rem_len = 0;
hudakz 0:238f0d0c0ba3 55 int rc = -1;
hudakz 0:238f0d0c0ba3 56 int i = 0;
hudakz 0:238f0d0c0ba3 57
hudakz 0:238f0d0c0ba3 58 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 59 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
hudakz 0:238f0d0c0ba3 60 {
hudakz 0:238f0d0c0ba3 61 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 62 goto exit;
hudakz 0:238f0d0c0ba3 63 }
hudakz 0:238f0d0c0ba3 64
hudakz 0:238f0d0c0ba3 65 header.byte = 0;
hudakz 0:238f0d0c0ba3 66 header.bits.type = UNSUBSCRIBE;
hudakz 0:238f0d0c0ba3 67 header.bits.dup = dup;
hudakz 0:238f0d0c0ba3 68 header.bits.qos = 1;
hudakz 0:238f0d0c0ba3 69 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 70
hudakz 0:238f0d0c0ba3 71 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
hudakz 0:238f0d0c0ba3 72
hudakz 0:238f0d0c0ba3 73 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 74
hudakz 0:238f0d0c0ba3 75 for (i = 0; i < count; ++i)
hudakz 0:238f0d0c0ba3 76 writeMQTTString(&ptr, topicFilters[i]);
hudakz 0:238f0d0c0ba3 77
hudakz 0:238f0d0c0ba3 78 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 79 exit:
hudakz 0:238f0d0c0ba3 80 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 81 return rc;
hudakz 0:238f0d0c0ba3 82 }
hudakz 0:238f0d0c0ba3 83
hudakz 0:238f0d0c0ba3 84
hudakz 0:238f0d0c0ba3 85 /**
hudakz 0:238f0d0c0ba3 86 * Deserializes the supplied (wire) buffer into unsuback data
hudakz 0:238f0d0c0ba3 87 * @param packetid returned integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 88 * @param buf the raw buffer data, of the correct length determined by the remaining length field
hudakz 0:238f0d0c0ba3 89 * @param buflen the length in bytes of the data in the supplied buffer
hudakz 0:238f0d0c0ba3 90 * @return error code. 1 is success, 0 is failure
hudakz 0:238f0d0c0ba3 91 */
hudakz 0:238f0d0c0ba3 92 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
hudakz 0:238f0d0c0ba3 93 {
hudakz 0:238f0d0c0ba3 94 unsigned char type = 0;
hudakz 0:238f0d0c0ba3 95 unsigned char dup = 0;
hudakz 0:238f0d0c0ba3 96 int rc = 0;
hudakz 0:238f0d0c0ba3 97
hudakz 0:238f0d0c0ba3 98 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 99 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
hudakz 0:238f0d0c0ba3 100 if (type == UNSUBACK)
hudakz 0:238f0d0c0ba3 101 rc = 1;
hudakz 0:238f0d0c0ba3 102 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 103 return rc;
hudakz 0:238f0d0c0ba3 104 }
hudakz 0:238f0d0c0ba3 105
hudakz 0:238f0d0c0ba3 106