123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #include "task.h"
- #include "cjson.h"
- #include "myFile.h"
- #include "gateway_message.h"
- #include "log.h"
- #include "malloc.h"
- #include "sx1276.h"
- #include "protocol.h"
- #include "usart.h"
- #include "node_data_acquisition.h"
- #include "sys_mqtt.h"
- void master_task(uint8_t *string,uint16_t stringlength);
- void slave_task();
- uint16_t BufferSize;
- uint8_t Buffer[256];
- uint32_t rx_num = 0;
- uint8_t PingMsg[] = "PING\0";
- uint8_t PongMsg[] = "PONG\0";
- tRadioDriver *Radio=NULL;
- /*
- *********************************************************************************************************
- * 函 数 名: void data_task(void *pdata)
- * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void data_task(void *pdata)
- {
- OS_CPU_SR cpu_sr;
- pdata = pdata;
-
- Radio = RadioDriverInit();
- Radio->Init();
- #ifdef MASTER
- char *lora_config_json = mymalloc(SRAMEX, 9 * 1024);
- read_file("lora_json.txt", lora_config_json);
- addGatewayParams(lora_config_json);
- myfree(SRAMEX, lora_config_json);
- GATEWAY_PARAMS *get;
- get= get_gateway_config_params();
- int nodeIndex=0;
- NODE_PARAMS *current_node=get->node_params;
- uint8_t string[256];
- uint16_t bufferLength;
- OS_Q_DATA Qnum;
- StringInfo message;
- char *mqttRecv;
- uint8_t err;
- while (current_node!=NULL)
- {
- while(!masterSendNodeString(nodeIndex,string,&bufferLength)) //轮询读出
- {
- master_task(string,bufferLength);
- }
- OSQQuery(JsonQ,&Qnum);
- //如果队列为空
- if(Qnum.OSNMsgs!=0)
- {
- mqttRecv=malloc(250);
- message=*(StringInfo *)OSQPend(JsonQ,0, &err);
- while(Qnum.OSNMsgs!=0)
- {
- sprintf(mqttRecv,"%s",message.p);
- }
- }
- if(current_node->nextNode!=NULL)
- {
- current_node=current_node->nextNode;
- nodeIndex++;
- }
- else
- {
- nodeIndex=0;
- current_node=get->node_params;
- }
-
- }
- #else
- dlt645_init(100);
- mmodbus_init(100);
- Radio->StartRx();
- while (1)
- {
- slave_task();
- OSTimeDlyHMSM(0, 0, 0, 200);
- }
- #endif
- }
- /*
- *********************************************************************************************************
- * 函 数 名: void master_task(char *string)
- * 功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- volatile uint32_t startTime; // 记录开始的时间且开始时间到结束时间超过一定范围则判定为超时状态,这时对其进行再次发送
- void master_task(uint8_t *string,uint16_t stringlength)
- {
- int retry_count=0; //发送计数,暂时不开启失败重发机制
- while (1)
- {
- switch (Radio->Process())
- {
- case RF_RX_DONE:
- Radio->GetRxPacket(Buffer, &BufferSize);
- if(GatewayProtocolAnalysis(Buffer,BufferSize)==0)
- {
- //接收的信息不属于节点信息重新进入接收模式
- Radio->StartRx();
- }
- else //完成一次发射应答过程
- {
- delay_ms(1000);
- return;
- }
- case RF_TX_DONE:
- Radio->StartRx();
- case RF_BUSY:
- case RF_IDLE:
- if(retry_count==0) //第一次发送
- {
- startTime = OSTimeGet();
- Radio->SetTxPacket(string, stringlength);
- retry_count++;
- }
- if (OSTimeGet() - startTime > 10000) // 每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
- {
- return;
- }
- default:
- OSTimeDlyHMSM(0, 0, 0, 200);
- break;
- }
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: void slave_task(char *string)
- * 功能说明: 负责从站数据处理
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- volatile bool rxflag = false;
- void slave_task()
- {
- switch (Radio->Process())
- {
- case RF_RX_DONE:
- Radio->GetRxPacket(Buffer, &BufferSize);
- if(SlaveProtocolAnalysis(Buffer,BufferSize)==1) //判断是否为该节点信息
- {
- data_acquisition();
- uint8_t node_string[256];
- uint16_t node_string_Length;
- nodeSendReaddValue(node_string,&node_string_Length); //组装回传包
- Radio->SetTxPacket(node_string, node_string_Length); //发送回传包
- }
- delay_ms(1000);
- break;
- case RF_TX_DONE:
- Radio->StartRx();
- break;
- case RF_IDLE:
- case RF_BUSY:
- default:
- OSTimeDlyHMSM(0, 0, 0, 200);
- }
- }
- #if 0 //测试代码不经过转发直接进行相应的解析
- SlaveProtocolAnalysis(string,bufferLength);
- data_acquisition();
- uint8_t node_string[256];
- uint16_t node_string_Length;
- nodeSendReaddValue(node_string,&node_string_Length);
- GatewayProtocolAnalysis(node_string,node_string_Length);
- #endif
-
|