81 lines
1.8 KiB
C++
81 lines
1.8 KiB
C++
|
|
|
|
#include "prot_pc.h"
|
|
#include "QDebug"
|
|
|
|
HandlePc *handlePcFind(int cmd)
|
|
{
|
|
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;
|
|
for (item = start; item < end; item++)
|
|
{
|
|
if (item != nullptr)
|
|
{
|
|
if (item->cmd == cmd)
|
|
return item->handle_get_fun();
|
|
}
|
|
}
|
|
qWarning("can not find protpc cmd= '%02x'", cmd);
|
|
return nullptr;
|
|
}
|
|
|
|
|
|
void ProtPc::init()
|
|
{
|
|
if_ = interFaceFind("uart_host");
|
|
codec_ = codecFind("codec_ym");
|
|
if(if_==nullptr||codec_==nullptr){
|
|
return;
|
|
}
|
|
|
|
if_->set_irq([=](myarray recv)
|
|
{
|
|
recv_data+=recv;
|
|
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());
|
|
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(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;
|
|
}
|
|
}
|
|
handle_=handlePcFind(cmd);
|
|
if(handle_!=nullptr){
|
|
connect(handle_,&HandlePc::send_data_signal,this,&ProtPc::send_data_slot);
|
|
handle_->dolater(cmd,data);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void ProtPc::send_data_slot(int cmd, myarray data)
|
|
{
|
|
if ((if_ != nullptr) && (codec_ != nullptr))
|
|
{
|
|
myarray send = codec_->encode(0, 0, cmd, data);
|
|
if_->write(send);
|
|
}
|
|
}
|
|
|
|
static ProtPc *g_protpc;
|
|
ProtPc *protPc()
|
|
{
|
|
if (g_protpc == nullptr)
|
|
{
|
|
g_protpc = new ProtPc();
|
|
g_protpc->init();
|
|
}
|
|
return g_protpc;
|
|
}
|