|
|
@ -15,6 +15,8 @@ using namespace iflytop; |
|
|
|
} \ |
|
|
|
} while (0) |
|
|
|
|
|
|
|
SocketCan::SocketCanError_t SocketCan::sendFrame(shared_ptr<SocketCanFrame> frame, int overtime) { return sendFrame(frame); } |
|
|
|
|
|
|
|
SocketCan::SocketCanError_t SocketCan::sendFrame(shared_ptr<SocketCanFrame> frame) { |
|
|
|
if (!m_canBusIsReady) { |
|
|
|
return kDeviceBusy; |
|
|
@ -25,12 +27,8 @@ SocketCan::SocketCanError_t SocketCan::sendFrame(shared_ptr<SocketCanFrame> fram |
|
|
|
return kParamError; |
|
|
|
} |
|
|
|
|
|
|
|
if (isTxing()) { |
|
|
|
logger->error("txing, can't send frame"); |
|
|
|
return kDeviceBusy; |
|
|
|
} |
|
|
|
|
|
|
|
canfd_frame_t canframe = frame->getCanFrame(); |
|
|
|
logger->debug("TX:{}", frame->toString()); |
|
|
|
return writeFrame(canframe); |
|
|
|
} |
|
|
|
SocketCan::SocketCanError_t SocketCan::writeFrame(const canfd_frame_t &_frame) { |
|
|
@ -39,50 +37,16 @@ SocketCan::SocketCanError_t SocketCan::writeFrame(const canfd_frame_t &_frame) { |
|
|
|
} |
|
|
|
|
|
|
|
canfd_frame_t txframe = _frame; |
|
|
|
txframe.can_id |= 0x10000000; |
|
|
|
setTxStateToTxing(txframe); |
|
|
|
// txframe.can_id |= 0x10000000;
|
|
|
|
|
|
|
|
int ret = write(m_socketCanFd, &txframe, 16); |
|
|
|
if (ret != (16)) { |
|
|
|
logger->error("write fail,{}", strerror(errno)); |
|
|
|
unsetTxStateToTxing(); |
|
|
|
return kWriteError; |
|
|
|
} |
|
|
|
return kSuccess; |
|
|
|
} |
|
|
|
|
|
|
|
SocketCan::SocketCanError_t SocketCan::sendFrame(shared_ptr<SocketCanFrame> frame, int overtime) { |
|
|
|
tp_steady start = tu_steady().now(); |
|
|
|
|
|
|
|
while (tu_steady().elapsedTimeMs(start) < overtime && !m_canBusIsReady) { |
|
|
|
usleep(100); |
|
|
|
} |
|
|
|
|
|
|
|
if (!m_canBusIsReady) { |
|
|
|
return kOvertime; |
|
|
|
} |
|
|
|
|
|
|
|
while (tu_steady().elapsedTimeMs(start) < overtime && isTxing()) { |
|
|
|
usleep(100); |
|
|
|
} |
|
|
|
|
|
|
|
if (isTxing()) { |
|
|
|
logger->error("send frame overtime"); |
|
|
|
return kOvertime; |
|
|
|
} |
|
|
|
return sendFrame(frame); |
|
|
|
} |
|
|
|
|
|
|
|
bool SocketCan::isTxing() { return m_txState.txState == kTxing; } |
|
|
|
|
|
|
|
bool SocketCan::isTxFrame(const canfd_frame_t &frame) { |
|
|
|
lock_guard<recursive_mutex> lock(m_txState.lock); |
|
|
|
if (m_txState.txbuf.can_id == frame.can_id && m_txState.txbuf.len == frame.len && memcmp(m_txState.txbuf.data, frame.data, frame.len) == 0) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
void SocketCan::initialize(shared_ptr<SocketCanConfig> socketCanConfig) { //
|
|
|
|
ZCHECK(socketCanConfig, "socketCanConfig is null"); |
|
|
|
m_socketCanConfig = socketCanConfig; |
|
|
@ -223,24 +187,6 @@ void SocketCan::dosystem(string cmd) { |
|
|
|
logger->info("do cmd:{}", cmd); |
|
|
|
system(cmd.c_str()); |
|
|
|
} |
|
|
|
bool SocketCan::tryUpdateTxState(canfd_frame_t &rxloopmsg) { |
|
|
|
lock_guard<recursive_mutex> lock(m_txState.lock); |
|
|
|
if (m_txState.txState == kTxing && isTxFrame(rxloopmsg)) { |
|
|
|
unsetTxStateToTxing(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
void SocketCan::setTxStateToTxing(const canfd_frame_t &txpacket) { |
|
|
|
lock_guard<recursive_mutex> lock(m_txState.lock); |
|
|
|
m_txState.txState = kTxing; |
|
|
|
m_txState.txbuf = txpacket; |
|
|
|
} |
|
|
|
void SocketCan::unsetTxStateToTxing() { |
|
|
|
lock_guard<recursive_mutex> lock(m_txState.lock); |
|
|
|
m_txState.txState = kTxIdle; |
|
|
|
} |
|
|
|
|
|
|
|
#define TEST_CANDRIVER_DO(exptr) \
|
|
|
|
do { \ |
|
|
@ -388,28 +334,22 @@ void SocketCan::socketCanReadThreadLoop() { |
|
|
|
// /usr/include/linux/can/error.h
|
|
|
|
// x can-bus error event 20000004,0004000000000086
|
|
|
|
logger->error("rx can-bus error event {:x},{}", canframe.can_id, StringUtils().bytesToString(canframe.data, canframe.len)); |
|
|
|
|
|
|
|
unsetTxStateToTxing(); |
|
|
|
// exit(-1);
|
|
|
|
break; |
|
|
|
} else { |
|
|
|
// logger->info("TX:{} {} {}", canframe.can_id, canframe.__res0, canframe.__res1);
|
|
|
|
if (!tryUpdateTxState(canframe)) { |
|
|
|
shared_ptr<SocketCanFrame> socketCanFrame = SocketCanFrame::createFrame(canframe); |
|
|
|
logger->debug("RX:{}", socketCanFrame->toString()); |
|
|
|
onSocketCanFrame(socketCanFrame); |
|
|
|
} |
|
|
|
shared_ptr<SocketCanFrame> socketCanFrame = SocketCanFrame::createFrame(canframe); |
|
|
|
logger->debug("RX:{}", socketCanFrame->toString()); |
|
|
|
onSocketCanFrame(socketCanFrame); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
m_canTriggerError = true; |
|
|
|
m_canBusIsReady = false; |
|
|
|
m_canErrorFlag = true; |
|
|
|
m_canBusIsReady = false; |
|
|
|
} |
|
|
|
|
|
|
|
void SocketCan::monitorLoop() { |
|
|
|
ThisThread thisThread; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
if (m_canTriggerError) { |
|
|
|
if (m_canErrorFlag) { |
|
|
|
// 尝试恢复CAN总线
|
|
|
|
logger->warn("try to recover can bus............................................"); |
|
|
|
if (m_thread) { |
|
|
@ -421,14 +361,6 @@ void SocketCan::monitorLoop() { |
|
|
|
close(m_socketCanFd); |
|
|
|
m_socketCanFd = -1; |
|
|
|
|
|
|
|
{ |
|
|
|
lock_guard<recursive_mutex> lock(m_txState.lock); |
|
|
|
memset(&m_txState.txbuf, 0x00, sizeof(m_txState.txbuf)); |
|
|
|
m_txState.txState = TxState_t::kTxIdle; |
|
|
|
m_txState.txAckCount = 0; |
|
|
|
m_txState.txLoopMessageRxTicket = zsteady_clock().now(); |
|
|
|
} |
|
|
|
|
|
|
|
logger->warn("re init can bus"); |
|
|
|
socketcanInitialize(); |
|
|
|
|
|
|
@ -437,8 +369,8 @@ void SocketCan::monitorLoop() { |
|
|
|
socketCanReadThreadLoop(); |
|
|
|
})); |
|
|
|
|
|
|
|
m_canTriggerError = false; |
|
|
|
m_canBusIsReady = true; |
|
|
|
m_canErrorFlag = false; |
|
|
|
m_canBusIsReady = true; |
|
|
|
|
|
|
|
logger->warn("recover can bus ok............................................"); |
|
|
|
} |
|
|
|