Vergil Cola
/
MQTTGateway2
Fork of my original MQTTGateway
MQTTSManager/MQTTPacket/MQTTConnectClient.c@0:a1734fe1ec4b, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:43:14 2017 +0000
- Revision:
- 0:a1734fe1ec4b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:a1734fe1ec4b | 1 | /******************************************************************************* |
vpcola | 0:a1734fe1ec4b | 2 | * Copyright (c) 2014 IBM Corp. |
vpcola | 0:a1734fe1ec4b | 3 | * |
vpcola | 0:a1734fe1ec4b | 4 | * All rights reserved. This program and the accompanying materials |
vpcola | 0:a1734fe1ec4b | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
vpcola | 0:a1734fe1ec4b | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
vpcola | 0:a1734fe1ec4b | 7 | * |
vpcola | 0:a1734fe1ec4b | 8 | * The Eclipse Public License is available at |
vpcola | 0:a1734fe1ec4b | 9 | * http://www.eclipse.org/legal/epl-v10.html |
vpcola | 0:a1734fe1ec4b | 10 | * and the Eclipse Distribution License is available at |
vpcola | 0:a1734fe1ec4b | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
vpcola | 0:a1734fe1ec4b | 12 | * |
vpcola | 0:a1734fe1ec4b | 13 | * Contributors: |
vpcola | 0:a1734fe1ec4b | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
vpcola | 0:a1734fe1ec4b | 15 | *******************************************************************************/ |
vpcola | 0:a1734fe1ec4b | 16 | |
vpcola | 0:a1734fe1ec4b | 17 | #include "MQTTPacket.h" |
vpcola | 0:a1734fe1ec4b | 18 | #include "StackTrace.h" |
vpcola | 0:a1734fe1ec4b | 19 | |
vpcola | 0:a1734fe1ec4b | 20 | #include <string.h> |
vpcola | 0:a1734fe1ec4b | 21 | |
vpcola | 0:a1734fe1ec4b | 22 | /** |
vpcola | 0:a1734fe1ec4b | 23 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. |
vpcola | 0:a1734fe1ec4b | 24 | * @param options the options to be used to build the connect packet |
vpcola | 0:a1734fe1ec4b | 25 | * @return the length of buffer needed to contain the serialized version of the packet |
vpcola | 0:a1734fe1ec4b | 26 | */ |
vpcola | 0:a1734fe1ec4b | 27 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) |
vpcola | 0:a1734fe1ec4b | 28 | { |
vpcola | 0:a1734fe1ec4b | 29 | int len = 0; |
vpcola | 0:a1734fe1ec4b | 30 | |
vpcola | 0:a1734fe1ec4b | 31 | FUNC_ENTRY; |
vpcola | 0:a1734fe1ec4b | 32 | |
vpcola | 0:a1734fe1ec4b | 33 | if (options->MQTTVersion == 3) |
vpcola | 0:a1734fe1ec4b | 34 | len = 12; /* variable depending on MQTT or MQIsdp */ |
vpcola | 0:a1734fe1ec4b | 35 | else if (options->MQTTVersion == 4) |
vpcola | 0:a1734fe1ec4b | 36 | len = 10; |
vpcola | 0:a1734fe1ec4b | 37 | |
vpcola | 0:a1734fe1ec4b | 38 | len += MQTTstrlen(options->clientID)+2; |
vpcola | 0:a1734fe1ec4b | 39 | if (options->willFlag) |
vpcola | 0:a1734fe1ec4b | 40 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; |
vpcola | 0:a1734fe1ec4b | 41 | if (options->username.cstring || options->username.lenstring.data) |
vpcola | 0:a1734fe1ec4b | 42 | len += MQTTstrlen(options->username)+2; |
vpcola | 0:a1734fe1ec4b | 43 | if (options->password.cstring || options->password.lenstring.data) |
vpcola | 0:a1734fe1ec4b | 44 | len += MQTTstrlen(options->password)+2; |
vpcola | 0:a1734fe1ec4b | 45 | |
vpcola | 0:a1734fe1ec4b | 46 | FUNC_EXIT_RC(len); |
vpcola | 0:a1734fe1ec4b | 47 | return len; |
vpcola | 0:a1734fe1ec4b | 48 | } |
vpcola | 0:a1734fe1ec4b | 49 | |
vpcola | 0:a1734fe1ec4b | 50 | |
vpcola | 0:a1734fe1ec4b | 51 | /** |
vpcola | 0:a1734fe1ec4b | 52 | * Serializes the connect options into the buffer. |
vpcola | 0:a1734fe1ec4b | 53 | * @param buf the buffer into which the packet will be serialized |
vpcola | 0:a1734fe1ec4b | 54 | * @param len the length in bytes of the supplied buffer |
vpcola | 0:a1734fe1ec4b | 55 | * @param options the options to be used to build the connect packet |
vpcola | 0:a1734fe1ec4b | 56 | * @return serialized length, or error if 0 |
vpcola | 0:a1734fe1ec4b | 57 | */ |
vpcola | 0:a1734fe1ec4b | 58 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) |
vpcola | 0:a1734fe1ec4b | 59 | { |
vpcola | 0:a1734fe1ec4b | 60 | unsigned char *ptr = buf; |
vpcola | 0:a1734fe1ec4b | 61 | MQTTHeader header = {0}; |
vpcola | 0:a1734fe1ec4b | 62 | MQTTConnectFlags flags = {0}; |
vpcola | 0:a1734fe1ec4b | 63 | int len = 0; |
vpcola | 0:a1734fe1ec4b | 64 | int rc = -1; |
vpcola | 0:a1734fe1ec4b | 65 | |
vpcola | 0:a1734fe1ec4b | 66 | FUNC_ENTRY; |
vpcola | 0:a1734fe1ec4b | 67 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) |
vpcola | 0:a1734fe1ec4b | 68 | { |
vpcola | 0:a1734fe1ec4b | 69 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
vpcola | 0:a1734fe1ec4b | 70 | goto exit; |
vpcola | 0:a1734fe1ec4b | 71 | } |
vpcola | 0:a1734fe1ec4b | 72 | |
vpcola | 0:a1734fe1ec4b | 73 | header.byte = 0; |
vpcola | 0:a1734fe1ec4b | 74 | header.bits.type = CONNECT; |
vpcola | 0:a1734fe1ec4b | 75 | writeChar(&ptr, header.byte); /* write header */ |
vpcola | 0:a1734fe1ec4b | 76 | |
vpcola | 0:a1734fe1ec4b | 77 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ |
vpcola | 0:a1734fe1ec4b | 78 | |
vpcola | 0:a1734fe1ec4b | 79 | if (options->MQTTVersion == 4) |
vpcola | 0:a1734fe1ec4b | 80 | { |
vpcola | 0:a1734fe1ec4b | 81 | writeCString(&ptr, "MQTT"); |
vpcola | 0:a1734fe1ec4b | 82 | writeChar(&ptr, (char) 4); |
vpcola | 0:a1734fe1ec4b | 83 | } |
vpcola | 0:a1734fe1ec4b | 84 | else |
vpcola | 0:a1734fe1ec4b | 85 | { |
vpcola | 0:a1734fe1ec4b | 86 | writeCString(&ptr, "MQIsdp"); |
vpcola | 0:a1734fe1ec4b | 87 | writeChar(&ptr, (char) 3); |
vpcola | 0:a1734fe1ec4b | 88 | } |
vpcola | 0:a1734fe1ec4b | 89 | |
vpcola | 0:a1734fe1ec4b | 90 | flags.all = 0; |
vpcola | 0:a1734fe1ec4b | 91 | flags.bits.cleansession = options->cleansession; |
vpcola | 0:a1734fe1ec4b | 92 | flags.bits.will = (options->willFlag) ? 1 : 0; |
vpcola | 0:a1734fe1ec4b | 93 | if (flags.bits.will) |
vpcola | 0:a1734fe1ec4b | 94 | { |
vpcola | 0:a1734fe1ec4b | 95 | flags.bits.willQoS = options->will.qos; |
vpcola | 0:a1734fe1ec4b | 96 | flags.bits.willRetain = options->will.retained; |
vpcola | 0:a1734fe1ec4b | 97 | } |
vpcola | 0:a1734fe1ec4b | 98 | |
vpcola | 0:a1734fe1ec4b | 99 | if (options->username.cstring || options->username.lenstring.data) |
vpcola | 0:a1734fe1ec4b | 100 | flags.bits.username = 1; |
vpcola | 0:a1734fe1ec4b | 101 | if (options->password.cstring || options->password.lenstring.data) |
vpcola | 0:a1734fe1ec4b | 102 | flags.bits.password = 1; |
vpcola | 0:a1734fe1ec4b | 103 | |
vpcola | 0:a1734fe1ec4b | 104 | writeChar(&ptr, flags.all); |
vpcola | 0:a1734fe1ec4b | 105 | writeInt(&ptr, options->keepAliveInterval); |
vpcola | 0:a1734fe1ec4b | 106 | writeMQTTString(&ptr, options->clientID); |
vpcola | 0:a1734fe1ec4b | 107 | if (options->willFlag) |
vpcola | 0:a1734fe1ec4b | 108 | { |
vpcola | 0:a1734fe1ec4b | 109 | writeMQTTString(&ptr, options->will.topicName); |
vpcola | 0:a1734fe1ec4b | 110 | writeMQTTString(&ptr, options->will.message); |
vpcola | 0:a1734fe1ec4b | 111 | } |
vpcola | 0:a1734fe1ec4b | 112 | if (flags.bits.username) |
vpcola | 0:a1734fe1ec4b | 113 | writeMQTTString(&ptr, options->username); |
vpcola | 0:a1734fe1ec4b | 114 | if (flags.bits.password) |
vpcola | 0:a1734fe1ec4b | 115 | writeMQTTString(&ptr, options->password); |
vpcola | 0:a1734fe1ec4b | 116 | |
vpcola | 0:a1734fe1ec4b | 117 | rc = ptr - buf; |
vpcola | 0:a1734fe1ec4b | 118 | |
vpcola | 0:a1734fe1ec4b | 119 | exit: FUNC_EXIT_RC(rc); |
vpcola | 0:a1734fe1ec4b | 120 | return rc; |
vpcola | 0:a1734fe1ec4b | 121 | } |
vpcola | 0:a1734fe1ec4b | 122 | |
vpcola | 0:a1734fe1ec4b | 123 | |
vpcola | 0:a1734fe1ec4b | 124 | /** |
vpcola | 0:a1734fe1ec4b | 125 | * Deserializes the supplied (wire) buffer into connack data - return code |
vpcola | 0:a1734fe1ec4b | 126 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) |
vpcola | 0:a1734fe1ec4b | 127 | * @param connack_rc returned integer value of the connack return code |
vpcola | 0:a1734fe1ec4b | 128 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
vpcola | 0:a1734fe1ec4b | 129 | * @param len the length in bytes of the data in the supplied buffer |
vpcola | 0:a1734fe1ec4b | 130 | * @return error code. 1 is success, 0 is failure |
vpcola | 0:a1734fe1ec4b | 131 | */ |
vpcola | 0:a1734fe1ec4b | 132 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) |
vpcola | 0:a1734fe1ec4b | 133 | { |
vpcola | 0:a1734fe1ec4b | 134 | MQTTHeader header = {0}; |
vpcola | 0:a1734fe1ec4b | 135 | unsigned char* curdata = buf; |
vpcola | 0:a1734fe1ec4b | 136 | unsigned char* enddata = NULL; |
vpcola | 0:a1734fe1ec4b | 137 | int rc = 0; |
vpcola | 0:a1734fe1ec4b | 138 | int mylen; |
vpcola | 0:a1734fe1ec4b | 139 | MQTTConnackFlags flags = {0}; |
vpcola | 0:a1734fe1ec4b | 140 | |
vpcola | 0:a1734fe1ec4b | 141 | FUNC_ENTRY; |
vpcola | 0:a1734fe1ec4b | 142 | header.byte = readChar(&curdata); |
vpcola | 0:a1734fe1ec4b | 143 | if (header.bits.type != CONNACK) |
vpcola | 0:a1734fe1ec4b | 144 | goto exit; |
vpcola | 0:a1734fe1ec4b | 145 | |
vpcola | 0:a1734fe1ec4b | 146 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ |
vpcola | 0:a1734fe1ec4b | 147 | enddata = curdata + mylen; |
vpcola | 0:a1734fe1ec4b | 148 | if (enddata - curdata < 2) |
vpcola | 0:a1734fe1ec4b | 149 | goto exit; |
vpcola | 0:a1734fe1ec4b | 150 | |
vpcola | 0:a1734fe1ec4b | 151 | flags.all = readChar(&curdata); |
vpcola | 0:a1734fe1ec4b | 152 | *sessionPresent = flags.bits.sessionpresent; |
vpcola | 0:a1734fe1ec4b | 153 | *connack_rc = readChar(&curdata); |
vpcola | 0:a1734fe1ec4b | 154 | |
vpcola | 0:a1734fe1ec4b | 155 | rc = 1; |
vpcola | 0:a1734fe1ec4b | 156 | exit: |
vpcola | 0:a1734fe1ec4b | 157 | FUNC_EXIT_RC(rc); |
vpcola | 0:a1734fe1ec4b | 158 | return rc; |
vpcola | 0:a1734fe1ec4b | 159 | } |
vpcola | 0:a1734fe1ec4b | 160 | |
vpcola | 0:a1734fe1ec4b | 161 | |
vpcola | 0:a1734fe1ec4b | 162 | |
vpcola | 0:a1734fe1ec4b | 163 | /** |
vpcola | 0:a1734fe1ec4b | 164 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket |
vpcola | 0:a1734fe1ec4b | 165 | * @param buf the buffer into which the packet will be serialized |
vpcola | 0:a1734fe1ec4b | 166 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
vpcola | 0:a1734fe1ec4b | 167 | * @param packettype the message type |
vpcola | 0:a1734fe1ec4b | 168 | * @return serialized length, or error if 0 |
vpcola | 0:a1734fe1ec4b | 169 | */ |
vpcola | 0:a1734fe1ec4b | 170 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) |
vpcola | 0:a1734fe1ec4b | 171 | { |
vpcola | 0:a1734fe1ec4b | 172 | MQTTHeader header = {0}; |
vpcola | 0:a1734fe1ec4b | 173 | int rc = -1; |
vpcola | 0:a1734fe1ec4b | 174 | unsigned char *ptr = buf; |
vpcola | 0:a1734fe1ec4b | 175 | |
vpcola | 0:a1734fe1ec4b | 176 | FUNC_ENTRY; |
vpcola | 0:a1734fe1ec4b | 177 | if (buflen < 2) |
vpcola | 0:a1734fe1ec4b | 178 | { |
vpcola | 0:a1734fe1ec4b | 179 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
vpcola | 0:a1734fe1ec4b | 180 | goto exit; |
vpcola | 0:a1734fe1ec4b | 181 | } |
vpcola | 0:a1734fe1ec4b | 182 | header.byte = 0; |
vpcola | 0:a1734fe1ec4b | 183 | header.bits.type = packettype; |
vpcola | 0:a1734fe1ec4b | 184 | writeChar(&ptr, header.byte); /* write header */ |
vpcola | 0:a1734fe1ec4b | 185 | |
vpcola | 0:a1734fe1ec4b | 186 | ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ |
vpcola | 0:a1734fe1ec4b | 187 | rc = ptr - buf; |
vpcola | 0:a1734fe1ec4b | 188 | exit: |
vpcola | 0:a1734fe1ec4b | 189 | FUNC_EXIT_RC(rc); |
vpcola | 0:a1734fe1ec4b | 190 | return rc; |
vpcola | 0:a1734fe1ec4b | 191 | } |
vpcola | 0:a1734fe1ec4b | 192 | |
vpcola | 0:a1734fe1ec4b | 193 | |
vpcola | 0:a1734fe1ec4b | 194 | /** |
vpcola | 0:a1734fe1ec4b | 195 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
vpcola | 0:a1734fe1ec4b | 196 | * @param buf the buffer into which the packet will be serialized |
vpcola | 0:a1734fe1ec4b | 197 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
vpcola | 0:a1734fe1ec4b | 198 | * @return serialized length, or error if 0 |
vpcola | 0:a1734fe1ec4b | 199 | */ |
vpcola | 0:a1734fe1ec4b | 200 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) |
vpcola | 0:a1734fe1ec4b | 201 | { |
vpcola | 0:a1734fe1ec4b | 202 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); |
vpcola | 0:a1734fe1ec4b | 203 | } |
vpcola | 0:a1734fe1ec4b | 204 | |
vpcola | 0:a1734fe1ec4b | 205 | |
vpcola | 0:a1734fe1ec4b | 206 | /** |
vpcola | 0:a1734fe1ec4b | 207 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
vpcola | 0:a1734fe1ec4b | 208 | * @param buf the buffer into which the packet will be serialized |
vpcola | 0:a1734fe1ec4b | 209 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
vpcola | 0:a1734fe1ec4b | 210 | * @return serialized length, or error if 0 |
vpcola | 0:a1734fe1ec4b | 211 | */ |
vpcola | 0:a1734fe1ec4b | 212 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) |
vpcola | 0:a1734fe1ec4b | 213 | { |
vpcola | 0:a1734fe1ec4b | 214 | return MQTTSerialize_zero(buf, buflen, PINGREQ); |
vpcola | 0:a1734fe1ec4b | 215 | } |