communication.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include "onechip_include_header.h"
  2. char DeviceNum[64] = "mini7 2312230001 v1.1, ";
  3. uint8_t pc_send_wait = 0; //数据发送等待,0可以立即发送;>0正在发送请等待
  4. uint8_t pc_send_cmd = 0; //当前发送cmd
  5. static unsigned char seq =0;
  6. extern uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
  7. extern uint16_t wifi_tx_len;
  8. updateProcessInf updateInf;
  9. unsigned int check_to_pc_busy(void){
  10. /*判断是否可以发送,缓冲区为空或者索引为0*/
  11. CmdMessage *pMessage = (CmdMessage *)wifi_tx_buf;
  12. if(pMessage->seq == 0 || pMessage->len == 0){
  13. return 1;
  14. }
  15. return 0;
  16. }
  17. /********************************************************************
  18. * uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
  19. *
  20. *描述:发生数据到PC
  21. *参数:Devicenum 0-发至主机,1发至从机
  22. *返回:无
  23. *其他:无
  24. *--------------------------------------------------------------------
  25. *记录:
  26. ********************************************************************/
  27. CmdMessage msg;
  28. int data_send_to_pc(pc_cmd_type cmd, char *data, unsigned short len)
  29. {
  30. uint16_t check;
  31. int ret = -1;
  32. msg.head = 0x55aa;
  33. msg.seq = CDM_FRAME_TYPE_REQ;
  34. msg.cmd = cmd;
  35. msg.tail = 0x5a;
  36. msg.len = len;
  37. msg.data = data;
  38. if((len+8) > WIFI_TX_BUF_SIZE){
  39. Van_Device_Printf(VAN_LOG_COMM,"send_to_pc :%x len:%d error ", cmd, len);
  40. return ret;
  41. }
  42. if(sysinf.pc_channel_recv == PC_CONNECT_TYPE_NULL && ((cmd != PC_CMD_SETIP) && (cmd != PC_CMD_STARTUPDATE)) ){
  43. /*无链接情况,只考虑*/
  44. return ret;
  45. }
  46. if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI && PC_CMD_DATASCOPE == cmd)|| /*网口不上报数据*/
  47. (sysinf.pc_channel_recv == PC_CONNECT_TYPE_UART && PC_CMD_SETIP == cmd)) /*串口不广播UDP*/{
  48. return ret;
  49. }
  50. switch(cmd)
  51. {
  52. case PC_CMD_QRCODE:
  53. case PC_CMD_DATA_UPLOAD:
  54. case PC_CMD_REPORT_RESULT:
  55. seq++;
  56. if(0xff == seq){
  57. seq =1;
  58. }
  59. msg.seq = seq;
  60. break;
  61. default: //应答
  62. break;
  63. }
  64. check = checkSum((const unsigned char *)&msg.seq, 4);
  65. check += checkSum((const unsigned char *)data, len);
  66. msg.checksum = check & 0xFF;
  67. msg.tail = (check >> 8);
  68. if(check_to_pc_busy()){
  69. memcpy(&wifi_tx_buf[0], (uint8_t*)&msg, MSG_CMDFRAME_LEN);
  70. memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN], data, len);
  71. memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN+len], (uint8_t*)&check, 2);
  72. wifi_tx_len = MSG_CMDFRAME_LEN+len+2;
  73. }else{
  74. return ret;
  75. }
  76. if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI || cmd == PC_CMD_SETIP ) && PC_CMD_SETWIFI != cmd)
  77. {
  78. return WIFI_ReSend();
  79. }
  80. else
  81. {
  82. Van_Debug_UART_Send_DMA((unsigned char *)wifi_tx_buf, wifi_tx_len); //将收到的信息发送出
  83. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  84. }
  85. return 0;
  86. }
  87. int data_send_to_pc_withindex(pc_cmd_type cmd, char *data, unsigned short len, unsigned char index)
  88. {
  89. uint16_t check;
  90. int ret = -1;
  91. msg.head = 0x55aa;
  92. msg.seq = index;
  93. msg.cmd = cmd;
  94. msg.tail = 0x5a;
  95. msg.len = len;
  96. msg.data = data;
  97. check = checkSum((const unsigned char *)&msg.seq, 4);
  98. check += checkSum((const unsigned char *)data, len);
  99. msg.checksum = check & 0xFF;
  100. msg.tail = (check >> 8);
  101. if(check_to_pc_busy()){
  102. memcpy(&wifi_tx_buf[0], (uint8_t*)&msg, MSG_CMDFRAME_LEN);
  103. memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN], data, len);
  104. memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN+len], (uint8_t*)&check, 2);
  105. wifi_tx_len = MSG_CMDFRAME_LEN+len+2;
  106. }else{
  107. return ret;
  108. }
  109. if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI || cmd == PC_CMD_SETIP ) && PC_CMD_SETWIFI != cmd)
  110. {
  111. return WIFI_ReSend();
  112. }
  113. else
  114. {
  115. Van_Debug_UART_Send_DMA((unsigned char *)wifi_tx_buf, wifi_tx_len); //将收到的信息发送出
  116. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  117. }
  118. return 0;
  119. }
  120. void PackDeviceNum(void){
  121. int len = 0;
  122. memset((uint8_t *)&DeviceNum, 0 ,sizeof(DeviceNum));
  123. uint32_t uid[3];
  124. uid[0] = HAL_GetUIDw0(); // 读取低32位[reference:16]
  125. uid[1] = HAL_GetUIDw1(); // 读取中间32位[reference:17]
  126. uid[2] = HAL_GetUIDw2(); // 读取高32位[reference:18]
  127. #ifdef ONECHIP_MINI_ONE
  128. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7_boot,V%d.%d.%d.%d",SOFT_VERSION_1,SOFT_VERSION_2,SOFT_VERSION_3,SOFT_VERSION_4);
  129. #else
  130. #ifdef ONECHIP_MINI_PLUS
  131. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7_boot,V%d.%d.%d.%d",SOFT_VERSION_1,SOFT_VERSION_2,SOFT_VERSION_3,SOFT_VERSION_4);
  132. #else
  133. my_sprintf(&DeviceNum[len],sizeof(DeviceNum), "Mini7_boot,V%d.%d.%d.%d",SOFT_VERSION_1,SOFT_VERSION_2,SOFT_VERSION_3,SOFT_VERSION_4);
  134. #endif //ONECHIP_MINI_PLUS
  135. #endif //ONECHIP_MINI_ONE
  136. strcat(DeviceNum, ",");
  137. len = strlen(DeviceNum);
  138. hex_to_ascii((const void *)uid, &DeviceNum[len], 12); //uid
  139. strcat(DeviceNum, ",");
  140. strcat(DeviceNum, (const char *)sysinf.SN);
  141. strcat(DeviceNum, " ");
  142. Van_Device_Printf(VAN_LOG_INFO,"DeviceNum:%s\n", DeviceNum);
  143. }
  144. /********************************************************************
  145. * uint16_t send_Devinf_pc(void)
  146. *
  147. *描述:发送设备信息到主机,便于主机下法TCP信息
  148. *参数:无
  149. *返回:无
  150. *其他:无
  151. *--------------------------------------------------------------------
  152. *记录:
  153. ********************************************************************/
  154. void send_Devinf_pc(void)
  155. {
  156. uint16_t len = 0;
  157. len = strlen(DeviceNum);
  158. data_send_to_pc(PC_CMD_SETIP,DeviceNum,len);
  159. }
  160. /********************************************************************
  161. * uint16_t send_Devinf_pc(void)
  162. *
  163. *描述:发送设备信息到主机,便于主机下法TCP信息
  164. *参数:无
  165. *返回:无
  166. *其他:无
  167. *--------------------------------------------------------------------
  168. *记录:
  169. ********************************************************************/
  170. int send_baseinf_pc(unsigned char cmd, unsigned char *parg)
  171. {
  172. uint16_t len = 0;
  173. len = strlen(DeviceNum);
  174. data_send_to_pc(PC_CMD_STARTUPDATE,DeviceNum,len);
  175. }
  176. /********************************************************************
  177. * int handshake_action(unsigned char cmd, unsigned char *parg)
  178. *
  179. *描述:握手命令处理
  180. *参数:无
  181. *返回:无
  182. *其他:无
  183. *--------------------------------------------------------------------
  184. *记录:
  185. ********************************************************************/
  186. int handshake_action(unsigned char cmd, unsigned char *parg)
  187. {
  188. sysinf.system_OnlineFlag = true;
  189. return 0;
  190. }
  191. void char_replace(unsigned char *str, int size, unsigned char ch, unsigned char replace)
  192. {
  193. for(int i = 0; i < size; i++)
  194. {
  195. if(str[i] == ch)
  196. str[i] = replace;
  197. }
  198. }
  199. /********************************************************************
  200. * int wifi_action(unsigned char cmd, unsigned char *parg)
  201. *
  202. *描述:设置wifi命令处理
  203. *参数:无
  204. *返回:无
  205. *其他:无
  206. *--------------------------------------------------------------------
  207. *记录:
  208. ********************************************************************/
  209. int wifi_action(unsigned char cmd, unsigned char *parg)
  210. {
  211. strcpy(sysinf.wifiname, (const char *)parg);
  212. strcpy(sysinf.password, (const char *)(parg + WIFI_AP_NAME_MAX_LEN));
  213. // wifi用户名和密码设置
  214. WIFI_set_ssid();
  215. return 1;
  216. }
  217. /********************************************************************
  218. * int wifi_action(unsigned char cmd, unsigned char *parg)
  219. *
  220. *描述:设置wifi命令处理
  221. *参数:无
  222. *返回:无
  223. *其他:无
  224. *--------------------------------------------------------------------
  225. *记录:
  226. ********************************************************************/
  227. int SN_action(unsigned char cmd, unsigned char *parg)
  228. {
  229. parg[15] = 0;
  230. strcpy((char *)sysinf.SN, (const char *)parg);
  231. // PackDeviceNum();
  232. return cfg_save_cfg_param();
  233. }
  234. /********************************************************************
  235. * int default_pc_response(struct CmdMessage *pMsg, unsigned char result)
  236. *
  237. *描述:默认回应PC
  238. *参数:无
  239. *返回:成功->0 失败->-1
  240. *其他:无
  241. *--------------------------------------------------------------------
  242. *记录:
  243. ********************************************************************/
  244. int default_pc_response(CmdMessage *pMsg, unsigned char result)
  245. {
  246. return data_send_to_pc((pc_cmd_type)pMsg->cmd, (char *)&result, 1);
  247. }
  248. /********************************************************************
  249. * int handshake_response(struct CmdMessage *pMsg, unsigned char result)
  250. *
  251. *描述:握手回应
  252. *参数:无
  253. *返回:无
  254. *其他:无
  255. *--------------------------------------------------------------------
  256. *记录:
  257. ********************************************************************/
  258. int handshake_response(CmdMessage *pMsg, unsigned char result)
  259. {
  260. unsigned short len = 0;
  261. PackDeviceNum();
  262. len = strlen(DeviceNum);
  263. return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)DeviceNum, len,pMsg->seq);
  264. }
  265. int set_response(CmdMessage *pMsg, unsigned char result)
  266. {
  267. return data_send_to_pc((pc_cmd_type)pMsg->cmd, (char*)&result, 1);
  268. return 0;
  269. }
  270. /********************************************************************
  271. * int set_IP_action(uint8_t cmd, uint8_t *parg)
  272. *
  273. *描述:设置IP
  274. *参数:无
  275. *返回:无
  276. *其他:无
  277. *--------------------------------------------------------------------
  278. *记录:
  279. ********************************************************************/
  280. int set_IP_action(uint8_t cmd, uint8_t *parg)
  281. {
  282. if(sysinf.connectTcpFlag == 0){
  283. /*TCP 链接状态下,不跟新IP地址*/
  284. memset(sysinf.TCP_IP_Addr,0,sizeof(sysinf.TCP_IP_Addr));
  285. memset(sysinf.TCP_IP_PORT,0,sizeof(sysinf.TCP_IP_PORT));
  286. unsigned int data_len = strlen((const char *)parg);
  287. unsigned int i=0;
  288. if(parg[i]<'0' || parg[i]>'9'){
  289. return 0;
  290. }
  291. for(;i<data_len;i++){
  292. if(parg[i] != ':'){
  293. sysinf.TCP_IP_Addr[i] = parg[i];
  294. }else{
  295. break;
  296. }
  297. }
  298. i++;
  299. unsigned int j =0;
  300. if(parg[i]<'0' || parg[i]>'9'){
  301. return 0;
  302. }
  303. for(;i<data_len;i++){
  304. if(parg[i] != ':'){
  305. sysinf.TCP_IP_PORT[j++] = parg[i];
  306. }else{
  307. break;
  308. }
  309. }
  310. if(sysinf.TCP_IP_Addr[0]){
  311. sysinf.connectTcpFlag = 1;
  312. }
  313. WIFI_set_CloseConnect();
  314. }
  315. return 0;
  316. }
  317. /********************************************************************
  318. * int updateStart_action(struct CmdMessage *pMsg, unsigned char result)
  319. *
  320. *描述:备份APP1到APP2区域
  321. *参数:无
  322. *返回:备份成功失败
  323. *其他:无
  324. *--------------------------------------------------------------------
  325. *记录:
  326. ********************************************************************/
  327. int updateStart_action(uint8_t cmd, uint8_t *parg)
  328. {
  329. static int restult = 0;
  330. updateInf.updateState = FLASH_UPDATE_ST_NUILL;
  331. if(restult==0){ ;
  332. restult = app_image_copy(FLASH_APP1_ADDR,FLASH_APP2_ADDR,FLASH_PAGE_SIZE,FLASH_APP2_SIZE);
  333. }
  334. if(restult){
  335. updateInf.updateState = FLASH_UPDATE_ST_BACKUP;
  336. }
  337. return restult;
  338. }
  339. int updateInfo_action(uint8_t cmd, uint8_t *parg)
  340. {
  341. updateHeader *upHead = (updateHeader *)parg;
  342. if(updateInf.updateState == FLASH_UPDATE_ST_BACKUP){
  343. updateInf.allchecksum = upHead->allchecksum;
  344. updateInf.appLen = upHead->appLen;
  345. updateInf.allpackage = upHead->allpackage;
  346. updateInf.packageSize = upHead->packageSize;
  347. if(FLASH_PAGE_SIZE%upHead->packageSize){
  348. /*包大小演出*/
  349. return 0;
  350. }
  351. updateInf.packageInPage = FLASH_PAGE_SIZE/upHead->packageSize;
  352. updateInf.writeAddr = FLASH_APP1_ADDR;
  353. updateInf.lenInPage = 0;
  354. updateInf.updateState = FLASH_UPDATE_ST_REC;
  355. updateInf.nextpackageOrder = 0;
  356. light_set(MODE_BREATH, 1000, COLOR_YELLOW, 0);
  357. light_set(MODE_BREATH, 1000, COLOR_YELLOW, 1);
  358. Flash_ErasePage(FLASH_APP1_ADDR + FLASH_APP_VERSION_PAGE);
  359. return 1;
  360. }
  361. return 0;
  362. }
  363. int updateDate_action(uint8_t cmd, uint8_t *parg)
  364. {
  365. if(updateInf.updateState == FLASH_UPDATE_ST_REC){
  366. Refresh_IWDG();
  367. updateData *upHead = (updateData *)parg;
  368. unsigned int posInPage;
  369. if(upHead->packageOrder !=updateInf.nextpackageOrder){
  370. /*序号不对,返回错误*/
  371. return 0;
  372. }
  373. if(updateInf.updateState != FLASH_UPDATE_ST_REC){
  374. /*状态不对,返回错误*/
  375. return 0;
  376. }
  377. posInPage = upHead->packageOrder%(updateInf.packageInPage);
  378. memcpy(&updateInf.data[posInPage*updateInf.packageSize], (uint8_t*)upHead->data, upHead->packageSize);
  379. updateInf.nextpackageOrder++;
  380. updateInf.lenInPage += upHead->packageSize;
  381. if((updateInf.nextpackageOrder%updateInf.packageInPage==0) || (updateInf.nextpackageOrder==updateInf.allpackage)){
  382. /*当前页收完,或包收完*/
  383. iap_write_apppage(updateInf.writeAddr,updateInf.data,updateInf.lenInPage);
  384. updateInf.writeAddr = updateInf.writeAddr+updateInf.lenInPage;
  385. updateInf.lenInPage = 0;
  386. }
  387. if(updateInf.nextpackageOrder==updateInf.allpackage){
  388. /*稍后添加check sum */;
  389. updateInf.updateState = FLASH_UPDATE_ST_PROG;
  390. }
  391. updateInf.updateWaitTime = 0;
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. int updatestart_respone(CmdMessage *pMsg, unsigned char result)
  397. {
  398. unsigned short len = 0;
  399. PackDeviceNum();
  400. len = strlen(DeviceNum);
  401. return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)DeviceNum, len,pMsg->seq);
  402. }
  403. int updateInfo_response(CmdMessage *pMsg, unsigned char result)
  404. {
  405. return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)&result, 1,pMsg->seq);
  406. }
  407. int updateDate_response(CmdMessage *pMsg, unsigned char result)
  408. {
  409. unsigned char *data = (unsigned char *)pMsg->data;
  410. *(unsigned int *)(data+4) = updateInf.nextpackageOrder;
  411. *(unsigned int *)(data+8) = result;
  412. return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, pMsg->data, 12,pMsg->seq);
  413. }
  414. int in_update_process(void)
  415. {
  416. updateInf.updateWaitTime++;
  417. if(updateInf.updateState == FLASH_UPDATE_ST_REC) {
  418. if(updateInf.updateWaitTime > 10){
  419. if(updateInf.nextpackageOrder > updateInf.packageInPage){
  420. /*接收数据超过1页,已经缓存,擦除标记,进行回退*/
  421. Flash_ErasePage(FLASH_APP1_ADDR + FLASH_APP_VERSION_PAGE);
  422. }
  423. updateInf.updateState = FLASH_UPDATE_ST_NUILL;
  424. return 0;
  425. }
  426. return 1;
  427. }
  428. return 0;
  429. }
  430. int update_process_end(void)
  431. {
  432. if(updateInf.updateState == FLASH_UPDATE_ST_PROG) return 1;
  433. return 0;
  434. }
  435. const struct PCActionPack mPCActionPack[] =
  436. {
  437. {
  438. .cmd = PC_CMD_HANDSHAKE,
  439. .action = handshake_action,
  440. .response = handshake_response,
  441. .datalen = 0,
  442. },
  443. {
  444. .cmd = PC_CMD_SETWIFI,
  445. .action = wifi_action,
  446. .response = set_response,
  447. .datalen = 0xff,
  448. },
  449. {
  450. .cmd = PC_CMD_SETSN,
  451. .action = SN_action,
  452. .response = handshake_response,
  453. .datalen = 7,
  454. },
  455. {
  456. .cmd = PC_CMD_STARTUPDATE,
  457. .action = updateStart_action,
  458. .response = updatestart_respone,
  459. .datalen = 0xff,
  460. },
  461. {
  462. .cmd = PC_CMD_UPDATEINFO,
  463. .action = updateInfo_action,
  464. .response = updateInfo_response,
  465. .datalen = 0xff,
  466. },
  467. {
  468. .cmd = PC_CMD_UPDATEDATA,
  469. .action = updateDate_action,
  470. .response = updateDate_response,
  471. .datalen = 0xff,
  472. },
  473. };
  474. void pc_resend_del(uint8_t seq){
  475. // struct CmdMessage *pMessage = (struct CmdMessage *)wifi_tx_buf;
  476. // if(pMessage->seq == seq){
  477. // wifi_tx_len = 0;
  478. // memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  479. // }
  480. }
  481. /********************************************************************
  482. * void query_pc_cmd(struct CmdMessage *pMsg)
  483. *
  484. *描述:处理PC命令
  485. *参数:无
  486. *返回:无
  487. *其他:无
  488. *--------------------------------------------------------------------
  489. *记录:
  490. ********************************************************************/
  491. void query_pc_cmd(CmdMessage *pMsg)
  492. {
  493. for (unsigned char i = 0; i < sizeof(mPCActionPack)/sizeof(mPCActionPack[0]); i++)
  494. {
  495. if (mPCActionPack[i].cmd == pMsg->cmd && (mPCActionPack[i].datalen == pMsg->len || mPCActionPack[i].datalen == 0xff)) //防止接收到其他设备的数据帧
  496. {
  497. if(pc_send_cmd == mPCActionPack[i].cmd)
  498. pc_send_wait = 0;
  499. if(mPCActionPack[i].cmd != 1)
  500. {
  501. pc_resend_del(pMsg->seq);
  502. Van_Device_Printf(VAN_LOG_INFO,"\n<-- pc cmd %x %d \n", pMsg->cmd, pMsg->seq);
  503. }
  504. pMsg->data[pMsg->len] = 0;
  505. unsigned char ret = mPCActionPack[i].action(mPCActionPack[i].cmd, (unsigned char *)pMsg->data);
  506. if (mPCActionPack[i].response)
  507. {
  508. mPCActionPack[i].response(pMsg, ret);
  509. }
  510. break;
  511. }
  512. }
  513. }