zephyr/subsys/net/ip/canbus_socket.c

31 lines
659 B
C
Raw Normal View History

/** @file
* @brief CANBUS Sockets related functions
*/
/*
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_sockets_can, CONFIG_NET_SOCKETS_LOG_LEVEL);
#include <errno.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/socketcan.h>
#include "connection.h"
enum net_verdict net_canbus_socket_input(struct net_pkt *pkt)
{
net: core: clean up inbound packet handling The net_core:process_data() and connection:net_conn_input() methods are the central network packet reception pipeline which: 1) guide network packets through all network layers, 2) decode, validate and filter packages along the way and 3) distribute packages to connections/sockets on all layers. This code seems to have grown complex and rather cluttered over time as all protocols, layers and socket implementations meet there in one single place. The code also reveals its origin as a pure IP stack which makes it hard to introduce non-IP protocols and their supporting socket infrastructure in a modularized way. For an outside contributor it seems almost impossible to add another protocol, protocol layer, filter rule or socket implementation without breaking things. This change doesn't try to solve all issues at once. It focuses exclusively on aspects that maintain backwards compatibility: * Improve modularization and encapsulation on implementation level by disentangling code that mixes up layers, protocols and socket implementations. * Make IP just one protocol among others by removing assymmetry in protocol handling logic and introduce preprocessor markup so that IP-specific code can be eliminated by the preprocessor if not needed. * Use preprocessor markup to delineate hook points for future modularization or expansion without introducing structural changes (as this would almost certainly break the API). * Reduce cyclomatic complexity, use positive rather than negative logic, improve variable naming, replace if/elseif/else blocks with switches, reduce variable span, introduce inline comments where code does not speak for itself, etc. as much as possible to make the code overall more human-friendly. Background: These are preparative steps for the introduction of IEEE 802.15.RAW sockets, DGRAM sockets and sockets bound to PAN IDs and device addresses similar to what the Linux kernel does. Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
2022-08-06 00:12:51 +02:00
__ASSERT_NO_MSG(net_pkt_family(pkt) == AF_CAN);
if (net_if_l2(net_pkt_iface(pkt)) == &NET_L2_GET_NAME(CANBUS_RAW)) {
return net_conn_input(pkt, NULL, CAN_RAW, NULL);
}
return NET_CONTINUE;
}