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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _MQTT_H_
#define _MQTT_H_
#include <net/mqtt_types.h>
#include <net/net_context.h>
#include <net/net_app.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MQTT library
* @defgroup mqtt MQTT library
* @ingroup networking
* @{
*/
/**
* MQTT application type
*/
enum mqtt_app {
/** Publisher and Subscriber application */
MQTT_APP_PUBLISHER_SUBSCRIBER,
/** Publisher only application */
MQTT_APP_PUBLISHER,
/** Subscriber only application */
MQTT_APP_SUBSCRIBER,
/** MQTT Server */
MQTT_APP_SERVER
};
/**
* MQTT context structure
*
* @details Context structure for the MQTT high-level API with support for QoS.
*
* This API is designed for asynchronous operation, so callbacks are
* executed when some events must be addressed outside the MQTT routines.
* Those events are triggered by the reception or transmission of MQTT messages
* and are defined by the MQTT v3.1.1 spec, see:
*
* http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
*
* For example, assume that Zephyr is operating as a MQTT_APP_SUBSCRIBER, so it
* may receive the MQTT PUBLISH and MQTT PUBREL (QoS2) messages. Once the
* messages are parsed and validated, the #publish_rx callback is executed.
*
* Internally, the #publish_rx callback must store the #mqtt_publish_msg message
* when a MQTT PUBLISH msg is received. When a MQTT PUBREL message is received,
* the application must evaluate if the PUBREL's Packet Identifier matches a
* previous received MQTT PUBLISH message. In this case, the user may provide a
* collection of #mqtt_publish_msg structs (array of structs) to store those
* messages.
*
* <b>NOTE: The application (and not the API) is in charge of keeping track of
* the state of the received and sent messages.</b>
*/
struct mqtt_ctx {
/** Net app context structure */
struct net_app_ctx net_app_ctx;
s32_t net_init_timeout;
s32_t net_timeout;
/** Connectivity */
char *peer_addr_str;
u16_t peer_port;
#if defined(CONFIG_MQTT_LIB_TLS)
/** TLS parameters */
u8_t *request_buf;
size_t request_buf_len;
u8_t *personalization_data;
size_t personalization_data_len;
char *cert_host;
/** TLS thread parameters */
struct k_mem_pool *tls_mem_pool;
k_thread_stack_t *tls_stack;
size_t tls_stack_size;
/** TLS callback */
net_app_ca_cert_cb_t cert_cb;
net_app_entropy_src_cb_t entropy_src_cb;
/** TLS handshake */
struct k_sem tls_hs_wait;
s32_t tls_hs_timeout;
#endif
/** Callback executed when a MQTT CONNACK msg is received and validated.
* If this function pointer is not used, must be set to NULL.
*/
void (*connect)(struct mqtt_ctx *ctx);
/** Callback executed when a MQTT DISCONNECT msg is sent.
* If this function pointer is not used, must be set to NULL.
*/
void (*disconnect)(struct mqtt_ctx *ctx);
/** Callback executed when a #MQTT_APP_PUBLISHER application receives
* a MQTT PUBxxxx msg.
* If type is MQTT_PUBACK, MQTT_PUBCOMP or MQTT_PUBREC, this callback
* must return 0 if pkt_id matches the packet id of a previously
* received MQTT_PUBxxx message. If this callback returns 0, the caller
* will continue.
* Any other value will stop the QoS handshake and the caller will
* return -EINVAL. The application must discard all the messages
* already processed.
*
* <b>Note: this callback must be not NULL</b>
*
* @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the input MQTT msg
* @param [in] type Packet type
*/
int (*publish_tx)(struct mqtt_ctx *ctx, u16_t pkt_id,
enum mqtt_packet type);
/** Callback executed when a MQTT_APP_SUBSCRIBER,
* MQTT_APP_PUBLISHER_SUBSCRIBER or MQTT_APP_SERVER applications receive
* a MQTT PUBxxxx msg. If this callback returns 0, the caller will
* continue. If type is MQTT_PUBREL this callback must return 0 if
* pkt_id matches the packet id of a previously received MQTT_PUBxxx
* message. Any other value will stop the QoS handshake and the caller
* will return -EINVAL
*
* <b>Note: this callback must be not NULL</b>
*
* @param [in] ctx MQTT context
* @param [in] msg Publish message, this parameter is only used
* when the type is MQTT_PUBLISH
* @param [in] pkt_id Packet Identifier for the input msg
* @param [in] type Packet type
*/
int (*publish_rx)(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg,
u16_t pkt_id, enum mqtt_packet type);
/** Callback executed when a MQTT_APP_SUBSCRIBER or
* MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT SUBACK message
* If this callback returns 0, the caller will continue. Any other
* value will make the caller return -EINVAL.
*
* <b>Note: this callback must be not NULL</b>
*
* @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the MQTT SUBACK msg
* @param [in] items Number of elements in the qos array
* @param [in] qos Array of QoS values
*/
int (*subscribe)(struct mqtt_ctx *ctx, u16_t pkt_id,
u8_t items, enum mqtt_qos qos[]);
/** Callback executed when a MQTT_APP_SUBSCRIBER or
* MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT UNSUBACK message
* If this callback returns 0, the caller will continue. Any other value
* will make the caller return -EINVAL
*
* <b>Note: this callback must be not NULL</b>
*
* @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the MQTT SUBACK msg
*/
int (*unsubscribe)(struct mqtt_ctx *ctx, u16_t pkt_id);
/** Callback executed when an incoming message doesn't pass the
* validation stage. This callback may be NULL.
* The pkt_type variable may be set to MQTT_INVALID, if the parsing
* stage is aborted before determining the MQTT msg packet type.
*
* @param [in] ctx MQTT context
* @param [in] pkt_type MQTT Packet type
*/
void (*malformed)(struct mqtt_ctx *ctx, u16_t pkt_type);
/* Internal use only */
int (*rcv)(struct mqtt_ctx *ctx, struct net_pkt *);
/** Application type, see: enum mqtt_app */
u8_t app_type;
/* Clean session is also part of the MQTT CONNECT msg, however app
* behavior is influenced by this parameter, so we keep a copy here
*/
/** MQTT Clean Session parameter */
u8_t clean_session:1;
/** 1 if the MQTT application is connected and 0 otherwise */
u8_t connected:1;
};
/**
* Initializes the MQTT context structure
*
* @param ctx MQTT context structure
* @param app_type See enum mqtt_app
* @retval 0 always
*/
int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type);
/**
* Release the MQTT context structure
*
* @param ctx MQTT context structure
* @retval 0 on success, and <0 if error
*/
int mqtt_close(struct mqtt_ctx *ctx);
/**
* Connect to an MQTT broker
*
* @param ctx MQTT context structure
* @retval 0 on success, and <0 if error
*/
int mqtt_connect(struct mqtt_ctx *ctx);
/**
* Sends the MQTT CONNECT message
*
* @param [in] ctx MQTT context structure
* @param [in] msg MQTT CONNECT msg
*
* @retval 0 on success
* @retval -EIO
* @retval -ENOMEM
* @retval -EINVAL
*/
int mqtt_tx_connect(struct mqtt_ctx *ctx, struct mqtt_connect_msg *msg);
/**
* Send the MQTT DISCONNECT message
*
* @param [in] ctx MQTT context structure
*
* @retval 0 on success
* @retval -EIO
* @retval -ENOMEM
* @retval -EINVAL
*/
int mqtt_tx_disconnect(struct mqtt_ctx *ctx);
/**
* Sends the MQTT PUBACK message with the given packet id
*
* @param [in] ctx MQTT context structure
* @param [in] id MQTT Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_puback(struct mqtt_ctx *ctx, u16_t id);
/**
* Sends the MQTT PUBCOMP message with the given packet id
*
* @param [in] ctx MQTT context structure
* @param [in] id MQTT Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_pubcomp(struct mqtt_ctx *ctx, u16_t id);
/**
* Sends the MQTT PUBREC message with the given packet id
*
* @param [in] ctx MQTT context structure
* @param [in] id MQTT Packet Identifier
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_pubrec(struct mqtt_ctx *ctx, u16_t id);
/**
* Sends the MQTT PUBREL message with the given packet id
*
* @param [in] ctx MQTT context structure
* @param [in] id MQTT Packet Identifier
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_pubrel(struct mqtt_ctx *ctx, u16_t id);
/**
* Sends the MQTT PUBLISH message
*
* @param [in] ctx MQTT context structure
* @param [in] msg MQTT PUBLISH msg
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_publish(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg);
/**
* Sends the MQTT PINGREQ message
*
* @param [in] ctx MQTT context structure
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_pingreq(struct mqtt_ctx *ctx);
/**
* Sends the MQTT SUBSCRIBE message
*
* @param [in] ctx MQTT context structure
* @param [in] pkt_id Packet identifier for the MQTT SUBSCRIBE msg
* @param [in] items Number of elements in 'topics' and 'qos' arrays
* @param [in] topics Array of 'items' elements containing C strings.
* For example: {"sensors", "lights", "doors"}
* @param [in] qos Array of 'items' elements containing MQTT QoS values:
* MQTT_QoS0, MQTT_QoS1, MQTT_QoS2. For example for the 'topics'
* array above the following QoS may be used: {MQTT_QoS0,
* MQTT_QoS2, MQTT_QoS1}, indicating that the subscription to
* 'lights' must be done with MQTT_QoS2
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_subscribe(struct mqtt_ctx *ctx, u16_t pkt_id, u8_t items,
const char *topics[], const enum mqtt_qos qos[]);
/**
* Sends the MQTT UNSUBSCRIBE message
*
* @param [in] ctx MQTT context structure
* @param [in] pkt_id Packet identifier for the MQTT UNSUBSCRIBE msg
* @param [in] items Number of elements in the 'topics' array
* @param [in] topics Array of 'items' elements containing C strings
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
* @retval -EIO
*/
int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, u16_t pkt_id, u8_t items,
const char *topics[]);
/**
* Parses and validates the MQTT CONNACK msg
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] clean_session MQTT clean session parameter
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_connack(struct mqtt_ctx *ctx, struct net_buf *rx,
int clean_session);
/**
* Parses and validates the MQTT PUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_puback(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses and validates the MQTT PUBCOMP message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubcomp(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses and validates the MQTT PUBREC message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubrec(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses and validates the MQTT PUBREL message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubrel(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses the MQTT PINGRESP message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pingresp(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses the MQTT SUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_suback(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses the MQTT UNSUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_unsuback(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* Parses the MQTT PUBLISH message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* _MQTT_H_ */
|