sys_mqtt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "string.h"
  2. #include "stm32f2xx.h"
  3. #include "ucos_ii.h"
  4. #include "hd_eth.h"
  5. #include "lwip/arch.h"
  6. #include "sys_mqtt.h"
  7. #include "transport.h"
  8. #include "MQTTFormat.h"
  9. #include "cJSON.h"
  10. #include "led.h"
  11. #include "MQTTClient.h"
  12. #include "malloc.h"
  13. #include "mmodbus.h"
  14. #include "myFile.h"
  15. #include "gateway_message.h"
  16. #include "task.h"
  17. /********************************************************
  18. * @brief 连接到服务器
  19. * @param sock: sock编号
  20. *********************************************************/
  21. int mqtt_userConnect(void)
  22. {
  23. GATEWAY_PARAMS* get;
  24. get = get_gateway_config_params();
  25. char* MQTT_SERVER_ADDR = (char*)get->host;
  26. int MQTT_SERVER_PORT = get->port;
  27. int sock = -1;
  28. //连接到tcp服务器
  29. sock = transport_open(MQTT_SERVER_ADDR, MQTT_SERVER_PORT);
  30. if(sock < 0)
  31. {
  32. MQTT_PRINTF("connect tcp server error \r\n");
  33. return -1;
  34. }
  35. MQTT_PRINTF("connect tcp server success \r\n");
  36. //连接到mqtt服务器
  37. if(mqtt_connectToMqttServer(sock) <= 0)
  38. {
  39. MQTT_PRINTF("connect mqtt server error \r\n");
  40. return -1;
  41. }
  42. MQTT_PRINTF("connect mqtt server success \r\n");
  43. return sock;
  44. }
  45. /************************************************************
  46. * @brief 订阅主题
  47. * @param sock: sock编号
  48. * @retval 1: 订阅成功
  49. *************************************************************/
  50. int mqtt_userSubscribeTopic(int sock)
  51. {
  52. GATEWAY_PARAMS* get;
  53. get = get_gateway_config_params();
  54. char* TOPICPC = (char*)get->commandTopic;
  55. return mqtt_subscribeTopic(sock, TOPICPC, 2);
  56. }
  57. /*
  58. *接收mqtt下发消息并存储进一个数据队列
  59. *name MQTT主题相关信息
  60. */
  61. void *json_message[10];
  62. #define QUEUE_SIZE 10 //队列深度
  63. //消息队列指针
  64. OS_EVENT *JsonQ;
  65. void mqtt_outputMsg(MQTTString *name, uint8_t *msgbuf, int msglen, uint16_t id, int qos)
  66. {
  67. int lenght=msglen;
  68. MQTT_PRINTF("receive a msg: id=%d qos=%d topic=%s msg=%s \r\n", id, qos, name->lenstring.data, msgbuf);
  69. StringInfo message;
  70. message.p=mymalloc(SRAMEX ,msglen);
  71. memcpy(message.p,msgbuf,msglen);
  72. OSMboxPost(mqtt_recvMseeageMbox,(void*)&message);
  73. }
  74. /************************************************************
  75. * @brief 处理来自平台发布的信息
  76. * @param sock: sock编号
  77. pbuf: 接收的数据
  78. buflen: 数据的长度
  79. * @retval 1: 消息正常处理
  80. * -2: 帧数据包错误
  81. *************************************************************/
  82. int mqtt_recvPublishMessage(int sock, uint8_t *pbuf, int buflen)
  83. {
  84. uint8_t dup, retained;
  85. int qos, payloadlen;
  86. uint16_t packetid;
  87. MQTTString topicName;
  88. uint8_t *payload;
  89. int msg;
  90. if(MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, &payload, &payloadlen, pbuf, buflen) == 1)
  91. {
  92. mqtt_outputMsg(&topicName, payload, payloadlen, packetid, qos);
  93. }
  94. else return -2;
  95. if(qos == 1)
  96. {
  97. mqtt_recvPublishQos1_packid = packetid;
  98. msg = MBOX_MQTT_QOS1PUBACK;
  99. OSMboxPost(mqtt_sendMseeageMbox, &msg);
  100. }
  101. if(qos == 2)
  102. {
  103. mqtt_recvPublishQos2_packid = packetid;
  104. msg = MBOX_MQTT_QOS2PUBREC;
  105. OSMboxPost(mqtt_sendMseeageMbox, &msg);
  106. }
  107. return 1;
  108. }
  109. /************************************************************
  110. * @brief 解析所有平台下发的数据包
  111. * @param sock: sock编号
  112. type: 数据帧的类型
  113. pbuf: 数据
  114. buflen: 数据的长度
  115. * @retval 1: 消息正常处理
  116. * -2: 帧数据包错误
  117. *************************************************************/
  118. int mqtt_userReceiveMessage(int sock, int type, uint8_t *pbuf, int len)
  119. {
  120. int rc;
  121. switch(type)
  122. {
  123. case PUBLISH: rc = mqtt_recvPublishMessage(sock, pbuf, len); break; //平台向设备发布消息
  124. case PUBACK: rc = mqtt_publishMessage_qos1_PUBACK(pbuf, len); break; //设备向平台发布 QOS1 确认报文
  125. case PUBREC: rc = mqtt_publishMessage_qos2_PUBREC(pbuf, len); break; //设备向平台发布 QOS2 确认报文
  126. case PUBREL: rc = mqtt_recvPublishMessage_qos2_PUBREL(sock, pbuf, len); break; //平台向设备发布 QOS2 消息释放报文
  127. case PUBCOMP: rc = mqtt_publishMessage_qos2_PUBCOMP(pbuf, len); break; //设备向平台发布 QOS2 消息释放报文的确认报文
  128. case SUBACK: rc = mqtt_subscribeTopic_SUBACK(pbuf, len);break; //设备向平台订阅报文时的响应报文
  129. case UNSUBACK: rc = mqtt_unSubscribeTopic_UNSUBACK(pbuf, len); break; //设备向平台发取消订阅报文时的响应报文
  130. case PINGRESP: rc = mqtt_pingResponse(); break; //心跳响应报文
  131. default: rc = 1; break;
  132. }
  133. return rc;
  134. }
  135. /************************************************************
  136. * @brief 处理发送邮箱 mqtt_recvMbox 中的消息,根据消息的类型,发送对应的数据包
  137. * @param sock: sock编号
  138. boxMsg: 消息内容
  139. * @retval 1: 消息正常处理
  140. * -1: 网络错误
  141. * -2: 组建发送数据时发送错误
  142. *************************************************************/
  143. char pubJsonString[jsonMaxSize];
  144. char pubJsonStringCopy[jsonMaxSize];
  145. int mqtt_userSendMessage(int sock, int boxMsg)
  146. {
  147. GATEWAY_PARAMS* get;
  148. get = get_gateway_config_params();
  149. char* TOPIC = (char*)get->messageTopic;
  150. int rc = 1;
  151. //用户发送的数据
  152. if((boxMsg & 0xf0000000) == 0x20000000)
  153. {
  154. switch(boxMsg)
  155. {
  156. case MBOX_USER_PUBLISHQOS0: rc = mqtt_publishMessage_qos0(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
  157. case MBOX_USER_PUBLISHQOS1: rc = mqtt_publishMessage_qos1(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
  158. case MBOX_USER_PUBLISHQOS2: rc = mqtt_publishMessage_qos2(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
  159. }
  160. }
  161. //协议站使用的数据
  162. else if((boxMsg & 0xf0000000) == 0x10000000)
  163. {
  164. switch(boxMsg)
  165. {
  166. case MBOX_MQTT_QOS1PUBACK: rc = mqtt_recvPublishMessage_qos1_PUBACK(sock, mqtt_recvPublishQos1_packid); break;
  167. case MBOX_MQTT_QOS2PUBREC: rc = mqtt_recvPublishMessage_qos2_PUBREC(sock, mqtt_recvPublishQos2_packid); break;
  168. case MBOX_MQTT_QOS2PUBCOMP: rc = mqtt_recvPublishMessage_qos2_PUBCOMP(sock, mqtt_recvPublishQos2_packid); break;
  169. }
  170. }
  171. return rc;
  172. }
  173. OS_EVENT *mqtt_sendMseeageMbox; //发送消息邮箱
  174. OS_EVENT *mqtt_recvMseeageMbox; //接收消息邮箱
  175. int mqtt_connectFlag; //成功连接服务器标志
  176. static int mysock; //连接mqtt服务器的sock编号
  177. /************************************************************
  178. * @brief MQTT主线程
  179. * @param arg: 未使用
  180. * @retval
  181. *************************************************************/
  182. void mqtt_userManThread(void *arg)
  183. {
  184. int rc;
  185. uint8_t err;
  186. void *mboxMsg;
  187. MQTT_PRINTF("mqtt mainthread start \r\n");
  188. __MQTT_START:
  189. //1.建立与服务器的连接
  190. mysock = -1;
  191. mqtt_connectFlag = 0;
  192. while(mysock < 0)
  193. {
  194. mysock = mqtt_userConnect();
  195. OSTimeDly(2000);
  196. }
  197. OSMboxAccept(mqtt_sendMseeageMbox); //清空mbox的数据
  198. mqtt_connectFlag = 1;
  199. //2.订阅服务器的主题
  200. rc = mqtt_userSubscribeTopic(mysock);
  201. if(rc <= 0)
  202. {
  203. MQTT_PRINTF("subscribe error \r\n");
  204. if(rc == -1) goto __MQTT_START; //如果网络发生错误,重新建立连接
  205. }
  206. else MQTT_PRINTF("subscribe success \r\n");
  207. //3.循环发送数据
  208. while(1)
  209. {
  210. mboxMsg = OSMboxPend(mqtt_sendMseeageMbox, 60000, &err);
  211. if(OS_ERR_NONE == err)
  212. {
  213. //接收到了 网络异常消息
  214. if((*(unsigned int *)mboxMsg) == MBOX_NETWORK_ERROR) goto __MQTT_START;
  215. else
  216. {
  217. if(mqtt_userSendMessage(mysock, *(unsigned int *)mboxMsg) == -1)
  218. goto __MQTT_START;
  219. memset(pubJsonString,0,strlen(pubJsonString));
  220. }
  221. }
  222. else //超时没有发送数据包,发送心跳包
  223. {
  224. rc = mqtt_pingReq(mysock);
  225. if(rc == -1) goto __MQTT_START;
  226. }
  227. OSTimeDly(100);
  228. }
  229. }
  230. /************************************************************
  231. * @brief MQTT接收线程,处理所有平台下发的数据帧, CONNACK除外
  232. * @param arg: 未使用
  233. * @retval
  234. *************************************************************/
  235. void mqtt_userReceiveThread(void *arg)
  236. {
  237. int len;
  238. int packetType;
  239. int msg;
  240. MQTT_PRINTF("mqtt receivethread start \r\n");
  241. while(1)
  242. {
  243. if(mqtt_connectFlag == 1)
  244. {
  245. len = transport_receive(mysock); //阻塞接收数据
  246. if(len <=0)
  247. {
  248. if(len == EWOULDBLOCK) //接收数据超时,重新接收
  249. {
  250. MQTT_PRINTF("receive data timeout \r\n");
  251. continue;
  252. }
  253. else //接收数据时,网络发生了异常,给主线程发送消息,重新建立连接
  254. {
  255. MQTT_PRINTF("sock close \r\n");
  256. transport_close(mysock);
  257. mqtt_connectFlag = 0;
  258. msg = MBOX_NETWORK_ERROR;
  259. OSMboxPost(mqtt_sendMseeageMbox, &msg);
  260. }
  261. }
  262. memset(mqtt_recvbuffer, 0, MQTT_RECVBUF_LENTH);
  263. packetType = MQTTPacket_read(mqtt_recvbuffer, len, transport_getdata);
  264. mqtt_userReceiveMessage(mysock, packetType, mqtt_recvbuffer, len);
  265. }
  266. OSTimeDly(100);
  267. }
  268. }
  269. #define APP_TASK_MQTTMAIN_PRIO 6
  270. #define APP_TASK_MQTTMAIN_STK_SIZE 1024
  271. static OS_STK mqttmainTaskStack[APP_TASK_MQTTMAIN_STK_SIZE];
  272. #define APP_TASK_MQTTRECEIVE_PRIO 7
  273. #define APP_TASK_MQTTRECEIVE_STK_SIZE 1024
  274. static OS_STK mqttreceiveTaskStack[APP_TASK_MQTTRECEIVE_STK_SIZE];
  275. /************************************************************
  276. * @brief 创建MQTT发送和接收线程,初始化相关数据
  277. * @param 此函数必须在LWIP协议栈初始完成以后再调用
  278. * @retval
  279. *************************************************************/
  280. void mqtt_threadCreate(void)
  281. {
  282. mqtt_connectFlag = 0;
  283. mqtt_sendMseeageMbox = OSMboxCreate(NULL);
  284. mqtt_recvMseeageMbox = OSMboxCreate(NULL);
  285. JsonQ = OSQCreate(json_message[0], QUEUE_SIZE); //创建json队列
  286. OSTaskCreate(mqtt_userManThread, NULL, &mqttmainTaskStack[APP_TASK_MQTTMAIN_STK_SIZE-1], APP_TASK_MQTTMAIN_PRIO);
  287. OSTaskCreate(mqtt_userReceiveThread, NULL, &mqttreceiveTaskStack[APP_TASK_MQTTRECEIVE_STK_SIZE-1], APP_TASK_MQTTRECEIVE_PRIO);
  288. }