Skip to content

Commit

Permalink
net: socket: can: prepend SocketCAN data types with socketcan
Browse files Browse the repository at this point in the history
Rename the SocketCAN data types to "socketcan_*" in preparation of renaming
the low-level CAN controller API data types.

This breaks the naming compatibility with the similar SocketCAN data types
from the Linux kernel, but Zephyr and Linux SocketCAN are not 100%
compatible anyways (only the structure fields are compatible, extended
functionality such filtering, error reporting etc. are not).

Signed-off-by: Henrik Brix Andersen <[email protected]>
  • Loading branch information
henrikbrixandersen authored and carlescufi committed Aug 18, 2022
1 parent 6099e18 commit d159947
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 88 deletions.
66 changes: 33 additions & 33 deletions include/zephyr/net/socket_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ struct sockaddr_can {
*
* @endcode
*/
typedef uint32_t canid_t;
typedef uint32_t socketcan_id_t;

/**
* @brief CAN frame for Linux SocketCAN compatibility.
*/
struct can_frame {
struct socketcan_frame {
/** 32-bit CAN ID + EFF/RTR/ERR flags. */
canid_t can_id;
socketcan_id_t can_id;

/** The data length code (DLC). */
uint8_t can_dlc;
Expand All @@ -109,71 +109,71 @@ struct can_frame {
*
* A filter is considered a match when `received_can_id & mask == can_id & can_mask`.
*/
struct can_filter {
struct socketcan_filter {
/** The CAN identifier to match. */
canid_t can_id;
socketcan_id_t can_id;
/** The mask applied to @a can_id for matching. */
canid_t can_mask;
socketcan_id_t can_mask;
};

/**
* @brief Translate a @a can_frame struct to a @a zcan_frame struct.
* @brief Translate a @a socketcan_frame struct to a @a zcan_frame struct.
*
* @param frame Pointer to can_frame struct.
* @param sframe Pointer to sockecan_frame struct.
* @param zframe Pointer to zcan_frame struct.
*/
static inline void can_copy_frame_to_zframe(const struct can_frame *frame,
static inline void can_copy_frame_to_zframe(const struct socketcan_frame *sframe,
struct zcan_frame *zframe)
{
zframe->id_type = (frame->can_id & BIT(31)) >> 31;
zframe->rtr = (frame->can_id & BIT(30)) >> 30;
zframe->id = frame->can_id & BIT_MASK(29);
zframe->dlc = frame->can_dlc;
memcpy(zframe->data, frame->data, sizeof(zframe->data));
zframe->id_type = (sframe->can_id & BIT(31)) >> 31;
zframe->rtr = (sframe->can_id & BIT(30)) >> 30;
zframe->id = sframe->can_id & BIT_MASK(29);
zframe->dlc = sframe->can_dlc;
memcpy(zframe->data, sframe->data, sizeof(zframe->data));
}

/**
* @brief Translate a @a zcan_frame struct to a @a can_frame struct.
* @brief Translate a @a zcan_frame struct to a @a socketcan_frame struct.
*
* @param zframe Pointer to zcan_frame struct.
* @param frame Pointer to can_frame struct.
* @param sframe Pointer to socketcan_frame struct.
*/
static inline void can_copy_zframe_to_frame(const struct zcan_frame *zframe,
struct can_frame *frame)
struct socketcan_frame *sframe)
{
frame->can_id = (zframe->id_type << 31) | (zframe->rtr << 30) | zframe->id;
frame->can_dlc = zframe->dlc;
memcpy(frame->data, zframe->data, sizeof(frame->data));
sframe->can_id = (zframe->id_type << 31) | (zframe->rtr << 30) | zframe->id;
sframe->can_dlc = zframe->dlc;
memcpy(sframe->data, zframe->data, sizeof(sframe->data));
}

/**
* @brief Translate a @a can_filter struct to a @a zcan_filter struct.
* @brief Translate a @a socketcan_filter struct to a @a zcan_filter struct.
*
* @param filter Pointer to can_filter struct.
* @param sfilter Pointer to socketcan_filter struct.
* @param zfilter Pointer to zcan_filter struct.
*/
static inline void can_copy_filter_to_zfilter(const struct can_filter *filter,
static inline void can_copy_filter_to_zfilter(const struct socketcan_filter *sfilter,
struct zcan_filter *zfilter)
{
zfilter->id_type = (filter->can_id & BIT(31)) >> 31;
zfilter->rtr = (filter->can_id & BIT(30)) >> 30;
zfilter->id = filter->can_id & BIT_MASK(29);
zfilter->rtr_mask = (filter->can_mask & BIT(30)) >> 30;
zfilter->id_mask = filter->can_mask & BIT_MASK(29);
zfilter->id_type = (sfilter->can_id & BIT(31)) >> 31;
zfilter->rtr = (sfilter->can_id & BIT(30)) >> 30;
zfilter->id = sfilter->can_id & BIT_MASK(29);
zfilter->rtr_mask = (sfilter->can_mask & BIT(30)) >> 30;
zfilter->id_mask = sfilter->can_mask & BIT_MASK(29);
}

/**
* @brief Translate a @a zcan_filter struct to a @a can_filter struct.
* @brief Translate a @a zcan_filter struct to a @a socketcan_filter struct.
*
* @param zfilter Pointer to zcan_filter struct.
* @param filter Pointer to can_filter struct.
* @param sfilter Pointer to socketcan_filter struct.
*/
static inline void can_copy_zfilter_to_filter(const struct zcan_filter *zfilter,
struct can_filter *filter)
struct socketcan_filter *sfilter)
{
filter->can_id = (zfilter->id_type << 31) |
sfilter->can_id = (zfilter->id_type << 31) |
(zfilter->rtr << 30) | zfilter->id;
filter->can_mask = (zfilter->rtr_mask << 30) |
sfilter->can_mask = (zfilter->rtr_mask << 30) |
(zfilter->id_type << 31) | zfilter->id_mask;
}

Expand Down
32 changes: 16 additions & 16 deletions samples/net/sockets/can/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ static const struct zcan_filter zfilter = {
.id_mask = CAN_STD_ID_MASK
};

static struct can_filter filter;
static struct socketcan_filter sfilter;

static void tx(int *can_fd)
{
int fd = POINTER_TO_INT(can_fd);
struct zcan_frame msg = {0};
struct can_frame frame = {0};
struct socketcan_frame sframe = {0};
int ret, i;

msg.dlc = 8U;
Expand All @@ -59,12 +59,12 @@ static void tx(int *can_fd)
msg.data[i] = 0xF0 | i;
}

can_copy_zframe_to_frame(&msg, &frame);
can_copy_zframe_to_frame(&msg, &sframe);

LOG_DBG("Sending CAN data...");

while (1) {
ret = send(fd, &frame, sizeof(frame), 0);
ret = send(fd, &sframe, sizeof(sframe), 0);
if (ret < 0) {
LOG_ERR("Cannot send CAN message (%d)", -errno);
}
Expand All @@ -73,7 +73,7 @@ static void tx(int *can_fd)
}
}

static int create_socket(const struct can_filter *filter)
static int create_socket(const struct socketcan_filter *sfilter)
{
struct sockaddr_can can_addr;
int fd, ret;
Expand All @@ -95,40 +95,40 @@ static int create_socket(const struct can_filter *filter)
return ret;
}

(void)setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, filter,
sizeof(*filter));
(void)setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, sfilter,
sizeof(*sfilter));

return fd;
}

static void rx(int *can_fd, int *do_close_period,
const struct can_filter *filter)
const struct socketcan_filter *sfilter)
{
int close_period = POINTER_TO_INT(do_close_period);
int fd = POINTER_TO_INT(can_fd);
struct sockaddr_can can_addr;
socklen_t addr_len;
struct zcan_frame msg;
struct can_frame frame;
struct socketcan_frame sframe;
int ret;

LOG_DBG("[%d] Waiting CAN data...", fd);

while (1) {
uint8_t *data;

memset(&frame, 0, sizeof(frame));
memset(&sframe, 0, sizeof(sframe));
addr_len = sizeof(can_addr);

ret = recvfrom(fd, &frame, sizeof(struct can_frame),
ret = recvfrom(fd, &sframe, sizeof(struct socketcan_frame),
0, (struct sockaddr *)&can_addr, &addr_len);
if (ret < 0) {
LOG_ERR("[%d] Cannot receive CAN message (%d)", fd,
-errno);
continue;
}

can_copy_frame_to_zframe(&frame, &msg);
can_copy_frame_to_zframe(&sframe, &msg);

LOG_INF("[%d] CAN msg: type 0x%x RTR 0x%x EID 0x%x DLC 0x%x",
fd, msg.id_type, msg.rtr, msg.id, msg.dlc);
Expand All @@ -152,7 +152,7 @@ static void rx(int *can_fd, int *do_close_period,

k_sleep(K_SECONDS(1));

fd = create_socket(filter);
fd = create_socket(sfilter);
if (fd < 0) {
LOG_ERR("Cannot get socket (%d)",
-errno);
Expand All @@ -172,7 +172,7 @@ static int setup_socket(void)
int fd, rx_fd;
int ret;

can_copy_zfilter_to_filter(&zfilter, &filter);
can_copy_zfilter_to_filter(&zfilter, &sfilter);

iface = net_if_get_first_by_type(&NET_L2_GET_NAME(CANBUS_RAW));
if (!iface) {
Expand Down Expand Up @@ -201,8 +201,8 @@ static int setup_socket(void)
goto cleanup;
}

ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &filter,
sizeof(filter));
ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &sfilter,
sizeof(sfilter));
if (ret < 0) {
ret = -errno;
LOG_ERR("Cannot set CAN sockopt (%d)", ret);
Expand Down
Loading

0 comments on commit d159947

Please sign in to comment.