diff --git a/base/base.h b/base/base.h index eae4557..a9293ad 100644 --- a/base/base.h +++ b/base/base.h @@ -65,6 +65,14 @@ public: myarray mid(qsizetype index,qsizetype len=-1LL){ return myarray(QByteArray::mid(index,len)); } + QList split(char sep){ + QByteArrayList list=QByteArray::split(sep); + QList myarrlist; + foreach (QByteArray qb, list) { + myarrlist.append(myarray(qb)); + }; + return myarrlist; + } }; class mystring : public QString diff --git a/base/check_cfg.cpp b/base/check_cfg.cpp index 8034603..5818b61 100644 --- a/base/check_cfg.cpp +++ b/base/check_cfg.cpp @@ -4,7 +4,6 @@ #include "QDir" // 配置路径 -#define CFG_PATH mystring("/home/root/config/") #define CHECK_CFG_FILE_NAME cfg_->def_check_cfg check_range my_json::json_to_range(QJsonValue v) @@ -55,12 +54,6 @@ check_cfg::check_cfg() this->cfg_ = syscfg(); this->scheme_ = nullptr; - QDir path; - if (!path.exists(CFG_PATH)) - { - path.mkdir(CFG_PATH); - } - reload(); } @@ -89,7 +82,7 @@ void check_cfg::scheme_json_to_struct() scheme_->task_num = get_check_task_num(); scheme_->timeout_m = get_check_time_out(); scheme_->marerr_num = errs.size(); - qDebug("scheme,id=%d,task_num=%d",scheme_->plan_id,scheme_->task_num); + qDebug("scheme,id=%d,task_num=%d", scheme_->plan_id, scheme_->task_num); for (int i = 0; i < scheme_->task_num; i++) { scheme_task_def *st = &scheme_->task[i]; @@ -102,10 +95,10 @@ void check_cfg::scheme_json_to_struct() QList rangs = ct.get_ranges(); int rangs_num = rangs.size(); int err_num = ret_errs.size(); - qDebug("st,err,item_num=%d,taskid=%d,taskindex=%d",st->item_num,st->taskid,st->taskindex); + qDebug("st,err,item_num=%d,taskid=%d,taskindex=%d", st->item_num, st->taskid, st->taskindex); for (int i = 0; i < st->item_num; i++) { - qDebug("\ti=%d",i); + qDebug("\ti=%d", i); if (rangs_num > i) { st->range[i].err = 0; @@ -162,7 +155,7 @@ bool check_cfg::updata(QString jstring) json_doc = QJsonDocument::fromJson(jstring.toUtf8(), &err); if (err.error != QJsonParseError::NoError) { - qWarning("parse json failed:%s",err.errorString().toLocal8Bit().data()); + qWarning("parse json failed:%s", err.errorString().toLocal8Bit().data()); qWarning() << jstring; return false; } @@ -495,7 +488,7 @@ QList check_cfg::get_ch_merrcode() if (!ch_errcode.isEmpty()) { // 有子错误没找到主错误 - qWarning()<<"many suberr left.%d"< &l, uint8_t code) return true; } } - qDebug("can not find mcode,subcode=%d",code); + qDebug("can not find mcode,subcode=%d", code); return false; } diff --git a/base/debug.cpp b/base/debug.cpp new file mode 100644 index 0000000..d08a568 --- /dev/null +++ b/base/debug.cpp @@ -0,0 +1,114 @@ + +#include +#include +#include +#include +#include +#include +#include +#include "base/mycfg.h" +#include +#include +#include "QApplication" +#include "QMutex" + +#define FILE_PATH "/home/root/log/" +#define FILE_NAME "_log.txt" + +char *msgHead[] = + { + (char *)"Debug ", + (char *)"Warning ", + (char *)"Critical", + (char *)"Fatal ", + (char *)"Info "}; + +struct mydebug +{ + QFile *file; + QUdpSocket *socket_; + QByteArray send_data; + QHostAddress host_addr; + quint16 port; + bool active; + void (*send_data_fun)(QByteArray data); + QMutex mutex; + // QList type_filter; + // QList file_filter; + // QList line_filter; +}; + +static mydebug g_debug; + + +void print_to_console(QtMsgType type, const QMessageLogContext &context, const QString &msg); +void print_to_file(QtMsgType type, const QMessageLogContext &context, const QString &msg); + + +// 创建日志文件,名称用于创建日志文件 +void mydebug_init(QString name) +{ + qSetMessagePattern("[%{type}] %{time yyyy-MM-dd hh:mm:ss.zzz} %{function}:%{line} %{message}"); + QDir tempDir; + if (!tempDir.exists(FILE_PATH)) + { + tempDir.mkpath(FILE_PATH); + } + if (g_debug.file != nullptr) + { + delete g_debug.file; + } + + g_debug.file = new QFile(FILE_PATH + name + FILE_NAME); + qInfo() << "set log file as " << g_debug.file->fileName(); + if (g_debug.file->exists()) + { + if (g_debug.file->remove() != true) + qWarning() << "remove file " << g_debug.file->fileName() << "err"; + } + + if (!g_debug.file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) + { + return; + } + + qInstallMessageHandler(print_to_file); +} + +void mydebug_init() +{ + qSetMessagePattern("[%{type}] %{time yyyy-MM-dd hh:mm:ss.zzz} %{function}:%{line} %{message}"); + qInfo() << "set log as console."; + qInstallMessageHandler(print_to_console); +} + +void print_to_console(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + g_debug.mutex.lock(); + QByteArray localMsg = msg.toLocal8Bit(); + QString current_date_time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz"); + Q_UNUSED(current_date_time); + if(context.file!=0){ + fprintf(stderr, "%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), &context.file[20], context.line, localMsg.constData()); + }else{ + fprintf(stderr,"%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), context.file, context.line, localMsg.constData()); + } + g_debug.mutex.unlock(); +} + +void print_to_file(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + g_debug.mutex.lock(); + QByteArray localMsg = msg.toLocal8Bit(); + QString current_date_time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz"); + if (g_debug.file) + { + QTextStream tWrite(g_debug.file); + if(context.file!=0){ + tWrite << QString::asprintf("%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), &context.file[20], context.line, localMsg.constData()); + }else{ + tWrite << QString::asprintf("%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), context.file, context.line, localMsg.constData()); + } + } + g_debug.mutex.unlock(); +} diff --git a/base/debug.h b/base/debug.h new file mode 100644 index 0000000..68be3bf --- /dev/null +++ b/base/debug.h @@ -0,0 +1,18 @@ +#ifndef debug_h__ +#define debug_h__ + + +#include "QDebug" + + +void mydebug_init(QString name); + +void mydebug_init(); + + + + + +#endif + + diff --git a/base/mycfg.cpp b/base/mycfg.cpp index cd28652..54d64c0 100644 --- a/base/mycfg.cpp +++ b/base/mycfg.cpp @@ -168,13 +168,13 @@ void mycfg::to_class(mystring str) debug_port = j.value("debug_port").toInt(12345); cmd_port = j.value("cmd_port").toInt(7777); log_redirect = j.value("log_redirect").toString("console"); - def_check_cfg = j.value("def_check_cfg").toString("checker_ye_cfg.json"); - def_mcu_app = j.value("def_mcu_app").toString("slave_app.pkt"); + def_check_cfg = j.value("def_check_cfg").toString(CFG_PATH+"checker_ye_cfg.json"); + def_mcu_app = j.value("def_mcu_app").toString(CFG_PATH+"slave_app.pkt"); use_lua_judge = j.value("use_lua_judge").toBool(false); device_type = j.value("device_type").toString("checker"); moter_count = j.value("moter_count").toInt(20000); uart_bsp = j.value("uart_bsp").toInt(57600); - def_lua_judge = j.value("lua_judge").toString("judge.lua"); + def_lua_judge = j.value("lua_judge").toString(CFG_PATH+"judge.lua"); coder_return_mode = j.value("coder_return_mode").toInt(1); slave_addr_start = j.value("slave_addr_start").toInt(0); slave_scheme_ext = j.value("slave_scheme_ext").toInt(0); @@ -252,7 +252,7 @@ void mycfg::init_env() { if (!info.privates.contains(name)) { - qInfo() << "release file " << file.fileName() << endl; + qInfo() << "release file " << file.fileName(); file.open(QIODevice::ReadWrite); file.write(d); file.close(); @@ -296,7 +296,7 @@ void mycfg::init_env() if (md5_1 != md5_2) { if (file_dst.remove() != true) - qWarning() << "remove file " << file_dst.fileName() << "err" << endl; + qWarning() << "remove file " << file_dst.fileName() << "err"; } } } @@ -304,7 +304,7 @@ void mycfg::init_env() { if (!QFile::copy(file_src.fileName(), file_dst.fileName())) { - qWarning() << "copy app failed." << endl; + qWarning() << "copy app failed."; } } if (file_dst.fileName() != file_src.fileName()) @@ -349,7 +349,7 @@ void mycfg::save() if (file.exists()) { if (file.remove() != true) - qWarning() << "remove file " << file.fileName() << "err" << endl; + qWarning() << "remove file " << file.fileName() << "err"; } file.open(QIODevice::ReadWrite); QTextStream stream(&file); @@ -369,7 +369,7 @@ bool mycfg::save_file(mystring name, myarray data) { if (file.remove() != true) { - qWarning() << "remove file " << file.fileName() << "err" << endl; + qWarning() << "remove file " << file.fileName() << "err"; return false; } } diff --git a/checker_host.pro b/checker_host.pro index 992af16..dc629ce 100644 --- a/checker_host.pro +++ b/checker_host.pro @@ -23,6 +23,7 @@ SOURCES += \ base/base.cpp \ base/check_cfg.cpp \ base/crc.cpp \ + base/debug.cpp \ base/mycfg.cpp \ elec/elec_judge.cpp \ elec/mystring.cpp \ @@ -36,9 +37,11 @@ SOURCES += \ interface/interface.cpp \ main.cpp \ mainwindow.cpp \ + prot/prot_cmdline.cpp \ prot/prot_m4.cpp \ prot/prot_pc.cpp \ prot/prot_slave.cpp \ + prot_cmd/cmd_cmdline.cpp \ prot_cmd/cmd_pc.cpp \ prot_cmd/cmd_slave.cpp @@ -46,6 +49,7 @@ HEADERS += \ base/base.h \ base/check_cfg.h \ base/crc.h \ + base/debug.h \ base/file.h \ base/mycfg.h \ elec/elec_judge.h \ @@ -59,6 +63,7 @@ HEADERS += \ interface/if_uart.h \ interface/interface.h \ mainwindow.h \ + prot/prot_cmdline.h \ prot/prot_m4.h \ prot/prot_pc.h \ prot/prot_slave.h \ diff --git a/complier_info.h b/complier_info.h index 730b915..ec4a3dc 100644 --- a/complier_info.h +++ b/complier_info.h @@ -6,7 +6,7 @@ -#define BUILD_DATE "2023-11-29 15:29:42" +#define BUILD_DATE "2023-12-01 11:08:00" diff --git a/info.json b/info.json index 9223c59..f5d1d53 100644 --- a/info.json +++ b/info.json @@ -1,10 +1,10 @@ { - "build_date": "2023-11-29 15:29:42", - "hard_version": "MHPZ2_V1.00", - "private": [ - "info.json", - "json.lua", - "prints.lua" - ], - "soft_version": "V1.09" -} + "build_date": "2023-12-01 11:08:00", + "hard_version": "MHPZ2_V1.00", + "private": [ + "info.json", + "json.lua", + "prints.lua" + ], + "soft_version": "V2.00" +} \ No newline at end of file diff --git a/interface/if_can.cpp b/interface/if_can.cpp index 83fee1f..430c1f4 100644 --- a/interface/if_can.cpp +++ b/interface/if_can.cpp @@ -84,6 +84,12 @@ can_host::can_host(int bitrate) can_host::~can_host() { + if(can_ !=nullptr){ + delete can_; + } + if(timer_ !=nullptr){ + delete timer_; + } } // 用于返回指定地址的从机对象 diff --git a/interface/if_tcp.cpp b/interface/if_tcp.cpp index 8fa26f7..73112b3 100644 --- a/interface/if_tcp.cpp +++ b/interface/if_tcp.cpp @@ -17,6 +17,15 @@ if_tcp::if_tcp() if_tcp::~if_tcp() { + if(timer_reconnect_!=nullptr){ + delete timer_reconnect_; + } + if(tcp_socket_!=nullptr){ + delete tcp_socket_; + } + if(timer_recv_end_!=nullptr){ + delete timer_recv_end_; + } } void if_tcp::init() @@ -88,15 +97,14 @@ void if_tcp::reconnect_cb() } } // 设置为配置的ip地址 - // if(QHostAddress(get_local_ip())!=QHostAddress(local_ip)) - if (get_local_ip().isEmpty()) - { - qDebug("modify local ip addr."); - QString str = "ifconfig eth0 %1"; - system(str.arg(cfg_->local_ip).toLocal8Bit().data()); - str = "route add default gw %1"; - system(str.arg(cfg_->gateway_ip).toLocal8Bit().data()); - } + // if (get_local_ip().isEmpty()) + // { + // qDebug("modify local ip addr."); + // QString str = "ifconfig eth0 %1"; + // system(str.arg(cfg_->local_ip).toLocal8Bit().data()); + // str = "route add default gw %1"; + // system(str.arg(cfg_->gateway_ip).toLocal8Bit().data()); + // } } } diff --git a/interface/if_uart.cpp b/interface/if_uart.cpp index 1738367..e61515c 100644 --- a/interface/if_uart.cpp +++ b/interface/if_uart.cpp @@ -4,43 +4,43 @@ #include "base/mycfg.h" #include "QDebug" - void if_uart::init() { - if(serial_==nullptr) + if (serial_ == nullptr) { - serial_=new QSerialPort(); - serial_->setPortName(com); - serial_->setBaudRate(bsp); - serial_->setParity(QSerialPort::NoParity); - serial_->setDataBits(QSerialPort::Data8); - serial_->setStopBits(QSerialPort::OneStop); - serial_->setFlowControl(QSerialPort::NoFlowControl); - if(serial_->open(QIODevice::ReadWrite)) - { - connect(serial_,&QSerialPort::readyRead,this,&if_uart::ready_read_cb); - qDebug("uart \"%s\" bsp=%d",serial_->portName().toLocal8Bit().data(), bsp); - serial_open=true; - } else - { - qWarning()<<"uart open failed."<portName(); - } + serial_ = new QSerialPort(); + serial_->setPortName(com); + serial_->setBaudRate(bsp); + serial_->setParity(QSerialPort::NoParity); + serial_->setDataBits(QSerialPort::Data8); + serial_->setStopBits(QSerialPort::OneStop); + serial_->setFlowControl(QSerialPort::NoFlowControl); + if (serial_->open(QIODevice::ReadWrite)) + { + connect(serial_, &QSerialPort::readyRead, this, &if_uart::ready_read_cb); + qDebug("uart \"%s\" bsp=%d", serial_->portName().toLocal8Bit().data(), bsp); + serial_open = true; + } + else + { + qWarning() << "uart open failed." << serial_->portName(); + } } } int if_uart::write(myarray data) { - if((nullptr == serial_)||(serial_open!=true)) + if ((nullptr == serial_) || (serial_open != true)) { - //qWarning() << "Can’t send data , TcpClient socket not connect."; + // qWarning() << "Can’t send data , TcpClient socket not connect."; return 0; } - qDebug("uart send:%s",data.toHex(' ').data()); + qDebug("uart send:%s", data.toHex(' ').data()); int wb = serial_->write(data); - if((!serial_->flush())||(wb!=data.size())) + if ((!serial_->flush()) || (wb != data.size())) { - //qWarning() << "uart data:"<readAll(); - qDebug("uart recv:%s",data.toHex(' ').data()); + qDebug("uart recv:%s", data.toHex(' ').data()); if (irq_fun) { irq_fun(myarray(data)); } } - - InterFace *if_uart_hmi() { static InterFace *if_ = nullptr; if (if_ == nullptr) { - if_ = new if_uart("/dev/ttySTM1",115200); + if_ = new if_uart("/dev/ttySTM1", 115200); // QTimer::singleShot(0, if_, &InterFace::init); if_->init(); } @@ -71,18 +69,17 @@ InterFace *if_uart_hmi() if_export(uart_hmi, if_uart_hmi); - - InterFace *if_uart_host() { static InterFace *if_ = nullptr; if (if_ == nullptr) { - int bsp=57600; - if(syscfg()->uart_bsp!=0){ - bsp=syscfg()->uart_bsp; + int bsp = 57600; + if (syscfg()->uart_bsp != 0) + { + bsp = syscfg()->uart_bsp; } - if_ = new if_uart("/dev/ttySTM2",bsp); + if_ = new if_uart("/dev/ttySTM2", bsp); // QTimer::singleShot(0, if_, &InterFace::init); if_->init(); } @@ -91,14 +88,12 @@ InterFace *if_uart_host() if_export(uart_host, if_uart_host); - - InterFace *if_uart_m4() { static InterFace *if_ = nullptr; if (if_ == nullptr) { - if_ = new if_uart("/dev/ttyRPMSG0",115200); + if_ = new if_uart("/dev/ttyRPMSG0", 115200); // QTimer::singleShot(0, if_, &InterFace::init); if_->init(); } @@ -106,4 +101,3 @@ InterFace *if_uart_m4() } if_export(uart_m4, if_uart_m4); - diff --git a/interface/if_uart.h b/interface/if_uart.h index 157420e..afd0f3a 100644 --- a/interface/if_uart.h +++ b/interface/if_uart.h @@ -1,7 +1,6 @@ #ifndef if_uart_h__ #define if_uart_h__ - #include #include #include "QTimer" @@ -9,43 +8,38 @@ #include #include "QByteArray" #include "base/mycfg.h" -#include // 提供访问串口的功能 -#include // 提供系统中存在的串口信息 - - - +#include // 提供访问串口的功能 +#include // 提供系统中存在的串口信息 class if_uart : public InterFace { Q_OBJECT public: - if_uart(mystring com,int bsp):InterFace(){ + if_uart(mystring com, int bsp) : InterFace() + { this->com = com; this->bsp = bsp; - serial_=nullptr; - serial_open=false; + serial_ = nullptr; + serial_open = false; } - virtual ~if_uart(){ - if(serial_open==true){ + virtual ~if_uart() + { + if (serial_open == true) + { serial_->close(); delete serial_; } - }; + } void init(); int write(myarray data); protected slots: void ready_read_cb(); -protected: - mystring com; - int bsp; - QSerialPort *serial_; - bool serial_open; +protected: + mystring com; + int bsp; + QSerialPort *serial_; + bool serial_open; }; - - - - - #endif diff --git a/main.cpp b/main.cpp index 671d803..7372323 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,4 @@ #include "mainwindow.h" - #include #include #include @@ -11,7 +10,8 @@ #include "prot/prot_m4.h" #include "prot/prot_pc.h" #include "prot/prot_slave.h" - +#include "prot/prot_cmdline.h" +#include "base/debug.h" @@ -22,11 +22,19 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); MainWindow w; // w.show(); - syscfg(); + mycfg *cfg=syscfg(); + + if(cfg->log_redirect=="file"){ + mydebug_init("debug"); + }else{ + mydebug_init(); + } + check_plan(); prot_m4 *m4=protM4(); protPc(); protSlave(); + command_start(); // 获取m4的编译时间 m4->set_irq_fun([=](myarray data){ diff --git a/prebuild.py b/prebuild.py index 7b2c354..3fd264c 100644 --- a/prebuild.py +++ b/prebuild.py @@ -45,7 +45,7 @@ def main(): list_str=["info.json","json.lua","prints.lua"] json_obj["private"]=list_str f.seek(0, os.SEEK_SET) - f.write(json.dumps(json_obj, sort_keys=True, indent=4, separators=(',', ': '))) + f.write(json.dumps(json_obj, sort_keys=True, indent=2, separators=(',', ': '))) if __name__=="__main__": main() diff --git a/prot/prot_cmdline.cpp b/prot/prot_cmdline.cpp new file mode 100644 index 0000000..4709780 --- /dev/null +++ b/prot/prot_cmdline.cpp @@ -0,0 +1,151 @@ + +#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 network = QNetworkInterface::allInterfaces(); + foreach (QNetworkInterface net, network) + { + QString netName = net.humanReadableName(); + if (netName == "eth0") + { + QList list = net.addressEntries(); // 获取IP地址与子掩码等 + foreach (QNetworkAddressEntry address, list) + { + if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) // 获取IPv4的地址 + { + return address.ip().toString(); + } + } + } + } + return QString(); +} + +void command::init_cb() +{ + mycfg *cfg_ = syscfg(); + if (get_local_ip().isEmpty()) + { + qDebug("modify local ip addr."); + QString str = "ifconfig eth0 %1"; + system(str.arg(cfg_->local_ip).toLocal8Bit().data()); + str = "route add default gw %1"; + system(str.arg(cfg_->gateway_ip).toLocal8Bit().data()); + } + 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; + } +} + +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 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 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 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; +} + + + + diff --git a/prot/prot_cmdline.h b/prot/prot_cmdline.h new file mode 100644 index 0000000..c675c19 --- /dev/null +++ b/prot/prot_cmdline.h @@ -0,0 +1,66 @@ + +#ifndef prot_cmdline_h__ +#define prot_cmdline_h__ + +#include "QObject" +#include "base/base.h" +#include "QList" +#include "interface/codec.h" +#include "interface/interface.h" +#include "base/mycfg.h" +#include "QThread" +#include +#include + +// using namespace std; +// using namespace std::placeholders; + +// typedef std::function prot_cmdline_cb; +typedef void (*prot_cmdline_cb)(QList data); + +typedef struct +{ + const char *cmd; + const char *help; + prot_cmdline_cb fun; +} cmdline_def; + +#define cmdline_export(name_, fun_, help_) \ + const static char __cmdline_##name_##_name[] __attribute__((section(".rodata.cmdlstr"))) = #name_; \ + const static char __cmdline_##name_##_help[] __attribute__((section(".rodata.cmdlstr"))) = #help_; \ + __attribute__((used)) static cmdline_def _cmdline_##name_ __attribute__((section("cmdldef"))) = { \ + .cmd = __cmdline_##name_##_name, \ + .help = __cmdline_##name_##_help, \ + .fun = fun_, \ + } + +class command : public QObject +{ + Q_OBJECT +public: + command() + { + socket_ = nullptr; + } + virtual ~command() + { + if (socket_ != nullptr) + delete socket_; + } + void send(QByteArray data); + void init_cb(); + void ready_read_cb(); + +private: + QUdpSocket *socket_; + QByteArray recv_data; + QHostAddress host_addr; + quint16 port; +}; + + +command *command_start(); + + + +#endif diff --git a/prot_cmd/cmd_cmdline.cpp b/prot_cmd/cmd_cmdline.cpp new file mode 100644 index 0000000..c545d04 --- /dev/null +++ b/prot_cmd/cmd_cmdline.cpp @@ -0,0 +1,48 @@ +#include "prot/prot_cmdline.h" +#include "base/mycfg.h" +#include "QNetworkConfigurationManager" +#include "QTimer" +#include "QtNetwork" + + + + +// 获取设备基本信息 +static void whos(QList 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 network = QNetworkInterface::allInterfaces(); + foreach (QNetworkInterface net, network) + { + QString str = "net:%1"; + QString netName = net.humanReadableName(); + str = str.arg(netName); + out.append(str + "\n"); + QList 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.); + + + + + + diff --git a/prot_cmd/cmd_pc.cpp b/prot_cmd/cmd_pc.cpp index b4c7513..5a77bae 100644 --- a/prot_cmd/cmd_pc.cpp +++ b/prot_cmd/cmd_pc.cpp @@ -211,7 +211,7 @@ static HandlePc *get_selfdev_slaveupdate(){ return new selfdev_slaveupdate(); } -protpc_export(0x40, get_selfdev_slaveupdate); +protpc_export(0x41, get_selfdev_slaveupdate); diff --git a/prot_cmd/cmd_pc.h b/prot_cmd/cmd_pc.h index 639d42e..a026af4 100644 --- a/prot_cmd/cmd_pc.h +++ b/prot_cmd/cmd_pc.h @@ -2,6 +2,7 @@ #define cmd_pc_h__ #include "prot/prot_pc.h" +#include "prot/prot_m4.h" // 检测 自研批检仪