Skip to content
Permalink
Browse files

net: context: Set local address properly for AF_CAN connections

At the moment there is no real address for local CANBUS socket,
but we can still set protocol family of local socket to AF_CAN
so that for example net-shell "net conn" command can show
information about it.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information...
jukkar committed Feb 27, 2019
1 parent ae89c22 commit 0032f6866938c5b4c5a254083d5372bcc8fdc51e
Showing with 14 additions and 3 deletions.
  1. +4 −0 subsys/net/ip/connection.c
  2. +10 −3 subsys/net/ip/net_context.c
@@ -306,6 +306,10 @@ int net_conn_register(u16_t proto, u8_t family,
if (net_sin(local_addr)->sin_addr.s_addr) {
flags |= NET_CONN_LOCAL_ADDR_SPEC;
}
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
local_addr->sa_family == AF_CAN) {
memcpy(&conn->local_addr, local_addr,
sizeof(struct sockaddr_can));
} else {
NET_ERR("Local address family not set");
goto error;
@@ -1645,6 +1645,7 @@ static enum net_verdict net_context_raw_packet_received(
static int recv_raw(struct net_context *context,
net_context_recv_cb_t cb,
s32_t timeout,
struct sockaddr *local_addr,
void *user_data)
{
int ret;
@@ -1665,7 +1666,7 @@ static int recv_raw(struct net_context *context,

ret = net_conn_register(net_context_get_ip_proto(context),
net_context_get_family(context),
NULL, NULL, 0, 0,
NULL, local_addr, 0, 0,
net_context_raw_packet_received,
user_data,
&context->conn_handler);
@@ -1704,10 +1705,16 @@ int net_context_recv(struct net_context *context,
} else {
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
net_context_get_family(context) == AF_PACKET) {
ret = recv_raw(context, cb, timeout, user_data);
ret = recv_raw(context, cb, timeout, NULL, user_data);
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
net_context_get_family(context) == AF_CAN) {
ret = recv_raw(context, cb, timeout, user_data);
struct sockaddr_can local_addr = {
.can_family = AF_CAN,
};

ret = recv_raw(context, cb, timeout,
(struct sockaddr *)&local_addr,
user_data);
if (ret == -EALREADY) {
/* This is perfectly normal for CAN sockets.
* The SocketCAN will dispatch the packet to

0 comments on commit 0032f68

Please sign in to comment.
You can’t perform that action at this time.