Files
checker_host/prot/prot_cmdline.cpp
ranchuan d1e617afd3 实现赋码仪命令
升级小板程序失败时停止
2023-12-21 18:51:58 +08:00

204 lines
4.5 KiB
C++

#include "prot_cmdline.h"
#include "base/mycfg.h"
#include "QNetworkConfigurationManager"
#include "QTimer"
prot_cmdline_cb cmdline_find(myarray name)
{
extern const int __start_cmdldef;
extern const int __stop_cmdldef;
cmdline_def *start = (cmdline_def *)&__start_cmdldef;
cmdline_def *end = (cmdline_def *)&__stop_cmdldef;
cmdline_def *item = 0;
for (item = start; item < end; item++)
{
if (item != nullptr)
{
if (name == item->cmd)
return item->fun;
// qDebug("if dev: %s",item->name);
}
}
qWarning("can not find cmdline named '%s'", name.data());
return nullptr;
}
// 获取本机地址
static QString get_local_ip()
{
QList<QNetworkInterface> network = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface net, network)
{
QString netName = net.humanReadableName();
if (netName == "eth0")
{
QList<QNetworkAddressEntry> list = net.addressEntries(); // 获取IP地址与子掩码等
foreach (QNetworkAddressEntry address, list)
{
if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) // 获取IPv4的地址
{
return address.ip().toString();
}
}
}
}
return QString();
}
bool CheckNetInfo()
{
QNetworkInterface net= QNetworkInterface::interfaceFromName("eth0");
if(net.flags().testFlag(QNetworkInterface::IsUp)
&& net.flags().testFlag(QNetworkInterface::IsRunning)
&& !net.flags().testFlag(QNetworkInterface::IsLoopBack)
&& (net.name() == "eth0"))
{
// qDebug() << "eth0 is Connected" << endl;
return true;
}
else
{
// qDebug() << "eth0 is Not Connected";
return false;
}
}
void command::init_cb()
{
mycfg *cfg_ = syscfg();
if (socket_ == nullptr)
{
socket_ = new QUdpSocket(this);
socket_->bind(QHostAddress::Any, cfg_->cmd_port);
connect(socket_, &QUdpSocket::readyRead, this, &command::ready_read_cb);
qInfo() << "udp command start.";
qInfo() << "cmd port=" << cfg_->cmd_port;
}
if(timer_==nullptr)
{
timer_=new QTimer();
connect(timer_,&QTimer::timeout,this,&command::timer_cb);
timer_->start(3000);
}
}
void command::timer_cb()
{
mycfg *cfg_ = syscfg();
// qDebug("command timer cb.");
if(CheckNetInfo()==true){
if(networt_state==0){
qDebug("network connected.");
networt_state=1;
}
if (get_local_ip().isEmpty())
{
qDebug()<<"modify local ip addr."<<endl;
QString str="ifconfig eth0 %1";
str=str.arg(cfg_->local_ip);
system(str.toLocal8Bit().data());
qDebug()<<str<<endl;
str="route add default gw %1";
str=str.arg(cfg_->gateway_ip);
system(str.toLocal8Bit().data());
qDebug()<<str<<endl;
}
}else{
if(networt_state!=0){
qDebug("network disconnected.");
networt_state=0;
}
}
}
void command::send(QByteArray data)
{
if (socket_ == nullptr)
return;
if (data.back() != '\n')
{
data.append('\n');
}
socket_->writeDatagram(data, host_addr, port);
}
static void cmdline_help(QList<myarray> args);
void command::ready_read_cb()
{
recv_data.resize(socket_->bytesAvailable());
socket_->readDatagram(recv_data.data(), recv_data.size(), &host_addr, &port);
myarray cmd = recv_data.simplified();
QList<myarray> args=cmd.split(' ');
if(args.size()>0){
prot_cmdline_cb fun=cmdline_find(args[0]);
if(fun!=nullptr){
fun(args);
}else{
mystring str;
str=mystring::asprintf("can not find command: %s",cmd.data());
send(str.data());
cmdline_help(args);
}
}
qInfo() << "udp command." << cmd;
recv_data.clear();
}
static void cmdline_help(QList<myarray> args)
{
command *c=command_start();
extern const int __start_cmdldef;
extern const int __stop_cmdldef;
cmdline_def *start = (cmdline_def *)&__start_cmdldef;
cmdline_def *end = (cmdline_def *)&__stop_cmdldef;
cmdline_def *item = 0;
for (item = start; item < end; item++)
{
if (item != nullptr)
{
QByteArray send_data;
send_data.append(item->cmd);
send_data.append(30-send_data.size(),'.');
send_data.append(item->help);
c->send(send_data);
}
}
}
cmdline_export(help,cmdline_help,print help information.);
static command *g_command;
static QThread g_thread;
command *command_start()
{
if (g_command == nullptr)
{
g_command = new command();
g_command->moveToThread(&g_thread);
QTimer::singleShot(0, g_command, &command::init_cb);
g_thread.start();
}
return g_command;
}