task.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "task.h"
  2. #include "cjson.h"
  3. #include "myFile.h"
  4. #include "gateway_message.h"
  5. #include "log.h"
  6. #include "malloc.h"
  7. #include "sx1276.h"
  8. #include "protocol.h"
  9. #include "usart.h"
  10. #include "node_data_acquisition.h"
  11. #include "sys_mqtt.h"
  12. void master_task(uint8_t *string,uint16_t stringlength);
  13. void slave_task();
  14. uint16_t BufferSize;
  15. uint8_t Buffer[256];
  16. uint32_t rx_num = 0;
  17. uint8_t PingMsg[] = "PING\0";
  18. uint8_t PongMsg[] = "PONG\0";
  19. tRadioDriver *Radio=NULL;
  20. /*
  21. *********************************************************************************************************
  22. * 函 数 名: void data_task(void *pdata)
  23. * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
  24. * 形 参:无
  25. * 返 回 值: 无
  26. *********************************************************************************************************
  27. */
  28. void data_task(void *pdata)
  29. {
  30. OS_CPU_SR cpu_sr;
  31. pdata = pdata;
  32. Radio = RadioDriverInit();
  33. Radio->Init();
  34. #ifdef MASTER
  35. char *lora_config_json = mymalloc(SRAMEX, 9 * 1024);
  36. read_file("lora_json.txt", lora_config_json);
  37. addGatewayParams(lora_config_json);
  38. myfree(SRAMEX, lora_config_json);
  39. GATEWAY_PARAMS *get;
  40. get= get_gateway_config_params();
  41. int nodeIndex=0;
  42. NODE_PARAMS *current_node=get->node_params;
  43. uint8_t string[256];
  44. uint16_t bufferLength;
  45. OS_Q_DATA Qnum;
  46. StringInfo message;
  47. char *mqttRecv;
  48. uint8_t err;
  49. while (current_node!=NULL)
  50. {
  51. while(!masterSendNodeString(nodeIndex,string,&bufferLength)) //轮询读出
  52. {
  53. master_task(string,bufferLength);
  54. }
  55. OSQQuery(JsonQ,&Qnum);
  56. //如果队列为空
  57. if(Qnum.OSNMsgs!=0)
  58. {
  59. mqttRecv=malloc(250);
  60. message=*(StringInfo *)OSQPend(JsonQ,0, &err);
  61. while(Qnum.OSNMsgs!=0)
  62. {
  63. sprintf(mqttRecv,"%s",message.p);
  64. }
  65. }
  66. if(current_node->nextNode!=NULL)
  67. {
  68. current_node=current_node->nextNode;
  69. nodeIndex++;
  70. }
  71. else
  72. {
  73. nodeIndex=0;
  74. current_node=get->node_params;
  75. }
  76. }
  77. #else
  78. dlt645_init(100);
  79. mmodbus_init(100);
  80. Radio->StartRx();
  81. while (1)
  82. {
  83. slave_task();
  84. OSTimeDlyHMSM(0, 0, 0, 200);
  85. }
  86. #endif
  87. }
  88. /*
  89. *********************************************************************************************************
  90. * 函 数 名: void master_task(char *string)
  91. * 功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
  92. * 形 参:无
  93. * 返 回 值: 无
  94. *********************************************************************************************************
  95. */
  96. volatile uint32_t startTime; // 记录开始的时间且开始时间到结束时间超过一定范围则判定为超时状态,这时对其进行再次发送
  97. void master_task(uint8_t *string,uint16_t stringlength)
  98. {
  99. int retry_count=0; //发送计数,暂时不开启失败重发机制
  100. while (1)
  101. {
  102. switch (Radio->Process())
  103. {
  104. case RF_RX_DONE:
  105. Radio->GetRxPacket(Buffer, &BufferSize);
  106. if(GatewayProtocolAnalysis(Buffer,BufferSize)==0)
  107. {
  108. //接收的信息不属于节点信息重新进入接收模式
  109. Radio->StartRx();
  110. }
  111. else //完成一次发射应答过程
  112. {
  113. delay_ms(1000);
  114. return;
  115. }
  116. case RF_TX_DONE:
  117. Radio->StartRx();
  118. case RF_BUSY:
  119. case RF_IDLE:
  120. if(retry_count==0) //第一次发送
  121. {
  122. startTime = OSTimeGet();
  123. Radio->SetTxPacket(string, stringlength);
  124. retry_count++;
  125. }
  126. if (OSTimeGet() - startTime > 10000) // 每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
  127. {
  128. return;
  129. }
  130. default:
  131. OSTimeDlyHMSM(0, 0, 0, 200);
  132. break;
  133. }
  134. }
  135. }
  136. /*
  137. *********************************************************************************************************
  138. * 函 数 名: void slave_task(char *string)
  139. * 功能说明: 负责从站数据处理
  140. * 形 参:无
  141. * 返 回 值: 无
  142. *********************************************************************************************************
  143. */
  144. volatile bool rxflag = false;
  145. void slave_task()
  146. {
  147. switch (Radio->Process())
  148. {
  149. case RF_RX_DONE:
  150. Radio->GetRxPacket(Buffer, &BufferSize);
  151. if(SlaveProtocolAnalysis(Buffer,BufferSize)==1) //判断是否为该节点信息
  152. {
  153. data_acquisition();
  154. uint8_t node_string[256];
  155. uint16_t node_string_Length;
  156. nodeSendReaddValue(node_string,&node_string_Length); //组装回传包
  157. Radio->SetTxPacket(node_string, node_string_Length); //发送回传包
  158. }
  159. delay_ms(1000);
  160. break;
  161. case RF_TX_DONE:
  162. Radio->StartRx();
  163. break;
  164. case RF_IDLE:
  165. case RF_BUSY:
  166. default:
  167. OSTimeDlyHMSM(0, 0, 0, 200);
  168. }
  169. }
  170. #if 0 //测试代码不经过转发直接进行相应的解析
  171. SlaveProtocolAnalysis(string,bufferLength);
  172. data_acquisition();
  173. uint8_t node_string[256];
  174. uint16_t node_string_Length;
  175. nodeSendReaddValue(node_string,&node_string_Length);
  176. GatewayProtocolAnalysis(node_string,node_string_Length);
  177. #endif