Zoltan Hudak
/
MQTT_Hello
MQTT client to test the ENC28J60-EMAC on NUCLEO-F446RE.
MQTT/MQTTPacket/MQTTConnectClient.c@0:238f0d0c0ba3, 2021-03-26 (annotated)
- 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?
User | Revision | Line number | New 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 connect packet that would be produced using the supplied connect options. |
hudakz | 0:238f0d0c0ba3 | 24 | * @param options the options to be used to build the connect packet |
hudakz | 0:238f0d0c0ba3 | 25 | * @return the length of buffer needed to contain the serialized version of the packet |
hudakz | 0:238f0d0c0ba3 | 26 | */ |
hudakz | 0:238f0d0c0ba3 | 27 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) |
hudakz | 0:238f0d0c0ba3 | 28 | { |
hudakz | 0:238f0d0c0ba3 | 29 | int len = 0; |
hudakz | 0:238f0d0c0ba3 | 30 | |
hudakz | 0:238f0d0c0ba3 | 31 | FUNC_ENTRY; |
hudakz | 0:238f0d0c0ba3 | 32 | |
hudakz | 0:238f0d0c0ba3 | 33 | if (options->MQTTVersion == 3) |
hudakz | 0:238f0d0c0ba3 | 34 | len = 12; /* variable depending on MQTT or MQIsdp */ |
hudakz | 0:238f0d0c0ba3 | 35 | else if (options->MQTTVersion == 4) |
hudakz | 0:238f0d0c0ba3 | 36 | len = 10; |
hudakz | 0:238f0d0c0ba3 | 37 | |
hudakz | 0:238f0d0c0ba3 | 38 | len += MQTTstrlen(options->clientID)+2; |
hudakz | 0:238f0d0c0ba3 | 39 | if (options->willFlag) |
hudakz | 0:238f0d0c0ba3 | 40 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; |
hudakz | 0:238f0d0c0ba3 | 41 | if (options->username.cstring || options->username.lenstring.data) |
hudakz | 0:238f0d0c0ba3 | 42 | len += MQTTstrlen(options->username)+2; |
hudakz | 0:238f0d0c0ba3 | 43 | if (options->password.cstring || options->password.lenstring.data) |
hudakz | 0:238f0d0c0ba3 | 44 | len += MQTTstrlen(options->password)+2; |
hudakz | 0:238f0d0c0ba3 | 45 | |
hudakz | 0:238f0d0c0ba3 | 46 | FUNC_EXIT_RC(len); |
hudakz | 0:238f0d0c0ba3 | 47 | return len; |
hudakz | 0:238f0d0c0ba3 | 48 | } |
hudakz | 0:238f0d0c0ba3 | 49 | |
hudakz | 0:238f0d0c0ba3 | 50 | |
hudakz | 0:238f0d0c0ba3 | 51 | /** |
hudakz | 0:238f0d0c0ba3 | 52 | * Serializes the connect options into the buffer. |
hudakz | 0:238f0d0c0ba3 | 53 | * @param buf the buffer into which the packet will be serialized |
hudakz | 0:238f0d0c0ba3 | 54 | * @param len the length in bytes of the supplied buffer |
hudakz | 0:238f0d0c0ba3 | 55 | * @param options the options to be used to build the connect packet |
hudakz | 0:238f0d0c0ba3 | 56 | * @return serialized length, or error if 0 |
hudakz | 0:238f0d0c0ba3 | 57 | */ |
hudakz | 0:238f0d0c0ba3 | 58 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) |
hudakz | 0:238f0d0c0ba3 | 59 | { |
hudakz | 0:238f0d0c0ba3 | 60 | unsigned char *ptr = buf; |
hudakz | 0:238f0d0c0ba3 | 61 | MQTTHeader header = {0}; |
hudakz | 0:238f0d0c0ba3 | 62 | MQTTConnectFlags flags = {0}; |
hudakz | 0:238f0d0c0ba3 | 63 | int len = 0; |
hudakz | 0:238f0d0c0ba3 | 64 | int rc = -1; |
hudakz | 0:238f0d0c0ba3 | 65 | |
hudakz | 0:238f0d0c0ba3 | 66 | FUNC_ENTRY; |
hudakz | 0:238f0d0c0ba3 | 67 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) |
hudakz | 0:238f0d0c0ba3 | 68 | { |
hudakz | 0:238f0d0c0ba3 | 69 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
hudakz | 0:238f0d0c0ba3 | 70 | goto exit; |
hudakz | 0:238f0d0c0ba3 | 71 | } |
hudakz | 0:238f0d0c0ba3 | 72 | |
hudakz | 0:238f0d0c0ba3 | 73 | header.byte = 0; |
hudakz | 0:238f0d0c0ba3 | 74 | header.bits.type = CONNECT; |
hudakz | 0:238f0d0c0ba3 | 75 | writeChar(&ptr, header.byte); /* write header */ |
hudakz | 0:238f0d0c0ba3 | 76 | |
hudakz | 0:238f0d0c0ba3 | 77 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ |
hudakz | 0:238f0d0c0ba3 | 78 | |
hudakz | 0:238f0d0c0ba3 | 79 | if (options->MQTTVersion == 4) |
hudakz | 0:238f0d0c0ba3 | 80 | { |
hudakz | 0:238f0d0c0ba3 | 81 | writeCString(&ptr, "MQTT"); |
hudakz | 0:238f0d0c0ba3 | 82 | writeChar(&ptr, (char) 4); |
hudakz | 0:238f0d0c0ba3 | 83 | } |
hudakz | 0:238f0d0c0ba3 | 84 | else |
hudakz | 0:238f0d0c0ba3 | 85 | { |
hudakz | 0:238f0d0c0ba3 | 86 | writeCString(&ptr, "MQIsdp"); |
hudakz | 0:238f0d0c0ba3 | 87 | writeChar(&ptr, (char) 3); |
hudakz | 0:238f0d0c0ba3 | 88 | } |
hudakz | 0:238f0d0c0ba3 | 89 | |
hudakz | 0:238f0d0c0ba3 | 90 | flags.all = 0; |
hudakz | 0:238f0d0c0ba3 | 91 | flags.bits.cleansession = options->cleansession; |
hudakz | 0:238f0d0c0ba3 | 92 | flags.bits.will = (options->willFlag) ? 1 : 0; |
hudakz | 0:238f0d0c0ba3 | 93 | if (flags.bits.will) |
hudakz | 0:238f0d0c0ba3 | 94 | { |
hudakz | 0:238f0d0c0ba3 | 95 | flags.bits.willQoS = options->will.qos; |
hudakz | 0:238f0d0c0ba3 | 96 | flags.bits.willRetain = options->will.retained; |
hudakz | 0:238f0d0c0ba3 | 97 | } |
hudakz | 0:238f0d0c0ba3 | 98 | |
hudakz | 0:238f0d0c0ba3 | 99 | if (options->username.cstring || options->username.lenstring.data) |
hudakz | 0:238f0d0c0ba3 | 100 | flags.bits.username = 1; |
hudakz | 0:238f0d0c0ba3 | 101 | if (options->password.cstring || options->password.lenstring.data) |
hudakz | 0:238f0d0c0ba3 | 102 | flags.bits.password = 1; |
hudakz | 0:238f0d0c0ba3 | 103 | |
hudakz | 0:238f0d0c0ba3 | 104 | writeChar(&ptr, flags.all); |
hudakz | 0:238f0d0c0ba3 | 105 | writeInt(&ptr, options->keepAliveInterval); |
hudakz | 0:238f0d0c0ba3 | 106 | writeMQTTString(&ptr, options->clientID); |
hudakz | 0:238f0d0c0ba3 | 107 | if (options->willFlag) |
hudakz | 0:238f0d0c0ba3 | 108 | { |
hudakz | 0:238f0d0c0ba3 | 109 | writeMQTTString(&ptr, options->will.topicName); |
hudakz | 0:238f0d0c0ba3 | 110 | writeMQTTString(&ptr, options->will.message); |
hudakz | 0:238f0d0c0ba3 | 111 | } |
hudakz | 0:238f0d0c0ba3 | 112 | if (flags.bits.username) |
hudakz | 0:238f0d0c0ba3 | 113 | writeMQTTString(&ptr, options->username); |
hudakz | 0:238f0d0c0ba3 | 114 | if (flags.bits.password) |
hudakz | 0:238f0d0c0ba3 | 115 | writeMQTTString(&ptr, options->password); |
hudakz | 0:238f0d0c0ba3 | 116 | |
hudakz | 0:238f0d0c0ba3 | 117 | rc = ptr - buf; |
hudakz | 0:238f0d0c0ba3 | 118 | |
hudakz | 0:238f0d0c0ba3 | 119 | exit: FUNC_EXIT_RC(rc); |
hudakz | 0:238f0d0c0ba3 | 120 | return rc; |
hudakz | 0:238f0d0c0ba3 | 121 | } |
hudakz | 0:238f0d0c0ba3 | 122 | |
hudakz | 0:238f0d0c0ba3 | 123 | |
hudakz | 0:238f0d0c0ba3 | 124 | /** |
hudakz | 0:238f0d0c0ba3 | 125 | * Deserializes the supplied (wire) buffer into connack data - return code |
hudakz | 0:238f0d0c0ba3 | 126 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) |
hudakz | 0:238f0d0c0ba3 | 127 | * @param connack_rc returned integer value of the connack return code |
hudakz | 0:238f0d0c0ba3 | 128 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
hudakz | 0:238f0d0c0ba3 | 129 | * @param len the length in bytes of the data in the supplied buffer |
hudakz | 0:238f0d0c0ba3 | 130 | * @return error code. 1 is success, 0 is failure |
hudakz | 0:238f0d0c0ba3 | 131 | */ |
hudakz | 0:238f0d0c0ba3 | 132 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) |
hudakz | 0:238f0d0c0ba3 | 133 | { |
hudakz | 0:238f0d0c0ba3 | 134 | MQTTHeader header = {0}; |
hudakz | 0:238f0d0c0ba3 | 135 | unsigned char* curdata = buf; |
hudakz | 0:238f0d0c0ba3 | 136 | unsigned char* enddata = NULL; |
hudakz | 0:238f0d0c0ba3 | 137 | int rc = 0; |
hudakz | 0:238f0d0c0ba3 | 138 | int mylen; |
hudakz | 0:238f0d0c0ba3 | 139 | MQTTConnackFlags flags = {0}; |
hudakz | 0:238f0d0c0ba3 | 140 | |
hudakz | 0:238f0d0c0ba3 | 141 | FUNC_ENTRY; |
hudakz | 0:238f0d0c0ba3 | 142 | header.byte = readChar(&curdata); |
hudakz | 0:238f0d0c0ba3 | 143 | if (header.bits.type != CONNACK) |
hudakz | 0:238f0d0c0ba3 | 144 | goto exit; |
hudakz | 0:238f0d0c0ba3 | 145 | |
hudakz | 0:238f0d0c0ba3 | 146 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ |
hudakz | 0:238f0d0c0ba3 | 147 | enddata = curdata + mylen; |
hudakz | 0:238f0d0c0ba3 | 148 | if (enddata - curdata < 2) |
hudakz | 0:238f0d0c0ba3 | 149 | goto exit; |
hudakz | 0:238f0d0c0ba3 | 150 | |
hudakz | 0:238f0d0c0ba3 | 151 | flags.all = readChar(&curdata); |
hudakz | 0:238f0d0c0ba3 | 152 | *sessionPresent = flags.bits.sessionpresent; |
hudakz | 0:238f0d0c0ba3 | 153 | *connack_rc = readChar(&curdata); |
hudakz | 0:238f0d0c0ba3 | 154 | |
hudakz | 0:238f0d0c0ba3 | 155 | rc = 1; |
hudakz | 0:238f0d0c0ba3 | 156 | exit: |
hudakz | 0:238f0d0c0ba3 | 157 | FUNC_EXIT_RC(rc); |
hudakz | 0:238f0d0c0ba3 | 158 | return rc; |
hudakz | 0:238f0d0c0ba3 | 159 | } |
hudakz | 0:238f0d0c0ba3 | 160 | |
hudakz | 0:238f0d0c0ba3 | 161 | |
hudakz | 0:238f0d0c0ba3 | 162 | |
hudakz | 0:238f0d0c0ba3 | 163 | /** |
hudakz | 0:238f0d0c0ba3 | 164 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket |
hudakz | 0:238f0d0c0ba3 | 165 | * @param buf the buffer into which the packet will be serialized |
hudakz | 0:238f0d0c0ba3 | 166 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
hudakz | 0:238f0d0c0ba3 | 167 | * @param packettype the message type |
hudakz | 0:238f0d0c0ba3 | 168 | * @return serialized length, or error if 0 |
hudakz | 0:238f0d0c0ba3 | 169 | */ |
hudakz | 0:238f0d0c0ba3 | 170 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) |
hudakz | 0:238f0d0c0ba3 | 171 | { |
hudakz | 0:238f0d0c0ba3 | 172 | MQTTHeader header = {0}; |
hudakz | 0:238f0d0c0ba3 | 173 | int rc = -1; |
hudakz | 0:238f0d0c0ba3 | 174 | unsigned char *ptr = buf; |
hudakz | 0:238f0d0c0ba3 | 175 | |
hudakz | 0:238f0d0c0ba3 | 176 | FUNC_ENTRY; |
hudakz | 0:238f0d0c0ba3 | 177 | if (buflen < 2) |
hudakz | 0:238f0d0c0ba3 | 178 | { |
hudakz | 0:238f0d0c0ba3 | 179 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
hudakz | 0:238f0d0c0ba3 | 180 | goto exit; |
hudakz | 0:238f0d0c0ba3 | 181 | } |
hudakz | 0:238f0d0c0ba3 | 182 | header.byte = 0; |
hudakz | 0:238f0d0c0ba3 | 183 | header.bits.type = packettype; |
hudakz | 0:238f0d0c0ba3 | 184 | writeChar(&ptr, header.byte); /* write header */ |
hudakz | 0:238f0d0c0ba3 | 185 | |
hudakz | 0:238f0d0c0ba3 | 186 | ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ |
hudakz | 0:238f0d0c0ba3 | 187 | rc = ptr - buf; |
hudakz | 0:238f0d0c0ba3 | 188 | exit: |
hudakz | 0:238f0d0c0ba3 | 189 | FUNC_EXIT_RC(rc); |
hudakz | 0:238f0d0c0ba3 | 190 | return rc; |
hudakz | 0:238f0d0c0ba3 | 191 | } |
hudakz | 0:238f0d0c0ba3 | 192 | |
hudakz | 0:238f0d0c0ba3 | 193 | |
hudakz | 0:238f0d0c0ba3 | 194 | /** |
hudakz | 0:238f0d0c0ba3 | 195 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
hudakz | 0:238f0d0c0ba3 | 196 | * @param buf the buffer into which the packet will be serialized |
hudakz | 0:238f0d0c0ba3 | 197 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
hudakz | 0:238f0d0c0ba3 | 198 | * @return serialized length, or error if 0 |
hudakz | 0:238f0d0c0ba3 | 199 | */ |
hudakz | 0:238f0d0c0ba3 | 200 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) |
hudakz | 0:238f0d0c0ba3 | 201 | { |
hudakz | 0:238f0d0c0ba3 | 202 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); |
hudakz | 0:238f0d0c0ba3 | 203 | } |
hudakz | 0:238f0d0c0ba3 | 204 | |
hudakz | 0:238f0d0c0ba3 | 205 | |
hudakz | 0:238f0d0c0ba3 | 206 | /** |
hudakz | 0:238f0d0c0ba3 | 207 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
hudakz | 0:238f0d0c0ba3 | 208 | * @param buf the buffer into which the packet will be serialized |
hudakz | 0:238f0d0c0ba3 | 209 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
hudakz | 0:238f0d0c0ba3 | 210 | * @return serialized length, or error if 0 |
hudakz | 0:238f0d0c0ba3 | 211 | */ |
hudakz | 0:238f0d0c0ba3 | 212 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) |
hudakz | 0:238f0d0c0ba3 | 213 | { |
hudakz | 0:238f0d0c0ba3 | 214 | return MQTTSerialize_zero(buf, buflen, PINGREQ); |
hudakz | 0:238f0d0c0ba3 | 215 | } |