| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- #include "onechip_include_header.h"
- char DeviceNum[64] = "mini7 2312230001 v1.1, ";
- uint8_t pc_send_wait = 0; //数据发送等待,0可以立即发送;>0正在发送请等待
- uint8_t pc_send_cmd = 0; //当前发送cmd
- static unsigned char seq =0;
- extern uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
- extern uint16_t wifi_tx_len;
- updateProcessInf updateInf;
- unsigned int check_to_pc_busy(void){
- /*判断是否可以发送,缓冲区为空或者索引为0*/
- CmdMessage *pMessage = (CmdMessage *)wifi_tx_buf;
- if(pMessage->seq == 0 || pMessage->len == 0){
- return 1;
- }
- return 0;
- }
- /********************************************************************
- * uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len)
- *
- *描述:发生数据到PC
- *参数:Devicenum 0-发至主机,1发至从机
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- CmdMessage msg;
- int data_send_to_pc(pc_cmd_type cmd, char *data, unsigned short len)
- {
- uint16_t check;
- int ret = -1;
- msg.head = 0x55aa;
- msg.seq = CDM_FRAME_TYPE_REQ;
- msg.cmd = cmd;
- msg.tail = 0x5a;
- msg.len = len;
- msg.data = data;
- if((len+8) > WIFI_TX_BUF_SIZE){
- Van_Device_Printf(VAN_LOG_COMM,"send_to_pc :%x len:%d error ", cmd, len);
- return ret;
- }
-
- if(sysinf.pc_channel_recv == PC_CONNECT_TYPE_NULL && ((cmd != PC_CMD_SETIP) && (cmd != PC_CMD_STARTUPDATE)) ){
- /*无链接情况,只考虑*/
- return ret;
- }
- if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI && PC_CMD_DATASCOPE == cmd)|| /*网口不上报数据*/
- (sysinf.pc_channel_recv == PC_CONNECT_TYPE_UART && PC_CMD_SETIP == cmd)) /*串口不广播UDP*/{
- return ret;
- }
- switch(cmd)
- {
- case PC_CMD_QRCODE:
- case PC_CMD_DATA_UPLOAD:
- case PC_CMD_REPORT_RESULT:
- seq++;
- if(0xff == seq){
- seq =1;
- }
- msg.seq = seq;
- break;
- default: //应答
- break;
- }
- check = checkSum((const unsigned char *)&msg.seq, 4);
- check += checkSum((const unsigned char *)data, len);
- msg.checksum = check & 0xFF;
- msg.tail = (check >> 8);
- if(check_to_pc_busy()){
- memcpy(&wifi_tx_buf[0], (uint8_t*)&msg, MSG_CMDFRAME_LEN);
- memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN], data, len);
- memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN+len], (uint8_t*)&check, 2);
- wifi_tx_len = MSG_CMDFRAME_LEN+len+2;
- }else{
- return ret;
- }
- if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI || cmd == PC_CMD_SETIP ) && PC_CMD_SETWIFI != cmd)
- {
- return WIFI_ReSend();
- }
- else
- {
- Van_Debug_UART_Send_DMA((unsigned char *)wifi_tx_buf, wifi_tx_len); //将收到的信息发送出
- memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
- }
- return 0;
- }
- int data_send_to_pc_withindex(pc_cmd_type cmd, char *data, unsigned short len, unsigned char index)
- {
- uint16_t check;
- int ret = -1;
- msg.head = 0x55aa;
- msg.seq = index;
- msg.cmd = cmd;
- msg.tail = 0x5a;
- msg.len = len;
- msg.data = data;
- check = checkSum((const unsigned char *)&msg.seq, 4);
- check += checkSum((const unsigned char *)data, len);
- msg.checksum = check & 0xFF;
- msg.tail = (check >> 8);
- if(check_to_pc_busy()){
- memcpy(&wifi_tx_buf[0], (uint8_t*)&msg, MSG_CMDFRAME_LEN);
- memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN], data, len);
- memcpy(&wifi_tx_buf[MSG_CMDFRAME_LEN+len], (uint8_t*)&check, 2);
- wifi_tx_len = MSG_CMDFRAME_LEN+len+2;
- }else{
- return ret;
- }
- if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI || cmd == PC_CMD_SETIP ) && PC_CMD_SETWIFI != cmd)
- {
- return WIFI_ReSend();
- }
- else
- {
- Van_Debug_UART_Send_DMA((unsigned char *)wifi_tx_buf, wifi_tx_len); //将收到的信息发送出
- memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
- }
- return 0;
- }
- void PackDeviceNum(void){
- int len = 0;
- memset((uint8_t *)&DeviceNum, 0 ,sizeof(DeviceNum));
- uint32_t uid[3];
- uid[0] = HAL_GetUIDw0(); // 读取低32位[reference:16]
- uid[1] = HAL_GetUIDw1(); // 读取中间32位[reference:17]
- uid[2] = HAL_GetUIDw2(); // 读取高32位[reference:18]
- #ifdef ONECHIP_MINI_ONE
- 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);
- #else
- #ifdef ONECHIP_MINI_PLUS
- 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);
- #else
- 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);
- #endif //ONECHIP_MINI_PLUS
- #endif //ONECHIP_MINI_ONE
- strcat(DeviceNum, ",");
- len = strlen(DeviceNum);
- hex_to_ascii((const void *)uid, &DeviceNum[len], 12); //uid
- strcat(DeviceNum, ",");
- strcat(DeviceNum, (const char *)sysinf.SN);
- strcat(DeviceNum, " ");
- Van_Device_Printf(VAN_LOG_INFO,"DeviceNum:%s\n", DeviceNum);
- }
- /********************************************************************
- * uint16_t send_Devinf_pc(void)
- *
- *描述:发送设备信息到主机,便于主机下法TCP信息
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- void send_Devinf_pc(void)
- {
- uint16_t len = 0;
- len = strlen(DeviceNum);
- data_send_to_pc(PC_CMD_SETIP,DeviceNum,len);
- }
- /********************************************************************
- * uint16_t send_Devinf_pc(void)
- *
- *描述:发送设备信息到主机,便于主机下法TCP信息
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int send_baseinf_pc(unsigned char cmd, unsigned char *parg)
- {
- uint16_t len = 0;
- len = strlen(DeviceNum);
- data_send_to_pc(PC_CMD_STARTUPDATE,DeviceNum,len);
- }
- /********************************************************************
- * int handshake_action(unsigned char cmd, unsigned char *parg)
- *
- *描述:握手命令处理
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int handshake_action(unsigned char cmd, unsigned char *parg)
- {
-
- sysinf.system_OnlineFlag = true;
- return 0;
- }
- void char_replace(unsigned char *str, int size, unsigned char ch, unsigned char replace)
- {
- for(int i = 0; i < size; i++)
- {
- if(str[i] == ch)
- str[i] = replace;
- }
- }
- /********************************************************************
- * int wifi_action(unsigned char cmd, unsigned char *parg)
- *
- *描述:设置wifi命令处理
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int wifi_action(unsigned char cmd, unsigned char *parg)
- {
- strcpy(sysinf.wifiname, (const char *)parg);
- strcpy(sysinf.password, (const char *)(parg + WIFI_AP_NAME_MAX_LEN));
- // wifi用户名和密码设置
- WIFI_set_ssid();
- return 1;
- }
- /********************************************************************
- * int wifi_action(unsigned char cmd, unsigned char *parg)
- *
- *描述:设置wifi命令处理
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int SN_action(unsigned char cmd, unsigned char *parg)
- {
-
- parg[15] = 0;
- strcpy((char *)sysinf.SN, (const char *)parg);
- // PackDeviceNum();
- return cfg_save_cfg_param();
- }
- /********************************************************************
- * int default_pc_response(struct CmdMessage *pMsg, unsigned char result)
- *
- *描述:默认回应PC
- *参数:无
- *返回:成功->0 失败->-1
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int default_pc_response(CmdMessage *pMsg, unsigned char result)
- {
- return data_send_to_pc((pc_cmd_type)pMsg->cmd, (char *)&result, 1);
- }
- /********************************************************************
- * int handshake_response(struct CmdMessage *pMsg, unsigned char result)
- *
- *描述:握手回应
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int handshake_response(CmdMessage *pMsg, unsigned char result)
- {
- unsigned short len = 0;
- PackDeviceNum();
- len = strlen(DeviceNum);
- return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)DeviceNum, len,pMsg->seq);
- }
- int set_response(CmdMessage *pMsg, unsigned char result)
- {
- return data_send_to_pc((pc_cmd_type)pMsg->cmd, (char*)&result, 1);
- return 0;
- }
- /********************************************************************
- * int set_IP_action(uint8_t cmd, uint8_t *parg)
- *
- *描述:设置IP
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int set_IP_action(uint8_t cmd, uint8_t *parg)
- {
- if(sysinf.connectTcpFlag == 0){
- /*TCP 链接状态下,不跟新IP地址*/
- memset(sysinf.TCP_IP_Addr,0,sizeof(sysinf.TCP_IP_Addr));
- memset(sysinf.TCP_IP_PORT,0,sizeof(sysinf.TCP_IP_PORT));
- unsigned int data_len = strlen((const char *)parg);
- unsigned int i=0;
- if(parg[i]<'0' || parg[i]>'9'){
- return 0;
- }
- for(;i<data_len;i++){
- if(parg[i] != ':'){
- sysinf.TCP_IP_Addr[i] = parg[i];
- }else{
- break;
- }
- }
- i++;
- unsigned int j =0;
- if(parg[i]<'0' || parg[i]>'9'){
- return 0;
- }
- for(;i<data_len;i++){
- if(parg[i] != ':'){
- sysinf.TCP_IP_PORT[j++] = parg[i];
- }else{
- break;
- }
- }
- if(sysinf.TCP_IP_Addr[0]){
- sysinf.connectTcpFlag = 1;
- }
-
- WIFI_set_CloseConnect();
- }
- return 0;
- }
- /********************************************************************
- * int updateStart_action(struct CmdMessage *pMsg, unsigned char result)
- *
- *描述:备份APP1到APP2区域
- *参数:无
- *返回:备份成功失败
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- int updateStart_action(uint8_t cmd, uint8_t *parg)
- {
- static int restult = 0;
- if(restult==0){
- restult = app_image_copy(FLASH_APP1_ADDR,FLASH_APP2_ADDR,FLASH_PAGE_SIZE,FLASH_APP2_SIZE);
- }
- return restult;
- }
- int updateInfo_action(uint8_t cmd, uint8_t *parg)
- {
- updateHeader *upHead = (updateHeader *)parg;
- updateInf.allchecksum = upHead->allchecksum;
- updateInf.appLen = upHead->appLen;
- updateInf.allpackage = upHead->allpackage;
- updateInf.packageSize = upHead->packageSize;
- if(FLASH_PAGE_SIZE%upHead->packageSize){
- /*包大小演出*/
- return 0;
- }
- updateInf.packageInPage = FLASH_PAGE_SIZE/upHead->packageSize;
- updateInf.writeAddr = FLASH_APP1_ADDR;
- updateInf.lenInPage = 0;
- updateInf.updateState = FLASH_UPDATE_ST_REC;
- updateInf.nextpackageOrder = 0;
- light_set(MODE_BREATH, 1000, COLOR_YELLOW, 0);
- light_set(MODE_BREATH, 1000, COLOR_YELLOW, 1);
- return 1;
- }
- int updateDate_action(uint8_t cmd, uint8_t *parg)
- {
- Refresh_IWDG();
- updateData *upHead = (updateData *)parg;
- unsigned int posInPage;
- if(upHead->packageOrder !=updateInf.nextpackageOrder){
- /*序号不对,返回错误*/
- return 0;
- }
- if(updateInf.updateState != FLASH_UPDATE_ST_REC){
- /*状态不对,返回错误*/
- return 0;
- }
- posInPage = upHead->packageOrder%(updateInf.packageInPage);
- memcpy(&updateInf.data[posInPage*updateInf.packageSize], (uint8_t*)upHead->data, upHead->packageSize);
- updateInf.nextpackageOrder++;
- updateInf.lenInPage += upHead->packageSize;
- if((updateInf.nextpackageOrder%updateInf.packageInPage==0) || (updateInf.nextpackageOrder==updateInf.allpackage)){
- /*当前页收完,或包收完*/
- iap_write_apppage(updateInf.writeAddr,updateInf.data,updateInf.lenInPage);
- updateInf.writeAddr = updateInf.writeAddr+updateInf.lenInPage;
- updateInf.lenInPage = 0;
- }
-
- if(updateInf.nextpackageOrder==updateInf.allpackage){
- /*稍后添加check sum */
- Flash_ErasePage(FLASH_APP1_ADDR + FLASH_APP_VERSION_PAGE);
- updateInf.updateState = FLASH_UPDATE_ST_PROG;
- }
- updateInf.updateWaitTime = 0;
- return 1;
- }
- int updatestart_respone(CmdMessage *pMsg, unsigned char result)
- {
- unsigned short len = 0;
- PackDeviceNum();
- len = strlen(DeviceNum);
- return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)DeviceNum, len,pMsg->seq);
- }
- int updateInfo_response(CmdMessage *pMsg, unsigned char result)
- {
- return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, (char*)&result, 1,pMsg->seq);
- }
- int updateDate_response(CmdMessage *pMsg, unsigned char result)
- {
- unsigned char *data = (unsigned char *)pMsg->data;
- *(unsigned int *)(data+4) = updateInf.nextpackageOrder;
- *(unsigned int *)(data+8) = result;
- return data_send_to_pc_withindex((pc_cmd_type)pMsg->cmd, pMsg->data, 12,pMsg->seq);
- }
- int in_update_process(void)
- {
- updateInf.updateWaitTime++;
- if(updateInf.updateState == FLASH_UPDATE_ST_REC) {
- if(updateInf.updateWaitTime > 10){
- if(updateInf.nextpackageOrder > updateInf.packageInPage){
- /*接收数据超过1页,已经缓存,擦除标记,进行回退*/
- Flash_ErasePage(FLASH_APP1_ADDR + FLASH_APP_VERSION_PAGE);
- }
- updateInf.updateState = FLASH_UPDATE_ST_NUILL;
- return 0;
- }
- return 1;
- }
- return 0;
- }
- int update_process_end(void)
- {
- if(updateInf.updateState == FLASH_UPDATE_ST_PROG) return 1;
- return 0;
- }
- const struct PCActionPack mPCActionPack[] =
- {
- {
- .cmd = PC_CMD_HANDSHAKE,
- .action = handshake_action,
- .response = handshake_response,
- .datalen = 0,
- },
- {
- .cmd = PC_CMD_SETWIFI,
- .action = wifi_action,
- .response = set_response,
- .datalen = 0xff,
- },
- {
- .cmd = PC_CMD_SETSN,
- .action = SN_action,
- .response = handshake_response,
- .datalen = 7,
- },
- {
- .cmd = PC_CMD_STARTUPDATE,
- .action = updateStart_action,
- .response = updatestart_respone,
- .datalen = 0xff,
- },
- {
- .cmd = PC_CMD_UPDATEINFO,
- .action = updateInfo_action,
- .response = updateInfo_response,
- .datalen = 0xff,
- },
- {
- .cmd = PC_CMD_UPDATEDATA,
- .action = updateDate_action,
- .response = updateDate_response,
- .datalen = 0xff,
- },
- };
- void pc_resend_del(uint8_t seq){
- // struct CmdMessage *pMessage = (struct CmdMessage *)wifi_tx_buf;
- // if(pMessage->seq == seq){
- // wifi_tx_len = 0;
- // memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
- // }
- }
- /********************************************************************
- * void query_pc_cmd(struct CmdMessage *pMsg)
- *
- *描述:处理PC命令
- *参数:无
- *返回:无
- *其他:无
- *--------------------------------------------------------------------
- *记录:
- ********************************************************************/
- void query_pc_cmd(CmdMessage *pMsg)
- {
- for (unsigned char i = 0; i < sizeof(mPCActionPack)/sizeof(mPCActionPack[0]); i++)
- {
- if (mPCActionPack[i].cmd == pMsg->cmd && (mPCActionPack[i].datalen == pMsg->len || mPCActionPack[i].datalen == 0xff)) //防止接收到其他设备的数据帧
- {
- if(pc_send_cmd == mPCActionPack[i].cmd)
- pc_send_wait = 0;
- if(mPCActionPack[i].cmd != 1)
- {
- pc_resend_del(pMsg->seq);
- Van_Device_Printf(VAN_LOG_INFO,"\n<-- pc cmd %x %d \n", pMsg->cmd, pMsg->seq);
- }
- pMsg->data[pMsg->len] = 0;
- unsigned char ret = mPCActionPack[i].action(mPCActionPack[i].cmd, (unsigned char *)pMsg->data);
- if (mPCActionPack[i].response)
- {
- mPCActionPack[i].response(pMsg, ret);
- }
-
- break;
- }
- }
- }
|