#include "prot_m4.h" #include "QDebug" void prot_m4::init() { if_ = interFaceFind("uart_m4"); codec_ = codecFind("codec_m4"); 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(); } }); } void prot_m4::send_data_slot(myarray data) { if ((if_ != nullptr) && (codec_ != nullptr)) { myarray send = codec_->encode(0, 0, 0, data); if_->write(send); } } bool prot_m4::exe_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 (funs[i].fun != nullptr) { funs[i].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) { int left; for (int i = 0; i < funs.size(); i++) { left = funs[i].cmd.size(); if (data.left(left) == funs[i].cmd) { return false; } } HandleM4_def m4_cmd; m4_cmd.cmd = data; m4_cmd.fun = 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) { funs.removeAt(i); return true; } } return false; }