Skip to content

Commit ae89c22

Browse files
committed
samples: net: can: Add second RX socket receiver
This is done so that we can test socket CAN dispatcher. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 6621491 commit ae89c22

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
common:
22
tags: net socket can CAN
33
depends_on: can
4+
harness: can
5+
platform_whitelist: stm32f072b_disco nucleo_l432kc
46
sample:
57
description: Test BSD sockets CAN support
68
name: Socket CAN example
79
tests:
8-
sample.net.sockets.can:
9-
platform_whitelist: stm32f072b_disco nucleo_l432kc
10+
sample.net.sockets.can.test_with_one_socket:
11+
extra_configs:
12+
- CONFIG_NET_SOCKETS_CAN_RECEIVERS=1
13+
sample.net.sockets.can.test_with_two_sockets:
14+
extra_configs:
15+
- CONFIG_NET_SOCKETS_CAN_RECEIVERS=2

samples/net/sockets/can/src/main.c

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ static k_tid_t tx_tid;
2020
static K_THREAD_STACK_DEFINE(tx_stack, STACKSIZE);
2121
static struct k_thread tx_data;
2222

23+
/* For testing purposes, we create another RX receiver if configured so */
24+
#if CONFIG_NET_SOCKETS_CAN_RECEIVERS == 2
25+
static k_tid_t rx_tid;
26+
static K_THREAD_STACK_DEFINE(rx_stack, STACKSIZE);
27+
static struct k_thread rx_data;
28+
#endif
29+
2330
static void tx(int *can_fd)
2431
{
2532
int fd = POINTER_TO_INT(can_fd);
@@ -50,15 +57,16 @@ static void tx(int *can_fd)
5057
}
5158
}
5259

53-
static void rx(int fd)
60+
static void rx(int *can_fd)
5461
{
62+
int fd = POINTER_TO_INT(can_fd);
5563
struct sockaddr_can can_addr;
5664
socklen_t addr_len;
5765
struct zcan_frame msg;
5866
struct can_frame frame;
5967
int ret;
6068

61-
LOG_DBG("Waiting CAN data...");
69+
LOG_DBG("[%d] Waiting CAN data...", fd);
6270

6371
while (1) {
6472
u8_t *data;
@@ -69,14 +77,15 @@ static void rx(int fd)
6977
ret = recvfrom(fd, &frame, sizeof(struct can_frame),
7078
0, (struct sockaddr *)&can_addr, &addr_len);
7179
if (ret < 0) {
72-
LOG_ERR("Cannot receive CAN message (%d)", ret);
80+
LOG_ERR("[%d] Cannot receive CAN message (%d)", fd,
81+
-errno);
7382
continue;
7483
}
7584

7685
can_copy_frame_to_zframe(&frame, &msg);
7786

78-
LOG_INF("CAN msg: type 0x%x RTR 0x%x EID 0x%x DLC 0x%x",
79-
msg.id_type, msg.rtr, msg.std_id, msg.dlc);
87+
LOG_INF("[%d] CAN msg: type 0x%x RTR 0x%x EID 0x%x DLC 0x%x",
88+
fd, msg.id_type, msg.rtr, msg.std_id, msg.dlc);
8089

8190
if (!msg.rtr) {
8291
if (msg.dlc > 8) {
@@ -87,7 +96,7 @@ static void rx(int fd)
8796

8897
LOG_HEXDUMP_INF(data, msg.dlc, "Data");
8998
} else {
90-
LOG_INF("EXT Remote message received");
99+
LOG_INF("[%d] EXT Remote message received", fd);
91100
}
92101
}
93102
}
@@ -104,7 +113,7 @@ static int setup_socket(void)
104113
struct can_filter filter;
105114
struct sockaddr_can can_addr;
106115
struct net_if *iface;
107-
int fd;
116+
int fd, rx_fd;
108117
int ret;
109118

110119
can_copy_zfilter_to_filter(&zfilter, &filter);
@@ -118,7 +127,7 @@ static int setup_socket(void)
118127
fd = socket(AF_CAN, SOCK_RAW, CAN_RAW);
119128
if (fd < 0) {
120129
ret = -errno;
121-
LOG_ERR("Cannot create CAN socket (%d)", ret);
130+
LOG_ERR("Cannot create %s CAN socket (%d)", "1st", ret);
122131
return ret;
123132
}
124133

@@ -128,7 +137,7 @@ static int setup_socket(void)
128137
ret = bind(fd, (struct sockaddr *)&can_addr, sizeof(can_addr));
129138
if (ret < 0) {
130139
ret = -errno;
131-
LOG_ERR("Cannot bind CAN socket (%d)", ret);
140+
LOG_ERR("Cannot bind %s CAN socket (%d)", "1st", ret);
132141
goto cleanup;
133142
}
134143

@@ -154,7 +163,47 @@ static int setup_socket(void)
154163

155164
LOG_DBG("Started socket CAN TX thread");
156165

157-
return fd;
166+
rx_fd = fd;
167+
168+
#if CONFIG_NET_SOCKETS_CAN_RECEIVERS == 2
169+
fd = socket(AF_CAN, SOCK_RAW, CAN_RAW);
170+
if (fd < 0) {
171+
ret = -errno;
172+
LOG_ERR("Cannot create %s CAN socket (%d)", "2nd", ret);
173+
fd = rx_fd;
174+
goto cleanup;
175+
}
176+
177+
can_addr.can_ifindex = net_if_get_by_iface(iface);
178+
can_addr.can_family = PF_CAN;
179+
180+
ret = bind(fd, (struct sockaddr *)&can_addr, sizeof(can_addr));
181+
if (ret < 0) {
182+
ret = -errno;
183+
LOG_ERR("Cannot bind %s CAN socket (%d)", "2nd", ret);
184+
goto cleanup2;
185+
}
186+
187+
setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &filter, sizeof(filter));
188+
189+
rx_tid = k_thread_create(&rx_data, rx_stack,
190+
K_THREAD_STACK_SIZEOF(rx_stack),
191+
(k_thread_entry_t)rx, INT_TO_POINTER(fd),
192+
NULL, NULL, PRIORITY, 0, K_NO_WAIT);
193+
if (!rx_tid) {
194+
ret = -ENOENT;
195+
errno = -ret;
196+
LOG_ERR("Cannot create 2nd RX thread!");
197+
goto cleanup2;
198+
}
199+
#endif
200+
201+
return rx_fd;
202+
203+
#if CONFIG_NET_SOCKETS_CAN_RECEIVERS == 2
204+
cleanup2:
205+
(void)close(rx_fd);
206+
#endif
158207

159208
cleanup:
160209
(void)close(fd);
@@ -171,5 +220,5 @@ void main(void)
171220
return;
172221
}
173222

174-
rx(fd);
223+
rx(INT_TO_POINTER(fd));
175224
}

0 commit comments

Comments
 (0)