drivers: can: use flags fields for can_frame and can_filter structs
The can_frame and can_filter structs support a number of different flags (standard/extended CAN ID type, Remote Transmission Request, CAN-FD format, Bit Rate Switch, ...). Each of these flags is represented as a discrete bit in the given structure. This design pattern requires every user of these structs to initialize all of these flags to either 0 or 1, which does not scale well for future flag additions. Some of these flags have associated enumerations to be used for assignment, some do not. CAN drivers and protocols tend to rely on the logical value of the flag instead of using the enumeration, leading to a very fragile API. The enumerations are used inconsistently between the can_frame and can_filter structures, which further complicates the API. Instead, convert these flags to bitfields with separate flag definitions for the can_frame and can_filter structures. This API allows for future extensions without having to revisit existing users of the two structures. Furthermore, this allows driver to easily check for unsupported flags in the respective API calls. As this change leads to the "id_mask" field of the can_filter to be the only mask present in that structure, rename it to "mask" for simplicity. Fixes: #50776 Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
parent
a9c7c58345
commit
f8a88cdb27
29 changed files with 661 additions and 626 deletions
|
@ -109,9 +109,8 @@ static void rx_thread(void *arg1, void *arg2, void *arg3)
|
|||
|
||||
LOG_DBG("Received %d bytes. Id: 0x%x, ID type: %s %s",
|
||||
frame.dlc, frame.id,
|
||||
frame.id_type == CAN_STANDARD_IDENTIFIER ?
|
||||
"standard" : "extended",
|
||||
frame.rtr == CAN_DATAFRAME ? "" : ", RTR frame");
|
||||
(frame.flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
|
||||
(frame.flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");
|
||||
|
||||
dispatch_frame(dev, &frame);
|
||||
}
|
||||
|
@ -132,18 +131,32 @@ static int can_npl_send(const struct device *dev, const struct can_frame *frame,
|
|||
|
||||
LOG_DBG("Sending %d bytes on %s. Id: 0x%x, ID type: %s %s",
|
||||
frame->dlc, dev->name, frame->id,
|
||||
frame->id_type == CAN_STANDARD_IDENTIFIER ?
|
||||
"standard" : "extended",
|
||||
frame->rtr == CAN_DATAFRAME ? "" : ", RTR frame");
|
||||
(frame->flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
|
||||
(frame->flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");
|
||||
|
||||
__ASSERT_NO_MSG(callback != NULL);
|
||||
|
||||
#ifdef CONFIG_CAN_FD_MODE
|
||||
if (data->mode_fd && frame->fd == 1) {
|
||||
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR |
|
||||
CAN_FRAME_FDF | CAN_FRAME_BRS)) != 0) {
|
||||
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if ((frame->flags & CAN_FRAME_FDF) != 0) {
|
||||
if (!data->mode_fd) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
max_dlc = CANFD_MAX_DLC;
|
||||
mtu = CANFD_MTU;
|
||||
}
|
||||
#endif /* CONFIG_CAN_FD_MODE */
|
||||
#else /* CONFIG_CAN_FD_MODE */
|
||||
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) {
|
||||
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
#endif /* !CONFIG_CAN_FD_MODE */
|
||||
|
||||
if (frame->dlc > max_dlc) {
|
||||
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, max_dlc);
|
||||
|
@ -184,13 +197,12 @@ static int can_npl_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
|
|||
int filter_id = -ENOSPC;
|
||||
|
||||
LOG_DBG("Setting filter ID: 0x%x, mask: 0x%x", filter->id,
|
||||
filter->id_mask);
|
||||
LOG_DBG("Filter type: %s ID %s mask",
|
||||
filter->id_type == CAN_STANDARD_IDENTIFIER ?
|
||||
"standard" : "extended",
|
||||
((filter->id_type && (filter->id_mask == CAN_STD_ID_MASK)) ||
|
||||
(!filter->id_type && (filter->id_mask == CAN_EXT_ID_MASK))) ?
|
||||
"with" : "without");
|
||||
filter->mask);
|
||||
|
||||
if ((filter->flags & ~(CAN_FILTER_IDE | CAN_FILTER_DATA | CAN_FILTER_RTR)) != 0) {
|
||||
LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
k_mutex_lock(&data->filter_mutex, K_FOREVER);
|
||||
|
||||
|
@ -384,9 +396,9 @@ static int can_npl_get_core_clock(const struct device *dev, uint32_t *rate)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int can_npl_get_max_filters(const struct device *dev, enum can_ide id_type)
|
||||
static int can_npl_get_max_filters(const struct device *dev, bool ide)
|
||||
{
|
||||
ARG_UNUSED(id_type);
|
||||
ARG_UNUSED(ide);
|
||||
|
||||
return CONFIG_CAN_MAX_FILTER;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue