sys_mqtt.c 5.2 KB

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