Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | /*
* Copyright (c) 2016 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file mqtt_pkt.h
*
* @brief MQTT v3.1.1 packet library, see:
* http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
*
* This file is part of the Zephyr Project
* http://www.zephyrproject.org
*/
#ifndef _MQTT_PKT_H_
#define _MQTT_PKT_H_
#include <zephyr/types.h>
#include <stddef.h>
#include <net/mqtt_types.h>
#define MQTT_PACKET_TYPE(first_byte) (((first_byte) & 0xF0) >> 4)
/**
* Packs the MQTT CONNACK message. See MQTT 3.2 CONNACK - Acknowledge
* connection request
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] session_present Session Present parameter (0 for clean session)
* @param [in] ret_code Connect return code. See MQTT 3.2.2.3
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_connack(u8_t *buf, u16_t *length, u16_t size,
u8_t session_present, u8_t ret_code);
/**
* Packs the MQTT PUBACK message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1 Packet Identifier
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_puback(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id);
/**
* Packs the MQTT PUBREC message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1 Packet Identifier
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_pubrec(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id);
/**
* Packs the MQTT PUBREL message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1 Packet Identifier
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_pubrel(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id);
/**
* Packs the MQTT PUBCOMP message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1 Packet Identifier
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_pubcomp(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id);
/**
* Packs the MQTT SUBACK message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1
* @param [in] elements Number of elements in the granted_qos array
* @param [in] granted_qos Array where the QoS values are stored
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_pack_suback(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id, u8_t elements,
enum mqtt_qos granted_qos[]);
/**
* Packs the MQTT CONNECT message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] msg MQTT CONNECT msg
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_pack_connect(u8_t *buf, u16_t *length, u16_t size,
struct mqtt_connect_msg *msg);
/**
* Unpacks the MQTT CONNECT message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length MQTT CONNECT message's length
* @param [out] msg MQTT CONNECT parameters
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_connect(u8_t *buf, u16_t length,
struct mqtt_connect_msg *msg);
/**
* Packs the MQTT SUBSCRIBE message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id MQTT Message Packet Identifier. See MQTT 2.3.1
* @param [in] items Number of elements in topics
* @param [in] topics Array of topics.
* For example: {"sensors", "lights", "doors"}
* @param [in] qos Array of QoS values per topic.
* For example: {MQTT_QoS1, MQTT_QoS2, MQTT_QoS0}
* NOTE: qos and topics must have the same cardinality
* (# of items)
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_pack_subscribe(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id, u8_t items, const char *topics[],
const enum mqtt_qos qos[]);
/**
* Packs the MQTT UNSUBSCRIBE message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id MQTT Message Packet Identifier. See MQTT 2.3.1
* @param [in] items Number of elements in topics
* @param [in] topics Array of topics.
* For example: {"sensors", "lights", "doors"}
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_pack_unsubscribe(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id, u8_t items, const char *topics[]);
/**
* Unpacks the MQTT SUBSCRIBE message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id MQTT Message Packet Identifier. See MQTT 2.3.1
* @param [out] items Number of recovered topics
* @param [in] elements Max number of topics to recover from buf
* @param [out] topics Array of topics. Each element in this array points to
* the beginning of a topic in buf
* @param [out] topic_len Array containing the length of each topic in topics
* @param [out] qos Array of QoS values
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_subscribe(u8_t *buf, u16_t length, u16_t *pkt_id,
u8_t *items, u8_t elements, char *topics[],
u16_t topic_len[], enum mqtt_qos qos[]);
/**
* Unpacks the MQTT SUBACK message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id MQTT Message Packet Identifier. See MQTT 2.3.1
* @param [out] items Number of recovered topics
* @param [in] elements Max number of topics to recover from buf
* @param [out] granted_qos Granted QoS values per topic. See MQTT 3.9
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_suback(u8_t *buf, u16_t length, u16_t *pkt_id,
u8_t *items, u8_t elements,
enum mqtt_qos granted_qos[]);
/**
* Packs the MQTT PUBLISH message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] msg MQTT PUBLISH message
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_pack_publish(u8_t *buf, u16_t *length, u16_t size,
struct mqtt_publish_msg *msg);
/**
* Unpacks the MQTT PUBLISH message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] msg MQTT PUBLISH message
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_publish(u8_t *buf, u16_t length,
struct mqtt_publish_msg *msg);
/**
* Unpacks the MQTT CONNACK message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] session Session Present. See MQTT 3.2.2.2
* @param [out] connect_rc CONNECT return code. See MQTT 3.2.2.3
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_connack(u8_t *buf, u16_t length, u8_t *session,
u8_t *connect_rc);
/**
* Packs the MQTT PINGREQ message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
*
* @retval 0 on success
* @retval -ENOMEM
*/
int mqtt_pack_pingreq(u8_t *buf, u16_t *length, u16_t size);
/**
* Packs the MQTT PINGRESP message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
*
* @retval 0 on success
* @retval -ENOMEM
*/
int mqtt_pack_pingresp(u8_t *buf, u16_t *length, u16_t size);
/**
* Packs the MQTT DISCONNECT message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
*
* @retval 0 on success
* @retval -ENOMEM
*/
int mqtt_pack_disconnect(u8_t *buf, u16_t *length, u16_t size);
/**
* Packs the MQTT UNSUBACK message
*
* @param [out] buf Buffer where the resultant message is stored
* @param [out] length Number of bytes required to codify the message
* @param [in] size Buffer size
* @param [in] pkt_id Packet Identifier. See MQTT 2.3.1 Packet Identifier
*
* @retval 0 on success
* @retval -ENOMEM if size < 4
*/
int mqtt_pack_unsuback(u8_t *buf, u16_t *length, u16_t size,
u16_t pkt_id);
/**
* Unpacks the MQTT PUBACK message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_puback(u8_t *buf, u16_t length, u16_t *pkt_id);
/**
* Unpacks the MQTT PUBREC message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_pubrec(u8_t *buf, u16_t length, u16_t *pkt_id);
/**
* Unpacks the MQTT PUBREL message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_pubrel(u8_t *buf, u16_t length, u16_t *pkt_id);
/**
* Unpacks the MQTT PUBCOMP message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_pubcomp(u8_t *buf, u16_t length, u16_t *pkt_id);
/**
* Unpacks the MQTT UNSUBACK message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
* @param [out] pkt_id Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_unsuback(u8_t *buf, u16_t length, u16_t *pkt_id);
/**
* Unpacks the MQTT PINGREQ message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_pingreq(u8_t *buf, u16_t length);
/**
* Unpacks the MQTT PINGRESP message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_pingresp(u8_t *buf, u16_t length);
/**
* Unpacks the MQTT DISCONNECT message
*
* @param [in] buf Buffer where the message is stored
* @param [in] length Message's length
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_unpack_disconnect(u8_t *buf, u16_t length);
#endif
|