Files
checker_host/prot/prot_pc.cpp

129 lines
2.5 KiB
C++
Raw Normal View History

2023-11-26 23:05:35 +08:00
#include "prot_pc.h"
#include "QDebug"
HandlePc *handlePcFind(int cmd)
{
2023-11-27 14:31:00 +08:00
extern const int __start_protpc;
extern const int __stop_protpc;
handlepc_def *start = (handlepc_def *)&__start_protpc;
handlepc_def *end = (handlepc_def *)&__stop_protpc;
2023-11-26 23:05:35 +08:00
handlepc_def *item = 0;
2023-11-27 14:31:00 +08:00
for (item = start; item < end; item++)
2023-11-26 23:05:35 +08:00
{
if (item != nullptr)
{
2023-11-27 14:31:00 +08:00
if (item->cmd == cmd)
2023-11-26 23:05:35 +08:00
return item->handle_get_fun();
}
}
qWarning("can not find protpc cmd= '%02x'", cmd);
return nullptr;
}
QList<int> handlePcList()
{
extern const int __start_protpc;
extern const int __stop_protpc;
handlepc_def *start = (handlepc_def *)&__start_protpc;
handlepc_def *end = (handlepc_def *)&__stop_protpc;
handlepc_def *item = 0;
QList<int> list;
for (item = start; item < end; item++)
{
if (item != nullptr)
{
list.append(item->cmd);
}
}
return list;
}
2023-11-26 23:05:35 +08:00
void ProtPc::init()
{
if(syscfg()->tcp_enable==false){
if_ = interFaceFind("uart_host");
}else{
if_ = interFaceFind("tcp");
}
2023-11-27 14:31:00 +08:00
codec_ = codecFind("codec_ym");
2023-11-29 15:36:45 +08:00
if(if_==nullptr||codec_==nullptr){
return;
}
2023-11-26 23:05:35 +08:00
2023-11-27 14:31:00 +08:00
if_->set_irq([=](myarray recv)
{
recv_data+=recv;
2023-11-29 15:36:45 +08:00
int pack_len=0;
while(pack_len=codec_->packCheck(recv_data),pack_len>0){
int cmd,src,dst;
// qDebug("host recv:%s",recv_data.toHex(' ').data());
2023-11-29 15:36:45 +08:00
myarray data=codec_->decode(src,dst,cmd,recv_data);
recv_data.remove(0,pack_len);
// qDebug("host recv removed:%s",recv_data.toHex(' ').data());
if(codec_->failed!=true){
qDebug("pc cmd:%02x.",cmd);
docmd(cmd,data);
}else{
qWarning("host data decode failed.");
}
2023-11-27 14:31:00 +08:00
}
2023-11-26 23:05:35 +08:00
});
}
2023-11-27 14:31:00 +08:00
void ProtPc::send_data_slot(int cmd, myarray data)
2023-11-26 23:05:35 +08:00
{
2023-11-27 14:31:00 +08:00
if ((if_ != nullptr) && (codec_ != nullptr))
{
myarray send = codec_->encode(0, 0, cmd, data);
if_->write(send);
}
2023-11-26 23:05:35 +08:00
}
2023-12-07 18:29:49 +08:00
bool ProtPc::docmd(int cmd,myarray data)
{
if(handle_!=nullptr){
if(handle_->busy==0){
disconnect(handle_,&HandlePc::send_data_signal,this,&ProtPc::send_data_slot);
delete handle_;
}else{
qWarning("prot_pc is busy.");
return false;
}
}
handle_=handlePcFind(cmd);
if(handle_!=nullptr){
connect(handle_,&HandlePc::send_data_signal,this,&ProtPc::send_data_slot);
handle_->dolater(cmd,data);
return true;
}
return false;
}
2023-11-26 23:05:35 +08:00
static ProtPc *g_protpc;
2023-11-27 14:31:00 +08:00
ProtPc *protPc()
{
if (g_protpc == nullptr)
{
g_protpc = new ProtPc();
g_protpc->init();
}
return g_protpc;
2023-11-26 23:05:35 +08:00
}