]> rtime.felk.cvut.cz Git - sojka/can-syscalls-examples.git/blob - common.h
8d7f3ab6a9c6ec076aec0efe11eabaebae11a792
[sojka/can-syscalls-examples.git] / common.h
1 /**************************************************************/
2 /* CAN system calls example                                   */
3 /* Author: Michal Sojka, Czech Technical University in Prague */
4 /* License: Public domain                                     */
5 /**************************************************************/
6
7 #ifndef COMMON_H
8 #define COMMON_H
9
10 #include <linux/can.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #define STRINGIFY(val) #val
15 #define TOSTRING(val) STRINGIFY(val)
16
17 /**
18  * Evaluate expression cmd and if its value is -1, print an error
19  * message with perror().
20  *
21  * @return Result of cmd evaluation
22  */
23 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
24 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
25
26 char *candump_parse_args(int argc, char *argv[])
27 {
28         if (argc < 2) {
29                 fprintf(stderr, "Usage: %s <CAN interface>\n", argv[0]);
30                 exit(1);
31         }
32         return argv[1];
33 }
34
35 void print_can_frame(const struct can_frame *cf)
36 {
37         int i;
38
39         printf("%#010x [%d]", cf->can_id, cf->can_dlc);
40         for (i = 0; i < cf->can_dlc; i++)
41                 printf(" %02x", cf->data[i]);
42         printf("\n");
43 }
44
45
46 #endif