mqtt_wss_client.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. // Copyright (C) 2020 Timotej Šiškovič
  3. #ifndef MQTT_WSS_CLIENT_H
  4. #define MQTT_WSS_CLIENT_H
  5. #include <stdint.h>
  6. #include <stddef.h> //size_t
  7. #include "mqtt_wss_log.h"
  8. #include "common_public.h"
  9. // All OK call me at your earliest convinience
  10. #define MQTT_WSS_OK 0
  11. /* All OK, poll timeout you requested when calling mqtt_wss_service expired - you might want to know if timeout
  12. * happened or we got some data or handle same as MQTT_WSS_OK
  13. */
  14. #define MQTT_WSS_OK_TO 1
  15. // Connection was closed by remote
  16. #define MQTT_WSS_ERR_CONN_DROP -1
  17. // Error in MQTT protocol (e.g. malformed packet)
  18. #define MQTT_WSS_ERR_PROTO_MQTT -2
  19. // Error in WebSocket protocol (e.g. malformed packet)
  20. #define MQTT_WSS_ERR_PROTO_WS -3
  21. #define MQTT_WSS_ERR_TX_BUF_TOO_SMALL -4
  22. #define MQTT_WSS_ERR_RX_BUF_TOO_SMALL -5
  23. #define MQTT_WSS_ERR_TOO_BIG_FOR_SERVER -6
  24. // if client was initialized with MQTT 3 but MQTT 5 feature
  25. // was requested by user of library
  26. #define MQTT_WSS_ERR_CANT_DO -8
  27. typedef struct mqtt_wss_client_struct *mqtt_wss_client;
  28. typedef void (*msg_callback_fnc_t)(const char *topic, const void *msg, size_t msglen, int qos);
  29. /* Creates new instance of MQTT over WSS. Doesn't start connection.
  30. * @param log_prefix this is prefix to be used when logging to discern between multiple
  31. * mqtt_wss instances. Can be NULL.
  32. * @param log_callback is function pointer to fnc to be called when mqtt_wss wants
  33. * to log. This allows plugging this library into your own logging system/solution.
  34. * If NULL STDOUT/STDERR will be used.
  35. * @param msg_callback is function pointer to function which will be called
  36. * when application level message arrives from broker (for subscribed topics).
  37. * Can be NULL if you are not interested about incoming messages.
  38. * @param puback_callback is function pointer to function to be called when QOS1 Publish
  39. * is acknowledged by server
  40. */
  41. mqtt_wss_client mqtt_wss_new(const char *log_prefix,
  42. mqtt_wss_log_callback_t log_callback,
  43. msg_callback_fnc_t msg_callback,
  44. void (*puback_callback)(uint16_t packet_id));
  45. void mqtt_wss_set_max_buf_size(mqtt_wss_client client, size_t size);
  46. void mqtt_wss_destroy(mqtt_wss_client client);
  47. struct mqtt_connect_params;
  48. struct mqtt_wss_proxy;
  49. #define MQTT_WSS_SSL_CERT_CHECK_FULL 0x00
  50. #define MQTT_WSS_SSL_ALLOW_SELF_SIGNED 0x01
  51. #define MQTT_WSS_SSL_DONT_CHECK_CERTS 0x08
  52. /* Will block until the MQTT over WSS connection is established or return error
  53. * @param client mqtt_wss_client which should connect
  54. * @param host to connect to (where MQTT over WSS server is listening)
  55. * @param port to connect to (where MQTT over WSS server is listening)
  56. * @param mqtt_params pointer to mqtt_connect_params structure which contains MQTT credentials and settings
  57. * @param ssl_flags parameters for OpenSSL, 0=MQTT_WSS_SSL_CERT_CHECK_FULL
  58. */
  59. int mqtt_wss_connect(mqtt_wss_client client, char *host, int port, struct mqtt_connect_params *mqtt_params, int ssl_flags, struct mqtt_wss_proxy *proxy);
  60. int mqtt_wss_service(mqtt_wss_client client, int timeout_ms);
  61. void mqtt_wss_disconnect(mqtt_wss_client client, int timeout_ms);
  62. // we redefine this instead of using MQTT-C flags as in future
  63. // we want to support different MQTT implementations if needed
  64. enum mqtt_wss_publish_flags {
  65. MQTT_WSS_PUB_QOS0 = 0x0,
  66. MQTT_WSS_PUB_QOS1 = 0x1,
  67. MQTT_WSS_PUB_QOS2 = 0x2,
  68. MQTT_WSS_PUB_QOSMASK = 0x3,
  69. MQTT_WSS_PUB_RETAIN = 0x4
  70. };
  71. struct mqtt_connect_params {
  72. const char *clientid;
  73. const char *username;
  74. const char *password;
  75. const char *will_topic;
  76. const void *will_msg;
  77. enum mqtt_wss_publish_flags will_flags;
  78. size_t will_msg_len;
  79. int keep_alive;
  80. int drop_on_publish_fail;
  81. };
  82. enum mqtt_wss_proxy_type {
  83. MQTT_WSS_DIRECT = 0,
  84. MQTT_WSS_PROXY_HTTP
  85. };
  86. struct mqtt_wss_proxy {
  87. enum mqtt_wss_proxy_type type;
  88. const char *host;
  89. int port;
  90. const char *username;
  91. const char *password;
  92. };
  93. /* TODO!!! update the description
  94. * Publishes MQTT message and gets message id
  95. * @param client mqtt_wss_client which should transfer the message
  96. * @param topic MQTT topic to publish message to (0 terminated C string)
  97. * @param msg Message to be published (no need for 0 termination)
  98. * @param msg_len Length of the message to be published
  99. * @param publish_flags see enum mqtt_wss_publish_flags e.g. (MQTT_WSS_PUB_QOS1 | MQTT_WSS_PUB_RETAIN)
  100. * @param packet_id is 16 bit unsigned int representing ID that can be used to pair with PUBACK callback
  101. * for usages where application layer wants to know which messages are delivered when
  102. * @return Returns 0 on success
  103. */
  104. int mqtt_wss_publish5(mqtt_wss_client client,
  105. char *topic,
  106. free_fnc_t topic_free,
  107. void *msg,
  108. free_fnc_t msg_free,
  109. size_t msg_len,
  110. uint8_t publish_flags,
  111. uint16_t *packet_id);
  112. int mqtt_wss_set_topic_alias(mqtt_wss_client client, const char *topic);
  113. /* Subscribes to MQTT topic
  114. * @param client mqtt_wss_client which should do the subscription
  115. * @param topic MQTT topic to subscribe to
  116. * @param max_qos_level maximum QOS level that broker can send to us on this subscription
  117. * @return Returns 0 on success
  118. */
  119. int mqtt_wss_subscribe(mqtt_wss_client client, char *topic, int max_qos_level);
  120. struct mqtt_wss_stats {
  121. uint64_t bytes_tx;
  122. uint64_t bytes_rx;
  123. #ifdef MQTT_WSS_CPUSTATS
  124. uint64_t time_keepalive;
  125. uint64_t time_read_socket;
  126. uint64_t time_write_socket;
  127. uint64_t time_process_websocket;
  128. uint64_t time_process_mqtt;
  129. #endif
  130. struct mqtt_ng_stats mqtt;
  131. };
  132. struct mqtt_wss_stats mqtt_wss_get_stats(mqtt_wss_client client);
  133. #ifdef MQTT_WSS_DEBUG
  134. #include <openssl/ssl.h>
  135. void mqtt_wss_set_SSL_CTX_keylog_cb(mqtt_wss_client client, void (*ssl_ctx_keylog_cb)(const SSL *ssl, const char *line));
  136. #endif
  137. #endif /* MQTT_WSS_CLIENT_H */