sys_mqtt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "usart.h"
  20. int mqtt_connectFlag;
  21. static void mqtt_publish_task(void const *arg);
  22. /*
  23. *接收并处理mqtt订阅消息
  24. */
  25. static void topic1_handler(void *client, message_data_t *msg)
  26. {
  27. (void)client;
  28. MQTT_LOG_I("topic: %s\nmessage:%s", msg->topic_name, (char *)msg->message->payload);
  29. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"Write Modbus Data");
  30. // json 解析数据
  31. if(ProtocolsModeFlag)
  32. {
  33. write_modbus_data((char*)msg->message->payload);
  34. }
  35. else if(TransparentModeFlag)
  36. {
  37. USART_485_Send(msg->message->payload, msg->message->payloadlen);
  38. }
  39. }
  40. /*
  41. *初始化mqtt连接
  42. */
  43. int8_t mqtt_init(mqtt_client_t *client, char *clientId, char *user_name, char *password, char *ip, char* port, uint16_t keepAlive)
  44. {
  45. mqtt_set_client_id(client, clientId);
  46. mqtt_set_port(client, port);
  47. mqtt_set_host(client, ip);
  48. mqtt_set_user_name(client, user_name);
  49. mqtt_set_password(client, password);
  50. mqtt_set_clean_session(client, 1);
  51. mqtt_set_keep_alive_interval(client, keepAlive);
  52. return mqtt_connect(client);
  53. }
  54. // 发送数据队列句柄
  55. QueueHandle_t xQueue1;
  56. void mqtt_task_creat()
  57. {
  58. mqtt_client_t *client = NULL; // 创建一个客户端
  59. mqtt_log_init();
  60. client = mqtt_lease();
  61. printf("\nwelcome to mqttclient test...\n");
  62. xQueue1 = xQueueCreate(10, sizeof(struct Pub_Queue *)); // 创建一个mqtt上传的队列
  63. osThreadDef(MQTT_task, mqtt_publish_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 8);
  64. osThreadCreate (osThread(MQTT_task), client);
  65. }
  66. static void mqtt_publish_task(void const *arg)
  67. {
  68. GATEWAY_PARAMS *get;
  69. get= get_gateway_config_params();
  70. int rc;
  71. char port[6];
  72. mqtt_client_t *client;
  73. __MQTT_START:
  74. rc = -1;
  75. mqtt_connectFlag = 0;
  76. client = (mqtt_client_t *)arg;
  77. while (rc != 0)
  78. {
  79. vTaskDelay(500);
  80. sprintf(port, "%hu", get->port);
  81. rc = mqtt_init(client, "client_id_002", (char*)&get->username,(char*)&get->passwd,(char*)&get->host,port, 120);
  82. // rc = mqtt_init(client, "client_id_002", "device","Dev&*(789","183.162.218.20","1883", 120);
  83. }
  84. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"MQTT Connect success");
  85. mqtt_connectFlag = 1;
  86. if(mqtt_subscribe(client, (char*)&get->commandTopic, QOS0, topic1_handler) != 0)
  87. {
  88. LogPrint(LOG_WARN,__FILE__,__FUNCTION__,__LINE__,"MQTT Subscribe fail");
  89. }
  90. mqtt_message_t msg;
  91. memset(&msg, 0, sizeof(msg));
  92. mqtt_list_subscribe_topic(client);
  93. struct Pub_Queue *pxMessage;
  94. while (1)
  95. {
  96. if (xQueue1 != 0)
  97. {
  98. if (xQueueReceive(xQueue1, &(pxMessage), (TickType_t)10))
  99. {
  100. // 处理队列内部数据并上传
  101. msg.payloadlen = pxMessage->pubLength;
  102. msg.qos = pxMessage->qos;
  103. msg.payload = (void *)pxMessage->message;
  104. if(mqtt_publish(client, pxMessage->pub_topic, &msg) != 0)// 0:成功
  105. {
  106. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"MQTT Publish Failed");
  107. myfree(SRAMEX, pxMessage->pub_topic);
  108. myfree(SRAMEX, pxMessage->message);
  109. myfree(SRAMEX, pxMessage);
  110. mqtt_unsubscribe(client,(char*)&get->commandTopic);
  111. mqtt_disconnect(client);
  112. platform_thread_destroy(client->mqtt_thread);
  113. client->mqtt_thread = NULL;
  114. goto __MQTT_START;
  115. }
  116. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"MQTT Publish Message");
  117. myfree(SRAMEX, pxMessage->pub_topic);
  118. myfree(SRAMEX, pxMessage->message);
  119. myfree(SRAMEX, pxMessage);
  120. }
  121. }
  122. vTaskDelay(500);
  123. }
  124. }
  125. /*
  126. * 函数名:void mqtt_publish_data(uint8_t *payload,mqtt_qos_t qos,uint16_t pub_length,char *topic)
  127. * 输入参数:payload上传的数据包,qos以什么等级去发布数据,pub_length上传的数据包长度,topic上传的topic
  128. * 输出参数:无
  129. * 返回值:无
  130. * 函数作用:向队列中写入mqtt上传的消息
  131. * TODO:队列满无法写入的情况处理
  132. */
  133. uint8_t size;
  134. void mqtt_publish_data(char *payload, mqtt_qos_t qos, uint16_t pub_length, char *topic)
  135. {
  136. size = my_mem_perused(SRAMEX);//9
  137. struct Pub_Queue *pxMessage = mymalloc(SRAMEX, sizeof(struct Pub_Queue));
  138. if(pxMessage == NULL)
  139. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"queue message malloc fail");
  140. size = my_mem_perused(SRAMEX);
  141. pxMessage->message = mymalloc(SRAMEX, pub_length + 1);
  142. if(pxMessage == NULL)
  143. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"queue message malloc fail");
  144. memset(pxMessage->message, 0, (pub_length + 1));
  145. memcpy(pxMessage->message, payload, pub_length);
  146. size = my_mem_perused(SRAMEX);
  147. pxMessage->pub_topic = mymalloc(SRAMEX,strlen(topic) + 1);
  148. if(pxMessage->pub_topic == NULL)
  149. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"queue message malloc fail");
  150. memset(pxMessage->pub_topic, 0, (strlen(topic) + 1));
  151. strcpy(pxMessage->pub_topic, topic);
  152. pxMessage->pubLength = pub_length;
  153. pxMessage->qos = qos;
  154. if(pxMessage->message == NULL)
  155. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"queue message malloc fail");
  156. xQueueSend(xQueue1, (void *)&pxMessage, (TickType_t)0);
  157. }