Files
checker_host/prot/prot_m4.cpp

92 lines
1.7 KiB
C++
Raw Normal View History

2023-11-26 23:05:35 +08:00
#include "prot_m4.h"
#include "QDebug"
void prot_m4::init()
{
2023-11-27 14:31:00 +08:00
if_ = interFaceFind("uart_m4");
codec_ = codecFind("codec_m4");
2023-11-26 23:05:35 +08:00
2023-11-27 14:31:00 +08:00
if_->set_irq([=](myarray recv)
{
if(codec_->packCheck(recv_data)==true){
int cmd,src,dst;
myarray data=codec_->decode(src,dst,cmd,recv_data);
if(exe_cb_fun(data)==false){
qWarning("can not find cb fun with:\"%s\"",data.data());
}
recv_data.clear();
}
2023-11-26 23:05:35 +08:00
});
}
void prot_m4::send_data_slot(myarray data)
{
2023-11-27 14:31:00 +08:00
if ((if_ != nullptr) && (codec_ != nullptr))
{
myarray send = codec_->encode(0, 0, 0, data);
if_->write(send);
}
2023-11-26 23:05:35 +08:00
}
bool prot_m4::exe_cb_fun(myarray data)
{
2023-11-27 14:31:00 +08:00
int left;
for (int i = 0; i < funs.size(); i++)
{
left = funs[i].cmd.size();
if (data.left(left) == funs[i].cmd)
{
if (funs[i].fun != nullptr)
{
funs[i].fun(data.mid(left));
return true;
}
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
}
return false;
2023-11-26 23:05:35 +08:00
}
static prot_m4 *g_protm4;
2023-11-27 14:31:00 +08:00
prot_m4 *protM4()
{
if (g_protm4 == nullptr)
{
g_protm4 = new prot_m4();
g_protm4->init();
}
return g_protm4;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
bool prot_m4::set_irq_fun(prot_m4_cb fun, myarray data)
2023-11-26 23:05:35 +08:00
{
2023-11-27 14:31:00 +08:00
int left;
for (int i = 0; i < funs.size(); i++)
{
left = funs[i].cmd.size();
if (data.left(left) == funs[i].cmd)
{
return false;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
}
HandleM4_def m4_cmd;
m4_cmd.cmd = data;
m4_cmd.fun = fun;
funs.append(m4_cmd);
return true;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
bool prot_m4::del_irq_fun(prot_m4_cb fun, myarray data)
2023-11-26 23:05:35 +08:00
{
2023-11-27 14:31:00 +08:00
int left;
for (int i = 0; i < funs.size(); i++)
{
left = funs[i].cmd.size();
if (data.left(left) == funs[i].cmd)
{
funs.removeAt(i);
return true;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
}
return false;
2023-11-26 23:05:35 +08:00
}