123
MQTTSNConnectClient.c@2:573d544b5d3c, 2019-01-03 (annotated)
- Committer:
- m_ahsan
- Date:
- Thu Jan 03 05:49:15 2019 +0000
- Revision:
- 2:573d544b5d3c
- Parent:
- 1:7fa362fa563f
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 0:c524a894b5e8 | 1 | /******************************************************************************* |
icraggs | 0:c524a894b5e8 | 2 | * Copyright (c) 2014, 2015 IBM Corp. |
icraggs | 0:c524a894b5e8 | 3 | * |
icraggs | 0:c524a894b5e8 | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 0:c524a894b5e8 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 0:c524a894b5e8 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
icraggs | 0:c524a894b5e8 | 7 | * |
icraggs | 0:c524a894b5e8 | 8 | * The Eclipse Public License is available at |
icraggs | 0:c524a894b5e8 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 0:c524a894b5e8 | 10 | * and the Eclipse Distribution License is available at |
icraggs | 0:c524a894b5e8 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
icraggs | 0:c524a894b5e8 | 12 | * |
icraggs | 0:c524a894b5e8 | 13 | * Contributors: |
icraggs | 0:c524a894b5e8 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 0:c524a894b5e8 | 15 | * Nicholas Humfrey - Reformatting to make more consistent; bug 453862 |
icraggs | 0:c524a894b5e8 | 16 | *******************************************************************************/ |
icraggs | 0:c524a894b5e8 | 17 | |
icraggs | 0:c524a894b5e8 | 18 | #include "MQTTSNPacket.h" |
icraggs | 0:c524a894b5e8 | 19 | #include "StackTrace.h" |
icraggs | 0:c524a894b5e8 | 20 | |
icraggs | 0:c524a894b5e8 | 21 | #include <string.h> |
icraggs | 0:c524a894b5e8 | 22 | |
icraggs | 0:c524a894b5e8 | 23 | /** |
icraggs | 0:c524a894b5e8 | 24 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. |
icraggs | 0:c524a894b5e8 | 25 | * @param options the options to be used to build the connect packet |
icraggs | 0:c524a894b5e8 | 26 | * @return the length of buffer needed to contain the serialized version of the packet |
icraggs | 0:c524a894b5e8 | 27 | */ |
icraggs | 0:c524a894b5e8 | 28 | int MQTTSNSerialize_connectLength(MQTTSNPacket_connectData* options) |
icraggs | 0:c524a894b5e8 | 29 | { |
icraggs | 0:c524a894b5e8 | 30 | int len = 0; |
icraggs | 0:c524a894b5e8 | 31 | |
icraggs | 0:c524a894b5e8 | 32 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 33 | len = 5 + MQTTSNstrlen(options->clientID); |
icraggs | 0:c524a894b5e8 | 34 | FUNC_EXIT_RC(len); |
icraggs | 0:c524a894b5e8 | 35 | return len; |
icraggs | 0:c524a894b5e8 | 36 | } |
icraggs | 0:c524a894b5e8 | 37 | |
icraggs | 0:c524a894b5e8 | 38 | |
icraggs | 0:c524a894b5e8 | 39 | /** |
icraggs | 0:c524a894b5e8 | 40 | * Serializes the connect options into the buffer. |
icraggs | 0:c524a894b5e8 | 41 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 42 | * @param len the length in bytes of the supplied buffer |
icraggs | 0:c524a894b5e8 | 43 | * @param options the options to be used to build the connect packet |
icraggs | 0:c524a894b5e8 | 44 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 45 | */ |
icraggs | 0:c524a894b5e8 | 46 | int MQTTSNSerialize_connect(unsigned char* buf, int buflen, MQTTSNPacket_connectData* options) |
icraggs | 0:c524a894b5e8 | 47 | { |
icraggs | 0:c524a894b5e8 | 48 | unsigned char *ptr = buf; |
icraggs | 0:c524a894b5e8 | 49 | MQTTSNFlags flags = {0}; |
icraggs | 0:c524a894b5e8 | 50 | int len = 0; |
icraggs | 0:c524a894b5e8 | 51 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 52 | |
icraggs | 0:c524a894b5e8 | 53 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 54 | if ((len = MQTTSNPacket_len(MQTTSNSerialize_connectLength(options))) > buflen) |
icraggs | 0:c524a894b5e8 | 55 | { |
icraggs | 0:c524a894b5e8 | 56 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 57 | goto exit; |
icraggs | 0:c524a894b5e8 | 58 | } |
icraggs | 0:c524a894b5e8 | 59 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
icraggs | 1:7fa362fa563f | 60 | MQTTSNPacket_writeChar(&ptr, MQTTSN_CONNECT); /* write message type */ |
icraggs | 0:c524a894b5e8 | 61 | |
icraggs | 0:c524a894b5e8 | 62 | flags.all = 0; |
icraggs | 0:c524a894b5e8 | 63 | flags.bits.cleanSession = options->cleansession; |
icraggs | 0:c524a894b5e8 | 64 | flags.bits.will = options->willFlag; |
icraggs | 1:7fa362fa563f | 65 | MQTTSNPacket_writeChar(&ptr, flags.all); |
icraggs | 1:7fa362fa563f | 66 | MQTTSNPacket_writeChar(&ptr, 0x01); /* protocol ID */ |
icraggs | 1:7fa362fa563f | 67 | MQTTSNPacket_writeInt(&ptr, options->duration); |
icraggs | 0:c524a894b5e8 | 68 | writeMQTTSNString(&ptr, options->clientID); |
icraggs | 0:c524a894b5e8 | 69 | |
icraggs | 0:c524a894b5e8 | 70 | rc = ptr - buf; |
icraggs | 0:c524a894b5e8 | 71 | exit: |
icraggs | 0:c524a894b5e8 | 72 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 73 | return rc; |
icraggs | 0:c524a894b5e8 | 74 | } |
icraggs | 0:c524a894b5e8 | 75 | |
icraggs | 0:c524a894b5e8 | 76 | |
icraggs | 0:c524a894b5e8 | 77 | /** |
icraggs | 0:c524a894b5e8 | 78 | * Deserializes the supplied (wire) buffer into connack data - return code |
icraggs | 0:c524a894b5e8 | 79 | * @param connack_rc returned integer value of the connack return code |
icraggs | 0:c524a894b5e8 | 80 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 81 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 82 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 83 | */ |
icraggs | 0:c524a894b5e8 | 84 | int MQTTSNDeserialize_connack(int* connack_rc, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 85 | { |
icraggs | 0:c524a894b5e8 | 86 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 87 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 88 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 89 | int mylen; |
icraggs | 0:c524a894b5e8 | 90 | |
icraggs | 0:c524a894b5e8 | 91 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 92 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 93 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 94 | if (enddata - buf < 3) |
icraggs | 0:c524a894b5e8 | 95 | goto exit; |
icraggs | 0:c524a894b5e8 | 96 | |
icraggs | 1:7fa362fa563f | 97 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_CONNACK) |
icraggs | 0:c524a894b5e8 | 98 | goto exit; |
icraggs | 0:c524a894b5e8 | 99 | |
icraggs | 1:7fa362fa563f | 100 | *connack_rc = MQTTSNPacket_readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 101 | |
icraggs | 0:c524a894b5e8 | 102 | rc = 1; |
icraggs | 0:c524a894b5e8 | 103 | exit: |
icraggs | 0:c524a894b5e8 | 104 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 105 | return rc; |
icraggs | 0:c524a894b5e8 | 106 | } |
icraggs | 0:c524a894b5e8 | 107 | |
icraggs | 0:c524a894b5e8 | 108 | |
icraggs | 0:c524a894b5e8 | 109 | /** |
icraggs | 0:c524a894b5e8 | 110 | * Determines the length of the MQTT disconnect packet (without length field) |
icraggs | 0:c524a894b5e8 | 111 | * @param duration the parameter used for the disconnect |
icraggs | 0:c524a894b5e8 | 112 | * @return the length of buffer needed to contain the serialized version of the packet |
icraggs | 0:c524a894b5e8 | 113 | */ |
icraggs | 0:c524a894b5e8 | 114 | int MQTTSNSerialize_disconnectLength(int duration) |
icraggs | 0:c524a894b5e8 | 115 | { |
icraggs | 0:c524a894b5e8 | 116 | int len = 0; |
icraggs | 0:c524a894b5e8 | 117 | |
icraggs | 0:c524a894b5e8 | 118 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 119 | len = (duration >= 0) ? 3 : 1; |
icraggs | 0:c524a894b5e8 | 120 | FUNC_EXIT_RC(len); |
icraggs | 0:c524a894b5e8 | 121 | return len; |
icraggs | 0:c524a894b5e8 | 122 | } |
icraggs | 0:c524a894b5e8 | 123 | |
icraggs | 0:c524a894b5e8 | 124 | |
icraggs | 0:c524a894b5e8 | 125 | /** |
icraggs | 0:c524a894b5e8 | 126 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
icraggs | 0:c524a894b5e8 | 127 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 128 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
icraggs | 0:c524a894b5e8 | 129 | * @param duration optional duration, not added to packet if < 0 |
icraggs | 0:c524a894b5e8 | 130 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 131 | */ |
icraggs | 0:c524a894b5e8 | 132 | int MQTTSNSerialize_disconnect(unsigned char* buf, int buflen, int duration) |
icraggs | 0:c524a894b5e8 | 133 | { |
icraggs | 0:c524a894b5e8 | 134 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 135 | unsigned char *ptr = buf; |
icraggs | 0:c524a894b5e8 | 136 | int len = 0; |
icraggs | 0:c524a894b5e8 | 137 | |
icraggs | 0:c524a894b5e8 | 138 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 139 | if ((len = MQTTSNPacket_len(MQTTSNSerialize_disconnectLength(duration))) > buflen) |
icraggs | 0:c524a894b5e8 | 140 | { |
icraggs | 0:c524a894b5e8 | 141 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 142 | goto exit; |
icraggs | 0:c524a894b5e8 | 143 | } |
icraggs | 0:c524a894b5e8 | 144 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
icraggs | 1:7fa362fa563f | 145 | MQTTSNPacket_writeChar(&ptr, MQTTSN_DISCONNECT); /* write message type */ |
icraggs | 0:c524a894b5e8 | 146 | |
icraggs | 0:c524a894b5e8 | 147 | if (duration >= 0) |
icraggs | 1:7fa362fa563f | 148 | MQTTSNPacket_writeInt(&ptr, duration); |
icraggs | 0:c524a894b5e8 | 149 | |
icraggs | 0:c524a894b5e8 | 150 | rc = ptr - buf; |
icraggs | 0:c524a894b5e8 | 151 | exit: |
icraggs | 0:c524a894b5e8 | 152 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 153 | return rc; |
icraggs | 0:c524a894b5e8 | 154 | } |
icraggs | 0:c524a894b5e8 | 155 | |
icraggs | 0:c524a894b5e8 | 156 | |
icraggs | 0:c524a894b5e8 | 157 | /** |
icraggs | 0:c524a894b5e8 | 158 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket |
icraggs | 0:c524a894b5e8 | 159 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 160 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns |
icraggs | 0:c524a894b5e8 | 161 | * @param clientid optional string, not added to packet string == NULL |
icraggs | 0:c524a894b5e8 | 162 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 163 | */ |
icraggs | 0:c524a894b5e8 | 164 | int MQTTSNSerialize_pingreq(unsigned char* buf, int buflen, MQTTSNString clientid) |
icraggs | 0:c524a894b5e8 | 165 | { |
icraggs | 0:c524a894b5e8 | 166 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 167 | unsigned char *ptr = buf; |
icraggs | 0:c524a894b5e8 | 168 | int len = 0; |
icraggs | 0:c524a894b5e8 | 169 | |
icraggs | 0:c524a894b5e8 | 170 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 171 | if ((len = MQTTSNPacket_len(MQTTSNstrlen(clientid) + 1)) > buflen) |
icraggs | 0:c524a894b5e8 | 172 | { |
icraggs | 0:c524a894b5e8 | 173 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 174 | goto exit; |
icraggs | 0:c524a894b5e8 | 175 | } |
icraggs | 0:c524a894b5e8 | 176 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
icraggs | 1:7fa362fa563f | 177 | MQTTSNPacket_writeChar(&ptr, MQTTSN_PINGREQ); /* write message type */ |
icraggs | 0:c524a894b5e8 | 178 | |
icraggs | 0:c524a894b5e8 | 179 | writeMQTTSNString(&ptr, clientid); |
icraggs | 0:c524a894b5e8 | 180 | |
icraggs | 0:c524a894b5e8 | 181 | rc = ptr - buf; |
icraggs | 0:c524a894b5e8 | 182 | exit: |
icraggs | 0:c524a894b5e8 | 183 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 184 | return rc; |
icraggs | 0:c524a894b5e8 | 185 | } |
icraggs | 0:c524a894b5e8 | 186 | |
icraggs | 0:c524a894b5e8 | 187 | |
icraggs | 0:c524a894b5e8 | 188 | /** |
icraggs | 0:c524a894b5e8 | 189 | * Deserializes the supplied (wire) buffer |
icraggs | 0:c524a894b5e8 | 190 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 191 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 192 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 193 | */ |
icraggs | 0:c524a894b5e8 | 194 | int MQTTSNDeserialize_pingresp(unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 195 | { |
icraggs | 0:c524a894b5e8 | 196 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 197 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 198 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 199 | int mylen; |
icraggs | 0:c524a894b5e8 | 200 | |
icraggs | 0:c524a894b5e8 | 201 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 202 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 203 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 204 | if (enddata - curdata < 2) |
icraggs | 0:c524a894b5e8 | 205 | goto exit; |
icraggs | 0:c524a894b5e8 | 206 | |
icraggs | 1:7fa362fa563f | 207 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_PINGRESP) |
icraggs | 0:c524a894b5e8 | 208 | goto exit; |
icraggs | 0:c524a894b5e8 | 209 | |
icraggs | 0:c524a894b5e8 | 210 | rc = 1; |
icraggs | 0:c524a894b5e8 | 211 | exit: |
icraggs | 0:c524a894b5e8 | 212 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 213 | return rc; |
icraggs | 0:c524a894b5e8 | 214 | } |
icraggs | 0:c524a894b5e8 | 215 | |
icraggs | 0:c524a894b5e8 | 216 | |
icraggs | 0:c524a894b5e8 | 217 | /** |
icraggs | 0:c524a894b5e8 | 218 | * Serializes a willtopic or willtopicupd packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 219 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 220 | * @param len the length in bytes of the supplied buffer |
icraggs | 0:c524a894b5e8 | 221 | * @param willQoS the qos of the will message |
icraggs | 0:c524a894b5e8 | 222 | * @param willRetain the retained flag of the will message |
icraggs | 0:c524a894b5e8 | 223 | * @param willTopic the topic of the will message |
icraggs | 0:c524a894b5e8 | 224 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 225 | */ |
icraggs | 0:c524a894b5e8 | 226 | int MQTTSNSerialize_willtopic1(unsigned char* buf, int buflen, int willQoS, unsigned char willRetain, MQTTSNString willTopic, |
icraggs | 0:c524a894b5e8 | 227 | enum MQTTSN_msgTypes packet_type) |
icraggs | 0:c524a894b5e8 | 228 | { |
icraggs | 0:c524a894b5e8 | 229 | unsigned char *ptr = buf; |
icraggs | 0:c524a894b5e8 | 230 | MQTTSNFlags flags = {0}; |
icraggs | 0:c524a894b5e8 | 231 | int len = 0; |
icraggs | 0:c524a894b5e8 | 232 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 233 | |
icraggs | 0:c524a894b5e8 | 234 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 235 | if ((len = MQTTSNPacket_len(MQTTSNstrlen(willTopic) + 2)) > buflen) |
icraggs | 0:c524a894b5e8 | 236 | { |
icraggs | 0:c524a894b5e8 | 237 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 238 | goto exit; |
icraggs | 0:c524a894b5e8 | 239 | } |
icraggs | 0:c524a894b5e8 | 240 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
icraggs | 1:7fa362fa563f | 241 | MQTTSNPacket_writeChar(&ptr, packet_type); /* write message type */ |
icraggs | 0:c524a894b5e8 | 242 | |
icraggs | 0:c524a894b5e8 | 243 | flags.all = 0; |
icraggs | 0:c524a894b5e8 | 244 | flags.bits.QoS = willQoS; |
icraggs | 0:c524a894b5e8 | 245 | flags.bits.retain = willRetain; |
icraggs | 1:7fa362fa563f | 246 | MQTTSNPacket_writeChar(&ptr, flags.all); |
icraggs | 0:c524a894b5e8 | 247 | |
icraggs | 0:c524a894b5e8 | 248 | writeMQTTSNString(&ptr, willTopic); |
icraggs | 0:c524a894b5e8 | 249 | |
icraggs | 0:c524a894b5e8 | 250 | rc = ptr - buf; |
icraggs | 0:c524a894b5e8 | 251 | |
icraggs | 0:c524a894b5e8 | 252 | exit: |
icraggs | 0:c524a894b5e8 | 253 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 254 | return rc; |
icraggs | 0:c524a894b5e8 | 255 | } |
icraggs | 0:c524a894b5e8 | 256 | |
icraggs | 0:c524a894b5e8 | 257 | |
icraggs | 0:c524a894b5e8 | 258 | /** |
icraggs | 0:c524a894b5e8 | 259 | * Serializes a willtopicupd packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 260 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 261 | * @param len the length in bytes of the supplied buffer |
icraggs | 0:c524a894b5e8 | 262 | * @param willQoS the qos of the will message |
icraggs | 0:c524a894b5e8 | 263 | * @param willRetain the retained flag of the will message |
icraggs | 0:c524a894b5e8 | 264 | * @param willTopic the topic of the will message |
icraggs | 0:c524a894b5e8 | 265 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 266 | */ |
icraggs | 0:c524a894b5e8 | 267 | int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, unsigned char willRetain, MQTTSNString willTopic) |
icraggs | 0:c524a894b5e8 | 268 | { |
icraggs | 0:c524a894b5e8 | 269 | return MQTTSNSerialize_willtopic1(buf, buflen, willQoS, willRetain, willTopic, MQTTSN_WILLTOPICUPD); |
icraggs | 0:c524a894b5e8 | 270 | } |
icraggs | 0:c524a894b5e8 | 271 | |
icraggs | 0:c524a894b5e8 | 272 | |
icraggs | 0:c524a894b5e8 | 273 | /** |
icraggs | 0:c524a894b5e8 | 274 | * Serializes a willtopic packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 275 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 276 | * @param len the length in bytes of the supplied buffer |
icraggs | 0:c524a894b5e8 | 277 | * @param willQoS the qos of the will message |
icraggs | 0:c524a894b5e8 | 278 | * @param willRetain the retained flag of the will message |
icraggs | 0:c524a894b5e8 | 279 | * @param willTopic the topic of the will message |
icraggs | 0:c524a894b5e8 | 280 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 281 | */ |
icraggs | 0:c524a894b5e8 | 282 | int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, unsigned char willRetain, MQTTSNString willTopic) |
icraggs | 0:c524a894b5e8 | 283 | { |
icraggs | 0:c524a894b5e8 | 284 | return MQTTSNSerialize_willtopic1(buf, buflen, willQoS, willRetain, willTopic, MQTTSN_WILLTOPIC); |
icraggs | 0:c524a894b5e8 | 285 | } |
icraggs | 0:c524a894b5e8 | 286 | |
icraggs | 0:c524a894b5e8 | 287 | |
icraggs | 0:c524a894b5e8 | 288 | /** |
icraggs | 0:c524a894b5e8 | 289 | * Serializes a willmsg or willmsgupd packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 290 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 291 | * @param buflen the length in bytes of the supplied buffer |
icraggs | 0:c524a894b5e8 | 292 | * @param willMsg the will message |
icraggs | 0:c524a894b5e8 | 293 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 294 | */ |
icraggs | 0:c524a894b5e8 | 295 | int MQTTSNSerialize_willmsg1(unsigned char* buf, int buflen, MQTTSNString willMsg, enum MQTTSN_msgTypes packet_type) |
icraggs | 0:c524a894b5e8 | 296 | { |
icraggs | 0:c524a894b5e8 | 297 | unsigned char *ptr = buf; |
icraggs | 0:c524a894b5e8 | 298 | int len = 0; |
icraggs | 0:c524a894b5e8 | 299 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 300 | |
icraggs | 0:c524a894b5e8 | 301 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 302 | if ((len = MQTTSNPacket_len(MQTTSNstrlen(willMsg) + 1)) > buflen) |
icraggs | 0:c524a894b5e8 | 303 | { |
icraggs | 0:c524a894b5e8 | 304 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 305 | goto exit; |
icraggs | 0:c524a894b5e8 | 306 | } |
icraggs | 0:c524a894b5e8 | 307 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
icraggs | 1:7fa362fa563f | 308 | MQTTSNPacket_writeChar(&ptr, packet_type); /* write message type */ |
icraggs | 0:c524a894b5e8 | 309 | |
icraggs | 0:c524a894b5e8 | 310 | writeMQTTSNString(&ptr, willMsg); |
icraggs | 0:c524a894b5e8 | 311 | |
icraggs | 0:c524a894b5e8 | 312 | rc = ptr - buf; |
icraggs | 0:c524a894b5e8 | 313 | exit: |
icraggs | 0:c524a894b5e8 | 314 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 315 | return rc; |
icraggs | 0:c524a894b5e8 | 316 | } |
icraggs | 0:c524a894b5e8 | 317 | |
icraggs | 0:c524a894b5e8 | 318 | |
icraggs | 0:c524a894b5e8 | 319 | /** |
icraggs | 0:c524a894b5e8 | 320 | * Serializes a willmsg packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 321 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 322 | * @param len the length in bytes of the supplied buffersage |
icraggs | 0:c524a894b5e8 | 323 | * @param willMsg the will message |
icraggs | 0:c524a894b5e8 | 324 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 325 | */ |
icraggs | 0:c524a894b5e8 | 326 | int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTSNString willMsg) |
icraggs | 0:c524a894b5e8 | 327 | { |
icraggs | 0:c524a894b5e8 | 328 | return MQTTSNSerialize_willmsg1(buf, buflen, willMsg, MQTTSN_WILLMSG); |
icraggs | 0:c524a894b5e8 | 329 | } |
icraggs | 0:c524a894b5e8 | 330 | |
icraggs | 0:c524a894b5e8 | 331 | |
icraggs | 0:c524a894b5e8 | 332 | /** |
icraggs | 0:c524a894b5e8 | 333 | * Serializes a willmsgupd packet into the supplied buffer. |
icraggs | 0:c524a894b5e8 | 334 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:c524a894b5e8 | 335 | * @param len the length in bytes of the supplied buffersage |
icraggs | 0:c524a894b5e8 | 336 | * @param willMsg the will message |
icraggs | 0:c524a894b5e8 | 337 | * @return serialized length, or error if 0 |
icraggs | 0:c524a894b5e8 | 338 | */ |
icraggs | 0:c524a894b5e8 | 339 | int MQTTSNSerialize_willmsgupd(unsigned char* buf, int buflen, MQTTSNString willMsg) |
icraggs | 0:c524a894b5e8 | 340 | { |
icraggs | 0:c524a894b5e8 | 341 | return MQTTSNSerialize_willmsg1(buf, buflen, willMsg, MQTTSN_WILLMSGUPD); |
icraggs | 0:c524a894b5e8 | 342 | } |
icraggs | 0:c524a894b5e8 | 343 | |
icraggs | 0:c524a894b5e8 | 344 | |
icraggs | 0:c524a894b5e8 | 345 | /** |
icraggs | 0:c524a894b5e8 | 346 | * Deserializes the supplied (wire) buffer |
icraggs | 0:c524a894b5e8 | 347 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 348 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 349 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 350 | */ |
icraggs | 0:c524a894b5e8 | 351 | int MQTTSNDeserialize_willtopicreq(unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 352 | { |
icraggs | 0:c524a894b5e8 | 353 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 354 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 355 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 356 | int mylen; |
icraggs | 0:c524a894b5e8 | 357 | |
icraggs | 0:c524a894b5e8 | 358 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 359 | if (MQTTSNPacket_decode(curdata++, buflen, &mylen) != 1) /* read length */ |
icraggs | 0:c524a894b5e8 | 360 | goto exit; |
icraggs | 0:c524a894b5e8 | 361 | if (mylen > buflen) |
icraggs | 0:c524a894b5e8 | 362 | { |
icraggs | 0:c524a894b5e8 | 363 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 364 | goto exit; |
icraggs | 0:c524a894b5e8 | 365 | } |
icraggs | 0:c524a894b5e8 | 366 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 367 | if (enddata - curdata < 1) |
icraggs | 0:c524a894b5e8 | 368 | goto exit; |
icraggs | 0:c524a894b5e8 | 369 | |
icraggs | 1:7fa362fa563f | 370 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_WILLTOPICREQ) |
icraggs | 0:c524a894b5e8 | 371 | goto exit; |
icraggs | 0:c524a894b5e8 | 372 | |
icraggs | 0:c524a894b5e8 | 373 | rc = 1; |
icraggs | 0:c524a894b5e8 | 374 | exit: |
icraggs | 0:c524a894b5e8 | 375 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 376 | return rc; |
icraggs | 0:c524a894b5e8 | 377 | } |
icraggs | 0:c524a894b5e8 | 378 | |
icraggs | 0:c524a894b5e8 | 379 | |
icraggs | 0:c524a894b5e8 | 380 | /** |
icraggs | 0:c524a894b5e8 | 381 | * Deserializes the supplied (wire) buffer |
icraggs | 0:c524a894b5e8 | 382 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 383 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 384 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 385 | */ |
icraggs | 0:c524a894b5e8 | 386 | int MQTTSNDeserialize_willmsgreq(unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 387 | { |
icraggs | 0:c524a894b5e8 | 388 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 389 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 390 | int rc = -1; |
icraggs | 0:c524a894b5e8 | 391 | int mylen; |
icraggs | 0:c524a894b5e8 | 392 | |
icraggs | 0:c524a894b5e8 | 393 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 394 | if (MQTTSNPacket_decode(curdata++, buflen, &mylen) != 1) /* read length */ |
icraggs | 0:c524a894b5e8 | 395 | goto exit; |
icraggs | 0:c524a894b5e8 | 396 | if (mylen > buflen) |
icraggs | 0:c524a894b5e8 | 397 | { |
icraggs | 0:c524a894b5e8 | 398 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:c524a894b5e8 | 399 | goto exit; |
icraggs | 0:c524a894b5e8 | 400 | } |
icraggs | 0:c524a894b5e8 | 401 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 402 | if (enddata - curdata < 1) |
icraggs | 0:c524a894b5e8 | 403 | goto exit; |
icraggs | 0:c524a894b5e8 | 404 | |
icraggs | 1:7fa362fa563f | 405 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_WILLMSGREQ) |
icraggs | 0:c524a894b5e8 | 406 | goto exit; |
icraggs | 0:c524a894b5e8 | 407 | |
icraggs | 0:c524a894b5e8 | 408 | rc = 1; |
icraggs | 0:c524a894b5e8 | 409 | exit: |
icraggs | 0:c524a894b5e8 | 410 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 411 | return rc; |
icraggs | 0:c524a894b5e8 | 412 | } |
icraggs | 0:c524a894b5e8 | 413 | |
icraggs | 0:c524a894b5e8 | 414 | |
icraggs | 0:c524a894b5e8 | 415 | /** |
icraggs | 0:c524a894b5e8 | 416 | * Deserializes the supplied (wire) buffer into willtopicresp data - return code |
icraggs | 0:c524a894b5e8 | 417 | * @param connack_rc returned integer value of the return code |
icraggs | 0:c524a894b5e8 | 418 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 419 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 420 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 421 | */ |
icraggs | 0:c524a894b5e8 | 422 | int MQTTSNDeserialize_willtopicresp(int* resp_rc, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 423 | { |
icraggs | 0:c524a894b5e8 | 424 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 425 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 426 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 427 | int mylen; |
icraggs | 0:c524a894b5e8 | 428 | |
icraggs | 0:c524a894b5e8 | 429 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 430 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 431 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 432 | if (enddata - buf < 3) |
icraggs | 0:c524a894b5e8 | 433 | goto exit; |
icraggs | 0:c524a894b5e8 | 434 | |
icraggs | 1:7fa362fa563f | 435 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_WILLTOPICRESP) |
icraggs | 0:c524a894b5e8 | 436 | goto exit; |
icraggs | 0:c524a894b5e8 | 437 | |
icraggs | 1:7fa362fa563f | 438 | *resp_rc = MQTTSNPacket_readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 439 | |
icraggs | 0:c524a894b5e8 | 440 | rc = 1; |
icraggs | 0:c524a894b5e8 | 441 | exit: |
icraggs | 0:c524a894b5e8 | 442 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 443 | return rc; |
icraggs | 0:c524a894b5e8 | 444 | } |
icraggs | 0:c524a894b5e8 | 445 | |
icraggs | 0:c524a894b5e8 | 446 | |
icraggs | 0:c524a894b5e8 | 447 | /** |
icraggs | 0:c524a894b5e8 | 448 | * Deserializes the supplied (wire) buffer into willmsgresp data - return code |
icraggs | 0:c524a894b5e8 | 449 | * @param connack_rc returned integer value of the return code |
icraggs | 0:c524a894b5e8 | 450 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:c524a894b5e8 | 451 | * @param len the length in bytes of the data in the supplied buffer |
icraggs | 0:c524a894b5e8 | 452 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:c524a894b5e8 | 453 | */ |
icraggs | 0:c524a894b5e8 | 454 | int MQTTSNDeserialize_willmsgresp(int* resp_rc, unsigned char* buf, int buflen) |
icraggs | 0:c524a894b5e8 | 455 | { |
icraggs | 0:c524a894b5e8 | 456 | unsigned char* curdata = buf; |
icraggs | 0:c524a894b5e8 | 457 | unsigned char* enddata = NULL; |
icraggs | 0:c524a894b5e8 | 458 | int rc = 0; |
icraggs | 0:c524a894b5e8 | 459 | int mylen; |
icraggs | 0:c524a894b5e8 | 460 | |
icraggs | 0:c524a894b5e8 | 461 | FUNC_ENTRY; |
icraggs | 0:c524a894b5e8 | 462 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
icraggs | 0:c524a894b5e8 | 463 | enddata = buf + mylen; |
icraggs | 0:c524a894b5e8 | 464 | if (enddata - buf < 3) |
icraggs | 0:c524a894b5e8 | 465 | goto exit; |
icraggs | 0:c524a894b5e8 | 466 | |
icraggs | 1:7fa362fa563f | 467 | if (MQTTSNPacket_readChar(&curdata) != MQTTSN_WILLMSGRESP) |
icraggs | 0:c524a894b5e8 | 468 | goto exit; |
icraggs | 0:c524a894b5e8 | 469 | |
icraggs | 1:7fa362fa563f | 470 | *resp_rc = MQTTSNPacket_readChar(&curdata); |
icraggs | 0:c524a894b5e8 | 471 | |
icraggs | 0:c524a894b5e8 | 472 | rc = 1; |
icraggs | 0:c524a894b5e8 | 473 | exit: |
icraggs | 0:c524a894b5e8 | 474 | FUNC_EXIT_RC(rc); |
icraggs | 0:c524a894b5e8 | 475 | return rc; |
icraggs | 0:c524a894b5e8 | 476 | } |
icraggs | 0:c524a894b5e8 | 477 | |
icraggs | 0:c524a894b5e8 | 478 | |
icraggs | 0:c524a894b5e8 | 479 |