431 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			431 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "prot/prot_cmdline.h"
 | |
| #include "base/mycfg.h"
 | |
| #include "QNetworkConfigurationManager"
 | |
| #include "QTimer"
 | |
| #include "QtNetwork"
 | |
| #include "prot/prot_slave.h"
 | |
| #include "prot_cmd/cmd_slave.h"
 | |
| #include "base/check_cfg.h"
 | |
| #include "QFile"
 | |
| #include "base/base.h"
 | |
| #include "cmd_cmdline.h"
 | |
| #include "interface/if_can.h"
 | |
| #include "elec/mystring.h"
 | |
| #include "prot/prot_pc.h"
 | |
| #include "elec/elec_judge.h"
 | |
| 
 | |
| 
 | |
| 
 | |
| /*
 | |
| 
 | |
| 命令行返回有三种
 | |
| data:[str]
 | |
| rate:[rate%],[str]
 | |
| end:[1:ok/0:failed],[str]
 | |
| 
 | |
| */
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void rate_slot(int rate,mystring str)
 | |
| {
 | |
|   command *c=command_start();
 | |
|   mystring strs("rate:%1,%2");
 | |
|   strs=strs.arg(rate).arg(str);
 | |
|   c->send(strs.data());
 | |
| }
 | |
| void end_slot(int addr,int ack,slave_data data)
 | |
