48
48
#include "avtp/acf/Common.h"
49
49
#include "avtp/acf/Can.h"
50
50
#include "avtp/CommonHeader.h"
51
+ #include "acf-can-common.h"
51
52
52
53
#define MAX_PDU_SIZE 1500
54
+ #define ARGPARSE_CAN_FD_OPTION 500
53
55
54
56
static char ifname [IFNAMSIZ ];
55
57
static uint8_t macaddr [ETH_ALEN ];
56
58
static uint8_t use_udp ;
57
59
static uint32_t udp_port = 17220 ;
58
- static char can_ifname [IFNAMSIZ ] = "STDOUT\0" ;
60
+ static Avtp_CanVariant_t can_variant = AVTP_CAN_CLASSIC ;
61
+ static char can_ifname [IFNAMSIZ ];
59
62
60
63
static char doc [] = "\nacf-can-listener -- a program designed to receive CAN messages from \
61
64
a remote CAN bus over Ethernet using Open1722 \
@@ -72,6 +75,7 @@ static char args_doc[] = "[ifname] dst-mac-address [can ifname]";
72
75
static struct argp_option options [] = {
73
76
{"port" , 'p' , "UDP_PORT" , 0 , "UDP Port to listen on if UDP enabled" },
74
77
{"udp" , 'u' , 0 , 0 , "Use UDP" },
78
+ {"fd" , ARGPARSE_CAN_FD_OPTION , 0 , 0 , "Use CAN-FD" },
75
79
{"can ifname" , 0 , 0 , OPTION_DOC , "CAN interface (set to STDOUT by default)" },
76
80
{"dst-mac-address" , 0 , 0 , OPTION_DOC , "Stream destination MAC address (If Ethernet)" },
77
81
{"ifname" , 0 , 0 , OPTION_DOC , "Network interface (If Ethernet)" },
@@ -89,6 +93,8 @@ static error_t parser(int key, char *arg, struct argp_state *state)
89
93
case 'u' :
90
94
use_udp = 1 ;
91
95
break ;
96
+ case ARGPARSE_CAN_FD_OPTION :
97
+ can_variant = AVTP_CAN_FD ;
92
98
93
99
case ARGP_KEY_NO_ARGS :
94
100
break ;
@@ -100,7 +106,6 @@ static error_t parser(int key, char *arg, struct argp_state *state)
100
106
}
101
107
102
108
if (!use_udp ){
103
-
104
109
strncpy (ifname , arg , sizeof (ifname ) - 1 );
105
110
106
111
if (state -> next < state -> argc )
@@ -154,7 +159,7 @@ void print_can_acf(uint8_t* acf_pdu)
154
159
155
160
Avtp_Can_t * pdu = (Avtp_Can_t * ) acf_pdu ;
156
161
157
- Avtp_Can_GetField (pdu , AVTP_ACF_FIELD_ACF_MSG_LENGTH , & acf_msg_len );
162
+ Avtp_Can_GetField (pdu , AVTP_CAN_FIELD_ACF_MSG_LENGTH , & acf_msg_len );
158
163
Avtp_Can_GetField (pdu , AVTP_CAN_FIELD_CAN_BUS_ID , & can_bus_id );
159
164
Avtp_Can_GetField (pdu , AVTP_CAN_FIELD_MESSAGE_TIMESTAMP , & timestamp );
160
165
Avtp_Can_GetField (pdu , AVTP_CAN_FIELD_CAN_IDENTIFIER , & can_identifier );
@@ -172,17 +177,13 @@ static int new_packet(int sk_fd, int can_socket) {
172
177
173
178
int res ;
174
179
uint64_t msg_length , proc_bytes = 0 , msg_proc_bytes = 0 ;
175
- uint64_t can_frame_id , udp_seq_num = 0 , subtype ;
180
+ uint64_t can_frame_id , udp_seq_num , subtype , flag ;
176
181
uint16_t payload_length , pdu_length ;
177
- uint8_t * can_payload , i ;
178
- uint8_t pdu [MAX_PDU_SIZE ];
179
- uint8_t * cf_pdu ;
180
- uint8_t * acf_pdu ;
181
- Avtp_Udp_t * udp_pdu ;
182
- char stdout_string [1000 ] = "\0" ;
183
- struct can_frame frame ;
184
- uint64_t eff ;
182
+ uint8_t pdu [MAX_PDU_SIZE ], i ;
183
+ uint8_t * cf_pdu , * acf_pdu , * udp_pdu , * can_payload ;
184
+ struct canfd_frame frame ;
185
185
186
+ memset (& frame , 0 , sizeof (struct canfd_frame ));
186
187
res = recv (sk_fd , pdu , MAX_PDU_SIZE , 0 );
187
188
188
189
if (res < 0 || res > MAX_PDU_SIZE ) {
@@ -191,8 +192,8 @@ static int new_packet(int sk_fd, int can_socket) {
191
192
}
192
193
193
194
if (use_udp ) {
194
- udp_pdu = ( Avtp_Udp_t * ) pdu ;
195
- Avtp_Udp_GetField (udp_pdu , AVTP_UDP_FIELD_ENCAPSULATION_SEQ_NO , & udp_seq_num );
195
+ udp_pdu = pdu ;
196
+ Avtp_Udp_GetField (( Avtp_Udp_t * ) udp_pdu , AVTP_UDP_FIELD_ENCAPSULATION_SEQ_NO , & udp_seq_num );
196
197
cf_pdu = pdu + AVTP_UDP_HEADER_LEN ;
197
198
proc_bytes += AVTP_UDP_HEADER_LEN ;
198
199
} else {
@@ -214,15 +215,10 @@ static int new_packet(int sk_fd, int can_socket) {
214
215
215
216
if (subtype == AVTP_SUBTYPE_TSCF ){
216
217
proc_bytes += AVTP_TSCF_HEADER_LEN ;
217
- res = Avtp_Tscf_GetField ((Avtp_Tscf_t * )cf_pdu , AVTP_TSCF_FIELD_STREAM_DATA_LENGTH , (uint64_t * ) & msg_length );
218
+ Avtp_Tscf_GetField ((Avtp_Tscf_t * )cf_pdu , AVTP_TSCF_FIELD_STREAM_DATA_LENGTH , (uint64_t * ) & msg_length );
218
219
}else {
219
220
proc_bytes += AVTP_NTSCF_HEADER_LEN ;
220
- res = Avtp_Ntscf_GetField ((Avtp_Ntscf_t * )cf_pdu , AVTP_NTSCF_FIELD_NTSCF_DATA_LENGTH , (uint64_t * ) & msg_length );
221
- }
222
-
223
- if (res < 0 ) {
224
- fprintf (stderr , "Failed to get message length: %d\n" , res );
225
- return -1 ;
221
+ Avtp_Ntscf_GetField ((Avtp_Ntscf_t * )cf_pdu , AVTP_NTSCF_FIELD_NTSCF_DATA_LENGTH , (uint64_t * ) & msg_length );
226
222
}
227
223
228
224
while (msg_proc_bytes < msg_length ) {
@@ -234,56 +230,50 @@ static int new_packet(int sk_fd, int can_socket) {
234
230
return -1 ;
235
231
}
236
232
237
- res = Avtp_Can_GetField ((Avtp_Can_t * )acf_pdu , AVTP_CAN_FIELD_CAN_IDENTIFIER ,
238
- & can_frame_id );
239
- if (res < 0 ) {
240
- fprintf (stderr , "Error: Getting CAN frame ID\n" );
241
- return -1 ;
242
- }
233
+ Avtp_Can_GetField ((Avtp_Can_t * )acf_pdu , AVTP_CAN_FIELD_CAN_IDENTIFIER ,
234
+ & (can_frame_id ));
235
+ frame .can_id = can_frame_id ;
243
236
244
237
can_payload = Avtp_Can_GetPayload ((Avtp_Can_t * )acf_pdu , & payload_length , & pdu_length );
245
238
msg_proc_bytes += pdu_length * 4 ;
246
239
247
- res = Avtp_Can_GetField ((Avtp_Can_t * )acf_pdu , AVTP_CAN_FIELD_EFF , & eff );
248
- if (res < 0 ) {
249
- fprintf (stderr , "Failed to get eff field: %d\n" , res );
250
- return -1 ;
251
- }
252
-
253
- if (can_frame_id > 0x7FF && !eff ) {
240
+ // Handle EFF Flag
241
+ Avtp_Can_GetField ((Avtp_Can_t * )acf_pdu , AVTP_CAN_FIELD_EFF , & flag );
242
+ if (frame .can_id > 0x7FF && !flag ) {
254
243
fprintf (stderr , "Error: CAN ID is > 0x7FF but the EFF bit is not set.\n" );
255
244
return -1 ;
256
245
}
246
+ if (flag ) frame .can_id |= CAN_EFF_FLAG ;
257
247
258
- if (can_socket == 0 ) {
259
- for (i = 0 ; i < payload_length ; i ++ ) {
260
- sprintf (stdout_string + (2 * i ), "%02x" , can_payload [i ]);
261
- }
248
+ // Handle RTR Flag
249
+ Avtp_Can_GetField ((Avtp_Can_t * )acf_pdu , AVTP_CAN_FIELD_RTR , & flag );
250
+ if (flag ) frame .can_id |= CAN_RTR_FLAG ;
262
251
263
- if (eff ) {
264
- fprintf ( stdout , "(000000.000000) elmcan 0000%03lx#%s\n" , can_frame_id , stdout_string );
265
- } else {
266
- fprintf ( stdout , "(000000.000000) elmcan %03lx#%s\n" , can_frame_id , stdout_string );
267
- }
268
- fflush ( stdout ) ;
269
- } else {
270
- frame . can_id = ( canid_t ) can_frame_id ;
271
- if (eff ) {
272
- frame . can_id |= CAN_EFF_FLAG ;
273
- }
274
- frame .can_dlc = payload_length ;
275
- memcpy (frame .data , can_payload , payload_length );
276
- if ( write (can_socket , & frame , sizeof (struct can_frame )) != sizeof (struct can_frame )) {
277
- return 1 ;
278
- }
252
+ if (can_variant == AVTP_CAN_FD ) {
253
+ Avtp_Can_GetField (( Avtp_Can_t * ) acf_pdu , AVTP_CAN_FIELD_BRS , & flag );
254
+ if ( flag ) frame . flags |= CANFD_BRS ;
255
+
256
+ Avtp_Can_GetField (( Avtp_Can_t * ) acf_pdu , AVTP_CAN_FIELD_FDF , & flag );
257
+ if ( flag ) frame . flags |= CANFD_FDF ;
258
+
259
+ Avtp_Can_GetField (( Avtp_Can_t * ) acf_pdu , AVTP_CAN_FIELD_ESI , & flag ) ;
260
+ if (flag ) frame . flags |= CANFD_ESI ;
261
+ }
262
+
263
+ frame .len = payload_length ;
264
+ memcpy (frame .data , can_payload , payload_length );
265
+ res = write (can_socket , & frame , sizeof (struct canfd_frame )) != sizeof (struct canfd_frame );
266
+ if ( res < 0 ) {
267
+ return res ;
279
268
}
269
+
280
270
}
281
271
return 1 ;
282
272
}
283
273
284
274
int main (int argc , char * argv [])
285
275
{
286
- int sk_fd , res ;
276
+ int fd , res ;
287
277
struct pollfd fds ;
288
278
289
279
int can_socket = 0 ;
@@ -292,33 +282,22 @@ int main(int argc, char *argv[])
292
282
293
283
argp_parse (& argp , argc , argv , 0 , NULL , NULL );
294
284
295
- // Open a CAN socket for reading frames if required
296
- if (strcmp (can_ifname , "STDOUT\0" )) {
297
- can_socket = socket (PF_CAN , SOCK_RAW , CAN_RAW );
298
- if (can_socket < 0 )
299
- return 1 ;
300
-
301
- strcpy (ifr .ifr_name , can_ifname );
302
- ioctl (can_socket , SIOCGIFINDEX , & ifr );
303
-
304
- memset (& can_addr , 0 , sizeof (can_addr ));
305
- can_addr .can_family = AF_CAN ;
306
- can_addr .can_ifindex = ifr .ifr_ifindex ;
307
- if (bind (can_socket , (struct sockaddr * )& can_addr , sizeof (can_addr )) < 0 )
308
- return 1 ;
309
- }
310
-
285
+ // Configure an appropriate socket: UDP or Ethernet Raw
311
286
if (use_udp ) {
312
- sk_fd = create_listener_socket_udp (udp_port );
287
+ fd = create_listener_socket_udp (udp_port );
313
288
} else {
314
- sk_fd = create_listener_socket (ifname , macaddr , ETH_P_TSN );
289
+ fd = create_listener_socket (ifname , macaddr , ETH_P_TSN );
315
290
}
316
- fds .fd = sk_fd ;
291
+ fds .fd = fd ;
317
292
fds .events = POLLIN ;
318
293
319
- if (sk_fd < 0 )
294
+ if (fd < 0 )
320
295
return 1 ;
321
296
297
+ // Open a CAN socket for reading frames
298
+ can_socket = setup_can_socket (can_ifname , can_variant );
299
+ if (!can_socket ) goto err ;
300
+
322
301
while (1 ) {
323
302
324
303
res = poll (& fds , 1 , -1 );
@@ -328,7 +307,7 @@ int main(int argc, char *argv[])
328
307
}
329
308
330
309
if (fds .revents & POLLIN ) {
331
- res = new_packet (sk_fd , can_socket );
310
+ res = new_packet (fd , can_socket );
332
311
if (res < 0 )
333
312
goto err ;
334
313
}
@@ -338,7 +317,7 @@ int main(int argc, char *argv[])
338
317
return 0 ;
339
318
340
319
err :
341
- close (sk_fd );
320
+ close (fd );
342
321
return 1 ;
343
322
344
323
}
0 commit comments