umqtt_sample.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-11 springcity the first version
  9. */
  10. #include <string.h>
  11. #include <rtthread.h>
  12. #define DBG_TAG "umqtt.sample"
  13. #ifdef PKG_UMQTT_USING_DEBUG
  14. #define DBG_LVL DBG_LOG
  15. #else
  16. #define DBG_LVL DBG_INFO
  17. #endif /* MQTT_DEBUG */
  18. #include <rtdbg.h>
  19. #include "umqtt.h"
  20. #include "umqtt_internal.h"
  21. // #define MQTT_URI "tcp://test.mosquitto.org:1883"
  22. // #define MQTT_URI "tcp://mq.tongxinmao.com:18831"
  23. #define MQTT_URI "tcp://192.168.12.83:1883"
  24. #define MQTT_SUBTOPIC "/umqtt/test"
  25. #define MQTT_PUBTOPIC "/umqtt/test"
  26. #define MQTT_WILLMSG "Goodbye!"
  27. static int is_started = 0;
  28. static umqtt_client_t m_umqtt_client = RT_NULL;
  29. static int user_callback(struct umqtt_client *client, enum umqtt_evt event)
  30. {
  31. RT_ASSERT(client);
  32. switch(event)
  33. {
  34. case UMQTT_EVT_LINK:
  35. LOG_D(" user callback, event - link!");
  36. break;
  37. case UMQTT_EVT_ONLINE:
  38. LOG_D(" user callback, event - online!");
  39. break;
  40. case UMQTT_EVT_OFFLINE:
  41. LOG_D(" user callback, event - offline!");
  42. break;
  43. case UMQTT_EVT_HEARTBEAT:
  44. LOG_D(" user callback, event - heartbeat!");
  45. break;
  46. default:
  47. LOG_D(" user callback, event:%d", event);
  48. break;
  49. }
  50. return 0;
  51. }
  52. static void umqtt_topic_recv_callback(struct umqtt_client *client, void *msg_data)
  53. {
  54. RT_ASSERT(client);
  55. RT_ASSERT(msg_data);
  56. struct umqtt_pkgs_publish *msg = (struct umqtt_pkgs_publish *)msg_data;
  57. LOG_D(" umqtt topic recv callback! name length: %d, name: %s, packet id: %d, payload len: %d ",
  58. msg->topic_name_len,
  59. msg->topic_name,
  60. msg->packet_id,
  61. // msg->payload,
  62. msg->payload_len);
  63. }
  64. static int umqtt_ex_start(int argc, char **argv)
  65. {
  66. LOG_D(" umqtt example start!");
  67. if (argc != 1)
  68. {
  69. LOG_E(" umqtt_start --start a umqtt worker thread.");
  70. return -1;
  71. }
  72. if (is_started)
  73. {
  74. LOG_E(" umqtt client is already connected.");
  75. return -1;
  76. }
  77. struct umqtt_info umqtt_info = { 0 };
  78. umqtt_info.uri = MQTT_URI;
  79. m_umqtt_client = umqtt_create(&umqtt_info);
  80. if (m_umqtt_client == RT_NULL)
  81. {
  82. LOG_E(" umqtt client create failed!");
  83. return -1;
  84. }
  85. umqtt_control(m_umqtt_client, UMQTT_CMD_EVT_CB, user_callback);
  86. if (umqtt_start(m_umqtt_client) >= 0)
  87. {
  88. LOG_I(" umqtt start success!");
  89. is_started = 1;
  90. }
  91. else
  92. {
  93. m_umqtt_client = RT_NULL;
  94. LOG_E(" umqtt start failed!");
  95. }
  96. return 0;
  97. }
  98. static int umqtt_ex_stop(int argc, char **argv)
  99. {
  100. LOG_D(" umqtt example stop!");
  101. if (argc != 1)
  102. {
  103. LOG_D("umqtt_stop --stop umqtt worker thread and free mqtt client object.\n");
  104. }
  105. is_started = 0;
  106. umqtt_stop(m_umqtt_client);
  107. umqtt_delete(m_umqtt_client);
  108. m_umqtt_client = RT_NULL;
  109. return 0;
  110. }
  111. static int str_to_int(const char *str)
  112. {
  113. int _ret = 0, _cnt = 0;
  114. int _strlen = strlen(str);
  115. for (_cnt = 0; _cnt < _strlen; _cnt++) {
  116. if ((str[_cnt] >= '0') && (str[_cnt] <= '9')) {
  117. _ret = _ret * 10 + str[_cnt] - '0';
  118. }
  119. }
  120. return _ret;
  121. }
  122. static int umqtt_ex_publish(int argc, char **argv)
  123. {
  124. LOG_D(" umqtt example publish!");
  125. if (is_started == 0)
  126. {
  127. LOG_E("mqtt client is not connected.");
  128. return -1;
  129. }
  130. if (argc == 4) {
  131. int _len = str_to_int(argv[2]);
  132. // LOG_D(" *argv[0]: %s, *argv[1]: %s, *argv[2]: %d, *argv[3]: %s ", argv[0], argv[1], _len, argv[3]);
  133. umqtt_publish(m_umqtt_client, ((_len > UMQTT_QOS2) ? UMQTT_QOS1 : _len), argv[1], argv[3], strlen(argv[3]) + 1, 100);
  134. } else {
  135. LOG_E("mqtt_publish <topic> <0/1/2> [message] --mqtt publish message to specified topic.\n");
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. static int umqtt_ex_subscribe(int argc, char **argv)
  141. {
  142. LOG_D(" umqtt example subscribe!");
  143. if (argc != 2)
  144. {
  145. LOG_E("umqtt_subscribe [topic] --send an umqtt subscribe packet and wait for suback before returning.\n");
  146. return -1;
  147. }
  148. if (is_started == 0)
  149. {
  150. LOG_E("umqtt client is not connected.");
  151. return -1;
  152. }
  153. return umqtt_subscribe(m_umqtt_client, argv[1], UMQTT_QOS1, umqtt_topic_recv_callback);
  154. }
  155. static int umqtt_ex_unsubscribe(int argc, char **argv)
  156. {
  157. LOG_D(" umqtt example unsubscribe!");
  158. if (argc != 2)
  159. {
  160. LOG_E("mqtt_unsubscribe [topic] --send an mqtt unsubscribe packet and wait for suback before returning.\n");
  161. return -1;
  162. }
  163. if (is_started == 0)
  164. {
  165. LOG_E("mqtt client is not connected.");
  166. return -1;
  167. }
  168. return umqtt_unsubscribe(m_umqtt_client, argv[1]);
  169. }
  170. #ifdef FINSH_USING_MSH
  171. MSH_CMD_EXPORT(umqtt_ex_start, startup umqtt client);
  172. MSH_CMD_EXPORT(umqtt_ex_stop, stop umqtt client);
  173. MSH_CMD_EXPORT(umqtt_ex_publish, umqtt publish message to specified topic);
  174. MSH_CMD_EXPORT(umqtt_ex_subscribe, umqtt subscribe topic);
  175. MSH_CMD_EXPORT(umqtt_ex_unsubscribe, umqtt unsubscribe topic);
  176. #endif