| {
 | |
|   // ack 0是成功,这里转化为1是成功
 | |
|   command *c=command_start();
 | |
|   mystring strs("ack:%1,%2");
 | |
|   strs=strs.arg(ack).arg("end");
 | |
|   c->send(strs.data());
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| // 获取设备基本信息
 | |
| static void whos(QList<myarray> args)
 | |
| {
 | |
|   QString out;
 | |
|   QString name = "local_id:%1,name:%2";
 | |
|   QString hostName = QHostInfo::localHostName();
 | |
|   command *c=command_start();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   name = name.arg(cfg_->local_id).arg(hostName);
 | |
|   out.append(name + "\n");
 | |
|   out.append("builed time:" + cfg_->build_date + "\n");
 | |
|   QList<QNetworkInterface> network = QNetworkInterface::allInterfaces();
 | |
|   foreach (QNetworkInterface net, network)
 | |
|   {
 | |
|     QString str = "net:%1";
 | |
|     QString netName = net.humanReadableName();
 | |
|     str = str.arg(netName);
 | |
|     out.append(str + "\n");
 | |
|     QList<QNetworkAddressEntry> list = net.addressEntries(); // 获取IP地址与子掩码等
 | |
|     foreach (QNetworkAddressEntry address, list)
 | |
|     {
 | |
|       QString str = "ip:%1";
 | |
|       if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) // 获取IPv4的地址
 | |
|       {
 | |
|         out.append(str.arg(address.ip().toString()) + "\n");
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   out = "ack:1," + out;
 | |
|   c->send(out.toLocal8Bit());
 | |
| }
 | |
| 
 | |
| cmdline_export(whos, whos, print device base info.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| static void mcu_cmdlines_help(){
 | |
|   command *c=command_start();
 | |
|   c->send("mcu updata [addrs] [file_name] to updata slave app,for example:\n"
 | |
|           "\tmcu updata 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 /home/root/config/checker_slave_app.pkt");
 | |
|   c->send("mcu upjwt [addrs] [file_name] to updata jwt app,for example:\n"
 | |
|           "\tmcu upjwt 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 /home/root/config/jwt_app.jwt");
 | |
|   c->send("mcu scheme [addrs] to updata slave scheme,for example:\n"
 | |
|           "\tmcu scheme 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20");
 | |
| }
 | |
| // mcu 升级程序及方案等
 | |
| static void mcu_cmdlines(QList<myarray> args)
 | |
| {
 | |
|   command *c=command_start();
 | |
|   prot_slave *slave=protSlave();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   check_cfg *ccfg_=check_plan();
 | |
|   if(args.size()<2){
 | |
|     c->send("cmd len too less.");
 | |
|     mcu_cmdlines_help();
 | |
|     return ;
 | |
|   }
 | |
|   if(args.size()>=4){
 | |
|     QList<int> addrs=str_to_nums<int>(args[2],',');
 | |
|     qDebug()<<"addrs:"<<addrs;
 | |
|     // 升级小板程序
 | |
|     if(args[1]=="updata"){
 | |
|       HandleBoardCast *b=new boardcast_updata();
 | |
|       bool ack=slave->set_boardcast_handle(addrs,b);
 | |
|       if(ack==false){
 | |
|         qWarning("handle is busy.");
 | |
|       }else{
 | |
|         b->set_cbfun([=](int addr,int ack,slave_data data){
 | |
|           end_slot(addr,!ack,data);
 | |
|         },[=](int rate,mystring str){
 | |
|           rate_slot(rate,str);
 | |
|         });
 | |
|         QFile file;
 | |
|         file.setFileName(args[3]);
 | |
|         if(file.open(QIODevice::ReadOnly)){
 | |
|           myarray data=file.readAll();
 | |
|           b->start(data);
 | |
|           file.close();
 | |
|         }else{
 | |
|           qWarning("open file failed.");
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|     // 升级jwt程序
 | |
|     else if(args[1]=="upjwt"){
 | |
|       HandleBoardCast *b=new boardcast_updata_jwt();
 | |
|       bool ack=slave->set_boardcast_handle(addrs,b);
 | |
|       if(ack==false){
 | |
|         qWarning("handle is busy.");
 | |
|       }else{
 | |
|         b->set_cbfun([=](int addr,int ack,slave_data data){
 | |
|           end_slot(addr,!ack,data);
 | |
|         },[=](int rate,mystring str){
 | |
|           rate_slot(rate,str);
 | |
|         });
 | |
|         QFile file;
 | |
|         file.setFileName(args[3]);
 | |
|         if(file.open(QIODevice::ReadOnly)){
 | |
|           myarray data=file.readAll();
 | |
|           b->start(data);
 | |
|           file.close();
 | |
|         }else{
 | |
|           qWarning("open file failed.");
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   }else if(args.size()>=3){
 | |
|     QList<int> addrs=str_to_nums<int>(args[2],',');
 | |
|     qDebug()<<"addrs:"<<addrs;
 | |
|     // 升级方案
 | |
|     if(args[1]=="scheme"){
 | |
|       HandleBoardCast *b=new boardcast_updata_scheme();
 | |
|       bool ack=slave->set_boardcast_handle(addrs,b);
 | |
|       if(ack==false){
 | |
|         qWarning("handle is busy.");
 | |
|       }else{
 | |
|         b->set_cbfun([=](int addr,int ack,slave_data data){
 | |
|           end_slot(addr,!ack,data);
 | |
|         },[=](int rate,mystring str){
 | |
|           rate_slot(rate,str);
 | |
|         });
 | |
|         myarray data;
 | |
| //        if(cfg_->slave_scheme_ext==0){
 | |
| //            data=myarray((const char *)ccfg_->check_scheme()->slave_data,2048);
 | |
| //        }else{
 | |
|             data=myarray((const char *)ccfg_->check_scheme(),ccfg_->check_scheme_size());
 | |
| //        }
 | |
|         b->start(data);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| }
 | |
| cmdline_export(mcu, mcu_cmdlines, mcu cmdlines` input [mcu] for detail.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| static int g_slave_addr;
 | |
| static void mcu_tran_cmdlines(QList<myarray> args){
 | |
|   command *c=command_start();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   check_cfg *ccfg_=check_plan();
 | |
|   can_host *can_=static_cast<can_host *>(interFaceFind("can"));
 | |
|   if(args.size()<3){
 | |
|     c->send("cmd len too less.");
 | |
|     c->send("tran [addr] [cmdline],for example\n"
 | |
|             "\ttran 1 sysinfo");
 | |
|     return ;
 | |
|   }
 | |
|   QList<int> addrs=str_to_nums<int>(args[1],',');
 | |
|   qDebug()<<"addrs:"<<addrs;
 | |
|   myarray can_d;
 | |
|   for(int i=2;i<args.size();i++){
 | |
|     can_d.append(' ');
 | |
|     can_d.append(args[i]);
 | |
|   }
 | |
|   g_slave_addr=addrs[0];
 | |
|   can_->set_irq([=](int src,myarray data){
 | |
|     if(str_is_print_str(data.data(),data.size())!=1){
 | |
|       return;
 | |
|     }
 | |
|     if(g_slave_addr!=src){
 | |
|       mystring str="src=%1";
 | |
|       str=str.arg(src);
 | |
|       c->send(str.toLocal8Bit());
 | |
|     }
 | |
|     c->send(data);
 | |
|   });
 | |
|   can_->write(g_slave_addr,can_d);
 | |
| }
 | |
| cmdline_export(tran, mcu_tran_cmdlines, tran cmdline to slave.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| static void slave_info(QList<myarray> args){
 | |
|   command *c=command_start();
 | |
|   prot_slave *slave=protSlave();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   HandleBoardCast *b=new slave_cmd2();
 | |
|   bool ack=slave->set_boardcast_handle(cfg_->calc_slave_addrs(),b);
 | |
|   if(ack==false){
 | |
|     qWarning("handle is busy.");
 | |
|   }else{
 | |
|     b->set_cbfun([=](int addr,int ack,slave_data data){
 | |
|       myarray r=myarray("data:",5);
 | |
|       for(int i=0;i<cfg_->slave_num;i++){
 | |
|         myarray t=data[i];
 | |
|         if(t.size()>0){
 | |
|           r.append("ok,");
 | |
|         }else{
 | |
|           r.append("ng,");
 | |
|         }
 | |
|       }
 | |
|       c->send(r);
 | |
|     },nullptr);
 | |
|     b->cmd=0x13;
 | |
|     b->start(myarray());
 | |
|   }
 | |
| }
 | |
| cmdline_export(slave_info, slave_info, get slave communicat info.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| #define print_item(str,data){\
 | |
|   myarray r=str;\
 | |
|   r[r.size()-1]=':';\
 | |
|   r.append(data);\
 | |
|   c->send(r);\
 | |
| }
 | |
|   
 | |
| 
 | |
| static void cfginfo(QList<myarray> args){
 | |
|   command *c=command_start();
 | |
|   prot_slave *slave=protSlave();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   print_item("tcp_enable",cfg_->tcp_enable?"true":"false");
 | |
|   print_item("server_ip",cfg_->server_ip.toLocal8Bit());
 | |
|   print_item("server_port",mystring::number(cfg_->server_port).toLocal8Bit());
 | |
|   print_item("local_id",mystring::number(cfg_->local_id).toLocal8Bit());
 | |
|   print_item("can_bitrate",mystring::number(cfg_->can_bitrate).toLocal8Bit());
 | |
|   print_item("slave_num",mystring::number(cfg_->slave_num).toLocal8Bit());
 | |
|   print_item("moter_count",mystring::number(cfg_->moter_count).toLocal8Bit());
 | |
|   print_item("uart_bsp",mystring::number(cfg_->uart_bsp).toLocal8Bit());
 | |
|   print_item("coder_return_mode",mystring::number(cfg_->coder_return_mode).toLocal8Bit());
 | |
|   print_item("slave_addr_start",mystring::number(cfg_->slave_addr_start).toLocal8Bit());
 | |
|   print_item("slave_scheme_ext",mystring::number(cfg_->slave_scheme_ext).toLocal8Bit())
 | |
| }
 | |
| cmdline_export(cfginfo, cfginfo, get cfg info.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void cmd_line_slots::auto_test(){
 | |
|   ProtPc *pc=protPc();
 | |
|   check_cfg *ccfg_=check_plan();
 | |
|   if(syscfg()->auto_test>0){
 | |
|     if(syscfg()->device_type=="checker"){
 | |
|       pc->send_data_slot(0x37,myarray(1,char(0)));
 | |
|     }else{
 | |
|       int chip=elec_extract_chip(ccfg_->get_plan_id());
 | |
|       QByteArray r;
 | |
|       r.append(char(chip));
 | |
|       pc->send_data_slot(0x8b,r);
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| void cmd_line_slots::auto_start()
 | |
| {
 | |
|   command *c=command_start();
 | |
|   if(timer_==nullptr){
 | |
|     timer_=new QTimer();
 | |
|     connect(timer_,&QTimer::timeout, this, &cmd_line_slots::auto_test);
 | |
|   }
 | |
|   if(syscfg()->auto_test>0){
 | |
|     c->send("auto test start.");
 | |
|     timer_->start(syscfg()->auto_test*1000);
 | |
|   }else{
 | |
|     c->send("auto test stop.");
 | |
|     timer_->stop();
 | |
|   }
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| static cmd_line_slots *g_slot_=nullptr;
 | |
| 
 | |
| static void cfgset(QList<myarray> args){
 | |
|   command *c=command_start();
 | |
|   prot_slave *slave=protSlave();
 | |
|   mycfg *cfg_=syscfg();
 | |
|   if(args.size()<3){
 | |
|     if((args.size()<2)||(args[1]!="save")){
 | |
|       c->send("cmd len too less.");
 | |
|       c->send("cfgset [item] [params],for example\n"
 | |
|               "\tcfgset tcp_enable true");      
 | |
|     }else{
 | |
|       syscfg()->save();
 | |
|       syscfg()->restart();
 | |
|     }
 | |
|     return ;
 | |
|   }
 | |
|   if(args[1]=="tcp_enable"){
 | |
|     if(args[2]=="true"){
 | |
|       cfg_->tcp_enable=true;
 | |
|     }else if(args[2]=="false"){
 | |
|       cfg_->tcp_enable=false;
 | |
|     }
 | |
|     print_item("tcp_enable",cfg_->tcp_enable?"true":"false");
 | |
|   }else if(args[1]=="server_ip"){
 | |
|     cfg_->server_ip=mystring(args[2]);
 | |
|     print_item("server_ip",cfg_->server_ip.toLocal8Bit());
 | |
|   }else if(args[1]=="server_port"){
 | |
|     cfg_->server_port=args[2].toInt();
 | |
|     print_item("server_port",mystring::number(cfg_->server_port).toLocal8Bit());
 | |
|   }else if(args[1]=="local_id"){
 | |
|     cfg_->local_id=args[2].toInt();
 | |
|     print_item("local_id",mystring::number(cfg_->local_id).toLocal8Bit());
 | |
|   }else if(args[1]=="can_bitrate"){
 | |
|     cfg_->can_bitrate=args[2].toInt();
 | |
|     print_item("can_bitrate",mystring::number(cfg_->can_bitrate).toLocal8Bit());
 | |
|   }else if(args[1]=="slave_num"){
 | |
|     cfg_->slave_num=args[2].toInt();
 | |
|     print_item("slave_num",mystring::number(cfg_->slave_num).toLocal8Bit());
 | |
|   }else if(args[1]=="moter_count"){
 | |
|     cfg_->moter_count=args[2].toInt();
 | |
|     print_item("moter_count",mystring::number(cfg_->moter_count).toLocal8Bit());
 | |
|   }else if(args[1]=="uart_bsp"){
 | |
|     cfg_->uart_bsp=args[2].toInt();
 | |
|     print_item("uart_bsp",mystring::number(cfg_->uart_bsp).toLocal8Bit());
 | |
|   }else if(args[1]=="coder_return_mode"){
 | |
|     cfg_->coder_return_mode=args[2].toInt();
 | |
|     print_item("coder_return_mode",mystring::number(cfg_->coder_return_mode).toLocal8Bit());
 | |
|   }else if(args[1]=="slave_addr_start"){
 | |
|     cfg_->slave_addr_start=args[2].toInt();
 | |
|     print_item("slave_addr_start",mystring::number(cfg_->slave_addr_start).toLocal8Bit());
 | |
|   }else if(args[1]=="slave_scheme_ext"){
 | |
|     cfg_->slave_scheme_ext=args[2].toInt();
 | |
|     print_item("slave_scheme_ext",mystring::number(cfg_->slave_scheme_ext).toLocal8Bit())
 | |
|   }else if(args[1]=="auto_test"){
 | |
|     cfg_->auto_test=args[2].toInt();
 | |
|     print_item("auto_test",mystring::number(cfg_->auto_test).toLocal8Bit())
 | |
|     if(g_slot_==nullptr){
 | |
|       g_slot_=new cmd_line_slots();
 | |
|     }
 | |
|     if(g_slot_!=nullptr){
 | |
|       QTimer::singleShot(0, g_slot_, &cmd_line_slots::auto_start);
 | |
|     }
 | |
|   }
 | |
| }
 | |
| cmdline_export(cfgset, cfgset, set cfg info.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| static QList<int>sort(QList<int> in){
 | |
|   int temp;
 | |
|   int num=in.size();
 | |
|   for(int i=0;i<num;i++){
 | |
|     temp=in[i];
 | |
|     for(int j=i+1;j<num;j++){
 | |
|       if(in[i]>in[j]){
 | |
|         in[i]=in[j];
 | |
|         in[j]=temp;
 | |
|         temp=in[i];
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   return in;
 | |
| }
 | |
| 
 | |
| static void pclist(QList<myarray> args){
 | |
|   command *c=command_start();
 | |
|   ProtPc *pc=protPc();
 | |
|   QList<int> list=handlePcList();
 | |
|   mystring s;
 | |
|   list=sort(list);
 | |
|   for (int i=0;i<list.size();i++){
 | |
|     s.append(mystring::asprintf("0x%02x,",list[i]));
 | |
|   }
 | |
|   c->send(s.data());
 | |
| }
 | |
| cmdline_export(pclist, pclist, print pc cmd list.);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | 
