Reworked open_can_dev function and can_handler struct
[apps/low-level-can-service.git] / ll-can-binding.h
1 /* max. number of CAN interfaces given on the cmdline */
2 #define MAXSOCK 16
3
4 /* buffer sizes for CAN frame string representations */
5 #define CL_ID (sizeof("12345678##1"))
6 #define CL_DATA sizeof(".AA")
7 #define CL_BINDATA sizeof(".10101010")
8
9  /* CAN FD ASCII hex short representation with DATA_SEPERATORs */
10 #define CL_CFSZ (2*CL_ID + 64*CL_DATA)
11
12 #define CANID_DELIM '#'
13
14 /*
15  * Interface between the daemon and the binding
16  */
17 static const struct afb_binding_interface *interface;
18
19 /*
20  * the type of position expected
21  *
22  * here, this type is the selection of protocol
23  */
24 enum type {
25         type_OBDII,
26         type_CAN,
27         type_DEFAULT = type_CAN,
28         type_INVALID = -1
29 };
30
31 #define type_size sizeof(enum type)-2
32
33 /*
34  * names of the types
35  */
36 static const char * const type_NAMES[type_size] = {
37         "OBDII",
38         "CAN"
39 };
40
41 /* CAN variable initialization */
42 struct canfd_frame canfd_frame;
43
44 struct can_handler {
45         int socket;
46         char *device;
47         bool is_fdmode_on;
48         struct sockaddr_can txAddress;
49 };
50
51 /*
52  * each generated event
53  */
54 typedef struct _event event;
55 struct _event {
56         event *next;                    /* link for the next event */
57         const char *name;               /* name of the event */
58         struct afb_event afb_event;     /* the event for the binder */
59         openxc_CanMessage can_message;  /* value for the can_message */
60 };
61
62 /*
63  * each can event, will browse by the id
64  */
65 typedef struct _can_event can_event;
66 struct _can_event {
67         can_event *next;        /* Link to the next other can message */
68         event *events;          /* events for the can message */
69         uint32_t id;            /* id of the event for unsubscribe */
70         enum type type;         /* the type of data expected */
71 };
72
73 can_event *can_events_list;
74
75 /* TODO : Add help comments :p */
76 static int connect_to_event_loop();
77
78 const char hex_asc_upper[] = "0123456789ABCDEF";
79
80 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0F)]
81 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xF0) >> 4]
82
83 static inline void put_hex_byte(char *buf, __u8 byte)
84 {
85         buf[0] = hex_asc_upper_hi(byte);
86         buf[1] = hex_asc_upper_lo(byte);
87 }
88
89 static inline void _put_id(char *buf, int end_offset, canid_t id)
90 {
91         /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
92         while (end_offset >= 0) {
93                 buf[end_offset--] = hex_asc_upper[id & 0xF];
94                 id >>= 4;
95         }
96 }
97
98 #define put_sff_id(buf, id) _put_id(buf, 2, id)
99 #define put_eff_id(buf, id) _put_id(buf, 7, id)
100
101 static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);
102
103
104 // Initialize default can_handler values
105 static struct can_handler can_handler = {
106         .socket = -1,
107         .device = "vcan0",
108         .is_fdmode_on = false,
109 };
110
111 static void send_event();