Files
checker_host/prot/prot_m4.cpp

142 lines
3.3 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-29 15:36:45 +08:00
if(if_==nullptr||codec_==nullptr){
return;
}
2023-11-27 14:31:00 +08:00
if_->set_irq([=](myarray recv)
{
2023-11-29 15:36:45 +08:00
recv_data+=recv;
int pack_len=0;
while(pack_len=codec_->packCheck(recv_data),pack_len>0){
int cmd,src,dst;
myarray data=codec_->decode(src,dst,cmd,recv_data);
recv_data.remove(0,pack_len);
if(wait>0) wait--;
if(exe_cb_fun(data)==false){
qWarning("can not find cb fun with:\"%s\"",data.data());
}
if(send_list.size()>0){
if_->write(send_list.takeFirst());
wait+=2;
}
2023-11-27 14:31:00 +08:00
}
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);
2023-11-29 15:36:45 +08:00
send_list.append(send);
if(wait==0){
wait+=2;
qDebug("send m4:%s",data.data());
if_->write(send_list.takeFirst());
}
2023-11-27 14:31:00 +08:00
}
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;
QList<HandleM4_def> cb_funs;
2023-11-27 14:31:00 +08:00
for (int i = 0; i < funs.size(); i++)
{
cb_funs.append(this->funs[i]);
}
for (int i = 0; i < cb_funs.size(); i++)
2023-11-27 14:31:00 +08:00
{
2023-11-29 15:36:45 +08:00
// 这里size包含结尾符要去掉
left = qstrlen(cb_funs[i].cmd.data());
// qDebug()<<"cmp m4 str:"<<data.left(left)<<cb_funs[i].cmd;
// if (data.left(left) == funs[i].cmd)
if (qstrncmp(data.data(),cb_funs[i].cmd.data(),left)==0)
2023-11-27 14:31:00 +08:00
{
for(int j=0;j<cb_funs[i].funs.size();j++){
prot_m4_cb fun=cb_funs[i].funs[j];
2023-11-29 15:36:45 +08:00
if(fun!=nullptr){
fun(data.mid(left));
}
2023-11-27 14:31:00 +08:00
}
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
{
if(fun==nullptr) return false;
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)
{
2023-11-29 15:36:45 +08:00
for(int j=0;j<funs[i].funs.size();j++){
prot_m4_cb temp=funs[i].funs[j];
if(fun.target<void(*)(myarray data)>()==temp.target<void(*)(myarray data)>()){
qDebug("this function pointer was exited.");
return false;
}
}
qDebug("add function pointer success.");
funs[i].funs.append(fun);
return true;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
}
HandleM4_def m4_cmd;
m4_cmd.cmd = data;
2023-11-29 15:36:45 +08:00
m4_cmd.funs.append(fun);
2023-11-27 14:31:00 +08:00
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)
{
2023-11-29 15:36:45 +08:00
if(fun==nullptr){
qDebug("del the same string cb.");
funs.removeAt(i);
return true;
}else{
for(int j=0;j<funs[i].funs.size();j++){
prot_m4_cb temp=funs[i].funs[j];
if(fun.target<void(*)(myarray data)>()==temp.target<void(*)(myarray data)>()){
qDebug("del the same function pointer.");
funs[i].funs.removeAt(j);
if(funs[i].funs.size()==0){
funs.removeAt(i);
}
return true;
}
}
return false;
}
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
}