arch/sim/can: add loopback support for CAN character dev

add loopback support for sim CAN character dev

Signed-off-by: raiden00pl <raiden00@railab.me>
This commit is contained in:
raiden00pl
2025-05-20 19:08:04 +02:00
committed by Xiang Xiao
parent cac1cc896a
commit 67eb2bfd29
3 changed files with 48 additions and 4 deletions
+32
View File
@@ -184,3 +184,35 @@ bool host_can_avail(struct sim_can_s *can)
return select(can->fd + 1, &fdset, NULL, NULL, &tv) > 0;
}
/****************************************************************************
* Name: host_can_loopback
****************************************************************************/
int host_can_loopback(struct sim_can_s *can, bool enable)
{
int ret;
int tmp;
/* Receive own messages */
tmp = 1;
ret = setsockopt(can->fd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
&tmp, sizeof(tmp));
if (ret < 0)
{
return -errno;
}
/* Set loopback mode */
tmp = 1;
ret = setsockopt(can->fd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &tmp,
sizeof(tmp));
if (ret < 0)
{
return -errno;
}
return 0;
}
+15 -4
View File
@@ -361,10 +361,18 @@ int sim_canchar_initialize(int devidx, int devno)
if (ret < 0)
{
canerr("host_can_init failed %d\n", ret);
kmm_free(priv);
return ret;
goto errout;
}
#ifdef CONFIG_CAN_LOOPBACK
ret = host_can_loopback(&priv->host, true);
if (ret < 0)
{
canerr("host_can_loopback failed %d\n", ret);
goto errout;
}
#endif
/* Initialzie CAN character driver */
priv->dev.cd_ops = &g_sim_can_ops;
@@ -377,9 +385,12 @@ int sim_canchar_initialize(int devidx, int devno)
if (ret < 0)
{
canerr("can_register failed %d\n", ret);
kmm_free(priv);
return ret;
goto errout;
}
return OK;
errout:
kmm_free(priv);
return ret;
}
+1
View File
@@ -62,5 +62,6 @@ int host_can_send(struct sim_can_s *can, void *frame, size_t len);
int host_can_ifup(struct sim_can_s *can);
int host_can_ifdown(struct sim_can_s *can);
bool host_can_avail(struct sim_can_s *can);
int host_can_loopback(struct sim_can_s *can, bool enable);
#endif /* __ARCH_SIM_SRC_SIM_CAN_H */