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:
Henrik Brix Andersen 2022-09-30 14:44:16 +02:00 committed by Carles Cufí
commit f8a88cdb27
29 changed files with 661 additions and 626 deletions

View file

@ -124,15 +124,16 @@ static void canopen_tx_retry(struct k_work *item)
int err;
uint16_t i;
memset(&frame, 0, sizeof(frame));
CO_LOCK_CAN_SEND();
for (i = 0; i < CANmodule->tx_size; i++) {
buffer = &CANmodule->tx_array[i];
if (buffer->bufferFull) {
frame.id_type = CAN_STANDARD_IDENTIFIER;
frame.id = buffer->ident;
frame.dlc = buffer->DLC;
frame.rtr = (buffer->rtr ? 1 : 0);
frame.flags |= (buffer->rtr ? CAN_FRAME_RTR : 0);
memcpy(frame.data, buffer->data, buffer->DLC);
err = can_send(CANmodule->dev, &frame, K_NO_WAIT,
@ -197,7 +198,7 @@ CO_ReturnError_t CO_CANmodule_init(CO_CANmodule_t *CANmodule,
return CO_ERROR_ILLEGAL_ARGUMENT;
}
max_filters = can_get_max_filters(ctx->dev, CAN_STANDARD_IDENTIFIER);
max_filters = can_get_max_filters(ctx->dev, false);
if (max_filters != -ENOSYS) {
if (max_filters < 0) {
LOG_ERR("unable to determine number of CAN RX filters");
@ -298,11 +299,9 @@ CO_ReturnError_t CO_CANrxBufferInit(CO_CANmodule_t *CANmodule, uint16_t index,
buffer->object = object;
buffer->pFunct = pFunct;
filter.id_type = CAN_STANDARD_IDENTIFIER;
filter.flags = (rtr ? CAN_FILTER_RTR : CAN_FILTER_DATA);
filter.id = ident;
filter.id_mask = mask;
filter.rtr = (rtr ? 1 : 0);
filter.rtr_mask = 1;
filter.mask = mask;
if (buffer->filter_id != -ENOSPC) {
can_remove_rx_filter(CANmodule->dev, buffer->filter_id);
@ -358,6 +357,8 @@ CO_ReturnError_t CO_CANsend(CO_CANmodule_t *CANmodule, CO_CANtx_t *buffer)
return CO_ERROR_ILLEGAL_ARGUMENT;
}
memset(&frame, 0, sizeof(frame));
CO_LOCK_CAN_SEND();
if (buffer->bufferFull) {
@ -369,10 +370,9 @@ CO_ReturnError_t CO_CANsend(CO_CANmodule_t *CANmodule, CO_CANtx_t *buffer)
ret = CO_ERROR_TX_OVERFLOW;
}
frame.id_type = CAN_STANDARD_IDENTIFIER;
frame.id = buffer->ident;
frame.dlc = buffer->DLC;
frame.rtr = (buffer->rtr ? 1 : 0);
frame.flags = (buffer->rtr ? CAN_FRAME_RTR : 0);
memcpy(frame.data, buffer->data, buffer->DLC);
err = can_send(CANmodule->dev, &frame, K_NO_WAIT, canopen_tx_callback,