Files
checker_host/prot/prot_m4.cpp
2024-01-06 09:59:58 +08:00

142 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "prot_m4.h"
#include "QDebug"
void prot_m4::init()
{
if_ = interFaceFind("uart_m4");
codec_ = codecFind("codec_m4");
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;
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;
}
}
});
}
void prot_m4::send_data_slot(myarray data)
{
if ((if_ != nullptr) && (codec_ != nullptr))
{
myarray send = codec_->encode(0, 0, 0, data);
send_list.append(send);
if(wait==0){
wait+=2;
// qDebug("send m4:%s",data.data());
if_->write(send_list.takeFirst());
}
}
}
bool prot_m4::exe_cb_fun(myarray data)
{
int left;
QList<HandleM4_def> cb_funs;
for (int i = 0; i < funs.size(); i++)
{
cb_funs.append(this->funs[i]);
}
for (int i = 0; i < cb_funs.size(); i++)
{
// 这里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)
{
for(int j=0;j<cb_funs[i].funs.size();j++){
prot_m4_cb fun=cb_funs[i].funs[j];
if(fun!=nullptr){
fun(data.mid(left));
}
}
return true;
}
}
return false;
}
static prot_m4 *g_protm4;
prot_m4 *protM4()
{
if (g_protm4 == nullptr)
{
g_protm4 = new prot_m4();
g_protm4->init();
}
return g_protm4;
}
bool prot_m4::set_irq_fun(prot_m4_cb fun, myarray data)
{
if(fun==nullptr) return false;
int left;
for (int i = 0; i < funs.size(); i++)
{
left = funs[i].cmd.size();
if (data.left(left) == funs[i].cmd)
{
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;
}
}
HandleM4_def m4_cmd;
m4_cmd.cmd = data;
m4_cmd.funs.append(fun);
funs.append(m4_cmd);
return true;
}
bool prot_m4::del_irq_fun(prot_m4_cb fun, myarray data)
{
int left;
for (int i = 0; i < funs.size(); i++)
{
left = funs[i].cmd.size();
if (data.left(left) == funs[i].cmd)
{
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;
}
}
}
return false;
}