621 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			621 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "cmd_slave.h"
 | |
| #include "base/mycfg.h"
 | |
| #include "base/check_cfg.h"
 | |
| #include "QDebug"
 | |
| #include "base/crc.h"
 | |
| #include "base/beep.h"
 | |
| 
 | |
| 
 | |
| 
 | |
| int slave_check::start(myarray data)
 | |
| {
 | |
|   mycfg *cfg_=syscfg();
 | |
|   check_cfg *ccfg_=check_plan();
 | |
|   int timeout=ccfg_->get_check_time_out()*1000;
 | |
|   busy=1;
 | |
|   cmd=0x0c;
 | |
|   qDebug("addr %d start check,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,myarray(),3);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| void slave_check::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d check timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_check::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   timeout_stop_retry();
 | |
|   if(cmd==0x0c){
 | |
|     if(data.size()<=16){
 | |
|       qDebug("addr %d recv first ack.",addr);
 | |
|     }else{
 | |
|       qDebug("addr %d recv secend ack.",addr);
 | |
|       send_data(0x14,myarray(),1);
 | |
|       end(0,data);
 | |
|       busy=0;
 | |
|       timeout_stop();
 | |
|     }
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| int slave_check2::start(myarray data)
 | |
| {
 | |
|   mycfg *cfg_=syscfg();
 | |
|   check_cfg *ccfg_=check_plan();
 | |
|   int timeout=ccfg_->get_check_time_out()*1000;
 | |
|   busy=1;
 | |
|   cmd=0x0c;
 | |
|   qDebug("addr %d start check,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,myarray(),3);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| void slave_check2::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d check timeout.",addr);
 | |
|   end(1,ret_data);
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_check2::dolater(int cmd, myarray data)
 | |
| {
 | |
|   this->addr_response=0;
 | |
|   timeout_stop_retry();
 | |
|   if(cmd==0x0c){
 | |
|     if(data.size()<=16){
 | |
|       qDebug("addr %d recv first ack.",addr);
 | |
|     }else{
 | |
|       qDebug("addr %d recv secend ack.",addr);
 | |
|       send_data(0x14,myarray(),5);
 | |
|       end(0,ret_data);
 | |
|       timeout_stop();
 | |
|     }
 | |
|   }else if(cmd==0x14){
 | |
|     // 所有从机都 停止上报时取消忙状态
 | |
|     busy=0;
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void slave_result::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d result timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_result::start(myarray data)
 | |
| {
 | |
|   int timeout=2000;
 | |
|   busy=1;
 | |
|   cmd=0x02;
 | |
|   qDebug("addr %d read result,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,myarray(),10);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int slave_result::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   timeout_stop_retry();
 | |
|   end(0,data);
 | |
|   busy=0;
 | |
|   timeout_stop();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void slave_sethardver::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d sethardver timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_sethardver::start(myarray data)
 | |
| {
 | |
|   int timeout=2000;
 | |
|   busy=1;
 | |
|   cmd=0x03;
 | |
|   qDebug("addr %d read sethardver,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,data,10);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int slave_sethardver::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   timeout_stop_retry();
 | |
|   end(0,data);
 | |
|   busy=0;
 | |
|   timeout_stop();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void slave_setresver::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d setresver timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_setresver::start(myarray data)
 | |
| {
 | |
|   int timeout=2000;
 | |
|   busy=1;
 | |
|   cmd=0x04;
 | |
|   qDebug("addr %d read setresver,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,data,10);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int slave_setresver::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   timeout_stop_retry();
 | |
|   end(0,data);
 | |
|   busy=0;
 | |
|   timeout_stop();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void slave_resvalue::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d resvalue timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_resvalue::start(myarray data)
 | |
| {
 | |
|   int timeout=2000;
 | |
|   busy=1;
 | |
|   cmd=0x05;
 | |
|   qDebug("addr %d read resvalue,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,data,10);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int slave_resvalue::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   timeout_stop_retry();
 | |
|   end(0,data);
 | |
|   busy=0;
 | |
|   timeout_stop();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| // 定义地址宏
 | |
| // 本程序中程序下载地址不是这个地址,在此用来计算偏移量
 | |
| #define MCU_APP_ADDR_BASE       0x8004000
 | |
| #define MCU_TASKID_ADDR_BASE    0x803f000
 | |
| #define MCU_JWT_ADDR_BASE       0x803B000
 | |
| 
 | |
| 
 | |
| 
 | |
| void boardcast_updata::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("slave updata timeout.");
 | |
|   end(1,ret_data);
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int boardcast_updata::start(myarray data)
 | |
| {
 | |
|   int timeout=5000;
 | |
|   busy=1;
 | |
|   qWarning()<<"addr "<<addr<<" updata start.";
 | |
|   timeout_start(timeout);
 | |
|   // 擦除
 | |
|   step=0;
 | |
|   this->data=data;
 | |
|   send_data(0xfe,myarray(),10);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int boardcast_updata::dolater(int cmd, myarray data)
 | |
| {
 | |
|   for(int i=0;i<ret_data.size();i++){
 | |
|     if(ret_data[i][0]!=0){
 | |
|       timeout_stop();
 | |
|       timeout_stop_retry();
 | |
|       qDebug("slave updata failed.");
 | |
|       end(1,ret_data);
 | |
|       busy=0;
 | |
|       return -1;
 | |
|     }
 | |
|   }
 | |
|   timeout_start(5000);
 | |
|   if(step==0){
 | |
|     if(send_packet()==true){
 | |
|       step=1;
 | |
|     }
 | |
|   }else if(step==1){
 | |
|     uint32_t addr_start=MCU_APP_ADDR_BASE;
 | |
|     uint32_t addr_end=MCU_APP_ADDR_BASE+count_sent;
 | |
|     uint32_t crc32=crc::crc32(this->data);
 | |
|     myarray s;
 | |
|     s.append((addr_start)&0xff);
 | |
|     s.append((addr_start>>8)&0xff);
 | |
|     s.append((addr_start>>16)&0xff);
 | |
|     s.append((addr_start>>24)&0xff);
 | |
|     s.append((addr_end)&0xff);
 | |
|     s.append((addr_end>>8)&0xff);
 | |
|     s.append((addr_end>>16)&0xff);
 | |
|     s.append((addr_end>>24)&0xff);
 | |
|     s.append((crc32)&0xff);
 | |
|     s.append((crc32>>8)&0xff);
 | |
|     s.append((crc32>>16)&0xff);
 | |
|     s.append((crc32>>24)&0xff);
 | |
|     send_data(0xfb,s,10);
 | |
|     step=2;
 | |
|   }else if(step==2){
 | |
|     timeout_stop();
 | |
|     timeout_stop_retry();
 | |
|     qDebug("slave updata end.");
 | |
|     end(0,ret_data);
 | |
|     busy=0;
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| bool boardcast_updata::send_packet()
 | |
| {
 | |
|   if(count_sent<data.size()){
 | |
|     uint32_t addr=MCU_APP_ADDR_BASE+count_sent;
 | |
|     int pack_size=236;
 | |
|     myarray s;
 | |
|     if(pack_size>data.size()-count_sent){
 | |
|       pack_size=data.size()-count_sent;
 | |
|     }
 | |
|     s.append((addr)&0xff);
 | |
|     s.append((addr>>8)&0xff);
 | |
|     s.append((addr>>16)&0xff);
 | |
|     s.append((addr>>24)&0xff);
 | |
|     s+=data.mid(count_sent,pack_size);
 | |
|     count_sent+=pack_size;
 | |
|     send_data(0xfc,s,10);
 | |
|     rate(count_sent*100/data.size(),"slave_updata");
 | |
|   }
 | |
|   if(count_sent<data.size()){
 | |
|     return false;
 | |
|   }else{
 | |
|     return true;
 | |
|   }
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void boardcast_updata_scheme::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("slave updata timeout.");
 | |
|   end(1,ret_data);
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int boardcast_updata_scheme::start(myarray data)
 | |
| {
 | |
|   int timeout=5000;
 | |
|   busy=1;
 | |
|   base_addr=MCU_TASKID_ADDR_BASE;
 | |
|   qWarning()<<"addr "<<addr<<" updata start.";
 | |
|   timeout_start(timeout);
 | |
|   // 擦除
 | |
|   step=0;
 | |
|   this->data=data;
 | |
|   send_packet();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| int boardcast_updata_scheme::dolater(int cmd, myarray data)
 | |
| {
 | |
|   for(int i=0;i<ret_data.size();i++){
 | |
|     if(ret_data[i][0]!=0){
 | |
|       timeout_stop();
 | |
|       timeout_stop_retry();
 | |
|       qDebug("slave updata scheme failed.");
 | |
|       end(1,ret_data);
 | |
|       busy=0;
 | |
|       return -1;
 | |
|     }
 | |
|   }
 | |
|   timeout_start(5000);
 | |
|   if(step==0){
 | |
|     if(send_packet()==true){
 | |
|       step=1;
 | |
|     }
 | |
|   }else if(step==1){
 | |
|     send_data(0x15,myarray(),10);
 | |
|     step=2;
 | |
|   }else if(step==2){
 | |
|     timeout_stop();
 | |
|     timeout_stop_retry();
 | |
|     qDebug("slave updata scheme end.");
 | |
|     end(0,ret_data);
 | |
|     busy=0;
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| bool boardcast_updata_scheme::send_packet()
 | |
| {
 | |
|   if(count_sent<data.size()){
 | |
|     uint32_t addr=base_addr+count_sent;
 | |
|     int pack_size=236;
 | |
|     myarray s;
 | |
|     if(pack_size>data.size()-count_sent){
 | |
|       pack_size=data.size()-count_sent;
 | |
|     }
 | |
|     s.append((addr)&0xff);
 | |
|     s.append((addr>>8)&0xff);
 | |
|     s.append((addr>>16)&0xff);
 | |
|     s.append((addr>>24)&0xff);
 | |
|     s+=data.mid(count_sent,pack_size);
 | |
|     count_sent+=pack_size;
 | |
|     send_data(0x11,s,10);
 | |
|     rate(count_sent*100/data.size(),"slave_updata_scheme");
 | |
|   }
 | |
|   if(count_sent<data.size()){
 | |
|     return false;
 | |
|   }else{
 | |
|     return true;
 | |
|   }
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| int boardcast_updata_jwt::start(myarray data)
 | |
| {
 | |
|   int timeout=5000;
 | |
|   busy=1;
 | |
|   base_addr=MCU_JWT_ADDR_BASE;
 | |
|   qWarning()<<"addr "<<addr<<" updata start.";
 | |
|   timeout_start(timeout);
 | |
|   // 擦除
 | |
|   step=0;
 | |
|   this->data=data;
 | |
|   send_packet();
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| // 获取自检信息
 | |
| int slave_bootinfo::start(myarray data)
 | |
| {
 | |
|   mycfg *cfg_=syscfg();
 | |
|   int timeout=5000;
 | |
|   busy=1;
 | |
|   cmd=0x13;
 | |
|   qDebug("addr %d get bootinfo,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,myarray(),3);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| void slave_bootinfo::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   qWarning("addr %d get bootinfo timeout.",addr);
 | |
|   end(1,myarray());
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_bootinfo::dolater(int cmd, myarray data)
 | |
| {
 | |
|   if(cmd!=this->cmd){
 | |
|     qWarning("addr %d recv err cmd:%02x",addr,cmd);
 | |
|     return 1;
 | |
|   }
 | |
|   qWarning("addr %d get bootinfo success.",addr);
 | |
|   timeout_stop();
 | |
|   timeout_stop_retry();
 | |
|   end(0,data);
 | |
|   busy=0;
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| int slave_cmd::start(myarray data)
 | |
| {
 | |
|   int timeout=5000;
 | |
|   busy=1;
 | |
|   if(data.size()==0){
 | |
|     qWarning("data size is 0.");
 | |
|     busy=0;
 | |
|     end(0,ret_data);
 | |
|     return 0;
 | |
|   }
 | |
|   step=0;
 | |
|   // 执行模式不为3时使用方案中的超时时间
 | |
|   if(data[0]!=3){
 | |
|     timeout=check_plan()->get_check_time_out()*1000;  
 | |
|   }
 | |
|   // 这里addr是bit数,用16进制打印
 | |
|   qDebug("addr 0x%06X start ,timeout=%d",addr,timeout);
 | |
|   this->data=data;
 | |
|   send_pack_num=0;
 | |
|   send_bytes=0;
 | |
|   send_next();
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| // 发送下一包,返回true完成
 | |
| bool slave_cmd::send_next()
 | |
| {
 | |
|   int pack_size=236;
 | |
|   int data_size=data.size();
 | |
|   if(send_bytes<data_size){
 | |
|     if(pack_size>data_size-send_bytes){
 | |
|       pack_size=data_size-send_bytes;
 | |
|     }
 | |
|     myarray d;
 | |
|     d.append(uint8_t(data_size&0xff));
 | |
|     d.append(uint8_t((data_size>>8)&0xff));
 | |
|     d.append(uint8_t((send_pack_num)&0xff));
 | |
|     d.append(uint8_t(pack_size&0xff));
 | |
|     d.append(data.mid(send_bytes,pack_size));
 | |
|     send_data(cmd,d,3);
 | |
|     send_bytes+=pack_size;
 | |
|     send_pack_num++;
 | |
|     return false;
 | |
|   }else{
 | |
|     return true;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void slave_cmd::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   int addr_timeout=addr^addr_response;
 | |
|   qWarning("addr 0x%06X timeout",addr_timeout);
 | |
|   Beep()->set_beep(300,300,1);
 | |
|   end(1,ret_data);
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_cmd::dolater(int cmd, myarray data)
 | |
| {
 | |
|   this->addr_response=0;
 | |
|   timeout_stop_retry();
 | |
|   if(cmd==this->cmd){
 | |
|     if(send_next()==true){
 | |
|       if(step==0){
 | |
|         qDebug("cmd send success.");
 | |
|         step=1;
 | |
|         addr_response=0;
 | |
|         ret_data.clear();
 | |
|       }else{
 | |
|         qDebug("cmd end success.");
 | |
|         busy=0;
 | |
|         end(0,ret_data);
 | |
|         timeout_stop();
 | |
|       }
 | |
|     }
 | |
|   }else{
 | |
|     qWarning("cmd:%02x err.",cmd);
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| int slave_cmd2::start(myarray data)
 | |
| {
 | |
|   int timeout=3500;
 | |
|   busy=1;
 | |
|   // 这里addr是bit数,用16进制打印
 | |
|   qDebug("addr 0x%06X start ,timeout=%d",addr,timeout);
 | |
|   send_data(cmd,data,3);
 | |
|   timeout_start(timeout);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| void slave_cmd2::timeout()
 | |
| {
 | |
|   timeout_stop();
 | |
|   int addr_timeout=addr^addr_response;
 | |
|   qWarning("addr 0x%06X timeout",addr_timeout);
 | |
|   end(1,ret_data);
 | |
|   busy=0;
 | |
| }
 | |
| 
 | |
| int slave_cmd2::dolater(int cmd, myarray data)
 | |
| {
 | |
|   this->addr_response=0;
 | |
|   timeout_stop_retry();
 | |
|   if(cmd==this->cmd){
 | |
|     qDebug("cmd end success.");
 | |
|     busy=0;
 | |
|     end(0,ret_data);
 | |
|     timeout_stop();
 | |
|   }else{
 | |
|     qWarning("cmd:%02x err.",cmd);
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | 
