task.c 4.7 KB

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