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 * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
hudakz 0:238f0d0c0ba3 16 *******************************************************************************/
hudakz 0:238f0d0c0ba3 17
hudakz 0:238f0d0c0ba3 18 #include "MQTTPacket.h"
hudakz 0:238f0d0c0ba3 19 #include "StackTrace.h"
hudakz 0:238f0d0c0ba3 20
hudakz 0:238f0d0c0ba3 21 #include <string.h>
hudakz 0:238f0d0c0ba3 22
hudakz 0:238f0d0c0ba3 23
hudakz 0:238f0d0c0ba3 24 /**
hudakz 0:238f0d0c0ba3 25 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
hudakz 0:238f0d0c0ba3 26 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
hudakz 0:238f0d0c0ba3 27 * @param topicName the topic name to be used in the publish
hudakz 0:238f0d0c0ba3 28 * @param payloadlen the length of the payload to be sent
hudakz 0:238f0d0c0ba3 29 * @return the length of buffer needed to contain the serialized version of the packet
hudakz 0:238f0d0c0ba3 30 */
hudakz 0:238f0d0c0ba3 31 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
hudakz 0:238f0d0c0ba3 32 {
hudakz 0:238f0d0c0ba3 33 int len = 0;
hudakz 0:238f0d0c0ba3 34
hudakz 0:238f0d0c0ba3 35 len += 2 + MQTTstrlen(topicName) + payloadlen;
hudakz 0:238f0d0c0ba3 36 if (qos > 0)
hudakz 0:238f0d0c0ba3 37 len += 2; /* packetid */
hudakz 0:238f0d0c0ba3 38 return len;
hudakz 0:238f0d0c0ba3 39 }
hudakz 0:238f0d0c0ba3 40
hudakz 0:238f0d0c0ba3 41
hudakz 0:238f0d0c0ba3 42 /**
hudakz 0:238f0d0c0ba3 43 * Serializes the supplied publish data into the supplied buffer, ready for sending
hudakz 0:238f0d0c0ba3 44 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 45 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 46 * @param dup integer - the MQTT dup flag
hudakz 0:238f0d0c0ba3 47 * @param qos integer - the MQTT QoS value
hudakz 0:238f0d0c0ba3 48 * @param retained integer - the MQTT retained flag
hudakz 0:238f0d0c0ba3 49 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 50 * @param topicName MQTTString - the MQTT topic in the publish
hudakz 0:238f0d0c0ba3 51 * @param payload byte buffer - the MQTT publish payload
hudakz 0:238f0d0c0ba3 52 * @param payloadlen integer - the length of the MQTT payload
hudakz 0:238f0d0c0ba3 53 * @return the length of the serialized data. <= 0 indicates error
hudakz 0:238f0d0c0ba3 54 */
hudakz 0:238f0d0c0ba3 55 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
hudakz 0:238f0d0c0ba3 56 MQTTString topicName, unsigned char* payload, int payloadlen)
hudakz 0:238f0d0c0ba3 57 {
hudakz 0:238f0d0c0ba3 58 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 59 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 60 int rem_len = 0;
hudakz 0:238f0d0c0ba3 61 int rc = 0;
hudakz 0:238f0d0c0ba3 62
hudakz 0:238f0d0c0ba3 63 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 64 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
hudakz 0:238f0d0c0ba3 65 {
hudakz 0:238f0d0c0ba3 66 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 67 goto exit;
hudakz 0:238f0d0c0ba3 68 }
hudakz 0:238f0d0c0ba3 69
hudakz 0:238f0d0c0ba3 70 header.bits.type = PUBLISH;
hudakz 0:238f0d0c0ba3 71 header.bits.dup = dup;
hudakz 0:238f0d0c0ba3 72 header.bits.qos = qos;
hudakz 0:238f0d0c0ba3 73 header.bits.retain = retained;
hudakz 0:238f0d0c0ba3 74 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 75
hudakz 0:238f0d0c0ba3 76 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
hudakz 0:238f0d0c0ba3 77
hudakz 0:238f0d0c0ba3 78 writeMQTTString(&ptr, topicName);
hudakz 0:238f0d0c0ba3 79
hudakz 0:238f0d0c0ba3 80 if (qos > 0)
hudakz 0:238f0d0c0ba3 81 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 82
hudakz 0:238f0d0c0ba3 83 memcpy(ptr, payload, payloadlen);
hudakz 0:238f0d0c0ba3 84 ptr += payloadlen;
hudakz 0:238f0d0c0ba3 85
hudakz 0:238f0d0c0ba3 86 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 87
hudakz 0:238f0d0c0ba3 88 exit:
hudakz 0:238f0d0c0ba3 89 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 90 return rc;
hudakz 0:238f0d0c0ba3 91 }
hudakz 0:238f0d0c0ba3 92
hudakz 0:238f0d0c0ba3 93
hudakz 0:238f0d0c0ba3 94
hudakz 0:238f0d0c0ba3 95 /**
hudakz 0:238f0d0c0ba3 96 * Serializes the ack packet into the supplied buffer.
hudakz 0:238f0d0c0ba3 97 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 98 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 99 * @param type the MQTT packet type
hudakz 0:238f0d0c0ba3 100 * @param dup the MQTT dup flag
hudakz 0:238f0d0c0ba3 101 * @param packetid the MQTT packet identifier
hudakz 0:238f0d0c0ba3 102 * @return serialized length, or error if 0
hudakz 0:238f0d0c0ba3 103 */
hudakz 0:238f0d0c0ba3 104 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
hudakz 0:238f0d0c0ba3 105 {
hudakz 0:238f0d0c0ba3 106 MQTTHeader header = {0};
hudakz 0:238f0d0c0ba3 107 int rc = 0;
hudakz 0:238f0d0c0ba3 108 unsigned char *ptr = buf;
hudakz 0:238f0d0c0ba3 109
hudakz 0:238f0d0c0ba3 110 FUNC_ENTRY;
hudakz 0:238f0d0c0ba3 111 if (buflen < 4)
hudakz 0:238f0d0c0ba3 112 {
hudakz 0:238f0d0c0ba3 113 rc = MQTTPACKET_BUFFER_TOO_SHORT;
hudakz 0:238f0d0c0ba3 114 goto exit;
hudakz 0:238f0d0c0ba3 115 }
hudakz 0:238f0d0c0ba3 116 header.bits.type = packettype;
hudakz 0:238f0d0c0ba3 117 header.bits.dup = dup;
hudakz 0:238f0d0c0ba3 118 header.bits.qos = (packettype == PUBREL) ? 1 : 0;
hudakz 0:238f0d0c0ba3 119 writeChar(&ptr, header.byte); /* write header */
hudakz 0:238f0d0c0ba3 120
hudakz 0:238f0d0c0ba3 121 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
hudakz 0:238f0d0c0ba3 122 writeInt(&ptr, packetid);
hudakz 0:238f0d0c0ba3 123 rc = ptr - buf;
hudakz 0:238f0d0c0ba3 124 exit:
hudakz 0:238f0d0c0ba3 125 FUNC_EXIT_RC(rc);
hudakz 0:238f0d0c0ba3 126 return rc;
hudakz 0:238f0d0c0ba3 127 }
hudakz 0:238f0d0c0ba3 128
hudakz 0:238f0d0c0ba3 129
hudakz 0:238f0d0c0ba3 130 /**
hudakz 0:238f0d0c0ba3 131 * Serializes a puback packet into the supplied buffer.
hudakz 0:238f0d0c0ba3 132 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 133 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 134 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 135 * @return serialized length, or error if 0
hudakz 0:238f0d0c0ba3 136 */
hudakz 0:238f0d0c0ba3 137 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
hudakz 0:238f0d0c0ba3 138 {
hudakz 0:238f0d0c0ba3 139 return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
hudakz 0:238f0d0c0ba3 140 }
hudakz 0:238f0d0c0ba3 141
hudakz 0:238f0d0c0ba3 142
hudakz 0:238f0d0c0ba3 143 /**
hudakz 0:238f0d0c0ba3 144 * Serializes a pubrel packet into the supplied buffer.
hudakz 0:238f0d0c0ba3 145 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 146 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 147 * @param dup integer - the MQTT dup flag
hudakz 0:238f0d0c0ba3 148 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 149 * @return serialized length, or error if 0
hudakz 0:238f0d0c0ba3 150 */
hudakz 0:238f0d0c0ba3 151 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
hudakz 0:238f0d0c0ba3 152 {
hudakz 0:238f0d0c0ba3 153 return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
hudakz 0:238f0d0c0ba3 154 }
hudakz 0:238f0d0c0ba3 155
hudakz 0:238f0d0c0ba3 156
hudakz 0:238f0d0c0ba3 157 /**
hudakz 0:238f0d0c0ba3 158 * Serializes a pubrel packet into the supplied buffer.
hudakz 0:238f0d0c0ba3 159 * @param buf the buffer into which the packet will be serialized
hudakz 0:238f0d0c0ba3 160 * @param buflen the length in bytes of the supplied buffer
hudakz 0:238f0d0c0ba3 161 * @param packetid integer - the MQTT packet identifier
hudakz 0:238f0d0c0ba3 162 * @return serialized length, or error if 0
hudakz 0:238f0d0c0ba3 163 */
hudakz 0:238f0d0c0ba3 164 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
hudakz 0:238f0d0c0ba3 165 {
hudakz 0:238f0d0c0ba3 166 return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
hudakz 0:238f0d0c0ba3 167 }
hudakz 0:238f0d0c0ba3 168