usr_b2pc_msg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. struct 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. /********************************************************************
  257. * int set_IP_action(uint8_t cmd, uint8_t *parg)
  258. *
  259. *描述:设置IP
  260. *参数:无
  261. *返回:无
  262. *其他:无
  263. *--------------------------------------------------------------------
  264. *记录:
  265. ********************************************************************/
  266. int set_IP_action(uint8_t cmd, uint8_t *parg)
  267. {
  268. if(connectTcpFlag == 0){
  269. /*TCP 链接状态下,不跟新IP地址*/
  270. memset(sysinf.TCP_IP_Addr,0,sizeof(sysinf.TCP_IP_Addr));
  271. memset(TCP_IP_PORT,0,sizeof(TCP_IP_PORT));
  272. unsigned int data_len = strlen(parg);
  273. unsigned int i=0;
  274. if(parg[i]<'0' || parg[i]>'9'){
  275. return 0;
  276. }
  277. for(;i<data_len;i++){
  278. if(parg[i] != ':'){
  279. sysinf.TCP_IP_Addr[i] = parg[i];
  280. }else{
  281. break;
  282. }
  283. }
  284. i++;
  285. unsigned int j =0;
  286. if(parg[i]<'0' || parg[i]>'9'){
  287. return 0;
  288. }
  289. for(;i<data_len;i++){
  290. if(parg[i] != ':'){
  291. sysinf.TCP_IP_PORT[j++] = parg[i];
  292. }else{
  293. break;
  294. }
  295. }
  296. if(sysinf.TCP_IP_Addr[0]){
  297. connectTcpFlag = 1;
  298. state_controlword = Hand_Shake_cmd;
  299. }
  300. WIFI_set_CloseConnect();
  301. }
  302. return 0;
  303. }
  304. void set_pc_chanel_recv(uint8_t channel)
  305. {
  306. pc_channel_recv = channel;
  307. }
  308. void set_pc_chanel_temp()
  309. {
  310. if(pc_channel_recv == 2)
  311. timeout_com = 0;
  312. pc_channel_send_backup = pc_channel_send;
  313. pc_channel_send = pc_channel_recv;
  314. }
  315. void reload_pc_chanel()
  316. {
  317. if(pc_channel_recv > pc_channel_send_backup) //串口优先
  318. {
  319. pc_channel_send_backup = pc_channel_recv;
  320. }
  321. pc_channel_send = pc_channel_send_backup;
  322. }
  323. void set_pc_chanel(uint8_t channel)
  324. {
  325. pc_channel_send_backup = channel;
  326. pc_channel_send = channel;
  327. }
  328. /********************************************************************
  329. * int handshake_action(uint8_t cmd, uint8_t *parg)
  330. *
  331. *描述:握手命令处理
  332. *参数:无
  333. *返回:无
  334. *其他:无
  335. *--------------------------------------------------------------------
  336. *记录:
  337. ********************************************************************/
  338. int handshake_action(uint8_t cmd, uint8_t *parg)
  339. {
  340. if(state_controlword < Hand_Shake_cmd) //上位机会间隔发送握手,保活
  341. {
  342. state_controlword = Hand_Shake_cmd;
  343. }
  344. return 0;
  345. }
  346. void char_replace(uint8_t *str, int size, uint8_t ch, uint8_t replace)
  347. {
  348. for(int i = 0; i < size; i++)
  349. {
  350. if(str[i] == ch)
  351. str[i] = replace;
  352. }
  353. }
  354. /********************************************************************
  355. * int wifi_action(uint8_t cmd, uint8_t *parg)
  356. *
  357. *描述:设置wifi命令处理
  358. *参数:无
  359. *返回:无
  360. *其他:无
  361. *--------------------------------------------------------------------
  362. *记录:
  363. ********************************************************************/
  364. int wifi_action(uint8_t cmd, uint8_t *parg)
  365. {
  366. // char_replace(parg, 64, 0xAA, 0);
  367. // char_replace(parg, 64, 0xAD, ' ');
  368. wifiCfg_set_new_wifi_param(parg, parg + WIFI_AP_NAME_MAX_LEN);
  369. // wifi用户名和密码设置
  370. WIFI_set_ssid();
  371. return 1;
  372. }
  373. int setState_action(uint8_t cmd, uint8_t *parg)
  374. {
  375. uint8_t block = *parg++;
  376. uint8_t nextState = *parg;
  377. if(block==0){
  378. if(SYS_STATE_WORKEND == nextState){
  379. psm.state = SW_move_card;
  380. }
  381. }else{
  382. /*下发命令,改变从模块状态*/
  383. b2b_send_device(B2B_CMD_STATE_SET, &nextState, sizeof(nextState), 1);
  384. }
  385. return 1;
  386. }
  387. /********************************************************************
  388. * int wifi_action(uint8_t cmd, uint8_t *parg)
  389. *
  390. *描述:设置wifi命令处理
  391. *参数:无
  392. *返回:无
  393. *其他:无
  394. *--------------------------------------------------------------------
  395. *记录:
  396. ********************************************************************/
  397. int SN_action(uint8_t cmd, uint8_t *parg)
  398. {
  399. parg[15] = 0;
  400. strcpy(g_cfg.SN, parg);
  401. PackDeviceNum();
  402. return cfg_save_cfg_param();
  403. }
  404. /********************************************************************
  405. * int seq_selftest_action(uint8_t cmd, uint8_t *parg)
  406. *
  407. *描述:设备自检处理
  408. *参数:无
  409. *返回:无
  410. *其他:无
  411. *--------------------------------------------------------------------
  412. *记录:
  413. ********************************************************************/
  414. int seq_selftest_action(uint8_t cmd, uint8_t *parg)
  415. {
  416. return 0;
  417. }
  418. /********************************************************************
  419. * int confirm_result_action(uint8_t cmd, uint8_t *parg)
  420. *
  421. *描述:设备收到pc的结果响应
  422. *参数:无
  423. *返回:无
  424. *其他:无
  425. *--------------------------------------------------------------------
  426. *记录:
  427. ********************************************************************/
  428. int confirm_result_action(uint8_t cmd, uint8_t *parg)
  429. {
  430. if(Devicenum)
  431. {
  432. return 0;
  433. }
  434. if(0 == memcmp(parg,card_param_info.pid, 10 ))
  435. state_controlword = result_move_card;
  436. return 0;
  437. }
  438. /********************************************************************
  439. * int default_pc_response(struct CmdMessage *pMsg, uint8_t result)
  440. *
  441. *描述:默认回应PC
  442. *参数:无
  443. *返回:成功->0 失败->-1
  444. *其他:无
  445. *--------------------------------------------------------------------
  446. *记录:
  447. ********************************************************************/
  448. int default_pc_response(struct CmdMessage *pMsg, uint8_t result)
  449. {
  450. if(Devicenum)
  451. {
  452. return 0;
  453. }
  454. return data_send_to_pc(pMsg->cmd, &result, 1);
  455. }
  456. /********************************************************************
  457. * int handshake_response(struct CmdMessage *pMsg, uint8_t result)
  458. *
  459. *描述:握手回应
  460. *参数:无
  461. *返回:无
  462. *其他:无
  463. *--------------------------------------------------------------------
  464. *记录:
  465. ********************************************************************/
  466. int handshake_response(struct CmdMessage *pMsg, uint8_t result)
  467. {
  468. int len = 0;
  469. // PackDeviceNum();
  470. len = strlen(DeviceNum);
  471. return data_send_to_pc(pMsg->cmd, (uint8_t*)DeviceNum, len);
  472. }
  473. int AskState_response(struct CmdMessage *pMsg, uint8_t result)
  474. {
  475. ANS_State();
  476. }
  477. int set_response(struct CmdMessage *pMsg, uint8_t result)
  478. {
  479. // char wifi_result = 1;
  480. // data_send_to_pc(PC_CMD_SETWIFI, &wifi_result, 1);
  481. }
  482. int setState_response(struct CmdMessage *pMsg, uint8_t result)
  483. {
  484. // char result = 1;
  485. data_send_to_pc(PC_CMD_SETSTATE, &result, 1);
  486. }
  487. int ans_response(struct CmdMessage *pMsg, uint8_t result)
  488. {
  489. /*清空需要上位机应答的消息*/
  490. switch(pMsg->cmd){
  491. case PC_CMD_REPORT_REEOR:
  492. // unsigned char *data = pMsg->data;
  493. // if(*data == Devicenum){
  494. // }
  495. clean_error_update();
  496. break;
  497. case PC_CMD_UPDATESTATE:
  498. clean_state_update();
  499. break;
  500. case PC_CMD_QRCODE:
  501. clean_QR_update();
  502. break;
  503. case PC_CMD_DATA_UPLOAD:{
  504. unsigned char *data = pMsg->data;
  505. unsigned int recdex = 0xfffffff;
  506. // unsigned char result = *data;
  507. // unsigned char part = *(data+1);
  508. if( pMsg->len == 6){
  509. recdex = *(data+5);
  510. recdex = recdex<<8;
  511. recdex += *(data+4);
  512. recdex = recdex<<8;
  513. recdex += *(data+3);
  514. recdex = recdex<<8;
  515. recdex += *(data+2);
  516. }else{
  517. if(seq == pMsg->seq){
  518. recdex = 0;
  519. }
  520. }
  521. updateData_next(recdex,data);
  522. }
  523. break;
  524. case PC_CMD_REPORT_RESULT:{
  525. unsigned char *data = pMsg->data;
  526. // unsigned char result = *data;
  527. // unsigned char part = *(data+1);
  528. clean_reult_next(data);
  529. // if(result){
  530. // /*消息成功,进行跟新*/
  531. // clean_reult_next(part);
  532. // }
  533. }
  534. break;
  535. }
  536. }
  537. int confirm_qrcode_action(uint8_t cmd, uint8_t *parg)
  538. {
  539. //PC返回成功,才认为扫码成功
  540. scan_qrcode_ropprt = 1;
  541. if(*parg == 1)
  542. {
  543. if(0 == mem_value(&card_param_info_temp, 0 , sizeof(str_card_param_info_t)))
  544. {
  545. // scan_qrcode_ropprt = 1;
  546. LOG_D("rec no card \n\r:");
  547. return -1;
  548. }
  549. if((psm.state == SW_Scan_Qrcode || psm.state == SW_Chip_Id )) //主机空闲,给自己
  550. {
  551. scan_qrcode_done = 1;
  552. memcpy(&card_param_info, &card_param_info_temp, sizeof(str_card_param_info_t));
  553. }
  554. else
  555. {
  556. //比较是否跟主模块相同
  557. if(0 == memcmp(&card_param_info, &card_param_info_temp, sizeof(str_card_param_info_t)))
  558. {
  559. return 0;
  560. }
  561. //主机不空,下发给从机
  562. card_param_info_temp.envtemp = evnTemp;
  563. b2b_send_device(B2B_CMD_REQ_QRCODE, (char *)&card_param_info_temp, sizeof(str_card_param_info_t), 1);
  564. }
  565. LOG_D("rec start\n\r:");
  566. return 0;
  567. }
  568. LOG_D("rec no macth\n\r:");
  569. return -1;
  570. }
  571. int confirm_error_action(uint8_t cmd, uint8_t *parg)
  572. {
  573. return 0;
  574. }
  575. int confirm_data_action(uint8_t cmd, uint8_t *parg)
  576. {
  577. return 0;
  578. }
  579. const struct PCActionPack mPCActionPack[] =
  580. {
  581. {
  582. .cmd = PC_CMD_HANDSHAKE,
  583. .action = handshake_action,
  584. .response = handshake_response,
  585. .datalen = 0,
  586. },
  587. {
  588. .cmd = PC_CMD_SETWIFI,
  589. .action = wifi_action,
  590. .response = set_response,
  591. .datalen = 0xff,
  592. },
  593. {
  594. .cmd = PC_CMD_SETSN,
  595. .action = SN_action,
  596. .response = handshake_response,
  597. .datalen = 7,
  598. },
  599. {
  600. .cmd = PC_CMD_QRCODE,
  601. .action = confirm_qrcode_action,
  602. .response = ans_response,
  603. .datalen = 0xff,
  604. },
  605. {
  606. .cmd = PC_CMD_SNANDCARD,
  607. .action = confirm_error_action,
  608. .datalen = 1,
  609. },
  610. {
  611. .cmd = PC_CMD_REPORT_RESULT,
  612. .action = confirm_result_action,
  613. .response = ans_response,
  614. .datalen = 0xff,
  615. },
  616. {
  617. .cmd = PC_CMD_REPORT_REEOR,
  618. .action = confirm_error_action,
  619. .response = ans_response,
  620. .datalen = 0xff,
  621. },
  622. {
  623. .cmd = PC_CMD_DATA_UPLOAD,
  624. .action = confirm_data_action,
  625. .response = ans_response,
  626. .datalen = 0xff,
  627. },
  628. {
  629. .cmd = PC_CMD_SETIP,
  630. .action = set_IP_action,
  631. .response = NULL,
  632. .datalen = 0xff,
  633. },
  634. {
  635. .cmd = PC_CMD_UPDATESTATE,
  636. .action = confirm_data_action,
  637. .response = ans_response,
  638. .datalen = 0xff,
  639. },
  640. {
  641. .cmd = PC_CMD_ASKSTATE,
  642. .action = confirm_data_action,
  643. .response = AskState_response,
  644. .datalen = 0xff,
  645. },
  646. {
  647. .cmd = PC_CMD_SETSTATE,
  648. .action = setState_action,
  649. .response = setState_response,
  650. .datalen = 0xff,
  651. },
  652. #ifdef ONECHIP_MINI_ONE
  653. {
  654. .cmd = PC_CMD_SETRUNTEMP,
  655. .action = send_temp_parm,
  656. .response = setTemp_response,
  657. .datalen = 0xff,
  658. },
  659. #endif
  660. };
  661. /********************************************************************
  662. * int handshake_action(uint8_t cmd, uint8_t *parg)
  663. *
  664. *描述:握手命令处理
  665. *参数:无
  666. *返回:无
  667. *其他:无
  668. *--------------------------------------------------------------------
  669. *记录:
  670. ********************************************************************/
  671. int con_heat_action(uint8_t cmd, uint8_t *parg )
  672. {
  673. // CmdMessage *pMsg = (CmdMessageDef *)parg;
  674. struct CmdMessage *pMsg = (struct CmdMessage *)parg;;
  675. miniConfigPram *pram = (miniConfigPram *)pMsg->data;
  676. if(pram->partID == Devicenum){
  677. if( pMsg->cmd == UINON_CMD_MOTORMOVE ){
  678. /*直接透传给从muc处理*/
  679. g_cfg.timeinner = *(float *)(pMsg->data+4) * 100;
  680. g_cfg.swingspeed = *(float *)(pMsg->data+8);
  681. g_cfg.swingtime = *(float *)(pMsg->data+12) * 100;
  682. g_cfg.swingtimeRight = *(float *)(pMsg->data+16) * 100;
  683. g_cfg.swingtimePause = *(float *)(pMsg->data+20) * 100;
  684. if((1 == *(int *)(pMsg->data+pMsg->len - 4))){
  685. cfg_save_cfg_param();
  686. Motor_Init();
  687. }else{
  688. Motor_mix(g_cfg.timeinner,g_cfg.swingspeed,g_cfg.swingtime,g_cfg.swingtimeRight,g_cfg.swingtimePause);
  689. }
  690. data_send_to_pc(pMsg->cmd, pMsg->data, pMsg->len);
  691. }
  692. if( pMsg->cmd == UINON_CMD_MOTORTRETURN ){
  693. /*直接透传给从muc处理*/
  694. uint32_t time = *(float *)(pMsg->data+4) * 10;
  695. uint32_t speed = *(float *)(pMsg->data+8) ;
  696. data_send_to_pc(pMsg->cmd, pMsg->data, pMsg->len);
  697. // sw_st = FIND_GAPNUM;
  698. // stateFistin = 1;
  699. }
  700. #ifdef ONECHIP_MINI_ONE
  701. if( pMsg->cmd == UINON_CMD_TEMP_ADJ ||
  702. pMsg->cmd == UINON_CMD_TEMP_PID ||
  703. pMsg->cmd == UINON_CMD_TEMP_ADJ_GET ||
  704. pMsg->cmd == UINON_CMD_TEMP_PID_GET ||
  705. pMsg->cmd == UINON_CMD_TEMP_START ||
  706. pMsg->cmd == UINON_CMD_TEMP_STOP){
  707. /*直接透传给从muc处理*/
  708. sendMsgtoSlaveMcu(pMsg);
  709. }
  710. #endif
  711. }else{
  712. #ifdef ONECHIP_MINI_ONE
  713. switch(pMsg->cmd){
  714. case UINON_CMD_THEROM_GOAL:{
  715. b2b_send_device(B2B_CMD_THEROM_GOAL, pMsg->data, pMsg->len, !Devicenum);
  716. }break;
  717. case UINON_CMD_THEROM_OFFSET:{
  718. b2b_send_device(B2B_CMD_THEROM_OFFSET, pMsg->data, pMsg->len, !Devicenum);
  719. }break;
  720. case UINON_CMD_THEROM_GET:{
  721. b2b_send_device(B2B_CMD_GETTHEROMPRAM_ACK, pMsg->data, pMsg->len, !Devicenum);
  722. }break;
  723. }
  724. /*发送给从模块 稍后实现*/
  725. #endif
  726. }
  727. return 0;
  728. }
  729. const struct PCActionPack mUnionActionPack[] =
  730. {
  731. {
  732. .cmd = UINON_CMD_MOTORMOVE,
  733. .action = con_heat_action,
  734. },
  735. {
  736. .cmd = UINON_CMD_MOTORTRETURN,
  737. .action = con_heat_action,
  738. },
  739. #ifdef ONECHIP_MINI_ONE
  740. {
  741. .cmd = UINON_CMD_TEMP_ADJ,
  742. .action = con_heat_action,
  743. },
  744. {
  745. .cmd = UINON_CMD_TEMP_PID,
  746. .action = con_heat_action,
  747. },
  748. {
  749. .cmd = UINON_CMD_TEMP_ADJ_GET,
  750. .action = con_heat_action,
  751. },
  752. {
  753. .cmd = UINON_CMD_TEMP_PID_GET,
  754. .action = con_heat_action,
  755. },
  756. {
  757. .cmd = UINON_CMD_TEMP_START,
  758. .action = con_heat_action,
  759. },
  760. {
  761. .cmd = UINON_CMD_TEMP_STOP,
  762. .action = con_heat_action,
  763. },
  764. {
  765. .cmd = UINON_CMD_THEROM_GOAL,
  766. .action = con_heat_action,
  767. },
  768. {
  769. .cmd = UINON_CMD_THEROM_OFFSET,
  770. .action = con_heat_action,
  771. },
  772. {
  773. .cmd = UINON_CMD_THEROM_GET,
  774. .action = con_heat_action,
  775. },
  776. {
  777. .cmd = UINON_CMD_TEMP_DEBUG,
  778. .response = handshake_response,
  779. },
  780. #endif
  781. };
  782. void deal_mUnion_cmd(struct CmdMessage *pMsg)
  783. {
  784. for (uint8_t i = 0; i < sizeof(mUnionActionPack)/sizeof(mUnionActionPack[0]); i++)
  785. {
  786. if (mUnionActionPack[i].cmd == pMsg->cmd )
  787. {
  788. uint8_t ret = mUnionActionPack[i].action( pMsg->cmd,(uint8_t *)pMsg);
  789. if (mUnionActionPack[i].response)
  790. {
  791. mUnionActionPack[i].response(pMsg, ret);
  792. }
  793. break;
  794. }
  795. }
  796. }
  797. /********************************************************************
  798. * void query_pc_cmd(struct CmdMessage *pMsg)
  799. *
  800. *描述:处理PC命令
  801. *参数:无
  802. *返回:无
  803. *其他:无
  804. *--------------------------------------------------------------------
  805. *记录:
  806. ********************************************************************/
  807. void query_pc_cmd(struct CmdMessage *pMsg)
  808. {
  809. for (uint8_t i = 0; i < sizeof(mPCActionPack)/sizeof(mPCActionPack[0]); i++)
  810. {
  811. if (mPCActionPack[i].cmd == pMsg->cmd && (mPCActionPack[i].datalen == pMsg->len || mPCActionPack[i].datalen == 0xff)) //防止接收到其他设备的数据帧
  812. {
  813. if(pc_send_cmd == mPCActionPack[i].cmd)
  814. pc_send_wait = 0;
  815. if(mPCActionPack[i].cmd != 1)
  816. {
  817. pc_resend_del(pMsg->seq);
  818. LOG_I("\n<-- pc cmd %x %d \n", pMsg->cmd, pMsg->seq);
  819. }
  820. pMsg->data[pMsg->len] = 0;
  821. uint8_t ret = mPCActionPack[i].action(mPCActionPack[i].cmd, pMsg->data);
  822. if (mPCActionPack[i].response)
  823. {
  824. mPCActionPack[i].response(pMsg, ret);
  825. }
  826. if(!(pc_channel_recv == 1 && !connectTcpFlag)){
  827. pc_link_timeout = 0; //确保TCP通信上在变灯
  828. }
  829. break;
  830. }
  831. }
  832. }