2019-01-24 21:57:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* CANBUS related functions that are generic in all the drivers. */
|
|
|
|
|
|
|
|
#include <net/net_pkt.h>
|
|
|
|
#include <net/socket_can.h>
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_DRIVERS_CAN_SOCKET_CAN_GENERIC_H_
|
|
|
|
#define ZEPHYR_DRIVERS_CAN_SOCKET_CAN_GENERIC_H_
|
|
|
|
|
2021-02-03 02:58:15 +08:00
|
|
|
#define SOCKET_CAN_NAME_0 "SOCKET_CAN_0"
|
2019-01-24 21:57:30 +08:00
|
|
|
#define SOCKET_CAN_NAME_1 "SOCKET_CAN_1"
|
2021-02-03 02:58:15 +08:00
|
|
|
#define SOCKET_CAN_NAME_2 "SOCKET_CAN_2"
|
2019-01-24 21:57:30 +08:00
|
|
|
#define SEND_TIMEOUT K_MSEC(100)
|
|
|
|
#define RX_THREAD_STACK_SIZE 512
|
|
|
|
#define RX_THREAD_PRIORITY 2
|
|
|
|
#define BUF_ALLOC_TIMEOUT K_MSEC(50)
|
|
|
|
|
|
|
|
/* TODO: make msgq size configurable */
|
|
|
|
CAN_DEFINE_MSGQ(socket_can_msgq, 5);
|
2020-08-01 03:29:38 +08:00
|
|
|
K_KERNEL_STACK_DEFINE(rx_thread_stack, RX_THREAD_STACK_SIZE);
|
2019-01-24 21:57:30 +08:00
|
|
|
|
|
|
|
struct socket_can_context {
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *can_dev;
|
2019-01-24 21:57:30 +08:00
|
|
|
struct net_if *iface;
|
|
|
|
struct k_msgq *msgq;
|
|
|
|
|
|
|
|
/* TODO: remove the thread and push data to net directly from rx isr */
|
|
|
|
k_tid_t rx_tid;
|
|
|
|
struct k_thread rx_thread_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void socket_can_iface_init(struct net_if *iface)
|
|
|
|
{
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *dev = net_if_get_device(iface);
|
2020-05-29 03:23:02 +08:00
|
|
|
struct socket_can_context *socket_context = dev->data;
|
2019-01-24 21:57:30 +08:00
|
|
|
|
|
|
|
socket_context->iface = iface;
|
|
|
|
|
|
|
|
LOG_DBG("Init CAN interface %p dev %p", iface, dev);
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void tx_irq_callback(uint32_t error_flags, void *arg)
|
2019-01-24 21:57:30 +08:00
|
|
|
{
|
2019-02-13 17:26:17 +08:00
|
|
|
char *caller_str = (char *)arg;
|
2019-01-24 21:57:30 +08:00
|
|
|
if (error_flags) {
|
2019-02-13 17:26:17 +08:00
|
|
|
LOG_DBG("TX error from %s! error-code: %d",
|
|
|
|
caller_str, error_flags);
|
2019-01-24 21:57:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called by net_if.c when packet is about to be sent */
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int socket_can_send(const struct device *dev,
|
|
|
|
struct net_pkt *pkt)
|
2019-01-24 21:57:30 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct socket_can_context *socket_context = dev->data;
|
2019-01-24 21:57:30 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (net_pkt_family(pkt) != AF_CAN) {
|
|
|
|
return -EPFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = can_send(socket_context->can_dev,
|
2019-02-21 19:23:44 +08:00
|
|
|
(struct zcan_frame *)pkt->frags->data,
|
2019-02-13 17:26:17 +08:00
|
|
|
SEND_TIMEOUT, tx_irq_callback, "socket_can_send");
|
2019-01-24 21:57:30 +08:00
|
|
|
if (ret) {
|
|
|
|
LOG_DBG("Cannot send socket CAN msg (%d)", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If something went wrong, then we need to return negative value to
|
|
|
|
* net_if.c:net_if_tx() so that the net_pkt will get released.
|
|
|
|
*/
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int socket_can_setsockopt(const struct device *dev, void *obj,
|
2019-01-24 21:57:30 +08:00
|
|
|
int level, int optname,
|
|
|
|
const void *optval, socklen_t optlen)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct socket_can_context *socket_context = dev->data;
|
2019-06-18 20:35:45 +08:00
|
|
|
struct net_context *ctx = obj;
|
2019-01-24 21:57:30 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (level != SOL_CAN_RAW && optname != CAN_RAW_FILTER) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:44:07 +08:00
|
|
|
__ASSERT_NO_MSG(optlen == sizeof(struct zcan_filter));
|
2019-01-24 21:57:30 +08:00
|
|
|
|
|
|
|
ret = can_attach_msgq(socket_context->can_dev, socket_context->msgq,
|
|
|
|
optval);
|
|
|
|
if (ret == CAN_NO_FREE_FILTER) {
|
|
|
|
errno = ENOSPC;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:35:45 +08:00
|
|
|
net_context_set_filter_id(ctx, ret);
|
|
|
|
|
2019-01-24 21:57:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline void socket_can_close(const struct device *dev, int filter_id)
|
2019-06-18 20:35:45 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct socket_can_context *socket_context = dev->data;
|
2019-06-18 20:35:45 +08:00
|
|
|
|
|
|
|
can_detach(socket_context->can_dev, filter_id);
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:57:30 +08:00
|
|
|
static struct canbus_api socket_can_api = {
|
|
|
|
.iface_api.init = socket_can_iface_init,
|
|
|
|
.send = socket_can_send,
|
2019-06-18 20:35:45 +08:00
|
|
|
.close = socket_can_close,
|
2019-01-24 21:57:30 +08:00
|
|
|
.setsockopt = socket_can_setsockopt,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void rx_thread(void *ctx, void *unused1, void *unused2)
|
|
|
|
{
|
|
|
|
struct socket_can_context *socket_context = ctx;
|
|
|
|
struct net_pkt *pkt;
|
2019-02-21 19:23:44 +08:00
|
|
|
struct zcan_frame msg;
|
2019-01-24 21:57:30 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ARG_UNUSED(unused1);
|
|
|
|
ARG_UNUSED(unused2);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
k_msgq_get((struct k_msgq *)socket_context->msgq, &msg,
|
|
|
|
K_FOREVER);
|
|
|
|
|
|
|
|
pkt = net_pkt_rx_alloc_with_buffer(socket_context->iface,
|
|
|
|
sizeof(msg),
|
|
|
|
AF_CAN, 0,
|
|
|
|
BUF_ALLOC_TIMEOUT);
|
|
|
|
if (!pkt) {
|
|
|
|
LOG_ERR("Failed to obtain RX buffer");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:01:57 +08:00
|
|
|
if (net_pkt_write(pkt, (void *)&msg, sizeof(msg))) {
|
2019-01-24 21:57:30 +08:00
|
|
|
LOG_ERR("Failed to append RX data");
|
|
|
|
net_pkt_unref(pkt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = net_recv_data(socket_context->iface, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
net_pkt_unref(pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ZEPHYR_DRIVERS_CAN_SOCKET_CAN_GENERIC_H_ */
|