3b0bd646696b5af7bf6cacf977d838ec283dd875
[apps/agl-service-can-low-level.git] / ll-can-binding.h
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /* max. number of CAN interfaces given on the cmdline */
19 #define MAXSOCK 16
20
21 /* buffer sizes for CAN frame string representations */
22 #define CL_ID (sizeof("12345678##1"))
23 #define CL_DATA sizeof(".AA")
24 #define CL_BINDATA sizeof(".10101010")
25
26  /* CAN FD ASCII hex short representation with DATA_SEPERATORs */
27 #define CL_CFSZ (2*CL_ID + 64*CL_DATA)
28
29 #define CANID_DELIM '#'
30
31 /*
32  * Interface between the daemon and the binding
33  */
34 static const struct afb_binding_interface *interface;
35
36 /*
37  * the type of position expected
38  *
39  * here, this type is the selection of protocol
40  */
41 enum type {
42         type_OBDII,
43         type_CAN,
44         type_DEFAULT = type_CAN,
45         type_INVALID = -1
46 };
47
48 #define type_size sizeof(enum type)-2
49
50 /*
51  * names of the types
52  */
53 static const char * const type_NAMES[type_size] = {
54         "OBDII",
55         "CAN"
56 };
57
58 /* CAN variable initialization */
59 struct canfd_frame canfd_frame;
60
61 struct can_handler {
62         int socket;
63         char *device;
64         bool is_fdmode_on;
65         struct sockaddr_can txAddress;
66 };
67
68 /*
69  * each generated event
70  */
71 typedef struct _event event;
72 struct _event {
73         event *next;                    /* link for the next event */
74         const char *name;               /* name of the event */
75         struct afb_event afb_event;     /* the event for the binder */
76         openxc_CanMessage can_message;  /* value for the can_message */
77 };
78
79 /*
80  * each can event, will browse by the id
81  */
82 typedef struct _can_event can_event;
83 struct _can_event {
84         can_event *next;        /* Link to the next other can message */
85         event *events;          /* events for the can message */
86         uint32_t id;            /* id of the event for unsubscribe */
87         enum type type;         /* the type of data expected */
88 };
89
90 can_event *can_events_list;
91
92 /* TODO : Add help comments :p */
93 static int connect_to_event_loop();
94
95 const char hex_asc_upper[] = "0123456789ABCDEF";
96
97 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0F)]
98 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xF0) >> 4]
99
100 static inline void put_hex_byte(char *buf, __u8 byte)
101 {
102         buf[0] = hex_asc_upper_hi(byte);
103         buf[1] = hex_asc_upper_lo(byte);
104 }
105
106 static inline void _put_id(char *buf, int end_offset, canid_t id)
107 {
108         /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
109         while (end_offset >= 0) {
110                 buf[end_offset--] = hex_asc_upper[id & 0xF];
111                 id >>= 4;
112         }
113 }
114
115 #define put_sff_id(buf, id) _put_id(buf, 2, id)
116 #define put_eff_id(buf, id) _put_id(buf, 7, id)
117
118 static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);
119
120
121 // Initialize default can_handler values
122 static struct can_handler can_handler = {
123         .socket = -1,
124         .device = "vcan0",
125         .is_fdmode_on = false,
126 };
127
128 static void send_event();