sys_mqtt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-11 21:53:07
  5. * @LastEditTime : 2022-06-15 23:03:30
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "mqtt_config.h"
  11. #include "mqtt_log.h"
  12. #include "sys_mqtt.h"
  13. #include "sys_http.h"
  14. #include "gateway_message.h"
  15. #include "data_task.h"
  16. #include "app_ethernet.h"
  17. #include "tcp_server.h"
  18. #include "main.h"
  19. int mqtt_connectFlag;
  20. static void mqtt_publish_task(void const *arg);
  21. /*
  22. *接收并处理mqtt订阅消息
  23. */
  24. static void topic1_handler(void *client, message_data_t *msg)
  25. {
  26. (void)client;
  27. MQTT_LOG_I("topic: %s\nmessage:%s", msg->topic_name, (char *)msg->message->payload);
  28. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"Write Modbus Data");
  29. // json 解析数据
  30. write_modbus_data((char*)msg->message->payload);
  31. }
  32. /*
  33. *初始化mqtt连接
  34. */
  35. int8_t mqtt_init(mqtt_client_t *client, char *clientId, char *user_name, char *password, char *ip, char* port, uint16_t keepAlive)
  36. {
  37. mqtt_set_client_id(client, clientId);
  38. mqtt_set_port(client, port);
  39. mqtt_set_host(client, ip);
  40. mqtt_set_user_name(client, user_name);
  41. mqtt_set_password(client, password);
  42. mqtt_set_clean_session(client, 1);
  43. mqtt_set_keep_alive_interval(client, keepAlive);
  44. return mqtt_connect(client);
  45. }
  46. // 发送数据队列句柄
  47. QueueHandle_t xQueue1;
  48. void mqtt_task_creat()
  49. {
  50. // mqtt_client_t *client = NULL; // 创建一个客户端
  51. mqtt_log_init();
  52. printf("\nwelcome to mqttclient test...\n");
  53. xQueue1 = xQueueCreate(10, sizeof(struct Pub_Queue *)); // 创建一个mqtt上传的队列
  54. osThreadDef(MQTT_task, mqtt_publish_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 8);
  55. osThreadCreate (osThread(MQTT_task), NULL);
  56. }
  57. static void mqtt_publish_task(void const *arg)
  58. {
  59. mqtt_client_t *client = NULL; // 创建一个客户端
  60. client = mqtt_lease();
  61. GATEWAY_PARAMS *get;
  62. get= get_gateway_config_params();
  63. int rc;
  64. char port[6];
  65. __MQTT_START:
  66. rc = -1;
  67. while (rc != 0)
  68. {
  69. vTaskDelay(500);
  70. sprintf(port, "%hu", get->port);
  71. rc = mqtt_init(client, "client_id_002", (char*)&get->username,(char*)&get->passwd,(char*)&get->host,port, 120);
  72. // rc = mqtt_init(client, "client_id_002", "device","Dev&*(789","183.162.218.20","1883", 120);
  73. }
  74. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"MQTT Connect success");
  75. mqtt_connectFlag = 1;
  76. if(mqtt_subscribe(client, (char*)&get->commandTopic, QOS0, topic1_handler) != 0)
  77. {
  78. LogPrint(LOG_WARN,__FILE__,__FUNCTION__,__LINE__,"MQTT Subscribe fail");
  79. }
  80. mqtt_message_t msg;
  81. memset(&msg, 0, sizeof(msg));
  82. mqtt_list_subscribe_topic(client);
  83. struct Pub_Queue *pxMessage;
  84. while (1)
  85. {
  86. if (xQueue1 != 0)
  87. {
  88. if (xQueueReceive(xQueue1, &(pxMessage), (TickType_t)10))
  89. {
  90. // 处理队列内部数据并上传
  91. msg.payloadlen = pxMessage->pubLength;
  92. msg.qos = pxMessage->qos;
  93. msg.payload = (void *)pxMessage->message;
  94. if(mqtt_publish(client, pxMessage->pub_topic, &msg) != 0)// 0:成功
  95. {
  96. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"MQTT Publish Failed");
  97. mqtt_unsubscribe(client,(char*)&get->commandTopic);
  98. mqtt_disconnect(client);
  99. platform_thread_destroy(client->mqtt_thread);
  100. client->mqtt_thread = NULL;
  101. goto __MQTT_START;
  102. }
  103. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"MQTT Publish Message");
  104. myfree(SRAMEX, pxMessage->pub_topic);
  105. myfree(SRAMEX, pxMessage->message);
  106. myfree(SRAMEX, pxMessage);
  107. }
  108. }
  109. vTaskDelay(500);
  110. }
  111. }
  112. /*
  113. * 函数名:void mqtt_publish_data(uint8_t *payload,mqtt_qos_t qos,uint16_t pub_length,char *topic)
  114. * 输入参数:payload上传的数据包,qos以什么等级去发布数据,pub_length上传的数据包长度,topic上传的topic
  115. * 输出参数:无
  116. * 返回值:无
  117. * 函数作用:向队列中写入mqtt上传的消息
  118. * TODO:队列满无法写入的情况处理
  119. */
  120. void mqtt_publish_data(char *payload, mqtt_qos_t qos, uint16_t pub_length, char *topic)
  121. {
  122. struct Pub_Queue *pxMessage = mymalloc(SRAMEX, sizeof(struct Pub_Queue));
  123. pxMessage->message = mymalloc(SRAMEX, pub_length + 1);
  124. memset(pxMessage->message, 0, (pub_length + 1));
  125. memcpy(pxMessage->message, payload, pub_length);
  126. pxMessage->pub_topic = mymalloc(SRAMEX,strlen(topic) + 1);
  127. memset(pxMessage->pub_topic, 0, (strlen(topic) + 1));
  128. strcpy(pxMessage->pub_topic, topic);
  129. pxMessage->pubLength = pub_length;
  130. pxMessage->qos = qos;
  131. if(pxMessage->message == NULL)
  132. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"queue message malloc fail");
  133. xQueueSend(xQueue1, (void *)&pxMessage, (TickType_t)0);
  134. }