mqtt_example.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Redistribution and use in source and binary forms, with or without modification,
  3. * are permitted provided that the following conditions are met:
  4. *
  5. * 1. Redistributions of source code must retain the above copyright notice,
  6. * this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright notice,
  8. * this list of conditions and the following disclaimer in the documentation
  9. * and/or other materials provided with the distribution.
  10. * 3. The name of the author may not be used to endorse or promote products
  11. * derived from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  16. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  18. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  21. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  22. * OF SUCH DAMAGE.
  23. *
  24. * This file is part of the lwIP TCP/IP stack.
  25. *
  26. * Author: Dirk Ziegelmeier <dziegel@gmx.de>
  27. *
  28. */
  29. #include "lwip/apps/mqtt.h"
  30. #include "mqtt_example.h"
  31. #if LWIP_TCP
  32. /** Define this to a compile-time IP address initialization
  33. * to connect anything else than IPv4 loopback
  34. */
  35. #ifndef LWIP_MQTT_EXAMPLE_IPADDR_INIT
  36. #if LWIP_IPV4
  37. #define LWIP_MQTT_EXAMPLE_IPADDR_INIT = IPADDR4_INIT(PP_HTONL(IPADDR_LOOPBACK))
  38. #else
  39. #define LWIP_MQTT_EXAMPLE_IPADDR_INIT
  40. #endif
  41. #endif
  42. static ip_addr_t mqtt_ip LWIP_MQTT_EXAMPLE_IPADDR_INIT;
  43. static mqtt_client_t* mqtt_client;
  44. static const struct mqtt_connect_client_info_t mqtt_client_info =
  45. {
  46. "test",
  47. NULL, /* user */
  48. NULL, /* pass */
  49. 100, /* keep alive */
  50. NULL, /* will_topic */
  51. NULL, /* will_msg */
  52. 0, /* will_msg_len */
  53. 0, /* will_qos */
  54. 0 /* will_retain */
  55. #if LWIP_ALTCP && LWIP_ALTCP_TLS
  56. , NULL
  57. #endif
  58. };
  59. static void
  60. mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
  61. {
  62. const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
  63. LWIP_UNUSED_ARG(data);
  64. LWIP_PLATFORM_DIAG(("MQTT client \"%s\" data cb: len %d, flags %d\n", client_info->client_id,
  65. (int)len, (int)flags));
  66. }
  67. static void
  68. mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
  69. {
  70. const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
  71. LWIP_PLATFORM_DIAG(("MQTT client \"%s\" publish cb: topic %s, len %d\n", client_info->client_id,
  72. topic, (int)tot_len));
  73. }
  74. static void
  75. mqtt_request_cb(void *arg, err_t err)
  76. {
  77. const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
  78. LWIP_PLATFORM_DIAG(("MQTT client \"%s\" request cb: err %d\n", client_info->client_id, (int)err));
  79. }
  80. static void
  81. mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
  82. {
  83. const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
  84. LWIP_UNUSED_ARG(client);
  85. LWIP_PLATFORM_DIAG(("MQTT client \"%s\" connection cb: status %d\n", client_info->client_id, (int)status));
  86. if (status == MQTT_CONNECT_ACCEPTED) {
  87. mqtt_sub_unsub(client,
  88. "topic_qos1", 1,
  89. mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
  90. 1);
  91. mqtt_sub_unsub(client,
  92. "topic_qos0", 0,
  93. mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
  94. 1);
  95. }
  96. }
  97. #endif /* LWIP_TCP */
  98. void
  99. mqtt_example_init(void)
  100. {
  101. #if LWIP_TCP
  102. mqtt_client = mqtt_client_new();
  103. mqtt_set_inpub_callback(mqtt_client,
  104. mqtt_incoming_publish_cb,
  105. mqtt_incoming_data_cb,
  106. LWIP_CONST_CAST(void*, &mqtt_client_info));
  107. mqtt_client_connect(mqtt_client,
  108. &mqtt_ip, MQTT_PORT,
  109. mqtt_connection_cb, LWIP_CONST_CAST(void*, &mqtt_client_info),
  110. &mqtt_client_info);
  111. #endif /* LWIP_TCP */
  112. }