|
|
@ -0,0 +1,63 @@ |
|
|
|
#include "can_service.hpp"
|
|
|
|
using namespace std; |
|
|
|
using namespace iflytop; |
|
|
|
|
|
|
|
void CanService::initialize() { |
|
|
|
GET_TO_SERVICE(m_config); |
|
|
|
|
|
|
|
m_socketCan.reset(new SocketCan()); |
|
|
|
m_iflytopCanProtocolStack.reset(new IflytopCanProtocolStack()); |
|
|
|
|
|
|
|
auto socketCanConfig = make_shared<SocketCanConfig>(); |
|
|
|
socketCanConfig->enablLoopback = false; // 根据 SocketCan::dumpCanDriverInfo() 的输出,确定该标志位是false还是true
|
|
|
|
socketCanConfig->m_canName = m_config->get_iflytopSubDeviceCanIFName(); |
|
|
|
socketCanConfig->m_canBaudrate = m_config->get_iflytopSubDeviceCanBitrate(); |
|
|
|
socketCanConfig->m_canfilters = {}; |
|
|
|
|
|
|
|
logger->info("CanService::initialize() m_canName:{} {}", socketCanConfig->m_canName, socketCanConfig->m_canBaudrate); |
|
|
|
|
|
|
|
m_socketCan->initialize(socketCanConfig); |
|
|
|
m_socketCan->startListen(); |
|
|
|
|
|
|
|
m_socketCan->onSocketCanFrame.connect([this](shared_ptr<SocketCanFrame> canframe) { //
|
|
|
|
processRx(canframe); |
|
|
|
}); |
|
|
|
|
|
|
|
// m_socketCan->initialize()
|
|
|
|
} |
|
|
|
|
|
|
|
void CanService::processRx(shared_ptr<SocketCanFrame> frame) { |
|
|
|
if (frame) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (frame->getCanIdentifier() == socketcan::kextFrame && //
|
|
|
|
frame->getFanFrameType() == socketcan::kdataframe) { |
|
|
|
processIflytopRxPacket(frame); |
|
|
|
} else { |
|
|
|
logger->warn("Rx unknown can frame"); |
|
|
|
} |
|
|
|
} |
|
|
|
void CanService::processHamiltonCanRxPacket(shared_ptr<SocketCanFrame> frame) { //
|
|
|
|
logger->info("Rx Hamilton packet : {}", frame->toString()); |
|
|
|
} |
|
|
|
void CanService::processIflytopRxPacket(shared_ptr<SocketCanFrame> frame) { |
|
|
|
shared_ptr<icps::Packet> icps_packet = m_iflytopCanProtocolStack->parseSocketCanFrame(frame); |
|
|
|
if (!icps_packet) { |
|
|
|
logger->error("parseSocketCanFrame fail,{}", frame->toString()); |
|
|
|
return; |
|
|
|
} |
|
|
|
onIflytopCanSubDevicePacket(icps_packet); |
|
|
|
} |
|
|
|
|
|
|
|
void CanService::sendPacketToIflytopCanSubDevice(shared_ptr<icps::Packet> icps_packet) { |
|
|
|
auto frame = m_iflytopCanProtocolStack->createSocketCanFrame(icps_packet); |
|
|
|
if (!frame) { |
|
|
|
logger->error("createSocketCanFrame fail,{}", icps_packet->toString()); |
|
|
|
return; |
|
|
|
} |
|
|
|
SocketCan::SocketCanError_t erro = m_socketCan->sendFrame(frame); |
|
|
|
if (erro != SocketCan::SocketCanError_t::kSuccess) { |
|
|
|
logger->error("sendFrame fail,{}", SocketCan::SocketCanError_t2Str(erro)); |
|
|
|
} |
|
|
|
} |