gw_ctrl.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. * @Description:
  3. * @Version: 2.0
  4. * @Author: Seven
  5. * @Date: 2022-08-22 09:55:26
  6. * @LastEditors: Seven
  7. * @LastEditTime: 2022-12-06 13:31:27
  8. */
  9. #include "gw_ctrl.h"
  10. #include "tools.h"
  11. #include "lora.h"
  12. #include "includes.h"
  13. #include "encoder.h"
  14. #include "tax.h"
  15. uint8_t rcv_buf[UART_RCV_PROCBUF_LEN];
  16. uint16_t rcv_size,rcv_size_temp;
  17. msg_para_t msg_para;
  18. lora_comm_info_t lora_comm_info;
  19. ciphertext_state_t ciphertext_state;
  20. read_encoder_para_t read_encoder_para;
  21. read_56_info_t read_56_info;
  22. cmd_73_info_t cmd_73_info[2];
  23. uart_timeout_times_t uart_timeout_times;//报税口超时连续次数
  24. //加密芯片状态初始化
  25. void ciphertext_state_init(void)
  26. {
  27. memset(&ciphertext_state,0,sizeof(ciphertext_state_t));
  28. }
  29. //打开加密芯片死机定时
  30. void ciphertext_time_start(void)
  31. {
  32. if(device_fixed_info.Encrypt == CIPHERTEXT)//密文
  33. {
  34. if(ciphertext_state.ciphertext.flag==0)//如果未打开定时
  35. {
  36. timeout_setValue(&ciphertext_state.ciphertext,CIPHERTEXT_RESET_TIME,1);//定时1hour
  37. }
  38. }
  39. }
  40. //加密芯片超时处理
  41. void ciphertext_state_isout(void)
  42. {
  43. //如果设备上电,加密模块发送次数超过一次且一次也没接收,延时1hour重启
  44. if(timeout_isOut(&ciphertext_state.ciphertext))
  45. {
  46. printf("加密模块死了");
  47. //复位加密芯片
  48. port_init(PORT_LED_OFF); //初始化加密模块
  49. ciphertext_state_init();
  50. }
  51. }
  52. //网关下发参数存储初始化
  53. void msg_para_init(void)
  54. {
  55. memset(&msg_para,0,sizeof(msg_para_t));
  56. }
  57. //离线次数判断处理
  58. void uart_timeout_time_handle(uint8_t tax)
  59. {
  60. if(tax == 1)
  61. {
  62. uart_timeout_times.tax1_times++;
  63. if(((uart_timeout_times.tax1_times%10)==9) && uart_timeout_times.tax1_times < 59)
  64. {
  65. tax_sw_reset(1);
  66. }
  67. printf("tax1 timeout num:%u\n",uart_timeout_times.tax1_times);
  68. }
  69. else if(tax == 2)
  70. {
  71. uart_timeout_times.tax2_times++;
  72. if(((uart_timeout_times.tax2_times%10)==9) && uart_timeout_times.tax2_times < 59)
  73. {
  74. tax_sw_reset(2);
  75. }
  76. printf("tax2 timeout num:%u\n",uart_timeout_times.tax2_times);
  77. }
  78. }
  79. //串口超时处理函数
  80. void uart_timeout_handle(void)
  81. {
  82. if(timeout_isOut(&lora_comm_info.uart_timeout))
  83. {
  84. // printf("timeout:%d\n",(TickCounter - lora_comm_info.uart_timeout.count));
  85. printf("uart_timeout:%d\n",lora_comm_info.uart_timeout.timeout);
  86. //离线次数
  87. uart_fifo_clear(UART4_ID);//清除串口缓存数据
  88. uart_fifo_clear(UART5_ID);//清除串口缓存数据
  89. if(tax_send_info.start)
  90. {
  91. if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT)
  92. {
  93. if(read_56_info.step==2)//uart4
  94. {
  95. coll_heartbeat_return();
  96. read_56_info.step = 0;
  97. msg_para_init();//清除存储的下发参数
  98. tax_send_end();
  99. return;
  100. }
  101. else//uart3
  102. {
  103. read_56_info.step = 2;
  104. tax_send_end();
  105. read_56_start(UART5_ID);
  106. return;
  107. }
  108. }
  109. }
  110. //上报超时数据
  111. if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3)
  112. {
  113. //关闭串口超时
  114. timeout_stop(&lora_comm_info.uart_timeout);
  115. if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据
  116. {
  117. transparent_up_ver3_return(UART_TIMEOUT,NULL,0);
  118. msg_rcv_info_init();
  119. }
  120. }
  121. else if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_FF)//版本查询
  122. {
  123. msg_rcv_info_init();
  124. printf("xsp version timeout\n");
  125. }
  126. else
  127. {
  128. if(msg_para.Uart == UART_485)//显示屏
  129. {
  130. gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TIMEOUT,NULL,0);
  131. }
  132. else//报税口
  133. {
  134. if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令
  135. coll_transparent_return(msg_para.First_type,UART_TIMEOUT,NULL,0);
  136. else
  137. gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TIMEOUT,NULL,0);
  138. tax_send_end();
  139. }
  140. msg_para_init();//清除存储的下发参数
  141. }
  142. // //上报超时数据
  143. // if(msg_para.Uart == UART3_ID)//显示屏
  144. // {
  145. // gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TIMEOUT,NULL,0);
  146. // }
  147. // else//报税口
  148. // {
  149. // if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令
  150. // coll_transparent_return(msg_para.First_type,UART_TIMEOUT,NULL,0);
  151. // else
  152. // gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TIMEOUT,NULL,0);
  153. // }
  154. // msg_para_init();//清除存储的下发参数
  155. }
  156. }
  157. //接收网关命令的返回
  158. void gateway_msg_return(collect_gateway_msg_format_t *p_msg, uint8_t msg_type, uint16_t len)
  159. {
  160. char *msg = NULL;
  161. uint16_t *p_crc;
  162. p_msg->frame_header = 0xfefe;
  163. p_msg->proto_Ver = PROTOCOL_VERSION;
  164. p_msg->msg_id = msg_para.Msg_id;
  165. p_msg->first_type = msg_type;
  166. if(msg_type == FIRST_TYPE_SYSTEM)
  167. p_msg->second_type = SYSTEM_SECOND_TRANS_UP;
  168. else
  169. p_msg->second_type = 0x00;
  170. p_msg->msg_len = len+2;//后面加两字节的CRC
  171. p_crc = (uint16_t*)&p_msg->info[len];
  172. *p_crc = CRC16_get((uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN-2);
  173. msg = (char *)lora_queue_mem_calloc_must(SYS_LORA_TX);
  174. if(msg) {
  175. memcpy(msg, (char *)p_msg, FRAME_HEADER_LEN + len+2);
  176. lora_queue_insert(SYS_LORA_TX,(char *)msg, FRAME_HEADER_LEN + len+2);
  177. }
  178. else
  179. {
  180. lora_queue_mem_free(SYS_LORA_TX,msg);
  181. }
  182. }
  183. //查询业务数据的返回
  184. void gateway_down_business_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len)
  185. {
  186. collect_gateway_msg_format_t p_msg;
  187. business_query_info_t *p_tax = (business_query_info_t *)p_msg.info;
  188. p_tax->device_sn = device_info.device_sn;
  189. p_tax->coll_no = msg_para.Coll_no;
  190. p_tax->device_type = msg_para.Devices_type;
  191. p_tax->tax_no = msg_para.Tax_no;
  192. p_tax->gun_no = msg_para.Gun_no;
  193. p_tax->prot_type = msg_para.Prot_type;
  194. p_tax->status = state;
  195. if(state == UART_SUCCESS)
  196. {
  197. p_tax->data_len = len;
  198. memcpy(p_tax->info,data,len);
  199. }
  200. else
  201. {
  202. p_tax->data_len = 0;
  203. }
  204. if((first_type == COLL_GATE_TAX_REPLY) && (state != TAX_GUN_ERR))
  205. {
  206. if(cmd_73_info[p_tax->tax_no-1].read_flag == 1)
  207. {
  208. p_tax->device_type = COLL_73;
  209. }
  210. }
  211. gateway_msg_return(&p_msg,first_type,p_tax->data_len+BUSINESS_QUERY_HEADER_LEN);
  212. // data_dump_debug(LORA_BUSINESS_DEBUG,"business return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  213. if(state == UART_SUCCESS)
  214. {
  215. if(p_msg.first_type == COLL_GATE_ENCODER)//编码器数据
  216. {
  217. data_dump_debug(MSG_ENCODER_DATA_TYPE1,"encoder return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  218. }
  219. else
  220. {
  221. data_dump_debug(LORA_BUSINESS_DEBUG,"business return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  222. }
  223. }
  224. else
  225. {
  226. data_dump_debug(MSG_ERR_DATA_TYPE1,"err return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  227. }
  228. }
  229. //报税口指示灯
  230. void tax_led_set(uint8_t uartid, uint8_t state)
  231. {
  232. if(uartid == UART4_ID)
  233. {
  234. if(state == SEND_LED_FLAG)
  235. led_start(TAX1_SEND_LED);
  236. else if(state == RCV_LED_FLAG)
  237. led_start(TAX1_RCV_LED);
  238. }
  239. else if(uartid == UART5_ID)
  240. {
  241. if(state == SEND_LED_FLAG)
  242. led_start(TAX2_SEND_LED);
  243. else if(state == RCV_LED_FLAG)
  244. led_start(TAX2_RCV_LED);
  245. }
  246. }
  247. //网关下发查询业务数据分析
  248. void gateway_down_business_proc(collect_gateway_msg_format_t *p_msg)
  249. {
  250. uint8_t encrypt=0;
  251. uint8_t order;
  252. uint8_t gunstatus;
  253. business_query_info_t *p_tax = (business_query_info_t *)p_msg->info;
  254. //保存下发的参数
  255. msg_para.Msg_id = p_msg->msg_id;
  256. msg_para.Devices_type = p_tax->device_type;
  257. msg_para.Tax_no = p_tax->tax_no;
  258. msg_para.Gun_no = p_tax->gun_no;
  259. msg_para.Prot_type = p_tax->prot_type;
  260. printf_debug(MSG_DISPLAY_DOWN_TYPE2,"p_msg_dev_type:%04x\n",p_tax->device_type);
  261. if(p_msg->first_type == GATE_COLL_TAX_QUERY) //查报税口
  262. {
  263. if(/*msg_para.Prot_type != device_fixed_info.Encrypt || */msg_para.Devices_type != TAX)//明文密文协议错误或查询设备类型错误
  264. {
  265. //关闭串口超时
  266. timeout_stop(&lora_comm_info.uart_timeout);
  267. gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TYPE_ERR,NULL,0);
  268. msg_para_init();//清除存储的下发参数
  269. return;
  270. }
  271. else if(msg_para.Tax_no != 1 && msg_para.Tax_no != 2)//报税口号错误
  272. {
  273. //关闭串口超时
  274. timeout_stop(&lora_comm_info.uart_timeout);
  275. gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_GUN_ERR,NULL,0);
  276. msg_para_init();//清除存储的下发参数
  277. return;
  278. }
  279. // if(tax_oil_judge_all())//报税口正在加油,不查询(所有报税口)
  280. // {
  281. // timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  282. // gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0);
  283. // msg_para_init();//清除存储的下发参数
  284. // return;
  285. // }
  286. gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口
  287. if(gunstatus)
  288. {
  289. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  290. gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0);
  291. msg_para_init();//清除存储的下发参数
  292. return;
  293. }
  294. if(read_encoder_para.flag)//读编码器后2s内
  295. {
  296. // gunstatus = Gun_Signal_Check_all();
  297. if(gunstatus != read_encoder_para.gunstatus)
  298. {
  299. //至少有一把枪的抬枪状态发生了变化
  300. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  301. gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0);
  302. msg_para_init();//清除存储的下发参数
  303. return;
  304. }
  305. }
  306. if(msg_para.Tax_no==1)
  307. {
  308. encrypt = device_fixed_info.Encrypt&0x03;
  309. }
  310. else if(msg_para.Tax_no==2)
  311. {
  312. encrypt = device_fixed_info.Encrypt&0x0C;
  313. encrypt >>= 2;
  314. }
  315. if(((msg_para.Prot_type&0x0f) != encrypt) && (msg_para.Prot_type&0x0f))
  316. {
  317. //关闭串口超时
  318. timeout_stop(&lora_comm_info.uart_timeout);
  319. gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TYPE_ERR,NULL,0);
  320. msg_para_init();//清除存储的下发参数
  321. return;
  322. }
  323. if(((encrypt&PLAINTEXT) == PLAINTEXT)) //明文
  324. {
  325. msg_para.Uart = UART4_ID+msg_para.Tax_no-1;
  326. tax_led_set(msg_para.Uart,SEND_LED_FLAG);//指示灯
  327. }
  328. else //密文或未读出状态
  329. {
  330. if((device_fixed_info.Encrypt&0x80) == 0x80) {
  331. msg_para.Uart = UART4_ID+msg_para.Tax_no-1;
  332. tax_send_info.uartid = UART4_ID+msg_para.Tax_no-1;//((msg_para.Tax_no == 0) ? UART3_ID : UART4_ID);
  333. tax_send_info.gun = msg_para.Gun_no - 1;
  334. tax_send_info.cmd = p_tax->info[3];
  335. //if(tax_send_info.cmd != 0x70)
  336. memcpy(tax_send_info.data,p_tax->info,p_tax->data_len);// 将当前的指令存储
  337. tax_send_info.dalen = p_tax->data_len;
  338. printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no);
  339. data_dump("收到指令 ", tax_send_info.data,tax_send_info.dalen);
  340. tax_send_start(GATE_COLL_TAX_QUERY); // 开始下发
  341. return;
  342. }
  343. else {
  344. msg_para.Uart = UART4_ID;
  345. //判断加密模块是否死机条件
  346. ciphertext_state.send_num++;
  347. if(ciphertext_state.send_num > 5)
  348. {
  349. ciphertext_time_start();
  350. }
  351. }
  352. }
  353. //打开串口超时
  354. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  355. if(p_tax->data_len > 128)//透传的数据长度太长
  356. {
  357. printf("uart send data too long\n");
  358. return;
  359. }
  360. uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len);
  361. printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no);
  362. data_dump_debug(MSG_TAX_DATA_TYPE1, "tax send", (uint8_t *)p_tax->info,p_tax->data_len);
  363. }
  364. else if(p_msg->first_type == GATE_COLL_XSP_QUERY) //查显示屏
  365. {
  366. if(msg_para.Devices_type != XSP_0904) //查询设备类型错误
  367. {
  368. //关闭串口超时
  369. timeout_stop(&lora_comm_info.uart_timeout);
  370. gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TYPE_ERR,NULL,0);
  371. msg_para_init();//清除存储的下发参数
  372. return;
  373. }
  374. msg_para.Uart = UART3_ID;
  375. //指示灯
  376. led_start(DISPLAY_SEND_LED);
  377. //打开串口超时
  378. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  379. if(p_tax->data_len > BUSINESS_DATA_LEN)//透传的数据长度太长
  380. {
  381. printf("uart send data too long\n");
  382. return;
  383. }
  384. // RS485_TX();
  385. uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len);
  386. // delay_us(120);//极限为210us,大于210us数据接收不完整。
  387. // RS485_RX();
  388. printf_debug(MSG_DISPLAY_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no);
  389. data_dump_debug(MSG_DISPLAY_DATA_TYPE1, "xsp send", (uint8_t *)p_tax->info,p_tax->data_len);
  390. }
  391. else if(p_msg->first_type == GATE_COLL_ENCODER) //查询编码器数据
  392. {
  393. order = p_tax->info[0];
  394. printf_debug(MSG_ENCODER_DOWN_TYPE2, "port:%d, gun:%d, order:%d\n",p_tax->tax_no,p_tax->gun_no, order);
  395. if(p_tax->device_type != ENCODER)//查询设备类型错误
  396. {
  397. gateway_down_business_return(COLL_GATE_ENCODER,UART_TYPE_ERR,NULL,0);
  398. msg_para_init();//清除存储的下发参数
  399. return;
  400. }
  401. if((msg_para.Tax_no != 1 && msg_para.Tax_no != 2))//报税口号错误
  402. {
  403. gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0);
  404. msg_para_init();//清除存储的下发参数
  405. return;
  406. }
  407. if(order > 3)//编码器指令错误
  408. {
  409. gateway_down_business_return(COLL_GATE_ENCODER,ORDER_ERR,NULL,0);
  410. msg_para_init();//清除存储的下发参数
  411. return;
  412. }
  413. if(order <= 1)//读单个枪
  414. {
  415. if(msg_para.Gun_no == 0 || msg_para.Gun_no > ENCODER_TAX_NUM*ENCODER_GUN_NUM)//报税口号或枪号错误
  416. {
  417. gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0);
  418. msg_para_init();//清除存储的下发参数
  419. return;
  420. }
  421. if(msg_para.Gun_no > ENCODER_GUN_NUM)//枪号大于一个编码口的最大枪数,报税口号只能为1
  422. {
  423. if(msg_para.Tax_no != 1)//报税口号或枪号错误
  424. {
  425. gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0);
  426. msg_para_init();//清除存储的下发参数
  427. return;
  428. }
  429. coll_encoder_return(1,msg_para.Gun_no-ENCODER_GUN_NUM,order);
  430. }
  431. else
  432. {
  433. coll_encoder_return(msg_para.Tax_no,msg_para.Gun_no,order);
  434. }
  435. }
  436. else if(order <= 3)//读单个报税口
  437. {
  438. coll_encoder_return(msg_para.Tax_no,msg_para.Gun_no,order);
  439. }
  440. msg_para_init();//清除存储的下发参数
  441. }
  442. }
  443. void tax_rcv_proc(uint8_t *data, uint16_t len)
  444. {
  445. tax_rcv_analyze(data,&tax_info,len);
  446. }
  447. //心跳包响应
  448. void coll_heartbeat_return(void)
  449. {
  450. collect_gateway_msg_format_t p_msg;
  451. coll_heartbeat_t *p_heart = (coll_heartbeat_t *)p_msg.info;
  452. p_heart->device_sn = device_info.device_sn;
  453. p_heart->coll_no = msg_para.Coll_no;
  454. p_heart->status = device_fixed_info.Encrypt;
  455. gateway_msg_return(&p_msg,COLL_GATE_HEARTBEAT,HEARTBEAT_HEADER_LEN);
  456. data_dump("heartbeat",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  457. }
  458. void tax_ciphertext_0x8C_process(void)
  459. {
  460. // uint8_t para[10],para_len;
  461. uint8_t send_buf[64];
  462. uint16_t send_len;
  463. uint8_t gunstatus;
  464. if(tax_send_info.start)
  465. {
  466. if(tax_send_info.send)
  467. {
  468. gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口
  469. if(gunstatus)
  470. {
  471. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  472. if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT)//读心跳
  473. {
  474. if(read_56_info.step==2)
  475. {
  476. coll_heartbeat_return();
  477. read_56_info.step = 0;
  478. msg_para_init();//清除存储的下发参数
  479. tax_send_end();
  480. return;
  481. }
  482. else
  483. {
  484. read_56_info.step = 2;
  485. tax_send_end();
  486. read_56_start(UART5_ID);
  487. return;
  488. }
  489. }
  490. else if(tax_send_info.startcmd == GATE_COLL_TAX_QUERY)
  491. {
  492. gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0);
  493. }
  494. msg_para_init();//清除存储的下发参数
  495. tax_send_end();
  496. return;
  497. }
  498. if(read_encoder_para.flag)//读编码器后2s内
  499. {
  500. // gunstatus = Gun_Signal_Check_all();
  501. if(gunstatus != read_encoder_para.gunstatus)
  502. {
  503. //至少有一把枪的抬枪状态发生了变化
  504. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  505. if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT)//读心跳
  506. {
  507. if(read_56_info.step==2)
  508. {
  509. coll_heartbeat_return();
  510. read_56_info.step = 0;
  511. msg_para_init();//清除存储的下发参数
  512. tax_send_end();
  513. return;
  514. }
  515. else
  516. {
  517. read_56_info.step = 2;
  518. tax_send_end();
  519. read_56_start(UART5_ID);
  520. return;
  521. }
  522. }
  523. else if(tax_send_info.startcmd == GATE_COLL_TAX_QUERY)
  524. {
  525. gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0);
  526. }
  527. msg_para_init();//清除存储的下发参数
  528. tax_send_end();
  529. return;
  530. }
  531. }
  532. tax_send_info.send = 0;
  533. switch(tax_send_info.step)
  534. {
  535. case CMD_72:
  536. send_len = tax_send_cmd_ciphertext(0x72,NULL,0,send_buf);
  537. uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len);
  538. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  539. tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯
  540. printf("send uartid:%d ",tax_send_info.uartid);
  541. data_dump("tax send",send_buf,send_len);
  542. break;
  543. case CMD_73:
  544. send_len = tax_send_cmd_ciphertext(0x73,tax_info.cmd_73_data,4,send_buf);
  545. uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len);
  546. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  547. tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯
  548. printf("send uartid:%d ",tax_send_info.uartid);
  549. data_dump("tax send",send_buf,send_len);
  550. break;
  551. case CMD_70:
  552. send_len = tax_send_cmd_ciphertext(0x70,NULL,0,send_buf);
  553. uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len);
  554. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  555. tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯
  556. printf("send uartid:%d ",tax_send_info.uartid);
  557. data_dump("tax send",send_buf,send_len);
  558. break;
  559. case CMD_8C:
  560. //send_len = tax_send_cmd_ciphertext(0x8C,tax_send_info.data,tax_send_info.dalen,send_buf);
  561. uart_msg_send(tax_send_info.uartid,(char *)tax_send_info.data,tax_send_info.dalen);
  562. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  563. tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯
  564. printf("send uartid:%d ",tax_send_info.uartid);
  565. data_dump("tax send",tax_send_info.data,tax_send_info.dalen);
  566. break;
  567. case CMD_86:
  568. send_len = tax_send_cmd(0x86,1,send_buf);
  569. uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len);
  570. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  571. tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯
  572. printf("send uartid:%d ",tax_send_info.uartid);
  573. data_dump("tax send",send_buf,send_len);
  574. break;
  575. default:
  576. tax_send_end();
  577. break;
  578. }
  579. }
  580. }
  581. }
  582. uart_fifo_t uart_fifo[5];
  583. //清除串口缓存数据
  584. void uart_fifo_clear(uint8_t uart_id)
  585. {
  586. if(uart_fifo[uart_id].flag)
  587. {
  588. uart_fifo[uart_id].flag = 0;
  589. uart_fifo[uart_id].len = 0;
  590. memset(uart_fifo[uart_id].data,0,256);
  591. }
  592. }
  593. uint8_t rcv_buf_uart3[256];
  594. uint16_t rcv_size_uart3;
  595. //业务串口接收处理
  596. void business_uart_rcv_proc(void)
  597. {
  598. uint8_t i,first_type;
  599. uint8_t tembuf[128] = {0};
  600. for(i = 0; i < 2; i++)
  601. {
  602. // rcv_size = uart_blocking_read(rcv_buf,UART3_ID+i);
  603. rcv_size = uart_blocking_read((char *)rcv_buf,UART4_ID+i,1);
  604. if(rcv_size)
  605. {
  606. rcv_size_temp = rcv_size;
  607. rcv_size = uart_blocking_read((char *)rcv_buf+rcv_size_temp, UART4_ID+i, 50);
  608. rcv_size+=rcv_size_temp;
  609. printf("UART:%d\n",UART4_ID+i+1);
  610. data_dump("uart rcv", rcv_buf, rcv_size);
  611. if(rcv_size > 128)
  612. {
  613. printf("uart rcv too long\n");
  614. return;
  615. }
  616. if(rcv_buf[2] != 0xFF)//帧号不为0xFF,
  617. {
  618. if(uart_fifo[UART4_ID+i].flag == 0)
  619. {
  620. uart_fifo[UART4_ID+i].len = 0;
  621. uart_fifo[UART4_ID+i].flag = 1;
  622. }
  623. if((uart_fifo[UART4_ID+i].len + rcv_size) > 128)
  624. {
  625. uart_fifo[UART4_ID+i].flag = 0;
  626. uart_fifo[UART4_ID+i].len = 0;
  627. return;
  628. }
  629. else{
  630. memcpy(uart_fifo[UART4_ID+i].data+uart_fifo[UART4_ID+i].len,rcv_buf,rcv_size);
  631. uart_fifo[UART4_ID+i].len += rcv_size;
  632. if(rcv_buf[2] != 0x01)
  633. return;
  634. }
  635. }
  636. if(uart_fifo[UART4_ID+i].flag)
  637. {
  638. uart_fifo[UART4_ID+i].flag = 0;
  639. memcpy(rcv_buf,uart_fifo[UART4_ID+i].data,uart_fifo[UART4_ID+i].len);
  640. rcv_size = uart_fifo[UART4_ID+i].len;
  641. uart_fifo_clear(UART4_ID+i);//清除串口缓存数据
  642. }
  643. printf("begin proc\n");
  644. if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3)
  645. {
  646. if(msg_rcv_info.uart == UART4_ID+i)
  647. {
  648. if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据
  649. {
  650. //关闭串口超时
  651. timeout_stop(&lora_comm_info.uart_timeout);
  652. //指示灯
  653. tax_led_set(UART4_ID+i,RCV_LED_FLAG);
  654. transparent_up_ver3_return(UART_SUCCESS,rcv_buf,rcv_size);
  655. msg_rcv_info_init();
  656. }
  657. }
  658. }
  659. else
  660. {
  661. printf("msg_para.uart = %d,msg_para.first_type:%d,tax_send_start:%d\n",msg_para.Uart, msg_para.First_type,tax_send_info.start);
  662. if(msg_para.Uart == UART4_ID+i)
  663. {
  664. //关闭串口超时
  665. timeout_stop(&lora_comm_info.uart_timeout);
  666. //指示灯
  667. tax_led_set(UART4_ID+i,RCV_LED_FLAG);
  668. first_type = COLL_GATE_TAX_REPLY;
  669. if(tax_send_info.start == 1){
  670. if( (tax_send_info.startcmd == GATE_COLL_TAX_QUERY) && (tax_send_info.step == CMD_8C)){ // 需要上报的指令
  671. memcpy(tembuf,tax_info.tax_serino,10);
  672. memcpy(&tembuf[10],rcv_buf,rcv_size);
  673. gateway_down_business_return(first_type,UART_SUCCESS,tembuf,rcv_size+10);
  674. tax_send_end();
  675. msg_para_init();//清除存储的下发参数
  676. }
  677. else if((tax_send_info.startcmd == GATE_COLL_TAX_QUERY) && (tax_send_info.step == CMD_70)){
  678. if(tax_send_info.cmd == 0x70){
  679. memcpy(&tembuf[10],rcv_buf,rcv_size);
  680. gateway_down_business_return(first_type,UART_SUCCESS,tembuf,rcv_size+10);
  681. tax_send_end();
  682. msg_para_init();//清除存储的下发参数
  683. }
  684. else tax_rcv_proc(rcv_buf,rcv_size); // 解析串口收到的72 73 79 指令
  685. }
  686. else if((tax_send_info.startcmd == GATE_COLL_HEARTBEAT) && ((tax_send_info.step == CMD_73) || (tax_send_info.step == CMD_86))) {
  687. tax_rcv_proc(rcv_buf,rcv_size);
  688. if(read_56_info.step == 2)
  689. {
  690. read_56_info.step = 0;
  691. tax_send_end();
  692. coll_heartbeat_return();
  693. msg_para_init();//清除存储的下发参数
  694. return;
  695. }
  696. else
  697. {
  698. read_56_info.step = 2;
  699. tax_send_end();
  700. read_56_start(UART5_ID);
  701. return;
  702. }
  703. // tax_send_end();
  704. // coll_heartbeat_return();
  705. // msg_para_init();//清除存储的下发参数
  706. // return;
  707. }
  708. else {
  709. tax_rcv_proc(rcv_buf,rcv_size); // 解析串口收到的72 73 79 指令
  710. }
  711. return;
  712. }
  713. if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令
  714. coll_transparent_return(msg_para.First_type,UART_SUCCESS,rcv_buf,rcv_size);
  715. else
  716. gateway_down_business_return(first_type,UART_SUCCESS,rcv_buf,rcv_size);
  717. msg_para_init();//清除存储的下发参数
  718. }
  719. }
  720. }
  721. }
  722. rcv_size_uart3 = uart_blocking_read((char *)rcv_buf_uart3,UART3_ID,1);
  723. if(rcv_size_uart3)
  724. {
  725. rcv_size_temp = rcv_size_uart3;
  726. rcv_size_uart3 = uart_blocking_read((char *)rcv_buf_uart3+rcv_size_uart3, UART3_ID, 50);
  727. rcv_size_uart3+=rcv_size_temp;
  728. // data_dump("uart3 rcv",rcv_buf,rcv_size);
  729. data_dump("uart3 rcv prot",rcv_buf_uart3,rcv_size_uart3);
  730. if(rcv_size_uart3 > BUSINESS_DATA_LEN)
  731. {
  732. printf("uart rcv too long\n");
  733. return;
  734. }
  735. if((device_fixed_info.Work_State == DEV_WORK_STATE_UPDATING) && (ota_state.update_mode == OTA_WORK_STATE_XSP) && (xsp_ota_state.state == OTA_RETRANSMIT))
  736. {
  737. //升级显示屏且重传阶段
  738. xsp_master_ota_proc(rcv_buf_uart3,rcv_size_uart3);
  739. return;
  740. }
  741. if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3)
  742. {
  743. if(msg_rcv_info.uart == UART_485)
  744. {
  745. //关闭串口超时
  746. timeout_stop(&lora_comm_info.uart_timeout);
  747. //指示灯
  748. led_start(DISPLAY_RCV_LED);
  749. if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据
  750. {
  751. transparent_up_ver3_return(UART_SUCCESS,rcv_buf_uart3,rcv_size_uart3);
  752. msg_rcv_info_init();
  753. }
  754. }
  755. }
  756. else if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_FF)//版本查询
  757. {
  758. //关闭串口超时
  759. timeout_stop(&lora_comm_info.uart_timeout);
  760. //指示灯
  761. led_start(DISPLAY_RCV_LED);
  762. //直接通过Lora返回
  763. lora_queue_insert(SYS_LORA_TX,(char *)rcv_buf_uart3, rcv_size_uart3);
  764. msg_rcv_info_init();
  765. }
  766. else
  767. {
  768. if(msg_para.Uart == UART_485)
  769. {
  770. //关闭串口超时
  771. timeout_stop(&lora_comm_info.uart_timeout);
  772. //指示灯
  773. led_start(DISPLAY_RCV_LED);
  774. first_type = COLL_GATE_XSP_REPLY;
  775. gateway_down_business_return(first_type,UART_SUCCESS,rcv_buf_uart3,rcv_size_uart3);
  776. msg_para_init();//清除存储的下发参数
  777. }
  778. }
  779. }
  780. }
  781. //73指令返回
  782. void coll_cmd73_return(uint8_t state)
  783. {
  784. collect_gateway_msg_format_t p_msg;
  785. cmd73_query_info_t *p_tax = (cmd73_query_info_t *)p_msg.info;
  786. p_tax->device_sn = device_info.device_sn;
  787. p_tax->coll_no = msg_para.Coll_no;
  788. p_tax->device_type = COLL_73;
  789. p_tax->tax_no = msg_para.Tax_no;
  790. p_tax->status = state;
  791. if(state != UART_SUCCESS)
  792. {
  793. p_tax->data_len = 0;
  794. }
  795. else
  796. {
  797. p_tax->data_len = cmd_73_info[p_tax->tax_no-1].len;
  798. memcpy(p_tax->info,cmd_73_info[p_tax->tax_no-1].data,cmd_73_info[p_tax->tax_no-1].len);
  799. cmd_73_info[p_tax->tax_no-1].read_flag = 0;
  800. }
  801. gateway_msg_return(&p_msg,COLL_GATE_CMD73,CMD73_QUERY_HEADER_LEN+p_tax->data_len);
  802. data_dump("cmd73",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN);
  803. }
  804. //网关下发查询73指令分析
  805. void gateway_down_cmd73_proc(collect_gateway_msg_format_t *p_msg)
  806. {
  807. cmd73_query_info_t *p_tax = (cmd73_query_info_t *)p_msg->info;
  808. //保存下发的参数
  809. msg_para.Msg_id = p_msg->msg_id;
  810. msg_para.Devices_type = p_tax->device_type;
  811. msg_para.Tax_no = p_tax->tax_no;
  812. if((p_tax->tax_no != 1) && (p_tax->tax_no != 2))
  813. {
  814. coll_cmd73_return(TAX_GUN_ERR);
  815. msg_para_init();//清除存储的下发参数
  816. return;
  817. }
  818. coll_cmd73_return(UART_SUCCESS);
  819. msg_para_init();//清除存储的下发参数
  820. }
  821. //查询固件信息响应
  822. void coll_hard_return(void)
  823. {
  824. collect_gateway_msg_format_t p_msg;
  825. coll_hard_reply_t *p_hard = (coll_hard_reply_t *)p_msg.info;
  826. p_hard->device_sn = device_info.device_sn;
  827. p_hard->coll_no = msg_para.Coll_no;
  828. p_hard->bootloader_vr = device_fixed_info.Soft_ver_boot;
  829. p_hard->app_vr = device_fixed_info.Soft_ver_app;
  830. p_hard->reset_num = device_fixed_info.Reset_total_cnt;
  831. p_hard->last_reset_type = device_fixed_info.Last_reset_flag;
  832. p_hard->run_times = TickCounter_u64/1000;
  833. p_hard->voltage = device_fixed_info.Voltage;
  834. p_hard->temperature = device_fixed_info.Temperature;
  835. p_hard->updata_state = updata_state;
  836. memcpy(p_hard->UUID,device_fixed_info.MCU_UUID,12);
  837. gateway_msg_return(&p_msg,COLL_GATE_HARD_REPLY,HARD_HEADER_LEN);
  838. }
  839. //网关下发设置Lora组号处理函数
  840. void gateway_coll_lorapara_proc(collect_gateway_msg_format_t *p_msg)
  841. {
  842. uint8_t eeprom_buf[2] = {0};
  843. gateway_lora_para_t *p_lora = (gateway_lora_para_t *)p_msg->info;
  844. printf("coll_lora_group:%d, gate_lora_group:%d\n",lora_para_choice,p_lora->lora_group);
  845. if(p_lora->lora_group < LORA_GROUP_NUM) //lora组号索引正确
  846. {
  847. if(lora_para_choice != p_lora->lora_group)//切换组号
  848. {
  849. lora_para_choice = p_lora->lora_group;
  850. lora_init();
  851. //写eeprom
  852. eeprom_buf[0] = lora_para_choice;
  853. eeprom_buf[1] = ~eeprom_buf[0];
  854. AT24CXX_Write(LORA_ADDR,(uint8_t *)eeprom_buf,2);
  855. printf_debug(MSG_LORA_DOWN_TYPE2, "lora choice:%d\n",lora_para_choice);
  856. }
  857. }
  858. }
  859. //时间同步处理函数
  860. void gateway_coll_timesync_proc(collect_gateway_msg_format_t *p_msg)
  861. {
  862. time_sync_t *p_time = (time_sync_t *)p_msg->info;
  863. RTC_Set_Stamp(p_time->time_stamp);
  864. }
  865. //编码器返回函数
  866. void coll_encoder_return(uint8_t tax, uint8_t gun, uint8_t order)
  867. {
  868. uint8_t data[128]= {0}, len = 0;
  869. //读编码器后计时参数
  870. timeout_stop(&read_encoder_para.tt_encoder);
  871. read_encoder_para.flag = 0;
  872. if(order == 0 || order == 1)
  873. {
  874. len = coll_encoder_return_0_1(tax,gun,order,data);
  875. }
  876. else if(order == 2 || order == 3)
  877. {
  878. len = coll_encoder_return_2_3(tax,gun,order,data);
  879. }
  880. gateway_down_business_return(COLL_GATE_ENCODER,UART_SUCCESS,data,len);
  881. }
  882. //根据设备类型复位分析
  883. void gateway_coll_reboot_type_proc(collect_gateway_msg_format_t *p_msg)
  884. {
  885. reboot_type_t *p_reb = (reboot_type_t *)p_msg->info;
  886. switch(p_reb->cmd)
  887. {
  888. case 0x01://设备复位
  889. if(p_reb->device_type == 0x0201)//采集器复位
  890. {
  891. printf("cpu reset\n");
  892. NVIC_SystemReset();
  893. }
  894. else if(p_reb->device_type == 0x0601)//加密模块
  895. {
  896. if(device_fixed_info.Encrypt == CIPHERTEXT)//密文
  897. {
  898. //复位加密芯片
  899. port_init(PORT_LED_OFF); //初始化加密模块
  900. printf("ciphertext reset\n");
  901. }
  902. }
  903. break;
  904. case 0x02://透传口复位
  905. if(p_reb->device_type == 0x0201)//采集器
  906. {
  907. tax_sw_reset(3);
  908. printf("pos reset\n");
  909. }
  910. break;
  911. }
  912. }
  913. //透传指令处理
  914. void gateway_coll_transparent_proc(collect_gateway_msg_format_t *p_msg)
  915. {
  916. uint8_t gunstatus;
  917. transparent_info_t *p_tax = (transparent_info_t *)p_msg->info;
  918. //保存下发的参数
  919. msg_para.Msg_id = p_msg->msg_id;
  920. msg_para.First_type = p_msg->first_type;
  921. msg_para.Second_type = p_msg->second_type;
  922. msg_para.Tax_no = p_tax->tax_no;
  923. msg_para.Gw_sn = p_tax->gw_sn;
  924. if((p_tax->tax_no != 1) && (p_tax->tax_no != 2))
  925. {
  926. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  927. coll_transparent_return(msg_para.First_type,TAX_GUN_ERR,NULL,0);
  928. msg_para_init();//清除存储的下发参数
  929. return;
  930. }
  931. gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口
  932. if(gunstatus)
  933. {
  934. timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时
  935. coll_transparent_return(msg_para.First_type,TAX_BUSY,NULL,0);
  936. msg_para_init();//清除存储的下发参数
  937. return;
  938. }
  939. //报税口发送指示灯
  940. if(msg_para.Tax_no == 1)
  941. {
  942. led_start(TAX1_SEND_LED);
  943. }
  944. else if(msg_para.Tax_no == 2)
  945. {
  946. led_start(TAX2_SEND_LED);
  947. }
  948. //报税口对应的串口号
  949. if(device_fixed_info.Encrypt == PLAINTEXT) //明文
  950. {
  951. if(msg_para.Tax_no == 1)
  952. {
  953. msg_para.Uart = UART4_ID;
  954. }
  955. else if(msg_para.Tax_no == 2)
  956. {
  957. msg_para.Uart = UART5_ID;
  958. }
  959. }
  960. else if(device_fixed_info.Encrypt == CIPHERTEXT) //密文
  961. {
  962. msg_para.Uart = UART2_ID;
  963. }
  964. //打开串口超时
  965. timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1);
  966. if(p_tax->data_len > 128)//透传的数据长度太长
  967. {
  968. printf("uart send data too long\n");
  969. return;
  970. }
  971. uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len);
  972. printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d\n",msg_para.Uart+1,msg_para.Tax_no);
  973. data_dump_debug(MSG_TAX_DATA_TYPE1, "tax send", (uint8_t *)p_tax->info,p_tax->data_len);
  974. }
  975. //采集器透传返回
  976. void coll_transparent_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len)
  977. {
  978. collect_gateway_msg_format_t p_msg;
  979. transparent_info_t *p_tax = (transparent_info_t *)p_msg.info;
  980. p_tax->gw_sn = msg_para.Gw_sn;
  981. p_tax->coll_sn = device_info.device_sn;
  982. p_tax->tax_no = msg_para.Tax_no;
  983. p_tax->status = state;
  984. if(state == UART_SUCCESS)
  985. {
  986. p_tax->data_len = len;
  987. memcpy(p_tax->info,data,len);
  988. }
  989. else
  990. {
  991. p_tax->data_len = 0;
  992. }
  993. gateway_msg_return(&p_msg,first_type,p_tax->data_len+TRANSPARENT_HEADER_LEN);
  994. }
  995. //读56初始化
  996. void read_56_init(void)
  997. {
  998. memset(&read_56_info,0,sizeof(read_56_info_t));
  999. }
  1000. //读56开始
  1001. void read_56_start(uint8_t uart_id)
  1002. {
  1003. tax_send_info.uartid = uart_id;
  1004. msg_para.Uart = uart_id;
  1005. tax_send_start(GATE_COLL_HEARTBEAT);
  1006. if(uart_id == UART3_ID)
  1007. {
  1008. if(read_56_info.read_way_tax1==0)//读86
  1009. {
  1010. tax_send_info.step = CMD_86;
  1011. }
  1012. read_56_info.read_way_tax1 = !read_56_info.read_way_tax1;
  1013. }
  1014. else if(uart_id == UART4_ID)
  1015. {
  1016. if(read_56_info.read_way_tax2==0)//读86
  1017. {
  1018. tax_send_info.step = CMD_86;
  1019. }
  1020. read_56_info.read_way_tax2 = !read_56_info.read_way_tax2;
  1021. }
  1022. // if(read_56_info.read_way)//读86
  1023. // {
  1024. // tax_send_info.step = CMD_86;
  1025. // }
  1026. }
  1027. //处理网关下发的控制命令
  1028. void gateway_coll_ctrl_comm(collect_gateway_msg_format_t *p_msg)
  1029. {
  1030. OS_ERR err;
  1031. uint8_t tax1=0,tax2=0,tax_num=0;
  1032. device_sn_info_t *p_sn = (device_sn_info_t *)p_msg->info;
  1033. switch (p_msg->first_type)
  1034. {
  1035. case GATE_COLL_CMD73://网关下发查询73指令
  1036. if(p_sn->device_sn == device_info.device_sn)
  1037. {
  1038. msg_para.Coll_no = p_sn->coll_no;
  1039. printf("device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1040. printf("type:%x\n",p_msg->first_type);
  1041. //lora接收指示灯
  1042. led_start(LORA_RCV_LED);
  1043. gateway_down_cmd73_proc(p_msg);
  1044. }
  1045. break;
  1046. case GATE_COLL_XSP_QUERY://网关下发查询显示屏数据
  1047. if(p_sn->device_sn == device_info.device_sn)
  1048. {
  1049. msg_para.Coll_no = p_sn->coll_no;
  1050. printf_debug(MSG_DISPLAY_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1051. printf_debug(MSG_DISPLAY_DOWN_TYPE1,"type:%x\n",p_msg->first_type);
  1052. //lora接收指示灯
  1053. led_start(LORA_RCV_LED);
  1054. gateway_down_business_proc(p_msg);
  1055. }
  1056. break;
  1057. case GATE_COLL_DISPLAY_QUERY://网关下发查询显示屏数据
  1058. if(p_sn->device_sn == device_info.device_sn)
  1059. {
  1060. msg_para.Coll_no = p_sn->coll_no;
  1061. printf_debug(MSG_DISPLAY_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1062. printf_debug(MSG_DISPLAY_DOWN_TYPE1,"type:%x\n",p_msg->first_type);
  1063. //lora接收指示灯
  1064. led_start(LORA_RCV_LED);
  1065. gateway_down_business_proc(p_msg);
  1066. }
  1067. break;
  1068. case GATE_COLL_HEARTBEAT://网关与采集器的下行心跳包
  1069. if(p_sn->device_sn == device_info.device_sn)
  1070. {
  1071. msg_para.Coll_no = p_sn->coll_no;
  1072. msg_para.Msg_id = p_msg->msg_id;
  1073. printf("device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1074. printf("type:%x\n",p_msg->first_type);
  1075. // data_dump("56 rcv",(uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN);
  1076. //lora接收指示灯
  1077. led_start(LORA_RCV_LED);
  1078. if(p_msg->msg_len > 7)//新协议
  1079. {
  1080. printf("tax index:%d\n",p_msg->info[5]);
  1081. if(p_msg->info[5] == 0x01)//报税口1
  1082. {
  1083. if((device_fixed_info.Encrypt&0x03) == 0x00)
  1084. {
  1085. read_56_info.step = 2;
  1086. read_56_start(UART4_ID);
  1087. }
  1088. else
  1089. {
  1090. delay_ms(20);
  1091. coll_heartbeat_return();
  1092. }
  1093. }
  1094. if(p_msg->info[5] == 0x02)//报税口2
  1095. {
  1096. if((device_fixed_info.Encrypt&0x0C) == 0x00)
  1097. {
  1098. read_56_info.step = 2;
  1099. read_56_start(UART5_ID);
  1100. }
  1101. else
  1102. {
  1103. delay_ms(20);
  1104. coll_heartbeat_return();
  1105. }
  1106. }
  1107. else if(p_msg->info[5] == 0x03)//报税口12
  1108. {
  1109. if((device_fixed_info.Encrypt&0x03) != 0x00 && (device_fixed_info.Encrypt&0x0C) != 0x00)//报税口12
  1110. {
  1111. delay_ms(20);
  1112. coll_heartbeat_return();
  1113. }
  1114. else
  1115. {
  1116. if((device_fixed_info.Encrypt&0x03) == 0x00)//
  1117. {
  1118. tax1 = 1;
  1119. tax_num++;
  1120. }
  1121. if((device_fixed_info.Encrypt&0x0C) == 0x00)//
  1122. {
  1123. tax2 = 1;
  1124. tax_num++;
  1125. }
  1126. if(tax_num==1)
  1127. {
  1128. if(tax1)
  1129. {
  1130. read_56_start(UART4_ID);
  1131. }
  1132. else if(tax2)
  1133. {
  1134. read_56_start(UART5_ID);
  1135. }
  1136. read_56_info.step = 2;
  1137. }
  1138. else if(tax_num==2)
  1139. {
  1140. read_56_start(UART4_ID);
  1141. read_56_info.step = 1;
  1142. }
  1143. }
  1144. }
  1145. }
  1146. else//老协议,只读报税口1
  1147. {
  1148. if((device_fixed_info.Encrypt&0x03) == 0x00)
  1149. {
  1150. read_56_info.step = 2;
  1151. read_56_start(UART4_ID);
  1152. }
  1153. else
  1154. {
  1155. delay_ms(20);
  1156. coll_heartbeat_return();
  1157. }
  1158. }
  1159. }
  1160. break;
  1161. case GATE_COLL_TAX_QUERY: //网关下发查询采集器数据(报税口)
  1162. if(p_sn->device_sn == device_info.device_sn)
  1163. {
  1164. msg_para.Coll_no = p_sn->coll_no;
  1165. printf_debug(MSG_TAX_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1166. printf_debug(MSG_TAX_DOWN_TYPE1,"type:%x\n",p_msg->first_type);
  1167. //lora接收指示灯
  1168. led_start(LORA_RCV_LED);
  1169. gateway_down_business_proc(p_msg);
  1170. }
  1171. break;
  1172. case GATE_COLL_HARD_QUERY: //网关下发查询固件信息
  1173. if(p_sn->device_sn == device_info.device_sn)
  1174. {
  1175. msg_para.Coll_no = p_sn->coll_no;
  1176. msg_para.Msg_id = p_msg->msg_id;
  1177. printf_debug(MSG_HART_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1178. printf_debug(MSG_HART_DOWN_TYPE1, "type:%x\n",p_msg->first_type);
  1179. //lora接收指示灯
  1180. led_start(LORA_RCV_LED);
  1181. OSTimeDlyHMSM(0, 0, 0, 20, OS_OPT_TIME_DLY, &err);
  1182. coll_hard_return();
  1183. }
  1184. break;
  1185. case GATE_COLL_REBOOT: //采集器设备重新启动
  1186. if(p_sn->device_sn == device_info.device_sn || p_sn->device_sn == 0xffffffff)
  1187. {
  1188. msg_para.Coll_no = p_sn->coll_no;
  1189. msg_para.Msg_id = p_msg->msg_id;
  1190. printf_debug(MSG_REBOOT_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1191. printf_debug(MSG_REBOOT_DOWN_TYPE1, "type:%x\n",p_msg->first_type);
  1192. //lora接收指示灯
  1193. led_start(LORA_RCV_LED);
  1194. NVIC_SystemReset();
  1195. }
  1196. break;
  1197. case GATE_COLL_CTRL_LORAPARA://网关下发切换lora参数
  1198. if(p_sn->device_sn == device_info.device_sn)
  1199. {
  1200. msg_para.Coll_no = p_sn->coll_no;
  1201. msg_para.Msg_id = p_msg->msg_id;
  1202. printf_debug(MSG_LORA_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1203. printf_debug(MSG_LORA_DOWN_TYPE1, "type:%x\n",p_msg->first_type);
  1204. //lora接收指示灯
  1205. led_start(LORA_RCV_LED);
  1206. gateway_coll_lorapara_proc(p_msg);
  1207. }
  1208. break;
  1209. case GATE_COLL_TIMESYNC: //时间同步
  1210. if(p_sn->device_sn == device_info.device_sn)
  1211. {
  1212. msg_para.Coll_no = p_sn->coll_no;
  1213. msg_para.Msg_id = p_msg->msg_id;
  1214. printf_debug(MSG_LORA_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1215. printf_debug(MSG_LORA_DOWN_TYPE1, "type:%x\n",p_msg->first_type);
  1216. //lora接收指示灯
  1217. led_start(LORA_RCV_LED);
  1218. gateway_coll_timesync_proc(p_msg);
  1219. }
  1220. break;
  1221. case GATE_COLL_ENCODER: //查询编码器数据
  1222. if(hd_id == PCB_V1)
  1223. {
  1224. if(p_sn->device_sn == device_info.device_sn)
  1225. {
  1226. msg_para.Coll_no = p_sn->coll_no;
  1227. printf_debug(MSG_ENCODER_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1228. printf_debug(MSG_ENCODER_DOWN_TYPE1, "type:%x\n",p_msg->first_type);
  1229. //lora接收指示灯
  1230. led_start(LORA_RCV_LED);
  1231. gateway_down_business_proc(p_msg);
  1232. }
  1233. }
  1234. break;
  1235. case GATE_COLL_REBOOT_TYPE: //根据设备类型重启
  1236. if(p_sn->device_sn == device_info.device_sn)
  1237. {
  1238. msg_para.Coll_no = p_sn->coll_no;
  1239. msg_para.Msg_id = p_msg->msg_id;
  1240. printf_debug(MSG_REBOOT_TYPE_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn);
  1241. printf_debug(MSG_REBOOT_TYPE_DOWN_TYPE1, "type:%04x\n",p_msg->first_type);
  1242. //lora接收指示灯
  1243. led_start(LORA_RCV_LED);
  1244. gateway_coll_reboot_type_proc(p_msg);
  1245. }
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. }