mqtt_ng.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright: SPDX-License-Identifier: GPL-3.0-only
  2. #include <stdint.h>
  3. #include <sys/types.h>
  4. #include <time.h>
  5. #include "c-rbuf/cringbuffer.h"
  6. #include "common_public.h"
  7. #define MQTT_NG_MSGGEN_OK 0
  8. // MQTT_NG_MSGGEN_USER_ERROR means parameters given to this function
  9. // do not make sense or are out of MQTT specs
  10. #define MQTT_NG_MSGGEN_USER_ERROR 1
  11. #define MQTT_NG_MSGGEN_BUFFER_OOM 2
  12. #define MQTT_NG_MSGGEN_MSG_TOO_BIG 3
  13. struct mqtt_ng_client;
  14. /* Converts integer to MQTT Variable Byte Integer as per 1.5.5 of MQTT 5 specs
  15. * @param input value to be converted
  16. * @param output pointer to memory where output will be written to. Must allow up to 4 bytes to be written.
  17. * @return number of bytes written to output or <= 0 if error in which case contents of output are undefined
  18. */
  19. int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output);
  20. struct mqtt_lwt_properties {
  21. char *will_topic;
  22. free_fnc_t will_topic_free;
  23. void *will_message;
  24. free_fnc_t will_message_free;
  25. size_t will_message_size;
  26. int will_qos;
  27. int will_retain;
  28. };
  29. struct mqtt_auth_properties {
  30. char *client_id;
  31. free_fnc_t client_id_free;
  32. char *username;
  33. free_fnc_t username_free;
  34. char *password;
  35. free_fnc_t password_free;
  36. };
  37. int mqtt_ng_connect(struct mqtt_ng_client *client,
  38. struct mqtt_auth_properties *auth,
  39. struct mqtt_lwt_properties *lwt,
  40. uint8_t clean_start,
  41. uint16_t keep_alive);
  42. int mqtt_ng_publish(struct mqtt_ng_client *client,
  43. char *topic,
  44. free_fnc_t topic_free,
  45. void *msg,
  46. free_fnc_t msg_free,
  47. size_t msg_len,
  48. uint8_t publish_flags,
  49. uint16_t *packet_id);
  50. struct mqtt_sub {
  51. char *topic;
  52. free_fnc_t topic_free;
  53. uint8_t options;
  54. };
  55. int mqtt_ng_subscribe(struct mqtt_ng_client *client, struct mqtt_sub *subscriptions, size_t subscription_count);
  56. int mqtt_ng_ping(struct mqtt_ng_client *client);
  57. typedef ssize_t (*mqtt_ng_send_fnc_t)(void *user_ctx, const void* buf, size_t len);
  58. struct mqtt_ng_init {
  59. mqtt_wss_log_ctx_t log;
  60. rbuf_t data_in;
  61. mqtt_ng_send_fnc_t data_out_fnc;
  62. void *user_ctx;
  63. void (*puback_callback)(uint16_t packet_id);
  64. void (*connack_callback)(void* user_ctx, int connack_reply);
  65. void (*msg_callback)(const char *topic, const void *msg, size_t msglen, int qos);
  66. };
  67. struct mqtt_ng_client *mqtt_ng_init(struct mqtt_ng_init *settings);
  68. void mqtt_ng_destroy(struct mqtt_ng_client *client);
  69. int mqtt_ng_disconnect(struct mqtt_ng_client *client, uint8_t reason_code);
  70. int mqtt_ng_sync(struct mqtt_ng_client *client);
  71. time_t mqtt_ng_last_send_time(struct mqtt_ng_client *client);
  72. void mqtt_ng_set_max_mem(struct mqtt_ng_client *client, size_t bytes);
  73. void mqtt_ng_get_stats(struct mqtt_ng_client *client, struct mqtt_ng_stats *stats);
  74. int mqtt_ng_set_topic_alias(struct mqtt_ng_client *client, const char *topic);