@@ -12,8 +12,8 @@ LOG_MODULE_REGISTER(net_socket_can_sample, LOG_LEVEL_DBG);
1212#include <net/socket.h>
1313#include <net/socket_can.h>
1414
15- #define PRIORITY 7
16- #define STACKSIZE 750
15+ #define PRIORITY k_thread_priority_get(k_current_get())
16+ #define STACKSIZE 1024
1717#define SLEEP_PERIOD K_SECONDS(1)
1818
1919static k_tid_t tx_tid ;
@@ -27,6 +27,18 @@ static K_THREAD_STACK_DEFINE(rx_stack, STACKSIZE);
2727static struct k_thread rx_data ;
2828#endif
2929
30+ #define CLOSE_PERIOD 15
31+
32+ static const struct zcan_filter zfilter = {
33+ .id_type = CAN_STANDARD_IDENTIFIER ,
34+ .rtr = CAN_DATAFRAME ,
35+ .std_id = 0x1 ,
36+ .rtr_mask = 1 ,
37+ .std_id_mask = CAN_STD_ID_MASK
38+ };
39+
40+ static struct can_filter filter ;
41+
3042static void tx (int * can_fd )
3143{
3244 int fd = POINTER_TO_INT (can_fd );
@@ -57,8 +69,38 @@ static void tx(int *can_fd)
5769 }
5870}
5971
60- static void rx ( int * can_fd )
72+ static int create_socket ( const struct can_filter * filter )
6173{
74+ struct sockaddr_can can_addr ;
75+ int fd , ret ;
76+
77+ fd = socket (AF_CAN , SOCK_RAW , CAN_RAW );
78+ if (fd < 0 ) {
79+ LOG_ERR ("Cannot create %s CAN socket (%d)" , "2nd" , fd );
80+ return fd ;
81+ }
82+
83+ can_addr .can_ifindex = net_if_get_by_iface (
84+ net_if_get_first_by_type (& NET_L2_GET_NAME (CANBUS )));
85+ can_addr .can_family = PF_CAN ;
86+
87+ ret = bind (fd , (struct sockaddr * )& can_addr , sizeof (can_addr ));
88+ if (ret < 0 ) {
89+ LOG_ERR ("Cannot bind %s CAN socket (%d)" , "2nd" , - errno );
90+ (void )close (fd );
91+ return ret ;
92+ }
93+
94+ (void )setsockopt (fd , SOL_CAN_RAW , CAN_RAW_FILTER , filter ,
95+ sizeof (* filter ));
96+
97+ return fd ;
98+ }
99+
100+ static void rx (int * can_fd , int * do_close_period ,
101+ const struct can_filter * filter )
102+ {
103+ int close_period = POINTER_TO_INT (do_close_period );
62104 int fd = POINTER_TO_INT (can_fd );
63105 struct sockaddr_can can_addr ;
64106 socklen_t addr_len ;
@@ -98,19 +140,29 @@ static void rx(int *can_fd)
98140 } else {
99141 LOG_INF ("[%d] EXT Remote message received" , fd );
100142 }
143+
144+ if (POINTER_TO_INT (do_close_period ) > 0 ) {
145+ close_period -- ;
146+ if (close_period <= 0 ) {
147+ (void )close (fd );
148+
149+ k_sleep (K_SECONDS (1 ));
150+
151+ fd = create_socket (filter );
152+ if (fd < 0 ) {
153+ LOG_ERR ("Cannot get socket (%d)" ,
154+ - errno );
155+ return ;
156+ }
157+
158+ close_period = POINTER_TO_INT (do_close_period );
159+ }
160+ }
101161 }
102162}
103163
104164static int setup_socket (void )
105165{
106- const struct zcan_filter zfilter = {
107- .id_type = CAN_STANDARD_IDENTIFIER ,
108- .rtr = CAN_DATAFRAME ,
109- .std_id = 0x1 ,
110- .rtr_mask = 1 ,
111- .std_id_mask = CAN_STD_ID_MASK
112- };
113- struct can_filter filter ;
114166 struct sockaddr_can can_addr ;
115167 struct net_if * iface ;
116168 int fd , rx_fd ;
@@ -163,38 +215,29 @@ static int setup_socket(void)
163215
164216 LOG_DBG ("Started socket CAN TX thread" );
165217
218+ LOG_INF ("1st RX fd %d" , fd );
219+
166220 rx_fd = fd ;
167221
168222#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 ));
223+ fd = create_socket (& filter );
224+ if (fd >= 0 ) {
225+ rx_tid = k_thread_create (& rx_data , rx_stack ,
226+ K_THREAD_STACK_SIZEOF (rx_stack ),
227+ (k_thread_entry_t )rx ,
228+ INT_TO_POINTER (fd ),
229+ INT_TO_POINTER (CLOSE_PERIOD ),
230+ & filter , PRIORITY , 0 , K_NO_WAIT );
231+ if (!rx_tid ) {
232+ ret = - ENOENT ;
233+ errno = - ret ;
234+ LOG_ERR ("Cannot create 2nd RX thread!" );
235+ goto cleanup2 ;
236+ }
188237
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 ;
238+ LOG_INF ("2nd RX fd %d" , fd );
239+ } else {
240+ LOG_ERR ("2nd RX not created (%d)" , fd );
198241 }
199242#endif
200243
@@ -214,11 +257,14 @@ void main(void)
214257{
215258 int fd ;
216259
260+ /* Let the device start before doing anything */
261+ k_sleep (K_SECONDS (2 ));
262+
217263 fd = setup_socket ();
218264 if (fd < 0 ) {
219265 LOG_ERR ("Cannot start CAN application (%d)" , fd );
220266 return ;
221267 }
222268
223- rx (INT_TO_POINTER (fd ));
269+ rx (INT_TO_POINTER (fd ), NULL , NULL );
224270}
0 commit comments