usr_b2pc_msg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * usr_b2pc_msg.c
  3. * pc同主机通信
  4. * Created on: 2023年9月18日
  5. * Author: longtengjun
  6. */
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "onechip_include_header.h"
  11. char DeviceNum[64] = "mini7 2312230001 v1.1, ";
  12. extern uint16_t WriteComm0(uint8_t *data, uint16_t len);
  13. uint8_t pc_channel_recv = 0; //接收pc的通道,0:未连接; 1:wifi; 2:串口
  14. uint8_t pc_channel_send = 0; //发给pc的通道,0:未连接; 1:wifi; 2:串口
  15. static uint8_t pc_channel_send_backup = 0; //发给pc的通道备份
  16. // extern float slave_temp;
  17. // extern uint8_t other_run_state;
  18. // str_error_info_t error_info = {0}; //错误信息
  19. uint8_t pc_send_wait = 0; //数据发送等待,0可以立即发送;>0正在发送请等待
  20. uint8_t pc_send_cmd = 0; //当前发送cmd
  21. // extern str_mcu_ack mcu_ack_ctrl;
  22. // extern uint32_t timeout_com;
  23. // extern uint32_t pc_link_timeout;
  24. // extern struct light_band_para_t light_band_para[2];
  25. // extern ProcessStateMachine_t psm;
  26. // extern CFG_PARAM_STRUCT g_cfg;
  27. // #ifdef ONECHIP_MINI_PLUS
  28. // extern CFG_PARAM_STRUCT g_cfg_slave;
  29. // #endif
  30. // extern float evnTemp;
  31. // extern uint32_t connectTcpFlag;
  32. // extern uint32_t broadDevFlag;
  33. // extern uint16_t scan_qrcode_ropprt;
  34. // extern uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
  35. // extern uint16_t wifi_tx_len;
  36. static unsigned char seq =0;
  37. //字节流转整数,大端模式
  38. uint32_t byte_to_u32(uint8_t *p, int len)
  39. {
  40. uint32_t temp = 0;
  41. for(int i = len-1; i >= 0; i--)
  42. {
  43. temp <<= 8;
  44. temp |= p[i];
  45. }
  46. return temp;
  47. }
  48. uint32_t PackDeviceNum(void){
  49. int len = 0;
  50. memset((uint8_t *)&DeviceNum, 0 ,sizeof(DeviceNum));
  51. #ifdef ONECHIP_MINI_ONE
  52. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7-one,V%d.%d.%d.%d",VERSION_1,VERSION_2,VERSION_3,VERSION_4);
  53. #else
  54. #ifdef ONECHIP_MINI_PLUS
  55. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7-plus,V%d.%d.%d.%d",VERSION_1,VERSION_2,VERSION_3,VERSION_4);
  56. #else
  57. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7,V%d.%d.%d.%d",VERSION_1,VERSION_2,VERSION_3,VERSION_4);
  58. #endif //ONECHIP_MINI_PLUS
  59. #endif //ONECHIP_MINI_ONE
  60. strcat(DeviceNum, ",");
  61. len = strlen(DeviceNum);
  62. hex_to_ascii(get_uid(), &DeviceNum[len], 12); //uid
  63. strcat(DeviceNum, ",");
  64. strcat(DeviceNum, g_cfg.SN);
  65. strcat(DeviceNum, " ");
  66. LOG("DeviceNum:%s\n", DeviceNum);
  67. }
  68. /********************************************************************
  69. * uint16_t b2b_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  70. *
  71. *描述:串口发送
  72. *参数:Devicenum 0-发至主机,1发至从机
  73. *返回:无
  74. *其他:无
  75. *--------------------------------------------------------------------
  76. *记录:
  77. ********************************************************************/
  78. uint16_t b2b_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  79. {
  80. uint16_t check;
  81. CmdMessage msg;
  82. msg.head = 0x55aa;
  83. msg.seq = cmd_frame_type_req;
  84. msg.cmd = cmd;
  85. msg.tail = 0x5a;
  86. msg.len = len;
  87. check = checkSum(&msg.seq, 4);
  88. check += checkSum(data, len);
  89. msg.checksum = check & 0xFF;
  90. msg.tail = check >> 8;
  91. WriteComm0((uint8_t*)&msg, g_uMsgCmdFrameLen);
  92. msg.data = data;
  93. WriteComm0(data, len);
  94. return WriteComm0(&msg.checksum, 2);
  95. }
  96. /********************************************************************
  97. * uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  98. *
  99. *描述:发生数据到PC
  100. *参数:Devicenum 0-发至主机,1发至从机
  101. *返回:无
  102. *其他:无
  103. *--------------------------------------------------------------------
  104. *记录:
  105. ********************************************************************/
  106. uint16_t union_data_send_to_pc(struct CmdMessage *pMsg)
  107. {
  108. uint16_t check;
  109. uint16_t try = 0;
  110. struct CmdMessage msg;
  111. int ret = 0;
  112. msg.head = 0x55aa;
  113. msg.seq = pMsg->seq;
  114. msg.cmd = pMsg->cmd;
  115. msg.tail = 0x5a;
  116. msg.len = pMsg->len;
  117. msg.data = pMsg->data;
  118. check = checkSum(&msg.seq, 4);
  119. *(unsigned int*)msg.data = Devicenum;/*打包模块编号*/
  120. check += checkSum(msg.data, msg.len);
  121. msg.checksum = check & 0xFF;
  122. msg.tail = (check >> 8);
  123. if(Devicenum)
  124. {
  125. /*从模块 通过IIC上报,稍后跟新*/
  126. }
  127. else
  128. {
  129. WriteComm0((uint8_t*)&msg, g_uMsgCmdFrameLen);
  130. WriteComm0(msg.data, msg.len);
  131. return WriteComm0(&msg.checksum, 2);
  132. }
  133. }
  134. /********************************************************************
  135. * uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  136. *
  137. *描述:发生数据到PC
  138. *参数:Devicenum 0-发至主机,1发至从机
  139. *返回:无
  140. *其他:无
  141. *--------------------------------------------------------------------
  142. *记录:
  143. ********************************************************************/
  144. uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  145. {
  146. uint16_t check;
  147. struct CmdMessage msg;
  148. int ret = 0;
  149. msg.head = 0x55aa;
  150. msg.seq = cmd_frame_type_req;
  151. msg.cmd = cmd;
  152. msg.tail = 0x5a;
  153. msg.len = len;
  154. msg.data = data;
  155. if((len+8) > WIFI_TX_BUF_SIZE){
  156. LOG_E("send_to_pc :%x len:%d error ", cmd, len);
  157. return -1;
  158. }
  159. if(pc_channel_send == 1 && !connectTcpFlag && cmd != PC_CMD_SETIP){
  160. /*UDP 模式,不响应设置IP以为指令*/
  161. return -1;
  162. }
  163. if((pc_channel_send == 1 && PC_CMD_DATASCOPE == cmd)|| /*网口不上报数据*/
  164. (pc_channel_send != 1 && PC_CMD_SETIP == cmd)) /*串口不广播UDP*/{
  165. return -1;
  166. }
  167. switch(cmd)
  168. {
  169. case PC_CMD_QRCODE:
  170. case PC_CMD_DATA_UPLOAD:
  171. case PC_CMD_REPORT_RESULT:
  172. seq++;
  173. if(0xff == seq){
  174. seq =1;
  175. }
  176. msg.seq = seq;
  177. break;
  178. default: //应答
  179. break;
  180. }
  181. check = checkSum(&msg.seq, 4);
  182. check += checkSum(data, len);
  183. msg.checksum = check & 0xFF;
  184. msg.tail = (check >> 8);
  185. if(check_to_pc_busy()){
  186. memcpy(&wifi_tx_buf[0], (uint8_t*)&msg, g_uMsgCmdFrameLen);
  187. memcpy(&wifi_tx_buf[g_uMsgCmdFrameLen], data, len);
  188. memcpy(&wifi_tx_buf[g_uMsgCmdFrameLen+len], (uint8_t*)&check, 2);
  189. wifi_tx_len = g_uMsgCmdFrameLen+len+2;
  190. }else{
  191. return -1;
  192. }
  193. if((pc_channel_send == 1 || cmd == PC_CMD_SETIP ) && PC_CMD_SETWIFI != cmd)
  194. {
  195. return WIFI_ReSend();
  196. }
  197. else
  198. {
  199. WriteComm0(wifi_tx_buf, wifi_tx_len);
  200. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  201. }
  202. }
  203. unsigned int check_to_pc_busy(void){
  204. /*判断是否可以发送,缓冲区为空或者索引为0*/
  205. struct CmdMessage *pMessage = (struct CmdMessage *)wifi_tx_buf;
  206. if(pMessage->seq == 0 || pMessage->len == 0){
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. ///********************************************************************
  212. // * void data_resend_to_pc(void)
  213. // *
  214. // *描述:判断缓冲区内容,是否需要重发
  215. // *参数:无
  216. // *返回:无
  217. // *其他:无
  218. // *--------------------------------------------------------------------
  219. // *记录:
  220. // ********************************************************************/
  221. void pc_resend_del(uint8_t seq){
  222. struct CmdMessage *pMessage = (struct CmdMessage *)wifi_tx_buf;
  223. if(pMessage->seq == seq){
  224. wifi_tx_len = 0;
  225. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  226. }
  227. }
  228. /********************************************************************
  229. * uint16_t send_Devinf_pc(void)
  230. *
  231. *描述:发送设备信息到主机,便于主机下法TCP信息
  232. *参数:无
  233. *返回:无
  234. *其他:无
  235. *--------------------------------------------------------------------
  236. *记录:
  237. ********************************************************************/
  238. void send_Devinf_pc(void)
  239. {
  240. uint16_t len = 0;
  241. len = strlen(DeviceNum);
  242. data_send_to_pc(PC_CMD_SETIP,DeviceNum,len);
  243. }
  244. #ifdef ONECHIP_MINI_ONE
  245. int send_temp_parm(uint8_t cmd, uint8_t *parg)
  246. {
  247. b2b_send_device(B2B_CMD_THEROM_GOAL_ONCE, parg, 16, !Devicenum);
  248. return 1;
  249. }
  250. int setTemp_response(struct CmdMessage *pMsg, uint8_t result)
  251. {
  252. // char result = 1;
  253. data_send_to_pc(PC_CMD_SETRUNTEMP, &result, 1);
  254. }
  255. #endif
  256. void set_pc_chanel_recv(uint8_t channel)
  257. {
  258. pc_channel_recv = channel;
  259. }
  260. void set_pc_chanel_temp()
  261. {
  262. if(pc_channel_recv == 2)
  263. timeout_com = 0;
  264. pc_channel_send_backup = pc_channel_send;
  265. pc_channel_send = pc_channel_recv;
  266. }
  267. void reload_pc_chanel()
  268. {
  269. if(pc_channel_recv > pc_channel_send_backup) //串口优先
  270. {
  271. pc_channel_send_backup = pc_channel_recv;
  272. }
  273. pc_channel_send = pc_channel_send_backup;
  274. }
  275. void set_pc_chanel(uint8_t channel)
  276. {
  277. pc_channel_send_backup = channel;
  278. pc_channel_send = channel;
  279. }
  280. /********************************************************************
  281. * int handshake_action(uint8_t cmd, uint8_t *parg)
  282. *
  283. *描述:握手命令处理
  284. *参数:无
  285. *返回:无
  286. *其他:无
  287. *--------------------------------------------------------------------
  288. *记录:
  289. ********************************************************************/
  290. int handshake_action(uint8_t cmd, uint8_t *parg)
  291. {
  292. if(state_controlword < Hand_Shake_cmd) //上位机会间隔发送握手,保活
  293. {
  294. state_controlword = Hand_Shake_cmd;
  295. }
  296. return 0;
  297. }
  298. void char_replace(uint8_t *str, int size, uint8_t ch, uint8_t replace)
  299. {
  300. for(int i = 0; i < size; i++)
  301. {
  302. if(str[i] == ch)
  303. str[i] = replace;
  304. }
  305. }
  306. /********************************************************************
  307. * int wifi_action(uint8_t cmd, uint8_t *parg)
  308. *
  309. *描述:设置wifi命令处理
  310. *参数:无
  311. *返回:无
  312. *其他:无
  313. *--------------------------------------------------------------------
  314. *记录:
  315. ********************************************************************/
  316. int wifi_action(uint8_t cmd, uint8_t *parg)
  317. {
  318. // char_replace(parg, 64, 0xAA, 0);
  319. // char_replace(parg, 64, 0xAD, ' ');
  320. wifiCfg_set_new_wifi_param(parg, parg + WIFI_AP_NAME_MAX_LEN);
  321. // wifi用户名和密码设置
  322. WIFI_set_ssid();
  323. return 1;
  324. }
  325. int setState_action(uint8_t cmd, uint8_t *parg)
  326. {
  327. uint8_t block = *parg++;
  328. uint8_t nextState = *parg;
  329. if(block==0){
  330. if(SYS_STATE_WORKEND == nextState){
  331. psm.state = SW_move_card;
  332. }
  333. }else{
  334. /*下发命令,改变从模块状态*/
  335. b2b_send_device(B2B_CMD_STATE_SET, &nextState, sizeof(nextState), 1);
  336. }
  337. return 1;
  338. }
  339. /********************************************************************
  340. * int wifi_action(uint8_t cmd, uint8_t *parg)
  341. *
  342. *描述:设置wifi命令处理
  343. *参数:无
  344. *返回:无
  345. *其他:无
  346. *--------------------------------------------------------------------
  347. *记录:
  348. ********************************************************************/
  349. int SN_action(uint8_t cmd, uint8_t *parg)
  350. {
  351. parg[15] = 0;
  352. strcpy(g_cfg.SN, parg);
  353. PackDeviceNum();
  354. return cfg_save_cfg_param();
  355. }
  356. /********************************************************************
  357. * int seq_selftest_action(uint8_t cmd, uint8_t *parg)
  358. *
  359. *描述:设备自检处理
  360. *参数:无
  361. *返回:无
  362. *其他:无
  363. *--------------------------------------------------------------------
  364. *记录:
  365. ********************************************************************/
  366. int seq_selftest_action(uint8_t cmd, uint8_t *parg)
  367. {
  368. return 0;
  369. }
  370. /********************************************************************
  371. * int confirm_result_action(uint8_t cmd, uint8_t *parg)
  372. *
  373. *描述:设备收到pc的结果响应
  374. *参数:无
  375. *返回:无
  376. *其他:无
  377. *--------------------------------------------------------------------
  378. *记录:
  379. ********************************************************************/
  380. int confirm_result_action(uint8_t cmd, uint8_t *parg)
  381. {
  382. if(Devicenum)
  383. {
  384. return 0;
  385. }
  386. if(0 == memcmp(parg,card_param_info.pid, 10 ))
  387. state_controlword = result_move_card;
  388. return 0;
  389. }
  390. /********************************************************************
  391. * int default_pc_response(struct CmdMessage *pMsg, uint8_t result)
  392. *
  393. *描述:默认回应PC
  394. *参数:无
  395. *返回:成功->0 失败->-1
  396. *其他:无
  397. *--------------------------------------------------------------------
  398. *记录:
  399. ********************************************************************/
  400. int default_pc_response(struct CmdMessage *pMsg, uint8_t result)
  401. {
  402. if(Devicenum)
  403. {
  404. return 0;
  405. }
  406. return data_send_to_pc(pMsg->cmd, &result, 1);
  407. }
  408. /********************************************************************
  409. * int handshake_response(struct CmdMessage *pMsg, uint8_t result)
  410. *
  411. *描述:握手回应
  412. *参数:无
  413. *返回:无
  414. *其他:无
  415. *--------------------------------------------------------------------
  416. *记录:
  417. ********************************************************************/
  418. int handshake_response(struct CmdMessage *pMsg, uint8_t result)
  419. {
  420. int len = 0;
  421. // PackDeviceNum();
  422. len = strlen(DeviceNum);
  423. return data_send_to_pc(pMsg->cmd, (uint8_t*)DeviceNum, len);
  424. }
  425. int AskState_response(struct CmdMessage *pMsg, uint8_t result)
  426. {
  427. ANS_State();
  428. }
  429. int set_response(struct CmdMessage *pMsg, uint8_t result)
  430. {
  431. // char wifi_result = 1;
  432. // data_send_to_pc(PC_CMD_SETWIFI, &wifi_result, 1);
  433. }
  434. int setState_response(struct CmdMessage *pMsg, uint8_t result)
  435. {
  436. // char result = 1;
  437. data_send_to_pc(PC_CMD_SETSTATE, &result, 1);
  438. }
  439. int ans_response(struct CmdMessage *pMsg, uint8_t result)
  440. {
  441. /*清空需要上位机应答的消息*/
  442. switch(pMsg->cmd){
  443. case PC_CMD_REPORT_REEOR:
  444. // unsigned char *data = pMsg->data;
  445. // if(*data == Devicenum){
  446. // }
  447. clean_error_update();
  448. break;
  449. case PC_CMD_UPDATESTATE:
  450. clean_state_update();
  451. break;
  452. case PC_CMD_QRCODE:
  453. clean_QR_update();
  454. break;
  455. case PC_CMD_DATA_UPLOAD:{
  456. unsigned char *data = pMsg->data;
  457. unsigned int recdex = 0xfffffff;
  458. // unsigned char result = *data;
  459. // unsigned char part = *(data+1);
  460. if( pMsg->len == 6){
  461. recdex = *(data+5);
  462. recdex = recdex<<8;
  463. recdex += *(data+4);
  464. recdex = recdex<<8;
  465. recdex += *(data+3);
  466. recdex = recdex<<8;
  467. recdex += *(data+2);
  468. }else{
  469. if(seq == pMsg->seq){
  470. recdex = 0;
  471. }
  472. }
  473. updateData_next(recdex,data);
  474. }
  475. break;
  476. case PC_CMD_REPORT_RESULT:{
  477. unsigned char *data = pMsg->data;
  478. // unsigned char result = *data;
  479. // unsigned char part = *(data+1);
  480. clean_reult_next(data);
  481. // if(result){
  482. // /*消息成功,进行跟新*/
  483. // clean_reult_next(part);
  484. // }
  485. }
  486. break;
  487. }
  488. }
  489. int confirm_qrcode_action(uint8_t cmd, uint8_t *parg)
  490. {
  491. //PC返回成功,才认为扫码成功
  492. scan_qrcode_ropprt = 1;
  493. if(*parg == 1)
  494. {
  495. if(0 == mem_value(&card_param_info_temp, 0 , sizeof(str_card_param_info_t)))
  496. {
  497. // scan_qrcode_ropprt = 1;
  498. LOG_D("rec no card \n\r:");
  499. return -1;
  500. }
  501. if((psm.state == SW_Scan_Qrcode || psm.state == SW_Chip_Id )) //主机空闲,给自己
  502. {
  503. scan_qrcode_done = 1;
  504. memcpy(&card_param_info, &card_param_info_temp, sizeof(str_card_param_info_t));
  505. }
  506. else
  507. {
  508. //比较是否跟主模块相同
  509. if(0 == memcmp(&card_param_info, &card_param_info_temp, sizeof(str_card_param_info_t)))
  510. {
  511. return 0;
  512. }
  513. //主机不空,下发给从机
  514. card_param_info_temp.envtemp = evnTemp;
  515. b2b_send_device(B2B_CMD_REQ_QRCODE, (char *)&card_param_info_temp, sizeof(str_card_param_info_t), 1);
  516. }
  517. LOG_D("rec start\n\r:");
  518. return 0;
  519. }
  520. LOG_D("rec no macth\n\r:");
  521. return -1;
  522. }
  523. int confirm_error_action(uint8_t cmd, uint8_t *parg)
  524. {
  525. return 0;
  526. }
  527. int confirm_data_action(uint8_t cmd, uint8_t *parg)
  528. {
  529. return 0;
  530. }
  531. const struct PCActionPack mPCActionPack[] =
  532. {
  533. {
  534. .cmd = PC_CMD_HANDSHAKE,
  535. .action = handshake_action,
  536. .response = handshake_response,
  537. .datalen = 0,
  538. },
  539. {
  540. .cmd = PC_CMD_SETWIFI,
  541. .action = wifi_action,
  542. .response = set_response,
  543. .datalen = 0xff,
  544. },
  545. {
  546. .cmd = PC_CMD_SETSN,
  547. .action = SN_action,
  548. .response = handshake_response,
  549. .datalen = 7,
  550. },
  551. {
  552. .cmd = PC_CMD_QRCODE,
  553. .action = confirm_qrcode_action,
  554. .response = ans_response,
  555. .datalen = 0xff,
  556. },
  557. {
  558. .cmd = PC_CMD_SNANDCARD,
  559. .action = confirm_error_action,
  560. .datalen = 1,
  561. },
  562. {
  563. .cmd = PC_CMD_REPORT_RESULT,
  564. .action = confirm_result_action,
  565. .response = ans_response,
  566. .datalen = 0xff,
  567. },
  568. {
  569. .cmd = PC_CMD_REPORT_REEOR,
  570. .action = confirm_error_action,
  571. .response = ans_response,
  572. .datalen = 0xff,
  573. },
  574. {
  575. .cmd = PC_CMD_DATA_UPLOAD,
  576. .action = confirm_data_action,
  577. .response = ans_response,
  578. .datalen = 0xff,
  579. },
  580. {
  581. .cmd = PC_CMD_SETIP,
  582. .action = set_IP_action,
  583. .response = NULL,
  584. .datalen = 0xff,
  585. },
  586. {
  587. .cmd = PC_CMD_UPDATESTATE,
  588. .action = confirm_data_action,
  589. .response = ans_response,
  590. .datalen = 0xff,
  591. },
  592. {
  593. .cmd = PC_CMD_ASKSTATE,
  594. .action = confirm_data_action,
  595. .response = AskState_response,
  596. .datalen = 0xff,
  597. },
  598. {
  599. .cmd = PC_CMD_SETSTATE,
  600. .action = setState_action,
  601. .response = setState_response,
  602. .datalen = 0xff,
  603. },
  604. #ifdef ONECHIP_MINI_ONE
  605. {
  606. .cmd = PC_CMD_SETRUNTEMP,
  607. .action = send_temp_parm,
  608. .response = setTemp_response,
  609. .datalen = 0xff,
  610. },
  611. #endif
  612. };
  613. /********************************************************************
  614. * int handshake_action(uint8_t cmd, uint8_t *parg)
  615. *
  616. *描述:握手命令处理
  617. *参数:无
  618. *返回:无
  619. *其他:无
  620. *--------------------------------------------------------------------
  621. *记录:
  622. ********************************************************************/
  623. int con_heat_action(uint8_t cmd, uint8_t *parg )
  624. {
  625. // CmdMessage *pMsg = (CmdMessageDef *)parg;
  626. struct CmdMessage *pMsg = (struct CmdMessage *)parg;;
  627. miniConfigPram *pram = (miniConfigPram *)pMsg->data;
  628. if(pram->partID == Devicenum){
  629. if( pMsg->cmd == UINON_CMD_MOTORMOVE ){
  630. /*直接透传给从muc处理*/
  631. g_cfg.timeinner = *(float *)(pMsg->data+4) * 100;
  632. g_cfg.swingspeed = *(float *)(pMsg->data+8);
  633. g_cfg.swingtime = *(float *)(pMsg->data+12) * 100;
  634. g_cfg.swingtimeRight = *(float *)(pMsg->data+16) * 100;
  635. g_cfg.swingtimePause = *(float *)(pMsg->data+20) * 100;
  636. if((1 == *(int *)(pMsg->data+pMsg->len - 4))){
  637. cfg_save_cfg_param();
  638. Motor_Init();
  639. }else{
  640. Motor_mix(g_cfg.timeinner,g_cfg.swingspeed,g_cfg.swingtime,g_cfg.swingtimeRight,g_cfg.swingtimePause);
  641. }
  642. data_send_to_pc(pMsg->cmd, pMsg->data, pMsg->len);
  643. }
  644. if( pMsg->cmd == UINON_CMD_MOTORTRETURN ){
  645. /*直接透传给从muc处理*/
  646. uint32_t time = *(float *)(pMsg->data+4) * 10;
  647. uint32_t speed = *(float *)(pMsg->data+8) ;
  648. data_send_to_pc(pMsg->cmd, pMsg->data, pMsg->len);
  649. // sw_st = FIND_GAPNUM;
  650. // stateFistin = 1;
  651. }
  652. #ifdef ONECHIP_MINI_ONE
  653. if( pMsg->cmd == UINON_CMD_TEMP_ADJ ||
  654. pMsg->cmd == UINON_CMD_TEMP_PID ||
  655. pMsg->cmd == UINON_CMD_TEMP_ADJ_GET ||
  656. pMsg->cmd == UINON_CMD_TEMP_PID_GET ||
  657. pMsg->cmd == UINON_CMD_TEMP_START ||
  658. pMsg->cmd == UINON_CMD_TEMP_STOP){
  659. /*直接透传给从muc处理*/
  660. sendMsgtoSlaveMcu(pMsg);
  661. }
  662. #endif
  663. }else{
  664. #ifdef ONECHIP_MINI_ONE
  665. switch(pMsg->cmd){
  666. case UINON_CMD_THEROM_GOAL:{
  667. b2b_send_device(B2B_CMD_THEROM_GOAL, pMsg->data, pMsg->len, !Devicenum);
  668. }break;
  669. case UINON_CMD_THEROM_OFFSET:{
  670. b2b_send_device(B2B_CMD_THEROM_OFFSET, pMsg->data, pMsg->len, !Devicenum);
  671. }break;
  672. case UINON_CMD_THEROM_GET:{
  673. b2b_send_device(B2B_CMD_GETTHEROMPRAM_ACK, pMsg->data, pMsg->len, !Devicenum);
  674. }break;
  675. }
  676. /*发送给从模块 稍后实现*/
  677. #endif
  678. }
  679. return 0;
  680. }
  681. const struct PCActionPack mUnionActionPack[] =
  682. {
  683. {
  684. .cmd = UINON_CMD_MOTORMOVE,
  685. .action = con_heat_action,
  686. },
  687. {
  688. .cmd = UINON_CMD_MOTORTRETURN,
  689. .action = con_heat_action,
  690. },
  691. #ifdef ONECHIP_MINI_ONE
  692. {
  693. .cmd = UINON_CMD_TEMP_ADJ,
  694. .action = con_heat_action,
  695. },
  696. {
  697. .cmd = UINON_CMD_TEMP_PID,
  698. .action = con_heat_action,
  699. },
  700. {
  701. .cmd = UINON_CMD_TEMP_ADJ_GET,
  702. .action = con_heat_action,
  703. },
  704. {
  705. .cmd = UINON_CMD_TEMP_PID_GET,
  706. .action = con_heat_action,
  707. },
  708. {
  709. .cmd = UINON_CMD_TEMP_START,
  710. .action = con_heat_action,
  711. },
  712. {
  713. .cmd = UINON_CMD_TEMP_STOP,
  714. .action = con_heat_action,
  715. },
  716. {
  717. .cmd = UINON_CMD_THEROM_GOAL,
  718. .action = con_heat_action,
  719. },
  720. {
  721. .cmd = UINON_CMD_THEROM_OFFSET,
  722. .action = con_heat_action,
  723. },
  724. {
  725. .cmd = UINON_CMD_THEROM_GET,
  726. .action = con_heat_action,
  727. },
  728. {
  729. .cmd = UINON_CMD_TEMP_DEBUG,
  730. .response = handshake_response,
  731. },
  732. #endif
  733. };
  734. void deal_mUnion_cmd(struct CmdMessage *pMsg)
  735. {
  736. for (uint8_t i = 0; i < sizeof(mUnionActionPack)/sizeof(mUnionActionPack[0]); i++)
  737. {
  738. if (mUnionActionPack[i].cmd == pMsg->cmd )
  739. {
  740. uint8_t ret = mUnionActionPack[i].action( pMsg->cmd,(uint8_t *)pMsg);
  741. if (mUnionActionPack[i].response)
  742. {
  743. mUnionActionPack[i].response(pMsg, ret);
  744. }
  745. break;
  746. }
  747. }
  748. }
  749. /********************************************************************
  750. * void query_pc_cmd(struct CmdMessage *pMsg)
  751. *
  752. *描述:处理PC命令
  753. *参数:无
  754. *返回:无
  755. *其他:无
  756. *--------------------------------------------------------------------
  757. *记录:
  758. ********************************************************************/
  759. void query_pc_cmd(struct CmdMessage *pMsg)
  760. {
  761. for (uint8_t i = 0; i < sizeof(mPCActionPack)/sizeof(mPCActionPack[0]); i++)
  762. {
  763. if (mPCActionPack[i].cmd == pMsg->cmd && (mPCActionPack[i].datalen == pMsg->len || mPCActionPack[i].datalen == 0xff)) //防止接收到其他设备的数据帧
  764. {
  765. if(pc_send_cmd == mPCActionPack[i].cmd)
  766. pc_send_wait = 0;
  767. if(mPCActionPack[i].cmd != 1)
  768. {
  769. pc_resend_del(pMsg->seq);
  770. LOG_I("\n<-- pc cmd %x %d \n", pMsg->cmd, pMsg->seq);
  771. }
  772. pMsg->data[pMsg->len] = 0;
  773. uint8_t ret = mPCActionPack[i].action(mPCActionPack[i].cmd, pMsg->data);
  774. if (mPCActionPack[i].response)
  775. {
  776. mPCActionPack[i].response(pMsg, ret);
  777. }
  778. if(!(pc_channel_recv == 1 && !connectTcpFlag)){
  779. pc_link_timeout = 0; //确保TCP通信上在变灯
  780. }
  781. break;
  782. }
  783. }
  784. }